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, int *out_val)
33 int reply_val, ret_dbus;
36 ret_dbus = gdbus_call_sync_with_reply(VIBRATOR_BUS_NAME,
38 VIBRATOR_INTERFACE_HAPTIC,
39 method, param, &reply);
42 _E("Failed to call %s: no reply", method);
46 if (!g_variant_get_safe(reply, "(i)", &reply_val))
47 _E("Failed to call %s: no message", method);
49 if ((reply_val == -ENOTSUP) || (reply_val == -ENOSYS)) {
50 _I("Not supported feature(%s): %d", method, reply_val);
52 } else if (reply_val == -ENODEV) {
53 _E("Failed to call %s. Device open fail: %d", method, reply_val);
54 } else if (reply_val < 0) {
55 _E("Failed to call %s. Returned fail: %d", method, reply_val);
57 _I("Success. %s: %d", method, reply_val);
62 g_variant_unref(reply);
70 static bool haptic_getcount()
72 _D("----------------------------------------------------------------------------------");
73 return request_haptic_method(METHOD_HAPTIC_GETCOUNT, NULL, NULL);
76 static bool haptic_opendevice(int index)
78 _D("----------------------------------------------------------------------------------");
79 return request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", index), NULL);
82 static bool haptic_closedevice()
87 _D("----------------------------------------------------------------------------------");
88 if (request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", 0), &handle))
89 ret = request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle), NULL);
91 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
96 static bool haptic_vibratemonotone(int duration, int level, int priority)
101 _D("----------------------------------------------------------------------------------");
102 if (request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", 0), &handle)) {
103 ret = request_haptic_method(METHOD_HAPTIC_VIBRATEMONOTONE, g_variant_new("(uiii)", handle, duration, level, priority), NULL);
104 usleep(duration*1000);
105 request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle), NULL);
107 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
112 static bool haptic_vibratepattern(char *pattern, int level, int priority)
117 _D("----------------------------------------------------------------------------------");
118 if (request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", 0), &handle)) {
119 ret = request_haptic_method(METHOD_HAPTIC_VIBRATEEFFECT, g_variant_new("(usii)", handle, pattern, level, priority), NULL);
120 usleep(1000*1000); // 1 sec
121 request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle), NULL);
123 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
128 static bool haptic_stopdevice()
133 _D("----------------------------------------------------------------------------------");
134 if (!request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", 0), &handle)) {
135 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
139 if (request_haptic_method(METHOD_HAPTIC_VIBRATEMONOTONE, g_variant_new("(uiii)", handle, 1000, 2, 0), NULL)) {
143 ret = request_haptic_method(METHOD_HAPTIC_STOPDEVICE, g_variant_new("(u)", handle), NULL);
145 request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle), NULL);
149 static bool haptic_getstate(int index)
151 _D("----------------------------------------------------------------------------------");
152 return request_haptic_method(METHOD_HAPTIC_GETSTATE, g_variant_new("(i)", index), NULL);
155 static bool haptic_showhandlelist()
157 _D("----------------------------------------------------------------------------------");
158 return request_haptic_method(METHOD_HAPTIC_SHOWHANDLELIST, NULL, NULL);
161 static bool haptic_issupported(char *pattern)
163 _D("----------------------------------------------------------------------------------");
164 return request_haptic_method(METHOD_HAPTIC_ISSUPPORTED, g_variant_new("(s)", pattern), NULL);
167 void haptic_test_all(int *success, int *fail)
169 struct timespec time = {0,};
175 (haptic_getcount()) ? s++ : f++;
176 (haptic_opendevice(0)) ? s++ : f++;
177 (haptic_closedevice()) ? s++ : f++;
178 (haptic_stopdevice()) ? s++ : f++;
179 nanosleep(&time, NULL);
180 (haptic_vibratemonotone(300, 2, 0)) ? s++ : f++;
181 //nanosleep(&time, NULL);
182 (haptic_vibratepattern("FEEDBACK_PATTERN_SIP", 2, 0)) ? s++ : f++;
183 //nanosleep(&time, NULL);
184 (haptic_getstate(0)) ? s++ : f++;
185 (haptic_showhandlelist()) ? s++ : f++;
186 (haptic_issupported("FEEDBACK_PATTERN_SIP")) ? s++ : f++;
189 if (NULL != success) *success = s;
190 if (NULL != fail) *fail = f;
193 static void haptic_init(void *data)
200 haptic_test_all(&success, &fail);
202 _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
205 static void haptic_exit(void *data)
210 static int haptic_unit(int argc, char **argv)
212 struct timespec time = {0,};
219 haptic_test_all(&success, &fail);
220 _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
221 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_GETCOUNT)) {
223 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_OPENDEVICE)) {
224 haptic_opendevice(atoi(argv[2]));
225 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_CLOSEDEVICE)) {
226 haptic_closedevice();
227 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_STOPDEVICE)) {
230 nanosleep(&time, NULL);
231 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_VIBRATEMONOTONE)) {
232 haptic_vibratemonotone(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
233 time.tv_sec = 1 + (atoi(argv[2]) / 1000);
234 nanosleep(&time, NULL);
235 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_VIBRATEEFFECT)) {
236 haptic_vibratepattern(argv[2], atoi(argv[3]), atoi(argv[4]));
238 nanosleep(&time, NULL);
239 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_GETSTATE)) {
240 haptic_getstate(atoi(argv[2]));
241 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_SHOWHANDLELIST)) {
242 haptic_showhandlelist();
243 } else if (0 == strcasecmp(argv[1], METHOD_HAPTIC_ISSUPPORTED)) {
244 haptic_issupported(argv[2]);
246 _E("Unknown test case.");
252 static const struct test_ops haptic_test_ops = {
253 .priority = TEST_PRIORITY_NORMAL,
260 TEST_OPS_REGISTER(&haptic_test_ops)