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"
29 const char _error[] = "error";
30 const char _application_id[] = "application_id";
31 const char _display_message[] = "display_message";
32 const char _reply_message[] = "reply_message";
33 const char _keyData[] = "data";
34 const char _keyId[] = "id";
37 HS_ClientManager *client_manager; // the connection session manager
38 HS_AppInfo *app_info; // application info
40 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
41 int init(afb_api_t api);
42 void setEventHook(const char *event, const event_hook_func f);
43 void onEvent(afb_api_t api, const char *event, struct json_object *object);
45 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
52 * - api : the api serving the request
59 int hs_instance::init(afb_api_t api)
61 if(client_manager == nullptr) {
62 AFB_ERROR("FATAL ERROR: client_manager is nullptr.");
65 client_manager->init();
67 if(app_info == nullptr) {
68 AFB_ERROR("FATAL ERROR: app_info is nullptr.");
80 * - event : event name
86 void hs_instance::setEventHook(const char *event, const event_hook_func f)
88 if(event == nullptr || f == nullptr) {
89 AFB_WARNING("argument is null.");
93 std::string ev(event);
94 auto it = event_hook_list.find(ev);
95 if(it != event_hook_list.end()) {
96 it->second.push_back(f);
99 std::list<event_hook_func> l;
101 event_hook_list[ev] = std::move(l);
109 * - api : the api serving the request
110 * - event : event name
111 * - object : event json object
116 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
118 std::string ev(event);
119 auto it = event_hook_list.find(ev);
120 if(it != event_hook_list.end()) {
121 for(auto &ref : it->second) {
122 if(ref(api, event, object))
128 static struct hs_instance *g_hs_instance;
134 * - event : event name
135 * - f : hook function pointer
140 void setEventHook(const char *event, const event_hook_func f)
142 if(g_hs_instance == nullptr) {
143 AFB_ERROR("g_hs_instance is null.");
147 g_hs_instance->setEventHook(event, f);
151 ********** Method of HomeScreen Service (API) **********
154 static void pingSample(afb_req_t request)
156 static int pingcount = 0;
157 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
158 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
163 * tap_shortcut notify for homescreen
164 * When Shortcut area is tapped, notify these applciations
168 * - application_id : application id
174 static void tap_shortcut (afb_req_t request)
176 AFB_DEBUG("called.");
178 const char* value = afb_req_value(request, _application_id);
180 AFB_INFO("request appid = %s.", value);
181 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
182 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
183 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
184 HS_AfmMainProxy afm_proxy;
185 afm_proxy.start(request, id);
190 ret = AFB_EVENT_BAD_REQUEST;
194 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
197 struct json_object *res = json_object_new_object();
198 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
200 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
205 * HomeScreen OnScreen message
209 * - display_message : message for display
215 static void on_screen_message (afb_req_t request)
217 AFB_DEBUG("called.");
218 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
220 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
223 struct json_object *res = json_object_new_object();
224 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
226 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
231 * HomeScreen OnScreen Reply
235 * - reply_message : message for reply
241 static void on_screen_reply (afb_req_t request)
243 AFB_DEBUG("called.");
244 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
246 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
249 struct json_object *res = json_object_new_object();
250 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
252 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
260 * - event : Event name. Event list is written in libhomescreen.cpp
266 static void subscribe(afb_req_t request)
268 AFB_DEBUG("called.");
270 std::string req_appid = std::move(get_application_id(request));
271 if(!req_appid.empty()) {
272 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
275 ret = AFB_EVENT_BAD_REQUEST;
279 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
282 struct json_object *res = json_object_new_object();
283 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
285 afb_req_success_f(request, res, "homescreen binder subscribe.");
293 * - event : Event name. Event list is written in libhomescreen.cpp
299 static void unsubscribe(afb_req_t request)
301 AFB_DEBUG("called.");
303 std::string req_appid = std::move(get_application_id(request));
304 if(!req_appid.empty()) {
305 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
308 ret = AFB_EVENT_BAD_REQUEST;
312 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
315 struct json_object *res = json_object_new_object();
316 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
318 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
326 * - request : the request
332 static void showWindow(afb_req_t request)
334 AFB_DEBUG("called.");
336 const char* value = afb_req_value(request, _application_id);
338 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
339 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
340 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
341 HS_AfmMainProxy afm_proxy;
342 afm_proxy.start(request, id);
347 ret = AFB_EVENT_BAD_REQUEST;
351 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
354 struct json_object *res = json_object_new_object();
355 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
357 afb_req_success(request, res, "afb_event_push event [showWindow]");
365 * - request : the request
371 static void hideWindow(afb_req_t request)
373 AFB_DEBUG("called.");
375 const char* value = afb_req_value(request, _application_id);
377 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
380 ret = AFB_EVENT_BAD_REQUEST;
384 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
387 struct json_object *res = json_object_new_object();
388 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
390 afb_req_success(request, res, "afb_event_push event [hideWindow]");
395 * replyShowWindow event
398 * - request : the request
404 static void replyShowWindow(afb_req_t request)
406 AFB_DEBUG("called.");
408 const char* value = afb_req_value(request, _application_id);
410 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
413 ret = AFB_EVENT_BAD_REQUEST;
417 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
420 struct json_object *res = json_object_new_object();
421 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
423 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
428 * showNotification event
430 * the contents to homescreen which display at top area.
433 * - request : the request
439 static void showNotification(afb_req_t request)
441 AFB_DEBUG("called.");
442 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
444 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __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(request, res, "afb_event_push event [showNotification]");
455 * showInformation event
457 * the contents to homescreen which display at bottom area.
460 * - request : the request
466 static void showInformation(afb_req_t request)
468 AFB_DEBUG("called.");
469 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
471 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
474 struct json_object *res = json_object_new_object();
475 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
477 afb_req_success(request, res, "afb_event_push event [showInformation]");
485 * - request : the request
491 static void getRunnables(afb_req_t request)
493 AFB_DEBUG("called.");
494 struct json_object* j_runnable = json_object_new_array();
495 g_hs_instance->app_info->getRunnables(&j_runnable);
497 /*create response json object*/
498 struct json_object *res = json_object_new_object();
499 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
500 json_object_object_add(res, _keyData, j_runnable);
501 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
505 * array of the verbs exported to afb-daemon
507 static const afb_verb_t verbs[]= {
508 /* VERB'S NAME FUNCTION TO CALL */
509 { .verb="ping", .callback=pingSample },
510 { .verb="tap_shortcut", .callback=tap_shortcut },
511 { .verb="showWindow", .callback=showWindow },
512 { .verb="hideWindow", .callback=hideWindow },
513 { .verb="replyShowWindow", .callback=replyShowWindow },
514 { .verb="on_screen_message", .callback=on_screen_message },
515 { .verb="on_screen_reply", .callback=on_screen_reply },
516 { .verb="subscribe", .callback=subscribe },
517 { .verb="unsubscribe", .callback=unsubscribe },
518 { .verb="showNotification", .callback=showNotification },
519 { .verb="showInformation", .callback=showInformation },
520 { .verb="getRunnables", .callback=getRunnables },
521 {NULL } /* marker for end of the array */
525 * homescreen binding preinit function
528 * - api : the api serving the request
534 static int preinit(afb_api_t api)
536 AFB_DEBUG("binding preinit (was register)");
541 * homescreen binding init function
544 * - api : the api serving the request
550 static int init(afb_api_t api)
552 AFB_DEBUG("binding init");
554 if(g_hs_instance != nullptr) {
555 AFB_WARNING( "g_hs_instance isn't null.");
556 delete g_hs_instance->client_manager;
557 delete g_hs_instance->app_info;
558 delete g_hs_instance;
559 g_hs_instance = nullptr;
561 g_hs_instance = new hs_instance();
562 if(g_hs_instance == nullptr) {
563 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
567 return g_hs_instance->init(api);
571 * homescreen binding event function
574 * - api : the api serving the request
575 * - event : event name
576 * - object : event json object
582 static void onevent(afb_api_t api, const char *event, struct json_object *object)
584 AFB_INFO("on_event %s", event);
585 g_hs_instance->onEvent(api, event, object);
588 const afb_binding_t afbBindingExport = {
590 .specification = NULL,