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.
22 #include <unordered_map>
24 #include "hs-helper.h"
25 #include "hs-clientmanager.h"
26 #include "hs-appinfo.h"
27 #include "hs-config.h"
31 const char _error[] = "error";
32 const char _application_id[] = "application_id";
33 const char _display_message[] = "display_message";
34 const char _reply_message[] = "reply_message";
35 const char _keyData[] = "data";
36 const char _keyId[] = "id";
39 hs_handshake(int times, int sleep) : m_times(times), m_sleep(sleep) {}
40 void start(afb_api_t api) const;
42 enum HandshakeStatus {
44 Handshake_Subscribing,
45 Handshake_Subscribe_Fail,
52 const std::string sub_event = "windowmanager/handshake";
57 int hs_handshake::hs_sts = hs_handshake::Handshake_Idle;
59 void handshake_subscribe_callback(struct json_object *obj, const char *error, const char *info)
61 if(error == nullptr) {
62 hs_handshake::hs_sts = hs_handshake::Handshake_WaitEvent;
65 hs_handshake::hs_sts = hs_handshake::Handshake_Subscribe_Fail;
69 int on_handshake_event(afb_api_t api, const char *event, struct json_object *object)
71 hs_handshake::hs_sts = hs_handshake::Handshake_Over;
75 void hs_handshake::start(afb_api_t api) const
77 setEventHook(sub_event.c_str(), on_handshake_event);
80 // try to subscribe handshake event
81 if(hs_handshake::hs_sts == hs_handshake::Handshake_Idle
82 || hs_handshake::hs_sts == hs_handshake::Handshake_Subscribe_Fail) {
83 hs_handshake::hs_sts = Handshake_Subscribing;
84 HS_WmProxy *wm_proxy = new HS_WmProxy();
85 wm_proxy->subscribe(api, HS_WmProxy::Event_Handshake, handshake_subscribe_callback);
88 // wait handshake event
89 if(hs_handshake::hs_sts == hs_handshake::Handshake_Over) {
95 } while(count < m_times);
96 AFB_WARNING("wait count is %d.", count);
100 HS_ClientManager *client_manager; // the connection session manager
101 HS_AppInfo *app_info; // application info
103 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
104 int init(afb_api_t api);
105 void setEventHook(const char *event, const event_hook_func f);
106 void onEvent(afb_api_t api, const char *event, struct json_object *object);
108 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
115 * - api : the api serving the request
122 int hs_instance::init(afb_api_t api)
124 if(client_manager == nullptr) {
125 AFB_ERROR("client_manager is nullptr.");
128 client_manager->init();
130 if(app_info == nullptr) {
131 AFB_ERROR("app_info is nullptr.");
137 if(hs_config.readConfig() < 0) {
138 AFB_ERROR("read config file failed.");
142 // const struct handshake_info *h = hs_config.getHandshakeInfo();
143 // struct hs_handshake handshake(h->times, h->sleep);
144 // handshake.start(api);
146 // recover application
156 * - event : event name
157 * - f : hook function
162 void hs_instance::setEventHook(const char *event, const event_hook_func f)
164 if(event == nullptr || f == nullptr) {
165 AFB_WARNING("argument is null.");
169 std::string ev(event);
170 auto it = event_hook_list.find(ev);
171 if(it != event_hook_list.end()) {
172 it->second.push_back(f);
175 std::list<event_hook_func> l;
177 event_hook_list[ev] = std::move(l);
185 * - api : the api serving the request
186 * - event : event name
187 * - object : event json object
192 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
194 std::string ev(event);
195 auto it = event_hook_list.find(ev);
196 if(it != event_hook_list.end()) {
197 for(auto &ref : it->second) {
198 if(ref(api, event, object))
204 static struct hs_instance *g_hs_instance;
210 * - event : event name
211 * - f : hook function pointer
216 void setEventHook(const char *event, const event_hook_func f)
218 if(g_hs_instance == nullptr) {
219 AFB_ERROR("g_hs_instance is null.");
223 g_hs_instance->setEventHook(event, f);
227 ********** Method of HomeScreen Service (API) **********
230 static void pingSample(afb_req_t request)
232 static int pingcount = 0;
233 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
234 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
239 * tap_shortcut notify for homescreen
240 * When Shortcut area is tapped, notify these applciations
244 * - application_id : application id
250 static void tap_shortcut (afb_req_t request)
252 AFB_DEBUG("called.");
254 const char* value = afb_req_value(request, _application_id);
256 AFB_INFO("request appid = %s.", value);
257 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
258 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
259 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
260 HS_AfmMainProxy afm_proxy;
261 afm_proxy.start(request, id);
266 ret = AFB_EVENT_BAD_REQUEST;
270 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
273 struct json_object *res = json_object_new_object();
274 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
276 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
281 * HomeScreen OnScreen message
285 * - display_message : message for display
291 static void on_screen_message (afb_req_t request)
293 AFB_DEBUG("called.");
294 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
296 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
299 struct json_object *res = json_object_new_object();
300 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
302 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
307 * HomeScreen OnScreen Reply
311 * - reply_message : message for reply
317 static void on_screen_reply (afb_req_t request)
319 AFB_DEBUG("called.");
320 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
322 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
325 struct json_object *res = json_object_new_object();
326 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
328 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
336 * - event : Event name. Event list is written in libhomescreen.cpp
342 static void subscribe(afb_req_t request)
344 AFB_DEBUG("called.");
346 std::string req_appid = std::move(get_application_id(request));
347 if(!req_appid.empty()) {
348 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
351 ret = AFB_EVENT_BAD_REQUEST;
355 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
358 struct json_object *res = json_object_new_object();
359 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
361 afb_req_success_f(request, res, "homescreen binder subscribe.");
369 * - event : Event name. Event list is written in libhomescreen.cpp
375 static void unsubscribe(afb_req_t request)
377 AFB_DEBUG("called.");
379 std::string req_appid = std::move(get_application_id(request));
380 if(!req_appid.empty()) {
381 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
384 ret = AFB_EVENT_BAD_REQUEST;
388 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
391 struct json_object *res = json_object_new_object();
392 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
394 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
402 * - request : the request
408 static void showWindow(afb_req_t request)
410 AFB_DEBUG("called.");
412 const char* value = afb_req_value(request, _application_id);
414 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
415 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
416 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
417 HS_AfmMainProxy afm_proxy;
418 afm_proxy.start(request, id);
423 ret = AFB_EVENT_BAD_REQUEST;
427 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
430 struct json_object *res = json_object_new_object();
431 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
433 afb_req_success(request, res, "afb_event_push event [showWindow]");
441 * - request : the request
447 static void hideWindow(afb_req_t request)
449 AFB_DEBUG("called.");
451 const char* value = afb_req_value(request, _application_id);
453 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
456 ret = AFB_EVENT_BAD_REQUEST;
460 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
463 struct json_object *res = json_object_new_object();
464 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
466 afb_req_success(request, res, "afb_event_push event [hideWindow]");
471 * replyShowWindow event
474 * - request : the request
480 static void replyShowWindow(afb_req_t request)
482 AFB_DEBUG("called.");
484 const char* value = afb_req_value(request, _application_id);
486 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
489 ret = AFB_EVENT_BAD_REQUEST;
493 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
496 struct json_object *res = json_object_new_object();
497 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
499 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
504 * showNotification event
506 * the contents to homescreen which display at top area.
509 * - request : the request
515 static void showNotification(afb_req_t request)
517 AFB_DEBUG("called.");
518 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
520 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
523 struct json_object *res = json_object_new_object();
524 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
526 afb_req_success(request, res, "afb_event_push event [showNotification]");
531 * showInformation event
533 * the contents to homescreen which display at bottom area.
536 * - request : the request
542 static void showInformation(afb_req_t request)
544 AFB_DEBUG("called.");
545 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
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 [showInformation]");
561 * - request : the request
567 static void getRunnables(afb_req_t request)
569 AFB_DEBUG("called.");
570 struct json_object* j_runnable = json_object_new_array();
571 g_hs_instance->app_info->getRunnables(&j_runnable);
573 /*create response json object*/
574 struct json_object *res = json_object_new_object();
575 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
576 json_object_object_add(res, _keyData, j_runnable);
577 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
581 * array of the verbs exported to afb-daemon
583 static const afb_verb_t verbs[]= {
584 /* VERB'S NAME FUNCTION TO CALL */
585 { .verb="ping", .callback=pingSample },
586 { .verb="tap_shortcut", .callback=tap_shortcut },
587 { .verb="showWindow", .callback=showWindow },
588 { .verb="hideWindow", .callback=hideWindow },
589 { .verb="replyShowWindow", .callback=replyShowWindow },
590 { .verb="on_screen_message", .callback=on_screen_message },
591 { .verb="on_screen_reply", .callback=on_screen_reply },
592 { .verb="subscribe", .callback=subscribe },
593 { .verb="unsubscribe", .callback=unsubscribe },
594 { .verb="showNotification", .callback=showNotification },
595 { .verb="showInformation", .callback=showInformation },
596 { .verb="getRunnables", .callback=getRunnables },
597 {NULL } /* marker for end of the array */
601 * homescreen binding preinit function
604 * - api : the api serving the request
610 static int preinit(afb_api_t api)
612 AFB_DEBUG("binding preinit (was register)");
617 * homescreen binding init function
620 * - api : the api serving the request
626 static int init(afb_api_t api)
628 AFB_DEBUG("binding init");
630 if(g_hs_instance != nullptr) {
631 AFB_WARNING( "g_hs_instance isn't null.");
632 delete g_hs_instance->client_manager;
633 delete g_hs_instance->app_info;
634 delete g_hs_instance;
635 g_hs_instance = nullptr;
637 g_hs_instance = new hs_instance();
638 if(g_hs_instance == nullptr) {
639 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
643 return g_hs_instance->init(api);
647 * homescreen binding event function
650 * - api : the api serving the request
651 * - event : event name
652 * - object : event json object
658 static void onevent(afb_api_t api, const char *event, struct json_object *object)
660 AFB_INFO("on_event %s", event);
661 g_hs_instance->onEvent(api, event, object);
664 const afb_binding_t afbBindingExport = {
666 .specification = NULL,