1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "osal_thread.h"
17 #include "signal.h"
18 #include "usbhost_ddk_test.h"
19
20 #define HDF_LOG_TAG USB_HOST_DDK_TEST
21 #define STR_LEN 256
22
23 #define PARAM_CMD_LENGTH 3
24 #define PARAM_SET_CMD_LEN 3
25 #define PARAM_GET_CMD_LEN 2
26 #define ARGV_CMD_API_TYPE 1
27 #define ARGV_CMD_TYPE (PARAM_GET_CMD_LEN - ARGV_CMD_API_TYPE)
28 #define ARGV_CMD_PARAM (PARAM_SET_CMD_LEN - ARGV_CMD_API_TYPE)
29 #define READ_SLEEP_TIME 500
30 int32_t run;
31
32 #ifdef __LITEOS_USB_HOST_DDK_TEST__
33 static struct OsalThread g_Getchar;
34 #endif
35
TestHelp(void)36 static void TestHelp(void)
37 {
38 printf("usage: usbhost_ddk_test [options]\n");
39 printf("\n");
40 printf("options include:\n");
41 printf(" -h, --help : help info\n");
42 printf(" -A, --DDK : test host ddk api function for acm\n");
43 printf(" -a, --RAW : test host raw api function for acm\n");
44 printf(" -E, --ECM : test host ddk api function for ecm\n");
45 printf(" -R, --syncRead : test sync read for acm\n");
46 printf(" -W, --syncWrite : test sync write for acm\n");
47 printf(" -r, --asyncRead : test async read for acm\n");
48 printf(" -w, --asyncWrite : test async write for acm\n");
49 printf(" -c, --ctrlClassSync : test class ctrl cmd for acm\n");
50 printf(" -s, --ctrlGetStatus : test get status ctrl cmd for acm\n");
51 printf(" -C, --ctrlSyncDescriptor : test sync get descriptor ctrl cmd for acm\n");
52 printf(" -d, --ctrlAsyncDescriptor : test async get descriptor ctrl cmd for acm\n");
53 printf(" -g, --ctrlGetConfiguration : test get configuration ctrl cmd for acm\n");
54 printf(" -i, --ctrlGetInterface : test get interface ctrl cmd for acm\n");
55 printf(" -S, --speedTest : test speed for acm\n");
56 printf(" -B, --setBaudrate : test set baudrate for acm\n");
57 printf(" -b, --getBaudrate : test get baudrate for acm\n");
58 printf(" -I, --addInterface [index} : test add interface for acm(not raw api function) and ecm\n");
59 printf(" -D, --removeInterface [index] : test remove interface for acm(not raw api function) and ecm\n");
60 printf("\n");
61 printf("Examples:\n");
62 printf(" usbhost_ddk_test -AR : test sync read for acm by host ddk api function\n");
63 printf(" usbhost_ddk_test -aw 123 : test async write 123 for acm by host raw api function\n");
64 }
65
TestParaseCommand(int32_t paramNum,const char * cmdParam,int32_t * cmdType,char * apiType)66 static int32_t TestParaseCommand(int32_t paramNum, const char *cmdParam, int32_t *cmdType, char *apiType)
67 {
68 if ((cmdParam == NULL) || (cmdType == NULL) || (apiType == NULL) || (strlen(cmdParam) < PARAM_CMD_LENGTH)) {
69 HDF_LOGE("%s:%d command or cmdType is NULL or cmdParam length is error", __func__, __LINE__);
70 return HDF_ERR_INVALID_PARAM;
71 }
72
73 uint32_t len = strlen(cmdParam);
74 for (uint32_t i = 0; i < len; i++) {
75 switch (cmdParam[i]) {
76 case 'A':
77 strcpy_s(apiType, DATA_MAX_LEN, "-SDK");
78 break;
79 case 'a':
80 strcpy_s(apiType, DATA_MAX_LEN, "-RAW");
81 break;
82 case 'E':
83 strcpy_s(apiType, DATA_MAX_LEN, "-ECM");
84 break;
85 case 'R':
86 if (paramNum != PARAM_GET_CMD_LEN) {
87 return HDF_FAILURE;
88 }
89 *cmdType = HOST_ACM_SYNC_READ;
90 break;
91 case 'W':
92 if (paramNum != PARAM_SET_CMD_LEN) {
93 return HDF_FAILURE;
94 }
95 *cmdType = HOST_ACM_SYNC_WRITE;
96 break;
97 case 'r':
98 if (paramNum != PARAM_GET_CMD_LEN) {
99 return HDF_FAILURE;
100 }
101 *cmdType = HOST_ACM_ASYNC_READ;
102 break;
103 case 'w':
104 if (paramNum != PARAM_SET_CMD_LEN) {
105 return HDF_FAILURE;
106 }
107 *cmdType = HOST_ACM_ASYNC_WRITE;
108 break;
109 case 'c':
110 if (paramNum != PARAM_GET_CMD_LEN) {
111 return HDF_FAILURE;
112 }
113 *cmdType = HOST_ACM_CTRL_CLASS_SYNC;
114 break;
115 case 's':
116 if (paramNum != PARAM_GET_CMD_LEN) {
117 return HDF_FAILURE;
118 }
119 *cmdType = HOST_ACM_CTRL_GET_STATUS;
120 break;
121 case 'C':
122 if (paramNum != PARAM_GET_CMD_LEN) {
123 return HDF_FAILURE;
124 }
125 *cmdType = HOST_ACM_CTRL_SYNC_DESCRIPTOR;
126 break;
127 case 'd':
128 if (paramNum != PARAM_GET_CMD_LEN) {
129 return HDF_FAILURE;
130 }
131 *cmdType = HOST_ACM_CTRL_ASYNC_DESCRIPTOR;
132 break;
133 case 'g':
134 if (paramNum != PARAM_GET_CMD_LEN) {
135 return HDF_FAILURE;
136 }
137 *cmdType = HOST_ACM_CTRL_GET_CONFIGURATION;
138 break;
139 case 'i':
140 if (paramNum != PARAM_GET_CMD_LEN) {
141 return HDF_FAILURE;
142 }
143 *cmdType = HOST_ACM_CTRL_GET_INTERFACE;
144 break;
145 case 'S':
146 if (paramNum != PARAM_GET_CMD_LEN) {
147 return HDF_FAILURE;
148 }
149 *cmdType = HOST_ACM_SPEED_TEST;
150 break;
151 case 'B':
152 if (paramNum != PARAM_SET_CMD_LEN) {
153 return HDF_FAILURE;
154 }
155 *cmdType = HOST_ACM_SET_BAUDRATE;
156 break;
157 case 'b':
158 if (paramNum != PARAM_GET_CMD_LEN) {
159 return HDF_FAILURE;
160 }
161 *cmdType = HOST_ACM_GET_BAUDRATE;
162 break;
163 case 'I':
164 if (paramNum != PARAM_SET_CMD_LEN) {
165 return HDF_FAILURE;
166 }
167 *cmdType = HOST_ACM_ADD_INTERFACE;
168 break;
169 case 'D':
170 if (paramNum != PARAM_SET_CMD_LEN) {
171 return HDF_FAILURE;
172 }
173 *cmdType = HOST_ACM_REMOVE_INTERFACE;
174 break;
175 case '-':
176 break;
177 default:
178 return HDF_FAILURE;
179 }
180 }
181
182 return HDF_SUCCESS;
183 }
184
TestCmdLoopOther(int32_t cmdType,const char * param)185 static void TestCmdLoopOther(int32_t cmdType, const char *param)
186 {
187 switch (cmdType) {
188 case HOST_ACM_CTRL_CLASS_SYNC:
189 UsbHostDdkTestCtrlClass(NULL);
190 break;
191 case HOST_ACM_CTRL_GET_STATUS:
192 UsbHostDdkTestStdGetStatus(NULL);
193 break;
194 case HOST_ACM_CTRL_SYNC_DESCRIPTOR:
195 UsbHostDdkTestStdGetDes(NULL);
196 break;
197 case HOST_ACM_CTRL_ASYNC_DESCRIPTOR:
198 UsbHostDdkTestStdGetDesAsync(NULL);
199 usleep(READ_SLEEP_TIME);
200 break;
201 case HOST_ACM_CTRL_GET_CONFIGURATION:
202 TestStdGetConf();
203 break;
204 case HOST_ACM_CTRL_GET_INTERFACE:
205 TestStdGetInterface();
206 break;
207 case HOST_ACM_SET_BAUDRATE:
208 UsbHostDdkTestSetBaudrate(atoi(param));
209 break;
210 case HOST_ACM_GET_BAUDRATE:
211 UsbHostDdkTestGetBaudrate(NULL);
212 break;
213 case HOST_ACM_ADD_INTERFACE:
214 UsbHostDdkTestAddInterface(atoi(param));
215 break;
216 case HOST_ACM_REMOVE_INTERFACE:
217 UsbHostDdkTestRemoveInterface(atoi(param));
218 break;
219 default:
220 break;
221 }
222 }
223
TestCmdLoop(int32_t cmdType,const char * param)224 static int32_t TestCmdLoop(int32_t cmdType, const char *param)
225 {
226 bool loopFlag = true;
227 bool asyncFlag = false;
228 int32_t cnt = 0;
229
230 if (TestGetExitFlag() == true) {
231 HDF_LOGD("%s:%d g_exitFlag is true!", __func__, __LINE__);
232 return HDF_FAILURE;
233 }
234
235 while ((loopFlag) && (!run)) {
236 switch (cmdType) {
237 case HOST_ACM_SYNC_READ:
238 UsbHostDdkTestSyncRead(NULL);
239 break;
240 case HOST_ACM_SYNC_WRITE:
241 UsbHostDdkTestSyncWrite(param);
242 break;
243 case HOST_ACM_ASYNC_READ:
244 if (UsbHostDdkTestAsyncRead(NULL) != HDF_SUCCESS) {
245 #ifdef __LITEOS_USB_HOST_DDK_TEST__
246 if (cnt++ > 10) {
247 asyncFlag = false;
248 return HDF_DEV_ERR_NO_DEVICE_SERVICE;
249 }
250 #else
251 asyncFlag = false;
252 #endif
253 } else {
254 cnt = 0;
255 asyncFlag = true;
256 usleep(READ_SLEEP_TIME);
257 }
258 break;
259 case HOST_ACM_ASYNC_WRITE:
260 UsbHostDdkTestAsyncWrite(param);
261 break;
262 default:
263 TestCmdLoopOther(cmdType, param);
264 break;
265 }
266
267 if (!asyncFlag) {
268 loopFlag = false;
269 }
270 }
271 return HDF_SUCCESS;
272 }
273
274 #ifdef __LITEOS_USB_HOST_DDK_TEST__
SigHandle(void * arg)275 static void *SigHandle(void *arg)
276 {
277 (void)arg;
278 run = 0;
279 return NULL;
280 }
281
GetCharThread(void * arg)282 static int32_t GetCharThread(void *arg)
283 {
284 char str[STR_LEN] = {0};
285 while (run) {
286 str[0] = (char)getchar();
287 }
288 return 0;
289 }
290 #endif
291
292 #define HDF_PROCESS_STACK_SIZE 100000
StartThreadGetChar()293 static int32_t StartThreadGetChar()
294 {
295 #ifdef __LITEOS_USB_HOST_DDK_TEST__
296 int32_t ret;
297 struct OsalThreadParam threadCfg;
298 memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg));
299 threadCfg.name = "get char process";
300 threadCfg.priority = OSAL_THREAD_PRI_DEFAULT;
301 threadCfg.stackSize = HDF_PROCESS_STACK_SIZE;
302
303 ret = OsalThreadCreate(&g_Getchar, (OsalThreadEntry)GetCharThread, NULL);
304 if (HDF_SUCCESS != ret) {
305 HDF_LOGE("%s:%d OsalThreadCreate failed, ret=%d ", __func__, __LINE__, ret);
306 return HDF_ERR_DEVICE_BUSY;
307 }
308
309 ret = OsalThreadStart(&g_Getchar, &threadCfg);
310 if (HDF_SUCCESS != ret) {
311 HDF_LOGE("%s:%d OsalThreadStart failed, ret=%d ", __func__, __LINE__, ret);
312 return HDF_ERR_DEVICE_BUSY;
313 }
314 #endif
315 return 0;
316 }
317
main(int32_t argc,char * argv[])318 int32_t main(int32_t argc, char *argv[])
319 {
320 int32_t status;
321 int32_t cmdType;
322 char apiType[DATA_MAX_LEN];
323
324 if ((argc < ARGV_CMD_TYPE) || (argc < PARAM_GET_CMD_LEN) || (argv[ARGV_CMD_TYPE] == NULL)) {
325 HDF_LOGE("%s:%d invalid parma, argc=%d", __func__, __LINE__, argc);
326 return HDF_FAILURE;
327 }
328
329 if ((argc == PARAM_GET_CMD_LEN) &&
330 ((!strcmp(argv[ARGV_CMD_TYPE], "-h")) || (!strcmp(argv[ARGV_CMD_TYPE], "--help")))) {
331 TestHelp();
332 return HDF_SUCCESS;
333 }
334 run = 1;
335
336 StartThreadGetChar();
337 status = TestParaseCommand(argc, argv[ARGV_CMD_TYPE], &cmdType, apiType);
338 if (status != HDF_SUCCESS) {
339 run = 0;
340 HDF_LOGE("%s:%d TestParaseCommand status=%d err", __func__, __LINE__, status);
341 TestHelp();
342 return status;
343 }
344
345 status = UsbHostDdkTestInit(apiType);
346 if (status != HDF_SUCCESS) {
347 run = 0;
348 HDF_LOGE("%s:%d UsbHostDdkTestInit status=%d err", __func__, __LINE__, status);
349 return status;
350 }
351
352 if (UsbHostDdkTestOpen(cmdType) != HDF_SUCCESS) {
353 goto OUT;
354 }
355 #ifdef __LITEOS_USB_HOST_DDK_TEST__
356 (void)signal(SIGINT, SigHandle);
357 #endif
358 status = TestCmdLoop(cmdType, argv[ARGV_CMD_PARAM]);
359 if (status == HDF_DEV_ERR_NO_DEVICE_SERVICE) {
360 goto OUT;
361 }
362
363 if ((cmdType != HOST_ACM_ADD_INTERFACE) && (cmdType != HOST_ACM_REMOVE_INTERFACE)) {
364 if (UsbHostDdkTestClose(cmdType) != HDF_SUCCESS) {
365 goto OUT;
366 }
367 }
368
369 OUT:
370 run = 0;
371 TestExit();
372 return HDF_SUCCESS;
373 }
374