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 struct json_object *ev_obj, *req_obj = afb_req_json(request);
175 if(json_object_object_get_ex(req_obj, _event, &ev_obj)) {
176 std::unordered_set<std::string> ev_set;
177 if(json_object_get_type(ev_obj) == json_type_string) {
178 ev_set.insert(json_object_get_string(ev_obj));
180 else if(json_object_get_type(ev_obj) == json_type_array) {
181 int array_len = json_object_array_length(ev_obj);
182 for (int i = 0; i < array_len; ++i) {
183 struct json_object *obj = json_object_array_get_idx(ev_obj, i);
184 ev_set.insert(json_object_get_string(obj));
188 AFB_WARNING("subscibe event type error.");
189 return AFB_EVENT_BAD_REQUEST;
193 ret = afb_req_subscribe(request, my_event);
198 for(auto &it : ev_set) {
199 AFB_INFO("subscribe event %s", it.c_str());
200 if(!isSupportEvent(it.c_str())) {
201 AFB_WARNING("subscibe event(%s) isn't existing.", it.c_str());
204 event_list.insert(it.c_str());
209 AFB_WARNING("Please input event name");
210 ret = AFB_EVENT_BAD_REQUEST;
219 * - request : the request
226 int HS_Client::unsubscribe(afb_req_t request)
229 const char *value = afb_req_value(request, _event);
231 AFB_INFO("unsubscribe %s event", value);
232 event_list.erase(std::string(value));
233 if(event_list.empty()) {
234 ret = afb_req_unsubscribe(request, my_event);
238 AFB_WARNING("Please input event name");
239 ret = AFB_EVENT_BAD_REQUEST;
248 * - request : the request
255 int HS_Client::showWindow(afb_req_t request)
257 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
259 struct json_object* push_obj = json_object_new_object();
260 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
261 const char* param = afb_req_value(request, _parameter);
263 std::string req_appid = std::move(get_application_id(request));
264 if(req_appid.empty()) {
265 AFB_WARNING("can't get application identifier");
266 return AFB_REQ_GETAPPLICATIONID_ERROR;
269 struct json_object* param_obj = json_tokener_parse(param);
270 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
271 json_object_object_add(push_obj, _parameter, param_obj);
272 afb_event_push(my_event, push_obj);
275 AFB_WARNING("please input correct parameter.");
276 ret = AFB_EVENT_BAD_REQUEST;
285 * - request : the request
292 int HS_Client::hideWindow(afb_req_t request)
294 std::string req_appid = std::move(get_application_id(request));
295 if(req_appid.empty()) {
296 AFB_WARNING("can't get application identifier");
297 return AFB_REQ_GETAPPLICATIONID_ERROR;
300 struct json_object* push_obj = json_object_new_object();
301 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
302 _type, __FUNCTION__);
303 struct json_object* param_obj = json_object_new_object();
304 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
305 json_object_object_add(push_obj, _parameter, param_obj);
306 afb_event_push(my_event, push_obj);
311 * replyShowWindow event
314 * - request : the request
321 int HS_Client::replyShowWindow(afb_req_t request)
323 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
325 struct json_object* push_obj = json_object_new_object();
326 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
327 const char* param = afb_req_value(request, _parameter);
329 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
330 afb_event_push(my_event, push_obj);
333 AFB_WARNING("please input correct parameter.");
334 ret = AFB_EVENT_BAD_REQUEST;
340 * showNotification event
343 * - request : the request
350 int HS_Client::showNotification(afb_req_t request)
353 const char *value = afb_req_value(request, _text);
355 AFB_INFO("text is %s", value);
356 std::string appid =std::move(get_application_id(request));
358 AFB_WARNING("can't get application identifier");
359 return AFB_REQ_GETAPPLICATIONID_ERROR;
362 struct json_object* param_obj = json_object_new_object();
363 const char *icon = afb_req_value(request, _icon);
365 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
366 json_object_object_add(param_obj, _text, json_object_new_string(value));
367 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
368 struct json_object* push_obj = json_object_new_object();
369 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
370 json_object_object_add(push_obj, _parameter, param_obj);
371 afb_event_push(my_event, push_obj);
374 AFB_WARNING("please input icon.");
375 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
379 AFB_WARNING("please input text.");
380 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
387 * showInformation event
390 * - request : the request
397 int HS_Client::showInformation(afb_req_t request)
400 const char *value = afb_req_value(request, _info);
402 AFB_INFO("info is %s", value);
403 std::string appid = std::move(get_application_id(request));
405 AFB_WARNING("can't get application identifier");
406 return AFB_REQ_GETAPPLICATIONID_ERROR;
409 struct json_object* param_obj = json_object_new_object();
410 json_object_object_add(param_obj, _info, json_object_new_string(value));
411 struct json_object* push_obj = json_object_new_object();
412 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
413 json_object_object_add(push_obj, _parameter, param_obj);
414 afb_event_push(my_event, push_obj);
417 AFB_WARNING("please input information.");
418 ret = AFB_REQ_SHOWINFORMATION_ERROR;
425 * registerShortcut event
428 * - request : the request
435 int HS_Client::registerShortcut(afb_req_t request)
438 struct json_object *param_obj;
439 if(json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)) {
440 struct json_object* push_obj = json_object_new_object();
441 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
442 json_object_object_add(push_obj, _parameter, param_obj);
443 afb_event_push(my_event, push_obj);
446 AFB_WARNING("please input parameter.");
447 ret = AFB_EVENT_BAD_REQUEST;
453 * updateShortcut event
456 * - request : the request
463 int HS_Client::updateShortcut(afb_req_t request)
465 AFB_INFO("%s application_id = %s.", __FUNCTION__, my_id.c_str());
467 std::list<std::pair<std::string, std::string>> new_shortcut_list;
468 struct json_object *req_json = afb_req_json(request);
469 struct json_object *param_obj, *shortcut_obj;
470 if(json_object_object_get_ex(afb_req_json(request), _parameter, ¶m_obj)
471 && json_object_object_get_ex(param_obj, _shortcut, &shortcut_obj)) {
472 if(json_object_get_type(shortcut_obj) == json_type_array ) {
473 int array_len = json_object_array_length(shortcut_obj);
474 for (int i = 0; i < array_len; ++i) {
475 struct json_object *obj = json_object_array_get_idx(shortcut_obj, i);
476 struct json_object *appid_obj, *appname_obj;
477 if(json_object_object_get_ex(obj, _shortcut_id, &appid_obj)
478 && json_object_object_get_ex(obj, _shortcut_name, &appname_obj)) {
479 new_shortcut_list.push_back(std::pair<std::string, std::string>(json_object_get_string(appid_obj),
480 json_object_get_string(appname_obj)));
483 AFB_WARNING("shortcut list json object pattern error.");
484 ret = AFB_EVENT_BAD_REQUEST;
490 AFB_WARNING("json object pattern error.");
491 ret = AFB_EVENT_BAD_REQUEST;
495 AFB_WARNING("input json object error.");
496 ret = AFB_EVENT_BAD_REQUEST;
500 HS_Client::shortcut_list.swap(new_shortcut_list);
501 pushUpdateShortcutEvent();
507 * check if client subscribe event
510 * - event: homescreen event, tap_shortcut etc.
517 bool HS_Client::checkEvent(const char* event)
519 auto ip = event_list.find(std::string(event));
520 if(ip == event_list.end())
527 * check if event is supporting
530 * - event: homescreen event, tap_shortcut etc.
534 * false: not fosupportund
537 bool HS_Client::isSupportEvent(const char* event)
539 int ret = hs_search_event_name_index(event);
540 return ret == -1 ? false : true;
544 * handle homescreen event
547 * - request : the request
548 * - verb: request verb name
555 int HS_Client::handleRequest(afb_req_t request, const char *verb)
557 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
560 int ret = AFB_EVENT_BAD_REQUEST;
561 auto ip = func_list.find(std::string(verb));
562 if(ip != func_list.end() && ip->second != nullptr) {
563 AFB_INFO("[%s]verb found", verb);
564 ret = (this->*(ip->second))(request);
573 * - event : the event want to push
574 * - param : the parameter contents of event
581 int HS_Client::pushEvent(const char *event, struct json_object *param)
583 if(!checkEvent(event))
586 AFB_INFO("called, event=%s.", event);
587 struct json_object* push_obj = json_object_new_object();
588 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, event);
590 json_object_object_add(push_obj, _parameter, param);
591 afb_event_push(my_event, push_obj);
596 * push updateShortcut event
605 void HS_Client::pushUpdateShortcutEvent(void)
607 struct json_object* arr_obj = json_object_new_array();
608 for(auto &it : HS_Client::shortcut_list) {
609 struct json_object* obj = json_object_new_object();
610 json_object_object_add(obj, _shortcut_id, json_object_new_string(it.first.c_str()));
611 json_object_object_add(obj, _shortcut_name, json_object_new_string(it.second.c_str()));
612 json_object_array_add(arr_obj, obj);
614 struct json_object* shortcut_obj = json_object_new_object();
615 json_object_object_add(shortcut_obj, _shortcut, arr_obj);
616 struct json_object* push_obj = json_object_new_object();
617 hs_add_object_to_json_object_str(push_obj, 4, _application_id, my_id.c_str(), _type, "updateShortcut");
618 json_object_object_add(push_obj, _parameter, shortcut_obj);
619 afb_event_push(my_event, push_obj);