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";
29 static const char _shortcut[] = "shortcut";
30 static const char _shortcut_id[] = "shortcut_id";
31 static const char _shortcut_name[] = "shortcut_name";
33 // homescreen-service event and event handler function list
34 const std::unordered_map<std::string, HS_Client::func_handler> HS_Client::func_list {
35 {"tap_shortcut", &HS_Client::tap_shortcut},
36 {"showWindow", &HS_Client::showWindow},
37 {"hideWindow", &HS_Client::hideWindow},
38 {"replyShowWindow", &HS_Client::replyShowWindow},
39 {"on_screen_message", &HS_Client::on_screen_message},
40 {"on_screen_reply", &HS_Client::on_screen_reply},
41 {"subscribe", &HS_Client::subscribe},
42 {"unsubscribe", &HS_Client::unsubscribe},
43 {"showNotification", &HS_Client::showNotification},
44 {"showInformation", &HS_Client::showInformation},
45 {"registerShortcut", &HS_Client::registerShortcut},
46 {"updateShortcut", &HS_Client::updateShortcut}
49 std::list<std::pair<std::string, std::string>> HS_Client::shortcut_list;
52 * HS_Client construction function
61 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
64 my_event = afb_api_make_event(request->api, id.c_str());
68 * HS_Client destruction function
77 HS_Client::~HS_Client()
80 afb_event_unref(my_event);
84 * push tap_shortcut event
87 * - request : the request
94 int HS_Client::tap_shortcut(afb_req_t request)
96 AFB_INFO("request appid = %s.", my_id.c_str());
97 struct json_object* push_obj = json_object_new_object();
98 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
100 afb_event_push(my_event, push_obj);
105 * push on_screen_message event
108 * - request : the request
115 int HS_Client::on_screen_message(afb_req_t request)
118 const char* value = afb_req_value(request, _display_message);
120 AFB_INFO("push %s event message [%s].", __FUNCTION__, value);
121 struct json_object* push_obj = json_object_new_object();
122 hs_add_object_to_json_object_str( push_obj, 4, _display_message, value,
123 _type, __FUNCTION__);
124 afb_event_push(my_event, push_obj);
127 AFB_WARNING("Please input display_message");
128 ret = AFB_EVENT_BAD_REQUEST;
134 * push on_screen_reply event
137 * - request : the request
144 int HS_Client::on_screen_reply(afb_req_t request)
147 const char* value = afb_req_value(request, _reply_message);
149 AFB_INFO("push %s event message [%s].", __FUNCTION__, value);
150 struct json_object* push_obj = json_object_new_object();
151 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, value,
152 _type, __FUNCTION__);
153 afb_event_push(my_event, push_obj);
156 AFB_WARNING("Please input reply_message");
157 ret = AFB_EVENT_BAD_REQUEST;
166 * - request : the request
173 int HS_Client::subscribe(afb_req_t request)
175 AFB_DEBUG(" called.");
177 const char *value = afb_req_value(request, _event);
179 AFB_INFO("subscribe event %s", value);
180 if(!isSupportEvent(value)) {
181 AFB_WARNING("subscibe event isn't existing.");
182 ret = AFB_EVENT_BAD_REQUEST;
185 event_list.insert(std::string(value));
187 ret = afb_req_subscribe(request, my_event);
192 if (!strcasecmp("updateShortcut", value)) {
193 pushUpdateShortcutEvent();
198 AFB_WARNING("Please input event name");
199 ret = AFB_EVENT_BAD_REQUEST;
208 * - request : the request
215 int HS_Client::unsubscribe(afb_req_t request)
217 AFB_DEBUG(" called.");
219 const char *value = afb_req_value(request, _event);
221 AFB_INFO("unsubscribe %s event", value);
222 event_list.erase(std::string(value));
223 if(event_list.empty()) {
224 ret = afb_req_unsubscribe(request, my_event);
228 AFB_WARNING("Please input event name");
229 ret = AFB_EVENT_BAD_REQUEST;
238 * - request : the request
245 int HS_Client::showWindow(afb_req_t request)
247 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
249 struct json_object* push_obj = json_object_new_object();
250 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
251 const char* param = afb_req_value(request, _parameter);
253 std::string req_appid = std::move(get_application_id(request));
254 if(req_appid.empty()) {
255 AFB_WARNING("can't get application identifier");
256 return AFB_REQ_GETAPPLICATIONID_ERROR;
259 struct json_object* param_obj = json_tokener_parse(param);
260 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
261 json_object_object_add(push_obj, _parameter, param_obj);
262 afb_event_push(my_event, push_obj);
265 AFB_WARNING("please input correct parameter.");
266 ret = AFB_EVENT_BAD_REQUEST;
275 * - request : the request
282 int HS_Client::hideWindow(afb_req_t request)
284 AFB_DEBUG(" called.");
285 std::string req_appid = std::move(get_application_id(request));
286 if(req_appid.empty()) {
287 AFB_WARNING("can't get application identifier");
288 return AFB_REQ_GETAPPLICATIONID_ERROR;
291 struct json_object* push_obj = json_object_new_object();
292 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
293 _type, __FUNCTION__);
294 struct json_object* param_obj = json_object_new_object();
295 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
296 json_object_object_add(push_obj, _parameter, param_obj);
297 afb_event_push(my_event, push_obj);
302 * replyShowWindow event
305 * - request : the request
312 int HS_Client::replyShowWindow(afb_req_t request)
314 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
316 struct json_object* push_obj = json_object_new_object();
317 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
318 const char* param = afb_req_value(request, _parameter);
320 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
321 afb_event_push(my_event, push_obj);
324 AFB_WARNING("please input correct parameter.");
325 ret = AFB_EVENT_BAD_REQUEST;
331 * showNotification event
334 * - request : the request
341 int HS_Client::showNotification(afb_req_t request)
343 AFB_DEBUG(" called.");
345 const char *value = afb_req_value(request, _text);
347 AFB_INFO("text is %s", value);
348 std::string appid =std::move(get_application_id(request));
350 AFB_WARNING("can't get application identifier");
351 return AFB_REQ_GETAPPLICATIONID_ERROR;
354 struct json_object* param_obj = json_object_new_object();
355 const char *icon = afb_req_value(request, _icon);
357 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
358 json_object_object_add(param_obj, _text, json_object_new_string(value));
359 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
360 struct json_object* push_obj = json_object_new_object();
361 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
362 json_object_object_add(push_obj, _parameter, param_obj);
363 afb_event_push(my_event, push_obj);
366 AFB_WARNING("please input icon.");
367 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
371 AFB_WARNING("please input text.");
372 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
379 * showInformation event
382 * - request : the request
389 int HS_Client::showInformation(afb_req_t request)
391 AFB_DEBUG(" called.");
393 const char *value = afb_req_value(request, _info);
395 AFB_INFO("info is %s", value);
396 std::string appid = std::move(get_application_id(request));
398 AFB_WARNING("can't get application identifier");
399 return AFB_REQ_GETAPPLICATIONID_ERROR;
402 struct json_object* param_obj = json_object_new_object();
403 json_object_object_add(param_obj, _info, json_object_new_string(value));
404 struct json_object* push_obj = json_object_new_object();
405 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
406 json_object_object_add(push_obj, _parameter, param_obj);
407 afb_event_push(my_event, push_obj);
410 AFB_WARNING("please input information.");
411 ret = AFB_REQ_SHOWINFORMATION_ERROR;
418 * registerShortcut event
421 * - request : the request
428 int HS_Client::registerShortcut(afb_req_t request)
431 struct json_object *param_obj;
432 if(json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)) {
433 struct json_object* push_obj = json_object_new_object();
434 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
435 json_object_object_add(push_obj, _parameter, param_obj);
436 afb_event_push(my_event, push_obj);
439 AFB_WARNING("please input parameter.");
440 ret = AFB_EVENT_BAD_REQUEST;
446 * updateShortcut event
449 * - request : the request
456 int HS_Client::updateShortcut(afb_req_t request)
458 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
460 std::list<std::pair<std::string, std::string>> new_shortcut_list;
461 struct json_object *req_json = afb_req_json(request);
462 struct json_object *param_obj, *shortcut_obj;
463 if(json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
464 && json_object_object_get_ex(param_obj, _shortcut, &shortcut_obj)) {
465 if(json_object_get_type(shortcut_obj) == json_type_array ) {
466 int array_len = json_object_array_length(shortcut_obj);
467 for (int i = 0; i < array_len; ++i) {
468 struct json_object *obj = json_object_array_get_idx(shortcut_obj, i);
469 struct json_object *appid_obj, *appname_obj;
470 if(json_object_object_get_ex(obj, _shortcut_id, &appid_obj)
471 && json_object_object_get_ex(obj, _shortcut_name, &appname_obj)) {
472 new_shortcut_list.push_back(std::pair<std::string, std::string>(json_object_get_string(appid_obj),
473 json_object_get_string(appname_obj)));
476 AFB_WARNING("shortcut list json object pattern error.");
477 ret = AFB_EVENT_BAD_REQUEST;
483 AFB_WARNING("json object pattern error.");
484 ret = AFB_EVENT_BAD_REQUEST;
488 AFB_WARNING("input json object error.");
489 ret = AFB_EVENT_BAD_REQUEST;
493 HS_Client::shortcut_list.swap(new_shortcut_list);
494 pushUpdateShortcutEvent();
500 * check if client subscribe event
503 * - event: homescreen event, tap_shortcut etc.
510 bool HS_Client::checkEvent(const char* event)
512 auto ip = event_list.find(std::string(event));
513 if(ip == event_list.end())
520 * check if event is supporting
523 * - event: homescreen event, tap_shortcut etc.
527 * false: not fosupportund
530 bool HS_Client::isSupportEvent(const char* event)
532 int ret = hs_search_event_name_index(event);
533 return ret == -1 ? false : true;
537 * handle homescreen event
540 * - request : the request
541 * - verb: request verb name
548 int HS_Client::handleRequest(afb_req_t request, const char *verb)
550 AFB_DEBUG("called.");
551 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
554 int ret = AFB_EVENT_BAD_REQUEST;
555 auto ip = func_list.find(std::string(verb));
556 if(ip != func_list.end() && ip->second != nullptr) {
557 AFB_INFO("[%s]verb found", verb);
558 ret = (this->*(ip->second))(request);
567 * - event : the event want to push
568 * - param : the parameter contents of event
575 int HS_Client::pushEvent(const char *event, struct json_object *param)
577 if(!checkEvent(event))
580 AFB_INFO("called, event=%s.",event);
581 struct json_object* push_obj = json_object_new_object();
582 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, event);
584 json_object_object_add(push_obj, _parameter, param);
585 afb_event_push(my_event, push_obj);
590 * push updateShortcut event
599 void HS_Client::pushUpdateShortcutEvent(void)
601 struct json_object* arr_obj = json_object_new_array();
602 for(auto &it : HS_Client::shortcut_list) {
603 struct json_object* obj = json_object_new_object();
604 json_object_object_add(obj, _shortcut_id, json_object_new_string(it.first.c_str()));
605 json_object_object_add(obj, _shortcut_name, json_object_new_string(it.second.c_str()));
606 json_object_array_add(arr_obj, obj);
608 struct json_object* shortcut_obj = json_object_new_object();
609 json_object_object_add(shortcut_obj, _shortcut, arr_obj);
610 struct json_object* push_obj = json_object_new_object();
611 hs_add_object_to_json_object_str(push_obj, 4, _application_id, my_id.c_str(), _type, "updateShortcut");
612 json_object_object_add(push_obj, _parameter, shortcut_obj);
613 afb_event_push(my_event, push_obj);