f4b2f0f06ba99784b5fc856bf962fc580ec4c14d
[platform/core/system/deviced.git] / src / auto-test / extcon.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 METHOD_EXTCON_GETSTATUS         "GetStatus"
21 #define METHOD_EXTCON_ENABLE            "enable"
22 #define METHOD_EXTCON_DISABLE           "disable"
23
24 #define METHOD_SYSNOTI_GETCRADLE        "GetCradle"
25 #define METHOD_SYSNOTI_GETHDMI          "GetHDMI"
26
27 static bool request_extcon_method(const char *method, GVariant *param)
28 {
29         GVariant *msg;
30         int val, err;
31         bool ret = FALSE;
32
33         err = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
34                                                 DEVICED_PATH_EXTCON,
35                                                 DEVICED_INTERFACE_EXTCON,
36                                                 method,
37                                                 param,
38                                                 &msg);
39         if (err < 0) {
40                 _E("fail (%s): no reply", method);
41                 return ret;
42         }
43
44         if (!g_variant_get_safe(msg, "(i)", &val))
45                 _E("fail (%s): no message", method);
46         else {
47                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
48                         _I("Not supported feature! (%s): %d", method, val);
49                         ret = TRUE;
50                 } else if (val < 0) {
51                         _E("fail (%s): returned fail (%d)", method, val);
52                 } else {
53                         _I("success (%s): %d", method, val);
54                         ret = TRUE;
55                 }
56         }
57
58         g_variant_unref(msg);
59         return ret;
60 }
61
62 static bool get_sysnoti_method(const char *method)
63 {
64         GVariant *msg;
65         int val, err;
66         bool ret = FALSE;
67
68         err = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
69                                         DEVICED_PATH_SYSNOTI,
70                                         DEVICED_INTERFACE_SYSNOTI,
71                                         method, NULL, &msg);
72         if (err < 0) {
73                 _E("fail (%s): no reply", method);
74                 return ret;
75         }
76
77         if (!g_variant_get_safe(msg, "(i)", &val))
78                 _E("fail (%s): no message", method);
79         else {
80                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
81                         _I("Not supported feature! (%s): %d", method, val);
82                         ret = TRUE;
83                 } else if ((val == -EINVAL) || (val == -ENOENT)) {
84                         _E("fail : returned fail (%d)", val);
85                 } else {
86                         _I("success (%s): %d", method, val);
87                         ret = TRUE;
88                 }
89         }
90
91         g_variant_unref(msg);
92         return ret;
93 }
94
95 static bool get_extcon_status(char *device_name)
96 {
97         GVariant *msg;
98         int val, err;
99         bool ret = FALSE;
100
101         err = dbus_handle_method_sync_with_reply_var(DEVICED_BUS_NAME,
102                                                 DEVICED_PATH_EXTCON,
103                                                 DEVICED_INTERFACE_EXTCON,
104                                                 METHOD_EXTCON_GETSTATUS,
105                                                 g_variant_new("(s)", device_name),
106                                                 &msg);
107         if (err < 0) {
108                 _E("fail : no reply");
109                 return ret;
110         }
111
112         if (!g_variant_get_safe(msg, "(i)", &val))
113                 _E("fail : no message");
114         else {
115                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
116                         _I("Not supported feature! : %d", val);
117                         ret = TRUE;
118                 } else if ((val == -EINVAL) || (val == -ENOENT)) {
119                         _E("fail : returned fail (%d)", val);
120                 } else {
121                         _I("success : %d", val);
122                         ret = TRUE;
123                 }
124         }
125
126         g_variant_unref(msg);
127         return ret;
128 }
129
130 static bool set_extcon_enable(char *device_name)
131 {
132         return request_extcon_method(METHOD_EXTCON_ENABLE, g_variant_new("(s)", device_name));
133 }
134
135 static bool set_extcon_disable(char *device_name)
136 {
137         return request_extcon_method(METHOD_EXTCON_DISABLE, g_variant_new("(s)", device_name));
138 }
139
140 static bool get_sysnoti_cradle()
141 {
142         return get_sysnoti_method(METHOD_SYSNOTI_GETCRADLE);
143 }
144
145 static bool get_sysnoti_hdmi()
146 {
147         return get_sysnoti_method(METHOD_SYSNOTI_GETHDMI);
148 }
149
150 void extcon_test_all(int *success, int *fail)
151 {
152         int s = 0;
153         int f = 0;
154
155         (set_extcon_enable("Headphone"))                ? s++ : f++;
156         (get_extcon_status("Headphone"))                ? s++ : f++;
157         (set_extcon_disable("Headphone"))               ? s++ : f++;
158
159         (get_sysnoti_cradle())                                  ? s++ : f++;
160         (get_sysnoti_hdmi())                                    ? s++ : f++;
161
162         if (NULL != success)    *success = s;
163         if (NULL != fail)       *fail = f;
164 }
165
166 static void extcon_init(void *data)
167 {
168         int success = 0;
169         int fail = 0;
170
171         _I("start test");
172
173         extcon_test_all(&success, &fail);
174
175         _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
176 }
177
178 static void extcon_exit(void *data)
179 {
180         _I("end test");
181 }
182
183 static int extcon_unit(int argc, char **argv)
184 {
185         if (argc < 4) {
186                 int success = 0;
187                 int fail = 0;
188
189                 _I("start test");
190                 extcon_test_all(&success, &fail);
191                 _I("Total: %d, Success: %d, Fail: %d", success+fail, success, fail);
192         } else if (0 == strcasecmp(argv[3], METHOD_EXTCON_GETSTATUS)) {
193                 get_extcon_status(argv[4]);
194         } else if (0 == strcasecmp(argv[3], METHOD_EXTCON_ENABLE)) {
195                 set_extcon_enable(argv[4]);
196         } else if (0 == strcasecmp(argv[3], METHOD_EXTCON_DISABLE)) {
197                 set_extcon_disable(argv[4]);
198         } else if (0 == strcasecmp(argv[3], METHOD_SYSNOTI_GETCRADLE)) {
199                 get_sysnoti_cradle();
200         } else if (0 == strcasecmp(argv[3], METHOD_SYSNOTI_GETHDMI)) {
201                 get_sysnoti_hdmi();
202         } else {
203                 _E("Unknown test case!!!");
204         }
205
206         return 0;
207 }
208
209 static const struct test_ops extcon_test_ops = {
210         .priority = TEST_PRIORITY_NORMAL,
211         .name     = "extcon",
212         .init     = extcon_init,
213         .exit    = extcon_exit,
214         .unit    = extcon_unit,
215 };
216
217 TEST_OPS_REGISTER(&extcon_test_ops)