4 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
30 static bool request_haptic_method(const char *method, GVariant *param)
36 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
38 VIBRATOR_INTERFACE_HAPTIC,
42 _E("Failed to call %s: no reply", method);
46 if (!dh_get_param_from_var(msg, "(i)", &val))
47 _E("Failed to call %s: no message", method);
49 if ((val == -ENOTSUP) || (val == -ENOSYS)) {
50 _I("Not supported feature(%s): %d", method, val);
52 } else if (val == -ENODEV) {
53 _E("Failed to call %s. Device open fail: %d", method, val);
55 _E("Failed to call %s. Returned fail: %d", method, val);
57 _I("Success. %s: %d", method, val);
66 static bool haptic_getcount()
68 _D("----------------------------------------------------------------------------------");
69 return request_haptic_method(METHOD_HAPTIC_GETCOUNT, NULL);
72 static bool haptic_opendevice(int index)
74 _D("----------------------------------------------------------------------------------");
75 return request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", index));
78 static bool haptic_closedevice()
84 _D("----------------------------------------------------------------------------------");
85 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
87 VIBRATOR_INTERFACE_HAPTIC,
88 METHOD_HAPTIC_OPENDEVICE,
89 g_variant_new("(i)", 0));
92 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
96 if (dh_get_param_from_var(msg, "(i)", &handle))
97 ret = request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle));
99 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
101 g_variant_unref(msg);
106 static bool haptic_vibratemonotone(int duration, int level, int priority)
112 _D("----------------------------------------------------------------------------------");
113 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
114 VIBRATOR_PATH_HAPTIC,
115 VIBRATOR_INTERFACE_HAPTIC,
116 METHOD_HAPTIC_OPENDEVICE,
117 g_variant_new("(i)", 0));
120 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
124 if (dh_get_param_from_var(msg, "(i)", &handle))
125 ret = request_haptic_method(METHOD_HAPTIC_VIBRATEMONOTONE, g_variant_new("(uiii)", handle, duration, level, priority));
127 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
129 g_variant_unref(msg);
134 static bool haptic_vibratepattern(char *pattern, int level, int priority)
140 _D("----------------------------------------------------------------------------------");
141 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
142 VIBRATOR_PATH_HAPTIC,
143 VIBRATOR_INTERFACE_HAPTIC,
144 METHOD_HAPTIC_OPENDEVICE,
145 g_variant_new("(i)", 0));
148 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
152 if (dh_get_param_from_var(msg, "(i)", &handle))
153 ret = request_haptic_method(METHOD_HAPTIC_VIBRATEEFFECT, g_variant_new("(usii)", handle, pattern, level, priority));
155 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
157 g_variant_unref(msg);
162 static bool haptic_stopdevice()
164 struct timespec time = {0,};
169 _D("----------------------------------------------------------------------------------");
170 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
171 VIBRATOR_PATH_HAPTIC,
172 VIBRATOR_INTERFACE_HAPTIC,
173 METHOD_HAPTIC_OPENDEVICE,
174 g_variant_new("(i)", 0));
177 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
181 if (!dh_get_param_from_var(msg, "(i)", &handle)) {
182 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
183 g_variant_unref(msg);
186 g_variant_unref(msg);
188 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
189 VIBRATOR_PATH_HAPTIC,
190 VIBRATOR_INTERFACE_HAPTIC,
191 METHOD_HAPTIC_VIBRATEMONOTONE,
192 g_variant_new("(uiii)", handle, 1000, 100, 0));
195 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_VIBRATEMONOTONE);
198 g_variant_unref(msg);
201 time.tv_nsec = 300 * NANO_SECOND_MULTIPLIER;
202 nanosleep(&time, NULL);
205 return request_haptic_method(METHOD_HAPTIC_STOPDEVICE, g_variant_new("(u)", handle));
208 static bool haptic_getstate(int index)
210 _D("----------------------------------------------------------------------------------");
211 return request_haptic_method(METHOD_HAPTIC_GETSTATE, g_variant_new("(i)", index));
214 static bool haptic_showhandlelist()
218 _D("----------------------------------------------------------------------------------");
219 msg = dbus_handle_method_sync_with_reply_var(VIBRATOR_BUS_NAME,
220 VIBRATOR_PATH_HAPTIC,
221 VIBRATOR_INTERFACE_HAPTIC,
222 METHOD_HAPTIC_SHOWHANDLELIST,
226 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
229 g_variant_unref(msg);
233 static bool haptic_issupported(char *pattern)
235 _D("----------------------------------------------------------------------------------");
236 return request_haptic_method(METHOD_HAPTIC_ISSUPPORTED, g_variant_new("(s)", pattern));
239 void haptic_test_all(int *success, int *fail)
241 struct timespec time = {0,};
247 (haptic_getcount()) ? s++ : f++;
248 (haptic_opendevice(0)) ? s++ : f++;
249 (haptic_closedevice()) ? s++ : f++;
250 (haptic_stopdevice()) ? s++ : f++;
251 nanosleep(&time, NULL);
252 (haptic_vibratemonotone(300, 100, 0)) ? s++ : f++;
253 nanosleep(&time, NULL);
254 (haptic_vibratepattern("FEEDBACK_PATTERN_SIP", 100, 0)) ? s++ : f++;
255 nanosleep(&time, NULL);
256 (haptic_getstate(0)) ? s++ : f++;
257 (haptic_showhandlelist()) ? s++ : f++;
258 (haptic_issupported("FEEDBACK_PATTERN_SIP")) ? s++ : f++;
261 if (NULL != success) *success = s;
262 if (NULL != fail) *fail = f;
265 static void haptic_init(void *data)
272 haptic_test_all(&success, &fail);
274 _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
277 static void haptic_exit(void *data)
282 static int haptic_unit(int argc, char **argv)
284 struct timespec time = {0,};
291 haptic_test_all(&success, &fail);
292 _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
293 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_GETCOUNT)) {
295 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_OPENDEVICE)) {
296 haptic_opendevice(atoi(argv[2]));
297 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_CLOSEDEVICE)) {
298 haptic_closedevice();
299 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_STOPDEVICE)) {
302 nanosleep(&time, NULL);
303 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_VIBRATEMONOTONE)) {
304 haptic_vibratemonotone(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
305 time.tv_sec = 1 + (atoi(argv[2]) / 1000);
306 nanosleep(&time, NULL);
307 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_VIBRATEEFFECT)) {
308 haptic_vibratepattern(argv[2], atoi(argv[3]), atoi(argv[4]));
310 nanosleep(&time, NULL);
311 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_GETSTATE)) {
312 haptic_getstate(atoi(argv[2]));
313 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_SHOWHANDLELIST)) {
314 haptic_showhandlelist();
315 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_ISSUPPORTED)) {
316 haptic_issupported(argv[2]);
318 _E("Unknown test case.");
324 static const struct test_ops haptic_test_ops = {
325 .priority = TEST_PRIORITY_NORMAL,
332 TEST_OPS_REGISTER(&haptic_test_ops)