From: Somin Kim Date: Thu, 3 Aug 2017 05:05:31 +0000 (+0900) Subject: Add Battery state publisher X-Git-Tag: submit/tizen/20170806.070303~1^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F01%2F142201%2F1;p=platform%2Fcore%2Fcontext%2Fjob-scheduler.git Add Battery state publisher Change-Id: Iee31042c13f9310ba998c05ec358d24002fe46d9 Signed-off-by: Somin Kim --- diff --git a/src/server/publisher/BatteryState.cpp b/src/server/publisher/BatteryState.cpp new file mode 100644 index 0000000..4ea4c92 --- /dev/null +++ b/src/server/publisher/BatteryState.cpp @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include +#include +#include +#include "../ContextPublisher.h" +#include "../IContextObserver.h" + +using namespace ctx; + +namespace { + class BatteryState : public ContextPublisher { + public: + BatteryState(uid_t uid); + ~BatteryState(); + + const char* getUri() const; + + protected: + void read(); + void subscribe(); + void unsubscribe(); + + private: + static void __deviceCb(device_callback_e deviceType, void* value, void* userData); + }; +} + +REGISTER_PUBLISHER(URI(BATTERY), BatteryState) + +BatteryState::BatteryState(uid_t uid) +{ + bool supported = false; + system_info_get_platform_bool("tizen.org/feature/battery", &supported); + IF_FAIL_THROW(supported, E_SUPPORT); + + _D("Created"); +} + +BatteryState::~BatteryState() +{ + unsubscribe(); + + _D("Destroyed"); +} + +const char* BatteryState::getUri() const +{ + return URI(BATTERY); +} + +void BatteryState::read() +{ + device_battery_level_e level; + bool isCharging = false; + + int err = device_battery_get_level_status(&level); + if (IS_FAILED(err)) + _E("Getting battery level failed (%d)", err); + + err = device_battery_is_charging(&isCharging); + if (IS_FAILED(err)) + _E("Getting charging state failed (%d)", err); + + __data[NAME(LEVEL)] = [&level]()->const char* { + if (level == DEVICE_BATTERY_LEVEL_EMPTY) + return VALUE(EMPTY); + if (level == DEVICE_BATTERY_LEVEL_CRITICAL) + return VALUE(CRITICAL); + if (level == DEVICE_BATTERY_LEVEL_LOW) + return VALUE(LOW); + if (level == DEVICE_BATTERY_LEVEL_HIGH) + return VALUE(HIGH); + if (level == DEVICE_BATTERY_LEVEL_FULL) /* Not used */ + return VALUE(FULL); + return VALUE(UNDEFINED); + }(); + + __data[NAME(IS_CHARGING)] = (isCharging ? VALUE(TRUE) : VALUE(FALSE)); +} + +void BatteryState::subscribe() +{ + int err = device_add_callback(DEVICE_CALLBACK_BATTERY_LEVEL, __deviceCb, this); + + if (IS_FAILED(err)) + _E("Add device callback failed (%d)", err); +} + +void BatteryState::unsubscribe() +{ + device_remove_callback(DEVICE_CALLBACK_BATTERY_LEVEL, __deviceCb); +} + +void BatteryState::__deviceCb(device_callback_e deviceType, void* value, void* userData) +{ + BatteryState* pubs = static_cast(userData); + pubs->read(); + pubs->notifyObservers(); +}