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"
31 #include "hs-vuiadapter.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, int times, int sleeps);
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, m_times, m_sleep);
139 void hs_handshake::handshake_loop(afb_api_t api, int times, int sleeps)
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) {
158 } while(count < times);
159 AFB_NOTICE("handshake over, m_times=%d, m_sleep=%d, count=%d.", times, sleeps, 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; // application recover
167 HS_VuiAdapter * vui_adapter; // vui function adapter
169 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()), vui_adapter(HS_VuiAdapter::instance()) {}
170 int init(afb_api_t api);
171 void setEventHook(const char *event, const event_hook_func f);
172 void onEvent(afb_api_t api, const char *event, struct json_object *object);
174 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
177 static struct hs_instance *g_hs_instance;
183 * - api : the api serving the request
190 int hs_instance::init(afb_api_t api)
192 if(client_manager == nullptr) {
193 AFB_ERROR("client_manager is nullptr.");
196 client_manager->init();
198 if(app_info == nullptr) {
199 AFB_ERROR("app_info is nullptr.");
205 if(hs_config.readConfig() < 0) {
206 AFB_ERROR("read config file failed.");
210 if(app_recover == nullptr) {
211 AFB_ERROR("app_recover is nullptr.");
214 app_recover->init(api);
215 app_recover->setRecoverMap(hs_config.getRecoverMap());
217 const struct handshake_info *h = hs_config.getHandshakeInfo();
218 struct hs_handshake handshake(h->times, h->sleep);
219 if(handshake.start(api) < 0) {
220 AFB_ERROR("handshake with windowmanager failed.");
224 if(vui_adapter == nullptr) {
225 AFB_ERROR("vui_adapter is nullptr.");
227 vui_adapter->init(api);
236 * - event : event name
237 * - f : hook function
242 void hs_instance::setEventHook(const char *event, const event_hook_func f)
244 AFB_INFO("hook event %s", event);
245 if(event == nullptr || f == nullptr) {
246 AFB_WARNING("argument is null.");
250 std::string ev(event);
251 auto it = event_hook_list.find(ev);
252 if(it != event_hook_list.end()) {
253 it->second.push_back(f);
256 std::list<event_hook_func> l;
258 event_hook_list[ev] = std::move(l);
266 * - api : the api serving the request
267 * - event : event name
268 * - object : event json object
273 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
275 std::string ev(event);
276 auto it = event_hook_list.find(ev);
277 if(it != event_hook_list.end()) {
278 for(auto &ref : it->second) {
279 if(ref(api, event, object))
284 AFB_INFO("don't find hook event %s", event);
292 * - event : event name
293 * - f : hook function pointer
298 void setEventHook(const char *event, const event_hook_func f)
300 if(g_hs_instance == nullptr) {
301 AFB_ERROR("g_hs_instance is null.");
305 g_hs_instance->setEventHook(event, f);
309 ********** Method of HomeScreen Service (API) **********
312 static void pingSample(afb_req_t request)
314 static int pingcount = 0;
315 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
316 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
321 * tap_shortcut notify for homescreen
322 * When Shortcut area is tapped, notify these applciations
326 * - application_id : application id
332 static void tap_shortcut (afb_req_t request)
335 const char* value = afb_req_value(request, _application_id);
337 AFB_INFO("request appid = %s.", value);
338 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
339 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
340 g_hs_instance->client_manager->setStartupAppid(std::string(value));
341 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
342 HS_AfmMainProxy afm_proxy;
343 afm_proxy.start(request->api, id);
348 ret = AFB_EVENT_BAD_REQUEST;
352 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
355 struct json_object *res = json_object_new_object();
356 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
358 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
363 * HomeScreen OnScreen message
367 * - display_message : message for display
373 static void on_screen_message (afb_req_t request)
375 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
377 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
380 struct json_object *res = json_object_new_object();
381 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
383 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
388 * HomeScreen OnScreen Reply
392 * - reply_message : message for reply
398 static void on_screen_reply (afb_req_t request)
400 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
402 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
405 struct json_object *res = json_object_new_object();
406 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
408 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
416 * - event : Event name. Event list is written in libhomescreen.cpp
422 static void subscribe(afb_req_t request)
425 std::string req_appid = std::move(get_application_id(request));
426 if(!req_appid.empty()) {
427 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
430 ret = AFB_EVENT_BAD_REQUEST;
434 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
437 struct json_object *res = json_object_new_object();
438 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
440 afb_req_success_f(request, res, "homescreen binder subscribe.");
448 * - event : Event name. Event list is written in libhomescreen.cpp
454 static void unsubscribe(afb_req_t request)
457 std::string req_appid = std::move(get_application_id(request));
458 if(!req_appid.empty()) {
459 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
462 ret = AFB_EVENT_BAD_REQUEST;
466 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
469 struct json_object *res = json_object_new_object();
470 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
472 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
480 * - request : the request
486 static void showWindow(afb_req_t request)
489 const char* value = afb_req_value(request, _application_id);
491 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
492 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
493 g_hs_instance->client_manager->setStartupAppid(std::string(value));
494 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
495 HS_AfmMainProxy afm_proxy;
496 afm_proxy.start(request->api, id);
501 ret = AFB_EVENT_BAD_REQUEST;
505 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
508 struct json_object *res = json_object_new_object();
509 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
511 afb_req_success(request, res, "afb_event_push event [showWindow]");
519 * - request : the request
525 static void hideWindow(afb_req_t request)
528 const char* value = afb_req_value(request, _application_id);
530 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
533 ret = AFB_EVENT_BAD_REQUEST;
537 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
540 struct json_object *res = json_object_new_object();
541 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
543 afb_req_success(request, res, "afb_event_push event [hideWindow]");
548 * replyShowWindow event
551 * - request : the request
557 static void replyShowWindow(afb_req_t request)
560 const char* value = afb_req_value(request, _application_id);
562 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
565 ret = AFB_EVENT_BAD_REQUEST;
569 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
572 struct json_object *res = json_object_new_object();
573 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
575 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
580 * showNotification event
582 * the contents to homescreen which display at top area.
585 * - request : the request
591 static void showNotification(afb_req_t request)
593 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
595 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
598 struct json_object *res = json_object_new_object();
599 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
601 afb_req_success(request, res, "afb_event_push event [showNotification]");
606 * showInformation event
608 * the contents to homescreen which display at bottom area.
611 * - request : the request
617 static void showInformation(afb_req_t request)
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 struct json_object* j_runnable = json_object_new_array();
644 g_hs_instance->app_info->getRunnables(&j_runnable);
646 /*create response json object*/
647 struct json_object *res = json_object_new_object();
648 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
649 json_object_object_add(res, _keyData, j_runnable);
650 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
654 * registerShortcut event
657 * - value : the json contents to MenuBar.
658 * {"application_id":"homescreen","parameter":{"shortcut_id":"dashboard@0.1","shortcut_name":"Dashboard","postion": 1}}
664 static void registerShortcut(afb_req_t request)
667 const char* value = afb_req_value(request, _application_id);
669 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
672 ret = AFB_EVENT_BAD_REQUEST;
676 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
679 struct json_object *res = json_object_new_object();
680 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
682 afb_req_success(request, res, "afb_event_push event [registerShortcut]");
687 * updateShortcut event
690 * - value : homescreen shortcut json contents.
691 * {"application_id":"launcher","parameter":{"shortcut":[{"shortcut_id":"hvac","shortcut_name":"HVAC"},...]}}
697 static void updateShortcut(afb_req_t request)
700 const char* value = afb_req_value(request, _application_id);
702 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
705 ret = AFB_EVENT_BAD_REQUEST;
709 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
712 struct json_object *res = json_object_new_object();
713 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
715 afb_req_success(request, res, "afb_event_push event [updateShortcut]");
720 * array of the verbs exported to afb-daemon
722 static const afb_verb_t verbs[]= {
723 /* VERB'S NAME FUNCTION TO CALL */
724 { .verb="ping", .callback=pingSample },
725 { .verb="tap_shortcut", .callback=tap_shortcut },
726 { .verb="showWindow", .callback=showWindow },
727 { .verb="hideWindow", .callback=hideWindow },
728 { .verb="replyShowWindow", .callback=replyShowWindow },
729 { .verb="on_screen_message", .callback=on_screen_message },
730 { .verb="on_screen_reply", .callback=on_screen_reply },
731 { .verb="subscribe", .callback=subscribe },
732 { .verb="unsubscribe", .callback=unsubscribe },
733 { .verb="showNotification", .callback=showNotification },
734 { .verb="showInformation", .callback=showInformation },
735 { .verb="getRunnables", .callback=getRunnables },
736 { .verb="registerShortcut", .callback=registerShortcut },
737 { .verb="updateShortcut", .callback=updateShortcut },
738 {NULL } /* marker for end of the array */
742 * homescreen binding preinit function
745 * - api : the api serving the request
751 static int preinit(afb_api_t api)
753 AFB_DEBUG("binding preinit (was register)");
758 * homescreen binding init function
761 * - api : the api serving the request
767 static int init(afb_api_t api)
769 AFB_DEBUG("binding init");
771 if(g_hs_instance != nullptr) {
772 AFB_WARNING( "g_hs_instance isn't null.");
773 delete g_hs_instance->client_manager;
774 delete g_hs_instance->app_info;
775 delete g_hs_instance->app_recover;
776 delete g_hs_instance->vui_adapter;
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( "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, object %s", event, json_object_to_json_string(object));
804 g_hs_instance->onEvent(api, event, object);
807 const afb_binding_t afbBindingExport = {
809 .specification = NULL,