Initialize Tizen 2.3
[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         e_dbus_message_send(edbus_conn, msg, NULL, -1, NULL);
113
114         dbus_message_unref(msg);
115         return 0;
116 }
117
118 static void poweroff_send_broadcast(char *status)
119 {
120         char *arr[2];
121         char str_status[32];
122
123         snprintf(str_status, sizeof(str_status), "%s", status);
124         arr[0] = str_status;
125         arr[1] = "0";
126         _D("broadcast poweroff %s %s", arr[0], arr[1]);
127
128         edbus_init();
129         broadcast_edbus_signal(DEVICED_OBJECT_PATH, DEVICED_INTERFACE_NAME,
130                         "poweroffpopup", "si", arr);
131         edbus_exit();
132 }
133
134 static void unit(char *unit, char *status)
135 {
136         int index;
137
138         for (index = 0; index < ARRAY_SIZE(boot_control_types); index++) {
139                 if (strcmp(unit, boot_control_types[index].name) != 0 ||
140                     strcmp(status, boot_control_types[index].status) != 0)
141                         continue;
142                 if (strcmp(unit, "poweroffpopup") == 0)
143                         poweroff_send_broadcast(boot_control_types[index].status);
144         }
145
146 }
147
148 static void boot_init(void *data)
149 {
150 }
151
152 static void boot_exit(void *data)
153 {
154 }
155
156 static int boot_unit(int argc, char **argv)
157 {
158         int status;
159
160         if (argv[1] == NULL)
161                 return -EINVAL;
162         if (argc < 3)
163                 return -EAGAIN;
164         unit(argv[argc-2], argv[argc-1]);
165 out:
166         return 0;
167 }
168
169 static const struct test_ops boot_test_ops = {
170         .priority = TEST_PRIORITY_HIGH,
171         .name     = "boot",
172         .init     = boot_init,
173         .exit    = boot_exit,
174         .unit    = boot_unit,
175 };
176
177 TEST_OPS_REGISTER(&boot_test_ops)