Change comments from Japanese to English
[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
31 #define COMMAND_EVENT_NUM 3
32 #define EVENT_SUBSCRIBE_ERROR_CODE 100
33
34 /* To Do hash table is better */
35 struct event{
36     const char* name;
37     struct afb_event* event;
38     };
39
40 static struct event event_list[COMMAND_EVENT_NUM];
41
42 static struct afb_event ev_tap_shortcut;
43 static struct afb_event ev_on_screen_message;
44 static struct afb_event ev_reserved;
45
46 static const char _error[] = "error";
47
48 static const char _application_name[] = "application_name";
49 static const char _display_message[] = "display_message";
50
51 /*
52 ********** Method of HomeScreen Service (API) **********
53 */
54
55 static void pingSample(struct afb_req request)
56 {
57    static int pingcount = 0;
58    afb_req_success_f(request, json_object_new_int(pingcount), "Ping count = %d", pingcount);
59    AFB_NOTICE("Verbosity macro at level notice invoked at ping invocation count = %d", pingcount);
60    pingcount++;
61 }
62
63 /**
64  * tap_shortcut notify for homescreen
65  * When Shortcut area is tapped,  notify these applciations
66   *
67  * #### Parameters
68  * Request key
69  * - application_name   : application name
70  *
71  * #### Return
72  * Nothing
73  *
74  */
75 static void tap_shortcut (struct afb_req request)
76 {
77     AFB_NOTICE("%s is called.", __FUNCTION__);
78
79     int ret = 0;
80     const char* value = afb_req_value(request, _application_name);
81     if (value) {
82
83       AFB_NOTICE("request params = %s.", value);
84
85       struct json_object* push_obj = json_object_new_object();
86       hs_add_object_to_json_object_str( push_obj, 2,
87       _application_name, value);
88       afb_event_push(ev_tap_shortcut, push_obj);
89     } else {
90       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
91       return;
92     }
93
94   // response to HomeScreen
95     struct json_object *res = json_object_new_object();
96     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
97       _error,  ret);
98     afb_req_success(request, res, "afb_event_push event [tap_shortcut]");
99 }
100
101 /**
102  * HomeScreen OnScreen message
103  *
104  * #### Parameters
105  * Request key
106  * - display_message   : message for display
107  *
108  * #### Return
109  * Nothing
110  *
111  */
112 static void on_screen_message (struct afb_req request)
113 {
114     AFB_NOTICE("%s is called.", __FUNCTION__);
115
116     int ret = 0;
117     const char* value = afb_req_value(request, _display_message);
118     if (value) {
119
120       AFB_NOTICE("request params = %s.", value);
121
122       struct json_object* push_obj = json_object_new_object();
123       hs_add_object_to_json_object_str( push_obj, 2,
124       _display_message, value);
125       afb_event_push(ev_on_screen_message, push_obj);
126     } else {
127       afb_req_fail_f(request, "failed", "called %s, Unknown palameter", __FUNCTION__);
128       return;
129     }
130
131   // response to HomeScreen
132     struct json_object *res = json_object_new_object();
133     hs_add_object_to_json_object_func(res, __FUNCTION__, 2,
134       _error,  ret);
135     afb_req_success(request, res, "afb_event_push event [on_screen_message]");
136 }
137
138 /**
139  * Subscribe event
140  *
141  * #### Parameters
142  *  - event  : Event name. Event list is written in libhomescreen.cpp
143  *
144  * #### Return
145  * Nothing
146  *
147  * #### Note
148  *
149  */
150 static void subscribe(struct afb_req request)
151 {
152     const char *value = afb_req_value(request, "event");
153     AFB_NOTICE("value is %s", value);
154     int ret = 0;
155     if(value) {
156         int index = hs_search_event_name_index(value);
157         if(index < 0)
158         {
159             AFB_NOTICE("dedicated event doesn't exist");
160             ret = EVENT_SUBSCRIBE_ERROR_CODE;
161         }
162         else
163         {
164             afb_req_subscribe(request, *event_list[index].event);
165         }
166     }
167     else{
168         AFB_NOTICE("Please input event name");
169         ret = EVENT_SUBSCRIBE_ERROR_CODE;
170     }
171     /*create response json object*/
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_f(request, res, "homescreen binder subscribe event name [%s]", value);
176 }
177
178 /**
179  * Unsubscribe 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 unsubscribe(struct afb_req request)
191 {
192     const char *value = afb_req_value(request, "event");
193     AFB_NOTICE("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             AFB_NOTICE("dedicated event doesn't exist");
200             ret = EVENT_SUBSCRIBE_ERROR_CODE;
201         }
202         else
203         {
204             afb_req_unsubscribe(request, *event_list[index].event);
205         }
206     }
207     else{
208         AFB_NOTICE("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 unsubscribe event name [%s]", value);
216 }
217
218 /*
219  * array of the verbs exported to afb-daemon
220  */
221 static const struct afb_verb_v2 verbs[]= {
222     /* VERB'S NAME                    SESSION MANAGEMENT                FUNCTION TO CALL                     */
223     { .verb = "ping",              .session = AFB_SESSION_NONE,    .callback = pingSample,        .auth = NULL },
224     { .verb = "tap_shortcut",      .session = AFB_SESSION_NONE,    .callback = tap_shortcut,      .auth = NULL },
225     { .verb = "on_screen_message", .session = AFB_SESSION_NONE,    .callback = on_screen_message, .auth = NULL },
226     { .verb = "subscribe",         .session = AFB_SESSION_NONE,    .callback = subscribe,         .auth = NULL },
227     { .verb = "unsubscribe",       .session = AFB_SESSION_NONE,    .callback = unsubscribe,       .auth = NULL },
228     {NULL } /* marker for end of the array */
229 };
230
231 static int preinit()
232 {
233    AFB_NOTICE("binding preinit (was register)");
234    return 0;
235 }
236
237 static int init()
238 {
239    AFB_NOTICE("binding init");
240
241    ev_tap_shortcut = afb_daemon_make_event(evlist[0]);
242    ev_on_screen_message = afb_daemon_make_event(evlist[1]);
243    ev_reserved = afb_daemon_make_event(evlist[2]);
244
245    event_list[0].name = evlist[0];
246    event_list[0].event = &ev_tap_shortcut;
247
248    event_list[1].name = evlist[1];
249    event_list[1].event = &ev_on_screen_message;
250
251    event_list[2].name = evlist[2];
252    event_list[2].event = &ev_reserved;
253
254    return 0;
255 }
256
257 static void onevent(const char *event, struct json_object *object)
258 {
259    AFB_NOTICE("on_event %s", event);
260 }
261
262 const struct afb_binding_v2 afbBindingV2 = {
263     .api = "homescreen",
264     .specification = NULL,
265     .verbs = verbs,
266     .preinit = preinit,
267     .init = init,
268     .onevent = onevent
269 };