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.
18 #define AFB_BINDING_VERSION 2
19 #include <afb/afb-binding.h>
26 #include <json-c/json.h>
29 #include "hs-helper.h"
30 #include "hmi-debug.h"
32 #define COMMAND_EVENT_NUM 4
33 #define EVENT_SUBSCRIBE_ERROR_CODE 100
35 /* To Do hash table is better */
38 struct afb_event* event;
41 static struct event event_list[COMMAND_EVENT_NUM];
43 static struct afb_event ev_tap_shortcut;
44 static struct afb_event ev_on_screen_message;
45 static struct afb_event ev_on_screen_reply;
46 static struct afb_event ev_reserved;
48 static const char _error[] = "error";
50 static const char _application_name[] = "application_name";
51 static const char _display_message[] = "display_message";
52 static const char _reply_message[] = "reply_message";
55 ********** Method of HomeScreen Service (API) **********
58 static void pingSample(struct afb_req request)
60 static int pingcount = 0;
61 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
62 HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
67 * tap_shortcut notify for homescreen
68 * When Shortcut area is tapped, notify these applciations
72 * - application_name : application name
78 static void tap_shortcut (struct afb_req request)
80 HMI_NOTICE("homescreen-service","called.");
83 const char* value = afb_req_value(request, _application_name);
86 HMI_NOTICE("homescreen-service","request params = %s.", value);
88 struct json_object* push_obj = json_object_new_object();
89 hs_add_object_to_json_object_str( push_obj, 2,
90 _application_name, value);
91 afb_event_push(ev_tap_shortcut, push_obj);
93 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
97 // response to HomeScreen
98 struct json_object *res = json_object_new_object();
99 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
101 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
105 * HomeScreen OnScreen message
109 * - display_message : message for display
115 static void on_screen_message (struct afb_req request)
117 HMI_NOTICE("homescreen-service","called.");
120 const char* value = afb_req_value(request, _display_message);
123 HMI_NOTICE("homescreen-service","request params = %s.", value);
125 struct json_object* push_obj = json_object_new_object();
126 hs_add_object_to_json_object_str( push_obj, 2,
127 _display_message, value);
128 afb_event_push(ev_on_screen_message, push_obj);
130 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
134 // response to HomeScreen
135 struct json_object *res = json_object_new_object();
136 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
138 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
142 * HomeScreen OnScreen Reply
146 * - reply_message : message for reply
152 static void on_screen_reply (struct afb_req request)
154 HMI_NOTICE("homescreen-service","called.");
157 const char* value = afb_req_value(request, _reply_message);
160 HMI_NOTICE("homescreen-service","request params = %s.", value);
162 struct json_object* push_obj = json_object_new_object();
163 hs_add_object_to_json_object_str( push_obj, 2,
164 _reply_message, value);
165 afb_event_push(ev_on_screen_reply, push_obj);
167 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
171 // response to HomeScreen
172 struct json_object *res = json_object_new_object();
173 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
175 afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
182 * - event : Event name. Event list is written in libhomescreen.cpp
190 static void subscribe(struct afb_req request)
192 const char *value = afb_req_value(request, "event");
193 HMI_NOTICE("homescreen-service","value is %s", value);
196 int index = hs_search_event_name_index(value);
199 HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
200 ret = EVENT_SUBSCRIBE_ERROR_CODE;
204 afb_req_subscribe(request, *event_list[index].event);
208 HMI_NOTICE("homescreen-service","Please input event name");
209 ret = EVENT_SUBSCRIBE_ERROR_CODE;
211 /*create response json object*/
212 struct json_object *res = json_object_new_object();
213 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
215 afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
222 * - event : Event name. Event list is written in libhomescreen.cpp
230 static void unsubscribe(struct afb_req request)
232 const char *value = afb_req_value(request, "event");
233 HMI_NOTICE("homescreen-service","value is %s", value);
236 int index = hs_search_event_name_index(value);
239 HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
240 ret = EVENT_SUBSCRIBE_ERROR_CODE;
244 afb_req_unsubscribe(request, *event_list[index].event);
248 HMI_NOTICE("homescreen-service","Please input event name");
249 ret = EVENT_SUBSCRIBE_ERROR_CODE;
251 /*create response json object*/
252 struct json_object *res = json_object_new_object();
253 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
255 afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
259 * array of the verbs exported to afb-daemon
261 static const struct afb_verb_v2 verbs[]= {
262 /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL */
263 { .verb = "ping", .session = AFB_SESSION_NONE, .callback = pingSample, .auth = NULL },
264 { .verb = "tap_shortcut", .session = AFB_SESSION_NONE, .callback = tap_shortcut, .auth = NULL },
265 { .verb = "on_screen_message", .session = AFB_SESSION_NONE, .callback = on_screen_message, .auth = NULL },
266 { .verb = "on_screen_reply", .session = AFB_SESSION_NONE, .callback = on_screen_reply, .auth = NULL },
267 { .verb = "subscribe", .session = AFB_SESSION_NONE, .callback = subscribe, .auth = NULL },
268 { .verb = "unsubscribe", .session = AFB_SESSION_NONE, .callback = unsubscribe, .auth = NULL },
269 {NULL } /* marker for end of the array */
274 HMI_NOTICE("homescreen-service","binding preinit (was register)");
280 HMI_NOTICE("homescreen-service","binding init");
282 ev_tap_shortcut = afb_daemon_make_event(evlist[0]);
283 ev_on_screen_message = afb_daemon_make_event(evlist[1]);
284 ev_on_screen_reply = afb_daemon_make_event(evlist[2]);
285 ev_reserved = afb_daemon_make_event(evlist[3]);
287 event_list[0].name = evlist[0];
288 event_list[0].event = &ev_tap_shortcut;
290 event_list[1].name = evlist[1];
291 event_list[1].event = &ev_on_screen_message;
293 event_list[2].name = evlist[2];
294 event_list[2].event = &ev_on_screen_reply;
296 event_list[3].name = evlist[3];
297 event_list[3].event = &ev_reserved;
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,