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.
21 #include "homescreen.h"
23 const char _error[] = "error";
24 const char _application_id[] = "application_id";
25 const char _display_message[] = "display_message";
26 const char _reply_message[] = "reply_message";
27 const char _keyData[] = "data";
28 const char _keyId[] = "id";
34 * - api : the api serving the request
41 int hs_instance::init(afb_api_t api)
43 if(client_manager == nullptr) {
44 AFB_ERROR("client_manager is nullptr.");
47 client_manager->init();
49 if(app_info == nullptr) {
50 AFB_ERROR("app_info is nullptr.");
62 * - event : event name
68 void hs_instance::setEventHook(const char *event, const event_hook_func f)
70 if(event == nullptr || f == nullptr) {
71 AFB_WARNING("argument is null.");
75 std::string ev(event);
76 auto it = event_hook_list.find(ev);
77 if(it != event_hook_list.end()) {
78 it->second.push_back(f);
81 std::list<event_hook_func> l;
83 event_hook_list[ev] = std::move(l);
91 * - api : the api serving the request
92 * - event : event name
93 * - object : event json object
98 void hs_instance::onEvent(afb_api_t api, const char *event, struct json_object *object)
100 std::string ev(event);
101 auto it = event_hook_list.find(ev);
102 if(it != event_hook_list.end()) {
103 for(auto &ref : it->second) {
104 if(ref(api, event, object))
110 static struct hs_instance *g_hs_instance;
116 * - event : event name
117 * - f : hook function pointer
122 void setEventHook(const char *event, const event_hook_func f)
124 if(g_hs_instance == nullptr) {
125 AFB_ERROR("g_hs_instance is null.");
129 g_hs_instance->setEventHook(event, f);
133 ********** Method of HomeScreen Service (API) **********
136 static void pingSample(afb_req_t request)
138 static int pingcount = 0;
139 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
140 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
145 * tap_shortcut notify for homescreen
146 * When Shortcut area is tapped, notify these applciations
150 * - application_id : application id
156 static void tap_shortcut (afb_req_t request)
159 const char* value = afb_req_value(request, _application_id);
161 AFB_INFO("request appid = %s.", value);
162 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
163 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
164 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
165 HS_AfmMainProxy afm_proxy;
166 afm_proxy.start(g_hs_instance, request, id);
171 ret = AFB_EVENT_BAD_REQUEST;
175 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
178 struct json_object *res = json_object_new_object();
179 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
181 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
186 * HomeScreen OnScreen message
190 * - display_message : message for display
196 static void on_screen_message (afb_req_t request)
198 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
200 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
203 struct json_object *res = json_object_new_object();
204 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
206 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
211 * HomeScreen OnScreen Reply
215 * - reply_message : message for reply
221 static void on_screen_reply (afb_req_t request)
223 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
225 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
228 struct json_object *res = json_object_new_object();
229 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
231 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
239 * - event : Event name. Event list is written in libhomescreen.cpp
245 static void subscribe(afb_req_t request)
248 std::string req_appid = std::move(get_application_id(request));
249 if(!req_appid.empty()) {
250 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
253 ret = AFB_EVENT_BAD_REQUEST;
257 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
260 struct json_object *res = json_object_new_object();
261 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
263 afb_req_success_f(request, res, "homescreen binder subscribe.");
271 * - event : Event name. Event list is written in libhomescreen.cpp
277 static void unsubscribe(afb_req_t request)
280 std::string req_appid = std::move(get_application_id(request));
281 if(!req_appid.empty()) {
282 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
285 ret = AFB_EVENT_BAD_REQUEST;
289 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
292 struct json_object *res = json_object_new_object();
293 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
295 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
303 * - request : the request
309 static void showWindow(afb_req_t request)
312 const char* value = afb_req_value(request, _application_id);
314 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
315 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
316 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
317 HS_AfmMainProxy afm_proxy;
318 afm_proxy.start(g_hs_instance, request, id);
323 ret = AFB_EVENT_BAD_REQUEST;
327 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
330 struct json_object *res = json_object_new_object();
331 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
333 afb_req_success(request, res, "afb_event_push event [showWindow]");
341 * - request : the request
347 static void hideWindow(afb_req_t request)
350 const char* value = afb_req_value(request, _application_id);
352 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
355 ret = AFB_EVENT_BAD_REQUEST;
359 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
362 struct json_object *res = json_object_new_object();
363 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
365 afb_req_success(request, res, "afb_event_push event [hideWindow]");
370 * replyShowWindow event
373 * - request : the request
379 static void replyShowWindow(afb_req_t request)
382 const char* value = afb_req_value(request, _application_id);
384 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
387 ret = AFB_EVENT_BAD_REQUEST;
391 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
394 struct json_object *res = json_object_new_object();
395 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
397 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
402 * showNotification event
404 * the contents to homescreen which display at top area.
407 * - request : the request
413 static void showNotification(afb_req_t request)
415 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
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 [showNotification]");
428 * showInformation event
430 * the contents to homescreen which display at bottom area.
433 * - request : the request
439 static void showInformation(afb_req_t request)
441 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
443 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
446 struct json_object *res = json_object_new_object();
447 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
449 afb_req_success(request, res, "afb_event_push event [showInformation]");
457 * - request : the request
463 static void getRunnables(afb_req_t request)
465 struct json_object* j_runnable = json_object_new_array();
466 g_hs_instance->app_info->getRunnables(&j_runnable);
468 /*create response json object*/
469 struct json_object *res = json_object_new_object();
470 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
471 json_object_object_add(res, _keyData, j_runnable);
472 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
476 * array of the verbs exported to afb-daemon
478 static const afb_verb_t verbs[]= {
479 /* VERB'S NAME FUNCTION TO CALL */
480 { .verb="ping", .callback=pingSample },
481 { .verb="tap_shortcut", .callback=tap_shortcut },
482 { .verb="showWindow", .callback=showWindow },
483 { .verb="hideWindow", .callback=hideWindow },
484 { .verb="replyShowWindow", .callback=replyShowWindow },
485 { .verb="on_screen_message", .callback=on_screen_message },
486 { .verb="on_screen_reply", .callback=on_screen_reply },
487 { .verb="subscribe", .callback=subscribe },
488 { .verb="unsubscribe", .callback=unsubscribe },
489 { .verb="showNotification", .callback=showNotification },
490 { .verb="showInformation", .callback=showInformation },
491 { .verb="getRunnables", .callback=getRunnables },
492 {NULL } /* marker for end of the array */
496 * homescreen binding preinit function
499 * - api : the api serving the request
505 static int preinit(afb_api_t api)
508 AFB_DEBUG("binding preinit (was register)");
513 * homescreen binding init function
516 * - api : the api serving the request
522 static int init(afb_api_t api)
524 AFB_DEBUG("binding init");
526 if(g_hs_instance != nullptr) {
527 AFB_WARNING( "g_hs_instance isn't null.");
528 delete g_hs_instance->client_manager;
529 delete g_hs_instance->app_info;
530 delete g_hs_instance;
531 g_hs_instance = nullptr;
533 g_hs_instance = new hs_instance();
534 if(g_hs_instance == nullptr) {
538 return g_hs_instance->init(api);
542 * homescreen binding event function
545 * - api : the api serving the request
546 * - event : event name
547 * - object : event json object
553 static void onevent(afb_api_t api, const char *event, struct json_object *object)
555 AFB_INFO("on_event %s", event);
556 g_hs_instance->onEvent(api, event, object);
559 const afb_binding_t afbBindingExport = {
561 .specification = NULL,