Initialize Tizen 2.3
[framework/system/deviced.git] / src / auto-test / power-supply.c
1 /*
2  * test
3  *
4  * Copyright (c) 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 "test.h"
20
21 #define S_ENTER 1
22 #define S_LEAVE 0
23
24 #define SIGNAL_CHARGE_NOW "ChargeNow"
25 #define SIGNAL_CHARGER_STATUS "ChargerStatus"
26 #define SIGNAL_TEMP_GOOD "TempGood"
27
28 static E_DBus_Signal_Handler *edbus_charge_now_handler;
29 static E_DBus_Signal_Handler *edbus_charger_status_handler;
30 static E_DBus_Signal_Handler *edbus_temp_good_handler;
31 static E_DBus_Connection     *edbus_conn;
32
33 static struct power_supply_type {
34         char *scenario;
35         int status;
36         char *capacity;
37         char *charge_status;
38         char *health;
39         char *online;
40         char *present;
41         char *name;
42 } power_supply_types [] = {
43         {"norm", S_ENTER, "100","Charging",    "Good", "2", "1", "CHARGE"},
44         {"norm", S_ENTER, "100","Discharging", "Good", "1", "1", "DISCHARGE"},
45         {"norm", S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
46
47         {"heat1",S_ENTER, "100","Discharging", "Overheat", "1", "1", NULL},
48         {"heat1",S_ENTER, "100","Not charging","Overheat", "2", "1", "HEALTH(H) BEFORE CHARGE"},
49         {"heat1",S_LEAVE, "100","Discharging", "Overheat", "1", "1", "DISCHARGE"},
50         {"heat1",S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
51
52         {"heat2",S_ENTER, "100","Charging",    "Good", "2", "1", NULL},
53         {"heat2",S_ENTER, "100","Not charging","Overheat", "2", "1", "HEALTH(H) AFTER CHARGE"},
54         {"heat2",S_LEAVE, "100","Discharging", "Overheat", "1", "1", "DISCHARGE"},
55         {"heat2",S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
56
57         {"cold1",S_ENTER, "100","Discharging", "Cold", "1", "1", NULL},
58         {"cold1",S_ENTER, "100","Not charging","Cold", "2", "1", "HEALTH(L) BEFORE CHARGE"},
59         {"cold1",S_LEAVE, "100","Discharging", "Cold", "1", "1", "DISCHARGE"},
60         {"cold1",S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
61
62         {"cold2",S_ENTER, "100","Charging",    "Good", "2", "1", NULL},
63         {"cold2",S_ENTER, "100","Not charging","Cold", "2", "1", "HEALTH(L) AFTER CHARGE"},
64         {"cold2",S_LEAVE, "100","Discharging", "Cold", "1", "1", "DISCHARGE"},
65         {"cold2",S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
66
67         {"ovp",  S_ENTER, "100","Discharging", "Over voltage", "1", "1", "OVP"},
68         {"ovp",  S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
69
70         {"pres1",S_ENTER, "100","Discharging", "Good", "1", "0", NULL},
71         {"pres1",S_ENTER, "100","Not charging","Good", "2", "0", "PRESENT BEFORE CHARGE"},
72         {"pres1",S_LEAVE, "100","Discharging", "Good", "1", "0", "DISCHARGE"},
73         {"pres1",S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
74
75         {"pres2",S_ENTER, "100","Charging",    "Good", "2", "1", NULL},
76         {"pres2",S_ENTER, "100","Not charging","Good", "2", "0", "PRESENT AFTER CHARGE"},
77         {"pres2",S_LEAVE, "100","Discharging", "Good", "1", "0", "DISCHARGE"},
78         {"pres2",S_LEAVE, "100","Discharging", "Good", "1", "1", NULL}, //init
79
80         {"bat15",S_ENTER, "15", "Discharging", "Good", "1", "1", "LOWBAT 15%"}, //lowbat 15%
81         {"bat15",S_LEAVE, "15", "Charging",    "Good", "2", "1", "LOWBAT 15%"},
82         {"bat5", S_ENTER, "5",  "Discharging", "Good", "1", "1", "LOWBAT 5%"},  //lowbat 5%
83         {"bat5", S_LEAVE, "5",  "Charging",    "Good", "2", "1", "LOWBAT 5%"},
84         {"bat3", S_ENTER, "3",  "Discharging", "Good", "1", "1", "LOWBAT 3%"},  //lowbat 3%
85         {"bat3", S_LEAVE, "3",  "Charging",    "Good", "2", "1", "LOWBAT 3%"},
86         {"bat1", S_ENTER, "1",  "Discharging", "Good", "1", "1", "LOWBAT 1%"},  //lowbat 1%
87         {"bat1", S_LEAVE, "1",  "Charging",    "Good", "2", "1", "LOWBAT 1%"},
88
89         {"ta",   S_ENTER, "100","Charging",    "Good", "2", "1", "CHARGE"},   //charging
90         {"ta",   S_LEAVE, "100","Discharging", "Good", "1", "1", "DISCHARGE"},//discharging
91         {"capacity",   S_ENTER, "100","Discharging", "Good", "1", "1", "CAPACITY"},//charging
92         {"capacity",   S_LEAVE, "100","Charging", "Good", "2", "1", "CAPACITY"},//discharging
93 };
94
95 static void unregister_edbus_signal_handler(void)
96 {
97         e_dbus_signal_handler_del(edbus_conn, edbus_charge_now_handler);
98         e_dbus_signal_handler_del(edbus_conn, edbus_charger_status_handler);
99         e_dbus_signal_handler_del(edbus_conn, edbus_temp_good_handler);
100         e_dbus_connection_close(edbus_conn);
101         e_dbus_shutdown();
102 }
103
104 static void power_supply_changed(void *data, DBusMessage *msg)
105 {
106         DBusError err;
107         int val;
108         int r;
109
110         _I("edbus signal Received");
111
112         r = dbus_message_is_signal(msg, DEVICED_INTERFACE_BATTERY, SIGNAL_CHARGE_NOW);
113         if (!r) {
114                 _E("dbus_message_is_signal error");
115                 return;
116         }
117
118         _I("%s - %s", DEVICED_INTERFACE_BATTERY, SIGNAL_CHARGE_NOW);
119
120         dbus_error_init(&err);
121         r = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
122         if (!r) {
123                 _E("dbus_message_get_args error");
124                 return;
125         }
126         _I("receive data : %d", val);
127 }
128
129 static int register_charge_now_handler(void)
130 {
131         int ret;
132
133         e_dbus_init();
134
135         edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
136         if (!(edbus_conn)) {
137                 ret = -ECONNREFUSED;
138                 goto edbus_handler_out;
139         }
140
141         edbus_charge_now_handler = e_dbus_signal_handler_add(edbus_conn, NULL, DEVICED_PATH_BATTERY,
142                         DEVICED_INTERFACE_BATTERY, SIGNAL_CHARGE_NOW, power_supply_changed, NULL);
143         if (!(edbus_charge_now_handler)) {
144                 ret = -ECONNREFUSED;
145                 goto edbus_handler_connection_out;
146         }
147         return 0;
148
149 edbus_handler_connection_out:
150         e_dbus_connection_close(edbus_conn);
151 edbus_handler_out:
152         e_dbus_shutdown();
153         return ret;
154 }
155
156 static void charger_status_changed(void *data, DBusMessage *msg)
157 {
158         DBusError err;
159         int val;
160         int r;
161
162         _I("edbus signal Received");
163
164         r = dbus_message_is_signal(msg, DEVICED_INTERFACE_BATTERY, SIGNAL_CHARGER_STATUS);
165         if (!r) {
166                 _E("dbus_message_is_signal error");
167                 return;
168         }
169
170         _I("%s - %s", DEVICED_INTERFACE_BATTERY, SIGNAL_CHARGER_STATUS);
171
172         dbus_error_init(&err);
173         r = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
174         if (!r) {
175                 _E("dbus_message_get_args error");
176                 return;
177         }
178         _I("receive data : %d", val);
179 }
180
181 static int register_charger_status_handler(void)
182 {
183         int ret;
184
185         e_dbus_init();
186
187         edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
188         if (!(edbus_conn)) {
189                 ret = -ECONNREFUSED;
190                 goto edbus_handler_out;
191         }
192
193         edbus_charger_status_handler = e_dbus_signal_handler_add(edbus_conn, NULL, DEVICED_PATH_BATTERY,
194                         DEVICED_INTERFACE_BATTERY, SIGNAL_CHARGER_STATUS, charger_status_changed, NULL);
195         if (!(edbus_charger_status_handler)) {
196                 ret = -ECONNREFUSED;
197                 goto edbus_handler_connection_out;
198         }
199         return 0;
200
201 edbus_handler_connection_out:
202         e_dbus_connection_close(edbus_conn);
203 edbus_handler_out:
204         e_dbus_shutdown();
205         return ret;
206 }
207
208 static void temp_status_changed(void *data, DBusMessage *msg)
209 {
210         DBusError err;
211         int val;
212         int r;
213
214         _I("edbus signal Received");
215
216         r = dbus_message_is_signal(msg, DEVICED_INTERFACE_BATTERY, SIGNAL_TEMP_GOOD);
217         if (!r) {
218                 _E("dbus_message_is_signal error");
219                 return;
220         }
221
222         _I("%s - %s", DEVICED_INTERFACE_BATTERY, SIGNAL_TEMP_GOOD);
223 }
224
225 static int register_temp_good_handler(void)
226 {
227         int ret;
228
229         e_dbus_init();
230
231         edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
232         if (!(edbus_conn)) {
233                 ret = -ECONNREFUSED;
234                 goto edbus_handler_out;
235         }
236
237         edbus_temp_good_handler = e_dbus_signal_handler_add(edbus_conn, NULL, DEVICED_PATH_BATTERY,
238                         DEVICED_INTERFACE_BATTERY, SIGNAL_TEMP_GOOD, temp_status_changed, NULL);
239         if (!(edbus_temp_good_handler)) {
240                 ret = -ECONNREFUSED;
241                 goto edbus_handler_connection_out;
242         }
243         return 0;
244
245 edbus_handler_connection_out:
246         e_dbus_connection_close(edbus_conn);
247 edbus_handler_out:
248         e_dbus_shutdown();
249         return ret;
250 }
251 static void test_signal(void)
252 {
253         _I("test");
254         register_charge_now_handler();
255         register_charger_status_handler();
256         register_temp_good_handler();
257         ecore_main_loop_begin();
258 }
259
260 static int test(int index)
261 {
262         DBusError err;
263         DBusMessage *msg;
264         int ret, ret_val;
265         char *param[7];
266
267         param[0] = POWER_SUBSYSTEM;
268         param[1] = "5";
269         param[2] = power_supply_types[index].capacity;
270         param[3] = power_supply_types[index].charge_status;
271         param[4] = power_supply_types[index].health;
272         param[5] = power_supply_types[index].online;
273         param[6] = power_supply_types[index].present;
274
275         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
276                         DEVICED_PATH_SYSNOTI,
277                         DEVICED_INTERFACE_SYSNOTI,
278                         POWER_SUBSYSTEM, "sisssss", param);
279         if (!msg) {
280                 _E("fail : %s %s %s %s",
281                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
282                         POWER_SUBSYSTEM);
283                 return -EBADMSG;
284         }
285
286         dbus_error_init(&err);
287
288         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
289         if (ret == 0) {
290                 _E("no message : [%s:%s]", err.name, err.message);
291                 dbus_error_free(&err);
292                 ret_val = -EBADMSG;
293         }
294
295         if (power_supply_types[index].name != NULL)
296                 _I("++++++++++[START] %s ++++++++++", power_supply_types[index].name);
297         _I("CAPACITY(%s , %s) P(%s) STATUS(%s) HEALTH(%s)",
298                 power_supply_types[index].capacity,
299                 power_supply_types[index].online,
300                 power_supply_types[index].present,
301                 power_supply_types[index].charge_status,
302                 power_supply_types[index].health);
303         if (power_supply_types[index].name != NULL)
304                 _I("++++++++++[END] %s ++++++++++", power_supply_types[index].name);
305
306         dbus_message_unref(msg);
307         dbus_error_free(&err);
308         sleep(TEST_WAIT_TIME_INTERVAL);
309         return ret_val;
310 }
311
312 static void scenario(char *scenario)
313 {
314         int index;
315
316         for (index = 0; index < ARRAY_SIZE(power_supply_types); index++) {
317                 if (strcmp(scenario, power_supply_types[index].scenario) != 0)
318                         continue;
319                 test(index);
320         }
321 }
322
323 static void unit(char *unit, int status)
324 {
325         int index;
326         int found = 0;
327         char *scenario = NULL;
328
329         for (index = 0; index < ARRAY_SIZE(power_supply_types); index++) {
330                 scenario = power_supply_types[index].scenario;
331                 if (strcmp(unit, scenario) != 0 ||
332                     power_supply_types[index].status != status)
333                         continue;
334                 found = 1;
335                 test(index);
336         }
337
338         if (found)
339                 return;
340
341         index = strlen(unit);
342         if (index < 0 || index > 3)
343                 return;
344
345         index = strtol(unit, NULL, 10);
346         if (index < 0 || index > 100)
347                 return;
348
349         for (index = 0; index < ARRAY_SIZE(power_supply_types); index++) {
350                 if (strcmp("capacity", power_supply_types[index].scenario) != 0 ||
351                     power_supply_types[index].status != status)
352                         continue;
353                 power_supply_types[index].capacity = unit;
354                 _D("%s", power_supply_types[index].capacity);
355                 test(index);
356         }
357 }
358
359 static void power_supply_init(void *data)
360 {
361         int index;
362
363         _I("start test");
364         for (index = 0; index < ARRAY_SIZE(power_supply_types); index++)
365                 test(index);
366 }
367
368 static void power_supply_exit(void *data)
369 {
370         _I("end test");
371 }
372
373 static int power_supply_unit(int argc, char **argv)
374 {
375         if (argv[1] == NULL)
376                 return -EINVAL;
377         else if (argc != 4)
378                 return -EAGAIN;
379         if (strcmp("wait", argv[2]) == 0)
380                 test_signal();
381         else if (strcmp("scenario", argv[2]) == 0)
382                 scenario(argv[3]);
383         else if (strcmp("enter", argv[3]) == 0)
384                 unit(argv[2], S_ENTER);
385         else if (strcmp("leave", argv[3]) == 0)
386                 unit(argv[2], S_LEAVE);
387         return 0;
388 }
389
390 static const struct test_ops power_test_ops = {
391         .priority = TEST_PRIORITY_NORMAL,
392         .name     = "power",
393         .init     = power_supply_init,
394         .exit    = power_supply_exit,
395         .unit    = power_supply_unit,
396 };
397
398 TEST_OPS_REGISTER(&power_test_ops)