2 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "hs-client.h"
18 #include "hs-helper.h"
19 #include "hmi-debug.h"
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";
29 * HS_Client construction function
38 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
40 HMI_NOTICE("homescreen-service","called.");
41 my_event = afb_api_make_event(request->api, id.c_str());
45 * HS_Client destruction function
54 HS_Client::~HS_Client()
56 HMI_NOTICE("homescreen-service","called.");
57 afb_event_unref(my_event);
61 * push tap_shortcut event
70 int HS_Client::tap_shortcut(const char* appid)
72 if(!checkEvent(__FUNCTION__))
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,
79 afb_event_push(my_event, push_obj);
84 * push on_screen_message event
87 * - message: post message.
93 int HS_Client::on_screen_message(afb_req_t request, const char* message)
95 if(!checkEvent(__FUNCTION__))
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);
107 * push on_screen_reply event
110 * - message: reply message.
116 int HS_Client::on_screen_reply(afb_req_t request, const char* message)
118 if(!checkEvent(__FUNCTION__))
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);
133 * - event: homescreen event, tap_shortcut etc.
139 int HS_Client::subscribe(afb_req_t request, const char* event)
142 auto ip = event_list.find(std::string(event));
143 if(ip == event_list.end()) {
144 event_list[std::string(event)] = 0;
147 ret = afb_req_subscribe(request, my_event);
159 * - event: homescreen event, tap_shortcut etc.
165 int HS_Client::unsubscribe(afb_req_t request, const char* event)
168 event_list.erase(std::string(event));
169 if(event_list.empty()) {
170 ret = afb_req_unsubscribe(request, my_event);
176 * check if client subscribe event
179 * - event: homescreen event, tap_shortcut etc.
186 bool HS_Client::checkEvent(const char* event)
188 auto ip = event_list.find(std::string(event));
189 if(ip == event_list.end())
198 * input contents : {"application_id":"the appid that want to display", "parameter":{"area": "display area", ...}}
201 * - request : the request
202 * - appid : the appid that want to display
209 int HS_Client::showWindow(afb_req_t request, const char* appid)
211 if(!checkEvent(__FUNCTION__))
214 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
215 struct json_object* push_obj = json_object_new_object();
216 hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
217 const char* param = afb_req_value(request, _parameter);
219 std::string req_appid = std::move(get_application_id(request));
220 if(req_appid.empty()) {
221 HMI_NOTICE("homescreen-service","can't get application identifier");
222 return AFB_REQ_GETAPPLICATIONID_ERROR;
225 struct json_object* param_obj = json_tokener_parse(param);
226 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
227 json_object_object_add(push_obj, _parameter, param_obj);
230 HMI_ERROR("homescreen-service","please input correct parameter.");
231 return AFB_EVENT_BAD_REQUEST;
233 afb_event_push(my_event, push_obj);
240 * input contents : {"application_id":"the appid that want to hide"}
243 * - request : the request
250 int HS_Client::hideWindow(afb_req_t request)
252 if(!checkEvent(__FUNCTION__))
255 HMI_NOTICE("homescreen-service","%s called.", __FUNCTION__);
256 std::string req_appid = std::move(get_application_id(request));
257 if(req_appid.empty()) {
258 HMI_NOTICE("homescreen-service","can't get application identifier");
259 return AFB_REQ_GETAPPLICATIONID_ERROR;
262 struct json_object* push_obj = json_object_new_object();
263 hs_add_object_to_json_object_str( push_obj, 4, _application_id, req_appid.c_str(),
264 _type, __FUNCTION__);
265 afb_event_push(my_event, push_obj);
270 * replyShowWindow event
272 * input contens : {"application_id": "the appid that want to reply", "parameter": {...}}
275 * - request : the request
276 * - appid : the appid that want to reply
283 int HS_Client::replyShowWindow(afb_req_t request, const char* appid)
285 if(!checkEvent(__FUNCTION__))
288 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, appid);
289 struct json_object* push_obj = json_object_new_object();
290 hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid, _type, __FUNCTION__);
291 const char* param = afb_req_value(request, _parameter);
293 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
296 HMI_ERROR("homescreen-service","please input correct parameter.");
297 return AFB_EVENT_BAD_REQUEST;
300 afb_event_push(my_event, push_obj);
305 * showNotification event
307 * input contents : {"icon": "icon path", "text": "message contents"}
310 * - request : the request
317 int HS_Client::showNotification(afb_req_t request)
320 const char *value = afb_req_value(request, _text);
322 HMI_NOTICE("homescreen-service","text is %s", value);
323 std::string appid =std::move(get_application_id(request));
325 HMI_NOTICE("homescreen-service","can't get application identifier");
326 return AFB_REQ_GETAPPLICATIONID_ERROR;
329 struct json_object* param_obj = json_object_new_object();
330 const char *icon = afb_req_value(request, _icon);
332 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
333 json_object_object_add(param_obj, _text, json_object_new_string(value));
334 struct json_object* push_obj = json_object_new_object();
335 hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid.c_str(), _type, __FUNCTION__);
336 json_object_object_add(push_obj, _parameter, param_obj);
337 afb_event_push(my_event, push_obj);
340 HMI_NOTICE("homescreen-service","please input icon.");
341 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
345 HMI_NOTICE("homescreen-service","please input text.");
346 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
353 * showInformation event
355 * input contents : {"info": "information contents"}
358 * - request : the request
365 int HS_Client::showInformation(afb_req_t request)
368 const char *value = afb_req_value(request, _info);
370 HMI_NOTICE("homescreen-service","info is %s", value);
371 std::string appid = std::move(get_application_id(request));
373 HMI_NOTICE("homescreen-service","can't get application identifier");
374 return AFB_REQ_GETAPPLICATIONID_ERROR;
377 struct json_object* param_obj = json_object_new_object();
378 json_object_object_add(param_obj, _info, json_object_new_string(value));
379 struct json_object* push_obj = json_object_new_object();
380 hs_add_object_to_json_object_str( push_obj, 4, _application_id, appid.c_str(), _type, __FUNCTION__);
381 json_object_object_add(push_obj, _parameter, param_obj);
382 afb_event_push(my_event, push_obj);
385 HMI_NOTICE("homescreen-service","please input information.");
386 ret = AFB_REQ_SHOWINFORMATION_ERROR;