2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <unordered_map>
26 #include "hs-helper.h"
27 #include "hs-clientmanager.h"
28 #include "hs-appinfo.h"
29 #include "hs-config.h"
30 #include "hs-apprecover.h"
34 const char _error[] = "error";
35 const char _application_id[] = "application_id";
36 const char _display_message[] = "display_message";
37 const char _reply_message[] = "reply_message";
38 const char _keyData[] = "data";
39 const char _keyId[] = "id";
42 hs_handshake(int times, int sleep) : m_times(times), m_sleep(sleep) {}
43 int start(afb_api_t api);
44 void handshake_loop(afb_api_t api);
46 enum HandshakeStatus {
48 Handshake_Subscribing,
49 Handshake_Subscribe_Fail,
56 const std::string sub_event = "windowmanager/handshake";
61 int hs_handshake::hs_sts = hs_handshake::Handshake_Idle;
64 * handshake callback function
67 * - obj : reply json object
68 * - error : api_call error
69 * - info : api_call information
75 void handshake_subscribe_callback(struct json_object *obj, const char *error, const char *info)
77 AFB_NOTICE("subscribe handshake reply: obj=%s, error=%s, info=%s", json_object_to_json_string(obj), error, info);
78 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
81 if(error == nullptr) {
82 hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
85 hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
90 * handshake event function
94 * - event : received event name
95 * - object : received json object
98 * 0 : event can transfer to others
99 * 1 : event not transfer to others
101 int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
103 AFB_NOTICE("received handshake event from windowmanager.");
104 hs_handshake::hs_sts = hs_handshake::Handshake_Over;
109 * start handshake function
115 * 0 : handshake success
116 * other : handshake fail
119 int hs_handshake::start(afb_api_t api)
121 AFB_NOTICE("start handshake with windowmanager.");
122 setEventHook(sub_event.c_str(), on_handshake_event);
124 std::thread th(&hs_handshake::handshake_loop, this, api);
139 void hs_handshake::handshake_loop(afb_api_t api)
143 // try to subscribe handshake event
144 if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
145 || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
146 hs_handshake::hs_sts = Handshake_Subscribing;
148 wm_proxy.subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
151 // wait handshake event
152 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
157 usleep(m_sleep*1000);
158 } while(count < m_times);
159 AFB_NOTICE("handshake over, count=%d.", count);
160 HS_AppRecover::instance()->startRecovery(api);
164 HS_ClientManager *client_manager; // the connection session manager
165 HS_AppInfo *app_info; // application info
166 HS_AppRecover *app_recover;
168 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()) {}
169 int init(afb_api_t api);
170 void setEventHook(const char *event, const event_hook_func f);
171 void onEvent(afb_api_t api, const char *event, struct json_object *object);
173 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
176 static struct hs_instance *g_hs_instance;
182 * - api : the api serving the request
189 int hs_instance::init(afb_api_t api)
191 if(client_manager == nullptr) {
192 AFB_ERROR("client_manager is nullptr.");
195 client_manager->init();
197 if(app_info == nullptr) {
198 AFB_ERROR("app_info is nullptr.");
204 if(hs_config.readConfig() < 0) {
205 AFB_ERROR("read config file failed.");
209 if(app_recover == nullptr) {
210 AFB_ERROR("app_recover is nullptr.");
213 app_recover->init(api);
214 app_recover->setRecoverMap(hs_config.getRecoverMap());
216 const struct handshake_info *h = hs_config.getHandshakeInfo();
217 struct hs_handshake handshake(h->times, h->sleep);
218 if(handshake.start(api) < 0) {
219 AFB_ERROR("handshake with windowmanager failed.");
230 * - event : event name
231 * - f : hook function
236 void hs_instance::setEventHook(const char *event, const event_hook_func f)
238 if(event == nullptr || f == nullptr) {
239 AFB_WARNING("argument is null.");
243 std::string ev(event);
244 auto it = event_hook_list.find(ev);
245 if(it != event_hook_list.end()) {
246 it->second.push_back(f);
249 std::list<event_hook_func> l;
251 event_hook_list[ev] = std::move(l);
259 * - api : the api serving the request
260 * - event : event name
261 * - object : event json object
266 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
268 std::string ev(event);
269 auto it = event_hook_list.find(ev);
270 if(it != event_hook_list.end()) {
271 for(auto &ref : it->second) {
272 if(ref(api, event, object))
282 * - event : event name
283 * - f : hook function pointer
288 void setEventHook(const char *event, const event_hook_func f)
290 if(g_hs_instance == nullptr) {
291 AFB_ERROR("g_hs_instance is null.");
295 g_hs_instance->setEventHook(event, f);
299 ********** Method of HomeScreen Service (API) **********
302 static void pingSample(afb_req_t request)
304 static int pingcount = 0;
305 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
306 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
311 * tap_shortcut notify for homescreen
312 * When Shortcut area is tapped, notify these applciations
316 * - application_id : application id
322 static void tap_shortcut (afb_req_t request)
324 AFB_DEBUG("called.");
326 const char* value = afb_req_value(request, _application_id);
328 AFB_INFO("request appid = %s.", value);
329 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
330 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
331 g_hs_instance->client_manager->setStartupAppid(std::string(value));
332 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
333 HS_AfmMainProxy afm_proxy;
334 afm_proxy.start(request->api, id);
339 ret = AFB_EVENT_BAD_REQUEST;
343 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
346 struct json_object *res = json_object_new_object();
347 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
349 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
354 * HomeScreen OnScreen message
358 * - display_message : message for display
364 static void on_screen_message (afb_req_t request)
366 AFB_DEBUG("called.");
367 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
369 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
372 struct json_object *res = json_object_new_object();
373 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
375 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
380 * HomeScreen OnScreen Reply
384 * - reply_message : message for reply
390 static void on_screen_reply (afb_req_t request)
392 AFB_DEBUG("called.");
393 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
395 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
398 struct json_object *res = json_object_new_object();
399 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
401 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
409 * - event : Event name. Event list is written in libhomescreen.cpp
415 static void subscribe(afb_req_t request)
417 AFB_DEBUG("called.");
419 std::string req_appid = std::move(get_application_id(request));
420 if(!req_appid.empty()) {
421 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
424 ret = AFB_EVENT_BAD_REQUEST;
428 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
431 struct json_object *res = json_object_new_object();
432 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
434 afb_req_success_f(request, res, "homescreen binder subscribe.");
442 * - event : Event name. Event list is written in libhomescreen.cpp
448 static void unsubscribe(afb_req_t request)
450 AFB_DEBUG("called.");
452 std::string req_appid = std::move(get_application_id(request));
453 if(!req_appid.empty()) {
454 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
457 ret = AFB_EVENT_BAD_REQUEST;
461 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
464 struct json_object *res = json_object_new_object();
465 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
467 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
475 * - request : the request
481 static void showWindow(afb_req_t request)
483 AFB_DEBUG("called.");
485 const char* value = afb_req_value(request, _application_id);
487 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
488 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
489 g_hs_instance->client_manager->setStartupAppid(std::string(value));
490 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
491 HS_AfmMainProxy afm_proxy;
492 afm_proxy.start(request->api, id);
497 ret = AFB_EVENT_BAD_REQUEST;
501 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
504 struct json_object *res = json_object_new_object();
505 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
507 afb_req_success(request, res, "afb_event_push event [showWindow]");
515 * - request : the request
521 static void hideWindow(afb_req_t request)
523 AFB_DEBUG("called.");
525 const char* value = afb_req_value(request, _application_id);
527 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
530 ret = AFB_EVENT_BAD_REQUEST;
534 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
537 struct json_object *res = json_object_new_object();
538 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
540 afb_req_success(request, res, "afb_event_push event [hideWindow]");
545 * replyShowWindow event
548 * - request : the request
554 static void replyShowWindow(afb_req_t request)
556 AFB_DEBUG("called.");
558 const char* value = afb_req_value(request, _application_id);
560 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
563 ret = AFB_EVENT_BAD_REQUEST;
567 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
570 struct json_object *res = json_object_new_object();
571 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
573 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
578 * showNotification event
580 * the contents to homescreen which display at top area.
583 * - request : the request
589 static void showNotification(afb_req_t request)
591 AFB_DEBUG("called.");
592 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
594 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
597 struct json_object *res = json_object_new_object();
598 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
600 afb_req_success(request, res, "afb_event_push event [showNotification]");
605 * showInformation event
607 * the contents to homescreen which display at bottom area.
610 * - request : the request
616 static void showInformation(afb_req_t request)
618 AFB_DEBUG("called.");
619 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
621 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
624 struct json_object *res = json_object_new_object();
625 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
627 afb_req_success(request, res, "afb_event_push event [showInformation]");
635 * - request : the request
641 static void getRunnables(afb_req_t request)
643 AFB_DEBUG("called.");
644 struct json_object* j_runnable = json_object_new_array();
645 g_hs_instance->app_info->getRunnables(&j_runnable);
647 /*create response json object*/
648 struct json_object *res = json_object_new_object();
649 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
650 json_object_object_add(res, _keyData, j_runnable);
651 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
655 * registerShortcut event
658 * - value : the json contents to MenuBar.
659 * {"application_id":"homescreen","parameter":{"shortcut_id":"dashboard@0.1","shortcut_name":"Dashboard","postion": 1}}
665 static void registerShortcut(afb_req_t request)
668 const char* value = afb_req_value(request, _application_id);
670 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
673 ret = AFB_EVENT_BAD_REQUEST;
677 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
680 struct json_object *res = json_object_new_object();
681 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
683 afb_req_success(request, res, "afb_event_push event [registerShortcut]");
688 * updateShortcut event
691 * - value : homescreen shortcut json contents.
692 * {"application_id":"launcher","parameter":{"shortcut":[{"shortcut_id":"hvac","shortcut_name":"HVAC"},...]}}
698 static void updateShortcut(afb_req_t request)
701 const char* value = afb_req_value(request, _application_id);
703 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
706 ret = AFB_EVENT_BAD_REQUEST;
710 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
713 struct json_object *res = json_object_new_object();
714 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
716 afb_req_success(request, res, "afb_event_push event [updateShortcut]");
721 * array of the verbs exported to afb-daemon
723 static const afb_verb_t verbs[]= {
724 /* VERB'S NAME FUNCTION TO CALL */
725 { .verb="ping", .callback=pingSample },
726 { .verb="tap_shortcut", .callback=tap_shortcut },
727 { .verb="showWindow", .callback=showWindow },
728 { .verb="hideWindow", .callback=hideWindow },
729 { .verb="replyShowWindow", .callback=replyShowWindow },
730 { .verb="on_screen_message", .callback=on_screen_message },
731 { .verb="on_screen_reply", .callback=on_screen_reply },
732 { .verb="subscribe", .callback=subscribe },
733 { .verb="unsubscribe", .callback=unsubscribe },
734 { .verb="showNotification", .callback=showNotification },
735 { .verb="showInformation", .callback=showInformation },
736 { .verb="registerShortcut", .callback=registerShortcut },
737 { .verb="getRunnables", .callback=getRunnables },
738 { .verb="updateShortcut", .callback=updateShortcut },
739 {NULL } /* marker for end of the array */
743 * homescreen binding preinit function
746 * - api : the api serving the request
752 static int preinit(afb_api_t api)
754 AFB_DEBUG("binding preinit (was register)");
759 * homescreen binding init function
762 * - api : the api serving the request
768 static int init(afb_api_t api)
770 AFB_DEBUG("binding init");
772 if(g_hs_instance != nullptr) {
773 AFB_WARNING( "g_hs_instance isn't null.");
774 delete g_hs_instance->client_manager;
775 delete g_hs_instance->app_info;
776 delete g_hs_instance->app_recover;
777 delete g_hs_instance;
778 g_hs_instance = nullptr;
780 g_hs_instance = new hs_instance();
781 if(g_hs_instance == nullptr) {
782 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
786 return g_hs_instance->init(api);
790 * homescreen binding event function
793 * - api : the api serving the request
794 * - event : event name
795 * - object : event json object
801 static void onevent(afb_api_t api, const char *event, struct json_object *object)
803 AFB_INFO("on_event %s", event);
804 g_hs_instance->onEvent(api, event, object);
807 const afb_binding_t afbBindingExport = {
809 .specification = NULL,