Change dbus function name
[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, int *out_val)
31 {
32         GVariant *reply;
33         int reply_val, ret_dbus;
34         bool ret = FALSE;
35
36         ret_dbus = gdbus_call_sync_with_reply(VIBRATOR_BUS_NAME,
37                 VIBRATOR_PATH_HAPTIC,
38                 VIBRATOR_INTERFACE_HAPTIC,
39                 method, param, &reply);
40
41         if (ret_dbus < 0) {
42                 _E("Failed to call %s: no reply", method);
43                 return ret;
44         }
45
46         if (!g_variant_get_safe(reply, "(i)", &reply_val))
47                 _E("Failed to call %s: no message", method);
48         else {
49                 if ((reply_val == -ENOTSUP) || (reply_val == -ENOSYS)) {
50                         _I("Not supported feature(%s): %d", method, reply_val);
51                         ret = TRUE;
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);
56                 } else {
57                         _I("Success. %s: %d", method, reply_val);
58                         ret = TRUE;
59                 }
60         }
61
62         g_variant_unref(reply);
63
64         if (out_val)
65                 *out_val = reply_val;
66
67         return ret;
68 }
69
70 static bool haptic_getcount()
71 {
72         _D("----------------------------------------------------------------------------------");
73         return request_haptic_method(METHOD_HAPTIC_GETCOUNT, NULL, NULL);
74 }
75
76 static bool haptic_opendevice(int index)
77 {
78         _D("----------------------------------------------------------------------------------");
79         return request_haptic_method(METHOD_HAPTIC_OPENDEVICE, g_variant_new("(i)", index), NULL);
80 }
81
82 static bool haptic_closedevice()
83 {
84         int handle;
85         bool ret = FALSE;
86
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);
90         else
91                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
92
93         return ret;
94 }
95
96 static bool haptic_vibratemonotone(int duration, int level, int priority)
97 {
98         int handle;
99         bool ret = FALSE;
100
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);
106         } else
107                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
108
109         return ret;
110 }
111
112 static bool haptic_vibratepattern(char *pattern, int level, int priority)
113 {
114         int handle;
115         bool ret = FALSE;
116
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);
122         } else
123                 _E("Failed to call dbus method(%s): no message", METHOD_HAPTIC_OPENDEVICE);
124
125         return ret;
126 }
127
128 static bool haptic_stopdevice()
129 {
130         int handle;
131         bool ret = FALSE;
132
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);
136                 return ret;
137         }
138
139         if (request_haptic_method(METHOD_HAPTIC_VIBRATEMONOTONE, g_variant_new("(uiii)", handle, 1000, 2, 0), NULL)) {
140                 _D("Sleep 300ms.");
141                 usleep(300*1000);
142                 _D("Wakeup.");
143                 ret = request_haptic_method(METHOD_HAPTIC_STOPDEVICE, g_variant_new("(u)", handle), NULL);
144         }
145         request_haptic_method(METHOD_HAPTIC_CLOSEDEVICE, g_variant_new("(u)", handle), NULL);
146         return ret;
147 }
148
149 static bool haptic_getstate(int index)
150 {
151         _D("----------------------------------------------------------------------------------");
152         return request_haptic_method(METHOD_HAPTIC_GETSTATE, g_variant_new("(i)", index), NULL);
153 }
154
155 static bool haptic_showhandlelist()
156 {
157         _D("----------------------------------------------------------------------------------");
158         return request_haptic_method(METHOD_HAPTIC_SHOWHANDLELIST, NULL, NULL);
159 }
160
161 static bool haptic_issupported(char *pattern)
162 {
163         _D("----------------------------------------------------------------------------------");
164         return request_haptic_method(METHOD_HAPTIC_ISSUPPORTED, g_variant_new("(s)", pattern), NULL);
165 }
166
167 void haptic_test_all(int *success, int *fail)
168 {
169         struct timespec time = {0,};
170         int s = 0;
171         int f = 0;
172
173         time.tv_sec = 1;
174
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++;
187
188
189         if (NULL != success)    *success = s;
190         if (NULL != fail)       *fail = f;
191 }
192
193 static void haptic_init(void *data)
194 {
195         int success = 0;
196         int fail = 0;
197
198         _I("Start test.");
199
200         haptic_test_all(&success, &fail);
201
202         _I("Total=%d Success=%d Fail=%d", success+fail, success, fail);
203 }
204
205 static void haptic_exit(void *data)
206 {
207         _I("End test.");
208 }
209
210 static int haptic_unit(int argc, char **argv)
211 {
212         struct timespec time = {0,};
213
214         if (argc < 2) {
215                 int success = 0;
216                 int fail = 0;
217
218                 _I("Start test.");
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)) {
222                 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)) {
228                 time.tv_sec = 1;
229                 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]));
237                 time.tv_sec = 2;
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]);
245         } else {
246                 _E("Unknown test case.");
247         }
248
249         return 0;
250 }
251
252 static const struct test_ops haptic_test_ops = {
253         .priority = TEST_PRIORITY_NORMAL,
254         .name     = "haptic",
255         .init     = haptic_init,
256         .exit    = haptic_exit,
257         .unit    = haptic_unit,
258 };
259
260 TEST_OPS_REGISTER(&haptic_test_ops)