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(error == nullptr) {
77 hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
80 hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
85 * handshake event function
89 * - event : received event name
90 * - object : received json object
93 * 0 : event can transfer to others
94 * 1 : event not transfer to others
96 int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
98 AFB_NOTICE("received handshake event from windowmanager.");
99 hs_handshake::hs_sts = hs_handshake::Handshake_Over;
104 * start handshake function
111 * 0 : handshake success
112 * -1 : handshake fail
115 int hs_handshake::start(afb_api_t api) const
117 AFB_NOTICE("start handshake with windowmanager.");
119 setEventHook(sub_event.c_str(), on_handshake_event);
122 // try to subscribe handshake event
123 if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
124 || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
125 hs_handshake::hs_sts = Handshake_Subscribing;
127 wm_proxy.subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
130 // wait handshake event
131 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
137 usleep(m_sleep*1000);
138 } while(count < m_times);
144 HS_ClientManager *client_manager; // the connection session manager
145 HS_AppInfo *app_info; // application info
146 HS_AppRecover *app_recover;
148 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()) {}
149 int init(afb_api_t api);
150 void setEventHook(const char *event, const event_hook_func f);
151 void onEvent(afb_api_t api, const char *event, struct json_object *object);
153 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
156 static struct hs_instance *g_hs_instance;
162 * - api : the api serving the request
169 int hs_instance::init(afb_api_t api)
171 if(client_manager == nullptr) {
172 AFB_ERROR("client_manager is nullptr.");
175 client_manager->init();
177 if(app_info == nullptr) {
178 AFB_ERROR("app_info is nullptr.");
184 if(hs_config.readConfig() < 0) {
185 AFB_ERROR("read config file failed.");
189 const struct handshake_info *h = hs_config.getHandshakeInfo();
190 struct hs_handshake handshake(h->times, h->sleep);
191 if(handshake.start(api) < 0) {
192 AFB_ERROR("handshake with windowmanager failed.");
196 if(app_recover == nullptr) {
197 AFB_ERROR("app_recover is nullptr.");
200 app_recover->init(api);
201 app_recover->startRecovery(api, hs_config.getRecoverMap());
210 * - event : event name
211 * - f : hook function
216 void hs_instance::setEventHook(const char *event, const event_hook_func f)
218 if(event == nullptr || f == nullptr) {
219 AFB_WARNING("argument is null.");
223 std::string ev(event);
224 auto it = event_hook_list.find(ev);
225 if(it != event_hook_list.end()) {
226 it->second.push_back(f);
229 std::list<event_hook_func> l;
231 event_hook_list[ev] = std::move(l);
239 * - api : the api serving the request
240 * - event : event name
241 * - object : event json object
246 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
248 std::string ev(event);
249 auto it = event_hook_list.find(ev);
250 if(it != event_hook_list.end()) {
251 for(auto &ref : it->second) {
252 if(ref(api, event, object))
262 * - event : event name
263 * - f : hook function pointer
268 void setEventHook(const char *event, const event_hook_func f)
270 if(g_hs_instance == nullptr) {
271 AFB_ERROR("g_hs_instance is null.");
275 g_hs_instance->setEventHook(event, f);
279 ********** Method of HomeScreen Service (API) **********
282 static void pingSample(afb_req_t request)
284 static int pingcount = 0;
285 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
286 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
291 * tap_shortcut notify for homescreen
292 * When Shortcut area is tapped, notify these applciations
296 * - application_id : application id
302 static void tap_shortcut (afb_req_t request)
304 AFB_DEBUG("called.");
306 const char* value = afb_req_value(request, _application_id);
308 AFB_INFO("request appid = %s.", value);
309 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
310 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
311 g_hs_instance->client_manager->setStartupAppid(std::string(value));
312 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
313 HS_AfmMainProxy afm_proxy;
314 afm_proxy.start(request->api, id);
319 ret = AFB_EVENT_BAD_REQUEST;
323 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
326 struct json_object *res = json_object_new_object();
327 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
329 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
334 * HomeScreen OnScreen message
338 * - display_message : message for display
344 static void on_screen_message (afb_req_t request)
346 AFB_DEBUG("called.");
347 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
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 [on_screen_message]");
360 * HomeScreen OnScreen Reply
364 * - reply_message : message for reply
370 static void on_screen_reply (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_reply]");
389 * - event : Event name. Event list is written in libhomescreen.cpp
395 static void subscribe(afb_req_t request)
397 AFB_DEBUG("called.");
399 std::string req_appid = std::move(get_application_id(request));
400 if(!req_appid.empty()) {
401 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
404 ret = AFB_EVENT_BAD_REQUEST;
408 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __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_f(request, res, "homescreen binder subscribe.");
422 * - event : Event name. Event list is written in libhomescreen.cpp
428 static void unsubscribe(afb_req_t request)
430 AFB_DEBUG("called.");
432 std::string req_appid = std::move(get_application_id(request));
433 if(!req_appid.empty()) {
434 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
437 ret = AFB_EVENT_BAD_REQUEST;
441 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
444 struct json_object *res = json_object_new_object();
445 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
447 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
455 * - request : the request
461 static void showWindow(afb_req_t request)
463 AFB_DEBUG("called.");
465 const char* value = afb_req_value(request, _application_id);
467 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
468 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
469 g_hs_instance->client_manager->setStartupAppid(std::string(value));
470 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
471 HS_AfmMainProxy afm_proxy;
472 afm_proxy.start(request->api, id);
477 ret = AFB_EVENT_BAD_REQUEST;
481 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
484 struct json_object *res = json_object_new_object();
485 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
487 afb_req_success(request, res, "afb_event_push event [showWindow]");
495 * - request : the request
501 static void hideWindow(afb_req_t request)
503 AFB_DEBUG("called.");
505 const char* value = afb_req_value(request, _application_id);
507 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
510 ret = AFB_EVENT_BAD_REQUEST;
514 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
517 struct json_object *res = json_object_new_object();
518 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
520 afb_req_success(request, res, "afb_event_push event [hideWindow]");
525 * replyShowWindow event
528 * - request : the request
534 static void replyShowWindow(afb_req_t request)
536 AFB_DEBUG("called.");
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 [replyShowWindow]");
558 * showNotification event
560 * the contents to homescreen which display at top area.
563 * - request : the request
569 static void showNotification(afb_req_t request)
571 AFB_DEBUG("called.");
572 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
574 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
577 struct json_object *res = json_object_new_object();
578 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
580 afb_req_success(request, res, "afb_event_push event [showNotification]");
585 * showInformation event
587 * the contents to homescreen which display at bottom area.
590 * - request : the request
596 static void showInformation(afb_req_t request)
598 AFB_DEBUG("called.");
599 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
601 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
604 struct json_object *res = json_object_new_object();
605 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
607 afb_req_success(request, res, "afb_event_push event [showInformation]");
615 * - request : the request
621 static void getRunnables(afb_req_t request)
623 AFB_DEBUG("called.");
624 struct json_object* j_runnable = json_object_new_array();
625 g_hs_instance->app_info->getRunnables(&j_runnable);
627 /*create response json object*/
628 struct json_object *res = json_object_new_object();
629 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
630 json_object_object_add(res, _keyData, j_runnable);
631 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
635 * array of the verbs exported to afb-daemon
637 static const afb_verb_t verbs[]= {
638 /* VERB'S NAME FUNCTION TO CALL */
639 { .verb="ping", .callback=pingSample },
640 { .verb="tap_shortcut", .callback=tap_shortcut },
641 { .verb="showWindow", .callback=showWindow },
642 { .verb="hideWindow", .callback=hideWindow },
643 { .verb="replyShowWindow", .callback=replyShowWindow },
644 { .verb="on_screen_message", .callback=on_screen_message },
645 { .verb="on_screen_reply", .callback=on_screen_reply },
646 { .verb="subscribe", .callback=subscribe },
647 { .verb="unsubscribe", .callback=unsubscribe },
648 { .verb="showNotification", .callback=showNotification },
649 { .verb="showInformation", .callback=showInformation },
650 { .verb="getRunnables", .callback=getRunnables },
651 {NULL } /* marker for end of the array */
655 * homescreen binding preinit function
658 * - api : the api serving the request
664 static int preinit(afb_api_t api)
666 AFB_DEBUG("binding preinit (was register)");
671 * homescreen binding init function
674 * - api : the api serving the request
680 static int init(afb_api_t api)
682 AFB_DEBUG("binding init");
684 if(g_hs_instance != nullptr) {
685 AFB_WARNING( "g_hs_instance isn't null.");
686 delete g_hs_instance->client_manager;
687 delete g_hs_instance->app_info;
688 delete g_hs_instance->app_recover;
689 delete g_hs_instance;
690 g_hs_instance = nullptr;
692 g_hs_instance = new hs_instance();
693 if(g_hs_instance == nullptr) {
694 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
698 return g_hs_instance->init(api);
702 * homescreen binding event function
705 * - api : the api serving the request
706 * - event : event name
707 * - object : event json object
713 static void onevent(afb_api_t api, const char *event, struct json_object *object)
715 AFB_INFO("on_event %s", event);
716 g_hs_instance->onEvent(api, event, object);
719 const afb_binding_t afbBindingExport = {
721 .specification = NULL,