Initialize Tizen 2.3
[framework/system/deviced.git] / src / auto-test / cradle.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 METHOD_GET_CRADLE       "GetCradle"
22
23 static const struct device_change_type {
24         char *name;
25         char *status;
26 } device_change_types [] = {
27         {"cradle",      "1"},
28         {"cradle",      "0"},
29 };
30
31 static int test_cradle(void)
32 {
33         DBusError err;
34         DBusMessage *msg;
35         int ret, level;
36
37         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
38                         DEVICED_PATH_SYSNOTI,
39                         DEVICED_INTERFACE_SYSNOTI,
40                         METHOD_GET_CRADLE, NULL, NULL);
41         if (!msg) {
42                 _E("fail : %s %s %s %s",
43                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
44                         METHOD_GET_CRADLE);
45                 return -EBADMSG;
46         }
47
48         dbus_error_init(&err);
49
50         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &level,
51                         DBUS_TYPE_INVALID);
52         if (!ret) {
53                 _E("no message : [%s:%s]", err.name, err.message);
54                 level = -1;
55         }
56         _I("%d", level);
57         dbus_message_unref(msg);
58         dbus_error_free(&err);
59         sleep(TEST_WAIT_TIME_INTERVAL);
60         return level;
61 }
62
63 static int test(int index)
64 {
65         DBusError err;
66         DBusMessage *msg;
67         int ret, ret_val;
68         char *param[4];
69
70         param[0] = METHOD_SET_DEVICE;
71         param[1] = "2";
72         param[2] = device_change_types[index].name;
73         param[3] = device_change_types[index].status;
74
75         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
76                         DEVICED_PATH_SYSNOTI,
77                         DEVICED_INTERFACE_SYSNOTI,
78                         METHOD_SET_DEVICE, "siss", param);
79         if (!msg) {
80                 _E("fail : %s %s %s %s",
81                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
82                         METHOD_SET_DEVICE);
83                 return -EBADMSG;
84         }
85
86         dbus_error_init(&err);
87
88         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &ret_val, DBUS_TYPE_INVALID);
89         if (ret == 0) {
90                 _E("no message : [%s:%s]", err.name, err.message);
91                 dbus_error_free(&err);
92                 ret_val = -EBADMSG;
93         }
94         _I("%s %s", device_change_types[index].name, device_change_types[index].status);
95         dbus_message_unref(msg);
96         dbus_error_free(&err);
97         sleep(TEST_WAIT_TIME_INTERVAL);
98         return ret_val;
99 }
100
101 static void unit(char *unit, char *status)
102 {
103         int index;
104
105         for (index = 0; index < ARRAY_SIZE(device_change_types); index++) {
106                 if (strcmp(unit, device_change_types[index].name) != 0 ||
107                     strcmp(status, device_change_types[index].status) != 0)
108                         continue;
109                 test(index);
110                 test_cradle();
111         }
112 }
113
114 static void cradle_init(void *data)
115 {
116         int index;
117
118         _I("start test");
119         for (index = 0; index < ARRAY_SIZE(device_change_types); index++)
120                 test(index);
121 }
122
123 static void cradle_exit(void *data)
124 {
125         _I("end test");
126 }
127
128 static int cradle_unit(int argc, char **argv)
129 {
130         int status;
131
132         if (argv[1] == NULL)
133                 return -EINVAL;
134         if (argc != 4)
135                 return -EAGAIN;
136
137         unit(argv[2], argv[3]);
138 out:
139         return 0;
140 }
141
142 static const struct test_ops cradle_test_ops = {
143         .priority = TEST_PRIORITY_NORMAL,
144         .name     = "cradle",
145         .init     = cradle_init,
146         .exit    = cradle_exit,
147         .unit    = cradle_unit,
148 };
149
150 TEST_OPS_REGISTER(&cradle_test_ops)