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