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 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
100 g_variant_unref(msg);
102 return request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle));
105 static bool haptic_vibratemonotone(int duration, int level, int priority)
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));
119 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
123 if (!dh_get_param_from_var(msg, "(i)", &handle)) {
124 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
127 g_variant_unref(msg);
129 return request_haptic_method(METHOD_HAPTIC_VIBRATEMONOTONE, g_variant_new("(uiii)", handle, duration, level, priority));
132 static bool haptic_vibratepattern(char *pattern, int level, int priority)
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));
146 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
150 if (!dh_get_param_from_var(msg, "(i)", &handle)) {
151 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
154 g_variant_unref(msg);
156 return request_haptic_method(METHOD_HAPTIC_VIBRATEEFFECT, g_variant_new("(usii)", handle, pattern, level, priority));
159 static bool haptic_stopdevice()
161 struct timespec time = {0,};
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));
174 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
178 if (!dh_get_param_from_var(msg, "(i)", &handle)) {
179 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
182 g_variant_unref(msg);
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));
191 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_VIBRATEMONOTONE);
196 time.tv_nsec = 300 * NANO_SECOND_MULTIPLIER;
197 nanosleep(&time, NULL);
200 return request_haptic_method(METHOD_HAPTIC_STOPDEVICE, g_variant_new("(u)", handle));
203 static bool haptic_getstate(int index)
205 _D("----------------------------------------------------------------------------------");
206 return request_haptic_method(METHOD_HAPTIC_GETSTATE, g_variant_new("(i)", index));
209 static bool haptic_showhandlelist()
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,
221 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
224 g_variant_unref(msg);
228 static bool haptic_issupported(char *pattern)
230 _D("----------------------------------------------------------------------------------");
231 return request_haptic_method(METHOD_HAPTIC_ISSUPPORTED, g_variant_new("(s)", pattern));
234 void haptic_test_all(int *success, int *fail)
236 struct timespec time = {0,};
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++;
256 if (NULL != success) *success = s;
257 if (NULL != fail) *fail = f;
260 static void haptic_init(void *data)
267 haptic_test_all(&success, &fail);
269 _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
272 static void haptic_exit(void *data)
277 static int haptic_unit(int argc, char **argv)
279 struct timespec time = {0,};
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)) {
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)) {
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]));
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]);
313 _E("Unknown test case.");
319 static const struct test_ops haptic_test_ops = {
320 .priority = TEST_PRIORITY_NORMAL,
327 TEST_OPS_REGISTER(&haptic_test_ops)