bug fix: TIVI-1997, TIVI-2161, An action is inaccurate when the menu of HomeScreen...
[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
20 #include <Ecore.h>
21
22 #include "CicoSCPolicyManager.h"
23 #include "CicoStateMachine.h"
24 #include "CicoState.h"
25 #include "CicoHistoryState.h"
26 #include "CicoStateMachineCreator.h"
27 #include "CicoSCPolicyDef.h"
28 #include "CicoSCResourceManager.h"
29 #include "CicoLog.h"
30 #include "ico_syc_error.h"
31 #include "CicoSCServer.h"
32 #include "CicoSCMessage.h"
33 #include "ico_syc_msg_cmd_def.h"
34 #include "ico_syc_type.h"
35 #include "CicoCommonDef.h"
36 #include "CicoSCVInfoManager.h"
37 #include "CicoSCVInfo.h"
38
39 //--------------------------------------------------------------------------
40 /**
41  *  @brief  default constructor
42  */
43 //--------------------------------------------------------------------------
44 CicoSCPolicyManager::CicoSCPolicyManager(CicoSCResourceManager* resourceMgr)
45     : m_initialized(false),
46       m_ecoreEvHandler(NULL),
47       m_stateMachine(NULL),
48       m_resourceMgr(resourceMgr)
49 {
50 }
51
52 //--------------------------------------------------------------------------
53 /**
54  *  @brief  destructor
55  */
56 //--------------------------------------------------------------------------
57 CicoSCPolicyManager::~CicoSCPolicyManager()
58 {
59 }
60
61 //--------------------------------------------------------------------------
62 /**
63  *  @brief  initialize policy manager
64  */
65 //--------------------------------------------------------------------------
66 int
67 CicoSCPolicyManager::initialize(void)
68 {
69     ICO_TRA("CicoSCPolicyManager::initialize Enter");
70
71     int ret = initStateMachine();
72     if (ICO_SYC_EOK != ret) {
73         return ret;
74     }
75
76     CicoSCVInfoManager* vinfomgr = CicoSCVInfoManager::getInstance();
77     m_ecoreEvHandler = ecore_event_handler_add(
78                             vinfomgr->getEcoreEvType(),
79                             CicoSCPolicyManager::changedVehicleInfoCB,
80                             this);
81
82     vinfomgr->getVehicleInfo(ICO_VINFO_NIGHT_MODE);
83     vinfomgr->getVehicleInfo(ICO_VINFO_DRIVING_MODE);
84
85     ICO_TRA("CicoSCPolicyManager::initialize Leave");
86     return ret;
87 }
88   
89 //--------------------------------------------------------------------------
90 /**
91  *  @brief  terminate policy manager
92  */
93 //--------------------------------------------------------------------------
94 void
95 CicoSCPolicyManager::terminate(void)
96 {
97     ICO_TRA("CicoSCPolicyManager::terminate Enter");
98     ICO_TRA("CicoSCPolicyManager::terminate Leave");
99 }
100
101 //--------------------------------------------------------------------------
102 /**
103  *  @brief
104  */
105 //--------------------------------------------------------------------------
106 void
107 CicoSCPolicyManager::onChangedVehicleInfo(int type, void *event)
108 {
109     ICO_TRA("CicoSCPolicyManager::onChangedVehicleInfo Enter");
110
111     if (NULL == event) {
112         ICO_WRN("data is null");
113         return;
114     }
115
116     if (CicoSCVInfoManager::getInstance()->getEcoreEvType() != type) {
117         ICO_WRN("unknown ecore event type(%d)", type);
118         return;
119     }
120
121     bool chgCamera     = false;
122     bool chgRegulation = false;
123     bool chgNightMode  = false;
124
125     CicoSCVInfo* vinfo = static_cast<CicoSCVInfo*>(event);
126     switch (vinfo->getProperty()) {
127     case ICO_VINFO_VEHICLE_SPEED:
128         ICO_DBG("ICO_VINFO_VEHICLE_SPEED : %d", vinfo->getInt32Value());
129 //        (void)sendSMEvent(EVID_VELOCTY, (int)vinfo->getInt32Value());
130 //        if (true == sendSMEvent(EVID_DRVREGULATION)) {
131 //            chgRegulation = true;
132 //        }
133         break;
134     case ICO_VINFO_SHIFT_POSITION:
135         ICO_DBG("ICO_VINFO_SHIFT_POSITION : %d", vinfo->getUint8Value());
136         (void)sendSMEvent(EVID_SHIFTPOS, (int)vinfo->getUint8Value());
137         if (true == sendSMEvent(EVID_CAMERA)) {
138             chgCamera = true;
139         }
140         break;
141     case ICO_VINFO_LIGHT_LEFT:
142         ICO_DBG("ICO_VINFO_LIGHT_LEFT : %s",
143                 vinfo->getBooleanValue() ? "true" : "false");
144         if (false == vinfo->getBooleanValue()) {
145             (void)sendSMEvent(EVID_TURN_OFF);
146         }
147         else {
148             (void)sendSMEvent(EVID_TURN_LEFT);
149         }
150         if (true == sendSMEvent(EVID_CAMERA)) {
151             chgCamera = true;
152         }
153         break;
154     case ICO_VINFO_LIGHT_RIGHT:
155         ICO_DBG("ICO_VINFO_LIGHT_RIGHT: %s",
156                 vinfo->getBooleanValue() ? "true" : "false");
157         if (false == vinfo->getBooleanValue()) {
158             (void)sendSMEvent(EVID_TURN_OFF);
159         }
160         else {
161             (void)sendSMEvent(EVID_TURN_RIGHT);
162         }
163         if (true == sendSMEvent(EVID_CAMERA)) {
164             chgCamera = true;
165         }
166         break;
167     case ICO_VINFO_NIGHT_MODE:
168         ICO_DBG("ICO_VINFO_NIGHT_MODE : %d", vinfo->getUint32Value());
169         chgNightMode = sendSMEvent(EVID_NIGHTMODE, (int)vinfo->getUint32Value());
170         break;
171     case ICO_VINFO_DRIVING_MODE:
172         ICO_DBG("ICO_VINFO_DRIVING_MODE : %d", vinfo->getUint32Value());
173         chgRegulation = sendSMEvent(EVID_DRVREGULATION, (int)vinfo->getUint32Value());
174         break;
175     default:
176         ICO_WRN("not such key (%d)", vinfo->getProperty());
177         break;
178     }
179
180     if (true == chgRegulation) {
181         ICO_DBG("true == chgRegulation");
182         // notify changed state to resource manager
183         CicoState* state =
184                 (CicoState*)m_stateMachine->getState(STID_DRVREGULATION);
185         if (NULL != state) {
186             vector<const CicoState*> currents;
187             state->getCurrentState(currents, CicoStateCore::ELvlTop);
188             if (0 != currents.size()) {
189                 ICO_DBG("current=%s", currents[0]->getName().c_str());
190                 notifyChangedState(currents[0]->getValue());
191             }
192         }
193
194         // Notify regulation changed state
195         CicoSCMessage *message = new CicoSCMessage();
196         message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
197         message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_REGULATION);
198         if (true == m_policyStates[STID_DRVREGULATION_ON]->isActive()) {
199             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
200         }
201         else {
202             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
203         }
204         CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
205     }
206
207     if (true == chgCamera) {
208         ICO_DBG("true == chgCamera");
209         // notify changed state to resource manager
210         CicoState* state = (CicoState*)m_stateMachine->getState(STID_CAMERA);
211         if (NULL != state) {
212             vector<const CicoState*> currents;
213             state->getCurrentState(currents, CicoStateCore::ELvlTop);
214             if (0 != currents.size()) {
215                 ICO_DBG("current=%s", currents[0]->getName().c_str());
216                 notifyChangedState(currents[0]->getValue());
217             }
218         }
219     }
220
221     if (true == chgNightMode) {
222         ICO_DBG("true == chgNightMode");
223         // Notify NightMode changed state
224         CicoSCMessage *message = new CicoSCMessage();
225         message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
226         message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_NIGHTMODE);
227         if (true == m_policyStates[STID_NIGHTMODE_ON]->isActive()) {
228             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
229         }
230         else {
231             message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
232         }
233         CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
234     }
235
236     ICO_TRA("CicoSCPolicyManager::onChangedVehicleInfo Leave");
237 }
238
239 //--------------------------------------------------------------------------
240 /**
241  *  @brief
242  */
243 //--------------------------------------------------------------------------
244 Eina_Bool
245 CicoSCPolicyManager::changedVehicleInfoCB(void *data, int type, void *event)
246 {
247     static_cast<CicoSCPolicyManager*>(data)->onChangedVehicleInfo(type, event);
248     return EINA_TRUE;
249 }
250
251 //--------------------------------------------------------------------------
252 /**
253  *  @brief  get policy states
254  */
255 //--------------------------------------------------------------------------
256 const std::map<int, const CicoState*>&
257 CicoSCPolicyManager::getPolicyStates(void)
258 {
259     return m_policyStates;
260 }
261
262 //--------------------------------------------------------------------------
263 /**
264  *  @brief  initialize state machine
265  */
266 //--------------------------------------------------------------------------
267 int
268 CicoSCPolicyManager::initStateMachine(void)
269 {
270     ICO_TRA("CicoSCPolicyManager::initStateMachine Enter");
271
272     CicoStateMachineCreator creator;
273
274     //TODO
275     m_stateMachine = creator.createFile("/usr/apps/org.tizen.ico.system-controller/res/config/policy.json");
276     if (NULL == m_stateMachine) {
277         ICO_ERR("CicoStateMachineCreator::createFile failed.(reason:%s)",
278                 creator.getError().c_str());
279         return ICO_SYC_ENOSYS;
280     }
281
282     int ret = m_stateMachine->start();
283     if (ret == 0) {
284         ICO_ERR("CicoStateMachine::start failed.");
285         ICO_TRA("CicoSCPolicyManager::initStateMachine Leave(ENOSYS)");
286         return ICO_SYC_ENOSYS;
287     }
288
289     vector<CicoStateCore*> objects;
290     m_stateMachine->getObjects(objects);
291     vector<CicoStateCore*>::iterator itr;
292     itr = objects.begin();
293     for (; itr != objects.end(); ++itr) {
294         const CicoState* state = static_cast<const CicoState*>(*itr);
295         m_policyStates[state->getValue()] = state;
296 #if 1   //-- { debug dump
297         ICO_DBG("State=[%-45s] Active=%s",
298                 state->getName().c_str(),
299                 state->isActive() ? "true" : "false");
300 #endif  //-- } debug dump
301     }
302
303     {
304         std::map<int, const CicoState*>::iterator itr;
305         itr = m_policyStates.find(STID_DISPLAY0_ZONE1);
306         m_dispZoneStates[1] = itr != m_policyStates.end() ? itr->second : NULL;
307         itr = m_policyStates.find(STID_DISPLAY0_ZONE2);
308         m_dispZoneStates[2] = itr != m_policyStates.end() ? itr->second : NULL;
309         itr = m_policyStates.find(STID_DISPLAY0_ZONE3);
310         m_dispZoneStates[3] = itr != m_policyStates.end() ? itr->second : NULL;
311         itr = m_policyStates.find(STID_DISPLAY0_ZONE4);
312         m_dispZoneStates[4] = itr != m_policyStates.end() ? itr->second : NULL;
313         itr = m_policyStates.find(STID_DISPLAY0_ZONE5);
314         m_dispZoneStates[5] = itr != m_policyStates.end() ? itr->second : NULL;
315         itr = m_policyStates.find(STID_DISPLAY0_ZONE6);
316         m_dispZoneStates[6] = itr != m_policyStates.end() ? itr->second : NULL;
317         itr = m_policyStates.find(STID_DISPLAY0_ZONE7);
318         m_dispZoneStates[7] = itr != m_policyStates.end() ? itr->second : NULL;
319         itr = m_policyStates.find(STID_DISPLAY0_ZONE8);
320         m_dispZoneStates[8] = itr != m_policyStates.end() ? itr->second : NULL;
321         itr = m_policyStates.find(STID_DISPLAY0_ZONE9);
322         m_dispZoneStates[9] = itr != m_policyStates.end() ? itr->second : NULL;
323         itr = m_policyStates.find(STID_DISPLAY0_ZONE10);
324         m_dispZoneStates[10] = itr != m_policyStates.end() ? itr->second : NULL;
325         itr = m_policyStates.find(STID_DISPLAY0_ZONE11);
326         m_dispZoneStates[11] = itr != m_policyStates.end() ? itr->second : NULL;
327         itr = m_policyStates.find(STID_DISPLAY0_ZONE12);
328         m_dispZoneStates[12] = itr != m_policyStates.end() ? itr->second : NULL;
329         itr = m_policyStates.find(STID_DISPLAY0_ZONE13);
330         m_dispZoneStates[13] = itr != m_policyStates.end() ? itr->second : NULL;
331         itr = m_policyStates.find(STID_DISPLAY0_ZONE14);
332         m_dispZoneStates[14] = itr != m_policyStates.end() ? itr->second : NULL;
333         itr = m_policyStates.find(STID_DISPLAY0_ZONE15);
334         m_dispZoneStates[15] = itr != m_policyStates.end() ? itr->second : NULL;
335         itr = m_policyStates.find(STID_DISPLAY0_ZONE16);
336         m_dispZoneStates[16] = itr != m_policyStates.end() ? itr->second : NULL;
337         itr = m_policyStates.find(STID_DISPLAY0_ZONE17);
338         m_dispZoneStates[17] = itr != m_policyStates.end() ? itr->second : NULL;
339         itr = m_policyStates.find(STID_DISPLAY0_ZONE18);
340         m_dispZoneStates[18] = itr != m_policyStates.end() ? itr->second : NULL;
341         itr = m_policyStates.find(STID_DISPLAY0_ZONE19);
342         m_dispZoneStates[19] = itr != m_policyStates.end() ? itr->second : NULL;
343         itr = m_policyStates.find(STID_DISPLAY0_ZONE20);
344         m_dispZoneStates[20] = itr != m_policyStates.end() ? itr->second : NULL;
345
346         itr = m_policyStates.find(STID_DISPLAY1_ZONE1);
347         m_dispZoneStates[21] = itr != m_policyStates.end() ? itr->second : NULL;
348         itr = m_policyStates.find(STID_DISPLAY1_ZONE2);
349         m_dispZoneStates[22] = itr != m_policyStates.end() ? itr->second : NULL;
350         itr = m_policyStates.find(STID_DISPLAY1_ZONE3);
351         m_dispZoneStates[23] = itr != m_policyStates.end() ? itr->second : NULL;
352         itr = m_policyStates.find(STID_DISPLAY1_ZONE4);
353         m_dispZoneStates[24] = itr != m_policyStates.end() ? itr->second : NULL;
354         itr = m_policyStates.find(STID_DISPLAY1_ZONE5);
355         m_dispZoneStates[25] = itr != m_policyStates.end() ? itr->second : NULL;
356         itr = m_policyStates.find(STID_DISPLAY1_ZONE6);
357         m_dispZoneStates[26] = itr != m_policyStates.end() ? itr->second : NULL;
358         itr = m_policyStates.find(STID_DISPLAY1_ZONE7);
359         m_dispZoneStates[27] = itr != m_policyStates.end() ? itr->second : NULL;
360         itr = m_policyStates.find(STID_DISPLAY1_ZONE8);
361         m_dispZoneStates[28] = itr != m_policyStates.end() ? itr->second : NULL;
362         itr = m_policyStates.find(STID_DISPLAY1_ZONE9);
363         m_dispZoneStates[29] = itr != m_policyStates.end() ? itr->second : NULL;
364         itr = m_policyStates.find(STID_DISPLAY1_ZONE10);
365         m_dispZoneStates[30] = itr != m_policyStates.end() ? itr->second : NULL;
366         itr = m_policyStates.find(STID_DISPLAY1_ZONE11);
367         m_dispZoneStates[31] = itr != m_policyStates.end() ? itr->second : NULL;
368         itr = m_policyStates.find(STID_DISPLAY1_ZONE12);
369         m_dispZoneStates[32] = itr != m_policyStates.end() ? itr->second : NULL;
370         itr = m_policyStates.find(STID_DISPLAY1_ZONE13);
371         m_dispZoneStates[33] = itr != m_policyStates.end() ? itr->second : NULL;
372         itr = m_policyStates.find(STID_DISPLAY1_ZONE14);
373         m_dispZoneStates[34] = itr != m_policyStates.end() ? itr->second : NULL;
374         itr = m_policyStates.find(STID_DISPLAY1_ZONE15);
375         m_dispZoneStates[35] = itr != m_policyStates.end() ? itr->second : NULL;
376         itr = m_policyStates.find(STID_DISPLAY1_ZONE16);
377         m_dispZoneStates[36] = itr != m_policyStates.end() ? itr->second : NULL;
378         itr = m_policyStates.find(STID_DISPLAY1_ZONE17);
379         m_dispZoneStates[37] = itr != m_policyStates.end() ? itr->second : NULL;
380         itr = m_policyStates.find(STID_DISPLAY1_ZONE18);
381         m_dispZoneStates[38] = itr != m_policyStates.end() ? itr->second : NULL;
382         itr = m_policyStates.find(STID_DISPLAY1_ZONE19);
383         m_dispZoneStates[39] = itr != m_policyStates.end() ? itr->second : NULL;
384         itr = m_policyStates.find(STID_DISPLAY1_ZONE20);
385         m_dispZoneStates[40] = itr != m_policyStates.end() ? itr->second : NULL;
386     }
387
388     m_soundZoneStates.push_back(NULL);
389     m_soundZoneStates.push_back(m_policyStates[STID_SOUND_ZONE1]);
390     m_soundZoneStates.push_back(m_policyStates[STID_SOUND_ZONE2]);
391     m_soundZoneStates.push_back(m_policyStates[STID_SOUND_ZONE3]);
392
393     m_inputStates.push_back(NULL);
394     m_inputStates.push_back(m_policyStates[STID_INPUT1_USING]);
395     m_inputStates.push_back(m_policyStates[STID_INPUT2_USING]);
396
397     ICO_TRA("CicoSCPolicyManager::initStateMachine Leave(EOK)");
398     return ICO_SYC_EOK;
399 }
400
401 //--------------------------------------------------------------------------
402 /**
403  *  @brief  query whether a state transition
404  *
405  *  @param [in] event_id    trigger event id
406  *
407  *  @return true on test success, false on test failed
408  */
409 //--------------------------------------------------------------------------
410 bool
411 CicoSCPolicyManager::testSMEvent(unsigned short event_id)
412 {
413     CicoEvent event(event_id);
414     return m_stateMachine->eventTest(event);
415 }
416
417 //--------------------------------------------------------------------------
418 /**
419  *  @brief  query whether a state transition
420  *
421  *  @param [in] event_id    trigger event id
422  *  @param [in] value       trigger optional integer value
423  *
424  *  @return true on test success, false on test failed
425  */
426 //--------------------------------------------------------------------------
427 bool
428 CicoSCPolicyManager::testSMEvent(unsigned short event_id, int value)
429 {
430     CicoEvent event(event_id, value);
431     return m_stateMachine->eventTest(event);
432 }
433
434 //--------------------------------------------------------------------------
435 /**
436  *  @brief  send tigger event
437  *
438  *  @param [in] event_id    trigger event id
439  *
440  *  @return true on state transition, false on not state transition
441  */
442 //--------------------------------------------------------------------------
443 bool
444 CicoSCPolicyManager::sendSMEvent(unsigned short event_id)
445 {
446     CicoEvent event(event_id);
447     return m_stateMachine->eventEntry(event);
448 }
449
450 //--------------------------------------------------------------------------
451 /**
452  *  @brief  send tigger event
453  *
454  *  @param [in] event_id    trigger event id
455  *  @param [in] value       trigger optional integer value
456  *
457  *  @return true on state transition, false on not state transition
458  */
459 //--------------------------------------------------------------------------
460 bool
461 CicoSCPolicyManager::sendSMEvent(unsigned short event_id, int value)
462 {
463     CicoEvent event(event_id, value);
464     return m_stateMachine->eventEntry(event);
465 }
466
467 bool
468 CicoSCPolicyManager::acquireDisplayResource(int type, int zoneid, int priority)
469 {
470     ICO_TRA("CicoSCPolicyManager::acquireDisplayResource Enter"
471             "(type=0x%08X zoneid=%d priority=%d)", type, zoneid, priority);
472
473     bool chg = false;
474
475     if (RESID_TYPE_BASIC == type) {
476 #if 1  // MK20131223 _MK_MK_
477         unsigned short category_ev = EVID_DISPLAY0_CATEGORY;
478         if (ZONEID_CENTER_UPPER == zoneid) {
479             category_ev = EVID_DISP0_ZONE2_CATEGORY;
480             
481         }
482         else if (ZONEID_CENTER_LOWER == zoneid) {
483             category_ev = EVID_DISP0_ZONE3_CATEGORY;
484         }
485
486         bool zoneChg = testSMEvent(EVID_DISPLAY_ZONE_ACQUIRE, zoneid);
487         bool cateChg = testSMEvent(category_ev, priority);
488         ICO_DBG("zoneChg=%d cateChg=%d", zoneChg, cateChg);
489         if ((true == zoneChg) && (true == cateChg)) {
490             sendSMEvent(EVID_DISPLAY_ZONE_ACQUIRE, zoneid);
491             sendSMEvent(category_ev, priority);
492             chg = true;
493         }
494 #else  // MK20131223 _MK_MK_
495         bool zoneChg = testSMEvent(EVID_DISPLAY_ZONE_ACQUIRE, zoneid);
496         bool cateChg = testSMEvent(EVID_DISPLAY0_CATEGORY, priority);
497         ICO_DBG("zoneChg=%d cateChg=%d", zoneChg, cateChg);
498         if ((true == zoneChg) && (true == cateChg)) {
499             sendSMEvent(EVID_DISPLAY_ZONE_ACQUIRE, zoneid);
500             sendSMEvent(EVID_DISPLAY0_CATEGORY, priority);
501             chg = true;
502         }
503 #endif  // MK20131223 _MK_MK_
504 #if 0   //-- { debug dump
505         else {
506             std::map<int, const CicoState*>::iterator itr;
507             itr = m_policyStates.begin();
508             for (; itr != m_policyStates.end(); ++itr) {
509                 ICO_DBG("State=[%-45s] Active=%s",
510                         itr->second->getName().c_str(),
511                         itr->second->isActive() ? "true" : "false");
512             }
513         }
514 #endif  //-- } debug dump
515     }
516     else if (RESID_TYPE_INTERRUPT == type) {
517         if (1 == zoneid) {
518             chg = sendSMEvent(EVID_INTTERPUT_D0_Z1, priority);
519         }
520         else if (2 == zoneid) {
521             chg = sendSMEvent(EVID_INTTERPUT_D0_Z2, priority);
522         }
523         else if (3 == zoneid) {
524             chg = sendSMEvent(EVID_INTTERPUT_D0_Z3, priority);
525         }
526     }
527     else if (RESID_TYPE_ONSCREEN == type) {
528         chg = sendSMEvent(EVID_ONSCREEN, priority);
529     }
530     ICO_TRA("CicoSCPolicyManager::acquireDisplayResource Leave(%s)",
531             chg ? "true" : "false");
532     return chg;
533 }
534
535 bool
536 CicoSCPolicyManager::releaseDisplayResource(int zoneid, int priority)
537 {
538     return sendSMEvent(EVID_DISPLAY_ZONE_RELEASE, zoneid);
539 }
540
541 bool
542 CicoSCPolicyManager::acquireSoundResource(int type, int zoneid, int priority)
543 {
544     ICO_TRA("CicoSCPolicyManager::acquireSoundResource Enter"
545             "(type=0x%08X zoneid=%d priority=%d)", type, zoneid, priority);
546
547     bool chg = false;
548
549     if (RESID_TYPE_BASIC == type) {
550         bool zoneChg = testSMEvent(EVID_SOUND_ZONE, zoneid);
551         bool cateChg = testSMEvent(EVID_SOUND_CATEGORY, priority);
552         ICO_DBG("zoneChg=%d cateChg=%d", zoneChg, cateChg);
553         if ((true == zoneChg) && (true == cateChg)) {
554             sendSMEvent(EVID_SOUND_ZONE, zoneid);
555             sendSMEvent(EVID_SOUND_CATEGORY, priority);
556             chg = true;
557         }
558     }
559     else if (RESID_TYPE_INTERRUPT == type) {
560         if (1 == zoneid) {
561             chg = sendSMEvent(EVID_INTTERPUT_S_Z1, priority);
562         }
563         else if (2 == zoneid) {
564             chg = sendSMEvent(EVID_INTTERPUT_S_Z2, priority);
565         }
566         else if (3 == zoneid) {
567             chg = sendSMEvent(EVID_INTTERPUT_S_Z3, priority);
568         }
569     }
570
571     ICO_TRA("CicoSCPolicyManager::acquireSoundResource Leave(%s)",
572             chg ? "true" : "false");
573     return chg;
574 }
575
576 bool
577 CicoSCPolicyManager::releaseSoundResource(int type, int zoneid)
578 {
579     ICO_TRA("CicoSCPolicyManager::acquireSoundResource Enter"
580             "(type=%d zoneid=%d)", type, zoneid);
581
582     bool chg = false;
583     if (RESID_TYPE_BASIC == type) {
584         chg = sendSMEvent(EVID_SOUND_ZONE_NOUSE);
585         chg = sendSMEvent(EVID_SOUND_CATEGORY_UNKNOWN);
586     }
587     else if (RESID_TYPE_INTERRUPT == type) {
588         if (1 == zoneid) {
589             chg = sendSMEvent(EVID_INTTERPUT_S_Z1_NOOUTPUT);
590         }
591         else if (2 == zoneid) {
592             chg = sendSMEvent(EVID_INTTERPUT_S_Z2_NOOUTPUT);
593         }
594         else if (3 == zoneid) {
595             chg = sendSMEvent(EVID_INTTERPUT_S_Z3_NOOUTPUT);
596         }
597     }
598
599     ICO_TRA("CicoSCPolicyManager::acquireSoundResource Leave(%s)",
600             chg ? "true" : "false");
601
602     return true;
603 }
604
605 bool
606 CicoSCPolicyManager::acquireInputResource(int input, int priority)
607 {
608     ICO_TRA("CicoSCPolicyManager::acquireInputResource Enter"
609             "(input=%d priority=%d)", input, priority);
610
611     bool chg = false;
612
613     if (1 == input) {
614         chg = testSMEvent(EVID_INPUT1_ACQUIRE, input);
615         if (true == chg) {
616             sendSMEvent(EVID_INPUT1_ACQUIRE, input);
617         }
618     }
619     else if (2 == input) {
620         chg = testSMEvent(EVID_INPUT2_ACQUIRE, input);
621         if (true == chg) {
622             sendSMEvent(EVID_INPUT2_ACQUIRE, input);
623         }
624     }
625
626     ICO_TRA("CicoSCPolicyManager::acquireInputResource Leave(%s)",
627             chg ? "true" : "false");
628     return chg;
629 }
630
631 bool
632 CicoSCPolicyManager::releaseInputResource(int input)
633 {
634     if (1 == input) {
635         (void)sendSMEvent(EVID_INPUT1_RELEASE, input);
636     }
637     else if (2 == input) {
638         (void)sendSMEvent(EVID_INPUT2_RELEASE, input);
639     }
640     return true;
641 }
642
643 void
644 CicoSCPolicyManager::notifyChangedState(int state)
645 {
646     m_resourceMgr->receiveChangedState(state);
647 }
648
649 bool
650 CicoSCPolicyManager::getDispZoneState(int zoneid)
651 {
652     if (0 >= zoneid) {
653         return false;
654     }
655
656     // find state instance
657     std::map<int, const CicoState*>::iterator itr;
658     itr = m_dispZoneStates.find(zoneid);
659     if (itr == m_dispZoneStates.end()) {
660         return false;
661     }
662
663     // if state instance is null
664     if (NULL == itr->second) {
665         return false;
666     }
667         
668     return itr->second->isActive();
669 }
670
671 bool
672 CicoSCPolicyManager::getSoundZoneState(int zoneid) const
673 {
674     if ((0 < zoneid) && ((int)m_soundZoneStates.size()-1 > zoneid)) {
675         return m_soundZoneStates[zoneid]->isActive();
676     }
677     return false;
678 }
679
680 bool
681 CicoSCPolicyManager::getInputState(int input) const
682 {
683     if ((0 < input) && ((int)m_inputStates.size()-1 > input)) {
684         return m_inputStates[input]->isActive();
685     }
686     return false;
687 }
688
689 bool
690 CicoSCPolicyManager::getRegulation(void)
691 {
692     return m_policyStates[STID_DRVREGULATION_ON]->isActive();
693 }
694
695 bool
696 CicoSCPolicyManager::isExistDisplayZoneOwer(int zoneid)
697 {
698     if ((zoneid >= ICO_DISPLAY0_ZONEID_MIN) &&
699         (zoneid <= ICO_DISPLAY0_ZONEID_MAX)) {
700         return !m_policyStates[STID_DISPLAY0_NOOWER]->isActive();
701     }
702     if ((zoneid >= ICO_DISPLAY1_ZONEID_MIN) &&
703         (zoneid <= ICO_DISPLAY1_ZONEID_MAX)) {
704         return !m_policyStates[STID_DISPLAY1_NOOWER]->isActive();
705     }
706     return true;
707 }
708
709 //--------------------------------------------------------------------------
710 /**
711  *  @brief  notify connected
712  */
713 //--------------------------------------------------------------------------
714 void
715 CicoSCPolicyManager::notifyConnected(const std::string & appid)
716 {
717     // Notify regulation changed state
718     CicoSCMessage *message = new CicoSCMessage();
719     message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
720     message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_REGULATION);
721     if (true == m_policyStates[STID_DRVREGULATION_ON]->isActive()) {
722         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
723     }
724     else {
725         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
726     }
727     CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
728
729     // Notify NightMode changed state
730     message = new CicoSCMessage();
731     message->addRootObject("command", MSG_CMD_NOTIFY_CHANGED_STATE);
732     message->addArgObject(MSG_PRMKEY_STATEID, ICO_SYC_STATE_NIGHTMODE);
733     if (true == m_policyStates[STID_NIGHTMODE_ON]->isActive()) {
734         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_ON);
735     }
736     else {
737         message->addArgObject(MSG_PRMKEY_STATE, ICO_SYC_STATE_OFF);
738     }
739     CicoSCServer::getInstance()->sendMessageToHomeScreen(message);
740 }
741 // vim:set expandtab ts=4 sw=4: