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)
63 my_event = afb_api_make_event(request->api, id.c_str());
67 * HS_Client destruction function
76 HS_Client::~HS_Client()
78 afb_event_unref(my_event);
82 * push tap_shortcut event
85 * - request : the request
92 int HS_Client::tap_shortcut(afb_req_t request)
94 AFB_INFO("request appid = %s.", my_id.c_str());
95 struct json_object* push_obj = json_object_new_object();
96 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
98 afb_event_push(my_event, push_obj);
103 * push on_screen_message event
106 * - request : the request
113 int HS_Client::on_screen_message(afb_req_t request)
116 const char* value = afb_req_value(request, _display_message);
118 AFB_INFO("push %s event message [%s].", __FUNCTION__, value);
119 struct json_object* push_obj = json_object_new_object();
120 hs_add_object_to_json_object_str( push_obj, 4, _display_message, value,
121 _type, __FUNCTION__);
122 afb_event_push(my_event, push_obj);
125 AFB_WARNING("Please input display_message");
126 ret = AFB_EVENT_BAD_REQUEST;
132 * push on_screen_reply event
135 * - request : the request
142 int HS_Client::on_screen_reply(afb_req_t request)
145 const char* value = afb_req_value(request, _reply_message);
147 AFB_INFO("push %s event message [%s].", __FUNCTION__, value);
148 struct json_object* push_obj = json_object_new_object();
149 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, value,
150 _type, __FUNCTION__);
151 afb_event_push(my_event, push_obj);
154 AFB_WARNING("Please input reply_message");
155 ret = AFB_EVENT_BAD_REQUEST;
164 * - request : the request
171 int HS_Client::subscribe(afb_req_t request)
174 const char *value = afb_req_value(request, _event);
176 AFB_INFO("subscribe event %s", value);
177 if(!isSupportEvent(value)) {
178 AFB_WARNING("subscibe event isn't existing.");
179 ret = AFB_EVENT_BAD_REQUEST;
182 event_list.insert(std::string(value));
184 ret = afb_req_subscribe(request, my_event);
189 if (!strcasecmp("updateShortcut", value)) {
190 pushUpdateShortcutEvent();
195 AFB_WARNING("Please input event name");
196 ret = AFB_EVENT_BAD_REQUEST;
205 * - request : the request
212 int HS_Client::unsubscribe(afb_req_t request)
215 const char *value = afb_req_value(request, _event);
217 AFB_INFO("unsubscribe %s event", value);
218 event_list.erase(std::string(value));
219 if(event_list.empty()) {
220 ret = afb_req_unsubscribe(request, my_event);
224 AFB_WARNING("Please input event name");
225 ret = AFB_EVENT_BAD_REQUEST;
234 * - request : the request
241 int HS_Client::showWindow(afb_req_t request)
243 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
245 struct json_object* push_obj = json_object_new_object();
246 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
247 const char* param = afb_req_value(request, _parameter);
249 std::string req_appid = std::move(get_application_id(request));
250 if(req_appid.empty()) {
251 AFB_WARNING("can't get application identifier");
252 return AFB_REQ_GETAPPLICATIONID_ERROR;
255 struct json_object* param_obj = json_tokener_parse(param);
256 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
257 json_object_object_add(push_obj, _parameter, param_obj);
258 afb_event_push(my_event, push_obj);
261 AFB_WARNING("please input correct parameter.");
262 ret = AFB_EVENT_BAD_REQUEST;
271 * - request : the request
278 int HS_Client::hideWindow(afb_req_t request)
280 std::string req_appid = std::move(get_application_id(request));
281 if(req_appid.empty()) {
282 AFB_WARNING("can't get application identifier");
283 return AFB_REQ_GETAPPLICATIONID_ERROR;
286 struct json_object* push_obj = json_object_new_object();
287 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
288 _type, __FUNCTION__);
289 struct json_object* param_obj = json_object_new_object();
290 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
291 json_object_object_add(push_obj, _parameter, param_obj);
292 afb_event_push(my_event, push_obj);
297 * replyShowWindow event
300 * - request : the request
307 int HS_Client::replyShowWindow(afb_req_t request)
309 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
311 struct json_object* push_obj = json_object_new_object();
312 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
313 const char* param = afb_req_value(request, _parameter);
315 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
316 afb_event_push(my_event, push_obj);
319 AFB_WARNING("please input correct parameter.");
320 ret = AFB_EVENT_BAD_REQUEST;
326 * showNotification event
329 * - request : the request
336 int HS_Client::showNotification(afb_req_t request)
339 const char *value = afb_req_value(request, _text);
341 AFB_INFO("text is %s", value);
342 std::string appid =std::move(get_application_id(request));
344 AFB_WARNING("can't get application identifier");
345 return AFB_REQ_GETAPPLICATIONID_ERROR;
348 struct json_object* param_obj = json_object_new_object();
349 const char *icon = afb_req_value(request, _icon);
351 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
352 json_object_object_add(param_obj, _text, json_object_new_string(value));
353 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
354 struct json_object* push_obj = json_object_new_object();
355 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
356 json_object_object_add(push_obj, _parameter, param_obj);
357 afb_event_push(my_event, push_obj);
360 AFB_WARNING("please input icon.");
361 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
365 AFB_WARNING("please input text.");
366 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
373 * showInformation event
376 * - request : the request
383 int HS_Client::showInformation(afb_req_t request)
386 const char *value = afb_req_value(request, _info);
388 AFB_INFO("info is %s", value);
389 std::string appid = std::move(get_application_id(request));
391 AFB_WARNING("can't get application identifier");
392 return AFB_REQ_GETAPPLICATIONID_ERROR;
395 struct json_object* param_obj = json_object_new_object();
396 json_object_object_add(param_obj, _info, json_object_new_string(value));
397 struct json_object* push_obj = json_object_new_object();
398 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
399 json_object_object_add(push_obj, _parameter, param_obj);
400 afb_event_push(my_event, push_obj);
403 AFB_WARNING("please input information.");
404 ret = AFB_REQ_SHOWINFORMATION_ERROR;
411 * registerShortcut event
414 * - request : the request
421 int HS_Client::registerShortcut(afb_req_t request)
424 struct json_object *param_obj;
425 if(json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)) {
426 struct json_object* push_obj = json_object_new_object();
427 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
428 json_object_object_add(push_obj, _parameter, param_obj);
429 afb_event_push(my_event, push_obj);
432 AFB_WARNING("please input parameter.");
433 ret = AFB_EVENT_BAD_REQUEST;
439 * updateShortcut event
442 * - request : the request
449 int HS_Client::updateShortcut(afb_req_t request)
451 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
453 std::list<std::pair<std::string, std::string>> new_shortcut_list;
454 struct json_object *req_json = afb_req_json(request);
455 struct json_object *param_obj, *shortcut_obj;
456 if(json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
457 && json_object_object_get_ex(param_obj, _shortcut, &shortcut_obj)) {
458 if(json_object_get_type(shortcut_obj) == json_type_array ) {
459 int array_len = json_object_array_length(shortcut_obj);
460 for (int i = 0; i < array_len; ++i) {
461 struct json_object *obj = json_object_array_get_idx(shortcut_obj, i);
462 struct json_object *appid_obj, *appname_obj;
463 if(json_object_object_get_ex(obj, _shortcut_id, &appid_obj)
464 && json_object_object_get_ex(obj, _shortcut_name, &appname_obj)) {
465 new_shortcut_list.push_back(std::pair<std::string, std::string>(json_object_get_string(appid_obj),
466 json_object_get_string(appname_obj)));
469 AFB_WARNING("shortcut list json object pattern error.");
470 ret = AFB_EVENT_BAD_REQUEST;
476 AFB_WARNING("json object pattern error.");
477 ret = AFB_EVENT_BAD_REQUEST;
481 AFB_WARNING("input json object error.");
482 ret = AFB_EVENT_BAD_REQUEST;
486 HS_Client::shortcut_list.swap(new_shortcut_list);
487 pushUpdateShortcutEvent();
493 * check if client subscribe event
496 * - event: homescreen event, tap_shortcut etc.
503 bool HS_Client::checkEvent(const char* event)
505 auto ip = event_list.find(std::string(event));
506 if(ip == event_list.end())
513 * check if event is supporting
516 * - event: homescreen event, tap_shortcut etc.
520 * false: not fosupportund
523 bool HS_Client::isSupportEvent(const char* event)
525 int ret = hs_search_event_name_index(event);
526 return ret == -1 ? false : true;
530 * handle homescreen event
533 * - request : the request
534 * - verb: request verb name
541 int HS_Client::handleRequest(afb_req_t request, const char *verb)
543 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
546 int ret = AFB_EVENT_BAD_REQUEST;
547 auto ip = func_list.find(std::string(verb));
548 if(ip != func_list.end() && ip->second != nullptr) {
549 AFB_INFO("[%s]verb found", verb);
550 ret = (this->*(ip->second))(request);
559 * - event : the event want to push
560 * - param : the parameter contents of event
567 int HS_Client::pushEvent(const char *event, struct json_object *param)
569 if(!checkEvent(event))
572 AFB_INFO("called, event=%s.", event);
573 struct json_object* push_obj = json_object_new_object();
574 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, event);
576 json_object_object_add(push_obj, _parameter, param);
577 afb_event_push(my_event, push_obj);
582 * push updateShortcut event
591 void HS_Client::pushUpdateShortcutEvent(void)
593 struct json_object* arr_obj = json_object_new_array();
594 for(auto &it : HS_Client::shortcut_list) {
595 struct json_object* obj = json_object_new_object();
596 json_object_object_add(obj, _shortcut_id, json_object_new_string(it.first.c_str()));
597 json_object_object_add(obj, _shortcut_name, json_object_new_string(it.second.c_str()));
598 json_object_array_add(arr_obj, obj);
600 struct json_object* shortcut_obj = json_object_new_object();
601 json_object_object_add(shortcut_obj, _shortcut, arr_obj);
602 struct json_object* push_obj = json_object_new_object();
603 hs_add_object_to_json_object_str(push_obj, 4, _application_id, my_id.c_str(), _type, "updateShortcut");
604 json_object_object_add(push_obj, _parameter, shortcut_obj);
605 afb_event_push(my_event, push_obj);