f508f3d09389025a607cd8f61f056f53b9f152ec
[apps/agl-service-homescreen.git] / 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 _event[] = "event";
22 static const char _type[] = "type";
23 static const char _text[] = "text";
24 static const char _info[] = "info";
25 static const char _icon[] = "icon";
26 static const char _parameter[] = "parameter";
27 static const char _replyto[] = "replyto";
28 static const char _caller[] = "caller";
29
30 // homescreen-service event and event handler function list
31 const std::unordered_map<std::string, HS_Client::func_handler> HS_Client::func_list {
32     {"tap_shortcut",        &HS_Client::tap_shortcut},
33     {"showWindow",          &HS_Client::showWindow},
34     {"hideWindow",          &HS_Client::hideWindow},
35     {"replyShowWindow",     &HS_Client::replyShowWindow},
36     {"on_screen_message",   &HS_Client::on_screen_message},
37     {"on_screen_reply",     &HS_Client::on_screen_reply},
38     {"subscribe",           &HS_Client::subscribe},
39     {"unsubscribe",         &HS_Client::unsubscribe},
40     {"showNotification",    &HS_Client::showNotification},
41     {"showInformation",     &HS_Client::showInformation}
42 };
43
44 /**
45  * HS_Client construction function
46  *
47  * #### Parameters
48  *  - id: app's id
49  *
50  * #### Return
51  * None
52  *
53  */
54 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
55 {
56     HMI_NOTICE("homescreen-service","called.");
57     my_event = afb_api_make_event(request->api, id.c_str());
58 }
59
60 /**
61  * HS_Client destruction function
62  *
63  * #### Parameters
64  *  - null
65  *
66  * #### Return
67  * None
68  *
69  */
70 HS_Client::~HS_Client()
71 {
72     HMI_NOTICE("homescreen-service","called.");
73     afb_event_unref(my_event);
74 }
75
76 /**
77  * push tap_shortcut event
78  *
79  * #### Parameters
80  *  - request : the request
81  *
82  * #### Return
83  * 0 : success
84  * others : fail
85  *
86  */
87 int HS_Client::tap_shortcut(afb_req_t request)
88 {
89     HMI_NOTICE("homescreen-service","request appid = %s.", my_id.c_str());
90     struct json_object* push_obj = json_object_new_object();
91     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
92     _type, __FUNCTION__);
93     afb_event_push(my_event, push_obj);
94     return 0;
95 }
96
97 /**
98  * push on_screen_message event
99  *
100  * #### Parameters
101  *  - request : the request
102  *
103  * #### Return
104  * 0 : success
105  * others : fail
106  *
107  */
108 int HS_Client::on_screen_message(afb_req_t request)
109 {
110     int ret = 0;
111     const char* value = afb_req_value(request, _display_message);
112     if (value) {
113         HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, value);
114         struct json_object* push_obj = json_object_new_object();
115         hs_add_object_to_json_object_str( push_obj, 4, _display_message, value,
116         _type, __FUNCTION__);
117         afb_event_push(my_event, push_obj);
118     }
119     else {
120         HMI_NOTICE("homescreen-service","Please input display_message");
121         ret = AFB_EVENT_BAD_REQUEST;
122     }
123     return ret;
124 }
125
126 /**
127  * push on_screen_reply event
128  *
129  * #### Parameters
130  *  - request : the request
131  *
132  * #### Return
133  * 0 : success
134  * others : fail
135  *
136  */
137 int HS_Client::on_screen_reply(afb_req_t request)
138 {
139     int ret = 0;
140     const char* value = afb_req_value(request, _reply_message);
141     if (value) {
142         HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, value);
143         struct json_object* push_obj = json_object_new_object();
144         hs_add_object_to_json_object_str( push_obj, 4, _reply_message, value,
145         _type, __FUNCTION__);
146         afb_event_push(my_event, push_obj);
147     }
148     else {
149         HMI_NOTICE("homescreen-service","Please input reply_message");
150         ret = AFB_EVENT_BAD_REQUEST;
151     }
152     return ret;
153 }
154
155 /**
156  * subscribe event
157  *
158  * #### Parameters
159  *  - request : the request
160  *
161  * #### Return
162  * 0 : success
163  * others : fail
164  *
165  */
166 int HS_Client::subscribe(afb_req_t request)
167 {
168     HMI_NOTICE("homescreen-service"," called.");
169     int ret = 0;
170     const char *value = afb_req_value(request, _event);
171     if(value) {
172         HMI_NOTICE("homescreen-service","subscribe event %s", value);
173         if(!isSupportEvent(value)) {
174             HMI_NOTICE("homescreen-service","subscibe event isn't existing.");
175             ret = AFB_EVENT_BAD_REQUEST;
176         }
177         else {
178             event_list.insert(std::string(value));
179             if(!subscription) {
180                 ret = afb_req_subscribe(request, my_event);
181                 if(ret == 0) {
182                     subscription = true;
183                 }
184             }
185         }
186     }
187     else {
188         HMI_NOTICE("homescreen-service","Please input event name");
189         ret = AFB_EVENT_BAD_REQUEST;
190     }
191     return ret;
192 }
193
194 /**
195  * unsubscribe event
196  *
197  * #### Parameters
198  *  - request : the request
199  *
200  * #### Return
201  * 0 : success
202  * others : fail
203  *
204  */
205 int HS_Client::unsubscribe(afb_req_t request)
206 {
207     HMI_NOTICE("homescreen-service"," called.");
208     int ret = 0;
209     const char *value = afb_req_value(request, _event);
210     if(value) {
211         HMI_NOTICE("homescreen-service","unsubscribe %s event", value);
212         event_list.erase(std::string(value));
213         if(event_list.empty()) {
214             ret = afb_req_unsubscribe(request, my_event);
215         }
216     }
217     else {
218         HMI_NOTICE("homescreen-service","Please input event name");
219         ret = AFB_EVENT_BAD_REQUEST;
220     }
221     return ret;
222 }
223
224 /**
225  * showWindow event
226  *
227  * #### Parameters
228  * - request : the request
229  *
230  * #### Return
231  * 0 : success
232  * others : fail
233  *
234  */
235 int HS_Client::showWindow(afb_req_t request)
236 {
237     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
238     int ret = 0;
239     struct json_object* push_obj = json_object_new_object();
240     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
241     const char* param = afb_req_value(request, _parameter);
242     if(param) {
243         std::string req_appid = std::move(get_application_id(request));
244         if(req_appid.empty()) {
245             HMI_NOTICE("homescreen-service","can't get application identifier");
246             return AFB_REQ_GETAPPLICATIONID_ERROR;
247         }
248
249         struct json_object* param_obj = json_tokener_parse(param);
250         json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
251         json_object_object_add(push_obj, _parameter, param_obj);
252         afb_event_push(my_event, push_obj);
253     }
254     else {
255         HMI_ERROR("homescreen-service","please input correct parameter.");
256         ret = AFB_EVENT_BAD_REQUEST;
257     }
258     return ret;
259 }
260
261 /**
262  * hideWindow event
263  *
264  * #### Parameters
265  * - request : the request
266  *
267  * #### Return
268  * 0 : success
269  * others : fail
270  *
271  */
272 int HS_Client::hideWindow(afb_req_t request)
273 {
274     HMI_NOTICE("homescreen-service"," called.");
275     std::string req_appid = std::move(get_application_id(request));
276     if(req_appid.empty()) {
277         HMI_NOTICE("homescreen-service","can't get application identifier");
278         return AFB_REQ_GETAPPLICATIONID_ERROR;
279     }
280
281     struct json_object* push_obj = json_object_new_object();
282     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
283     _type, __FUNCTION__);
284     struct json_object* param_obj = json_object_new_object();
285     json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
286     json_object_object_add(push_obj, _parameter, param_obj);
287     afb_event_push(my_event, push_obj);
288     return 0;
289 }
290
291 /**
292  * replyShowWindow event
293  *
294  * #### Parameters
295  * - request : the request
296  *
297  * #### Return
298  * 0 : success
299  * others : fail
300  *
301  */
302 int HS_Client::replyShowWindow(afb_req_t request)
303 {
304     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
305     int ret = 0;
306     struct json_object* push_obj = json_object_new_object();
307     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
308     const char* param = afb_req_value(request, _parameter);
309     if(param) {
310         json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
311         afb_event_push(my_event, push_obj);
312     }
313     else {
314         HMI_ERROR("homescreen-service","please input correct parameter.");
315         ret = AFB_EVENT_BAD_REQUEST;
316     }
317     return ret;
318 }
319
320 /**
321  * showNotification event
322  *
323  * #### Parameters
324  *  - request : the request
325  *
326  * #### Return
327  * 0 : success
328  * others : fail
329  *
330  */
331 int HS_Client::showNotification(afb_req_t request)
332 {
333     HMI_NOTICE("homescreen-service"," called.");
334     int ret = 0;
335     const char *value = afb_req_value(request, _text);
336     if(value) {
337         HMI_NOTICE("homescreen-service","text is %s", value);
338         std::string appid =std::move(get_application_id(request));
339         if(appid.empty()) {
340             HMI_NOTICE("homescreen-service","can't get application identifier");
341             return AFB_REQ_GETAPPLICATIONID_ERROR;
342         }
343
344         struct json_object* param_obj = json_object_new_object();
345         const char *icon = afb_req_value(request, _icon);
346         if(icon) {
347             json_object_object_add(param_obj, _icon, json_object_new_string(icon));
348             json_object_object_add(param_obj, _text, json_object_new_string(value));
349             json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
350             struct json_object* push_obj = json_object_new_object();
351             hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
352             json_object_object_add(push_obj, _parameter, param_obj);
353             afb_event_push(my_event, push_obj);
354         }
355         else {
356             HMI_NOTICE("homescreen-service","please input icon.");
357             ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
358         }
359     }
360     else {
361         HMI_NOTICE("homescreen-service","please input text.");
362         ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
363     }
364
365     return ret;
366 }
367
368 /**
369  * showInformation event
370  *
371  * #### Parameters
372  *  - request : the request
373  *
374  * #### Return
375  * 0 : success
376  * others : fail
377  *
378  */
379 int HS_Client::showInformation(afb_req_t request)
380 {
381     HMI_NOTICE("homescreen-service"," called.");
382     int ret = 0;
383     const char *value = afb_req_value(request, _info);
384     if(value) {
385         HMI_NOTICE("homescreen-service","info is %s", value);
386         std::string appid = std::move(get_application_id(request));
387         if(appid.empty()) {
388             HMI_NOTICE("homescreen-service","can't get application identifier");
389             return AFB_REQ_GETAPPLICATIONID_ERROR;
390         }
391
392         struct json_object* param_obj = json_object_new_object();
393         json_object_object_add(param_obj, _info, json_object_new_string(value));
394         struct json_object* push_obj = json_object_new_object();
395         hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
396         json_object_object_add(push_obj, _parameter, param_obj);
397         afb_event_push(my_event, push_obj);
398     }
399     else {
400         HMI_NOTICE("homescreen-service","please input information.");
401         ret = AFB_REQ_SHOWINFORMATION_ERROR;
402     }
403
404     return ret;
405 }
406
407 /**
408  * check if client subscribe event
409  *
410  * #### Parameters
411  *  - event: homescreen event, tap_shortcut etc.
412  *
413  * #### Return
414  * true: found
415  * false: not found
416  *
417  */
418 bool HS_Client::checkEvent(const char* event)
419 {
420     auto ip = event_list.find(std::string(event));
421     if(ip == event_list.end())
422         return false;
423     else
424         return true;
425 }
426
427 /**
428  * check if event is supporting
429  *
430  * #### Parameters
431  *  - event: homescreen event, tap_shortcut etc.
432  *
433  * #### Return
434  * true: support
435  * false: not fosupportund
436  *
437  */
438 bool HS_Client::isSupportEvent(const char* event)
439 {
440     auto ip = func_list.find(std::string(event));
441     if(ip == func_list.end())
442         return false;
443     else
444         return true;
445 }
446
447 /**
448  * handle homescreen event
449  *
450  * #### Parameters
451  *  - request : the request
452  *  - verb: request verb name
453  *
454  * #### Return
455  * 0: success
456  * others: fail
457  *
458  */
459 int HS_Client::handleRequest(afb_req_t request, const char *verb)
460 {
461     HMI_NOTICE("homescreen-service","called.");
462     if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
463         return 0;
464
465     int ret = AFB_EVENT_BAD_REQUEST;
466     auto ip = func_list.find(std::string(verb));
467     if(ip != func_list.end()) {
468         HMI_NOTICE("homescreen-service","[%s]verb found", verb);
469         ret = (this->*(ip->second))(request);
470     }
471     return ret;
472 }