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"
28 * - event : received event name
29 * - object : received json object
32 * 0 : event can transfer to others
33 * 1 : event not transfer to others
35 int event_handler(afb_api_t api, const char *event, struct json_object *object)
37 return HS_VuiAdapter::instance()->onEvent(api, event, object);
40 /* -------------------------------------Vui_Navigation------------------------------------------ */
42 const std::map<std::string, Vui_Navigation::func_handler> Vui_Navigation::func_list = {
43 {"set_destination", &Vui_Navigation::set_destination},
44 {"cancel_navigation", &Vui_Navigation::cancel_navigation}
46 const char _vui_prefixe[] = "vui";
47 const char _poi[] = "poi";
48 const char _navigation[] = "navigation";
49 const char _destination[] = "destination";
50 const char _coordinate[] = "coordinate";
51 const char _latitudeInDegrees[] = "latitudeInDegrees";
52 const char _longitudeInDegrees[] = "longitudeInDegrees";
53 const char _setDestination[] = "setDestination";
54 const char _cancelDestination[] = "cancelDestination";
55 const char _startNavigation[] = "startNavigation";
56 const char _stopNavigation[] = "stopNavigation";
68 void Vui_Navigation::init(afb_api_t api)
70 std::list<std::string> ev_list;
71 for(auto &it : func_list) {
72 ev_list.push_back(it.first);
73 std::string ev_name = std::string(_vshl_capabilities) + '/' + it.first;
74 AFB_INFO("setEventHook event %s", ev_name.c_str());
75 setEventHook(ev_name.c_str(), event_handler);
77 HS_VshlCapabilitiesProxy proxy;
78 proxy.subscribe(api, _navigation, ev_list);
85 * - api : the api serving the request
86 * - event : event name
87 * - object : event json object
90 * 0 : continue transfer
94 int Vui_Navigation::onEvent(afb_api_t api, const char *event, struct json_object *object)
96 std::string ev(event);
97 auto pos = ev.find('/');
98 auto ip = func_list.find(ev.substr(pos + 1));
100 if(ip != func_list.end()) {
101 (this->*(ip->second))(api, object);
108 * set_destination event handler
111 * - api : the api serving the reques
112 * - object : event json object
118 void Vui_Navigation::set_destination(afb_api_t api, struct json_object *object)
120 AFB_INFO("set dest: %s", json_object_to_json_string(object));
121 struct json_object *j_dest, *j_coord, *j_latitude, *j_longitude;
122 struct json_object *j_obj = json_tokener_parse(json_object_get_string(object));
123 if(json_object_object_get_ex(j_obj, _destination, &j_dest)
124 && json_object_object_get_ex(j_dest, _coordinate, &j_coord)
125 && json_object_object_get_ex(j_coord, _latitudeInDegrees, &j_latitude)
126 && json_object_object_get_ex(j_coord, _longitudeInDegrees, &j_longitude)) {
127 m_dest = std::make_pair(json_object_get_double(j_latitude), json_object_get_double(j_longitude));
130 AFB_WARNING("input data error.");
134 auto b_pair = std::make_pair<bool, bool>(false, false);
135 if(HS_ClientManager::instance()->isAppStarted(std::string(_poi))) {
137 set_destination2poi(api);
140 this->addListenAppId(_poi);
141 std::string id = HS_AppInfo::instance()->getAppProperty(_poi, _keyId);
142 HS_AfmMainProxy afm_proxy;
143 afm_proxy.start(api, id);
146 if(HS_ClientManager::instance()->isAppStarted(std::string(_navigation))) {
147 b_pair.second = true;
148 start_navigation(api);
151 this->addListenAppId(_navigation);
152 std::string id = HS_AppInfo::instance()->getAppProperty(_navigation, _keyId);
153 HS_AfmMainProxy afm_proxy;
154 afm_proxy.start(api, id);
156 m_start_flg.swap(b_pair);
157 if (!listenAppEmpty()) {
158 HS_ClientManager::instance()->addListener(this);
163 * cancel_navigation event handler
166 * - api : the api serving the reques
167 * - object : event json object
173 void Vui_Navigation::cancel_navigation(afb_api_t api, struct json_object *object)
175 HS_ClientManager::instance()->pushEvent(_stopNavigation, nullptr, _navigation);
183 * - appid : application id
189 void Vui_Navigation::notify(afb_api_t api, std::string appid)
191 AFB_INFO("recover appid=[%s].", appid.c_str());
192 if(isListenAppId(appid)) {
194 m_start_flg.first = true;
196 set_destination2poi(api);
198 else if(appid == _navigation) {
199 m_start_flg.second = true;
200 start_navigation(api);
203 AFB_WARNING("%s isn't interest app.", appid.c_str());
208 if(m_start_flg.first && m_start_flg.second) {
210 // HS_ClientManager::instance()->removeListener(this);
215 * set destination to poiapp
224 void Vui_Navigation::set_destination2poi(afb_api_t api)
226 struct json_object *param = json_object_new_object();
227 json_object_object_add(param, _latitudeInDegrees, json_object_new_double(m_dest.first));
228 json_object_object_add(param, _longitudeInDegrees, json_object_new_double(m_dest.second));
229 HS_ClientManager::instance()->pushEvent(_setDestination, param, _poi);
233 * set destination and start navigation
242 void Vui_Navigation::start_navigation(afb_api_t api)
244 HS_ClientManager::instance()->pushEvent(_startNavigation, nullptr, _navigation);
245 HS_ClientManager::instance()->pushEvent("showWindow", nullptr, _navigation);
248 /* -------------------------------------HS_VuiAdapter------------------------------------------ */
250 HS_VuiAdapter* HS_VuiAdapter::me = nullptr;
259 * HS_VuiAdapter instance pointer
262 HS_VuiAdapter* HS_VuiAdapter::instance(void)
265 me = new HS_VuiAdapter();
280 void HS_VuiAdapter::init(afb_api_t api)
282 if(afb_api_require_api(api, _vshl_capabilities, 0)) {
283 AFB_INFO("%s api isn't existing.", _vshl_capabilities);
287 AFB_INFO("%s api is existing.", _vshl_capabilities);
290 std::string uid = std::string(_vui_prefixe) + std::string("-") + _navigation;
291 module_list[uid] = new Vui_Navigation(uid);
293 for(auto &it : module_list) {
294 it.second->init(api);
302 * - api : the api serving the request
303 * - event : event name
304 * - object : event json object
307 * 0 : continue transfer
311 int HS_VuiAdapter::onEvent(afb_api_t api, const char *event, struct json_object *object)
313 for(auto &it : module_list) {
314 if(it.second->onEvent(api, event, object))