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