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 "hs-helper.h"
23 #include "hmi-debug.h"
24 #include "hs-clientmanager.h"
25 #include "hs-appinfo.h"
28 const char _error[] = "error";
29 const char _application_id[] = "application_id";
30 const char _display_message[] = "display_message";
31 const char _reply_message[] = "reply_message";
32 const char _keyData[] = "data";
33 const char _keyId[] = "id";
36 HS_ClientManager *client_manager; // the connection session manager
37 HS_AppInfo *app_info; // application info
39 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
40 int init(afb_api_t api);
47 * - api : the api serving the request
54 int hs_instance::init(afb_api_t api)
56 if(client_manager == nullptr) {
57 HMI_ERROR("homescreen-service","FATAL ERROR: client_manager is nullptr.");
60 client_manager->init();
62 if(app_info == nullptr) {
63 HMI_ERROR("homescreen-service","FATAL ERROR: app_info is nullptr.");
71 static struct hs_instance *g_hs_instance;
74 ********** Method of HomeScreen Service (API) **********
77 static void pingSample(afb_req_t request)
79 static int pingcount = 0;
80 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
81 HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
86 * tap_shortcut notify for homescreen
87 * When Shortcut area is tapped, notify these applciations
91 * - application_id : application id
97 static void tap_shortcut (afb_req_t request)
99 HMI_NOTICE("homescreen-service","called.");
101 const char* value = afb_req_value(request, _application_id);
103 HMI_NOTICE("homescreen-service","request appid = %s.", value);
104 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
105 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
106 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
107 HS_AfmMainProxy afm_proxy;
108 afm_proxy.start(request, id);
113 ret = AFB_EVENT_BAD_REQUEST;
117 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
120 struct json_object *res = json_object_new_object();
121 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
123 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
128 * HomeScreen OnScreen message
132 * - display_message : message for display
138 static void on_screen_message (afb_req_t request)
140 HMI_NOTICE("homescreen-service","called.");
141 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
143 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
146 struct json_object *res = json_object_new_object();
147 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
149 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
154 * HomeScreen OnScreen Reply
158 * - reply_message : message for reply
164 static void on_screen_reply (afb_req_t request)
166 HMI_NOTICE("homescreen-service","called.");
167 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
169 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
172 struct json_object *res = json_object_new_object();
173 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
175 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
183 * - event : Event name. Event list is written in libhomescreen.cpp
189 static void subscribe(afb_req_t request)
191 HMI_NOTICE("homescreen-service","called.");
193 std::string req_appid = std::move(get_application_id(request));
194 if(!req_appid.empty()) {
195 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
198 ret = AFB_EVENT_BAD_REQUEST;
202 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
205 struct json_object *res = json_object_new_object();
206 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
208 afb_req_success_f(request, res, "homescreen binder subscribe.");
216 * - event : Event name. Event list is written in libhomescreen.cpp
222 static void unsubscribe(afb_req_t request)
224 HMI_NOTICE("homescreen-service","called.");
226 std::string req_appid = std::move(get_application_id(request));
227 if(!req_appid.empty()) {
228 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
231 ret = AFB_EVENT_BAD_REQUEST;
235 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
238 struct json_object *res = json_object_new_object();
239 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
241 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
249 * - request : the request
255 static void showWindow(afb_req_t request)
257 HMI_NOTICE("homescreen-service","called.");
259 const char* value = afb_req_value(request, _application_id);
261 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
262 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
263 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
264 HS_AfmMainProxy afm_proxy;
265 afm_proxy.start(request, id);
270 ret = AFB_EVENT_BAD_REQUEST;
274 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
277 struct json_object *res = json_object_new_object();
278 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
280 afb_req_success(request, res, "afb_event_push event [showWindow]");
288 * - request : the request
294 static void hideWindow(afb_req_t request)
296 HMI_NOTICE("homescreen-service","called.");
298 const char* value = afb_req_value(request, _application_id);
300 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
303 ret = AFB_EVENT_BAD_REQUEST;
307 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __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(request, res, "afb_event_push event [hideWindow]");
318 * replyShowWindow event
321 * - request : the request
327 static void replyShowWindow(afb_req_t request)
329 HMI_NOTICE("homescreen-service","called.");
331 const char* value = afb_req_value(request, _application_id);
333 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
336 ret = AFB_EVENT_BAD_REQUEST;
340 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
343 struct json_object *res = json_object_new_object();
344 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
346 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
351 * showNotification event
353 * the contents to homescreen which display at top area.
356 * - request : the request
362 static void showNotification(afb_req_t request)
364 HMI_NOTICE("homescreen-service","called.");
365 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
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 [showNotification]");
378 * showInformation event
380 * the contents to homescreen which display at bottom area.
383 * - request : the request
389 static void showInformation(afb_req_t request)
391 HMI_NOTICE("homescreen-service","called.");
392 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
394 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
397 struct json_object *res = json_object_new_object();
398 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
400 afb_req_success(request, res, "afb_event_push event [showInformation]");
408 * - request : the request
414 static void getRunnables(afb_req_t request)
416 HMI_NOTICE("homescreen-service","called.");
417 struct json_object* j_runnable = json_object_new_array();
418 g_hs_instance->app_info->getRunnables(&j_runnable);
420 /*create response json object*/
421 struct json_object *res = json_object_new_object();
422 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
423 json_object_object_add(res, _keyData, j_runnable);
424 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
428 * array of the verbs exported to afb-daemon
430 static const afb_verb_t verbs[]= {
431 /* VERB'S NAME FUNCTION TO CALL */
432 { .verb="ping", .callback=pingSample },
433 { .verb="tap_shortcut", .callback=tap_shortcut },
434 { .verb="showWindow", .callback=showWindow },
435 { .verb="hideWindow", .callback=hideWindow },
436 { .verb="replyShowWindow", .callback=replyShowWindow },
437 { .verb="on_screen_message", .callback=on_screen_message },
438 { .verb="on_screen_reply", .callback=on_screen_reply },
439 { .verb="subscribe", .callback=subscribe },
440 { .verb="unsubscribe", .callback=unsubscribe },
441 { .verb="showNotification", .callback=showNotification },
442 { .verb="showInformation", .callback=showInformation },
443 { .verb="getRunnables", .callback=getRunnables },
444 {NULL } /* marker for end of the array */
448 * homescreen binding preinit function
451 * - api : the api serving the request
457 static int preinit(afb_api_t api)
459 HMI_NOTICE("homescreen-service","binding preinit (was register)");
464 * homescreen binding init function
467 * - api : the api serving the request
473 static int init(afb_api_t api)
475 HMI_NOTICE("homescreen-service","binding init");
477 if(g_hs_instance != nullptr) {
478 HMI_WARNING("homescreen-service", "g_hs_instance isn't null.");
479 delete g_hs_instance->client_manager;
480 delete g_hs_instance->app_info;
481 delete g_hs_instance;
482 g_hs_instance = nullptr;
484 g_hs_instance = new hs_instance();
485 if(g_hs_instance == nullptr) {
486 HMI_ERROR("homescreen-service", "Fatal Error: new g_hs_instance failed.");
490 return g_hs_instance->init(api);
494 * homescreen binding event function
497 * - api : the api serving the request
498 * - event : event name
499 * - object : event json object
505 static void onevent(afb_api_t api, const char *event, struct json_object *object)
507 HMI_NOTICE("homescreen-service","on_event %s", event);
508 g_hs_instance->app_info->onEvent(api, event, object);
511 const afb_binding_t afbBindingExport = {
513 .specification = NULL,