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