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 event_list.insert(std::string(value));
161 ret = afb_req_subscribe(request, my_event);
168 HMI_NOTICE("homescreen-service","Please input event name");
169 ret = AFB_EVENT_BAD_REQUEST;
178 * - request : the request
185 int HS_Client::unsubscribe(afb_req_t request)
187 HMI_NOTICE("homescreen-service"," called.");
189 const char *value = afb_req_value(request, _event);
191 HMI_NOTICE("homescreen-service","unsubscribe %s event", value);
192 event_list.erase(std::string(value));
193 if(event_list.empty()) {
194 ret = afb_req_unsubscribe(request, my_event);
198 HMI_NOTICE("homescreen-service","Please input event name");
199 ret = AFB_EVENT_BAD_REQUEST;
208 * - request : the request
215 int HS_Client::showWindow(afb_req_t request)
217 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
219 struct json_object* push_obj = json_object_new_object();
220 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
221 const char* param = afb_req_value(request, _parameter);
223 std::string req_appid = std::move(get_application_id(request));
224 if(req_appid.empty()) {
225 HMI_NOTICE("homescreen-service","can't get application identifier");
226 return AFB_REQ_GETAPPLICATIONID_ERROR;
229 struct json_object* param_obj = json_tokener_parse(param);
230 json_object_object_add(param_obj, _replyto, json_object_new_string(req_appid.c_str()));
231 json_object_object_add(push_obj, _parameter, param_obj);
232 afb_event_push(my_event, push_obj);
235 HMI_ERROR("homescreen-service","please input correct parameter.");
236 ret = AFB_EVENT_BAD_REQUEST;
245 * - request : the request
252 int HS_Client::hideWindow(afb_req_t request)
254 HMI_NOTICE("homescreen-service"," called.");
255 std::string req_appid = std::move(get_application_id(request));
256 if(req_appid.empty()) {
257 HMI_NOTICE("homescreen-service","can't get application identifier");
258 return AFB_REQ_GETAPPLICATIONID_ERROR;
261 struct json_object* push_obj = json_object_new_object();
262 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(),
263 _type, __FUNCTION__);
264 struct json_object* param_obj = json_object_new_object();
265 json_object_object_add(param_obj, _caller, json_object_new_string(req_appid.c_str()));
266 json_object_object_add(push_obj, _parameter, param_obj);
267 afb_event_push(my_event, push_obj);
272 * replyShowWindow event
275 * - request : the request
282 int HS_Client::replyShowWindow(afb_req_t request)
284 HMI_NOTICE("homescreen-service","%s application_id = %s.", __FUNCTION__, my_id.c_str());
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(), _type, __FUNCTION__);
288 const char* param = afb_req_value(request, _parameter);
290 json_object_object_add(push_obj, _parameter, json_tokener_parse(param));
291 afb_event_push(my_event, push_obj);
294 HMI_ERROR("homescreen-service","please input correct parameter.");
295 ret = AFB_EVENT_BAD_REQUEST;
301 * showNotification event
304 * - request : the request
311 int HS_Client::showNotification(afb_req_t request)
313 HMI_NOTICE("homescreen-service"," called.");
315 const char *value = afb_req_value(request, _text);
317 HMI_NOTICE("homescreen-service","text is %s", value);
318 std::string appid =std::move(get_application_id(request));
320 HMI_NOTICE("homescreen-service","can't get application identifier");
321 return AFB_REQ_GETAPPLICATIONID_ERROR;
324 struct json_object* param_obj = json_object_new_object();
325 const char *icon = afb_req_value(request, _icon);
327 json_object_object_add(param_obj, _icon, json_object_new_string(icon));
328 json_object_object_add(param_obj, _text, json_object_new_string(value));
329 json_object_object_add(param_obj, _caller, json_object_new_string(appid.c_str()));
330 struct json_object* push_obj = json_object_new_object();
331 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
332 json_object_object_add(push_obj, _parameter, param_obj);
333 afb_event_push(my_event, push_obj);
336 HMI_NOTICE("homescreen-service","please input icon.");
337 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
341 HMI_NOTICE("homescreen-service","please input text.");
342 ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
349 * showInformation event
352 * - request : the request
359 int HS_Client::showInformation(afb_req_t request)
361 HMI_NOTICE("homescreen-service"," called.");
363 const char *value = afb_req_value(request, _info);
365 HMI_NOTICE("homescreen-service","info is %s", value);
366 std::string appid = std::move(get_application_id(request));
368 HMI_NOTICE("homescreen-service","can't get application identifier");
369 return AFB_REQ_GETAPPLICATIONID_ERROR;
372 struct json_object* param_obj = json_object_new_object();
373 json_object_object_add(param_obj, _info, json_object_new_string(value));
374 struct json_object* push_obj = json_object_new_object();
375 hs_add_object_to_json_object_str( push_obj, 4, _application_id, my_id.c_str(), _type, __FUNCTION__);
376 json_object_object_add(push_obj, _parameter, param_obj);
377 afb_event_push(my_event, push_obj);
380 HMI_NOTICE("homescreen-service","please input information.");
381 ret = AFB_REQ_SHOWINFORMATION_ERROR;
388 * check if client subscribe event
391 * - event: homescreen event, tap_shortcut etc.
398 bool HS_Client::checkEvent(const char* event)
400 auto ip = event_list.find(std::string(event));
401 if(ip == event_list.end())
408 * handle homescreen event
411 * - request : the request
412 * - verb: request verb name
419 int HS_Client::handleRequest(afb_req_t request, const char *verb)
421 HMI_NOTICE("homescreen-service","called.");
422 if((strcasecmp(verb, "subscribe") && strcasecmp(verb, "unsubscribe")) && !checkEvent(verb))
425 int ret = AFB_EVENT_BAD_REQUEST;
426 auto ip = func_list.find(std::string(verb));
427 if(ip != func_list.end()) {
428 HMI_NOTICE("homescreen-service","[%s]verb found", verb);
429 ret = (this->*(ip->second))(request);