--- /dev/null
+/*
+ * 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 <system_info.h>
+#include <device/callback.h>
+#include <device/battery.h>
+
+#include <ContextTypes.h>
+#include <JobSchedulerTypesPrivate.h>
+#include <job_scheduler_types_internal.h>
+#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<BatteryState*>(userData);
+ pubs->read();
+ pubs->notifyObservers();
+}