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";
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;
170 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()) {}
171 int init(afb_api_t api);
172 void setEventHook(const char *event, const event_hook_func f);
173 void onEvent(afb_api_t api, const char *event, struct json_object *object);
175 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
178 static struct hs_instance *g_hs_instance;
184 * - api : the api serving the request
191 int hs_instance::init(afb_api_t api)
193 if(client_manager == nullptr) {
194 AFB_ERROR("client_manager is nullptr.");
197 client_manager->init();
199 if(app_info == nullptr) {
200 AFB_ERROR("app_info is nullptr.");
206 if(hs_config.readConfig() < 0) {
207 AFB_ERROR("read config file failed.");
211 if(app_recover == nullptr) {
212 AFB_ERROR("app_recover is nullptr.");
215 app_recover->init(api);
216 app_recover->setRecoverMap(hs_config.getRecoverMap());
218 const struct handshake_info *h = hs_config.getHandshakeInfo();
219 struct hs_handshake handshake(h->times, h->sleep);
220 if(handshake.start(api) < 0) {
221 AFB_ERROR("handshake with windowmanager failed.");
232 * - event : event name
233 * - f : hook function
238 void hs_instance::setEventHook(const char *event, const event_hook_func f)
240 if(event == nullptr || f == nullptr) {
241 AFB_WARNING("argument is null.");
245 std::string ev(event);
246 auto it = event_hook_list.find(ev);
247 if(it != event_hook_list.end()) {
248 it->second.push_back(f);
251 std::list<event_hook_func> l;
253 event_hook_list[ev] = std::move(l);
261 * - api : the api serving the request
262 * - event : event name
263 * - object : event json object
268 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
270 std::string ev(event);
271 auto it = event_hook_list.find(ev);
272 if(it != event_hook_list.end()) {
273 for(auto &ref : it->second) {
274 if(ref(api, event, object))
284 * - event : event name
285 * - f : hook function pointer
290 void setEventHook(const char *event, const event_hook_func f)
292 if(g_hs_instance == nullptr) {
293 AFB_ERROR("g_hs_instance is null.");
297 g_hs_instance->setEventHook(event, f);
301 ********** Method of HomeScreen Service (API) **********
304 static void pingSample(afb_req_t request)
306 static int pingcount = 0;
307 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
308 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
313 * tap_shortcut notify for homescreen
314 * When Shortcut area is tapped, notify these applciations
318 * - application_id : application id
324 static void tap_shortcut (afb_req_t request)
326 AFB_DEBUG("called.");
328 struct json_object *param_obj, *area_obj;
329 const char* value = afb_req_value(request, _application_id);
331 && json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
332 && json_object_object_get_ex(param_obj, _area, &area_obj)) {
333 AFB_INFO("request appid = %s.", value);
334 const char* area = json_object_get_string(area_obj);
335 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
336 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
337 g_hs_instance->client_manager->setStartupAppidAndArea(make_pair(std::string(value), std::string(area)));
338 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
339 HS_AfmMainProxy afm_proxy;
340 afm_proxy.start(request->api, id);
345 ret = AFB_EVENT_BAD_REQUEST;
349 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
352 struct json_object *res = json_object_new_object();
353 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
355 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
360 * HomeScreen OnScreen message
364 * - display_message : message for display
370 static void on_screen_message (afb_req_t request)
372 AFB_DEBUG("called.");
373 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
375 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
378 struct json_object *res = json_object_new_object();
379 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
381 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
386 * HomeScreen OnScreen Reply
390 * - reply_message : message for reply
396 static void on_screen_reply (afb_req_t request)
398 AFB_DEBUG("called.");
399 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
401 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
404 struct json_object *res = json_object_new_object();
405 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
407 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
415 * - event : Event name. Event list is written in libhomescreen.cpp
421 static void subscribe(afb_req_t request)
423 AFB_DEBUG("called.");
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)
456 AFB_DEBUG("called.");
458 std::string req_appid = std::move(get_application_id(request));
459 if(!req_appid.empty()) {
460 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
463 ret = AFB_EVENT_BAD_REQUEST;
467 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
470 struct json_object *res = json_object_new_object();
471 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
473 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
481 * - request : the request
487 static void showWindow(afb_req_t request)
489 AFB_DEBUG("called.");
491 struct json_object *param_obj, *area_obj;
492 const char* value = afb_req_value(request, _application_id);
494 && json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
495 && json_object_object_get_ex(param_obj, _area, &area_obj)) {
496 const char* area = json_object_get_string(area_obj);
497 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
498 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
499 g_hs_instance->client_manager->setStartupAppidAndArea(make_pair(std::string(value), std::string(area)));
500 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
501 HS_AfmMainProxy afm_proxy;
502 afm_proxy.start(request->api, id);
507 ret = AFB_EVENT_BAD_REQUEST;
511 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
514 struct json_object *res = json_object_new_object();
515 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
517 afb_req_success(request, res, "afb_event_push event [showWindow]");
525 * - request : the request
531 static void hideWindow(afb_req_t request)
533 AFB_DEBUG("called.");
535 const char* value = afb_req_value(request, _application_id);
537 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
540 ret = AFB_EVENT_BAD_REQUEST;
544 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
547 struct json_object *res = json_object_new_object();
548 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
550 afb_req_success(request, res, "afb_event_push event [hideWindow]");
555 * replyShowWindow event
558 * - request : the request
564 static void replyShowWindow(afb_req_t request)
566 AFB_DEBUG("called.");
568 const char* value = afb_req_value(request, _application_id);
570 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
573 ret = AFB_EVENT_BAD_REQUEST;
577 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
580 struct json_object *res = json_object_new_object();
581 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
583 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
588 * showNotification event
590 * the contents to homescreen which display at top area.
593 * - request : the request
599 static void showNotification(afb_req_t request)
601 AFB_DEBUG("called.");
602 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
604 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
607 struct json_object *res = json_object_new_object();
608 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
610 afb_req_success(request, res, "afb_event_push event [showNotification]");
615 * showInformation event
617 * the contents to homescreen which display at bottom area.
620 * - request : the request
626 static void showInformation(afb_req_t request)
628 AFB_DEBUG("called.");
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 AFB_DEBUG("called.");
654 struct json_object* j_runnable = json_object_new_array();
655 g_hs_instance->app_info->getRunnables(&j_runnable);
657 /*create response json object*/
658 struct json_object *res = json_object_new_object();
659 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
660 json_object_object_add(res, _keyData, j_runnable);
661 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
665 * registerShortcut event
668 * - value : the json contents to MenuBar.
669 * {"application_id":"homescreen","parameter":{"shortcut_id":"dashboard@0.1","shortcut_name":"Dashboard","postion": 1}}
675 static void registerShortcut(afb_req_t request)
678 const char* value = afb_req_value(request, _application_id);
680 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
683 ret = AFB_EVENT_BAD_REQUEST;
687 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
690 struct json_object *res = json_object_new_object();
691 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
693 afb_req_success(request, res, "afb_event_push event [registerShortcut]");
698 * updateShortcut event
701 * - value : homescreen shortcut json contents.
702 * {"application_id":"launcher","parameter":{"shortcut":[{"shortcut_id":"hvac","shortcut_name":"HVAC"},...]}}
708 static void updateShortcut(afb_req_t request)
711 const char* value = afb_req_value(request, _application_id);
713 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
716 ret = AFB_EVENT_BAD_REQUEST;
720 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
723 struct json_object *res = json_object_new_object();
724 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
726 afb_req_success(request, res, "afb_event_push event [updateShortcut]");
731 * array of the verbs exported to afb-daemon
733 static const afb_verb_t verbs[]= {
734 /* VERB'S NAME FUNCTION TO CALL */
735 { .verb="ping", .callback=pingSample },
736 { .verb="tap_shortcut", .callback=tap_shortcut },
737 { .verb="showWindow", .callback=showWindow },
738 { .verb="hideWindow", .callback=hideWindow },
739 { .verb="replyShowWindow", .callback=replyShowWindow },
740 { .verb="on_screen_message", .callback=on_screen_message },
741 { .verb="on_screen_reply", .callback=on_screen_reply },
742 { .verb="subscribe", .callback=subscribe },
743 { .verb="unsubscribe", .callback=unsubscribe },
744 { .verb="showNotification", .callback=showNotification },
745 { .verb="showInformation", .callback=showInformation },
746 { .verb="registerShortcut", .callback=registerShortcut },
747 { .verb="getRunnables", .callback=getRunnables },
748 { .verb="updateShortcut", .callback=updateShortcut },
749 {NULL } /* marker for end of the array */
753 * homescreen binding preinit function
756 * - api : the api serving the request
762 static int preinit(afb_api_t api)
764 AFB_DEBUG("binding preinit (was register)");
769 * homescreen binding init function
772 * - api : the api serving the request
778 static int init(afb_api_t api)
780 AFB_DEBUG("binding init");
782 if(g_hs_instance != nullptr) {
783 AFB_WARNING( "g_hs_instance isn't null.");
784 delete g_hs_instance->client_manager;
785 delete g_hs_instance->app_info;
786 delete g_hs_instance->app_recover;
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( "Fatal 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", event);
814 g_hs_instance->onEvent(api, event, object);
817 const afb_binding_t afbBindingExport = {
819 .specification = NULL,