tizen 2.3 release
[framework/system/deviced.git] / src / auto-test / boot.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 #include "test.h"
19
20 #define EDBUS_INIT_RETRY_COUNT 5
21 static int edbus_init_val;
22 static DBusConnection *conn;
23 static E_DBus_Connection *edbus_conn;
24
25 static const struct boot_control_type {
26         char *name;
27         char *status;
28 } boot_control_types [] = {
29         {"poweroffpopup",       "pwroff-popup"},
30         {"poweroffpopup",       "reboot"},
31         {"poweroffpopup",       "reboot-recovery"},
32         {"poweroffpopup",       "poweroff"},
33         {"poweroffpopup",       "fota"},
34 };
35
36 static void edbus_init(void)
37 {
38         DBusError error;
39         int retry = 0;
40         int i, ret;
41
42         dbus_threads_init_default();
43         dbus_error_init(&error);
44
45         do {
46                 edbus_init_val = e_dbus_init();
47                 if (edbus_init_val)
48                         break;
49                 if (retry == EDBUS_INIT_RETRY_COUNT) {
50                         _E("fail to init edbus");
51                         return;
52                 }
53                 retry++;
54         } while (retry <= EDBUS_INIT_RETRY_COUNT);
55
56         retry = 0;
57         do {
58                 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
59                 if (conn)
60                         break;
61                 if (retry == EDBUS_INIT_RETRY_COUNT) {
62                         _E("fail to get dbus");
63                         goto out1;
64                 }
65                 retry++;
66         } while (retry <= EDBUS_INIT_RETRY_COUNT);
67
68         retry = 0;
69         do {
70                 edbus_conn = e_dbus_connection_setup(conn);
71                 if (edbus_conn)
72                         break;
73                 if (retry == EDBUS_INIT_RETRY_COUNT) {
74                         _E("fail to get edbus");
75                         goto out2;
76                 }
77                 retry++;
78         } while (retry <= EDBUS_INIT_RETRY_COUNT);
79         return;
80 out2:
81         dbus_connection_set_exit_on_disconnect(conn, FALSE);
82 out1:
83         e_dbus_shutdown();
84 }
85
86 static void edbus_exit(void)
87 {
88         e_dbus_connection_close(edbus_conn);
89         e_dbus_shutdown();
90 }
91
92 static int broadcast_edbus_signal(const char *path, const char *interface,
93                 const char *name, const char *sig, char *param[])
94 {
95         DBusMessage *msg;
96         DBusMessageIter iter;
97         int r;
98
99         msg = dbus_message_new_signal(path, interface, name);
100         if (!msg) {
101                 _E("fail to allocate new %s.%s signal", interface, name);
102                 return -EPERM;
103         }
104
105         dbus_message_iter_init_append(msg, &iter);
106         r = append_variant(&iter, sig, param);
107         if (r < 0) {
108                 _E("append_variant error(%d)", r);
109                 return -EPERM;
110         }
111
112         r = dbus_connection_send(conn, msg, NULL);
113         dbus_message_unref(msg);
114
115         if (r != TRUE) {
116                 _E("dbus_connection_send error (%s:%s-%s)", path, interface, name);
117                 return -ECOMM;
118         }
119         return 0;
120 }
121
122 static void poweroff_popup(char *status)
123 {
124         char *arr[2];
125         char str_status[32];
126         int val;
127
128         snprintf(str_status, sizeof(str_status), "%s", status);
129         arr[0] = str_status;
130         arr[1] = "0";
131         _D("broadcast poweroff %s %s", arr[0], arr[1]);
132
133         edbus_init();
134         val = broadcast_edbus_signal(DEVICED_OBJECT_PATH, DEVICED_INTERFACE_NAME,
135                         "poweroffpopup", "si", arr);
136         if (val < 0)
137                 _R("[NG] ---- %s", __func__);
138         else
139                 _R("[OK] ---- %s   : V(%s)", __func__, status);
140         edbus_exit();
141 }
142
143 static void unit(char *unit, char *status)
144 {
145         int index;
146
147         for (index = 0; index < ARRAY_SIZE(boot_control_types); index++) {
148                 if (strcmp(unit, boot_control_types[index].name) != 0 ||
149                     strcmp(status, boot_control_types[index].status) != 0)
150                         continue;
151                 if (strcmp(unit, "poweroffpopup") == 0)
152                         poweroff_popup(boot_control_types[index].status);
153         }
154
155 }
156
157 static void boot_init(void *data)
158 {
159 }
160
161 static void boot_exit(void *data)
162 {
163 }
164
165 static int boot_unit(int argc, char **argv)
166 {
167         int status;
168
169         if (argv[1] == NULL)
170                 return -EINVAL;
171         if (argc < 3)
172                 return -EAGAIN;
173         unit(argv[argc-2], argv[argc-1]);
174 out:
175         return 0;
176 }
177
178 static const struct test_ops boot_test_ops = {
179         .priority = TEST_PRIORITY_HIGH,
180         .name     = "boot",
181         .init     = boot_init,
182         .exit    = boot_exit,
183         .unit    = boot_unit,
184 };
185
186 TEST_OPS_REGISTER(&boot_test_ops)