src/homescreen, hs-client, hs-proxy: Fix trivial unused warnings
[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 <cstring>
18 #include "hs-client.h"
19 #include "hs-helper.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     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     afb_event_unref(my_event);
73 }
74
75 /**
76  * push tap_shortcut event
77  *
78  * #### Parameters
79  *  - request : the request
80  *
81  * #### Return
82  * 0 : success
83  * others : fail
84  *
85  */
86 int HS_Client::tap_shortcut(afb_req_t request)
87 {
88     (void) request;
89
90     AFB_INFO("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         AFB_INFO("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         AFB_WARNING("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         AFB_INFO("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         AFB_WARNING("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     int ret = 0;
170     const char *value = afb_req_value(request, _event);
171     if(value) {
172         AFB_INFO("subscribe event %s", value);
173         if(!isSupportEvent(value)) {
174             AFB_WARNING("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         AFB_WARNING("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     int ret = 0;
208     const char *value = afb_req_value(request, _event);
209     if(value) {
210         AFB_INFO("unsubscribe %s event", value);
211         event_list.erase(std::string(value));
212         if(event_list.empty()) {
213             ret = afb_req_unsubscribe(request, my_event);
214         }
215     }
216     else {
217         AFB_WARNING("Please input event name");
218         ret = AFB_EVENT_BAD_REQUEST;
219     }
220     return ret;
221 }
222
223 /**
224  * showWindow event
225  *
226  * #### Parameters
227  * - request : the request
228  *
229  * #### Return
230  * 0 : success
231  * others : fail
232  *
233  */
234 int HS_Client::showWindow(afb_req_t request)
235 {
236     AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
237     int ret = 0;
238     const char* param = afb_req_value(request, _parameter);
239     if(param) {
240         std::string req_appid = std::move(get_application_id(request));
241         if(req_appid.empty()) {
242             AFB_WARNING("can't get application identifier");
243             return AFB_REQ_GETAPPLICATIONID_ERROR;
244         }
245
246         struct json_object* push_obj = json_object_new_object();
247         hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
248         struct json_object* param_obj = json_tokener_parse(param);
249         json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
250         json_object_object_add(push_obj, _parameter, param_obj);
251         afb_event_push(my_event, push_obj);
252     }
253     else {
254         AFB_WARNING("please input correct parameter.");
255         ret = AFB_EVENT_BAD_REQUEST;
256     }
257     return ret;
258 }
259
260 /**
261  * hideWindow event
262  *
263  * #### Parameters
264  * - request : the request
265  *
266  * #### Return
267  * 0 : success
268  * others : fail
269  *
270  */
271 int HS_Client::hideWindow(afb_req_t request)
272 {
273     std::string req_appid = std::move(get_application_id(request));
274     if(req_appid.empty()) {
275         AFB_WARNING("can't get application identifier");
276         return AFB_REQ_GETAPPLICATIONID_ERROR;
277     }
278
279     struct json_object* push_obj = json_object_new_object();
280     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
281     _type, __FUNCTION__);
282     struct json_object* param_obj = json_object_new_object();
283     json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
284     json_object_object_add(push_obj, _parameter, param_obj);
285     afb_event_push(my_event, push_obj);
286     return 0;
287 }
288
289 /**
290  * replyShowWindow event
291  *
292  * #### Parameters
293  * - request : the request
294  *
295  * #### Return
296  * 0 : success
297  * others : fail
298  *
299  */
300 int HS_Client::replyShowWindow(afb_req_t request)
301 {
302     AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
303     int ret = 0;
304     const char* param = afb_req_value(request, _parameter);
305     if(param) {
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         json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
309         afb_event_push(my_event, push_obj);
310     }
311     else {
312         AFB_WARNING("please input correct parameter.");
313         ret = AFB_EVENT_BAD_REQUEST;
314     }
315     return ret;
316 }
317
318 /**
319  * showNotification event
320  *
321  * #### Parameters
322  *  - request : the request
323  *
324  * #### Return
325  * 0 : success
326  * others : fail
327  *
328  */
329 int HS_Client::showNotification(afb_req_t request)
330 {
331     int ret = 0;
332     const char *value = afb_req_value(request, _text);
333     if(value) {
334         AFB_INFO("text is %s", value);
335         std::string appid =std::move(get_application_id(request));
336         if(appid.empty()) {
337             AFB_WARNING("can't get application identifier");
338             return AFB_REQ_GETAPPLICATIONID_ERROR;
339         }
340
341         const char *icon = afb_req_value(request, _icon);
342         if(icon) {
343             struct json_object* param_obj = json_object_new_object();
344             json_object_object_add(param_obj, _icon, json_object_new_string(icon));
345             json_object_object_add(param_obj, _text, json_object_new_string(value));
346             json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
347             struct json_object* push_obj = json_object_new_object();
348             hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
349             json_object_object_add(push_obj, _parameter, param_obj);
350             afb_event_push(my_event, push_obj);
351         }
352         else {
353             AFB_WARNING("please input icon.");
354             ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
355         }
356     }
357     else {
358         AFB_WARNING("please input text.");
359         ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
360     }
361
362     return ret;
363 }
364
365 /**
366  * showInformation event
367  *
368  * #### Parameters
369  *  - request : the request
370  *
371  * #### Return
372  * 0 : success
373  * others : fail
374  *
375  */
376 int HS_Client::showInformation(afb_req_t request)
377 {
378     int ret = 0;
379     const char *value = afb_req_value(request, _info);
380     if(value) {
381         AFB_INFO("info is %s", value);
382         std::string appid = std::move(get_application_id(request));
383         if(appid.empty()) {
384             AFB_WARNING("can't get application identifier");
385             return AFB_REQ_GETAPPLICATIONID_ERROR;
386         }
387
388         struct json_object* param_obj = json_object_new_object();
389         json_object_object_add(param_obj, _info, json_object_new_string(value));
390         struct json_object* push_obj = json_object_new_object();
391         hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
392         json_object_object_add(push_obj, _parameter, param_obj);
393         afb_event_push(my_event, push_obj);
394     }
395     else {
396         AFB_WARNING("please input information.");
397         ret = AFB_REQ_SHOWINFORMATION_ERROR;
398     }
399
400     return ret;
401 }
402
403 /**
404  * check if client subscribe event
405  *
406  * #### Parameters
407  *  - event: homescreen event, tap_shortcut etc.
408  *
409  * #### Return
410  * true: found
411  * false: not found
412  *
413  */
414 bool HS_Client::checkEvent(const char* event)
415 {
416     auto ip = event_list.find(std::string(event));
417     if(ip == event_list.end())
418         return false;
419     else
420         return true;
421 }
422
423 /**
424  * check if event is supporting
425  *
426  * #### Parameters
427  *  - event: homescreen event, tap_shortcut etc.
428  *
429  * #### Return
430  * true: support
431  * false: not fosupportund
432  *
433  */
434 bool HS_Client::isSupportEvent(const char* event)
435 {
436     auto ip = func_list.find(std::string(event));
437     if(ip == func_list.end())
438         return false;
439     else
440         return true;
441 }
442
443 /**
444  * handle homescreen event
445  *
446  * #### Parameters
447  *  - request : the request
448  *  - verb: request verb name
449  *
450  * #### Return
451  * 0: success
452  * others: fail
453  *
454  */
455 int HS_Client::handleRequest(afb_req_t request, const char *verb)
456 {
457     if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
458         return 0;
459
460     int ret = AFB_EVENT_BAD_REQUEST;
461     auto ip = func_list.find(std::string(verb));
462     if(ip != func_list.end() && ip->second != nullptr) {
463         AFB_INFO("[%s]verb found", verb);
464         ret = (this->*(ip->second))(request);
465     }
466     return ret;
467 }
468
469 /**
470  * push event
471  *
472  * #### Parameters
473  *  - event : the event want to push
474  *  - param : the parameter contents of event
475  *
476  * #### Return
477  * 0 : success
478  * others : fail
479  *
480  */
481 int HS_Client::pushEvent(const char *event, struct json_object *param)
482 {
483     if(!checkEvent(event))
484         return 0;
485
486     AFB_INFO("called, event=%s.",event);
487     struct json_object* push_obj = json_object_new_object();
488     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, event);
489     if(param != nullptr)
490         json_object_object_add(push_obj, _parameter, param);
491     afb_event_push(my_event, push_obj);
492     return 0;
493 }