• 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 "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 "osal_thread.h"
24 #include "osal_mutex.h"
25 #include "osal_time.h"
26 #include "securec.h"
27 #define HDF_LOG_TAG   cdc_acm_write
28 enum UsbSerialCmd {
29     USB_SERIAL_OPEN = 0,
30     USB_SERIAL_CLOSE,
31     USB_SERIAL_READ,
32     USB_SERIAL_WRITE,
33     USB_SERIAL_GET_BAUDRATE,
34     USB_SERIAL_SET_BAUDRATE,
35     USB_SERIAL_SET_PROP,
36     USB_SERIAL_GET_PROP,
37     USB_SERIAL_REGIST_PROP,
38 };
39 struct HdfSBuf *g_data;
40 struct HdfSBuf *g_reply;
41 struct HdfIoService *g_acmService;
42 
check_service()43 int32_t check_service()
44 {
45     if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
46         HDF_LOGE("%s: GetService err", __func__);
47         return HDF_FAILURE;
48     }
49     if (g_data == NULL || g_reply == NULL) {
50         HDF_LOGE("%s: GetService err", __func__);
51         return HDF_FAILURE;
52     }
53     return HDF_SUCCESS;
54 }
55 
acm_open()56 void acm_open()
57 {
58     int status;
59     g_acmService = HdfIoServiceBind("usbfn_cdcacm");
60     if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
61         HDF_LOGE("%s: GetService err", __func__);
62     }
63     g_data = HdfSBufObtainDefaultSize();
64     g_reply = HdfSBufObtainDefaultSize();
65     if (g_data == NULL || g_reply == NULL) {
66         HDF_LOGE("%s: GetService err", __func__);
67     }
68     status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
69     if (status) {
70         HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
71     }
72 }
73 
acm_close()74 void acm_close()
75 {
76     int status;
77     if (check_service()) {
78         HDF_LOGE("%s: GetService err", __func__);
79         return;
80     }
81     status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_CLOSE, g_data, g_reply);
82     if (status) {
83         HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
84     }
85     HdfSBufRecycle(g_data);
86     HdfSBufRecycle(g_reply);
87     HdfIoServiceRecycle(g_acmService);
88 }
89 
acm_write(const char * buf)90 void acm_write(const char *buf)
91 {
92     if (check_service()) {
93         HDF_LOGE("%s: GetService err", __func__);
94         return;
95     }
96     HdfSbufFlush(g_data);
97     (void)HdfSbufWriteString(g_data, buf);
98     int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_WRITE, g_data, g_reply);
99     if (status <= 0) {
100         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
101         return;
102     }
103     printf("acm_write:%s\n", buf);
104 }
105 
acm_read(char * str,int timeout)106 void acm_read(char *str, int timeout)
107 {
108     int ret;
109     uint32_t maxLen = 256;
110     if (check_service()) {
111         HDF_LOGE("%s: GetService err", __func__);
112         return;
113     }
114     while (timeout-- > 0) {
115         HdfSbufFlush(g_reply);
116         int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_READ, g_data, g_reply);
117         if (status) {
118             HDF_LOGE("%s: Dispatch USB_SERIAL_READ failed status = %d", __func__, status);
119             return;
120         }
121         const char *tmp = HdfSbufReadString(g_reply);
122         if (str && tmp && strlen(tmp) > 0) {
123             ret = memcpy_s(str, maxLen, tmp, strlen(tmp));
124             if (ret != EOK) {
125                 HDF_LOGE("%s:%d ret=%d memcpy_s error", ret);
126             }
127             printf("acm_read:%s\n", tmp);
128             return;
129         }
130         sleep(1);
131     }
132 }
133 
acm_prop_regist(const char * propName,const char * propValue)134 void acm_prop_regist(const char *propName, const char *propValue)
135 {
136     if (check_service()) {
137         HDF_LOGE("%s: GetService err", __func__);
138         return;
139     }
140     HdfSbufFlush(g_data);
141     (void)HdfSbufWriteString(g_data, propName);
142     (void)HdfSbufWriteString(g_data, propValue);
143     int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_REGIST_PROP, g_data, g_reply);
144     if (status) {
145         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
146         return;
147     }
148     printf("prop_regist:%s = %s\n", propName, propValue);
149 }
150 
acm_prop_write(const char * propName,const char * propValue)151 void acm_prop_write(const char *propName, const char *propValue)
152 {
153     if (check_service()) {
154         HDF_LOGE("%s: GetService err", __func__);
155         return;
156     }
157     HdfSbufFlush(g_data);
158     HdfSbufFlush(g_reply);
159     (void)HdfSbufWriteString(g_data, propName);
160     (void)HdfSbufWriteString(g_data, propValue);
161     if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
162         HDF_LOGE("%s: GetService err", __func__);
163     }
164     int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_SET_PROP, g_data, g_reply);
165     if (status) {
166         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
167         return;
168     }
169     printf("prop_write:%s = %s\n", propName, propValue);
170 }
171 
acm_prop_read(const char * propName,char * propValue)172 void acm_prop_read(const char *propName, char *propValue)
173 {
174     uint32_t maxLen = 256;
175     if (check_service()) {
176         HDF_LOGE("%s: GetService err", __func__);
177         return;
178     }
179     HdfSbufFlush(g_data);
180     HdfSbufFlush(g_reply);
181     (void)HdfSbufWriteString(g_data, propName);
182     int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_GET_PROP, g_data, g_reply);
183     if (status) {
184         HDF_LOGE("%s: Dispatch USB_SERIAL_GET_PROP failed status = %d", __func__, status);
185         return;
186     }
187     const char *tmp = HdfSbufReadString(g_reply);
188     if (propValue && tmp && strlen(tmp) > 0) {
189         errno_t err = memcpy_s(propValue, maxLen, tmp, strlen(tmp));
190         if (err != EOK) {
191             HDF_LOGE("%s:%d err=%d memcpy_s error", err);
192         }
193         printf("prop_read:%s\n", tmp);
194         return;
195     }
196 }
197