From 5b69c9408188c123ee3572ffb35af6d67d2f2162 Mon Sep 17 00:00:00 2001 From: shilong Date: Mon, 22 Apr 2024 14:56:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eddk=5Fbase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shilong --- drivers/external_device_manager/base/BUILD.gn | 33 +++++++ .../external_device_manager/base/ddk_api.h | 89 ++++++++++++++++++ .../external_device_manager/base/ddk_types.h | 91 +++++++++++++++++++ .../base/libbase.ndk.json | 14 +++ drivers/external_device_manager/usb/BUILD.gn | 2 + .../usb/libusb.ndk.json | 3 + .../external_device_manager/usb/usb_ddk_api.h | 13 +++ 7 files changed, 245 insertions(+) create mode 100644 drivers/external_device_manager/base/BUILD.gn create mode 100644 drivers/external_device_manager/base/ddk_api.h create mode 100644 drivers/external_device_manager/base/ddk_types.h create mode 100644 drivers/external_device_manager/base/libbase.ndk.json diff --git a/drivers/external_device_manager/base/BUILD.gn b/drivers/external_device_manager/base/BUILD.gn new file mode 100644 index 000000000..f02cd7542 --- /dev/null +++ b/drivers/external_device_manager/base/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("ddk_header") { + dest_dir = "$ndk_headers_out_dir/ddk/" + sources = [ + "ddk_api.h", + "ddk_types.h", + ] +} + +ohos_ndk_library("libddk_base") { + ndk_description_file = "./libbase.ndk.json" + min_compact_version = "12" + output_name = "ddk_base" + system_capability = "SystemCapability.Driver.DDK.Extension" + system_capability_headers = [ + "base/ddk_api.h", + "base/ddk_types.h", + ] +} diff --git a/drivers/external_device_manager/base/ddk_api.h b/drivers/external_device_manager/base/ddk_api.h new file mode 100644 index 000000000..201a29abd --- /dev/null +++ b/drivers/external_device_manager/base/ddk_api.h @@ -0,0 +1,89 @@ +/* + * 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. + */ +#ifndef DDK_API_H +#define DDK_API_H + +/** + * @addtogroup Ddk + * @{ + * + * @brief Provides Base DDK APIs, including creating the shared memory, mapping the shared memory,\n + * unmapping the shared memory, and destroying the shared memory. + * + * @since 12 + */ + +/** + * @file ddk_api.h + * + * @brief Declares the Base DDK APIs. + * + * @library libddk_base.z.so + * @syscap SystemCapability.Driver.DDK.Extension + * @since 12 + */ + +#include +#include "ddk_types.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Creates shared memory. To prevent resource leakage, destroy the shared memory that is not required by\n + * calling OH_DDK_DestroyAshmem. + * + * @param name Pointer to the shared memory to create. + * @param size Size of the buffer corresponding to the shared memory. + * @param ashmem Pointer to the shared memory created. + * @return Returns DDK_SUCCESS if the operation is successful; returns a negative value otherwise. + * @since 12 + */ +DDK_RetCode OH_DDK_CreateAshmem(const uint8_t *name, uint32_t size, DDK_Ashmem **ashmem); + +/** + * @brief Maps the created shared memory to the user space. Unmap the shared memory that is not required by using\n + * OH_DDK_UnmapAshmem. + * + * @param ashmem Pointer of the shared memory to map. + * @param ashmemMapType Protection permission value of the shared memory. + * @return Returns DDK_SUCCESS if the operation is successful; returns a negative value otherwise. + * @since 12 + */ +DDK_RetCode OH_DDK_MapAshmem(DDK_Ashmem *ashmem, const uint8_t ashmemMapType); + +/** + * @brief Unmaps shared memory. + * + * @param ashmem Pointer of the shared memory to unmap. + * @return Returns DDK_SUCCESS if the operation is successful; returns a negative value otherwise. + * @since 12 + */ +DDK_RetCode OH_DDK_UnmapAshmem(DDK_Ashmem *ashmem); + +/** + * @brief Destroys shared memory. + * + * @param ashmem Pointer of the shared memory to destroy. + * @return Returns DDK_SUCCESS if the operation is successful; returns a negative value otherwise. + * @since 12 + */ +DDK_RetCode OH_DDK_DestroyAshmem(DDK_Ashmem *ashmem); +#ifdef __cplusplus +} +/** @} */ +#endif /* __cplusplus */ +#endif // DDK_APIS_H \ No newline at end of file diff --git a/drivers/external_device_manager/base/ddk_types.h b/drivers/external_device_manager/base/ddk_types.h new file mode 100644 index 000000000..8326fd725 --- /dev/null +++ b/drivers/external_device_manager/base/ddk_types.h @@ -0,0 +1,91 @@ +/* + * 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. + */ +#ifndef DDK_TYPES_H +#define DDK_TYPES_H + +/** + * @addtogroup Ddk + * @{ + * + * @brief Provides Base DDK types and declares the macros, enums, and\n + * data structs used by the Base DDK APIs. + * + * @since 12 + */ + +/** + * @file ddk_types.h + * + * @brief Provides the enums, structs, and macros used in USB Base APIs. + * + * @library libddk_base.z.so + * @syscap SystemCapability.Driver.DDK.Extension + * @since 12 + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Defines the shared memory created by using OH_DDK_CreateAshmem.\n + * A buffer for the shared memory provides better performance. + * + * @since 12 + */ +typedef struct DDK_Ashmem { + /** File descriptor of the shared memory. */ + int32_t ashmemFd; + /** Buffer address. */ + const uint8_t *address; + /** Buffer size. */ + const uint32_t size; + /** Offset of the used buffer. The default value is 0, which indicates that there is no offset\n + * and the buffer starts from the specified address. + */ + uint32_t offset; + /** Length of the used buffer. By default, the value is equal to the size, which indicates that\n + * the entire buffer is used. + */ + uint32_t bufferLength; + /** Length of the transferred data. */ + uint32_t transferredLength; +} DDK_Ashmem; + +/** + * @brief Enumerates the error codes used in the Base DDK. + * + * @since 12 + */ +typedef enum { + /** Operation success */ + DDK_SUCCESS = 0, + /** Operation failed */ + DDK_FAILURE = 28600001, + /** Invalid parameter */ + DDK_INVALID_PARAMETER, + /** Invalid operation */ + DDK_INVALID_OPERATION, + /** Null pointer exception */ + DDK_NULL_PTR +} DDK_RetCode; +#ifdef __cplusplus +} +/** @} */ +#endif /* __cplusplus */ +#endif // DDK_TYPES_H \ No newline at end of file diff --git a/drivers/external_device_manager/base/libbase.ndk.json b/drivers/external_device_manager/base/libbase.ndk.json new file mode 100644 index 000000000..37586f4bd --- /dev/null +++ b/drivers/external_device_manager/base/libbase.ndk.json @@ -0,0 +1,14 @@ +[ + { + "name": "OH_DDK_CreateAshmem" + }, + { + "name": "OH_DDK_MapAshmem" + }, + { + "name": "OH_DDK_UnmapAshmem" + }, + { + "name": "OH_DDK_DestroyAshmem" + } +] \ No newline at end of file diff --git a/drivers/external_device_manager/usb/BUILD.gn b/drivers/external_device_manager/usb/BUILD.gn index 36f24db18..1729e8970 100644 --- a/drivers/external_device_manager/usb/BUILD.gn +++ b/drivers/external_device_manager/usb/BUILD.gn @@ -16,6 +16,7 @@ import("//build/ohos.gni") ohos_ndk_headers("usb_header") { dest_dir = "$ndk_headers_out_dir/usb/" sources = [ + "../base/ddk_types.h", "usb_ddk_api.h", "usb_ddk_types.h", ] @@ -29,5 +30,6 @@ ohos_ndk_library("libusb_ndk") { system_capability_headers = [ "usb/usb_ddk_api.h", "usb/usb_ddk_types.h", + "base/ddk_types.h", ] } diff --git a/drivers/external_device_manager/usb/libusb.ndk.json b/drivers/external_device_manager/usb/libusb.ndk.json index 4cc70335e..d3ad5dbd3 100644 --- a/drivers/external_device_manager/usb/libusb.ndk.json +++ b/drivers/external_device_manager/usb/libusb.ndk.json @@ -35,6 +35,9 @@ { "name": "OH_Usb_SendPipeRequest" }, + { + "name": "OH_Usb_SendPipeRequestWithAshmem" + }, { "name": "OH_Usb_CreateDeviceMemMap" }, diff --git a/drivers/external_device_manager/usb/usb_ddk_api.h b/drivers/external_device_manager/usb/usb_ddk_api.h index e128a7d34..39f5b950e 100644 --- a/drivers/external_device_manager/usb/usb_ddk_api.h +++ b/drivers/external_device_manager/usb/usb_ddk_api.h @@ -38,6 +38,7 @@ #include +#include "ddk_types.h" #include "usb_ddk_types.h" #ifdef __cplusplus @@ -198,6 +199,18 @@ int32_t OH_Usb_SendControlWriteRequest(uint64_t interfaceHandle, const struct Us */ int32_t OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap); +/** + * @brief Sends a pipe request. This API works in a synchronous manner. This API applies to interrupt transfer\n + * and bulk transfer. + * + * @permission ohos.permission.ACCESS_DDK_USB + * @param pipe Pipe used to transfer data. + * @param ashmem Shared memory, which can be obtained by calling OH_DDK_CreateAshmem. + * @return 0 if the operation is successful; a negative value otherwise. + * @since 12 + */ +int32_t OH_Usb_SendPipeRequestWithAshmem(const struct UsbRequestPipe *pipe, DDK_Ashmem *ashmem); + /** * @brief Creates a buffer. To avoid resource leakage, destroy a buffer by calling\n * OH_Usb_DestroyDeviceMemMap after use. -- Gitee