Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / system / FSys_BatteryImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
17 /**
18  * @file                FSys_BatteryImpl.cpp
19  * @brief               This is the implementation file for _BatteryImpl class.
20  */
21
22 #include <device.h>
23 #include <runtime_info.h>
24
25 #include <FSysDeviceManager.h>
26
27 #include <FBaseSysLog.h>
28 #include <FBase_NativeError.h>
29 #include <FSys_BatteryImpl.h>
30 #include <FSys_Types.h>
31
32 #include "FSys_SystemInfoImpl.h"
33
34 using namespace Tizen::Base;
35
36 namespace Tizen { namespace System
37 {
38
39 _BatteryImpl::_BatteryImpl(void)
40 {
41 }
42
43 _BatteryImpl::~_BatteryImpl(void)
44 {
45 }
46
47 result
48 _BatteryImpl::IsCharging(bool& charging)
49 {
50         bool chargeState = false;
51         int ret = 0;
52         ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_BATTERY_IS_CHARGING, &chargeState);
53         SysTryReturnResult(NID_SYS, ret == 0, E_SYSTEM, "[E_SYSTEM] runtime_info_get_value_bool returns %d.", ret);
54         charging = chargeState;
55         return E_SUCCESS;
56 }
57
58 result
59 _BatteryImpl::GetCurrentLevelInPercentage(int& level)
60 {
61         int ret = 0;
62         int batteryLevel = 0;
63
64         ret = device_battery_get_percent(&batteryLevel);
65         SysTryReturnResult(NID_SYS, ret == DEVICE_ERROR_NONE, E_SYSTEM, "Failed to get the device battery percentage");
66
67         level = batteryLevel;
68
69         return E_SUCCESS;
70 }
71
72 result
73 _BatteryImpl::GetCurrentLevel(BatteryLevel& level)
74 {
75         int ret = 0;
76         int batteryLevel = 0;
77         device_battery_warn_e batteryStatus;
78
79         result r = device_battery_get_percent(&batteryLevel);
80         SysTryReturnResult(NID_SYS, !IsFailed(r), r, "Failed to get the device battery percentage");
81
82         if (batteryLevel == _BATTERY_LEVEL_FULL)
83         {
84                 level = BATTERY_FULL;
85                 return E_SUCCESS;
86         }
87
88         ret = device_battery_get_warning_status(&batteryStatus);
89
90         switch (batteryStatus)
91         {
92         case DEVICE_BATTERY_WARN_NORMAL:
93                 level = BATTERY_HIGH;
94                 break;
95         case DEVICE_BATTERY_WARN_LOW:
96                 level = BATTERY_LOW;
97                 break;
98         case DEVICE_BATTERY_WARN_CRITICAL:
99                 level = BATTERY_CRITICAL;
100                 break;
101         case DEVICE_BATTERY_WARN_EMPTY:
102                 level = BATTERY_EMPTY;
103                 break;
104         default:
105                 SysLogException(NID_SYS, E_SYSTEM, "There is a system error occured.");
106                 return E_SYSTEM;
107         }
108
109         return E_SUCCESS;
110 }
111
112 } } // Tizen::System