merge with master
[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 static int battery_warning_low_act(void *data)
86 {
87         char lowbat_noti_name[NAME_MAX];
88
89         heynoti_get_snoti_name(_SYS_LOW_POWER, lowbat_noti_name, NAME_MAX);
90         ss_noti_send(lowbat_noti_name);
91
92         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, WARNING_LOW_BAT_ACT);
93         return 0;
94 }
95
96 static int battery_critical_low_act(void *data)
97 {
98         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, CRITICAL_LOW_BAT_ACT);
99         return 0;
100 }
101
102 static int battery_power_off_act(void *data)
103 {
104         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, POWER_OFF_BAT_ACT);
105         return 0;
106 }
107
108 static int battery_charge_act(void *data)
109 {
110         return 0;
111 }
112
113 int ss_lowbat_set_charge_on(int onoff)
114 {
115         if(vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, onoff)!=0) {
116                 PRT_TRACE_ERR("fail to set charge vconf value");
117                 return -1;
118         }
119         return 0;
120 }
121
122 int ss_lowbat_is_charge_in_now()
123 {
124         int val = 0;
125         if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CHARGE_NOW, &val) < 0) {
126                 PRT_TRACE_ERR("fail to read charge now from kernel");
127                 ss_lowbat_set_charge_on(0);
128                 return 0;
129         }
130
131         if (val == 1) {
132                 ss_lowbat_set_charge_on(1);
133                 return 1;
134         } else {
135                 ss_lowbat_set_charge_on(0);
136                 return 0;
137         }
138 }
139
140 static int lowbat_process(int bat_percent, void *ad)
141 {
142         int new_bat_capacity;
143         int new_bat_state;
144         int vconf_state = -1;
145         int bat_full = -1;
146         int i, ret = 0;
147         int val = 0;
148         new_bat_capacity = bat_percent;
149         if (new_bat_capacity < 0)
150                 return -1;
151         if (new_bat_capacity != cur_bat_capacity) {
152                 PRT_TRACE("[BAT_MON] cur = %d new = %d", cur_bat_capacity, new_bat_capacity);
153                 if (vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, new_bat_capacity) == 0)
154                         cur_bat_capacity = new_bat_capacity;
155         }
156
157         if (0 > vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &vconf_state)) {
158                 PRT_TRACE_ERR("vconf_get_int() failed");
159                 return -1;
160         }
161
162         if (new_bat_capacity <= BATTERY_REAL_POWER_OFF) {
163                 if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CHARGE_NOW, &val) < 0) {
164                         PRT_TRACE_ERR("fail to read charge now from kernel");
165                 }
166                 PRT_TRACE("charge_now status %d",val);
167                 if (val == 1) {
168                         new_bat_state = BATTERY_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 {
172                         new_bat_state = BATTERY_REAL_POWER_OFF;
173                         if (vconf_state != VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF)
174                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF);
175                 }
176         } else if (new_bat_capacity <= BATTERY_POWER_OFF) {
177                 new_bat_state = BATTERY_POWER_OFF;
178                 if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
179                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_POWER_OFF);
180         } else if (new_bat_capacity <= BATTERY_CRITICAL_LOW) {
181                 new_bat_state = BATTERY_CRITICAL_LOW;
182                 if (vconf_state != VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
183                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_CRITICAL_LOW);
184         } else if (new_bat_capacity <= BATTERY_WARNING_LOW) {
185                 new_bat_state = BATTERY_WARNING_LOW;
186                 if (vconf_state != VCONFKEY_SYSMAN_BAT_WARNING_LOW)
187                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_WARNING_LOW);
188         } else {
189                 new_bat_state = BATTERY_NORMAL;
190                 if (new_bat_capacity == BATTERY_FULL) {
191                         if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CHARGE_FULL, &bat_full) < 0) {
192                                 PRT_TRACE_ERR("fail to read charge full from kernel");
193                         }
194                         if (bat_full == 1) {
195                                 if (vconf_state != VCONFKEY_SYSMAN_BAT_FULL)
196                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_FULL);
197                         } else {
198                                 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
199                                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
200                         }
201                 } else {
202                         if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
203                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
204                 }
205         }
206
207         if(ret < 0)
208                 return -1;
209
210         ss_lowbat_is_charge_in_now();
211
212         if (cur_bat_state == new_bat_state) {
213                 return 0;
214         }
215
216         if (cur_bat_state == BATTERY_UNKNOWN) {
217                 for (i = 0;
218                      i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
219                      i++) {
220                         if (new_bat_state == lpe[i].new_bat_state) {
221                                 lpe[i].action(ad);
222                                 cur_bat_state = new_bat_state;
223                                 return 0;
224                         }
225                 }
226         } else {
227                 for (i = 0;
228                      i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
229                      i++) {
230                         if ((cur_bat_state == lpe[i].cur_bat_state)
231                             && (new_bat_state == lpe[i].new_bat_state)) {
232                                 lpe[i].action(ad);
233                                 cur_bat_state = new_bat_state;
234                                 return 0;
235                         }
236                 }
237         }
238         PRT_TRACE("[BATMON] Unknown battery state cur:%d new:%d",cur_bat_state,new_bat_state);
239         cur_bat_state = new_bat_state;
240         return -1;
241 }
242
243 static int lowbat_read()
244 {
245         int bat_percent;
246
247         if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_CAPACITY, &bat_percent) < 0) {
248                 PRT_TRACE_ERR("fail to read power capacity from kernel");
249                 return -1;
250         }
251
252         return bat_percent;
253 }
254
255 static void __ss_change_lowbat_level(int bat_percent)
256 {
257         int prev, now;
258
259         if (cur_bat_capacity == bat_percent)
260                 return;
261
262         if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &prev) < 0) {
263                 PRT_TRACE_ERR("vconf_get_int() failed");
264                 return;
265         }
266
267
268         if (bat_percent > BATTERY_LEVEL_CHECK_FULL) {
269                 now = VCONFKEY_SYSMAN_BAT_LEVEL_FULL;
270         } else if (bat_percent > BATTERY_LEVEL_CHECK_HIGH) {
271                 now = VCONFKEY_SYSMAN_BAT_LEVEL_HIGH;
272         } else if (bat_percent > BATTERY_LEVEL_CHECK_LOW) {
273                 now = VCONFKEY_SYSMAN_BAT_LEVEL_LOW;
274         } else if (bat_percent > BATTERY_LEVEL_CHECK_CRITICAL) {
275                 now = VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL;
276         } else {
277                 now = VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY;
278         }
279
280         if (prev != now)
281                 vconf_set_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, now);
282 }
283
284 static int __check_lowbat_percent(void)
285 {
286         int bat_percent;
287
288         bat_percent = lowbat_read();
289         if (bat_percent < 0) {
290                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
291                 bat_err_count++;
292                 if (bat_err_count > MAX_BATTERY_ERROR) {
293                         PRT_TRACE_ERR
294                             ("[BATMON] Cannot read battery gage. stop read fuel gage");
295                 }
296                 return -1;
297         }
298         if (bat_percent > 100)
299                 bat_percent = 100;
300         __ss_change_lowbat_level(bat_percent);
301         return bat_percent;
302 }
303
304 int ss_lowbat_monitor(void *data)
305 {
306         struct ss_main_data *ad = (struct ss_main_data *)data;
307         int bat_percent;
308
309         bat_percent = __check_lowbat_percent();
310
311         if (lowbat_process(bat_percent, ad) < 0)
312                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
313         else
314                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL);
315
316         return 1;
317 }
318
319 static int wakeup_cb(keynode_t *key_nodes, void *data)
320 {
321         int pm_state = 0;
322
323         if ((pm_state =
324              vconf_keynode_get_int(key_nodes)) == VCONFKEY_PM_STATE_LCDOFF)
325                 ss_lowbat_monitor(NULL);
326
327         return 0;
328 }
329
330 /* for debugging (request by kernel) */
331 static int check_battery()
332 {
333         int r;
334         int ret = -1;
335
336         if (device_get_property(DEVICE_TYPE_POWER, PROP_POWER_PRESENT, &ret) < 0) {
337                 PRT_TRACE_ERR("[BATMON] battery check : %d", ret);
338         }
339         PRT_TRACE("[BATMON] battery check : %d", ret);
340
341         return ret;
342 }
343
344 int ss_lowbat_init(struct ss_main_data *ad)
345 {
346         int i;
347
348         /* need check battery */
349         lowbat_timer =
350             ecore_timer_add(BAT_MON_INTERVAL_MIN, ss_lowbat_monitor, ad);
351
352         __check_lowbat_percent();
353
354         ss_lowbat_is_charge_in_now();
355
356         vconf_notify_key_changed(VCONFKEY_PM_STATE, (void *)wakeup_cb, NULL);
357
358         return 0;
359 }