sync with tizen_2.0
[platform/framework/native/appfw.git] / src / system / FSys_BatteryImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_BatteryImpl.cpp
20  * @brief               This is the implementation file for _BatteryImpl class.
21  */
22
23 #include <device.h>
24 #include <runtime_info.h>
25
26 #include <FSysDeviceManager.h>
27
28 #include <FBaseSysLog.h>
29 #include <FBase_NativeError.h>
30 #include <FSys_BatteryImpl.h>
31 #include <FSys_Types.h>
32
33 #include "FSys_SystemInfoImpl.h"
34
35 using namespace Tizen::Base;
36
37 namespace Tizen { namespace System
38 {
39
40 _BatteryImpl::_BatteryImpl(void)
41 {
42 }
43
44 _BatteryImpl::~_BatteryImpl(void)
45 {
46 }
47
48 result
49 _BatteryImpl::IsCharging(bool& charging)
50 {
51         bool chargeState = false;
52         int ret = 0;
53         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
54         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] runtime_info_get_value_bool returns %d.", ret);
55         charging = chargeState;
56         return E_SUCCESS;
57 }
58
59 result
60 _BatteryImpl::GetCurrentLevelInPercentage(int& level)
61 {
62         int ret = 0;
63         int batteryLevel = 0;
64
65         ret = device_battery_get_percent(&batteryLevel);
66         SysTryReturnResult(NID_SYS, ret == DEVICE_ERROR_NONE, E_SYSTEM, "Failed to get the device battery percentage");
67
68         level = batteryLevel;
69
70         return E_SUCCESS;
71 }
72
73 result
74 _BatteryImpl::GetCurrentLevel(BatteryLevel& level)
75 {
76         int ret = 0;
77         int batteryLevel = 0;
78         device_battery_warn_e batteryStatus;
79
80         result r = device_battery_get_percent(&batteryLevel);
81         SysTryReturnResult(NID_SYS, !IsFailed(r), r, "Failed to get the device battery percentage");
82
83         if (batteryLevel == _BATTERY_LEVEL_FULL)
84         {
85                 level = BATTERY_FULL;
86                 return E_SUCCESS;
87         }
88
89         ret = device_battery_get_warning_status(&batteryStatus);
90
91         switch (batteryStatus)
92         {
93         case DEVICE_BATTERY_WARN_NORMAL:
94                 level = BATTERY_HIGH;
95                 break;
96         case DEVICE_BATTERY_WARN_LOW:
97                 level = BATTERY_LOW;
98                 break;
99         case DEVICE_BATTERY_WARN_CRITICAL:
100                 level = BATTERY_CRITICAL;
101                 break;
102         case DEVICE_BATTERY_WARN_EMPTY:
103                 level = BATTERY_EMPTY;
104                 break;
105         default:
106                 SysLogException(NID_SYS, E_SYSTEM, "There is a system error occured.");
107                 return E_SYSTEM;
108         }
109
110         return E_SUCCESS;
111 }
112
113 } } // Tizen::System