• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
25 #define HDF_LOG_TAG   cdc_acm_write
26 
27 struct HdfSBuf *g_data;
28 struct HdfSBuf *g_reply;
29 struct HdfRemoteService *g_acmService;
30 
TestWrite(char * buf)31 static void TestWrite(char *buf)
32 {
33     HdfSbufFlush(g_data);
34     (void)HdfSbufWriteString(g_data, buf);
35     int status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_WRITE, g_data, g_reply);
36     if (status <= 0) {
37         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
38     }
39 }
40 
41 #define STR_LEN 1024
42 #define NUM_INPUT 2
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45     char str[STR_LEN] = {0};
46     int status;
47     FILE *fp = NULL;
48     struct timeval time;
49     struct HDIServiceManager *servmgr = HDIServiceManagerGet();
50     if (servmgr == NULL) {
51         HDF_LOGE("%s: HDIServiceManagerGet err", __func__);
52         return HDF_FAILURE;
53     }
54     g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm");
55     HDIServiceManagerRelease(servmgr);
56     if (g_acmService == NULL) {
57         HDF_LOGE("%s: GetService err", __func__);
58         return HDF_FAILURE;
59     }
60 
61     g_data = HdfSBufTypedObtain(SBUF_IPC);
62     g_reply = HdfSBufTypedObtain(SBUF_IPC);
63     if (g_data == NULL || g_reply == NULL) {
64         HDF_LOGE("%s: GetService err", __func__);
65         return HDF_FAILURE;
66     }
67 
68     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
69     if (status) {
70         HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
71         return HDF_FAILURE;
72     }
73     if (argc >= NUM_INPUT) {
74         gettimeofday(&time, NULL);
75         fp = fopen("/data/acm_write_xts", "a+");
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         (void)fwrite(str, strlen(str), 1, fp);
83         (void)fclose(fp);
84         TestWrite(argv[1]);
85     }
86     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
87     if (status) {
88         HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
89         return HDF_FAILURE;
90     }
91 
92     HdfSBufRecycle(g_data);
93     HdfSBufRecycle(g_reply);
94 
95     return 0;
96 }
97