6c638e098b71b951070c226fe13cf685aed0e7ce
[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         event_list.insert(std::string(value));
160         if(!subscription) {
161             ret = afb_req_subscribe(request, my_event);
162             if(ret == 0) {
163                 subscription = true;
164             }
165         }
166     }
167     else {
168         HMI_NOTICE("homescreen-service","Please input event name");
169         ret = AFB_EVENT_BAD_REQUEST;
170     }
171     return ret;
172 }
173
174 /**
175  * unsubscribe event
176  *
177  * #### Parameters
178  *  - request : the request
179  *
180  * #### Return
181  * 0 : success
182  * others : fail
183  *
184  */
185 int HS_Client::unsubscribe(afb_req_t request)
186 {
187     HMI_NOTICE("homescreen-service"," called.");
188     int ret = 0;
189     const char *value = afb_req_value(request, _event);
190     if(value) {
191         HMI_NOTICE("homescreen-service","unsubscribe %s event", value);
192         event_list.erase(std::string(value));
193         if(event_list.empty()) {
194             ret = afb_req_unsubscribe(request, my_event);
195         }
196     }
197     else {
198         HMI_NOTICE("homescreen-service","Please input event name");
199         ret = AFB_EVENT_BAD_REQUEST;
200     }
201     return ret;
202 }
203
204 /**
205  * showWindow event
206  *
207  * #### Parameters
208  * - request : the request
209  *
210  * #### Return
211  * 0 : success
212  * others : fail
213  *
214  */
215 int HS_Client::showWindow(afb_req_t request)
216 {
217     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
218     int ret = 0;
219     struct json_object* push_obj = json_object_new_object();
220     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
221     const char* param = afb_req_value(request, _parameter);
222     if(param) {
223         std::string req_appid = std::move(get_application_id(request));
224         if(req_appid.empty()) {
225             HMI_NOTICE("homescreen-service","can't get application identifier");
226             return AFB_REQ_GETAPPLICATIONID_ERROR;
227         }
228
229         struct json_object* param_obj = json_tokener_parse(param);
230         json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
231         json_object_object_add(push_obj, _parameter, param_obj);
232         afb_event_push(my_event, push_obj);
233     }
234     else {
235         HMI_ERROR("homescreen-service","please input correct parameter.");
236         ret = AFB_EVENT_BAD_REQUEST;
237     }
238     return ret;
239 }
240
241 /**
242  * hideWindow event
243  *
244  * #### Parameters
245  * - request : the request
246  *
247  * #### Return
248  * 0 : success
249  * others : fail
250  *
251  */
252 int HS_Client::hideWindow(afb_req_t request)
253 {
254     HMI_NOTICE("homescreen-service"," called.");
255     std::string req_appid = std::move(get_application_id(request));
256     if(req_appid.empty()) {
257         HMI_NOTICE("homescreen-service","can't get application identifier");
258         return AFB_REQ_GETAPPLICATIONID_ERROR;
259     }
260
261     struct json_object* push_obj = json_object_new_object();
262     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
263     _type, __FUNCTION__);
264     struct json_object* param_obj = json_object_new_object();
265     json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
266     json_object_object_add(push_obj, _parameter, param_obj);
267     afb_event_push(my_event, push_obj);
268     return 0;
269 }
270
271 /**
272  * replyShowWindow event
273  *
274  * #### Parameters
275  * - request : the request
276  *
277  * #### Return
278  * 0 : success
279  * others : fail
280  *
281  */
282 int HS_Client::replyShowWindow(afb_req_t request)
283 {
284     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
285     int ret = 0;
286     struct json_object* push_obj = json_object_new_object();
287     hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
288     const char* param = afb_req_value(request, _parameter);
289     if(param) {
290         json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
291         afb_event_push(my_event, push_obj);
292     }
293     else {
294         HMI_ERROR("homescreen-service","please input correct parameter.");
295         ret = AFB_EVENT_BAD_REQUEST;
296     }
297     return ret;
298 }
299
300 /**
301  * showNotification event
302  *
303  * #### Parameters
304  *  - request : the request
305  *
306  * #### Return
307  * 0 : success
308  * others : fail
309  *
310  */
311 int HS_Client::showNotification(afb_req_t request)
312 {
313     HMI_NOTICE("homescreen-service"," called.");
314     int ret = 0;
315     const char *value = afb_req_value(request, _text);
316     if(value) {
317         HMI_NOTICE("homescreen-service","text is %s", value);
318         std::string appid =std::move(get_application_id(request));
319         if(appid.empty()) {
320             HMI_NOTICE("homescreen-service","can't get application identifier");
321             return AFB_REQ_GETAPPLICATIONID_ERROR;
322         }
323
324         struct json_object* param_obj = json_object_new_object();
325         const char *icon = afb_req_value(request, _icon);
326         if(icon) {
327             json_object_object_add(param_obj, _icon, json_object_new_string(icon));
328             json_object_object_add(param_obj, _text, json_object_new_string(value));
329             json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
330             struct json_object* push_obj = json_object_new_object();
331             hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
332             json_object_object_add(push_obj, _parameter, param_obj);
333             afb_event_push(my_event, push_obj);
334         }
335         else {
336             HMI_NOTICE("homescreen-service","please input icon.");
337             ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
338         }
339     }
340     else {
341         HMI_NOTICE("homescreen-service","please input text.");
342         ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
343     }
344
345     return ret;
346 }
347
348 /**
349  * showInformation event
350  *
351  * #### Parameters
352  *  - request : the request
353  *
354  * #### Return
355  * 0 : success
356  * others : fail
357  *
358  */
359 int HS_Client::showInformation(afb_req_t request)
360 {
361     HMI_NOTICE("homescreen-service"," called.");
362     int ret = 0;
363     const char *value = afb_req_value(request, _info);
364     if(value) {
365         HMI_NOTICE("homescreen-service","info is %s", value);
366         std::string appid = std::move(get_application_id(request));
367         if(appid.empty()) {
368             HMI_NOTICE("homescreen-service","can't get application identifier");
369             return AFB_REQ_GETAPPLICATIONID_ERROR;
370         }
371
372         struct json_object* param_obj = json_object_new_object();
373         json_object_object_add(param_obj, _info, json_object_new_string(value));
374         struct json_object* push_obj = json_object_new_object();
375         hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
376         json_object_object_add(push_obj, _parameter, param_obj);
377         afb_event_push(my_event, push_obj);
378     }
379     else {
380         HMI_NOTICE("homescreen-service","please input information.");
381         ret = AFB_REQ_SHOWINFORMATION_ERROR;
382     }
383
384     return ret;
385 }
386
387 /**
388  * check if client subscribe event
389  *
390  * #### Parameters
391  *  - event: homescreen event, tap_shortcut etc.
392  *
393  * #### Return
394  * true: found
395  * false: not found
396  *
397  */
398 bool HS_Client::checkEvent(const char* event)
399 {
400     auto ip = event_list.find(std::string(event));
401     if(ip == event_list.end())
402         return false;
403     else
404         return true;
405 }
406
407 /**
408  * handle homescreen event
409  *
410  * #### Parameters
411  *  - request : the request
412  *  - verb: request verb name
413  *
414  * #### Return
415  * 0: success
416  * others: fail
417  *
418  */
419 int HS_Client::handleRequest(afb_req_t request, const char *verb)
420 {
421     HMI_NOTICE("homescreen-service","called.");
422     if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
423         return 0;
424
425     int ret = AFB_EVENT_BAD_REQUEST;
426     auto ip = func_list.find(std::string(verb));
427     if(ip != func_list.end()) {
428         HMI_NOTICE("homescreen-service","[%s]verb found", verb);
429         ret = (this->*(ip->second))(request);
430     }
431     return ret;
432 }