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}
45 * HS_Client construction function
54 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
56 HMI_NOTICE("homescreen-service","called.");
57 my_event = afb_api_make_event(request->api, id.c_str());
61 * HS_Client destruction function
70 HS_Client::~HS_Client()
72 HMI_NOTICE("homescreen-service","called.");
73 afb_event_unref(my_event);
77 * push tap_shortcut event
80 * - request : the request
87 int HS_Client::tap_shortcut(afb_req_t request)
89 HMI_NOTICE("homescreen-service","request appid = %s.", my_id.c_str());
90 struct json_object* push_obj = json_object_new_object();
91 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
93 afb_event_push(my_event, push_obj);
98 * push on_screen_message event
101 * - request : the request
108 int HS_Client::on_screen_message(afb_req_t request)
111 const char* value = afb_req_value(request, _display_message);
113 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, value);
114 struct json_object* push_obj = json_object_new_object();
115 hs_add_object_to_json_object_str( push_obj, 4, _display_message, value,
116 _type, __FUNCTION__);
117 afb_event_push(my_event, push_obj);
120 HMI_NOTICE("homescreen-service","Please input display_message");
121 ret = AFB_EVENT_BAD_REQUEST;
127 * push on_screen_reply event
130 * - request : the request
137 int HS_Client::on_screen_reply(afb_req_t request)
140 const char* value = afb_req_value(request, _reply_message);
142 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, value);
143 struct json_object* push_obj = json_object_new_object();
144 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, value,
145 _type, __FUNCTION__);
146 afb_event_push(my_event, push_obj);
149 HMI_NOTICE("homescreen-service","Please input reply_message");
150 ret = AFB_EVENT_BAD_REQUEST;
159 * - request : the request
166 int HS_Client::subscribe(afb_req_t request)
168 HMI_NOTICE("homescreen-service"," called.");
170 const char *value = afb_req_value(request, _event);
172 HMI_NOTICE("homescreen-service","subscribe event %s", value);
173 if(!isSupportEvent(value)) {
174 HMI_NOTICE("homescreen-service","subscibe event isn't existing.");
175 ret = AFB_EVENT_BAD_REQUEST;
178 event_list.insert(std::string(value));
180 ret = afb_req_subscribe(request, my_event);
188 HMI_NOTICE("homescreen-service","Please input event name");
189 ret = AFB_EVENT_BAD_REQUEST;
198 * - request : the request
205 int HS_Client::unsubscribe(afb_req_t request)
207 HMI_NOTICE("homescreen-service"," called.");
209 const char *value = afb_req_value(request, _event);
211 HMI_NOTICE("homescreen-service","unsubscribe %s event", value);
212 event_list.erase(std::string(value));
213 if(event_list.empty()) {
214 ret = afb_req_unsubscribe(request, my_event);
218 HMI_NOTICE("homescreen-service","Please input event name");
219 ret = AFB_EVENT_BAD_REQUEST;
228 * - request : the request
235 int HS_Client::showWindow(afb_req_t request)
237 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
239 struct json_object* push_obj = json_object_new_object();
240 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
241 const char* param = afb_req_value(request, _parameter);
243 std::string req_appid = std::move(get_application_id(request));
244 if(req_appid.empty()) {
245 HMI_NOTICE("homescreen-service","can't get application identifier");
246 return AFB_REQ_GETAPPLICATIONID_ERROR;
249 struct json_object* param_obj = json_tokener_parse(param);
250 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
251 json_object_object_add(push_obj, _parameter, param_obj);
252 afb_event_push(my_event, push_obj);
255 HMI_ERROR("homescreen-service","please input correct parameter.");
256 ret = AFB_EVENT_BAD_REQUEST;
265 * - request : the request
272 int HS_Client::hideWindow(afb_req_t request)
274 HMI_NOTICE("homescreen-service"," called.");
275 std::string req_appid = std::move(get_application_id(request));
276 if(req_appid.empty()) {
277 HMI_NOTICE("homescreen-service","can't get application identifier");
278 return AFB_REQ_GETAPPLICATIONID_ERROR;
281 struct json_object* push_obj = json_object_new_object();
282 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
283 _type, __FUNCTION__);
284 struct json_object* param_obj = json_object_new_object();
285 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
286 json_object_object_add(push_obj, _parameter, param_obj);
287 afb_event_push(my_event, push_obj);
292 * replyShowWindow event
295 * - request : the request
302 int HS_Client::replyShowWindow(afb_req_t request)
304 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
306 struct json_object* push_obj = json_object_new_object();
307 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
308 const char* param = afb_req_value(request, _parameter);
310 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
311 afb_event_push(my_event, push_obj);
314 HMI_ERROR("homescreen-service","please input correct parameter.");
315 ret = AFB_EVENT_BAD_REQUEST;
321 * showNotification event
324 * - request : the request
331 int HS_Client::showNotification(afb_req_t request)
333 HMI_NOTICE("homescreen-service"," called.");
335 const char *value = afb_req_value(request, _text);
337 HMI_NOTICE("homescreen-service","text is %s", value);
338 std::string appid =std::move(get_application_id(request));
340 HMI_NOTICE("homescreen-service","can't get application identifier");
341 return AFB_REQ_GETAPPLICATIONID_ERROR;
344 struct json_object* param_obj = json_object_new_object();
345 const char *icon = afb_req_value(request, _icon);
347 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
348 json_object_object_add(param_obj, _text, json_object_new_string(value));
349 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
350 struct json_object* push_obj = json_object_new_object();
351 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
352 json_object_object_add(push_obj, _parameter, param_obj);
353 afb_event_push(my_event, push_obj);
356 HMI_NOTICE("homescreen-service","please input icon.");
357 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
361 HMI_NOTICE("homescreen-service","please input text.");
362 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
369 * showInformation event
372 * - request : the request
379 int HS_Client::showInformation(afb_req_t request)
381 HMI_NOTICE("homescreen-service"," called.");
383 const char *value = afb_req_value(request, _info);
385 HMI_NOTICE("homescreen-service","info is %s", value);
386 std::string appid = std::move(get_application_id(request));
388 HMI_NOTICE("homescreen-service","can't get application identifier");
389 return AFB_REQ_GETAPPLICATIONID_ERROR;
392 struct json_object* param_obj = json_object_new_object();
393 json_object_object_add(param_obj, _info, json_object_new_string(value));
394 struct json_object* push_obj = json_object_new_object();
395 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
396 json_object_object_add(push_obj, _parameter, param_obj);
397 afb_event_push(my_event, push_obj);
400 HMI_NOTICE("homescreen-service","please input information.");
401 ret = AFB_REQ_SHOWINFORMATION_ERROR;
408 * check if client subscribe event
411 * - event: homescreen event, tap_shortcut etc.
418 bool HS_Client::checkEvent(const char* event)
420 auto ip = event_list.find(std::string(event));
421 if(ip == event_list.end())
428 * check if event is supporting
431 * - event: homescreen event, tap_shortcut etc.
435 * false: not fosupportund
438 bool HS_Client::isSupportEvent(const char* event)
440 auto ip = func_list.find(std::string(event));
441 if(ip == func_list.end())
448 * handle homescreen event
451 * - request : the request
452 * - verb: request verb name
459 int HS_Client::handleRequest(afb_req_t request, const char *verb)
461 HMI_NOTICE("homescreen-service","called.");
462 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
465 int ret = AFB_EVENT_BAD_REQUEST;
466 auto ip = func_list.find(std::string(verb));
467 if(ip != func_list.end()) {
468 HMI_NOTICE("homescreen-service","[%s]verb found", verb);
469 ret = (this->*(ip->second))(request);