Add some comments for hs-helper
[apps/agl-service-homescreen.git] / src / homescreen.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef _GNU_SOURCE
18 #define _GNU_SOURCE
19 #endif
20 #include "hs-helper.h"
21 #include "hmi-debug.h"
22
23 #define COMMAND_EVENT_NUM 4
24 #define EVENT_SUBSCRIBE_ERROR_CODE 100
25
26 /* To Do hash table is better */
27 struct event{
28     const char* name;
29     struct afb_event* event;
30     };
31
32 static struct event event_list[COMMAND_EVENT_NUM];
33
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;
38
39 static const char _error[] = "error";
40
41 static const char _application_name[] = "application_name";
42 static const char _display_message[] = "display_message";
43 static const char _reply_message[] = "reply_message";
44
45 /*
46 ********** Method of HomeScreen Service (API) **********
47 */
48
49 static void pingSample(struct afb_req request)
50 {
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);
54    pingcount++;
55 }
56
57 /**
58  * tap_shortcut notify for homescreen
59  * When Shortcut area is tapped,  notify these applciations
60  *
61  * #### Parameters
62  * Request key
63  * - application_name   : application name
64  *
65  * #### Return
66  * None
67  *
68  */
69 static void tap_shortcut (struct afb_req request)
70 {
71     HMI_NOTICE("homescreen-service","called.");
72
73     int ret = 0;
74     const char* value = afb_req_value(request, _application_name);
75     if (value) {
76
77       HMI_NOTICE("homescreen-service","request params = %s.", value);
78
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);
83     } else {
84       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
85       return;
86     }
87
88   // response to HomeScreen
89     struct json_object *res = json_object_new_object();
90     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
91       _error,  ret);
92     afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
93 }
94
95 /**
96  * HomeScreen OnScreen message
97  *
98  * #### Parameters
99  * Request key
100  * - display_message   : message for display
101  *
102  * #### Return
103  * None
104  *
105  */
106 static void on_screen_message (struct afb_req request)
107 {
108     HMI_NOTICE("homescreen-service","called.");
109
110     int ret = 0;
111     const char* value = afb_req_value(request, _display_message);
112     if (value) {
113
114       HMI_NOTICE("homescreen-service","request params = %s.", value);
115
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);
120     } else {
121       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
122       return;
123     }
124
125   // response to HomeScreen
126     struct json_object *res = json_object_new_object();
127     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
128       _error,  ret);
129     afb_req_success(request, res, "afb_event_push event [on_screen_message]");
130 }
131
132 /**
133  * HomeScreen OnScreen Reply
134  *
135  * #### Parameters
136  * Request key
137  * - reply_message   : message for reply
138  *
139  * #### Return
140  * None
141  *
142  */
143 static void on_screen_reply (struct afb_req request)
144 {
145     HMI_NOTICE("homescreen-service","called.");
146
147     int ret = 0;
148     const char* value = afb_req_value(request, _reply_message);
149     if (value) {
150
151       HMI_NOTICE("homescreen-service","request params = %s.", value);
152
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);
157     } else {
158       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
159       return;
160     }
161
162   // response to HomeScreen
163     struct json_object *res = json_object_new_object();
164     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
165       _error,  ret);
166     afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
167 }
168
169 /**
170  * Subscribe event
171  *
172  * #### Parameters
173  *  - event  : Event name. Event list is written in libhomescreen.cpp
174  *
175  * #### Return
176  * None
177  *
178  */
179 static void subscribe(struct afb_req request)
180 {
181     const char *value = afb_req_value(request, "event");
182     HMI_NOTICE("homescreen-service","value is %s", value);
183     int ret = 0;
184     if(value) {
185         int index = hs_search_event_name_index(value);
186         if(index < 0)
187         {
188             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
189             ret = EVENT_SUBSCRIBE_ERROR_CODE;
190         }
191         else
192         {
193             afb_req_subscribe(request, *event_list[index].event);
194         }
195     }
196     else{
197         HMI_NOTICE("homescreen-service","Please input event name");
198         ret = EVENT_SUBSCRIBE_ERROR_CODE;
199     }
200     /*create response json object*/
201     struct json_object *res = json_object_new_object();
202     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
203         _error, ret);
204     afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
205 }
206
207 /**
208  * Unsubscribe event
209  *
210  * #### Parameters
211  *  - event  : Event name. Event list is written in libhomescreen.cpp
212  *
213  * #### Return
214  * None
215  *
216  */
217 static void unsubscribe(struct afb_req request)
218 {
219     const char *value = afb_req_value(request, "event");
220     HMI_NOTICE("homescreen-service","value is %s", value);
221     int ret = 0;
222     if(value) {
223         int index = hs_search_event_name_index(value);
224         if(index < 0)
225         {
226             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
227             ret = EVENT_SUBSCRIBE_ERROR_CODE;
228         }
229         else
230         {
231             afb_req_unsubscribe(request, *event_list[index].event);
232         }
233     }
234     else{
235         HMI_NOTICE("homescreen-service","Please input event name");
236         ret = EVENT_SUBSCRIBE_ERROR_CODE;
237     }
238     /*create response json object*/
239     struct json_object *res = json_object_new_object();
240     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
241         _error, ret);
242     afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
243 }
244
245 /*
246  * array of the verbs exported to afb-daemon
247  */
248 static const struct afb_verb_v2 verbs[]= {
249     /* VERB'S NAME                 FUNCTION TO CALL               authorisation     some info         SESSION MANAGEMENT                                    */
250     { .verb = "ping",              .callback = pingSample,        .auth = NULL,     .info = NULL,     .session = AFB_SESSION_NONE     },
251     { .verb = "tap_shortcut",      .callback = tap_shortcut,      .auth = NULL,     .info = NULL,     .session = AFB_SESSION_NONE     },
252     { .verb = "on_screen_message", .callback = on_screen_message, .auth = NULL,     .info = NULL,     .session = AFB_SESSION_NONE     },
253     { .verb = "on_screen_reply",   .callback = on_screen_reply,   .auth = NULL,     .info = NULL,     .session = AFB_SESSION_NONE     },
254     { .verb = "subscribe",         .callback = subscribe,         .auth = NULL,     .info = NULL,     .session = AFB_SESSION_NONE     },
255     { .verb = "unsubscribe",       .callback = unsubscribe,       .auth = NULL,     .info = NULL,     .session = AFB_SESSION_NONE     },
256     {NULL } /* marker for end of the array */
257 };
258
259 /**
260  * homescreen binding preinit function
261  *
262  * #### Parameters
263  *  - null
264  *
265  * #### Return
266  * None
267  *
268  */
269 static int preinit()
270 {
271    HMI_NOTICE("homescreen-service","binding preinit (was register)");
272    return 0;
273 }
274
275 /**
276  * homescreen binding init function
277  *
278  * #### Parameters
279  *  - null
280  *
281  * #### Return
282  * None
283  *
284  */
285 static int init()
286 {
287    HMI_NOTICE("homescreen-service","binding init");
288
289    ev_tap_shortcut = afb_daemon_make_event(evlist[0]);
290    ev_on_screen_message = afb_daemon_make_event(evlist[1]);
291    ev_on_screen_reply = afb_daemon_make_event(evlist[2]);
292    ev_reserved = afb_daemon_make_event(evlist[3]);
293
294    event_list[0].name = evlist[0];
295    event_list[0].event = &ev_tap_shortcut;
296
297    event_list[1].name = evlist[1];
298    event_list[1].event = &ev_on_screen_message;
299
300    event_list[2].name = evlist[2];
301    event_list[2].event = &ev_on_screen_reply;
302
303    event_list[3].name = evlist[3];
304    event_list[3].event = &ev_reserved;
305
306    return 0;
307 }
308
309 /**
310  * homescreen binding event function
311  *
312  * #### Parameters
313  *  - event  : event name
314  *  - object : event json object
315  *
316  * #### Return
317  * None
318  *
319  */
320 static void onevent(const char *event, struct json_object *object)
321 {
322    HMI_NOTICE("homescreen-service","on_event %s", event);
323 }
324
325 const struct afb_binding_v2 afbBindingV2 = {
326     .api = "homescreen",
327     .specification = NULL,
328     .info = NULL,
329     .verbs = verbs,
330     .preinit = preinit,
331     .init = init,
332     .onevent = onevent
333 };