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.
20 #include "hs-helper.h"
21 #include "hmi-debug.h"
23 #define COMMAND_EVENT_NUM 4
24 #define EVENT_SUBSCRIBE_ERROR_CODE 100
26 /* To Do hash table is better */
29 struct afb_event event;
32 static struct event event_list[COMMAND_EVENT_NUM];
34 static const char _error[] = "error";
35 static const char _application_name[] = "application_name";
36 static const char _display_message[] = "display_message";
37 static const char _reply_message[] = "reply_message";
40 ********** Method of HomeScreen Service (API) **********
43 static void pingSample(struct afb_req request)
45 static int pingcount = 0;
46 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
47 HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
52 * tap_shortcut notify for homescreen
53 * When Shortcut area is tapped, notify these applciations
57 * - application_name : application name
63 static void tap_shortcut (struct afb_req request)
65 HMI_NOTICE("homescreen-service","called.");
68 const char* value = afb_req_value(request, _application_name);
71 HMI_NOTICE("homescreen-service","request params = %s.", value);
73 struct json_object* push_obj = json_object_new_object();
74 hs_add_object_to_json_object_str( push_obj, 2,
75 _application_name, value);
76 afb_event_push(event_list[hs_search_event_name_index(__FUNCTION__)].event, push_obj);
78 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
82 // response to HomeScreen
83 struct json_object *res = json_object_new_object();
84 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
86 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
90 * HomeScreen OnScreen message
94 * - display_message : message for display
100 static void on_screen_message (struct afb_req request)
102 HMI_NOTICE("homescreen-service","called.");
105 const char* value = afb_req_value(request, _display_message);
108 HMI_NOTICE("homescreen-service","request params = %s.", value);
110 struct json_object* push_obj = json_object_new_object();
111 hs_add_object_to_json_object_str( push_obj, 2,
112 _display_message, value);
113 afb_event_push(event_list[hs_search_event_name_index(__FUNCTION__)].event, push_obj);
115 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
119 // response to HomeScreen
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 [on_screen_message]");
127 * HomeScreen OnScreen Reply
131 * - reply_message : message for reply
137 static void on_screen_reply (struct afb_req request)
139 HMI_NOTICE("homescreen-service","called.");
142 const char* value = afb_req_value(request, _reply_message);
145 HMI_NOTICE("homescreen-service","request params = %s.", value);
147 struct json_object* push_obj = json_object_new_object();
148 hs_add_object_to_json_object_str( push_obj, 2,
149 _reply_message, value);
150 afb_event_push(event_list[hs_search_event_name_index(__FUNCTION__)].event, push_obj);
152 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
156 // response to HomeScreen
157 struct json_object *res = json_object_new_object();
158 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
160 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
167 * - event : Event name. Event list is written in libhomescreen.cpp
173 static void subscribe(struct afb_req request)
175 const char *value = afb_req_value(request, "event");
176 HMI_NOTICE("homescreen-service","value is %s", value);
179 int index = hs_search_event_name_index(value);
182 HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
183 ret = EVENT_SUBSCRIBE_ERROR_CODE;
187 afb_req_subscribe(request, event_list[index].event);
191 HMI_NOTICE("homescreen-service","Please input event name");
192 ret = EVENT_SUBSCRIBE_ERROR_CODE;
194 /*create response json object*/
195 struct json_object *res = json_object_new_object();
196 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
198 afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
205 * - event : Event name. Event list is written in libhomescreen.cpp
211 static void unsubscribe(struct afb_req request)
213 const char *value = afb_req_value(request, "event");
214 HMI_NOTICE("homescreen-service","value is %s", value);
217 int index = hs_search_event_name_index(value);
220 HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
221 ret = EVENT_SUBSCRIBE_ERROR_CODE;
225 afb_req_unsubscribe(request, event_list[index].event);
229 HMI_NOTICE("homescreen-service","Please input event name");
230 ret = EVENT_SUBSCRIBE_ERROR_CODE;
232 /*create response json object*/
233 struct json_object *res = json_object_new_object();
234 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
236 afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
240 * array of the verbs exported to afb-daemon
242 static const struct afb_verb_v2 verbs[]= {
243 /* VERB'S NAME FUNCTION TO CALL authorisation some info SESSION MANAGEMENT */
244 { .verb = "ping", .callback = pingSample, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
245 { .verb = "tap_shortcut", .callback = tap_shortcut, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
246 { .verb = "on_screen_message", .callback = on_screen_message, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
247 { .verb = "on_screen_reply", .callback = on_screen_reply, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
248 { .verb = "subscribe", .callback = subscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
249 { .verb = "unsubscribe", .callback = unsubscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
250 {NULL } /* marker for end of the array */
254 * homescreen binding preinit function
265 HMI_NOTICE("homescreen-service","binding preinit (was register)");
270 * homescreen binding init function
281 HMI_NOTICE("homescreen-service","binding init");
283 for(int i = 0; i < COMMAND_EVENT_NUM; ++i) {
284 event_list[i].name = evlist[i];
285 event_list[i].event = afb_daemon_make_event(evlist[i]);
292 * homescreen binding event function
295 * - event : event name
296 * - object : event json object
302 static void onevent(const char *event, struct json_object *object)
304 HMI_NOTICE("homescreen-service","on_event %s", event);
307 const struct afb_binding_v2 afbBindingV2 = {
309 .specification = NULL,