add new features in homescreen-service and homescreen
[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 _type[] = "type";
22 static const char _text[] = "text";
23 static const char _info[] = "info";
24 static const char _icon[] = "icon";
25 static const char _parameter[] = "parameter";
26 static const char _replyto[] = "replyto";
27
28 /**
29  * HS_Client construction function
30  *
31  * #### Parameters
32  *  - id: app's id
33  *
34  * #### Return
35  * None
36  *
37  */
38 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
39 {
40     HMI_NOTICE("homescreen-service","called.");
41     my_event = afb_api_make_event(request->api, id.c_str());
42 }
43
44 /**
45  * HS_Client destruction function
46  *
47  * #### Parameters
48  *  - null
49  *
50  * #### Return
51  * None
52  *
53  */
54 HS_Client::~HS_Client()
55 {
56     HMI_NOTICE("homescreen-service","called.");
57     afb_event_unref(my_event);
58 }
59
60 /**
61  * push tap_shortcut event
62  *
63  * #### Parameters
64  *  - appid: app's id.
65  *
66  * #### Return
67  * result
68  *
69  */
70 int HS_Client::tap_shortcut(const char* appid)
71 {
72     if(!checkEvent(__FUNCTION__))
73         return 0;
74
75     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
76     struct json_object* push_obj = json_object_new_object();
77     hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid,
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  *  - message: post message.
88  *
89  * #### Return
90  * result
91  *
92  */
93 int HS_Client::on_screen_message(afb_req_t request, const char* message)
94 {
95     if(!checkEvent(__FUNCTION__))
96         return 0;
97
98     HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
99     struct json_object* push_obj = json_object_new_object();
100     hs_add_object_to_json_object_str( push_obj, 4, _display_message, message,
101     _type, __FUNCTION__);
102     afb_event_push(my_event, push_obj);
103     return 0;
104 }
105
106 /**
107  * push on_screen_reply event
108  *
109  * #### Parameters
110  *  - message: reply message.
111  *
112  * #### Return
113  * result
114  *
115  */
116 int HS_Client::on_screen_reply(afb_req_t request, const char* message)
117 {
118     if(!checkEvent(__FUNCTION__))
119         return 0;
120
121     HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, message);
122     struct json_object* push_obj = json_object_new_object();
123     hs_add_object_to_json_object_str( push_obj, 4, _reply_message, message,
124     _type, __FUNCTION__);
125     afb_event_push(my_event, push_obj);
126     return 0;
127 }
128
129 /**
130  * subscribe event
131  *
132  * #### Parameters
133  *  - event: homescreen event, tap_shortcut etc.
134  *
135  * #### Return
136  * result
137  *
138  */
139 int HS_Client::subscribe(afb_req_t request, const char* event)
140 {
141     int ret = 0;
142     auto ip = event_list.find(std::string(event));
143     if(ip == event_list.end()) {
144         event_list[std::string(event)] = 0;
145         ret = afb_req_subscribe(request, my_event);
146     }
147     return ret;
148 }
149
150 /**
151  * unsubscribe event
152  *
153  * #### Parameters
154  *  - event: homescreen event, tap_shortcut etc.
155  *
156  * #### Return
157  * result
158  *
159  */
160 int HS_Client::unsubscribe(afb_req_t request, const char* event)
161 {
162     int ret = 0;
163     event_list.erase(std::string(event));
164     if(event_list.empty()) {
165         ret = afb_req_unsubscribe(request, my_event);
166     }
167     return ret;
168 }
169
170 /**
171  * check if client subscribe event
172  *
173  * #### Parameters
174  *  - event: homescreen event, tap_shortcut etc.
175  *
176  * #### Return
177  * true: found
178  * false: not found
179  *
180  */
181 bool HS_Client::checkEvent(const char* event)
182 {
183     auto ip = event_list.find(std::string(event));
184     if(ip == event_list.end())
185         return false;
186     else
187         return true;
188 }
189
190 /**
191  * showWindow event
192  *
193  * input contents : {"application_id":"the appid that want to display", "parameter":{"area": "display area", ...}}
194  *
195  * #### Parameters
196  * - request : the request
197  * - appid : the appid that want to display
198  *
199  * #### Return
200  * 0 : success
201  * others : fail
202  *
203  */
204 int HS_Client::showWindow(afb_req_t request, const char* appid)
205 {
206     if(!checkEvent(__FUNCTION__))
207         return 0;
208
209     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
210     struct json_object* push_obj = json_object_new_object();
211     hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
212     const char* param = afb_req_value(request, _parameter);
213     if(param) {
214         const char* req_appid = afb_req_get_application_id(request);
215         struct json_object* param_obj = json_tokener_parse(param);
216         json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid));
217         json_object_object_add(push_obj, _parameter, param_obj);
218     }
219     else {
220         HMI_ERROR("homescreen-service","please input correct parameter.");
221         return AFB_EVENT_BAD_REQUEST;
222     }
223     afb_event_push(my_event, push_obj);
224     return 0;
225 }
226
227 /**
228  * hideWindow event
229  *
230  * input contents : {"application_id":"the appid that want to hide"}
231  *
232  * #### Parameters
233  * - request : the request
234  *
235  * #### Return
236  * 0 : success
237  * others : fail
238  *
239  */
240 int HS_Client::hideWindow(afb_req_t request)
241 {
242     if(!checkEvent(__FUNCTION__))
243         return 0;
244
245     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__);
246     const char* req_appid = afb_req_get_application_id(request);
247     struct json_object* push_obj = json_object_new_object();
248     hs_add_object_to_json_object_str( push_obj, 4, _application_id, req_appid,
249     _type, __FUNCTION__);
250     afb_event_push(my_event, push_obj);
251     return 0;
252 }
253
254 /**
255  * replyShowWindow event
256  *
257  * input contens : {"application_id": "the appid that want to reply",  "parameter": {...}}
258  *
259  * #### Parameters
260  * - request : the request
261  * - appid : the appid that want to reply
262  *
263  * #### Return
264  * 0 : success
265  * others : fail
266  *
267  */
268 int HS_Client::replyShowWindow(afb_req_t request, const char* appid)
269 {
270     if(!checkEvent(__FUNCTION__))
271         return 0;
272
273     HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
274     struct json_object* push_obj = json_object_new_object();
275     hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
276     const char* param = afb_req_value(request, _parameter);
277     if(param) {
278         json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
279     }
280     else {
281         HMI_ERROR("homescreen-service","please input correct parameter.");
282         return AFB_EVENT_BAD_REQUEST;
283     }
284
285     afb_event_push(my_event, push_obj);
286     return 0;
287 }
288
289 /**
290  * showNotification event
291  *
292  * input contents : {"icon": "icon path", "text": "message contents"}
293  *
294  * #### Parameters
295  *  - request : the request
296  *
297  * #### Return
298  * 0 : success
299  * others : fail
300  *
301  */
302 int HS_Client::showNotification(afb_req_t request)
303 {
304     int ret = 0;
305     const char *value = afb_req_value(request, _text);
306     if(value) {
307         HMI_NOTICE("homescreen-service","text is %s", value);
308         const char* appid = afb_req_get_application_id(request);
309         struct json_object* param_obj = json_object_new_object();
310         const char *icon = afb_req_value(request, _icon);
311         if(icon) {
312             json_object_object_add(param_obj, _icon, json_object_new_string(icon));
313             json_object_object_add(param_obj, _text, json_object_new_string(value));
314             struct json_object* push_obj = json_object_new_object();
315             hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
316             json_object_object_add(push_obj, _parameter, param_obj);
317             afb_event_push(my_event, push_obj);
318         }
319         else {
320             HMI_NOTICE("homescreen-service","please input icon.");
321             ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
322         }
323     }
324     else {
325         HMI_NOTICE("homescreen-service","please input text.");
326         ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
327     }
328
329     return ret;
330 }
331
332 /**
333  * showInformation event
334  *
335  * input contents : {"info": "information contents"}
336  *
337  * #### Parameters
338  *  - request : the request
339  *
340  * #### Return
341  * 0 : success
342  * others : fail
343  *
344  */
345 int HS_Client::showInformation(afb_req_t request)
346 {
347     int ret = 0;
348     const char *value = afb_req_value(request, _info);
349     if(value) {
350         HMI_NOTICE("homescreen-service","info is %s", value);
351         const char* appid = afb_req_get_application_id(request);
352         struct json_object* param_obj = json_object_new_object();
353         json_object_object_add(param_obj, _info, json_object_new_string(value));
354         struct json_object* push_obj = json_object_new_object();
355         hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
356         json_object_object_add(push_obj, _parameter, param_obj);
357         afb_event_push(my_event, push_obj);
358     }
359     else {
360         HMI_NOTICE("homescreen-service","please input information.");
361         ret = AFB_REQ_SHOWINFORMATION_ERROR;
362     }
363
364     return ret;
365 }