2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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.
26 #include "ss_launch.h"
29 #include "ss_device_plugin.h"
30 #include "include/ss_data.h"
32 #define BAT_MON_INTERVAL 30
33 #define BAT_MON_INTERVAL_MIN 2
35 #define BATTERY_CHARGING 65535
36 #define BATTERY_UNKNOWN -1
37 #define BATTERY_FULL 100
38 #define BATTERY_NORMAL 100
39 #define BATTERY_WARNING_LOW 15
40 #define BATTERY_CRITICAL_LOW 5
41 #define BATTERY_POWER_OFF 1
42 #define BATTERY_REAL_POWER_OFF 0
44 #define MAX_BATTERY_ERROR 10
45 #define RESET_RETRY_COUNT 3
47 #define LOWBAT_EXEC_PATH PREFIX"/bin/lowbatt-popup"
49 static int battery_level_table[] = {
50 5, /* BATTERY_LEVEL0 */
59 #define _SYS_LOW_POWER "LOW_POWER"
61 struct lowbat_process_entry {
62 unsigned cur_bat_state;
63 unsigned new_bat_state;
64 int (*action) (void *);
67 static Ecore_Timer *lowbat_timer;
68 static int cur_bat_state = BATTERY_UNKNOWN;
69 static int cur_bat_capacity = -1;
71 static int bat_err_count = 0;
73 static int battery_warning_low_act(void *ad);
74 static int battery_critical_low_act(void *ad);
75 static int battery_power_off_act(void *ad);
77 static struct lowbat_process_entry lpe[] = {
78 {BATTERY_NORMAL, BATTERY_WARNING_LOW, battery_warning_low_act},
79 {BATTERY_WARNING_LOW, BATTERY_CRITICAL_LOW, battery_critical_low_act},
80 {BATTERY_CRITICAL_LOW, BATTERY_POWER_OFF, battery_critical_low_act},
81 {BATTERY_POWER_OFF, BATTERY_REAL_POWER_OFF, battery_power_off_act},
82 {BATTERY_NORMAL, BATTERY_CRITICAL_LOW, battery_critical_low_act},
83 {BATTERY_WARNING_LOW, BATTERY_POWER_OFF, battery_critical_low_act},
84 {BATTERY_CRITICAL_LOW, BATTERY_REAL_POWER_OFF, battery_power_off_act},
85 {BATTERY_NORMAL, BATTERY_POWER_OFF, battery_critical_low_act},
86 {BATTERY_WARNING_LOW, BATTERY_REAL_POWER_OFF, battery_power_off_act},
87 {BATTERY_NORMAL, BATTERY_REAL_POWER_OFF, battery_power_off_act},
90 static int battery_warning_low_act(void *data)
92 char lowbat_noti_name[NAME_MAX];
94 heynoti_get_snoti_name(_SYS_LOW_POWER, lowbat_noti_name, NAME_MAX);
95 ss_noti_send(lowbat_noti_name);
97 ss_action_entry_call_internal(PREDEF_LOWBAT, 1, WARNING_LOW_BAT_ACT);
101 static int battery_critical_low_act(void *data)
103 ss_action_entry_call_internal(PREDEF_LOWBAT, 1, CRITICAL_LOW_BAT_ACT);
107 static int battery_power_off_act(void *data)
109 ss_action_entry_call_internal(PREDEF_LOWBAT, 1, POWER_OFF_BAT_ACT);
113 static int battery_charge_act(void *data)
118 int ss_lowbat_set_charge_on(int onoff)
120 if(vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, onoff)!=0) {
121 PRT_TRACE_ERR("fail to set charge vconf value");
127 int ss_lowbat_is_charge_in_now()
130 if (0 > plugin_intf->OEM_sys_get_battery_charge_now(&val)) {
131 PRT_TRACE_ERR("fail to read charge now from kernel");
132 ss_lowbat_set_charge_on(0);
137 ss_lowbat_set_charge_on(1);
140 ss_lowbat_set_charge_on(0);
145 static int lowbat_process(int bat_percent, void *ad)
147 int new_bat_capacity;
149 int vconf_state = -1;
153 new_bat_capacity = bat_percent;
154 if (new_bat_capacity < 0)
156 if (new_bat_capacity != cur_bat_capacity) {
157 if (vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, new_bat_capacity) == 0)
158 cur_bat_capacity = new_bat_capacity;
159 PRT_TRACE("[BAT_MON] cur = %d new = %d", cur_bat_capacity, new_bat_capacity);
162 if (0 > vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &vconf_state)) {
163 PRT_TRACE_ERR("vconf_get_int() failed");
167 if (new_bat_capacity <= BATTERY_REAL_POWER_OFF) {
168 new_bat_state = BATTERY_REAL_POWER_OFF;
169 if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
170 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_POWER_OFF);
171 } else if (new_bat_capacity <= BATTERY_POWER_OFF) {
172 new_bat_state = BATTERY_POWER_OFF;
173 if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
174 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_POWER_OFF);
175 } else if (new_bat_capacity <= BATTERY_CRITICAL_LOW) {
176 new_bat_state = BATTERY_CRITICAL_LOW;
177 if (vconf_state != VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
178 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
179 } else if (new_bat_capacity <= BATTERY_WARNING_LOW) {
180 new_bat_state = BATTERY_WARNING_LOW;
181 if (vconf_state != VCONFKEY_SYSMAN_BAT_WARNING_LOW)
182 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_WARNING_LOW);
184 new_bat_state = BATTERY_NORMAL;
185 if (new_bat_capacity == BATTERY_FULL) {
186 plugin_intf->OEM_sys_get_battery_charge_full(&bat_full);
188 if (vconf_state != VCONFKEY_SYSMAN_BAT_FULL)
189 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_FULL);
191 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
192 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
195 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
196 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
203 ss_lowbat_is_charge_in_now();
205 if (cur_bat_state == new_bat_state
206 && cur_bat_state != BATTERY_POWER_OFF)
209 if (cur_bat_state == BATTERY_UNKNOWN) {
211 i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
213 if (new_bat_state == lpe[i].new_bat_state) {
215 cur_bat_state = new_bat_state;
221 i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
223 if ((cur_bat_state == lpe[i].cur_bat_state)
224 && (new_bat_state == lpe[i].new_bat_state)) {
226 cur_bat_state = new_bat_state;
231 cur_bat_state = new_bat_state;
232 PRT_TRACE("[BATMON] Unknown battery state");
236 static int lowbat_read()
240 plugin_intf->OEM_sys_get_battery_capacity(&bat_percent);
245 int ss_lowbat_monitor(void *data)
247 struct ss_main_data *ad = (struct ss_main_data *)data;
250 bat_percent = lowbat_read();
251 if (bat_percent < 0) {
252 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
254 if (bat_err_count > MAX_BATTERY_ERROR) {
256 ("[BATMON] Cannot read battery gage. stop read fuel gage");
261 if (bat_percent > 100)
264 if (lowbat_process(bat_percent, ad) < 0)
265 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
267 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL);
272 static int wakeup_cb(keynode_t *key_nodes, void *data)
277 vconf_keynode_get_int(key_nodes)) == VCONFKEY_PM_STATE_LCDOFF)
278 ss_lowbat_monitor(NULL);
283 /* for debugging (request by kernel) */
284 static int check_battery()
289 if (0 > plugin_intf->OEM_sys_get_battery_present(&ret)) {
290 PRT_TRACE_ERR("[BATMON] battery check : %d", ret);
292 PRT_TRACE("[BATMON] battery check : %d", ret);
297 int ss_lowbat_init(struct ss_main_data *ad)
301 /* need check battery */
303 ecore_timer_add(BAT_MON_INTERVAL_MIN, ss_lowbat_monitor, ad);
304 ss_lowbat_is_charge_in_now();
306 vconf_notify_key_changed(VCONFKEY_PM_STATE, (void *)wakeup_cb, NULL);