• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 /**
17  * @addtogroup USBSerialDDK
18  * @{
19  *
20  * @brief Provides USB SERIAL DDK types and declares the macros, enumerated variables, and\n
21  * data structures required by the USB SERIAL DDK APIs.
22  *
23  * @since 18
24  */
25 
26 /**
27  * @file usb_serial_api.h
28  *
29  * @brief Declares the USB SERIAL DDK interfaces for the usb host to access an usb serial device.
30  *
31  * @kit DriverDevelopmentKit
32  * @library libusb_serial.z.so
33  * @syscap SystemCapability.Driver.UsbSerial.Extension
34  * @since 18
35  */
36 
37 #ifndef DDK_USB_SERIAL_API_H
38 #define DDK_USB_SERIAL_API_H
39 
40 #include <stdint.h>
41 #include "usb_serial_types.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @brief Initializes the USB serial DDK.
49  *
50  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
51  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
52  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
53  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk init error.
54  * @since 18
55  */
56 int32_t OH_UsbSerial_Init(void);
57 
58 /**
59  * @brief Releases the USB serial DDK.
60  *
61  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
62  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
63  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
64  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
65  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
66  * @since 18
67  */
68 int32_t OH_UsbSerial_Release(void);
69 
70 /**
71  * @brief Open USB serial device by deviceId.
72  *
73  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
74  * @param deviceId ID of the device to be operated.
75  * @param interfaceIndex Interface index, which corresponds to interface which supports USB Protocol ACM.
76  * @param dev Device handle.
77  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
78  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
79  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null.
80  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
81  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
82  *         {@link USB_SERIAL_DDK_MEMORY_ERROR} insufficient memory.
83  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
84  *         {@link USB_SERIAL_DDK_DEVICE_NOT_FOUND} device or interface not found.
85  * @since 18
86  */
87 int32_t OH_UsbSerial_Open(uint64_t deviceId, uint8_t interfaceIndex, UsbSerial_Device **dev);
88 
89 /**
90  * @brief Close USB serial device.
91  *
92  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
93  * @param dev Device handle.
94  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
95  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
96  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1. dev is null.\n
97  *         2. *dev is null.
98  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
99  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
100  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
101  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
102  * @since 18
103  */
104 int32_t OH_UsbSerial_Close(UsbSerial_Device **dev);
105 
106 /**
107  * @brief Read bytesRead into buff from UsbSerial device.
108  *
109  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
110  * @param dev Device handle.
111  * @param buff Received data from a serial device.
112  * @param bufferSize The buffer size.
113  * @param bytesRead Actual bytes read.
114  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
115  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
116  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n
117  *         2.buff is null; 3.bufferSize is zero; 4.bytesRead is null.
118  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
119  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
120  *         {@link USB_SERIAL_DDK_MEMORY_ERROR} the buff is outside accessible address space error.
121  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
122  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
123  * @since 18
124  */
125 int32_t OH_UsbSerial_Read(UsbSerial_Device *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesRead);
126 
127 /**
128  * @brief Write bytesWritten from buff to UsbSerial device.
129  *
130  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
131  * @param dev Device handle.
132  * @param buff Serial information write to device.
133  * @param bufferSize The buffer size.
134  * @param bytesWritten Actual bytes written.
135  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
136  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
137  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n
138  *         2.buff is null; 3.bufferSize is zero; 4. bytesWritten is null.
139  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
140  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
141  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
142  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
143  * @since 18
144  */
145 int32_t OH_UsbSerial_Write(UsbSerial_Device *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesWritten);
146 
147 /**
148  * @brief Set the serial port baud rate.
149  *
150  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
151  * @param dev Device handle.
152  * @param baudRate Serial port baud rate set to connect device.
153  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
154  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
155  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null.
156  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
157  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
158  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
159  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
160  * @since 18
161  */
162 int32_t OH_UsbSerial_SetBaudRate(UsbSerial_Device *dev, uint32_t baudRate);
163 
164 /**
165  * @brief Set the serial port parameters.
166  *
167  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
168  * @param dev Device handle.
169  * @param params Serial port params set to connect device.
170  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
171  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
172  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n
173  *         2.params is null.
174  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
175  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
176  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
177  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
178  * @since 18
179  */
180 int32_t OH_UsbSerial_SetParams(UsbSerial_Device *dev, UsbSerial_Params *params);
181 
182 /**
183  * @brief Set the timeout in milliseconds.
184  * The timeout value defaults to 0 without calling this function.
185  *
186  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
187  * @param dev Device handle.
188  * @param timeout Set to -1 to infinite timeout, 0 to return immediately with any data,
189  * or > 0 to wait for data for a specified number of milliseconds.
190  * Timeout will be rounded to the nearest 100ms. Maximum value limited to 25500ms.
191  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
192  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
193  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n
194  *         2. timeout < -1 or timeout > 25500.
195  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
196  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
197  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
198  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
199  * @since 18
200  */
201 int32_t OH_UsbSerial_SetTimeout(UsbSerial_Device *dev, int timeout);
202 
203 /**
204  * @brief Set the flow control.
205  * It defaults to no flow control without calling this function.
206  *
207  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
208  * @param dev Device handle.
209  * @param flowControl {@link UsbSerial_FlowControl} flow control mode.
210  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
211  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
212  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null.
213  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
214  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
215  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
216  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
217  * @since 18
218  */
219 int32_t OH_UsbSerial_SetFlowControl(UsbSerial_Device *dev, UsbSerial_FlowControl flowControl);
220 
221 /**
222  * @brief Flush the input and output buffers after finish writting.
223  *
224  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
225  * @param dev Device handle.
226  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
227  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
228  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null.
229  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
230  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
231  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
232  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
233  * @since 18
234  */
235 int32_t OH_UsbSerial_Flush(UsbSerial_Device *dev);
236 
237 /**
238  * @brief Flush the input buffer, and the data in the buffer will be cleared directly.
239  *
240  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
241  * @param dev Device handle.
242  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
243  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
244  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null.
245  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
246  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
247  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
248  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
249  * @since 18
250  */
251 int32_t OH_UsbSerial_FlushInput(UsbSerial_Device *dev);
252 
253 /**
254  * @brief Flush the output buffer, and the data in the buffer will be cleared directly.
255  *
256  * @permission ohos.permission.ACCESS_DDK_USB_SERIAL
257  * @param dev Device handle.
258  * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful.
259  *         {@link USB_SERIAL_DDK_NO_PERM} permission check failed.
260  *         {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null.
261  *         {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init.
262  *         {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed.
263  *         {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error.
264  *         {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation.
265  * @since 18
266  */
267 int32_t OH_UsbSerial_FlushOutput(UsbSerial_Device *dev);
268 #ifdef __cplusplus
269 }
270 #endif /* __cplusplus */
271 #endif // DDK_USB_SERIAL_API_H
272 /** @} */
273