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