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