tizen 2.3 release
[framework/system/deviced.git] / src / auto-test / udev.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 static const struct device_change_type {
21         char *name;
22         char *status;
23 } device_change_types [] = {
24         {"udev",                "start"},
25         {"udev",                "stop"},
26 };
27
28 static int udev(int index)
29 {
30         DBusError err;
31         DBusMessage *msg;
32         int ret, val;
33         char *param[3];
34
35         param[0] = UDEV;
36         param[1] = "1";
37         param[2] = device_change_types[index].status;
38
39         msg = dbus_method_sync_with_reply(DEVICED_BUS_NAME,
40                         DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
41                         UDEV, "sis", param);
42         if (!msg) {
43                 _E("fail : %s %s %s %s",
44                         DEVICED_BUS_NAME, DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI,
45                         UDEV);
46                 return -EBADMSG;
47         }
48
49         dbus_error_init(&err);
50
51         ret = dbus_message_get_args(msg, &err, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
52         if (!ret) {
53                 _E("no message : [%s:%s]", err.name, err.message);
54                 val = -EBADMSG;
55         }
56         _I("%s", device_change_types[index].status);
57         if (val < 0)
58                 _R("[NG] ---- %s             : %s",
59                 __func__, device_change_types[index].status);
60         else
61                 _R("[OK] ---- %s             : %s",
62                 __func__, device_change_types[index].status);
63         dbus_message_unref(msg);
64         dbus_error_free(&err);
65         sleep(TEST_WAIT_TIME_INTERVAL);
66         return val;
67 }
68
69 static void unit(char *unit, char *status)
70 {
71         int index;
72
73         for (index = 0; index < ARRAY_SIZE(device_change_types); index++) {
74                 if (strcmp(unit, device_change_types[index].name) != 0 ||
75                     strcmp(status, device_change_types[index].status) != 0)
76                         continue;
77                 udev(index);
78         }
79 }
80
81 static void udev_init(void *data)
82 {
83         int index;
84
85         _I("start test");
86         for (index = 0; index < ARRAY_SIZE(device_change_types); index++)
87                 udev(index);
88 }
89
90 static void udev_exit(void *data)
91 {
92         _I("end test");
93 }
94
95 static int udev_unit(int argc, char **argv)
96 {
97         int status;
98
99         if (argv[1] == NULL)
100                 return -EINVAL;
101         if (argc != 3)
102                 return -EAGAIN;
103
104         unit(argv[1], argv[2]);
105 out:
106         return 0;
107 }
108
109 static const struct test_ops udev_test_ops = {
110         .priority = TEST_PRIORITY_HIGH,
111         .name     = "udev",
112         .init     = udev_init,
113         .exit    = udev_exit,
114         .unit    = udev_unit,
115 };
116
117 TEST_OPS_REGISTER(&udev_test_ops)