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 if(error == nullptr) {
76 hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
79 hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
84 * handshake event function
88 * - event : received event name
89 * - object : received json object
92 * 0 : event can transfer to others
93 * 1 : event not transfer to others
95 int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
97 hs_handshake::hs_sts = hs_handshake::Handshake_Over;
102 * start handshake function
109 * 0 : handshake success
110 * -1 : handshake fail
113 int hs_handshake::start(afb_api_t api) const
116 setEventHook(sub_event.c_str(), on_handshake_event);
119 // try to subscribe handshake event
120 if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
121 || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
122 hs_handshake::hs_sts = Handshake_Subscribing;
124 wm_proxy.subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
127 // wait handshake event
128 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
134 usleep(m_sleep*1000);
135 } while(count < m_times);
141 HS_ClientManager *client_manager; // the connection session manager
142 HS_AppInfo *app_info; // application info
143 HS_AppRecover *app_recover;
145 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()), app_recover(HS_AppRecover::instance()) {}
146 int init(afb_api_t api);
147 void setEventHook(const char *event, const event_hook_func f);
148 void onEvent(afb_api_t api, const char *event, struct json_object *object);
150 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
153 static struct hs_instance *g_hs_instance;
159 * - api : the api serving the request
166 int hs_instance::init(afb_api_t api)
168 if(client_manager == nullptr) {
169 AFB_ERROR("client_manager is nullptr.");
172 client_manager->init();
174 if(app_info == nullptr) {
175 AFB_ERROR("app_info is nullptr.");
181 if(hs_config.readConfig() < 0) {
182 AFB_ERROR("read config file failed.");
186 const struct handshake_info *h = hs_config.getHandshakeInfo();
187 struct hs_handshake handshake(h->times, h->sleep);
188 if(handshake.start(api) < 0) {
189 AFB_ERROR("handshake with windowmanager failed.");
193 if(app_recover == nullptr) {
194 AFB_ERROR("app_recover is nullptr.");
197 app_recover->init(api);
198 app_recover->startRecovery(api, hs_config.getRecoverMap());
207 * - event : event name
208 * - f : hook function
213 void hs_instance::setEventHook(const char *event, const event_hook_func f)
215 if(event == nullptr || f == nullptr) {
216 AFB_WARNING("argument is null.");
220 std::string ev(event);
221 auto it = event_hook_list.find(ev);
222 if(it != event_hook_list.end()) {
223 it->second.push_back(f);
226 std::list<event_hook_func> l;
228 event_hook_list[ev] = std::move(l);
236 * - api : the api serving the request
237 * - event : event name
238 * - object : event json object
243 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
245 std::string ev(event);
246 auto it = event_hook_list.find(ev);
247 if(it != event_hook_list.end()) {
248 for(auto &ref : it->second) {
249 if(ref(api, event, object))
259 * - event : event name
260 * - f : hook function pointer
265 void setEventHook(const char *event, const event_hook_func f)
267 if(g_hs_instance == nullptr) {
268 AFB_ERROR("g_hs_instance is null.");
272 g_hs_instance->setEventHook(event, f);
276 ********** Method of HomeScreen Service (API) **********
279 static void pingSample(afb_req_t request)
281 static int pingcount = 0;
282 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
283 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
288 * tap_shortcut notify for homescreen
289 * When Shortcut area is tapped, notify these applciations
293 * - application_id : application id
299 static void tap_shortcut (afb_req_t request)
301 AFB_DEBUG("called.");
303 const char* value = afb_req_value(request, _application_id);
305 AFB_INFO("request appid = %s.", value);
306 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
307 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
308 g_hs_instance->client_manager->setStartupAppid(std::string(value));
309 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
310 HS_AfmMainProxy afm_proxy;
311 afm_proxy.start(request->api, id);
316 ret = AFB_EVENT_BAD_REQUEST;
320 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
323 struct json_object *res = json_object_new_object();
324 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
326 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
331 * HomeScreen OnScreen message
335 * - display_message : message for display
341 static void on_screen_message (afb_req_t request)
343 AFB_DEBUG("called.");
344 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
346 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
349 struct json_object *res = json_object_new_object();
350 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
352 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
357 * HomeScreen OnScreen Reply
361 * - reply_message : message for reply
367 static void on_screen_reply (afb_req_t request)
369 AFB_DEBUG("called.");
370 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
372 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
375 struct json_object *res = json_object_new_object();
376 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
378 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
386 * - event : Event name. Event list is written in libhomescreen.cpp
392 static void subscribe(afb_req_t request)
394 AFB_DEBUG("called.");
396 std::string req_appid = std::move(get_application_id(request));
397 if(!req_appid.empty()) {
398 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
401 ret = AFB_EVENT_BAD_REQUEST;
405 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
408 struct json_object *res = json_object_new_object();
409 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
411 afb_req_success_f(request, res, "homescreen binder subscribe.");
419 * - event : Event name. Event list is written in libhomescreen.cpp
425 static void unsubscribe(afb_req_t request)
427 AFB_DEBUG("called.");
429 std::string req_appid = std::move(get_application_id(request));
430 if(!req_appid.empty()) {
431 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
434 ret = AFB_EVENT_BAD_REQUEST;
438 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
441 struct json_object *res = json_object_new_object();
442 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
444 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
452 * - request : the request
458 static void showWindow(afb_req_t request)
460 AFB_DEBUG("called.");
462 const char* value = afb_req_value(request, _application_id);
464 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
465 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
466 g_hs_instance->client_manager->setStartupAppid(std::string(value));
467 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
468 HS_AfmMainProxy afm_proxy;
469 afm_proxy.start(request->api, id);
474 ret = AFB_EVENT_BAD_REQUEST;
478 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
481 struct json_object *res = json_object_new_object();
482 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
484 afb_req_success(request, res, "afb_event_push event [showWindow]");
492 * - request : the request
498 static void hideWindow(afb_req_t request)
500 AFB_DEBUG("called.");
502 const char* value = afb_req_value(request, _application_id);
504 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
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 [hideWindow]");
522 * replyShowWindow event
525 * - request : the request
531 static void replyShowWindow(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 [replyShowWindow]");
555 * showNotification event
557 * the contents to homescreen which display at top area.
560 * - request : the request
566 static void showNotification(afb_req_t request)
568 AFB_DEBUG("called.");
569 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
571 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
574 struct json_object *res = json_object_new_object();
575 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
577 afb_req_success(request, res, "afb_event_push event [showNotification]");
582 * showInformation event
584 * the contents to homescreen which display at bottom area.
587 * - request : the request
593 static void showInformation(afb_req_t request)
595 AFB_DEBUG("called.");
596 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
598 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
601 struct json_object *res = json_object_new_object();
602 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
604 afb_req_success(request, res, "afb_event_push event [showInformation]");
612 * - request : the request
618 static void getRunnables(afb_req_t request)
620 AFB_DEBUG("called.");
621 struct json_object* j_runnable = json_object_new_array();
622 g_hs_instance->app_info->getRunnables(&j_runnable);
624 /*create response json object*/
625 struct json_object *res = json_object_new_object();
626 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
627 json_object_object_add(res, _keyData, j_runnable);
628 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
632 * array of the verbs exported to afb-daemon
634 static const afb_verb_t verbs[]= {
635 /* VERB'S NAME FUNCTION TO CALL */
636 { .verb="ping", .callback=pingSample },
637 { .verb="tap_shortcut", .callback=tap_shortcut },
638 { .verb="showWindow", .callback=showWindow },
639 { .verb="hideWindow", .callback=hideWindow },
640 { .verb="replyShowWindow", .callback=replyShowWindow },
641 { .verb="on_screen_message", .callback=on_screen_message },
642 { .verb="on_screen_reply", .callback=on_screen_reply },
643 { .verb="subscribe", .callback=subscribe },
644 { .verb="unsubscribe", .callback=unsubscribe },
645 { .verb="showNotification", .callback=showNotification },
646 { .verb="showInformation", .callback=showInformation },
647 { .verb="getRunnables", .callback=getRunnables },
648 {NULL } /* marker for end of the array */
652 * homescreen binding preinit function
655 * - api : the api serving the request
661 static int preinit(afb_api_t api)
663 AFB_DEBUG("binding preinit (was register)");
668 * homescreen binding init function
671 * - api : the api serving the request
677 static int init(afb_api_t api)
679 AFB_DEBUG("binding init");
681 if(g_hs_instance != nullptr) {
682 AFB_WARNING( "g_hs_instance isn't null.");
683 delete g_hs_instance->client_manager;
684 delete g_hs_instance->app_info;
685 delete g_hs_instance;
686 g_hs_instance = nullptr;
688 g_hs_instance = new hs_instance();
689 if(g_hs_instance == nullptr) {
690 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
694 return g_hs_instance->init(api);
698 * homescreen binding event function
701 * - api : the api serving the request
702 * - event : event name
703 * - object : event json object
709 static void onevent(afb_api_t api, const char *event, struct json_object *object)
711 AFB_INFO("on_event %s", event);
712 g_hs_instance->onEvent(api, event, object);
715 const afb_binding_t afbBindingExport = {
717 .specification = NULL,