7f5f067bbfdd5b7cfdf73f0b30af0974e7955135
[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 3
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_reserved;
46
47 static const char _error[] = "error";
48
49 static const char _application_name[] = "application_name";
50 static const char _display_message[] = "display_message";
51
52 /*
53 ********** Method of HomeScreen Service (API) **********
54 */
55
56 static void pingSample(struct afb_req request)
57 {
58    static int pingcount = 0;
59    afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
60    HMI_NOTICE("homescreen-service","Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
61    pingcount++;
62 }
63
64 /**
65  * tap_shortcut notify for homescreen
66  * When Shortcut area is tapped,  notify these applciations
67   *
68  * #### Parameters
69  * Request key
70  * - application_name   : application name
71  *
72  * #### Return
73  * Nothing
74  *
75  */
76 static void tap_shortcut (struct afb_req request)
77 {
78     HMI_NOTICE("homescreen-service","called.");
79
80     int ret = 0;
81     const char* value = afb_req_value(request, _application_name);
82     if (value) {
83
84       HMI_NOTICE("homescreen-service","request params = %s.", value);
85
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);
90     } else {
91       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
92       return;
93     }
94
95   // response to HomeScreen
96     struct json_object *res = json_object_new_object();
97     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
98       _error,  ret);
99     afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
100 }
101
102 /**
103  * HomeScreen OnScreen message
104  *
105  * #### Parameters
106  * Request key
107  * - display_message   : message for display
108  *
109  * #### Return
110  * Nothing
111  *
112  */
113 static void on_screen_message (struct afb_req request)
114 {
115     HMI_NOTICE("homescreen-service","called.");
116
117     int ret = 0;
118     const char* value = afb_req_value(request, _display_message);
119     if (value) {
120
121       HMI_NOTICE("homescreen-service","request params = %s.", value);
122
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);
127     } else {
128       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
129       return;
130     }
131
132   // response to HomeScreen
133     struct json_object *res = json_object_new_object();
134     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
135       _error,  ret);
136     afb_req_success(request, res, "afb_event_push event [on_screen_message]");
137 }
138
139 /**
140  * Subscribe event
141  *
142  * #### Parameters
143  *  - event  : Event name. Event list is written in libhomescreen.cpp
144  *
145  * #### Return
146  * Nothing
147  *
148  * #### Note
149  *
150  */
151 static void subscribe(struct afb_req request)
152 {
153     const char *value = afb_req_value(request, "event");
154     HMI_NOTICE("homescreen-service","value is %s", value);
155     int ret = 0;
156     if(value) {
157         int index = hs_search_event_name_index(value);
158         if(index < 0)
159         {
160             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
161             ret = EVENT_SUBSCRIBE_ERROR_CODE;
162         }
163         else
164         {
165             afb_req_subscribe(request, *event_list[index].event);
166         }
167     }
168     else{
169         HMI_NOTICE("homescreen-service","Please input event name");
170         ret = EVENT_SUBSCRIBE_ERROR_CODE;
171     }
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,
175         _error, ret);
176     afb_req_success_f(request, res, "homescreen binder subscribe event name [%s]", value);
177 }
178
179 /**
180  * Unsubscribe event
181  *
182  * #### Parameters
183  *  - event  : Event name. Event list is written in libhomescreen.cpp
184  *
185  * #### Return
186  * Nothing
187  *
188  * #### Note
189  *
190  */
191 static void unsubscribe(struct afb_req request)
192 {
193     const char *value = afb_req_value(request, "event");
194     HMI_NOTICE("homescreen-service","value is %s", value);
195     int ret = 0;
196     if(value) {
197         int index = hs_search_event_name_index(value);
198         if(index < 0)
199         {
200             HMI_NOTICE("homescreen-service","dedicated event doesn't exist");
201             ret = EVENT_SUBSCRIBE_ERROR_CODE;
202         }
203         else
204         {
205             afb_req_unsubscribe(request, *event_list[index].event);
206         }
207     }
208     else{
209         HMI_NOTICE("homescreen-service","Please input event name");
210         ret = EVENT_SUBSCRIBE_ERROR_CODE;
211     }
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,
215         _error, ret);
216     afb_req_success_f(request, res, "homescreen binder unsubscribe event name [%s]", value);
217 }
218
219 /*
220  * array of the verbs exported to afb-daemon
221  */
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 */
230 };
231
232 static int preinit()
233 {
234    HMI_NOTICE("homescreen-service","binding preinit (was register)");
235    return 0;
236 }
237
238 static int init()
239 {
240    HMI_NOTICE("homescreen-service","binding init");
241
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]);
245
246    event_list[0].name = evlist[0];
247    event_list[0].event = &ev_tap_shortcut;
248
249    event_list[1].name = evlist[1];
250    event_list[1].event = &ev_on_screen_message;
251
252    event_list[2].name = evlist[2];
253    event_list[2].event = &ev_reserved;
254
255    return 0;
256 }
257
258 static void onevent(const char *event, struct json_object *object)
259 {
260    HMI_NOTICE("homescreen-service","on_event %s", event);
261 }
262
263 const struct afb_binding_v2 afbBindingV2 = {
264     .api = "homescreen",
265     .specification = NULL,
266     .verbs = verbs,
267     .preinit = preinit,
268     .init = init,
269     .onevent = onevent
270 };