Init afb_daemon event with loop
[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 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";
38
39 /*
40 ********** Method of HomeScreen Service (API) **********
41 */
42
43 static void pingSample(struct afb_req request)
44 {
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);
48    pingcount++;
49 }
50
51 /**
52  * tap_shortcut notify for homescreen
53  * When Shortcut area is tapped,  notify these applciations
54  *
55  * #### Parameters
56  * Request key
57  * - application_name   : application name
58  *
59  * #### Return
60  * None
61  *
62  */
63 static void tap_shortcut (struct afb_req request)
64 {
65     HMI_NOTICE("homescreen-service","called.");
66
67     int ret = 0;
68     const char* value = afb_req_value(request, _application_name);
69     if (value) {
70
71       HMI_NOTICE("homescreen-service","request params = %s.", value);
72
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);
77     } else {
78       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
79       return;
80     }
81
82   // response to HomeScreen
83     struct json_object *res = json_object_new_object();
84     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
85       _error,  ret);
86     afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
87 }
88
89 /**
90  * HomeScreen OnScreen message
91  *
92  * #### Parameters
93  * Request key
94  * - display_message   : message for display
95  *
96  * #### Return
97  * None
98  *
99  */
100 static void on_screen_message (struct afb_req request)
101 {
102     HMI_NOTICE("homescreen-service","called.");
103
104     int ret = 0;
105     const char* value = afb_req_value(request, _display_message);
106     if (value) {
107
108       HMI_NOTICE("homescreen-service","request params = %s.", value);
109
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);
114     } else {
115       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
116       return;
117     }
118
119   // response to HomeScreen
120     struct json_object *res = json_object_new_object();
121     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
122       _error,  ret);
123     afb_req_success(request, res, "afb_event_push event [on_screen_message]");
124 }
125
126 /**
127  * HomeScreen OnScreen Reply
128  *
129  * #### Parameters
130  * Request key
131  * - reply_message   : message for reply
132  *
133  * #### Return
134  * None
135  *
136  */
137 static void on_screen_reply (struct afb_req request)
138 {
139     HMI_NOTICE("homescreen-service","called.");
140
141     int ret = 0;
142     const char* value = afb_req_value(request, _reply_message);
143     if (value) {
144
145       HMI_NOTICE("homescreen-service","request params = %s.", value);
146
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);
151     } else {
152       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
153       return;
154     }
155
156   // response to HomeScreen
157     struct json_object *res = json_object_new_object();
158     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
159       _error,  ret);
160     afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
161 }
162
163 /**
164  * Subscribe event
165  *
166  * #### Parameters
167  *  - event  : Event name. Event list is written in libhomescreen.cpp
168  *
169  * #### Return
170  * None
171  *
172  */
173 static void subscribe(struct afb_req request)
174 {
175     const char *value = afb_req_value(request, "event");
176     HMI_NOTICE("homescreen-service","value is %s", value);
177     int ret = 0;
178     if(value) {
179         int index = hs_search_event_name_index(value);
180         if(index < 0)
181         {
182             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
183             ret = EVENT_SUBSCRIBE_ERROR_CODE;
184         }
185         else
186         {
187             afb_req_subscribe(request, event_list[index].event);
188         }
189     }
190     else{
191         HMI_NOTICE("homescreen-service","Please input event name");
192         ret = EVENT_SUBSCRIBE_ERROR_CODE;
193     }
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,
197         _error, ret);
198     afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
199 }
200
201 /**
202  * Unsubscribe event
203  *
204  * #### Parameters
205  *  - event  : Event name. Event list is written in libhomescreen.cpp
206  *
207  * #### Return
208  * None
209  *
210  */
211 static void unsubscribe(struct afb_req request)
212 {
213     const char *value = afb_req_value(request, "event");
214     HMI_NOTICE("homescreen-service","value is %s", value);
215     int ret = 0;
216     if(value) {
217         int index = hs_search_event_name_index(value);
218         if(index < 0)
219         {
220             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
221             ret = EVENT_SUBSCRIBE_ERROR_CODE;
222         }
223         else
224         {
225             afb_req_unsubscribe(request, event_list[index].event);
226         }
227     }
228     else{
229         HMI_NOTICE("homescreen-service","Please input event name");
230         ret = EVENT_SUBSCRIBE_ERROR_CODE;
231     }
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,
235         _error, ret);
236     afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
237 }
238
239 /*
240  * array of the verbs exported to afb-daemon
241  */
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 */
251 };
252
253 /**
254  * homescreen binding preinit function
255  *
256  * #### Parameters
257  *  - null
258  *
259  * #### Return
260  * None
261  *
262  */
263 static int preinit()
264 {
265    HMI_NOTICE("homescreen-service","binding preinit (was register)");
266    return 0;
267 }
268
269 /**
270  * homescreen binding init function
271  *
272  * #### Parameters
273  *  - null
274  *
275  * #### Return
276  * None
277  *
278  */
279 static int init()
280 {
281     HMI_NOTICE("homescreen-service","binding init");
282
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]);
286     }
287
288     return 0;
289 }
290
291 /**
292  * homescreen binding event function
293  *
294  * #### Parameters
295  *  - event  : event name
296  *  - object : event json object
297  *
298  * #### Return
299  * None
300  *
301  */
302 static void onevent(const char *event, struct json_object *object)
303 {
304    HMI_NOTICE("homescreen-service","on_event %s", event);
305 }
306
307 const struct afb_binding_v2 afbBindingV2 = {
308     .api = "homescreen",
309     .specification = NULL,
310     .info = NULL,
311     .verbs = verbs,
312     .preinit = preinit,
313     .init = init,
314     .onevent = onevent
315 };