4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <eventsystem.h>
27 #include <hal/device/hal-battery.h>
28 #include <libsyscommon/libgdbus.h>
29 #include <libsyscommon/list.h>
31 #include "lowbat-handler.h"
32 #include "battery-ops.h"
36 #include "shared/devices.h"
37 #include "shared/device-notifier.h"
38 #include "shared/common.h"
39 #include "shared/apps.h"
40 #include "shared/eventsystem.h"
41 #include "shared/plugin.h"
42 #include "display/setting.h"
43 #include "display/poll.h"
44 #include "display/display-ops.h"
45 #include "power/power.h"
46 #include "power/power-off.h"
47 #include "power-supply.h"
49 #define BATTERY_UNKNOWN -1
50 #define BATTERY_DISABLED 0
51 #define BATTERY_ENABLED 1
53 #define VCONF_KEY_BATTERY_WARNING_LEVEL "db/sysman/battery_warning_level"
55 #define MIN_INOW_VALUE -50
57 enum custom_level_control_type {
63 struct lowbat_process_entry {
66 int (*func) (void *data);
69 static struct display_plugin *disp_plgn;
70 static struct battery_plugin *battery_plgn;
72 static struct battery_status *battery;
74 static int cur_bat_state = BATTERY_UNKNOWN;
75 static int cur_bat_capacity = BATTERY_UNKNOWN;
76 static int custom_warning_level = BATTERY_UNKNOWN;
77 static bool custom_warning_launch;
79 struct battery_config_info battery_info = {
80 .normal = BATTERY_NORMAL,
81 .warning = BATTERY_WARNING,
82 .critical = BATTERY_CRITICAL,
83 .poweroff = BATTERY_POWEROFF,
87 static int scenario_count;
89 static guint low_batt_sig_timer;
91 static int uevent_buffering = 0;
92 static int lowbat_monitor_init(void *data);
94 static int lowbat_initialized(void *data)
96 static int status = BATTERY_UNKNOWN;
101 status = *(int *)data;
105 static int lowbat_scenario(int old, int now, void *data)
108 struct lowbat_process_entry *scenario;
111 if (old == now && battery->charge_now)
113 SYS_G_LIST_FOREACH(lpe, n, scenario) {
114 if (old != scenario->old || now != scenario->now)
118 scenario->func(data);
125 static int lowbat_add_scenario(int old, int now, int (*func)(void *data))
127 struct lowbat_process_entry *scenario;
129 _D("Lowbat_add_scenario: old(%d) now(%d) func(%p)", old, now, func);
132 _E("Invalid func address.");
136 scenario = malloc(sizeof(struct lowbat_process_entry));
138 _E("Failed to malloc for notifier.");
144 scenario->func = func;
146 SYS_G_LIST_APPEND(lpe, scenario);
151 static int power_execute(int state)
154 CRITICAL_LOG("Poweroff by lowbattery is disabled at emulator.");
158 power_request_change_state(state, LOWBAT_POWEROFF_REASON);
163 static int delayed_init_done(void *data)
175 status = lowbat_initialized(NULL);
176 if (status == BATTERY_UNKNOWN)
177 lowbat_monitor_init(NULL);
185 if (battery->charge_now != CHARGER_CHARGING &&
186 battery->charge_full != CHARGING_FULL)
187 lowbat_popup(BAT_OPT_NONE);
189 _I("Skip low battery popup during charging.");
195 int lowbat_popup(int option)
197 static int launched_poweroff;
198 static int lowbat_popup_option;
201 if (option == BAT_OPT_NONE) {
208 if (option == BAT_OPT_POWEROFF)
209 launched_poweroff = 0;
212 case BAT_OPT_CRITICAL:
213 value = "lowbattery_critical";
215 case BAT_OPT_WARNING:
216 value = "lowbattery_warning";
218 case BAT_OPT_POWEROFF:
221 case BAT_OPT_ERR_TEMP_LOW:
222 value = "chargeerrlow";
224 case BAT_OPT_ERR_TEMP_HIGH:
225 value = "chargeerrhigh";
227 case BAT_OPT_ERR_CF_OPEN:
228 value = "battdisconnect";
234 lowbat_popup_option = option;
237 _D("Popup value=%s", value);
238 if (delayed_init_done(NULL)) {
240 if (launched_poweroff == 1) {
241 _I("Will be foreced power off.");
242 power_execute(POWER_STATE_POWEROFF);
246 if (lowbat_popup_option == BAT_OPT_POWEROFF)
247 launched_poweroff = 1;
249 if (battery_do_not_disturb()) {
250 _I("block LCD and %s Popup", value);
253 if (lowbat_popup_option == BAT_OPT_ERR_TEMP_LOW ||
254 lowbat_popup_option == BAT_OPT_ERR_TEMP_HIGH ||
255 lowbat_popup_option == BAT_OPT_ERR_CF_OPEN) {
256 if (disp_plgn->pm_change_internal)
257 disp_plgn->pm_change_internal(INTERNAL_LOCK_LOWBAT, LCD_DIM);
259 if (disp_plgn->pm_change_internal)
260 disp_plgn->pm_change_internal(INTERNAL_LOCK_LOWBAT, LCD_NORMAL);
262 if (lowbat_popup_option == BAT_OPT_ERR_TEMP_LOW ||
263 lowbat_popup_option == BAT_OPT_ERR_TEMP_HIGH ||
264 lowbat_popup_option == BAT_OPT_ERR_CF_OPEN)
265 return launch_system_app(APP_ABNORMAL,
266 2, APP_KEY_TYPE, value);
267 return launch_system_app(APP_DEFAULT,
268 2, APP_KEY_TYPE, value);
270 _D("Boot-animation running yet.");
275 static int battery_warning_low_act(void *data)
277 if (battery_plgn->lowbat_noti_launch)
278 battery_plgn->lowbat_noti_launch(cur_bat_capacity, BAT_OPT_WARNING);
280 (void)lowbat_popup(BAT_OPT_WARNING);
284 static int battery_critical_low_act(void *data)
286 if (battery_plgn->lowbat_noti_launch)
287 battery_plgn->lowbat_noti_launch(cur_bat_capacity, BAT_OPT_CRITICAL);
289 (void)lowbat_popup(BAT_OPT_CRITICAL);
293 static int battery_power_off_act(void *data)
295 CRITICAL_LOG("Low battery power off.");
296 return power_execute(POWER_STATE_POWEROFF);
299 int battery_charge_err_cf_act(void *data)
301 return lowbat_popup(BAT_OPT_ERR_CF_OPEN);
304 int battery_charge_err_low_act(void *data)
306 if (disp_plgn->pm_lock_internal)
307 disp_plgn->pm_lock_internal(INTERNAL_LOCK_OVERCOOL, LCD_OFF, STAY_CUR_STATE, 60000);
308 return lowbat_popup(BAT_OPT_ERR_TEMP_LOW);
311 int battery_charge_err_high_act(void *data)
313 if (disp_plgn->pm_lock_internal)
314 disp_plgn->pm_lock_internal(INTERNAL_LOCK_OVERHEAT, LCD_OFF, STAY_CUR_STATE, 60000);
315 return lowbat_popup(BAT_OPT_ERR_TEMP_HIGH);
318 static void lowbat_scenario_init(void)
322 ret = vconf_get_int(VCONF_KEY_BATTERY_WARNING_LEVEL, &custom_warning_level);
324 _E("Failed to get vconf value for battery warning level: %d", vconf_get_ext_errno());
326 if (custom_warning_level != BATTERY_UNKNOWN)
327 battery_info.warning = custom_warning_level;
329 lowbat_add_scenario(battery_info.normal, battery_info.warning, battery_warning_low_act);
330 lowbat_add_scenario(battery_info.normal, battery_info.critical, battery_critical_low_act);
331 lowbat_add_scenario(battery_info.normal, battery_info.poweroff, battery_power_off_act);
332 lowbat_add_scenario(battery_info.warning, battery_info.warning, battery_warning_low_act);
333 lowbat_add_scenario(battery_info.warning, battery_info.critical, battery_critical_low_act);
334 lowbat_add_scenario(battery_info.warning, battery_info.poweroff, battery_power_off_act);
335 lowbat_add_scenario(battery_info.critical, battery_info.critical, battery_critical_low_act);
336 lowbat_add_scenario(battery_info.critical, battery_info.poweroff, battery_power_off_act);
337 lowbat_add_scenario(battery_info.poweroff, battery_info.poweroff, battery_power_off_act);
340 static void battery_level_send_system_event(int bat_percent)
343 static const char *prev;
345 if (bat_percent >= battery_info.normal && battery->charge_full == CHARGING_FULL)
346 str = EVT_VAL_BATTERY_LEVEL_FULL;
347 else if (bat_percent > battery_info.warning)
348 str = EVT_VAL_BATTERY_LEVEL_HIGH;
349 else if (bat_percent > battery_info.critical)
350 str = EVT_VAL_BATTERY_LEVEL_LOW;
351 else if (bat_percent > battery_info.poweroff)
352 str = EVT_VAL_BATTERY_LEVEL_CRITICAL;
354 str = EVT_VAL_BATTERY_LEVEL_EMPTY;
356 if (battery_plgn->lowbat_noti_clean && prev)
357 battery_plgn->lowbat_noti_clean(prev, str);
364 _D("System_event: %s", str);
365 CRITICAL_LOG("Battery %s.", str);
366 event_system_send(SYS_EVENT_BATTERY_LEVEL_STATUS,
367 EVT_KEY_BATTERY_LEVEL_STATUS, str);
370 static void change_lowbat_level(int bat_percent)
374 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, &prev);
376 _E("Failed to get vconf value for battery level status: %d", vconf_get_ext_errno());
380 if (bat_percent >= battery_info.normal) {
381 if (battery->charge_full == CHARGING_FULL)
382 now = VCONFKEY_SYSMAN_BAT_LEVEL_FULL;
384 now = VCONFKEY_SYSMAN_BAT_LEVEL_HIGH;
385 } else if (bat_percent > battery_info.warning)
386 now = VCONFKEY_SYSMAN_BAT_LEVEL_HIGH;
387 else if (bat_percent > battery_info.critical)
388 now = VCONFKEY_SYSMAN_BAT_LEVEL_LOW;
389 else if (bat_percent > battery_info.poweroff)
390 now = VCONFKEY_SYSMAN_BAT_LEVEL_CRITICAL;
392 now = VCONFKEY_SYSMAN_BAT_LEVEL_EMPTY;
395 ret = vconf_set_int(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS, now);
397 _E("Failed to set vconf value for battery level status: %d", vconf_get_ext_errno());
401 static void lowbat_custom_popup(int online, int capacity)
403 static int custom_type = BATTERY_UNKNOWN;
405 if (custom_type == CUSTOM_CONTROL_NONE)
407 if (custom_warning_level == BATTERY_UNKNOWN ||
408 custom_warning_level == battery_info.warning)
411 if (custom_type == BATTERY_UNKNOWN) {
412 if (custom_warning_level < battery_info.warning)
413 custom_type = LOWER_THAN_WARNING;
414 else if (custom_warning_level > battery_info.warning)
415 custom_type = HIGHER_THAN_WARNING;
417 custom_type = CUSTOM_CONTROL_NONE;
418 _E("Custom type is not defined: %d", custom_warning_level);
423 if (custom_type == LOWER_THAN_WARNING && capacity <= battery_info.critical)
425 if (custom_type == HIGHER_THAN_WARNING && capacity <= battery_info.warning)
428 if (capacity > custom_warning_level) {
429 custom_warning_launch = false;
432 if (online == battery->charger_connected && custom_warning_launch)
434 if (battery->charger_connected == 1 && custom_warning_launch)
437 custom_warning_launch = true;
438 _I("Launch custom lowbattery warning popup. online(%d, %d), capacity(%d, %d)", online, battery->charger_connected, capacity, custom_warning_level);
439 (void)battery_warning_low_act(NULL);
442 void check_extreme_status(int status)
447 if (status <= VCONFKEY_SYSMAN_BAT_POWER_OFF ||
448 status > VCONFKEY_SYSMAN_BAT_FULL)
450 ret = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &extreme);
452 _E("Failed to get vconf value for pm key ignore: %d", vconf_get_ext_errno());
453 if (ret != 0 || extreme != 1)
456 ret = vconf_set_int(VCONFKEY_PM_KEY_IGNORE, 0);
458 _E("Failed to set vconf value for pm key ignore: %d", vconf_get_ext_errno());
461 _I("Release key ignore.");
464 static int lowbat_process(int bat_percent, void *ad)
467 static bool low_bat_old;
468 static int low_bat_skip_cnt = 0;
469 int new_bat_capacity;
471 int vconf_state = -1;
474 bool low_bat = false;
477 if (!battery_initialized)
480 new_bat_capacity = bat_percent;
481 change_lowbat_level(new_bat_capacity);
482 lowbat_custom_popup(online, new_bat_capacity);
483 battery_level_send_system_event(new_bat_capacity);
485 cur_bat_capacity = new_bat_capacity;
487 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &vconf_state);
489 _E("Failed to get vconf value for battery status low: %d", vconf_get_ext_errno());
493 if (new_bat_capacity <= battery_info.poweroff) {
494 new_bat_state = battery_info.poweroff;
495 if (vconf_state != VCONFKEY_SYSMAN_BAT_POWER_OFF)
496 status = VCONFKEY_SYSMAN_BAT_POWER_OFF;
497 } else if (new_bat_capacity <= battery_info.critical) {
498 new_bat_state = battery_info.critical;
499 if (vconf_state != VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
500 status = VCONFKEY_SYSMAN_BAT_CRITICAL_LOW;
501 } else if (new_bat_capacity <= battery_info.warning) {
502 new_bat_state = battery_info.warning;
503 if (vconf_state != VCONFKEY_SYSMAN_BAT_WARNING_LOW)
504 status = VCONFKEY_SYSMAN_BAT_WARNING_LOW;
506 new_bat_state = battery_info.normal;
507 if (new_bat_capacity == BATTERY_FULL) {
508 if (battery->charge_full) {
509 if (vconf_state != VCONFKEY_SYSMAN_BAT_FULL)
510 status = VCONFKEY_SYSMAN_BAT_FULL;
512 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
513 status = VCONFKEY_SYSMAN_BAT_NORMAL;
516 if (vconf_state != VCONFKEY_SYSMAN_BAT_NORMAL)
517 status = VCONFKEY_SYSMAN_BAT_NORMAL;
521 /* If the battery continues to run out even though it is being charged
522 * for a certain period of time, turn off the device. */
523 if (new_bat_capacity <= battery_info.poweroff && battery->charge_now == CHARGER_CHARGING) {
524 if (low_bat_skip_cnt >= RETRY_MAX) {
525 _I("Go to real poweroff inspite of charging (c:%d charge:%d online:%d current_now:%d)",
526 battery->capacity, battery->charge_now, battery->charger_connected, battery->current_now);
527 } else if (battery->current_now <= MIN_INOW_VALUE) {
530 low_bat_skip_cnt = 0;
533 low_bat_skip_cnt = 0;
538 if (disp_plgn->update_pm_setting)
539 disp_plgn->update_pm_setting(SETTING_LOW_BATT, status);
541 result = vconf_state;
543 check_extreme_status(status);
545 if (new_bat_capacity <= battery_info.warning)
548 if (low_bat_old != low_bat) {
549 device_notify(DEVICE_NOTIFIER_LOWBAT, (void *)&low_bat);
550 low_bat_old = low_bat;
553 if (battery->charger_connected == -1)
555 if (cur_bat_state == new_bat_state && online == battery->charger_connected)
557 if (new_bat_state <= battery_info.poweroff && battery->charger_connected && low_bat_skip_cnt < RETRY_MAX)
560 if (cur_bat_state == BATTERY_UNKNOWN)
561 cur_bat_state = battery_info.normal;
562 if (lowbat_scenario(cur_bat_state, new_bat_state, NULL))
563 _I("Cur(%d) new(%d) capacity(%d).", cur_bat_state, new_bat_state, bat_percent);
565 online = battery->charger_connected;
566 cur_bat_state = new_bat_state;
571 static int check_lowbat_percent(int *pct)
575 bat_percent = battery->capacity;
578 if (bat_percent > 100)
580 change_lowbat_level(bat_percent);
581 battery_level_send_system_event(bat_percent);
586 static int lowbat_monitor(void *data)
590 r = lowbat_initialized(NULL);
591 if (r != BATTERY_ENABLED)
592 return battery->capacity_level;
595 r = check_lowbat_percent(&bat_percent);
597 return battery->capacity_level;
599 bat_percent = *(int *)data;
600 return lowbat_process(bat_percent, NULL);
603 static int lowbat_monitor_init(void *data)
607 status = lowbat_initialized(NULL);
608 if (status != BATTERY_UNKNOWN)
611 status = BATTERY_ENABLED;
612 lowbat_initialized(&status);
614 /* it's called just this once. */
615 unregister_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, lowbat_monitor_init);
617 /* load battery configuration file */
618 battery_config_load(&battery_info);
619 _I("Battery conf: %d %d %d %d", battery_info.normal, battery_info.warning,
620 battery_info.critical, battery_info.poweroff);
622 lowbat_scenario_init();
623 check_lowbat_percent(&battery->capacity);
624 lowbat_process(battery->capacity, NULL);
629 static GVariant * dbus_set_lowbat_level(GDBusConnection *conn,
630 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
631 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
637 g_variant_get(param, "(i)", &now);
639 status = lowbat_initialized(NULL);
640 if (status != BATTERY_ENABLED) {
641 _E("Invalid lowbat handler.");
646 if (now <= battery_info.warning && now != BATTERY_UNKNOWN) {
647 _E("Invalid level is requested: %d", now);
652 custom_warning_level = now;
653 custom_warning_launch = false;
655 ret = vconf_set_int(VCONF_KEY_BATTERY_WARNING_LEVEL, custom_warning_level);
657 _E("Failed to set vconf value for battery warning level: %d", vconf_get_ext_errno());
658 _D("Custom warning level is changed to %d.", custom_warning_level);
661 return g_variant_new("(i)", ret);
664 static GVariant * dbus_get_lowbat_level(GDBusConnection *conn,
665 const gchar *sender, const gchar *path, const gchar *iface, const gchar *name,
666 GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data)
668 _D("Warning level: %d", custom_warning_level);
669 return g_variant_new("(i)", custom_warning_level);
672 static const dbus_method_s dbus_methods[] = {
673 { "SetLowbatLevel", "i", "i", dbus_set_lowbat_level },
674 { "GetLowbatLevel", NULL, "i", dbus_get_lowbat_level },
675 /* Add methods here */
678 static const dbus_interface_u dbus_interface = {
680 .name = DEVICED_INTERFACE_BATTERY,
681 .methods = dbus_methods,
682 .nr_methods = ARRAY_SIZE(dbus_methods),
685 static void lowbat_init(void *data)
689 register_notifier(DEVICE_NOTIFIER_DELAYED_INIT, delayed_init_done);
690 register_notifier(DEVICE_NOTIFIER_POWER_SUPPLY, lowbat_monitor_init);
692 ret = gdbus_add_object(NULL, DEVICED_PATH_BATTERY, &dbus_interface);
694 _E("Failed to init dbus method: %d", ret);
697 static void lowbat_exit(void *data)
699 int status = BATTERY_DISABLED;
701 lowbat_initialized(&status);
702 if (low_batt_sig_timer) {
703 g_source_remove(low_batt_sig_timer);
704 low_batt_sig_timer = 0;
708 static gboolean low_battery_charging_status(void *data)
710 low_batt_sig_timer = 0;
711 battery->capacity_level = lowbat_monitor(data);
712 if (battery->capacity_level > 0 && old_battery.capacity_level != battery->capacity_level) {
713 if (vconf_set_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, battery->capacity_level) < 0)
714 _E("Fail to set level.");
715 if (power_supply_broadcast(CHARGE_LEVEL_SIGNAL, battery->capacity_level) < 0)
716 _E("power_supply_broadcast failed");
718 return G_SOURCE_REMOVE;
721 static int lowbat_execute(void *data)
723 int capacity = *(int *)data;
725 /* In battery kernel side, it has a requirement that battery charging event must get quickly.
726 * So deviced receives the discharging and charging event within 1.5 sec,
727 * added a timer to wait for the second signal, if there is no second signal then execute
728 * the lowbat_execute.
730 if (low_batt_sig_timer) {
731 g_source_remove(low_batt_sig_timer);
732 low_batt_sig_timer = 0;
735 /* Do lowbat_process immediately rather deferring it when poweroff is needed.
736 * This prevents poweroff from being delayed infinitely when the uevent continues
737 * to occur shorter than 1.5 seconds on poweroff capacity */
738 if (capacity <= battery_info.poweroff || !uevent_buffering)
739 low_battery_charging_status(data);
741 low_batt_sig_timer = g_timeout_add(1500, low_battery_charging_status, data);
746 void lowbat_enable_uevent_buffering(void)
748 uevent_buffering = 1;
751 void lowbat_disable_uevent_buffering(void)
753 uevent_buffering = 0;
756 static const struct device_ops lowbat_device_ops = {
757 DECLARE_NAME_LEN("lowbat"),
759 .execute = lowbat_execute,
763 DEVICE_OPS_REGISTER(&lowbat_device_ops)
765 static void __CONSTRUCTOR__ initialize(void)
767 disp_plgn = get_var_display_plugin();
769 _E("Failed to get display plugin variable.");
771 battery_plgn = get_var_battery_plugin();
773 _E("Failed to get battery plugin variable.");
775 battery = get_var_battery_status();
777 _E("Failed to get battery status structure variable.");