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 _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";
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}
46 * HS_Client construction function
55 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
57 HMI_NOTICE("homescreen-service","called.");
58 my_event = afb_api_make_event(request->api, id.c_str());
62 * HS_Client destruction function
71 HS_Client::~HS_Client()
73 HMI_NOTICE("homescreen-service","called.");
74 afb_event_unref(my_event);
78 * push tap_shortcut event
81 * - request : the request
88 int HS_Client::tap_shortcut(afb_req_t request)
90 HMI_NOTICE("homescreen-service","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(),
94 afb_event_push(my_event, push_obj);
99 * push on_screen_message event
102 * - request : the request
109 int HS_Client::on_screen_message(afb_req_t request)
112 const char* value = afb_req_value(request, _display_message);
114 HMI_NOTICE("homescreen-service","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);
121 HMI_NOTICE("homescreen-service","Please input display_message");
122 ret = AFB_EVENT_BAD_REQUEST;
128 * push on_screen_reply event
131 * - request : the request
138 int HS_Client::on_screen_reply(afb_req_t request)
141 const char* value = afb_req_value(request, _reply_message);
143 HMI_NOTICE("homescreen-service","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);
150 HMI_NOTICE("homescreen-service","Please input reply_message");
151 ret = AFB_EVENT_BAD_REQUEST;
160 * - request : the request
167 int HS_Client::subscribe(afb_req_t request)
169 HMI_NOTICE("homescreen-service"," called.");
171 const char *value = afb_req_value(request, _event);
173 HMI_NOTICE("homescreen-service","subscribe event %s", value);
174 if(!isSupportEvent(value)) {
175 HMI_NOTICE("homescreen-service","subscibe event isn't existing.");
176 ret = AFB_EVENT_BAD_REQUEST;
179 event_list.insert(std::string(value));
181 ret = afb_req_subscribe(request, my_event);
189 HMI_NOTICE("homescreen-service","Please input event name");
190 ret = AFB_EVENT_BAD_REQUEST;
199 * - request : the request
206 int HS_Client::unsubscribe(afb_req_t request)
208 HMI_NOTICE("homescreen-service"," called.");
210 const char *value = afb_req_value(request, _event);
212 HMI_NOTICE("homescreen-service","unsubscribe %s event", value);
213 event_list.erase(std::string(value));
214 if(event_list.empty()) {
215 ret = afb_req_unsubscribe(request, my_event);
219 HMI_NOTICE("homescreen-service","Please input event name");
220 ret = AFB_EVENT_BAD_REQUEST;
229 * - request : the request
236 int HS_Client::showWindow(afb_req_t request)
238 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
240 struct json_object* push_obj = json_object_new_object();
241 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
242 const char* param = afb_req_value(request, _parameter);
244 std::string req_appid = std::move(get_application_id(request));
245 if(req_appid.empty()) {
246 HMI_NOTICE("homescreen-service","can't get application identifier");
247 return AFB_REQ_GETAPPLICATIONID_ERROR;
250 struct json_object* param_obj = json_tokener_parse(param);
251 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
252 json_object_object_add(push_obj, _parameter, param_obj);
253 afb_event_push(my_event, push_obj);
256 HMI_ERROR("homescreen-service","please input correct parameter.");
257 ret = AFB_EVENT_BAD_REQUEST;
266 * - request : the request
273 int HS_Client::hideWindow(afb_req_t request)
275 HMI_NOTICE("homescreen-service"," called.");
276 std::string req_appid = std::move(get_application_id(request));
277 if(req_appid.empty()) {
278 HMI_NOTICE("homescreen-service","can't get application identifier");
279 return AFB_REQ_GETAPPLICATIONID_ERROR;
282 struct json_object* push_obj = json_object_new_object();
283 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
284 _type, __FUNCTION__);
285 struct json_object* param_obj = json_object_new_object();
286 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
287 json_object_object_add(push_obj, _parameter, param_obj);
288 afb_event_push(my_event, push_obj);
293 * replyShowWindow event
296 * - request : the request
303 int HS_Client::replyShowWindow(afb_req_t request)
305 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
307 struct json_object* push_obj = json_object_new_object();
308 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
309 const char* param = afb_req_value(request, _parameter);
311 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
312 afb_event_push(my_event, push_obj);
315 HMI_ERROR("homescreen-service","please input correct parameter.");
316 ret = AFB_EVENT_BAD_REQUEST;
322 * showNotification event
325 * - request : the request
332 int HS_Client::showNotification(afb_req_t request)
334 HMI_NOTICE("homescreen-service"," called.");
336 const char *value = afb_req_value(request, _text);
338 HMI_NOTICE("homescreen-service","text is %s", value);
339 std::string appid =std::move(get_application_id(request));
341 HMI_NOTICE("homescreen-service","can't get application identifier");
342 return AFB_REQ_GETAPPLICATIONID_ERROR;
345 struct json_object* param_obj = json_object_new_object();
346 const char *icon = afb_req_value(request, _icon);
348 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
349 json_object_object_add(param_obj, _text, json_object_new_string(value));
350 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
351 struct json_object* push_obj = json_object_new_object();
352 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
353 json_object_object_add(push_obj, _parameter, param_obj);
354 afb_event_push(my_event, push_obj);
357 HMI_NOTICE("homescreen-service","please input icon.");
358 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
362 HMI_NOTICE("homescreen-service","please input text.");
363 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
370 * showInformation event
373 * - request : the request
380 int HS_Client::showInformation(afb_req_t request)
382 HMI_NOTICE("homescreen-service"," called.");
384 const char *value = afb_req_value(request, _info);
386 HMI_NOTICE("homescreen-service","info is %s", value);
387 std::string appid = std::move(get_application_id(request));
389 HMI_NOTICE("homescreen-service","can't get application identifier");
390 return AFB_REQ_GETAPPLICATIONID_ERROR;
393 struct json_object* param_obj = json_object_new_object();
394 json_object_object_add(param_obj, _info, json_object_new_string(value));
395 struct json_object* push_obj = json_object_new_object();
396 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
397 json_object_object_add(push_obj, _parameter, param_obj);
398 afb_event_push(my_event, push_obj);
401 HMI_NOTICE("homescreen-service","please input information.");
402 ret = AFB_REQ_SHOWINFORMATION_ERROR;
409 * check if client subscribe event
412 * - event: homescreen event, tap_shortcut etc.
419 bool HS_Client::checkEvent(const char* event)
421 auto ip = event_list.find(std::string(event));
422 if(ip == event_list.end())
429 * check if event is supporting
432 * - event: homescreen event, tap_shortcut etc.
436 * false: not fosupportund
439 bool HS_Client::isSupportEvent(const char* event)
441 auto ip = func_list.find(std::string(event));
442 if(ip == func_list.end())
449 * handle homescreen event
452 * - request : the request
453 * - verb: request verb name
460 int HS_Client::handleRequest(afb_req_t request, const char *verb)
462 HMI_NOTICE("homescreen-service","called.");
463 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
466 int ret = AFB_EVENT_BAD_REQUEST;
467 auto ip = func_list.find(std::string(verb));
468 if(ip != func_list.end()) {
469 HMI_NOTICE("homescreen-service","[%s]verb found", verb);
470 ret = (this->*(ip->second))(request);