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 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
309 HS_AfmMainProxy afm_proxy;
310 afm_proxy.start(request->api, id);
315 ret = AFB_EVENT_BAD_REQUEST;
319 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
322 struct json_object *res = json_object_new_object();
323 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
325 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
330 * HomeScreen OnScreen message
334 * - display_message : message for display
340 static void on_screen_message (afb_req_t request)
342 AFB_DEBUG("called.");
343 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
345 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
348 struct json_object *res = json_object_new_object();
349 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
351 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
356 * HomeScreen OnScreen Reply
360 * - reply_message : message for reply
366 static void on_screen_reply (afb_req_t request)
368 AFB_DEBUG("called.");
369 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
371 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
374 struct json_object *res = json_object_new_object();
375 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
377 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
385 * - event : Event name. Event list is written in libhomescreen.cpp
391 static void subscribe(afb_req_t request)
393 AFB_DEBUG("called.");
395 std::string req_appid = std::move(get_application_id(request));
396 if(!req_appid.empty()) {
397 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
400 ret = AFB_EVENT_BAD_REQUEST;
404 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
407 struct json_object *res = json_object_new_object();
408 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
410 afb_req_success_f(request, res, "homescreen binder subscribe.");
418 * - event : Event name. Event list is written in libhomescreen.cpp
424 static void unsubscribe(afb_req_t request)
426 AFB_DEBUG("called.");
428 std::string req_appid = std::move(get_application_id(request));
429 if(!req_appid.empty()) {
430 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
433 ret = AFB_EVENT_BAD_REQUEST;
437 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
440 struct json_object *res = json_object_new_object();
441 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
443 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
451 * - request : the request
457 static void showWindow(afb_req_t request)
459 AFB_DEBUG("called.");
461 const char* value = afb_req_value(request, _application_id);
463 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
464 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
465 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
466 HS_AfmMainProxy afm_proxy;
467 afm_proxy.start(request->api, id);
472 ret = AFB_EVENT_BAD_REQUEST;
476 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
479 struct json_object *res = json_object_new_object();
480 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
482 afb_req_success(request, res, "afb_event_push event [showWindow]");
490 * - request : the request
496 static void hideWindow(afb_req_t request)
498 AFB_DEBUG("called.");
500 const char* value = afb_req_value(request, _application_id);
502 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
505 ret = AFB_EVENT_BAD_REQUEST;
509 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
512 struct json_object *res = json_object_new_object();
513 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
515 afb_req_success(request, res, "afb_event_push event [hideWindow]");
520 * replyShowWindow event
523 * - request : the request
529 static void replyShowWindow(afb_req_t request)
531 AFB_DEBUG("called.");
533 const char* value = afb_req_value(request, _application_id);
535 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
538 ret = AFB_EVENT_BAD_REQUEST;
542 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
545 struct json_object *res = json_object_new_object();
546 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
548 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
553 * showNotification event
555 * the contents to homescreen which display at top area.
558 * - request : the request
564 static void showNotification(afb_req_t request)
566 AFB_DEBUG("called.");
567 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
569 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
572 struct json_object *res = json_object_new_object();
573 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
575 afb_req_success(request, res, "afb_event_push event [showNotification]");
580 * showInformation event
582 * the contents to homescreen which display at bottom area.
585 * - request : the request
591 static void showInformation(afb_req_t request)
593 AFB_DEBUG("called.");
594 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
596 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
599 struct json_object *res = json_object_new_object();
600 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
602 afb_req_success(request, res, "afb_event_push event [showInformation]");
610 * - request : the request
616 static void getRunnables(afb_req_t request)
618 AFB_DEBUG("called.");
619 struct json_object* j_runnable = json_object_new_array();
620 g_hs_instance->app_info->getRunnables(&j_runnable);
622 /*create response json object*/
623 struct json_object *res = json_object_new_object();
624 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
625 json_object_object_add(res, _keyData, j_runnable);
626 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
630 * array of the verbs exported to afb-daemon
632 static const afb_verb_t verbs[]= {
633 /* VERB'S NAME FUNCTION TO CALL */
634 { .verb="ping", .callback=pingSample },
635 { .verb="tap_shortcut", .callback=tap_shortcut },
636 { .verb="showWindow", .callback=showWindow },
637 { .verb="hideWindow", .callback=hideWindow },
638 { .verb="replyShowWindow", .callback=replyShowWindow },
639 { .verb="on_screen_message", .callback=on_screen_message },
640 { .verb="on_screen_reply", .callback=on_screen_reply },
641 { .verb="subscribe", .callback=subscribe },
642 { .verb="unsubscribe", .callback=unsubscribe },
643 { .verb="showNotification", .callback=showNotification },
644 { .verb="showInformation", .callback=showInformation },
645 { .verb="getRunnables", .callback=getRunnables },
646 {NULL } /* marker for end of the array */
650 * homescreen binding preinit function
653 * - api : the api serving the request
659 static int preinit(afb_api_t api)
661 AFB_DEBUG("binding preinit (was register)");
666 * homescreen binding init function
669 * - api : the api serving the request
675 static int init(afb_api_t api)
677 AFB_DEBUG("binding init");
679 if(g_hs_instance != nullptr) {
680 AFB_WARNING( "g_hs_instance isn't null.");
681 delete g_hs_instance->client_manager;
682 delete g_hs_instance->app_info;
683 delete g_hs_instance;
684 g_hs_instance = nullptr;
686 g_hs_instance = new hs_instance();
687 if(g_hs_instance == nullptr) {
688 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
692 return g_hs_instance->init(api);
696 * homescreen binding event function
699 * - api : the api serving the request
700 * - event : event name
701 * - object : event json object
707 static void onevent(afb_api_t api, const char *event, struct json_object *object)
709 AFB_INFO("on_event %s", event);
710 g_hs_instance->onEvent(api, event, object);
713 const afb_binding_t afbBindingExport = {
715 .specification = NULL,