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);
166 HS_AfmMainProxy afm_proxy;
167 afm_proxy.start(g_hs_instance, request, id);
170 ret = AFB_EVENT_BAD_REQUEST;
175 ret = AFB_EVENT_BAD_REQUEST;
179 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
182 struct json_object *res = json_object_new_object();
183 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
185 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
190 * HomeScreen OnScreen message
194 * - display_message : message for display
200 static void on_screen_message (afb_req_t request)
202 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
204 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
207 struct json_object *res = json_object_new_object();
208 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
210 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
215 * HomeScreen OnScreen Reply
219 * - reply_message : message for reply
225 static void on_screen_reply (afb_req_t request)
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_reply]");
243 * - event : Event name. Event list is written in libhomescreen.cpp
249 static void subscribe(afb_req_t request)
252 std::string req_appid = std::move(get_application_id(request));
253 if(!req_appid.empty()) {
254 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
257 ret = AFB_EVENT_BAD_REQUEST;
261 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
264 struct json_object *res = json_object_new_object();
265 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
267 afb_req_success_f(request, res, "homescreen binder subscribe.");
275 * - event : Event name. Event list is written in libhomescreen.cpp
281 static void unsubscribe(afb_req_t request)
284 std::string req_appid = std::move(get_application_id(request));
285 if(!req_appid.empty()) {
286 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
289 ret = AFB_EVENT_BAD_REQUEST;
293 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
296 struct json_object *res = json_object_new_object();
297 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
299 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
307 * - request : the request
313 static void showWindow(afb_req_t request)
316 const char* value = afb_req_value(request, _application_id);
318 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
319 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
320 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
322 HS_AfmMainProxy afm_proxy;
323 afm_proxy.start(g_hs_instance, request, id);
326 ret = AFB_EVENT_BAD_REQUEST;
331 ret = AFB_EVENT_BAD_REQUEST;
335 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
338 struct json_object *res = json_object_new_object();
339 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
341 afb_req_success(request, res, "afb_event_push event [showWindow]");
349 * - request : the request
355 static void hideWindow(afb_req_t request)
358 const char* value = afb_req_value(request, _application_id);
360 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
363 ret = AFB_EVENT_BAD_REQUEST;
367 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
370 struct json_object *res = json_object_new_object();
371 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
373 afb_req_success(request, res, "afb_event_push event [hideWindow]");
378 * replyShowWindow event
381 * - request : the request
387 static void replyShowWindow(afb_req_t request)
390 const char* value = afb_req_value(request, _application_id);
392 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
395 ret = AFB_EVENT_BAD_REQUEST;
399 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
402 struct json_object *res = json_object_new_object();
403 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
405 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
410 * showNotification event
412 * the contents to homescreen which display at top area.
415 * - request : the request
421 static void showNotification(afb_req_t request)
423 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
425 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
428 struct json_object *res = json_object_new_object();
429 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
431 afb_req_success(request, res, "afb_event_push event [showNotification]");
436 * showInformation event
438 * the contents to homescreen which display at bottom area.
441 * - request : the request
447 static void showInformation(afb_req_t request)
449 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
451 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
454 struct json_object *res = json_object_new_object();
455 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
457 afb_req_success(request, res, "afb_event_push event [showInformation]");
465 * - request : the request
471 static void getRunnables(afb_req_t request)
473 struct json_object* j_runnable = json_object_new_array();
474 g_hs_instance->app_info->getRunnables(&j_runnable);
476 /*create response json object*/
477 struct json_object *res = json_object_new_object();
478 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
479 json_object_object_add(res, _keyData, j_runnable);
480 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
484 * array of the verbs exported to afb-daemon
486 static const afb_verb_t verbs[]= {
487 /* VERB'S NAME FUNCTION TO CALL */
488 { .verb="ping", .callback=pingSample },
489 { .verb="tap_shortcut", .callback=tap_shortcut },
490 { .verb="showWindow", .callback=showWindow },
491 { .verb="hideWindow", .callback=hideWindow },
492 { .verb="replyShowWindow", .callback=replyShowWindow },
493 { .verb="on_screen_message", .callback=on_screen_message },
494 { .verb="on_screen_reply", .callback=on_screen_reply },
495 { .verb="subscribe", .callback=subscribe },
496 { .verb="unsubscribe", .callback=unsubscribe },
497 { .verb="showNotification", .callback=showNotification },
498 { .verb="showInformation", .callback=showInformation },
499 { .verb="getRunnables", .callback=getRunnables },
500 {NULL } /* marker for end of the array */
504 * homescreen binding preinit function
507 * - api : the api serving the request
513 static int preinit(afb_api_t api)
516 AFB_DEBUG("binding preinit (was register)");
521 * homescreen binding init function
524 * - api : the api serving the request
530 static int init(afb_api_t api)
532 AFB_DEBUG("binding init");
534 if(g_hs_instance != nullptr) {
535 AFB_WARNING( "g_hs_instance isn't null.");
536 delete g_hs_instance->client_manager;
537 delete g_hs_instance->app_info;
538 delete g_hs_instance;
539 g_hs_instance = nullptr;
541 g_hs_instance = new hs_instance();
542 if(g_hs_instance == nullptr) {
546 return g_hs_instance->init(api);
550 * homescreen binding event function
553 * - api : the api serving the request
554 * - event : event name
555 * - object : event json object
561 static void onevent(afb_api_t api, const char *event, struct json_object *object)
563 AFB_INFO("on_event %s", event);
564 g_hs_instance->onEvent(api, event, object);
567 const afb_binding_t afbBindingExport = {
569 .specification = NULL,