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"
30 const char _error[] = "error";
31 const char _application_id[] = "application_id";
32 const char _display_message[] = "display_message";
33 const char _reply_message[] = "reply_message";
34 const char _keyData[] = "data";
35 const char _keyId[] = "id";
38 HS_ClientManager *client_manager; // the connection session manager
39 HS_AppInfo *app_info; // application info
41 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
42 int init(afb_api_t api);
43 void setEventHook(const char *event, const event_hook_func f);
44 void onEvent(afb_api_t api, const char *event, struct json_object *object);
46 std::unordered_map<std::string, std::list<event_hook_func>> event_hook_list;
53 * - api : the api serving the request
60 int hs_instance::init(afb_api_t api)
62 if(client_manager == nullptr) {
63 AFB_ERROR("client_manager is nullptr.");
66 client_manager->init();
68 if(app_info == nullptr) {
69 AFB_ERROR("app_info is nullptr.");
75 if(hs_config.readConfig() < 0) {
76 AFB_ERROR("read config file failed.");
89 * - event : event name
95 void hs_instance::setEventHook(const char *event, const event_hook_func f)
97 if(event == nullptr || f == nullptr) {
98 AFB_WARNING("argument is null.");
102 std::string ev(event);
103 auto it = event_hook_list.find(ev);
104 if(it != event_hook_list.end()) {
105 it->second.push_back(f);
108 std::list<event_hook_func> l;
110 event_hook_list[ev] = std::move(l);
118 * - api : the api serving the request
119 * - event : event name
120 * - object : event json object
125 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
127 std::string ev(event);
128 auto it = event_hook_list.find(ev);
129 if(it != event_hook_list.end()) {
130 for(auto &ref : it->second) {
131 if(ref(api, event, object))
137 static struct hs_instance *g_hs_instance;
143 * - event : event name
144 * - f : hook function pointer
149 void setEventHook(const char *event, const event_hook_func f)
151 if(g_hs_instance == nullptr) {
152 AFB_ERROR("g_hs_instance is null.");
156 g_hs_instance->setEventHook(event, f);
160 ********** Method of HomeScreen Service (API) **********
163 static void pingSample(afb_req_t request)
165 static int pingcount = 0;
166 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
167 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
172 * tap_shortcut notify for homescreen
173 * When Shortcut area is tapped, notify these applciations
177 * - application_id : application id
183 static void tap_shortcut (afb_req_t request)
185 AFB_DEBUG("called.");
187 const char* value = afb_req_value(request, _application_id);
189 AFB_INFO("request appid = %s.", value);
190 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
191 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
192 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
193 HS_AfmMainProxy afm_proxy;
194 afm_proxy.start(request, id);
199 ret = AFB_EVENT_BAD_REQUEST;
203 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
206 struct json_object *res = json_object_new_object();
207 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
209 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
214 * HomeScreen OnScreen message
218 * - display_message : message for display
224 static void on_screen_message (afb_req_t request)
226 AFB_DEBUG("called.");
227 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
229 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
232 struct json_object *res = json_object_new_object();
233 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
235 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
240 * HomeScreen OnScreen Reply
244 * - reply_message : message for reply
250 static void on_screen_reply (afb_req_t request)
252 AFB_DEBUG("called.");
253 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
255 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
258 struct json_object *res = json_object_new_object();
259 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
261 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
269 * - event : Event name. Event list is written in libhomescreen.cpp
275 static void subscribe(afb_req_t request)
277 AFB_DEBUG("called.");
279 std::string req_appid = std::move(get_application_id(request));
280 if(!req_appid.empty()) {
281 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
284 ret = AFB_EVENT_BAD_REQUEST;
288 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
291 struct json_object *res = json_object_new_object();
292 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
294 afb_req_success_f(request, res, "homescreen binder subscribe.");
302 * - event : Event name. Event list is written in libhomescreen.cpp
308 static void unsubscribe(afb_req_t request)
310 AFB_DEBUG("called.");
312 std::string req_appid = std::move(get_application_id(request));
313 if(!req_appid.empty()) {
314 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
317 ret = AFB_EVENT_BAD_REQUEST;
321 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
324 struct json_object *res = json_object_new_object();
325 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
327 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
335 * - request : the request
341 static void showWindow(afb_req_t request)
343 AFB_DEBUG("called.");
345 const char* value = afb_req_value(request, _application_id);
347 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
348 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
349 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
350 HS_AfmMainProxy afm_proxy;
351 afm_proxy.start(request, id);
356 ret = AFB_EVENT_BAD_REQUEST;
360 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
363 struct json_object *res = json_object_new_object();
364 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
366 afb_req_success(request, res, "afb_event_push event [showWindow]");
374 * - request : the request
380 static void hideWindow(afb_req_t request)
382 AFB_DEBUG("called.");
384 const char* value = afb_req_value(request, _application_id);
386 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
389 ret = AFB_EVENT_BAD_REQUEST;
393 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
396 struct json_object *res = json_object_new_object();
397 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
399 afb_req_success(request, res, "afb_event_push event [hideWindow]");
404 * replyShowWindow event
407 * - request : the request
413 static void replyShowWindow(afb_req_t request)
415 AFB_DEBUG("called.");
417 const char* value = afb_req_value(request, _application_id);
419 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
422 ret = AFB_EVENT_BAD_REQUEST;
426 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
429 struct json_object *res = json_object_new_object();
430 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
432 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
437 * showNotification event
439 * the contents to homescreen which display at top area.
442 * - request : the request
448 static void showNotification(afb_req_t request)
450 AFB_DEBUG("called.");
451 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
453 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
456 struct json_object *res = json_object_new_object();
457 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
459 afb_req_success(request, res, "afb_event_push event [showNotification]");
464 * showInformation event
466 * the contents to homescreen which display at bottom area.
469 * - request : the request
475 static void showInformation(afb_req_t request)
477 AFB_DEBUG("called.");
478 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
480 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
483 struct json_object *res = json_object_new_object();
484 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
486 afb_req_success(request, res, "afb_event_push event [showInformation]");
494 * - request : the request
500 static void getRunnables(afb_req_t request)
502 AFB_DEBUG("called.");
503 struct json_object* j_runnable = json_object_new_array();
504 g_hs_instance->app_info->getRunnables(&j_runnable);
506 /*create response json object*/
507 struct json_object *res = json_object_new_object();
508 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
509 json_object_object_add(res, _keyData, j_runnable);
510 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
514 * array of the verbs exported to afb-daemon
516 static const afb_verb_t verbs[]= {
517 /* VERB'S NAME FUNCTION TO CALL */
518 { .verb="ping", .callback=pingSample },
519 { .verb="tap_shortcut", .callback=tap_shortcut },
520 { .verb="showWindow", .callback=showWindow },
521 { .verb="hideWindow", .callback=hideWindow },
522 { .verb="replyShowWindow", .callback=replyShowWindow },
523 { .verb="on_screen_message", .callback=on_screen_message },
524 { .verb="on_screen_reply", .callback=on_screen_reply },
525 { .verb="subscribe", .callback=subscribe },
526 { .verb="unsubscribe", .callback=unsubscribe },
527 { .verb="showNotification", .callback=showNotification },
528 { .verb="showInformation", .callback=showInformation },
529 { .verb="getRunnables", .callback=getRunnables },
530 {NULL } /* marker for end of the array */
534 * homescreen binding preinit function
537 * - api : the api serving the request
543 static int preinit(afb_api_t api)
545 AFB_DEBUG("binding preinit (was register)");
550 * homescreen binding init function
553 * - api : the api serving the request
559 static int init(afb_api_t api)
561 AFB_DEBUG("binding init");
563 if(g_hs_instance != nullptr) {
564 AFB_WARNING( "g_hs_instance isn't null.");
565 delete g_hs_instance->client_manager;
566 delete g_hs_instance->app_info;
567 delete g_hs_instance;
568 g_hs_instance = nullptr;
570 g_hs_instance = new hs_instance();
571 if(g_hs_instance == nullptr) {
572 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
576 return g_hs_instance->init(api);
580 * homescreen binding event function
583 * - api : the api serving the request
584 * - event : event name
585 * - object : event json object
591 static void onevent(afb_api_t api, const char *event, struct json_object *object)
593 AFB_INFO("on_event %s", event);
594 g_hs_instance->onEvent(api, event, object);
597 const afb_binding_t afbBindingExport = {
599 .specification = NULL,