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 <stdio.h>
17 #include <sys/time.h>
18 #include <unistd.h>
19 #include "hdf_io_service_if.h"
20 #include "hdf_log.h"
21 #include "osal_mem.h"
22 #include "osal_mutex.h"
23 #include "osal_thread.h"
24 #include "osal_time.h"
25 #include "securec.h"
26 #define HDF_LOG_TAG cdc_acm_read
27
28 enum UsbSerialCmd {
29 USB_SERIAL_OPEN = 0,
30 USB_SERIAL_CLOSE,
31 USB_SERIAL_READ,
32 USB_SERIAL_WRITE,
33 USB_SERIAL_GET_BAUDRATE,
34 USB_SERIAL_SET_BAUDRATE,
35 USB_SERIAL_SET_PROP,
36 USB_SERIAL_GET_PROP,
37 USB_SERIAL_REGIST_PROP,
38 };
39
40 struct HdfSBuf *g_data;
41 struct HdfSBuf *g_reply;
42 struct HdfIoService *g_acmService;
43
44 #define STR_LEN 8192
45 #define SLEEP_READ 100
TestRead(FILE * fp)46 static void TestRead(FILE *fp)
47 {
48 int ret;
49 char str[STR_LEN] = {0};
50 struct timeval time;
51 HdfSbufFlush(g_reply);
52 int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_READ, g_data, g_reply);
53 if (status) {
54 HDF_LOGE("%s: Dispatch USB_SERIAL_READ failed status = %d", __func__, status);
55 return;
56 }
57 const char *tmp = HdfSbufReadString(g_reply);
58 if (tmp && strlen(tmp) > 0) {
59 gettimeofday(&time, NULL);
60 ret = snprintf_s(str, STR_LEN, STR_LEN - 1, "[XTSCHECK] %d.%06d, recv data[%s] from host\n",
61 time.tv_sec, time.tv_usec, tmp);
62 if (ret < 0) {
63 HDF_LOGE("%s: snprintf_s failed", __func__);
64 return;
65 }
66 (void)fwrite(str, strlen(str), 1, fp);
67 fflush(fp);
68 }
69 OsalMDelay(SLEEP_READ);
70 }
71
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74 int status;
75
76 g_acmService = HdfIoServiceBind("usbfn_cdcacm");
77 if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
78 HDF_LOGE("%s: GetService err", __func__);
79 return HDF_FAILURE;
80 }
81
82 g_data = HdfSBufObtainDefaultSize();
83 g_reply = HdfSBufObtainDefaultSize();
84 if (g_data == NULL || g_reply == NULL) {
85 HDF_LOGE("%s: GetService err", __func__);
86 return HDF_FAILURE;
87 }
88
89 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
90 if (status) {
91 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
92 return HDF_FAILURE;
93 }
94 FILE *fp = fopen("/data/acm_read_xts", "a+");
95 if (fp == NULL) {
96 HDF_LOGE("%s: fopen err", __func__);
97 return HDF_FAILURE;
98 }
99 while (1) {
100 TestRead(fp);
101 }
102 (void)fclose(fp);
103 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_CLOSE, g_data, g_reply);
104 if (status) {
105 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
106 return HDF_FAILURE;
107 }
108
109 HdfSBufRecycle(g_data);
110 HdfSBufRecycle(g_reply);
111
112 return 0;
113 }
114