Bug Fix for TIVI-1935.
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCPolicyManager.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   CicoSCPolicyManager.cpp
13  *
14  *  @brief  This file implementation of CicoSCPolicyManager class
15  */
16 //==========================================================================
17
18 #include <string>
19 using namespace std;
20
21 #include "CicoSCPolicyManager.h"
22 #include "CicoStateMachine.h"
23 #include "CicoState.h"
24 #include "CicoHistoryState.h"
25 #include "CicoStateMachineCreator.h"
26 #include "CicoSCPolicyDef.h"
27 #include "CicoSCResourceManager.h"
28 #include "CicoLog.h"
29 #include "ico_syc_error.h"
30 #include "CicoSCServer.h"
31 #include "CicoSCMessage.h"
32 #include "ico_syc_msg_cmd_def.h"
33 #include "ico_syc_type.h"
34
35 //==========================================================================
36 //  define
37 //==========================================================================
38
39 // Defines of D-Bus target.
40 #define DBUS_SERVICE    "org.automotive.message.broker"
41 #define DBUS_INTERFACE  "org.freedesktop.DBus.Properties"
42 #define DBUS_METHOD     "Get"
43
44 // Defines of maximum number.
45 #define AMB_MAX_VHCLINFO    10  /**< maximum number of vehicle info */
46
47 // Enumerations of Vehicle information key.
48 typedef enum {
49     AMB_VHCL_SPEED  = 1,  /**< Vehicle speed */
50     AMB_SHIFT_POSITION,   /**< Shift position */
51     AMB_LIGHT_LEFT,       /**< Light left(Turn left) */
52     AMB_LIGHT_RIGHT,      /**< Light right(Turn right) */
53     AMB_NIGHT_MODE,       /**< Night mode */
54     AMB_MAX_INFO          /**< Maximum number of type */
55 } _amb_vhcl_key_e;
56
57 // 
58 typedef struct _vehicle_info_property_t {
59     int     key;        /* Vehicle Information key */
60     const char    *property;  /* D-Bus property name     */
61     const char    *path;      /* D-Bus path name         */
62     const char    *interface; /* D-Bus interface name    */
63 } vhcl_info_prop_t;
64
65 // structure of Vehicle information data.
66 typedef struct _vehicle_info_data {
67     int             key;    /* Vehicle Information key */
68     DBusPendingCall *pending;
69     int             request;
70     double          val;
71 } _vhcldata_t;
72
73 _vhcldata_t vhcl_data[AMB_MAX_INFO];
74
75 //*========================================================================
76 //  global variable
77 //========================================================================
78
79 static const vhcl_info_prop_t apf_vhcl_info[] = {
80     {
81         AMB_VHCL_SPEED,
82         "VehicleSpeed",
83         "/org/automotive/runningstatus/VehicleSpeed",
84         "org.automotive.VehicleSpeed"
85     },
86     {
87         AMB_SHIFT_POSITION,
88         "ShiftPosition",
89         "/org/automotive/runningstatus/Transmission",
90         "org.automotive.Transmission"
91     },
92 #if 0   // use LightStatus
93 /* use LightStatus, because AMB not support access of TurnSignal by D-Bus   */
94     {
95         ICO_SYC_VEHICLEINFO_TURN_SIGNAL,
96         "TurnSignal",
97         "/org/automotive/runningstatus/TurnSignal",
98         "org.automotive.TurnSignal"
99     },
100 #else   // use LightStatus
101     {
102         AMB_LIGHT_LEFT,
103         "LeftTurn",
104         "/org/automotive/runningstatus/LightStatus",
105         "org.automotive.LightStatus"
106     },
107     {
108         AMB_LIGHT_RIGHT,
109         "RightTurn",
110         "/org/automotive/runningstatus/LightStatus",
111         "org.automotive.LightStatus"
112     },
113 #endif   // use LightStatus
114     {
115         AMB_NIGHT_MODE,
116         "NightMode",
117         "/org/automotive/custom/NightMode",
118         "org.automotive.NightMode"
119     },
120     { 0, "\0", "\0", "\0" }
121 };
122
123 //--------------------------------------------------------------------------
124 /**
125  *  @brief  default constructor
126  */
127 //--------------------------------------------------------------------------
128 CicoSCPolicyManager::CicoSCPolicyManager(CicoSCResourceManager* resourceMgr)
129     : m_initialized(false),
130       m_dbusConnection(NULL),
131       m_ecoreTimer(NULL),
132       m_stateMachine(NULL),
133       m_resourceMgr(resourceMgr)
134 {
135 }
136
137 //--------------------------------------------------------------------------
138 /**
139  *  @brief  destructor
140  */
141 //--------------------------------------------------------------------------
142 CicoSCPolicyManager::~CicoSCPolicyManager()
143 {
144 }
145
146 //--------------------------------------------------------------------------
147 /**
148  *  @brief  initialize policy manager
149  */
150 //--------------------------------------------------------------------------
151 int
152 CicoSCPolicyManager::initialize(void)
153 {
154     ICO_DBG("CicoSCPolicyManager::initialize Enter");
155
156     int ret = ICO_SYC_EOK;
157     ret = initAMB();
158     if (ICO_SYC_EOK != ret) {
159         return ret;
160     }
161
162     ret = initStateMachine();
163     if (ICO_SYC_EOK != ret) {
164         return ret;
165     }
166
167     ICO_DBG("CicoSCPolicyManager::initialize Leave");
168     return ret;
169 }
170   
171 //--------------------------------------------------------------------------
172 /**
173  *  @brief  terminate policy manager
174  */
175 //--------------------------------------------------------------------------
176 void
177 CicoSCPolicyManager::terminate(void)
178 {
179     ICO_DBG("CicoSCPolicyManager::terminate Enter");
180     ICO_DBG("CicoSCPolicyManager::terminate Leave");
181 }
182
183 //--------------------------------------------------------------------------
184 /**
185  *  @brief  initialize amb connection
186  */
187 //--------------------------------------------------------------------------
188 int
189 CicoSCPolicyManager::initAMB(void)
190 {
191     ICO_DBG("CicoSCPolicyManager::initAMB Enter");
192
193     int ret = 0;
194     DBusError dbus_error;
195
196     if (true == m_initialized) {
197         ICO_DBG("CicoSCPolicyManager::initAMB Leave(EOK)");
198         return ICO_SYC_EOK;
199     }
200
201     /* Zero clear vhcl_data */
202     memset(vhcl_data, 0, sizeof(vhcl_data));
203
204     /* Reset D-Bus error */
205     dbus_error_init(&dbus_error);
206
207     /* Get D-Bus connection */
208     m_dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
209     if (! m_dbusConnection) {
210         ICO_ERR("dbus_bus_get failed.");
211         ICO_ERR("CicoSCPolicyManager::initAMB Leave(EIO)");
212         return ICO_SYC_EIO;
213     }
214
215     /* send request to AMB */
216     sendAMBRequest();
217
218     /* recv response from AMB timer start */
219     ret = ecore_init();
220     if (ret == 0) {
221         ICO_ERR("ecore_init");
222         ICO_ERR("CicoSCPolicyManager::initAMB Leave(ENOSYS)");
223         return ICO_SYC_ENOSYS;
224     }
225
226     m_ecoreTimer = ecore_timer_add(0.2, //TODO
227                                    CicoSCPolicyManager::ecoreTimerCB, this);
228     if (! m_ecoreTimer)    {
229         ICO_ERR("ecore_timer_add failed.");
230         ICO_ERR("CicoSCPolicyManager::initAMB Leave(ENOSYS)");
231         return ICO_SYC_ENOSYS;
232     }
233
234     m_initialized = true;
235
236     ICO_DBG("CicoSCPolicyManager::initAMB Leave(EOK)");
237     return ICO_SYC_EOK;
238 }
239
240 //--------------------------------------------------------------------------
241 /**
242  *  @brief  send AMB request
243  */
244 //--------------------------------------------------------------------------
245 int
246 CicoSCPolicyManager::sendAMBRequest(void)
247 {
248 //    ICO_DBG("CicoSCPolicyManager::sendAMBRequest Enter");
249
250     DBusMessage *dbus_message = NULL;
251     int     idx;
252     int     ret = ICO_SYC_EOK;
253
254     for (idx = 0; apf_vhcl_info[idx].key; idx++) {
255         /* set vehicle info key */
256         vhcl_data[idx].key = apf_vhcl_info[idx].key;
257
258         /* status is pending ? */
259         if (vhcl_data[idx].pending)  {
260             ICO_WRN("(%s) not complete", apf_vhcl_info[idx].property);
261             continue;
262         }
263
264         if (apf_vhcl_info[idx].path[0] == 0) {
265             /* currently not support this vehicle information */
266             continue;
267         }
268
269         /* Create send message */
270         dbus_message = dbus_message_new_method_call(DBUS_SERVICE,
271                                                     apf_vhcl_info[idx].path,
272                                                     DBUS_INTERFACE,
273                                                     DBUS_METHOD);
274         if (! dbus_message) {
275             ICO_ERR("dbus_message_new_method_call");
276             ret = ICO_SYC_EIO;
277         }
278         /* Set parameters into message */
279         else if (! dbus_message_append_args(dbus_message,
280                                             DBUS_TYPE_STRING,
281                                             &apf_vhcl_info[idx].interface,
282                                             DBUS_TYPE_STRING,
283                                             &apf_vhcl_info[idx].property,
284                                             DBUS_TYPE_INVALID)) {
285             ICO_ERR("dbus_message_append_args");
286             ret = ICO_SYC_EIO;
287         }
288         /* Set destination */
289         else if (! dbus_message_set_destination(dbus_message, 
290                                                 DBUS_SERVICE)) {
291             ICO_ERR("dbus_message_set_destination");
292             ret = ICO_SYC_EIO;
293         }
294         /* Send message */
295         else if (! dbus_connection_send_with_reply(m_dbusConnection, 
296                                                     dbus_message,
297                                                     &vhcl_data[idx].pending,
298                                                     200)) {
299             ICO_ERR("dbus_connection_send");
300             vhcl_data[idx].pending = NULL;
301             ret = ICO_SYC_EIO;
302         }
303         else {
304             //ICO_DBG("REQUEST req (%s)", apf_vhcl_info[idx].property);
305         }
306
307         if (dbus_message) {
308             /* Release message */
309             dbus_message_unref(dbus_message);
310         }
311     }
312
313     /* dispatch if data queue exist */
314     do  {
315         dbus_connection_read_write_dispatch(m_dbusConnection, 0);
316     } while (dbus_connection_get_dispatch_status(m_dbusConnection)
317              == DBUS_DISPATCH_DATA_REMAINS);
318
319     //ICO_DBG("CicoSCPolicyManager::sendAMBRequest Leave");
320     return ret;
321 }
322
323 //--------------------------------------------------------------------------
324 /**
325  *  @brief  get vehicle information
326  */
327 //--------------------------------------------------------------------------
328 int
329 CicoSCPolicyManager::getVehicleInfo(void)
330 {
331     DBusMessage *dbus_message = NULL;
332     DBusMessageIter iter_head;
333     DBusMessageIter iter;
334     int         idx;
335     char        type;
336     int32_t     i32;
337     int16_t     i16;
338     uint32_t    u32;
339     uint16_t    u16;
340     dbus_bool_t b;
341     uint8_t     u8;
342     double      d64;
343
344     /* dispatch if data queue exist */
345     do {
346         dbus_connection_read_write_dispatch(m_dbusConnection, 0);
347     } while (dbus_connection_get_dispatch_status(m_dbusConnection)
348              == DBUS_DISPATCH_DATA_REMAINS);
349
350     /* analyze reply data */
351     for (idx = 0; apf_vhcl_info[idx].key; idx++) {
352         if (! vhcl_data[idx].pending)    {
353             continue;
354         }
355         if (! dbus_pending_call_get_completed(vhcl_data[idx].pending))   {
356             //ICO_WRN("(%s) NOT complete", apf_vhcl_info[idx].property);
357             continue;
358         }
359
360         dbus_message = dbus_pending_call_steal_reply(vhcl_data[idx].pending);
361         if (! dbus_message) {
362             //ICO_WRN("(%s) NO reply", apf_vhcl_info[idx].property);
363             continue;
364         }
365
366         if (dbus_message_get_type(dbus_message) == DBUS_MESSAGE_TYPE_ERROR) {
367             dbus_message_unref(dbus_message);
368             dbus_pending_call_cancel(vhcl_data[idx].pending);
369             vhcl_data[idx].pending = NULL;
370             //ICO_ERR("(%s) reply error", apf_vhcl_info[idx].property);
371             continue;
372         }
373
374         dbus_message_iter_init(dbus_message, &iter_head);
375         dbus_message_iter_recurse(&iter_head, &iter);
376
377         type = dbus_message_iter_get_arg_type(&iter);
378         switch (type)   {
379         case DBUS_TYPE_INT32:
380             dbus_message_iter_get_basic(&iter, &i32);
381             vhcl_data[idx].val = (double)i32;
382             break;
383         case DBUS_TYPE_INT16:
384             dbus_message_iter_get_basic(&iter, &i16);
385             vhcl_data[idx].val = (double)i16;
386             break;
387         case DBUS_TYPE_UINT32:
388             dbus_message_iter_get_basic(&iter, &u32);
389             break;
390         case DBUS_TYPE_UINT16:
391             dbus_message_iter_get_basic(&iter, &u16);
392             vhcl_data[idx].val = (double)u16;
393             break;
394         case DBUS_TYPE_BOOLEAN:
395             dbus_message_iter_get_basic(&iter, &b);
396             if (b)      vhcl_data[idx].val = (double)1.0;
397             else        vhcl_data[idx].val = (double)0.0;
398             break;
399         case DBUS_TYPE_BYTE:
400             dbus_message_iter_get_basic(&iter, &u8);
401             vhcl_data[idx].val = (double)u8;
402             break;
403         case DBUS_TYPE_DOUBLE:
404             dbus_message_iter_get_basic(&iter, &d64);
405             vhcl_data[idx].val = (double)d64;
406             break;
407         default:
408             ICO_ERR("(%s) illegal data type(0x%02x)",
409                     apf_vhcl_info[idx].property, ((int)type) & 0x0ff);
410             break;
411         }
412         //ICO_DBG("REQUEST ans (%s) = %.2f",
413         //        apf_vhcl_info[idx].property, vhcl_data[idx].val);
414
415         /* free message and pending */
416         dbus_message_unref(dbus_message);
417         dbus_pending_call_cancel(vhcl_data[idx].pending);
418         vhcl_data[idx].pending = NULL;
419     };
420
421     return ICO_SYC_EOK;
422 }
423
424 //--------------------------------------------------------------------------
425 /**
426  *  @brief  ecore timer callback
427  */
428 //--------------------------------------------------------------------------
429 Eina_Bool
430 CicoSCPolicyManager::ecoreTimerCB(void *user_data)
431 {
432     CicoSCPolicyManager *policyMgr =
433         static_cast<CicoSCPolicyManager*>(user_data);
434     policyMgr->recvAMBVehicleInfo();
435
436     return ECORE_CALLBACK_RENEW;
437 }
438
439 //--------------------------------------------------------------------------
440 /**
441  *  @brief  receive AMB vehicle information
442  */
443 //--------------------------------------------------------------------------
444 void
445 CicoSCPolicyManager::recvAMBVehicleInfo(void)
446 {
447 //    ICO_DBG("CicoSCPolicyManager::recvAMBVehicleInfo Enter");
448
449     int idx = 0;
450     int key = 0;
451     bool chgCamera     = false;
452     bool chgRegulation = false;
453     bool chgNightMode  = false;
454
455     getVehicleInfo();
456
457     /* get vehicle info values  */
458     for (idx = 0; idx < AMB_MAX_VHCLINFO; idx++)   {
459         if (vhcl_data[idx].key == 0) break;
460         key = vhcl_data[idx].key;
461         switch (key) {
462         case AMB_VHCL_SPEED:
463             (void)sendSMEvent(EVID_VELOCTY, (int)vhcl_data[idx].val);
464             if (true == sendSMEvent(EVID_DRVREGULATION)) {
465                 chgRegulation = true;
466             }
467             break;
468         case AMB_SHIFT_POSITION:
469             (void)sendSMEvent(EVID_SHIFTPOS, (int)vhcl_data[idx].val);
470             if (true == sendSMEvent(EVID_CAMERA)) {
471                 chgCamera = true;
472             }
473             break;
474         case AMB_LIGHT_LEFT:
475             if (0.0 == vhcl_data[idx].val) {
476                 (void)sendSMEvent(EVID_TURN_OFF);
477             }
478             else {
479                 (void)sendSMEvent(EVID_TURN_LEFT);
480             }
481             if (true == sendSMEvent(EVID_CAMERA)) {
482                 chgCamera = true;
483             }
484             break;
485         case AMB_LIGHT_RIGHT:
486             if (0.0 == vhcl_data[idx].val) {
487                 (void)sendSMEvent(EVID_TURN_OFF);
488             }
489             else {
490                 (void)sendSMEvent(EVID_TURN_LEFT);
491             }
492             if (true == sendSMEvent(EVID_CAMERA)) {
493                 chgCamera = true;
494             }
495             break;
496         case AMB_NIGHT_MODE:
497             chgNightMode = sendSMEvent(EVID_NIGHTMODE, (int)vhcl_data[idx].val);
498             break;
499         default:
500             ICO_WRN("not such key (%d)", key);
501             break;
502         }
503     }
504
505     if (true == chgRegulation) {
506         ICO_DBG("true == chgRegulation");
507         // notify changed state to resource manager
508         CicoState* state =
509                 (CicoState*)m_stateMachine->getState(STID_DRVREGULATION);
510         if (NULL != state) {
511             vector<const CicoState*> currents;
512             state->getCurrentState(currents, CicoStateCore::ELvlTop);
513             if (0 != currents.size()) {
514                 ICO_DBG("current=%s", currents[0]->getName().c_str());
515                 notifyChangedState(currents[0]->getValue());
516             }
517         }
518
519         // Notify regulation changed state
520         CicoSCMessage *message = new CicoSCMessage();
521         message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
522         message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_REGULATION);
523         if (true == m_policyStates[STID_DRVREGULATION_ON]->isActive()) {
524             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
525         }
526         else {
527             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
528         }
529         CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
530     }
531
532     if (true == chgCamera) {
533         ICO_DBG("true == chgCamera");
534         // notify changed state to resource manager
535         CicoState* state = (CicoState*)m_stateMachine->getState(STID_CAMERA);
536         if (NULL != state) {
537             vector<const CicoState*> currents;
538             state->getCurrentState(currents, CicoStateCore::ELvlTop);
539             if (0 != currents.size()) {
540                 ICO_DBG("current=%s", currents[0]->getName().c_str());
541                 notifyChangedState(currents[0]->getValue());
542             }
543         }
544     }
545
546     if (true == chgNightMode) {
547         ICO_DBG("true == chgNightMode");
548         // Notify NightMode changed state
549         CicoSCMessage *message = new CicoSCMessage();
550         message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
551         message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_NIGHTMODE);
552         if (true == m_policyStates[STID_NIGHTMODE_ON]->isActive()) {
553             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
554         }
555         else {
556             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
557         }
558         CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
559     }
560         
561     /* send request to AMB */
562     sendAMBRequest();
563
564 //    ICO_DBG("CicoSCPolicyManager::recvAMBVehicleInfo Leave");
565 }
566
567 //--------------------------------------------------------------------------
568 /**
569  *  @brief  get policy states
570  */
571 //--------------------------------------------------------------------------
572 const std::map<int, const CicoState*>&
573 CicoSCPolicyManager::getPolicyStates(void)
574 {
575     return m_policyStates;
576 }
577
578 //--------------------------------------------------------------------------
579 /**
580  *  @brief  initialize state machine
581  */
582 //--------------------------------------------------------------------------
583 int
584 CicoSCPolicyManager::initStateMachine(void)
585 {
586     ICO_DBG("CicoSCPolicyManager::initStateMachine Enter");
587
588     CicoStateMachineCreator creator;
589
590     //TODO
591     m_stateMachine = creator.createFile("/usr/apps/org.tizen.ico.system-controller/res/config/policy.json");
592     if (NULL == m_stateMachine) {
593         ICO_ERR("CicoStateMachineCreator::createFile failed.(reason:%s)",
594                 creator.getError().c_str());
595         return ICO_SYC_ENOSYS;
596     }
597
598     int ret = m_stateMachine->start();
599     if (ret == 0) {
600         ICO_ERR("CicoStateMachine::start failed.");
601         return ICO_SYC_ENOSYS;
602     }
603
604     vector<CicoStateCore*> objects;
605     m_stateMachine->getObjects(objects);
606     vector<CicoStateCore*>::iterator itr;
607     itr = objects.begin();
608     for (; itr != objects.end(); ++itr) {
609         const CicoState* state = static_cast<const CicoState*>(*itr);
610         m_policyStates[state->getValue()] = state;
611 #if 1   //-- { debug dump
612         ICO_DBG("State=[%-45s] Active=%s",
613                 state->getName().c_str(),
614                 state->isActive() ? "true" : "false");
615 #endif  //-- } debug dump
616     }
617
618     m_dispZoneStates.push_back(NULL);
619     m_dispZoneStates.push_back(m_policyStates[STID_DISPLAY0_ZONE1]);
620     m_dispZoneStates.push_back(m_policyStates[STID_DISPLAY0_ZONE2]);
621     m_dispZoneStates.push_back(m_policyStates[STID_DISPLAY0_ZONE3]);
622
623     m_soundZoneStates.push_back(NULL);
624     m_soundZoneStates.push_back(m_policyStates[STID_SOUND_ZONE1]);
625     m_soundZoneStates.push_back(m_policyStates[STID_SOUND_ZONE2]);
626     m_soundZoneStates.push_back(m_policyStates[STID_SOUND_ZONE3]);
627
628     m_inputStates.push_back(NULL);
629     m_inputStates.push_back(m_policyStates[STID_INPUT1_USING]);
630     m_inputStates.push_back(m_policyStates[STID_INPUT2_USING]);
631
632     ICO_DBG("CicoSCPolicyManager::initStateMachine Leave");
633     return ICO_SYC_EOK;
634 }
635
636 //--------------------------------------------------------------------------
637 /**
638  *  @brief  query whether a state transition
639  *
640  *  @param [in] event_id    trigger event id
641  *
642  *  @return true on test success, false on test failed
643  */
644 //--------------------------------------------------------------------------
645 bool
646 CicoSCPolicyManager::testSMEvent(unsigned short event_id)
647 {
648     CicoEvent event(event_id);
649     return m_stateMachine->eventTest(event);
650 }
651
652 //--------------------------------------------------------------------------
653 /**
654  *  @brief  query whether a state transition
655  *
656  *  @param [in] event_id    trigger event id
657  *  @param [in] value       trigger optional integer value
658  *
659  *  @return true on test success, false on test failed
660  */
661 //--------------------------------------------------------------------------
662 bool
663 CicoSCPolicyManager::testSMEvent(unsigned short event_id, int value)
664 {
665     CicoEvent event(event_id, value);
666     return m_stateMachine->eventTest(event);
667 }
668
669 //--------------------------------------------------------------------------
670 /**
671  *  @brief  send tigger event
672  *
673  *  @param [in] event_id    trigger event id
674  *
675  *  @return true on state transition, false on not state transition
676  */
677 //--------------------------------------------------------------------------
678 bool
679 CicoSCPolicyManager::sendSMEvent(unsigned short event_id)
680 {
681     CicoEvent event(event_id);
682     return m_stateMachine->eventEntry(event);
683 }
684
685 //--------------------------------------------------------------------------
686 /**
687  *  @brief  send tigger event
688  *
689  *  @param [in] event_id    trigger event id
690  *  @param [in] value       trigger optional integer value
691  *
692  *  @return true on state transition, false on not state transition
693  */
694 //--------------------------------------------------------------------------
695 bool
696 CicoSCPolicyManager::sendSMEvent(unsigned short event_id, int value)
697 {
698     CicoEvent event(event_id, value);
699     return m_stateMachine->eventEntry(event);
700 }
701
702 bool
703 CicoSCPolicyManager::acquireDisplayResource(int type, int zoneid, int priority)
704 {
705     ICO_DBG("CicoSCPolicyManager::acquireDisplayResource Enter"
706             "(type=0x%08X zoneid=%d priority=%d)", type, zoneid, priority);
707
708     bool chg = false;
709
710     if (RESID_TYPE_BASIC == type) {
711         bool zoneChg = testSMEvent(EVID_DISPLAY0_ZONE, zoneid);
712         bool cateChg = testSMEvent(EVID_DISPLAY0_CATEGORY, priority);
713         ICO_DBG("zoneChg=%d cateChg=%d", zoneChg, cateChg);
714         if ((true == zoneChg) && (true == cateChg)) {
715             sendSMEvent(EVID_DISPLAY0_ZONE, zoneid);
716             sendSMEvent(EVID_DISPLAY0_CATEGORY, priority);
717             chg = true;
718         }
719     }
720     else if (RESID_TYPE_INTERRUPT == type) {
721         if (1 == zoneid) {
722             chg = sendSMEvent(EVID_INTTERPUT_D0_Z1, priority);
723         }
724         else if (2 == zoneid) {
725             chg = sendSMEvent(EVID_INTTERPUT_D0_Z2, priority);
726         }
727         else if (3 == zoneid) {
728             chg = sendSMEvent(EVID_INTTERPUT_D0_Z3, priority);
729         }
730     }
731     else if (RESID_TYPE_ONSCREEN == type) {
732         chg = sendSMEvent(EVID_ONSCREEN, priority);
733     }
734     ICO_DBG("CicoSCPolicyManager::acquireDisplayResource Leave(%s)",
735             chg ? "true" : "false");
736     return chg;
737 }
738
739 bool
740 CicoSCPolicyManager::releaseDisplayResource(int zoneid, int priority)
741 {
742     return true;
743 }
744
745 bool
746 CicoSCPolicyManager::acquireSoundResource(int type, int zoneid, int priority)
747 {
748     ICO_DBG("CicoSCPolicyManager::acquireSoundResource Enter"
749             "(type=0x%08X zoneid=%d priority=%d)", type, zoneid, priority);
750
751     bool chg = false;
752
753     if (RESID_TYPE_BASIC == type) {
754         bool zoneChg = testSMEvent(EVID_SOUND_ZONE, zoneid);
755         bool cateChg = testSMEvent(EVID_SOUND_CATEGORY, priority);
756         ICO_DBG("zoneChg=%d cateChg=%d", zoneChg, cateChg);
757         if ((true == zoneChg) && (true == cateChg)) {
758             sendSMEvent(EVID_SOUND_ZONE, zoneid);
759             sendSMEvent(EVID_SOUND_CATEGORY, priority);
760             chg = true;
761         }
762     }
763     else if (RESID_TYPE_INTERRUPT == type) {
764         if (1 == zoneid) {
765             chg = sendSMEvent(EVID_INTTERPUT_S_Z1, priority);
766         }
767         else if (2 == zoneid) {
768             chg = sendSMEvent(EVID_INTTERPUT_S_Z2, priority);
769         }
770         else if (3 == zoneid) {
771             chg = sendSMEvent(EVID_INTTERPUT_S_Z3, priority);
772         }
773     }
774
775     ICO_DBG("CicoSCPolicyManager::acquireSoundResource Leave(%s)",
776             chg ? "true" : "false");
777     return chg;
778 }
779
780 bool
781 CicoSCPolicyManager::releaseSoundResource(int type, int zoneid)
782 {
783     ICO_DBG("CicoSCPolicyManager::acquireSoundResource Enter"
784             "(type=%d zoneid=%d)", type, zoneid);
785
786     bool chg = false;
787     if (RESID_TYPE_BASIC == type) {
788         chg = sendSMEvent(EVID_SOUND_ZONE_NOUSE);
789         chg = sendSMEvent(EVID_SOUND_CATEGORY_UNKNOWN);
790     }
791     else if (RESID_TYPE_INTERRUPT == type) {
792         if (1 == zoneid) {
793             chg = sendSMEvent(EVID_INTTERPUT_S_Z1_NOOUTPUT);
794         }
795         else if (2 == zoneid) {
796             chg = sendSMEvent(EVID_INTTERPUT_S_Z2_NOOUTPUT);
797         }
798         else if (3 == zoneid) {
799             chg = sendSMEvent(EVID_INTTERPUT_S_Z3_NOOUTPUT);
800         }
801     }
802
803     ICO_DBG("CicoSCPolicyManager::acquireSoundResource Leave(%s)",
804             chg ? "true" : "false");
805
806     return true;
807 }
808
809 bool
810 CicoSCPolicyManager::acquireInputResource(int input, int priority)
811 {
812     ICO_DBG("CicoSCPolicyManager::acquireInputResource Enter"
813             "input=%d priority=%d", input, priority);
814
815     bool chg = false;
816
817     if (1 == input) {
818         chg = sendSMEvent(EVID_INPUT1_ACQUIRE, input);
819     }
820     else if (2 == input) {
821         chg = sendSMEvent(EVID_INPUT2_ACQUIRE, input);
822     }
823
824     ICO_DBG("CicoSCPolicyManager::acquireInputResource Leave(%s)",
825             chg ? "true" : "false");
826     return chg;
827 }
828
829 bool
830 CicoSCPolicyManager::releaseInputResource(int input)
831 {
832     if (1 == input) {
833         (void)sendSMEvent(EVID_INPUT1_RELEASE, input);
834     }
835     else if (2 == input) {
836         (void)sendSMEvent(EVID_INPUT2_RELEASE, input);
837     }
838     return true;
839 }
840
841 void
842 CicoSCPolicyManager::notifyChangedState(int state)
843 {
844     m_resourceMgr->receiveChangedState(state);
845 }
846
847 bool
848 CicoSCPolicyManager::getDispZoneState(int zoneid) const
849 {
850     if ((0 < zoneid) && ((int)m_dispZoneStates.size()-1 > zoneid)) {
851         return m_dispZoneStates[zoneid]->isActive();
852     }
853     return false;
854 }
855
856 bool
857 CicoSCPolicyManager::getSoundZoneState(int zoneid) const
858 {
859     if ((0 < zoneid) && ((int)m_soundZoneStates.size()-1 > zoneid)) {
860         return m_soundZoneStates[zoneid]->isActive();
861     }
862     return false;
863 }
864
865 bool
866 CicoSCPolicyManager::getInputState(int input) const
867 {
868     if ((0 < input) && ((int)m_inputStates.size()-1 > input)) {
869         return m_inputStates[input]->isActive();
870     }
871     return false;
872 }
873
874 //--------------------------------------------------------------------------
875 /**
876  *  @brief  notify connected
877  */
878 //--------------------------------------------------------------------------
879 void
880 CicoSCPolicyManager::notifyConnected(const string & appid)
881 {
882     // Notify regulation changed state
883     CicoSCMessage *message = new CicoSCMessage();
884     message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
885     message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_REGULATION);
886     if (true == m_policyStates[STID_DRVREGULATION_ON]->isActive()) {
887         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
888     }
889     else {
890         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
891     }
892     CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
893
894     // Notify NightMode changed state
895     message = new CicoSCMessage();
896     message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
897     message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_NIGHTMODE);
898     if (true == m_policyStates[STID_NIGHTMODE_ON]->isActive()) {
899         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
900     }
901     else {
902         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
903     }
904     CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
905 }
906 // vim:set expandtab ts=4 sw=4: