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