change usb control process name to usb-server
[framework/system/system-server.git] / ss_lowbat_handler.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *      http://www.tizenopensource.org/license
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 "ss_device_plugin.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 static int battery_level_table[] = {
50         5,                      /* BATTERY_LEVEL0 */
51         15,                     /* 1 */
52         25,                     /* 2 */
53         40,                     /* 3 */
54         60,                     /* 4 */
55         80,                     /* 5 */
56         100,                    /* 6 */
57 };
58
59 #define _SYS_LOW_POWER "LOW_POWER"
60
61 struct lowbat_process_entry {
62         unsigned cur_bat_state;
63         unsigned new_bat_state;
64         int (*action) (void *);
65 };
66
67 static Ecore_Timer *lowbat_timer;
68 static int cur_bat_state = BATTERY_UNKNOWN;
69 static int cur_bat_capacity = -1;
70
71 static int bat_err_count = 0;
72
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);
76
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},
88 };
89
90 static int battery_warning_low_act(void *data)
91 {
92         char lowbat_noti_name[NAME_MAX];
93
94         heynoti_get_snoti_name(_SYS_LOW_POWER, lowbat_noti_name, NAME_MAX);
95         ss_noti_send(lowbat_noti_name);
96
97         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, WARNING_LOW_BAT_ACT);
98         return 0;
99 }
100
101 static int battery_critical_low_act(void *data)
102 {
103         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, CRITICAL_LOW_BAT_ACT);
104         return 0;
105 }
106
107 static int battery_power_off_act(void *data)
108 {
109         ss_action_entry_call_internal(PREDEF_LOWBAT, 1, POWER_OFF_BAT_ACT);
110         return 0;
111 }
112
113 static int battery_charge_act(void *data)
114 {
115         return 0;
116 }
117
118 int ss_lowbat_set_charge_on(int onoff)
119 {
120         if(vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, onoff)!=0) {
121                 PRT_TRACE_ERR("fail to set charge vconf value");
122                 return -1;
123         }
124         return 0;
125 }
126
127 int ss_lowbat_is_charge_in_now()
128 {
129         int val = 0;
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);
133                 return 0;
134         }
135
136         if (val == 1) {
137                 ss_lowbat_set_charge_on(1);
138                 return 1;
139         } else {
140                 ss_lowbat_set_charge_on(0);
141                 return 0;
142         }
143 }
144
145 static int lowbat_process(int bat_percent, void *ad)
146 {
147         int new_bat_capacity;
148         int new_bat_state;
149         int vconf_state = -1;
150         int bat_full = -1;
151         int i, ret = 0;
152
153         new_bat_capacity = bat_percent;
154         if (new_bat_capacity < 0)
155                 return -1;
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);
160         }
161
162         if (0 > vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &vconf_state)) {
163                 PRT_TRACE_ERR("vconf_get_int() failed");
164                 return -1;
165         }
166
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);
183         } else {
184                 new_bat_state = BATTERY_NORMAL;
185                 if (new_bat_capacity == BATTERY_FULL) {
186                         plugin_intf->OEM_sys_get_battery_charge_full(&bat_full);
187                         if (bat_full == 1) {
188                                 if (vconf_state != VCONFKEY_SYSMAN_BAT_FULL)
189                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_FULL);
190                         } else {
191                                 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
192                                         ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
193                         }
194                 } else {
195                         if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
196                                 ret=vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, VCONFKEY_SYSMAN_BAT_NORMAL);
197                 }
198         }
199
200         if(ret < 0)
201                 return -1;
202
203         ss_lowbat_is_charge_in_now();
204
205         if (cur_bat_state == new_bat_state
206             && cur_bat_state != BATTERY_POWER_OFF)
207                 return 0;
208
209         if (cur_bat_state == BATTERY_UNKNOWN) {
210                 for (i = 0;
211                      i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
212                      i++) {
213                         if (new_bat_state == lpe[i].new_bat_state) {
214                                 lpe[i].action(ad);
215                                 cur_bat_state = new_bat_state;
216                                 return 0;
217                         }
218                 }
219         } else {
220                 for (i = 0;
221                      i < sizeof(lpe) / sizeof(struct lowbat_process_entry);
222                      i++) {
223                         if ((cur_bat_state == lpe[i].cur_bat_state)
224                             && (new_bat_state == lpe[i].new_bat_state)) {
225                                 lpe[i].action(ad);
226                                 cur_bat_state = new_bat_state;
227                                 return 0;
228                         }
229                 }
230         }
231         cur_bat_state = new_bat_state;
232         PRT_TRACE("[BATMON] Unknown battery state");
233         return -1;
234 }
235
236 static int lowbat_read()
237 {
238         int bat_percent;
239
240         plugin_intf->OEM_sys_get_battery_capacity(&bat_percent);
241
242         return bat_percent;
243 }
244
245 int ss_lowbat_monitor(void *data)
246 {
247         struct ss_main_data *ad = (struct ss_main_data *)data;
248         int bat_percent;
249
250         bat_percent = lowbat_read();
251         if (bat_percent < 0) {
252                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
253                 bat_err_count++;
254                 if (bat_err_count > MAX_BATTERY_ERROR) {
255                         PRT_TRACE_ERR
256                             ("[BATMON] Cannot read battery gage. stop read fuel gage");
257                         return 0;
258                 }
259                 return 1;
260         }
261         if (bat_percent > 100)
262                 bat_percent = 100;
263
264         if (lowbat_process(bat_percent, ad) < 0)
265                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL_MIN);
266         else
267                 ecore_timer_interval_set(lowbat_timer, BAT_MON_INTERVAL);
268
269         return 1;
270 }
271
272 static int wakeup_cb(keynode_t *key_nodes, void *data)
273 {
274         int pm_state = 0;
275
276         if ((pm_state =
277              vconf_keynode_get_int(key_nodes)) == VCONFKEY_PM_STATE_LCDOFF)
278                 ss_lowbat_monitor(NULL);
279
280         return 0;
281 }
282
283 /* for debugging (request by kernel) */
284 static int check_battery()
285 {
286         int r;
287         int ret = -1;
288
289         if (0 > plugin_intf->OEM_sys_get_battery_present(&ret)) {
290                 PRT_TRACE_ERR("[BATMON] battery check : %d", ret);
291         }
292         PRT_TRACE("[BATMON] battery check : %d", ret);
293
294         return ret;
295 }
296
297 int ss_lowbat_init(struct ss_main_data *ad)
298 {
299         int i;
300
301         /* need check battery */
302         lowbat_timer =
303             ecore_timer_add(BAT_MON_INTERVAL_MIN, ss_lowbat_monitor, ad);
304         ss_lowbat_is_charge_in_now();
305
306         vconf_notify_key_changed(VCONFKEY_PM_STATE, (void *)wakeup_cb, NULL);
307
308         return 0;
309 }