tizen 2.3 release
[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, val;
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, &val,
51                         DBUS_TYPE_INVALID);
52         if (!ret) {
53                 _E("no message : [%s:%s]", err.name, err.message);
54                 val = -1;
55         }
56         _I("%d", val);
57         if (val < 0)
58                 _R("[NG] ---- %s", __func__);
59         else
60                 _R("[OK] ---- %s      : V(%d)", __func__, val);
61         dbus_message_unref(msg);
62         dbus_error_free(&err);
63         sleep(TEST_WAIT_TIME_INTERVAL);
64         return val;
65 }
66
67 static int cradle(int index)
68 {
69         DBusError err;
70         DBusMessage *msg;
71         int ret, val;
72         char *param[4];
73
74         param[0] = METHOD_SET_DEVICE;
75         param[1] = "2";
76         param[2] = device_change_types[index].name;
77         param[3] = device_change_types[index].status;
78
79         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
80                         DEVICED_PATH_SYSNOTI,
81                         DEVICED_INTERFACE_SYSNOTI,
82                         METHOD_SET_DEVICE, "siss", param);
83         if (!msg) {
84                 _E("fail : %s %s %s %s",
85                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
86                         METHOD_SET_DEVICE);
87                 return -EBADMSG;
88         }
89
90         dbus_error_init(&err);
91
92         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
93         if (ret == 0) {
94                 _E("no message : [%s:%s]", err.name, err.message);
95                 dbus_error_free(&err);
96                 val = -EBADMSG;
97         }
98         _I("%s %s", device_change_types[index].name, device_change_types[index].status);
99         if (val < 0)
100                 _R("[NG] ---- %s", __func__);
101         else
102                 _R("[OK] ---- %s           : V(%s %s)",
103                 __func__, device_change_types[index].name, device_change_types[index].status);
104         dbus_message_unref(msg);
105         dbus_error_free(&err);
106         sleep(TEST_WAIT_TIME_INTERVAL);
107         return val;
108 }
109
110 static void unit(char *unit, char *status)
111 {
112         int index;
113
114         for (index = 0; index < ARRAY_SIZE(device_change_types); index++) {
115                 if (strcmp(unit, device_change_types[index].name) != 0 ||
116                     strcmp(status, device_change_types[index].status) != 0)
117                         continue;
118                 cradle(index);
119                 test_cradle();
120         }
121 }
122
123 static void cradle_init(void *data)
124 {
125         int index;
126
127         _I("start test");
128         for (index = 0; index < ARRAY_SIZE(device_change_types); index++)
129                 cradle(index);
130 }
131
132 static void cradle_exit(void *data)
133 {
134         _I("end test");
135 }
136
137 static int cradle_unit(int argc, char **argv)
138 {
139         int status;
140
141         if (argv[1] == NULL)
142                 return -EINVAL;
143         if (argc != 4)
144                 return -EAGAIN;
145
146         unit(argv[1], argv[2]);
147 out:
148         return 0;
149 }
150
151 static const struct test_ops cradle_test_ops = {
152         .priority = TEST_PRIORITY_NORMAL,
153         .name     = "cradle",
154         .init     = cradle_init,
155         .exit    = cradle_exit,
156         .unit    = cradle_unit,
157 };
158
159 TEST_OPS_REGISTER(&cradle_test_ops)