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 "securec.h"
17 #include "hdf_log.h"
18 #include "osal_mem.h"
19 #include "hdf_io_service_if.h"
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include "osal_thread.h"
25 #include "osal_mutex.h"
26 #include "osal_time.h"
27 #include "osal_file.h"
28 #include "securec.h"
29 #define HDF_LOG_TAG cdc_acm_write
30
31 enum UsbSerialCmd {
32 USB_SERIAL_OPEN = 0,
33 USB_SERIAL_CLOSE,
34 USB_SERIAL_READ,
35 USB_SERIAL_WRITE,
36 USB_SERIAL_GET_BAUDRATE,
37 USB_SERIAL_SET_BAUDRATE,
38 USB_SERIAL_SET_PROP,
39 USB_SERIAL_GET_PROP,
40 USB_SERIAL_REGIST_PROP,
41 };
42
43 static struct HdfSBuf *g_data;
44 static struct HdfSBuf *g_reply;
45 static struct HdfIoService *g_acmService;
46
TestWrite(const char * buf)47 static void TestWrite(const char *buf)
48 {
49 HdfSbufFlush(g_data);
50 (void)HdfSbufWriteString(g_data, buf);
51 int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_WRITE, g_data, g_reply);
52 if (status <= 0) {
53 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
54 }
55 }
56
57 #define NUM_INPUT 2
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60 int status;
61
62 g_acmService = HdfIoServiceBind("usbfn_cdcacm");
63 if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
64 HDF_LOGE("%s: GetService err", __func__);
65 return HDF_FAILURE;
66 }
67
68 g_data = HdfSBufObtainDefaultSize();
69 g_reply = HdfSBufObtainDefaultSize();
70 if (g_data == NULL || g_reply == NULL) {
71 HDF_LOGE("%s: GetService err", __func__);
72 return HDF_FAILURE;
73 }
74
75 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
76 if (status) {
77 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
78 return HDF_FAILURE;
79 }
80
81 if (argc >= NUM_INPUT) {
82 TestWrite(argv[1]);
83 }
84
85 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_CLOSE, g_data, g_reply);
86 if (status) {
87 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
88 return HDF_FAILURE;
89 }
90
91 HdfSBufRecycle(g_data);
92 HdfSBufRecycle(g_reply);
93 HdfIoServiceRecycle(g_acmService);
94 return HDF_SUCCESS;
95 }
96
97