Update lastest code from agl-service-homescreen-2017
[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  * Nothing
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  * Nothing
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  * Nothing
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  * Nothing
177  *
178  * #### Note
179  *
180  */
181 static void subscribe(struct afb_req request)
182 {
183     const char *value = afb_req_value(request, "event");
184     HMI_NOTICE("homescreen-service","value is %s", value);
185     int ret = 0;
186     if(value) {
187         int index = hs_search_event_name_index(value);
188         if(index < 0)
189         {
190             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
191             ret = EVENT_SUBSCRIBE_ERROR_CODE;
192         }
193         else
194         {
195             afb_req_subscribe(request, *event_list[index].event);
196         }
197     }
198     else{
199         HMI_NOTICE("homescreen-service","Please input event name");
200         ret = EVENT_SUBSCRIBE_ERROR_CODE;
201     }
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,
205         _error, ret);
206     afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
207 }
208
209 /**
210  * Unsubscribe event
211  *
212  * #### Parameters
213  *  - event  : Event name. Event list is written in libhomescreen.cpp
214  *
215  * #### Return
216  * Nothing
217  *
218  * #### Note
219  *
220  */
221 static void unsubscribe(struct afb_req request)
222 {
223     const char *value = afb_req_value(request, "event");
224     HMI_NOTICE("homescreen-service","value is %s", value);
225     int ret = 0;
226     if(value) {
227         int index = hs_search_event_name_index(value);
228         if(index < 0)
229         {
230             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
231             ret = EVENT_SUBSCRIBE_ERROR_CODE;
232         }
233         else
234         {
235             afb_req_unsubscribe(request, *event_list[index].event);
236         }
237     }
238     else{
239         HMI_NOTICE("homescreen-service","Please input event name");
240         ret = EVENT_SUBSCRIBE_ERROR_CODE;
241     }
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,
245         _error, ret);
246     afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
247 }
248
249 /*
250  * array of the verbs exported to afb-daemon
251  */
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 */
261 };
262
263 static int preinit()
264 {
265    HMI_NOTICE("homescreen-service","binding preinit (was register)");
266    return 0;
267 }
268
269 static int init()
270 {
271    HMI_NOTICE("homescreen-service","binding init");
272
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]);
277
278    event_list[0].name = evlist[0];
279    event_list[0].event = &ev_tap_shortcut;
280
281    event_list[1].name = evlist[1];
282    event_list[1].event = &ev_on_screen_message;
283
284    event_list[2].name = evlist[2];
285    event_list[2].event = &ev_on_screen_reply;
286
287    event_list[3].name = evlist[3];
288    event_list[3].event = &ev_reserved;
289
290    return 0;
291 }
292
293 static void onevent(const char *event, struct json_object *object)
294 {
295    HMI_NOTICE("homescreen-service","on_event %s", event);
296 }
297
298 const struct afb_binding_v2 afbBindingV2 = {
299     .api = "homescreen",
300     .specification = NULL,
301     .info = NULL,
302     .verbs = verbs,
303     .preinit = preinit,
304     .init = init,
305     .onevent = onevent
306 };