From 339732e453428ba3cf39ceb23edcf42c5ac0d918 Mon Sep 17 00:00:00 2001 From: zianed Date: Mon, 12 Apr 2021 20:38:13 +0800 Subject: [PATCH] add liteos_m adapter --- .gitignore | 1 + khdf/liteos_m/BUILD.gn | 37 +++ khdf/liteos_m/LICENSE | 28 +++ khdf/liteos_m/README.md | 26 +++ khdf/liteos_m/README_zh.md | 26 +++ khdf/liteos_m/core/BUILD.gn | 107 +++++++++ .../core/common/src/devmgr_service_start.c | 128 ++++++++++ .../core/common/src/hdf_device_node_ext.c | 123 ++++++++++ khdf/liteos_m/hcs/BUILD.gn | 48 ++++ khdf/liteos_m/hcs/hcs_build.py | 59 +++++ khdf/liteos_m/hdf.gni | 31 +++ khdf/liteos_m/osal/BUILD.gn | 58 +++++ khdf/liteos_m/osal/include/hdf_log_adapter.h | 74 ++++++ khdf/liteos_m/osal/include/hdf_types.h | 40 ++++ khdf/liteos_m/osal/include/osal_atomic_def.h | 100 ++++++++ khdf/liteos_m/osal/include/osal_math.h | 48 ++++ khdf/liteos_m/osal/src/osal_mem.c | 88 +++++++ khdf/liteos_m/osal/src/osal_mutex.c | 135 +++++++++++ khdf/liteos_m/osal/src/osal_sem.c | 120 ++++++++++ khdf/liteos_m/osal/src/osal_spinlock.c | 114 +++++++++ khdf/liteos_m/osal/src/osal_thread.c | 218 ++++++++++++++++++ khdf/liteos_m/osal/src/osal_time.c | 94 ++++++++ khdf/liteos_m/osal/src/osal_timer.c | 185 +++++++++++++++ khdf/liteos_m/test/sample_driver/BUILD.gn | 47 ++++ .../sample_driver/include/sample_service.h | 42 ++++ .../test/sample_driver/src/sample_driver.c | 90 ++++++++ 26 files changed, 2067 insertions(+) create mode 100644 khdf/liteos_m/BUILD.gn create mode 100644 khdf/liteos_m/LICENSE create mode 100644 khdf/liteos_m/README.md create mode 100644 khdf/liteos_m/README_zh.md create mode 100644 khdf/liteos_m/core/BUILD.gn create mode 100644 khdf/liteos_m/core/common/src/devmgr_service_start.c create mode 100644 khdf/liteos_m/core/common/src/hdf_device_node_ext.c create mode 100644 khdf/liteos_m/hcs/BUILD.gn create mode 100644 khdf/liteos_m/hcs/hcs_build.py create mode 100644 khdf/liteos_m/hdf.gni create mode 100644 khdf/liteos_m/osal/BUILD.gn create mode 100644 khdf/liteos_m/osal/include/hdf_log_adapter.h create mode 100644 khdf/liteos_m/osal/include/hdf_types.h create mode 100644 khdf/liteos_m/osal/include/osal_atomic_def.h create mode 100644 khdf/liteos_m/osal/include/osal_math.h create mode 100644 khdf/liteos_m/osal/src/osal_mem.c create mode 100644 khdf/liteos_m/osal/src/osal_mutex.c create mode 100644 khdf/liteos_m/osal/src/osal_sem.c create mode 100644 khdf/liteos_m/osal/src/osal_spinlock.c create mode 100644 khdf/liteos_m/osal/src/osal_thread.c create mode 100644 khdf/liteos_m/osal/src/osal_time.c create mode 100644 khdf/liteos_m/osal/src/osal_timer.c create mode 100644 khdf/liteos_m/test/sample_driver/BUILD.gn create mode 100644 khdf/liteos_m/test/sample_driver/include/sample_service.h create mode 100644 khdf/liteos_m/test/sample_driver/src/sample_driver.c diff --git a/.gitignore b/.gitignore index 92aa549..b939ea0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ khdf/linux +hdf_config_gen* diff --git a/khdf/liteos_m/BUILD.gn b/khdf/liteos_m/BUILD.gn new file mode 100644 index 0000000..241db0f --- /dev/null +++ b/khdf/liteos_m/BUILD.gn @@ -0,0 +1,37 @@ +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +group("hdf_lite") { + deps = [ + "osal:hdf_osal_lite", + "core:hdf_core", + "hcs:hdf_hcs", + "test\sample_driver:sample_driver" + ] +} diff --git a/khdf/liteos_m/LICENSE b/khdf/liteos_m/LICENSE new file mode 100644 index 0000000..208b4cd --- /dev/null +++ b/khdf/liteos_m/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of + conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list + of conditions and the following disclaimer in the documentation and/or other materials + provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used + to endorse or promote products derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/khdf/liteos_m/README.md b/khdf/liteos_m/README.md new file mode 100644 index 0000000..455c6b4 --- /dev/null +++ b/khdf/liteos_m/README.md @@ -0,0 +1,26 @@ +# khdf\_liteosm + +- [Introduction](#section11660541593) +- [Directory Structure](#section161941989596) +- [Repositories Involved](#section1371113476307) + +## Introduction + +This repository stores the code and compilation scripts for the OpenHarmony driver subsystem to adapt to the liteos\_m kernel and to deploy the hardware driver foundation \(HDF\). + +## Directory Structure + +``` +/drivers/adapter/khdf/liteos_m +├── core # Driver code for adapting to the LiteOS Cortex-M kernel +├── hcs # Script for compiling the HDF configuration source (HCS) file +├── osal # System APIs for adapting to the LiteOS Cortex-M kernel +└── test # Test code for the kernel driver framework +``` + +## Repositories Involved + +[Driver subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/driver-subsystem.md) + +[drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) + diff --git a/khdf/liteos_m/README_zh.md b/khdf/liteos_m/README_zh.md new file mode 100644 index 0000000..b8270a1 --- /dev/null +++ b/khdf/liteos_m/README_zh.md @@ -0,0 +1,26 @@ +# khdf\_liteosm + +- [简介](#section11660541593) +- [目录](#section161941989596) +- [相关仓](#section1371113476307) + +## 简介 + +该仓主要存放OpenHarmony驱动子系统适配liteos\_m内核的代码和编译脚本,在liteos\_m内核中部署OpenHarmony驱动框架。 + +## 目录 + +``` +/drivers/adapter/khdf/liteos_m +├── core #适配liteos_m内核的框架代码 +├── hcs #hcs配置编译脚本 +├── osal #适配liteos_m内核的系统接口 +└── test #内核驱动框架测试代码 +``` + +## 相关仓 + +[驱动子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/%E9%A9%B1%E5%8A%A8%E5%AD%90%E7%B3%BB%E7%BB%9F.md) + +[drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README_zh.md) + diff --git a/khdf/liteos_m/core/BUILD.gn b/khdf/liteos_m/core/BUILD.gn new file mode 100644 index 0000000..9be8f0e --- /dev/null +++ b/khdf/liteos_m/core/BUILD.gn @@ -0,0 +1,107 @@ +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//drivers/adapter/khdf/liteos_m/hdf.gni") + +config("libhdf_core_pub_config") { + visibility = [ ":*" ] + +include_dirs = [ + "$hdf_fwk_path/include", + "$hdf_fwk_path/include/osal", + "$hdf_fwk_path/include/utils", + "$hdf_fwk_path/utils/include", + "$hdf_fwk_path/include/platform", + "$hdf_fwk_path/include/core", + "$hdf_fwk_path/include/net", + "$hdf_fwk_path/include/wifi", + "$hdf_fwk_path/model/network/common/netdevice", + "$hdf_fwk_path/ability/sbuf/include", + "$hdf_fwk_path/support/platform/include", + "$hdf_fwk_path/core/host/include", + "$hdf_fwk_path/core/manager/include", + "$hdf_fwk_path/core/shared/include", + "$hdf_fwk_path/core/common/include/manager", + "$hdf_fwk_path/core/common/include/host", + "$hdf_fwk_path/core/adapter/vnode/include", + "$hdf_fwk_path/core/adapter/syscall/include", + "$hdf_fwk_path/include/config", + "$hdf_fwk_path/ability/config/hcs_parser/include", + ] +} + +static_library("hdf_core") { + sources = [ + "common/src/hdf_device_node_ext.c", + "common/src/devmgr_service_start.c", + "$hdf_fwk_path/utils/src/hdf_cstring.c", + "$hdf_fwk_path/utils/src/hdf_slist.c", + "$hdf_fwk_path/utils/src/hdf_sref.c", + "$hdf_fwk_path/ability/sbuf/src/hdf_sbuf.c", + "$hdf_fwk_path/core/host/src/devhost_service.c", + "$hdf_fwk_path/core/host/src/devmgr_service_clnt.c", + "$hdf_fwk_path/core/host/src/devsvc_manager_clnt.c", + "$hdf_fwk_path/core/host/src/hdf_device.c", + "$hdf_fwk_path/core/host/src/hdf_device_node.c", + "$hdf_fwk_path/core/host/src/hdf_device_token.c", + "$hdf_fwk_path/core/host/src/hdf_driver_loader.c", + "$hdf_fwk_path/core/host/src/hdf_observer_record.c", + "$hdf_fwk_path/core/host/src/hdf_service_subscriber.c", + "$hdf_fwk_path/core/host/src/hdf_device_object.c", + "$hdf_fwk_path/core/host/src/hdf_service_observer.c", + "$hdf_fwk_path/core/host/src/power_state_token.c", + "$hdf_fwk_path/core/manager/src/devhost_service_clnt.c", + "$hdf_fwk_path/core/manager/src/device_token_clnt.c", + "$hdf_fwk_path/core/manager/src/devmgr_service.c", + "$hdf_fwk_path/core/manager/src/devsvc_manager.c", + "$hdf_fwk_path/core/manager/src/hdf_driver_installer.c", + "$hdf_fwk_path/core/manager/src/hdf_host_info.c", + "$hdf_fwk_path/core/manager/src/power_state_manager.c", + "$hdf_fwk_path/core/manager/src/power_state_token_clnt.c", + "$hdf_fwk_path/core/shared/src/hdf_service_record.c", + "$hdf_fwk_path/core/shared/src/hdf_device_info.c", + "$hdf_fwk_path/core/shared/src/hdf_object_manager.c", + "$hdf_fwk_path/core/common/src/devlite_object_config.c", + "$hdf_fwk_path/core/common/src/hdf_attribute.c", + "$hdf_fwk_path/core/common/src/load_driver_entry.c", + "$hdf_fwk_path/ability/config/hcs_parser/src/hcs_blob_if.c", + "$hdf_fwk_path/ability/config/hcs_parser/src/hcs_parser.c", + "$hdf_fwk_path/ability/config/hcs_parser/src/hcs_generate_tree.c", + "$hdf_fwk_path/ability/config/hcs_parser/src/hcs_tree_if.c", + "$hdf_fwk_path/ability/config/device_resource_if.c", + ] + + include_dirs = [] + public_configs = [ + ":libhdf_core_pub_config" + ] + deps = [ + "$khdf_path/osal:hdf_osal_lite" + ] +} \ No newline at end of file diff --git a/khdf/liteos_m/core/common/src/devmgr_service_start.c b/khdf/liteos_m/core/common/src/devmgr_service_start.c new file mode 100644 index 0000000..0cea250 --- /dev/null +++ b/khdf/liteos_m/core/common/src/devmgr_service_start.c @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "devmgr_service_start.h" +#include "devhost_service_clnt.h" +#include "devmgr_service.h" +#include "devsvc_manager_clnt.h" +#include "hdf_base.h" +#include "hdf_device_node.h" +#include "hdf_io_service.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" + +#define DEV_MGR_NODE_PERM 0660 + +static int g_isQuickLoad = DEV_MGR_SLOW_LOAD; + +int32_t HdfGetServiceNameByDeviceClass(DeviceClass deviceClass, struct HdfSBuf *reply) +{ + struct HdfSListIterator itHost; + struct HdfSListIterator itDeviceInfo; + struct HdfDeviceInfo *deviceInfo = NULL; + struct DevHostServiceClnt *hostClnt = NULL; + struct DevmgrService *devMgrSvc = (struct DevmgrService *)DevmgrServiceGetInstance(); + if (devMgrSvc == NULL || reply == NULL) { + return; + } + + reply->readPos = 0; + reply->writePos = 0; + HdfSListIteratorInit(&itHost, &devMgrSvc->hosts); + while (HdfSListIteratorHasNext(&itHost)) { + hostClnt = (struct DevHostServiceClnt *)HdfSListIteratorNext(&itHost); + HdfSListIteratorInit(&itDeviceInfo, hostClnt->deviceInfos); + while (HdfSListIteratorHasNext(&itDeviceInfo)) { + deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&itDeviceInfo); + if (deviceInfo->policy == SERVICE_POLICY_CAPACITY) { + struct HdfDeviceObject *deviceObject = DevSvcManagerClntGetDeviceObject(deviceInfo->svcName); + if (deviceObject == NULL || (deviceObject->deviceClass != deviceClass)) { + continue; + } + HdfSbufWriteString(reply, deviceInfo->svcName); + } + } + } + HdfSbufWriteString(reply, NULL); +} + +int32_t HdfLoadDriverByServiceName(const char *svcName) +{ + if (svcName == NULL) { + HDF_LOGE("%s: load svc name is null", __func__); + return HDF_ERR_INVALID_PARAM; + } + static struct SubscriberCallback callback = { + .deviceObject = NULL, + .OnServiceConnected = NULL, + }; + return DevSvcManagerClntSubscribeService(svcName, callback); +} + +int DeviceManagerUnloadService(const char *svcName) +{ + if (svcName == NULL) { + HDF_LOGE("%s: unload svc name is null", __func__); + return HDF_ERR_INVALID_PARAM; + } + return DevSvcManagerClntUnsubscribeService(svcName); +} + +void DeviceManagerSetQuickLoad(int loadFlag) +{ + g_isQuickLoad = loadFlag; +} + +int DeviceManagerIsQuickLoad() +{ + return g_isQuickLoad; +} + +int DeviceManagerStart() +{ + struct IDevmgrService *instance = DevmgrServiceGetInstance(); + HDF_LOGE("%s in", __func__); + if (instance == NULL || instance->StartService == NULL) { + HDF_LOGE("Device manager start failed, service instance is null!"); + return HDF_FAILURE; + } + return instance->StartService(instance); +} + +int DeviceManagerStartStep2() +{ + if (DeviceManagerIsQuickLoad() == DEV_MGR_SLOW_LOAD) { + HDF_LOGW("%s device manager is not set quick load!", __func__); + return HDF_SUCCESS; + } + struct DevmgrService *devMgrSvc = (struct DevmgrService *)DevmgrServiceGetInstance(); + return DevmgrServiceLoadLeftDriver(devMgrSvc); +} + diff --git a/khdf/liteos_m/core/common/src/hdf_device_node_ext.c b/khdf/liteos_m/core/common/src/hdf_device_node_ext.c new file mode 100644 index 0000000..e2a4db8 --- /dev/null +++ b/khdf/liteos_m/core/common/src/hdf_device_node_ext.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "hdf_device_node_ext.h" +#include "devsvc_manager_clnt.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "hdf_io_service.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG device_node_ext + +static int DeviceNodeExtDispatch(struct HdfObject *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct IDeviceIoService *deviceMethod = NULL; + const struct HdfDeviceInfo *deviceInfo = NULL; + struct HdfDeviceNode *devNode = NULL; + + if (stub == NULL) { + HDF_LOGE("input ioService null"); + return HDF_FAILURE; + } + uint64_t ioClientPtr = 0; + if (!HdfSbufReadUint64(reply, &ioClientPtr) || ioClientPtr == 0) { + HDF_LOGE("input ioClient null"); + return HDF_FAILURE; + } + HdfSbufFlush(reply); + devNode = CONTAINER_OF(stub, struct HdfDeviceNode, deviceObject); + deviceMethod = devNode->deviceObject.service; + if (deviceMethod == NULL) { + HDF_LOGE("Device service interface is null"); + return HDF_FAILURE; + } + deviceInfo = devNode->deviceInfo; + if (deviceInfo == NULL) { + HDF_LOGE("Device deviceInfo is null"); + return HDF_FAILURE; + } + if (deviceInfo->policy == SERVICE_POLICY_CAPACITY) { + if (deviceMethod->Dispatch == NULL) { + HDF_LOGE("Remote service dispatch is null"); + return HDF_FAILURE; + } + return deviceMethod->Dispatch((struct HdfDeviceIoClient *)((uintptr_t)ioClientPtr), code, data, reply); + } + return HDF_FAILURE; +} + +static int DeviceNodeExtPublishService(struct HdfDeviceNode *inst, const char *serviceName) +{ + const struct HdfDeviceInfo *deviceInfo = NULL; + struct HdfDeviceObject *deviceObject = NULL; + struct DeviceNodeExt *devNodeExt = (struct DeviceNodeExt *)inst; + if (devNodeExt == NULL) { + return HDF_FAILURE; + } + int ret = HdfDeviceNodePublishPublicService(inst, serviceName); + if (ret != HDF_SUCCESS) { + HDF_LOGE("Device publish service failed, ret is: %d", ret); + } + + return ret; +} + +static void DeviceNodeExtConstruct(struct DeviceNodeExt *inst) +{ + struct IDeviceNode *nodeIf = (struct IDeviceNode *)inst; + if (nodeIf != NULL) { + HdfDeviceNodeConstruct(&inst->super); + nodeIf->PublishService = DeviceNodeExtPublishService; + } +} + +struct HdfObject *DeviceNodeExtCreate() +{ + struct DeviceNodeExt *instance = + (struct DeviceNodeExt *)OsalMemCalloc(sizeof(struct DeviceNodeExt)); + if (instance != NULL) { + DeviceNodeExtConstruct(instance); + instance->ioService = NULL; + } + return (struct HdfObject *)instance; +} + +void DeviceNodeExtRelease(struct HdfObject *object) +{ + struct DeviceNodeExt *instance = (struct DeviceNodeExt *)object; + if (instance != NULL) { + HdfDeviceNodeDestruct(&instance->super); + OsalMemFree(instance); + } +} + diff --git a/khdf/liteos_m/hcs/BUILD.gn b/khdf/liteos_m/hcs/BUILD.gn new file mode 100644 index 0000000..20462f7 --- /dev/null +++ b/khdf/liteos_m/hcs/BUILD.gn @@ -0,0 +1,48 @@ +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//drivers/adapter/khdf/liteos_m/hdf.gni") + +static_library("hdf_hcs") { + sources = [ + "hdf_config_gen_hex.c" + ] + config_path = product_path + "/config/hdf.hcs" + print("aaa " + ohos_root_path) + exec_script( + "hcs_build.py", + [ + ohos_root_path+"$hdf_fwk_path/tools/hc-gen/bin/hc-gen", + "-i", + "-o", + rebase_path("hdf_config_gen.c"), + config_path + ], + "") +} \ No newline at end of file diff --git a/khdf/liteos_m/hcs/hcs_build.py b/khdf/liteos_m/hcs/hcs_build.py new file mode 100644 index 0000000..9346941 --- /dev/null +++ b/khdf/liteos_m/hcs/hcs_build.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import sys +import argparse +import platform +import subprocess +import time + + +def exec_command(cmd): + print("exec_command:") + print(cmd) + process = subprocess.Popen(cmd) + process.wait() + ret_code = process.returncode + + if ret_code != 0: + raise Exception("{} failed, return code is {}".format(cmd, ret_code)) + + +def main(argv): + build_cmd = argv[1:] + exec_command(build_cmd) + +if __name__ == '__main__': + sys.exit(main(sys.argv)) + diff --git a/khdf/liteos_m/hdf.gni b/khdf/liteos_m/hdf.gni new file mode 100644 index 0000000..ea7e0d5 --- /dev/null +++ b/khdf/liteos_m/hdf.gni @@ -0,0 +1,31 @@ +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +khdf_path = "//drivers/adapter/khdf/liteos_m" +hdf_fwk_path = "//drivers/framework" \ No newline at end of file diff --git a/khdf/liteos_m/osal/BUILD.gn b/khdf/liteos_m/osal/BUILD.gn new file mode 100644 index 0000000..30b69dd --- /dev/null +++ b/khdf/liteos_m/osal/BUILD.gn @@ -0,0 +1,58 @@ +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//drivers/adapter/khdf/liteos_m/hdf.gni") + +config("libhdf_osal_pub_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "//utils/native/lite/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//kernel/liteos_m/kernel/arch/include", + "//base/hiviewdfx/hilog_lite/interfaces/native/innerkits", + ] +} + +static_library("hdf_osal_lite") { + sources = [ + "src/osal_mutex.c", + "src/osal_mem.c", + "src/osal_sem.c", + "src/osal_thread.c", + "src/osal_time.c", + "src/osal_timer.c", + "src/osal_spinlock.c", + ] + include_dirs = [] + public_configs = [ + ":libhdf_osal_pub_config", + ] +} \ No newline at end of file diff --git a/khdf/liteos_m/osal/include/hdf_log_adapter.h b/khdf/liteos_m/osal/include/hdf_log_adapter.h new file mode 100644 index 0000000..3a91995 --- /dev/null +++ b/khdf/liteos_m/osal/include/hdf_log_adapter.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef HDF_LOG_ADAPTER_H +#define HDF_LOG_ADAPTER_H + +#ifdef LOSCFG_BASE_CORE_HILOG +#include "hilog/log.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef LOSCFG_BASE_CORE_HILOG +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD002500 + +#define HDF_LOGV_WRAPPER(fmt, arg...) HILOG_DEBUG(LOG_DOMAIN, fmt, ##arg) + +#define HDF_LOGD_WRAPPER(fmt, arg...) HILOG_DEBUG(LOG_DOMAIN, fmt, ##arg) + +#define HDF_LOGI_WRAPPER(fmt, arg...) HILOG_INFO(LOG_DOMAIN, fmt, ##arg) + +#define HDF_LOGW_WRAPPER(fmt, arg...) HILOG_WARN(LOG_DOMAIN, fmt, ##arg) + +#define HDF_LOGE_WRAPPER(fmt, arg...) HILOG_ERROR(LOG_DOMAIN, fmt, ##arg) +#else +int hal_trace_printf(int attr, const char *fmt, ...) __attribute__((weak)); +#define PRINTF(level, fmt, ...) do { if (hal_trace_printf) hal_trace_printf(level, fmt, ##__VA_ARGS__); } while (0) + +#define HDF_LOGV_WRAPPER(fmt, arg...) PRINTF(6, "[HDF:V/" LOG_TAG "]" fmt "\n", ##arg) + +#define HDF_LOGD_WRAPPER(fmt, arg...) PRINTF(5, "[HDF:D/" LOG_TAG "]" fmt "\n", ##arg) + +#define HDF_LOGI_WRAPPER(fmt, arg...) PRINTF(4, "[HDF:I/" LOG_TAG "]" fmt "\n", ##arg) + +#define HDF_LOGW_WRAPPER(fmt, arg...) PRINTF(2, "[HDF:W/" LOG_TAG "]" fmt "\n", ##arg) + +#define HDF_LOGE_WRAPPER(fmt, arg...) PRINTF(1, "[HDF:E/" LOG_TAG "]" fmt "\n", ##arg) +#endif +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDF_LOG_ADAPTER_H */ + diff --git a/khdf/liteos_m/osal/include/hdf_types.h b/khdf/liteos_m/osal/include/hdf_types.h new file mode 100644 index 0000000..bd2e254 --- /dev/null +++ b/khdf/liteos_m/osal/include/hdf_types.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef HDF_TYPE_H +#define HDF_TYPE_H + +#include +#include +#include +#include +#include + +#endif /* HDF_TYPE_H */ diff --git a/khdf/liteos_m/osal/include/osal_atomic_def.h b/khdf/liteos_m/osal/include/osal_atomic_def.h new file mode 100644 index 0000000..d8b940b --- /dev/null +++ b/khdf/liteos_m/osal/include/osal_atomic_def.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef OSAL_ATOMIC_DEF_H +#define OSAL_ATOMIC_DEF_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define OsalAtomicReadWrapper(v) ((v)->counter) +#define OsalAtomicSetWrapper(v, value) (v)->counter = value +#define OsalAtomicIncWrapper(v) ((v)->counter)++ +#define OsalAtomicIncRetWrapper(v) (++((v)->counter)) +#define OsalAtomicDecWrapper(v) (((v)->counter)--) +#define OsalAtomicDecRetWrapper(v) (--((v)->counter)) + +#define OSAL_BITS_PER_LONG 32 +#define OSAL_BIT_MASK(nr) (1UL << ((nr) % OSAL_BITS_PER_LONG)) +#define OSAL_BIT_WORD(nr) ((nr) / OSAL_BITS_PER_LONG) +#define OSAL_BITOP_WORD(nr) ((nr) / OSAL_BITS_PER_LONG) + +static inline int32_t OsalTestBitWrapper(unsigned long nr, const volatile unsigned long *addr) +{ + return (1UL & (addr[OSAL_BIT_WORD(nr)] >> (nr & (OSAL_BITS_PER_LONG - 1)))); +} + +static inline int32_t OsalTestSetBitWrapper(unsigned long nr, volatile unsigned long *addr) +{ + uint32_t intSave = LOS_IntLock(); + + const unsigned long mask = OSAL_BIT_MASK(nr); + unsigned long *p = ((unsigned long *)addr) + OSAL_BIT_WORD(nr); + unsigned long old = *p; + *p = old | mask; + + LOS_IntRestore(intSave); + return ((old & mask) != 0); +} + +static inline int32_t OsalTestClearBitWrapper(unsigned long nr, volatile unsigned long *addr) +{ + uint32_t intSave = LOS_IntLock(); + + const unsigned long mask = OSAL_BIT_MASK(nr); + unsigned long *p = ((unsigned long *)addr) + OSAL_BIT_WORD(nr); + unsigned long old = *p; + *p = old & ~mask; + + LOS_IntRestore(intSave); + return ((old & mask) != 0); +} + +static inline void OsalClearBitWrapper(unsigned long nr, volatile unsigned long *addr) +{ + uint32_t intSave = LOS_IntLock(); + + const unsigned long mask = OSAL_BIT_MASK(nr); + unsigned long *p = ((unsigned long *)addr) + OSAL_BIT_WORD(nr); + *p &= ~mask; + + LOS_IntRestore(intSave); +} + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* OSAL_ATOMIC_DEF_H */ + diff --git a/khdf/liteos_m/osal/include/osal_math.h b/khdf/liteos_m/osal/include/osal_math.h new file mode 100644 index 0000000..d93bead --- /dev/null +++ b/khdf/liteos_m/osal/include/osal_math.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef OSAL_MATH_H +#define OSAL_MATH_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +static inline int64_t OsalDivS64(int64_t dividend, int32_t divisor) +{ + return ((divisor != 0) ? (dividend / divisor) : 0); +} + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* OSAL_MATH_H */ + diff --git a/khdf/liteos_m/osal/src/osal_mem.c b/khdf/liteos_m/osal/src/osal_mem.c new file mode 100644 index 0000000..779d03a --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_mem.c @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_mem.h" +#include "hdf_log.h" +#include "los_memory.h" +#include "osal_atomic_def.h" +#include "securec.h" +#define HDF_LOG_TAG osal_mem + +void *OsalMemAlloc(size_t size) +{ + void *buf = NULL; + + if (size == 0) { + HDF_LOGE("%s invalid param", __func__); + return NULL; + } + + buf = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, size); + + return buf; +} + +void *OsalMemCalloc(size_t size) +{ + void *buf = NULL; + + if (size == 0) { + HDF_LOGE("%s invalid param", __func__); + return NULL; + } + + buf = OsalMemAlloc(size); + if (buf != NULL) { + (void)memset_s(buf, size, 0, size); + } + + return buf; +} + +void *OsalMemAllocAlign(size_t alignment, size_t size) +{ + void *buf = NULL; + + if (size == 0) { + HDF_LOGE("%s invalid param", __func__); + return NULL; + } + + buf = LOS_MemAllocAlign((VOID *)OS_SYS_MEM_ADDR, size, alignment); + + return buf; +} + +void OsalMemFree(void *mem) +{ + if (mem != NULL) { + LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, mem); + } +} diff --git a/khdf/liteos_m/osal/src/osal_mutex.c b/khdf/liteos_m/osal/src/osal_mutex.c new file mode 100644 index 0000000..0cedac2 --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_mutex.c @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_mutex.h" +#include "hdf_log.h" +#include "los_mux.h" + +#define HDF_LOG_TAG osal_mutex + +#define HDF_INVALID_MUX_ID UINT32_MAX + +int32_t OsalMutexInit(struct OsalMutex *mutex) +{ + uint32_t ret; + uint32_t muxId = 0; + + if (mutex == NULL) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxCreate(&muxId); + if (ret == LOS_OK) { + mutex->realMutex = (void *)(uintptr_t)muxId; + } else { + mutex->realMutex = (void *)(uintptr_t)HDF_INVALID_MUX_ID; + HDF_LOGE("%s create fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t OsalMutexDestroy(struct OsalMutex *mutex) +{ + uint32_t ret; + + if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxDelete((uint32_t)(uintptr_t)mutex->realMutex); + if (ret != LOS_OK) { + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + mutex->realMutex = (void *)(uintptr_t)HDF_INVALID_MUX_ID; + return HDF_SUCCESS; +} + +int32_t OsalMutexLock(struct OsalMutex *mutex) +{ + uint32_t ret; + + if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxPend((uint32_t)(uintptr_t)mutex->realMutex, LOS_MS2Tick(HDF_WAIT_FOREVER)); + if (ret != LOS_OK) { + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t OsalMutexTimedLock(struct OsalMutex *mutex, uint32_t ms) +{ + uint32_t ret; + + if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxPend((uint32_t)(uintptr_t)mutex->realMutex, LOS_MS2Tick(ms)); + if (ret != LOS_OK) { + if (ret == LOS_ERRNO_MUX_TIMEOUT) { + return HDF_ERR_TIMEOUT; + } + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t OsalMutexUnlock(struct OsalMutex *mutex) +{ + uint32_t ret; + + if (mutex == NULL || mutex->realMutex == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxPost((uint32_t)(uintptr_t)mutex->realMutex); + if (ret != LOS_OK) { + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} diff --git a/khdf/liteos_m/osal/src/osal_sem.c b/khdf/liteos_m/osal/src/osal_sem.c new file mode 100644 index 0000000..94b7cd9 --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_sem.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_sem.h" +#ifndef __LITEOS_M__ +#include "los_hwi.h" +#endif +#include "los_sem.h" +#ifndef __LITEOS_M__ +#include "los_typedef.h" +#endif +#include "hdf_log.h" + +#define HDF_LOG_TAG osal_sem +#define HDF_INVALID_SEM_ID UINT32_MAX + +int32_t OsalSemInit(struct OsalSem *sem, uint32_t value) +{ + uint32_t semId; + uint32_t ret; + + if (sem == NULL) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_SemCreate((uint16_t)value, &semId); + if (ret == LOS_OK) { + sem->realSemaphore = (void *)(uintptr_t)semId; + return HDF_SUCCESS; + } else { + sem->realSemaphore = (void *)(uintptr_t)HDF_INVALID_SEM_ID; + HDF_LOGE("%s create fail %u", __func__, ret); + return HDF_FAILURE; + } +} + +int32_t OsalSemWait(struct OsalSem *sem, uint32_t ms) +{ + uint32_t ret; + + if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_SemPend((uint32_t)(uintptr_t)sem->realSemaphore, LOS_MS2Tick(ms)); + if (ret == LOS_OK) { + return HDF_SUCCESS; + } else { + if (ret == LOS_ERRNO_SEM_TIMEOUT) { + return HDF_ERR_TIMEOUT; + } + HDF_LOGE("%s LOS_SemPend fail %u", __func__, ret); + return HDF_FAILURE; + } +} + +int32_t OsalSemPost(struct OsalSem *sem) +{ + uint32_t ret; + + if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_SemPost((uint32_t)(uintptr_t)sem->realSemaphore); + if (ret == LOS_OK) { + return HDF_SUCCESS; + } else { + HDF_LOGE("%s LOS_SemPost fail %u", __func__, ret); + return HDF_FAILURE; + } +} + +int32_t OsalSemDestroy(struct OsalSem *sem) +{ + uint32_t ret; + + if (sem == NULL || sem->realSemaphore == (void *)(uintptr_t)HDF_INVALID_SEM_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_SemDelete((uint32_t)(uintptr_t)sem->realSemaphore); + if (ret != LOS_OK) { + HDF_LOGE("%s LOS_SemDelete fail %u", __func__, ret); + return HDF_FAILURE; + } + sem->realSemaphore = (void *)(uintptr_t)HDF_INVALID_SEM_ID; + return HDF_SUCCESS; +} diff --git a/khdf/liteos_m/osal/src/osal_spinlock.c b/khdf/liteos_m/osal/src/osal_spinlock.c new file mode 100644 index 0000000..fab77da --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_spinlock.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_spinlock.h" +#include "hdf_log.h" +#include "los_mux.h" + +#define HDF_LOG_TAG osal_spinlock + +#define HDF_INVALID_MUX_ID UINT32_MAX + +int32_t OsalSpinInit(OsalSpinlock *spinlock) +{ + uint32_t ret; + uint32_t muxId = 0; + + if (spinlock == NULL) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxCreate(&muxId); + if (ret == LOS_OK) { + spinlock->realSpinlock = (void *)(uintptr_t)muxId; + } else { + spinlock->realSpinlock = (void *)(uintptr_t)HDF_INVALID_MUX_ID; + HDF_LOGE("%s create fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t OsalSpinDestroy(OsalSpinlock *spinlock) +{ + uint32_t ret; + + if (spinlock == NULL || spinlock->realSpinlock == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxDelete((uint32_t)(uintptr_t)spinlock->realSpinlock); + if (ret != LOS_OK) { + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + spinlock->realSpinlock = (void *)(uintptr_t)HDF_INVALID_MUX_ID; + return HDF_SUCCESS; +} + +int32_t OsalSpinLock(OsalSpinlock *spinlock) +{ + uint32_t ret; + + if (spinlock == NULL || spinlock->realSpinlock == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxPend((uint32_t)(uintptr_t)spinlock->realSpinlock, LOS_MS2Tick(HDF_WAIT_FOREVER)); + if (ret != LOS_OK) { + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t OsalSpinUnlock(OsalSpinlock *spinlock) +{ + uint32_t ret; + + if (spinlock == NULL || spinlock->realSpinlock == (void *)(uintptr_t)HDF_INVALID_MUX_ID) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_MuxPost((uint32_t)(uintptr_t)spinlock->realSpinlock); + if (ret != LOS_OK) { + HDF_LOGE("%s fail %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} diff --git a/khdf/liteos_m/osal/src/osal_thread.c b/khdf/liteos_m/osal/src/osal_thread.c new file mode 100644 index 0000000..af025da --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_thread.c @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_thread.h" +#include "hdf_log.h" +#include "los_interrupt.h" +#include "los_task.h" +#include "osal_mem.h" +#include "securec.h" + +#define HDF_LOG_TAG osal_thread + +#define LOS_PRIORITY_WIN 3 +#define OS_TASK_PRIORITY_LOWEST 31 +#define OSAL_THREAD_NAME "hdf_thread" +#define OSAL_INVALID_THREAD_ID UINT32_MAX +#define OSAL_INVALID_CPU_ID UINT32_MAX + +struct ThreadWrapper { + OsalThreadEntry threadEntry; + void *entryPara; + uint32_t cpuID; + uint32_t tid; +}; + +enum { + OSAL_PRIORITY_LOW = 15, + OSAL_PRIORITY_MIDDLE = 16, + OSAL_PRIORITY_HIGH = 24, + OSAL_PRIORITY_HIGHEST = 31, +}; +int32_t OsalThreadCreate(struct OsalThread *thread, OsalThreadEntry threadEntry, void *entryPara) +{ + struct ThreadWrapper *para = NULL; + + if (thread == NULL || threadEntry == NULL) { + HDF_LOGE("%s invalid param", __func__); + return HDF_ERR_INVALID_PARAM; + } + + thread->realThread = NULL; + para = (struct ThreadWrapper *)OsalMemCalloc(sizeof(*para)); + if (para == NULL) { + HDF_LOGE("%s malloc fail", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + para->entryPara = entryPara; + para->threadEntry = threadEntry; + para->tid = OSAL_INVALID_THREAD_ID; + para->cpuID = OSAL_INVALID_CPU_ID; + thread->realThread = para; + + return HDF_SUCCESS; +} + +int32_t OsalThreadBind(struct OsalThread *thread, unsigned int cpuID) +{ + struct ThreadWrapper *para = NULL; + + if (thread == NULL || thread->realThread == NULL) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + para = (struct ThreadWrapper *)thread->realThread; + para->cpuID = cpuID; + return HDF_SUCCESS; +} + +int32_t OsalThreadStart(struct OsalThread *thread, const struct OsalThreadParam *param) +{ + uint32_t ret; + TSK_INIT_PARAM_S stTskInitParam; + uint16_t priority; + struct ThreadWrapper *para = NULL; + + if (OS_INT_ACTIVE) { + return HDF_FAILURE; + } + + if (thread == NULL || thread->realThread == NULL || param == NULL || param->priority > OSAL_THREAD_PRI_HIGHEST) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + para = (struct ThreadWrapper *)thread->realThread; + (void)memset_s(&stTskInitParam, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S)); + stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)para->threadEntry; + if (param->stackSize != 0) { + stTskInitParam.uwStackSize = param->stackSize; + } + stTskInitParam.pcName = param->name; + if (param->priority == OSAL_THREAD_PRI_HIGHEST) { + priority = OSAL_PRIORITY_HIGHEST; + } else if (param->priority == OSAL_THREAD_PRI_HIGH) { + priority = OSAL_PRIORITY_HIGH; + } else if (param->priority == OSAL_THREAD_PRI_DEFAULT) { + priority = OSAL_PRIORITY_MIDDLE; + } else { + priority = OSAL_PRIORITY_LOW; + } + + stTskInitParam.usTaskPrio = OS_TASK_PRIORITY_LOWEST - (priority - LOS_PRIORITY_WIN); + stTskInitParam.uwArg = (uintptr_t)para->entryPara; + ret = LOS_TaskCreate(¶->tid, &stTskInitParam); + if (ret != LOS_OK) { + para->tid = OSAL_INVALID_THREAD_ID; + HDF_LOGE("%s LOS_TaskCreate fail %u %u", __func__, ret, priority); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static bool OsalCheckPara(struct OsalThread *thread) +{ + struct ThreadWrapper *para = NULL; + + if (OS_INT_ACTIVE) { + return false; + } + + if (thread == NULL || thread->realThread == NULL) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return false; + } + + para = (struct ThreadWrapper *)thread->realThread; + if (para->tid == OSAL_INVALID_THREAD_ID) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return false; + } + + return true; +} + +int32_t OsalThreadSuspend(struct OsalThread *thread) +{ + uint32_t ret; + bool flag = false; + + flag = OsalCheckPara(thread); + if (flag == false) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_TaskSuspend(((struct ThreadWrapper *)thread->realThread)->tid); + if (ret != LOS_OK) { + HDF_LOGE("%s LOS_TaskSuspend failed %u", __func__, ret); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t OsalThreadDestroy(struct OsalThread *thread) +{ + bool flag = false; + + flag = OsalCheckPara(thread); + if (flag == false) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + OsalMemFree(thread->realThread); + thread->realThread = NULL; + + return HDF_SUCCESS; +} + +int32_t OsalThreadResume(struct OsalThread *thread) +{ + uint32_t ret; + bool flag = false; + + flag = OsalCheckPara(thread); + if (flag == false) { + HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + ret = LOS_TaskResume(((struct ThreadWrapper *)thread->realThread)->tid); + if (ret != LOS_OK) { + HDF_LOGE("%s failed %u %d", __func__, ret, __LINE__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} diff --git a/khdf/liteos_m/osal/src/osal_time.c b/khdf/liteos_m/osal/src/osal_time.c new file mode 100644 index 0000000..f672111 --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_time.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_time.h" +#include +#include +#include "hdf_log.h" +#include "los_task.h" + +#define HDF_LOG_TAG osal_time +#define TM_SINCE_YEAR 1900 + +int32_t OsalGetTime(OsalTimespec *time) +{ + uint64_t ms; + + if (time == NULL) { + HDF_LOGE("%{public}s invalid para", __func__); + return HDF_ERR_INVALID_PARAM; + } + ms = LOS_TickCountGet() * (HDF_KILO_UNIT / LOSCFG_BASE_CORE_TICK_PER_SECOND); + + time->sec = ms / HDF_KILO_UNIT; + time->usec = (ms % HDF_KILO_UNIT) * HDF_KILO_UNIT; + + return HDF_SUCCESS; +} + +int32_t OsalDiffTime(const OsalTimespec *start, const OsalTimespec *end, OsalTimespec *diff) +{ + uint32_t usec = 0; + uint32_t sec = 0; + + if (start == NULL || end == NULL || diff == NULL) { + HDF_LOGE("%{public}s invalid para", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (start->sec > end->sec) { + HDF_LOGE("%{public}s start time later then end time", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (end->usec < start->usec) { + usec = (HDF_KILO_UNIT * HDF_KILO_UNIT); + sec = 1; + } + diff->usec = usec + end->usec - start->usec; + diff->sec = end->sec - start->sec - sec; + + return HDF_SUCCESS; +} + +uint64_t OsalGetSysTimeMs(void) +{ + return LOS_TickCountGet() * (HDF_KILO_UNIT / LOSCFG_BASE_CORE_TICK_PER_SECOND); +} + +void OsalSleep(uint32_t sec) +{ + LOS_Msleep(sec * HDF_KILO_UNIT); +} + +void OsalMSleep(uint32_t ms) +{ + LOS_Msleep(ms); +} diff --git a/khdf/liteos_m/osal/src/osal_timer.c b/khdf/liteos_m/osal/src/osal_timer.c new file mode 100644 index 0000000..6edec85 --- /dev/null +++ b/khdf/liteos_m/osal/src/osal_timer.c @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "osal_timer.h" +#include "hdf_log.h" +#include "los_interrupt.h" +#include "los_swtmr.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG osal_timer +#define OSAL_INVALID_TIMER_ID UINT32_MAX + +struct OsalLitetimer { + uintptr_t arg; + uint16_t timerID; + OsalTimerFunc func; + uint32_t interval; +}; + +int32_t OsalTimerCreate(OsalTimer *timer, uint32_t interval, OsalTimerFunc func, uintptr_t arg) +{ + struct OsalLitetimer *liteTimer = NULL; + + if (func == NULL || timer == NULL || interval == 0) { + HDF_LOGE("%s invalid para", __func__); + return HDF_ERR_INVALID_PARAM; + } + + timer->realTimer = NULL; + + liteTimer = (struct OsalLitetimer *)OsalMemCalloc(sizeof(*liteTimer)); + if (liteTimer == NULL) { + HDF_LOGE("%s malloc fail", __func__); + return HDF_ERR_MALLOC_FAIL; + } + liteTimer->timerID = OSAL_INVALID_TIMER_ID; + liteTimer->arg = arg; + liteTimer->func = func; + liteTimer->interval = interval; + timer->realTimer = (void *)liteTimer; + + return HDF_SUCCESS; +} + +static int32_t OsalStartTimer(OsalTimer *timer, UINT8 mode) +{ + uint32_t ret; + uint32_t intSave; + uint32_t interval; + uint16_t timerID = 0; + struct OsalLitetimer *liteTimer = NULL; + + if (timer == NULL || timer->realTimer == NULL) { + HDF_LOGE("%s invalid para %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + liteTimer = (struct OsalLitetimer *)timer->realTimer; + if (liteTimer->interval == 0 || liteTimer->func == NULL) { + HDF_LOGE("%s invalid para %d", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + interval = liteTimer->interval; + intSave = LOS_IntLock(); + ret = LOS_SwtmrCreate(LOS_MS2Tick(interval), mode, (SWTMR_PROC_FUNC)liteTimer->func, &timerID, liteTimer->arg); + if (ret != LOS_OK) { + LOS_IntRestore(intSave); + HDF_LOGE("%s LOS_SwtmrCreate fail %u", __func__, ret); + return HDF_FAILURE; + } + + ret = LOS_SwtmrStart(timerID); + if (ret != LOS_OK) { + LOS_SwtmrDelete(timerID); + LOS_IntRestore(intSave); + HDF_LOGE("%s LOS_SwtmrStart fail %u", __func__, ret); + return HDF_FAILURE; + } + LOS_IntRestore(intSave); + + liteTimer->timerID = timerID; + return HDF_SUCCESS; +} +int32_t OsalTimerStartLoop(OsalTimer *timer) +{ + return OsalStartTimer(timer, LOS_SWTMR_MODE_PERIOD); +} + +int32_t OsalTimerStartOnce(OsalTimer *timer) +{ + return OsalStartTimer(timer, LOS_SWTMR_MODE_ONCE); +} + +int32_t OsalTimerSetTimeout(OsalTimer *timer, uint32_t interval) +{ + struct OsalLitetimer *liteTimer = NULL; + uint32_t intSave; + uint32_t ret; + + if (timer == NULL || timer->realTimer == NULL || interval == 0) { + HDF_LOGE("%s invalid para", __func__); + return HDF_ERR_INVALID_PARAM; + } + + liteTimer = (struct OsalLitetimer *)timer->realTimer; + if (liteTimer->timerID == OSAL_INVALID_TIMER_ID) { + HDF_LOGE("%s timer id invalid %u", __func__, liteTimer->timerID); + return HDF_FAILURE; + } + + if (liteTimer->interval == interval) { + return HDF_SUCCESS; + } + + liteTimer->interval = interval; + + intSave = LOS_IntLock(); + ret = LOS_SwtmrDelete(liteTimer->timerID); + if (ret != LOS_OK) { + LOS_IntRestore(intSave); + HDF_LOGE("%s LOS_SwtmrDelete fail %u", __func__, ret); + return HDF_FAILURE; + } + LOS_IntRestore(intSave); + + return OsalTimerStartLoop(timer); +} + +int32_t OsalTimerDelete(OsalTimer *timer) +{ + uint32_t intSave; + uint32_t ret; + struct OsalLitetimer *liteTimer = NULL; + + if (timer == NULL || timer->realTimer == NULL) { + HDF_LOGE("%s invalid para", __func__); + return HDF_ERR_INVALID_PARAM; + } + + liteTimer = (struct OsalLitetimer *)timer->realTimer; + if (liteTimer->timerID == OSAL_INVALID_TIMER_ID) { + HDF_LOGE("%s timer id invalid %u", __func__, liteTimer->timerID); + return HDF_FAILURE; + } + intSave = LOS_IntLock(); + ret = LOS_SwtmrDelete(liteTimer->timerID); + if (ret == LOS_OK) { + LOS_IntRestore(intSave); + OsalMemFree(timer->realTimer); + timer->realTimer = NULL; + return HDF_SUCCESS; + } else { + LOS_IntRestore(intSave); + HDF_LOGE("%s LOS_SwtmrDelete fail %u", __func__, ret); + return HDF_FAILURE; + } +} diff --git a/khdf/liteos_m/test/sample_driver/BUILD.gn b/khdf/liteos_m/test/sample_driver/BUILD.gn new file mode 100644 index 0000000..13db1c4 --- /dev/null +++ b/khdf/liteos_m/test/sample_driver/BUILD.gn @@ -0,0 +1,47 @@ +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//drivers/adapter/khdf/liteos_m/hdf.gni") + +static_library("sample_driver") { + sources = [ + "src/sample_driver.c" + ] + include_dirs = [ + "include" + ] + + deps = [ + "$khdf_path/osal:hdf_osal_lite", + ] + + ldflags = [ + "-Wl,--whole-archive" + ] +} \ No newline at end of file diff --git a/khdf/liteos_m/test/sample_driver/include/sample_service.h b/khdf/liteos_m/test/sample_driver/include/sample_service.h new file mode 100644 index 0000000..7516692 --- /dev/null +++ b/khdf/liteos_m/test/sample_driver/include/sample_service.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef HDF_SAMPLE_DRIVER_H +#define HDF_SAMPLE_DRIVER_H + +#include "hdf_device_desc.h" + +struct SampleService { + struct IDeviceIoService service; + const char *(*getData)(void); + int32_t (*setData)(const char *data); +}; + +#endif // HDF_SAMPLE_DRIVER_H \ No newline at end of file diff --git a/khdf/liteos_m/test/sample_driver/src/sample_driver.c b/khdf/liteos_m/test/sample_driver/src/sample_driver.c new file mode 100644 index 0000000..3b33e96 --- /dev/null +++ b/khdf/liteos_m/test/sample_driver/src/sample_driver.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "hdf_device_desc.h" +#include "hdf_log.h" +#include "sample_service.h" + +#define HDF_LOG_TAG sample_driver + +static const char *SampleServiceGetData(void) +{ + return "sample"; +} + +static int32_t SampleServiceSetData(const char *data) +{ + if (data == NULL) { + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGD("%s:%s", __func__, data); + return HDF_SUCCESS; +} + +static void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) +{ + (void)deviceObject; + return; +} + +static int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGD("%s::enter, deviceObject=%p", __func__, deviceObject); + if (deviceObject == NULL) { + return HDF_FAILURE; + } + static struct SampleService sampleService = { + .getData = SampleServiceGetData, + .setData = SampleServiceSetData, + }; + deviceObject->service = &sampleService.service; + return HDF_SUCCESS; +} + +static int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGD("%s::enter, deviceObject=%p", __func__, deviceObject); + if (deviceObject == NULL) { + HDF_LOGE("%s::ptr is null!", __func__); + return HDF_FAILURE; + } + HDF_LOGD("%s:Init success", __func__); + return HDF_SUCCESS; +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .moduleName = "sample_driver", + .Bind = HdfSampleDriverBind, + .Init = HdfSampleDriverInit, + .Release = HdfSampleDriverRelease, +}; + +HDF_INIT(g_sampleDriverEntry);