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("client_manager is nullptr.");
59 client_manager->init();
61 if(app_info == nullptr) {
62 AFB_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)
99 const char* value = afb_req_value(request, _application_id);
101 AFB_INFO("request appid = %s.", value);
102 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
103 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
104 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
105 HS_AfmMainProxy afm_proxy;
106 afm_proxy.start(request, id);
111 ret = AFB_EVENT_BAD_REQUEST;
115 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
118 struct json_object *res = json_object_new_object();
119 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
121 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
126 * HomeScreen OnScreen message
130 * - display_message : message for display
136 static void on_screen_message (afb_req_t request)
138 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
140 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
143 struct json_object *res = json_object_new_object();
144 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
146 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
151 * HomeScreen OnScreen Reply
155 * - reply_message : message for reply
161 static void on_screen_reply (afb_req_t request)
163 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__);
165 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
168 struct json_object *res = json_object_new_object();
169 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
171 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
179 * - event : Event name. Event list is written in libhomescreen.cpp
185 static void subscribe(afb_req_t request)
188 std::string req_appid = std::move(get_application_id(request));
189 if(!req_appid.empty()) {
190 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
193 ret = AFB_EVENT_BAD_REQUEST;
197 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
200 struct json_object *res = json_object_new_object();
201 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
203 afb_req_success_f(request, res, "homescreen binder subscribe.");
211 * - event : Event name. Event list is written in libhomescreen.cpp
217 static void unsubscribe(afb_req_t request)
220 std::string req_appid = std::move(get_application_id(request));
221 if(!req_appid.empty()) {
222 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, req_appid.c_str());
225 ret = AFB_EVENT_BAD_REQUEST;
229 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __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_f(request, res, "homescreen binder unsubscribe success.");
243 * - request : the request
249 static void showWindow(afb_req_t request)
252 const char* value = afb_req_value(request, _application_id);
254 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
255 if(ret == AFB_REQ_NOT_STARTED_APPLICATION) {
256 std::string id = g_hs_instance->app_info->getAppProperty(value, _keyId);
257 HS_AfmMainProxy afm_proxy;
258 afm_proxy.start(request, id);
263 ret = AFB_EVENT_BAD_REQUEST;
267 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
270 struct json_object *res = json_object_new_object();
271 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
273 afb_req_success(request, res, "afb_event_push event [showWindow]");
281 * - request : the request
287 static void hideWindow(afb_req_t request)
290 const char* value = afb_req_value(request, _application_id);
292 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
295 ret = AFB_EVENT_BAD_REQUEST;
299 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
302 struct json_object *res = json_object_new_object();
303 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
305 afb_req_success(request, res, "afb_event_push event [hideWindow]");
310 * replyShowWindow event
313 * - request : the request
319 static void replyShowWindow(afb_req_t request)
322 const char* value = afb_req_value(request, _application_id);
324 ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, value);
327 ret = AFB_EVENT_BAD_REQUEST;
331 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
334 struct json_object *res = json_object_new_object();
335 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
337 afb_req_success(request, res, "afb_event_push event [replyShowWindow]");
342 * showNotification event
344 * the contents to homescreen which display at top area.
347 * - request : the request
353 static void showNotification(afb_req_t request)
355 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
357 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
360 struct json_object *res = json_object_new_object();
361 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
363 afb_req_success(request, res, "afb_event_push event [showNotification]");
368 * showInformation event
370 * the contents to homescreen which display at bottom area.
373 * - request : the request
379 static void showInformation(afb_req_t request)
381 int ret = g_hs_instance->client_manager->handleRequest(request, __FUNCTION__, "homescreen");
383 afb_req_fail_f(request, "failed", "called %s, Unknown parameter", __FUNCTION__);
386 struct json_object *res = json_object_new_object();
387 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
389 afb_req_success(request, res, "afb_event_push event [showInformation]");
397 * - request : the request
403 static void getRunnables(afb_req_t request)
405 struct json_object* j_runnable = json_object_new_array();
406 g_hs_instance->app_info->getRunnables(&j_runnable);
408 /*create response json object*/
409 struct json_object *res = json_object_new_object();
410 hs_add_object_to_json_object_func(res, __FUNCTION__, 2, _error, 0);
411 json_object_object_add(res, _keyData, j_runnable);
412 afb_req_success_f(request, res, "homescreen binder unsubscribe success.");
416 * array of the verbs exported to afb-daemon
418 static const afb_verb_t verbs[]= {
419 /* VERB'S NAME FUNCTION TO CALL */
420 { .verb="ping", .callback=pingSample },
421 { .verb="tap_shortcut", .callback=tap_shortcut },
422 { .verb="showWindow", .callback=showWindow },
423 { .verb="hideWindow", .callback=hideWindow },
424 { .verb="replyShowWindow", .callback=replyShowWindow },
425 { .verb="on_screen_message", .callback=on_screen_message },
426 { .verb="on_screen_reply", .callback=on_screen_reply },
427 { .verb="subscribe", .callback=subscribe },
428 { .verb="unsubscribe", .callback=unsubscribe },
429 { .verb="showNotification", .callback=showNotification },
430 { .verb="showInformation", .callback=showInformation },
431 { .verb="getRunnables", .callback=getRunnables },
432 {NULL } /* marker for end of the array */
436 * homescreen binding preinit function
439 * - api : the api serving the request
445 static int preinit(afb_api_t api)
447 AFB_DEBUG("binding preinit (was register)");
452 * homescreen binding init function
455 * - api : the api serving the request
461 static int init(afb_api_t api)
463 AFB_DEBUG("binding init");
465 if(g_hs_instance != nullptr) {
466 AFB_WARNING( "g_hs_instance isn't null.");
467 delete g_hs_instance->client_manager;
468 delete g_hs_instance->app_info;
469 delete g_hs_instance;
470 g_hs_instance = nullptr;
472 g_hs_instance = new hs_instance();
473 if(g_hs_instance == nullptr) {
477 return g_hs_instance->init(api);
481 * homescreen binding event function
484 * - api : the api serving the request
485 * - event : event name
486 * - object : event json object
492 static void onevent(afb_api_t api, const char *event, struct json_object *object)
494 AFB_DEBUG("on_event %s", event);
495 g_hs_instance->app_info->onEvent(api, event, object);
498 const afb_binding_t afbBindingExport = {
500 .specification = NULL,