bca81599d4fad06dc6b3bed5c22d35b074a0cdfe
[platform/core/system/feedbackd.git] / src / auto-test / haptic.c
1 /*
2  * feedbackd auto-test
3  *
4  * Copyright (c) 2018 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 requhapticed 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_HAPTIC_GETCOUNT          "GetCount"
21 #define METHOD_HAPTIC_OPENDEVICE        "OpenDevice"
22 #define METHOD_HAPTIC_CLOSEDEVICE       "CloseDevice"
23 #define METHOD_HAPTIC_STOPDEVICE        "StopDevice"
24 #define METHOD_HAPTIC_VIBRATEMONOTONE   "VibrateMonotone"
25 #define METHOD_HAPTIC_VIBRATEEFFECT     "VibratePattern"
26 #define METHOD_HAPTIC_GETSTATE          "GetState"
27 #define METHOD_HAPTIC_SHOWHANDLELIST    "ShowHandleList"
28 #define METHOD_HAPTIC_ISSUPPORTED       "IsSupported"
29
30 static bool request_haptic_method(const char *method, GVariant *param)
31 {
32         GVariant *msg;
33         int val;
34         bool ret = FALSE;
35
36         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
37                 VIBRATOR_PATH_HAPTIC,
38                 VIBRATOR_INTERFACE_HAPTIC,
39                 method, param);
40
41         if (!msg) {
42                 _E("Failed to call %s: no reply", method);
43                 return ret;
44         }
45
46         if (!dh_get_param_from_var(msg, "(i)", &val))
47                 _E("Failed to call %s: no message", method);
48         else {
49                 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
50                         _I("Not supported feature(%s): %d", method, val);
51                         ret = TRUE;
52                 } else if (val == -ENODEV) {
53                         _E("Failed to call %s. Device open fail: %d", method, val);
54                 } else if (val < 0) {
55                         _E("Failed to call %s. Returned fail: %d", method, val);
56                 } else {
57                         _I("Success. %s: %d", method, val);
58                         ret = TRUE;
59                 }
60         }
61
62         g_variant_unref(msg);
63         return ret;
64 }
65
66 static bool haptic_getcount()
67 {
68         _D("----------------------------------------------------------------------------------");
69         return request_haptic_method(METHOD_HAPTIC_GETCOUNT, NULL);
70 }
71
72 static bool haptic_opendevice(int index)
73 {
74         _D("----------------------------------------------------------------------------------");
75         return request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", index));
76 }
77
78 static bool haptic_closedevice()
79 {
80         GVariant *msg;
81         int handle;
82         bool ret = FALSE;
83
84         _D("----------------------------------------------------------------------------------");
85         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
86                 VIBRATOR_PATH_HAPTIC,
87                 VIBRATOR_INTERFACE_HAPTIC,
88                 METHOD_HAPTIC_OPENDEVICE,
89                 g_variant_new("(i)", 0));
90
91         if (!msg) {
92                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
93                 return ret;
94         }
95
96         if (!dh_get_param_from_var(msg, "(i)", &handle)) {
97                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
98                 return ret;
99         }
100         g_variant_unref(msg);
101
102         return request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle));
103 }
104
105 static bool haptic_vibratemonotone(int duration, int level, int priority)
106 {
107         GVariant *msg;
108         int handle;
109         bool ret = FALSE;
110
111         _D("----------------------------------------------------------------------------------");
112         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
113                 VIBRATOR_PATH_HAPTIC,
114                 VIBRATOR_INTERFACE_HAPTIC,
115                 METHOD_HAPTIC_OPENDEVICE,
116                 g_variant_new("(i)", 0));
117
118         if (!msg) {
119                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
120                 return ret;
121         }
122
123         if (!dh_get_param_from_var(msg, "(i)", &handle)) {
124                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
125                 return ret;
126         }
127         g_variant_unref(msg);
128
129         return request_haptic_method(METHOD_HAPTIC_VIBRATEMONOTONE, g_variant_new("(uiii)", handle, duration, level, priority));
130 }
131
132 static bool haptic_vibratepattern(char *pattern, int level, int priority)
133 {
134         GVariant *msg;
135         int handle;
136         bool ret = FALSE;
137
138         _D("----------------------------------------------------------------------------------");
139         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
140                 VIBRATOR_PATH_HAPTIC,
141                 VIBRATOR_INTERFACE_HAPTIC,
142                 METHOD_HAPTIC_OPENDEVICE,
143                 g_variant_new("(i)", 0));
144
145         if (!msg) {
146                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
147                 return ret;
148         }
149
150         if (!dh_get_param_from_var(msg, "(i)", &handle)) {
151                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
152                 return ret;
153         }
154         g_variant_unref(msg);
155
156         return request_haptic_method(METHOD_HAPTIC_VIBRATEEFFECT, g_variant_new("(usii)", handle, pattern, level, priority));
157 }
158
159 static bool haptic_stopdevice()
160 {
161         struct timespec time = {0,};
162         GVariant *msg;
163         int handle;
164         bool ret = FALSE;
165
166         _D("----------------------------------------------------------------------------------");
167         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
168                 VIBRATOR_PATH_HAPTIC,
169                 VIBRATOR_INTERFACE_HAPTIC,
170                 METHOD_HAPTIC_OPENDEVICE,
171                 g_variant_new("(i)", 0));
172
173         if (!msg) {
174                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
175                 return ret;
176         }
177
178         if (!dh_get_param_from_var(msg, "(i)", &handle)) {
179                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
180                 return ret;
181         }
182         g_variant_unref(msg);
183
184         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
185                 VIBRATOR_PATH_HAPTIC,
186                 VIBRATOR_INTERFACE_HAPTIC,
187                 METHOD_HAPTIC_VIBRATEMONOTONE,
188                 g_variant_new("(uiii)", handle, 1000, 100, 0));
189
190         if (!msg) {
191                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_VIBRATEMONOTONE);
192                 return ret;
193         }
194
195         _D("Sleep 300ms.");
196         time.tv_nsec = 300 * NANO_SECOND_MULTIPLIER;
197         nanosleep(&time, NULL);
198         _D("Wakeup.");
199
200         return request_haptic_method(METHOD_HAPTIC_STOPDEVICE, g_variant_new("(u)", handle));
201 }
202
203 static bool haptic_getstate(int index)
204 {
205         _D("----------------------------------------------------------------------------------");
206         return request_haptic_method(METHOD_HAPTIC_GETSTATE, g_variant_new("(i)", index));
207 }
208
209 static bool haptic_showhandlelist()
210 {
211         GVariant *msg;
212
213         _D("----------------------------------------------------------------------------------");
214         msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
215                 VIBRATOR_PATH_HAPTIC,
216                 VIBRATOR_INTERFACE_HAPTIC,
217                 METHOD_HAPTIC_SHOWHANDLELIST,
218                 NULL);
219
220         if (!msg) {
221                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
222                 return FALSE;
223         }
224         g_variant_unref(msg);
225         return TRUE;
226 }
227
228 static bool haptic_issupported(char *pattern)
229 {
230         _D("----------------------------------------------------------------------------------");
231         return request_haptic_method(METHOD_HAPTIC_ISSUPPORTED, g_variant_new("(s)", pattern));
232 }
233
234 void haptic_test_all(int *success, int *fail)
235 {
236         struct timespec time = {0,};
237         int s = 0;
238         int f = 0;
239
240         time.tv_sec = 1;
241
242         (haptic_getcount())                                     ? s++ : f++;
243         (haptic_opendevice(0))                                  ? s++ : f++;
244         (haptic_closedevice())                                  ? s++ : f++;
245         (haptic_stopdevice())                                   ? s++ : f++;
246         nanosleep(&time, NULL);
247         (haptic_vibratemonotone(300, 100, 0))                   ? s++ : f++;
248         nanosleep(&time, NULL);
249         (haptic_vibratepattern("FEEDBACK_PATTERN_SIP", 100, 0)) ? s++ : f++;
250         nanosleep(&time, NULL);
251         (haptic_getstate(0))                                    ? s++ : f++;
252         (haptic_showhandlelist())                               ? s++ : f++;
253         (haptic_issupported("FEEDBACK_PATTERN_SIP"))            ? s++ : f++;
254
255
256         if (NULL != success)    *success = s;
257         if (NULL != fail)       *fail = f;
258 }
259
260 static void haptic_init(void *data)
261 {
262         int success = 0;
263         int fail = 0;
264
265         _I("Start test.");
266
267         haptic_test_all(&success, &fail);
268
269         _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
270 }
271
272 static void haptic_exit(void *data)
273 {
274         _I("End test.");
275 }
276
277 static int haptic_unit(int argc, char **argv)
278 {
279         struct timespec time = {0,};
280
281         if (argc < 2) {
282                 int success = 0;
283                 int fail = 0;
284
285                 _I("Start test.");
286                 haptic_test_all(&success, &fail);
287                 _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
288         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_GETCOUNT)) {
289                 haptic_getcount();
290         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_OPENDEVICE)) {
291                 haptic_opendevice(atoi(argv[2]));
292         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_CLOSEDEVICE)) {
293                 haptic_closedevice();
294         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_STOPDEVICE)) {
295                 time.tv_sec = 1;
296                 haptic_stopdevice();
297                 nanosleep(&time, NULL);
298         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_VIBRATEMONOTONE)) {
299                 haptic_vibratemonotone(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
300                 time.tv_sec = 1 + (atoi(argv[2]) / 1000);
301                 nanosleep(&time, NULL);
302         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_VIBRATEEFFECT)) {
303                 haptic_vibratepattern(argv[2], atoi(argv[3]), atoi(argv[4]));
304                 time.tv_sec = 2;
305                 nanosleep(&time, NULL);
306         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_GETSTATE)) {
307                 haptic_getstate(atoi(argv[2]));
308         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_SHOWHANDLELIST)) {
309                 haptic_showhandlelist();
310         } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_ISSUPPORTED)) {
311                 haptic_issupported(argv[2]);
312         } else {
313                 _E("Unknown test case.");
314         }
315
316         return 0;
317 }
318
319 static const struct test_ops haptic_test_ops = {
320         .priority = TEST_PRIORITY_NORMAL,
321         .name     = "haptic",
322         .init     = haptic_init,
323         .exit    = haptic_exit,
324         .unit    = haptic_unit,
325 };
326
327 TEST_OPS_REGISTER(&haptic_test_ops)