tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Power / Battery.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include <vconf.h>
17 #include <Commons/Exception.h>
18 #include <Power/EventCharging.h>
19 #include <Power/EventRemaining.h>
20 #include "Battery.h"
21
22 namespace WrtDeviceApis {
23 namespace Power {
24
25 Battery::Battery(std::size_t index) :
26     m_index(index),
27     m_onChargingKey(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW),
28     m_onRemainingKey(VCONFKEY_SYSMAN_BATTERY_LEVEL),
29     m_onLowKey(VCONFKEY_TELEPHONY_LOW_BATTERY)
30 {
31 }
32
33 Battery::~Battery()
34 {
35 }
36
37 bool Battery::isCharging() const
38 {
39     Try {
40         return m_onChargingKey.getBool();
41     }
42     Catch(Commons::ConversionException) {
43         ReThrowMsg(Commons::PlatformException,
44                    "Could not discover battery state.");
45     }
46 }
47
48 unsigned int Battery::getRemaining() const
49 {
50     Try {
51         int level = m_onRemainingKey.getInt();
52         double ratio =
53             (level / static_cast<double>(VCONFKEY_SYSMAN_BAT_LEVEL_MAX));
54         return static_cast<unsigned int>(ratio * 100);
55     }
56     Catch(Commons::ConversionException) {
57         ReThrowMsg(Commons::PlatformException,
58                    "Could not discover battery level.");
59     }
60 }
61
62 void Battery::addOnCharging(const Power::Api::EventChargingEmitterPtr& emitter)
63 {
64     DPL::Mutex::ScopedLock lock(&m_onChargingMtx);
65     ChargingEventEmitters::iterator it = m_onCharging.find(emitter->getId());
66     if (it == m_onCharging.end()) {
67         if (m_onCharging.size() == 0) {
68             if (!m_onChargingKey.attachCallback(Battery::onCharging, this)) {
69                 ThrowMsg(Commons::PlatformException,
70                          "Could not set platform listener.");
71             }
72         }
73         m_onCharging.insert(ChargingEventEmitters::value_type(emitter->getId(),
74                                                               emitter));
75     }
76 }
77
78 void Battery::removeOnCharging(Power::Api::EventChargingEmitter::IdType id)
79 {
80     DPL::Mutex::ScopedLock lock(&m_onChargingMtx);
81     ChargingEventEmitters::iterator it = m_onCharging.find(id);
82     if (it != m_onCharging.end()) {
83         m_onCharging.erase(it);
84         if (m_onCharging.size() == 0) {
85             m_onChargingKey.detachCallback();
86         }
87     }
88 }
89
90 void Battery::addOnRemaining(
91         const Api::EventRemainingEmitterPtr& emitter)
92 {
93     DPL::Mutex::ScopedLock lock(&m_onRemainingMtx);
94     RemainingEventEmitters::iterator it = m_onRemaining.find(emitter->getId());
95     if (it == m_onRemaining.end()) {
96         if (m_onRemaining.size() == 0) {
97             if (!m_onRemainingKey.attachCallback(Battery::onRemaining, this)) {
98                 ThrowMsg(Commons::PlatformException,
99                          "Could not set platform listener.");
100             }
101         }
102         m_onRemaining.insert(RemainingEventEmitters::value_type(emitter->getId(),
103                                                                 emitter));
104     }
105 }
106
107 void Battery::removeOnRemaining(Api::EventRemainingEmitter::IdType id)
108 {
109     DPL::Mutex::ScopedLock lock(&m_onRemainingMtx);
110     RemainingEventEmitters::iterator it = m_onRemaining.find(id);
111     if (it != m_onRemaining.end()) {
112         m_onRemaining.erase(it);
113         if (m_onRemaining.size() == 0) {
114             m_onRemainingKey.detachCallback();
115         }
116     }
117 }
118
119 void Battery::addOnLow(const Api::EventLowEmitterPtr& emitter)
120 {
121     DPL::Mutex::ScopedLock lock(&m_onLowMtx);
122     LowEventEmitters::iterator it = m_onLow.find(emitter->getId());
123     if (it == m_onLow.end()) {
124         if (m_onLow.size() == 0) {
125             if (!m_onLowKey.attachCallback(Battery::onLow, this)) {
126                 ThrowMsg(Commons::PlatformException,
127                          "Could not set platform listener.");
128             }
129         }
130         m_onLow.insert(LowEventEmitters::value_type(emitter->getId(), emitter));
131     }
132 }
133
134 void Battery::removeOnLow(Api::EventLowEmitter::IdType id)
135 {
136     DPL::Mutex::ScopedLock lock(&m_onLowMtx);
137     LowEventEmitters::iterator it = m_onLow.find(id);
138     if (it != m_onLow.end()) {
139         m_onLow.erase(it);
140         if (m_onLow.size() == 0) {
141             m_onLowKey.detachCallback();
142         }
143     }
144 }
145
146 void Battery::onCharging(const VConf::Node* /* node */,
147         void* data)
148 {
149     Battery* this_ = static_cast<Battery*>(data);
150     if (this_) {
151         Api::EventChargingPtr event(new Api::EventCharging());
152
153         DPL::Mutex::ScopedLock lock(&this_->m_onChargingMtx);
154         ChargingEventEmitters::iterator it = this_->m_onCharging.begin();
155         for (; it != this_->m_onCharging.end(); ++it) {
156             if (!!it->second) {
157                 it->second->emit(event);
158             }
159         }
160     }
161 }
162
163 void Battery::onRemaining(const VConf::Node* /* node */,
164         void* data)
165 {
166     Battery* this_ = static_cast<Battery*>(data);
167     if (this_) {
168         Api::EventRemainingPtr event(new Api::EventRemaining());
169         Try {
170             event->setResult(this_->getRemaining());
171         }
172         Catch(Commons::PlatformException) {
173             event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
174         }
175
176         DPL::Mutex::ScopedLock lock(&this_->m_onRemainingMtx);
177         RemainingEventEmitters::iterator it = this_->m_onRemaining.begin();
178         for (; it != this_->m_onRemaining.end(); ++it) {
179             if (!!it->second) {
180                 it->second->emit(event);
181             }
182         }
183     }
184 }
185
186 void Battery::onLow(const VConf::Node* /* node */,
187         void* data)
188 {
189     Battery* this_ = static_cast<Battery*>(data);
190     if (this_) {
191         Api::EventLowPtr event(new Api::EventLow());
192         Try {
193             event->setResult(this_->getRemaining());
194         }
195         Catch(Commons::PlatformException) {
196             event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
197         }
198
199         DPL::Mutex::ScopedLock lock(&this_->m_onLowMtx);
200         LowEventEmitters::iterator it = this_->m_onLow.begin();
201         for (; it != this_->m_onLow.end(); ++it) {
202             if (!!it->second) {
203                 it->second->emit(event);
204             }
205         }
206     }
207 }
208 } // Power
209 } // WrtDeviceApis