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";
40 const char _parameter[] = "parameter";
41 const char _area[] = "area";
44 hs_handshake(int times, int sleep) : m_times(times), m_sleep(sleep) {}
45 int start(afb_api_t api);
46 void handshake_loop(afb_api_t api, int times, int sleeps);
48 enum HandshakeStatus {
50 Handshake_Subscribing,
51 Handshake_Subscribe_Fail,
58 const std::string sub_event = "windowmanager/handshake";
63 int hs_handshake::hs_sts = hs_handshake::Handshake_Idle;
66 * handshake callback function
69 * - obj : reply json object
70 * - error : api_call error
71 * - info : api_call information
77 void handshake_subscribe_callback(struct json_object *obj, const char *error, const char *info)
79 AFB_NOTICE("subscribe handshake reply: obj=%s, error=%s, info=%s", json_object_to_json_string(obj), error, info);
80 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
83 if(error == nullptr) {
84 hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
87 hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
92 * handshake event function
96 * - event : received event name
97 * - object : received json object
100 * 0 : event can transfer to others
101 * 1 : event not transfer to others
103 int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
105 AFB_NOTICE("received handshake event from windowmanager.");
106 hs_handshake::hs_sts = hs_handshake::Handshake_Over;
111 * start handshake function
117 * 0 : handshake success
118 * other : handshake fail
121 int hs_handshake::start(afb_api_t api)
123 AFB_NOTICE("start handshake with windowmanager.");
124 setEventHook(sub_event.c_str(), on_handshake_event);
126 std::thread th(&hs_handshake::handshake_loop, this, api, m_times, m_sleep);
141 void hs_handshake::handshake_loop(afb_api_t api, int times, int sleeps)
145 // try to subscribe handshake event
146 if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
147 || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
148 hs_handshake::hs_sts = Handshake_Subscribing;
150 wm_proxy.subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
153 // wait handshake event
154 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
160 } while(count < times);
161 AFB_NOTICE("handshake over, m_times=%d, m_sleep=%d, count=%d.", times, sleeps, count);
162 HS_AppRecover::instance()->startRecovery(api);
166 HS_ClientManager *client_manager; // the connection session manager
167 HS_AppInfo *app_info; // application info
168 HS_AppRecover *app_recover; // application recover
169 HS_VuiAdapter * vui_adapter; // vui function adapter
171 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()), vui_adapter(HS_VuiAdapter::instance()) {}
172 int init(afb_api_t api);
173 void setEventHook(const char *event, const event_hook_func f);
174 void onEvent(afb_api_t api, const char *event, struct json_object *object);
176 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
179 static struct hs_instance *g_hs_instance;
185 * - api : the api serving the request
192 int hs_instance::init(afb_api_t api)
194 if(client_manager == nullptr) {
195 AFB_ERROR("client_manager is nullptr.");
198 client_manager->init();
200 if(app_info == nullptr) {
201 AFB_ERROR("app_info is nullptr.");
207 if(hs_config.readConfig() < 0) {
208 AFB_ERROR("read config file failed.");
212 if(app_recover == nullptr) {
213 AFB_ERROR("app_recover is nullptr.");
216 app_recover->init(api);
217 app_recover->setRecoverMap(hs_config.getRecoverMap());
219 const struct handshake_info *h = hs_config.getHandshakeInfo();
220 struct hs_handshake handshake(h->times, h->sleep);
221 if(handshake.start(api) < 0) {
222 AFB_ERROR("handshake with windowmanager failed.");
226 if(vui_adapter == nullptr) {
227 AFB_ERROR("vui_adapter is nullptr.");
229 vui_adapter->init(api);
238 * - event : event name
239 * - f : hook function
244 void hs_instance::setEventHook(const char *event, const event_hook_func f)
246 AFB_INFO("hook event %s", event);
247 if(event == nullptr || f == nullptr) {
248 AFB_WARNING("argument is null.");
252 std::string ev(event);
253 auto it = event_hook_list.find(ev);
254 if(it != event_hook_list.end()) {
255 it->second.push_back(f);
258 std::list<event_hook_func> l;
260 event_hook_list[ev] = std::move(l);
268 * - api : the api serving the request
269 * - event : event name
270 * - object : event json object
275 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
277 std::string ev(event);
278 auto it = event_hook_list.find(ev);
279 if(it != event_hook_list.end()) {
280 for(auto &ref : it->second) {
281 if(ref(api, event, object))
286 AFB_INFO("don't find hook event %s", event);
294 * - event : event name
295 * - f : hook function pointer
300 void setEventHook(const char *event, const event_hook_func f)
302 if(g_hs_instance == nullptr) {
303 AFB_ERROR("g_hs_instance is null.");
307 g_hs_instance->setEventHook(event, f);
311 ********** Method of HomeScreen Service (API) **********
314 static void pingSample(afb_req_t request)
316 static int pingcount = 0;
317 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
318 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
323 * tap_shortcut notify for homescreen
324 * When Shortcut area is tapped, notify these applciations
328 * - application_id : application id
334 static void tap_shortcut (afb_req_t request)
337 struct json_object *param_obj, *area_obj;
338 const char* value = afb_req_value(request, _application_id);
340 && json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
341 && json_object_object_get_ex(param_obj, _area, &area_obj)) {
342 AFB_INFO("request appid = %s.", value);
343 const char* area = json_object_get_string(area_obj);
344 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
345 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
346 g_hs_instance->client_manager->setStartupAppidAndArea(make_pair(std::string(value), std::string(area)));
347 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
348 HS_AfmMainProxy afm_proxy;
349 afm_proxy.start(request->api, id);
354 ret = AFB_EVENT_BAD_REQUEST;
358 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
361 struct json_object *res = json_object_new_object();
362 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
364 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
369 * HomeScreen OnScreen message
373 * - display_message : message for display
379 static void on_screen_message (afb_req_t request)
381 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
383 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
386 struct json_object *res = json_object_new_object();
387 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
389 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
394 * HomeScreen OnScreen Reply
398 * - reply_message : message for reply
404 static void on_screen_reply (afb_req_t request)
406 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
408 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
411 struct json_object *res = json_object_new_object();
412 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
414 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
422 * - event : Event name. Event list is written in libhomescreen.cpp
428 static void subscribe(afb_req_t request)
431 std::string req_appid = std::move(get_application_id(request));
432 if(!req_appid.empty()) {
433 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
436 ret = AFB_EVENT_BAD_REQUEST;
440 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
443 struct json_object *res = json_object_new_object();
444 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
446 afb_req_success_f(request, res, "homescreen binder subscribe.");
454 * - event : Event name. Event list is written in libhomescreen.cpp
460 static void unsubscribe(afb_req_t request)
463 std::string req_appid = std::move(get_application_id(request));
464 if(!req_appid.empty()) {
465 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
468 ret = AFB_EVENT_BAD_REQUEST;
472 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
475 struct json_object *res = json_object_new_object();
476 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
478 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
486 * - request : the request
492 static void showWindow(afb_req_t request)
495 struct json_object *param_obj, *area_obj;
496 const char* value = afb_req_value(request, _application_id);
498 && json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
499 && json_object_object_get_ex(param_obj, _area, &area_obj)) {
500 const char* area = json_object_get_string(area_obj);
501 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
502 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
503 g_hs_instance->client_manager->setStartupAppidAndArea(make_pair(std::string(value), std::string(area)));
504 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
505 HS_AfmMainProxy afm_proxy;
506 afm_proxy.start(request->api, id);
511 ret = AFB_EVENT_BAD_REQUEST;
515 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
518 struct json_object *res = json_object_new_object();
519 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
521 afb_req_success(request, res, "afb_event_push event [showWindow]");
529 * - request : the request
535 static void hideWindow(afb_req_t request)
538 const char* value = afb_req_value(request, _application_id);
540 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
543 ret = AFB_EVENT_BAD_REQUEST;
547 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
550 struct json_object *res = json_object_new_object();
551 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
553 afb_req_success(request, res, "afb_event_push event [hideWindow]");
558 * replyShowWindow event
561 * - request : the request
567 static void replyShowWindow(afb_req_t request)
570 const char* value = afb_req_value(request, _application_id);
572 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
575 ret = AFB_EVENT_BAD_REQUEST;
579 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
582 struct json_object *res = json_object_new_object();
583 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
585 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
590 * showNotification event
592 * the contents to homescreen which display at top area.
595 * - request : the request
601 static void showNotification(afb_req_t request)
603 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
605 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
608 struct json_object *res = json_object_new_object();
609 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
611 afb_req_success(request, res, "afb_event_push event [showNotification]");
616 * showInformation event
618 * the contents to homescreen which display at bottom area.
621 * - request : the request
627 static void showInformation(afb_req_t request)
629 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
631 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
634 struct json_object *res = json_object_new_object();
635 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
637 afb_req_success(request, res, "afb_event_push event [showInformation]");
645 * - request : the request
651 static void getRunnables(afb_req_t request)
653 struct json_object* j_runnable = json_object_new_array();
654 g_hs_instance->app_info->getRunnables(&j_runnable);
656 /*create response json object*/
657 struct json_object *res = json_object_new_object();
658 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
659 json_object_object_add(res, _keyData, j_runnable);
660 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
664 * registerShortcut event
667 * - value : the json contents to MenuBar.
668 * {"application_id":"homescreen","parameter":{"shortcut_id":"dashboard@0.1","shortcut_name":"Dashboard","postion": 1}}
674 static void registerShortcut(afb_req_t request)
677 const char* value = afb_req_value(request, _application_id);
679 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
682 ret = AFB_EVENT_BAD_REQUEST;
686 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
689 struct json_object *res = json_object_new_object();
690 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
692 afb_req_success(request, res, "afb_event_push event [registerShortcut]");
697 * updateShortcut event
700 * - value : homescreen shortcut json contents.
701 * {"application_id":"launcher","parameter":{"shortcut":[{"shortcut_id":"hvac","shortcut_name":"HVAC"},...]}}
707 static void updateShortcut(afb_req_t request)
710 const char* value = afb_req_value(request, _application_id);
712 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
715 ret = AFB_EVENT_BAD_REQUEST;
719 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
722 struct json_object *res = json_object_new_object();
723 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
725 afb_req_success(request, res, "afb_event_push event [updateShortcut]");
730 * array of the verbs exported to afb-daemon
732 static const afb_verb_t verbs[]= {
733 /* VERB'S NAME FUNCTION TO CALL */
734 { .verb="ping", .callback=pingSample },
735 { .verb="tap_shortcut", .callback=tap_shortcut },
736 { .verb="showWindow", .callback=showWindow },
737 { .verb="hideWindow", .callback=hideWindow },
738 { .verb="replyShowWindow", .callback=replyShowWindow },
739 { .verb="on_screen_message", .callback=on_screen_message },
740 { .verb="on_screen_reply", .callback=on_screen_reply },
741 { .verb="subscribe", .callback=subscribe },
742 { .verb="unsubscribe", .callback=unsubscribe },
743 { .verb="showNotification", .callback=showNotification },
744 { .verb="showInformation", .callback=showInformation },
745 { .verb="getRunnables", .callback=getRunnables },
746 { .verb="registerShortcut", .callback=registerShortcut },
747 { .verb="updateShortcut", .callback=updateShortcut },
748 {NULL } /* marker for end of the array */
752 * homescreen binding preinit function
755 * - api : the api serving the request
761 static int preinit(afb_api_t api)
763 AFB_DEBUG("binding preinit (was register)");
768 * homescreen binding init function
771 * - api : the api serving the request
777 static int init(afb_api_t api)
779 AFB_DEBUG("binding init");
781 if(g_hs_instance != nullptr) {
782 AFB_WARNING( "g_hs_instance isn't null.");
783 delete g_hs_instance->client_manager;
784 delete g_hs_instance->app_info;
785 delete g_hs_instance->app_recover;
786 delete g_hs_instance->vui_adapter;
787 delete g_hs_instance;
788 g_hs_instance = nullptr;
790 g_hs_instance = new hs_instance();
791 if(g_hs_instance == nullptr) {
792 AFB_ERROR( "new g_hs_instance failed.");
796 return g_hs_instance->init(api);
800 * homescreen binding event function
803 * - api : the api serving the request
804 * - event : event name
805 * - object : event json object
811 static void onevent(afb_api_t api, const char *event, struct json_object *object)
813 AFB_INFO("on_event %s, object %s", event, json_object_to_json_string(object));
814 g_hs_instance->onEvent(api, event, object);
817 const afb_binding_t afbBindingExport = {
819 .specification = NULL,