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>
25 #include "hs-helper.h"
26 #include "hs-clientmanager.h"
27 #include "hs-appinfo.h"
28 #include "hs-config.h"
29 #include "hs-apprecover.h"
33 const char _error[] = "error";
34 const char _application_id[] = "application_id";
35 const char _display_message[] = "display_message";
36 const char _reply_message[] = "reply_message";
37 const char _keyData[] = "data";
38 const char _keyId[] = "id";
41 hs_handshake(int times, int sleep) : m_times(times), m_sleep(sleep) {}
42 int start(afb_api_t api) const;
44 enum HandshakeStatus {
46 Handshake_Subscribing,
47 Handshake_Subscribe_Fail,
54 const std::string sub_event = "windowmanager/handshake";
59 int hs_handshake::hs_sts = hs_handshake::Handshake_Idle;
62 * handshake callback function
65 * - obj : reply json object
66 * - error : api_call error
67 * - info : api_call information
73 void handshake_subscribe_callback(struct json_object *obj, const char *error, const char *info)
75 AFB_NOTICE("subscribe handshake reply: obj=%s, error=%s, info=%s", json_object_to_json_string(obj), error, info);
76 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
79 if(error == nullptr) {
80 hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
83 hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
88 * handshake event function
92 * - event : received event name
93 * - object : received json object
96 * 0 : event can transfer to others
97 * 1 : event not transfer to others
99 int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
101 AFB_NOTICE("received handshake event from windowmanager.");
102 hs_handshake::hs_sts = hs_handshake::Handshake_Over;
107 * start handshake function
114 * 0 : handshake success
115 * -1 : handshake fail
118 int hs_handshake::start(afb_api_t api) const
120 AFB_NOTICE("start handshake with windowmanager.");
122 setEventHook(sub_event.c_str(), on_handshake_event);
125 // try to subscribe handshake event
126 if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
127 || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
128 hs_handshake::hs_sts = Handshake_Subscribing;
130 wm_proxy.subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
133 // wait handshake event
134 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
140 usleep(m_sleep*1000);
141 } while(count < m_times);
147 HS_ClientManager *client_manager; // the connection session manager
148 HS_AppInfo *app_info; // application info
149 HS_AppRecover *app_recover;
151 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()) {}
152 int init(afb_api_t api);
153 void setEventHook(const char *event, const event_hook_func f);
154 void onEvent(afb_api_t api, const char *event, struct json_object *object);
156 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
159 static struct hs_instance *g_hs_instance;
165 * - api : the api serving the request
172 int hs_instance::init(afb_api_t api)
174 if(client_manager == nullptr) {
175 AFB_ERROR("client_manager is nullptr.");
178 client_manager->init();
180 if(app_info == nullptr) {
181 AFB_ERROR("app_info is nullptr.");
187 if(hs_config.readConfig() < 0) {
188 AFB_ERROR("read config file failed.");
192 const struct handshake_info *h = hs_config.getHandshakeInfo();
193 struct hs_handshake handshake(h->times, h->sleep);
194 if(handshake.start(api) < 0) {
195 AFB_ERROR("handshake with windowmanager failed.");
199 if(app_recover == nullptr) {
200 AFB_ERROR("app_recover is nullptr.");
203 app_recover->init(api);
204 app_recover->startRecovery(api, hs_config.getRecoverMap());
213 * - event : event name
214 * - f : hook function
219 void hs_instance::setEventHook(const char *event, const event_hook_func f)
221 if(event == nullptr || f == nullptr) {
222 AFB_WARNING("argument is null.");
226 std::string ev(event);
227 auto it = event_hook_list.find(ev);
228 if(it != event_hook_list.end()) {
229 it->second.push_back(f);
232 std::list<event_hook_func> l;
234 event_hook_list[ev] = std::move(l);
242 * - api : the api serving the request
243 * - event : event name
244 * - object : event json object
249 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
251 std::string ev(event);
252 auto it = event_hook_list.find(ev);
253 if(it != event_hook_list.end()) {
254 for(auto &ref : it->second) {
255 if(ref(api, event, object))
265 * - event : event name
266 * - f : hook function pointer
271 void setEventHook(const char *event, const event_hook_func f)
273 if(g_hs_instance == nullptr) {
274 AFB_ERROR("g_hs_instance is null.");
278 g_hs_instance->setEventHook(event, f);
282 ********** Method of HomeScreen Service (API) **********
285 static void pingSample(afb_req_t request)
287 static int pingcount = 0;
288 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
289 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
294 * tap_shortcut notify for homescreen
295 * When Shortcut area is tapped, notify these applciations
299 * - application_id : application id
305 static void tap_shortcut (afb_req_t request)
307 AFB_DEBUG("called.");
309 const char* value = afb_req_value(request, _application_id);
311 AFB_INFO("request appid = %s.", value);
312 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
313 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
314 g_hs_instance->client_manager->setStartupAppid(std::string(value));
315 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
316 HS_AfmMainProxy afm_proxy;
317 afm_proxy.start(request->api, id);
322 ret = AFB_EVENT_BAD_REQUEST;
326 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
329 struct json_object *res = json_object_new_object();
330 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
332 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
337 * HomeScreen OnScreen message
341 * - display_message : message for display
347 static void on_screen_message (afb_req_t request)
349 AFB_DEBUG("called.");
350 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
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 [on_screen_message]");
363 * HomeScreen OnScreen Reply
367 * - reply_message : message for reply
373 static void on_screen_reply (afb_req_t request)
375 AFB_DEBUG("called.");
376 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
378 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
381 struct json_object *res = json_object_new_object();
382 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
384 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
392 * - event : Event name. Event list is written in libhomescreen.cpp
398 static void subscribe(afb_req_t request)
400 AFB_DEBUG("called.");
402 std::string req_appid = std::move(get_application_id(request));
403 if(!req_appid.empty()) {
404 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
407 ret = AFB_EVENT_BAD_REQUEST;
411 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
414 struct json_object *res = json_object_new_object();
415 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
417 afb_req_success_f(request, res, "homescreen binder subscribe.");
425 * - event : Event name. Event list is written in libhomescreen.cpp
431 static void unsubscribe(afb_req_t request)
433 AFB_DEBUG("called.");
435 std::string req_appid = std::move(get_application_id(request));
436 if(!req_appid.empty()) {
437 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
440 ret = AFB_EVENT_BAD_REQUEST;
444 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
447 struct json_object *res = json_object_new_object();
448 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
450 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
458 * - request : the request
464 static void showWindow(afb_req_t request)
466 AFB_DEBUG("called.");
468 const char* value = afb_req_value(request, _application_id);
470 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
471 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
472 g_hs_instance->client_manager->setStartupAppid(std::string(value));
473 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
474 HS_AfmMainProxy afm_proxy;
475 afm_proxy.start(request->api, id);
480 ret = AFB_EVENT_BAD_REQUEST;
484 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
487 struct json_object *res = json_object_new_object();
488 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
490 afb_req_success(request, res, "afb_event_push event [showWindow]");
498 * - request : the request
504 static void hideWindow(afb_req_t request)
506 AFB_DEBUG("called.");
508 const char* value = afb_req_value(request, _application_id);
510 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
513 ret = AFB_EVENT_BAD_REQUEST;
517 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
520 struct json_object *res = json_object_new_object();
521 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
523 afb_req_success(request, res, "afb_event_push event [hideWindow]");
528 * replyShowWindow event
531 * - request : the request
537 static void replyShowWindow(afb_req_t request)
539 AFB_DEBUG("called.");
541 const char* value = afb_req_value(request, _application_id);
543 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
546 ret = AFB_EVENT_BAD_REQUEST;
550 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
553 struct json_object *res = json_object_new_object();
554 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
556 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
561 * showNotification event
563 * the contents to homescreen which display at top area.
566 * - request : the request
572 static void showNotification(afb_req_t request)
574 AFB_DEBUG("called.");
575 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
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 [showNotification]");
588 * showInformation event
590 * the contents to homescreen which display at bottom area.
593 * - request : the request
599 static void showInformation(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 [showInformation]");
618 * - request : the request
624 static void getRunnables(afb_req_t request)
626 AFB_DEBUG("called.");
627 struct json_object* j_runnable = json_object_new_array();
628 g_hs_instance->app_info->getRunnables(&j_runnable);
630 /*create response json object*/
631 struct json_object *res = json_object_new_object();
632 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
633 json_object_object_add(res, _keyData, j_runnable);
634 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
638 * registerShortcut event
641 * - value : the json contents to MenuBar.
642 * {"application_id":"homescreen","parameter":{"shortcut_id":"dashboard@0.1","shortcut_name":"Dashboard","postion": 1}}
648 static void registerShortcut(afb_req_t request)
651 const char* value = afb_req_value(request, _application_id);
653 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
656 ret = AFB_EVENT_BAD_REQUEST;
660 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
663 struct json_object *res = json_object_new_object();
664 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
666 afb_req_success(request, res, "afb_event_push event [registerShortcut]");
671 * updateShortcut event
674 * - value : homescreen shortcut json contents.
675 * {"application_id":"launcher","parameter":{"shortcut":[{"shortcut_id":"hvac","shortcut_name":"HVAC"},...]}}
681 static void updateShortcut(afb_req_t request)
684 const char* value = afb_req_value(request, _application_id);
686 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
689 ret = AFB_EVENT_BAD_REQUEST;
693 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
696 struct json_object *res = json_object_new_object();
697 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
699 afb_req_success(request, res, "afb_event_push event [updateShortcut]");
704 * array of the verbs exported to afb-daemon
706 static const afb_verb_t verbs[]= {
707 /* VERB'S NAME FUNCTION TO CALL */
708 { .verb="ping", .callback=pingSample },
709 { .verb="tap_shortcut", .callback=tap_shortcut },
710 { .verb="showWindow", .callback=showWindow },
711 { .verb="hideWindow", .callback=hideWindow },
712 { .verb="replyShowWindow", .callback=replyShowWindow },
713 { .verb="on_screen_message", .callback=on_screen_message },
714 { .verb="on_screen_reply", .callback=on_screen_reply },
715 { .verb="subscribe", .callback=subscribe },
716 { .verb="unsubscribe", .callback=unsubscribe },
717 { .verb="showNotification", .callback=showNotification },
718 { .verb="showInformation", .callback=showInformation },
719 { .verb="registerShortcut", .callback=registerShortcut },
720 { .verb="getRunnables", .callback=getRunnables },
721 { .verb="updateShortcut", .callback=updateShortcut },
722 {NULL } /* marker for end of the array */
726 * homescreen binding preinit function
729 * - api : the api serving the request
735 static int preinit(afb_api_t api)
737 AFB_DEBUG("binding preinit (was register)");
742 * homescreen binding init function
745 * - api : the api serving the request
751 static int init(afb_api_t api)
753 AFB_DEBUG("binding init");
755 if(g_hs_instance != nullptr) {
756 AFB_WARNING( "g_hs_instance isn't null.");
757 delete g_hs_instance->client_manager;
758 delete g_hs_instance->app_info;
759 delete g_hs_instance->app_recover;
760 delete g_hs_instance;
761 g_hs_instance = nullptr;
763 g_hs_instance = new hs_instance();
764 if(g_hs_instance == nullptr) {
765 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
769 return g_hs_instance->init(api);
773 * homescreen binding event function
776 * - api : the api serving the request
777 * - event : event name
778 * - object : event json object
784 static void onevent(afb_api_t api, const char *event, struct json_object *object)
786 AFB_INFO("on_event %s", event);
787 g_hs_instance->onEvent(api, event, object);
790 const afb_binding_t afbBindingExport = {
792 .specification = NULL,