536ff6e9773aedc060f7241b5e4bcff96d72a4ea
[apps/agl-service-homescreen.git] / src / hs-client.cpp
1 /*
2  * Copyright (c) 2018 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 #include "hs-client.h"
18 #include "hs-helper.h"
19 #include "hmi-debug.h"
20
21 static const char _type[] = "type";
22 static const char _text[] = "text";
23 static const char _info[] = "info";
24 static const char _icon[] = "icon";
25 static const char _parameter[] = "parameter";
26 static const char _replyto[] = "replyto";
27
28 /**
29  * HS_Client construction function
30  *
31  * #### Parameters
32  *  - id: app's id
33  *
34  * #### Return
35  * None
36  *
37  */
38 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
39 {
40     HMI_NOTICE("homescreen-service","called.");
41     my_event = afb_api_make_event(request->api, id.c_str());
42 }
43
44 /**
45  * HS_Client destruction function
46  *
47  * #### Parameters
48  *  - null
49  *
50  * #### Return
51  * None
52  *
53  */
54 HS_Client::~HS_Client()
55 {
56     HMI_NOTICE("homescreen-service","called.");
57     afb_event_unref(my_event);
58 }
59
60 /**
61  * push tap_shortcut event
62  *
63  * #### Parameters
64  *  - appid: app's id.
65  *
66  * #### Return
67  * result
68  *
69  */
70 int HS_Client::tap_shortcut(const char* appid)
71 {
72     if(!checkEvent(__FUNCTION__))
73         return 0;
74
75     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
76     struct json_object* push_obj = json_object_new_object();
77     hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid,
78     _type, __FUNCTION__);
79     afb_event_push(my_event, push_obj);
80     return 0;
81 }
82
83 /**
84  * push on_screen_message event
85  *
86  * #### Parameters
87  *  - message: post message.
88  *
89  * #### Return
90  * result
91  *
92  */
93 int HS_Client::on_screen_message(afb_req_t request, const char* message)
94 {
95     if(!checkEvent(__FUNCTION__))
96         return 0;
97
98     HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
99     struct json_object* push_obj = json_object_new_object();
100     hs_add_object_to_json_object_str( push_obj, 4, _display_message, message,
101     _type, __FUNCTION__);
102     afb_event_push(my_event, push_obj);
103     return 0;
104 }
105
106 /**
107  * push on_screen_reply event
108  *
109  * #### Parameters
110  *  - message: reply message.
111  *
112  * #### Return
113  * result
114  *
115  */
116 int HS_Client::on_screen_reply(afb_req_t request, const char* message)
117 {
118     if(!checkEvent(__FUNCTION__))
119         return 0;
120
121     HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
122     struct json_object* push_obj = json_object_new_object();
123     hs_add_object_to_json_object_str( push_obj, 4, _reply_message, message,
124     _type, __FUNCTION__);
125     afb_event_push(my_event, push_obj);
126     return 0;
127 }
128
129 /**
130  * subscribe event
131  *
132  * #### Parameters
133  *  - event: homescreen event, tap_shortcut etc.
134  *
135  * #### Return
136  * result
137  *
138  */
139 int HS_Client::subscribe(afb_req_t request, const char* event)
140 {
141     int ret = 0;
142     auto ip = event_list.find(std::string(event));
143     if(ip == event_list.end()) {
144         event_list[std::string(event)] = 0;
145     }
146     if(!subscription) {
147         ret = afb_req_subscribe(request, my_event);
148         if(ret == 0) {
149             subscription = true;
150         }
151     }
152     return ret;
153 }
154
155 /**
156  * unsubscribe event
157  *
158  * #### Parameters
159  *  - event: homescreen event, tap_shortcut etc.
160  *
161  * #### Return
162  * result
163  *
164  */
165 int HS_Client::unsubscribe(afb_req_t request, const char* event)
166 {
167     int ret = 0;
168     event_list.erase(std::string(event));
169     if(event_list.empty()) {
170         ret = afb_req_unsubscribe(request, my_event);
171     }
172     return ret;
173 }
174
175 /**
176  * check if client subscribe event
177  *
178  * #### Parameters
179  *  - event: homescreen event, tap_shortcut etc.
180  *
181  * #### Return
182  * true: found
183  * false: not found
184  *
185  */
186 bool HS_Client::checkEvent(const char* event)
187 {
188     auto ip = event_list.find(std::string(event));
189     if(ip == event_list.end())
190         return false;
191     else
192         return true;
193 }
194
195 /**
196  * showWindow event
197  *
198  * input contents : {"application_id":"the appid that want to display", "parameter":{"area": "display area", ...}}
199  *
200  * #### Parameters
201  * - request : the request
202  * - appid : the appid that want to display
203  *
204  * #### Return
205  * 0 : success
206  * others : fail
207  *
208  */
209 int HS_Client::showWindow(afb_req_t request, const char* appid)
210 {
211     if(!checkEvent(__FUNCTION__))
212         return 0;
213
214     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
215     struct json_object* push_obj = json_object_new_object();
216     hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
217     const char* param = afb_req_value(request, _parameter);
218     if(param) {
219         const char* req_appid = afb_req_get_application_id(request);
220         struct json_object* param_obj = json_tokener_parse(param);
221         json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid));
222         json_object_object_add(push_obj, _parameter, param_obj);
223     }
224     else {
225         HMI_ERROR("homescreen-service","please input correct parameter.");
226         return AFB_EVENT_BAD_REQUEST;
227     }
228     afb_event_push(my_event, push_obj);
229     return 0;
230 }
231
232 /**
233  * hideWindow event
234  *
235  * input contents : {"application_id":"the appid that want to hide"}
236  *
237  * #### Parameters
238  * - request : the request
239  *
240  * #### Return
241  * 0 : success
242  * others : fail
243  *
244  */
245 int HS_Client::hideWindow(afb_req_t request)
246 {
247     if(!checkEvent(__FUNCTION__))
248         return 0;
249
250     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__);
251     const char* req_appid = afb_req_get_application_id(request);
252     struct json_object* push_obj = json_object_new_object();
253     hs_add_object_to_json_object_str( push_obj, 4, _application_id, req_appid,
254     _type, __FUNCTION__);
255     afb_event_push(my_event, push_obj);
256     return 0;
257 }
258
259 /**
260  * replyShowWindow event
261  *
262  * input contens : {"application_id": "the appid that want to reply",  "parameter": {...}}
263  *
264  * #### Parameters
265  * - request : the request
266  * - appid : the appid that want to reply
267  *
268  * #### Return
269  * 0 : success
270  * others : fail
271  *
272  */
273 int HS_Client::replyShowWindow(afb_req_t request, const char* appid)
274 {
275     if(!checkEvent(__FUNCTION__))
276         return 0;
277
278     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
279     struct json_object* push_obj = json_object_new_object();
280     hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
281     const char* param = afb_req_value(request, _parameter);
282     if(param) {
283         json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
284     }
285     else {
286         HMI_ERROR("homescreen-service","please input correct parameter.");
287         return AFB_EVENT_BAD_REQUEST;
288     }
289
290     afb_event_push(my_event, push_obj);
291     return 0;
292 }
293
294 /**
295  * showNotification event
296  *
297  * input contents : {"icon": "icon path", "text": "message contents"}
298  *
299  * #### Parameters
300  *  - request : the request
301  *
302  * #### Return
303  * 0 : success
304  * others : fail
305  *
306  */
307 int HS_Client::showNotification(afb_req_t request)
308 {
309     int ret = 0;
310     const char *value = afb_req_value(request, _text);
311     if(value) {
312         HMI_NOTICE("homescreen-service","text is %s", value);
313         const char* appid = afb_req_get_application_id(request);
314         struct json_object* param_obj = json_object_new_object();
315         const char *icon = afb_req_value(request, _icon);
316         if(icon) {
317             json_object_object_add(param_obj, _icon, json_object_new_string(icon));
318             json_object_object_add(param_obj, _text, json_object_new_string(value));
319             struct json_object* push_obj = json_object_new_object();
320             hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
321             json_object_object_add(push_obj, _parameter, param_obj);
322             afb_event_push(my_event, push_obj);
323         }
324         else {
325             HMI_NOTICE("homescreen-service","please input icon.");
326             ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
327         }
328     }
329     else {
330         HMI_NOTICE("homescreen-service","please input text.");
331         ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
332     }
333
334     return ret;
335 }
336
337 /**
338  * showInformation event
339  *
340  * input contents : {"info": "information contents"}
341  *
342  * #### Parameters
343  *  - request : the request
344  *
345  * #### Return
346  * 0 : success
347  * others : fail
348  *
349  */
350 int HS_Client::showInformation(afb_req_t request)
351 {
352     int ret = 0;
353     const char *value = afb_req_value(request, _info);
354     if(value) {
355         HMI_NOTICE("homescreen-service","info is %s", value);
356         const char* appid = afb_req_get_application_id(request);
357         struct json_object* param_obj = json_object_new_object();
358         json_object_object_add(param_obj, _info, json_object_new_string(value));
359         struct json_object* push_obj = json_object_new_object();
360         hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
361         json_object_object_add(push_obj, _parameter, param_obj);
362         afb_event_push(my_event, push_obj);
363     }
364     else {
365         HMI_NOTICE("homescreen-service","please input information.");
366         ret = AFB_REQ_SHOWINFORMATION_ERROR;
367     }
368
369     return ret;
370 }