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.
18 #include "hs-client.h"
19 #include "hs-helper.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 my_event = afb_api_make_event(request->api, id.c_str());
61 * HS_Client destruction function
70 HS_Client::~HS_Client()
72 afb_event_unref(my_event);
76 * push tap_shortcut event
79 * - request : the request
86 int HS_Client::tap_shortcut(afb_req_t request)
90 AFB_INFO("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 AFB_INFO("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 AFB_WARNING("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 AFB_INFO("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 AFB_WARNING("Please input reply_message");
151 ret = AFB_EVENT_BAD_REQUEST;
160 * - request : the request
167 int HS_Client::subscribe(afb_req_t request)
170 const char *value = afb_req_value(request, _event);
172 AFB_INFO("subscribe event %s", value);
173 if(!isSupportEvent(value)) {
174 AFB_WARNING("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 AFB_WARNING("Please input event name");
189 ret = AFB_EVENT_BAD_REQUEST;
198 * - request : the request
205 int HS_Client::unsubscribe(afb_req_t request)
208 const char *value = afb_req_value(request, _event);
210 AFB_INFO("unsubscribe %s event", value);
211 event_list.erase(std::string(value));
212 if(event_list.empty()) {
213 ret = afb_req_unsubscribe(request, my_event);
217 AFB_WARNING("Please input event name");
218 ret = AFB_EVENT_BAD_REQUEST;
227 * - request : the request
234 int HS_Client::showWindow(afb_req_t request)
236 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
238 const char* param = afb_req_value(request, _parameter);
240 std::string req_appid = std::move(get_application_id(request));
241 if(req_appid.empty()) {
242 AFB_WARNING("can't get application identifier");
243 return AFB_REQ_GETAPPLICATIONID_ERROR;
246 struct json_object* push_obj = json_object_new_object();
247 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
248 struct json_object* param_obj = json_tokener_parse(param);
249 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
250 json_object_object_add(push_obj, _parameter, param_obj);
251 afb_event_push(my_event, push_obj);
254 AFB_WARNING("please input correct parameter.");
255 ret = AFB_EVENT_BAD_REQUEST;
264 * - request : the request
271 int HS_Client::hideWindow(afb_req_t request)
273 std::string req_appid = std::move(get_application_id(request));
274 if(req_appid.empty()) {
275 AFB_WARNING("can't get application identifier");
276 return AFB_REQ_GETAPPLICATIONID_ERROR;
279 struct json_object* push_obj = json_object_new_object();
280 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
281 _type, __FUNCTION__);
282 struct json_object* param_obj = json_object_new_object();
283 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
284 json_object_object_add(push_obj, _parameter, param_obj);
285 afb_event_push(my_event, push_obj);
290 * replyShowWindow event
293 * - request : the request
300 int HS_Client::replyShowWindow(afb_req_t request)
302 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
304 const char* param = afb_req_value(request, _parameter);
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 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
309 afb_event_push(my_event, push_obj);
312 AFB_WARNING("please input correct parameter.");
313 ret = AFB_EVENT_BAD_REQUEST;
319 * showNotification event
322 * - request : the request
329 int HS_Client::showNotification(afb_req_t request)
332 const char *value = afb_req_value(request, _text);
334 AFB_INFO("text is %s", value);
335 std::string appid =std::move(get_application_id(request));
337 AFB_WARNING("can't get application identifier");
338 return AFB_REQ_GETAPPLICATIONID_ERROR;
341 const char *icon = afb_req_value(request, _icon);
343 struct json_object* param_obj = json_object_new_object();
344 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
345 json_object_object_add(param_obj, _text, json_object_new_string(value));
346 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
347 struct json_object* push_obj = json_object_new_object();
348 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
349 json_object_object_add(push_obj, _parameter, param_obj);
350 afb_event_push(my_event, push_obj);
353 AFB_WARNING("please input icon.");
354 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
358 AFB_WARNING("please input text.");
359 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
366 * showInformation event
369 * - request : the request
376 int HS_Client::showInformation(afb_req_t request)
379 const char *value = afb_req_value(request, _info);
381 AFB_INFO("info is %s", value);
382 std::string appid = std::move(get_application_id(request));
384 AFB_WARNING("can't get application identifier");
385 return AFB_REQ_GETAPPLICATIONID_ERROR;
388 struct json_object* param_obj = json_object_new_object();
389 json_object_object_add(param_obj, _info, json_object_new_string(value));
390 struct json_object* push_obj = json_object_new_object();
391 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
392 json_object_object_add(push_obj, _parameter, param_obj);
393 afb_event_push(my_event, push_obj);
396 AFB_WARNING("please input information.");
397 ret = AFB_REQ_SHOWINFORMATION_ERROR;
404 * check if client subscribe event
407 * - event: homescreen event, tap_shortcut etc.
414 bool HS_Client::checkEvent(const char* event)
416 auto ip = event_list.find(std::string(event));
417 if(ip == event_list.end())
424 * check if event is supporting
427 * - event: homescreen event, tap_shortcut etc.
431 * false: not fosupportund
434 bool HS_Client::isSupportEvent(const char* event)
436 auto ip = func_list.find(std::string(event));
437 if(ip == func_list.end())
444 * handle homescreen event
447 * - request : the request
448 * - verb: request verb name
455 int HS_Client::handleRequest(afb_req_t request, const char *verb)
457 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
460 int ret = AFB_EVENT_BAD_REQUEST;
461 auto ip = func_list.find(std::string(verb));
462 if(ip != func_list.end() && ip->second != nullptr) {
463 AFB_INFO("[%s]verb found", verb);
464 ret = (this->*(ip->second))(request);
473 * - event : the event want to push
474 * - param : the parameter contents of event
481 int HS_Client::pushEvent(const char *event, struct json_object *param)
483 if(!checkEvent(event))
486 AFB_INFO("called, event=%s.",event);
487 struct json_object* push_obj = json_object_new_object();
488 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, event);
490 json_object_object_add(push_obj, _parameter, param);
491 afb_event_push(my_event, push_obj);