sleep add
[apps/agl-service-homescreen.git] / src / hs-vuiadapter.cpp
1 /*
2  * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "hs-vuiadapter.h"
18 #include "hs-clientmanager.h"
19 #include "hs-proxy.h"
20 #include "hs-appinfo.h"
21 #include "unistd.h"
22
23 /**
24  * event handler
25  *
26  * #### Parameters
27  * - api : the api
28  * - event : received event name
29  * - object : received json object
30  *
31  * #### Return
32  * 0 : event can transfer to others
33  * 1 : event not transfer to others
34  */
35 int event_handler(afb_api_t api, const char *event, struct json_object *object)
36 {
37     return HS_VuiAdapter::instance()->onEvent(api, event, object);
38 }
39
40 /* -------------------------------------Vui_Navigation------------------------------------------ */
41
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}
45 };
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";
57
58 /**
59  * init
60  *
61  * #### Parameters
62  *  - api : the api
63  *
64  * #### Return
65  * None
66  *
67  */
68 void Vui_Navigation::init(afb_api_t api)
69 {
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);
76     }
77     HS_VshlCapabilitiesProxy proxy;
78     proxy.subscribe(api, _navigation, ev_list);
79 }
80
81 /**
82  * handle event
83  *
84  * #### Parameters
85  *  - api : the api serving the request
86  *  - event  : event name
87  *  - object : event json object
88  *
89  * #### Return
90  * 0 : continue transfer
91  * 1 : blocked
92  *
93  */
94 int Vui_Navigation::onEvent(afb_api_t api, const char *event, struct json_object *object)
95 {
96     std::string ev(event);
97     auto pos = ev.find('/');
98     auto ip = func_list.find(ev.substr(pos + 1));
99     int ret = 0;
100     if(ip != func_list.end()) {
101         (this->*(ip->second))(api, object);
102         ret = 1;
103     }
104     return ret;
105 }
106
107 /**
108  * set_destination event handler
109  *
110  * #### Parameters
111  *  - api : the api serving the reques
112  *  - object : event json object
113  *
114  * #### Return
115  * None
116  *
117  */
118 void Vui_Navigation::set_destination(afb_api_t api, struct json_object *object)
119 {
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));
128     }
129     else {
130         AFB_WARNING("input data error.");
131         return;
132     }
133
134     auto b_pair = std::make_pair<bool, bool>(false, false);
135     if(HS_ClientManager::instance()->isAppStarted(std::string(_poi))) {
136         b_pair.first = true;
137         set_destination2poi(api);
138     }
139     else {
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);
144     }
145
146     if(HS_ClientManager::instance()->isAppStarted(std::string(_navigation))) {
147         b_pair.second = true;
148         start_navigation(api);
149     }
150     else {
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);
155     }
156     m_start_flg.swap(b_pair);
157     if (!listenAppEmpty()) {
158         HS_ClientManager::instance()->addListener(this);
159     }
160 }
161
162 /**
163  * cancel_navigation event handler
164  *
165  * #### Parameters
166  *  - api : the api serving the reques
167  *  - object : event json object
168  *
169  * #### Return
170  * None
171  *
172  */
173 void Vui_Navigation::cancel_navigation(afb_api_t api, struct json_object *object)
174 {
175     HS_ClientManager::instance()->pushEvent(_stopNavigation, nullptr);
176 }
177
178 /**
179  * notify
180  *
181  * #### Parameters
182  *  - api : the api
183  *  - appid : application id
184  *
185  * #### Return
186  * None
187  *
188  */
189 void Vui_Navigation::notify(afb_api_t api, std::string appid)
190 {
191     if(isListenAppId(appid)) {
192         if (appid == _poi) {
193             m_start_flg.first = true;
194             sleep(1);
195             set_destination2poi(api);
196         }
197         else if(appid == _navigation) {
198             m_start_flg.second = true;
199             start_navigation(api);
200         }
201         else {
202             AFB_WARNING("%s isn't interest app.", appid.c_str());
203             return;
204         }
205     }
206     if(m_start_flg.first && m_start_flg.second) {
207         HS_ClientManager::instance()->removeListener(this);
208         clearListenAppSet();
209     }
210 }
211
212 /**
213  * set destination to poiapp
214  *
215  * #### Parameters
216  *  - api : the api
217  *
218  * #### Return
219  * None
220  *
221  */
222 void Vui_Navigation::set_destination2poi(afb_api_t api)
223 {
224     struct json_object *param = json_object_new_object();
225     json_object_object_add(param, _latitudeInDegrees, json_object_new_double(m_dest.first));
226     json_object_object_add(param, _longitudeInDegrees, json_object_new_double(m_dest.second));
227     HS_ClientManager::instance()->pushEvent(_setDestination, param);
228 }
229
230 /**
231  * set destination and start navigation
232  *
233  * #### Parameters
234  *  - api : the ap
235  *
236  * #### Return
237  * None
238  *
239  */
240 void Vui_Navigation::start_navigation(afb_api_t api)
241 {
242     HS_ClientManager::instance()->pushEvent(_startNavigation, nullptr);
243     HS_ClientManager::instance()->pushEvent("showWindow", nullptr, _navigation);
244 }
245
246 /* -------------------------------------HS_VuiAdapter------------------------------------------ */
247
248 HS_VuiAdapter* HS_VuiAdapter::me = nullptr;
249
250 /**
251  * get instance
252  *
253  * #### Parameters
254  *  - Nothing
255  *
256  * #### Return
257  * HS_VuiAdapter instance pointer
258  *
259  */
260 HS_VuiAdapter* HS_VuiAdapter::instance(void)
261 {
262     if(me == nullptr)
263         me = new HS_VuiAdapter();
264
265     return me;
266 }
267
268 /**
269  * init
270  *
271  * #### Parameters
272  *  - api : the api
273  *
274  * #### Return
275  * None
276  *
277  */
278 void HS_VuiAdapter::init(afb_api_t api)
279 {
280     std::string uid = std::string(_vui_prefixe) + std::string("-") + _navigation;
281     module_list[uid] = new Vui_Navigation(uid);
282
283     for(auto &it : module_list) {
284         it.second->init(api);
285     }
286 }
287
288 /**
289  * handle event
290  *
291  * #### Parameters
292  *  - api : the api serving the request
293  *  - event  : event name
294  *  - object : event json object
295  *
296  * #### Return
297  * 0 : continue transfer
298  * 1 : blocked
299  *
300  */
301 int HS_VuiAdapter::onEvent(afb_api_t api, const char *event, struct json_object *object)
302 {
303     for(auto &it : module_list) {
304         if(it.second->onEvent(api, event, object))
305             return 1;
306     }
307     return 0;
308 }