Apply consistent log messages.
[platform/core/system/system-popup.git] / src / battery / battery-wearable.c
1 /*
2  *  system-popup
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
20 #include "popup-common.h"
21
22 #define DEVICED_PATH_SYSNOTI        "/Org/Tizen/System/DeviceD/SysNoti"
23 #define DEVICED_INTERFACE_SYSNOTI   "org.tizen.system.deviced.SysNoti"
24 #define SIGNAL_CHARGEERR_RESPONSE   "ChargeErrResponse"
25
26 static const struct popup_ops lowbattery_poweroff_ops;
27 static const struct popup_ops charge_error_low_ops;
28 static const struct popup_ops charge_error_high_ops;
29 static const struct popup_ops battery_disconnected_ops;
30
31 #define DEVICED_BUS_NAME        "org.tizen.system.deviced"
32 #define REBOOT_OBJECT_PATH     "/Org/Tizen/System/DeviceD/Reboot"
33 #define REBOOT_INTERFACE_NAME  DEVICED_BUS_NAME".reboot"
34
35 #define REBOOT_METHOD            "reboot"
36 #define REBOOT_OPERATION_OFF     "poweroff"
37
38 static int lowbattery_launch(bundle *b, const struct popup_ops *ops);
39
40 static int remove_other_lowbattery_popups(const struct popup_ops *ops)
41 {
42         if (ops != &lowbattery_poweroff_ops)
43                 unload_simple_popup(&lowbattery_poweroff_ops);
44
45         return 0;
46 }
47
48 static int remove_other_charge_popups(bundle *b, const struct popup_ops *ops)
49 {
50         if (ops != &charge_error_low_ops)
51                 unload_simple_popup(&charge_error_low_ops);
52
53         if (ops != &charge_error_high_ops)
54                 unload_simple_popup(&charge_error_high_ops);
55
56         if (ops != &battery_disconnected_ops)
57                 unload_simple_popup(&battery_disconnected_ops);
58
59         return 0;
60 }
61
62 static void charger_status_changed(keynode_t *key, void *data)
63 {
64         int status;
65         const struct popup_ops *ops = data;
66
67         status = vconf_keynode_get_int(key);
68         if (status != VCONFKEY_SYSMAN_CHARGER_CONNECTED)
69                 return;
70
71         if (vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
72                                 charger_status_changed) < 0)
73                 _E("Failed to release vconf key handler.");
74
75         unload_simple_popup(ops);
76
77         terminate_if_no_popup();
78 }
79
80 static void battery_status_changed(keynode_t *key, void *data)
81 {
82         int status;
83         const struct popup_ops *ops = data;
84
85         status = vconf_keynode_get_int(key);
86         if (status < VCONFKEY_SYSMAN_BAT_LEVEL_HIGH)
87                 return;
88
89         if (vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
90                                 battery_status_changed) < 0)
91                 _E("Failed to release vconf key handler.");
92
93         unload_simple_popup(ops);
94
95         terminate_if_no_popup();
96 }
97
98 static void unregister_charger_status_handler(void)
99 {
100         vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
101                                 charger_status_changed);
102 }
103
104 static void unregister_battery_status_handler(void)
105 {
106         vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
107                                 battery_status_changed);
108 }
109
110 static void register_charger_status_handler(const struct popup_ops *ops)
111 {
112         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
113                                 charger_status_changed, (void *)ops) < 0)
114                 _E("Failed to register vconf key handler.");
115 }
116
117 static void register_battery_status_handler(const struct popup_ops *ops)
118 {
119         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_LEVEL_STATUS,
120                                 battery_status_changed, (void *)ops) < 0)
121                 _E("Failed to register vconf key handler.");
122 }
123
124 static int lowbattery_launch(bundle *b, const struct popup_ops *ops)
125 {
126         unregister_charger_status_handler();
127         unregister_battery_status_handler();
128         remove_other_lowbattery_popups(ops);
129         register_charger_status_handler(ops);
130         register_battery_status_handler(ops);
131
132         return 0;
133 }
134
135 static void lowbattery_terminate(const struct popup_ops *ops)
136 {
137         unregister_charger_status_handler();
138         unregister_battery_status_handler();
139 }
140
141 static void poweroff_clicked(const struct popup_ops *ops)
142 {
143         static int bPowerOff = 0;
144         char *param[2];
145         char data[8];
146         int ret;
147
148         if (bPowerOff == 1)
149                 return;
150         bPowerOff = 1;
151
152         unload_simple_popup(ops);
153
154         param[0] = REBOOT_OPERATION_OFF;
155         snprintf(data, sizeof(data), "0");
156         param[1] = data;
157         ret = popup_dbus_method_sync(DEVICED_BUS_NAME,
158                         REBOOT_OBJECT_PATH,
159                         REBOOT_INTERFACE_NAME,
160                         REBOOT_METHOD,
161                         "si", param);
162         if (ret < 0)
163                 _E("Failed to request poweroff to deviced: %d", ret);
164 }
165
166 static void charge_error_ok_clicked(const struct popup_ops *ops)
167 {
168         _I("OK is selected.");
169         unload_simple_popup(ops);
170
171         if (broadcast_dbus_signal(DEVICED_PATH_SYSNOTI,
172                                 DEVICED_INTERFACE_SYSNOTI,
173                                 SIGNAL_CHARGEERR_RESPONSE,
174                                 NULL, NULL) < 0)
175                 _E("Failed to send signal.");
176
177         terminate_if_no_popup();
178 }
179
180 static int remove_battery_popups(bundle *b, const struct popup_ops *ops)
181 {
182         _I("Remove battery related popups.");
183         unload_simple_popup(&lowbattery_poweroff_ops);
184         unload_simple_popup(&charge_error_low_ops);
185         unload_simple_popup(&charge_error_high_ops);
186         unload_simple_popup(&battery_disconnected_ops);
187         terminate_if_no_popup();
188         return 0;
189 }
190
191 static const struct popup_ops lowbattery_poweroff_ops = {
192         .name           = "lowbattery_poweroff",
193         .pattern        = FEEDBACK_PATTERN_LOWBATT,
194         .show   = load_simple_popup,
195         .content        = "IDS_COM_POP_LOW_BATTERY_PHONE_WILL_SHUT_DOWN",
196         .left_text      = "IDS_COM_SK_OK",
197         .left           = poweroff_clicked,
198         .pre            = lowbattery_launch,
199         .terminate  = lowbattery_terminate,
200 };
201
202 static const struct popup_ops battery_remove_ops = {
203         .name           = "remove_battery_popups",
204         .show   = remove_battery_popups,
205 };
206
207 static const struct popup_ops charge_error_low_ops = {
208         .name           = "chargeerrlow",//"charge_error_low",
209         .pattern        = FEEDBACK_PATTERN_LOWBATT,
210         .show   = load_simple_popup,
211         .content        = "IDS_QP_BODY_CHARGING_PAUSED_BATTERY_TEMPERATURE_TOO_LOW",
212         .left_text      = "IDS_COM_SK_OK",
213         .left           = charge_error_ok_clicked,
214         .pre            = remove_other_charge_popups,
215 };
216
217 static const struct popup_ops charge_error_high_ops = {
218         .name           = "chargeerrhigh",//"charge_error_high",
219         .pattern        = FEEDBACK_PATTERN_LOWBATT,
220         .show   = load_simple_popup,
221         .content        = "IDS_QP_BODY_CHARGING_PAUSED_BATTERY_TEMPERATURE_TOO_HIGH",
222         .left_text      = "IDS_COM_SK_OK",
223         .left           = charge_error_ok_clicked,
224         .pre            = remove_other_charge_popups,
225 };
226
227 static const struct popup_ops battery_disconnected_ops = {
228         .name           = "battdisconnect",//"battery_disconnected",
229         .pattern        = FEEDBACK_PATTERN_LOWBATT,
230         .show   = load_simple_popup,
231         .content        = "IDS_COM_POP_BATTERY_DISCONNECTED_ABB",
232         .left_text      = "IDS_COM_SK_OK",
233 };
234
235 /* Constructor to register lowbattery button */
236 static __attribute__ ((constructor)) void battery_register_popup(void)
237 {
238         register_popup(&lowbattery_poweroff_ops);
239         register_popup(&battery_remove_ops);
240         register_popup(&charge_error_low_ops);
241         register_popup(&charge_error_high_ops);
242         register_popup(&battery_disconnected_ops);
243 }