2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <Commons/Exception.h>
18 #include <Power/EventCharging.h>
19 #include <Power/EventRemaining.h>
22 namespace WrtDeviceApis {
25 Battery::Battery(std::size_t index) :
27 m_onChargingKey(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW),
28 m_onRemainingKey(VCONFKEY_SYSMAN_BATTERY_LEVEL),
29 m_onLowKey(VCONFKEY_TELEPHONY_LOW_BATTERY)
37 bool Battery::isCharging() const
40 return m_onChargingKey.getBool();
42 Catch(Commons::ConversionException) {
43 ReThrowMsg(Commons::PlatformException,
44 "Could not discover battery state.");
48 unsigned int Battery::getRemaining() const
51 int level = m_onRemainingKey.getInt();
53 (level / static_cast<double>(VCONFKEY_SYSMAN_BAT_LEVEL_MAX));
54 return static_cast<unsigned int>(ratio * 100);
56 Catch(Commons::ConversionException) {
57 ReThrowMsg(Commons::PlatformException,
58 "Could not discover battery level.");
62 void Battery::addOnCharging(const Power::Api::EventChargingEmitterPtr& emitter)
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.");
73 m_onCharging.insert(ChargingEventEmitters::value_type(emitter->getId(),
78 void Battery::removeOnCharging(Power::Api::EventChargingEmitter::IdType id)
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();
90 void Battery::addOnRemaining(
91 const Api::EventRemainingEmitterPtr& emitter)
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.");
102 m_onRemaining.insert(RemainingEventEmitters::value_type(emitter->getId(),
107 void Battery::removeOnRemaining(Api::EventRemainingEmitter::IdType id)
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();
119 void Battery::addOnLow(const Api::EventLowEmitterPtr& emitter)
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.");
130 m_onLow.insert(LowEventEmitters::value_type(emitter->getId(), emitter));
134 void Battery::removeOnLow(Api::EventLowEmitter::IdType id)
136 DPL::Mutex::ScopedLock lock(&m_onLowMtx);
137 LowEventEmitters::iterator it = m_onLow.find(id);
138 if (it != m_onLow.end()) {
140 if (m_onLow.size() == 0) {
141 m_onLowKey.detachCallback();
146 void Battery::onCharging(const VConf::Node* /* node */,
149 Battery* this_ = static_cast<Battery*>(data);
151 Api::EventChargingPtr event(new Api::EventCharging());
153 DPL::Mutex::ScopedLock lock(&this_->m_onChargingMtx);
154 ChargingEventEmitters::iterator it = this_->m_onCharging.begin();
155 for (; it != this_->m_onCharging.end(); ++it) {
157 it->second->emit(event);
163 void Battery::onRemaining(const VConf::Node* /* node */,
166 Battery* this_ = static_cast<Battery*>(data);
168 Api::EventRemainingPtr event(new Api::EventRemaining());
170 event->setResult(this_->getRemaining());
172 Catch(Commons::PlatformException) {
173 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
176 DPL::Mutex::ScopedLock lock(&this_->m_onRemainingMtx);
177 RemainingEventEmitters::iterator it = this_->m_onRemaining.begin();
178 for (; it != this_->m_onRemaining.end(); ++it) {
180 it->second->emit(event);
186 void Battery::onLow(const VConf::Node* /* node */,
189 Battery* this_ = static_cast<Battery*>(data);
191 Api::EventLowPtr event(new Api::EventLow());
193 event->setResult(this_->getRemaining());
195 Catch(Commons::PlatformException) {
196 event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
199 DPL::Mutex::ScopedLock lock(&this_->m_onLowMtx);
200 LowEventEmitters::iterator it = this_->m_onLow.begin();
201 for (; it != this_->m_onLow.end(); ++it) {
203 it->second->emit(event);