Revert "Use appid between homescreen-service and apps"
[apps/agl-service-homescreen.git] / src / homescreen.c
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 #define _GNU_SOURCE
18 #define AFB_BINDING_VERSION 2
19 #include <afb/afb-binding.h>
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <json-c/json.h>
27 #include <glib.h>
28 #include <pthread.h>
29 #include "hs-helper.h"
30 #include "hmi-debug.h"
31
32 #define COMMAND_EVENT_NUM 4
33 #define EVENT_SUBSCRIBE_ERROR_CODE 100
34
35 /* To Do hash table is better */
36 struct event{
37     const char* name;
38     struct afb_event* event;
39     };
40
41 static struct event event_list[COMMAND_EVENT_NUM];
42
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;
47
48 static const char _error[] = "error";
49
50 static const char _application_name[] = "application_name";
51 static const char _display_message[] = "display_message";
52 static const char _reply_message[] = "reply_message";
53
54 /*
55 ********** Method of HomeScreen Service (API) **********
56 */
57
58 static void pingSample(struct afb_req request)
59 {
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);
63    pingcount++;
64 }
65
66 /**
67  * tap_shortcut notify for homescreen
68  * When Shortcut area is tapped,  notify these applciations
69   *
70  * #### Parameters
71  * Request key
72  * - application_name   : application name
73  *
74  * #### Return
75  * Nothing
76  *
77  */
78 static void tap_shortcut (struct afb_req request)
79 {
80     HMI_NOTICE("homescreen-service","called.");
81
82     int ret = 0;
83     const char* value = afb_req_value(request, _application_name);
84     if (value) {
85
86       HMI_NOTICE("homescreen-service","request params = %s.", value);
87
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);
92     } else {
93       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
94       return;
95     }
96
97   // response to HomeScreen
98     struct json_object *res = json_object_new_object();
99     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
100       _error,  ret);
101     afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
102 }
103
104 /**
105  * HomeScreen OnScreen message
106  *
107  * #### Parameters
108  * Request key
109  * - display_message   : message for display
110  *
111  * #### Return
112  * Nothing
113  *
114  */
115 static void on_screen_message (struct afb_req request)
116 {
117     HMI_NOTICE("homescreen-service","called.");
118
119     int ret = 0;
120     const char* value = afb_req_value(request, _display_message);
121     if (value) {
122
123       HMI_NOTICE("homescreen-service","request params = %s.", value);
124
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);
129     } else {
130       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
131       return;
132     }
133
134   // response to HomeScreen
135     struct json_object *res = json_object_new_object();
136     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
137       _error,  ret);
138     afb_req_success(request, res, "afb_event_push event [on_screen_message]");
139 }
140
141 /**
142  * HomeScreen OnScreen Reply
143  *
144  * #### Parameters
145  * Request key
146  * - reply_message   : message for reply
147  *
148  * #### Return
149  * Nothing
150  *
151  */
152 static void on_screen_reply (struct afb_req request)
153 {
154     HMI_NOTICE("homescreen-service","called.");
155
156     int ret = 0;
157     const char* value = afb_req_value(request, _reply_message);
158     if (value) {
159
160       HMI_NOTICE("homescreen-service","request params = %s.", value);
161
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);
166     } else {
167       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
168       return;
169     }
170
171   // response to HomeScreen
172     struct json_object *res = json_object_new_object();
173     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
174       _error,  ret);
175     afb_req_success(request, res, "afb_event_push event [on_screen_reply]");
176 }
177
178 /**
179  * Subscribe event
180  *
181  * #### Parameters
182  *  - event  : Event name. Event list is written in libhomescreen.cpp
183  *
184  * #### Return
185  * Nothing
186  *
187  * #### Note
188  *
189  */
190 static void subscribe(struct afb_req request)
191 {
192     const char *value = afb_req_value(request, "event");
193     HMI_NOTICE("homescreen-service","value is %s", value);
194     int ret = 0;
195     if(value) {
196         int index = hs_search_event_name_index(value);
197         if(index < 0)
198         {
199             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
200             ret = EVENT_SUBSCRIBE_ERROR_CODE;
201         }
202         else
203         {
204             afb_req_subscribe(request, *event_list[index].event);
205         }
206     }
207     else{
208         HMI_NOTICE("homescreen-service","Please input event name");
209         ret = EVENT_SUBSCRIBE_ERROR_CODE;
210     }
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,
214         _error, ret);
215     afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
216 }
217
218 /**
219  * Unsubscribe event
220  *
221  * #### Parameters
222  *  - event  : Event name. Event list is written in libhomescreen.cpp
223  *
224  * #### Return
225  * Nothing
226  *
227  * #### Note
228  *
229  */
230 static void unsubscribe(struct afb_req request)
231 {
232     const char *value = afb_req_value(request, "event");
233     HMI_NOTICE("homescreen-service","value is %s", value);
234     int ret = 0;
235     if(value) {
236         int index = hs_search_event_name_index(value);
237         if(index < 0)
238         {
239             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
240             ret = EVENT_SUBSCRIBE_ERROR_CODE;
241         }
242         else
243         {
244             afb_req_unsubscribe(request, *event_list[index].event);
245         }
246     }
247     else{
248         HMI_NOTICE("homescreen-service","Please input event name");
249         ret = EVENT_SUBSCRIBE_ERROR_CODE;
250     }
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,
254         _error, ret);
255     afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
256 }
257
258 /*
259  * array of the verbs exported to afb-daemon
260  */
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 */
270 };
271
272 static int preinit()
273 {
274    HMI_NOTICE("homescreen-service","binding preinit (was register)");
275    return 0;
276 }
277
278 static int init()
279 {
280    HMI_NOTICE("homescreen-service","binding init");
281
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]);
286
287    event_list[0].name = evlist[0];
288    event_list[0].event = &ev_tap_shortcut;
289
290    event_list[1].name = evlist[1];
291    event_list[1].event = &ev_on_screen_message;
292
293    event_list[2].name = evlist[2];
294    event_list[2].event = &ev_on_screen_reply;
295
296    event_list[3].name = evlist[3];
297    event_list[3].event = &ev_reserved;
298
299    return 0;
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     .verbs = verbs,
311     .preinit = preinit,
312     .init = init,
313     .onevent = onevent
314 };