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 <inttypes.h>
18 #include <securec.h>
19 #include <signal.h>
20
21 #define HDF_LOG_TAG USB_HOST_DDK_TEST
22
23 #ifdef __LITEOS_USB_HOST_DDK_TEST__
24 typedef enum {
25 CMD_OPEN_PARM = 0,
26 CMD_CLOSE_PARM,
27 CMD_WRITE_PARM,
28 CMD_READ_PARM,
29 CMD_GET_BAUDRATE,
30 CMD_SET_BAUDRATE,
31 CMD_WRITE_DATA_SYNC,
32 CMD_READ_DATA_SYNC,
33 CMD_CLASS_CTRL_SYNC,
34 CMD_STD_CTRL_GET_DESCRIPTOR_CMD,
35 CMD_STD_CTRL_GET_STATUS_CMD,
36 CMD_STD_CTRL_GET_CONFIGURATION,
37 CMD_STD_CTRL_GET_INTERFACE,
38 CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC,
39 CMD_ADD_INTERFACE,
40 CMD_REMOVE_INTERFACE,
41 } SerialOPCmd;
42 #endif
43
44 #define BUFFER_MAX_LEN 1024
45 #define SPEED_SLEEP_TIME 300
46
47 struct HdfSBuf *g_data = NULL;
48 struct HdfSBuf *g_reply = NULL;
49 #ifdef __LITEOS_USB_HOST_DDK_TEST__
50 static struct HdfIoService *g_acmService = NULL;
51 #else
52 struct HdfRemoteService *g_acmService = NULL;
53 #endif
54 static bool g_exitFlag = false;
55
UsbObtainSbuf(void)56 int32_t UsbObtainSbuf(void)
57 {
58 if (g_acmService == NULL) {
59 HDF_LOGE("%s:%d GetService err", __func__, __LINE__);
60 return HDF_FAILURE;
61 }
62
63 #ifdef __LITEOS_USB_HOST_DDK_TEST__
64 g_data = HdfSbufObtainDefaultSize();
65 g_reply = HdfSbufObtainDefaultSize();
66 #else
67 g_data = HdfSbufTypedObtain(SBUF_IPC);
68 g_reply = HdfSbufTypedObtain(SBUF_IPC);
69 #endif
70 if (g_data == NULL || g_reply == NULL) {
71 HDF_LOGE("%s:%d HdfSbufTypedObtain err", __func__, __LINE__);
72 return HDF_FAILURE;
73 }
74 return HDF_SUCCESS;
75 }
76
UsbHostDdkTestInit(const char * apiType)77 int32_t UsbHostDdkTestInit(const char *apiType)
78 {
79 const char *acmRawServiceName = "usbhost_acm_rawapi_service";
80 const char *acmServiceName = "usbhost_acm_pnp_service";
81 const char *ecmServiceName = "usbhost_ecm_pnp_service";
82 #ifndef __LITEOS_USB_HOST_DDK_TEST__
83 struct HDIServiceManager *servmgr = HDIServiceManagerGet();
84 if (servmgr == NULL) {
85 HDF_LOGE("%s:%d HDIServiceManagerGet err", __func__, __LINE__);
86 return HDF_FAILURE;
87 }
88 #endif
89 if (apiType == NULL) {
90 return HDF_FAILURE;
91 }
92 if (!strcmp(apiType, "-SDK")) {
93 HDF_LOGI("%s:%d test SDK API, service=%s", __func__, __LINE__, acmServiceName);
94 #ifdef __LITEOS_USB_HOST_DDK_TEST__
95 g_acmService = HdfIoServiceBind(acmServiceName);
96 #else
97 g_acmService = servmgr->GetService(servmgr, acmServiceName);
98 #endif
99 } else if (!strcmp(apiType, "-RAW")) {
100 HDF_LOGI("%s:%d test RAW API, service=%s", __func__, __LINE__, acmRawServiceName);
101 #ifdef __LITEOS_USB_HOST_DDK_TEST__
102 g_acmService = HdfIoServiceBind(acmRawServiceName);
103 #else
104 g_acmService = servmgr->GetService(servmgr, acmRawServiceName);
105 #endif
106 } else if (!strcmp(apiType, "-ECM")) {
107 HDF_LOGI("%s:%d test ECM API, service=%s", __func__, __LINE__, ecmServiceName);
108 #ifdef __LITEOS_USB_HOST_DDK_TEST__
109 g_acmService = HdfIoServiceBind(ecmServiceName);
110 #else
111 g_acmService = servmgr->GetService(servmgr, ecmServiceName);
112 #endif
113 } else {
114 HDF_LOGE("%s:%d apiType=%s is not define", __func__, __LINE__, apiType);
115 return HDF_FAILURE;
116 }
117 #ifndef __LITEOS_USB_HOST_DDK_TEST__
118 HDIServiceManagerRelease(servmgr);
119 #endif
120 if (UsbObtainSbuf() != HDF_SUCCESS) {
121 return HDF_FAILURE;
122 }
123 return HDF_SUCCESS;
124 }
125
TestModuleWriteLog(int32_t cmdType,const char * str)126 static void TestModuleWriteLog(int32_t cmdType, const char *str)
127 {
128 #ifdef __LITEOS_USB_HOST_DDK_TEST__
129 bool runFlag = false;
130 #else
131 bool runFlag = true;
132 #endif
133 if (runFlag) {
134 int32_t ret;
135 char buffer[BUFFER_MAX_LEN];
136 FILE *fp = NULL;
137 struct timeval time;
138
139 gettimeofday(&time, NULL);
140 switch (cmdType) {
141 case HOST_ACM_SYNC_READ:
142 case HOST_ACM_ASYNC_READ:
143 fp = fopen("/data/acm_read_xts", "a+");
144 ret = snprintf_s(buffer, BUFFER_MAX_LEN, BUFFER_MAX_LEN - 1,
145 "[XTSCHECK] %d.%06d, recv data[%s] from device\n", time.tv_sec, time.tv_usec, str);
146 break;
147 case HOST_ACM_SYNC_WRITE:
148 case HOST_ACM_ASYNC_WRITE:
149 fp = fopen("/data/acm_write_xts", "a+");
150 ret = snprintf_s(buffer, BUFFER_MAX_LEN, BUFFER_MAX_LEN - 1,
151 "[XTSCHECK] %d.%06d, send data[%s] to device\n", time.tv_sec, time.tv_usec, str);
152 break;
153 case HOST_ACM_CTRL_READ:
154 fp = fopen("/data/acm_read_xts", "a+");
155 ret = snprintf_s(buffer, BUFFER_MAX_LEN, BUFFER_MAX_LEN - 1, "[XTSCHECK] %d.%06d, %s\n", time.tv_sec,
156 time.tv_usec, str);
157 break;
158 case HOST_ACM_CTRL_WRITE:
159 fp = fopen("/data/acm_write_xts", "a+");
160 ret = snprintf_s(buffer, BUFFER_MAX_LEN, BUFFER_MAX_LEN - 1,
161 "[XTSCHECK] %d.%06d, usb serial control command[%s] done\n", time.tv_sec, time.tv_usec, str);
162 break;
163 case HOST_ACM_SPEED_TEST:
164 default:
165 ret = HDF_SUCCESS;
166 }
167
168 if (ret != HDF_SUCCESS) {
169 (void)fclose(fp);
170 return;
171 }
172 (void)fwrite(buffer, strlen(buffer), 1, fp);
173 (void)fclose(fp);
174 }
175 }
176
UsbHostDdkTestAsyncRead(char * const readSbuf)177 int32_t UsbHostDdkTestAsyncRead(char * const readSbuf)
178 {
179 HdfSbufFlush(g_reply);
180 #ifdef __LITEOS_USB_HOST_DDK_TEST__
181 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_READ_PARM, g_data, g_reply);
182 #else
183 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_READ_PARM, g_data, g_reply);
184 #endif
185 if (status < 0) {
186 g_exitFlag = true;
187 printf("%s:%d Dispatch USB_SERIAL_READ failed status = %d\n", __func__, __LINE__, status);
188 HDF_LOGE("%s:%d Dispatch USB_SERIAL_READ failed status = %d", __func__, __LINE__, status);
189 return status;
190 }
191
192 const char *tmp = HdfSbufReadString(g_reply);
193 if (tmp && strlen(tmp) > 0) {
194 if (readSbuf != NULL) {
195 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
196 if (err != EOK) {
197 HDF_LOGE("%s:%d err=%d", __func__, __LINE__, err);
198 }
199 }
200 printf("%s:%d %s-%zu!\n", __func__, __LINE__, tmp, strlen(tmp));
201 HDF_LOGD("%s:%d %s-%zu!", __func__, __LINE__, tmp, strlen(tmp));
202 TestModuleWriteLog(HOST_ACM_ASYNC_READ, tmp);
203 }
204
205 return HDF_SUCCESS;
206 }
207
UsbHostDdkTestAsyncWrite(const char * buf)208 void UsbHostDdkTestAsyncWrite(const char *buf)
209 {
210 if (buf == NULL) {
211 return;
212 }
213 HdfSbufFlush(g_data);
214
215 if (!HdfSbufWriteString(g_data, buf)) {
216 printf("%s:%d HdfSbufWriteString error\n", __func__, __LINE__);
217 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
218 return;
219 }
220
221 #ifdef __LITEOS_USB_HOST_DDK_TEST__
222 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_WRITE_PARM, g_data, g_reply);
223 #else
224 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_WRITE_PARM, g_data, g_reply);
225 #endif
226 if (status <= HDF_SUCCESS) {
227 g_exitFlag = true;
228 printf("%s:%d Dispatch USB_SERIAL_WRITE failed status = %d\n", __func__, __LINE__, status);
229 HDF_LOGE("%s:%d Dispatch USB_SERIAL_WRITE failed status = %d", __func__, __LINE__, status);
230 return;
231 }
232
233 printf("%s:%d %s-%zu!\n", __func__, __LINE__, buf, strlen(buf));
234 HDF_LOGI("%s:%d %s-%zu!\n", __func__, __LINE__, buf, strlen(buf));
235 TestModuleWriteLog(HOST_ACM_ASYNC_WRITE, buf);
236 }
237
UsbHostDdkTestSyncRead(char * readSbuf)238 void UsbHostDdkTestSyncRead(char *readSbuf)
239 {
240 HdfSbufFlush(g_reply);
241
242 #ifdef __LITEOS_USB_HOST_DDK_TEST__
243 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_READ_DATA_SYNC, g_data, g_reply);
244 #else
245 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_READ_DATA_SYNC, g_data, g_reply);
246 #endif
247 if (status != HDF_SUCCESS) {
248 g_exitFlag = true;
249 printf("%s:%d Dispatch CMD_READ_DATA_SYNC failed status = %d\n", __func__, __LINE__, status);
250 HDF_LOGE("%s:%d Dispatch CMD_READ_DATA_SYNC failed status = %d", __func__, __LINE__, status);
251 return;
252 }
253
254 const char *tmp = HdfSbufReadString(g_reply);
255 if (tmp && strlen(tmp) > 0) {
256 if (readSbuf != NULL) {
257 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
258 if (err != EOK) {
259 HDF_LOGE("%s:%d err=%d", __func__, __LINE__, err);
260 }
261 }
262 printf("%s:%d %s-%zu!\n", __func__, __LINE__, tmp, strlen(tmp));
263 HDF_LOGD("%s:%d %s-%zu !", __func__, __LINE__, tmp, strlen(tmp));
264 TestModuleWriteLog(HOST_ACM_SYNC_READ, tmp);
265 }
266 }
267
UsbHostDdkTestSyncWrite(const char * buf)268 void UsbHostDdkTestSyncWrite(const char *buf)
269 {
270 HdfSbufFlush(g_data);
271
272 if (!HdfSbufWriteString(g_data, buf)) {
273 printf("%s:%d HdfSbufWriteString error\n", __func__, __LINE__);
274 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
275 return;
276 }
277
278 #ifdef __LITEOS_USB_HOST_DDK_TEST__
279 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_WRITE_DATA_SYNC, g_data, g_reply);
280 #else
281 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_WRITE_DATA_SYNC, g_data, g_reply);
282 #endif
283 if (status < HDF_SUCCESS) {
284 g_exitFlag = true;
285 printf("%s:%d Dispatch CMD_WRITE_DATA_SYNC failed status = %d\n", __func__, __LINE__, status);
286 HDF_LOGE("%s:%d Dispatch CMD_WRITE_DATA_SYNC failed status = %d", __func__, __LINE__, status);
287 return;
288 }
289
290 printf("%s:%d %s-%zu!\n", __func__, __LINE__, buf, strlen(buf));
291 HDF_LOGD("%s:%d %s-%zu!", __func__, __LINE__, buf, strlen(buf));
292 TestModuleWriteLog(HOST_ACM_SYNC_WRITE, buf);
293 }
294
UsbHostDdkTestCtrlClass(char * readSbuf)295 void UsbHostDdkTestCtrlClass(char *readSbuf)
296 {
297 HdfSbufFlush(g_reply);
298
299 #ifdef __LITEOS_USB_HOST_DDK_TEST__
300 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_CLASS_CTRL_SYNC, g_data, g_reply);
301 #else
302 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_CLASS_CTRL_SYNC, g_data, g_reply);
303 #endif
304 if (status < 0) {
305 g_exitFlag = true;
306 printf("%s:%d Dispatch CMD_CLASS_CTRL_SYNC failed status = %d\n", __func__, __LINE__, status);
307 HDF_LOGE("%s:%d Dispatch CMD_CLASS_CTRL_SYNC failed status = %d", __func__, __LINE__, status);
308 } else {
309 if (readSbuf != NULL) {
310 const char tmp[] = "CMD_CLASS_CTRL";
311 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
312 if (err != EOK) {
313 HDF_LOGE("%s:%d err = %d", __func__, __LINE__, err);
314 }
315 }
316 printf("%s:%d usb serial control CMD_CLASS_CTRL command done\n", __func__, __LINE__);
317 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_CLASS_CTRL");
318 }
319 }
320
UsbHostDdkTestStdGetDes(char * readSbuf)321 void UsbHostDdkTestStdGetDes(char *readSbuf)
322 {
323 HdfSbufFlush(g_reply);
324 #ifdef __LITEOS_USB_HOST_DDK_TEST__
325 int32_t status =
326 g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_STD_CTRL_GET_DESCRIPTOR_CMD, g_data, g_reply);
327 #else
328 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_STD_CTRL_GET_DESCRIPTOR_CMD, g_data, g_reply);
329 #endif
330 if (status < 0) {
331 g_exitFlag = true;
332 printf("%s:%d Dispatch UsbHostDdkTestStdGetDes failed status = %d\n", __func__, __LINE__, status);
333 HDF_LOGE("%s:%d Dispatch UsbHostDdkTestStdGetDes failed status = %d", __func__, __LINE__, status);
334 }
335
336 printf("%s:%d usb serial control CMD_STD_CTRL_GET_DESCRIPTOR command done\n", __func__, __LINE__);
337 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_STD_CTRL_GET_DESCRIPTOR");
338 const char *tmp = HdfSbufReadString(g_reply);
339 if (tmp && strlen(tmp) > 0) {
340 if (readSbuf != NULL) {
341 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
342 if (err != EOK) {
343 HDF_LOGE("%s:%d err = %d", __func__, __LINE__, err);
344 }
345 }
346 printf("%s:%d %s!\n", __func__, __LINE__, tmp);
347 TestModuleWriteLog(HOST_ACM_CTRL_READ, tmp);
348 } else {
349 printf("%s:%d HdfSbufReadBuffer failed\n", __func__, __LINE__);
350 HDF_LOGE("%s:%d HdfSbufReadBuffer failed", __func__, __LINE__);
351 }
352 }
353
UsbHostDdkTestStdGetDesAsync(char * readSbuf)354 void UsbHostDdkTestStdGetDesAsync(char *readSbuf)
355 {
356 HdfSbufFlush(g_reply);
357 #ifdef __LITEOS_USB_HOST_DDK_TEST__
358 int32_t status =
359 g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC, g_data, g_reply);
360 #else
361 int32_t status =
362 g_acmService->dispatcher->Dispatch(g_acmService, CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC, g_data, g_reply);
363 #endif
364 if (status < 0) {
365 g_exitFlag = true;
366 printf("%s:%d Dispatch CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC failed status = %d\n", __func__, __LINE__, status);
367 HDF_LOGE("%s:%d Dispatch CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC failed status = %d", __func__, __LINE__, status);
368 return;
369 }
370
371 printf("%s:%d usb serial control CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC command done\n", __func__, __LINE__);
372 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC");
373 const char *tmp = HdfSbufReadString(g_reply);
374 if (tmp && strlen(tmp) > 0) {
375 if (readSbuf != NULL) {
376 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
377 if (err != EOK) {
378 HDF_LOGE("%s:%d err = %d", __func__, __LINE__, err);
379 }
380 }
381 printf("%s:%d %s!\n", __func__, __LINE__, tmp);
382 TestModuleWriteLog(HOST_ACM_CTRL_READ, tmp);
383 } else {
384 printf("%s:%d HdfSbufReadBuffer failed\n", __func__, __LINE__);
385 HDF_LOGE("%s:%d HdfSbufReadBuffer failed", __func__, __LINE__);
386 }
387 }
388
UsbHostDdkTestStdGetStatus(char * readSbuf)389 void UsbHostDdkTestStdGetStatus(char *readSbuf)
390 {
391 uint16_t data = 0;
392
393 HdfSbufFlush(g_reply);
394 #ifdef __LITEOS_USB_HOST_DDK_TEST__
395 int32_t status =
396 g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_STD_CTRL_GET_STATUS_CMD, g_data, g_reply);
397 #else
398 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_STD_CTRL_GET_STATUS_CMD, g_data, g_reply);
399 #endif
400 if (status < 0) {
401 g_exitFlag = true;
402 printf("%s:%d Dispatch CMD_STD_CTRL_GET_STATUS_CMD failed status = %d\n", __func__, __LINE__, status);
403 HDF_LOGE("%s:%d Dispatch CMD_STD_CTRL_GET_STATUS_CMD failed status = %d", __func__, __LINE__, status);
404 }
405
406 status = HdfSbufReadUint16(g_reply, &data);
407 if (!status) {
408 printf("%s:%d HdfSbufReadBuffer status = %d\n", __func__, __LINE__, status);
409 HDF_LOGE("%s:%d HdfSbufReadBuffer status = %d", __func__, __LINE__, status);
410 } else {
411 if (readSbuf != NULL) {
412 const char tmp[DATA_MAX_LEN] = "CMD_STD_CTRL_GET_STATUS";
413 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
414 if (err != EOK) {
415 HDF_LOGE("%s:%d err = %d", __func__, __LINE__, err);
416 }
417 }
418 printf("%s:%d usb serial control CMD_STD_CTRL_GET_STATUS command done,data = %hu\n", __func__, __LINE__, data);
419 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_STD_CTRL_GET_STATUS");
420 }
421 }
422
TestStdGetConf(void)423 void TestStdGetConf(void)
424 {
425 uint8_t data;
426
427 HdfSbufFlush(g_reply);
428
429 #ifdef __LITEOS_USB_HOST_DDK_TEST__
430 int32_t status =
431 g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_STD_CTRL_GET_CONFIGURATION, g_data, g_reply);
432 #else
433 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_STD_CTRL_GET_CONFIGURATION, g_data, g_reply);
434 #endif
435 if (status < 0) {
436 g_exitFlag = true;
437 printf("%s:%d Dispatch CMD_STD_CTRL_GET_CONFIGURATION failed status = %d\n", __func__, __LINE__, status);
438 HDF_LOGE("%s:%d Dispatch CMD_STD_CTRL_GET_CONFIGURATION failed status = %d", __func__, __LINE__, status);
439 }
440
441 printf("%s:%d usb serial control CMD_STD_CTRL_GET_CONFIGURATION command done\n", __func__, __LINE__);
442 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_STD_CTRL_GET_CONFIGURATION");
443 status = HdfSbufReadUint8(g_reply, &data);
444 if (status < 0) {
445 printf("%s:%d HdfSbufReadBuffer status = %d\n", __func__, __LINE__, status);
446 HDF_LOGE("%s:%d HdfSbufReadBuffer status = %d", __func__, __LINE__, status);
447 }
448 }
449
TestStdGetInterface(void)450 void TestStdGetInterface(void)
451 {
452 uint8_t data;
453
454 HdfSbufFlush(g_reply);
455 #ifdef __LITEOS_USB_HOST_DDK_TEST__
456 int32_t status =
457 g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_STD_CTRL_GET_INTERFACE, g_data, g_reply);
458 #else
459 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_STD_CTRL_GET_INTERFACE, g_data, g_reply);
460 #endif
461 if (status < 0) {
462 g_exitFlag = true;
463 printf("%s:%d Dispatch CMD_STD_CTRL_GET_INTERFACE failed status = %d\n", __func__, __LINE__, status);
464 HDF_LOGE("%s:%d Dispatch CMD_STD_CTRL_GET_INTERFACE failed status = %d", __func__, __LINE__, status);
465 }
466
467 printf("%s:%d usb serial control CMD_STD_CTRL_GET_INTERFACE command done\n", __func__, __LINE__);
468 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_STD_CTRL_GET_INTERFACE");
469 status = HdfSbufReadUint8(g_reply, &data);
470 if (status < 0) {
471 printf("%s:%d HdfSbufReadBuffer status = %d\n", __func__, __LINE__, status);
472 HDF_LOGE("%s:%d HdfSbufReadBuffer status = %d", __func__, __LINE__, status);
473 }
474 }
475
UsbHostDdkTestSetBaudrate(uint32_t value)476 void UsbHostDdkTestSetBaudrate(uint32_t value)
477 {
478 HdfSbufFlush(g_data);
479
480 if (!HdfSbufWriteUint32(g_data, value)) {
481 printf("%s:%d HdfSbufWriteString error\n", __func__, __LINE__);
482 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
483 return;
484 }
485
486 #ifdef __LITEOS_USB_HOST_DDK_TEST__
487 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_SET_BAUDRATE, g_data, g_reply);
488 #else
489 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_SET_BAUDRATE, g_data, g_reply);
490 #endif
491 if (status != HDF_SUCCESS) {
492 g_exitFlag = true;
493 printf("%s:%d Dispatch CMD_SET_BAUDRATE failed status = %d\n", __func__, __LINE__, status);
494 HDF_LOGE("%s:%d Dispatch CMD_SET_BAUDRATE failed status = %d", __func__, __LINE__, status);
495 return;
496 }
497
498 printf("%s:%d CMD_SET_BAUDRATE success\n", __func__, __LINE__);
499 HDF_LOGI("%s:%d CMD_SET_BAUDRATE success", __func__, __LINE__);
500 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_SET_BAUDRATE");
501 }
502
UsbHostDdkTestGetBaudrate(char * readSbuf)503 void UsbHostDdkTestGetBaudrate(char *readSbuf)
504 {
505 uint32_t value = 0;
506
507 HdfSbufFlush(g_reply);
508 #ifdef __LITEOS_USB_HOST_DDK_TEST__
509 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_GET_BAUDRATE, g_data, g_reply);
510 #else
511 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_GET_BAUDRATE, g_data, g_reply);
512 #endif
513 if (status < 0) {
514 g_exitFlag = true;
515 printf("%s:%d Dispatch CMD_GET_BAUDRATE failed status = %d\n", __func__, __LINE__, status);
516 HDF_LOGE("%s:%d Dispatch CMD_GET_BAUDRATE failed status = %d", __func__, __LINE__, status);
517 return;
518 }
519
520 if (HdfSbufReadUint32(g_reply, &value) == true) {
521 if (readSbuf != NULL) {
522 const char tmp[] = "CMD_GET_BAUDRATE";
523 errno_t err = memcpy_s(readSbuf, DATA_MAX_LEN, tmp, strlen(tmp));
524 if (err != EOK) {
525 HDF_LOGE("%s:%d err=%d", __func__, __LINE__, err);
526 }
527 }
528 printf("%s:%d baudrate=%u usb serial control CMD_GET_BAUDRATE command done\n", __func__, __LINE__, value);
529 TestModuleWriteLog(HOST_ACM_CTRL_WRITE, "CMD_GET_BAUDRATE");
530 } else {
531 printf("%s:%d HdfSbufReadUint32 failed!\n", __func__, __LINE__);
532 HDF_LOGD("%s:%d HdfSbufReadUint32 failed!", __func__, __LINE__);
533 }
534 }
535
UsbHostDdkTestAddInterface(uint32_t value)536 void UsbHostDdkTestAddInterface(uint32_t value)
537 {
538 HdfSbufFlush(g_data);
539
540 if (!HdfSbufWriteUint32(g_data, value)) {
541 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
542 return;
543 }
544
545 #ifdef __LITEOS_USB_HOST_DDK_TEST__
546 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_ADD_INTERFACE, g_data, g_reply);
547 #else
548 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_ADD_INTERFACE, g_data, g_reply);
549 #endif
550 if (status != HDF_SUCCESS) {
551 g_exitFlag = true;
552 HDF_LOGE("%s:%d Dispatch CMD_ADD_INTERFACE failed status = %d", __func__, __LINE__, status);
553 return;
554 }
555
556 printf("%s:%d CMD_ADD_INTERFACE success!\n", __func__, __LINE__);
557 HDF_LOGD("%s:%d CMD_ADD_INTERFACE success!", __func__, __LINE__);
558 }
559
UsbHostDdkTestRemoveInterface(uint32_t value)560 void UsbHostDdkTestRemoveInterface(uint32_t value)
561 {
562 HdfSbufFlush(g_data);
563
564 if (!HdfSbufWriteUint32(g_data, value)) {
565 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
566 return;
567 }
568
569 #ifdef __LITEOS_USB_HOST_DDK_TEST__
570 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_REMOVE_INTERFACE, g_data, g_reply);
571 #else
572 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_REMOVE_INTERFACE, g_data, g_reply);
573 #endif
574 if (status != HDF_SUCCESS) {
575 g_exitFlag = true;
576 HDF_LOGE("%s:%d Dispatch CMD_REMOVE_INTERFACE failed status = %d", __func__, __LINE__, status);
577 return;
578 }
579
580 printf("%s:%d CMD_REMOVE_INTERFACE success!\n", __func__, __LINE__);
581 HDF_LOGD("%s:%d CMD_REMOVE_INTERFACE success!", __func__, __LINE__);
582 }
583
UsbHostDdkTestOpen(int32_t cmdType)584 int32_t UsbHostDdkTestOpen(int32_t cmdType)
585 {
586 if (g_exitFlag) {
587 HDF_LOGD("%s:%d g_exitFlag is true!", __func__, __LINE__);
588 return HDF_FAILURE;
589 }
590
591 HdfSbufFlush(g_data);
592 if (!HdfSbufWriteInt32(g_data, cmdType)) {
593 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
594 return HDF_FAILURE;
595 }
596
597 #ifdef __LITEOS_USB_HOST_DDK_TEST__
598 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_OPEN_PARM, g_data, g_reply);
599 #else
600 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_OPEN_PARM, g_data, g_reply);
601 #endif
602 if (status != HDF_SUCCESS) {
603 g_exitFlag = true;
604 HDF_LOGE("%s:%d Dispatch CMD_OPEN_PARM status=%d err", __func__, __LINE__, status);
605 }
606
607 return status;
608 }
609
UsbHostDdkTestClose(int32_t cmdType)610 int32_t UsbHostDdkTestClose(int32_t cmdType)
611 {
612 if (g_exitFlag) {
613 HDF_LOGD("%s:%d g_exitFlag is true!", __func__, __LINE__);
614 return HDF_FAILURE;
615 }
616
617 HdfSbufFlush(g_data);
618 if (!HdfSbufWriteInt32(g_data, cmdType)) {
619 HDF_LOGE("%s:%d HdfSbufWriteString error", __func__, __LINE__);
620 return HDF_FAILURE;
621 }
622
623 #ifdef __LITEOS_USB_HOST_DDK_TEST__
624 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, CMD_CLOSE_PARM, g_data, g_reply);
625 #else
626 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, CMD_CLOSE_PARM, g_data, g_reply);
627 #endif
628 if (status != HDF_SUCCESS) {
629 g_exitFlag = true;
630 HDF_LOGE("%s:%d Dispatch CMD_CLOSE_PARM status=%d err", __func__, __LINE__, status);
631 }
632
633 return status;
634 }
635
TestExit(void)636 void TestExit(void)
637 {
638 #ifdef __LITEOS_USB_HOST_DDK_TEST__
639 HdfIoServiceRecycle(g_acmService);
640 #else
641 HdfRemoteServiceRecycle(g_acmService);
642 #endif
643 HdfSbufRecycle(g_data);
644 HdfSbufRecycle(g_reply);
645 }
646
TestGetExitFlag(void)647 bool TestGetExitFlag(void)
648 {
649 return g_exitFlag;
650 }
651