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