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