diff --git a/drivers/external_device_manager/serial/BUILD.gn b/drivers/external_device_manager/serial/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c2290201424fb847f8f8a482395f5702053a384a --- /dev/null +++ b/drivers/external_device_manager/serial/BUILD.gn @@ -0,0 +1,33 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_ndk_headers("usb_serial_header") { + dest_dir = "$ndk_headers_out_dir/usb_serial/" + sources = [ + "usb_serial_ddk_api.h", + "usb_serial_ddk_types.h", + ] +} + +ohos_ndk_library("libusb_serial_ndk") { + ndk_description_file = "./libusb_serial.ndk.json" + min_compact_version = "9" + output_name = "usb_serial_ndk" + system_capability = "SystemCapability.Driver.SERIAL.Extension" + system_capability_headers = [ + "serial/usb_serial_ddk_api.h", + "serial/usb_serial_ddk_types.h", + ] +} diff --git a/drivers/external_device_manager/serial/libusb_serial.ndk.json b/drivers/external_device_manager/serial/libusb_serial.ndk.json new file mode 100644 index 0000000000000000000000000000000000000000..2dabe7776d4edd10a90552916184c80332e2e471 --- /dev/null +++ b/drivers/external_device_manager/serial/libusb_serial.ndk.json @@ -0,0 +1,41 @@ +[ + { + "name": "OH_UsbSerial_Init" + }, + { + "name": "OH_UsbSerial_Release" + }, + { + "name": "OH_UsbSerial_Open" + }, + { + "name": "OH_UsbSerial_Close" + }, + { + "name": "OH_UsbSerial_Read" + }, + { + "name": "OH_UsbSerial_Write" + }, + { + "name": "OH_UsbSerial_SetBaudRate" + }, + { + "name": "OH_UsbSerial_SetParams" + }, + { + "name": "OH_UsbSerial_SetTimeout" + }, + { + "name": "OH_UsbSerial_SetFlowControl" + }, + { + "name": "OH_UsbSerial_Flush" + }, + { + "name": "OH_UsbSerial_FlushInput" + }, + { + "name": "OH_UsbSerial_FlushOutput" + } +] \ No newline at end of file diff --git a/drivers/external_device_manager/serial/usb_serial_ddk_api.h b/drivers/external_device_manager/serial/usb_serial_ddk_api.h new file mode 100644 index 0000000000000000000000000000000000000000..5d64ab61ddd430208974f54300459310274d01c8 --- /dev/null +++ b/drivers/external_device_manager/serial/usb_serial_ddk_api.h @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup SerialDdk + * @{ + * + * @brief Provides USB SERIAL DDK types and declares the macros, enumerated variables, and\n + * data structures required by the USB SERIAL DDK APIs. + * + * @syscap SystemCapability.Driver.SERIAL.Extension + * @since 16 + */ + +/** + * @file usb_serial_ddk_api.h + * + * @brief Declares the USB SERIAL DDK interfaces for the usb host to access an usb serial device. + * + * @kit DriverDevelopmentKit + * @library libusb_serial.z.so + * @syscap SystemCapability.Driver.SERIAL.Extension + * File to include: + * @since 16 + */ +#ifndef USB_SERIAL_DDK_API_H +#define USB_SERIAL_DDK_API_H + +#include +#include "usb_serial_ddk_types.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Initializes the USB serial DDK. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk init error. + * @since 16 + */ +int32_t OH_UsbSerial_Init(void); + +/** + * @brief Releases the USB serial DDK. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * @since 16 + */ +int32_t OH_UsbSerial_Release(void); + +/** + * @brief Open USB serial device by deviceId. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param deviceId ID of the device to be operated. + * @param interfaceIndex Interface index, which corresponds to interface which supports USB Protocol ACM. + * @param dev Device handle. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_MEMORY_ERROR} insufficient memory. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_DEVICE_NOT_FOUND} device or interface not found. + * @since 16 + */ +int32_t OH_UsbSerial_Open(uint64_t deviceId, uint8_t interfaceIndex, UsbSerial_DeviceHandle **dev); + +/** + * @brief Close USB serial device. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_Close(UsbSerial_DeviceHandle *dev); + +/** + * @brief Read bytesRead into buff from UsbSerial device. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @param buff Received data from a serial device. + * @param bufferSize The buffer size. + * @param bytesRead Actual bytes read. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n + * 2.buff is null; 3.bufferSize is zero; 4.bytesRead is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_MEMORY_ERROR} the buff is outside accessible address space error. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_Read(UsbSerial_DeviceHandle *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesRead); + +/** + * @brief Write bytesWritten from buff to UsbSerial device. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @param buff Serial information write to device. + * @param bufferSize The buffer size. + * @param bytesWritten Actual bytes written. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n + * 2.buff is null; 3.bufferSize is zero; 4. bytesWritten is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_MEMORY_ERROR} the buff is outside accessible address space error. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_Write(UsbSerial_DeviceHandle *dev, uint8_t *buff, uint32_t bufferSize, uint32_t *bytesWritten); + +/** + * @brief Set the serial port baud rate. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @param baudRate Serial port baud rate set to connect device. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_SetBaudRate(UsbSerial_DeviceHandle *dev, uint32_t baudRate); + +/** + * @brief Set the serial port parameters. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @param params Serial port params set to connect device. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n + * 2.params is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_SetParams(UsbSerial_DeviceHandle *dev, UsbSerial_Params *params); + +/** + * @brief Set the timeout in milliseconds. + * The timeout value defaults to 0 without calling this function. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @param timeout Set to -1 to infinite timeout, 0 to return immediately with any data, + * or > 0 to wait for data for a specified number of milliseconds. + * Timeout will be rounded to the nearest 100ms. Maximum value limited to 25500ms. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: 1.dev is null;\n + * 2. timeout < -1 or timeout > 25500. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_SetTimeout(UsbSerial_DeviceHandle *dev, int timeout); + +/** + * @brief Set the flow control. + * It defaults to no flow control without calling this function. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @param flowControl {@link UsbSerial_FlowControl} flow control mode. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_SetFlowControl(UsbSerial_DeviceHandle *dev, UsbSerial_FlowControl flowControl); + +/** + * @brief Flush the input and output buffers after finish writting. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_Flush(UsbSerial_DeviceHandle *dev); + +/** + * @brief Flush the input buffer. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_FlushInput(UsbSerial_DeviceHandle *dev); + +/** + * @brief Flush the output buffer. + * + * @permission ohos.permission.ACCESS_DDK_USB_SERIAL + * @param dev Device handle. + * @return {@link USB_SERIAL_DDK_SUCCESS} the operation is successful. + * {@link USB_SERIAL_DDK_NO_PERM} permission check failed. + * {@link USB_SERIAL_DDK_INVALID_PARAMETER} parameter check failed. Possible causes: dev is null. + * {@link USB_SERIAL_DDK_INIT_ERROR} the ddk not init. + * {@link USB_SERIAL_DDK_SERVICE_ERROR} communication with the ddk service failed. + * {@link USB_SERIAL_DDK_IO_ERROR} the ddk I/O error. + * {@link USB_SERIAL_DDK_INVALID_OPERATION} invalid operation. + * @since 16 + */ +int32_t OH_UsbSerial_FlushOutput(UsbSerial_DeviceHandle *dev); + +/** @} */ +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif // USB_DDK_API_H \ No newline at end of file diff --git a/drivers/external_device_manager/serial/usb_serial_ddk_types.h b/drivers/external_device_manager/serial/usb_serial_ddk_types.h new file mode 100644 index 0000000000000000000000000000000000000000..ab59e2dd6b608a6eff164a27b03e38f791b57f24 --- /dev/null +++ b/drivers/external_device_manager/serial/usb_serial_ddk_types.h @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @addtogroup SerialDdk + * @{ + * + * @brief Provides USB SERIAL DDK types and declares the macros, enumerated variables, and\n + * data structures required by the USB SERIAL DDK APIs. + * + * @syscap SystemCapability.Driver.SERIAL.Extension + * @since 16 + */ + +/** + * @file usb_serial_ddk_types.h + * + * @brief Provides the enumerated variables, structures, and macros used in USB SERIAL DDK APIs. + * + * @kit DriverDevelopmentKit + * @library libusb_serial.z.so + * @syscap SystemCapability.Driver.SERIAL.Extension + * File to include: + * @since 16 + */ +#ifndef USB_SERIAL_DDK_TYPES_H +#define USB_SERIAL_DDK_TYPES_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Defines USB Serial Port Params for USB SERIAL DDK. + * + * @since 16 + */ +typedef struct UsbSerial_Params { + /** The baud rate requested by the system */ + uint32_t baudRate; + /** The number of data bits to transmit */ + uint8_t nDataBits; + /** The number of half stop bits. */ + uint8_t nStopBits; + /** The parity setting to use during communication */ + uint8_t parity; +} __attribute__((aligned(8))) UsbSerial_Params; + +/** + * @brief Defines flow control for USB SERIAL DDK. + * + * @since 16 + */ +typedef enum { + /** No flow control */ + USB_SERIAL_FLOWCONTROL_NONE = 0, + /** Software flow control */ + USB_SERIAL_FLOWCONTROL_SOFTWARE = 1, + /** Hardware flow control */ + USB_SERIAL_FLOWCONTROL_HARDWARE = 2, +} UsbSerial_FlowControl; + +/** + * @brief Defines parity for USB SERIAL DDK. + * + * @since 16 + */ +typedef enum { + /** No parity */ + USB_SERIAL_PARITY_NONE = 0, + /** Odd parity */ + USB_SERIAL_PARITY_ODD = 1, + /** Even parity */ + USB_SERIAL_PARITY_EVEN = 2, +} UsbSerial_Parity; + +/** + * @brief Defines Return codes for USB SERIAL DDK. + * + * @since 16 + */ +typedef enum { + /** @error Permission denied */ + USB_SERIAL_DDK_NO_PERM = 201, + /** @error Invalid parameter */ + USB_SERIAL_DDK_INVALID_PARAMETER = 401, + /** @error Operation successful */ + USB_SERIAL_DDK_SUCCESS = 31600000, + /** @error Invalid operation */ + USB_SERIAL_DDK_INVALID_OPERATION = 31600001, + /** @error Init operation */ + USB_SERIAL_DDK_INIT_ERROR = 31600002, + /** @error Service Error operation */ + USB_SERIAL_DDK_SERVICE_ERROR = 31600003, + /** @error Memory-related error, for example, insufficient memory, memory data copy failure,\n + * or memory application failure. + */ + USB_SERIAL_DDK_MEMORY_ERROR = 31600004, + /** @error I/O Error */ + USB_SERIAL_DDK_IO_ERROR = 31600005, + /** @error Device not found */ + USB_SERIAL_DDK_DEVICE_NOT_FOUND = 31600006, +} UsbSerial_DdkRetCode; + +/** + * @brief Opaque USB Serial device structure. + * + * @since 16 + */ +typedef struct UsbSerial_DeviceHandle UsbSerial_DeviceHandle; + +#ifdef __cplusplus +} +/** @} */ +#endif /* __cplusplus */ +#endif // USB_DDK_TYPES_H \ No newline at end of file diff --git a/ndk_targets.gni b/ndk_targets.gni index fa07e23a9f2548741bfec329862c84eb1b074136..238d4ce7333f15454fd69a10387a07b4fcc1a614 100644 --- a/ndk_targets.gni +++ b/ndk_targets.gni @@ -184,6 +184,8 @@ _ndk_library_targets = [ "//interface/sdk_c/distributeddatamgr/preferences:libohpreferences", "//interface/sdk_c/drivers/external_device_manager/usb:libusb_ndk", "//interface/sdk_c/drivers/external_device_manager/usb:usb_header", + "//interface/sdk_c/drivers/external_device_manager/serial:libusb_serial_ndk", + "//interface/sdk_c/drivers/external_device_manager/serial:usb_serial_header", "//interface/sdk_c/drivers/external_device_manager/hid:libhid", "//interface/sdk_c/drivers/external_device_manager/hid:hid_header", "//interface/sdk_c/drivers/external_device_manager/base:libddk_base",