2 * Copyright (c) 2019 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-vuiadapter.h"
18 #include "hs-clientmanager.h"
20 #include "hs-appinfo.h"
27 * - event : received event name
28 * - object : received json object
31 * 0 : event can transfer to others
32 * 1 : event not transfer to others
34 int event_handler(afb_api_t api, const char *event, struct json_object *object)
36 return HS_VuiAdapter::instance()->onEvent(api, event, object);
39 /* -------------------------------------Vui_Navigation------------------------------------------ */
41 const std::map<std::string, Vui_Navigation::func_handler> Vui_Navigation::func_list = {
42 {"set_destination", &Vui_Navigation::set_destination},
43 {"cancel_navigation", &Vui_Navigation::cancel_navigation}
45 const char _vui_prefixe[] = "vui";
46 const char _poi[] = "poi";
47 const char _navigation[] = "navigation";
48 const char _destination[] = "destination";
49 const char _coordinate[] = "coordinate";
50 const char _latitudeInDegrees[] = "latitudeInDegrees";
51 const char _longitudeInDegrees[] = "longitudeInDegrees";
52 const char _setDestination[] = "setDestination";
53 const char _cancelDestination[] = "cancelDestination";
54 const char _startNavigation[] = "startNavigation";
55 const char _stopNavigation[] = "stopNavigation";
67 void Vui_Navigation::init(afb_api_t api)
69 std::list<std::string> ev_list;
70 for(auto &it : func_list) {
71 ev_list.push_back(it.first);
72 std::string ev_name = std::string(_vshl_capabilities) + '/' + it.first;
73 AFB_INFO("setEventHook event %s", ev_name.c_str());
74 setEventHook(ev_name.c_str(), event_handler);
76 HS_VshlCapabilitiesProxy proxy;
77 proxy.subscribe(api, _navigation, ev_list);
84 * - api : the api serving the request
85 * - event : event name
86 * - object : event json object
89 * 0 : continue transfer
93 int Vui_Navigation::onEvent(afb_api_t api, const char *event, struct json_object *object)
95 std::string ev(event);
96 auto pos = ev.find('/');
97 auto ip = func_list.find(ev.substr(pos + 1));
99 if(ip != func_list.end()) {
100 (this->*(ip->second))(api, object);
107 * set_destination event handler
110 * - api : the api serving the reques
111 * - object : event json object
117 void Vui_Navigation::set_destination(afb_api_t api, struct json_object *object)
119 AFB_INFO("set dest: %s", json_object_to_json_string(object));
120 struct json_object *j_dest, *j_coord, *j_latitude, *j_longitude;
121 struct json_object *j_obj = json_tokener_parse(json_object_get_string(object));
122 if(json_object_object_get_ex(j_obj, _destination, &j_dest)
123 && json_object_object_get_ex(j_dest, _coordinate, &j_coord)
124 && json_object_object_get_ex(j_coord, _latitudeInDegrees, &j_latitude)
125 && json_object_object_get_ex(j_coord, _longitudeInDegrees, &j_longitude)) {
126 m_dest = std::make_pair(json_object_get_double(j_latitude), json_object_get_double(j_longitude));
129 AFB_WARNING("input data error.");
133 auto b_pair = std::make_pair<bool, bool>(false, false);
134 if(HS_ClientManager::instance()->isAppStarted(std::string(_poi))) {
136 set_destination2poi(api);
139 this->addListenAppId(_poi);
140 std::string id = HS_AppInfo::instance()->getAppProperty(_poi, _keyId);
141 HS_AfmMainProxy afm_proxy;
142 afm_proxy.start(api, id);
145 if(HS_ClientManager::instance()->isAppStarted(std::string(_navigation))) {
146 b_pair.second = true;
147 start_navigation(api);
150 this->addListenAppId(_navigation);
151 std::string id = HS_AppInfo::instance()->getAppProperty(_navigation, _keyId);
152 HS_AfmMainProxy afm_proxy;
153 afm_proxy.start(api, id);
155 m_start_flg.swap(b_pair);
156 if (!listenAppEmpty()) {
157 HS_ClientManager::instance()->addListener(this);
162 * cancel_navigation event handler
165 * - api : the api serving the reques
166 * - object : event json object
172 void Vui_Navigation::cancel_navigation(afb_api_t api, struct json_object *object)
174 HS_ClientManager::instance()->pushEvent(_stopNavigation, nullptr);
182 * - appid : application id
188 void Vui_Navigation::notify(afb_api_t api, std::string appid)
190 if(isListenAppId(appid)) {
192 m_start_flg.first = true;
193 set_destination2poi(api);
195 else if(appid == _navigation) {
196 m_start_flg.second = true;
197 start_navigation(api);
200 AFB_WARNING("%s isn't interest app.", appid.c_str());
204 if(m_start_flg.first && m_start_flg.second) {
205 HS_ClientManager::instance()->removeListener(this);
211 * set destination to poiapp
220 void Vui_Navigation::set_destination2poi(afb_api_t api)
222 struct json_object *param = json_object_new_object();
223 json_object_object_add(param, _latitudeInDegrees, json_object_new_double(m_dest.first));
224 json_object_object_add(param, _longitudeInDegrees, json_object_new_double(m_dest.second));
225 HS_ClientManager::instance()->pushEvent(_setDestination, param);
229 * set destination and start navigation
238 void Vui_Navigation::start_navigation(afb_api_t api)
240 HS_ClientManager::instance()->pushEvent(_startNavigation, nullptr);
243 /* -------------------------------------HS_VuiAdapter------------------------------------------ */
245 HS_VuiAdapter* HS_VuiAdapter::me = nullptr;
254 * HS_VuiAdapter instance pointer
257 HS_VuiAdapter* HS_VuiAdapter::instance(void)
260 me = new HS_VuiAdapter();
275 void HS_VuiAdapter::init(afb_api_t api)
277 std::string uid = std::string(_vui_prefixe) + std::string("-") + _navigation;
278 module_list[uid] = new Vui_Navigation(uid);
280 for(auto &it : module_list) {
281 it.second->init(api);
289 * - api : the api serving the request
290 * - event : event name
291 * - object : event json object
294 * 0 : continue transfer
298 int HS_VuiAdapter::onEvent(afb_api_t api, const char *event, struct json_object *object)
300 for(auto &it : module_list) {
301 if(it.second->onEvent(api, event, object))