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 "hs-clientmanager.h"
24 #include "hs-appinfo.h"
27 const char _error[] = "error";
28 const char _application_id[] = "application_id";
29 const char _display_message[] = "display_message";
30 const char _reply_message[] = "reply_message";
31 const char _keyData[] = "data";
32 const char _keyId[] = "id";
35 HS_ClientManager *client_manager; // the connection session manager
36 HS_AppInfo *app_info; // application info
38 hs_instance() : client_manager(HS_ClientManager::instance()), app_info(HS_AppInfo::instance()) {}
39 int init(afb_api_t api);
46 * - api : the api serving the request
53 int hs_instance::init(afb_api_t api)
55 if(client_manager == nullptr) {
56 AFB_ERROR("FATAL ERROR: client_manager is nullptr.");
59 client_manager->init();
61 if(app_info == nullptr) {
62 AFB_ERROR("FATAL ERROR: app_info is nullptr.");
70 static struct hs_instance *g_hs_instance;
73 ********** Method of HomeScreen Service (API) **********
76 static void pingSample(afb_req_t request)
78 static int pingcount = 0;
79 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
80 AFB_DEBUG("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
85 * tap_shortcut notify for homescreen
86 * When Shortcut area is tapped, notify these applciations
90 * - application_id : application id
96 static void tap_shortcut (afb_req_t request)
100 const char* value = afb_req_value(request, _application_id);
102 AFB_INFO("request appid = %s.", value);
103 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
104 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
105 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
106 HS_AfmMainProxy afm_proxy;
107 afm_proxy.start(request, id);
112 ret = AFB_EVENT_BAD_REQUEST;
116 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
119 struct json_object *res = json_object_new_object();
120 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
122 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
127 * HomeScreen OnScreen message
131 * - display_message : message for display
137 static void on_screen_message (afb_req_t request)
139 AFB_DEBUG("called.");
140 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
142 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
145 struct json_object *res = json_object_new_object();
146 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
148 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
153 * HomeScreen OnScreen Reply
157 * - reply_message : message for reply
163 static void on_screen_reply (afb_req_t request)
165 AFB_DEBUG("called.");
166 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
168 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
171 struct json_object *res = json_object_new_object();
172 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
174 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
182 * - event : Event name. Event list is written in libhomescreen.cpp
188 static void subscribe(afb_req_t request)
190 AFB_DEBUG("called.");
192 std::string req_appid = std::move(get_application_id(request));
193 if(!req_appid.empty()) {
194 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
197 ret = AFB_EVENT_BAD_REQUEST;
201 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
204 struct json_object *res = json_object_new_object();
205 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
207 afb_req_success_f(request, res, "homescreen binder subscribe.");
215 * - event : Event name. Event list is written in libhomescreen.cpp
221 static void unsubscribe(afb_req_t request)
223 AFB_DEBUG("called.");
225 std::string req_appid = std::move(get_application_id(request));
226 if(!req_appid.empty()) {
227 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
230 ret = AFB_EVENT_BAD_REQUEST;
234 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
237 struct json_object *res = json_object_new_object();
238 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
240 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
248 * - request : the request
254 static void showWindow(afb_req_t request)
256 AFB_DEBUG("called.");
258 const char* value = afb_req_value(request, _application_id);
260 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
261 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
262 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
263 HS_AfmMainProxy afm_proxy;
264 afm_proxy.start(request, id);
269 ret = AFB_EVENT_BAD_REQUEST;
273 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
276 struct json_object *res = json_object_new_object();
277 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
279 afb_req_success(request, res, "afb_event_push event [showWindow]");
287 * - request : the request
293 static void hideWindow(afb_req_t request)
295 AFB_DEBUG("called.");
297 const char* value = afb_req_value(request, _application_id);
299 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
302 ret = AFB_EVENT_BAD_REQUEST;
306 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
309 struct json_object *res = json_object_new_object();
310 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
312 afb_req_success(request, res, "afb_event_push event [hideWindow]");
317 * replyShowWindow event
320 * - request : the request
326 static void replyShowWindow(afb_req_t request)
328 AFB_DEBUG("called.");
330 const char* value = afb_req_value(request, _application_id);
332 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
335 ret = AFB_EVENT_BAD_REQUEST;
339 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
342 struct json_object *res = json_object_new_object();
343 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
345 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
350 * showNotification event
352 * the contents to homescreen which display at top area.
355 * - request : the request
361 static void showNotification(afb_req_t request)
363 AFB_DEBUG("called.");
364 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
366 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
369 struct json_object *res = json_object_new_object();
370 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
372 afb_req_success(request, res, "afb_event_push event [showNotification]");
377 * showInformation event
379 * the contents to homescreen which display at bottom area.
382 * - request : the request
388 static void showInformation(afb_req_t request)
390 AFB_DEBUG("called.");
391 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
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 [showInformation]");
407 * - request : the request
413 static void getRunnables(afb_req_t request)
415 AFB_DEBUG("called.");
416 struct json_object* j_runnable = json_object_new_array();
417 g_hs_instance->app_info->getRunnables(&j_runnable);
419 /*create response json object*/
420 struct json_object *res = json_object_new_object();
421 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
422 json_object_object_add(res, _keyData, j_runnable);
423 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
427 * array of the verbs exported to afb-daemon
429 static const afb_verb_t verbs[]= {
430 /* VERB'S NAME FUNCTION TO CALL */
431 { .verb="ping", .callback=pingSample },
432 { .verb="tap_shortcut", .callback=tap_shortcut },
433 { .verb="showWindow", .callback=showWindow },
434 { .verb="hideWindow", .callback=hideWindow },
435 { .verb="replyShowWindow", .callback=replyShowWindow },
436 { .verb="on_screen_message", .callback=on_screen_message },
437 { .verb="on_screen_reply", .callback=on_screen_reply },
438 { .verb="subscribe", .callback=subscribe },
439 { .verb="unsubscribe", .callback=unsubscribe },
440 { .verb="showNotification", .callback=showNotification },
441 { .verb="showInformation", .callback=showInformation },
442 { .verb="getRunnables", .callback=getRunnables },
443 {NULL } /* marker for end of the array */
447 * homescreen binding preinit function
450 * - api : the api serving the request
456 static int preinit(afb_api_t api)
458 AFB_DEBUG("binding preinit (was register)");
463 * homescreen binding init function
466 * - api : the api serving the request
472 static int init(afb_api_t api)
474 AFB_DEBUG("binding init");
476 if(g_hs_instance != nullptr) {
477 AFB_WARNING( "g_hs_instance isn't null.");
478 delete g_hs_instance->client_manager;
479 delete g_hs_instance->app_info;
480 delete g_hs_instance;
481 g_hs_instance = nullptr;
483 g_hs_instance = new hs_instance();
484 if(g_hs_instance == nullptr) {
485 AFB_ERROR( "Fatal Error: new g_hs_instance failed.");
489 return g_hs_instance->init(api);
493 * homescreen binding event function
496 * - api : the api serving the request
497 * - event : event name
498 * - object : event json object
504 static void onevent(afb_api_t api, const char *event, struct json_object *object)
506 AFB_DEBUG("on_event %s", event);
507 g_hs_instance->app_info->onEvent(api, event, object);
510 const afb_binding_t afbBindingExport = {
512 .specification = NULL,