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