Adapt the AIL filter used to new ones
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCVInfoManager.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 //==========================================================================
11 /**
12  *  @file   CicoSCVInfoManager.cpp
13  *
14  *  @brief  This file implementation of CicoSCVInfoManager class
15  */
16 //==========================================================================
17
18 #include <string>
19
20 #include <Ecore.h>
21
22 #include "CicoLog.h"
23 #include <ico_dbus_amb_efl.h>
24 #include "CicoSCVInfoManager.h"
25 #include "CicoSystemConfig.h"
26 #include "CicoConf.h"
27 #include "CicoSCVInfo.h"
28
29 //==========================================================================    
30 //  private static variable
31 //==========================================================================    
32 CicoSCVInfoManager* CicoSCVInfoManager::ms_myInstance = NULL;
33 int CicoSCVInfoManager::ms_ecoreEvType= 0;
34 dbus_type CicoSCVInfoManager::ms_dbusTypes[ICO_TYPE_MAX];
35 int CicoSCVInfoManager::ms_retryCnt = 0;
36
37 //--------------------------------------------------------------------------
38 /**
39  *  @brief  default constructor
40  */
41 //--------------------------------------------------------------------------
42 CicoSCVInfoManager::CicoSCVInfoManager()
43     : m_initialized(false)
44 {
45     ms_dbusTypes[ICO_TYPE_UNKNOW]  = DBUS_TYPE_INT32;
46     ms_dbusTypes[ICO_TYPE_BOOLEAN] = DBUS_TYPE_BOOLEAN;
47     ms_dbusTypes[ICO_TYPE_INT8]    = DBUS_TYPE_BYTE;
48     ms_dbusTypes[ICO_TYPE_UINT8]   = DBUS_TYPE_BYTE;
49     ms_dbusTypes[ICO_TYPE_INT16]   = DBUS_TYPE_INT16;
50     ms_dbusTypes[ICO_TYPE_UINT16]  = DBUS_TYPE_UINT16;
51     ms_dbusTypes[ICO_TYPE_INT32]   = DBUS_TYPE_INT32;
52     ms_dbusTypes[ICO_TYPE_UINT32]  = DBUS_TYPE_UINT32;
53     ms_dbusTypes[ICO_TYPE_INT64]   = DBUS_TYPE_INT64;
54     ms_dbusTypes[ICO_TYPE_UINT64]  = DBUS_TYPE_UINT64;
55     ms_dbusTypes[ICO_TYPE_DOUBLE]  = DBUS_TYPE_DOUBLE;
56     ms_dbusTypes[ICO_TYPE_STRING]  = DBUS_TYPE_STRING;
57
58     ms_ecoreEvType = ecore_event_type_new();
59
60     CicoSCVInfoManager::ms_myInstance = this;
61 }
62
63 //--------------------------------------------------------------------------
64 /**
65  *  @brief  destructor
66  */
67 //--------------------------------------------------------------------------
68 CicoSCVInfoManager::~CicoSCVInfoManager()
69 {
70     CicoSCVInfoManager::ms_myInstance = NULL;
71 }
72
73 //--------------------------------------------------------------------------
74 /**
75  *  @brief  get instance of CicoSystemConfig
76  *
77  *  @return  pointer of CicoSystemConfig object
78  */
79 //--------------------------------------------------------------------------
80 CicoSCVInfoManager*
81 CicoSCVInfoManager::getInstance(void)
82 {
83     if (NULL == ms_myInstance) {
84         ms_myInstance = new CicoSCVInfoManager();
85     }
86     return ms_myInstance;
87 }
88
89 //--------------------------------------------------------------------------
90 /**
91  *  @brief  startup communication of vehicle information 
92  *
93  *  @return result
94  *  @retval true    success
95  *  @retval false   error
96  */
97 //--------------------------------------------------------------------------
98 bool
99 CicoSCVInfoManager::startup(void)
100 {
101     ICO_TRA("CicoSCVInfoManager::startup Enter");
102
103     if (true == m_initialized) {
104         ICO_DBG("already initialized.");
105         ICO_TRA("CicoSCVInfoManager::startup Leave(true)");
106     }
107
108     ICO_DBG("called: ico_dbus_amb_start");
109     int ret = ico_dbus_amb_start();
110     if (0 != ret) {
111         ICO_ERR("ico_dbus_amb_start failed. ret=%d", ret);
112         ICO_TRA("CicoSCVInfoManager::startup Leave(false)");
113         return false;
114     }
115
116     m_initialized = true;
117
118     startMonitoring();
119    
120
121     ICO_TRA("CicoSCVInfoManager::startup Leave(true)");
122     return true;
123 }
124   
125 //--------------------------------------------------------------------------
126 /**
127  *  @brief  teardown communication of vehicle information
128  *
129  *  @return result
130  *  @retval true    success
131  *  @retval false   error
132  */
133 //--------------------------------------------------------------------------
134 bool
135 CicoSCVInfoManager::teardown(void)
136 {
137     ICO_TRA("CicoSCVInfoManager::teardown Enter");
138
139     if (false == m_initialized) {
140         ICO_DBG("not initialized.");
141         ICO_TRA("CicoSCVInfoManager::teardown Leave(true)");
142     }
143
144     m_initialized = false;
145
146     stopMonitoring();
147
148     ICO_DBG("called: ico_dbus_amb_end");
149     int ret = ico_dbus_amb_end();
150     if (0 != ret) {
151         ICO_ERR("ico_dbus_amb_end faile. ret=%d", ret);
152         ICO_TRA("CicoSCVInfoManager::teardown Leave(false)");
153         return false;
154     }
155
156     ICO_TRA("CicoSCVInfoManager::teardown Leave(true)");
157     return true;
158 }
159
160 //--------------------------------------------------------------------------
161 /**
162  *  @brief  get ecore event type of changed vehicle information
163  *
164  *  @return ecore event type
165  */
166 //--------------------------------------------------------------------------
167 int
168 CicoSCVInfoManager::getEcoreEvType(void)
169 {
170     return ms_ecoreEvType;
171 }
172
173 //--------------------------------------------------------------------------
174 /**
175  *  @brief  get vehicle information
176  *
177  *  @param [in] property    vehicle information property
178  *
179  *  @return result
180  *  @retval true    success
181  *  @retval false   error
182  */
183 //--------------------------------------------------------------------------
184 bool
185 CicoSCVInfoManager::getVehicleInfo(int property)
186 {
187     ICO_TRA("CicoSCVInfoManager::getVehicleInfo Enter(prop=%d)", property);
188
189     if (false == m_initialized) {
190         ICO_DBG("not initialized.");
191         ICO_TRA("CicoSCVInfoManager::getVehicleInfo Leave(false)");
192         return false;
193     }
194
195     CicoSystemConfig* sysconf = CicoSystemConfig::getInstance();
196     CicoSCVehicleInfoConf* viconf = sysconf->getVehicleInfoConf();
197     std::map<int, CicoSCVIPropertyConf*>::iterator itr;
198     itr = viconf->properties.find(property);
199     if (itr == viconf->properties.end()) {
200         ICO_WRN("not found propetry(%d)", property);
201         return false;
202     }
203  
204     ICO_DBG("called: ico_dbus_amb_get(objname=%s prop=%s zone=%d type=%d)",
205             itr->second->objname.c_str(),
206             itr->second->property.c_str(),
207             itr->second->zone,
208             ms_dbusTypes[itr->second->type]);
209     int ret = ico_dbus_amb_get(itr->second->objname.c_str(),
210                                itr->second->property.c_str(),
211                                itr->second->zone,
212                                ms_dbusTypes[itr->second->type],
213                                CicoSCVInfoManager::dbusAMBGetCB,
214                                itr->second);
215     if (0 != ret) {
216         ICO_WRN("ico_dbus_amb_get failed. ret=%d", ret);
217         ICO_TRA("CicoSCVInfoManager::getVehicleInfo Leave(false)");
218         return false;
219     }
220
221     ICO_TRA("CicoSCVInfoManager::getVehicleInfo Leave(true)");
222     return true;
223 }
224
225 //==========================================================================
226 //  private functions
227 //==========================================================================
228
229 //--------------------------------------------------------------------------
230 /**
231  *  @brief  start vehicle information monitoring
232  */
233 //--------------------------------------------------------------------------
234 void
235 CicoSCVInfoManager::startMonitoring(void)
236 {
237     ICO_TRA("CicoSCVInfoManager::startMonitoring Enter");
238
239     CicoSystemConfig* sysconf = CicoSystemConfig::getInstance();
240     CicoSCVehicleInfoConf* viconf = sysconf->getVehicleInfoConf();
241     std::map<int, CicoSCVIPropertyConf*>::iterator itr;
242     itr = viconf->properties.begin();
243
244     for (; itr != viconf->properties.end(); ++itr) {
245         ICO_DBG("called: ico_dbus_amb_find_property"
246                 "(objname=%s prop=%s zone=%d type=%d)",
247                 itr->second->objname.c_str(),
248                 itr->second->property.c_str(),
249                 itr->second->zone,
250                 ms_dbusTypes[itr->second->type]);
251         int ret = ico_dbus_amb_find_property(itr->second->objname.c_str(),
252                                              itr->second->property.c_str(),
253                                              itr->second->zone,
254                                              ms_dbusTypes[itr->second->type],
255                                              CicoSCVInfoManager::dbusAMBFindCB,
256                                              itr->second);
257         if (0 != ret) {
258             ICO_WRN("ico_dbus_amb_find_property failed. ret=%d", ret);
259         }
260     }
261
262     ICO_TRA("CicoSCVInfoManager::startMonitoring Leave");
263 }
264
265 //--------------------------------------------------------------------------
266 /**
267  *  @brief  stop vehicle information monitoring
268  */
269 //--------------------------------------------------------------------------
270 void
271 CicoSCVInfoManager::stopMonitoring(void)
272 {
273     ICO_TRA("CicoSCVInfoManager::stopMonitoring Enter");
274
275     CicoSystemConfig* sysconf = CicoSystemConfig::getInstance();
276     CicoSCVehicleInfoConf* viconf = sysconf->getVehicleInfoConf();
277     std::map<int, CicoSCVIPropertyConf*>::iterator itr;
278     itr = viconf->properties.begin();
279     for (; itr != viconf->properties.end(); ++itr) {
280         ICO_DBG("called: ico_dbus_amb_unsubscribe"
281                 "(objname=%s prop=%s zone=%d type=%d)",
282                 itr->second->objname.c_str(),
283                 itr->second->property.c_str(),
284                 itr->second->zone,
285                 ms_dbusTypes[itr->second->type]);
286         int ret = ico_dbus_amb_unsubscribe(itr->second->objname.c_str(),
287                                            itr->second->property.c_str(),
288                                            itr->second->zone);
289         if (0 != ret) {
290             ICO_WRN("ico_dbus_amb_subscribe failed. ret=%d", ret);
291         }
292     }
293
294     ICO_TRA("CicoSCVInfoManager::stopMonitoring Leave");
295 }
296
297 //--------------------------------------------------------------------------
298 /**
299  *  @brief  callback function of Ecore_End_Cb
300  *
301  *  @param [in] user_data   user data
302  *  @param [in] func_data   funnction data
303  */
304 //--------------------------------------------------------------------------
305 void
306 CicoSCVInfoManager::evVInfoFree(void* user_data, void* func_data)
307 {
308     ICO_TRA("CicoSCVInfoManager::evVInfoFree Enter");
309     delete static_cast<CicoSCVInfo*>(func_data);
310     ICO_TRA("CicoSCVInfoManager::evVInfoFree Leave");
311 }
312
313 //--------------------------------------------------------------------------
314 /**
315  *  @brief  callback function of ecore timer
316  *
317  *  @param [in] data    user data
318  */
319 //--------------------------------------------------------------------------
320 Eina_Bool
321 CicoSCVInfoManager::ecoreRetryFindPropCB(void *data)
322 {
323     ICO_TRA("CicoSCVInfoManager::ecoreRetryFindPropCB Enter");
324     if (NULL != data) {
325         CicoSCVIPropertyConf* viProp = static_cast<CicoSCVIPropertyConf*>(data);
326         ICO_DBG("called: ico_dbus_amb_find_property"
327                 "(objname=%s prop=%s zone=%d type=%d)",
328                 viProp->objname.c_str(),
329                 viProp->property.c_str(),
330                 viProp->zone,
331                 ms_dbusTypes[viProp->type]);
332         int ret = ico_dbus_amb_find_property(viProp->objname.c_str(),
333                                              viProp->property.c_str(),
334                                              viProp->zone,
335                                              ms_dbusTypes[viProp->type],
336                                              CicoSCVInfoManager::dbusAMBFindCB,
337                                              viProp);
338         if (0 != ret) {
339             ICO_WRN("ico_dbus_amb_property failed. ret=%d", ret);
340         }
341
342     }
343     ICO_TRA("CicoSCVInfoManager::ecoreRetryFindPropCB Leave(cancel)");
344     return ECORE_CALLBACK_CANCEL;
345 }
346
347 //--------------------------------------------------------------------------
348 /**
349  *  @brief  callback function of ico_dbus_amb_get result
350  *
351  *  @param [in] objectname  dbus object name
352  *  @param [in] property    vehicle information's name
353  *  @param [in] type        type of vechcle information
354  *  @param [in] data        user data
355  *  @param [in] error       error information
356  */
357 //--------------------------------------------------------------------------
358 void
359 CicoSCVInfoManager::dbusAMBFindCB(const char *objectname,
360                                  const char *property,
361                                  dbus_type type,
362                                  void *data,
363                                  ico_dbus_error_t *error)
364 {
365     ICO_TRA("CicoSCVInfoManager::dbusAMBFindCB Enter"
366             "(objname=%s prop=%s type=%d)",
367             objectname, property, type);
368
369     if (NULL == data) {
370         ICO_WRN("data is null");
371         return;
372     }
373
374     CicoSCVIPropertyConf* viProp = static_cast<CicoSCVIPropertyConf*>(data);
375
376     // if find property is error, retry.
377     if (NULL != error) {
378         CicoSystemConfig* sysconf = CicoSystemConfig::getInstance();
379         CicoSCVehicleInfoConf* viconf = sysconf->getVehicleInfoConf();
380         ICO_DBG("retryCnt=%d/%d", ms_retryCnt, viconf->retryCnt);
381         if (viconf->retryCnt > ms_retryCnt) {
382             ms_retryCnt++;
383             ICO_DBG("called: ecore_timer_add");
384             Ecore_Timer *timer = NULL;
385             timer = ecore_timer_add((double)viconf->waitTime/1000.0f,
386                                     CicoSCVInfoManager::ecoreRetryFindPropCB,
387                                     viProp);
388             if (NULL == timer) {
389                 ICO_ERR("ecore_timer_add() failed.");
390             }
391
392         }
393         ICO_TRA("CicoSCVInfoManager::dbusAMBFindCB Levae");
394         return;
395     }
396
397     // get vehicle information
398     ICO_DBG("called: ico_dbus_amb_get(objname=%s prop=%s zone=%d type=%d)",
399             viProp->objname.c_str(),
400             viProp->property.c_str(),
401             viProp->zone,
402             ms_dbusTypes[viProp->type]);
403     int ret = ico_dbus_amb_get(viProp->objname.c_str(),
404                                viProp->property.c_str(),
405                                viProp->zone,
406                                ms_dbusTypes[viProp->type],
407                                CicoSCVInfoManager::dbusAMBGetCB,
408                                viProp);
409     if (0 != ret) {
410         ICO_WRN("ico_dbus_amb_subscribe failed. ret=%d", ret);
411     }
412
413     // subscribe vehicle information
414     ICO_DBG("called: ico_dbus_amb_subscribe"
415             "(objname=%s prop=%s zone=%d type=%d)",
416             viProp->objname.c_str(),
417             viProp->property.c_str(),
418             viProp->zone,
419             ms_dbusTypes[viProp->type]);
420     ret = ico_dbus_amb_subscribe(viProp->objname.c_str(),
421                                  viProp->property.c_str(),
422                                  viProp->zone,
423                                  ms_dbusTypes[viProp->type],
424                                  CicoSCVInfoManager::dbusAMBNotifyCB,
425                                  viProp);
426     if (0 != ret) {
427         ICO_WRN("ico_dbus_amb_subscribe failed. ret=%d", ret);
428     }
429
430     ICO_TRA("CicoSCVInfoManager::dbusAMBFindCB Levae");
431 }
432
433 //--------------------------------------------------------------------------
434 /**
435  *  @brief  callback function of ico_dbus_amb_get result
436  *
437  *  @param [in] objectname  dbus object name
438  *  @param [in] property    vehicle information's name
439  *  @param [in] type        type of vechcle information
440  *  @param [in] value       vechcle information's value
441  *  @param [in] data        user data
442  */
443 //--------------------------------------------------------------------------
444 void
445 CicoSCVInfoManager::dbusAMBGetCB(const char *objectname,
446                                  const char *property,
447                                  dbus_type type,
448                                  union dbus_value_variant value,
449                                  void *data)
450 {
451     ICO_TRA("CicoSCVInfoManager::dbusAMBGetCB Enter"
452             "(objname=%s prop=%s type=%d)",
453             objectname, property, type);
454
455     if (NULL == data) {
456         ICO_WRN("data is null");
457         return;
458     }
459
460     CicoSCVIPropertyConf* viConf = static_cast<CicoSCVIPropertyConf*>(data);
461
462     CicoSCVInfo* vinfo = new CicoSCVInfo(type, viConf->id, value);
463     ecore_event_add(ms_ecoreEvType, vinfo,
464                     CicoSCVInfoManager::evVInfoFree, NULL);
465
466     ICO_TRA("CicoSCVInfoManager::dbusAMBGetCB Levae");
467 }
468
469 //--------------------------------------------------------------------------
470 /**
471  *  @brief  callback function of ico_dbus_amb_get result
472  *
473  *  @param [in] objectname  dbus object name
474  *  @param [in] property    vehicle information's name
475  *  @param [in] type        type of vechcle information
476  *  @param [in] value       vechcle information's value
477  *  @param [in] sequence    number of sequence
478  *  @parma [in] tv          recored time
479  *  @param [in] data        user data
480  */
481 //--------------------------------------------------------------------------
482 void
483 CicoSCVInfoManager::dbusAMBNotifyCB(const char *objectname,
484                                     const char *property,
485                                     dbus_type type,
486                                     union dbus_value_variant value,
487                                     int sequence,
488                                     struct timeval tv,
489                                     void *data)
490 {
491     ICO_TRA("CicoSCVInfoManager::dbusAMBNotifyCB Enter"
492             "(objname=%s prop=%s type=%d)",
493             objectname, property, type);
494
495     if (NULL == data) {
496         ICO_WRN("data is null");
497         return;
498     }
499
500     CicoSCVIPropertyConf* viConf = static_cast<CicoSCVIPropertyConf*>(data);
501
502     CicoSCVInfo* vinfo = new CicoSCVInfo(type, viConf->id, value);
503     ecore_event_add(ms_ecoreEvType, vinfo,
504                     CicoSCVInfoManager::evVInfoFree, NULL);
505
506     ICO_TRA("CicoSCVInfoManager::dbusAMBNotifyCB Leave");
507 }
508 // vim:set expandtab ts=4 sw=4: