b091de98bb723bddd17b8026479175675d823527
[platform/core/system/system-server.git] / ss_lowbat_handler.c
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 #include <assert.h>
19 #include <limits.h>
20 #include <heynoti.h>
21 #include <vconf.h>
22 #include <sysman.h>
23 #include <fcntl.h>
24
25 #include "ss_log.h"
26 #include "ss_launch.h"
27 #include "ss_noti.h"
28 #include "ss_queue.h"
29 #include "device-node.h"
30 #include "include/ss_data.h"
31
32 #define BAT_MON_INTERVAL                30
33 #define BAT_MON_INTERVAL_MIN            2
34
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
43
44 #define MAX_BATTERY_ERROR               10
45 #define RESET_RETRY_COUNT               3
46
47 #define LOWBAT_EXEC_PATH                PREFIX"/bin/lowbatt-popup"
48
49 #define BATTERY_LEVEL_CHECK_FULL        95
50 #define BATTERY_LEVEL_CHECK_HIGH        15
51 #define BATTERY_LEVEL_CHECK_LOW         5
52 #define BATTERY_LEVEL_CHECK_CRITICAL    1
53
54 #define _SYS_LOW_POWER "LOW_POWER"
55
56 struct lowbat_process_entry {
57         unsigned cur_bat_state;
58         unsigned new_bat_state;
59         int (*action) (void *);
60 };
61
62 static Ecore_Timer *lowbat_timer;
63 static int cur_bat_state = BATTERY_UNKNOWN;
64 static int cur_bat_capacity = -1;
65
66 static int bat_err_count = 0;
67
68 static int battery_warning_low_act(void *ad);
69 static int battery_critical_low_act(void *ad);
70 static int battery_power_off_act(void *ad);
71
72 static struct lowbat_process_entry lpe[] = {
73         {BATTERY_NORMAL, BATTERY_WARNING_LOW, battery_warning_low_act},
74         {BATTERY_WARNING_LOW, BATTERY_CRITICAL_LOW, battery_critical_low_act},
75         {BATTERY_CRITICAL_LOW,  BATTERY_POWER_OFF,              battery_critical_low_act},
76         {BATTERY_POWER_OFF,             BATTERY_REAL_POWER_OFF, battery_power_off_act},
77         {BATTERY_NORMAL, BATTERY_CRITICAL_LOW, battery_critical_low_act},
78         {BATTERY_WARNING_LOW,   BATTERY_POWER_OFF,              battery_critical_low_act},
79         {BATTERY_CRITICAL_LOW,  BATTERY_REAL_POWER_OFF, battery_power_off_act},
80         {BATTERY_NORMAL,                BATTERY_POWER_OFF,              battery_critical_low_act},
81         {BATTERY_WARNING_LOW,   BATTERY_REAL_POWER_OFF, battery_power_off_act},
82         {BATTERY_NORMAL,                BATTERY_REAL_POWER_OFF, battery_power_off_act},
83 };
84
85 /*
86  * TODO: remove this function
87  */
88 static void print_lowbat_state(unsigned int bat_percent)
89 {
90 #if 0
91         int i;
92         for (i = 0; i < BAT_MON_SAMPLES; i++)
93                 PRT_TRACE("\t%d", recent_bat_percent[i]);
94 #endif
95 }
96
97 static int battery_warning_low_act(void *data)
98 {
99         char lowbat_noti_name[NAME_MAX];
100
101         heynoti_get_snoti_name(_SYS_LOW_POWER, lowbat_noti_name, NAME_MAX);
102         ss_noti_send(lowbat_noti_name);
103
104         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, WARNING_LOW_BAT_ACT);
105         return 0;
106 }
107
108 static int battery_critical_low_act(void *data)
109 {
110         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, CRITICAL_LOW_BAT_ACT);
111         return 0;
112 }
113
114 static int battery_power_off_act(void *data)
115 {
116         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, POWER_OFF_BAT_ACT);
117         return 0;
118 }
119
120 static int battery_charge_act(void *data)
121 {
122         return 0;
123 }
124
125 int ss_lowbat_set_charge_on(int onoff)
126 {
127         if(vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, onoff)!=0) {
128                 PRT_TRACE_ERR("fail to set charge vconf value");
129                 return -1;
130         }
131         return 0;
132 }
133
134 int ss_lowbat_is_charge_in_now()
135 {
136         int val = 0;
137         if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CHARGE_NOW, &val) < 0) {
138                 PRT_TRACE_ERR("fail to read charge now from kernel");
139                 ss_lowbat_set_charge_on(0);
140                 return 0;
141         }
142
143         if (val == 1) {
144                 ss_lowbat_set_charge_on(1);
145                 return 1;
146         } else {
147                 ss_lowbat_set_charge_on(0);
148                 return 0;
149         }
150 }
151
152 static int lowbat_process(int bat_percent, void *ad)
153 {
154         int new_bat_capacity;
155         int new_bat_state;
156         int vconf_state = -1;
157         int bat_full = -1;
158         int i, ret = 0;
159         int val = 0;
160         new_bat_capacity = bat_percent;
161         if (new_bat_capacity < 0)
162                 return -1;
163         if (new_bat_capacity != cur_bat_capacity) {
164                 PRT_TRACE("[BAT_MON] cur = %d new = %d", cur_bat_capacity, new_bat_capacity);
165                 if (vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, new_bat_capacity) == 0)
166                         cur_bat_capacity = new_bat_capacity;
167         }
168
169         if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &vconf_state) < 0) {
170                 PRT_TRACE_ERR("vconf_get_int() failed");
171                 return -1;
172         }
173
174         if (new_bat_capacity <= BATTERY_REAL_POWER_OFF) {
175                 if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CHARGE_NOW, &val) < 0) {
176                         PRT_TRACE_ERR("fail to read charge now from kernel");
177                 }
178                 PRT_TRACE("charge_now status %d",val);
179                 if (val == 1) {
180                         new_bat_state = BATTERY_POWER_OFF;
181                         if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
182                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_POWER_OFF);
183                 } else {
184                         new_bat_state = BATTERY_REAL_POWER_OFF;
185                         if (vconf_state != VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF)
186                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF);
187                 }
188         } else if (new_bat_capacity <= BATTERY_POWER_OFF) {
189                 new_bat_state = BATTERY_POWER_OFF;
190                 if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
191                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_POWER_OFF);
192         } else if (new_bat_capacity <= BATTERY_CRITICAL_LOW) {
193                 new_bat_state = BATTERY_CRITICAL_LOW;
194                 if (vconf_state != VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
195                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
196         } else if (new_bat_capacity <= BATTERY_WARNING_LOW) {
197                 new_bat_state = BATTERY_WARNING_LOW;
198                 if (vconf_state != VCONFKEY_SYSMAN_BAT_WARNING_LOW)
199                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_WARNING_LOW);
200         } else {
201                 new_bat_state = BATTERY_NORMAL;
202                 if (new_bat_capacity == BATTERY_FULL) {
203                         device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CHARGE_FULL, &bat_full);
204                         if (bat_full == 1) {
205                                 if (vconf_state != VCONFKEY_SYSMAN_BAT_FULL)
206                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_FULL);
207                         } else {
208                                 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
209                                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
210                         }
211                 } else {
212                         if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
213                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
214                 }
215         }
216
217         if(ret < 0)
218                 return -1;
219
220         ss_lowbat_is_charge_in_now();
221
222         if (cur_bat_state == new_bat_state) {
223                 return 0;
224         }
225
226         if (cur_bat_state == BATTERY_UNKNOWN) {
227                 for (i = 0;
228                      i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
229                      i++) {
230                         if (new_bat_state == lpe[i].new_bat_state) {
231                                 lpe[i].action(ad);
232                                 cur_bat_state = new_bat_state;
233                                 return 0;
234                         }
235                 }
236         } else {
237                 for (i = 0;
238                      i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
239                      i++) {
240                         if ((cur_bat_state == lpe[i].cur_bat_state)
241                             && (new_bat_state == lpe[i].new_bat_state)) {
242                                 lpe[i].action(ad);
243                                 cur_bat_state = new_bat_state;
244                                 return 0;
245                         }
246                 }
247         }
248         PRT_TRACE("[BATMON] Unknown battery state cur:%d new:%d",cur_bat_state,new_bat_state);
249         cur_bat_state = new_bat_state;
250         return -1;
251 }
252
253 static int lowbat_read()
254 {
255         int bat_percent;
256
257         device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CAPACITY, &bat_percent);
258
259         return bat_percent;
260 }
261
262 static void __ss_change_lowbat_level(int bat_percent)
263 {
264         int prev, now;
265
266         if (cur_bat_capacity == bat_percent)
267                 return;
268
269         if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &prev) < 0) {
270                 PRT_TRACE_ERR("vconf_get_int() failed");
271                 return;
272         }
273
274
275         if (bat_percent > BATTERY_LEVEL_CHECK_FULL) {
276                 now = VCONFKEY_SYSMAN_BAT_LEVEL_FULL;
277         } else if (bat_percent > BATTERY_LEVEL_CHECK_HIGH) {
278                 now = VCONFKEY_SYSMAN_BAT_LEVEL_HIGH;
279         } else if (bat_percent > BATTERY_LEVEL_CHECK_LOW) {
280                 now = VCONFKEY_SYSMAN_BAT_LEVEL_LOW;
281         } else if (bat_percent > BATTERY_LEVEL_CHECK_CRITICAL) {
282                 now = VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL;
283         } else {
284                 now = VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY;
285         }
286
287         if (prev != now)
288                 vconf_set_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, now);
289 }
290
291 static int __check_lowbat_percent(void)
292 {
293         int bat_percent;
294
295         bat_percent = lowbat_read();
296         if (bat_percent < 0) {
297                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
298                 bat_err_count++;
299                 if (bat_err_count > MAX_BATTERY_ERROR) {
300                         PRT_TRACE_ERR
301                             ("[BATMON] Cannot read battery gage. stop read fuel gage");
302                         return 0;
303                 }
304                 return 1;
305         }
306         if (bat_percent > 100)
307                 bat_percent = 100;
308         __ss_change_lowbat_level(bat_percent);
309         return bat_percent;
310 }
311
312 int ss_lowbat_monitor(void *data)
313 {
314         int bat_percent;
315         struct ss_main_data *ad = (struct ss_main_data *)data;
316
317         bat_percent = __check_lowbat_percent();
318         print_lowbat_state(bat_percent);
319
320         if (lowbat_process(bat_percent, ad) < 0)
321                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
322         else
323                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL);
324
325         return 1;
326 }
327
328 static int wakeup_cb(keynode_t *key_nodes, void *data)
329 {
330         int pm_state = 0;
331
332         if ((pm_state =
333              vconf_keynode_get_int(key_nodes)) == VCONFKEY_PM_STATE_LCDOFF)
334                 ss_lowbat_monitor(NULL);
335
336         return 0;
337 }
338
339 /* for debugging (request by kernel) */
340 static int check_battery()
341 {
342         int r;
343         int ret = -1;
344
345         if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_PRESENT, &ret) < 0) {
346                 PRT_TRACE_ERR("[BATMON] battery check : %d", ret);
347         }
348         PRT_TRACE("[BATMON] battery check : %d", ret);
349
350         return ret;
351 }
352
353 int ss_lowbat_init(struct ss_main_data *ad)
354 {
355         int i;
356
357         /* need check battery */
358         lowbat_timer =
359                 ecore_timer_add(BAT_MON_INTERVAL_MIN, ss_lowbat_monitor, ad);
360
361         __check_lowbat_percent();
362
363         ss_lowbat_is_charge_in_now();
364
365         vconf_notify_key_changed(VCONFKEY_PM_STATE, (void *)wakeup_cb, NULL);
366
367         return 0;
368 }