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("client_manager is nullptr.");
65 client_manager->init();
67 if(app_info == nullptr) {
68 AFB_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)
177 const char* value = afb_req_value(request, _application_id);
179 AFB_INFO("request appid = %s.", value);
180 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
181 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
182 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
183 HS_AfmMainProxy afm_proxy;
184 afm_proxy.start(request, id);
189 ret = AFB_EVENT_BAD_REQUEST;
193 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
196 struct json_object *res = json_object_new_object();
197 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
199 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
204 * HomeScreen OnScreen message
208 * - display_message : message for display
214 static void on_screen_message (afb_req_t request)
216 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
218 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
221 struct json_object *res = json_object_new_object();
222 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
224 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
229 * HomeScreen OnScreen Reply
233 * - reply_message : message for reply
239 static void on_screen_reply (afb_req_t request)
241 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
243 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
246 struct json_object *res = json_object_new_object();
247 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
249 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
257 * - event : Event name. Event list is written in libhomescreen.cpp
263 static void subscribe(afb_req_t request)
266 std::string req_appid = std::move(get_application_id(request));
267 if(!req_appid.empty()) {
268 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
271 ret = AFB_EVENT_BAD_REQUEST;
275 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
278 struct json_object *res = json_object_new_object();
279 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
281 afb_req_success_f(request, res, "homescreen binder subscribe.");
289 * - event : Event name. Event list is written in libhomescreen.cpp
295 static void unsubscribe(afb_req_t request)
298 std::string req_appid = std::move(get_application_id(request));
299 if(!req_appid.empty()) {
300 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
303 ret = AFB_EVENT_BAD_REQUEST;
307 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
310 struct json_object *res = json_object_new_object();
311 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
313 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
321 * - request : the request
327 static void showWindow(afb_req_t request)
330 const char* value = afb_req_value(request, _application_id);
332 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
333 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
334 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
335 HS_AfmMainProxy afm_proxy;
336 afm_proxy.start(request, id);
341 ret = AFB_EVENT_BAD_REQUEST;
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 [showWindow]");
359 * - request : the request
365 static void hideWindow(afb_req_t request)
368 const char* value = afb_req_value(request, _application_id);
370 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
373 ret = AFB_EVENT_BAD_REQUEST;
377 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
380 struct json_object *res = json_object_new_object();
381 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
383 afb_req_success(request, res, "afb_event_push event [hideWindow]");
388 * replyShowWindow event
391 * - request : the request
397 static void replyShowWindow(afb_req_t request)
400 const char* value = afb_req_value(request, _application_id);
402 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
405 ret = AFB_EVENT_BAD_REQUEST;
409 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
412 struct json_object *res = json_object_new_object();
413 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
415 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
420 * showNotification event
422 * the contents to homescreen which display at top area.
425 * - request : the request
431 static void showNotification(afb_req_t request)
433 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
435 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
438 struct json_object *res = json_object_new_object();
439 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
441 afb_req_success(request, res, "afb_event_push event [showNotification]");
446 * showInformation event
448 * the contents to homescreen which display at bottom area.
451 * - request : the request
457 static void showInformation(afb_req_t request)
459 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
461 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
464 struct json_object *res = json_object_new_object();
465 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
467 afb_req_success(request, res, "afb_event_push event [showInformation]");
475 * - request : the request
481 static void getRunnables(afb_req_t request)
483 struct json_object* j_runnable = json_object_new_array();
484 g_hs_instance->app_info->getRunnables(&j_runnable);
486 /*create response json object*/
487 struct json_object *res = json_object_new_object();
488 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
489 json_object_object_add(res, _keyData, j_runnable);
490 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
494 * array of the verbs exported to afb-daemon
496 static const afb_verb_t verbs[]= {
497 /* VERB'S NAME FUNCTION TO CALL */
498 { .verb="ping", .callback=pingSample },
499 { .verb="tap_shortcut", .callback=tap_shortcut },
500 { .verb="showWindow", .callback=showWindow },
501 { .verb="hideWindow", .callback=hideWindow },
502 { .verb="replyShowWindow", .callback=replyShowWindow },
503 { .verb="on_screen_message", .callback=on_screen_message },
504 { .verb="on_screen_reply", .callback=on_screen_reply },
505 { .verb="subscribe", .callback=subscribe },
506 { .verb="unsubscribe", .callback=unsubscribe },
507 { .verb="showNotification", .callback=showNotification },
508 { .verb="showInformation", .callback=showInformation },
509 { .verb="getRunnables", .callback=getRunnables },
510 {NULL } /* marker for end of the array */
514 * homescreen binding preinit function
517 * - api : the api serving the request
523 static int preinit(afb_api_t api)
525 AFB_DEBUG("binding preinit (was register)");
530 * homescreen binding init function
533 * - api : the api serving the request
539 static int init(afb_api_t api)
541 AFB_DEBUG("binding init");
543 if(g_hs_instance != nullptr) {
544 AFB_WARNING( "g_hs_instance isn't null.");
545 delete g_hs_instance->client_manager;
546 delete g_hs_instance->app_info;
547 delete g_hs_instance;
548 g_hs_instance = nullptr;
550 g_hs_instance = new hs_instance();
551 if(g_hs_instance == nullptr) {
555 return g_hs_instance->init(api);
559 * homescreen binding event function
562 * - api : the api serving the request
563 * - event : event name
564 * - object : event json object
570 static void onevent(afb_api_t api, const char *event, struct json_object *object)
572 AFB_INFO("on_event %s", event);
573 g_hs_instance->onEvent(api, event, object);
576 const afb_binding_t afbBindingExport = {
578 .specification = NULL,