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 "usb_dev_test.h"
25
26 #define HDF_LOG_TAG cdc_acm_write
27
28 static struct HdfSBuf *g_data;
29 static struct HdfSBuf *g_reply;
30 static struct HdfRemoteService *g_acmService;
31
TestWrite(char * buf)32 static void TestWrite(char *buf)
33 {
34 HdfSbufFlush(g_data);
35 (void)HdfSbufWriteString(g_data, buf);
36 int32_t status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_WRITE, g_data, g_reply);
37 if (status <= 0) {
38 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
39 }
40 }
41
42 #define STR_LEN 1024
43 #define NUM_INPUT 2
acm_write(int32_t argc,const char * argv[])44 int32_t acm_write(int32_t argc, const char *argv[])
45 {
46 char str[STR_LEN] = {0};
47 int32_t status;
48 FILE *fp = NULL;
49 struct timeval time;
50 struct HDIServiceManager *servmgr = HDIServiceManagerGet();
51 if (servmgr == NULL) {
52 HDF_LOGE("%s: HDIServiceManagerGet err", __func__);
53 return HDF_FAILURE;
54 }
55 g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm");
56 HDIServiceManagerRelease(servmgr);
57 if (g_acmService == NULL) {
58 HDF_LOGE("%s: GetService err", __func__);
59 return HDF_FAILURE;
60 }
61
62 g_data = HdfSbufTypedObtain(SBUF_IPC);
63 g_reply = HdfSbufTypedObtain(SBUF_IPC);
64 if (g_data == NULL || g_reply == NULL) {
65 HDF_LOGE("%s: GetService err", __func__);
66 return HDF_FAILURE;
67 }
68
69 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
70 if (status) {
71 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
72 return HDF_FAILURE;
73 }
74 if (argc >= NUM_INPUT) {
75 gettimeofday(&time, NULL);
76 status = snprintf_s(str, STR_LEN, STR_LEN - 1, "[XTSCHECK] %d.%06d, send data[%s] to host\n",
77 time.tv_sec, time.tv_usec, argv[1]);
78 if (status < 0) {
79 HDF_LOGE("%s: snprintf_s failed", __func__);
80 return HDF_FAILURE;
81 }
82 fp = fopen("/data/acm_write_xts", "a+");
83 if (!fp) {
84 HDF_LOGE("%s: fopen failed", __func__);
85 return HDF_FAILURE;
86 }
87 (void)fwrite(str, strlen(str), 1, fp);
88 (void)fclose(fp);
89 TestWrite((char *)argv[1]);
90 }
91 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
92 if (status) {
93 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
94 return HDF_FAILURE;
95 }
96
97 HdfSbufRecycle(g_data);
98 HdfSbufRecycle(g_reply);
99
100 return 0;
101 }
102