battery: Relocate remove_health_popup() from mobile plugin
[platform/core/system/deviced.git] / plugins / mobile / battery / battery-notification.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <libsyscommon/libgdbus.h>
23 #include <libsyscommon/resource-manager.h>
24 #include <system/syscommon-plugin-deviced-common-interface.h>
25 #include <system/syscommon-plugin-deviced-display-interface.h>
26
27 #include <libsyscommon/log.h>
28 #include "battery.h"
29 #include "battery-parser.h"
30 #include "power-supply.h"
31 #include "lowbat-handler.h"
32 #include "battery-ops.h"
33 #include "core.h"
34 #include "display-lock.h"
35 #include "display-ops.h"
36 #include "display-state-transition.h"
37 #include "poll.h"
38 #include "shared/eventsystem.h"
39 #include "shared/plugin.h"
40 #include "shared/device-notifier.h"
41 #include "shared/apps.h"
42
43 #define REMOVE_POPUP        "remove_battery_popups"
44
45 static struct display_plugin *disp_plgn;
46 static struct battery_status *battery;
47 static guint abnormal_timer;
48
49 static int check_power_supply_noti(void)
50 {
51         return 1;
52 }
53
54 static void health_timer_reset(void)
55 {
56         abnormal_timer = 0;
57 }
58
59 static gboolean health_timer_cb(void *data)
60 {
61         health_timer_reset();
62
63         if (battery->health != HEALTH_LOW && battery->health != HEALTH_HIGH)
64                 return G_SOURCE_REMOVE;
65
66         CRITICAL_LOG("Popup: Battery health status is not good, %s.", battery->health_s);
67         syscommon_notifier_emit_notify(DEVICED_NOTIFIER_BATTERY_HEALTH, (void *)&battery->health);
68         battery_pm_change_internal(DEVICED_EVENT_MISC_POPUP, LCD_DIM);
69         display_lock_request_unlock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_OFF, PM_SLEEP_MARGIN);
70         display_lock_request_lock_with_option(DEVICED_EVENT_MISC_POPUP, LCD_DIM, STAY_CUR_STATE, 0);
71         if (battery->health == HEALTH_LOW)
72                 battery_charge_err_low_act(NULL);
73         else if (battery->health == HEALTH_HIGH)
74                 battery_charge_err_high_act(NULL);
75         return G_SOURCE_REMOVE;
76 }
77
78 static void abnormal_popup_dbus_signal_handler(GDBusConnection  *conn,
79                 const gchar      *sender,
80                 const gchar      *path,
81                 const gchar      *iface,
82                 const gchar      *name,
83                 GVariant         *param,
84                 gpointer          user_data)
85
86 {
87         if (battery->health == HEALTH_GOOD)
88                 return;
89
90         _I("Restart health timer.");
91         abnormal_timer = g_timeout_add_seconds(ABNORMAL_CHECK_TIMER_INTERVAL,
92                                                 health_timer_cb, NULL);
93         if (abnormal_timer == 0)
94                 _E("Failed to add abnormal check timer.");
95 }
96
97 static void battery_notification_init(void *data)
98 {
99         struct battery_plugin *plugin = (struct battery_plugin *)data;
100         int ret;
101
102         if (!plugin)
103                 return;
104
105         _D("Add plugins for battery notification.");
106         plugin->check_power_supply_noti = check_power_supply_noti;
107
108         ret = gdbus_signal_subscribe(NULL, DEVICED_PATH_SYSNOTI,
109                 DEVICED_INTERFACE_SYSNOTI, SIGNAL_CHARGEERR_RESPONSE, abnormal_popup_dbus_signal_handler, NULL, NULL);
110         if (ret <= 0)
111                 _E("Failed to init dbus signal: %d", ret);
112 }
113
114 static const struct battery_ops battery_notification_ops = {
115         .name     = "battery_notification",
116         .init     = battery_notification_init,
117 };
118
119 BATTERY_OPS_REGISTER(&battery_notification_ops)
120
121 static void __CONSTRUCTOR__ initialize(void)
122 {
123         disp_plgn = get_var_display_plugin();
124         if (!disp_plgn)
125                 _E("Failed to get display plugin variable.");
126
127         battery = get_var_battery_status();
128         if (!battery)
129                 _E("Failed to get battery status structure.");
130 }