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)
88 AFB_INFO("request appid = %s.", my_id.c_str());
89 struct json_object* push_obj = json_object_new_object();
90 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
92 afb_event_push(my_event, push_obj);
97 * push on_screen_message event
100 * - request : the request
107 int HS_Client::on_screen_message(afb_req_t request)
110 const char* value = afb_req_value(request, _display_message);
112 AFB_INFO("push %s event message [%s].", __FUNCTION__, value);
113 struct json_object* push_obj = json_object_new_object();
114 hs_add_object_to_json_object_str( push_obj, 4, _display_message, value,
115 _type, __FUNCTION__);
116 afb_event_push(my_event, push_obj);
119 AFB_WARNING("Please input display_message");
120 ret = AFB_EVENT_BAD_REQUEST;
126 * push on_screen_reply event
129 * - request : the request
136 int HS_Client::on_screen_reply(afb_req_t request)
139 const char* value = afb_req_value(request, _reply_message);
141 AFB_INFO("push %s event message [%s].", __FUNCTION__, value);
142 struct json_object* push_obj = json_object_new_object();
143 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, value,
144 _type, __FUNCTION__);
145 afb_event_push(my_event, push_obj);
148 AFB_WARNING("Please input reply_message");
149 ret = AFB_EVENT_BAD_REQUEST;
158 * - request : the request
165 int HS_Client::subscribe(afb_req_t request)
168 const char *value = afb_req_value(request, _event);
170 AFB_INFO("subscribe event %s", value);
171 if(!isSupportEvent(value)) {
172 AFB_WARNING("subscibe event isn't existing.");
173 ret = AFB_EVENT_BAD_REQUEST;
176 event_list.insert(std::string(value));
178 ret = afb_req_subscribe(request, my_event);
186 AFB_WARNING("Please input event name");
187 ret = AFB_EVENT_BAD_REQUEST;
196 * - request : the request
203 int HS_Client::unsubscribe(afb_req_t request)
206 const char *value = afb_req_value(request, _event);
208 AFB_INFO("unsubscribe %s event", value);
209 event_list.erase(std::string(value));
210 if(event_list.empty()) {
211 ret = afb_req_unsubscribe(request, my_event);
215 AFB_WARNING("Please input event name");
216 ret = AFB_EVENT_BAD_REQUEST;
225 * - request : the request
232 int HS_Client::showWindow(afb_req_t request)
234 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
236 const char* param = afb_req_value(request, _parameter);
238 std::string req_appid = std::move(get_application_id(request));
239 if(req_appid.empty()) {
240 AFB_WARNING("can't get application identifier");
241 return AFB_REQ_GETAPPLICATIONID_ERROR;
244 struct json_object* push_obj = json_object_new_object();
245 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
246 struct json_object* param_obj = json_tokener_parse(param);
247 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
248 json_object_object_add(push_obj, _parameter, param_obj);
249 afb_event_push(my_event, push_obj);
252 AFB_WARNING("please input correct parameter.");
253 ret = AFB_EVENT_BAD_REQUEST;
262 * - request : the request
269 int HS_Client::hideWindow(afb_req_t request)
271 std::string req_appid = std::move(get_application_id(request));
272 if(req_appid.empty()) {
273 AFB_WARNING("can't get application identifier");
274 return AFB_REQ_GETAPPLICATIONID_ERROR;
277 struct json_object* push_obj = json_object_new_object();
278 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
279 _type, __FUNCTION__);
280 struct json_object* param_obj = json_object_new_object();
281 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
282 json_object_object_add(push_obj, _parameter, param_obj);
283 afb_event_push(my_event, push_obj);
288 * replyShowWindow event
291 * - request : the request
298 int HS_Client::replyShowWindow(afb_req_t request)
300 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
302 const char* param = afb_req_value(request, _parameter);
304 struct json_object* push_obj = json_object_new_object();
305 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
306 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
307 afb_event_push(my_event, push_obj);
310 AFB_WARNING("please input correct parameter.");
311 ret = AFB_EVENT_BAD_REQUEST;
317 * showNotification event
320 * - request : the request
327 int HS_Client::showNotification(afb_req_t request)
330 const char *value = afb_req_value(request, _text);
332 AFB_INFO("text is %s", value);
333 std::string appid =std::move(get_application_id(request));
335 AFB_WARNING("can't get application identifier");
336 return AFB_REQ_GETAPPLICATIONID_ERROR;
339 const char *icon = afb_req_value(request, _icon);
341 struct json_object* param_obj = json_object_new_object();
342 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
343 json_object_object_add(param_obj, _text, json_object_new_string(value));
344 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
345 struct json_object* push_obj = json_object_new_object();
346 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
347 json_object_object_add(push_obj, _parameter, param_obj);
348 afb_event_push(my_event, push_obj);
351 AFB_WARNING("please input icon.");
352 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
356 AFB_WARNING("please input text.");
357 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
364 * showInformation event
367 * - request : the request
374 int HS_Client::showInformation(afb_req_t request)
377 const char *value = afb_req_value(request, _info);
379 AFB_INFO("info is %s", value);
380 std::string appid = std::move(get_application_id(request));
382 AFB_WARNING("can't get application identifier");
383 return AFB_REQ_GETAPPLICATIONID_ERROR;
386 struct json_object* param_obj = json_object_new_object();
387 json_object_object_add(param_obj, _info, json_object_new_string(value));
388 struct json_object* push_obj = json_object_new_object();
389 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
390 json_object_object_add(push_obj, _parameter, param_obj);
391 afb_event_push(my_event, push_obj);
394 AFB_WARNING("please input information.");
395 ret = AFB_REQ_SHOWINFORMATION_ERROR;
402 * check if client subscribe event
405 * - event: homescreen event, tap_shortcut etc.
412 bool HS_Client::checkEvent(const char* event)
414 auto ip = event_list.find(std::string(event));
415 if(ip == event_list.end())
422 * check if event is supporting
425 * - event: homescreen event, tap_shortcut etc.
429 * false: not fosupportund
432 bool HS_Client::isSupportEvent(const char* event)
434 auto ip = func_list.find(std::string(event));
435 if(ip == func_list.end())
442 * handle homescreen event
445 * - request : the request
446 * - verb: request verb name
453 int HS_Client::handleRequest(afb_req_t request, const char *verb)
455 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
458 int ret = AFB_EVENT_BAD_REQUEST;
459 auto ip = func_list.find(std::string(verb));
460 if(ip != func_list.end() && ip->second != nullptr) {
461 AFB_INFO("[%s]verb found", verb);
462 ret = (this->*(ip->second))(request);
471 * - event : the event want to push
472 * - param : the parameter contents of event
479 int HS_Client::pushEvent(const char *event, struct json_object *param)
481 if(!checkEvent(event))
484 AFB_INFO("called, event=%s.",event);
485 struct json_object* push_obj = json_object_new_object();
486 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, event);
488 json_object_object_add(push_obj, _parameter, param);
489 afb_event_push(my_event, push_obj);