summaryrefslogtreecommitdiff
path: root/src/battery/lowbat-handler.c
blob: f83bccedbe38576084bea94a28ed43939e96a622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/*
 * deviced
 *
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdbool.h>
#include <assert.h>
#include <limits.h>
#include <vconf.h>
#include <fcntl.h>
#include <bundle.h>
#include <eventsystem.h>

#include "battery.h"
#include "config.h"
#include "core/log.h"
#include "core/launch.h"
#include "core/devices.h"
#include "core/device-notifier.h"
#include "core/common.h"
#include "core/list.h"
#include "core/udev.h"
#include "device-node.h"
#include "display/setting.h"
#include "display/poll.h"
#include "core/edbus-handler.h"
#include "power/power-handler.h"
#include "apps/apps.h"
#include "power-supply.h"

#define CHARGE_POWERSAVE_FREQ_ACT	"charge_powersave_freq_act"
#define CHARGE_RELEASE_FREQ_ACT		"charge_release_freq_act"

#define POWER_OFF_CHECK_TIMER	(30)

#define BATTERY_CHARGING	65535
#define BATTERY_UNKNOWN		-1

#define WARNING_LOW_BAT_ACT		"warning_low_bat_act"
#define CRITICAL_LOW_BAT_ACT		"critical_low_bat_act"
#define POWER_OFF_BAT_ACT		"power_off_bat_act"
#define CHARGE_BAT_ACT			"charge_bat_act"
#define CHARGE_CHECK_ACT		"charge_check_act"
#define CHARGE_ERROR_ACT		"charge_error_act"
#define CHARGE_ERROR_LOW_ACT		"charge_error_low_act"
#define CHARGE_ERROR_HIGH_ACT		"charge_error_high_act"
#define CHARGE_ERROR_OVP_ACT		"charge_error_ovp_act"
#define POWER_SOURCE_USB		"usb"
#define POWER_SOURCE_AC			"ac"
#define POWER_SOURCE_NONE		"none"
#define POWER_SOURCE_WIRELESS		"wireless"

#define WAITING_INTERVAL	10

#define LOWBAT_CPU_CTRL_ID	"id6"
#define LOWBAT_CPU_FREQ_RATE	(0.7)

#define POWER_OFF_UNLOCK	0
#define POWER_OFF_LOCK		1

enum low_battery_type {
	LOW_BATTERY,
	CRITICAL_BATTERY,
};

struct lowbat_process_entry {
	int old;
	int now;
	int (*func) (void *data);
};

static int cur_bat_state = BATTERY_UNKNOWN;
static int cur_bat_capacity = -1;
static int event_noti_on = -1, active_noti_on = -1;

static struct battery_config_info battery_info = {
	.normal   = BATTERY_NORMAL,
	.warning  = BATTERY_WARNING,
	.critical = BATTERY_CRITICAL,
	.poweroff = BATTERY_POWEROFF,
	.realoff  = BATTERY_REALOFF,
};

static dd_list *lpe;
static int scenario_count;
static int power_off_lock = POWER_OFF_UNLOCK;
static Ecore_Timer *power_off_timer;

static int lowbat_popup(char *option);

static int lowbat_initialized(void *data)
{
	static int status;

	if (!data)
		return status;

	status = *(int *)data;
	return status;
}

static int lowbat_scenario(int old, int now, void *data)
{
	dd_list *n;
	struct lowbat_process_entry *scenario;
	int found = 0;

	if (old == now && battery.charge_now)
		return found;
	DD_LIST_FOREACH(lpe, n, scenario) {
		if (old != scenario->old || now != scenario->now)
			continue;
		if (!scenario->func)
			continue;
		scenario->func(data);
		found = 1;
		break;
	}
	return found;
}

static int lowbat_add_scenario(int old, int now, int (*func)(void *data))
{
	struct lowbat_process_entry *scenario;

	_I("%d %d, %x", old, now, func);

	if (!func) {
		_E("invalid func address!");
		return -EINVAL;
	}

	scenario = malloc(sizeof(struct lowbat_process_entry));
	if (!scenario) {
		_E("Fail to malloc for notifier!");
		return -ENOMEM;
	}

	scenario->old = old;
	scenario->now = now;
	scenario->func = func;

	DD_LIST_APPEND(lpe, scenario);
	scenario_count++;
	return 0;
}

static void print_lowbat_state(unsigned int bat_percent)
{
#if 0
	int i;
	for (i = 0; i < BAT_MON_SAMPLES; i++)
		_D("\t%d", recent_bat_percent[i]);
#endif
}

static int power_execute(void *data)
{
	static const struct device_ops *ops;

	FIND_DEVICE_INT(ops, POWER_OPS_NAME);

	return ops->execute(data);
}

static int booting_done(void *data)
{
	static int done;
	static int popup;

	if (data == NULL) {
		if (!done)
			popup = 1;
		goto out;
	}
	done = *(int *)data;
	if (!done)
		goto out;
	_I("booting done");
	if (popup) {
		popup = 0;
		lowbat_popup(NULL);
	}
out:
	return done;
}

static void power_off_pm_lock(void)
{
	if (power_off_lock == POWER_OFF_UNLOCK) {
		pm_lock_internal(INTERNAL_LOCK_LOWBAT, LCD_OFF, STAY_CUR_STATE, 0);
		power_off_lock = POWER_OFF_LOCK;
	}
}

static void power_off_pm_unlock(void)
{
	if (power_off_lock == POWER_OFF_LOCK) {
		pm_unlock_internal(INTERNAL_LOCK_LOWBAT, LCD_OFF, PM_SLEEP_MARGIN);
		power_off_lock = POWER_OFF_UNLOCK;
	}
}

static Eina_Bool power_off_cb(void *data)
{
	power_off_pm_unlock();
	power_execute(POWER_POWEROFF);
	return EINA_FALSE;
}

void power_off_timer_start(void)
{
	if (power_off_timer)
		return;
	_I("power off after %d", POWER_OFF_CHECK_TIMER);
	power_off_pm_lock();
	power_off_timer = ecore_timer_add(POWER_OFF_CHECK_TIMER,
				power_off_cb, NULL);
	if (power_off_timer == NULL)
		_E("fail to add battery init timer during booting");
}

void power_off_timer_stop(void)
{
	if (!power_off_timer)
		return;
	_I("cancel power off");
	power_off_pm_unlock();
	ecore_timer_del(power_off_timer);
	power_off_timer = NULL;
}

static int get_lowbat_noti_value(int low_battery_type, char **actvie_noti, char **event_noti)
{
	int len;
	int ret = 1;

	len = strlen(battery.power_source_s);
	if (!strncmp(battery.power_source_s, POWER_SOURCE_NONE, len)) {
		if (low_battery_type == LOW_BATTERY)
			*actvie_noti = "BatteryLowActNotiOn";
		else if (low_battery_type == CRITICAL_BATTERY)
			*actvie_noti = "BatteryCriticalActNotiOn";
#ifdef PROFILE_MOBILE
		if (low_battery_type == LOW_BATTERY)
			*event_noti = "BatteryLowNotiOn";
		else if (low_battery_type == CRITICAL_BATTERY)
			*event_noti = "BatteryCriticalNotiOn";
#else
		if (low_battery_type == LOW_BATTERY)
			*event_noti = NULL;
		else if (low_battery_type == CRITICAL_BATTERY)
			*event_noti = NULL;
#endif
		ret = 0;
	} else if (!strncmp(battery.power_source_s, POWER_SOURCE_AC, len) ||
				!strncmp(battery.power_source_s, POWER_SOURCE_WIRELESS, len)) {
		if (low_battery_type == LOW_BATTERY) {
			*actvie_noti = "BatteryLowActNotiChgOn";
			*event_noti = NULL;
		} else if (low_battery_type == CRITICAL_BATTERY) {
			*actvie_noti = "BatteryCriticalActNotiChgOn";
			*event_noti = NULL;
		}
	} else if (!strncmp(battery.power_source_s, POWER_SOURCE_USB, len)) {
		if (low_battery_type == LOW_BATTERY) {
			*actvie_noti = "BatteryLowActNotiUsbOn";
			*event_noti = NULL;
		} else if (low_battery_type == CRITICAL_BATTERY) {
			*actvie_noti = "BatteryCriticalActNotiUsbOn";
			*event_noti = NULL;
		}
	} else {
		_E("Invalid power resource type(%s)", battery.power_source_s);
		*actvie_noti = NULL;
		*event_noti = NULL;
	}
	_I("actvie_noti=%s event_noti=%s", actvie_noti, event_noti);
	return ret;
}

static int lowbat_popup(char *option)
{
	static int launched_poweroff;
	static int lowbat_popup_option;
	int ret;
	int power_source = 1;
	int r_disturb, s_disturb, r_block, s_block;
	static char *value;
	char *active_noti = NULL, *event_noti = NULL;
	char temp[10] = {NULL};

	if (!option) {
		if (!value)
			return -1;
		else
			goto direct_launch;
	}

	if (strcmp(option, POWER_OFF_BAT_ACT))
		launched_poweroff = 0;

	if (!strcmp(option, WARNING_LOW_BAT_ACT)) {
		power_source = get_lowbat_noti_value(LOW_BATTERY, &active_noti, &event_noti);
		lowbat_popup_option = LOWBAT_OPT_CHECK;
	} else if (!strcmp(option, CRITICAL_LOW_BAT_ACT)) {
		power_source = get_lowbat_noti_value(CRITICAL_BATTERY, &active_noti, &event_noti);
		lowbat_popup_option = LOWBAT_OPT_WARNING;
	} else if (!strcmp(option, POWER_OFF_BAT_ACT)) {
		value = "poweroff";
		lowbat_popup_option = LOWBAT_OPT_POWEROFF;
	} else if (!strcmp(option, CHARGE_ERROR_ACT)) {
		value = "chargeerr";
		lowbat_popup_option = LOWBAT_OPT_CHARGEERR;
	} else if (!strcmp(option, CHARGE_ERROR_LOW_ACT)) {
		value = "chargeerrlow";
		lowbat_popup_option = LOWBAT_OPT_CHARGEERR;
	} else if (!strcmp(option, CHARGE_ERROR_HIGH_ACT)) {
		value = "chargeerrhigh";
		lowbat_popup_option = LOWBAT_OPT_CHARGEERR;
	} else if (!strcmp(option, CHARGE_ERROR_OVP_ACT)) {
		value = "chargeerrovp";
		lowbat_popup_option = LOWBAT_OPT_CHARGEERR;
	} else if (!strcmp(option, CHARGE_CHECK_ACT)) {
		launched_poweroff = 0;
		return 0;
	} else
		return -1;

direct_launch:
	_D("%s", value);
	if (booting_done(NULL)) {

		if (launched_poweroff == 1) {
			_I("will be foreced power off");
			power_execute(POWER_POWEROFF);
			return 0;
		}

		if (lowbat_popup_option == LOWBAT_OPT_POWEROFF)
			launched_poweroff = 1;

		r_disturb = vconf_get_int("memory/shealth/sleep/do_not_disturb", &s_disturb);
		r_block = vconf_get_bool("db/setting/blockmode_wearable", &s_block);
		if ((r_disturb != 0 && r_block != 0) ||
		    (s_disturb == 0 && s_block == 0) ||
		    lowbat_popup_option == LOWBAT_OPT_CHARGEERR)
			pm_change_internal(INTERNAL_LOCK_LOWBAT, LCD_NORMAL);
		else
			_I("block LCD");

		if (lowbat_popup_option == LOWBAT_OPT_CHECK ||
			lowbat_popup_option == LOWBAT_OPT_WARNING) {
			if (event_noti) {
				if (event_noti_on > 0) {
					ret = remove_notification("BatteryLowCriticalNotiOff", event_noti_on);
					if (ret < 0)
						_E("Failed to remove event_noti : %d", ret);
				}

				if (!power_source) {
					ret = add_notification(event_noti, NULL, NULL);
					if (ret < 0)
						_E("Failed to launch event_noti : %d", ret);
					else
						event_noti_on = ret;
				}
			}

			if (active_noti) {
				if (active_noti_on > 0) {
					ret = remove_notification("BatteryLowCriticalNotiOff", active_noti_on);
					if (ret < 0)
						_E("Failed to launch remove active_noti : %d", ret);
				}

				if (!power_source) {
					snprintf(temp, sizeof(temp), "%d", cur_bat_capacity);
					ret = add_notification(active_noti, "s", temp);
				} else
					ret = add_notification(active_noti, NULL, NULL);

				if (ret < 0)
					_E("Failed to launch active noti : %d", ret);
				else
					active_noti_on = ret;
				return ret;
			}
		} else
			return launch_system_app(APP_DEFAULT,
					2, APP_KEY_TYPE, value);
	} else
		_D("boot-animation running yet");

	return 0;
}

static int battery_check_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(CHARGE_CHECK_ACT);
	return 0;
}

static int battery_warning_low_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(WARNING_LOW_BAT_ACT);
	return 0;
}

static int battery_critical_low_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(CRITICAL_LOW_BAT_ACT);
	return 0;
}

int battery_power_off_act(void *data)
{
	lowbat_popup(CRITICAL_LOW_BAT_ACT);
	power_off_timer_start();
	return 0;
}

int battery_charge_err_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(CHARGE_ERROR_ACT);
	return 0;
}

int battery_charge_err_low_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(CHARGE_ERROR_LOW_ACT);
	return 0;
}

int battery_charge_err_high_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(CHARGE_ERROR_HIGH_ACT);
	return 0;
}

int battery_charge_err_ovp_act(void *data)
{
	power_off_timer_stop();
	lowbat_popup(CHARGE_ERROR_OVP_ACT);
	return 0;
}

static void lowbat_scenario_init(void)
{
	lowbat_add_scenario(battery_info.normal, battery_info.warning, battery_warning_low_act);
	lowbat_add_scenario(battery_info.normal, battery_info.critical, battery_critical_low_act);
	lowbat_add_scenario(battery_info.normal, battery_info.poweroff, battery_critical_low_act);
	lowbat_add_scenario(battery_info.normal, battery_info.realoff, battery_power_off_act);
	lowbat_add_scenario(battery_info.warning, battery_info.warning, battery_warning_low_act);
	lowbat_add_scenario(battery_info.warning, battery_info.critical, battery_critical_low_act);
	lowbat_add_scenario(battery_info.warning, battery_info.poweroff, battery_critical_low_act);
	lowbat_add_scenario(battery_info.warning, battery_info.realoff, battery_power_off_act);
	lowbat_add_scenario(battery_info.critical, battery_info.critical, battery_critical_low_act);
	lowbat_add_scenario(battery_info.critical, battery_info.realoff, battery_power_off_act);
	lowbat_add_scenario(battery_info.poweroff, battery_info.poweroff, battery_critical_low_act);
	lowbat_add_scenario(battery_info.poweroff, battery_info.realoff, battery_power_off_act);
	lowbat_add_scenario(battery_info.realoff, battery_info.realoff, battery_power_off_act);
	lowbat_add_scenario(battery_info.realoff, battery_info.normal, battery_check_act);
	lowbat_add_scenario(battery_info.realoff, battery_info.warning, battery_check_act);
	lowbat_add_scenario(battery_info.realoff, battery_info.critical, battery_check_act);
	lowbat_add_scenario(battery_info.realoff, battery_info.poweroff, battery_check_act);
	lowbat_add_scenario(battery_info.realoff, battery_info.realoff, battery_power_off_act);
}

void clear_noti_if_needed(const char *old_string, const char *new_string)
{
	int ret = -1;

	if (old_string == new_string)
		return;

	if (event_noti_on > 0) {
		ret = remove_notification("BatteryLowCriticalNotiOff", event_noti_on);
		if (ret < 0)
			_E("Failed to launch remove_event_noti : %d", ret);
		event_noti_on = -1;
	}

	if (active_noti_on > 0) {
		ret = remove_notification("BatteryLowCriticalNotiOff", active_noti_on);
		if (ret < 0)
			_E("Failed to launch remove_active_noti : %d", ret);
		active_noti_on = -1;
	}
}

static void battery_level_send_system_event(int bat_percent)
{
	bundle *b;
	const char *str;
	static const char *prev;

	if (bat_percent > battery_info.normal)
		str = EVT_VAL_BATTERY_LEVEL_FULL;
	else if (bat_percent > battery_info.warning)
		str = EVT_VAL_BATTERY_LEVEL_HIGH;
	else if (bat_percent > battery_info.critical)
		str = EVT_VAL_BATTERY_LEVEL_LOW;
	else if (bat_percent > battery_info.poweroff)
		str = EVT_VAL_BATTERY_LEVEL_CRITICAL;
	else
		str = EVT_VAL_BATTERY_LEVEL_EMPTY;

	clear_noti_if_needed(prev, str);

	if (prev == str)
		return;

	prev = str;

	_D("system_event(%s)", str);

	b = bundle_create();
	bundle_add_str(b, EVT_KEY_BATTERY_LEVEL_STATUS, str);
	eventsystem_send_system_event(SYS_EVENT_BATTERY_LEVEL_STATUS, b);
	bundle_free(b);
}

static void change_lowbat_level(int bat_percent)
{
	int prev, now;

	if (cur_bat_capacity == bat_percent)
		return;

	if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &prev) < 0) {
		_E("vconf_get_int() failed");
		return;
	}

	if (bat_percent > battery_info.normal)
		now = VCONFKEY_SYSMAN_BAT_LEVEL_FULL;
	else if (bat_percent > battery_info.warning)
		now = VCONFKEY_SYSMAN_BAT_LEVEL_HIGH;
	else if (bat_percent > battery_info.critical)
		now = VCONFKEY_SYSMAN_BAT_LEVEL_LOW;
	else if (bat_percent > battery_info.poweroff)
		now = VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL;
	else
		now = VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY;

	if (prev != now)
		vconf_set_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, now);
}

static int lowbat_process(int bat_percent, void *ad)
{
	static int online;
	int new_bat_capacity;
	int new_bat_state;
	int vconf_state = -1;
	int ret = 0;
	int status = -1;
	bool low_bat = false;
	int result = 0;
	int lock = -1;

	new_bat_capacity = bat_percent;
	if (new_bat_capacity < 0)
		return -EINVAL;
	change_lowbat_level(new_bat_capacity);
	battery_level_send_system_event(new_bat_capacity);

	if (new_bat_capacity != cur_bat_capacity) {
		_D("[BAT_MON] cur = %d new = %d", cur_bat_capacity, new_bat_capacity);
		if (vconf_set_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, new_bat_capacity) == 0)
			cur_bat_capacity = new_bat_capacity;
		power_supply_broadcast(CHARGE_CAPACITY_SIGNAL, new_bat_capacity);
	}

	if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &vconf_state) < 0) {
		_E("vconf_get_int() failed");
		result = -EIO;
		goto exit;
	}

	if (new_bat_capacity <= battery_info.realoff) {
		if (battery.charge_now) {
			new_bat_state = battery_info.poweroff;
			if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
				status = VCONFKEY_SYSMAN_BAT_POWER_OFF;
		} else {
			new_bat_state = battery_info.realoff;
			if (vconf_state != VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF)
				status = VCONFKEY_SYSMAN_BAT_REAL_POWER_OFF;
		}
	} else if (new_bat_capacity <= battery_info.poweroff) {
		new_bat_state = battery_info.poweroff;
		if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
			status = VCONFKEY_SYSMAN_BAT_POWER_OFF;
	} else if (new_bat_capacity <= battery_info.critical) {
		new_bat_state = battery_info.critical;
		if (vconf_state != VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
			status = VCONFKEY_SYSMAN_BAT_CRITICAL_LOW;
	} else if (new_bat_capacity <= battery_info.warning) {
		new_bat_state = battery_info.warning;
		if (vconf_state != VCONFKEY_SYSMAN_BAT_WARNING_LOW)
			status = VCONFKEY_SYSMAN_BAT_WARNING_LOW;
	} else {
		new_bat_state = battery_info.normal;
		if (new_bat_capacity == BATTERY_FULL) {
			if (battery.charge_full) {
				if (vconf_state != VCONFKEY_SYSMAN_BAT_FULL)
					status = VCONFKEY_SYSMAN_BAT_FULL;
			} else {
				if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
					status = VCONFKEY_SYSMAN_BAT_NORMAL;
			}
		} else {
			if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
				status = VCONFKEY_SYSMAN_BAT_NORMAL;
		}
	}

	if (status != -1) {
		lock = pm_lock_internal(INTERNAL_LOCK_BATTERY, LCD_OFF, STAY_CUR_STATE, 0);
		ret = vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, status);
		power_supply_broadcast(CHARGE_LEVEL_SIGNAL, status);
		if (update_pm_setting)
			update_pm_setting(SETTING_LOW_BATT, status);
	}

	if (ret < 0) {
		result = -EIO;
		goto exit;
	}

	if (new_bat_capacity <= battery_info.warning)
		low_bat = true;

	device_notify(DEVICE_NOTIFIER_LOWBAT, (void *)low_bat);

	if (battery.online == POWER_SUPPLY_TYPE_UNKNOWN)
		goto exit;
	if (cur_bat_state == new_bat_state &&
	    online == battery.online)
		goto exit;
	online = battery.online;
	if (cur_bat_state == BATTERY_UNKNOWN)
		cur_bat_state = battery_info.normal;
	result = lowbat_scenario(cur_bat_state, new_bat_state, NULL);
	if (result)
		_I("cur %d, new %d(capacity %d)",
		cur_bat_state, new_bat_state, bat_percent);
	cur_bat_state = new_bat_state;
exit:
	if (lock == 0)
		pm_unlock_internal(INTERNAL_LOCK_BATTERY, LCD_OFF, PM_SLEEP_MARGIN);

	return result;
}

static int check_lowbat_percent(int *pct)
{
	int bat_percent;

	bat_percent = battery.capacity;
	if (bat_percent < 0) {
		_E("[BATMON] Cannot read battery gage. stop read fuel gage");
		return -ENODEV;
	}
	if (bat_percent > 100)
		bat_percent = 100;
	change_lowbat_level(bat_percent);
	battery_level_send_system_event(bat_percent);
	*pct = bat_percent;
	return 0;
}

static void lowbat_monitor(void *data)
{
	int bat_percent, r;

	r = lowbat_initialized(NULL);
	if (!r)
		return;

	if (data == NULL) {
		r = check_lowbat_percent(&bat_percent);
		if (r < 0)
			return;
	} else
		bat_percent = *(int *)data;
	print_lowbat_state(bat_percent);
	lowbat_process(bat_percent, NULL);
}

static int lowbat_monitor_init(void *data)
{
	int status = 1;

	lowbat_initialized(&status);

	/* it's called just this once. */
	unregister_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, lowbat_monitor_init);

	/* load battery configuration file */
	battery_config_load(&battery_info);
	_I("battery conf %d %d %d %d %d", battery_info.normal, battery_info.warning,
		battery_info.critical, battery_info.poweroff, battery_info.realoff);

	lowbat_scenario_init();
	check_lowbat_percent(&battery.capacity);
	lowbat_process(battery.capacity, NULL);
	return 0;
}

static void lowbat_init(void *data)
{
	register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
	register_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, lowbat_monitor_init);
}

static void lowbat_exit(void *data)
{
	int status = 0;

	lowbat_initialized(&status);
	unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
}

static int lowbat_execute(void *data)
{
	lowbat_monitor(data);
	return 0;
}

static const struct device_ops lowbat_device_ops = {
	.name     = "lowbat",
	.init     = lowbat_init,
	.execute  = lowbat_execute,
	.exit     = lowbat_exit,
};

DEVICE_OPS_REGISTER(&lowbat_device_ops)