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 int32_t 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 return;
63 }
64 g_data = HdfSbufObtainDefaultSize();
65 g_reply = HdfSbufObtainDefaultSize();
66 if (g_data == NULL || g_reply == NULL) {
67 HDF_LOGE("%s: GetService err", __func__);
68 }
69 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
70 if (status) {
71 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
72 }
73 }
74
acm_close()75 void acm_close()
76 {
77 int32_t status;
78 if (check_service()) {
79 HDF_LOGE("%s: GetService err", __func__);
80 return;
81 }
82 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_CLOSE, g_data, g_reply);
83 if (status) {
84 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
85 }
86 HdfSbufRecycle(g_data);
87 HdfSbufRecycle(g_reply);
88 HdfIoServiceRecycle(g_acmService);
89 }
90
acm_write(const char * buf)91 void acm_write(const char *buf)
92 {
93 if (check_service()) {
94 HDF_LOGE("%s: GetService err", __func__);
95 return;
96 }
97 HdfSbufFlush(g_data);
98 (void)HdfSbufWriteString(g_data, buf);
99 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_WRITE, g_data, g_reply);
100 if (status <= 0) {
101 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
102 return;
103 }
104 printf("acm_write:%s\n", buf);
105 }
106
acm_read(char * str,int32_t timeout)107 void acm_read(char *str, int32_t timeout)
108 {
109 int32_t ret;
110 uint32_t maxLen = 256;
111 if (check_service()) {
112 HDF_LOGE("%s: GetService err", __func__);
113 return;
114 }
115 while (timeout-- > 0) {
116 HdfSbufFlush(g_reply);
117 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_READ, g_data, g_reply);
118 if (status) {
119 HDF_LOGE("%s: Dispatch USB_SERIAL_READ failed status = %d", __func__, status);
120 return;
121 }
122 const char *tmp = HdfSbufReadString(g_reply);
123 if (str && tmp && strlen(tmp) > 0) {
124 ret = memcpy_s(str, maxLen, tmp, strlen(tmp));
125 if (ret != EOK) {
126 HDF_LOGE("%s:%d ret=%d memcpy_s error", __func__, __LINE__, ret);
127 }
128 printf("acm_read:%s\n", tmp);
129 return;
130 }
131 sleep(1);
132 }
133 }
134
acm_prop_regist(const char * propName,const char * propValue)135 void acm_prop_regist(const char *propName, const char *propValue)
136 {
137 if (check_service()) {
138 HDF_LOGE("%s: GetService err", __func__);
139 return;
140 }
141 HdfSbufFlush(g_data);
142 (void)HdfSbufWriteString(g_data, propName);
143 (void)HdfSbufWriteString(g_data, propValue);
144 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_REGIST_PROP, g_data, g_reply);
145 if (status) {
146 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
147 return;
148 }
149 printf("prop_regist:%s = %s\n", propName, propValue);
150 }
151
acm_prop_write(const char * propName,const char * propValue)152 void acm_prop_write(const char *propName, const char *propValue)
153 {
154 if (check_service()) {
155 HDF_LOGE("%s: GetService err", __func__);
156 return;
157 }
158 HdfSbufFlush(g_data);
159 HdfSbufFlush(g_reply);
160 (void)HdfSbufWriteString(g_data, propName);
161 (void)HdfSbufWriteString(g_data, propValue);
162 if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
163 HDF_LOGE("%s: GetService err", __func__);
164 return;
165 }
166 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_SET_PROP, g_data, g_reply);
167 if (status) {
168 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
169 return;
170 }
171 printf("prop_write:%s = %s\n", propName, propValue);
172 }
173
acm_prop_read(const char * propName,char * propValue)174 void acm_prop_read(const char *propName, char *propValue)
175 {
176 uint32_t maxLen = 256;
177 if (check_service()) {
178 HDF_LOGE("%s: GetService err", __func__);
179 return;
180 }
181 HdfSbufFlush(g_data);
182 HdfSbufFlush(g_reply);
183 (void)HdfSbufWriteString(g_data, propName);
184 int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_GET_PROP, g_data, g_reply);
185 if (status) {
186 HDF_LOGE("%s:%d Dispatch USB_SERIAL_GET_PROP failed status = %d", __func__, __LINE__, status);
187 return;
188 }
189 const char *tmp = HdfSbufReadString(g_reply);
190 if (propValue && tmp && strlen(tmp) > 0) {
191 errno_t err = memcpy_s(propValue, maxLen, tmp, strlen(tmp));
192 if (err != EOK) {
193 HDF_LOGE("%s:%d err=%d memcpy_s error", __func__, __LINE__, err);
194 }
195 printf("prop_read:%s\n", tmp);
196 return;
197 }
198 }
199