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 struct afb_event ev_tap_shortcut;
35 static struct afb_event ev_on_screen_message;
36 static struct afb_event ev_on_screen_reply;
37 static struct afb_event ev_reserved;
39 static const char _error[] = "error";
41 static const char _application_name[] = "application_name";
42 static const char _display_message[] = "display_message";
43 static const char _reply_message[] = "reply_message";
46 ********** Method of HomeScreen Service (API) **********
49 static void pingSample(struct afb_req request)
51 static int pingcount = 0;
52 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
53 HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
58 * tap_shortcut notify for homescreen
59 * When Shortcut area is tapped, notify these applciations
63 * - application_name : application name
69 static void tap_shortcut (struct afb_req request)
71 HMI_NOTICE("homescreen-service","called.");
74 const char* value = afb_req_value(request, _application_name);
77 HMI_NOTICE("homescreen-service","request params = %s.", value);
79 struct json_object* push_obj = json_object_new_object();
80 hs_add_object_to_json_object_str( push_obj, 2,
81 _application_name, value);
82 afb_event_push(ev_tap_shortcut, push_obj);
84 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
88 // response to HomeScreen
89 struct json_object *res = json_object_new_object();
90 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
92 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
96 * HomeScreen OnScreen message
100 * - display_message : message for display
106 static void on_screen_message (struct afb_req request)
108 HMI_NOTICE("homescreen-service","called.");
111 const char* value = afb_req_value(request, _display_message);
114 HMI_NOTICE("homescreen-service","request params = %s.", value);
116 struct json_object* push_obj = json_object_new_object();
117 hs_add_object_to_json_object_str( push_obj, 2,
118 _display_message, value);
119 afb_event_push(ev_on_screen_message, push_obj);
121 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
125 // response to HomeScreen
126 struct json_object *res = json_object_new_object();
127 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
129 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
133 * HomeScreen OnScreen Reply
137 * - reply_message : message for reply
143 static void on_screen_reply (struct afb_req request)
145 HMI_NOTICE("homescreen-service","called.");
148 const char* value = afb_req_value(request, _reply_message);
151 HMI_NOTICE("homescreen-service","request params = %s.", value);
153 struct json_object* push_obj = json_object_new_object();
154 hs_add_object_to_json_object_str( push_obj, 2,
155 _reply_message, value);
156 afb_event_push(ev_on_screen_reply, push_obj);
158 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
162 // response to HomeScreen
163 struct json_object *res = json_object_new_object();
164 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
166 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
173 * - event : Event name. Event list is written in libhomescreen.cpp
181 static void subscribe(struct afb_req request)
183 const char *value = afb_req_value(request, "event");
184 HMI_NOTICE("homescreen-service","value is %s", value);
187 int index = hs_search_event_name_index(value);
190 HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
191 ret = EVENT_SUBSCRIBE_ERROR_CODE;
195 afb_req_subscribe(request, *event_list[index].event);
199 HMI_NOTICE("homescreen-service","Please input event name");
200 ret = EVENT_SUBSCRIBE_ERROR_CODE;
202 /*create response json object*/
203 struct json_object *res = json_object_new_object();
204 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
206 afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
213 * - event : Event name. Event list is written in libhomescreen.cpp
221 static void unsubscribe(struct afb_req request)
223 const char *value = afb_req_value(request, "event");
224 HMI_NOTICE("homescreen-service","value is %s", value);
227 int index = hs_search_event_name_index(value);
230 HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
231 ret = EVENT_SUBSCRIBE_ERROR_CODE;
235 afb_req_unsubscribe(request, *event_list[index].event);
239 HMI_NOTICE("homescreen-service","Please input event name");
240 ret = EVENT_SUBSCRIBE_ERROR_CODE;
242 /*create response json object*/
243 struct json_object *res = json_object_new_object();
244 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
246 afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
250 * array of the verbs exported to afb-daemon
252 static const struct afb_verb_v2 verbs[]= {
253 /* VERB'S NAME FUNCTION TO CALL authorisation some info SESSION MANAGEMENT */
254 { .verb = "ping", .callback = pingSample, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
255 { .verb = "tap_shortcut", .callback = tap_shortcut, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
256 { .verb = "on_screen_message", .callback = on_screen_message, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
257 { .verb = "on_screen_reply", .callback = on_screen_reply, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
258 { .verb = "subscribe", .callback = subscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
259 { .verb = "unsubscribe", .callback = unsubscribe, .auth = NULL, .info = NULL, .session = AFB_SESSION_NONE },
260 {NULL } /* marker for end of the array */
265 HMI_NOTICE("homescreen-service","binding preinit (was register)");
271 HMI_NOTICE("homescreen-service","binding init");
273 ev_tap_shortcut = afb_daemon_make_event(evlist[0]);
274 ev_on_screen_message = afb_daemon_make_event(evlist[1]);
275 ev_on_screen_reply = afb_daemon_make_event(evlist[2]);
276 ev_reserved = afb_daemon_make_event(evlist[3]);
278 event_list[0].name = evlist[0];
279 event_list[0].event = &ev_tap_shortcut;
281 event_list[1].name = evlist[1];
282 event_list[1].event = &ev_on_screen_message;
284 event_list[2].name = evlist[2];
285 event_list[2].event = &ev_on_screen_reply;
287 event_list[3].name = evlist[3];
288 event_list[3].event = &ev_reserved;
293 static void onevent(const char *event, struct json_object *object)
295 HMI_NOTICE("homescreen-service","on_event %s", event);
298 const struct afb_binding_v2 afbBindingV2 = {
300 .specification = NULL,