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"
26 #define EVENT_SUBSCRIBE_ERROR_CODE 100
28 const char _error[] = "error";
29 const char _application_name[] = "application_name";
30 const char _display_message[] = "display_message";
31 const char _reply_message[] = "reply_message";
33 static HS_ClientManager* g_client_manager = HS_ClientManager::instance();
36 ********** Method of HomeScreen Service (API) **********
39 static void pingSample(struct afb_req request)
41 static int pingcount = 0;
42 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
43 HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
48 * tap_shortcut notify for homescreen
49 * When Shortcut area is tapped, notify these applciations
53 * - application_name : application name
59 static void tap_shortcut (struct afb_req request)
61 HMI_NOTICE("homescreen-service","called.");
64 const char* value = afb_req_value(request, _application_name);
66 HMI_NOTICE("homescreen-service","request params = %s.", value);
67 // first step get appid from appname, next step change appname to appid
68 std::string appid(value);
69 std::transform(appid.begin(), appid.end(), appid.begin(), ::tolower);
70 HS_Client* client = g_client_manager->find(appid);
71 if(client != nullptr) {
72 if(client->tap_shortcut(value) != 0) {
73 afb_req_fail_f(request, "afb_event_push failed", "called %s.", __FUNCTION__);
78 // app is not started, do nothing
81 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
85 // response to HomeScreen
86 struct json_object *res = json_object_new_object();
87 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
89 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
93 * HomeScreen OnScreen message
97 * - display_message : message for display
103 static void on_screen_message (struct afb_req request)
105 HMI_NOTICE("homescreen-service","called.");
108 const char* value = afb_req_value(request, _display_message);
111 HMI_NOTICE("homescreen-service","request params = %s.", value);
112 for(auto m : g_client_manager->getAllClient()) {
113 if(m->on_screen_message(request, value) != 0) {
114 afb_req_fail_f(request, "afb_event_push failed", "called %s.", __FUNCTION__);
119 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
123 // response to HomeScreen
124 struct json_object *res = json_object_new_object();
125 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
127 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
131 * HomeScreen OnScreen Reply
135 * - reply_message : message for reply
141 static void on_screen_reply (struct afb_req request)
143 HMI_NOTICE("homescreen-service","called.");
146 const char* value = afb_req_value(request, _reply_message);
149 HMI_NOTICE("homescreen-service","request params = %s.", value);
150 for(auto m : g_client_manager->getAllClient()) {
151 if(m->on_screen_reply(request, value) != 0) {
152 afb_req_fail_f(request, "afb_event_push failed", "called %s.", __FUNCTION__);
157 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
161 // response to HomeScreen
162 struct json_object *res = json_object_new_object();
163 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
165 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
172 * - event : Event name. Event list is written in libhomescreen.cpp
178 static void subscribe(struct afb_req request)
180 const char *value = afb_req_value(request, "event");
181 HMI_NOTICE("homescreen-service","value is %s", value);
184 std::string appid(afb_req_get_application_id(request));
185 std::transform(appid.begin(), appid.end(), appid.begin(), ::tolower);
186 if(g_client_manager->getClient(request, appid)->subscribe(request, value) != 0) {
187 afb_req_fail_f(request, "afb_req_subscribe failed", "called %s.", __FUNCTION__);
192 HMI_NOTICE("homescreen-service","Please input event name");
193 ret = EVENT_SUBSCRIBE_ERROR_CODE;
195 /*create response json object*/
196 struct json_object *res = json_object_new_object();
197 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
199 afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
206 * - event : Event name. Event list is written in libhomescreen.cpp
212 static void unsubscribe(struct afb_req request)
214 const char *value = afb_req_value(request, "event");
215 HMI_NOTICE("homescreen-service","value is %s", value);
218 std::string appid(afb_req_get_application_id(request));
219 std::transform(appid.begin(), appid.end(), appid.begin(), ::tolower);
220 HS_Client* client = g_client_manager->find(appid);
221 if(client != nullptr) {
222 if(client->unsubscribe(request, value) != 0) {
223 afb_req_fail_f(request, "afb_req_unsubscribe failed", "called %s.", __FUNCTION__);
228 HMI_NOTICE("homescreen-service","not find app's client, unsubscribe failed");
229 ret = EVENT_SUBSCRIBE_ERROR_CODE;
233 HMI_NOTICE("homescreen-service","Please input event name");
234 ret = EVENT_SUBSCRIBE_ERROR_CODE;
236 /*create response json object*/
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 event name [%s]", value);
244 * array of the verbs exported to afb-daemon
246 static const struct afb_verb_v2 verbs[]= {
247 /* VERB'S NAME FUNCTION TO CALL authorisation some info SESSION MANAGEMENT */
248 { .verb = "ping", .callback = pingSample, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
249 { .verb = "tap_shortcut", .callback = tap_shortcut, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
250 { .verb = "on_screen_message", .callback = on_screen_message, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
251 { .verb = "on_screen_reply", .callback = on_screen_reply, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
252 { .verb = "subscribe", .callback = subscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
253 { .verb = "unsubscribe", .callback = unsubscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
254 {NULL } /* marker for end of the array */
258 * homescreen binding preinit function
269 HMI_NOTICE("homescreen-service","binding preinit (was register)");
274 * homescreen binding init function
285 HMI_NOTICE("homescreen-service","binding init");
287 g_client_manager->init();
293 * homescreen binding event function
296 * - event : event name
297 * - object : event json object
303 static void onevent(const char *event, struct json_object *object)
305 HMI_NOTICE("homescreen-service","on_event %s", event);
308 const struct afb_binding_v2 afbBindingV2 = {
310 .specification = NULL,