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"
31 #define COMMAND_EVENT_NUM 3
32 #define EVENT_SUBSCRIBE_ERROR_CODE 100
34 /* To Do hash table is better */
37 struct afb_event* event;
40 static struct event event_list[COMMAND_EVENT_NUM];
42 static struct afb_event ev_tap_shortcut;
43 static struct afb_event ev_on_screen_message;
44 static struct afb_event ev_reserved;
46 static const char _error[] = "error";
48 static const char _application_name[] = "application_name";
49 static const char _display_message[] = "display_message";
52 ********** Method of HomeScreen Service (API) **********
55 static void pingSample(struct afb_req request)
57 static int pingcount = 0;
58 afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
59 AFB_NOTICE("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
65 * ショートカットがタップされたことをアプリケーションに通知するために使用する
70 * - application_name : アプリケーション名
76 static void tap_shortcut (struct afb_req request)
78 AFB_NOTICE("%s is called.", __FUNCTION__);
81 const char* value = afb_req_value(request, _application_name);
84 AFB_NOTICE("request params = %s.", value);
86 struct json_object* push_obj = json_object_new_object();
87 hs_add_object_to_json_object_str( push_obj, 2,
88 _application_name, value);
89 afb_event_push(ev_tap_shortcut, push_obj);
91 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
96 struct json_object *res = json_object_new_object();
97 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
99 afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
103 * HomeScreenのOnScreenを表示する
107 * - display_message : 表示したい文字列
113 static void on_screen_message (struct afb_req request)
115 AFB_NOTICE("%s is called.", __FUNCTION__);
118 const char* value = afb_req_value(request, _display_message);
121 AFB_NOTICE("request params = %s.", value);
123 struct json_object* push_obj = json_object_new_object();
124 hs_add_object_to_json_object_str( push_obj, 2,
125 _display_message, value);
126 afb_event_push(ev_on_screen_message, push_obj);
128 afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
132 // HomeScreenに返すレスポンス
133 struct json_object *res = json_object_new_object();
134 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
136 afb_req_success(request, res, "afb_event_push event [on_screen_message]");
143 * - event : Event name. Event list is written in libhomescreen.cpp
151 static void subscribe(struct afb_req request)
153 const char *value = afb_req_value(request, "event");
154 AFB_NOTICE("value is %s", value);
157 int index = hs_search_event_name_index(value);
160 AFB_NOTICE("dedicated event doesn't exist");
161 ret = EVENT_SUBSCRIBE_ERROR_CODE;
165 afb_req_subscribe(request, *event_list[index].event);
169 AFB_NOTICE("Please input event name");
170 ret = EVENT_SUBSCRIBE_ERROR_CODE;
172 /*create response json object*/
173 struct json_object *res = json_object_new_object();
174 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
176 afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
183 * - event : Event name. Event list is written in libhomescreen.cpp
191 static void unsubscribe(struct afb_req request)
193 const char *value = afb_req_value(request, "event");
194 AFB_NOTICE("value is %s", value);
197 int index = hs_search_event_name_index(value);
200 AFB_NOTICE("dedicated event doesn't exist");
201 ret = EVENT_SUBSCRIBE_ERROR_CODE;
205 afb_req_unsubscribe(request, *event_list[index].event);
209 AFB_NOTICE("Please input event name");
210 ret = EVENT_SUBSCRIBE_ERROR_CODE;
212 /*create response json object*/
213 struct json_object *res = json_object_new_object();
214 hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
216 afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
220 * array of the verbs exported to afb-daemon
222 static const struct afb_verb_v2 verbs[]= {
223 /* VERB'S NAME SESSION MANAGEMENT FUNCTION TO CALL */
224 { .verb = "ping", .session = AFB_SESSION_NONE, .callback = pingSample, .auth = NULL },
225 { .verb = "tap_shortcut", .session = AFB_SESSION_NONE, .callback = tap_shortcut, .auth = NULL },
226 { .verb = "on_screen_message", .session = AFB_SESSION_NONE, .callback = on_screen_message, .auth = NULL },
227 { .verb = "subscribe", .session = AFB_SESSION_NONE, .callback = subscribe, .auth = NULL },
228 { .verb = "unsubscribe", .session = AFB_SESSION_NONE, .callback = unsubscribe, .auth = NULL },
229 {NULL } /* marker for end of the array */
234 AFB_NOTICE("binding preinit (was register)");
240 AFB_NOTICE("binding init");
242 ev_tap_shortcut = afb_daemon_make_event(evlist[0]);
243 ev_on_screen_message = afb_daemon_make_event(evlist[1]);
244 ev_reserved = afb_daemon_make_event(evlist[2]);
246 event_list[0].name = evlist[0];
247 event_list[0].event = &ev_tap_shortcut;
249 event_list[1].name = evlist[1];
250 event_list[1].event = &ev_on_screen_message;
252 event_list[2].name = evlist[2];
253 event_list[2].event = &ev_reserved;
258 static void onevent(const char *event, struct json_object *object)
260 AFB_NOTICE("on_event %s", event);
263 const struct afb_binding_v2 afbBindingV2 = {
265 .specification = NULL,