dbus: Add signature checking before using g_variant_get()
[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                 ret = request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle));
98         else
99                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
100
101         g_variant_unref(msg);
102
103         return ret;
104 }
105
106 static bool haptic_vibratemonotone(int duration, int level, int priority)
107 {
108         GVariant *msg;
109         int handle;
110         bool ret = FALSE;
111
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));
118
119         if (!msg) {
120                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
121                 return ret;
122         }
123
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));
126         else
127                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
128
129         g_variant_unref(msg);
130
131         return ret;
132 }
133
134 static bool haptic_vibratepattern(char *pattern, int level, int priority)
135 {
136         GVariant *msg;
137         int handle;
138         bool ret = FALSE;
139
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));
146
147         if (!msg) {
148                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
149                 return ret;
150         }
151
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));
154         else
155                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
156
157         g_variant_unref(msg);
158
159         return ret;
160 }
161
162 static bool haptic_stopdevice()
163 {
164         struct timespec time = {0,};
165         GVariant *msg;
166         int handle;
167         bool ret = FALSE;
168
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));
175
176         if (!msg) {
177                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
178                 return ret;
179         }
180
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);
184                 return ret;
185         }
186         g_variant_unref(msg);
187
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));
193
194         if (!msg) {
195                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_VIBRATEMONOTONE);
196                 return ret;
197         }
198         g_variant_unref(msg);
199
200         _D("Sleep 300ms.");
201         time.tv_nsec = 300 * NANO_SECOND_MULTIPLIER;
202         nanosleep(&time, NULL);
203         _D("Wakeup.");
204
205         return request_haptic_method(METHOD_HAPTIC_STOPDEVICE, g_variant_new("(u)", handle));
206 }
207
208 static bool haptic_getstate(int index)
209 {
210         _D("----------------------------------------------------------------------------------");
211         return request_haptic_method(METHOD_HAPTIC_GETSTATE, g_variant_new("(i)", index));
212 }
213
214 static bool haptic_showhandlelist()
215 {
216         GVariant *msg;
217
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,
223                 NULL);
224
225         if (!msg) {
226                 _E("Failed to call dbus method(%s): no reply", METHOD_HAPTIC_OPENDEVICE);
227                 return FALSE;
228         }
229         g_variant_unref(msg);
230         return TRUE;
231 }
232
233 static bool haptic_issupported(char *pattern)
234 {
235         _D("----------------------------------------------------------------------------------");
236         return request_haptic_method(METHOD_HAPTIC_ISSUPPORTED, g_variant_new("(s)", pattern));
237 }
238
239 void haptic_test_all(int *success, int *fail)
240 {
241         struct timespec time = {0,};
242         int s = 0;
243         int f = 0;
244
245         time.tv_sec = 1;
246
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++;
259
260
261         if (NULL != success)    *success = s;
262         if (NULL != fail)       *fail = f;
263 }
264
265 static void haptic_init(void *data)
266 {
267         int success = 0;
268         int fail = 0;
269
270         _I("Start test.");
271
272         haptic_test_all(&success, &fail);
273
274         _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
275 }
276
277 static void haptic_exit(void *data)
278 {
279         _I("End test.");
280 }
281
282 static int haptic_unit(int argc, char **argv)
283 {
284         struct timespec time = {0,};
285
286         if (argc < 2) {
287                 int success = 0;
288                 int fail = 0;
289
290                 _I("Start test.");
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)) {
294                 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)) {
300                 time.tv_sec = 1;
301                 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]));
309                 time.tv_sec = 2;
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]);
317         } else {
318                 _E("Unknown test case.");
319         }
320
321         return 0;
322 }
323
324 static const struct test_ops haptic_test_ops = {
325         .priority = TEST_PRIORITY_NORMAL,
326         .name     = "haptic",
327         .init     = haptic_init,
328         .exit    = haptic_exit,
329         .unit    = haptic_unit,
330 };
331
332 TEST_OPS_REGISTER(&haptic_test_ops)