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.
17 #include "hs-client.h"
18 #include "hs-helper.h"
19 #include "hmi-debug.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";
31 * HS_Client construction function
40 HS_Client::HS_Client(afb_req_t request, std::string id) : my_id(id)
42 HMI_NOTICE("homescreen-service","called.");
43 my_event = afb_api_make_event(request->api, id.c_str());
47 * HS_Client destruction function
56 HS_Client::~HS_Client()
58 HMI_NOTICE("homescreen-service","called.");
59 afb_event_unref(my_event);
63 * push tap_shortcut event
66 * - request : the request
73 int HS_Client::tap_shortcut(afb_req_t request)
75 HMI_NOTICE("homescreen-service","request appid = %s.", my_id.c_str());
76 struct json_object* push_obj = json_object_new_object();
77 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
79 afb_event_push(my_event, push_obj);
84 * push on_screen_message event
87 * - request : the request
94 int HS_Client::on_screen_message(afb_req_t request)
97 const char* value = afb_req_value(request, _display_message);
99 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, value);
100 struct json_object* push_obj = json_object_new_object();
101 hs_add_object_to_json_object_str( push_obj, 4, _display_message, value,
102 _type, __FUNCTION__);
103 afb_event_push(my_event, push_obj);
106 HMI_NOTICE("homescreen-service","Please input display_message");
107 ret = AFB_EVENT_BAD_REQUEST;
113 * push on_screen_reply event
116 * - request : the request
123 int HS_Client::on_screen_reply(afb_req_t request)
126 const char* value = afb_req_value(request, _reply_message);
128 HMI_NOTICE("homescreen-service","push %s event message [%s].", __FUNCTION__, value);
129 struct json_object* push_obj = json_object_new_object();
130 hs_add_object_to_json_object_str( push_obj, 4, _reply_message, value,
131 _type, __FUNCTION__);
132 afb_event_push(my_event, push_obj);
135 HMI_NOTICE("homescreen-service","Please input reply_message");
136 ret = AFB_EVENT_BAD_REQUEST;
145 * - request : the request
152 int HS_Client::subscribe(afb_req_t request)
154 HMI_NOTICE("homescreen-service"," called.");
156 const char *value = afb_req_value(request, _event);
158 HMI_NOTICE("homescreen-service","subscribe event %s", value);
159 if(!isSupportEvent(value)) {
160 HMI_NOTICE("homescreen-service","subscibe event isn't existing.");
161 ret = AFB_EVENT_BAD_REQUEST;
164 event_list.insert(std::string(value));
166 ret = afb_req_subscribe(request, my_event);
174 HMI_NOTICE("homescreen-service","Please input event name");
175 ret = AFB_EVENT_BAD_REQUEST;
184 * - request : the request
191 int HS_Client::unsubscribe(afb_req_t request)
193 HMI_NOTICE("homescreen-service"," called.");
195 const char *value = afb_req_value(request, _event);
197 HMI_NOTICE("homescreen-service","unsubscribe %s event", value);
198 event_list.erase(std::string(value));
199 if(event_list.empty()) {
200 ret = afb_req_unsubscribe(request, my_event);
204 HMI_NOTICE("homescreen-service","Please input event name");
205 ret = AFB_EVENT_BAD_REQUEST;
214 * - request : the request
221 int HS_Client::showWindow(afb_req_t request)
223 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
225 struct json_object* push_obj = json_object_new_object();
226 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
227 const char* param = afb_req_value(request, _parameter);
229 std::string req_appid = std::move(get_application_id(request));
230 if(req_appid.empty()) {
231 HMI_NOTICE("homescreen-service","can't get application identifier");
232 return AFB_REQ_GETAPPLICATIONID_ERROR;
235 struct json_object* param_obj = json_tokener_parse(param);
236 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
237 json_object_object_add(push_obj, _parameter, param_obj);
238 afb_event_push(my_event, push_obj);
241 HMI_ERROR("homescreen-service","please input correct parameter.");
242 ret = AFB_EVENT_BAD_REQUEST;
251 * - request : the request
258 int HS_Client::hideWindow(afb_req_t request)
260 HMI_NOTICE("homescreen-service"," called.");
261 std::string req_appid = std::move(get_application_id(request));
262 if(req_appid.empty()) {
263 HMI_NOTICE("homescreen-service","can't get application identifier");
264 return AFB_REQ_GETAPPLICATIONID_ERROR;
267 struct json_object* push_obj = json_object_new_object();
268 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
269 _type, __FUNCTION__);
270 struct json_object* param_obj = json_object_new_object();
271 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
272 json_object_object_add(push_obj, _parameter, param_obj);
273 afb_event_push(my_event, push_obj);
278 * replyShowWindow event
281 * - request : the request
288 int HS_Client::replyShowWindow(afb_req_t request)
290 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
292 struct json_object* push_obj = json_object_new_object();
293 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
294 const char* param = afb_req_value(request, _parameter);
296 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
297 afb_event_push(my_event, push_obj);
300 HMI_ERROR("homescreen-service","please input correct parameter.");
301 ret = AFB_EVENT_BAD_REQUEST;
307 * showNotification event
310 * - request : the request
317 int HS_Client::showNotification(afb_req_t request)
319 HMI_NOTICE("homescreen-service"," called.");
321 const char *value = afb_req_value(request, _text);
323 HMI_NOTICE("homescreen-service","text is %s", value);
324 std::string appid =std::move(get_application_id(request));
326 HMI_NOTICE("homescreen-service","can't get application identifier");
327 return AFB_REQ_GETAPPLICATIONID_ERROR;
330 struct json_object* param_obj = json_object_new_object();
331 const char *icon = afb_req_value(request, _icon);
333 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
334 json_object_object_add(param_obj, _text, json_object_new_string(value));
335 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
336 struct json_object* push_obj = json_object_new_object();
337 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
338 json_object_object_add(push_obj, _parameter, param_obj);
339 afb_event_push(my_event, push_obj);
342 HMI_NOTICE("homescreen-service","please input icon.");
343 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
347 HMI_NOTICE("homescreen-service","please input text.");
348 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
355 * showInformation event
358 * - request : the request
365 int HS_Client::showInformation(afb_req_t request)
367 HMI_NOTICE("homescreen-service"," called.");
369 const char *value = afb_req_value(request, _info);
371 HMI_NOTICE("homescreen-service","info is %s", value);
372 std::string appid = std::move(get_application_id(request));
374 HMI_NOTICE("homescreen-service","can't get application identifier");
375 return AFB_REQ_GETAPPLICATIONID_ERROR;
378 struct json_object* param_obj = json_object_new_object();
379 json_object_object_add(param_obj, _info, json_object_new_string(value));
380 struct json_object* push_obj = json_object_new_object();
381 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
382 json_object_object_add(push_obj, _parameter, param_obj);
383 afb_event_push(my_event, push_obj);
386 HMI_NOTICE("homescreen-service","please input information.");
387 ret = AFB_REQ_SHOWINFORMATION_ERROR;
394 * check if client subscribe event
397 * - event: homescreen event, tap_shortcut etc.
404 bool HS_Client::checkEvent(const char* event)
406 auto ip = event_list.find(std::string(event));
407 if(ip == event_list.end())
414 * check if event is supporting
417 * - event: homescreen event, tap_shortcut etc.
421 * false: not fosupportund
424 bool HS_Client::isSupportEvent(const char* event)
426 auto ip = func_list.find(std::string(event));
427 if(ip == func_list.end())
434 * handle homescreen event
437 * - request : the request
438 * - verb: request verb name
445 int HS_Client::handleRequest(afb_req_t request, const char *verb)
447 HMI_NOTICE("homescreen-service","called.");
448 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
451 int ret = AFB_EVENT_BAD_REQUEST;
452 auto ip = func_list.find(std::string(verb));
453 if(ip != func_list.end()) {
454 HMI_NOTICE("homescreen-service","[%s]verb found", verb);
455 ret = (this->*(ip->second))(request);