From a97c01bb53deb8ee2ec59be5f5e0446a62e84c87 Mon Sep 17 00:00:00 2001 From: gonghui Date: Tue, 18 May 2021 08:37:58 +0800 Subject: [PATCH] sync l2 code to l1 --- uhdf2/README.md | 35 + uhdf2/README_zh.md | 35 + uhdf2/config/BUILD.gn | 58 + uhdf2/config/include/hcs_blob_load.h | 23 + uhdf2/config/src/hcs_blob_load.c | 75 ++ uhdf2/config/src/hcs_dm_parser.c | 83 ++ uhdf2/hcs/BUILD.gn | 36 + uhdf2/hcs/default/device_info.hcs | 20 + uhdf2/hcs/default/hdf.hcs | 5 + uhdf2/hcs/large/device_info.hcs | 20 + uhdf2/hcs/large/hdf.hcs | 5 + uhdf2/hcs/standard/device_info.hcs | 81 ++ uhdf2/hcs/standard/hdf.hcs | 7 + uhdf2/hcs/standard/usb_cdcacm.hcs | 242 ++++ uhdf2/hcs/standard/usb_pnp_device.hcs | 47 + uhdf2/hdi/BUILD.gn | 88 ++ uhdf2/hdi/src/devmgr_hdi.c | 251 ++++ uhdf2/hdi/src/hdf_load_hdi.c | 99 ++ uhdf2/hdi/src/iservmgr_hdi.cpp | 73 ++ uhdf2/hdi/src/servmgr_hdi.c | 105 ++ uhdf2/hdi/test/BUILD.gn | 78 ++ uhdf2/hdi/test/dev_query/hdf_dev_query_test.c | 222 ++++ .../unittest/hcs/tas/device_info.hcs | 1050 +++++++++++++++++ .../sample_driver/hdf_sample_driver.c | 53 + .../sample_client_c/sample_client_c_test.cpp | 335 ++++++ .../sample_client_c/unittest/BUILD.gn | 52 + .../sample_client_c/unittest/isample.h | 101 ++ .../sample_client_c/unittest/sample_proxy.c | 844 +++++++++++++ .../sample_client_cpp_test.cpp | 300 +++++ .../sample_client_cpp/unittest/BUILD.gn | 55 + .../sample_client_cpp/unittest/isample.h | 127 ++ .../unittest/sample_proxy.cpp | 421 +++++++ .../sample_client_cpp/unittest/sample_proxy.h | 72 ++ .../test/hdi_sample/sample_service_c/BUILD.gn | 43 + .../hdi_sample/sample_service_c/isample.h | 95 ++ .../sample_service_c/sample_driver.c | 69 ++ .../sample_service_c/sample_service.c | 167 +++ .../sample_service_c/sample_service_stub.c | 489 ++++++++ .../hdi_sample/sample_service_cpp/BUILD.gn | 49 + .../hdi_sample/sample_service_cpp/isample.h | 102 ++ .../sample_service_cpp/sample_driver.cpp | 87 ++ .../sample_service_cpp/sample_service.cpp | 128 ++ .../sample_service_cpp/sample_service.h | 69 ++ .../sample_service_stub.cpp | 484 ++++++++ .../sample_service_cpp/sample_service_stub.h | 67 ++ uhdf2/hdi/test/load_hdi/hdf_load_hdi_test.c | 290 +++++ .../hdf_test_load_hdi_driver.c | 59 + .../reg_pnp_device/hdf_dev_reg_pnp_test.c | 326 +++++ .../unittest/sample_driver/hdf_pnp_driver.c | 111 ++ .../unittest/sample_driver/hdf_pnp_driver.h | 37 + .../hdf_dev_reg_virtual_test.c | 340 ++++++ .../sample_driver/hdf_virtual_driver.c | 54 + .../servmgr/service_manager_hdi_c_test.cpp | 210 ++++ .../test/servmgr/service_manager_hdi_test.cpp | 154 +++ uhdf2/host/BUILD.gn | 168 +++ uhdf2/host/devhost.c | 96 ++ uhdf2/host/include/devhost_service_full.h | 39 + uhdf2/host/include/devhost_service_stub.h | 32 + uhdf2/host/include/device_service_stub.h | 40 + uhdf2/host/include/device_token_stub.h | 30 + uhdf2/host/include/devmgr_service_proxy.h | 30 + uhdf2/host/include/devsvc_manager_proxy.h | 38 + uhdf2/host/include/driver_loader_full.h | 28 + uhdf2/host/include/hdf_device_full.h | 30 + uhdf2/host/include/hdf_device_thread.h | 43 + uhdf2/host/src/devhost_object_config.c | 75 ++ uhdf2/host/src/devhost_service_full.c | 183 +++ uhdf2/host/src/devhost_service_stub.c | 125 ++ uhdf2/host/src/device_service_stub.c | 106 ++ uhdf2/host/src/device_token_stub.c | 64 + uhdf2/host/src/devmgr_service_proxy.c | 160 +++ uhdf2/host/src/devsvc_manager_proxy.c | 180 +++ uhdf2/host/src/driver_loader_full.c | 109 ++ uhdf2/host/src/hdf_device_full.c | 114 ++ uhdf2/host/src/hdf_device_thread.c | 107 ++ uhdf2/host/src/hdf_devsvc_manager_clnt.c | 25 + uhdf2/host/test/BUILD.gn | 54 + uhdf2/host/test/unittest/devmgr_test.cpp | 64 + .../host/test/unittest/sample_driver/BUILD.gn | 43 + .../unittest/sample_driver/sample_driver.c | 67 ++ .../test/unittest/sample_driver/sample_hdi.h | 114 ++ .../sample_driver/sample_hdi_service.c | 60 + .../sample_driver/sample_hdi_service_stub.c | 140 +++ uhdf2/include/config/hcs_dm_parser.h | 36 + uhdf2/include/devhost/hdf_device_full.h | 45 + uhdf2/include/hdf_devmgr.h | 42 + uhdf2/include/hdi/devmgr_hdi.h | 63 + uhdf2/include/hdi/hdf_load_hdi.h | 47 + uhdf2/include/hdi/iservmgr_hdi.h | 37 + uhdf2/include/hdi/servmgr_hdi.h | 37 + uhdf2/include/host/hdf_device_section.h | 24 + uhdf2/ipc/BUILD.gn | 78 ++ uhdf2/ipc/include/hdf_remote_adapter.h | 61 + uhdf2/ipc/include/hdf_remote_adapter_if.h | 43 + uhdf2/ipc/include/hdf_sbuf_ipc.h | 25 + uhdf2/ipc/src/hdf_remote_adapter.cpp | 230 ++++ uhdf2/ipc/src/hdf_remote_service.c | 48 + uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp | 504 ++++++++ uhdf2/manager/BUILD.gn | 101 ++ uhdf2/manager/device_manager.c | 47 + uhdf2/manager/hdf_devmgr.rc | 19 + uhdf2/manager/include/devhost_service_proxy.h | 37 + uhdf2/manager/include/device_token_proxy.h | 30 + uhdf2/manager/include/devmgr_pnp_service.h | 33 + uhdf2/manager/include/devmgr_query_device.h | 24 + uhdf2/manager/include/devmgr_service_full.h | 37 + uhdf2/manager/include/devmgr_service_stub.h | 46 + .../manager/include/devmgr_virtual_service.h | 25 + uhdf2/manager/include/devsvc_manager_stub.h | 34 + uhdf2/manager/include/driver_installer_full.h | 24 + uhdf2/manager/src/devhost_service_proxy.c | 122 ++ uhdf2/manager/src/device_token_proxy.c | 44 + uhdf2/manager/src/devmgr_object_config.c | 38 + uhdf2/manager/src/devmgr_pnp_service.c | 306 +++++ uhdf2/manager/src/devmgr_query_device.c | 74 ++ uhdf2/manager/src/devmgr_service_full.c | 144 +++ uhdf2/manager/src/devmgr_service_stub.c | 996 ++++++++++++++++ uhdf2/manager/src/devmgr_virtual_service.c | 76 ++ uhdf2/manager/src/devsvc_manager_stub.c | 184 +++ uhdf2/manager/src/driver_installer_full.c | 102 ++ uhdf2/manager/src/hdf_get_attribute.c | 270 +++++ uhdf2/ohos.build | 44 + uhdf2/osal/BUILD.gn | 93 ++ uhdf2/osal/include/hdf_log_adapter.h | 48 + uhdf2/osal/include/hdf_types.h | 25 + uhdf2/osal/include/osal_atomic_def.h | 45 + uhdf2/osal/osal_sysevent.c | 202 ++++ uhdf2/osal/test/BUILD.gn | 74 ++ .../test/unittest/common/osal_slist_test.cpp | 174 +++ .../osal/test/unittest/common/sample_driver.c | 150 +++ .../osal/test/unittest/common/sample_driver.h | 33 + uhdf2/security/BUILD.gn | 51 + uhdf2/security/include/hdf_security.h | 38 + uhdf2/security/src/hdf_security.c | 245 ++++ uhdf2/shared/include/dev_attribute_parcel.h | 25 + uhdf2/shared/include/hdf_device_info_full.h | 31 + uhdf2/shared/include/hdf_remote_service.h | 56 + uhdf2/shared/src/dev_attribute_parcel.c | 217 ++++ uhdf2/shared/src/hdf_attribute_full.c | 83 ++ uhdf2/shared/src/hdf_device_info_full.c | 70 ++ uhdf2/test/BUILD.gn | 17 + uhdf2/test/resource/config/ohos_test.xml | 22 + uhdf2/test/unittest/common/BUILD.gn | 48 + uhdf2/test/unittest/config/BUILD.gn | 41 + uhdf2/test/unittest/manager/BUILD.gn | 127 ++ uhdf2/test/unittest/osal/BUILD.gn | 56 + uhdf2/test/unittest/platform/BUILD.gn | 63 + uhdf2/uhdf.gni | 15 + 148 files changed, 17118 insertions(+) create mode 100755 uhdf2/README.md create mode 100755 uhdf2/README_zh.md create mode 100755 uhdf2/config/BUILD.gn create mode 100644 uhdf2/config/include/hcs_blob_load.h create mode 100644 uhdf2/config/src/hcs_blob_load.c create mode 100644 uhdf2/config/src/hcs_dm_parser.c create mode 100755 uhdf2/hcs/BUILD.gn create mode 100755 uhdf2/hcs/default/device_info.hcs create mode 100755 uhdf2/hcs/default/hdf.hcs create mode 100755 uhdf2/hcs/large/device_info.hcs create mode 100755 uhdf2/hcs/large/hdf.hcs create mode 100755 uhdf2/hcs/standard/device_info.hcs create mode 100755 uhdf2/hcs/standard/hdf.hcs create mode 100755 uhdf2/hcs/standard/usb_cdcacm.hcs create mode 100755 uhdf2/hcs/standard/usb_pnp_device.hcs create mode 100755 uhdf2/hdi/BUILD.gn create mode 100644 uhdf2/hdi/src/devmgr_hdi.c create mode 100644 uhdf2/hdi/src/hdf_load_hdi.c create mode 100755 uhdf2/hdi/src/iservmgr_hdi.cpp create mode 100644 uhdf2/hdi/src/servmgr_hdi.c create mode 100755 uhdf2/hdi/test/BUILD.gn create mode 100644 uhdf2/hdi/test/dev_query/hdf_dev_query_test.c create mode 100755 uhdf2/hdi/test/dev_query/unittest/hcs/tas/device_info.hcs create mode 100644 uhdf2/hdi/test/dev_query/unittest/sample_driver/hdf_sample_driver.c create mode 100755 uhdf2/hdi/test/hdi_sample/sample_client_c/sample_client_c_test.cpp create mode 100755 uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn create mode 100644 uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/isample.h create mode 100644 uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/sample_proxy.c create mode 100755 uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp create mode 100755 uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn create mode 100644 uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/isample.h create mode 100755 uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp create mode 100644 uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.h create mode 100755 uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_c/isample.h create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service.c create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service_stub.c create mode 100755 uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_cpp/isample.h create mode 100755 uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_driver.cpp create mode 100755 uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.cpp create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.h create mode 100755 uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp create mode 100644 uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.h create mode 100644 uhdf2/hdi/test/load_hdi/hdf_load_hdi_test.c create mode 100644 uhdf2/hdi/test/load_hdi/unittest/sample_hdi_driver/hdf_test_load_hdi_driver.c create mode 100644 uhdf2/hdi/test/reg_pnp_device/hdf_dev_reg_pnp_test.c create mode 100644 uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.c create mode 100644 uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h create mode 100644 uhdf2/hdi/test/reg_virtual_device/hdf_dev_reg_virtual_test.c create mode 100644 uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c create mode 100755 uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp create mode 100755 uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp create mode 100755 uhdf2/host/BUILD.gn create mode 100644 uhdf2/host/devhost.c create mode 100644 uhdf2/host/include/devhost_service_full.h create mode 100644 uhdf2/host/include/devhost_service_stub.h create mode 100644 uhdf2/host/include/device_service_stub.h create mode 100644 uhdf2/host/include/device_token_stub.h create mode 100644 uhdf2/host/include/devmgr_service_proxy.h create mode 100644 uhdf2/host/include/devsvc_manager_proxy.h create mode 100644 uhdf2/host/include/driver_loader_full.h create mode 100644 uhdf2/host/include/hdf_device_full.h create mode 100644 uhdf2/host/include/hdf_device_thread.h create mode 100644 uhdf2/host/src/devhost_object_config.c create mode 100644 uhdf2/host/src/devhost_service_full.c create mode 100644 uhdf2/host/src/devhost_service_stub.c create mode 100644 uhdf2/host/src/device_service_stub.c create mode 100644 uhdf2/host/src/device_token_stub.c create mode 100644 uhdf2/host/src/devmgr_service_proxy.c create mode 100644 uhdf2/host/src/devsvc_manager_proxy.c create mode 100644 uhdf2/host/src/driver_loader_full.c create mode 100644 uhdf2/host/src/hdf_device_full.c create mode 100644 uhdf2/host/src/hdf_device_thread.c create mode 100644 uhdf2/host/src/hdf_devsvc_manager_clnt.c create mode 100755 uhdf2/host/test/BUILD.gn create mode 100755 uhdf2/host/test/unittest/devmgr_test.cpp create mode 100755 uhdf2/host/test/unittest/sample_driver/BUILD.gn create mode 100644 uhdf2/host/test/unittest/sample_driver/sample_driver.c create mode 100644 uhdf2/host/test/unittest/sample_driver/sample_hdi.h create mode 100644 uhdf2/host/test/unittest/sample_driver/sample_hdi_service.c create mode 100644 uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.c create mode 100644 uhdf2/include/config/hcs_dm_parser.h create mode 100644 uhdf2/include/devhost/hdf_device_full.h create mode 100644 uhdf2/include/hdf_devmgr.h create mode 100644 uhdf2/include/hdi/devmgr_hdi.h create mode 100644 uhdf2/include/hdi/hdf_load_hdi.h create mode 100644 uhdf2/include/hdi/iservmgr_hdi.h create mode 100644 uhdf2/include/hdi/servmgr_hdi.h create mode 100644 uhdf2/include/host/hdf_device_section.h create mode 100755 uhdf2/ipc/BUILD.gn create mode 100644 uhdf2/ipc/include/hdf_remote_adapter.h create mode 100644 uhdf2/ipc/include/hdf_remote_adapter_if.h create mode 100644 uhdf2/ipc/include/hdf_sbuf_ipc.h create mode 100755 uhdf2/ipc/src/hdf_remote_adapter.cpp create mode 100644 uhdf2/ipc/src/hdf_remote_service.c create mode 100755 uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp create mode 100755 uhdf2/manager/BUILD.gn create mode 100644 uhdf2/manager/device_manager.c create mode 100755 uhdf2/manager/hdf_devmgr.rc create mode 100644 uhdf2/manager/include/devhost_service_proxy.h create mode 100644 uhdf2/manager/include/device_token_proxy.h create mode 100644 uhdf2/manager/include/devmgr_pnp_service.h create mode 100644 uhdf2/manager/include/devmgr_query_device.h create mode 100644 uhdf2/manager/include/devmgr_service_full.h create mode 100644 uhdf2/manager/include/devmgr_service_stub.h create mode 100644 uhdf2/manager/include/devmgr_virtual_service.h create mode 100644 uhdf2/manager/include/devsvc_manager_stub.h create mode 100644 uhdf2/manager/include/driver_installer_full.h create mode 100644 uhdf2/manager/src/devhost_service_proxy.c create mode 100644 uhdf2/manager/src/device_token_proxy.c create mode 100644 uhdf2/manager/src/devmgr_object_config.c create mode 100644 uhdf2/manager/src/devmgr_pnp_service.c create mode 100644 uhdf2/manager/src/devmgr_query_device.c create mode 100644 uhdf2/manager/src/devmgr_service_full.c create mode 100644 uhdf2/manager/src/devmgr_service_stub.c create mode 100644 uhdf2/manager/src/devmgr_virtual_service.c create mode 100644 uhdf2/manager/src/devsvc_manager_stub.c create mode 100644 uhdf2/manager/src/driver_installer_full.c create mode 100644 uhdf2/manager/src/hdf_get_attribute.c create mode 100755 uhdf2/ohos.build create mode 100755 uhdf2/osal/BUILD.gn create mode 100644 uhdf2/osal/include/hdf_log_adapter.h create mode 100644 uhdf2/osal/include/hdf_types.h create mode 100644 uhdf2/osal/include/osal_atomic_def.h create mode 100644 uhdf2/osal/osal_sysevent.c create mode 100755 uhdf2/osal/test/BUILD.gn create mode 100755 uhdf2/osal/test/unittest/common/osal_slist_test.cpp create mode 100644 uhdf2/osal/test/unittest/common/sample_driver.c create mode 100644 uhdf2/osal/test/unittest/common/sample_driver.h create mode 100755 uhdf2/security/BUILD.gn create mode 100644 uhdf2/security/include/hdf_security.h create mode 100644 uhdf2/security/src/hdf_security.c create mode 100644 uhdf2/shared/include/dev_attribute_parcel.h create mode 100644 uhdf2/shared/include/hdf_device_info_full.h create mode 100644 uhdf2/shared/include/hdf_remote_service.h create mode 100644 uhdf2/shared/src/dev_attribute_parcel.c create mode 100644 uhdf2/shared/src/hdf_attribute_full.c create mode 100644 uhdf2/shared/src/hdf_device_info_full.c create mode 100755 uhdf2/test/BUILD.gn create mode 100755 uhdf2/test/resource/config/ohos_test.xml create mode 100755 uhdf2/test/unittest/common/BUILD.gn create mode 100755 uhdf2/test/unittest/config/BUILD.gn create mode 100755 uhdf2/test/unittest/manager/BUILD.gn create mode 100755 uhdf2/test/unittest/osal/BUILD.gn create mode 100755 uhdf2/test/unittest/platform/BUILD.gn create mode 100755 uhdf2/uhdf.gni diff --git a/uhdf2/README.md b/uhdf2/README.md new file mode 100755 index 0000000..c8efe7f --- /dev/null +++ b/uhdf2/README.md @@ -0,0 +1,35 @@ +# full\_uhdf + +- [Introduction](#section11660541593) +- [Directory Structure](#section161941989596) +- [Repositories Involved](#section1371113476307) + +## Introduction + +This repository stores the code and compilation scripts for the user-mode Hardware Driver Foundation \(HDF\) of the OpenHarmony driver subsystem and provides basic capabilities for the HDF. + +## Directory Structure + +``` +/drivers/adapter/uhdf2 +├── config # Configuration parsing module +├── hdi # Driver HDIs +├── host # Host environment module +├── include # Header files +├── ipc # IPC module +├── manager # Management module +├── osal # Compilation scripts for adapting to the POSIX APIs +├── security # Security module +└── shared # Code shared by the host and manager modules +``` + +## 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) + +drivers\_adapter + +[drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README.md) + diff --git a/uhdf2/README_zh.md b/uhdf2/README_zh.md new file mode 100755 index 0000000..67a3d90 --- /dev/null +++ b/uhdf2/README_zh.md @@ -0,0 +1,35 @@ +# full\_uhdf + +- [简介](#section11660541593) +- [目录](#section161941989596) +- [相关仓](#section1371113476307) + +## 简介 + +该仓主要存放OpenHarmony驱动子系统用户态驱动框架的代码和编译脚本,在用户态中提供驱动框架的能力。 + +## 目录 + +``` +/drivers/adapter/uhdf2 +├── config #用户态驱动配置解析模块 +├── hdi #用户态驱动的hdi接口 +├── host #用户态驱动宿主环境模块 +├── include #用户态驱动框架提供能力头文件 +├── ipc #用户态驱动进程间通信模块 +├── manager #用户态驱动框架管理模块 +├── osal #适配posix接口代码的编译脚本适配 +├── security #用户态驱动安全模块 +└── shared #manager和host共享代码模块 +``` + +## 相关仓 + +[驱动子系统](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) + +drivers\_adapter + +[drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README_zh.md) + diff --git a/uhdf2/config/BUILD.gn b/uhdf2/config/BUILD.gn new file mode 100755 index 0000000..76920c2 --- /dev/null +++ b/uhdf2/config/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +group("uhdf_hcs_pkg") { + deps = [ ":libhdf_hcs" ] +} + +ohos_shared_library("libhdf_hcs") { + include_dirs = [ + "$hdf_uhdf_path/config/include", + "$hdf_uhdf_path/include/config", + "$hdf_framework_path/include/config", + "$hdf_framework_path/ability/config/hcs_parser/include", + ] + sources = [ + "$hdf_framework_path/ability/config/device_resource_if.c", + "$hdf_framework_path/ability/config/hcs_parser/src/hcs_blob_if.c", + "$hdf_framework_path/ability/config/hcs_parser/src/hcs_generate_tree.c", + "$hdf_framework_path/ability/config/hcs_parser/src/hcs_parser.c", + "$hdf_framework_path/ability/config/hcs_parser/src/hcs_tree_if.c", + "$hdf_uhdf_path/config/src/hcs_blob_load.c", + "$hdf_uhdf_path/config/src/hcs_dm_parser.c", + ] + + deps = [ + "$hdf_uhdf_path/osal:libhdf_utils", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/config/include/hcs_blob_load.h b/uhdf2/config/include/hcs_blob_load.h new file mode 100644 index 0000000..922e5df --- /dev/null +++ b/uhdf2/config/include/hcs_blob_load.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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 HCS_BLOB_LOAD_H +#define HCS_BLOB_LOAD_H + +#include "hdf_base.h" + +uint32_t OpenHcsBlobFile(const char *hcsBlobPath, char **hcsBlob); + +#endif // HCS_BLOB_LOAD_H diff --git a/uhdf2/config/src/hcs_blob_load.c b/uhdf2/config/src/hcs_blob_load.c new file mode 100644 index 0000000..2d05d65 --- /dev/null +++ b/uhdf2/config/src/hcs_blob_load.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hcs_blob_load.h" +#include +#include +#include "hcs_blob_if.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG hcs_blob_load +#define HBC_FILE_EXT ".hcb" + +static bool IsHcbFile(const char *fname) +{ + char *hcbExt = strstr(fname, HBC_FILE_EXT); + if (hcbExt == NULL) { + return false; + } + return true; +} + +uint32_t OpenHcsBlobFile(const char *hcsBlobPath, char **hcsBlob) +{ + // the 0 is error. + int32_t length = 0; + if ((hcsBlobPath == NULL) || (hcsBlob == NULL) || !IsHcbFile(hcsBlobPath)) { + HDF_LOGE("%s failed, pls check the param", __func__); + return length; + } + + char path[PATH_MAX] = { 0 }; + if (realpath(hcsBlobPath, path) == NULL) { + HDF_LOGE("file %s is invalid", hcsBlobPath); + return length; + } + FILE *fp = fopen(path, "rb"); + do { + if (fp == NULL) { + HDF_LOGE("%s failed, pls check the path of %s", __func__, hcsBlobPath); + break; + } + fseek(fp, 0, SEEK_END); + length = ftell(fp); + if ((length <= 0) || (length >= HBC_BLOB_MAX_LENGTH)) { + length = 0; + HDF_LOGE("%s failed, the HcsBlob file length is %d", __func__, length); + break; + } + *hcsBlob = (char *)OsalMemCalloc(length + 1); + if ((*hcsBlob) == NULL) { + length = 0; + HDF_LOGE("%s failed, OsalMemCalloc hcsBlob memory failed", __func__); + break; + } + fseek(fp, 0, SEEK_SET); + fread((void *)(*hcsBlob), length, 1, fp); + } while (0); + if (fp != NULL) { + fclose(fp); + } + return (length > 0) ? length : 0; +} diff --git a/uhdf2/config/src/hcs_dm_parser.c b/uhdf2/config/src/hcs_dm_parser.c new file mode 100644 index 0000000..011ef1a --- /dev/null +++ b/uhdf2/config/src/hcs_dm_parser.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hcs_dm_parser.h" +#include +#include "hcs_blob_if.h" +#include "hcs_blob_load.h" +#include "hcs_parser.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG hcs_dm_parser +static char *g_hcsBlob = NULL; +static struct DeviceResourceNode *g_hcsTreeRoot = NULL; +static const char *g_hcsBlobPath = NULL; +static pthread_mutex_t g_getDmRootMutex = PTHREAD_MUTEX_INITIALIZER; + +void ReleaseHcsTree(void) +{ + OsalMemFree(g_hcsTreeRoot); + g_hcsTreeRoot = NULL; + OsalMemFree(g_hcsBlob); + g_hcsBlob = NULL; +} + +void SetHcsBlobPath(const char *path) +{ + if (g_hcsBlobPath != NULL) { + OsalMemFree((void *)g_hcsBlobPath); + } + g_hcsBlobPath = strdup(path); +} + +static bool CreateHcsToTree(void) +{ + bool ret = true; + do { + uint32_t length = OpenHcsBlobFile(g_hcsBlobPath, &g_hcsBlob); + if (length == 0) { + ret = false; + break; + } + if (!HcsCheckBlobFormat(g_hcsBlob, length)) { + ret = false; + break; + } + if (!HcsDecompile((const char *)g_hcsBlob, HBC_HEADER_LENGTH, &g_hcsTreeRoot)) { + ret = false; + break; + } + } while (0); + + if (!ret) { + ReleaseHcsTree(); + } + return ret; +} + +const struct DeviceResourceNode *HcsGetRootNode(void) +{ + if (g_hcsTreeRoot == NULL) { + pthread_mutex_lock(&g_getDmRootMutex); + if ((g_hcsTreeRoot == NULL) && !CreateHcsToTree()) { + pthread_mutex_unlock(&g_getDmRootMutex); + HDF_LOGE("%s is failed", __func__); + return NULL; + } + pthread_mutex_unlock(&g_getDmRootMutex); + } + return g_hcsTreeRoot; +} \ No newline at end of file diff --git a/uhdf2/hcs/BUILD.gn b/uhdf2/hcs/BUILD.gn new file mode 100755 index 0000000..e77bfc4 --- /dev/null +++ b/uhdf2/hcs/BUILD.gn @@ -0,0 +1,36 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +ohos_prebuilt_etc("hdf_default.hcb") { + source = "hdf_default.hcb" + relative_install_dir = "hdfconfig" + subsystem_name = "hdf" + hcs_config_path = "default" + if (is_standard_system) { + hcs_config_path = "standard" + } else if (is_large_system) { + hcs_config_path = "large" + } + exec_script( + "$hdf_framework_path/tools/hc-gen/bin/hc-gen", + [ + "-o", + rebase_path("hdf_default.hcb"), + rebase_path(hcs_config_path + "/hdf.hcs") + ], + "") + part_name = "hdf" +} \ No newline at end of file diff --git a/uhdf2/hcs/default/device_info.hcs b/uhdf2/hcs/default/device_info.hcs new file mode 100755 index 0000000..de00b8b --- /dev/null +++ b/uhdf2/hcs/default/device_info.hcs @@ -0,0 +1,20 @@ + root { + device_info { + match_attr = "hdf_manager"; + template host { + hostName = ""; + priority = 100; + template device { + template deviceNode { + policy = 0; + priority = 100; + preload = 0; + permission = 0664; + moduleName = ""; + serviceName = ""; + deviceMatchAttr = ""; + } + } + } + } +} diff --git a/uhdf2/hcs/default/hdf.hcs b/uhdf2/hcs/default/hdf.hcs new file mode 100755 index 0000000..bf8dc25 --- /dev/null +++ b/uhdf2/hcs/default/hdf.hcs @@ -0,0 +1,5 @@ +#include "device_info.hcs" + +root { + module = "default"; +} diff --git a/uhdf2/hcs/large/device_info.hcs b/uhdf2/hcs/large/device_info.hcs new file mode 100755 index 0000000..de00b8b --- /dev/null +++ b/uhdf2/hcs/large/device_info.hcs @@ -0,0 +1,20 @@ + root { + device_info { + match_attr = "hdf_manager"; + template host { + hostName = ""; + priority = 100; + template device { + template deviceNode { + policy = 0; + priority = 100; + preload = 0; + permission = 0664; + moduleName = ""; + serviceName = ""; + deviceMatchAttr = ""; + } + } + } + } +} diff --git a/uhdf2/hcs/large/hdf.hcs b/uhdf2/hcs/large/hdf.hcs new file mode 100755 index 0000000..2dba784 --- /dev/null +++ b/uhdf2/hcs/large/hdf.hcs @@ -0,0 +1,5 @@ +#include "device_info.hcs" + +root { + module = "full"; +} diff --git a/uhdf2/hcs/standard/device_info.hcs b/uhdf2/hcs/standard/device_info.hcs new file mode 100755 index 0000000..718a2fd --- /dev/null +++ b/uhdf2/hcs/standard/device_info.hcs @@ -0,0 +1,81 @@ + root { + device_info { + match_attr = "hdf_manager"; + template host { + hostName = ""; + priority = 100; + template device { + template deviceNode { + policy = 0; + priority = 100; + preload = 0; + permission = 0664; + moduleName = ""; + serviceName = ""; + deviceMatchAttr = ""; + } + } + } + platform :: host { + hostName = "sample_host"; + priority = 50; + sample_device :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + moduleName = "libsample_driver.z.so"; + serviceName = "sample_driver_service"; + } + } + } + acm :: host { + hostName = "acm_host"; + priority = 50; + acm_device :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + moduleName = "libusbfn_cdcacm.z.so"; + serviceName = "usbfn_cdcacm"; + deviceMatchAttr = "usbfn_cdcacm_driver"; + } + } + } + power :: host { + hostName = "power_host"; + priority = 50; + battery_device :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + moduleName = "libbatteryd.z.so"; + serviceName = "batteryd"; + } + } + } + platform_camera :: host { + hostName = "camera_host"; + priority = 50; + camera_device :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + moduleName = "libhdi_impl.z.so"; + serviceName = "camera_service"; + } + } + } + riladapter :: host { + hostName = "riladapter_host"; + priority = 50; + riladapter_device :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + moduleName = "libril_init.z.so"; + serviceName = "cellular_radio1"; + } + } + } + } +} diff --git a/uhdf2/hcs/standard/hdf.hcs b/uhdf2/hcs/standard/hdf.hcs new file mode 100755 index 0000000..01b37ac --- /dev/null +++ b/uhdf2/hcs/standard/hdf.hcs @@ -0,0 +1,7 @@ +#include "device_info.hcs" +#include "usb_pnp_device.hcs" +#include "usb_cdcacm.hcs" + +root { + module = "default"; +} diff --git a/uhdf2/hcs/standard/usb_cdcacm.hcs b/uhdf2/hcs/standard/usb_cdcacm.hcs new file mode 100755 index 0000000..74009f3 --- /dev/null +++ b/uhdf2/hcs/standard/usb_cdcacm.hcs @@ -0,0 +1,242 @@ +root { + module = "cdcacm"; + sampl_config { + match_attr = "usbfn_cdcacm_driver"; + use_hcs = 0; + udc_name = "100e0000.hidwc3_1"; + usb_dev_desc = "UsbDeviceDescriptor"; + usb_dev_string = "UsbDeviceStrings"; + usb_configuration = "UsbConfigs"; + UsbDeviceDescriptor { + bLength = 18; + bDescriptorType = 0x01; + bcdUSB = 0x0200; + bDeviceClass = 2; + bDeviceSubClass = 0; + bDeviceProtocol = 0; + bMaxPacketSize0 = 0x40; + idVendor = 0x0525; + idProduct = 0xA4A7; + bcdDevice = 0x0100; + manufacturer = 0; + product = 1; + serialnumber = 2; + numConfigurations = 1; + } + UsbDeviceStrings { + stringTabList = ["string_1"]; + string_1 { + language = 0x0409; + stringList = ["str_1", "str_2","str_3", "str_4"]; + str_1 { + id = 0; + str = ""; + } + str_2 { + id = 1; + str = "CDC ACM Sample for USB Device SDK"; + } + str_3 { + id = 2; + str = ""; + } + str_4 { + id = 3; + str = "CDC ACM"; + } + } + } + UsbConfigs { + configList = ["config_1"]; + config_1 { + configurationValue = 1; + iConfiguration = 3; + attributes = 0xC0; + maxPower = 500; + functionList = ["func_1"]; + func_1 { + funcName = "f1"; + stringTabList = ["fnString_1"]; + fnString_1 { + language = 0x0409; + stringList = ["str_1", "str_2", "str_3"]; + str_1 { + id = 0; + str = "CDC Serial"; + } + str_2 { + id = 1; + str = "CDC Abstract Control Model (ACM)"; + } + str_3 { + id = 2; + str = "CDC ACM Data"; + } + } + fsDescList = ["assoc_interface", "ControlInterface", "cdc_header", + "cdc_call_mgmt", "cdc_acm", "cdc_union", "FsNotify_endpoint", + "DataInterface", "FsIn_endpoint", "FsOut_endpoint"]; + assoc_interface { + bLength = 0x08; + bDescriptorType = 0x0B; + bFirstInterface = 0x00; + bInterfaceCount = 0x02; + bFunctionClass = 0x02; + bFunctionSubClass = 0x02; + bFunctionProtocol = 0x01; + iFunction = 0x00; + } + ControlInterface { + bLength = 0x09; + bDescriptorType = 0x04; + bInterfaceNumber = 0x00; + bAlternateSetting = 0x00; + bNumEndpoints = 0x01; + bInterfaceClass = 0x02; + bInterfaceSubClass = 0x02; + bInterfaceProtocol = 0x01; + iInterface = 0x01; + } + cdc_header { + bLength = 0x05; + bDescriptorType = 0x24; + desc_data = [0x05, 0x24, 0x00, 0x10, 0x01]; + } + cdc_call_mgmt { + bLength = 0x05; + bDescriptorType = 0x24; + desc_data = [0x05, 0x24, 0x01, 0x00, 0x01]; + } + cdc_acm { + bLength = 0x04; + bDescriptorType = 0x24; + desc_data = [0x04, 0x24, 0x02, 0x02]; + } + cdc_union { + bLength = 0x05; + bDescriptorType = 0x24; + desc_data = [0x05, 0x24, 0x06, 0x00, 0x01]; + } + FsNotify_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x81; + bmAttributes = 0x03; + wMaxPacketSize = 10; + bInterval = 32; + } + DataInterface { + bLength = 0x09; + bDescriptorType = 0x04; + bInterfaceNumber = 0x01; + bAlternateSetting = 0x00; + bNumEndpoints = 0x02; + bInterfaceClass = 0x0A; + bInterfaceSubClass = 0x00; + bInterfaceProtocol = 0x00; + iInterface = 0x02; + } + FsIn_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x82; + bmAttributes = 0x02; + wMaxPacketSize = 0; + bInterval = 0; + } + FsOut_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x3; + bmAttributes = 0x02; + wMaxPacketSize = 0; + bInterval = 0; + } + hsDescList = ["assoc_interface", "ControlInterface", "cdc_header", + "cdc_call_mgmt", "cdc_acm", "cdc_union", "HsNotify_endpoint", + "DataInterface", "HsIn_endpoint", "HsOut_endpoint"]; + HsNotify_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x81; + bmAttributes = 0x03; + wMaxPacketSize = 10; + bInterval = 9; + } + HsIn_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x82; + bmAttributes = 0x02; + wMaxPacketSize = 512; + bInterval = 0; + } + HsOut_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x3; + bmAttributes = 0x02; + wMaxPacketSize = 512; + bInterval = 0; + } + ssDescList = ["assoc_interface", "ControlInterface", "cdc_header", + "cdc_call_mgmt", "cdc_acm", "cdc_union", "HsNotify_endpoint", + "ss_ep_comp", "DataInterface", "SsIn_endpoint", + "ss_ep_comp","SsOut_endpoint","ss_ep_comp"]; + ss_ep_comp { + bLength = 0x06; + bDescriptorType = 0x30; + bMaxBurst = 0x00; + bmAttributes = 0x00; + wBytesPerInterval = 0x00; + } + SsIn_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x82; + bmAttributes = 0x02; + wMaxPacketSize = 1024; + bInterval = 0; + } + SsOut_endpoint { + bLength = 0x07; + bDescriptorType = 0x05; + bEndpointAddress = 0x3; + bmAttributes = 0x02; + wMaxPacketSize = 1024; + bInterval = 0; + } + } + } + } + custom_prop { + propTable = ["propList_1", "propList_2"]; + propList_1 { + configNum = 1; + interfaceNum = 0; + propList = ["prop_1", "prop_2"]; + prop_1 { + name = "testx"; + value = "xxxxxx"; + } + prop_2 { + name = "testy"; + value = "yyyyyy"; + } + } + propList_2 { + configNum = 1; + interfaceNum = 1; + propList = ["prop_1", "prop_2"]; + prop_1 { + name = "testm"; + value = "mmmmm"; + } + prop_2 { + name = "testn"; + value = "nnnnn"; + } + } + } + } +} diff --git a/uhdf2/hcs/standard/usb_pnp_device.hcs b/uhdf2/hcs/standard/usb_pnp_device.hcs new file mode 100755 index 0000000..6e49a99 --- /dev/null +++ b/uhdf2/hcs/standard/usb_pnp_device.hcs @@ -0,0 +1,47 @@ +root { + module = "usb_pnp_device"; + usb_pnp_config { + match_attr = "usb_pnp_match"; + usb_pnp_device_id = "UsbPnpDeviceId"; + UsbPnpDeviceId { + idTableList = [ + "sample_table", + "host_acm_table" + ]; + sample_table { + moduleName = "usb_pnp_sample_driver"; + serviceName = "usb_pnp_sample_service"; + length = 19; + matchFlag = 0x0003; + vendorId = 0xFFF0; + productId = 0xFFF0; + bcdDeviceLow = 0x0000; + bcdDeviceHigh = 0x0000; + deviceClass = 0; + deviceSubClass = 0; + deviceProtocol = 0; + interfaceClass = 0; + interfaceSubClass = 0; + interfaceProtocol = 0; + interfaceNumber = [0, 1]; + } + host_acm_table { + moduleName = "usbhost_acm"; + serviceName = "usbhost_acm_pnp_service"; + length = 19; + matchFlag = 0x0403; + vendorId = 0x0526; + productId = 0xA4A7; + bcdDeviceLow = 0x0000; + bcdDeviceHigh = 0x0000; + deviceClass = 0; + deviceSubClass = 0; + deviceProtocol = 0; + interfaceClass = 0; + interfaceSubClass = 0; + interfaceProtocol = 0; + interfaceNumber = [0, 1]; + } + } + } +} diff --git a/uhdf2/hdi/BUILD.gn b/uhdf2/hdi/BUILD.gn new file mode 100755 index 0000000..bb7c148 --- /dev/null +++ b/uhdf2/hdi/BUILD.gn @@ -0,0 +1,88 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("uhdf_hidl_pkg") { + deps = [ ":libhdi" ] +} + +config("libhdi_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ + "//utils/native/base/include", + "$hdf_framework_path/ability/sbuf/include", + "$hdf_uhdf_path/ipc/include", + "$hdf_uhdf_path/hdi/include", + "$hdf_uhdf_path/include/hdi", + "$hdf_uhdf_path/shared/include", + ] +} + +ohos_shared_library("libhdi") { + include_dirs = [ + "$hdf_framework_path/include", + "$hdf_framework_path/include/core", + "$hdf_framework_path/include/osal", + "$hdf_framework_path/include/utils", + "$hdf_framework_path/core/sec/include", + "$hdf_framework_path/utils/include", + "$hdf_framework_path/core/shared/include", + "$hdf_framework_path/core/host/include", + "$hdf_framework_path/core/manager/include", + "$hdf_uhdf_path/include/devhost", + "$hdf_uhdf_path/devhost/include", + "$hdf_uhdf_path/devmgr/include", + "$hdf_uhdf_path/host/include", + "$hdf_uhdf_path/manager/include", + "$hdf_uhdf_path/include/host", + ] + public_configs = [ ":libhdi_pub_config" ] + sources = [ + "src/devmgr_hdi.c", + "src/hdf_load_hdi.c", + "src/iservmgr_hdi.cpp", + "src/servmgr_hdi.c", + ] + + deps = [ + "$hdf_uhdf_path/ipc:libhdf_ipc_adapter", + "$hdf_uhdf_path/osal:libhdf_utils", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ + "communication_L2:ipc_core", + "hiviewdfx_hilog_native:libhilog", + "samgr_L2:samgr_proxy", + ] + } else { + external_deps = [ + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/hdi/src/devmgr_hdi.c b/uhdf2/hdi/src/devmgr_hdi.c new file mode 100644 index 0000000..4c41a8e --- /dev/null +++ b/uhdf2/hdi/src/devmgr_hdi.c @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_hdi.h" +#include +/* + * The "devmgr_service_stub.h" header file should be used. Actually, only the interface ID definition and service name + * are required here. This dependency can be removed when interface proxy impl is automatically generated from IDL. + */ +#include +#include +#include +#include +#include +#include + +#define HDF_LOG_TAG devmgr_interface + +static void DevmgrFreeQueryDeviceListImpl(struct DeviceInfoList *list); + +static int32_t DeviceManagerHdiCall(struct HDIDeviceManager *iDevMgr, int32_t id, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + if (iDevMgr->remote == NULL || iDevMgr->remote->dispatcher == NULL || + iDevMgr->remote->dispatcher->Dispatch == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + return iDevMgr->remote->dispatcher->Dispatch(iDevMgr->remote, id, data, reply); +} + +static int32_t HdfObtainDeviceInfo(struct DeviceInfoList *list, struct HdfSBuf *reply) +{ + struct DeviceInfoNode *node = NULL; + const char *svrName = NULL; + int32_t svrNameLen; + char *base = NULL; + int32_t deviceType; + + while ((svrName = HdfSbufReadString(reply))) { + svrNameLen = strlen(svrName) + 1; + base = (char *)OsalMemCalloc(sizeof(*node) + svrNameLen); + if (base == NULL) { + DevmgrFreeQueryDeviceListImpl(list); + return HDF_FAILURE; + } + node = (struct DeviceInfoNode *)base; + node->svcName = base + sizeof(*node); + if (strcpy_s(node->svcName, svrNameLen, svrName) != EOK) { + HDF_LOGE("strcpy service name %s failed", svrName); + OsalMemFree(base); + continue; + } + HDF_LOGD("%s %s", __func__, svrName); + HdfSbufReadInt32(reply, &deviceType); + if (deviceType != HDF_LOCAL_SERVICE && deviceType != HDF_REMOTE_SERVICE) { + HDF_LOGE("device type error %d ", deviceType); + OsalMemFree(base); + continue; + } + node->deviceType = deviceType; + DListInsertTail(&node->node, &list->list); + list->deviceCnt++; + } + + return HDF_SUCCESS; +} +static int32_t DevmgrQueryDeviceInfo(struct HDIDeviceManager *iDevMgr, struct DeviceInfoList *list, int32_t type) +{ + struct HdfSBuf *reply = NULL; + struct HdfSBuf *data = NULL; + int32_t ret; + + reply = HdfSBufTypedObtain(SBUF_IPC); + if (reply == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + + data = HdfSBufTypedObtain(SBUF_IPC); + if (data == NULL) { + HdfSBufRecycle(reply); + return HDF_ERR_MALLOC_FAIL; + } + + list->deviceCnt = 0; + DListHeadInit(&list->list); + + HdfSbufWriteInt32(data, type); + ret = DeviceManagerHdiCall(iDevMgr, DEVMGR_SERVICE_QUERY_DEVICE, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("DevmgrProxyQueryDevice failed"); + goto finished; + } + + ret = HdfObtainDeviceInfo(list, reply); + +finished: + HdfSBufRecycle(reply); + HdfSBufRecycle(data); + + return ret; +} + +static int32_t DevmgrQueryUsableDeviceInfo(struct HDIDeviceManager *self, struct DeviceInfoList *list) +{ + if (self == NULL || list == NULL) { + return HDF_ERR_INVALID_PARAM; + } + return DevmgrQueryDeviceInfo(self, list, HDF_SERVICE_USABLE); +} + +static int32_t DevmgrQueryUnusableDeviceInfo(struct HDIDeviceManager *self, struct DeviceInfoList *list) +{ + if (self == NULL || list == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + return DevmgrQueryDeviceInfo(self, list, HDF_SERVICE_UNUSABLE); +} + +static void DevmgrFreeQueryDeviceListImpl(struct DeviceInfoList *list) +{ + if (list == NULL) { + return; + } + + struct DeviceInfoNode *devNode = NULL; + struct DeviceInfoNode *tmp = NULL; + DLIST_FOR_EACH_ENTRY_SAFE(devNode, tmp, &list->list, struct DeviceInfoNode, node) { + DListRemove(&devNode->node); + OsalMemFree(devNode); + } + list->deviceCnt = 0; +} + +static void DevmgrFreeQueryDeviceList(struct HDIDeviceManager *self, struct DeviceInfoList *list) +{ + (void)self; + DevmgrFreeQueryDeviceListImpl(list); +} + +static int32_t HdfOpsDevice(struct HDIDeviceManager *iDevMgr, + const char *moduleName, const char *serviceName, int opsId) +{ + int32_t status = HDF_FAILURE; + if (iDevMgr == NULL || moduleName == NULL || serviceName == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + if (data == NULL || reply == NULL) { + status = HDF_ERR_MALLOC_FAIL; + goto out; + } + + if (!HdfSbufWriteString(data, moduleName)) { + HDF_LOGE("%s: writing module name failed!", __func__); + goto out; + } + if (!HdfSbufWriteString(data, serviceName)) { + HDF_LOGE("%s: writing service name failed!", __func__); + goto out; + } + status = DeviceManagerHdiCall(iDevMgr, opsId, data, reply); + if (status == HDF_SUCCESS) { + HdfSbufReadInt32(reply, &status); + } +out: + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return status; +} + +static int32_t DevmgrRegPnpDevice(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName) +{ + return HdfOpsDevice(self, moduleName, serviceName, DEVMGR_SERVICE_REGIST_PNP_DEVICE); +} + +static int32_t DevmgrUnRegPnpDevice(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName) +{ + return HdfOpsDevice(self, moduleName, serviceName, DEVMGR_SERVICE_UNREGIST_PNP_DEVICE); +} + +static int32_t DevmgrRegVirtualDevice(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName) +{ + return HdfOpsDevice(self, moduleName, serviceName, DEVMGR_SERVICE_REGISTER_VIRTUAL_DEVICE); +} + +static int32_t DevmgrUnRegVirtualDevice(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName) +{ + return HdfOpsDevice(self, moduleName, serviceName, DEVMGR_SERVICE_UNREGISTER_VIRTUAL_DEVICE); +} + +static void HDIDeviceManagerConstruct(struct HDIDeviceManager *inst) +{ + inst->FreeQueryDeviceList = DevmgrFreeQueryDeviceList; + inst->QueryUsableDeviceInfo = DevmgrQueryUsableDeviceInfo; + inst->QueryUnusableDeviceInfo = DevmgrQueryUnusableDeviceInfo; + inst->RegPnpDevice = DevmgrRegPnpDevice; + inst->UnRegPnpDevice = DevmgrUnRegPnpDevice; + inst->RegVirtualDevice = DevmgrRegVirtualDevice; + inst->UnRegVirtualDevice = DevmgrUnRegVirtualDevice; +} + +struct HDIDeviceManager *HDIDeviceManagerGet(void) +{ + struct HDIServiceManager *serviceMgr = HDIServiceManagerGet(); + if (serviceMgr == NULL) { + return NULL; + } + struct HdfRemoteService *remote = serviceMgr->GetService(serviceMgr, DEVICE_MANAGER_SERVICE); + HDIServiceManagerRelease(serviceMgr); + if (remote == NULL) { + HDF_LOGE("%s: hdi service %s not found", __func__, DEVICE_MANAGER_SERVICE); + return NULL; + } + + struct HDIDeviceManager *iDevMgr = OsalMemAlloc(sizeof(struct HDIDeviceManager)); + if (iDevMgr == NULL) { + HDF_LOGE("%s: OOM", __func__); + HdfRemoteServiceRecycle(remote); + return NULL; + } + + iDevMgr->remote = remote; + HDIDeviceManagerConstruct(iDevMgr); + return iDevMgr; +} + +void HDIDeviceManagerRelease(struct HDIDeviceManager *devmgr) +{ + if (devmgr == NULL) { + return; + } + + HdfRemoteServiceRecycle(devmgr->remote); + OsalMemFree(devmgr); +} \ No newline at end of file diff --git a/uhdf2/hdi/src/hdf_load_hdi.c b/uhdf2/hdi/src/hdf_load_hdi.c new file mode 100644 index 0000000..07c0dd0 --- /dev/null +++ b/uhdf2/hdi/src/hdf_load_hdi.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include "hdf_base.h" +#include "hdf_load_hdi.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "securec.h" + +#define HDF_LOG_TAG dev_load_hdi + +#ifdef __OHOS_STANDARD_SYS__ +#define HDI_SO_PATH "/system/lib" +#else +#define HDI_SO_PATH "/vendor/lib64" +#endif +struct HdiObject *LoadHdi(const char *name, uint32_t version) +{ + char path[PATH_MAX + 1] = {0}; + char resolvedPath[PATH_MAX + 1] = {0}; + + if (name == NULL) { + HDF_LOGE("name is NULL"); + return NULL; + } + + if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s/%s", HDI_SO_PATH, name) < 0) { + HDF_LOGE("%s snprintf_s failed", __func__); + return NULL; + } + if (realpath(path, resolvedPath) == NULL) { + HDF_LOGE("%s file name invalid", __func__); + return NULL; + } + + struct HdiObject *hdi = (struct HdiObject *)OsalMemCalloc(sizeof(*hdi)); + if (hdi == NULL) { + HDF_LOGE("%s malloc failed", __func__); + return NULL; + } + void *handler = dlopen(resolvedPath, RTLD_LAZY); + if (handler == NULL) { + HDF_LOGE("%s dlopen failed %s", __func__, dlerror()); + OsalMemFree(hdi); + return NULL; + } + struct HdiBase *base = *(struct HdiBase **)dlsym(handler, "hdfHdiDesc"); + if (base == NULL) { + HDF_LOGE("%s dlsym failed %s", __func__, dlerror()); + dlclose(handler); + OsalMemFree(hdi); + return NULL; + } + if (version != base->moduleVersion) { + dlclose(handler); + OsalMemFree(hdi); + return NULL; + } + if (base->OpenHdi) { + base->OpenHdi(); + } + hdi->dlHandler = (uintptr_t)handler; + hdi->hdiBase = base; + return hdi; +} + +void CloseHdi(struct HdiObject *hdi) +{ + if (hdi == NULL || hdi->dlHandler == 0 || hdi->hdiBase == NULL) { + HDF_LOGE("%s para invalid", __func__); + return; + } + + struct HdiBase *base = hdi->hdiBase; + + if (base->CloseHdi) { + base->CloseHdi(); + } + + dlclose((void *)hdi->dlHandler); + hdi->dlHandler = 0; + hdi->hdiBase = NULL; + OsalMemFree(hdi); +} + diff --git a/uhdf2/hdi/src/iservmgr_hdi.cpp b/uhdf2/hdi/src/iservmgr_hdi.cpp new file mode 100755 index 0000000..0e5b5d5 --- /dev/null +++ b/uhdf2/hdi/src/iservmgr_hdi.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include "iservmgr_hdi.h" +#include "hdf_log.h" + +namespace OHOS { +namespace HDI { +namespace ServiceManager { +namespace V1_0 { +constexpr int DEVICE_SERVICE_MANAGER_SA_ID = 5001; +constexpr int DEVSVC_MANAGER_GET_SERVICE = 2; + +class ServiceManagerProxy : public IRemoteProxy { +public: + explicit ServiceManagerProxy(const sptr& impl) : IRemoteProxy(impl) {} + ~ServiceManagerProxy() {} + virtual sptr GetService(const char* serviceName) override; +private: + static inline BrokerDelegator delegator_; +}; + +sptr IServiceManager::Get() +{ + auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + HDF_LOGE("failed to get sa manager"); + return nullptr; + } + sptr remote = saManager->GetSystemAbility(DEVICE_SERVICE_MANAGER_SA_ID); + if (remote != nullptr) { + return iface_cast(remote); + } + + HDF_LOGE("failed to get sa hdf service manager"); + return nullptr; +} + +sptr ServiceManagerProxy::GetService(const char* serviceName) +{ + MessageParcel data; + MessageParcel reply; + if (!data.WriteCString(serviceName)) { + return nullptr; + } + + MessageOption option; + int status = Remote()->SendRequest(DEVSVC_MANAGER_GET_SERVICE, data, reply, option); + if (status) { + HDF_LOGE("get hdi service call failed, %d", status); + return nullptr; + } + HDF_LOGE("get hdi service call success, %d", status); + return reply.ReadRemoteObject(); +} +} // namespace V1_0 +} // namespace ServiceManager +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/uhdf2/hdi/src/servmgr_hdi.c b/uhdf2/hdi/src/servmgr_hdi.c new file mode 100644 index 0000000..82312fa --- /dev/null +++ b/uhdf2/hdi/src/servmgr_hdi.c @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "servmgr_hdi.h" +#include +#include +#include +#include +#include + +static int32_t ServiceManagerHdiCall(struct HDIServiceManager *iServMgr, int32_t id, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + if (iServMgr->remote == NULL || iServMgr->remote->dispatcher == NULL || + iServMgr->remote->dispatcher->Dispatch == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + return iServMgr->remote->dispatcher->Dispatch(iServMgr->remote, id, data, reply); +} + + +struct HdfRemoteService *HDIServiceManagerGetService(struct HDIServiceManager *iServMgr, const char* serviceName) +{ + if (iServMgr == NULL || serviceName == NULL) { + return NULL; + } + + struct HdfSBuf *data = NULL; + struct HdfSBuf *reply = NULL; + struct HdfRemoteService *service = NULL; + + do { + data = HdfSBufTypedObtain(SBUF_IPC); + reply = HdfSBufTypedObtain(SBUF_IPC); + if (data == NULL || reply == NULL) { + break; + } + + if (!HdfSbufWriteString(data, serviceName)) { + break; + } + int status = ServiceManagerHdiCall(iServMgr, DEVSVC_MANAGER_GET_SERVICE, data, reply); + if (status == HDF_SUCCESS) { + service = HdfSBufReadRemoteService(reply); + } else { + HDF_LOGI("%s: %s not found", __func__, serviceName); + } + } while (0); + + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } + return service; +} + +void HDIServiceManagerConstruct(struct HDIServiceManager *inst) +{ + inst->GetService = HDIServiceManagerGetService; +} + +struct HDIServiceManager *HDIServiceManagerGet(void) +{ + struct HdfRemoteService *remote = HdfRemoteServiceGet(DEVICE_SERVICE_MANAGER_SA_ID); + if (remote == NULL) { + HDF_LOGE("%s: hdi service %s not found", __func__, DEVICE_SERVICE_MANAGER); + return NULL; + } + + struct HDIServiceManager *iServMgr = OsalMemAlloc(sizeof(struct HDIServiceManager)); + if (iServMgr == NULL) { + HDF_LOGE("%s: OOM", __func__); + HdfRemoteServiceRecycle(remote); + return NULL; + } + + iServMgr->remote = remote; + HDIServiceManagerConstruct(iServMgr); + return iServMgr; +} + +void HDIServiceManagerRelease(struct HDIServiceManager *servmgr) +{ + if (servmgr == NULL) { + return; + } + + HdfRemoteServiceRecycle(servmgr->remote); + OsalMemFree(servmgr); +} \ No newline at end of file diff --git a/uhdf2/hdi/test/BUILD.gn b/uhdf2/hdi/test/BUILD.gn new file mode 100755 index 0000000..8e24ab1 --- /dev/null +++ b/uhdf2/hdi/test/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2021 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/test.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +module_output_path = "hdf/hdi" + +###########################palTest########################### + +ohos_unittest("HdiServiceManagerTest") { + module_out_path = module_output_path + sources = [ "servmgr/service_manager_hdi_c_test.cpp" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/ipc:libhdf_ipc_adapter", + "$hdf_uhdf_path/osal:libhdf_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + include_dirs = [ "$hdf_uhdf_path/host/test/unittest/sample_driver" ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} + +ohos_unittest("HdiServiceManagerTestCC") { + module_out_path = module_output_path + sources = [ "servmgr/service_manager_hdi_test.cpp" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/osal:libhdf_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + include_dirs = [ "$hdf_uhdf_path/host/test/unittest/sample_driver" ] + if (is_standard_system) { + external_deps = [ + "communication_L2:ipc_core", + "hiviewdfx_hilog_native:libhilog", + "samgr_L2:samgr_proxy", + ] + } else { + external_deps = [ + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + } +} + +###########################end########################### +group("unittest") { + testonly = true + deps = [ + ":HdiServiceManagerTest", + ":HdiServiceManagerTestCC", + ] +} diff --git a/uhdf2/hdi/test/dev_query/hdf_dev_query_test.c b/uhdf2/hdi/test/dev_query/hdf_dev_query_test.c new file mode 100644 index 0000000..4afe0f4 --- /dev/null +++ b/uhdf2/hdi/test/dev_query/hdf_dev_query_test.c @@ -0,0 +1,222 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "devmgr_hdi.h" +#include "osal_time.h" + +static int32_t g_testItemCnt = 0; +static int32_t g_testItemFailCnt = 0; + +#define HDF_USABLE_DEVICE_CNT 100 +#define HDF_UNUSABLE_DEVICE_CNT 10 +#define HDF_QUERY_TIME_RANGE 10 +#define HDF_QUERY_DEVICE_ONE 0xffff +#define HDF_QUERY_DEVICE_MORE 1000 +#define HDF_MICRO_UNIT 1000000 +#define TEST_ITEM_CNT 3 +#define USABLE 0 +#define UNUSABLE 1 +#define FREE 2 + +static int g_index[TEST_ITEM_CNT]; +static uint64_t g_totalUsec[TEST_ITEM_CNT]; + +#define GET_TIME_BEGIN(time) OsalGetTime(time) +#define GET_TIME_END(index, time1, time2, diff) do { \ + OsalGetTime(time2); \ + OsalDiffTime(time1, time2, diff); \ + CalcAverageTime(index, diff); \ +} while (0) + +#define UT_TEST_CHECK_RET(val, flag) do { \ + if (!(val)) { \ + HDF_LOGE("[HDF_TEST] %s line:%d HDF_TEST_FAIL", __func__, __LINE__); \ + printf("[HDF_TEST] %s line:%d HDF_TEST_FAIL\r\n", __func__, __LINE__); \ + g_testItemFailCnt++; \ + } else if ((flag)) { \ + HDF_LOGD("[HDF_TEST] %s line:%d HDF_TEST_PASS", __func__, __LINE__); \ + printf("[HDF_TEST] %s line:%d HDF_TEST_PASS\r\n", __func__, __LINE__); \ + } \ + g_testItemCnt++; \ +} while (0) + +#define HDF_LOG_TAG hdf_query_device_test + +void PrintTestResult() +{ + HDF_LOGE("[HDF_TEST] %s test items: %d", __func__, g_testItemCnt); + HDF_LOGE("[HDF_TEST] %s test PASS: %d", __func__, g_testItemCnt - g_testItemFailCnt); + HDF_LOGE("[HDF_TEST] %s test FAIL: %d", __func__, g_testItemFailCnt); + printf("[HDF_TEST] %s test items: %d\r\n", __func__, g_testItemCnt); + printf("[HDF_TEST] %s test PASS: %d\r\n", __func__, g_testItemCnt - g_testItemFailCnt); + printf("[HDF_TEST] %s test FAIL: %d\r\n", __func__, g_testItemFailCnt); +} + +static void CalcAverageTime(int index, const OsalTimespec *diff) +{ + g_totalUsec[index] += diff->sec * HDF_MICRO_UNIT + diff->usec; + g_index[index]++; +} + +static void CheckAverageTime() +{ + HDF_LOGD("[HDF_TEST] %s line:%d test query usable %d time use time:%lu us", + __func__, __LINE__, g_index[USABLE], g_totalUsec[USABLE]); + printf("[HDF_TEST] %s line:%d test query usable %d time use time: %lu us\r\n", + __func__, __LINE__, g_index[USABLE], g_totalUsec[USABLE]); + + HDF_LOGD("[HDF_TEST] %s line:%d test query unusable %d time use time:%lu us", + __func__, __LINE__, g_index[UNUSABLE], g_totalUsec[UNUSABLE]); + printf("[HDF_TEST] %s line:%d test query nusable %d time use time: %lu us\r\n", + __func__, __LINE__, g_index[UNUSABLE], g_totalUsec[UNUSABLE]); + + HDF_LOGD("[HDF_TEST] %s line:%d test query free %d time use time:%lu us", + __func__, __LINE__, g_index[FREE], g_totalUsec[FREE]); + printf("[HDF_TEST] %s line:%d test query free %d time use time: %lu us\r\n", + __func__, __LINE__, g_index[FREE], g_totalUsec[FREE]); + + UT_TEST_CHECK_RET((g_totalUsec[USABLE] / HDF_KILO_UNIT / g_index[USABLE]) <= HDF_QUERY_TIME_RANGE, true); + UT_TEST_CHECK_RET((g_totalUsec[UNUSABLE] / HDF_KILO_UNIT / g_index[UNUSABLE]) <= HDF_QUERY_TIME_RANGE, true); + UT_TEST_CHECK_RET((g_totalUsec[FREE] / HDF_KILO_UNIT / g_index[FREE]) <= HDF_QUERY_TIME_RANGE, true); +} + + +void HdfFreeQueryDeviceList(struct DeviceInfoList *list) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return; + } + + iDevmgr->FreeQueryDeviceList(iDevmgr, list); + + HDIDeviceManagerRelease(iDevmgr); +} + +int32_t HdfQueryUsableDeviceInfo(struct DeviceInfoList *list) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->QueryUsableDeviceInfo(iDevmgr, list); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +int32_t HdfQueryUnusableDeviceInfo(struct DeviceInfoList *list) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->QueryUnusableDeviceInfo(iDevmgr, list); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +/* + * HDF_QUERY_DEVICE_BaseFunction_AR_001 HDF_QUERY_DEVICE_BaseFunction_AR_002 + * HDF_QUERY_DEVICE_BaseFunction_AR_005 HDF_QUERY_DEVICE_BaseFunction_AR_006 + */ +static void QueryDeviceUsableTest(int index) +{ + int32_t ret; + OsalTimespec time1 = { 0, 0 }; + OsalTimespec time2 = { 0, 0 }; + OsalTimespec diff = { 0, 0 }; + bool flag = (index == HDF_QUERY_DEVICE_ONE) ? true : false; + + struct DeviceInfoList list; + struct DeviceInfoNode *devNode = NULL; + GET_TIME_BEGIN(&time1); + + ret = HdfQueryUsableDeviceInfo(&list); + GET_TIME_END(USABLE, &time1, &time2, &diff); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS, flag); + if (ret == HDF_SUCCESS) { + UT_TEST_CHECK_RET(list.deviceCnt == HDF_USABLE_DEVICE_CNT, flag); + HDF_LOGD("[HDF_TEST] %s usable:%d line:%d", __func__, list.deviceCnt, __LINE__); + if (flag) { + printf("[HDF_TEST] %s usable:%d line:%d\r\n", __func__, list.deviceCnt, __LINE__); + } + DLIST_FOR_EACH_ENTRY(devNode, &list.list, struct DeviceInfoNode, node) { + HDF_LOGD("usable: %s %d", devNode->svcName, devNode->deviceType); + } + } else { + HDF_LOGE("[HDF_TEST] %s line:%d", __func__, __LINE__); + } + + GET_TIME_BEGIN(&time1); + HdfFreeQueryDeviceList(&list); + GET_TIME_END(FREE, &time1, &time2, &diff); +} + +/* HDF_QUERY_DEVICE_BaseFunction_AR_003 HDF_QUERY_DEVICE_BaseFunction_AR_004 HDF_QUERY_DEVICE_BaseFunction_AR_007 */ +static void QueryDeviceUnusableTest(int index) +{ + int32_t ret; + OsalTimespec time1 = {0, 0}; + OsalTimespec time2 = {0, 0}; + OsalTimespec diff = {0, 0}; + bool flag = (index == HDF_QUERY_DEVICE_ONE) ? true : false; + + struct DeviceInfoList list; + struct DeviceInfoNode *devNode = NULL; + GET_TIME_BEGIN(&time1); + ret = HdfQueryUnusableDeviceInfo(&list); + GET_TIME_END(UNUSABLE, &time1, &time2, &diff); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS, flag); + if (ret == HDF_SUCCESS) { + UT_TEST_CHECK_RET(list.deviceCnt == HDF_UNUSABLE_DEVICE_CNT, flag); + HDF_LOGD("[HDF_TEST] %s unusable:%d line:%d", __func__, list.deviceCnt, __LINE__); + if (flag) { + printf("[HDF_TEST] %s unusable:%d line:%d\r\n", __func__, list.deviceCnt, __LINE__); + } + DLIST_FOR_EACH_ENTRY(devNode, &list.list, struct DeviceInfoNode, node) { + HDF_LOGD("[HDF_TEST] usable: %s %d", devNode->svcName, devNode->deviceType); + } + } else { + HDF_LOGE("%s %d", __func__, __LINE__); + } + + HdfFreeQueryDeviceList(&list); + GET_TIME_END(FREE, &time1, &time2, &diff); +} + +/* HDF_QUERY_DEVICE_BaseFunction_AR_008 */ +int main(int argc, char **argv) +{ + (void)argv; + if (argc == 1) { + QueryDeviceUsableTest(HDF_QUERY_DEVICE_ONE); + QueryDeviceUnusableTest(HDF_QUERY_DEVICE_ONE); + } else { + int testCnt = HDF_QUERY_DEVICE_MORE; + for (int i = 0; i < testCnt; i++) { + QueryDeviceUsableTest(i); + QueryDeviceUnusableTest(i); + } + } + CheckAverageTime(); + PrintTestResult(); + return 0; +} + diff --git a/uhdf2/hdi/test/dev_query/unittest/hcs/tas/device_info.hcs b/uhdf2/hdi/test/dev_query/unittest/hcs/tas/device_info.hcs new file mode 100755 index 0000000..6a917fd --- /dev/null +++ b/uhdf2/hdi/test/dev_query/unittest/hcs/tas/device_info.hcs @@ -0,0 +1,1050 @@ + root { + device_info { + match_attr = "hdf_manager"; + template host { + hostName = ""; + priority = 100; + template device { + template deviceNode { + policy = 0; + priority = 100; + preload = 0; + permission = 0664; + moduleName = ""; + serviceName = ""; + deviceMatchAttr = ""; + } + } + } + platform :: host { + hostName = "platform_host"; + priority = 50; + driver_io_ability :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver"; + } + } + driver_io_ability1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver1"; + } + } + driver_io_ability2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver2"; + } + } + driver_io_ability3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver3"; + } + } + driver_io_ability4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver4"; + } + } + driver_io_ability5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver5"; + } + } + driver_io_ability6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "sample_driver_no_SO"; + } + } + driver_io_ability7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver7"; + } + } + driver_io_ability8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver8"; + } + } + driver_io_ability9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver9"; + } + } + driver_io_ability10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "sample_driver10"; + } + } + } + serviceA :: host { + hostName = "serviceA_host"; + priority = 50; + serviceA :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver"; + } + } + serviceA1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver1"; + } + } + serviceA2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver2"; + } + } + serviceA3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver3"; + } + } + serviceA4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver4"; + } + } + serviceA5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver5"; + } + } + serviceA6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceA_driver_no_SO"; + } + } + serviceA7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver7"; + } + } + serviceA8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver8"; + } + } + serviceA9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver9"; + } + } + serviceA10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceA_driver10"; + } + } + } + serviceB :: host { + hostName = "serviceB_host"; + priority = 50; + serviceB :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver"; + } + } + serviceB1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver1"; + } + } + serviceB2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver2"; + } + } + serviceB3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver3"; + } + } + serviceB4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver4"; + } + } + serviceB5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver5"; + } + } + serviceB6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceB_driver_no_SO"; + } + } + serviceB7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver7"; + } + } + serviceB8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver8"; + } + } + serviceB9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver9"; + } + } + serviceB10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceB_driver10"; + } + } + } + serviceC :: host { + hostName = "serviceC_host"; + priority = 50; + serviceC :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver"; + } + } + serviceC1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver1"; + } + } + serviceC2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver2"; + } + } + serviceC3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver3"; + } + } + serviceC4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver4"; + } + } + serviceC5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver5"; + } + } + serviceC6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceC_driver_no_SO"; + } + } + serviceC7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver7"; + } + } + serviceC8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver8"; + } + } + serviceC9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver9"; + } + } + serviceC10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceC_driver10"; + } + } + } + serviceD :: host { + hostName = "serviceD_host"; + priority = 50; + serviceD :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver"; + } + } + serviceD1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver1"; + } + } + serviceD2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver2"; + } + } + serviceD3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver3"; + } + } + serviceD4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver4"; + } + } + serviceD5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver5"; + } + } + serviceD6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceD_driver_no_SO"; + } + } + serviceD7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver7"; + } + } + serviceD8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver8"; + } + } + serviceD9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver9"; + } + } + serviceD10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceD_driver10"; + } + } + } + serviceE :: host { + hostName = "serviceE_host"; + priority = 50; + serviceE :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver"; + } + } + serviceE1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver1"; + } + } + serviceE2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver2"; + } + } + serviceE3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver3"; + } + } + serviceE4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver4"; + } + } + serviceE5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver5"; + } + } + serviceE6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceE_driver_no_SO"; + } + } + serviceE7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver7"; + } + } + serviceE8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver8"; + } + } + serviceE9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver9"; + } + } + serviceE10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceE_driver10"; + } + } + } + serviceF :: host { + hostName = "serviceF_host"; + priority = 50; + serviceF :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver"; + } + } + serviceF1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver1"; + } + } + serviceF2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver2"; + } + } + serviceF3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver3"; + } + } + serviceF4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver4"; + } + } + serviceF5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver5"; + } + } + serviceF6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceF_driver_no_SO"; + } + } + serviceF7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver7"; + } + } + serviceF8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver8"; + } + } + serviceF9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver9"; + } + } + serviceF10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceF_driver10"; + } + } + } + serviceH :: host { + hostName = "serviceH_host"; + priority = 50; + serviceH :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver"; + } + } + serviceH1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver1"; + } + } + serviceH2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver2"; + } + } + serviceH3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver3"; + } + } + serviceH4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver4"; + } + } + serviceH5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver5"; + } + } + serviceH6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceH_driver_no_SO"; + } + } + serviceH7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver7"; + } + } + serviceH8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver8"; + } + } + serviceH9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver9"; + } + } + serviceH10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceH_driver10"; + } + } + } + serviceG :: host { + hostName = "serviceG_host"; + priority = 50; + serviceG :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver"; + } + } + serviceG1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver1"; + } + } + serviceG2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver2"; + } + } + serviceG3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver3"; + } + } + serviceG4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver4"; + } + } + serviceG5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver5"; + } + } + serviceG6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceG_driver_no_SO"; + } + } + serviceG7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver7"; + } + } + serviceG8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver8"; + } + } + serviceG9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver9"; + } + } + serviceG10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceG_driver10"; + } + } + } + serviceI :: host { + hostName = "serviceI_host"; + priority = 50; + serviceI :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver"; + } + } + serviceI1 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver1"; + } + } + serviceI2 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver2"; + } + } + serviceI3 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver3"; + } + } + serviceI4 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver4"; + } + } + serviceI5 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver5"; + } + } + serviceI6 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver1.so"; + serviceName = "serviceI_driver_no_SO"; + } + } + serviceI7 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver7"; + } + } + serviceI8 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver8"; + } + } + serviceI9 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver9"; + } + } + serviceI10 :: device { + device0 :: deviceNode { + policy = 2; + priority = 100; + permission = 0644; + moduleName = "hdf_test_query_device_driver.so"; + serviceName = "serviceI_driver10"; + } + } + } + } +} diff --git a/uhdf2/hdi/test/dev_query/unittest/sample_driver/hdf_sample_driver.c b/uhdf2/hdi/test/dev_query/unittest/sample_driver/hdf_sample_driver.c new file mode 100644 index 0000000..df2e8ff --- /dev/null +++ b/uhdf2/hdi/test/dev_query/unittest/sample_driver/hdf_sample_driver.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" + +#define HDF_LOG_TAG hdf_sample_driver + +int32_t HdfSampleDriverBind(struct HdfDeviceObject *para) +{ + (void)para; + + HDF_LOGE("%s", __func__); + return 0; +} + +int32_t HdfSampleDriverInit(struct HdfDeviceObject *para) +{ + (void)para; + + HDF_LOGE("%s", __func__); + return 0; +} + +void HdfSampleDriverRelease(struct HdfDeviceObject *para) +{ + (void)para; + + HDF_LOGE("%s", __func__); +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .Bind = HdfSampleDriverBind, + .Init = HdfSampleDriverInit, + .Release = HdfSampleDriverRelease, + .moduleName = "sample_driver", +}; +HDF_INIT(g_sampleDriverEntry); + diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_c/sample_client_c_test.cpp b/uhdf2/hdi/test/hdi_sample/sample_client_c/sample_client_c_test.cpp new file mode 100755 index 0000000..7580b3b --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_c/sample_client_c_test.cpp @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include +#include +#include "securec.h" +#include "isample.h" + +using namespace OHOS; +using namespace testing::ext; + +#define HDF_LOG_TAG sample_client_c_test + +constexpr const char *TEST_SERVICE_NAME = "sample_service_c"; + +class SampleObjCTest : public testing::Test { +public: + static void SetUpTestCase(){} + static void TearDownTestCase(){} + void SetUp(){} + void TearDown(){} +}; + +HWTEST_F(SampleObjCTest, SampleObjCTest_001, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + bool input = true; + bool output = false; + + int32_t ec = sampleObj->BooleanTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_TRUE(output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_002, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int8_t input = 10; + int8_t output; + + int32_t ec = sampleObj->ByteTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_003, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int16_t input = 10; + int16_t output; + + int32_t ec = sampleObj->ShortTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_004, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int32_t input = 10; + int32_t output; + + int32_t ec = sampleObj->IntTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_005, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int64_t input = 10; + int64_t output; + + int32_t ec = sampleObj->LongTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_006, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + float input = 10; + float output; + + int32_t ec = sampleObj->FloatTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_007, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + double input = 10; + double output; + + int32_t ec = sampleObj->DoubleTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_008, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + const char *input = "c sample client test"; + char *output = nullptr; + + int32_t ec = sampleObj->StringTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + EXPECT_STREQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_009, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint8_t input = 10; + uint8_t output; + + int32_t ec = sampleObj->UcharTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_010, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint16_t input = 10; + uint16_t output; + + int32_t ec = sampleObj->UshortTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_011, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint32_t input = 10; + uint32_t output; + + int32_t ec = sampleObj->UintTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_012, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint64_t input = 10; + uint64_t output; + + int32_t ec = sampleObj->UlongTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_013, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint32_t inSize = 5; + int8_t *input = (int8_t *)OsalMemAlloc(sizeof(int8_t) * inSize); + ASSERT_TRUE(input != nullptr); + + for (uint32_t i = 0; i < inSize; i++) { + input[i] = static_cast(i); + } + + uint32_t outSize = 0; + int8_t *output = nullptr; + + int32_t ec = sampleObj->ListTypeTest(sampleObj, input, inSize, &output, &outSize); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_TRUE(output != nullptr); + ASSERT_EQ(outSize, inSize); + + for (uint32_t i = 0; i < outSize; i++) { + ASSERT_EQ(output[i], input[i]); + } + + if (input != nullptr) { + OsalMemFree(input); + } + if (output != nullptr) { + OsalMemFree(output); + } +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_014, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint32_t inSize = 5; + int8_t *input = (int8_t *)OsalMemAlloc(sizeof(int8_t) * inSize); + ASSERT_TRUE(input != nullptr); + + for (uint32_t i = 0; i < inSize; i++) { + input[i] = static_cast(i); + } + + uint32_t outSize = 0; + int8_t *output = nullptr; + + int32_t ec = sampleObj->ArrayTypeTest(sampleObj, input, inSize, &output, &outSize); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_TRUE(output != nullptr); + ASSERT_EQ(outSize, inSize); + + for (uint32_t i = 0; i < outSize; i++) { + ASSERT_EQ(output[i], input[i]); + } + + if (input != nullptr) { + OsalMemFree(input); + } + if (output != nullptr) { + OsalMemFree(output); + } +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_015, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + struct StructSample input = { + .first = 1, + .second = 2, + }; + + struct StructSample output; + int32_t ec = sampleObj->StructTypeTest(sampleObj, &input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(output.first, input.first); + ASSERT_EQ(output.second, input.second); + + HdiSampleRelease(sampleObj); +} + +HWTEST_F(SampleObjCTest, SampleObjCTest_016, TestSize.Level0) +{ + struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + enum EnumSample input = MEM_SECOND; + enum EnumSample output; + + int32_t ec = sampleObj->EnumTypeTest(sampleObj, input, &output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); + + HdiSampleRelease(sampleObj); +} + diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn new file mode 100755 index 0000000..43755d1 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("sample_client_c") { + deps = [ ":libsample_client_c" ] +} + +ohos_shared_library("libsample_client_c") { + + include_dirs = [ + "$hdf_framework_path/ability/sbuf/include", + "$hdf_uhdf_path/manager/include", + "$hdf_uhdf_path/include/hdi", + ] + + sources = [ + "sample_proxy.c" + ] + + deps = [ + "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/host:libhdf_host", + "$hdf_uhdf_path/hdi:libhdi" + ] + + if (is_standard_system) { + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "hilog:libhilog", + ] + } + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/isample.h b/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/isample.h new file mode 100644 index 0000000..6aef4c0 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/isample.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_CLIENT_C_INF_H +#define HDI_SAMPLE_CLIENT_C_INF_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct HdfRemoteService; + +struct StructSample { + int8_t first; + int16_t second; +}; + +enum EnumSample { + MEM_FIRST, + MEM_SECOND, + MEM_THIRD, +}; + +enum { + CMD_BOOLEAN_TYPE_TEST, + CMD_BYTE_TYPE_TEST, + CMD_SHORT_TYPE_TEST, + CMD_INT_TYPE_TEST, + CMD_LONG_TYPE_TEST, + CMD_FLOAT_TYPE_TEST, + CMD_DOUBLE_TYPE_TEST, + CMD_STRING_TYPE_TEST, + CMD_UCHAR_TYPE_TEST, + CMD_USHORT_TYPE_TEST, + CMD_UINT_TYPE_TEST, + CMD_ULONG_TYPE_TEST, + CMD_LIST_TYPE_TEST, + CMD_ARRAY_TYPE_TEST, + CMD_STRUCT_TYPE_TEST, + CMD_ENUM_TYPE_TEST, +}; + +struct ISample { + struct HdfRemoteService *remote; + + int32_t (*BooleanTypeTest)(struct ISample *self, const bool input, bool *output); + + int32_t (*ByteTypeTest)(struct ISample *self, const int8_t input, int8_t *output); + + int32_t (*ShortTypeTest)(struct ISample *self, const int16_t input, int16_t *output); + + int32_t (*IntTypeTest)(struct ISample *self, const int32_t input, int32_t *output); + + int32_t (*LongTypeTest)(struct ISample *self, const int64_t input, int64_t *output); + + int32_t (*FloatTypeTest)(struct ISample *self, const float input, float *output); + + int32_t (*DoubleTypeTest)(struct ISample *self, const double input, double *output); + + int32_t (*StringTypeTest)(struct ISample *self, const char* input, char **output); + + int32_t (*UcharTypeTest)(struct ISample *self, const uint8_t input, uint8_t *output); + + int32_t (*UshortTypeTest)(struct ISample *self, const uint16_t input, uint16_t *output); + + int32_t (*UintTypeTest)(struct ISample *self, const uint32_t input, uint32_t *output); + + int32_t (*UlongTypeTest)(struct ISample *self, const uint64_t input, uint64_t *output); + + int32_t (*ListTypeTest)(struct ISample *self, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize); + + int32_t (*ArrayTypeTest)(struct ISample *self, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize); + + int32_t (*StructTypeTest)(struct ISample *self, const struct StructSample *input, struct StructSample *output); + + int32_t (*EnumTypeTest)(struct ISample *self, const enum EnumSample input, enum EnumSample *output); +}; + +struct ISample *HdiSampleGet(const char *serviceName); + +void HdiSampleRelease(struct ISample *instance); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif // HDI_SAMPLE_CLIENT_C_INF_H \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/sample_proxy.c b/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/sample_proxy.c new file mode 100644 index 0000000..222b3b3 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/sample_proxy.c @@ -0,0 +1,844 @@ +/* + * Copyright (c) 2021 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. + */ + +#include + +#include +#include +#include +#include +#include + +#include "isample.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +static int32_t SampleProxyCall(struct ISample *self, + int32_t id, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + if (self->remote == NULL || self->remote->dispatcher == NULL || + self->remote->dispatcher->Dispatch == NULL) { + HDF_LOGE("%{public}s: obj is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + return self->remote->dispatcher->Dispatch(self->remote, id, data, reply); +} + +static int32_t SampleProxyBooleanTypeTest(struct ISample *self, const bool input, bool *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + HDF_LOGE("%{public}s: HdfSubf malloc failed!", __func__); + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint32(data, (uint32_t)input)) { + HDF_LOGE("%{public}s: write bool input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_BOOLEAN_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint32(reply, (uint32_t *)output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyByteTypeTest(struct ISample *self, const int8_t input, int8_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteInt8(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_BYTE_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadInt8(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyShortTypeTest(struct ISample *self, const int16_t input, int16_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteInt16(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_SHORT_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadInt16(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyIntTypeTest(struct ISample *self, const int32_t input, int32_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteInt32(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_INT_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadInt32(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyLongTypeTest(struct ISample *self, const int64_t input, int64_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteInt64(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_LONG_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadInt64(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyFloatTypeTest(struct ISample *self, const float input, float *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteFloat(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_FLOAT_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadFloat(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + + return ec; +} + +static int32_t SampleProxyDoubleTypeTest(struct ISample *self, const double input, double *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteDouble(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_DOUBLE_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadDouble(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + + return ec; +} + +static int32_t SampleProxyStringTypeTest(struct ISample *self, const char* input, char **output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL || input == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteString(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_STRING_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + const char *result = HdfSbufReadString(reply); + if (result == NULL) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + *output = strdup(result); + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyUcharTypeTest(struct ISample *self, const uint8_t input, uint8_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint8(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_UCHAR_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint8(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyUshortTypeTest(struct ISample *self, const uint16_t input, uint16_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint16(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_USHORT_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint16(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyUintTypeTest(struct ISample *self, const uint32_t input, uint32_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint32(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_UINT_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint32(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyUlongTypeTest(struct ISample *self, const uint64_t input, uint64_t *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint64(data, input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_ULONG_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint64(reply, output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyListTypeTest(struct ISample *self, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL || input == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint32(data, inSize)) { + HDF_LOGE("%{public}s: write input size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + for (uint32_t i = 0; i < inSize; i++) { + if (!HdfSbufWriteInt8(data, input[i])) { + HDF_LOGE("%{public}s: write input[%d] failed!", __func__, i); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + } + + ec = SampleProxyCall(self, CMD_LIST_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint32(reply, outSize)) { + HDF_LOGE("%{public}s: read output size failed!", __func__); + goto finished; + } + + if (*outSize == 0) { + HDF_LOGE("%{public}s: read outSize = %{public}d", __func__, *outSize); + goto finished; + } + + int8_t *result = (int8_t *)OsalMemAlloc(sizeof(int8_t) * (*outSize)); + if (result == NULL) { + HDF_LOGE("%{public}s: malloc output failed!", __func__); + goto finished; + } + + for (uint32_t i = 0; i < *outSize; i++) { + if (!HdfSbufReadInt8(reply, &(result[i]))) { + HDF_LOGE("%{public}s: read output[%{public}d] failed!", __func__, i); + OsalMemFree(result); + goto finished; + } + } + *output = result; + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyArrayTypeTest(struct ISample *self, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL || input == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint32(data, inSize)) { + HDF_LOGE("%{public}s: write input size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + for (uint32_t i = 0; i < inSize; i++) { + if (!HdfSbufWriteInt8(data, input[i])) { + HDF_LOGE("%{public}s: write input[%d] failed!", __func__, i); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + } + + ec = SampleProxyCall(self, CMD_ARRAY_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint32(reply, outSize)) { + HDF_LOGE("%{public}s: read output size failed!", __func__); + goto finished; + } + + if (*outSize == 0) { + HDF_LOGE("%{public}s: read outSize = %{public}d", __func__, *outSize); + goto finished; + } + + int8_t *result = (int8_t *)OsalMemAlloc(sizeof(int8_t) * (*outSize)); + if (result == NULL) { + HDF_LOGE("%{public}s: malloc output failed!", __func__); + goto finished; + } + + for (uint32_t i = 0; i < *outSize; i++) { + if (!HdfSbufReadInt8(reply, &(result[i]))) { + HDF_LOGE("%{public}s: read output[%{public}d] failed!", __func__, i); + OsalMemFree(result); + goto finished; + } + } + *output = result; + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyStructTypeTest(struct ISample *self, const struct StructSample *input, + struct StructSample *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteBuffer(data, (const void *)input, sizeof(struct StructSample))) { + HDF_LOGE("%{public}s: write struct StructSample input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_STRUCT_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + struct StructSample *result = NULL; + uint32_t outSize = 0; + if (!HdfSbufReadBuffer(reply, (const void **)&result, &outSize)) { + HDF_LOGE("%{public}s: read struct StructSample result failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + if (result == NULL || outSize != sizeof(struct StructSample)) { + HDF_LOGE("%{public}s: result is error", __func__); + ec = HDF_ERR_INVALID_PARAM; + } + output->first = result->first; + output->second = result->second; + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static int32_t SampleProxyEnumTypeTest(struct ISample *self, const enum EnumSample input, enum EnumSample *output) +{ + int32_t ec = HDF_FAILURE; + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + + if (data == NULL || reply == NULL) { + ec = HDF_ERR_MALLOC_FAIL; + goto finished; + } + + if (!HdfSbufWriteUint32(data, (uint32_t)input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = SampleProxyCall(self, CMD_ENUM_TYPE_TEST, data, reply); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufReadUint32(reply, (uint32_t *)output)) { + HDF_LOGE("%{public}s: read result failed!", __func__); + goto finished; + } + +finished: + if (data != NULL) { + HdfSBufRecycle(data); + } + if (reply != NULL) { + HdfSBufRecycle(reply); + } + return ec; +} + +static void SampleConstruct(struct ISample *inst) +{ + inst->BooleanTypeTest = SampleProxyBooleanTypeTest; + inst->ByteTypeTest = SampleProxyByteTypeTest; + inst->ShortTypeTest = SampleProxyShortTypeTest; + inst->IntTypeTest = SampleProxyIntTypeTest; + inst->LongTypeTest = SampleProxyLongTypeTest; + inst->FloatTypeTest = SampleProxyFloatTypeTest; + inst->DoubleTypeTest = SampleProxyDoubleTypeTest; + inst->StringTypeTest = SampleProxyStringTypeTest; + inst->UcharTypeTest = SampleProxyUcharTypeTest; + inst->UshortTypeTest = SampleProxyUshortTypeTest; + inst->UintTypeTest = SampleProxyUintTypeTest; + inst->UlongTypeTest = SampleProxyUlongTypeTest; + inst->ListTypeTest = SampleProxyListTypeTest; + inst->ArrayTypeTest = SampleProxyArrayTypeTest; + inst->StructTypeTest = SampleProxyStructTypeTest; + inst->EnumTypeTest = SampleProxyEnumTypeTest; +} + +struct ISample *HdiSampleGet(const char *serviceName) +{ + struct HDIServiceManager *serviceMgr = HDIServiceManagerGet(); + if (serviceMgr == NULL) { + HDF_LOGE("%{public}s: HDIServiceManager not found!", __func__); + return NULL; + } + + struct HdfRemoteService *remote = serviceMgr->GetService(serviceMgr, serviceName); + if (remote == NULL) { + HDF_LOGE("%{public}s: HdfRemoteService not found!", __func__); + return NULL; + } + + struct ISample *sampleClient = (struct ISample *)OsalMemAlloc(sizeof(struct ISample)); + if (sampleClient == NULL) { + HDF_LOGE("%{public}s: malloc sample instance failed!", __func__); + HdfRemoteServiceRecycle(remote); + return NULL; + } + + sampleClient->remote = remote; + SampleConstruct(sampleClient); + return sampleClient; +} + +void HdiSampleRelease(struct ISample *instance) +{ + if (instance == NULL) { + return; + } + + HdfRemoteServiceRecycle(instance->remote); + OsalMemFree(instance); +} + +#ifdef __cplusplus +} +#endif /* __cplusplus */ \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp new file mode 100755 index 0000000..7977183 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/sample_client_cpp_test.cpp @@ -0,0 +1,300 @@ +/* + * Copyright (c) 2021 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. + */ +#include +#include +#include +#include +#include +#include +#include +#include "securec.h" +#include "sample_proxy.h" + +using namespace OHOS; +using namespace testing::ext; +using namespace OHOS::HDI::Sample::V1_0; + +#define HDF_LOG_TAG sample_client_cpp_test + +constexpr const char *TEST_SERVICE_NAME = "sample_service_cpp"; + +class SampleObjCPPTest : public testing::Test { +public: + static void SetUpTestCase(){} + static void TearDownTestCase(){} + void SetUp(){} + void TearDown(){} +}; + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_001, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + bool input = true; + bool output = false; + + int32_t ec = sampleObj->BooleanTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(output, input); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_002, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int8_t input = 5; + int8_t output; + + int32_t ec = sampleObj->ByteTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_003, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int16_t input = 5; + int16_t output; + + int32_t ec = sampleObj->ShortTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_004, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int32_t input = 5; + int32_t output; + + int32_t ec = sampleObj->IntTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_005, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + int64_t input = 5; + int64_t output; + + int32_t ec = sampleObj->LongTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_006, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + float input = 5; + float output; + + int32_t ec = sampleObj->FloatTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_007, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + double input = 5; + double output; + + int32_t ec = sampleObj->DoubleTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_008, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + std::string input("cpp sample client test"); + std::string output; + + int32_t ec = sampleObj->StringTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + EXPECT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_009, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint8_t input = 5; + uint8_t output; + + int32_t ec = sampleObj->UcharTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_010, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint16_t input = 5; + uint16_t output; + + int32_t ec = sampleObj->UshortTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_011, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint32_t input = 5; + uint32_t output; + + int32_t ec = sampleObj->UintTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_012, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + uint64_t input = 5; + uint64_t output; + + int32_t ec = sampleObj->UlongTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input, output); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_013, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + std::list input{1, 2, 3, 4, 5}; + std::list output; + + int32_t ec = sampleObj->ListTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(output.size(), input.size()); + + auto inIter = input.begin(); + auto outIter = output.begin(); + while (inIter != input.end() && outIter != output.end()) { + ASSERT_EQ(*(inIter++), *(outIter++)); + } +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_014, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + std::map input; + for (int8_t i = 0; i < 10; i++) { + input[i] = i; + } + std::map output; + + int32_t ec = sampleObj->MapTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input.size(), output.size()); + + std::map::iterator inIter = input.begin(); + for (; inIter != input.begin(); inIter++) { + std::map::iterator outIter = output.find(inIter->first); + + ASSERT_TRUE(outIter != output.end()); + ASSERT_EQ(outIter->second, inIter->second); + } +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_015, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + std::vector input; + std::vector output; + + int32_t ec = sampleObj->ArrayTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(input.size(), output.size()); + + for (uint32_t i = 0; i < output.size(); i++) { + ASSERT_EQ(output[i], input[i]); + } +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_016, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + StructSample input = { + .first = 5, + .second = 10, + }; + + StructSample output; + int32_t ec = sampleObj->StructTypeTest(input, output); + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(output.first, input.first); + ASSERT_EQ(output.second, input.second); +} + +HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_017, TestSize.Level0) +{ + sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleObj != nullptr); + + EnumSample input = EnumSample::MEM_SECOND; + EnumSample output; + + int32_t ec = sampleObj->EnumTypeTest(input, output); + + ASSERT_EQ(ec, HDF_SUCCESS); + ASSERT_EQ(output, input); +} diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn new file mode 100755 index 0000000..579e47b --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("sample_client_cpp") { + deps = [ ":libsample_client_cpp" ] +} + +ohos_shared_library("libsample_client_cpp") { + + include_dirs = [ + "$hdf_framework_path/ability/sbuf/include", + "$hdf_uhdf_path/manager/include", + "$hdf_uhdf_path/include/hdi", + ] + + sources = [ + "sample_proxy.cpp", + ] + + deps = [ + "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/host:libhdf_host", + "//utils/native/base:utils", + "$hdf_uhdf_path/hdi:libhdi" + ] + + if (is_standard_system) { + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "communication_L2:ipc_core", + ] + } else { + external_deps = [ + "hilog:libhilog", + "ipc:ipc_core", + ] + } + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/isample.h b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/isample.h new file mode 100644 index 0000000..19a65c8 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/isample.h @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_CLIENT_CPP_INF_H +#define HDI_SAMPLE_CLIENT_CPP_INF_H + +#include +#include +#include +#include +#include + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +struct StructSample { + int8_t first; + int16_t second; +}; + +enum EnumSample { + MEM_FIRST, + MEM_SECOND, + MEM_THIRD, +}; + + +enum { + CMD_BOOLEAN_TYPE_TEST, + CMD_BYTE_TYPE_TEST, + CMD_SHORT_TYPE_TEST, + CMD_INT_TYPE_TEST, + CMD_LONG_TYPE_TEST, + CMD_FLOAT_TYPE_TEST, + CMD_DOUBLE_TYPE_TEST, + CMD_STRING_TYPE_TEST, + CMD_UCHAR_TYPE_TEST, + CMD_USHORT_TYPE_TEST, + CMD_UINT_TYPE_TEST, + CMD_ULONG_TYPE_TEST, + CMD_LIST_TYPE_TEST, + CMD_MAP_TYPE_TEST, + CMD_ARRAY_TYPE_TEST, + CMD_STRUCT_TYPE_TEST, + CMD_ENUM_TYPE_TEST, +}; + +class ISample : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"HDI.Sample.V1_0"); + + virtual ~ISample(){} + + static sptr Get(const std::string& serviceName); + + virtual int32_t BooleanTypeTest(const bool input, bool& output) = 0; + + virtual int32_t ByteTypeTest(const int8_t input, int8_t& output) = 0; + + virtual int32_t ShortTypeTest(const int16_t input, int16_t& output) = 0; + + virtual int32_t IntTypeTest(const int32_t input, int32_t& output) = 0; + + virtual int32_t LongTypeTest(const int64_t input, int64_t& output) = 0; + + virtual int32_t FloatTypeTest(const float input, float& output) = 0; + + virtual int32_t DoubleTypeTest(const double input, double& output) = 0; + + virtual int32_t StringTypeTest(const std::string& input, std::string& output) = 0; + + virtual int32_t UcharTypeTest(const uint8_t input, uint8_t& output) = 0; + + virtual int32_t UshortTypeTest(const uint16_t input, uint16_t& output) = 0; + + virtual int32_t UintTypeTest(const uint32_t input, uint32_t& output) = 0; + + virtual int32_t UlongTypeTest(const uint64_t input, uint64_t& output) = 0; + + virtual int32_t ListTypeTest(const std::list& input, std::list& output) = 0; + + virtual int32_t MapTypeTest(const std::map& input, std::map& output) = 0; + + virtual int32_t ArrayTypeTest(const std::vector& input, std::vector& output) = 0; + + virtual int32_t StructTypeTest(const StructSample& input, StructSample& output) = 0; + + virtual int32_t EnumTypeTest(const EnumSample& input, EnumSample& output) = 0; +}; + +sptr ISample::Get(const std::string& serviceName) +{ + do { + using namespace OHOS::HDI::ServiceManager::V1_0; + auto servMgr = IServiceManager::Get(); + if (servMgr == nullptr) { + break; + } + + sptr remote = servMgr->GetService(serviceName.c_str()); + if (remote != nullptr) { + return iface_cast(remote); + } + } while(false); + + HDF_LOGE("%s: get %s failed!", __func__, serviceName.c_str()); + return nullptr; +} +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_SAMPLE_CLIENT_CPP_INF_H \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp new file mode 100755 index 0000000..7fd2e3d --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.cpp @@ -0,0 +1,421 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "sample_proxy.h" +#include +#include + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +int32_t SampleProxy::BooleanTypeTest(const bool input, bool& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteBool(input)) { + HDF_LOGE("%{public}s: write bool input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_BOOLEAN_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadBool(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::ByteTypeTest(const int8_t input, int8_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInt8(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_BYTE_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadInt8(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::ShortTypeTest(const int16_t input, int16_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInt16(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_SHORT_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadInt16(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::IntTypeTest(const int32_t input, int32_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInt32(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_INT_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadInt32(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::LongTypeTest(const int64_t input, int64_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInt64(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_LONG_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadInt64(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::FloatTypeTest(const float input, float& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteFloat(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_FLOAT_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadFloat(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::DoubleTypeTest(const double input, double& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteDouble(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_DOUBLE_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadDouble(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::StringTypeTest(const std::string& input, std::string& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteString(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_STRING_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadString(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::UcharTypeTest(const uint8_t input, uint8_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint8(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_UCHAR_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadUint8(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::UshortTypeTest(const uint16_t input, uint16_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint16(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_UCHAR_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadUint16(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::UintTypeTest(const uint32_t input, uint32_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint32(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_UINT_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadUint32(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::UlongTypeTest(const uint64_t input, uint64_t& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint64(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_ULONG_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = reply.ReadUint64(); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::ListTypeTest(const std::list& input, std::list& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint32(input.size())) { + HDF_LOGE("%{public}s: write input size failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + for (auto it : input) { + if (!data.WriteUint8(it)) { + HDF_LOGE("%{public}s: write input data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + } + + int32_t ec = Remote()->SendRequest(CMD_LIST_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + + uint32_t outSize = reply.ReadUint32(); + for (uint32_t i = 0; i < outSize; i++) { + uint8_t curData = reply.ReadUint8(); + output.push_back(curData); + } + + return HDF_SUCCESS; +} + +int32_t SampleProxy::MapTypeTest(const std::map& input, std::map& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint32(input.size())) { + HDF_LOGE("%{public}s: write input size failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + for (auto it : input) { + if (!data.WriteUint8(it.first)) { + HDF_LOGE("%{public}s: write input data->first failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!data.WriteUint8(it.second)) { + HDF_LOGE("%{public}s: write input data->second failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + } + + int32_t ec = Remote()->SendRequest(CMD_MAP_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + + uint32_t outSize = reply.ReadUint32(); + for (uint32_t i = 0; i < outSize; i++) { + uint8_t key = reply.ReadUint8(); + uint8_t val = reply.ReadUint8(); + output[key] = val; + } + + return HDF_SUCCESS; +} + + +int32_t SampleProxy::ArrayTypeTest(const std::vector& input, std::vector& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInt8Vector(input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_ARRAY_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + reply.ReadInt8Vector(&output); + + return HDF_SUCCESS; +} + +int32_t SampleProxy::StructTypeTest(const StructSample& input, StructSample& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteBuffer((void *)&input, sizeof(StructSample))) { + HDF_LOGE("%{public}s: write input data failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_STRUCT_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + + StructSample *result = (StructSample *)data.ReadBuffer(sizeof(StructSample)); + if (result == nullptr) { + HDF_LOGE("%{public}s: read output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + output.first = result->first; + output.second = result->second; + return HDF_SUCCESS; +} + +int32_t SampleProxy::EnumTypeTest(const EnumSample& input, EnumSample& output) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteUint32((uint32_t)input)) { + HDF_LOGE("%{public}s: write input failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = Remote()->SendRequest(CMD_ENUM_TYPE_TEST, data, reply, option); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ec); + return ec; + } + output = (EnumSample)reply.ReadUint32(); + + return HDF_SUCCESS; +} +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.h b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.h new file mode 100644 index 0000000..4ddc29f --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/sample_proxy.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_CLIENT_PROXY_CPP_INF_H +#define HDI_SAMPLE_CLIENT_PROXY_CPP_INF_H + +#include +#include "isample.h" + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +class SampleProxy : public IRemoteProxy { +public: + explicit SampleProxy(const sptr& impl) : IRemoteProxy(impl) {} + virtual ~SampleProxy() override {} + + virtual int32_t BooleanTypeTest(const bool input, bool& output) override; + + virtual int32_t ByteTypeTest(const int8_t input, int8_t& output) override; + + virtual int32_t ShortTypeTest(const int16_t input, int16_t& output) override; + + virtual int32_t IntTypeTest(const int32_t input, int32_t& output) override; + + virtual int32_t LongTypeTest(const int64_t input, int64_t& output) override; + + virtual int32_t FloatTypeTest(const float input, float& output) override; + + virtual int32_t DoubleTypeTest(const double input, double& output) override; + + virtual int32_t StringTypeTest(const std::string& input, std::string& output) override; + + virtual int32_t UcharTypeTest(const uint8_t input, uint8_t& output) override; + + virtual int32_t UshortTypeTest(const uint16_t input, uint16_t& output) override; + + virtual int32_t UintTypeTest(const uint32_t input, uint32_t& output) override; + + virtual int32_t UlongTypeTest(const uint64_t input, uint64_t& output) override; + + virtual int32_t ListTypeTest(const std::list& input, std::list& output) override; + + virtual int32_t MapTypeTest(const std::map& input, std::map& output) override; + + virtual int32_t ArrayTypeTest(const std::vector& input, std::vector& output) override; + + virtual int32_t StructTypeTest(const StructSample& input, StructSample& output) override; + + virtual int32_t EnumTypeTest(const EnumSample& input, EnumSample& output) override; + +private: + static inline BrokerDelegator delegator_; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_SAMPLE_CLIENT_PROXY_CPP_INF_H \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn new file mode 100755 index 0000000..2f87135 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("sample_service_c") { + deps = [ ":libsample_service_c" ] +} + +ohos_shared_library("libsample_service_c") { + sources = [ + "sample_driver.c", + "sample_service_stub.c", + "sample_service.c", + ] + + deps = [ + "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/host:libhdf_host", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/isample.h b/uhdf2/hdi/test/hdi_sample/sample_service_c/isample.h new file mode 100644 index 0000000..5842145 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_c/isample.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_C_INF_H +#define HDI_SAMPLE_C_INF_H + +struct HdfSBuf; +struct HdfDeviceObject; +struct HdfDeviceIoClient; + +struct StructSample { + int8_t first; + int16_t second; +}; + +enum EnumSample { + MEM_FIRST, + MEM_SECOND, + MEM_THIRD, +}; + +enum { + CMD_BOOLEAN_TYPE_TEST, + CMD_BYTE_TYPE_TEST, + CMD_SHORT_TYPE_TEST, + CMD_INT_TYPE_TEST, + CMD_LONG_TYPE_TEST, + CMD_FLOAT_TYPE_TEST, + CMD_DOUBLE_TYPE_TEST, + CMD_STRING_TYPE_TEST, + CMD_UCHAR_TYPE_TEST, + CMD_USHORT_TYPE_TEST, + CMD_UINT_TYPE_TEST, + CMD_ULONG_TYPE_TEST, + CMD_LIST_TYPE_TEST, + CMD_ARRAY_TYPE_TEST, + CMD_STRUCT_TYPE_TEST, + CMD_ENUM_TYPE_TEST, +}; + +struct ISample { + int32_t (*BooleanTypeTest)(struct HdfDeviceObject *device, const bool input, bool *output); + + int32_t (*ByteTypeTest)(struct HdfDeviceObject *device, const int8_t input, int8_t *output); + + int32_t (*ShortTypeTest)(struct HdfDeviceObject *device, const int16_t input, int16_t *output); + + int32_t (*IntTypeTest)(struct HdfDeviceObject *device, const int32_t input, int32_t *output); + + int32_t (*LongTypeTest)(struct HdfDeviceObject *device, const int64_t input, int64_t *output); + + int32_t (*FloatTypeTest)(struct HdfDeviceObject *device, const float input, float *output); + + int32_t (*DoubleTypeTest)(struct HdfDeviceObject *device, const double input, double *output); + + int32_t (*StringTypeTest)(struct HdfDeviceObject *device, const char *input, char **output); + + int32_t (*UcharTypeTest)(struct HdfDeviceObject *device, const uint8_t input, uint8_t *output); + + int32_t (*UshortTypeTest)(struct HdfDeviceObject *device, const uint16_t input, uint16_t *output); + + int32_t (*UintTypeTest)(struct HdfDeviceObject *device, const uint32_t input, uint32_t *output); + + int32_t (*UlongTypeTest)(struct HdfDeviceObject *device, const uint64_t input, uint64_t *output); + + int32_t (*ListTypeTest)(struct HdfDeviceObject *device, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize); + + int32_t (*ArrayTypeTest)(struct HdfDeviceObject *device, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize); + + int32_t (*StructTypeTest)(struct HdfDeviceObject *device, const struct StructSample *input, + struct StructSample *output); + + int32_t (*EnumTypeTest)(struct HdfDeviceObject *device, const enum EnumSample input, enum EnumSample *output); +}; + +const struct ISample *HdiSampleImplInstance(); + +int32_t SampleServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply); + +#endif // HDI_SAMPLE_C_INF_H \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c new file mode 100644 index 0000000..3b64cf1 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include + +#include "isample.h" + +#define HDF_LOG_TAG sample_service_c + +static int32_t SampleServiceDispatch(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + return SampleServiceOnRemoteRequest(client, cmdId, data, reply); +} + +void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) +{ + struct IDeviceIoService *testService = deviceObject->service; + OsalMemFree(testService); +} + +int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGE("HdfSampleDriverBind enter!"); + + struct IDeviceIoService *ioService = (struct IDeviceIoService *)OsalMemAlloc(sizeof(struct IDeviceIoService)); + if (ioService == NULL) { + HDF_LOGE("HdfSampleDriverBind OsalMemAlloc IDeviceIoService failed!"); + return HDF_FAILURE; + } + + ioService->Dispatch = SampleServiceDispatch; + ioService->Open = NULL; + ioService->Release = NULL; + + deviceObject->service = ioService; + return HDF_SUCCESS; +} + +int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGE("HdfSampleDriverCInit enter, new hdi impl"); + return HDF_SUCCESS; +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .moduleName = "sample_service_c", + .Bind = HdfSampleDriverBind, + .Init = HdfSampleDriverInit, + .Release = HdfSampleDriverRelease, +}; + +HDF_INIT(g_sampleDriverEntry); \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service.c b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service.c new file mode 100644 index 0000000..332585b --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service.c @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include + +#include "isample.h" + +static int32_t HdiBooleanTypeTestImpl(struct HdfDeviceObject *device, const bool input, bool *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiByteTypeTestImpl(struct HdfDeviceObject *device, const int8_t input, int8_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiShortTypeTestImpl(struct HdfDeviceObject *device, const int16_t input, int16_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiIntTypeTestImpl(struct HdfDeviceObject *device, const int32_t input, int32_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiLongTypeTestImpl(struct HdfDeviceObject *device, const int64_t input, int64_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiFloatTypeTestImpl(struct HdfDeviceObject *device, const float input, float *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiDoubleTypeTestImpl(struct HdfDeviceObject *device, const double input, double *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiStringTypeTestImpl(struct HdfDeviceObject *device, const char *input, char **output) +{ + *output = strdup(input); + return HDF_SUCCESS; +} + +static int32_t HdiUcharTypeTestImpl(struct HdfDeviceObject *device, const uint8_t input, uint8_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiUshortTypeTestImpl(struct HdfDeviceObject *device, const uint16_t input, uint16_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiUintTypeTestImpl(struct HdfDeviceObject *device, const uint32_t input, uint32_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiUlongTypeTestImpl(struct HdfDeviceObject *device, const uint64_t input, uint64_t *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static int32_t HdiListTypeTestImpl(struct HdfDeviceObject *device, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize) +{ + int8_t *result = (int8_t*)OsalMemAlloc(sizeof(int8_t) * inSize); + if (result == NULL) { + HDF_LOGE("%{public}s: result OsalMemAlloc failed", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + for (uint32_t i = 0; i < inSize; i++) { + result[i] = input[i]; + } + + *output = result; + *outSize = inSize; + return HDF_SUCCESS; +} + +static int32_t HdiArrayTypeTestImpl(struct HdfDeviceObject *device, const int8_t *input, const uint32_t inSize, + int8_t **output, uint32_t *outSize) +{ + int8_t *result = (int8_t*)OsalMemAlloc(sizeof(int8_t) * inSize); + if (result == NULL) { + HDF_LOGE("%{public}s: result OsalMemAlloc failed", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + for (uint32_t i = 0; i < inSize; i++) { + result[i] = input[i]; + } + + *output = result; + *outSize = inSize; + return HDF_SUCCESS; +} + +static int32_t HdiStructTypeTestImpl(struct HdfDeviceObject *device, const struct StructSample *input, + struct StructSample *output) +{ + output->first = input->first; + output->second = input->second; + return HDF_SUCCESS; +} + +static int32_t HdiEnumTypeTestImpl(struct HdfDeviceObject *device, const enum EnumSample input, enum EnumSample *output) +{ + *output = input; + return HDF_SUCCESS; +} + +static const struct ISample g_hdiSampleImpl = { + .BooleanTypeTest = HdiBooleanTypeTestImpl, + .ByteTypeTest = HdiByteTypeTestImpl, + .ShortTypeTest = HdiShortTypeTestImpl, + .IntTypeTest = HdiIntTypeTestImpl, + .LongTypeTest = HdiLongTypeTestImpl, + .FloatTypeTest = HdiFloatTypeTestImpl, + .DoubleTypeTest = HdiDoubleTypeTestImpl, + .StringTypeTest = HdiStringTypeTestImpl, + .UcharTypeTest = HdiUcharTypeTestImpl, + .UshortTypeTest = HdiUshortTypeTestImpl, + .UintTypeTest = HdiUintTypeTestImpl, + .UlongTypeTest = HdiUlongTypeTestImpl, + .ListTypeTest = HdiListTypeTestImpl, + .ArrayTypeTest = HdiArrayTypeTestImpl, + .StructTypeTest = HdiStructTypeTestImpl, + .EnumTypeTest = HdiEnumTypeTestImpl, +}; + +const struct ISample *HdiSampleImplInstance() +{ + return &g_hdiSampleImpl; +} diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service_stub.c b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service_stub.c new file mode 100644 index 0000000..c8c1ebf --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service_stub.c @@ -0,0 +1,489 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include + +#include "isample.h" + + +static int32_t SerStubBooleanTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + bool input = false; + bool output = false; + if (!HdfSbufReadUint32(data, (uint32_t *)&input)) { + HDF_LOGE("%{public}s: read bool data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->BooleanTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call BooleanTypeTest fuc failed!", __func__); + return ec; + } + + if (!HdfSbufWriteUint32(reply, (uint32_t)&output)) { + HDF_LOGE("%{public}s: write bool output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return ec; +} + +static int32_t SerStubByteTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int8_t input; + int8_t output; + if (!HdfSbufReadInt8(data, &input)) { + HDF_LOGE("%{public}s: read int8_t data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->ByteTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call ByteTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteInt8(reply, output)) { + HDF_LOGE("%{public}s: write int8_t output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubShortTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int16_t input; + int16_t output; + if (!HdfSbufReadInt16(data, &input)) { + HDF_LOGE("%{public}s: read int16_t data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->ShortTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call ShortTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteInt16(reply, output)) { + HDF_LOGE("%{public}s: write int16_t output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubIntTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t input; + int32_t output; + if (!HdfSbufReadInt32(data, &input)) { + HDF_LOGE("%{public}s: read int32_t data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->IntTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call IntTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteInt32(reply, output)) { + HDF_LOGE("%{public}s: write int32_t output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubLongTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int64_t input; + int64_t output; + if (!HdfSbufReadInt64(data, &input)) { + HDF_LOGE("%{public}s: read int64_t data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->LongTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call LongTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteInt64(reply, output)) { + HDF_LOGE("%{public}s: write int64_t output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return ec; +} + +static int32_t SerStubFloatTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + float input; + float output; + if (!HdfSbufReadFloat(data, &input)) { + HDF_LOGE("%{public}s: read float data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->FloatTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call FloatTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteFloat(reply, output)) { + HDF_LOGE("%{public}s: write float output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubDoubleTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + double input; + double output; + if (!HdfSbufReadDouble(data, &input)) { + HDF_LOGE("%{public}s: read double data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->DoubleTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call DoubleTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteDouble(reply, output)) { + HDF_LOGE("%{public}s: write double output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubStringTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + const char *input = HdfSbufReadString(data); + if (input == NULL) { + HDF_LOGE("%{public}s: read string data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + char *output = NULL; + + int32_t ec = HdiSampleImplInstance()->StringTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call StringTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteString(reply, output)) { + HDF_LOGE("%{public}s: write string output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubUcharTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint8_t input; + uint8_t output; + if (!HdfSbufReadUint8(data, &input)) { + HDF_LOGE("%{public}s: read data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->UcharTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call UcharTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteUint8(reply, output)) { + HDF_LOGE("%{public}s: write output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubUshortTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint16_t input; + uint16_t output; + if (!HdfSbufReadUint16(data, &input)) { + HDF_LOGE("%{public}s: read data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->UshortTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call UshortTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteUint16(reply, output)) { + HDF_LOGE("%{public}s: write output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubUintTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint32_t input; + uint32_t output; + if (!HdfSbufReadUint32(data, &input)) { + HDF_LOGE("%{public}s: read data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->UintTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call UintTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteUint32(reply, output)) { + HDF_LOGE("%{public}s: write output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubUlongTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint64_t input; + uint64_t output; + if (!HdfSbufReadUint64(data, &input)) { + HDF_LOGE("%{public}s: read data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->UlongTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call UlongTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteUint64(reply, output)) { + HDF_LOGE("%{public}s: write output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +static int32_t SerStubListTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ec = HDF_FAILURE; + uint32_t inSize = 0; + int8_t *input = NULL; + uint32_t outSize = 0; + int8_t *output = NULL; + + if (!HdfSbufReadUint32(data, &inSize)) { + HDF_LOGE("%{public}s: read data size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + return ec; + } + + input = (int8_t *)OsalMemAlloc(sizeof(int8_t) * inSize); + for (uint32_t i = 0; i < inSize; i++) { + if (!HdfSbufReadInt8(data, (input + i))) { + HDF_LOGE("%{public}s: read data size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + } + + ec = HdiSampleImplInstance()->ArrayTypeTest(client->device, input, inSize, &output, &outSize); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed!", __func__); + goto finished; + } + + if (!HdfSbufWriteUint32(reply, outSize)) { + HDF_LOGE("%{public}s: write output size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + for (uint8_t i = 0; i < outSize; i++) { + if (!HdfSbufWriteInt8(reply, output[i])) { + HDF_LOGE("%{public}s: write output failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + } + +finished: + if (input != NULL) { + (void)OsalMemFree(input); + } + if (output != NULL) { + (void)OsalMemFree(output); + } + return ec; +} + +static int32_t SerStubArrayTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ec = HDF_FAILURE; + uint32_t inSize = 0; + int8_t *input = NULL; + uint32_t outSize = 0; + int8_t *output = NULL; + + if (!HdfSbufReadUint32(data, &inSize)) { + HDF_LOGE("%{public}s: read data size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + return ec; + } + + input = (int8_t *)OsalMemAlloc(sizeof(int8_t) * inSize); + for (uint32_t i = 0; i < inSize; i++) { + if (!HdfSbufReadInt8(data, (input + i))) { + HDF_LOGE("%{public}s: read data size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + } + + ec = HdiSampleImplInstance()->ArrayTypeTest(client->device, input, inSize, &output, &outSize); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed!", __func__); + goto finished; + } + + if (!HdfSbufWriteUint32(reply, outSize)) { + HDF_LOGE("%{public}s: write output size failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + for (uint8_t i = 0; i < outSize; i++) { + if (!HdfSbufWriteInt8(reply, output[i])) { + HDF_LOGE("%{public}s: write output failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + } + +finished: + if (input != NULL) { + (void)OsalMemFree(input); + } + if (output != NULL) { + (void)OsalMemFree(output); + } + return ec; +} + +static int32_t SerStubStructTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ec = HDF_FAILURE; + uint32_t dataSize = 0; + struct StructSample *input = NULL; + struct StructSample output; + + if (!HdfSbufReadBuffer(data, (const void **)&input, &dataSize)) { + HDF_LOGE("%{public}s: read struct data failed!", __func__); + ec = HDF_ERR_INVALID_PARAM; + goto finished; + } + + ec = HdiSampleImplInstance()->StructTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call StructTypeTest failed! error code is %{public}d", __func__, ec); + goto finished; + } + + if (!HdfSbufWriteBuffer(reply, (const void *)&output, sizeof(output))) { + HDF_LOGE("%{public}s: struct result write failed", __func__); + goto finished; + } +finished: + return ec; +} + +static int32_t SerStubEnumTypeTest(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + enum EnumSample input; + enum EnumSample output; + + if (!HdfSbufReadUint32(data, (uint32_t *)&input)) { + HDF_LOGE("%{public}s: read EnumSample data failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ec = HdiSampleImplInstance()->EnumTypeTest(client->device, input, &output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call EnumTypeTest failed!", __func__); + return ec; + } + + if (!HdfSbufWriteUint32(reply, (uint32_t)output)) { + HDF_LOGE("%{public}s: write EnumSample output failed!", __func__); + return HDF_ERR_INVALID_PARAM; + } + return ec; +} + +int32_t SampleServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + switch (cmdId) { + case CMD_BOOLEAN_TYPE_TEST: + return SerStubBooleanTypeTest(client, data, reply); + case CMD_BYTE_TYPE_TEST: + return SerStubByteTypeTest(client, data, reply); + case CMD_SHORT_TYPE_TEST: + return SerStubShortTypeTest(client, data, reply); + case CMD_INT_TYPE_TEST: + return SerStubIntTypeTest(client, data, reply); + case CMD_LONG_TYPE_TEST: + return SerStubLongTypeTest(client, data, reply); + case CMD_FLOAT_TYPE_TEST: + return SerStubFloatTypeTest(client, data, reply); + case CMD_DOUBLE_TYPE_TEST: + return SerStubDoubleTypeTest(client, data, reply); + case CMD_STRING_TYPE_TEST: + return SerStubStringTypeTest(client, data, reply); + case CMD_UCHAR_TYPE_TEST: + return SerStubUcharTypeTest(client, data, reply); + case CMD_USHORT_TYPE_TEST: + return SerStubUshortTypeTest(client, data, reply); + case CMD_UINT_TYPE_TEST: + return SerStubUintTypeTest(client, data, reply); + case CMD_ULONG_TYPE_TEST: + return SerStubUlongTypeTest(client, data, reply); + case CMD_LIST_TYPE_TEST: + return SerStubListTypeTest(client, data, reply); + case CMD_ARRAY_TYPE_TEST: + return SerStubArrayTypeTest(client, data, reply); + case CMD_STRUCT_TYPE_TEST: + return SerStubStructTypeTest(client, data, reply); + case CMD_ENUM_TYPE_TEST: + return SerStubEnumTypeTest(client, data, reply); + default: { + HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId); + return HDF_ERR_INVALID_PARAM; + } + } + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn new file mode 100755 index 0000000..0490269 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("sample_service_cpp") { + deps = [ ":libsample_service_cpp" ] +} + +ohos_shared_library("libsample_service_cpp") { + sources = [ + "sample_driver.cpp", + "sample_service_stub.cpp", + "sample_service.cpp" + ] + + deps = [ + "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/host:libhdf_host", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "communication_L2:ipc_core" + ] + } else { + external_deps = [ + "ipc:ipc_core", + "hilog:libhilog", + ] + } + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/isample.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/isample.h new file mode 100644 index 0000000..c53b838 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/isample.h @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_CPP_INF_H +#define HDI_SAMPLE_CPP_INF_H + +#include +#include +#include +#include + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +struct StructSample { + int8_t first; + int16_t second; +}; + +enum EnumSample { + MEM_FIRST, + MEM_SECOND, + MEM_THIRD, +}; + +enum { + CMD_BOOLEAN_TYPE_TEST, + CMD_BYTE_TYPE_TEST, + CMD_SHORT_TYPE_TEST, + CMD_INT_TYPE_TEST, + CMD_LONG_TYPE_TEST, + CMD_FLOAT_TYPE_TEST, + CMD_DOUBLE_TYPE_TEST, + CMD_STRING_TYPE_TEST, + CMD_UCHAR_TYPE_TEST, + CMD_USHORT_TYPE_TEST, + CMD_UINT_TYPE_TEST, + CMD_ULONG_TYPE_TEST, + CMD_LIST_TYPE_TEST, + CMD_MAP_TYPE_TEST, + CMD_ARRAY_TYPE_TEST, + CMD_STRUCT_TYPE_TEST, + CMD_ENUM_TYPE_TEST, +}; + +class ISample { +public: + virtual ~ISample(){} + + virtual int32_t BooleanTypeTest(const bool input, bool& output) const = 0; + + virtual int32_t ByteTypeTest(const int8_t input, int8_t& output) const = 0; + + virtual int32_t ShortTypeTest(const int16_t input, int16_t& output) const = 0; + + virtual int32_t IntTypeTest(const int32_t input, int32_t& output) const = 0; + + virtual int32_t LongTypeTest(const int64_t input, int64_t& output) const = 0; + + virtual int32_t FloatTypeTest(const float input, float& output) const = 0; + + virtual int32_t DoubleTypeTest(const double input, double& output) const = 0; + + virtual int32_t StringTypeTest(const std::string& input, std::string& output) const = 0; + + virtual int32_t UcharTypeTest(const uint8_t input, uint8_t& output) const = 0; + + virtual int32_t UshortTypeTest(const uint16_t input, uint16_t& output) const = 0; + + virtual int32_t UintTypeTest(const uint32_t input, uint32_t& output) const = 0; + + virtual int32_t UlongTypeTest(const uint64_t input, uint64_t& output) const = 0; + + virtual int32_t ListTypeTest(const std::list& input, std::list& output) const = 0; + + virtual int32_t MapTypeTest(const std::map& input, std::map& output) const = 0; + + virtual int32_t ArrayTypeTest(const std::vector& input, std::vector& output) const = 0; + + virtual int32_t StructTypeTest(const StructSample& input, StructSample& output) const = 0; + + virtual int32_t EnumTypeTest(const EnumSample& input, EnumSample& output) const = 0; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_SAMPLE_CPP_INF_H \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_driver.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_driver.cpp new file mode 100755 index 0000000..f12acad --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_driver.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2021 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. + */ +#include +#include +#include +#include +#include "sample_service_stub.h" + +#define HDF_LOG_TAG sample_service_cpp + +using namespace OHOS::HDI::Sample::V1_0; + +struct HdfSampleService { + struct IDeviceIoService ioservice; + void *instance; +}; + +static int32_t SampleServiceDispatch(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct HdfSampleService *hdfSampleService = CONTAINER_OF( + client->device->service, struct HdfSampleService, ioservice); + return SampleServiceOnRemoteRequest(hdfSampleService->instance, cmdId, *data, *reply); +} + +int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGE("HdfSampleDriverInit enter, new hdi impl"); + return HDF_SUCCESS; +} + +int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGI("HdfSampleDriverBind enter!"); + + struct HdfSampleService *hdfSampleService = (struct HdfSampleService *)OsalMemAlloc( + sizeof(struct HdfSampleService)); + if (hdfSampleService == nullptr) { + HDF_LOGE("HdfSampleDriverBind OsalMemAlloc HdfSampleService failed!"); + return HDF_FAILURE; + } + + hdfSampleService->ioservice.Dispatch = SampleServiceDispatch; + hdfSampleService->ioservice.Open = NULL; + hdfSampleService->ioservice.Release = NULL; + hdfSampleService->instance = SampleStubInstance(); + + deviceObject->service = &hdfSampleService->ioservice; + return HDF_SUCCESS; +} + +void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) +{ + struct HdfSampleService *hdfSampleService = CONTAINER_OF(deviceObject->service, struct HdfSampleService, ioservice); + SampleStubRelease(hdfSampleService->instance); + OsalMemFree(hdfSampleService); +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .moduleName = "sample_service_cpp", + .Bind = HdfSampleDriverBind, + .Init = HdfSampleDriverInit, + .Release = HdfSampleDriverRelease, +}; + +#ifndef __cplusplus +extern "C" { +#endif + +HDF_INIT(g_sampleDriverEntry); + +#ifndef __cplusplus +} +#endif \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.cpp new file mode 100755 index 0000000..c947695 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "sample_service.h" +#include + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +int32_t SampleService::BooleanTypeTest(const bool input, bool& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::ByteTypeTest(const int8_t input, int8_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::ShortTypeTest(const int16_t input, int16_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::IntTypeTest(const int32_t input, int32_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::LongTypeTest(const int64_t input, int64_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::FloatTypeTest(const float input, float& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::DoubleTypeTest(const double input, double& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::StringTypeTest(const std::string& input, std::string& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::UcharTypeTest(const uint8_t input, uint8_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::UshortTypeTest(const uint16_t input, uint16_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::UintTypeTest(const uint32_t input, uint32_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::UlongTypeTest(const uint64_t input, uint64_t& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::ListTypeTest(const std::list& input, std::list& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::MapTypeTest(const std::map& input, std::map& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::ArrayTypeTest(const std::vector& input, std::vector& output) const +{ + output = input; + return 0; +} + +int32_t SampleService::StructTypeTest(const StructSample& input, StructSample& output) const +{ + output.first = input.first; + output.second = input.second; + return 0; +} + +int32_t SampleService::EnumTypeTest(const EnumSample& input, EnumSample& output) const +{ + output = input; + return 0; +} +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.h new file mode 100644 index 0000000..6accfc2 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_SERVICE_CPP_INF_H +#define HDI_SAMPLE_SERVICE_CPP_INF_H + +#include "isample.h" + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +class SampleService : public ISample { +public: + SampleService(){} + virtual ~SampleService() override {} + + virtual int32_t BooleanTypeTest(const bool input, bool& output) const override; + + virtual int32_t ByteTypeTest(const int8_t input, int8_t& output) const override; + + virtual int32_t ShortTypeTest(const int16_t input, int16_t& output) const override; + + virtual int32_t IntTypeTest(const int32_t input, int32_t& output) const override; + + virtual int32_t LongTypeTest(const int64_t input, int64_t& output) const override; + + virtual int32_t FloatTypeTest(const float input, float& output) const override; + + virtual int32_t DoubleTypeTest(const double input, double& output) const override; + + virtual int32_t StringTypeTest(const std::string& input, std::string& output) const override; + + virtual int32_t UcharTypeTest(const uint8_t input, uint8_t& output) const override; + + virtual int32_t UshortTypeTest(const uint16_t input, uint16_t& output) const override; + + virtual int32_t UintTypeTest(const uint32_t input, uint32_t& output) const override; + + virtual int32_t UlongTypeTest(const uint64_t input, uint64_t& output) const override; + + virtual int32_t ListTypeTest(const std::list& input, std::list& output) const override; + + virtual int32_t MapTypeTest(const std::map& input, std::map& output) const override; + + virtual int32_t ArrayTypeTest(const std::vector& input, std::vector& output) const override; + + virtual int32_t StructTypeTest(const StructSample& input, StructSample& output) const override; + + virtual int32_t EnumTypeTest(const EnumSample& input, EnumSample& output) const override; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_SAMPLE_SERVICE_CPP_INF_H diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp new file mode 100755 index 0000000..3a502fe --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.cpp @@ -0,0 +1,484 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "sample_service_stub.h" +#include +#include +#include + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +int32_t SampleServiceStub::SampleStubBooleanTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const bool input = data.ReadBool(); + bool output = false; + + int32_t ec = service.BooleanTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteBool(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubByteTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const int8_t input = data.ReadInt8(); + int8_t output; + + int32_t ec = service.ByteTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteInt8(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubShortTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const int16_t input = data.ReadInt16(); + int16_t output; + + int32_t ec = service.ShortTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteInt16(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubIntTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const int32_t input = data.ReadInt32(); + int32_t output; + + int32_t ec = service.IntTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteInt32(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubLongTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const int64_t input = data.ReadInt64(); + int64_t output; + + int32_t ec = service.LongTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteInt64(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubFloatTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const float input = data.ReadFloat(); + float output; + + int32_t ec = service.FloatTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteFloat(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubDoubleTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const double input = data.ReadDouble(); + double output; + + int32_t ec = service.DoubleTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteDouble(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubStringTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const std::string input = data.ReadString(); + std::string output; + + int32_t ec = service.StringTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteString(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubUcharTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint8_t input = data.ReadUint8(); + uint8_t output; + + int32_t ec = service.UcharTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint8(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubUshortTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint16_t input = data.ReadUint16(); + uint16_t output; + + int32_t ec = service.UshortTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint16(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubUintTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint32_t input = data.ReadUint32(); + uint32_t output; + + int32_t ec = service.UintTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint32(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubUlongTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint64_t input = data.ReadUint64(); + uint64_t output; + + int32_t ec = service.UlongTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint64(output)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubListTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint32_t inSize = data.ReadUint32(); + std::list input; + std::list output; + + for (uint32_t i = 0; i < inSize; i++) { + input.push_back(data.ReadInt8()); + } + + int32_t ec = service.ListTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint32(output.size())) { + HDF_LOGE("%s: write result size failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + for (auto it : output) { + if (!reply.WriteInt8(it)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubMapTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint32_t inSize = data.ReadUint32(); + std::map input; + std::map output; + + for (uint32_t i = 0; i < inSize; i++) { + int8_t key = data.ReadInt8(); + int8_t val = data.ReadInt8(); + input[key] = val; + } + + int32_t ec = service.MapTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint32(output.size())) { + HDF_LOGE("%s: write result size failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + for (auto it : output) { + if (!reply.WriteInt8(it.first) || !reply.WriteInt8(it.second)) { + HDF_LOGE("%s: write result size failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubArrayTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + const uint32_t inSize = data.ReadUint32(); + std::vector input; + std::vector output; + + for (uint32_t i = 0; i < inSize; i++) { + input.push_back(data.ReadInt8()); + } + + int32_t ec = service.ArrayTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%s: call failed, error code is %d", __func__, ec); + return ec; + } + + if (!reply.WriteUint32(output.size())) { + HDF_LOGE("%s: write result size failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + for (auto it : output) { + if (!reply.WriteInt8(it)) { + HDF_LOGE("%s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubStructTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + StructSample *input = (StructSample *)data.ReadBuffer(sizeof(StructSample)); + if (input == nullptr) { + HDF_LOGE("%{public}s: read struct data failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + StructSample output; + + int32_t ec = service.StructTypeTest(*input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec); + return ec; + } + + if (!reply.WriteBuffer((void *)&output, sizeof(StructSample))) { + HDF_LOGE("%{public}s: write output data failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleStubEnumTypeTest(MessageParcel& data, + MessageParcel& reply, MessageOption& option) const +{ + EnumSample input = (EnumSample)data.ReadUint32(); + EnumSample output; + + int32_t ec = service.EnumTypeTest(input, output); + if (ec != HDF_SUCCESS) { + HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec); + return ec; + } + + if (!reply.WriteUint32((uint32_t)output)) { + HDF_LOGE("%{public}s: write result failed", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceStub::SampleServiceStubOnRemoteRequest(int cmdId, + MessageParcel& data, MessageParcel& reply, MessageOption& option) const +{ + switch (cmdId) { + case CMD_BOOLEAN_TYPE_TEST: + return SampleStubBooleanTypeTest(data, reply, option); + case CMD_BYTE_TYPE_TEST: + return SampleStubByteTypeTest(data, reply, option); + case CMD_SHORT_TYPE_TEST: + return SampleStubShortTypeTest(data, reply, option); + case CMD_INT_TYPE_TEST: + return SampleStubIntTypeTest(data, reply, option); + case CMD_LONG_TYPE_TEST: + return SampleStubLongTypeTest(data, reply, option); + case CMD_FLOAT_TYPE_TEST: + return SampleStubFloatTypeTest(data, reply, option); + case CMD_DOUBLE_TYPE_TEST: + return SampleStubDoubleTypeTest(data, reply, option); + case CMD_STRING_TYPE_TEST: + return SampleStubStringTypeTest(data, reply, option); + case CMD_UCHAR_TYPE_TEST: + return SampleStubUcharTypeTest(data, reply, option); + case CMD_USHORT_TYPE_TEST: + return SampleStubUshortTypeTest(data, reply, option); + case CMD_UINT_TYPE_TEST: + return SampleStubUintTypeTest(data, reply, option); + case CMD_ULONG_TYPE_TEST: + return SampleStubUlongTypeTest(data, reply, option); + case CMD_LIST_TYPE_TEST: + return SampleStubListTypeTest(data, reply, option); + case CMD_MAP_TYPE_TEST: + return SampleStubMapTypeTest(data, reply, option); + case CMD_ARRAY_TYPE_TEST: + return SampleStubArrayTypeTest(data, reply, option); + case CMD_STRUCT_TYPE_TEST: + return SampleStubStructTypeTest(data, reply, option); + case CMD_ENUM_TYPE_TEST: + return SampleStubEnumTypeTest(data, reply, option); + default: { + HDF_LOGE("%s: not support cmd %d", __func__, cmdId); + return HDF_ERR_INVALID_PARAM; + } + } + return HDF_SUCCESS; +} +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +void *SampleStubInstance() +{ + using namespace OHOS::HDI::Sample::V1_0; + return reinterpret_cast(new SampleServiceStub()); +} + +void SampleStubRelease(void *obj) +{ + using namespace OHOS::HDI::Sample::V1_0; + delete reinterpret_cast(obj); +} + +int32_t SampleServiceOnRemoteRequest(void *stub, int cmdId, struct HdfSBuf& data, struct HdfSBuf& reply) +{ + using namespace OHOS::HDI::Sample::V1_0; + SampleServiceStub *sampleStub = reinterpret_cast(stub); + OHOS::MessageParcel *dataParcel = nullptr; + OHOS::MessageParcel *replyParcel = nullptr; + + (void)SbufToParcel(&reply, &replyParcel); + if (SbufToParcel(&data, &dataParcel) != HDF_SUCCESS) { + HDF_LOGE("%s:invalid data sbuf object to dispatch", __func__); + return HDF_ERR_INVALID_PARAM; + } + + OHOS::MessageOption option; + return sampleStub->SampleServiceStubOnRemoteRequest(cmdId, *dataParcel, *replyParcel, option); +} diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.h new file mode 100644 index 0000000..8d76a63 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service_stub.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 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 HDI_SAMPLE_SERVICE_STUB_CPP_INF_H +#define HDI_SAMPLE_SERVICE_STUB_CPP_INF_H + +#include +#include +#include +#include "sample_service.h" + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +class SampleServiceStub { +public: + SampleServiceStub(){} + virtual ~SampleServiceStub(){} + + int32_t SampleStubBooleanTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubByteTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubShortTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubIntTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubLongTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubFloatTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubDoubleTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubStringTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubUcharTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubUshortTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubUintTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubUlongTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubListTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubMapTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubArrayTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubStructTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + int32_t SampleStubEnumTypeTest(MessageParcel& data, MessageParcel& reply, MessageOption& option) const; + + int32_t SampleServiceStubOnRemoteRequest(int cmdId, MessageParcel& data, MessageParcel& reply, + MessageOption& option) const; +private: + SampleService service; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +void *SampleStubInstance(); + +void SampleStubRelease(void *obj); + +int32_t SampleServiceOnRemoteRequest(void *stub, int cmdId, struct HdfSBuf& data, struct HdfSBuf& reply); + +#endif // HDI_SAMPLE_SERVICE_STUB_CPP_INF_H \ No newline at end of file diff --git a/uhdf2/hdi/test/load_hdi/hdf_load_hdi_test.c b/uhdf2/hdi/test/load_hdi/hdf_load_hdi_test.c new file mode 100644 index 0000000..34fad61 --- /dev/null +++ b/uhdf2/hdi/test/load_hdi/hdf_load_hdi_test.c @@ -0,0 +1,290 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "hdf_load_hdi.h" +#include "osal_time.h" + +static int32_t g_testItemCnt = 0; +static int32_t g_testItemFailCnt = 0; + +#define UT_TEST_CHECK_RET(val, flag) do { \ + if (!(val)) { \ + HDF_LOGE("[HDF_TEST] %s line:%d HDF_TEST_FAIL", __func__, __LINE__); \ + printf("[HDF_TEST] %s line:%d HDF_TEST_FAIL\r\n", __func__, __LINE__); \ + g_testItemFailCnt++; \ + } else if ((flag)) { \ + HDF_LOGD("[HDF_TEST] %s line:%d HDF_TEST_PASS", __func__, __LINE__); \ + printf("[HDF_TEST] %s line:%d HDF_TEST_PASS\r\n", __func__, __LINE__); \ + } \ + g_testItemCnt++; \ +} while (0) + +#define SERRVICE_A_RESULT 100 +#define SERRVICE_B_RESULT 101 +#define HDI_LOAD_TIME_RANGE 10 +#define HDI_LOAD_PRESSURE_TIME 1000 +#define HDF_MICRO_UNIT 1000000 + +struct HdiObject *g_hdi[HDI_LOAD_PRESSURE_TIME]; + +#define HDF_LOG_TAG hdf_hdi_test + +struct HdiSampleA { + struct HdiBase base; + int (*ServiceA)(); + int (*ServiceB)(int para); +}; + +void PrintTestResult() +{ + HDF_LOGE("[HDF_TEST] %s test items: %d", __func__, g_testItemCnt); + HDF_LOGE("[HDF_TEST] %s test PASS: %d", __func__, g_testItemCnt - g_testItemFailCnt); + HDF_LOGE("[HDF_TEST] %s test FAIL: %d", __func__, g_testItemFailCnt); + printf("[HDF_TEST] %s test items: %d\r\n", __func__, g_testItemCnt); + printf("[HDF_TEST] %s test PASS: %d\r\n", __func__, g_testItemCnt - g_testItemFailCnt); + printf("[HDF_TEST] %s test FAIL: %d\r\n", __func__, g_testItemFailCnt); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_001/HDF_LOAD_HDI_BaseFunction_AR_002 */ +static void HdiOpenTest() +{ + int32_t ret; + OsalTimespec time1 = {0, 0}; + OsalTimespec time2 = {0, 0}; + OsalTimespec diff = {0, 0}; + struct HdiObject *hdi = NULL; + struct HdiSampleA *sampleA = NULL; + + OsalGetTime(&time1); + + hdi = LoadHdi("hdf_test_load_hdi_driver.so", 1); + OsalGetTime(&time2); + UT_TEST_CHECK_RET(hdi != NULL, true); + if (hdi == NULL) { + return; + } + sampleA = (struct HdiSampleA *)hdi->hdiBase; + UT_TEST_CHECK_RET(sampleA != NULL, true); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_A_RESULT, true); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_B_RESULT, true); + CloseHdi(hdi); + OsalDiffTime(&time1, &time2, &diff); + HDF_LOGD("%s load one times: use %lu s %lu us", __func__, diff.sec, diff.usec); + printf("[HDF_TEST] %s load one times: use %lu s %lu us\r\n", __func__, diff.sec, diff.usec); + UT_TEST_CHECK_RET((diff.usec / HDF_KILO_UNIT) <= HDI_LOAD_TIME_RANGE, true); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_003 */ +static void HdiReCloseTest() +{ + int32_t ret; + struct HdiObject *hdi = NULL; + struct HdiSampleA *sampleA = NULL; + + hdi = LoadHdi("hdf_test_load_hdi_driver.so", 1); + UT_TEST_CHECK_RET(hdi != NULL, true); + if (hdi == NULL) { + return; + } + sampleA = (struct HdiSampleA *)hdi->hdiBase; + UT_TEST_CHECK_RET(sampleA != NULL, true); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_A_RESULT, true); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_B_RESULT, true); + CloseHdi(hdi); + CloseHdi(hdi); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_004 */ +static void HdiOpenVersionErrorTest() +{ + int32_t ret; + struct HdiObject *hdi = NULL; + struct HdiSampleA *sampleA = NULL; + + hdi = LoadHdi("hdf_test_load_hdi_driver.so", 0); + UT_TEST_CHECK_RET(hdi == NULL, true); + if (hdi == NULL) { + return; + } + sampleA = (struct HdiSampleA *)hdi->hdiBase; + UT_TEST_CHECK_RET(sampleA == NULL, true); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret != SERRVICE_A_RESULT, true); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret != SERRVICE_B_RESULT, true); + CloseHdi(hdi); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_005 */ +static void HdiOpenNameErrorTest() +{ + int32_t ret; + struct HdiObject *hdi = NULL; + struct HdiSampleA *sampleA = NULL; + + hdi = LoadHdi("hdf_test_load_hdi_service_error.so", 1); + UT_TEST_CHECK_RET(hdi == NULL, true); + if (hdi == NULL) { + return; + } + sampleA = (struct HdiSampleA *)hdi->hdiBase; + UT_TEST_CHECK_RET(sampleA == NULL, true); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret != SERRVICE_A_RESULT, true); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret != SERRVICE_B_RESULT, true); + CloseHdi(hdi); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_006 */ +static void HdiOpenErrorTest() +{ + int32_t ret; + struct HdiObject *hdi = NULL; + struct HdiSampleA *sampleA = NULL; + + hdi = LoadHdi("hdf_test_load_hdi_service_error.so", 0); + UT_TEST_CHECK_RET(hdi == NULL, true); + if (hdi == NULL) { + return; + } + sampleA = (struct HdiSampleA *)hdi->hdiBase; + UT_TEST_CHECK_RET(sampleA == NULL, true); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret != SERRVICE_A_RESULT, true); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret != SERRVICE_B_RESULT, true); + CloseHdi(hdi); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_007 HDF_LOAD_HDI_BaseFunction_AR_008 */ +static void HdiOpenMoreTest() +{ + int32_t ret; + struct HdiSampleA *sampleA = NULL; + struct HdiObject *hdi = NULL; + OsalTimespec time1 = {0, 0}; + OsalTimespec time2 = {0, 0}; + OsalTimespec diff = {0, 0}; + uint64_t totalUsec; + + OsalGetTime(&time1); + for (int i = 0; i < HDI_LOAD_PRESSURE_TIME; i++) { + g_hdi[i] = LoadHdi("hdf_test_load_hdi_driver.so", 1); + UT_TEST_CHECK_RET(g_hdi[i] != NULL, false); + } + OsalGetTime(&time2); + OsalDiffTime(&time1, &time2, &diff); + totalUsec = diff.sec * HDF_MICRO_UNIT + diff.usec; + HDF_LOGD("%s load %d times: use %lu s %lu us", __func__, HDI_LOAD_PRESSURE_TIME, diff.sec, diff.usec); + printf("[HDF_TEST] %s load %d times: use %lu s %lu us\r\n", __func__, HDI_LOAD_PRESSURE_TIME, diff.sec, diff.usec); + UT_TEST_CHECK_RET((totalUsec / HDF_KILO_UNIT / HDI_LOAD_PRESSURE_TIME) <= HDI_LOAD_TIME_RANGE, true); + + for (int i = 0; i < HDI_LOAD_PRESSURE_TIME; i++) { + hdi = g_hdi[i]; + if (hdi == NULL) { + continue; + } + sampleA = (struct HdiSampleA *)hdi->hdiBase; + UT_TEST_CHECK_RET(sampleA != NULL, false); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_A_RESULT, false); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_B_RESULT, false); + } + OsalGetTime(&time1); + for (int i = 0; i < HDI_LOAD_PRESSURE_TIME; i++) { + hdi = g_hdi[i++]; + CloseHdi(hdi); + } + OsalGetTime(&time2); + OsalDiffTime(&time1, &time2, &diff); + totalUsec = diff.sec * HDF_MICRO_UNIT + diff.usec; + HDF_LOGD("%s close %d times: use %lu s %lu us", __func__, HDI_LOAD_PRESSURE_TIME, diff.sec, diff.usec); + printf("[HDF_TEST] %s close %d times: use %lu s %lu us\r\n", __func__, HDI_LOAD_PRESSURE_TIME, diff.sec, diff.usec); + UT_TEST_CHECK_RET((totalUsec / HDF_KILO_UNIT / HDI_LOAD_PRESSURE_TIME) <= HDI_LOAD_TIME_RANGE, true); +} + +/* HDF_LOAD_HDI_BaseFunction_AR_009 */ +static void HdiOpenTwoSoTest() +{ + int32_t ret; + struct HdiObject *hdiA = NULL; + struct HdiObject *hdiB = NULL; + struct HdiSampleA *sampleA = NULL; + struct HdiSampleA *sampleB = NULL; + + hdiA = LoadHdi("hdf_test_load_hdi_driver.so", 1); + UT_TEST_CHECK_RET(hdiA != NULL, true); + if (hdiA == NULL) { + return; + } + sampleA = (struct HdiSampleA *)hdiA->hdiBase; + UT_TEST_CHECK_RET(sampleA != NULL, true); + ret = sampleA->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_A_RESULT, true); + ret = sampleA->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_B_RESULT, true); + + hdiB = LoadHdi("hdf_test_load_hdi_driver_b.so", 1); + UT_TEST_CHECK_RET(hdiB != NULL, true); + if (hdiB == NULL) { + return; + } + sampleB = (struct HdiSampleA *)hdiB->hdiBase; + UT_TEST_CHECK_RET(sampleB != NULL, true); + ret = sampleB->ServiceA(); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_A_RESULT, true); + ret = sampleB->ServiceB(0); + HDF_LOGD("%s %d ", __func__, ret); + UT_TEST_CHECK_RET(ret == SERRVICE_B_RESULT, true); + + CloseHdi(hdiA); + CloseHdi(hdiB); +} + +int main(void) +{ + HdiOpenTest(); + HdiReCloseTest(); + HdiOpenVersionErrorTest(); + HdiOpenNameErrorTest(); + HdiOpenErrorTest(); + HdiOpenMoreTest(); + HdiOpenTwoSoTest(); + PrintTestResult(); + return 0; +} + diff --git a/uhdf2/hdi/test/load_hdi/unittest/sample_hdi_driver/hdf_test_load_hdi_driver.c b/uhdf2/hdi/test/load_hdi/unittest/sample_hdi_driver/hdf_test_load_hdi_driver.c new file mode 100644 index 0000000..cc3a5ed --- /dev/null +++ b/uhdf2/hdi/test/load_hdi/unittest/sample_hdi_driver/hdf_test_load_hdi_driver.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "hdf_base.h" +#include "hdf_load_hdi.h" + +#define HDF_LOG_TAG hdf_sample_service_a +#define SAMPLEA_RET 100 +#define SAMPLEB_RET 101 + +int SampleAServiceA() +{ + HDF_LOGE("%s", __func__); + return SAMPLEA_RET; +} + +int SampleAServiceB(int para) +{ + HDF_LOGE("%s %d", __func__, para); + return SAMPLEB_RET; +} + +int SampleAOpen() +{ + HDF_LOGE("%s", __func__); + return 0; +} + +struct HdiSampleA { + struct HdiBase base; + int (*ServiceA)(); + int (*ServiceB)(int para); +}; + +struct HdiSampleA g_sampleA = { + .base = { + .moduleVersion = 1, + .moduleName = "SampleServiceA", + .OpenHdi = SampleAOpen, + }, + .ServiceA = SampleAServiceA, + .ServiceB = SampleAServiceB, +}; + +HDF_HDI_INIT(g_sampleA); + diff --git a/uhdf2/hdi/test/reg_pnp_device/hdf_dev_reg_pnp_test.c b/uhdf2/hdi/test/reg_pnp_device/hdf_dev_reg_pnp_test.c new file mode 100644 index 0000000..ca5e4a6 --- /dev/null +++ b/uhdf2/hdi/test/reg_pnp_device/hdf_dev_reg_pnp_test.c @@ -0,0 +1,326 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include "hdf_log.h" +#include "devmgr_hdi.h" +#include "hdf_remote_service.h" +#include "osal_time.h" +#include "securec.h" + +static int32_t g_testItemCnt = 0; +static int32_t g_testItemFailCnt = 0; + +#ifdef __VND__ +#define IS_HDI 0 +#else +#define IS_HDI 1 +#endif + +#define UT_TEST_CHECK_RET(val) do { \ + if (!(val)) { \ + printf("[HDF_TEST] %s line:%d HDF_TEST_FAIL\r\n", __func__, __LINE__); \ + g_testItemFailCnt++; \ + } else { \ + printf("[HDF_TEST] %s line:%d HDF_TEST_PASS\r\n", __func__, __LINE__); \ + } \ + g_testItemCnt++; \ +} while (0) + +#define HDF_LOG_TAG hdf_reg_pnp_test + +#define PNP_MODULE_NAME "libpnp_driver.so" +#define PNP_SERVICE_NAME "pnp_driver_service" + +void PrintTestResult() +{ + printf("[HDF_TEST] %s test items: %d\r\n", __func__, g_testItemCnt); + printf("[HDF_TEST] %s test PASS: %d\r\n", __func__, g_testItemCnt - g_testItemFailCnt); + printf("[HDF_TEST] %s test FAIL: %d\r\n", __func__, g_testItemFailCnt); +} + +int32_t GetProcessId(const char *processName) +{ + FILE *fp = NULL; + char buffer[80] = {0}; + char cmd[100] = "ps -ef |grep "; + if (strcat_s(cmd, sizeof(cmd) - strlen(cmd), processName) != 0) { + return 0; + } + if (strcat_s(cmd, sizeof(cmd) - strlen(cmd), " | grep -v grep | awk '{print $2}'") != 0) { + return 0; + } + fp = popen(cmd, "r"); + if (fp == NULL) { + return 0; + } + fgets(buffer, sizeof(buffer), fp); + pclose(fp); + return atoi(buffer); +} + +void KillProcessById(int32_t procssId) +{ + char cmd[100] = "kill "; + char buf[10] = {0}; + if (sprintf_s(buf, sizeof(buf) - 1, "%d", procssId) < 0) { + return; + } + if (strcat_s(cmd, sizeof(cmd) - strlen(cmd), buf) != 0) { + return; + } + system(cmd); +} + + +static int32_t HdfRegPnpDevice(const char *moduleName, const char *serviceName) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->RegPnpDevice(iDevmgr, moduleName, serviceName); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +static int32_t HdfUnRegPnpDevice(const char *moduleName, const char *serviceName) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->UnRegPnpDevice(iDevmgr, moduleName, serviceName); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +/* +* @tc.name: HDF_PNP_DEVICE_BaseFunction_AR_001 +* @tc.desc: reg pnp driver +* @tc.type: FUNC +* @tc.require: SR000F86A5 +*/ +static void HdfRegPnpDriverTest_001() +{ + int32_t ret; + ret = HdfRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + ret = HdfRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret != HDF_SUCCESS); +} + +/* +* @tc.name: HDF_PNP_DEVICE_BaseFunction_AR_002 +* @tc.desc: unreg pnp driver +* @tc.type: FUNC +* @tc.require: SR000F86A5 +*/ +static void HdfRegPnpDriverTest_002() +{ + int32_t ret; + ret = HdfUnRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + ret = HdfUnRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret != HDF_SUCCESS); +} + +/* +* @tc.name: HDF__PNP_DEVICE_BaseFunction_AR_003 +* @tc.desc: reg pnp driver +* @tc.type: FUNC +* @tc.require: AR000F86A6 AR000F86A7 AR000F86A8 AR000F86A9 +*/ +static void HdfRegPnpDriverTest_003() +{ + int32_t ret; + ret = HdfRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + struct HDIServiceManager *hdiServMgr = HDIServiceManagerGet(); + UT_TEST_CHECK_RET(hdiServMgr != NULL); + + struct HdfRemoteService *remote = hdiServMgr->GetService(hdiServMgr, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(remote != NULL); + if (remote != NULL) { + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + HdfSbufWriteString(data, "test for pnp dispatch!"); + int32_t processIdBefor = GetProcessId("pnp_host"); + ret = remote->dispatcher->Dispatch(remote, 0, data, reply); + int32_t processIdAfter = GetProcessId("pnp_host"); + UT_TEST_CHECK_RET(processIdBefor == processIdAfter); + int32_t result = 0; + HdfParcelReadInt(reply, &result); + UT_TEST_CHECK_RET(result == 1); + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + } + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + HDIServiceManagerRelease(hdiServMgr); +} + +/* +* @tc.name: HDF__PNP_DEVICE_BaseFunction_AR_004 +* @tc.desc: unreg pnp driver +* @tc.type: FUNC +* @tc.require: AR000F86A6 AR000F86A7 AR000F86A8 AR000F86A9 +*/ +static void HdfRegPnpDriverTest_004() +{ + int ret = HdfUnRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + struct HdfRemoteService *remote = HdfRemoteServiceGet(PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(remote == NULL); +} + +/* +* @tc.name: HDF__PNP_DEVICE_BaseFunction_AR_005 +* @tc.desc: reg fault pnp driver +* @tc.type: FUNC +* @tc.require: AR000F86A6 AR000F86A7 AR000F86A8 AR000F86A9 +*/ +static void HdfRegPnpDriverTest_005() +{ + int ret = HdfRegPnpDevice("libpnp_driver1.so", "pnp_driver_service_1"); + struct HdfRemoteService *remote = HdfRemoteServiceGet("pnp_driver_service_1"); + UT_TEST_CHECK_RET(remote == NULL); + if (ret == HDF_SUCCESS) { + HdfUnRegPnpDevice("libpnp_driver1.so", "pnp_driver_service1"); + } +} + +/* +* @tc.name: HDF__PNP_DEVICE_BaseFunction_AR_006 +* @tc.desc: reg fault pnp driver +* @tc.type: FUNC +* @tc.require: AR000F86A6 AR000F86A7 AR000F86A8 AR000F86A9 +*/ +static void HdfRegPnpDriverTest_006() +{ + int32_t ret; + uint64_t totalTime = 0; + for (int i = 0; i < 1000; i++) { + uint64_t timeBefor = OsalGetSysTimeMs(); + char svcName[128] = {0}; + if (sprintf_s(svcName, sizeof(svcName) - 1, "%s%d", PNP_SERVICE_NAME, i) < 0) { + continue; + } + const char *serviceName = svcName; + ret = HdfRegPnpDevice(PNP_MODULE_NAME, serviceName); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + ret = HdfUnRegPnpDevice(PNP_MODULE_NAME, serviceName); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + uint64_t timeAfter = OsalGetSysTimeMs(); + totalTime += timeAfter - timeBefor; + } + printf("total time is %ld \n", totalTime); + UT_TEST_CHECK_RET((totalTime / 1000) < 20); +} + +/* +* @tc.name: HdfRestore_001 +* @tc.desc: restore host process +* @tc.type: FUNC +* @tc.require: AR000F86AL AR000F86AM +*/ +static void HdfRestore_001() +{ + int32_t ret; + ret = HdfRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + struct HDIServiceManager *hdiServMgr = HDIServiceManagerGet(); + UT_TEST_CHECK_RET(hdiServMgr != NULL); + + struct HdfRemoteService *remote = hdiServMgr->GetService(hdiServMgr, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(remote != NULL); + if (remote != NULL) { + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + HdfSbufWriteString(data, "test for pnp dispatch!"); + int32_t processIdBefor = GetProcessId("pnp_host"); + ret = remote->dispatcher->Dispatch(remote, 1, data, reply); + int32_t processIdAfter = GetProcessId("pnp_host"); + UT_TEST_CHECK_RET(processIdBefor != processIdAfter); + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + } + UT_TEST_CHECK_RET(ret != HDF_SUCCESS); + HDIServiceManagerRelease(hdiServMgr); + ret = HdfUnRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); +} + +/* +* @tc.name: HdfRestore_002 +* @tc.desc: restore host process +* @tc.type: FUNC +* @tc.require: AR000F86AL AR000F86AM +*/ +static void HdfRestore_002() +{ + int32_t ret; + ret = HdfRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); + struct HDIServiceManager *hdiServMgr = HDIServiceManagerGet(); + UT_TEST_CHECK_RET(hdiServMgr != NULL); + + struct HdfRemoteService *remote = hdiServMgr->GetService(hdiServMgr, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(remote != NULL); + + int32_t processIdBefor = GetProcessId("pnp_host"); + KillProcessById(processIdBefor); + int32_t processIdAfter = GetProcessId("pnp_host"); + UT_TEST_CHECK_RET(processIdBefor != processIdAfter); + remote = hdiServMgr->GetService(hdiServMgr, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(remote != NULL); + HDIServiceManagerRelease(hdiServMgr); + ret = HdfUnRegPnpDevice(PNP_MODULE_NAME, PNP_SERVICE_NAME); + UT_TEST_CHECK_RET(ret == HDF_SUCCESS); +} + +/* +* @tc.name: HdfRestore_003 +* @tc.desc: restore manager process +* @tc.type: FUNC +* @tc.require: SR000F86AK +*/ +static void HdfRestore_003() +{ + int32_t processIdBefor = GetProcessId("hdf_devmgr"); + KillProcessById(processIdBefor); + int32_t processIdAfter = GetProcessId("hdf_devmgr"); + UT_TEST_CHECK_RET((processIdAfter != 0) && (processIdBefor != processIdAfter)); +} + +int main(void) +{ + HdfRegPnpDriverTest_001(); + HdfRegPnpDriverTest_002(); + HdfRegPnpDriverTest_003(); + HdfRegPnpDriverTest_004(); + HdfRegPnpDriverTest_005(); + HdfRegPnpDriverTest_006(); + HdfRestore_001(); + HdfRestore_002(); + HdfRestore_003(); + PrintTestResult(); + return 0; +} + diff --git a/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.c b/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.c new file mode 100644 index 0000000..2f5ebcf --- /dev/null +++ b/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "hdf_pnp_driver.h" +#include "pal_if.h" + +#define HDF_LOG_TAG hdf_pnp_driver + +static int g_gpioFlg = 0; + +int32_t PnpDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + (void)client; + if (data == NULL || reply == NULL) { + HDF_LOGE("%s failed, input is null", __func__); + return HDF_FAILURE; + } + int32_t ret = HDF_SUCCESS; + switch (cmdId) { + case HDF_PNP_MES_TEST: { + const char *str = HdfSbufReadString(data); + HDF_LOGE("%s pnp get str is %s", __func__, str); + HdfSbufWriteInt32(reply, 1); + break; + } + case HDF_PNP_MES_FAULT: { + // test to crash process + const char *str = NULL; + HDF_LOGE("%s test to crash procss is %c", __func__, str[1]); + break; + } + case HDF_PNP_MES_SECUREC: { + if (g_gpioFlg == 0) { + ret = HDF_FAILURE; + } else { + ret = HDF_SUCCESS; + } + break; + } + default: { + HDF_LOGE("%s failed, cmd id is %d", __func__, cmdId); + break; + } + } + return ret; +} + +int32_t HdfPnpDriverBind(struct HdfDeviceObject *para) +{ + HDF_LOGE("%s", __func__); + static struct IDeviceIoService virtualService = { + .object.objectId = 1, + .Dispatch = PnpDriverDispatch, + }; + para->service = &virtualService; + return 0; +} + +int32_t HdfPnpDriverInit(struct HdfDeviceObject *para) +{ + HDF_LOGE("%s enter!", __func__); + if (para == NULL) { + HDF_LOGE("%s init failed, input is null", __func__); + return HDF_FAILURE; + } + DeviceHandle *palHandle = DeviceHandleCreate(PAL_GPIO_TYPE, NULL); + if (palHandle == NULL) { + HDF_LOGE("%s %d open gpio failed", __func__, __LINE__); + g_gpioFlg = 0; + } else { + HDF_LOGE("%s %d open gpio success", __func__, __LINE__); + DeviceHandleDestroy(palHandle); + g_gpioFlg = 1; + } + return 0; +} + +void HdfPnpDriverRelease(struct HdfDeviceObject *para) +{ + HDF_LOGE("%s enter!", __func__); + if (para == NULL) { + HDF_LOGE("%s release failed, input is null", __func__); + return; + } + para->service = NULL; +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .Bind = HdfPnpDriverBind, + .Init = HdfPnpDriverInit, + .Release = HdfPnpDriverRelease, + .moduleName = "pnp_driver", +}; + +HDF_INIT(g_sampleDriverEntry); \ No newline at end of file diff --git a/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h b/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h new file mode 100644 index 0000000..b5c3cc8 --- /dev/null +++ b/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 HDF_PNP_DRIVER_H +#define HDF_PNP_DRIVER_H + +#include "hdf_base.h" +#include "hdf_dlist.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +enum HDF_PNP_DRIVER_CODE { + HDF_PNP_MES_TEST = 0, + HDF_PNP_MES_FAULT, + HDF_PNP_MES_SECUREC, +}; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDF_PNP_DRIVER_H */ + diff --git a/uhdf2/hdi/test/reg_virtual_device/hdf_dev_reg_virtual_test.c b/uhdf2/hdi/test/reg_virtual_device/hdf_dev_reg_virtual_test.c new file mode 100644 index 0000000..fc2069d --- /dev/null +++ b/uhdf2/hdi/test/reg_virtual_device/hdf_dev_reg_virtual_test.c @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "devmgr_hdi.h" +#include "osal_time.h" + +static int32_t g_testItemCnt = 0; +static int32_t g_testItemFailCnt = 0; + +#define UT_TEST_CHECK_RET(val, flag) do { \ + if (!(val)) { \ + HDF_LOGE("[HDF_TEST] %s line:%d HDF_TEST_FAIL", __func__, __LINE__); \ + printf("[HDF_TEST] %s line:%d HDF_TEST_FAIL\r\n", __func__, __LINE__); \ + g_testItemFailCnt++; \ + } else if ((flag)) { \ + HDF_LOGD("[HDF_TEST] %s line:%d HDF_TEST_PASS", __func__, __LINE__); \ + printf("[HDF_TEST] %s line:%d HDF_TEST_PASS\r\n", __func__, __LINE__); \ + } \ + g_testItemCnt++; \ +} while (0) + +#define GET_TIME_BEGIN(time) OsalGetTime(time) +#define GET_TIME_END(incex, type, time1, time2, diff, func, line) do { \ + OsalGetTime(time2); \ + OsalDiffTime(time1, time2, diff); \ + CalcAverageTime(index, type, diff, func, line); \ +} while (0) + +#define HDF_LOG_TAG hdf_reg_virtual_test + +#define VIRTUAL_MODULE_NAME "hdf_test_virtual_driver.so" +#define VIRTUAL_MODULE_NAME_NO "hdf_test_virtual_driver1.so" +#define VIRTUAL_SERVICE_NAME "hdf_test_virtual_driver" +#define VIRTUAL_SERVICE_NAME1 "hdf_test_virtual_driver1" +#define VIRTUAL_SERVICE_NAME2 "hdf_test_virtual_driver2" +#define VIRTUAL_SERVICE_NO_DRIVER "hdf_test_virtual_driver3" + +#define SERVICE_CNT 3 +#define SERVICE_NO_DRIVER_CNT 1 + +#define REGISTER_WAIT_TIME 100 +static char *g_testServiceName[SERVICE_CNT] = { VIRTUAL_SERVICE_NAME, VIRTUAL_SERVICE_NAME1, VIRTUAL_SERVICE_NAME2 }; +static char *g_testServiceNameNoDrv[SERVICE_NO_DRIVER_CNT] = { VIRTUAL_SERVICE_NO_DRIVER }; +#define HDF_REG_DEVICE_ONE 0xffff +#define HDF_REG_TIME_RANGE 10 +#define HDF_REG_DEVICE_MORE 200 +#define HDF_MICRO_UNIT 1000000 +#define TEST_ITEM_CNT 2 +#define ITEM_REG 0 +#define ITEM_UNREG 1 + +static int g_index[TEST_ITEM_CNT]; +static uint64_t g_totalUsec[TEST_ITEM_CNT]; + +void PrintTestResult() +{ + HDF_LOGE("[HDF_TEST] %s test items: %d", __func__, g_testItemCnt); + HDF_LOGE("[HDF_TEST] %s test PASS: %d", __func__, g_testItemCnt - g_testItemFailCnt); + HDF_LOGE("[HDF_TEST] %s test FAIL: %d", __func__, g_testItemFailCnt); + printf("[HDF_TEST] %s test items: %d\r\n", __func__, g_testItemCnt); + printf("[HDF_TEST] %s test PASS: %d\r\n", __func__, g_testItemCnt - g_testItemFailCnt); + printf("[HDF_TEST] %s test FAIL: %d\r\n", __func__, g_testItemFailCnt); +} + +static void CalcAverageTime(int index, bool type, const OsalTimespec *diff, const char *func, int line) +{ + if (type == true) { + HDF_LOGD("[HDF_TEST] %s line:%d test 1 time use time:%lu s %lu us", func, line, diff->sec, diff->usec); + printf("[HDF_TEST] %s line:%d test 1 time use time:%lu s %lu us\r\n", func, line, diff->sec, diff->usec); + g_totalUsec[index] += diff->sec * HDF_MICRO_UNIT + diff->usec; + } else { + g_totalUsec[index] += diff->sec * HDF_MICRO_UNIT + diff->usec; + } + g_index[index]++; +} + +static void CheckAverageTime() +{ + HDF_LOGD("[HDF_TEST] %s line:%d test reg %d time use time:%lu us", + __func__, __LINE__, g_index[ITEM_REG], g_totalUsec[ITEM_REG]); + printf("[HDF_TEST] %s line:%d test reg %d time use time: %lu us\r\n", + __func__, __LINE__, g_index[ITEM_REG], g_totalUsec[ITEM_REG]); + + HDF_LOGD("[HDF_TEST] %s line:%d test unreg %d time use time:%lu us", + __func__, __LINE__, g_index[ITEM_UNREG], g_totalUsec[ITEM_UNREG]); + printf("[HDF_TEST] %s line:%d test unreg %d time use time: %lu us\r\n", + __func__, __LINE__, g_index[ITEM_UNREG], g_totalUsec[ITEM_UNREG]); + + UT_TEST_CHECK_RET((g_totalUsec[ITEM_REG] / HDF_KILO_UNIT / g_index[ITEM_REG]) <= HDF_REG_TIME_RANGE, true); + UT_TEST_CHECK_RET((g_totalUsec[ITEM_UNREG] / HDF_KILO_UNIT / g_index[ITEM_UNREG]) <= HDF_REG_TIME_RANGE, true); +} + +static int32_t HdfRegVirtualDevice(const char *moduleName, const char *serviceName) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->RegVirtualDevice(iDevmgr, moduleName, serviceName); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +static int32_t HdfUnRegVirtualDevice(const char *moduleName, const char *serviceName) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->UnRegVirtualDevice(iDevmgr, moduleName, serviceName); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +static void HdfRegOneVirtualTest( + int index, bool type, const char *moduleName, const char *serviceName, const char *func, int line, int result) +{ + int32_t ret; + OsalTimespec time1 = { 0, 0 }; + OsalTimespec time2 = { 0, 0 }; + OsalTimespec diff = { 0, 0 }; + GET_TIME_BEGIN(&time1); + ret = HdfRegVirtualDevice(moduleName, serviceName); + if (ret != HDF_SUCCESS) { + ret = HDF_FAILURE; + } + GET_TIME_END(index, type, &time1, &time2, &diff, func, line); + UT_TEST_CHECK_RET(ret == result, type); + if (ret == result) { + HDF_LOGE("[HDF_TEST] %s line:%d", func, line); + } else { + HDF_LOGE("[HDF_TEST] %s line:%d %d", func, line, ret); + printf("[HDF_TEST] %s line:%d %d\r\n", func, line, ret); + } +} + +static void HdfUnRegOneVirtualTest( + int index, bool type, const char *moduleName, const char *serviceName, const char *func, int line, int result) +{ + int32_t ret; + OsalTimespec time1 = { 0, 0 }; + OsalTimespec time2 = { 0, 0 }; + OsalTimespec diff = { 0, 0 }; + GET_TIME_BEGIN(&time1); + ret = HdfUnRegVirtualDevice(moduleName, serviceName); + GET_TIME_END(index, type, &time1, &time2, &diff, func, line); + if (ret != HDF_SUCCESS) { + ret = HDF_FAILURE; + } + + UT_TEST_CHECK_RET(ret == result, type); + if (ret == result) { + HDF_LOGE("[HDF_TEST] %s line:%d", __func__, __LINE__); + } else { + HDF_LOGE("[HDF_TEST] %s line:%d %d", __func__, __LINE__, ret); + } +} + +/* + * HDF_VIRTUAL_DEVICE_BaseFunction_AR_001 HDF_VIRTUAL_DEVICE_BaseFunction_AR_002 + * HDF_VIRTUAL_DEVICE_BaseFunction_AR_007 + */ +static void HdfRegVirtualTest(bool type) +{ + /* HDF_VIRTUAL_DEVICE_BaseFunction_AR_005 */ + HdfRegOneVirtualTest(ITEM_REG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME, __func__, __LINE__, HDF_SUCCESS); + HdfRegOneVirtualTest(ITEM_REG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME, __func__, __LINE__, HDF_FAILURE); + HdfRegOneVirtualTest(ITEM_REG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME1, __func__, __LINE__, HDF_SUCCESS); + HdfRegOneVirtualTest(ITEM_REG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME2, __func__, __LINE__, HDF_SUCCESS); + HdfRegOneVirtualTest(ITEM_REG, type, VIRTUAL_MODULE_NAME_NO, VIRTUAL_SERVICE_NO_DRIVER, + __func__, __LINE__, HDF_FAILURE); +} + +/* + * HDF__VIRTUAL_DEVICE_BaseFunction_AR_003 HDF__VIRTUAL_DEVICE_BaseFunction_AR_004 + * HDF_VIRTUAL_DEVICE_BaseFunction_AR_008 + */ +static void HdfUnRegVirtualTest(bool type) +{ + HdfUnRegOneVirtualTest(ITEM_UNREG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME, + __func__, __LINE__, HDF_SUCCESS); + HdfUnRegOneVirtualTest(ITEM_UNREG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME1, + __func__, __LINE__, HDF_SUCCESS); + HdfUnRegOneVirtualTest(ITEM_UNREG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME2, + __func__, __LINE__, HDF_SUCCESS); + HdfUnRegOneVirtualTest(ITEM_UNREG, type, VIRTUAL_MODULE_NAME, VIRTUAL_SERVICE_NAME2, + __func__, __LINE__, HDF_FAILURE); + /* HDF_VIRTUAL_DEVICE_BaseFunction_AR_006 */ + HdfUnRegOneVirtualTest(ITEM_UNREG, type, VIRTUAL_MODULE_NAME, "test_no_unregister_123", + __func__, __LINE__, HDF_FAILURE); + HdfUnRegOneVirtualTest(ITEM_UNREG, type, VIRTUAL_MODULE_NAME_NO, VIRTUAL_SERVICE_NO_DRIVER, + __func__, __LINE__, HDF_FAILURE); +} + +static bool CheckAllService(const char *name, bool usable) +{ + char **arr = NULL; + int32_t serviceCnt; + if (usable) { + arr = g_testServiceName; + serviceCnt = SERVICE_CNT; + } else { + arr = g_testServiceNameNoDrv; + serviceCnt = SERVICE_NO_DRIVER_CNT; + } + for (int i = 0; i < serviceCnt; i++) { + if (strcmp(name, arr[i]) == 0) { + return true; + } + } + return false; +} + + +void HdfFreeQueryDeviceList(struct DeviceInfoList *list) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return; + } + + iDevmgr->FreeQueryDeviceList(iDevmgr, list); + + HDIDeviceManagerRelease(iDevmgr); +} + +int32_t HdfQueryUsableDeviceInfo(struct DeviceInfoList *list) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->QueryUsableDeviceInfo(iDevmgr, list); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + +int32_t HdfQueryUnusableDeviceInfo(struct DeviceInfoList *list) +{ + struct HDIDeviceManager *iDevmgr = HDIDeviceManagerGet(); + if (iDevmgr == NULL) { + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + int32_t ret = iDevmgr->QueryUnusableDeviceInfo(iDevmgr, list); + + HDIDeviceManagerRelease(iDevmgr); + return ret; +} + + +static void RegQueryDevice(bool printFlag, bool flag, bool usable) +{ + int32_t ret; + int32_t serviceCnt; + struct DeviceInfoList list; + struct DeviceInfoNode *devNode = NULL; + int cnt = 0; + char *debugInfo = NULL; + if (usable) { + ret = HdfQueryUsableDeviceInfo(&list); + serviceCnt = SERVICE_CNT; + debugInfo = "usable"; + } else { + ret = HdfQueryUnusableDeviceInfo(&list); + serviceCnt = 0; + debugInfo = "un usable"; + } + UT_TEST_CHECK_RET(ret == HDF_SUCCESS, printFlag); + if (ret == HDF_SUCCESS) { + HDF_LOGD("[HDF_TEST] %s %s:%d line:%d", __func__, debugInfo, list.deviceCnt, __LINE__); + DLIST_FOR_EACH_ENTRY(devNode, &list.list, struct DeviceInfoNode, node) { + HDF_LOGD("[HDF_TEST] %s: %s %d", debugInfo, devNode->svcName, devNode->deviceType); + if (CheckAllService(devNode->svcName, usable)) { + cnt++; + } + } + if (flag) { + UT_TEST_CHECK_RET(cnt == serviceCnt, printFlag); + if (cnt != serviceCnt) { + HDF_LOGE("[HDF_TEST] %s find usable service failed %d != %d", __func__, cnt, serviceCnt); + printf("[HDF_TEST] %s find usable service failed %d != %d\r\n", __func__, cnt, serviceCnt); + } + } else { + UT_TEST_CHECK_RET(cnt == 0, printFlag); + if (cnt != 0) { + HDF_LOGE("[HDF_TEST] %s find unusable service failed %d != %d", __func__, cnt, 0); + printf("[HDF_TEST] %s find unusable service failed %d != %d\r\n", __func__, cnt, 0); + } + } + } else { + HDF_LOGE("[HDF_TEST] %s line:%d", __func__, __LINE__); + UT_TEST_CHECK_RET(false, true); + } + HdfFreeQueryDeviceList(&list); +} + +int main(int argc, char **argv) +{ + int testCnt = 1; + bool printFlag = true; + (void)argv; + if (argc != 1) { + testCnt = HDF_REG_DEVICE_MORE; + printFlag = false; + } + for (int i = 0; i < testCnt; i++) { + HdfRegVirtualTest(printFlag); + RegQueryDevice(printFlag, true, true); + RegQueryDevice(printFlag, true, false); + HdfUnRegVirtualTest(printFlag); + RegQueryDevice(printFlag, false, true); + RegQueryDevice(printFlag, false, false); + } + if (argc != 1) { + CheckAverageTime(); + } + PrintTestResult(); + return 0; +} + diff --git a/uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c b/uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c new file mode 100644 index 0000000..a55435e --- /dev/null +++ b/uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_log.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" + +#define HDF_LOG_TAG hdf_virtual_driver + +int32_t HdfVirtualDriverBind(struct HdfDeviceObject *para) +{ + HDF_LOGE("%s", __func__); + static struct IDeviceIoService virtualService = { + .object.objectId = 1, + .Dispatch = NULL, + }; + para->service = &virtualService; + return 0; +} + +int32_t HdfVirtualDriverInit(struct HdfDeviceObject *para) +{ + (void)para; + HDF_LOGE("%s", __func__); + return 0; +} + +void HdfVirtualDriverRelease(struct HdfDeviceObject *para) +{ + (void)para; + + HDF_LOGE("%s", __func__); +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .Bind = HdfVirtualDriverBind, + .Init = HdfVirtualDriverInit, + .Release = HdfVirtualDriverRelease, + .moduleName = "virtual_driver", +}; +HDF_INIT(g_sampleDriverEntry); \ No newline at end of file diff --git a/uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp b/uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp new file mode 100755 index 0000000..75fbcba --- /dev/null +++ b/uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2021 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. + */ + + +#include +#include +#include +#include +#include +#include "sample_hdi.h" + +#define HDF_LOG_TAG service_manager_test + +using namespace testing::ext; + +constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; +constexpr int PAYLOAD_NUM = 1234; + +class HdfServiceMangerHdiCTest : public testing::Test { +public: + static void SetUpTestCase() {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest001, TestSize.Level0) +{ + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + ASSERT_TRUE(servmgr != nullptr); + HDIServiceManagerRelease(servmgr); +} + +HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest002, TestSize.Level0) +{ + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + ASSERT_TRUE(servmgr != nullptr); + + struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + ASSERT_TRUE(sampleService != nullptr); + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + ASSERT_TRUE(data != nullptr); + ASSERT_TRUE(reply != nullptr); + + bool ret = HdfSbufWriteString(data, "sample_service test call"); + ASSERT_EQ(ret, true); + + int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_SERVICE_PING, data, reply); + ASSERT_EQ(status, 0); + + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} + +static int32_t g_callbackPayload = 0; + +int ServiceManagerTestCallbackDispatch(struct HdfRemoteService *service, int code, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + HDF_LOGI("ServiceManagerTestCallbackDispatch called, code = %{public}d", code); + HdfSbufReadInt32(data, &g_callbackPayload); + return HDF_SUCCESS; +} + +struct HdfRemoteDispatcher g_callbackDispatcher { + .Dispatch = ServiceManagerTestCallbackDispatch, +}; + +HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest003, TestSize.Level0) +{ + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + ASSERT_TRUE(servmgr != nullptr); + + struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + ASSERT_TRUE(sampleService != nullptr); + + struct HdfRemoteService *callback = HdfRemoteServiceObtain(NULL, &g_callbackDispatcher); + ASSERT_NE(callback, nullptr); + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + ASSERT_TRUE(data != nullptr); + ASSERT_TRUE(reply != nullptr); + + int32_t payload = PAYLOAD_NUM; + HdfSbufWriteInt32(data, payload); + HdfSBufWriteRemoteService(data, callback); + + int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_SERVICE_CALLBACK, data, reply); + ASSERT_EQ(status, 0); + ASSERT_EQ(g_callbackPayload, payload); + + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} + +HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest004, TestSize.Level0) +{ + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + ASSERT_TRUE(servmgr != nullptr); + + struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + ASSERT_TRUE(sampleService != nullptr); + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + ASSERT_TRUE(data != nullptr); + ASSERT_TRUE(reply != nullptr); + HdfSbufWriteInt32(data, PAYLOAD_NUM); + HdfSbufWriteInt32(data, PAYLOAD_NUM); + + int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_SERVICE_SUM, data, reply); + ASSERT_EQ(status, 0); + int32_t result; + bool ret = HdfSbufReadInt32(reply, &result); + ASSERT_TRUE(ret); + + int32_t expRes = PAYLOAD_NUM + PAYLOAD_NUM; + ASSERT_EQ(result, expRes); + + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} + +HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest005, TestSize.Level0) +{ + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + ASSERT_TRUE(servmgr != nullptr); + + struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + ASSERT_TRUE(sampleService != nullptr); + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + ASSERT_TRUE(data != nullptr); + ASSERT_TRUE(reply != nullptr); + + struct DataBlock dataBlock = { 1, 2, "dataBolck", 3}; + bool ret = DataBlockBlockMarshalling(&dataBlock, data); + ASSERT_TRUE(ret); + + int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_STRUCT_TRANS, data, reply); + ASSERT_EQ(status, 0); + + struct DataBlock *dataBlock_ = DataBlockBlockUnmarshalling(reply); + ASSERT_TRUE(dataBlock_ != nullptr); + + ASSERT_EQ(dataBlock_->a, dataBlock.a); + ASSERT_EQ(dataBlock_->b, dataBlock.b); + ASSERT_EQ(dataBlock_->c, dataBlock.c); + ASSERT_TRUE(!strcmp(dataBlock_->str, dataBlock.str)); + DataBlockFree(dataBlock_); + + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} + +HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest006, TestSize.Level0) +{ + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + ASSERT_TRUE(servmgr != nullptr); + + struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + ASSERT_TRUE(sampleService != nullptr); + + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + ASSERT_TRUE(data != nullptr); + ASSERT_TRUE(reply != nullptr); + + constexpr int buffersize = 10; + uint8_t dataBuffer[buffersize]; + for (int i = 0; i < buffersize; i++) { + dataBuffer[i] = i; + } + + bool ret = HdfSbufWriteUnpadBuffer(data, dataBuffer, sizeof(dataBuffer)); + ASSERT_TRUE(ret); + + int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_BUFFER_TRANS, data, reply); + ASSERT_EQ(status, 0); + + const uint8_t *retBuffer = HdfSbufReadUnpadBuffer(reply, buffersize); + ASSERT_TRUE(retBuffer != nullptr); + + for (int i = 0; i < buffersize; i++) { + ASSERT_EQ(retBuffer[i], i); + } + + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} \ No newline at end of file diff --git a/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp b/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp new file mode 100755 index 0000000..e208956 --- /dev/null +++ b/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "sample_hdi.h" + +#define HDF_LOG_TAG service_manager_test_cpp + +using namespace testing::ext; +using OHOS::sptr; +using OHOS::IRemoteObject; +using OHOS::HDI::ServiceManager::V1_0::IServiceManager; + +constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; +constexpr int PAYLOAD_NUM = 1234; + +class HdfServiceMangerHdiTest : public testing::Test { +public: + static void SetUpTestCase() {}; + static void TearDownTestCase() {}; + void SetUp() {}; + void TearDown() {}; +}; + +HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest001, TestSize.Level0) +{ + auto servmgr = IServiceManager::Get(); + ASSERT_TRUE(servmgr != nullptr); +} + +HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest002, TestSize.Level0) +{ + auto servmgr = IServiceManager::Get(); + ASSERT_TRUE(servmgr != nullptr); + + auto sampleService = servmgr->GetService(TEST_SERVICE_NAME); + + ASSERT_TRUE(sampleService != nullptr); + + OHOS::MessageParcel data; + OHOS::MessageParcel reply; + data.WriteCString("sample_service test call"); + + OHOS::MessageOption option; + int status = sampleService->SendRequest(SAMPLE_SERVICE_PING, data, reply, option); + ASSERT_EQ(status, 0); +} + +class IPCObjectStubTest : public OHOS::IPCObjectStub { +public: + explicit IPCObjectStubTest() : OHOS::IPCObjectStub(u"") {}; + virtual ~IPCObjectStubTest() = default; + int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, + OHOS::MessageParcel &reply, OHOS::MessageOption &option) override + { + HDF_LOGI("IPCObjectStubTest::OnRemoteRequest called, code = %{public}d", code); + payload = data.ReadInt32(); + + return HDF_SUCCESS; + } + + static int32_t payload; +}; + +int32_t IPCObjectStubTest::payload = 0; + +HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest003, TestSize.Level0) +{ + auto servmgr = IServiceManager::Get(); + ASSERT_TRUE(servmgr != nullptr); + + auto sampleService = servmgr->GetService(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleService != nullptr); + + sptr callback = new IPCObjectStubTest(); + OHOS::MessageParcel data; + OHOS::MessageParcel reply; + int32_t payload = PAYLOAD_NUM; + data.WriteInt32(payload); + data.WriteRemoteObject(callback); + + OHOS::MessageOption option; + int status = sampleService->SendRequest(SAMPLE_SERVICE_CALLBACK, data, reply, option); + ASSERT_EQ(status, 0); + ASSERT_EQ(IPCObjectStubTest::payload, payload); +} + +HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest004, TestSize.Level0) +{ + auto servmgr = IServiceManager::Get(); + ASSERT_TRUE(servmgr != nullptr); + + auto sampleService = servmgr->GetService(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleService != nullptr); + + OHOS::MessageParcel data; + OHOS::MessageParcel reply; + data.WriteInt32(PAYLOAD_NUM); + data.WriteInt32(PAYLOAD_NUM); + + OHOS::MessageOption option; + int status = sampleService->SendRequest(SAMPLE_SERVICE_SUM, data, reply, option); + ASSERT_EQ(status, 0); + int32_t result = reply.ReadInt32(); + int32_t expRes = PAYLOAD_NUM + PAYLOAD_NUM; + ASSERT_EQ(result, expRes); +} + +HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest006, TestSize.Level0) +{ + auto servmgr = IServiceManager::Get(); + ASSERT_TRUE(servmgr != nullptr); + + auto sampleService = servmgr->GetService(TEST_SERVICE_NAME); + ASSERT_TRUE(sampleService != nullptr); + + OHOS::MessageParcel data; + OHOS::MessageParcel reply; + + constexpr int buffersize = 10; + uint8_t dataBuffer[buffersize]; + for (int i = 0; i < buffersize; i++) { + dataBuffer[i] = i; + } + + bool ret = data.WriteUnpadBuffer(dataBuffer, sizeof(dataBuffer)); + ASSERT_TRUE(ret); + + OHOS::MessageOption option; + int status = sampleService->SendRequest(SAMPLE_BUFFER_TRANS, data, reply, option); + ASSERT_EQ(status, 0); + + const uint8_t *retBuffer = reply.ReadUnpadBuffer(buffersize); + ASSERT_TRUE(retBuffer != nullptr); + + for (int i = 0; i < buffersize; i++) { + ASSERT_EQ(retBuffer[i], i); + } +} \ No newline at end of file diff --git a/uhdf2/host/BUILD.gn b/uhdf2/host/BUILD.gn new file mode 100755 index 0000000..d2f1d38 --- /dev/null +++ b/uhdf2/host/BUILD.gn @@ -0,0 +1,168 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("libhdf_host_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ + "//utils/native/base/include", + "$hdf_framework_path/core/manager/include", + "$hdf_framework_path/core/host/include", + "$hdf_framework_path/core/shared/include", + "$hdf_framework_path/include/osal", + "$hdf_framework_path/include/utils", + "$hdf_framework_path/include/core", + "$hdf_framework_path/utils/include", + "$hdf_framework_path/ability/sbuf/include", + "$hdf_framework_path/include/config", + "$hdf_framework_path/ability/config/hcs_parser/include", + "$hdf_uhdf_path/include/host", + "$hdf_uhdf_path/shared/include", + "$hdf_uhdf_path/manager/include", + "include", + ] +} + +ohos_shared_library("libhdf_host") { + public_configs = [ + ":libhdf_host_pub_config", + ] + include_dirs = [ + "//utils/native/base/include", + "$hdf_framework_path/core/manager/include", + "$hdf_framework_path/core/host/include", + "$hdf_framework_path/core/shared/include", + "$hdf_framework_path/include/osal", + "$hdf_framework_path/include/utils", + "$hdf_framework_path/include/core", + "$hdf_framework_path/utils/include", + "$hdf_framework_path/ability/sbuf/include", + "$hdf_framework_path/include/config", + "$hdf_framework_path/ability/config/hcs_parser/include", + "$hdf_uhdf_path/include/config", + "$hdf_uhdf_path/include/host", + "$hdf_uhdf_path/shared/include", + "$hdf_uhdf_path/manager/include", + "$hdf_uhdf_path/security/include", + "include", + ] + + sources = [ + "$hdf_framework_path/core/host/src/devhost_service.c", + "$hdf_framework_path/core/host/src/devmgr_service_clnt.c", + "$hdf_framework_path/core/host/src/devsvc_manager_clnt.c", + "$hdf_framework_path/core/host/src/hdf_device.c", + "$hdf_framework_path/core/host/src/hdf_device_node.c", + "$hdf_framework_path/core/host/src/hdf_device_object.c", + "$hdf_framework_path/core/host/src/hdf_device_token.c", + "$hdf_framework_path/core/host/src/hdf_driver_loader.c", + "$hdf_framework_path/core/host/src/hdf_observer_record.c", + "$hdf_framework_path/core/host/src/hdf_service_observer.c", + "$hdf_framework_path/core/host/src/hdf_service_subscriber.c", + "$hdf_framework_path/core/host/src/power_state_token.c", + "$hdf_framework_path/core/shared/src/hdf_device_info.c", + "$hdf_framework_path/core/shared/src/hdf_object_manager.c", + "$hdf_framework_path/core/shared/src/hdf_service_record.c", + "$hdf_framework_path/core/manager/src/hdf_host_info.c", + "$hdf_uhdf_path/shared/src/hdf_device_info_full.c", + "$hdf_uhdf_path/shared/src/dev_attribute_parcel.c", + "$hdf_uhdf_path/shared/src/hdf_attribute_full.c", + "$hdf_uhdf_path/host/src/devhost_object_config.c", + "$hdf_uhdf_path/host/src/devhost_service_full.c", + "$hdf_uhdf_path/host/src/devhost_service_stub.c", + "$hdf_uhdf_path/host/src/device_service_stub.c", + "$hdf_uhdf_path/host/src/device_token_stub.c", + "$hdf_uhdf_path/host/src/devmgr_service_proxy.c", + "$hdf_uhdf_path/host/src/devsvc_manager_proxy.c", + "$hdf_uhdf_path/host/src/driver_loader_full.c", + "$hdf_uhdf_path/host/src/hdf_device_full.c", + "$hdf_uhdf_path/host/src/hdf_device_thread.c", + "$hdf_uhdf_path/host/src/hdf_devsvc_manager_clnt.c", + ] + + deps = [ + "$hdf_uhdf_path/ipc:libhdf_ipc_adapter", + "$hdf_uhdf_path/osal:libhdf_utils", + "$hdf_uhdf_path/config:uhdf_hcs_pkg", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} + +ohos_executable("hdf_devhost") { + include_dirs = [ + "//utils/native/base/include", + "$hdf_framework_path/core/manager/include", + "$hdf_framework_path/core/host/include", + "$hdf_framework_path/core/shared/include", + "$hdf_framework_path/include/osal", + "$hdf_framework_path/include/utils", + "$hdf_framework_path/include/core", + "$hdf_framework_path/utils/include", + "$hdf_framework_path/ability/sbuf/include", + "$hdf_framework_path/include/config", + "$hdf_framework_path/ability/config/hcs_parser/include", + "$hdf_uhdf_path/ipc/include", + "$hdf_uhdf_path/osal/include", + "$hdf_uhdf_path/include/config", + "$hdf_uhdf_path/include/host", + "$hdf_uhdf_path/shared/include", + "$hdf_uhdf_path/manager/include", + "include", + ] + + sources = [ "devhost.c" ] + + deps = [ + "$hdf_uhdf_path/ipc:libhdf_ipc_adapter", + "$hdf_uhdf_path/osal:libhdf_utils", + "$hdf_uhdf_path/config:uhdf_hcs_pkg", + "$hdf_uhdf_path/host:libhdf_host", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + install_enable = true + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/host/devhost.c b/uhdf2/host/devhost.c new file mode 100644 index 0000000..91cc177 --- /dev/null +++ b/uhdf2/host/devhost.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "hdf_base.h" +#include "devhost_service.h" +#include "devhost_service_full.h" +#include "hdf_cstring.h" +#include "hdf_log.h" +#include "securec.h" + +#define HDF_LOG_TAG hdf_device_host +#define DEVHOST_INPUT_PARAM_NUM 3 +#define DEVHOST_INPUT_PARAM_HOSTID_POS 1 + +bool HdfStringToInt(const char *str, int *value) +{ + if (str == NULL || value == NULL) { + return false; + } + + char *end = NULL; + errno = 0; + const int base = 10; + long result = strtol(str, &end, base); + if (end == str || end[0] != '\0' || errno == ERANGE || result > INT_MAX || result < INT_MIN) { + return false; + } + + *value = (int)result; + return true; +} + +static void SetProcTitle(char **argv, const char *newTitle) +{ + if (strlen(newTitle) > strlen(argv[0])) { + return; + } + if (strcpy_s(argv[0], strlen(argv[0]) + 1, newTitle) != EOK) { + return; + } + prctl(PR_SET_NAME, newTitle); +} + +int main(int argc, char **argv) +{ + if (argc != DEVHOST_INPUT_PARAM_NUM) { + HDF_LOGE("Devhost main parameter error, argc: %{public}d", argc); + return HDF_ERR_INVALID_PARAM; + } + + int hostId = 0; + if (!HdfStringToInt(argv[DEVHOST_INPUT_PARAM_HOSTID_POS], &hostId)) { + HDF_LOGE("Devhost main parameter error, argv[1]: %{public}s", argv[DEVHOST_INPUT_PARAM_HOSTID_POS]); + return HDF_ERR_INVALID_PARAM; + } + const char *hostName = argv[argc - 1]; + HDF_LOGD("hdf device host %{public}s start", hostName); + SetProcTitle(argv, hostName); + + struct IDevHostService *instance = DevHostServiceNewInstance(hostId, hostName); + if (instance == NULL || instance->StartService == NULL) { + HDF_LOGE("DevHostServiceGetInstance fail"); + return HDF_ERR_INVALID_OBJECT; + } + int status = instance->StartService(instance); + if (status != HDF_SUCCESS) { + HDF_LOGE("Devhost StartService fail, return: %{public}d", status); + DevHostServiceFreeInstance(instance); + return status; + } + struct DevHostServiceFull *fullService = (struct DevHostServiceFull*)instance; + struct HdfMessageLooper *looper = &fullService->looper; + if ((looper != NULL) && (looper->Start != NULL)) { + looper->Start(looper); + } + + DevHostServiceFreeInstance(instance); + return status; +} + diff --git a/uhdf2/host/include/devhost_service_full.h b/uhdf2/host/include/devhost_service_full.h new file mode 100644 index 0000000..45a9cac --- /dev/null +++ b/uhdf2/host/include/devhost_service_full.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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 DEVHOST_SERVICE_FULL_H +#define DEVHOST_SERVICE_FULL_H +#include "devhost_service.h" +#include "hdf_message_task.h" +#include "hdf_message_looper.h" +#include "hdf_device_info.h" +#include "osal_sysevent.h" + +struct DevHostServiceFull { + struct DevHostService super; + struct HdfMessageTask task; + struct HdfMessageLooper looper; + struct HdfSysEventNotifyNode sysEventNotifyNode; +}; + +enum { + DEVHOST_MESSAGE_ADD_DEVICE = 1, + DEVHOST_MESSAGE_DEL_DEVICE, +}; + +void DevHostServiceFullConstruct(struct DevHostServiceFull *inst); +void DevHostServiceFullDestruct(struct DevHostServiceFull *inst); + +#endif /* DEVHOST_SERVICE_FULL_H */ diff --git a/uhdf2/host/include/devhost_service_stub.h b/uhdf2/host/include/devhost_service_stub.h new file mode 100644 index 0000000..c994cb1 --- /dev/null +++ b/uhdf2/host/include/devhost_service_stub.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 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 DEVHOST_SERVICE_STUB_H +#define DEVHOST_SERVICE_STUB_H + +#include "hdf_remote_service.h" +#include "devhost_service_full.h" +#include "osal_mutex.h" + +struct DevHostServiceStub { + struct DevHostServiceFull super; + struct HdfRemoteService *remote; + struct OsalMutex hostSvcMutex; +}; + +struct HdfObject *DevHostServiceStubCreate(void); +void DevHostServiceStubRelease(struct HdfObject *object); + +#endif /* DEVHOST_SERVICE_STUB_H */ diff --git a/uhdf2/host/include/device_service_stub.h b/uhdf2/host/include/device_service_stub.h new file mode 100644 index 0000000..d837443 --- /dev/null +++ b/uhdf2/host/include/device_service_stub.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 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 DEVICE_SERVICE_STUB_H +#define DEVICE_SERVICE_STUB_H + +#include "hdf_remote_service.h" +#include "hdf_device_node.h" + +struct IRemoteService { + struct HdfObject object; + int (*Dispatch)(int code, struct HdfSBuf *data, struct HdfSBuf *reply); +}; + +struct DeviceServiceStub { + struct HdfDeviceNode super; + struct HdfRemoteService *remote; +}; + +enum { + IFACE_OBJECT_SERVICE = 0, + IFACE_REMOTE_SERVICE +}; + +struct HdfObject *DeviceServiceStubCreate(void); +void DeviceServiceStubRelease(struct HdfObject *object); + +#endif /* DEVICE_SERVICE_STUB_H */ diff --git a/uhdf2/host/include/device_token_stub.h b/uhdf2/host/include/device_token_stub.h new file mode 100644 index 0000000..33568f1 --- /dev/null +++ b/uhdf2/host/include/device_token_stub.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 DEVICE_TOKEN_STUB_H +#define DEVICE_TOKEN_STUB_H + +#include "hdf_remote_service.h" +#include "hdf_device_token.h" + +struct DeviceTokenStub { + struct HdfDeviceToken super; + struct HdfRemoteService *remote; +}; + +struct HdfObject *DeviceTokenStubCreate(void); +void DeviceTokenStubRelease(struct HdfObject *object); + +#endif /* DEVICE_TOKEN_STUB_H */ diff --git a/uhdf2/host/include/devmgr_service_proxy.h b/uhdf2/host/include/devmgr_service_proxy.h new file mode 100644 index 0000000..de7e379 --- /dev/null +++ b/uhdf2/host/include/devmgr_service_proxy.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 DEVMGR_SERVICE_PROXY_H +#define DEVMGR_SERVICE_PROXY_H + +#include "hdf_remote_service.h" +#include "devmgr_service_if.h" + +struct DevmgrServiceProxy { + struct IDevmgrService super; + struct HdfRemoteService *remote; +}; + +struct HdfObject *DevmgrServiceProxyCreate(void); +void DevmgrServiceProxyRelease(struct HdfObject *object); + +#endif /* DEVMGR_SERVICE_PROXY_H */ diff --git a/uhdf2/host/include/devsvc_manager_proxy.h b/uhdf2/host/include/devsvc_manager_proxy.h new file mode 100644 index 0000000..89c0a5e --- /dev/null +++ b/uhdf2/host/include/devsvc_manager_proxy.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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 DEVSVC_MANAGER_PROXY_H +#define DEVSVC_MANAGER_PROXY_H + +#include "hdf_remote_service.h" +#include "devsvc_manager_if.h" + +struct DevSvcManagerProxy { + struct IDevSvcManager pvtbl; + struct HdfDeathRecipient recipient; + struct HdfRemoteService *remote; + uint32_t hostId; +}; + +enum { + DEVSVC_MANAGER_ADD_SERVICE = 1, + DEVSVC_MANAGER_GET_SERVICE = 2, + DEVSVC_MANAGER_REMOVE_SERVICE, + DEVSVC_MANAGER_GET_DEVICE_LIST +}; + +struct HdfObject *DevSvcManagerProxyCreate(void); +void DevSvcManagerProxyRelease(struct HdfObject *object); +#endif /* DEVSVC_MANAGER_PROXY_H */ diff --git a/uhdf2/host/include/driver_loader_full.h b/uhdf2/host/include/driver_loader_full.h new file mode 100644 index 0000000..9c91c97 --- /dev/null +++ b/uhdf2/host/include/driver_loader_full.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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 DRIVER_LOADER_FULL_H +#define DRIVER_LOADER_FULL_H + +#include "hdf_driver_loader.h" + +struct DriverLoaderFull { + struct HdfDriverLoader super; +}; + +struct HdfObject *HdfDriverLoaderFullCreate(void); +void HdfDriverLoaderFullRelease(struct HdfObject *object); + +#endif /* DRIVER_LOADER_FULL_H */ diff --git a/uhdf2/host/include/hdf_device_full.h b/uhdf2/host/include/hdf_device_full.h new file mode 100644 index 0000000..6559e33 --- /dev/null +++ b/uhdf2/host/include/hdf_device_full.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 HDF_DEVICE_FULL_H +#define HDF_DEVICE_FULL_H + +#include "hdf_device.h" +#include "hdf_device_thread.h" + +struct HdfDeviceFull { + struct HdfDevice super; + struct DeviceThread *deviceThread; +}; + +struct HdfObject *HdfDeviceFullCreate(void); +void HdfDeviceFullRelease(struct HdfObject *object); + +#endif /* HDF_DEVICE_FULL_H */ diff --git a/uhdf2/host/include/hdf_device_thread.h b/uhdf2/host/include/hdf_device_thread.h new file mode 100644 index 0000000..0f36912 --- /dev/null +++ b/uhdf2/host/include/hdf_device_thread.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 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 DEVICE_THREAD_H +#define DEVICE_THREAD_H + +#include "hdf_thread_ex.h" +#include "hdf_message_task.h" +#include "hdf_message_looper.h" +#include "hdf_device_info.h" +#include "hdf_device_node.h" + +struct DeviceThread { + struct HdfThread super; + struct HdfMessageTask task; + struct HdfDeviceInfo *attribute; + struct HdfMessageLooper looper; +}; + +enum { + DEVICE_SERVICE_MESSAGE_LAUNCH, + DEVICE_SERVICE_MESSAGE_SUSPEND, + DEVICE_SERVICE_MESSAGE_RESUME +}; + +struct DeviceThread *DeviceThreadNewInstance(void); +void DeviceThreadFreeInstance(struct DeviceThread *thread); +void DeviceThreadMain(void *args); +void DeviceThreadAttach(struct DeviceThread *inst, struct IHdfDevice *device, struct HdfDeviceNode *service); + +#endif /* DEVICE_THREAD_H */ diff --git a/uhdf2/host/src/devhost_object_config.c b/uhdf2/host/src/devhost_object_config.c new file mode 100644 index 0000000..a5a45c2 --- /dev/null +++ b/uhdf2/host/src/devhost_object_config.c @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devhost_service_stub.h" +#include "device_token_stub.h" +#include "devmgr_service_proxy.h" +#include "devsvc_manager_proxy.h" +#include "driver_loader_full.h" +#include "hdf_device_full.h" +#include "device_service_stub.h" +#include "hdf_log.h" +#include "hdf_object_manager.h" + +#define HDF_LOG_TAG devhost_object_config + +static const struct HdfObjectCreator g_fullDevHostObjectCreators[] = { + [HDF_OBJECT_ID_DEVMGR_SERVICE] = + { + .Create = DevmgrServiceProxyCreate, + .Release = DevmgrServiceProxyRelease, + }, + [HDF_OBJECT_ID_DEVSVC_MANAGER] = + { + .Create = DevSvcManagerProxyCreate, + .Release = DevSvcManagerProxyRelease, + }, + [HDF_OBJECT_ID_DEVHOST_SERVICE] = + { + .Create = DevHostServiceStubCreate, + .Release = DevHostServiceStubRelease, + }, + [HDF_OBJECT_ID_DRIVER_LOADER] = + { + .Create = HdfDriverLoaderFullCreate, + .Release = HdfDriverLoaderFullRelease, + }, + [HDF_OBJECT_ID_DEVICE] = + { + .Create = HdfDeviceFullCreate, + .Release = HdfDeviceFullRelease, + }, + [HDF_OBJECT_ID_DEVICE_TOKEN] = + { + .Create = DeviceTokenStubCreate, + .Release = DeviceTokenStubRelease, + }, + [HDF_OBJECT_ID_DEVICE_SERVICE] = + { + .Create = DeviceServiceStubCreate, + .Release = DeviceServiceStubRelease, + } +}; + +const struct HdfObjectCreator *HdfObjectManagerGetCreators(int objectId) +{ + int numConfigs = sizeof(g_fullDevHostObjectCreators) / sizeof(g_fullDevHostObjectCreators[0]); + if (objectId < 0 || objectId >= numConfigs) { + HDF_LOGE("Invalid objectId: %d", objectId); + return NULL; + } + return &g_fullDevHostObjectCreators[objectId]; +} + diff --git a/uhdf2/host/src/devhost_service_full.c b/uhdf2/host/src/devhost_service_full.c new file mode 100644 index 0000000..7116c0f --- /dev/null +++ b/uhdf2/host/src/devhost_service_full.c @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devhost_service_full.h" +#include "devmgr_service_clnt.h" +#include "hdf_device_node.h" +#include "power_state_token.h" +#include "hdf_base.h" +#include "hdf_device_info.h" +#include "hdf_log.h" +#include "osal_message.h" +#include "hdf_device_info_full.h" + +#define HDF_LOG_TAG devhost_service_full + +static int32_t DevHostServiceFullDispatchMessage(struct HdfMessageTask *task, struct HdfMessage *msg) +{ + struct DevHostServiceFull *hostService = + HDF_SLIST_CONTAINER_OF(struct HdfMessageTask, task, struct DevHostServiceFull, task); + + int status = HDF_SUCCESS; + switch (msg->messageId) { + case DEVHOST_MESSAGE_ADD_DEVICE: { + struct HdfDeviceInfo *attribute = (struct HdfDeviceInfo *)msg->data[0]; + status = DevHostServiceAddDevice(&hostService->super.super, attribute); + if (status != HDF_SUCCESS) { + HDF_LOGE("DevHostServiceAddDevice failed and return %d", status); + } + break; + } + case DEVHOST_MESSAGE_DEL_DEVICE: { + struct HdfDeviceInfo *attribute = (struct HdfDeviceInfo *)msg->data[0]; + status = DevHostServiceDelDevice(&hostService->super.super, attribute); + if (status != HDF_SUCCESS) { + HDF_LOGE("DevHostServiceDelDevice failed and return %d", status); + } + HdfDeviceInfoFullFreeInstance((struct HdfDeviceInfoFull *)attribute); + break; + } + default: { + HDF_LOGE("DevHostServiceFullDispatchMessage unknown message %d", msg->messageId); + break; + } + } + + return status; +} + +static int DevHostServiceFullOpsDevice( + struct IDevHostService *devHostService, const struct HdfDeviceInfo *attribute, int cmdCode) +{ + if (devHostService == NULL || attribute == NULL) { + HDF_LOGE("input is null"); + return HDF_FAILURE; + } + struct DevHostServiceFull *inst = (struct DevHostServiceFull *)devHostService; + struct HdfMessageTask *task = &inst->task; + struct HdfMessage *message = HdfMessageObtain(0); + if (message == NULL) { + HDF_LOGE("HdfMessageObtain(0) return null"); + return HDF_ERR_MALLOC_FAIL; + } + + message->messageId = cmdCode; + message->data[0] = (void *)attribute; + return task->SendMessage(task, message, true); +} + +static int DevHostServiceFullAddDevice( + struct IDevHostService *devHostService, const struct HdfDeviceInfo *attribute) +{ + return DevHostServiceFullOpsDevice(devHostService, attribute, DEVHOST_MESSAGE_ADD_DEVICE); +} + +static int DevHostServiceFullDelDevice( + struct IDevHostService *devHostService, const struct HdfDeviceInfo *attribute) +{ + return DevHostServiceFullOpsDevice(devHostService, attribute, DEVHOST_MESSAGE_DEL_DEVICE); +} + +static int DispatchSysEventToDevice(struct HdfDevice *device, uint32_t event) +{ + struct HdfSListIterator deviceNodeIt; + struct HdfDeviceNode *deviceNode = NULL; + int ret = HDF_SUCCESS; + + HdfSListIteratorInit(&deviceNodeIt, &device->services); + while (HdfSListIteratorHasNext(&deviceNodeIt)) { + deviceNode = HDF_SLIST_CONTAINER_OF(struct HdfSListNode, HdfSListIteratorNext(&deviceNodeIt), + struct HdfDeviceNode, entry); + if (deviceNode->powerToken != NULL) { + ret = PowerStateOnSysStateChange(deviceNode->powerToken, event); + } + } + + return ret; +} + +static int OnSysEventReceived(struct HdfSysEventNotifyNode *self, uint64_t eventClass, + uint32_t event, const char* content) +{ + (void)(content); + struct HdfDevice *device = NULL; + + if (self == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct DevHostService *hostService = CONTAINER_OF(self, struct DevHostService, sysEventNotifyNode); + HDF_LOGI("host receive eventClass=%llu, event=%u", (unsigned long long)eventClass, event); + + if (HdfPmIsWakeEvent(event)) { + DLIST_FOR_EACH_ENTRY_REVERSE(device, &hostService->devices, struct HdfDevice, node) { + DispatchSysEventToDevice(device, event); + } + } else { + DLIST_FOR_EACH_ENTRY(device, &hostService->devices, struct HdfDevice, node) { + DispatchSysEventToDevice(device, event); + } + } + + return HDF_SUCCESS; +} + +static int DevHostServiceFullStartService(struct IDevHostService *service) +{ + struct DevHostService *hostService = (struct DevHostService*)service; + if (hostService == NULL) { + HDF_LOGE("Start device service failed, hostService is null"); + return HDF_FAILURE; + } + + int ret = DevmgrServiceClntAttachDeviceHost(hostService->hostId, service); + if (ret != HDF_SUCCESS) { + return ret; + } + + hostService->sysEventNotifyNode.callback = OnSysEventReceived; + ret = HdfSysEventNotifyRegister(&hostService->sysEventNotifyNode, HDF_SYSEVENT_CLASS_POWER); + if (ret != HDF_SUCCESS) { + HDF_LOGW("failed to register power event listener"); + } else { + HDF_LOGD("host register power event listener success"); + } + + return HDF_SUCCESS; +} + +void DevHostServiceFullConstruct(struct DevHostServiceFull *inst) +{ + struct IDevHostService *hostServiceIf = &inst->super.super; + static struct IHdfMessageHandler handler = { + .Dispatch = DevHostServiceFullDispatchMessage + }; + DevHostServiceConstruct(&inst->super); + hostServiceIf->AddDevice = DevHostServiceFullAddDevice; + hostServiceIf->DelDevice = DevHostServiceFullDelDevice; + hostServiceIf->StartService = DevHostServiceFullStartService; + HdfMessageLooperConstruct(&inst->looper); + HdfMessageTaskConstruct(&inst->task, &inst->looper, &handler); +} + +void DevHostServiceFullDestruct(struct DevHostServiceFull *inst) +{ + if (inst != NULL) { + DevHostServiceDestruct(&inst->super); + if (inst->looper.Stop != NULL) { + inst->looper.Stop(&inst->looper); + } + } +} diff --git a/uhdf2/host/src/devhost_service_stub.c b/uhdf2/host/src/devhost_service_stub.c new file mode 100644 index 0000000..be28654 --- /dev/null +++ b/uhdf2/host/src/devhost_service_stub.c @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devhost_service_stub.h" +#include "devhost_service_proxy.h" +#include "dev_attribute_parcel.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "hdf_security.h" +#include "osal_mem.h" +#include "unistd.h" + +#define HDF_LOG_TAG devhost_service_stub + +static void DevHostSetCurrentSecurec(const char *hostName) +{ + (void) hostName; +} + +static int DevHostServiceStubDispatch( + struct HdfRemoteService *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + (void)reply; + int ret = HDF_FAILURE; + if (stub == NULL || data == NULL) { + return ret; + } + struct DevHostServiceStub *serviceStub = (struct DevHostServiceStub *)stub; + struct IDevHostService *serviceIf = (struct IDevHostService *)&serviceStub->super; + DevHostSetCurrentSecurec(serviceStub->super.super.hostName); + OsalMutexLock(&serviceStub->hostSvcMutex); + switch (code) { + case DEVHOST_SERVICE_ADD_DEVICE: { + if ((serviceIf == NULL) || (serviceIf->AddDevice == NULL)) { + HDF_LOGE("serviceIf or serviceIf->AddDevice is NULL"); + break; + } + struct HdfDeviceInfoFull *attribute = DeviceAttributeFullRead(data); + if (attribute == NULL) { + HDF_LOGE("Dispatch failed, attribute is null"); + break; + } + ret = serviceIf->AddDevice(serviceIf, &attribute->super); + if (ret != HDF_SUCCESS) { + HDF_LOGE("Dispatch failed, add service failed and ret is %d", ret); + } + break; + } + case DEVHOST_SERVICE_DEL_DEVICE: { + if ((serviceIf == NULL) || (serviceIf->DelDevice == NULL)) { + HDF_LOGE("serviceIf or serviceIf->DelDevice is NULL"); + break; + } + struct HdfDeviceInfoFull *attribute = DeviceAttributeFullRead(data); + if (attribute == NULL) { + HDF_LOGE("Dispatch failed, attribute is null"); + break; + } + ret = serviceIf->DelDevice(serviceIf, &attribute->super); + if (ret != HDF_SUCCESS) { + HDF_LOGE("Dispatch failed, del service failed and ret is %d", ret); + } + break; + } + default: { + HDF_LOGE("DevHostServiceStubDispatch unknown code:%d", code); + break; + } + } + OsalMutexUnlock(&serviceStub->hostSvcMutex); + return ret; +} + + +static void DevHostServiceStubConstruct(struct DevHostServiceStub *inst) +{ + static struct HdfRemoteDispatcher dispatcher = { + .Dispatch = DevHostServiceStubDispatch + }; + + DevHostServiceFullConstruct(&inst->super); + inst->remote = HdfRemoteServiceObtain((struct HdfObject *)inst, &dispatcher); + + OsalMutexInit(&inst->hostSvcMutex); +} + +struct HdfObject *DevHostServiceStubCreate(void) +{ + struct DevHostServiceStub *instance = + (struct DevHostServiceStub *)OsalMemCalloc(sizeof(struct DevHostServiceStub)); + if (instance != NULL) { + DevHostServiceStubConstruct(instance); + return (struct HdfObject *)&instance->super; + } + return NULL; +} + + +void DevHostServiceStubRelease(struct HdfObject *object) +{ + struct DevHostServiceStub *instance = (struct DevHostServiceStub *)object; + if (instance != NULL) { + DevHostServiceFullDestruct(&instance->super); + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + OsalMutexDestroy(&instance->hostSvcMutex); + OsalMemFree(instance); + } +} + diff --git a/uhdf2/host/src/device_service_stub.c b/uhdf2/host/src/device_service_stub.c new file mode 100644 index 0000000..696d9a3 --- /dev/null +++ b/uhdf2/host/src/device_service_stub.c @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_sbuf.h" +#include "device_service_stub.h" +#include "devsvc_manager_clnt.h" +#include "hdf_base.h" +#include "osal_mem.h" +#include "hdf_log.h" + +int DeviceServiceStubDispatch( + struct HdfRemoteService *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct DeviceServiceStub *service = (struct DeviceServiceStub *)stub; + struct IDeviceIoService *ioService = service->super.deviceObject.service; + int ret = HDF_FAILURE; + + if (ioService == NULL) { + return HDF_FAILURE; + } + + struct HdfDeviceIoClient client = { + .device = &service->super.deviceObject, + .priv = NULL, + }; + + if (ioService->Dispatch != NULL) { + ret = ioService->Dispatch(&client, code, data, reply); + } + return ret; +} + +static struct HdfRemoteDispatcher g_deviceServiceDispatcher = { + .Dispatch = DeviceServiceStubDispatch +}; + +int DeviceServiceStubPublishService(struct HdfDeviceNode *service, const char *serviceName) +{ + int status = HDF_FAILURE; + struct DeviceServiceStub *fullService = (struct DeviceServiceStub *)service; + const struct HdfDeviceInfo *deviceInfo = service->deviceInfo; + + if (fullService->remote != NULL) { + HDF_LOGE("%s:service %s already published", __func__, serviceName); + return HDF_ERR_INVALID_OBJECT; + } + + if (deviceInfo->policy == SERVICE_POLICY_PUBLIC || deviceInfo->policy == SERVICE_POLICY_CAPACITY) { + fullService->remote = HdfRemoteServiceObtain((struct HdfObject *)fullService, &g_deviceServiceDispatcher); + if (fullService->remote == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + struct DevSvcManagerClnt *serviceManager = + (struct DevSvcManagerClnt *)DevSvcManagerClntGetInstance(); + if (serviceManager != NULL) { + status = DevSvcManagerClntAddService(serviceName, &fullService->super.deviceObject); + } + } + + return status; +} + +void DeviceServiceStubConstruct(struct DeviceServiceStub *inst) +{ + HdfDeviceNodeConstruct(&inst->super); + struct IDeviceNode *serviceIf = (struct IDeviceNode *)inst; + if (serviceIf != NULL) { + serviceIf->PublishService = DeviceServiceStubPublishService; + } +} + +struct HdfObject *DeviceServiceStubCreate() +{ + struct DeviceServiceStub *instance = + (struct DeviceServiceStub *)OsalMemCalloc(sizeof(struct DeviceServiceStub)); + if (instance != NULL) { + DeviceServiceStubConstruct(instance); + } + return (struct HdfObject *)instance; +} + +void DeviceServiceStubRelease(struct HdfObject *object) +{ + struct DeviceServiceStub *instance = (struct DeviceServiceStub *)object; + if (instance != NULL) { + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + HdfDeviceNodeDestruct(&instance->super); + OsalMemFree(instance); + } +} + diff --git a/uhdf2/host/src/device_token_stub.c b/uhdf2/host/src/device_token_stub.c new file mode 100644 index 0000000..52ccb62 --- /dev/null +++ b/uhdf2/host/src/device_token_stub.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "osal_mem.h" +#include "device_token_stub.h" +#include "hdf_base.h" +#include "hdf_log.h" + +#define HDF_LOG_TAG device_token_stub + +int DeviceTokenStubDispatch( + struct HdfObject *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + (void)code; + (void)data; + (void)reply; + struct DeviceTokenStub *tokenStub = (struct DeviceTokenStub *)stub; + struct IDevHostService *super = (struct IDevHostService *)&tokenStub->super; + if (super == NULL) { + HDF_LOGE("Dispatch failed, super is null"); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +void DeviceTokenStubConstruct(struct DeviceTokenStub *inst) +{ + inst->remote = NULL; +} + +struct HdfObject *DeviceTokenStubCreate() +{ + struct DeviceTokenStub *instance = + (struct DeviceTokenStub *)OsalMemCalloc(sizeof(struct DeviceTokenStub)); + if (instance != NULL) { + DeviceTokenStubConstruct(instance); + } + return (struct HdfObject *)instance; +} + +void DeviceTokenStubRelease(struct HdfObject *object) +{ + struct DeviceTokenStub *instance = (struct DeviceTokenStub *)object; + if (instance != NULL) { + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + OsalMemFree(instance); + } +} + diff --git a/uhdf2/host/src/devmgr_service_proxy.c b/uhdf2/host/src/devmgr_service_proxy.c new file mode 100644 index 0000000..2b21184 --- /dev/null +++ b/uhdf2/host/src/devmgr_service_proxy.c @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_service_proxy.h" +#include "devhost_service_stub.h" +#include "devmgr_service_stub.h" +#include "device_token_stub.h" +#include "devmgr_service.h" +#include "devsvc_manager_clnt.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG devmgr_service_proxy + +int DevmgrServiceProxyAttachDeviceHost( + struct IDevmgrService *inst, uint16_t hostId, struct IDevHostService *service) +{ + int status = HDF_FAILURE; + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + struct HdfRemoteDispatcher *dipatcher = NULL; + struct HdfRemoteService *remoteService = NULL; + struct DevmgrServiceProxy *serviceProxy = (struct DevmgrServiceProxy *)inst; + struct DevHostServiceStub *hostStub = (struct DevHostServiceStub *)service; + if ((serviceProxy->remote == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("DevmgrServiceProxyAttachDeviceHost failed, host id is %u", hostId); + goto finished; + } + remoteService = serviceProxy->remote; + dipatcher = remoteService->dispatcher; + HdfSbufWriteInt32(data, hostId); + HdfSBufWriteRemoteService(data, hostStub->remote); + status = dipatcher->Dispatch(remoteService, DEVMGR_SERVICE_ATTACH_DEVICE_HOST, data, reply); + HDF_LOGI("Attach device host dispatch finish, status is %d", status); +finished: + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } + return status; +} + +int DevmgrServiceProxyAttachDevice( + struct IDevmgrService *inst, const struct HdfDeviceInfo *deviceInfo, struct IHdfDeviceToken *token) +{ + int status = HDF_FAILURE; + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + struct HdfRemoteDispatcher *dipatcher = NULL; + struct HdfRemoteService *remoteService = NULL; + struct DevmgrServiceProxy *serviceProxy = (struct DevmgrServiceProxy *)inst; + struct DeviceTokenStub *tokenStub = (struct DeviceTokenStub *)token; + if ((serviceProxy == NULL) || (serviceProxy->remote == NULL) || (deviceInfo == NULL) || (data == NULL) || + (reply == NULL) || (tokenStub == NULL)) { + HDF_LOGE("DevmgrServiceProxyAttachDevice failed"); + goto finished; + } + remoteService = serviceProxy->remote; + dipatcher = remoteService->dispatcher; + HdfSbufWriteInt32(data, deviceInfo->hostId); + HdfSbufWriteInt32(data, deviceInfo->deviceId); + status = dipatcher->Dispatch(remoteService, DEVMGR_SERVICE_ATTACH_DEVICE, data, reply); +finished: + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } + return status; +} + +static void DevmgrServiceProxyConstruct(struct DevmgrServiceProxy *inst) +{ + struct IDevmgrService *pvtbl = (struct IDevmgrService *)inst; + pvtbl->AttachDeviceHost = DevmgrServiceProxyAttachDeviceHost; + pvtbl->AttachDevice = DevmgrServiceProxyAttachDevice; + pvtbl->StartService = NULL; +} + + +static struct IDevmgrService *DevmgrServiceProxyObtain(struct HdfRemoteService *service) +{ + if (service != NULL) { + struct DevmgrServiceProxy *demgrServicProxy = + (struct DevmgrServiceProxy *)OsalMemCalloc(sizeof(struct DevmgrServiceProxy)); + if (demgrServicProxy != NULL) { + demgrServicProxy->remote = service; + DevmgrServiceProxyConstruct(demgrServicProxy); + return &demgrServicProxy->super; + } + } + HDF_LOGE("DevmgrServiceProxyObtain failed"); + return NULL; +} + +struct HdfObject *DevmgrServiceProxyCreate() +{ + static struct IDevmgrService *instance = NULL; + if (instance == NULL) { + struct IDevSvcManager *serviceManagerIf = NULL; + struct DevSvcManagerClnt *serviceManager = (struct DevSvcManagerClnt *)DevSvcManagerClntGetInstance(); + if ((serviceManager == NULL) || (serviceManager->devSvcMgrIf == NULL)) { + HDF_LOGE("Fail to Create Service Manager Client"); + return NULL; + } + serviceManagerIf = serviceManager->devSvcMgrIf; + if (serviceManagerIf->GetService == NULL) { + HDF_LOGE("Get Service is not implement!!!"); + return NULL; + } else { + struct HdfRemoteService *remote = (struct HdfRemoteService *) + serviceManagerIf->GetService(serviceManagerIf, DEVICE_MANAGER_SERVICE); + if (remote != NULL) { + instance = DevmgrServiceProxyObtain(remote); + } + } + } + return (struct HdfObject *)instance; +} + +void DevmgrServiceProxyRelease(struct HdfObject *object) +{ + struct DevmgrServiceProxy *instance = (struct DevmgrServiceProxy *)object; + if (instance != NULL) { + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + OsalMemFree(instance); + } +} + +int DevmgrServiceLoadDevice(const char *svcName) +{ + (void)svcName; + return HDF_SUCCESS; +} + +int DevmgrServiceUnLoadDevice(const char *svcName) +{ + (void)svcName; + return HDF_SUCCESS; +} diff --git a/uhdf2/host/src/devsvc_manager_proxy.c b/uhdf2/host/src/devsvc_manager_proxy.c new file mode 100644 index 0000000..55523d0 --- /dev/null +++ b/uhdf2/host/src/devsvc_manager_proxy.c @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devsvc_manager_proxy.h" +#include "device_service_stub.h" +#include "devsvc_manager_stub.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG devsvc_manager_proxy + +static int DevSvcManagerProxyAddService( + struct IDevSvcManager *inst, const char *svcName, struct HdfDeviceObject *service) +{ + struct DevSvcManagerProxy *serviceProxy = (struct DevSvcManagerProxy *)inst; + if (service == NULL || svcName == NULL) { + HDF_LOGE("%s:service or name is null", __func__); + return HDF_ERR_INVALID_PARAM; + } + if ((serviceProxy == NULL) || (serviceProxy->remote == NULL)) { + HDF_LOGE("Add service failed, serviceProxy is invalid"); + return HDF_ERR_INVALID_PARAM; + } + + int status = HDF_FAILURE; + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + struct HdfRemoteDispatcher *dispatcher = NULL; + struct HdfRemoteService *remoteService = NULL; + do { + if ((data == NULL) || (reply == NULL)) { + HDF_LOGE("Add service failed, failed to obtain sbuf"); + break; + } + + remoteService = serviceProxy->remote; + dispatcher = remoteService->dispatcher; + if (!HdfSbufWriteString(data, svcName)) { + HDF_LOGE("Add service failed, failed to write service name"); + break; + } + + struct HdfDeviceNode *devNode = HDF_SLIST_CONTAINER_OF( + struct HdfDeviceObject, service, struct HdfDeviceNode, deviceObject); + struct DeviceServiceStub *deviceFullService = (struct DeviceServiceStub *)devNode; + if (deviceFullService->remote == NULL) { + HDF_LOGE("%s: device service is broken", __func__); + break; + } + + if (HdfSBufWriteRemoteService(data, deviceFullService->remote) != HDF_SUCCESS) { + HDF_LOGE("Add service failed, failed to write remote object"); + break; + } + status = dispatcher->Dispatch(remoteService, DEVSVC_MANAGER_ADD_SERVICE, data, reply); + HDF_LOGI("servmgr add service %{public}s, result is %{public}d", svcName, status); + } while(0); + + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } + return status; +} + +struct HdfObject *DevSvcManagerProxyGetService(struct IDevSvcManager *inst, const char *svcName) +{ + int status = HDF_FAILURE; + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + struct HdfRemoteDispatcher *dispatcher = NULL; + struct HdfRemoteService *remoteService = NULL; + struct DevSvcManagerProxy *serviceProxy = (struct DevSvcManagerProxy *) inst; + if ((serviceProxy->remote == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("Get service failed, serviceProxy->remote or data or reply is null"); + goto finished; + } + dispatcher = serviceProxy->remote->dispatcher; + HdfSbufWriteString(data, svcName); + status = dispatcher->Dispatch(serviceProxy->remote, DEVSVC_MANAGER_GET_SERVICE, data, reply); + if (status == HDF_SUCCESS) { + remoteService = HdfSBufReadRemoteService(reply); + } +finished: + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } + HDF_LOGI("DevSvcManagerProxyGetService finish, and status is %d", status); + return (remoteService == NULL) ? NULL : &remoteService->object_; +} + +void DevSvcManagerProxyRemoveService(struct IDevSvcManager *inst, const char *svcName) +{ + if (inst == NULL || svcName == NULL) { + return; + } + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + struct HdfRemoteDispatcher *dispatcher = NULL; + struct HdfRemoteService *remoteService = NULL; + struct DevSvcManagerProxy *serviceProxy = (struct DevSvcManagerProxy *) inst; + if ((serviceProxy->remote == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("Remove service failed, serviceProxy->remote or data or reply is null"); + goto finished; + } + remoteService = serviceProxy->remote; + dispatcher = remoteService->dispatcher; + HdfSbufWriteString(data, svcName); + int status = dispatcher->Dispatch(remoteService, DEVSVC_MANAGER_REMOVE_SERVICE, data, reply); + HDF_LOGD("Device service manager proxy remove service status is %d", status); +finished: + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } +} + +void DevSvcManagerProxyConstruct(struct DevSvcManagerProxy *inst, struct HdfRemoteService *remote) +{ + inst->pvtbl.AddService = DevSvcManagerProxyAddService; + inst->pvtbl.GetService = DevSvcManagerProxyGetService; + inst->pvtbl.RemoveService = DevSvcManagerProxyRemoveService; + inst->remote = remote; +} + +static struct IDevSvcManager *DevSvcManagerProxyObtain(struct HdfRemoteService *remote) +{ + struct DevSvcManagerProxy *instance = + (struct DevSvcManagerProxy *)OsalMemCalloc(sizeof(struct DevSvcManagerProxy)); + if (instance != NULL) { + DevSvcManagerProxyConstruct(instance, remote); + } + return (struct IDevSvcManager *)instance; +} + +struct HdfObject *DevSvcManagerProxyCreate() +{ + static struct IDevSvcManager *instance = NULL; + if (instance == NULL) { + struct HdfRemoteService *remote = HdfRemoteServiceGet(DEVICE_SERVICE_MANAGER_SA_ID); + if (remote != NULL) { + instance = DevSvcManagerProxyObtain(remote); + } + } + return (struct HdfObject *)instance; +} + +void DevSvcManagerProxyRelease(struct HdfObject *object) +{ + struct DevSvcManagerProxy *instance = (struct DevSvcManagerProxy *)object; + if (instance != NULL) { + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + OsalMemFree(instance); + } +} + diff --git a/uhdf2/host/src/driver_loader_full.c b/uhdf2/host/src/driver_loader_full.c new file mode 100644 index 0000000..1027b07 --- /dev/null +++ b/uhdf2/host/src/driver_loader_full.c @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "driver_loader_full.h" +#include +#include +#include +#include +#include +#include "hdf_device.h" +#include "hdf_device_info_full.h" +#include "hdf_device_node.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "securec.h" + +#define DRIVER_DESC "driverDesc" +#define HDF_LOG_TAG driver_loader_full +#ifdef __OHOS_STANDARD_SYS__ +#define DRIVER_PATH "/system/lib/" +#else +#define DRIVER_PATH "/system/lib64/" +#endif + +static struct DriverLoaderFull *g_fullLoader = NULL; + +struct HdfDriverEntry *HdfDriverLoaderGetDriverEntry(const struct HdfDeviceInfo *deviceInfo) +{ + void *deviceHandle = NULL; + struct HdfDriverEntry **deviceEntry = NULL; + struct HdfDeviceInfoFull *fullAttribute = (struct HdfDeviceInfoFull *)deviceInfo; + char realPath[PATH_MAX] = { 0 }; + char driverPath[PATH_MAX] = { 0 }; + if (deviceInfo == NULL || deviceInfo->moduleName == NULL) { + return NULL; + } + + if (strcat_s(driverPath, sizeof(driverPath) - 1, DRIVER_PATH) != EOK) { + HDF_LOGE("%s get driver path failed", __func__); + return NULL; + } + + if (strcat_s(driverPath, (sizeof(driverPath) - 1 - sizeof(DRIVER_PATH)), deviceInfo->moduleName) != EOK) { + HDF_LOGE("%s get full driver path failed", __func__); + return NULL; + } + + if (realpath(driverPath, realPath) == NULL) { + HDF_LOGE("%{public}s no valid, errno:%{public}d", driverPath, errno); + return NULL; + } + deviceHandle = dlopen(realPath, RTLD_LAZY); + if (deviceHandle == NULL) { + HDF_LOGE("Get device entry failed, %s load fail", realPath); + return NULL; + } + fullAttribute->deviceHandle = deviceHandle; + deviceEntry = (struct HdfDriverEntry **)dlsym(deviceHandle, DRIVER_DESC); + if (deviceEntry == NULL) { + HDF_LOGE("Get device entry failed, dlsym failed"); + dlclose(deviceHandle); + fullAttribute->deviceHandle = NULL; + return NULL; + } + return *deviceEntry; +} + +void HdfDriverLoaderFullConstruct(struct DriverLoaderFull *inst) +{ + struct HdfDriverLoader *pvtbl = (struct HdfDriverLoader *)inst; + HdfDriverLoaderConstruct(pvtbl); + pvtbl->super.GetDriverEntry = HdfDriverLoaderGetDriverEntry; +} + +struct HdfObject *HdfDriverLoaderFullCreate() +{ + if (g_fullLoader == NULL) { + struct DriverLoaderFull *instance = + (struct DriverLoaderFull *)OsalMemCalloc(sizeof(struct DriverLoaderFull)); + if (instance != NULL) { + HdfDriverLoaderFullConstruct(instance); + g_fullLoader = instance; + } + } + return (struct HdfObject *)g_fullLoader; +} + +void HdfDriverLoaderFullRelease(struct HdfObject *object) +{ + struct DriverLoaderFull *instance = (struct DriverLoaderFull *)object; + if (instance == g_fullLoader) { + g_fullLoader = NULL; + } + if (instance != NULL) { + OsalMemFree(instance); + } +} diff --git a/uhdf2/host/src/hdf_device_full.c b/uhdf2/host/src/hdf_device_full.c new file mode 100644 index 0000000..7971fbb --- /dev/null +++ b/uhdf2/host/src/hdf_device_full.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_device_full.h" +#include "device_service_stub.h" +#include "hdf_base.h" +#include "hdf_device_info_full.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG hdf_device_full + +struct HdfDeviceFull *HdfDeviceFullReinterpretCast(struct IHdfDevice *device) +{ + return (struct HdfDeviceFull *)device; +} + +static int HdfDeviceFullAttach( + struct IHdfDevice *device, struct HdfDeviceNode *deviceService) +{ + struct DeviceThread *deviceThread = NULL; + struct HdfDeviceFull *fullDevice = HdfDeviceFullReinterpretCast(device); + if (fullDevice == NULL) { + HDF_LOGE("HdfDeviceFullAttach failed, fullDevice is null"); + return HDF_FAILURE; + } + + if (fullDevice->deviceThread == NULL) { + fullDevice->deviceThread = DeviceThreadNewInstance(); + } + + deviceThread = fullDevice->deviceThread; + if (deviceThread != NULL) { + struct HdfThread *thread = (struct HdfThread *)&deviceThread->super; + if (!thread->IsRunning(thread)) { + thread->Start(thread); + } + // modified list operation. + HdfSListAdd(&fullDevice->super.services, &deviceService->entry); + DeviceThreadAttach(deviceThread, device, deviceService); + } + return HDF_SUCCESS; +} + +static void HdfDeviceFullDettach(struct IHdfDevice *device, struct HdfDeviceNode *devNode) +{ + struct HdfDeviceFull *fullDevice = HdfDeviceFullReinterpretCast(device); + if (fullDevice == NULL || devNode == NULL) { + HDF_LOGE("%s input is null", __func__); + return; + } + + if (devNode->driverEntry != NULL && devNode->driverEntry->Release != NULL) { + devNode->driverEntry->Release(&devNode->deviceObject); + } + struct DevHostService *hostService = devNode->hostService; + if (hostService != NULL) { + HdfServiceObserverRemoveRecord(&hostService->observer, devNode->deviceInfo->svcName); + } + HdfDeviceInfoFullFreeInstance((struct HdfDeviceInfoFull *)devNode->deviceInfo); + DeviceServiceStubRelease(&devNode->super.object); + + if (fullDevice->deviceThread != NULL) { + fullDevice->deviceThread->looper.Stop(&fullDevice->deviceThread->looper); + DeviceThreadFreeInstance(fullDevice->deviceThread); + fullDevice->deviceThread = NULL; + } +} + +void HdfDeviceFullConstruct(struct HdfDeviceFull *inst) +{ + if (inst != NULL) { + struct IHdfDevice *deviceIf = (struct IHdfDevice *)&inst->super; + HdfDeviceConstruct(&inst->super); + deviceIf->Attach = HdfDeviceFullAttach; + deviceIf->Detach = HdfDeviceFullDettach; + } +} + +struct HdfObject *HdfDeviceFullCreate() +{ + struct HdfDeviceFull *device = + (struct HdfDeviceFull *)OsalMemCalloc(sizeof(struct HdfDeviceFull)); + if (device != NULL) { + HdfDeviceFullConstruct(device); + } + return (struct HdfObject *)device; +} + + +void HdfDeviceFullRelease(struct HdfObject *object) +{ + struct HdfDeviceFull *fullDevice = (struct HdfDeviceFull *)object; + if (fullDevice != NULL) { + HdfDeviceDestruct(&fullDevice->super); + if (fullDevice->deviceThread != NULL) { + DeviceThreadFreeInstance(fullDevice->deviceThread); + } + OsalMemFree(fullDevice); + } +} + diff --git a/uhdf2/host/src/hdf_device_thread.c b/uhdf2/host/src/hdf_device_thread.c new file mode 100644 index 0000000..32b98f1 --- /dev/null +++ b/uhdf2/host/src/hdf_device_thread.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_device_thread.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "osal_message.h" + +#define HDF_LOG_TAG hdf_device_thread + +int32_t DeviceThreadMessageHandler(struct HdfMessageTask *task, struct HdfMessage *msg) +{ + (void)task; + struct HdfDevice *device = (struct HdfDevice *)msg->data[0]; + switch (msg->messageId) { + case DEVICE_SERVICE_MESSAGE_LAUNCH: { + struct HdfDeviceNode *devService = (struct HdfDeviceNode *)msg->data[1]; + struct IDeviceNode *serviceIf = (struct IDeviceNode *)devService; + if ((serviceIf != NULL) && (serviceIf->LaunchNode != NULL)) { + serviceIf->LaunchNode(devService, &device->super); + } + break; + } + case DEVICE_SERVICE_MESSAGE_SUSPEND: { + break; + } + case DEVICE_SERVICE_MESSAGE_RESUME: { + break; + } + default: { + HDF_LOGE("Unsupported message, message id is %u", msg->messageId); + break; + } + } + return HDF_SUCCESS; +} + +void DeviceThreadAttach(struct DeviceThread *inst, struct IHdfDevice *device, struct HdfDeviceNode *service) +{ + if (inst != NULL) { + // Thread is already running + struct HdfMessageTask *task = &inst->task; + struct HdfMessage *message = HdfMessageObtain(sizeof(struct HdfDeviceNode *)); + if (message == NULL) { + HDF_LOGE("Get message error"); + return; + } + message->messageId = DEVICE_SERVICE_MESSAGE_LAUNCH; + message->data[0] = (void *)device; + message->data[1] = (void *)service; + task->SendMessage(task, message, true); + } +} + +void DeviceThreadMain(void *args) +{ + struct DeviceThread *currentThread = (struct DeviceThread *)args; + if (currentThread != NULL) { + struct HdfMessageLooper *looper = ¤tThread->looper; + if (looper->Start != NULL) { + looper->Start(looper); + } + } +} + +void DeviceThreadConstruct(struct DeviceThread *inst) +{ + static struct IHdfMessageHandler handler = { + .Dispatch = DeviceThreadMessageHandler + }; + + HdfMessageLooperConstruct(&inst->looper); + HdfMessageTaskConstruct(&inst->task, &inst->looper, &handler); + inst->super.ThreadEntry = DeviceThreadMain; + HdfThreadConstruct(&inst->super); +} + +struct DeviceThread *DeviceThreadNewInstance() +{ + struct DeviceThread *thread = + (struct DeviceThread *)OsalMemCalloc(sizeof(struct DeviceThread)); + if (thread != NULL) { + DeviceThreadConstruct(thread); + } + return thread; +} + +void DeviceThreadFreeInstance(struct DeviceThread *thread) +{ + if (thread != NULL) { + HdfThreadDestruct(&thread->super); + OsalMemFree(thread); + } +} + diff --git a/uhdf2/host/src/hdf_devsvc_manager_clnt.c b/uhdf2/host/src/hdf_devsvc_manager_clnt.c new file mode 100644 index 0000000..ed37495 --- /dev/null +++ b/uhdf2/host/src/hdf_devsvc_manager_clnt.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devsvc_manager_clnt.h" +#include "hdf_log.h" + +#define HDF_LOG_TAG devsvc_manager_clnt + +struct HdfRemoteService *HdfManagerClntGetService(const char *svcName) +{ + return (struct HdfRemoteService *)DevSvcManagerClntGetService(svcName); +} + diff --git a/uhdf2/host/test/BUILD.gn b/uhdf2/host/test/BUILD.gn new file mode 100755 index 0000000..157fbf9 --- /dev/null +++ b/uhdf2/host/test/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (c) 2021 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/test.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +module_output_path = "hdf/devmgr" + +###########################palTest########################### +ohos_unittest("DevMgrTest") { + module_out_path = module_output_path + sources = [ "unittest/devmgr_test.cpp" ] + + include_dirs = [ + "//utils/native/base/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/utils/include", + "//drivers/adapter/uhdf2/hdi/include", + "//drivers/adapter/uhdf2/include/hidl", + "unittest/common/", + ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/osal:libhdf_utils", + "$hdf_uhdf_path/host/test/unittest/sample_driver:sample_driver", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} + +###########################end########################### +group("unittest") { + testonly = true + deps = [ ":DevMgrTest" ] +} diff --git a/uhdf2/host/test/unittest/devmgr_test.cpp b/uhdf2/host/test/unittest/devmgr_test.cpp new file mode 100755 index 0000000..cc74782 --- /dev/null +++ b/uhdf2/host/test/unittest/devmgr_test.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "hdf_log.h" +#include "osal_mem.h" +#include "securec.h" + +#define HDF_LOG_TAG driver_manager + +namespace OHOS { +using namespace testing::ext; + +class DevMgrTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void DevMgrTest::SetUpTestCase() +{ +} + +void DevMgrTest::TearDownTestCase() +{ +} + +void DevMgrTest::SetUp() +{ +} + +void DevMgrTest::TearDown() +{ +} + +/* +* @tc.name: DriverLoaderTest_001 +* @tc.desc: driver load test +* @tc.type: FUNC +* @tc.require: AR000DT1TK +*/ +HWTEST_F(DevMgrTest, DriverLoaderTest_001, TestSize.Level0) +{ + bool flag = true; + EXPECT_TRUE(flag); +} +} // namespace OHOS diff --git a/uhdf2/host/test/unittest/sample_driver/BUILD.gn b/uhdf2/host/test/unittest/sample_driver/BUILD.gn new file mode 100755 index 0000000..9fb8343 --- /dev/null +++ b/uhdf2/host/test/unittest/sample_driver/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("sample_driver") { + deps = [ ":libsample_driver" ] +} + +ohos_shared_library("libsample_driver") { + sources = [ + "sample_driver.c", + "sample_hdi_service_stub.c", + "sample_hdi_service.c", + ] + + deps = [ + "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/host:libhdf_host", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/host/test/unittest/sample_driver/sample_driver.c b/uhdf2/host/test/unittest/sample_driver/sample_driver.c new file mode 100644 index 0000000..dc65a35 --- /dev/null +++ b/uhdf2/host/test/unittest/sample_driver/sample_driver.c @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "sample_hdi.h" + +#define HDF_LOG_TAG sample_driver + +static int32_t SampleServiceDispatch(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + return SampleServiceOnRemoteRequest(client, cmdId, data, reply); +} + +void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) +{ + (void)deviceObject; + return; +} + +int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGE("HdfSampleDriverBind enter!"); + static struct IDeviceIoService testService = { + .Dispatch = SampleServiceDispatch, + .Open = NULL, + .Release = NULL, + }; + deviceObject->service = &testService; + return HDF_SUCCESS; +} + +int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGE("HdfSampleDriverInit enter, new hdi impl"); + + return HDF_SUCCESS; +} + +struct HdfDriverEntry g_sampleDriverEntry = { + .moduleVersion = 1, + .moduleName = "sample_driver", + .Bind = HdfSampleDriverBind, + .Init = HdfSampleDriverInit, + .Release = HdfSampleDriverRelease, +}; + +HDF_INIT(g_sampleDriverEntry); + diff --git a/uhdf2/host/test/unittest/sample_driver/sample_hdi.h b/uhdf2/host/test/unittest/sample_driver/sample_hdi.h new file mode 100644 index 0000000..1077d3b --- /dev/null +++ b/uhdf2/host/test/unittest/sample_driver/sample_hdi.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021 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 SAMPLE_SERVICE_HDF_H +#define SAMPLE_SERVICE_HDF_H + +#include +#include +#include +#include + +struct HdfDeviceObject; +struct HdfDeviceIoClient; + +struct DataBlock { + int a; + int b; + const char *str; + int c; +}; + +enum { + SAMPLE_SERVICE_PING = 0, + SAMPLE_SERVICE_SUM, + SAMPLE_SERVICE_CALLBACK, + SAMPLE_STRUCT_TRANS, + SAMPLE_BUFFER_TRANS, +}; + +struct SampleHdi { + int32_t (*ping)(struct HdfDeviceObject *device, const char* info, char** infoOut); + int32_t (*sum)(struct HdfDeviceObject *device, int32_t x0, int32_t x1, int32_t *result); + int32_t (*callback)(struct HdfDeviceObject *device, struct HdfRemoteService *callback, int32_t code); +}; + +const struct SampleHdi *SampleHdiImplInstance(); + +int32_t SampleServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply); + + +static inline void DataBlockFree(struct DataBlock *dataBlock) +{ + if (dataBlock != NULL) { + OsalMemFree((void *)dataBlock->str); + OsalMemFree(dataBlock); + } +} + +static inline struct DataBlock *DataBlockBlockUnmarshalling(struct HdfSBuf *data) +{ + const struct DataBlock *dataBlock_ = NULL; + uint32_t readSize = 0; + + if (!HdfSbufReadBuffer(data, (const void **)&dataBlock_, &readSize)) { + HDF_LOGE("%{public}s: failed to read dataBlock", __func__); + return NULL; + } + + if (readSize != sizeof(struct DataBlock)) { + HDF_LOGE("%{public}s: dataBlock size mismatch %d", __func__, readSize); + return NULL; + } + + struct DataBlock *dataBlock = (struct DataBlock *)OsalMemAlloc(sizeof(struct DataBlock)); + if (dataBlock == NULL) { + return NULL; + } + HDF_LOGE("%{public}s: DataBlock mem: %{public}d %{public}d %{public}d", __func__, + dataBlock_->a, dataBlock_->b, dataBlock_->c); + (void)memcpy_s(dataBlock, sizeof(*dataBlock), dataBlock_, sizeof(*dataBlock)); + + const char *str = NULL; + if (!HdfSbufReadBuffer(data, (const void **)&str, &readSize)) { + HDF_LOGE("%{public}s: failed to read dataBlock.str", __func__); + return NULL; + } + + dataBlock->str = strdup(str); + if (dataBlock->str == NULL) { + OsalMemFree(dataBlock); + return NULL; + } + + return dataBlock; +} + +static inline bool DataBlockBlockMarshalling(struct DataBlock *dataBlock, struct HdfSBuf *data) +{ + if (!HdfSbufWriteBuffer(data, dataBlock, sizeof(struct DataBlock))) { + return false; + } + + if (!HdfSbufWriteBuffer(data, dataBlock->str, strlen(dataBlock->str) + 1)) { + return false; + } + + return true; +} + + +#endif // SAMPLE_SERVICE_HDF_H \ No newline at end of file diff --git a/uhdf2/host/test/unittest/sample_driver/sample_hdi_service.c b/uhdf2/host/test/unittest/sample_driver/sample_hdi_service.c new file mode 100644 index 0000000..cd2befe --- /dev/null +++ b/uhdf2/host/test/unittest/sample_driver/sample_hdi_service.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "sample_hdi.h" + +int32_t SampleServicePing(struct HdfDeviceObject *device, const char* info, char** infoOut) +{ + (void)device; + HDF_LOGI("Sample:info is %s", info); + *infoOut = strdup(info); + return 0; +} + +int32_t SampleServiceSum(struct HdfDeviceObject *device, int32_t x0, int32_t x1, int32_t *result) +{ + (void)device; + *result = x0 + x1; + + return 0; +} + +int32_t SampleServiceCallback(struct HdfDeviceObject *device, struct HdfRemoteService *callback, int32_t code) +{ + (void)device; + struct HdfSBuf *dataSbuf = HdfSBufTypedObtain(SBUF_IPC); + HdfSbufWriteInt32(dataSbuf, code); + int ret = callback->dispatcher->Dispatch(callback, 0, dataSbuf, NULL); + if (ret != HDF_SUCCESS) { + HDF_LOGE("failed to do callback, ret = %d", ret); + } + HdfSBufRecycle(dataSbuf); + return ret; +} + +static const struct SampleHdi g_sampleHdiImpl = { + .ping = SampleServicePing, + .sum = SampleServiceSum, + .callback = SampleServiceCallback, +}; + +const struct SampleHdi *SampleHdiImplInstance() +{ + return &g_sampleHdiImpl; +} \ No newline at end of file diff --git a/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.c b/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.c new file mode 100644 index 0000000..33a4207 --- /dev/null +++ b/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.c @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "sample_hdi.h" + +static int32_t SampleServiceStubPing(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + char *outInfo = NULL; + const char *info = HdfSbufReadString(data); + + int32_t ret = SampleHdiImplInstance()->ping(client->device, info, &outInfo); + + HdfSbufWriteString(reply, outInfo); + OsalMemFree(outInfo); + return ret; +} + +static int32_t SampleServiceStubSum(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t parm0; + int32_t parm1; + int32_t result; + + if (!HdfSbufReadInt32(data, &parm0)) { + HDF_LOGE("SampleHdi.sum: miss parm0"); + return HDF_ERR_INVALID_PARAM; + } + + if (!HdfSbufReadInt32(data, &parm1)) { + HDF_LOGE("SampleHdi.sum: miss parm1"); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ret = SampleHdiImplInstance()->sum(client->device, parm0, parm1, &result); + if (ret == HDF_SUCCESS) { + if (!HdfSbufWriteInt32(reply, result)) { + HDF_LOGE("SampleHdi.sum: failed to write result "); + return HDF_FAILURE; + } + } + + return ret; +} + +static int32_t SampleServiceStubCallback(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t code; + if (!HdfSbufReadInt32(data, &code)) { + HDF_LOGE("SampleHdi.callback: miss parameter code"); + return HDF_ERR_INVALID_PARAM; + } + + struct HdfRemoteService *callback = HdfSBufReadRemoteService(data); + if (callback == NULL) { + HDF_LOGE("SampleHdi.callback: miss parameter callback"); + return HDF_ERR_INVALID_PARAM; + } + return SampleHdiImplInstance()->callback(client->device, callback, code); +} + +static int32_t SampleServiceStubStructTrans(struct HdfDeviceIoClient *client, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + HDF_LOGI("SampleServiceStubStructTrans: in"); + struct DataBlock *dataBlock = DataBlockBlockUnmarshalling(data); + if (dataBlock == NULL) { + HDF_LOGE("SampleServiceStubStructTrans: failed to read dataBlock"); + return HDF_ERR_INVALID_PARAM; + } + + int32_t ret = HDF_SUCCESS; + if (!DataBlockBlockMarshalling(dataBlock, reply)) { + HDF_LOGE("SampleServiceStubStructTrans: failed to write dataBlock"); + ret = HDF_ERR_INVALID_PARAM; + } else { + HDF_LOGI("SampleServiceStubStructTrans: good return"); + } + DataBlockFree(dataBlock); + return ret; +} + +#define SAMPLE_TEST_BUFFER_SIZE 10 +static int32_t SampleServiceStubBufferTrans(struct HdfDeviceIoClient *client, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + HDF_LOGI("SampleServiceStubBufferTrans: in"); + + const uint8_t *buffer = HdfSbufReadUnpadBuffer(data, SAMPLE_TEST_BUFFER_SIZE); + if (buffer == NULL) { + HDF_LOGI("SampleServiceStubBufferTrans: read buffer failed"); + return HDF_ERR_INVALID_PARAM; + } + + if (!HdfSbufWriteUnpadBuffer(reply, buffer, SAMPLE_TEST_BUFFER_SIZE)) { + HDF_LOGE("SampleServiceStubBufferTrans: failed to write buffer"); + return HDF_ERR_INVALID_PARAM; + } else { + HDF_LOGI("SampleServiceStubBufferTrans: good return"); + } + + return HDF_SUCCESS; +} + +int32_t SampleServiceOnRemoteRequest(struct HdfDeviceIoClient *client, int cmdId, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + switch (cmdId) { + case SAMPLE_SERVICE_PING: + return SampleServiceStubPing(client, data, reply); + case SAMPLE_SERVICE_SUM: + return SampleServiceStubSum(client, data, reply); + case SAMPLE_SERVICE_CALLBACK: + return SampleServiceStubCallback(client, data, reply); + case SAMPLE_STRUCT_TRANS: + return SampleServiceStubStructTrans(client, data, reply); + case SAMPLE_BUFFER_TRANS: + return SampleServiceStubBufferTrans(client, data, reply); + default: + HDF_LOGE("SampleServiceDispatch: not support cmd %d", cmdId); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/uhdf2/include/config/hcs_dm_parser.h b/uhdf2/include/config/hcs_dm_parser.h new file mode 100644 index 0000000..880da86 --- /dev/null +++ b/uhdf2/include/config/hcs_dm_parser.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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 HCS_DM_PARSER_H +#define HCS_DM_PARSER_H + +#include "device_resource_if.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +void SetHcsBlobPath(const char *path); +void ReleaseHcsTree(void); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif // HCS_DM_PARSER_H diff --git a/uhdf2/include/devhost/hdf_device_full.h b/uhdf2/include/devhost/hdf_device_full.h new file mode 100644 index 0000000..f456dda --- /dev/null +++ b/uhdf2/include/devhost/hdf_device_full.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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 HDF_DEVICE_FULL_H +#define HDF_DEVICE_FULL_H + +#include "hdf_attribute.h" +#include "hdf_dev_token.h" +#include "hdf_device.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +struct HdfDeviceFull { + struct HdfDevice device; + struct HdfAttribute *attribute; + struct HdfDevToken devToken; + struct DListHead list; +}; + +struct HdfDeviceFull *HdfDeviceFullNewInstance(struct HdfAttribute *devAttr); +void HdfDeviceFullDeleteInstance(struct HdfDeviceFull *device); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* HDF_DEVICE_FULL_H */ diff --git a/uhdf2/include/hdf_devmgr.h b/uhdf2/include/hdf_devmgr.h new file mode 100644 index 0000000..12380c4 --- /dev/null +++ b/uhdf2/include/hdf_devmgr.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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 __HDF_DEVMGR_H__ +#define __HDF_DEVMGR_H__ + +#if defined(__KERNEL__) + +#elif defined(CONFIG_LITE_OS) + +#else +#include "devhost/hdf_attribute.h" +#include "devhost/hdf_device.h" +#include "devhost/hdf_device_full.h" +#include "devhost/hdf_driver.h" +#include "devhost/dm_service_if.h" +#include "devhost/ds_manager_if.h" + +#include "hidl/hdf_dev_token.h" +#include "hidl/hdf_ds_manager_clnt.h" + +#include "mock/hdf_sbuf.h" +#include "mock/hdf_remote_object.h" +#include "mock/hdf_service_registry.h" + +#include "../third_party/utils/include/of/fdt_convertor.h" +#include "../third_party/utils/include/of/of.h" +#endif + +#endif /* __HDF_DEVMGR_H__ */ diff --git a/uhdf2/include/hdi/devmgr_hdi.h b/uhdf2/include/hdi/devmgr_hdi.h new file mode 100644 index 0000000..810e4a6 --- /dev/null +++ b/uhdf2/include/hdi/devmgr_hdi.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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 HDI_DEVICE_MANAGER_INF_H +#define HDI_DEVICE_MANAGER_INF_H + +#include "hdf_base.h" +#include "hdf_dlist.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct HdfRemoteService; + +enum HDF_DEVICE_TYPE { + HDF_LOCAL_SERVICE, + HDF_REMOTE_SERVICE, +}; + +struct DeviceInfoNode { + char *svcName; + enum HDF_DEVICE_TYPE deviceType; + struct DListHead node; +}; + +struct DeviceInfoList { + uint32_t deviceCnt; + struct DListHead list; +}; + +struct HDIDeviceManager { + struct HdfRemoteService *remote; + + void (*FreeQueryDeviceList)(struct HDIDeviceManager *self, struct DeviceInfoList *list); + int32_t (*QueryUsableDeviceInfo)(struct HDIDeviceManager *self, struct DeviceInfoList *list); + int32_t (*QueryUnusableDeviceInfo)(struct HDIDeviceManager *self, struct DeviceInfoList *list); + int32_t (*RegPnpDevice)(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName); + int32_t (*UnRegPnpDevice)(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName); + int32_t (*RegVirtualDevice)(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName); + int32_t (*UnRegVirtualDevice)(struct HDIDeviceManager *self, const char *moduleName, const char *serviceName); +}; + +struct HDIDeviceManager *HDIDeviceManagerGet(void); +void HDIDeviceManagerRelease(struct HDIDeviceManager *devmgr); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDI_DEVICE_MANAGER_INF_H */ \ No newline at end of file diff --git a/uhdf2/include/hdi/hdf_load_hdi.h b/uhdf2/include/hdi/hdf_load_hdi.h new file mode 100644 index 0000000..184e159 --- /dev/null +++ b/uhdf2/include/hdi/hdf_load_hdi.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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 HDF_LOAD_HDI_H +#define HDF_LOAD_HDI_H + +#include "hdf_base.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct HdiBase { + uint32_t moduleVersion; + const char *moduleName; + int (*OpenHdi)(); + int (*CloseHdi)(); +}; + +struct HdiObject { + uintptr_t dlHandler; + struct HdiBase *hdiBase; +}; + +struct HdiObject *LoadHdi(const char *name, uint32_t version); +void CloseHdi(struct HdiObject *hdi); + +#define HDF_HDI_INIT(module) struct HdiBase *hdfHdiDesc = (struct HdiBase *)&(module) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDF_LOAD_HDI_H */ + diff --git a/uhdf2/include/hdi/iservmgr_hdi.h b/uhdf2/include/hdi/iservmgr_hdi.h new file mode 100644 index 0000000..7c0f261 --- /dev/null +++ b/uhdf2/include/hdi/iservmgr_hdi.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 HDI_SERVICE_MANAGER_INF_H +#define HDI_SERVICE_MANAGER_INF_H + +#include +#include + +namespace OHOS { +namespace HDI { +namespace ServiceManager { +namespace V1_0 { + +struct IServiceManager : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"HDI.IServiceManager.V1_0"); + static ::OHOS::sptr Get(); + virtual ::OHOS::sptr GetService(const char* serviceName) = 0; +}; +} // namespace V1_0 +} // namespace ServiceManager +} // namespace HDI +} // namespace OHOS + +#endif /* HDI_SERVICE_MANAGER_INF_H */ \ No newline at end of file diff --git a/uhdf2/include/hdi/servmgr_hdi.h b/uhdf2/include/hdi/servmgr_hdi.h new file mode 100644 index 0000000..5b44ecd --- /dev/null +++ b/uhdf2/include/hdi/servmgr_hdi.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 HDI_SERVICE_MANAGER_INF_H +#define HDI_SERVICE_MANAGER_INF_H + +#include "hdf_remote_service.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct HDIServiceManager { + struct HdfRemoteService *remote; + + struct HdfRemoteService *(*GetService)(struct HDIServiceManager *self, const char* serviceName); +}; + +struct HDIServiceManager *HDIServiceManagerGet(void); +void HDIServiceManagerRelease(struct HDIServiceManager *servmgr); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDI_SERVICE_MANAGER_INF_H */ \ No newline at end of file diff --git a/uhdf2/include/host/hdf_device_section.h b/uhdf2/include/host/hdf_device_section.h new file mode 100644 index 0000000..dd171d2 --- /dev/null +++ b/uhdf2/include/host/hdf_device_section.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 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 HDF_DEVICE_SECTION_H +#define HDF_DEVICE_SECTION_H + +struct HdfDriverEntry; + +#define HDF_DRIVER_INIT(module) struct HdfDriverEntry *driverDesc = &(module) + +#endif /* HDF_DEVICE_SECTION_H */ + diff --git a/uhdf2/ipc/BUILD.gn b/uhdf2/ipc/BUILD.gn new file mode 100755 index 0000000..8878741 --- /dev/null +++ b/uhdf2/ipc/BUILD.gn @@ -0,0 +1,78 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("libhdf_ipc_adapter_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ + "//utils/native/base/include", + "//drivers/framework/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/utils/include", + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/core/adapter/syscall/include", + "//drivers/framework/core/adapter/vnode/include", + "//drivers/framework/core/shared/include", + "//drivers/framework/include/core/", + "//drivers/adapter/uhdf2/shared/include", + "include", + ] +} + +ohos_shared_library("libhdf_ipc_adapter") { + include_dirs = [] + + public_configs = [ ":libhdf_ipc_adapter_pub_config" ] + + sources = [ + # "src/hdf_parcel_adapter.cpp", + "src/hdf_remote_adapter.cpp", + "src/hdf_remote_service.c", + "src/hdf_sbuf_impl_hipc.cpp", + + # "$hdf_uhdf_path/shared/src/hdf_parcel.c", + ] + + deps = [ + "$hdf_uhdf_path/osal:libhdf_utils", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ + "communication_L2:ipc_core", + "hiviewdfx_hilog_native:libhilog", + "samgr_L2:samgr_proxy", + ] + } else { + external_deps = [ + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/ipc/include/hdf_remote_adapter.h b/uhdf2/ipc/include/hdf_remote_adapter.h new file mode 100644 index 0000000..cf491a0 --- /dev/null +++ b/uhdf2/ipc/include/hdf_remote_adapter.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2021 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 HDF_REMOTE_ADPATER_H +#define HDF_REMOTE_ADPATER_H + +#include +#include +#include "hdf_remote_service.h" +#include "hdf_remote_adapter_if.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +class HdfRemoteServiceStub : public OHOS::IPCObjectStub { +public: + HdfRemoteServiceStub(struct HdfRemoteService *service); + int OnRemoteRequest(uint32_t code, + OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) override; + ~HdfRemoteServiceStub(); +private: + struct HdfRemoteService *service_; +}; + +class HdfDeathNotifier : public OHOS::IRemoteObject::DeathRecipient { +public: + HdfDeathNotifier(struct HdfRemoteService *service, struct HdfDeathRecipient *recipient); + virtual ~HdfDeathNotifier(); + void OnRemoteDied(const OHOS::wptr &object) override; + +private: + struct HdfDeathRecipient *recipient_; + struct HdfRemoteService *service_; +}; + +struct HdfRemoteServiceHolder { + HdfRemoteServiceHolder(); + struct HdfRemoteService service_; + OHOS::sptr remote_; + std::vector> recipients; +}; + +struct HdfRemoteService *HdfRemoteAdapterBind(OHOS::sptr binder); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* HDF_REMOTE_ADPATER_H */ diff --git a/uhdf2/ipc/include/hdf_remote_adapter_if.h b/uhdf2/ipc/include/hdf_remote_adapter_if.h new file mode 100644 index 0000000..acad39a --- /dev/null +++ b/uhdf2/ipc/include/hdf_remote_adapter_if.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 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 HDF_REMOTE_ADAPATER_IF_H +#define HDF_REMOTE_ADAPATER_IF_H + +#include "hdf_remote_service.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct HdfRemoteService *HdfRemoteAdapterObtain(void); + +void HdfRemoteAdapterRecycle(struct HdfRemoteService *service); + +int HdfRemoteAdapterAddService(const char *name, struct HdfRemoteService *service); + +struct HdfRemoteService *HdfRemoteAdapterGetService(const char *name); + +int HdfRemoteAdapterAddSa(int32_t saId, struct HdfRemoteService *service); + +struct HdfRemoteService *HdfRemoteAdapterGetSa(int32_t saId); + +void HdfRemoteAdapterAddDeathRecipient( + struct HdfRemoteService *service, struct HdfDeathRecipient *recipient); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* HDF_REMOTE_ADAPATER_IF_H */ \ No newline at end of file diff --git a/uhdf2/ipc/include/hdf_sbuf_ipc.h b/uhdf2/ipc/include/hdf_sbuf_ipc.h new file mode 100644 index 0000000..d1eda65 --- /dev/null +++ b/uhdf2/ipc/include/hdf_sbuf_ipc.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 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 __HDF_SBUF_IPC_IMPL_H_ +#define __HDF_SBUF_IPC_IMPL_H_ + +#include +#include "hdf_sbuf.h" + +struct HdfSBuf *ParcelToSbuf(OHOS::MessageParcel *parcel); +int32_t SbufToParcel(struct HdfSBuf *sbuf, OHOS::MessageParcel **parcel); + +#endif // __HDF_SBUF_IPC_IMPL_H_ diff --git a/uhdf2/ipc/src/hdf_remote_adapter.cpp b/uhdf2/ipc/src/hdf_remote_adapter.cpp new file mode 100755 index 0000000..5ddd113 --- /dev/null +++ b/uhdf2/ipc/src/hdf_remote_adapter.cpp @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_remote_adapter.h" +#include +#include +#include +#include "hdf_log.h" +#include "hdf_object_manager.h" +#include "hdf_sbuf_ipc.h" + +static constexpr int32_t THREAD_POOL_BASE_THREAD_COUNT = 1; +static int32_t g_remoteThreadMax = THREAD_POOL_BASE_THREAD_COUNT; + +HdfRemoteServiceStub::HdfRemoteServiceStub(struct HdfRemoteService *service) + : IPCObjectStub(std::u16string(u"")), service_(service) +{ +} + +int HdfRemoteServiceStub::OnRemoteRequest(uint32_t code, + OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) +{ + (void)option; + if (service_ == nullptr) { + return HDF_ERR_INVALID_OBJECT; + } + + int ret = HDF_FAILURE; + struct HdfSBuf *dataSbuf = ParcelToSbuf(&data); + struct HdfSBuf *replySbuf = ParcelToSbuf(&reply); + + struct HdfRemoteDispatcher *dispatcher = service_->dispatcher; + if (dispatcher != nullptr && dispatcher->Dispatch != nullptr) { + ret = dispatcher->Dispatch((HdfRemoteService *)service_->target, code, dataSbuf, replySbuf); + } else { + HDF_LOGE("dispatcher or dispatcher->Dispatch is null, flags is: %d", option.GetFlags()); + } + HDF_LOGD("OnRemoteRequest finished, ret = %{public}d", ret); + + HdfSBufRecycle(dataSbuf); + HdfSBufRecycle(replySbuf); + return ret; +} + +HdfRemoteServiceStub::~HdfRemoteServiceStub() +{ +} + +HdfDeathNotifier::HdfDeathNotifier(struct HdfRemoteService *service, struct HdfDeathRecipient *recipient) + : recipient_(recipient), service_(service) +{ +} + +HdfDeathNotifier::~HdfDeathNotifier() +{ +} + +void HdfDeathNotifier::OnRemoteDied(const OHOS::wptr &) /* who = 0 */ +{ + if (recipient_ != nullptr) { + recipient_->OnRemoteDied(recipient_, service_); + } +} + +static int HdfRemoteAdapterDispatch(struct HdfRemoteService *service, int code, HdfSBuf *data, HdfSBuf *reply) +{ + if (service == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + + OHOS::MessageParcel *dataParcel = nullptr; + OHOS::MessageParcel *replyParcel = nullptr; + + (void)SbufToParcel(reply, &replyParcel); + if (SbufToParcel(data, &dataParcel)) { + HDF_LOGE("%s:invalid data sbuf object to dispatch", __func__); + return HDF_ERR_INVALID_PARAM; + } + + OHOS::MessageOption option; + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + if (dataParcel != nullptr) { + OHOS::sptr remote = holder->remote_; + if (remote != nullptr) { + return remote->SendRequest(code, *dataParcel, *replyParcel, option); + } + } + return HDF_FAILURE; +} + +HdfRemoteServiceHolder::HdfRemoteServiceHolder() : remote_(nullptr) +{ + service_.object_.objectId = HDF_OBJECT_ID_REMOTE_SERVICE; + service_.dispatcher = nullptr; + service_.target = nullptr; +} + +void HdfRemoteAdapterAddDeathRecipient( + struct HdfRemoteService *service, struct HdfDeathRecipient *recipient) +{ + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + if (holder == NULL) { + return; + } + OHOS::sptr remote = holder->remote_; + if (remote == nullptr) { + return; + } + HdfDeathNotifier *notifier = new HdfDeathNotifier(service, recipient); + remote->AddDeathRecipient(notifier); +} + +struct HdfRemoteService *HdfRemoteAdapterBind(OHOS::sptr binder) +{ + struct HdfRemoteService *remoteService = nullptr; + static HdfRemoteDispatcher dispatcher = { + .Dispatch = HdfRemoteAdapterDispatch + }; + + struct HdfRemoteServiceHolder *holder = new HdfRemoteServiceHolder(); + if (holder != nullptr) { + holder->remote_ = binder; + remoteService = &holder->service_; + remoteService->dispatcher = &dispatcher; + return remoteService; + } + return nullptr; +} + +struct HdfRemoteService *HdfRemoteAdapterObtain(void) +{ + struct HdfRemoteServiceHolder *holder = new HdfRemoteServiceHolder(); + holder->remote_ = new HdfRemoteServiceStub(&holder->service_); + return &holder->service_; +} + +void HdfRemoteAdapterRecycle(struct HdfRemoteService *object) +{ + struct HdfRemoteServiceHolder *holder = reinterpret_cast(object); + if (holder != nullptr) { + if (holder->remote_ != nullptr) { + holder->remote_ = nullptr; + } + delete holder; + } +} + +int HdfRemoteAdapterAddService(const char *name, struct HdfRemoteService *service) +{ + if (name == nullptr || service == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + + OHOS::sptr sr = OHOS::ServiceRegistry::GetInstance(); + if (sr == nullptr) { + HDF_LOGE("failed to get service registry"); + return HDF_FAILURE; + } + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + int ret = sr->AddService(OHOS::Str8ToStr16(name), holder->remote_); + if (ret == 0) { + (void)OHOS::IPCSkeleton::GetInstance().SetMaxWorkThreadNum(g_remoteThreadMax++); + } + return ret; +} + +struct HdfRemoteService *HdfRemoteAdapterGetService(const char *name) +{ + if (name == nullptr) { + return nullptr; + } + + OHOS::sptr sr = OHOS::ServiceRegistry::GetInstance(); + if (sr == nullptr) { + HDF_LOGE("failed to get service registry"); + return nullptr; + } + OHOS::sptr remote = sr->GetService(OHOS::Str8ToStr16(name)); + if (remote != nullptr) { + return HdfRemoteAdapterBind(remote); + } + return nullptr; +} + +int HdfRemoteAdapterAddSa(int32_t saId, struct HdfRemoteService *service) +{ + if (service == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + + auto saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + HDF_LOGE("failed to get sa manager"); + return HDF_FAILURE; + } + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + int ret = saManager->AddSystemAbility(saId, holder->remote_); + (void)OHOS::IPCSkeleton::GetInstance().SetMaxWorkThreadNum(g_remoteThreadMax++); + HDF_LOGI("add sa %d, ret = %s", saId, ret == 0 ? "succ" : "fail"); + + return HDF_SUCCESS; +} + +struct HdfRemoteService *HdfRemoteAdapterGetSa(int32_t saId) +{ + auto saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + HDF_LOGE("failed to get sa manager"); + return nullptr; + } + OHOS::sptr remote = saManager->GetSystemAbility(saId); + if (remote != nullptr) { + return HdfRemoteAdapterBind(remote); + } else { + HDF_LOGE("failed to get sa %d", saId); + } + return nullptr; +} diff --git a/uhdf2/ipc/src/hdf_remote_service.c b/uhdf2/ipc/src/hdf_remote_service.c new file mode 100644 index 0000000..3dbca96 --- /dev/null +++ b/uhdf2/ipc/src/hdf_remote_service.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_remote_service.h" +#include "hdf_remote_adapter_if.h" + +void HdfRemoteServiceAddDeathRecipient( + struct HdfRemoteService *service, struct HdfDeathRecipient *recipient) +{ + HdfRemoteAdapterAddDeathRecipient(service, recipient); +} + +struct HdfRemoteService *HdfRemoteServiceObtain(struct HdfObject *object, struct HdfRemoteDispatcher *dispatcher) +{ + struct HdfRemoteService *service = HdfRemoteAdapterObtain(); + if ((service != NULL) && (service->dispatcher == NULL)) { + service->dispatcher = dispatcher; + service->target = object; + } + return service; +} + +void HdfRemoteServiceRecycle(struct HdfRemoteService *service) +{ + HdfRemoteAdapterRecycle(service); +} + +struct HdfRemoteService *HdfRemoteServiceGet(int32_t serviceId) +{ + return HdfRemoteAdapterGetSa(serviceId); +} + +int HdfRemoteServiceRegister(int32_t serviceId, struct HdfRemoteService *service) +{ + return HdfRemoteAdapterAddSa(serviceId, service); +} diff --git a/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp b/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp new file mode 100755 index 0000000..a7d37ea --- /dev/null +++ b/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp @@ -0,0 +1,504 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include "hdf_sbuf_ipc.h" +#include "hdf_sbuf_impl.h" +#include "hdf_log.h" +#include "hdf_remote_adapter.h" + +using namespace OHOS; + +static void MParcelImplInterfaceAssign(struct HdfSbufImpl *inf); + +struct SBufMParcelImpl { + SBufMParcelImpl(OHOS::MessageParcel *parcel, bool owned = true): realParcel_(parcel), owned_(owned) + { + MParcelImplInterfaceAssign(&infImpl); + } + ~SBufMParcelImpl() + { + if (owned_ && realParcel_ != nullptr) { + delete realParcel_; + realParcel_ = nullptr; + } + } + struct HdfSbufImpl infImpl; + OHOS::MessageParcel *realParcel_; + std::vector str16Pool_; + bool owned_; +}; + +static MessageParcel *MParcelCast(struct HdfSbufImpl *impl) +{ + SBufMParcelImpl *sbufImpl = reinterpret_cast(impl); + return sbufImpl->realParcel_; +} + +static bool SbufMParcelImplWriteBuffer(struct HdfSbufImpl *sbuf, const uint8_t *data, uint32_t writeSize) +{ + if (sbuf == nullptr) { + return false; + } + auto parcel = MParcelCast(sbuf); + if (!parcel->WriteUint32(writeSize)) { + return false; + } + return parcel->WriteUnpadBuffer((const void *)data, writeSize); +} + +static bool SbufMParcelImplWriteUnpadBuffer(struct HdfSbufImpl *sbuf, const uint8_t *data, uint32_t writeSize) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteUnpadBuffer(static_cast(data), writeSize); +} + +static bool SbufMParcelImplWriteUint64(struct HdfSbufImpl *sbuf, uint64_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteUint64(value); +} + +static bool SbufMParcelImplWriteUint32(struct HdfSbufImpl *sbuf, uint32_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteUint32(value); +} + +static bool SbufMParcelImplWriteUint16(struct HdfSbufImpl *sbuf, uint16_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteUint16(value); +} + +static bool SbufMParcelImplWriteUint8(struct HdfSbufImpl *sbuf, uint8_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteUint8(value); +} + +static bool SbufMParcelImplWriteInt64(struct HdfSbufImpl *sbuf, int64_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteInt64(value); +} + +static bool SbufMParcelImplWriteInt32(struct HdfSbufImpl *sbuf, int32_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteInt32(value); +} + +static bool SbufMParcelImplWriteInt16(struct HdfSbufImpl *sbuf, int16_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteInt16(value); +} + +static bool SbufMParcelImplWriteInt8(struct HdfSbufImpl *sbuf, int8_t value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteInt8(value); +} + +static bool SbufMParcelImplWriteString(struct HdfSbufImpl *sbuf, const char *value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteCString(value); +} + +static bool SbufMParcelImplWriteString16(struct HdfSbufImpl *sbuf, const char16_t *value, uint32_t size) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteString16WithLength(value, size); +} + +static bool SbufMParcelImplWriteFileDescriptor(struct HdfSbufImpl *sbuf, int fd) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteFileDescriptor(fd); +} + +static bool SbufMParcelImplWriteDouble(struct HdfSbufImpl *sbuf, double value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteDouble(value); +} + +static bool SbufMParcelImplWriteFloat(struct HdfSbufImpl *sbuf, float value) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->WriteFloat(value); +} + +static bool SbufMParcelImplReadFloat(struct HdfSbufImpl *sbuf, float *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + float v = 0; + bool ret = MParcelCast(sbuf)->ReadFloat(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadDouble(struct HdfSbufImpl *sbuf, double *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + double v = 0; + bool ret = MParcelCast(sbuf)->ReadDouble(v); + *value = v; + return ret; +} + +static int SbufMParcelImplReadFileDescriptor(struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return false; + } + return MParcelCast(sbuf)->ReadFileDescriptor(); +} + +static bool SbufMParcelImplReadBuffer(struct HdfSbufImpl *sbuf, const uint8_t **data, uint32_t *readSize) +{ + if (sbuf == nullptr || data == nullptr || readSize == nullptr) { + return false; + } + MessageParcel *parcel = MParcelCast(sbuf); + *readSize = parcel->ReadUint32(); + *data = parcel->ReadUnpadBuffer(*readSize); + return *data != nullptr; +} + +static const uint8_t *SbufMParcelImplReadUnpadBuffer(struct HdfSbufImpl *sbuf, size_t length) +{ + if (sbuf == nullptr) { + return nullptr; + } + return MParcelCast(sbuf)->ReadUnpadBuffer(length); +} + +static bool SbufMParcelImplReadUint64(struct HdfSbufImpl *sbuf, uint64_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + uint64_t v = 0; + bool ret = MParcelCast(sbuf)->ReadUint64(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadUint32(struct HdfSbufImpl *sbuf, uint32_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + uint32_t v = 0; + bool ret = MParcelCast(sbuf)->ReadUint32(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadUint16(struct HdfSbufImpl *sbuf, uint16_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + uint16_t v = 0; + bool ret = MParcelCast(sbuf)->ReadUint16(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadUint8(struct HdfSbufImpl *sbuf, uint8_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + uint8_t v = 0; + bool ret = MParcelCast(sbuf)->ReadUint8(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadInt64(struct HdfSbufImpl *sbuf, int64_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + int64_t v = 0; + bool ret = MParcelCast(sbuf)->ReadInt64(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadInt32(struct HdfSbufImpl *sbuf, int32_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + int32_t v = 0; + bool ret = MParcelCast(sbuf)->ReadInt32(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadInt16(struct HdfSbufImpl *sbuf, int16_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + int16_t v = 0; + bool ret = MParcelCast(sbuf)->ReadInt16(v); + *value = v; + return ret; +} + +static bool SbufMParcelImplReadInt8(struct HdfSbufImpl *sbuf, int8_t *value) +{ + if (sbuf == nullptr || value == nullptr) { + return false; + } + int8_t v = 0; + bool ret = MParcelCast(sbuf)->ReadInt8(v); + *value = v; + return ret; +} + +static const char *SbufMParcelImplReadString(struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return nullptr; + } + return MParcelCast(sbuf)->ReadCString(); +} + +static const char16_t *SbufMParcelImplReadString16(struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return nullptr; + } + SBufMParcelImpl *sbufImpl = reinterpret_cast(sbuf); + sbufImpl->str16Pool_.emplace_back(sbufImpl->realParcel_->ReadString16()); + return sbufImpl->str16Pool_.rbegin()->c_str(); +} + +static int32_t SbufMParcelImplWriteRemoteService(struct HdfSbufImpl *sbuf, const struct HdfRemoteService *service) +{ + if (sbuf == nullptr || service == nullptr) { + return false; + } + MessageParcel *parcel = MParcelCast(sbuf); + const struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + if (holder != nullptr) { + return parcel->WriteRemoteObject(holder->remote_) ? HDF_SUCCESS : HDF_FAILURE; + } + return HDF_FAILURE; +} + +static struct HdfRemoteService *SbufMParcelImplReadRemoteService(struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return nullptr; + } + auto remote = MParcelCast(sbuf)->ReadRemoteObject(); + if (remote == nullptr) { + HDF_LOGE("%s: read remote object fail", __func__); + return nullptr; + } + return HdfRemoteAdapterBind(remote); +} + +static const uint8_t *SbufMParcelImplGetData(const struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return nullptr; + } + return reinterpret_cast(MParcelCast(const_cast(sbuf))->GetData()); +} + +static void SbufMParcelImplFlush(struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return; + } + return MParcelCast(sbuf)->FlushBuffer(); +} + +static size_t SbufMParcelImplGetCapacity(const struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return HDF_FAILURE; + } + + return MParcelCast(const_cast(sbuf))->GetDataCapacity(); +} + +static size_t SbufMParcelImplGetDataSize(const struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return HDF_FAILURE; + } + return MParcelCast(const_cast(sbuf))->GetDataSize(); +} + +static void SbufMParcelImplSetDataSize(struct HdfSbufImpl *sbuf, size_t size) +{ + if (sbuf == nullptr) { + return; + } + MParcelCast(sbuf)->SetDataSize(size); +} + +static void SbufMParcelImplRecycle(struct HdfSbufImpl *sbuf) +{ + if (sbuf == nullptr) { + return; + } + SBufMParcelImpl *sbufImpl = reinterpret_cast(sbuf); + delete sbufImpl; +} + +static void MParcelImplInterfaceAssign(struct HdfSbufImpl *inf) +{ + inf->writeBuffer = SbufMParcelImplWriteBuffer; + inf->writeUnpadBuffer = SbufMParcelImplWriteUnpadBuffer; + inf->writeUint64 = SbufMParcelImplWriteUint64; + inf->writeUint32 = SbufMParcelImplWriteUint32; + inf->writeUint16 = SbufMParcelImplWriteUint16; + inf->writeUint8 = SbufMParcelImplWriteUint8; + inf->writeInt64 = SbufMParcelImplWriteInt64; + inf->writeInt32 = SbufMParcelImplWriteInt32; + inf->writeInt16 = SbufMParcelImplWriteInt16; + inf->writeInt8 = SbufMParcelImplWriteInt8; + inf->writeString = SbufMParcelImplWriteString; + inf->writeString16 = SbufMParcelImplWriteString16; + inf->writeFileDescriptor = SbufMParcelImplWriteFileDescriptor; + inf->writeFloat = SbufMParcelImplWriteFloat; + inf->writeDouble = SbufMParcelImplWriteDouble; + inf->readDouble = SbufMParcelImplReadDouble; + inf->readFloat = SbufMParcelImplReadFloat; + inf->readFileDescriptor = SbufMParcelImplReadFileDescriptor; + inf->readBuffer = SbufMParcelImplReadBuffer; + inf->readUnpadBuffer = SbufMParcelImplReadUnpadBuffer; + inf->readUint64 = SbufMParcelImplReadUint64; + inf->readUint32 = SbufMParcelImplReadUint32; + inf->readUint16 = SbufMParcelImplReadUint16; + inf->readUint8 = SbufMParcelImplReadUint8; + inf->readInt64 = SbufMParcelImplReadInt64; + inf->readInt32 = SbufMParcelImplReadInt32; + inf->readInt16 = SbufMParcelImplReadInt16; + inf->readInt8 = SbufMParcelImplReadInt8; + inf->readString = SbufMParcelImplReadString; + inf->readString16 = SbufMParcelImplReadString16; + inf->writeRemoteService = SbufMParcelImplWriteRemoteService; + inf->readRemoteService = SbufMParcelImplReadRemoteService; + inf->getData = SbufMParcelImplGetData; + inf->flush = SbufMParcelImplFlush; + inf->getCapacity = SbufMParcelImplGetCapacity; + inf->getDataSize = SbufMParcelImplGetDataSize; + inf->setDataSize = SbufMParcelImplSetDataSize; + inf->recycle = SbufMParcelImplRecycle; + inf->transDataOwnership = nullptr; + inf->copy = nullptr; + inf->move = nullptr; +} + +extern "C" struct HdfSbufImpl *SbufObtainIpc(size_t capacity) +{ + (void)capacity; + struct SBufMParcelImpl *sbuf = new SBufMParcelImpl(new MessageParcel()); + return &sbuf->infImpl; +} + +class SbufAllocator : public Allocator { +public: + virtual void *Realloc(void *data, size_t newSize) override + { + (void)data; + (void)newSize; + return nullptr; + } + + virtual void *Alloc(size_t size) override + { + (void)size; + return nullptr; + } + + virtual void Dealloc(void *data) override + { + (void)data; + } +}; + +extern "C" struct HdfSbufImpl *SbufBindIpc(uintptr_t base, size_t size) +{ + struct SBufMParcelImpl *sbuf = new SBufMParcelImpl(new MessageParcel(new SbufAllocator())); + sbuf->realParcel_->ParseFrom(base, size); + return &sbuf->infImpl; +} + +struct HdfSBuf *ParcelToSbuf(OHOS::MessageParcel *parcel) +{ + struct SBufMParcelImpl *sbuf = new SBufMParcelImpl(parcel, false); + return HdfSBufTypedObtainInplace(SBUF_IPC, &sbuf->infImpl); +} + +int32_t SbufToParcel(struct HdfSBuf *sbuf, OHOS::MessageParcel **parcel) +{ + if (sbuf == nullptr || parcel == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + struct SBufMParcelImpl *impl = reinterpret_cast(HdfSbufGetImpl(sbuf)); + if (impl == nullptr) { + return HDF_ERR_INVALID_OBJECT; + } + + *parcel = impl->realParcel_; + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/uhdf2/manager/BUILD.gn b/uhdf2/manager/BUILD.gn new file mode 100755 index 0000000..a6d52c5 --- /dev/null +++ b/uhdf2/manager/BUILD.gn @@ -0,0 +1,101 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +ohos_executable("hdf_devmgr") { + include_dirs = [ + "//utils/native/base/include", + "$hdf_framework_path/core/manager/include", + "$hdf_framework_path/core/common/include/manager/", + "$hdf_framework_path/core/host/include", + "$hdf_framework_path/core/shared/include", + "$hdf_framework_path/include/osal", + "$hdf_framework_path/include/utils", + "$hdf_framework_path/utils/include", + "$hdf_framework_path/include/core", + "$hdf_framework_path/include/config", + "$hdf_framework_path/ability/sbuf/include", + "$hdf_framework_path/ability/config/hcs_parser/include", + "$hdf_uhdf_path/ipc/include", + "$hdf_uhdf_path/osal/include/", + "$hdf_uhdf_path/include/config", + "$hdf_uhdf_path/include/osal", + "$hdf_uhdf_path/include/host", + "$hdf_uhdf_path/shared/include", + "$hdf_uhdf_path/manager/include", + "$hdf_uhdf_path/host/include", + "$hdf_uhdf_path/security/include", + "include", + ] + + sources = [ + "$hdf_uhdf_path/shared/src/dev_attribute_parcel.c", + "$hdf_uhdf_path/shared/src/hdf_attribute_full.c", + "$hdf_uhdf_path/shared/src/hdf_device_info_full.c", + "$hdf_framework_path/core/manager/src/devhost_service_clnt.c", + "$hdf_framework_path/core/manager/src/device_token_clnt.c", + "$hdf_framework_path/core/manager/src/devmgr_service.c", + "$hdf_framework_path/core/manager/src/devsvc_manager.c", + "$hdf_framework_path/core/manager/src/hdf_driver_installer.c", + "$hdf_framework_path/core/manager/src/hdf_host_info.c", + "$hdf_framework_path/core/manager/src/power_state_manager.c", + "$hdf_framework_path/core/shared/src/hdf_device_info.c", + "$hdf_framework_path/core/shared/src/hdf_object_manager.c", + "$hdf_framework_path/core/shared/src/hdf_service_record.c", + "device_manager.c", + "src/devhost_service_proxy.c", + "src/device_token_proxy.c", + "src/devmgr_object_config.c", + "src/devmgr_pnp_service.c", + "src/devmgr_query_device.c", + "src/devmgr_service_full.c", + "src/devmgr_service_stub.c", + "src/devmgr_virtual_service.c", + "src/devsvc_manager_stub.c", + "src/driver_installer_full.c", + "src/hdf_get_attribute.c", + ] + + deps = [ + "$hdf_uhdf_path/ipc:libhdf_ipc_adapter", + "$hdf_uhdf_path/osal:libhdf_utils", + "$hdf_uhdf_path/config:uhdf_hcs_pkg", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + install_enable = true + subsystem_name = "hdf" + part_name = "hdf" +} + +ohos_prebuilt_etc("hdf_devmgr.rc") { + source = "hdf_devmgr.rc" + relative_install_dir = "init" + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/manager/device_manager.c b/uhdf2/manager/device_manager.c new file mode 100644 index 0000000..af254bc --- /dev/null +++ b/uhdf2/manager/device_manager.c @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_service.h" +#include "devmgr_service_full.h" +#include "hdf_base.h" +#include "hdf_log.h" + +#define HDF_LOG_TAG hdf_device_manager + +int main() +{ + HDF_LOGI("start hdf device manager"); + int status = HDF_FAILURE; + struct IDevmgrService* instance = DevmgrServiceGetInstance(); + if (instance == NULL) { + HDF_LOGE("Getting DevmgrService instance failed"); + return status; + } + + if (instance->StartService != NULL) { + status = instance->StartService(instance); + } + + if (status == HDF_SUCCESS) { + struct DevmgrServiceFull *fullService = (struct DevmgrServiceFull *)instance; + struct HdfMessageLooper *looper = &fullService->looper; + if ((looper != NULL) && (looper->Start != NULL)) { + looper->Start(looper); + } + } + + HDF_LOGE("starting device manager service failed, status is %d", status); + return status; +} diff --git a/uhdf2/manager/hdf_devmgr.rc b/uhdf2/manager/hdf_devmgr.rc new file mode 100755 index 0000000..4587b70 --- /dev/null +++ b/uhdf2/manager/hdf_devmgr.rc @@ -0,0 +1,19 @@ +# Copyright (c) 2021 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. + +service hdf_devmgr /system/bin/hdf_devmgr + user root + group system + +on post-fs-data + start hdf_devmgr diff --git a/uhdf2/manager/include/devhost_service_proxy.h b/uhdf2/manager/include/devhost_service_proxy.h new file mode 100644 index 0000000..8b1d031 --- /dev/null +++ b/uhdf2/manager/include/devhost_service_proxy.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 DEVHOST_SERVICE_PROXY_H +#define DEVHOST_SERVICE_PROXY_H + +#include "devhost_service_if.h" +#include "hdf_remote_service.h" + +struct DevHostServiceProxy { + struct IDevHostService super; + struct HdfRemoteService *remote; + struct HdfDeathRecipient recipient; + uint32_t hostId; +}; + +enum { + DEVHOST_SERVICE_ADD_DEVICE = 1, + DEVHOST_SERVICE_DEL_DEVICE, +}; + +struct IDevHostService *DevHostServiceProxyObtain(uint32_t hostId, struct HdfRemoteService *remote); +void DevHostServiceProxyRecycle(struct DevHostServiceProxy *inst); + +#endif /* DEVHOST_SERVICE_PROXY_H */ diff --git a/uhdf2/manager/include/device_token_proxy.h b/uhdf2/manager/include/device_token_proxy.h new file mode 100644 index 0000000..44e1707 --- /dev/null +++ b/uhdf2/manager/include/device_token_proxy.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 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 DEVICE_TOKEN_PROXY_H +#define DEVICE_TOKEN_PROXY_H + +#include "device_token_if.h" +#include "hdf_remote_service.h" + +struct HdfDevTokenProxy { + struct IHdfDeviceToken super; + struct HdfRemoteService *remote; +}; + +struct HdfDevTokenProxy *HdfDevTokenProxyObtain(struct HdfRemoteService *remote); +void HdfDevTokenProxyRecycle(struct HdfDevTokenProxy *inst); + +#endif /* DEVICE_TOKEN_PROXY_H */ diff --git a/uhdf2/manager/include/devmgr_pnp_service.h b/uhdf2/manager/include/devmgr_pnp_service.h new file mode 100644 index 0000000..0358e26 --- /dev/null +++ b/uhdf2/manager/include/devmgr_pnp_service.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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 DEVMGR_PNP_SERVICE_H +#define DEVMGR_PNP_SERVICE_H + +#include "devmgr_service.h" +#include "hdf_slist.h" + +#define PNP_HOST_NAME "pnp_host" + +struct HdfSList *DevmgrServiceGetPnpDeviceInfo(); + +int32_t DevmgrServiceRegPnpDevice( + struct IDevmgrService *devmgrSvc, const char *moduleName, const char *serviceName, + const void *privateData); + +int32_t DevmgrServiceUnRegPnpDevice( + struct IDevmgrService *devmgrSvc, const char *moduleName, const char *serviceName); + +#endif diff --git a/uhdf2/manager/include/devmgr_query_device.h b/uhdf2/manager/include/devmgr_query_device.h new file mode 100644 index 0000000..d12e532 --- /dev/null +++ b/uhdf2/manager/include/devmgr_query_device.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 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 DEVMGR_QUERY_DEVICE_H +#define DEVMGR_QUERY_DEVICE_H + +#include "devmgr_service_if.h" +#include "hdf_sbuf.h" + +int DevFillQueryDeviceInfo(struct IDevmgrService *super, struct HdfSBuf *data, struct HdfSBuf *reply); + +#endif /* DEVMGR_QUERY_DEVICE_H */ diff --git a/uhdf2/manager/include/devmgr_service_full.h b/uhdf2/manager/include/devmgr_service_full.h new file mode 100644 index 0000000..b746c84 --- /dev/null +++ b/uhdf2/manager/include/devmgr_service_full.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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 DEVMGR_SERVICE_FULL_H +#define DEVMGR_SERVICE_FULL_H + +#include "devmgr_service.h" +#include "hdf_message_task.h" +#include "hdf_message_looper.h" + +struct DevmgrServiceFull { + struct DevmgrService super; + struct HdfMessageLooper looper; + struct HdfMessageTask task; +}; + +enum { + DEVMGR_MESSAGE_DEVHOST_DIED, +}; + +void DevmgrServiceFullConstruct(struct DevmgrServiceFull *inst); +struct HdfObject *DevmgrServiceFullCreate(void); +struct HdfMessageTask *DevmgrServiceFullGetMessageTask(void); + +#endif /* DEVMGR_SERVICE_FULL_H */ diff --git a/uhdf2/manager/include/devmgr_service_stub.h b/uhdf2/manager/include/devmgr_service_stub.h new file mode 100644 index 0000000..464baa3 --- /dev/null +++ b/uhdf2/manager/include/devmgr_service_stub.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 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 DEVMGR_SERVICE_STUB_H +#define DEVMGR_SERVICE_STUB_H + +#include "hdf_remote_service.h" +#include "devmgr_service_full.h" +#include "osal_mutex.h" + +#define DEVICE_MANAGER_SERVICE "hdf_device_manager" +#define DEVICE_MANAGER_SERVICE_SA_ID 5002 + + +struct DevmgrServiceStub { + struct DevmgrServiceFull super; + struct HdfRemoteService *remote; + struct OsalMutex devmgrStubMutx; +}; + +enum { + DEVMGR_SERVICE_ATTACH_DEVICE_HOST = 1, + DEVMGR_SERVICE_ATTACH_DEVICE, + DEVMGR_SERVICE_REGIST_PNP_DEVICE, + DEVMGR_SERVICE_UNREGIST_PNP_DEVICE, + DEVMGR_SERVICE_QUERY_DEVICE, + DEVMGR_SERVICE_REGISTER_VIRTUAL_DEVICE, + DEVMGR_SERVICE_UNREGISTER_VIRTUAL_DEVICE, +}; + +struct HdfObject *DevmgrServiceStubCreate(void); +void DevmgrServiceStubRelease(struct HdfObject *object); + +#endif /* DEVMGR_SERVICE_STUB_H */ diff --git a/uhdf2/manager/include/devmgr_virtual_service.h b/uhdf2/manager/include/devmgr_virtual_service.h new file mode 100644 index 0000000..fa00deb --- /dev/null +++ b/uhdf2/manager/include/devmgr_virtual_service.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 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 DEVMGR_VIRTUAL_SERVICE_H +#define DEVMGR_VIRTUAL_SERVICE_H + +#include "devmgr_service_if.h" +#include "hdf_sbuf.h" + +int DevmgrServiceVirtualDevice( + struct IDevmgrService *inst, struct HdfSBuf *data, struct HdfSBuf *reply, bool flag); + +#endif /* DEVMGR_VIRTUAL_SERVICE_H */ diff --git a/uhdf2/manager/include/devsvc_manager_stub.h b/uhdf2/manager/include/devsvc_manager_stub.h new file mode 100644 index 0000000..4d9ea4d --- /dev/null +++ b/uhdf2/manager/include/devsvc_manager_stub.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 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 DEVSVC_MANAGER_STUB_H +#define DEVSVC_MANAGER_STUB_H + +#include "devsvc_manager.h" +#include "hdf_remote_service.h" + +#define DEVICE_SERVICE_MANAGER "hdf_device_service_manager" +#define DEVICE_SERVICE_MANAGER_SA_ID 5001 + +struct DevSvcManagerStub { + struct DevSvcManager super; + struct HdfRemoteService *remote; + struct HdfDeathRecipient recipient; +}; + +struct HdfObject *DevSvcManagerStubCreate(void); +void DevSvcManagerStubRelease(struct HdfObject *object); + +#endif /* DEVSVC_MANAGER_STUB_H */ diff --git a/uhdf2/manager/include/driver_installer_full.h b/uhdf2/manager/include/driver_installer_full.h new file mode 100644 index 0000000..e31dc41 --- /dev/null +++ b/uhdf2/manager/include/driver_installer_full.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2021 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 DRIVER_INSTALLER_FULL_H +#define DRIVER_INSTALLER_FULL_H + +#include "hdf_object.h" + +struct HdfObject *DriverInstallerFullCreate(void); +void DriverInstallerFullRelease(struct HdfObject *object); + +#endif /* DEVMGR_SERVICE_STUB_H */ diff --git a/uhdf2/manager/src/devhost_service_proxy.c b/uhdf2/manager/src/devhost_service_proxy.c new file mode 100644 index 0000000..d1ff9b9 --- /dev/null +++ b/uhdf2/manager/src/devhost_service_proxy.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devhost_service_proxy.h" +#include "dev_attribute_parcel.h" +#include "devmgr_service_full.h" +#include "hdf_base.h" +#include "hdf_device_info_full.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "osal_mem.h" +#include "osal_message.h" + +#define HDF_LOG_TAG devhost_service_proxy + +static int32_t DevHostServiceProxyOpsDevice( + struct IDevHostService *inst, const struct HdfDeviceInfo *attribute, int opsCode) +{ + int status = HDF_FAILURE; + const struct HdfDeviceInfoFull *fullAttribute = HdfDeviceInfoFullReinterpretCast(attribute); + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + struct HdfRemoteDispatcher *dipatcher = NULL; + struct HdfRemoteService *remoteService = NULL; + struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst; + if ((hostClnt->remote == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("Adding device failed, hostClnt->remote or data or reply is null"); + goto finished; + } + remoteService = hostClnt->remote; + dipatcher = remoteService->dispatcher; + DeviceAttributeFullWrite(fullAttribute, data); + status = dipatcher->Dispatch(remoteService, opsCode, data, reply); + HDF_LOGI("%s: dipatch finished, status is %d", __FUNCTION__, status); +finished: + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (data != NULL) { + HdfSBufRecycle(data); + } + return status; +} + +static int32_t DevHostServiceProxyAddDevice( + struct IDevHostService *inst, const struct HdfDeviceInfo *attribute) +{ + return DevHostServiceProxyOpsDevice(inst, attribute, DEVHOST_SERVICE_ADD_DEVICE); +} + +static int32_t DevHostServiceProxyDelDevice( + struct IDevHostService *inst, const struct HdfDeviceInfo *attribute) +{ + return DevHostServiceProxyOpsDevice(inst, attribute, DEVHOST_SERVICE_DEL_DEVICE); +} + +void DevHostServiceProxyOnRemoteDied(struct HdfDeathRecipient *recipient, struct HdfRemoteService *service) +{ + if (recipient == NULL || service == NULL) { + return; + } + + struct HdfMessageTask *task = DevmgrServiceFullGetMessageTask(); + if (task == NULL) { + return; + } + struct HdfMessage *message = HdfMessageObtain(0); + if (message == NULL) { + return; + } + struct DevHostServiceProxy *proxy = HDF_SLIST_CONTAINER_OF( + struct HdfDeathRecipient, recipient, struct DevHostServiceProxy, recipient); + message->messageId = DEVMGR_MESSAGE_DEVHOST_DIED; + message->data[0] = (void *)(uintptr_t)proxy->hostId; + HDF_LOGD("%s: hostId is %u", __FUNCTION__, proxy->hostId); + task->SendMessage(task, message, false); +} + +void DevHostServiceProxyConstruct( + struct DevHostServiceProxy *inst, struct HdfRemoteService *remote) +{ + inst->remote = remote; + inst->super.AddDevice = DevHostServiceProxyAddDevice; + inst->super.DelDevice = DevHostServiceProxyDelDevice; + inst->recipient.OnRemoteDied = DevHostServiceProxyOnRemoteDied; + HdfRemoteServiceAddDeathRecipient(remote, &inst->recipient); +} + +struct IDevHostService *DevHostServiceProxyObtain(uint32_t hostId, struct HdfRemoteService *remote) +{ + struct DevHostServiceProxy *instance = + (struct DevHostServiceProxy *)OsalMemCalloc(sizeof(struct DevHostServiceProxy)); + if (instance != NULL) { + instance->hostId = hostId; + DevHostServiceProxyConstruct(instance, remote); + } + return (struct IDevHostService *)instance; +} + +void DevHostServiceProxyRecycle(struct DevHostServiceProxy *inst) +{ + if (inst != NULL) { + if (inst->remote != NULL) { + HdfRemoteServiceRecycle(inst->remote); + inst->remote = NULL; + } + OsalMemFree(inst); + } +} + diff --git a/uhdf2/manager/src/device_token_proxy.c b/uhdf2/manager/src/device_token_proxy.c new file mode 100644 index 0000000..189f2ab --- /dev/null +++ b/uhdf2/manager/src/device_token_proxy.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "device_token_proxy.h" +#include "osal_mem.h" + +static void HdfDevTokenProxyConstruct(struct HdfDevTokenProxy *inst, struct HdfRemoteService *remote) +{ + inst->remote = remote; +} + +struct HdfDevTokenProxy *HdfDevTokenProxyObtain(struct HdfRemoteService *remote) +{ + struct HdfDevTokenProxy *instance = + (struct HdfDevTokenProxy *)OsalMemCalloc(sizeof(struct HdfDevTokenProxy)); + if (instance != NULL) { + HdfDevTokenProxyConstruct(instance, remote); + } + return instance; +} + +void HdfDevTokenProxyRecycle(struct HdfDevTokenProxy *inst) +{ + if (inst != NULL) { + if (inst->remote != NULL) { + HdfRemoteServiceRecycle(inst->remote); + inst->remote = NULL; + } + OsalMemFree(inst); + } +} + diff --git a/uhdf2/manager/src/devmgr_object_config.c b/uhdf2/manager/src/devmgr_object_config.c new file mode 100644 index 0000000..8848167 --- /dev/null +++ b/uhdf2/manager/src/devmgr_object_config.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_object_manager.h" +#include "devmgr_service_stub.h" +#include "devsvc_manager_stub.h" +#include "driver_installer_full.h" +#include "hdf_log.h" + +#define HDF_LOG_TAG devmgr_object_config + +const struct HdfObjectCreator g_fullDevMgrObjectCreators[] = { + [HDF_OBJECT_ID_DEVMGR_SERVICE] = {.Create = DevmgrServiceStubCreate}, + [HDF_OBJECT_ID_DEVSVC_MANAGER] = {.Create = DevSvcManagerStubCreate}, + [HDF_OBJECT_ID_DRIVER_INSTALLER] = {.Create = DriverInstallerFullCreate}, +}; + +const struct HdfObjectCreator* HdfObjectManagerGetCreators(int objectId) +{ + int numConfigs = sizeof(g_fullDevMgrObjectCreators) / sizeof(g_fullDevMgrObjectCreators[0]); + if (objectId < 0 || objectId >= numConfigs) { + HDF_LOGE("Invalid objectId %d", objectId); + return NULL; + } + return &g_fullDevMgrObjectCreators[objectId]; +} diff --git a/uhdf2/manager/src/devmgr_pnp_service.c b/uhdf2/manager/src/devmgr_pnp_service.c new file mode 100644 index 0000000..827b2b1 --- /dev/null +++ b/uhdf2/manager/src/devmgr_pnp_service.c @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_pnp_service.h" +#include "devhost_service_clnt.h" +#include "hdf_device_info_full.h" +#include "hdf_driver_installer.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "osal_time.h" +#include "securec.h" + +#define PNP_SLEEP_TIME 50 // ms + +static struct HdfSList *g_deviceInfos = NULL; +static uint16_t g_hostId = 0xFFFF; +static uint32_t g_deviceId = 0; + +struct HdfSList *DevmgrServiceGetPnpDeviceInfo() +{ + return g_deviceInfos; +} + +void DevmgrServiceSetPnpHostId(uint16_t hostId) +{ + struct HdfSListIterator it; + struct HdfDeviceInfoFull *deviceInfo = NULL; + g_hostId = hostId; + HdfSListIteratorInit(&it, g_deviceInfos); + while (HdfSListIteratorHasNext(&it)) { + deviceInfo = (struct HdfDeviceInfoFull *)HdfSListIteratorNext(&it); + deviceInfo->super.hostId = hostId; + } +} + +static bool DevmgrServiceIsPnpDeviceExist(const char *moduleName, const char *serviceName) +{ + struct HdfSListIterator it; + struct HdfDeviceInfoFull *deviceInfo = NULL; + HdfSListIteratorInit(&it, g_deviceInfos); + while (HdfSListIteratorHasNext(&it)) { + deviceInfo = (struct HdfDeviceInfoFull *)HdfSListIteratorNext(&it); + if ((strcmp(deviceInfo->super.moduleName, moduleName) == 0) && + (strcmp(deviceInfo->super.svcName, serviceName) == 0)) { + return true; + } + } + return false; +} + +static bool DevmgrServiceAddPnpDeviceInfo(struct HdfDeviceInfoFull *deviceInfo) +{ + if (g_deviceInfos == NULL) { + g_deviceInfos = OsalMemCalloc(sizeof(struct HdfSList)); + if (g_deviceInfos == NULL) { + return false; + } + } + deviceInfo->super.deviceId = g_deviceId++; // HdfSListCount g_deviceInfos + deviceInfo->super.hostId = g_hostId; + deviceInfo->super.policy = SERVICE_POLICY_PUBLIC; + deviceInfo->super.preload = DEVICE_PRELOAD_ENABLE; + deviceInfo->super.deviceMatchAttr = ""; + HdfSListAdd(g_deviceInfos, &deviceInfo->super.node); + return true; +} + +static bool DevmgrServiceAddPrivateData( + struct HdfDeviceInfoFull *deviceInfo, const void *privateData) +{ + if (privateData != NULL) { + deviceInfo->super.private = (const void *)OsalMemCalloc(sizeof(struct UsbPnpNotifyServiceInfo)); + if (deviceInfo->super.private != NULL) { + int32_t length = ((struct UsbPnpNotifyServiceInfo *)(privateData))->length; + HDF_LOGD("%{public}s: private length=%{public}d", __func__, length); + + memcpy_s((void *)(deviceInfo->super.private), sizeof(struct UsbPnpNotifyServiceInfo), privateData, length); + if (deviceInfo->super.private == NULL) { + HDF_LOGE("%{public}s: memcpy_s private error", __func__); + return false; + } + } else { + HDF_LOGE("%{public}s: OsalMemCalloc private error", __func__); + return false; + } + } else { + HDF_LOGW("%{public}s: privateData is NULL", __func__); + } + + return true; +} + +static bool DevmgrServiceAddPnpDevice( + const char *moduleName, const char *serviceName, const void *privateData) +{ + if (moduleName == NULL || serviceName == NULL) { + return false; + } + if (DevmgrServiceIsPnpDeviceExist(moduleName, serviceName)) { + HDF_LOGE("%s adding pnp device failed, %s already exist", __func__, moduleName); + return false; + } + struct HdfDeviceInfoFull *deviceInfo = HdfDeviceInfoFullNewInstance(); + if (deviceInfo == NULL) { + return false; + } + deviceInfo->super.moduleName = strdup(moduleName); + if (deviceInfo->super.moduleName == NULL) { + HdfDeviceInfoFullFreeInstance(deviceInfo); + return false; + } + deviceInfo->super.svcName = strdup(serviceName); + if (deviceInfo->super.svcName == NULL) { + HdfDeviceInfoFullFreeInstance(deviceInfo); + return false; + } + + if (!DevmgrServiceAddPrivateData(deviceInfo, privateData)) { + HdfDeviceInfoFullFreeInstance(deviceInfo); + return false; + } + + if (!DevmgrServiceAddPnpDeviceInfo(deviceInfo)) { + HdfDeviceInfoFullFreeInstance(deviceInfo); + return false; + } + return true; +} + +static void DevmgrServiceDelPnpDevice(const char *moduleName, const char *serviceName) +{ + struct HdfSListIterator it; + struct HdfDeviceInfoFull *deviceInfo = NULL; + HdfSListIteratorInit(&it, g_deviceInfos); + while (HdfSListIteratorHasNext(&it)) { + deviceInfo = (struct HdfDeviceInfoFull *)HdfSListIteratorNext(&it); + if ((strcmp(deviceInfo->super.moduleName, moduleName) == 0) && + (strcmp(deviceInfo->super.svcName, serviceName) == 0)) { + HdfSListRemove(g_deviceInfos, &deviceInfo->super.node); + HdfDeviceInfoFullFreeInstance(deviceInfo); + return; + } + } +} + +struct DevHostServiceClnt *DevmgrServiceGetPnpHostClnt(struct DevmgrService *inst) +{ + struct HdfSListIterator it; + struct DevHostServiceClnt *hostClnt = NULL; + if (inst == NULL) { + return NULL; + } + + HdfSListIteratorInit(&it, &inst->hosts); + while (HdfSListIteratorHasNext(&it)) { + hostClnt = (struct DevHostServiceClnt *)HdfSListIteratorNext(&it); + if (strcmp(hostClnt->hostName, PNP_HOST_NAME) == 0) { + return hostClnt; + } + } + return NULL; +} + +int32_t DevmgrServiceStartPnpHost(struct DevmgrService *inst) +{ + struct DevHostServiceClnt *hostClnt = NULL; + struct IDriverInstaller *installer = NULL; + int32_t ret = HDF_FAILURE; + + installer = DriverInstallerGetInstance(); + if ((installer == NULL) || (installer->StartDeviceHost == NULL)) { + HDF_LOGE("%s installer or installer->StartDeviceHost is null", __func__); + return ret; + } + + uint16_t hostId = HdfSListCount(&inst->hosts); + DevmgrServiceSetPnpHostId(hostId); + hostClnt = DevHostServiceClntNewInstance(hostId, PNP_HOST_NAME); + if (hostClnt == NULL) { + HDF_LOGW("%s creating new device host client failed", __func__); + return ret; + } + HdfSListAdd(&inst->hosts, &hostClnt->node); + ret = installer->StartDeviceHost(hostClnt->hostId, hostClnt->hostName); + if (ret != HDF_SUCCESS) { + HDF_LOGW("%s starting host failed, host name is %s", __func__, hostClnt->hostName); + HdfSListRemove(&inst->hosts, &hostClnt->node); + DevHostServiceClntFreeInstance(hostClnt); + } + return ret; +} + +static int DevmgrServiceInstallDevice(struct DevHostServiceClnt *hostClnt, const char *serviceName) +{ + int ret = HDF_FAILURE; + struct HdfSListIterator it; + struct HdfDeviceInfo *deviceInfo = NULL; + struct IDevHostService *devHostSvcIf = NULL; + + devHostSvcIf = (struct IDevHostService *)hostClnt->hostService; + if (devHostSvcIf == NULL || devHostSvcIf->AddDevice == NULL) { + HDF_LOGE("devHostSvcIf or devHostSvcIf->AddDevice is null"); + return ret; + } + HdfSListIteratorInit(&it, hostClnt->deviceInfos); + while (HdfSListIteratorHasNext(&it)) { + deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&it); + if (strcmp(deviceInfo->svcName, serviceName) == 0) { + ret = devHostSvcIf->AddDevice(devHostSvcIf, deviceInfo); + if (ret != HDF_SUCCESS) { + HDF_LOGE("Installing %s driver failed, ret = %d", deviceInfo->svcName, ret); + } + break; + } + } + return ret; +} + +int32_t DevmgrServiceRegPnpDevice( + struct IDevmgrService *devmgrSvc, const char *moduleName, const char *serviceName, + const void *privateData) +{ + int32_t ret = HDF_FAILURE; + struct DevmgrService *inst = (struct DevmgrService *)devmgrSvc; + if (inst == NULL) { + return HDF_ERR_INVALID_PARAM; + } + struct DevHostServiceClnt *hostClnt = DevmgrServiceGetPnpHostClnt(inst); + if (hostClnt == NULL) { + ret = DevmgrServiceStartPnpHost(inst); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s add pnp device failed!", __func__); + return ret; + } + OsalMSleep(PNP_SLEEP_TIME); + hostClnt = DevmgrServiceGetPnpHostClnt(inst); + } + if (hostClnt == NULL || hostClnt->hostService == NULL) { + HDF_LOGE("%s host service is null!", __func__); + return HDF_ERR_INVALID_OBJECT; + } + if (!DevmgrServiceAddPnpDevice(moduleName, serviceName, privateData)) { + HDF_LOGE("%s add pnp device failed!", __func__); + return ret; + } + + hostClnt->deviceInfos = DevmgrServiceGetPnpDeviceInfo(); + ret = DevmgrServiceInstallDevice(hostClnt, serviceName); + if (ret != HDF_SUCCESS) { + DevmgrServiceDelPnpDevice(moduleName, serviceName); + HDF_LOGE("%s: failed %d %s", __func__, hostClnt->hostId, hostClnt->hostName); + } + return ret; +} + + +int32_t DevmgrServiceUnRegPnpDevice( + struct IDevmgrService *devmgrSvc, const char *moduleName, const char *serviceName) +{ + int ret = HDF_FAILURE; + struct DevmgrService *inst = (struct DevmgrService *)devmgrSvc; + struct DevHostServiceClnt *hostClnt = DevmgrServiceGetPnpHostClnt(inst); + if (hostClnt == NULL || hostClnt->hostService == NULL) { + HDF_LOGE("%s host service is not init", __func__); + OsalMSleep(PNP_SLEEP_TIME); + hostClnt = DevmgrServiceGetPnpHostClnt(inst); + if (hostClnt == NULL || hostClnt->hostService == NULL) { + HDF_LOGE("%s host service is not init", __func__); + return ret; + } + } + + struct HdfSListIterator it; + struct HdfDeviceInfo *deviceInfo = NULL; + struct IDevHostService *devHostSvcIf = NULL; + + devHostSvcIf = (struct IDevHostService *)hostClnt->hostService; + if (devHostSvcIf->DelDevice == NULL) { + HDF_LOGE("%s host del device is not init", __func__); + return ret; + } + HdfSListIteratorInit(&it, hostClnt->deviceInfos); + while (HdfSListIteratorHasNext(&it)) { + deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&it); + if ((strcmp(deviceInfo->moduleName, moduleName) == 0) && + (strcmp(deviceInfo->svcName, serviceName) == 0)) { + deviceInfo->status = HDF_SERVICE_UNUSABLE; + ret = devHostSvcIf->DelDevice(devHostSvcIf, deviceInfo); + DevmgrServiceDelPnpDevice(moduleName, serviceName); + } + } + return ret; +} + diff --git a/uhdf2/manager/src/devmgr_query_device.c b/uhdf2/manager/src/devmgr_query_device.c new file mode 100644 index 0000000..df67ddd --- /dev/null +++ b/uhdf2/manager/src/devmgr_query_device.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devhost_service_clnt.h" +#include "devmgr_service.h" +#include "hdf_base.h" +#include "hdf_cstring.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" + +#define HDF_LOG_TAG devmgr_query_device + +int DevFillQueryDeviceInfo(struct IDevmgrService *inst, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct DevmgrService *devMgrSvc = (struct DevmgrService *)inst; + struct HdfSListIterator itHost; + struct HdfSListIterator itDeviceInfo; + struct HdfDeviceInfo *deviceInfo = NULL; + struct DevHostServiceClnt *hostClnt = NULL; + bool flag = false; + uint16_t status = 0; + + if (devMgrSvc == NULL || data == NULL || reply == NULL) { + HDF_LOGE("%s failed, parameter is null", __func__); + return HDF_FAILURE; + } + + HdfSbufReadUint16(data, &status); + if (status != HDF_SERVICE_UNUSABLE && status != HDF_SERVICE_USABLE) { + HDF_LOGE("%s failed, status is invalid %d", __func__, status); + return HDF_FAILURE; + } + HDF_LOGD("%s, %d", __func__, status); + + HdfSListIteratorInit(&itHost, &devMgrSvc->hosts); + while (HdfSListIteratorHasNext(&itHost)) { + hostClnt = (struct DevHostServiceClnt *)HdfSListIteratorNext(&itHost); + HdfSListIteratorInit(&itDeviceInfo, hostClnt->deviceInfos); + HDF_LOGD("%s, host:%s %d", __func__, hostClnt->hostName, hostClnt->hostId); + while (HdfSListIteratorHasNext(&itDeviceInfo)) { + deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&itDeviceInfo); + HDF_LOGD("%s %s policy:%d status:%d type:%d", + __func__, deviceInfo->svcName, deviceInfo->policy, deviceInfo->status, deviceInfo->deviceType); + if ((deviceInfo->policy == SERVICE_POLICY_CAPACITY || deviceInfo->policy == SERVICE_POLICY_PUBLIC) && + deviceInfo->status == status) { + flag = HdfSbufWriteString(reply, deviceInfo->svcName); + if (flag == false) { + HDF_LOGE("%s writing string failed %s", __func__, deviceInfo->svcName); + return HDF_FAILURE; + } + flag = HdfSbufWriteInt32(reply, deviceInfo->deviceType); + if (flag == false) { + HDF_LOGE("%s writing int32 failed %d", __func__, deviceInfo->deviceType); + return HDF_FAILURE; + } + } + } + } + + return HDF_SUCCESS; +} + diff --git a/uhdf2/manager/src/devmgr_service_full.c b/uhdf2/manager/src/devmgr_service_full.c new file mode 100644 index 0000000..7134dbc --- /dev/null +++ b/uhdf2/manager/src/devmgr_service_full.c @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_service_full.h" +#include "devhost_service.h" +#include "devhost_service_clnt.h" +#include "device_token_clnt.h" +#include "hdf_device_token.h" +#include "hdf_driver_installer.h" +#include "hdf_log.h" +#include "hdf_map.h" +#include "hdf_message_looper.h" +#include "osal_message.h" + +#define HDF_LOG_TAG devmgr_service_full +#define INVALID_PID (-1) + +static Map g_hostMap = {0}; +#define HOST_INIT_DIE_NUM 1 +#define HOST_MAX_DIE_NUM 3 + +static int32_t DevmgrServiceFullHandleDeviceHostDied(struct DevHostServiceClnt *hostClnt) +{ + if (!HdfSListIsEmpty(hostClnt->deviceInfos)) { + if (g_hostMap.nodeSize == 0) { + MapInit(&g_hostMap); + } + int *hostDieValue = (int *)MapGet(&g_hostMap, hostClnt->hostName); + if (hostDieValue == NULL) { + int hostDieNum = HOST_INIT_DIE_NUM; + MapSet(&g_hostMap, hostClnt->hostName, &hostDieNum, sizeof(int)); + } else { + if (*hostDieValue > HOST_MAX_DIE_NUM) { + *hostDieValue = 0; + return INVALID_PID; + } + (*hostDieValue)++; + } + struct IDriverInstaller *installer = DriverInstallerGetInstance(); + if ((installer != NULL) && (installer->StartDeviceHost)) { + hostClnt->hostPid = installer->StartDeviceHost(hostClnt->hostId, hostClnt->hostName); + return hostClnt->hostPid; + } + } + return INVALID_PID; +} + +void DevmgrServiceFullOnDeviceHostDied(struct DevmgrServiceFull *inst, uint32_t hostId) +{ + (void)hostId; + struct HdfSListIterator it; + struct DevHostServiceClnt *hostClnt = NULL; + if (inst == NULL) { + return; + } + OsalMutexLock(&inst->super.devMgrMutex); + HdfSListIteratorInit(&it, &inst->super.hosts); + while (HdfSListIteratorHasNext(&it)) { + hostClnt = (struct DevHostServiceClnt *)HdfSListIteratorNext(&it); + if (hostClnt->hostId == hostId) { + int32_t ret = DevmgrServiceFullHandleDeviceHostDied(hostClnt); + if (ret == INVALID_PID) { + HdfSListRemove(&inst->super.hosts, &hostClnt->node); + DevHostServiceClntFreeInstance(hostClnt); + } + break; + } + } + OsalMutexUnlock(&inst->super.devMgrMutex); +} + +int32_t DevmgrServiceFullDispatchMessage(struct HdfMessageTask *task, struct HdfMessage *msg) +{ + (void)task; + struct DevmgrServiceFull *fullService = + (struct DevmgrServiceFull *)DevmgrServiceGetInstance(); + if (msg == NULL) { + HDF_LOGE("Input msg is null"); + return HDF_ERR_INVALID_PARAM; + } + switch (msg->messageId) { + case DEVMGR_MESSAGE_DEVHOST_DIED: { + int hostId = (int)(uintptr_t)msg->data[0]; + DevmgrServiceFullOnDeviceHostDied(fullService, hostId); + break; + } + default: { + HDF_LOGE("Message is wrong, message is %u", msg->messageId); + } + } + + return HDF_SUCCESS; +} + +struct HdfMessageTask *DevmgrServiceFullGetMessageTask() +{ + struct DevmgrServiceFull *fullService = + (struct DevmgrServiceFull *)DevmgrServiceGetInstance(); + if (fullService != NULL) { + HDF_LOGE("Get message task failed, fullService is null"); + return &fullService->task; + } + return NULL; +} + +void DevmgrServiceFullConstruct(struct DevmgrServiceFull *inst) +{ + static struct IHdfMessageHandler handler = { + .Dispatch = DevmgrServiceFullDispatchMessage + }; + if (inst != NULL) { + HdfMessageLooperConstruct(&inst->looper); + DevmgrServiceConstruct(&inst->super); + HdfMessageTaskConstruct(&inst->task, &inst->looper, &handler); + } +} + +struct HdfObject *DevmgrServiceFullCreate() +{ + static struct DevmgrServiceFull *instance = NULL; + if (instance == NULL) { + static struct DevmgrServiceFull fullInstance; + DevmgrServiceFullConstruct(&fullInstance); + instance = &fullInstance; + } + return (struct HdfObject *)instance; +} + +int DeviceManagerIsQuickLoad(void) +{ + return false; +} diff --git a/uhdf2/manager/src/devmgr_service_stub.c b/uhdf2/manager/src/devmgr_service_stub.c new file mode 100644 index 0000000..1119651 --- /dev/null +++ b/uhdf2/manager/src/devmgr_service_stub.c @@ -0,0 +1,996 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_service_stub.h" +#include "devhost_service_clnt.h" +#include "devhost_service_proxy.h" +#include "device_token_proxy.h" +#include "devmgr_query_device.h" +#include "devmgr_pnp_service.h" +#include "devmgr_virtual_service.h" +#include "devsvc_manager.h" +#include "hdf_device_info_full.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "osal_mem.h" +#include "osal_sysevent.h" +#include "securec.h" +#include "hdf_attribute_manager.h" +#include "device_resource_if.h" +#include "hcs_tree_if.h" +#include + +#define HDF_LOG_TAG devmgr_service_stub + +#define USB_PNP_NOTIFY_SERVICE_NAME "hdf_usb_pnp_notify_service" + +struct UsbPnpMatchIdTable { + const char *moduleName; + const char *serviceName; + + int32_t interfaceLength; + + uint8_t length; + + uint16_t matchFlag; + + uint16_t vendorId; + uint16_t productId; + + uint16_t bcdDeviceLow; + uint16_t bcdDeviceHigh; + + uint8_t deviceClass; + uint8_t deviceSubClass; + uint8_t deviceProtocol; + + uint8_t interfaceClass; + uint8_t interfaceSubClass; + uint8_t interfaceProtocol; + + uint8_t interfaceNumber[USB_PNP_INFO_MAX_INTERFACES]; +}; + +struct UsbPnpDeviceListTable { + struct DListHead list; + const char *moduleName; + const char *serviceName; + uint32_t usbDevAddr; + int32_t devNum; + int32_t busNum; + int32_t interfaceLength; + uint8_t interfaceNumber[USB_PNP_INFO_MAX_INTERFACES]; +}; + +struct UsbPnpRemoveInfo { + uint8_t removeType; + uint32_t usbDevAddr; + int32_t devNum; + int32_t busNum; + uint8_t interfaceNum; +}; + +struct UsbPnpDeviceListTable *g_usbPnpDeviceTableList = NULL; + +static int32_t DevmgrServiceStubDispatchAttachDevice( + struct IDevmgrService *devmgrSvc, struct HdfSBuf *data) +{ + uint32_t hostId; + uint32_t deviceId; + if (!HdfSbufReadUint32(data, &hostId) || !HdfSbufReadUint32(data, &deviceId)) { + HDF_LOGE("%s:failed to get host id and device id", __func__); + return HDF_ERR_INVALID_PARAM; + } + + struct HdfDevTokenProxy *tokenClnt = HdfDevTokenProxyObtain(NULL); + if (tokenClnt == NULL) { + return HDF_FAILURE; + } + struct HdfDeviceInfo deviceInfo; + deviceInfo.deviceId = deviceId; + deviceInfo.hostId = hostId; + return devmgrSvc->AttachDevice(devmgrSvc, &deviceInfo, &tokenClnt->super); +} + +static int32_t DevmgrServiceStubDispatchPnpDevice( + struct IDevmgrService *devmgrSvc, struct HdfSBuf *data, bool isReg) +{ + int32_t ret = HDF_FAILURE; + uint32_t infoSize = 0; + struct UsbPnpNotifyServiceInfo *privateData = NULL; + + const char *moduleName = HdfSbufReadString(data); + if (moduleName == NULL) { + return ret; + } + const char *serviceName = HdfSbufReadString(data); + if (serviceName == NULL) { + return ret; + } + + if (!HdfSbufReadBuffer(data, (const void **)(&privateData), &infoSize)) { + HDF_LOGW("%{public}s: HdfSbufReadBuffer privateData error!", __func__); + privateData = NULL; + } + if (privateData == NULL) { + HDF_LOGW("%{public}s: privateData is NULL!", __func__); + } else { + HDF_LOGD("%{public}s: length=%{public}d, infoSize=%{public}d, moduleName=%{public}s, serviceName=%{public}s", + __func__, privateData->length, infoSize, moduleName, serviceName); + } + + if (isReg) { + ret = DevmgrServiceRegPnpDevice(devmgrSvc, moduleName, serviceName, (const void *)privateData); + } else { + ret = DevmgrServiceUnRegPnpDevice(devmgrSvc, moduleName, serviceName); + } + return ret; +} + +int32_t DevmgrServiceStubDispatch( + struct HdfRemoteService* stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + struct DevmgrServiceStub *serviceStub = (struct DevmgrServiceStub *)stub; + if (serviceStub == NULL) { + return HDF_ERR_INVALID_PARAM; + } + struct IDevmgrService *super = (struct IDevmgrService *)&serviceStub->super; + HDF_LOGE("%s devmgr service stub dispatch cmd %d", __func__, code); + switch (code) { + case DEVMGR_SERVICE_ATTACH_DEVICE_HOST: { + uint32_t hostId = 0; + if (!HdfSbufReadUint32(data, &hostId)) { + HDF_LOGE("invalid host id"); + return HDF_FAILURE; + } + struct HdfRemoteService *service = HdfSBufReadRemoteService(data); + struct IDevHostService *hostIf = DevHostServiceProxyObtain(hostId, service); + ret = super->AttachDeviceHost(super, hostId, hostIf); + break; + } + case DEVMGR_SERVICE_ATTACH_DEVICE: { + ret = DevmgrServiceStubDispatchAttachDevice(super, data); + break; + } + case DEVMGR_SERVICE_REGIST_PNP_DEVICE: { + ret = DevmgrServiceStubDispatchPnpDevice(super, data, true); + break; + } + case DEVMGR_SERVICE_UNREGIST_PNP_DEVICE: { + ret = DevmgrServiceStubDispatchPnpDevice(super, data, false); + break; + } + case DEVMGR_SERVICE_QUERY_DEVICE: { + ret = DevFillQueryDeviceInfo(super, data, reply); + break; + } + case DEVMGR_SERVICE_REGISTER_VIRTUAL_DEVICE: { + ret = DevmgrServiceVirtualDevice(super, data, reply, true); + break; + } + case DEVMGR_SERVICE_UNREGISTER_VIRTUAL_DEVICE: { + ret = DevmgrServiceVirtualDevice(super, data, reply, false); + break; + } + default: + break; + } + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s devmgr service stub dispach failed, cmd id is %d", __func__, code); + HdfSbufWriteInt32(reply, ret); + } + + return ret; +} + +static struct HdfRemoteDispatcher g_devmgrDispatcher = { + .Dispatch = DevmgrServiceStubDispatch +}; + +static struct HdfSBuf *DevmgrServiceStubBufCreate(const char *moduleName, + const char *serviceName, struct UsbPnpNotifyServiceInfo serviceInfo) +{ + struct HdfSBuf *pnpData = NULL; + char modName[128] = {0}; + + pnpData = HdfSBufObtainDefaultSize(); + if (pnpData == NULL) { + HDF_LOGE("%{public}s: HdfSBufTypedObtain pnpData fail", __func__); + return NULL; + } + + if (sprintf_s(modName, sizeof(modName) - 1, "lib%s.z.so", moduleName) < 0) { + HDF_LOGE("%{public}s: sprintf_s modName fail", __func__); + goto out; + } + if (!HdfSbufWriteString(pnpData, modName)) { + HDF_LOGE("%{public}s: write modName name failed!", __func__); + goto out; + } + + if (!HdfSbufWriteString(pnpData, serviceName)) { + HDF_LOGE("%{public}s: write service name failed!", __func__); + goto out; + } + + if (!HdfSbufWriteBuffer(pnpData, (const void *)(&serviceInfo), sizeof(struct UsbPnpNotifyServiceInfo))) { + HDF_LOGE("%{public}s: write privateData failed!", __func__); + goto out; + } + + HDF_LOGD("%{public}s: create: devNum=%{public}d, busNum=%{public}d, moduleName=%{public}s, \ + serviceName=%{public}s", __func__, serviceInfo.devNum, serviceInfo.busNum, modName, serviceName); + + return pnpData; + +out: + HdfSBufRecycle(pnpData); + + return NULL; +} + +static bool DevmgrServiceStubUsbMatchDevice(struct UsbPnpNotifyMatchInfoTable *dev, + const struct UsbPnpMatchIdTable *id) +{ + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_VENDOR) && + (id->vendorId != dev->deviceInfo.vendorId)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_PRODUCT) && + (id->productId != dev->deviceInfo.productId)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_LOW) && + (id->bcdDeviceLow > dev->deviceInfo.bcdDeviceLow)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_HIGH) && + (id->bcdDeviceHigh < dev->deviceInfo.bcdDeviceHigh)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_CLASS) && + (id->deviceClass != dev->deviceInfo.deviceClass)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_SUBCLASS) && + (id->deviceSubClass != dev->deviceInfo.deviceSubClass)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_DEV_PROTOCOL) && + (id->deviceProtocol != dev->deviceInfo.deviceProtocol)) { + return false; + } + + return true; +} + +static bool DevmgrServiceStubUsbMatchOneIdIntf(struct UsbPnpNotifyMatchInfoTable *dev, + int8_t index, struct UsbPnpMatchIdTable *id) +{ + int32_t i; + uint8_t j; + + if (dev->deviceInfo.deviceClass == USB_CLASS_VENDOR_SPEC && + !(id->matchFlag & USB_PNP_NOTIFY_MATCH_VENDOR) && + (id->matchFlag & (USB_PNP_NOTIFY_MATCH_INT_CLASS | USB_PNP_NOTIFY_MATCH_INT_SUBCLASS | + USB_PNP_NOTIFY_MATCH_INT_PROTOCOL | USB_PNP_NOTIFY_MATCH_INT_NUMBER))) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_CLASS) && + (id->interfaceClass != dev->interfaceInfo[index].interfaceClass)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_SUBCLASS) && + (id->interfaceSubClass != dev->interfaceInfo[index].interfaceSubClass)) { + return false; + } + + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_PROTOCOL) && + (id->interfaceProtocol != dev->interfaceInfo[index].interfaceProtocol)) { + return false; + } + + if (id->interfaceLength == 1) { + if ((id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER) && + (id->interfaceNumber[0] != dev->interfaceInfo[index].interfaceNumber)) { + return false; + } + + id->interfaceNumber[0] = dev->interfaceInfo[index].interfaceNumber; + } else { + if (id->matchFlag & USB_PNP_NOTIFY_MATCH_INT_NUMBER) { + for (i = 0; i < id->interfaceLength; i++) { + for (j = 0; j < dev->numInfos; j++) { + if (id->interfaceNumber[i] == dev->interfaceInfo[j].interfaceNumber) { + break; + } + } + + if (j >= dev->numInfos) { + return false; + } + } + } else { + id->interfaceLength = 1; + id->interfaceNumber[0] = dev->interfaceInfo[index].interfaceNumber; + } + } + + return true; +} + +static int32_t DevmgrServiceStubParseIdTable(const struct DeviceResourceNode *node, + struct DeviceResourceIface *devResIface, struct UsbPnpMatchIdTable *table) +{ + if (node == NULL || table == NULL || devResIface == NULL) { + HDF_LOGE("%{public}s: node or table or devResIface is NULL!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetString(node, "moduleName", &table->moduleName, "") != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read moduleName fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetString(node, "serviceName", &table->serviceName, "") != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read serviceName fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "length", &table->length, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read length fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint16(node, "matchFlag", &table->matchFlag, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read matchFlag fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint16(node, "vendorId", &table->vendorId, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read vendorId fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint16(node, "productId", &table->productId, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read productId fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint16(node, "bcdDeviceLow", &table->bcdDeviceLow, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read bcdDeviceLow fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint16(node, "bcdDeviceHigh", &table->bcdDeviceHigh, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read bcdDeviceHigh fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "deviceClass", &table->deviceClass, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read deviceClass fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "deviceSubClass", &table->deviceSubClass, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read deviceSubClass fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "deviceProtocol", &table->deviceProtocol, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read deviceProtocol fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "interfaceClass", &table->interfaceClass, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read interfaceClass fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "interfaceSubClass", &table->interfaceSubClass, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read interfaceSubClass fail!", __func__); + return HDF_FAILURE; + } + + if (devResIface->GetUint8(node, "interfaceProtocol", &table->interfaceProtocol, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read interfaceProtocol fail!", __func__); + return HDF_FAILURE; + } + + table->interfaceLength = devResIface->GetElemNum(node, "interfaceNumber"); + if (table->interfaceLength <= 0) { + HDF_LOGE("%{public}s: read interfaceLength=%{public}d fail!", __func__, table->interfaceLength); + return HDF_FAILURE; + } + if (devResIface->GetUint8Array(node, "interfaceNumber", table->interfaceNumber, \ + table->interfaceLength, 0) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: read interfaceNumber fail!", __func__); + return HDF_FAILURE; + } + + HDF_LOGD("%{public}s:%{public}s parseIdTable success, length=%{public}d", \ + __func__, USB_PNP_DEBUG_STRING, table->length); + + return HDF_SUCCESS; +} + +static struct UsbPnpMatchIdTable **DevmgrServiceStubParseTableList(const struct DeviceResourceNode *node) +{ + struct DeviceResourceIface *devResIface = NULL; + int32_t idTabCount; + int32_t count; + int32_t ret; + const char *idTableName = NULL; + const struct DeviceResourceNode *tableNode = NULL; + struct UsbPnpMatchIdTable **idTable = NULL; + + if (node == NULL) { + HDF_LOGE("%s: node is NULL!", __func__); + return NULL; + } + devResIface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (devResIface == NULL) { + HDF_LOGE("%{public}s: devResIface is NULL!", __func__); + return NULL; + } + idTabCount = devResIface->GetElemNum(node, "idTableList"); + if (idTabCount <= 0) { + HDF_LOGE("%{public}s: idTableList not found!", __func__); + return NULL; + } + HDF_LOGD("%{public}s:%{public}s idTabCount=%{public}d!", \ + __func__, USB_PNP_DEBUG_STRING, idTabCount); + idTable = (struct UsbPnpMatchIdTable **)OsalMemCalloc((idTabCount + 1) * sizeof(struct UsbPnpMatchIdTable *)); + if (idTable == NULL) { + HDF_LOGE("%{public}s: OsalMemCalloc failure!", __func__); + return NULL; + } + idTable[idTabCount] = NULL; + for (count = 0; count < idTabCount; count++) { + idTable[count] = (struct UsbPnpMatchIdTable *)OsalMemCalloc(sizeof(struct UsbPnpMatchIdTable)); + if (idTable[count] == NULL) { + HDF_LOGE("%{public}s: OsalMemCalloc failure!", __func__); + goto out; + } + ret = devResIface->GetStringArrayElem(node, "idTableList", count, &idTableName, NULL); + if (ret != HDF_SUCCESS) { + goto out; + } + tableNode = devResIface->GetChildNode(node, idTableName); + if (tableNode == NULL) { + HDF_LOGE("%{public}s: tableNode is NULL!", __func__); + goto out; + } + if (DevmgrServiceStubParseIdTable(tableNode, devResIface, idTable[count]) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: DevmgrServiceStubParseIdTable failure!", __func__); + goto out; + } + } + + HDF_LOGD("%{public}s: %{public}s parse usb pnp id table success, idTabCount=%{public}d!", \ + __func__, USB_PNP_DEBUG_STRING, idTabCount); + + return idTable; + +out: + while ((--count) >= 0) { + OsalMemFree(idTable[count]); + } + OsalMemFree(idTable); + + return NULL; +} + +static struct UsbPnpMatchIdTable **DevmgrServiceStubParseDeviceId(const struct DeviceResourceNode *node) +{ + const char *deviceIdName = NULL; + struct DeviceResourceIface *devResIface = NULL; + const struct DeviceResourceNode *deviceIdNode = NULL; + + if (node == NULL) { + HDF_LOGE("%{public}s: node is NULL!", __func__); + return NULL; + } + + devResIface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (devResIface == NULL) { + HDF_LOGE("%{public}s: devResIface is NULL!", __func__); + return NULL; + } + + if (devResIface->GetString(node, "usb_pnp_device_id", &deviceIdName, NULL) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get usb_pnp_device_id name failure!", __func__); + return NULL; + } + + deviceIdNode = devResIface->GetChildNode(node, deviceIdName); + if (deviceIdNode == NULL) { + HDF_LOGE("%{public}s: deviceIdNode is NULL!", __func__); + return NULL; + } + + return DevmgrServiceStubParseTableList(deviceIdNode); +} + +static struct UsbPnpMatchIdTable **DevmgrServiceStubPnpMatch(void) +{ + struct DeviceResourceIface *devResInstance = NULL; + const struct DeviceResourceNode *rootNode = NULL; + const struct DeviceResourceNode *usbPnpNode = NULL; + + devResInstance = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (devResInstance == NULL) { + HDF_LOGE("%{public}s: devResInstance is NULL!", __func__); + return NULL; + } + + rootNode = devResInstance->GetRootNode(); + if (rootNode == NULL) { + HDF_LOGE("%{public}s: devResNode is NULL!", __func__); + return NULL; + } + + usbPnpNode = devResInstance->GetNodeByMatchAttr(rootNode, "usb_pnp_match"); + if (usbPnpNode == NULL) { + HDF_LOGE("%{public}s: usbPnpNode is NULL!", __func__); + return NULL; + } + + return DevmgrServiceStubParseDeviceId(usbPnpNode); +} + +static const struct UsbPnpMatchIdTable *DevmgrServiceStubPnpAddInterface(uint8_t index, + struct UsbPnpNotifyMatchInfoTable *infoTable, struct UsbPnpMatchIdTable **matchIdTable) +{ + struct UsbPnpMatchIdTable *idTable = NULL; + int32_t tableCount; + + for (tableCount = 0, idTable = matchIdTable[0]; idTable != NULL; idTable = matchIdTable[++tableCount]) { + HDF_LOGD("%{public}s:%{public}s matchDevice doing, idTable=%{public}p-%{public}d-0x%{public}x-0x%{public}x", \ + __func__, USB_PNP_DEBUG_STRING, idTable, idTable->matchFlag, idTable->vendorId, idTable->productId); + + if (!DevmgrServiceStubUsbMatchDevice(infoTable, idTable)) { + continue; + } + + if (!DevmgrServiceStubUsbMatchOneIdIntf(infoTable, index, idTable)) { + continue; + } + + HDF_LOGD("%{public}s: matchDevice end, index=%{public}d tableCount=%{public}d is match idTable=%{public}p, \ + moduleName=%{public}s, serviceName=%{public}s", __func__, index, tableCount, idTable, \ + idTable->moduleName, idTable->serviceName); + + return idTable; + } + + HDF_LOGD("%{public}s:%{public}s matchDevice end, idTable=%{public}p", \ + __func__, USB_PNP_DEBUG_STRING, idTable); + + return NULL; +} + +static int DevmgrServiceStubPnpRemoveInterface(struct IDevmgrService *devmgrSvc, + struct UsbPnpRemoveInfo removeInfo) +{ + int ret = HDF_SUCCESS; + struct HdfSBuf *pnpData = NULL; + struct UsbPnpDeviceListTable *deviceListTablePos = NULL; + struct UsbPnpDeviceListTable *deviceListTableTemp = NULL; + bool findFlag = false; + struct UsbPnpNotifyServiceInfo serviceInfo; + int32_t i; + + HDF_LOGD("%{public}s:%{public}s removeType=%{public}d Enter", \ + __func__, USB_PNP_DEBUG_STRING, removeInfo.removeType); + + if (DListIsEmpty(&g_usbPnpDeviceTableList->list)) { + HDF_LOGE("%{public}s:%{public}d g_usbPnpDeviceTableList is empty. ", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + + DLIST_FOR_EACH_ENTRY_SAFE(deviceListTablePos, deviceListTableTemp, &g_usbPnpDeviceTableList->list, \ + struct UsbPnpDeviceListTable, list) { + if ((deviceListTablePos->usbDevAddr == removeInfo.usbDevAddr) && \ + (deviceListTablePos->busNum == removeInfo.busNum)) { + if (removeInfo.removeType == USB_PNP_NOTIFY_REMOVE_INTERFACE_NUM) { + for (i = 0; i < deviceListTablePos->interfaceLength; i++) { + if (deviceListTablePos->interfaceNumber[i] == removeInfo.interfaceNum) { + break; + } + } + + if (i >= deviceListTablePos->interfaceLength) { + continue; + } + } + findFlag = true; + + serviceInfo.length = sizeof(struct UsbPnpNotifyServiceInfo) - (USB_PNP_INFO_MAX_INTERFACES \ + - deviceListTablePos->interfaceLength); + serviceInfo.devNum = deviceListTablePos->devNum; + serviceInfo.busNum = deviceListTablePos->busNum; + serviceInfo.interfaceLength = deviceListTablePos->interfaceLength; + memcpy_s(serviceInfo.interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, \ + deviceListTablePos->interfaceNumber, USB_PNP_INFO_MAX_INTERFACES); + pnpData = DevmgrServiceStubBufCreate(deviceListTablePos->moduleName, deviceListTablePos->serviceName, \ + serviceInfo); + if (pnpData == NULL) { + ret = HDF_FAILURE; + HDF_LOGE("%{public}s: DevmgrServiceStubBufCreate faile", __func__); + break; + } + ret = DevmgrServiceStubDispatchPnpDevice(devmgrSvc, pnpData, false); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: DevmgrServiceStubDispatchPnpDevice faile", __func__); + break; + } + + DListRemove(&deviceListTablePos->list); + OsalMemFree(deviceListTablePos); + + HDF_LOGD("%{public}s:%{public}s usbDevAddr=0x%{public}x, device=%{public}d-%{public}d-%{public}d \ + to be remove success. ", __func__, USB_PNP_DEBUG_STRING, removeInfo.usbDevAddr, \ + removeInfo.devNum, removeInfo.busNum, removeInfo.interfaceNum); + } + } + + if (findFlag == false) { + HDF_LOGE("%{public}s:%{public}s removeType=%{public}d, usbDevAddr=0x%{public}x, \ + device=%{public}d-%{public}d-%{public}d to be remove but not exist. ", \ + __func__, USB_PNP_DEBUG_STRING, removeInfo.removeType, removeInfo.usbDevAddr, \ + removeInfo.devNum, removeInfo.busNum, removeInfo.interfaceNum); + ret = HDF_FAILURE; + } + + return ret; +} + +static int DevmgrServiceStubDeviceListAdd(struct UsbPnpNotifyMatchInfoTable *info, + const struct UsbPnpMatchIdTable *idTable) +{ + int ret; + unsigned char *ptr = NULL; + struct UsbPnpDeviceListTable *deviceTableListTemp = NULL; + + ptr = OsalMemAlloc(sizeof(struct UsbPnpDeviceListTable)); + if (ptr == NULL) { + ret = HDF_ERR_MALLOC_FAIL; + HDF_LOGE("%{public}s:%{public}d OsalMemAlloc faile, ret=%{public}d ", __func__, __LINE__, ret); + } else { + deviceTableListTemp = (struct UsbPnpDeviceListTable *)ptr; + + DListHeadInit(&deviceTableListTemp->list); + deviceTableListTemp->moduleName = idTable->moduleName; + deviceTableListTemp->serviceName = idTable->serviceName; + deviceTableListTemp->usbDevAddr = info->usbDevAddr; + deviceTableListTemp->devNum = info->devNum; + deviceTableListTemp->busNum = info->busNum; + deviceTableListTemp->interfaceLength = idTable->interfaceLength; + memcpy_s(deviceTableListTemp->interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, \ + idTable->interfaceNumber, USB_PNP_INFO_MAX_INTERFACES); + + DListInsertTail(&deviceTableListTemp->list, &g_usbPnpDeviceTableList->list); + HDF_LOGD("%{public}s:%{public}s add %{public}s-%{public}s-0x%{public}x-%{public}d-%{public}d-\ + %{public}d-%{public}d device to g_usbPnpDeviceTableList", __func__, USB_PNP_DEBUG_STRING, \ + deviceTableListTemp->moduleName, deviceTableListTemp->serviceName, deviceTableListTemp->usbDevAddr, \ + deviceTableListTemp->devNum, deviceTableListTemp->busNum, deviceTableListTemp->interfaceLength, \ + deviceTableListTemp->interfaceNumber[0]); + + ret = HDF_SUCCESS; + } + + return ret; +} + +static int DevmgrServiceStubEventReceived(void *priv, uint32_t id, struct HdfSBuf *data) +{ + int ret; + bool flag; + uint32_t infoSize; + struct UsbPnpNotifyMatchInfoTable *infoTable = NULL; + struct HdfSBuf *pnpData = NULL; + struct IDevmgrService *super = (struct IDevmgrService *)priv; + struct UsbPnpMatchIdTable **matchIdTable = NULL; + const struct UsbPnpMatchIdTable *idTable = NULL; + int8_t i; + int32_t tableCount; + struct UsbPnpRemoveInfo removeInfo; + struct UsbPnpNotifyServiceInfo serviceInfo; + + HDF_LOGD("%{public}s:%{public}s enter", __func__, USB_PNP_DEBUG_STRING); + + flag = HdfSbufReadBuffer(data, (const void **)(&infoTable), &infoSize); + if ((flag == false) || (infoTable == NULL)) { + ret = HDF_ERR_INVALID_PARAM; + HDF_LOGE("%{public}s: fail to read infoTable in event data, flag=%{public}d, infoTable=%{public}p", \ + __func__, flag, infoTable); + return ret; + } + HDF_LOGD("%{public}s: %{public}s infoSize=%{public}d, usbDevAddr=0x%{public}x, devNum=%{public}d, \ + busNum=%{public}d, infoTable=0x%{public}x-0x%{public}x success", __func__, USB_PNP_DEBUG_STRING, \ + infoSize, infoTable->usbDevAddr, infoTable->devNum, infoTable->busNum, infoTable->deviceInfo.vendorId, \ + infoTable->deviceInfo.productId); + + HDF_LOGD("%{public}s: priv=%{public}p received:id=%{public}u", __func__, priv, id); + switch (id) { + case USB_PNP_NOTIFY_ADD_INTERFACE: + matchIdTable = DevmgrServiceStubPnpMatch(); + if ((matchIdTable == NULL) || (matchIdTable[0] == NULL)) { + ret = HDF_ERR_INVALID_PARAM; + HDF_LOGE("%{public}s: matchIdTable or matchIdTable[0] is NULL!", __func__); + return ret; + } + + for (i = 0; i < infoTable->numInfos; i++) { + idTable = DevmgrServiceStubPnpAddInterface(i, infoTable, matchIdTable); + if (idTable == NULL) { + HDF_LOGD("%{public}s: i=%{public}d is not match", __func__, i); + continue; + } + + serviceInfo.length = sizeof(struct UsbPnpNotifyServiceInfo) - (USB_PNP_INFO_MAX_INTERFACES \ + - idTable->interfaceLength); + serviceInfo.devNum = infoTable->devNum; + serviceInfo.busNum = infoTable->busNum; + serviceInfo.interfaceLength = idTable->interfaceLength; + memcpy_s(serviceInfo.interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, \ + idTable->interfaceNumber, USB_PNP_INFO_MAX_INTERFACES); + pnpData = DevmgrServiceStubBufCreate(idTable->moduleName, idTable->serviceName, serviceInfo); + if (pnpData == NULL) { + ret = HDF_FAILURE; + HDF_LOGE("%{public}s: DevmgrServiceStubBufCreate faile", __func__); + break; + } + + ret = DevmgrServiceStubDispatchPnpDevice(super, pnpData, true); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}s handle failed, id is %{public}d, ret=%{public}d", \ + __func__, USB_PNP_DEBUG_STRING, id, ret); + } else { + ret = DevmgrServiceStubDeviceListAdd(infoTable, idTable); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}s DevmgrServiceStubDeviceListAdd faile", \ + __func__, USB_PNP_DEBUG_STRING); + break; + } + } + } + + HDF_LOGD("%{public}s:%{public}s OsalMemFree matchIdTable, ret=%{public}d", \ + __func__, USB_PNP_DEBUG_STRING, ret); + + tableCount = 0; + for (idTable = matchIdTable[0]; idTable != NULL;) { + tableCount++; + idTable = matchIdTable[tableCount]; + } + while ((--tableCount) >= 0) { + OsalMemFree(matchIdTable[tableCount]); + } + OsalMemFree(matchIdTable); + break; + case USB_PNP_NOTIFY_REMOVE_INTERFACE: + removeInfo.removeType = infoTable->removeType; + removeInfo.usbDevAddr = infoTable->usbDevAddr; + removeInfo.devNum = infoTable->devNum; + removeInfo.busNum = infoTable->busNum; + removeInfo.interfaceNum = infoTable->interfaceInfo[0].interfaceNumber; + ret = DevmgrServiceStubPnpRemoveInterface(super, removeInfo); + HDF_LOGD("%{public}s: %{public}s remove interface done", __func__, USB_PNP_DEBUG_STRING); + break; + case USB_PNP_NOTIFY_REPORT_INTERFACE: + ret = HDF_ERR_NOT_SUPPORT; + break; + default: + ret = HDF_ERR_INVALID_PARAM; + break; + } + + HDF_LOGD("%{public}s:%{public}s ret=%{public}d DONE", __func__, USB_PNP_DEBUG_STRING, ret); + + return ret; +} + +static int DevmgrServiceStubEventSend(struct HdfIoService *serv, char *eventData) +{ + int ret; + int replyData = 0; + struct HdfSBuf *data = NULL; + + data = HdfSBufObtainDefaultSize(); + if (data == NULL) { + ret = HDF_DEV_ERR_NO_MEMORY; + HDF_LOGE("%{public}s: fail to obtain sbuf data", __func__); + return ret; + } + + struct HdfSBuf *reply = HdfSBufObtainDefaultSize(); + if (reply == NULL) { + ret = HDF_DEV_ERR_NO_MEMORY; + HDF_LOGE("%{public}s: fail to obtain sbuf reply", __func__); + goto out; + } + + if (!HdfSbufWriteString(data, eventData)) { + ret = HDF_FAILURE; + HDF_LOGE("%{public}s: fail to write sbuf", __func__); + goto out; + } + + ret = serv->dispatcher->Dispatch(&serv->object, USB_PNP_NOTIFY_REPORT_INTERFACE, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: fail to send serivice call, ret=%{public}d", __func__, ret); + goto out; + } + + if (!HdfSbufReadInt32(reply, &replyData)) { + ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s: fail to get service call reply", __func__); + goto out; + } + + HDF_LOGE("%{public}s: get reply is 0x%{public}x", __func__, replyData); + +out: + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + + return ret; +} + +static int DevmgrServiceStubEventHandle(struct IDevmgrService *inst) +{ + int status; + struct HdfIoService *usbPnpServ = HdfIoServiceBind(USB_PNP_NOTIFY_SERVICE_NAME); + static struct HdfDevEventlistener usbPnpListener = { + .callBack = DevmgrServiceStubEventReceived, + }; + usbPnpListener.priv = (void *)(inst); + + if (g_usbPnpDeviceTableList == NULL) { + g_usbPnpDeviceTableList = (struct UsbPnpDeviceListTable *)OsalMemCalloc(sizeof(struct UsbPnpDeviceListTable)); + if (g_usbPnpDeviceTableList == NULL) { + status = HDF_ERR_MALLOC_FAIL; + HDF_LOGE("%{public}s: OsalMemCalloc g_usbPnpDeviceTableList faile status=%{public}d", \ + __func__, status); + return status; + } + DListHeadInit(&g_usbPnpDeviceTableList->list); + g_usbPnpDeviceTableList->moduleName = ""; + g_usbPnpDeviceTableList->serviceName = ""; + g_usbPnpDeviceTableList->usbDevAddr = 0; + g_usbPnpDeviceTableList->devNum = 0; + g_usbPnpDeviceTableList->busNum = 0; + g_usbPnpDeviceTableList->interfaceLength = 0; + memset_s(g_usbPnpDeviceTableList->interfaceNumber, USB_PNP_INFO_MAX_INTERFACES, 0, USB_PNP_INFO_MAX_INTERFACES); + HDF_LOGI("%{public}s OsalMemCalloc success,g_usbPnpDeviceTableList=%{public}p", \ + USB_PNP_DEBUG_STRING, g_usbPnpDeviceTableList); + } else { + HDF_LOGD("%{public}s g_usbPnpDeviceTableList=%{public}p is not NULL", \ + USB_PNP_DEBUG_STRING, g_usbPnpDeviceTableList); + } + + status = HdfDeviceRegisterEventListener(usbPnpServ, &usbPnpListener); + if (status != HDF_SUCCESS) { + HDF_LOGE("HdfDeviceRegisterEventListener faile status=%{public}d", status); + goto out; + } + HDF_LOGI("%{public}s HdfDeviceRegisterEventListener success,inst=%{public}p", USB_PNP_DEBUG_STRING, inst); + + status = DevmgrServiceStubEventSend(usbPnpServ, "USB PNP Handle Info"); + if (status != HDF_SUCCESS) { + HDF_LOGE("DevmgrServiceStubEventSend faile status=%{public}d", status); + goto out; + } + HDF_LOGI("DevmgrServiceStubEventSend success"); + +out: + OsalMemFree(g_usbPnpDeviceTableList); + + return status; +} + +int DevmgrServiceStubStartService(struct IDevmgrService *inst) +{ + int status = HDF_FAILURE; + struct HdfRemoteService *remoteService = NULL; + struct DevmgrServiceStub *fullService = (struct DevmgrServiceStub *)inst; + struct IDevSvcManager *serviceManager = DevSvcManagerGetInstance(); + if (fullService == NULL) { + HDF_LOGI("Start service failed, fullService is null"); + return HDF_FAILURE; + } + remoteService = HdfRemoteServiceObtain((struct HdfObject *)inst, &g_devmgrDispatcher); + if ((remoteService == NULL) || (serviceManager == NULL)) { + HDF_LOGI("Start service failed, remoteService or serviceManager is null"); + return HDF_FAILURE; + } + struct HdfDeviceObject *deviceObject = OsalMemCalloc(sizeof(struct HdfDeviceObject)); + if (deviceObject == NULL) { + return HDF_FAILURE; + } + deviceObject->service = (struct IDeviceIoService *)remoteService; + if (serviceManager->AddService != NULL) { + status = DevSvcManagerAddService( + serviceManager, DEVICE_MANAGER_SERVICE, deviceObject); + } + if (status != HDF_SUCCESS) { + OsalMemFree(deviceObject); + return status; + } + fullService->remote = remoteService; + + status = DevmgrServiceStartService((struct IDevmgrService *)&fullService->super); + + HDF_LOGI("%{public}s:%{public}s Start service status is %{public}d", __func__, USB_PNP_DEBUG_STRING, status); + + HDF_LOGI("%{public}s:%{public}s", __func__, USB_PNP_DEBUG_STRING); + if (status == HDF_SUCCESS) { + if (DevmgrServiceStubEventHandle(inst) != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}s error", __func__, USB_PNP_DEBUG_STRING); + } + } + + return status; +} + +static void DevmgrServiceStubConstruct(struct DevmgrServiceStub *inst) +{ + struct IDevmgrService *pvtbl = (struct IDevmgrService *)inst; + + DevmgrServiceFullConstruct(&inst->super); + pvtbl->StartService = DevmgrServiceStubStartService; + inst->remote = NULL; + OsalMutexInit(&inst->devmgrStubMutx); +} + +struct HdfObject *DevmgrServiceStubCreate() +{ + static struct DevmgrServiceStub *instance = NULL; + if (instance == NULL) { + instance = (struct DevmgrServiceStub *)OsalMemCalloc(sizeof(struct DevmgrServiceStub)); + if (instance == NULL) { + HDF_LOGE("Creating devmgr service stub failed, alloc mem error"); + return NULL; + } + DevmgrServiceStubConstruct(instance); + } + return (struct HdfObject *)instance; +} + +void DevmgrServiceStubRelease(struct HdfObject *object) +{ + struct DevmgrServiceStub *instance = (struct DevmgrServiceStub *)object; + if (instance != NULL) { + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + OsalMutexDestroy(&instance->devmgrStubMutx); + OsalMemFree(instance); + } +} + diff --git a/uhdf2/manager/src/devmgr_virtual_service.c b/uhdf2/manager/src/devmgr_virtual_service.c new file mode 100644 index 0000000..76bf080 --- /dev/null +++ b/uhdf2/manager/src/devmgr_virtual_service.c @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devhost_service_clnt.h" +#include "devmgr_pnp_service.h" +#include "devmgr_service.h" +#include "devsvc_manager.h" +#include "hdf_base.h" +#include "hdf_cstring.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" + +#define HDF_LOG_TAG devmgr_reg_virtual_device + +static void DevmgrUpdateDeviceType(struct HdfSList *list, const char *moduleName, const char *serviceName) +{ + struct HdfSListIterator it; + struct HdfDeviceInfo *deviceInfo = NULL; + + HdfSListIteratorInit(&it, list); + while (HdfSListIteratorHasNext(&it)) { + deviceInfo = (struct HdfDeviceInfo *)HdfSListIteratorNext(&it); + if ((deviceInfo != NULL) && + (strcmp(deviceInfo->moduleName, moduleName) == 0) && (strcmp(deviceInfo->svcName, serviceName) == 0)) { + deviceInfo->deviceType = HDF_DEV_REMOTE_SERVICE; + HDF_LOGD("%s: set remote service %s %s %d", __func__, moduleName, serviceName, deviceInfo->deviceType); + return; + } + } + HDF_LOGE("%s: remote service %s %s not found", __func__, moduleName, serviceName); +} + +int DevmgrServiceVirtualDevice( + struct IDevmgrService *inst, struct HdfSBuf *data, struct HdfSBuf *reply, bool flag) +{ + int32_t ret; + struct HdfSList *list = NULL; + + (void)reply; + if (inst == NULL || data == NULL) { + HDF_LOGE("%s para invalid", __func__); + return HDF_FAILURE; + } + + const char *moduleName = HdfSbufReadString(data); + const char *serviceName = HdfSbufReadString(data); + if (moduleName == NULL || serviceName == NULL) { + HDF_LOGE("%s: reading string failed", __func__); + return HDF_FAILURE; + } + + if (flag) { + ret = DevmgrServiceRegPnpDevice(inst, moduleName, serviceName, NULL); + if (ret == HDF_SUCCESS) { + list = DevmgrServiceGetPnpDeviceInfo(); + DevmgrUpdateDeviceType(list, moduleName, serviceName); + } + } else { + ret = DevmgrServiceUnRegPnpDevice(inst, moduleName, serviceName); + } + + return ret; +} + diff --git a/uhdf2/manager/src/devsvc_manager_stub.c b/uhdf2/manager/src/devsvc_manager_stub.c new file mode 100644 index 0000000..6b73005 --- /dev/null +++ b/uhdf2/manager/src/devsvc_manager_stub.c @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devsvc_manager_stub.h" +#include "device_token_proxy.h" +#include "devmgr_service_stub.h" +#include "devsvc_manager.h" +#include "devsvc_manager_proxy.h" +#include "hdf_base.h" +#include "hdf_cstring.h" +#include "hdf_log.h" +#include "hdf_sbuf.h" +#include "hdf_slist.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG devsvc_manager_stub + +static int32_t DevSvcManagerStubAddService(struct IDevSvcManager *super, struct HdfSBuf *data) +{ + int ret = HDF_FAILURE; + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + const char *name = HdfSbufReadString(data); + if (name == NULL) { + HDF_LOGE("%s failed, name is null", __func__); + return ret; + } + struct HdfRemoteService *service = HdfSBufReadRemoteService(data); + if (service == NULL) { + HDF_LOGE("%s failed, service is null", __func__); + return ret; + } + HdfRemoteServiceAddDeathRecipient(service, &stub->recipient); + struct HdfDeviceObject *deviceObject = OsalMemCalloc(sizeof(struct HdfDeviceObject)); + if (deviceObject == NULL) { + return ret; + } + deviceObject->service = (struct IDeviceIoService *)service; + ret = super->AddService(super, name, deviceObject); + if (ret != HDF_SUCCESS) { + OsalMemFree(deviceObject); + } + return ret; +} + +static int32_t DevSvcManagerStubGetService( + struct IDevSvcManager *super, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int ret = HDF_FAILURE; + const char *name = HdfSbufReadString(data); + if (name == NULL) { + HDF_LOGE("%s failed, name is null", __func__); + return ret; + } + struct HdfRemoteService *remoteService = + (struct HdfRemoteService *)super->GetService(super, name); + if (remoteService != NULL) { + ret = HDF_SUCCESS; + HdfSBufWriteRemoteService(reply, remoteService); + } else { + HDF_LOGE("service %{public}s not found", name); + } + + return ret; +} + +static int32_t DevSvcManagerStubRemoveService(struct IDevSvcManager *super, struct HdfSBuf *data) +{ + const char *name = HdfSbufReadString(data); + if (name == NULL) { + HDF_LOGE("%s failed, name is null", __func__); + return HDF_FAILURE; + } + struct HdfDeviceObject *deviceObject = super->GetObject(super, name); + super->RemoveService(super, name); + OsalMemFree(deviceObject); + return HDF_SUCCESS; +} + +int DevSvcManagerStubDispatch( + struct HdfRemoteService* service, int code, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int ret = HDF_FAILURE; + HDF_LOGV("DevSvcManagerStubDispatch in, code=%d", code); + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)service; + if (stub == NULL) { + HDF_LOGE("DevSvcManagerStubDispatch failed, object is null, code is %d", code); + return ret; + } + struct IDevSvcManager *super = (struct IDevSvcManager *)&stub->super; + switch (code) { + case DEVSVC_MANAGER_ADD_SERVICE: { + ret = DevSvcManagerStubAddService(super, data); + break; + } + case DEVSVC_MANAGER_GET_SERVICE: { + ret = DevSvcManagerStubGetService(super, data, reply); + break; + } + case DEVSVC_MANAGER_REMOVE_SERVICE: { + ret = DevSvcManagerStubRemoveService(super, data); + break; + } + default: { + HDF_LOGE("Unknown code : %d", code); + ret = HDF_FAILURE; + } + } + return ret; +} + +void DevSvcManagerOnServiceDied(struct HdfDeathRecipient *recipient, struct HdfRemoteService *service) +{ + (void)service; + struct DevSvcManagerStub *stub = + HDF_SLIST_CONTAINER_OF(struct HdfDeathRecipient, recipient, struct DevSvcManagerStub, recipient); + if (stub != NULL) { + struct IDevSvcManager *svcOps = (struct IDevSvcManager *)stub; + if (svcOps->RemoveService != NULL) { + svcOps->RemoveService(svcOps, NULL); + } + return; + } +} + +bool DevSvcManagerStubConstruct(struct DevSvcManagerStub *inst) +{ + static struct HdfRemoteDispatcher dispatcher = { + .Dispatch = DevSvcManagerStubDispatch + }; + if (!DevSvcManagerConstruct(&inst->super)) { + HDF_LOGE("Device service manager construct failed"); + return false; + } + inst->remote = HdfRemoteServiceObtain((struct HdfObject *)inst, &dispatcher); + if (inst->remote == NULL) { + HDF_LOGE("Device service manager failed to obtain remote service"); + return false; + } + + inst->recipient.OnRemoteDied = DevSvcManagerOnServiceDied; + + int ret = HdfRemoteServiceRegister(DEVICE_SERVICE_MANAGER_SA_ID, inst->remote); + if (ret != 0) { + HDF_LOGE("Device service manager failed to publish hdi, %d", ret); + } + + HDF_LOGI("Device service manager publish success"); + return true; +} + +struct HdfObject *DevSvcManagerStubCreate() +{ + static struct DevSvcManagerStub instance; + if (instance.remote == NULL) { + if (!DevSvcManagerStubConstruct(&instance)) { + HDF_LOGE("Creating device service manager stub failed"); + } + } + return (struct HdfObject *)&instance; +} + +void DevSvcManagerStubRelease(struct HdfObject *object) +{ + struct DevmgrServiceStub *instance = (struct DevmgrServiceStub *)object; + if (instance != NULL) { + if (instance->remote != NULL) { + HdfRemoteServiceRecycle(instance->remote); + instance->remote = NULL; + } + } +} + diff --git a/uhdf2/manager/src/driver_installer_full.c b/uhdf2/manager/src/driver_installer_full.c new file mode 100644 index 0000000..1ee9265 --- /dev/null +++ b/uhdf2/manager/src/driver_installer_full.c @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "driver_installer_full.h" +#include +#include +#include +#include +#include "hdf_base.h" +#include "hdf_driver_installer.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "securec.h" + +#define DEV_HOST_BINARY "/system/bin/hdf_devhost" +#define HDF_LOG_TAG driver_installer_full +#define MAX_CMD_LEN 256 + +static bool g_sigFlag = false; +static struct DriverInstaller *g_fullInstaller = NULL; + +void SigChildProc(int signo) +{ + int stat; + (void)signo; + wait(&stat); +} + +int DriverInstallerFullStartDeviceHost(uint32_t devHostId, const char* devHostName) +{ + char cmd[MAX_CMD_LEN] = {0}; + // fork process. + if (snprintf_s(cmd, sizeof(cmd), sizeof(cmd) - 1, " %d", devHostId) < 0) { + HDF_LOGE("Starting device host, snprintf_s failed"); + return HDF_FAILURE; + } + if (!g_sigFlag) { + if (signal(SIGCHLD, SigChildProc) == SIG_ERR) { + HDF_LOGE("Starting device host, signal failed"); + return HDF_FAILURE; + } + g_sigFlag = true; + } + + pid_t fpid; + fpid = fork(); + if (fpid < 0) { + HDF_LOGE("Starting device host, fork failed"); + return HDF_FAILURE; + } else if (fpid == 0) { + char * const args[] = {DEV_HOST_BINARY, cmd, (char * const)devHostName, NULL}; + char * const envs[] = {NULL}; + if (execve(DEV_HOST_BINARY, args, envs) == -1) { + HDF_LOGE("Start device host, execve failed"); + return HDF_FAILURE; + } + } else { + HDF_LOGI("Starting device host success"); + } + return HDF_SUCCESS; +} + +static void DriverInstallerFullConstruct(struct DriverInstaller *inst) +{ + struct IDriverInstaller *pvtbl = (struct IDriverInstaller *)inst; + pvtbl->StartDeviceHost = DriverInstallerFullStartDeviceHost; +} + +struct HdfObject *DriverInstallerFullCreate(void) +{ + if (g_fullInstaller == NULL) { + g_fullInstaller = (struct DriverInstaller *)OsalMemCalloc(sizeof(struct DriverInstaller)); + if (g_fullInstaller != NULL) { + DriverInstallerFullConstruct(g_fullInstaller); + } + } + + return (struct HdfObject *)g_fullInstaller; +} + +void DriverInstallerFullRelease(struct HdfObject *object) +{ + struct DriverInstaller *installer = (struct DriverInstaller *)object; + if (g_fullInstaller == installer) { + g_fullInstaller = NULL; + } + if (installer != NULL) { + OsalMemFree(installer); + } +} diff --git a/uhdf2/manager/src/hdf_get_attribute.c b/uhdf2/manager/src/hdf_get_attribute.c new file mode 100644 index 0000000..41327cf --- /dev/null +++ b/uhdf2/manager/src/hdf_get_attribute.c @@ -0,0 +1,270 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include "devmgr_pnp_service.h" +#include "hcs_parser.h" +#include "hcs_dm_parser.h" +#include "hcs_tree_if.h" +#include "hdf_attribute_manager.h" +#include "hdf_device_info_full.h" +#include "hdf_host_info.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "securec.h" + +#define HDF_LOG_TAG attribute_manager + +#define ATTR_HOST_NAME "hostName" +#define ATTR_DEV_POLICY "policy" +#define ATTR_DEV_PRIORITY "priority" +#define ATTR_DEV_PRELOAD "preload" +#define ATTR_DEV_PERMISSION "permission" +#define ATTR_DEV_MODULENAME "moduleName" +#define ATTR_DEV_SVCNAME "serviceName" +#define ATTR_DEV_MATCHATTR "deviceMatchAttr" +#define MANAGER_NODE_MATCH_ATTR "hdf_manager" + +static bool HdfHostListCompare(struct HdfSListNode *listEntryFirst, struct HdfSListNode *listEntrySecond) +{ + if (listEntryFirst == NULL || listEntrySecond == NULL) { + return false; + } + struct HdfHostInfo *attrFirst = (struct HdfHostInfo *)listEntryFirst; + struct HdfHostInfo *attrSecond = (struct HdfHostInfo *)listEntrySecond; + return attrFirst->priority <= attrSecond->priority; +} + +static const struct DeviceResourceNode *GetHdfManagerNode(const struct DeviceResourceNode *node) +{ + return HcsGetNodeByMatchAttr(node, MANAGER_NODE_MATCH_ATTR); +} + +static bool GetHostInfo(const struct DeviceResourceNode *hostNode, struct HdfHostInfo *hostInfo) +{ + uint16_t readNum = 0; + if ((HcsGetString(hostNode, ATTR_HOST_NAME, &hostInfo->hostName, NULL) != HDF_SUCCESS) || + (strcmp(hostInfo->hostName, "") == 0)) { + HDF_LOGW("%s get host name failed", __func__); + return false; + } + if ((HcsGetUint16(hostNode, ATTR_DEV_PRIORITY, &readNum, 0) != HDF_SUCCESS) || + (readNum > MAX_PRIORITY_NUM)) { + HDF_LOGW("%s get host priority failed, priority is: %d", __func__, readNum); + return false; + } + hostInfo->priority = readNum; + return true; +} + +bool HdfAttributeManagerGetHostList(struct HdfSList *hostList) +{ + const struct DeviceResourceNode *hdfManagerNode = NULL; + const struct DeviceResourceNode *hostNode = NULL; + uint16_t hostId = 0; + if (hostList == NULL) { + return false; + } + + hdfManagerNode = GetHdfManagerNode(HdfGetRootNode()); + if (hdfManagerNode == NULL) { + HDF_LOGE("%s get hdf manager node is null", __func__); + return false; + } + + hostNode = hdfManagerNode->child; + while (hostNode != NULL) { + struct HdfHostInfo *hostInfo = HdfHostInfoNewInstance(); + if (hostInfo == NULL) { + HdfSListFlush(hostList, HdfHostInfoDelete); + HDF_LOGE("%s new hostInfo is null", __func__); + return false; + } + if (!GetHostInfo(hostNode, hostInfo)) { + HdfHostInfoFreeInstance(hostInfo); + hostNode = hostNode->sibling; + continue; + } + hostInfo->hostId = hostId; + if (!HdfSListAddOrder(hostList, &hostInfo->node, HdfHostListCompare)) { + HdfHostInfoFreeInstance(hostInfo); + hostNode = hostNode->sibling; + continue; + } + hostId++; + hostNode = hostNode->sibling; + } + return true; +} + +static bool HdfDeviceListCompare(struct HdfSListNode *listEntryFirst, struct HdfSListNode *listEntrySecond) +{ + if (listEntryFirst == NULL || listEntrySecond == NULL) { + return false; + } + struct HdfDeviceInfo *attrFirst = (struct HdfDeviceInfo *)listEntryFirst; + struct HdfDeviceInfo *attrSecond = (struct HdfDeviceInfo *)listEntrySecond; + return attrFirst->priority <= attrSecond->priority; +} + +static const struct DeviceResourceNode *GetHostNode(const char *inHostName) +{ + const struct DeviceResourceNode *hdfManagerNode = NULL; + const struct DeviceResourceNode *hostNode = NULL; + const char *hostName = NULL; + + hdfManagerNode = GetHdfManagerNode(HdfGetRootNode()); + if (hdfManagerNode == NULL) { + return NULL; + } + hostNode = hdfManagerNode->child; + while (hostNode != NULL) { + if (HcsGetString(hostNode, ATTR_HOST_NAME, &hostName, NULL) != HDF_SUCCESS) { + hostNode = hostNode->sibling; + continue; + } + if (strcmp(hostName, inHostName) == 0) { + return hostNode; + } + hostNode = hostNode->sibling; + } + return NULL; +} + +static bool CheckDeviceInfo(struct HdfDeviceInfo *deviceNodeInfo) +{ + if (deviceNodeInfo->policy > SERVICE_POLICY_PRIVATE) { + HDF_LOGE("%s policy is invalid", __func__); + return false; + } + + if (deviceNodeInfo->priority > MAX_PRIORITY_NUM) { + HDF_LOGE("%s priority is invalid", __func__); + return false; + } + + if (deviceNodeInfo->preload > DEVICE_PRELOAD_DISABLE) { + HDF_LOGE("%s preload is invalid", __func__); + return false; + } + return true; +} + +static bool GetDeviceNodeInfo(const struct DeviceResourceNode *deviceNode, struct HdfDeviceInfo *deviceNodeInfo) +{ + uint16_t readNum = 0; + const char *readString = NULL; + if (HcsGetUint16(deviceNode, ATTR_DEV_POLICY, &readNum, 0) != HDF_SUCCESS) { + HDF_LOGE("%s get policy failed", __func__); + return false; + } + deviceNodeInfo->policy = readNum; + + if (HcsGetUint16(deviceNode, ATTR_DEV_PRIORITY, &readNum, 0) != HDF_SUCCESS) { + HDF_LOGE("%s get priority failed", __func__); + return false; + } + deviceNodeInfo->priority = readNum; + + if (HcsGetUint16(deviceNode, ATTR_DEV_PRELOAD, &readNum, 0) != HDF_SUCCESS) { + HDF_LOGE("%s get preload failed", __func__); + return false; + } + deviceNodeInfo->preload = readNum; + + if (HcsGetString(deviceNode, ATTR_DEV_MODULENAME, &readString, NULL) != HDF_SUCCESS) { + HDF_LOGE("%s get module name failed", __func__); + return false; + } + deviceNodeInfo->moduleName = strdup(readString); + if (deviceNodeInfo->moduleName == NULL) { + return false; + } + + if (HcsGetString(deviceNode, ATTR_DEV_SVCNAME, &readString, NULL) != HDF_SUCCESS) { + HDF_LOGE("%s get service name failed", __func__); + return false; + } + deviceNodeInfo->svcName = strdup(readString); + if (deviceNodeInfo->svcName == NULL) { + return false; + } + + if (HcsGetString(deviceNode, ATTR_DEV_MATCHATTR, &readString, NULL) != HDF_SUCCESS) { + HDF_LOGE("%s get service name failed", __func__); + return false; + } + deviceNodeInfo->deviceMatchAttr = readString; + return CheckDeviceInfo(deviceNodeInfo); +} + +struct HdfSList *HdfAttributeManagerGetDeviceList(uint16_t hostId, const char *hostName) +{ + uint16_t deviceIdx = 0; + if (hostName == NULL) { + return NULL; + } + if (strcmp(hostName, PNP_HOST_NAME) == 0) { + return DevmgrServiceGetPnpDeviceInfo(); + } + + const struct DeviceResourceNode *hostNode = GetHostNode(hostName); + if (hostNode == NULL) { + return NULL; + } + + struct HdfSList *deviceList = (struct HdfSList *)OsalMemCalloc(sizeof(struct HdfSList)); + if (deviceList == NULL) { + return NULL; + } + const struct DeviceResourceNode *device = hostNode->child; + while (device != NULL) { + const struct DeviceResourceNode *deviceNode = device->child; + while (deviceNode != NULL) { + struct HdfDeviceInfoFull *deviceNodeInfo = HdfDeviceInfoFullNewInstance(); + if (deviceNodeInfo == NULL) { + HdfSListFlush(deviceList, HdfDeviceInfoDelete); + OsalMemFree(deviceList); + return NULL; + } + deviceNodeInfo->super.hostId = hostId; + if (!GetDeviceNodeInfo(deviceNode, &deviceNodeInfo->super)) { + HdfDeviceInfoFullFreeInstance(deviceNodeInfo); + HDF_LOGE("%s get device failed", __func__); + deviceNodeInfo = NULL; + deviceNode = deviceNode->sibling; + continue; + } + if (!HdfSListAddOrder(deviceList, &deviceNodeInfo->super.node, HdfDeviceListCompare)) { + HDF_LOGE("%s add device %s failed", __func__, deviceNodeInfo->super.svcName); + HdfDeviceInfoFullFreeInstance(deviceNodeInfo); + deviceNodeInfo = NULL; + deviceNode = deviceNode->sibling; + continue; + } + deviceNodeInfo->super.deviceId = deviceIdx; + deviceNode = deviceNode->sibling; + } + device = device->sibling; + deviceIdx++; + } + if (HdfSListCount(deviceList) == 0) { + OsalMemFree(deviceList); + return NULL; + } + return deviceList; +} + diff --git a/uhdf2/ohos.build b/uhdf2/ohos.build new file mode 100755 index 0000000..896e396 --- /dev/null +++ b/uhdf2/ohos.build @@ -0,0 +1,44 @@ +{ + "subsystem": "hdf", + "parts": { + "hdf": { + "module_list": [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", + "//drivers/adapter/uhdf2/hdi:libhdi", + "//drivers/adapter/uhdf2/manager:hdf_devmgr", + "//drivers/adapter/uhdf2/manager:hdf_devmgr.rc", + "//drivers/adapter/uhdf2/host:hdf_devhost", + "//drivers/adapter/uhdf2/host:libhdf_host", + "//drivers/adapter/uhdf2/config:libhdf_hcs", + "//drivers/adapter/uhdf2/host/test/unittest/sample_driver:libsample_driver", + "//drivers/adapter/uhdf2/hcs:hdf_default.hcb", + "//drivers/peripheral/input/hal:hdi_input", + "//drivers/peripheral/display/hal:hdi_display", + "//drivers/peripheral/wlan/client:wifi_driver_client", + "//drivers/peripheral/wlan/hal:wifi_hal", + "//drivers/peripheral/camera/hal:camera_hal", + "//drivers/peripheral/usb/core:libusb_core", + "//drivers/peripheral/usb/serial:hostsdk_test", + "//drivers/peripheral/usb/serial:libusbhost_acm", + "//drivers/peripheral/usb/serial:hostacm_test", + "//drivers/peripheral/usb/gadget:libusbfn_cdcacm", + "//drivers/peripheral/usb/gadget:acm_test", + "//drivers/peripheral/usb/gadget:acm_read", + "//drivers/peripheral/usb/gadget:acm_write", + "//drivers/peripheral/usb/gadget:prop_test", + "//drivers/peripheral/base:hdf_peripheral.rc", + "//drivers/peripheral/sensor/hal:hdi_sensor" + ], + "test_list": [ + "//drivers/peripheral/wlan/test:hdf_test_wifi", + "//drivers/peripheral/sensor/test:hdf_test_sensor", + "//drivers/adapter/uhdf2/test:hdf_test_uhdf", + "//drivers/adapter/uhdf2/osal/test:unittest", + "//drivers/adapter/uhdf2/host/test:unittest", + "//drivers/adapter/uhdf2/hdi/test:unittest", + "//drivers/peripheral/usb/moduletest:moduletest" + ] + } + } +} diff --git a/uhdf2/osal/BUILD.gn b/uhdf2/osal/BUILD.gn new file mode 100755 index 0000000..9cc802b --- /dev/null +++ b/uhdf2/osal/BUILD.gn @@ -0,0 +1,93 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +group("uhdf_utils_pkg") { + deps = [ ":libhdf_utils" ] +} + +config("libhdf_utils_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ + "//utils/native/base/include", + "$hdf_framework_path/include", + "$hdf_framework_path/include/core", + "$hdf_framework_path/include/osal", + "$hdf_framework_path/include/utils", + "$hdf_framework_path/utils/include", + "$hdf_framework_path/ability/sbuf/include", + "$hdf_framework_path/core/adapter/syscall/include", + "$hdf_framework_path/core/adapter/vnode/include", + "$hdf_framework_path/core/shared/include", + "$hdf_framework_path/include/core/", + "$hdf_uhdf_path/osal/include" + ] + defines = [ "__OHOS_USER__" ] + + if (is_standard_system) { + defines += [ + "__OHOS_STANDARD_SYS__" + ] + } +} + +ohos_shared_library("libhdf_utils") { + public_configs = [ + ":libhdf_utils_pub_config", + ] + + sources = [ + "$hdf_framework_path/ability/sbuf/src/hdf_sbuf.c", + "$hdf_framework_path/ability/sbuf/src/hdf_sbuf_impl_raw.c", + "$hdf_framework_path/core/adapter/syscall/src/hdf_devmgr_adapter.c", + "$hdf_framework_path/core/adapter/syscall/src/hdf_syscall_adapter.c", + "$hdf_framework_path/core/shared/src/hdf_io_service.c", + "$hdf_framework_path/utils/src/hdf_map.c", + "$hdf_framework_path/utils/src/hdf_cstring.c", + "$hdf_framework_path/utils/src/hdf_message_looper.c", + "$hdf_framework_path/utils/src/hdf_message_task.c", + "$hdf_framework_path/utils/src/hdf_slist.c", + "$hdf_framework_path/utils/src/hdf_sref.c", + "$hdf_framework_path/utils/src/osal_message.c", + "$hdf_framework_path/utils/src/osal_msg_queue.c", + "$hdf_framework_path/utils/src/hdf_thread_ex.c", + "$hdf_framework_path/support/posix/src/osal_mem.c", + "$hdf_framework_path/support/posix/src/osal_mutex.c", + "$hdf_framework_path/support/posix/src/osal_sem.c", + "$hdf_framework_path/support/posix/src/osal_spinlock.c", + "osal_sysevent.c", + "$hdf_framework_path/support/posix/src/osal_thread.c", + "$hdf_framework_path/support/posix/src/osal_time.c", + ] + + deps = [ "//utils/native/base:utils" ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + ] + + # install_images = [ "vendor" ] + subsystem_name = "hdf" + part_name = "hdf" +} diff --git a/uhdf2/osal/include/hdf_log_adapter.h b/uhdf2/osal/include/hdf_log_adapter.h new file mode 100644 index 0000000..0290bcb --- /dev/null +++ b/uhdf2/osal/include/hdf_log_adapter.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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 HDF_LOG_ADAPTER_H +#define HDF_LOG_ADAPTER_H + +#include "hilog/log.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD002500 + +#ifndef LOG_TAG +#define LOG_TAG HDF +#endif + +#define HDF_LOGV_WRAPPER(fmt, arg...) HILOG_DEBUG(LOG_CORE, fmt, ##arg) + +#define HDF_LOGD_WRAPPER(fmt, arg...) HILOG_DEBUG(LOG_CORE, fmt, ##arg) + +#define HDF_LOGI_WRAPPER(fmt, arg...) HILOG_INFO(LOG_CORE, fmt, ##arg) + +#define HDF_LOGW_WRAPPER(fmt, arg...) HILOG_WARN(LOG_CORE, fmt, ##arg) + +#define HDF_LOGE_WRAPPER(fmt, arg...) HILOG_ERROR(LOG_CORE, fmt, ##arg) + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDF_LOG_ADAPTER_H */ + diff --git a/uhdf2/osal/include/hdf_types.h b/uhdf2/osal/include/hdf_types.h new file mode 100644 index 0000000..5e32b24 --- /dev/null +++ b/uhdf2/osal/include/hdf_types.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 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 HDF_TYPE_H +#define HDF_TYPE_H + +#include +#include +#include +#include +#include + +#endif /* HDF_TYPE_H */ diff --git a/uhdf2/osal/include/osal_atomic_def.h b/uhdf2/osal/include/osal_atomic_def.h new file mode 100644 index 0000000..0cbbf29 --- /dev/null +++ b/uhdf2/osal/include/osal_atomic_def.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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 OSAL_ATOMIC_DEF_H +#define OSAL_ATOMIC_DEF_H + +#include "hdf_base.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define OsalAtomicReadWrapper(v) (*(volatile int *)&(v)->counter) +#define OsalAtomicSetWrapper(v, value) ((v)->counter = (value)) +#define OsalAtomicIncWrapper(v) __sync_fetch_and_add(&(v)->counter, 1) +#define OsalAtomicDecWrapper(v) __sync_sub_and_fetch(&(v)->counter, 1) +static inline int32_t BitDoNotSupport(int nr, unsigned long *addr) +{ + (void)nr; + (void)addr; + return HDF_ERR_NOT_SUPPORT; +} +#define OsalTestBitWrapper(nr, addr) BitDoNotSupport(nr, addr) +#define OsalTestSetBitWrapper(nr, addr) BitDoNotSupport(nr, addr) +#define OsalTestClearBitWrapper(nr, addr) BitDoNotSupport(nr, addr) +#define OsalClearBitWrapper(nr, addr) BitDoNotSupport(nr, addr) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* OSAL_ATOMIC_DEF_H */ + diff --git a/uhdf2/osal/osal_sysevent.c b/uhdf2/osal/osal_sysevent.c new file mode 100644 index 0000000..78116ef --- /dev/null +++ b/uhdf2/osal/osal_sysevent.c @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "osal_sysevent.h" +#include "hdf_io_service_if.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "osal_mutex.h" + +#define HDF_LOG_TAG usysevent + +#define KEVENT_IOSERVICE_NAME "hdf_kevent" + +#define KEVENT_COMPLETE_EVENT 1 + +struct HdfSysEventNotifier { + struct OsalMutex mutex; + struct DListHead notifyNodeList; + struct DListHead waitList; + struct HdfDevEventlistener ioServiceListener; + struct HdfIoService *keventIoService; +}; + +struct HdfSysEventNotifier *g_hdfSysEventNotifier; + +static struct HdfSysEventNotifier *HdfSysEventNotifierGetInstance(void) +{ + if (g_hdfSysEventNotifier != NULL) { + return g_hdfSysEventNotifier; + } + + struct HdfSysEventNotifier *notifier = OsalMemCalloc(sizeof(struct HdfSysEventNotifier)); + if (notifier == NULL) { + return NULL; + } + + int ret = OsalMutexInit(¬ifier->mutex); + if (ret != HDF_SUCCESS) { + OsalMemFree(notifier); + return NULL; + } + + DListHeadInit(¬ifier->notifyNodeList); + + g_hdfSysEventNotifier = notifier; + + return notifier; +} + +static int FinishEvent(struct HdfIoService *service, const struct HdfSysEvent *event) +{ + struct HdfSBuf *sbuf = HdfSBufObtain(sizeof(uint64_t)); + + if (sbuf == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + + if (!HdfSbufWriteUint64(sbuf, event->syncToken)) { + HdfSBufRecycle(sbuf); + return HDF_FAILURE; + } + + int ret = service->dispatcher->Dispatch(&service->object, KEVENT_COMPLETE_EVENT, sbuf, NULL); + if (ret != HDF_SUCCESS) { + HDF_LOGE("failed to finish sysevent, %d", ret); + } + + HdfSBufRecycle(sbuf); + return ret; +} + +static int OnKEventReceived( + struct HdfDevEventlistener *listener, struct HdfIoService *service, uint32_t id, struct HdfSBuf *data) +{ + struct HdfSysEventNotifier *notifier = (struct HdfSysEventNotifier *)listener->priv; + if (notifier == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + if (id != HDF_SYSEVENT) { + return HDF_ERR_INVALID_OBJECT; + } + + struct HdfSysEvent *receivedEvent = NULL; + uint32_t receivedEventLen = 0; + + if (!HdfSbufReadBuffer(data, (const void **)&receivedEvent, &receivedEventLen) + || receivedEventLen != sizeof(struct HdfSysEvent)) { + HDF_LOGE("failed to read kevent object"); + return HDF_FAILURE; + } + + receivedEvent->content = HdfSbufReadString(data); + + OsalMutexLock(¬ifier->mutex); + + struct HdfSysEventNotifyNode *notifyNode = NULL; + DLIST_FOR_EACH_ENTRY(notifyNode, ¬ifier->notifyNodeList, struct HdfSysEventNotifyNode, listNode) + { + if (receivedEvent->eventClass & notifyNode->classFilter) { + (void)notifyNode->callback( + notifyNode, receivedEvent->eventClass, receivedEvent->eventid, receivedEvent->content); + } + } + + if (receivedEvent->syncToken != 0) { + FinishEvent(service, receivedEvent); + } + + OsalMutexUnlock(¬ifier->mutex); + + return HDF_SUCCESS; +} + +static int InitKeventIoServiceListenerLocked(struct HdfSysEventNotifier *notifier) +{ + if (notifier->keventIoService == NULL) { + notifier->keventIoService = HdfIoServiceBind(KEVENT_IOSERVICE_NAME); + } + if (notifier->keventIoService == NULL) { + HDF_LOGE(" ioservice %s is invalid", KEVENT_IOSERVICE_NAME); + return HDF_DEV_ERR_NO_DEVICE; + } + + notifier->ioServiceListener.onReceive = OnKEventReceived; + notifier->ioServiceListener.priv = notifier; + int ret = HdfDeviceRegisterEventListener(notifier->keventIoService, ¬ifier->ioServiceListener); + if (ret != HDF_SUCCESS) { + HDF_LOGE(" ioservice %s is invalid", KEVENT_IOSERVICE_NAME); + HdfIoServiceRecycle(notifier->keventIoService); + notifier->keventIoService = NULL; + } + + return ret; +} + +static void DeInitKeventIoServiceListenerLocked(struct HdfSysEventNotifier *notifier) +{ + if (notifier->keventIoService == NULL) { + return; + } + + (void)HdfDeviceUnregisterEventListener(notifier->keventIoService, ¬ifier->ioServiceListener); + HdfIoServiceRecycle(notifier->keventIoService); + notifier->keventIoService = NULL; +} + +int HdfSysEventNotifyRegister(struct HdfSysEventNotifyNode *notifierNode, uint64_t classSet) +{ + if (notifierNode == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + struct HdfSysEventNotifier *notifier = HdfSysEventNotifierGetInstance(); + + if (notifier == NULL) { + return HDF_DEV_ERR_NO_MEMORY; + } + + OsalMutexLock(¬ifier->mutex); + DListInsertTail(¬ifierNode->listNode, ¬ifier->notifyNodeList); + notifierNode->classFilter = classSet; + int ret = InitKeventIoServiceListenerLocked(notifier); + if (ret != HDF_SUCCESS) { + DListRemove(¬ifierNode->listNode); + } + OsalMutexUnlock(¬ifier->mutex); + + return ret; +} + +void HdfSysEventNotifyUnregister(struct HdfSysEventNotifyNode *notifierNode) +{ + if (notifierNode == NULL) { + return; + } + + struct HdfSysEventNotifier *notifier = HdfSysEventNotifierGetInstance(); + + if (notifier == NULL) { + return; + } + OsalMutexLock(¬ifier->mutex); + + DListRemove(¬ifierNode->listNode); + if (DListIsEmpty(¬ifier->notifyNodeList)) { + DeInitKeventIoServiceListenerLocked(notifier); + } + OsalMutexUnlock(¬ifier->mutex); +} diff --git a/uhdf2/osal/test/BUILD.gn b/uhdf2/osal/test/BUILD.gn new file mode 100755 index 0000000..2736e5c --- /dev/null +++ b/uhdf2/osal/test/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (c) 2021 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/test.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +module_output_path = "hdf/osal" + +###########################palTest########################### +ohos_unittest("OsalTest") { + module_out_path = module_output_path + sources = [ "unittest/common/osal_slist_test.cpp" ] + + include_dirs = [ + "//utils/native/base/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/utils/include", + "unittest/common/", + ] + + deps = [ + "$hdf_uhdf_path/osal:libhdf_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} + +module_output_path = "hdf/sbuf" +ohos_unittest("SbufTest") { + module_out_path = module_output_path + sources = [ + "$hdf_framework_path/core/manager/test/unittest/common/hdf_sbuf_test.cpp", + ] + + deps = [ + "$hdf_uhdf_path/osal:libhdf_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} + +###########################end########################### +group("unittest") { + testonly = true + deps = [ + ":OsalTest", + ":SbufTest", + ] +} diff --git a/uhdf2/osal/test/unittest/common/osal_slist_test.cpp b/uhdf2/osal/test/unittest/common/osal_slist_test.cpp new file mode 100755 index 0000000..c6c8377 --- /dev/null +++ b/uhdf2/osal/test/unittest/common/osal_slist_test.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "hdf_log.h" +#include "hdf_slist.h" +#include "osal_mem.h" +#include "securec.h" + +#define HDF_LOG_TAG osal_slist_test_cpp + +namespace OHOS { +using namespace testing::ext; + +class HdfOsalSlistTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void HdfOsalSlistTest::SetUpTestCase() +{ +} + +void HdfOsalSlistTest::TearDownTestCase() +{ +} + +void HdfOsalSlistTest::SetUp() +{ +} + +void HdfOsalSlistTest::TearDown() +{ +} + +struct TestList { + struct HdfSListNode entry; + uint32_t data; +}; + +void TestListDeleter(struct HdfSListNode *listNode) +{ + struct TestList *data = (struct TestList *)listNode; + if (data != nullptr) { + data->data = 0; + OsalMemFree(data); + } +} + +bool HdfTestSListSearchCompare(struct HdfSListNode *entry, uint32_t data) +{ + struct TestList *node = (struct TestList *)entry; + if (node->data == data) { + return true; + } + return false; +} + +/* +* @tc.name: SlistInitTest001 +* @tc.desc: Slist init +* @tc.type: FUNC +* @tc.require: AR000DT1TK +*/ +HWTEST_F(HdfOsalSlistTest, SlistInitTest001, TestSize.Level1) +{ + struct HdfSList list; + HdfSListInit(&list); + EXPECT_EQ(NULL, list.root); + EXPECT_TRUE(HdfSListIsEmpty(&list)); +} + +/* +* @tc.name: SlistCountTest001 +* @tc.desc: Slist init +* @tc.type: FUNC +* @tc.require: AR000DT1TK +*/ +HWTEST_F(HdfOsalSlistTest, SlistCountTest001, TestSize.Level1) +{ + struct HdfSList list; + HdfSListInit(&list); + uint32_t insertData[] = {3, 1, 4, 5}; + int totalCount = (int)(sizeof(insertData)/ sizeof(insertData[0])); + for (int i = 0; i < totalCount; i++) { + struct TestList *testData = (struct TestList *)OsalMemAlloc(sizeof(struct TestList)); + testData->data = insertData[i]; + HdfSListAdd(&list, &testData->entry); + } + EXPECT_EQ(totalCount, HdfSListCount(&list)); + HdfSListFlush(&list, TestListDeleter); + EXPECT_EQ(0, HdfSListCount(&list)); +} + +/* +* @tc.name: SlistSearchTest001 +* @tc.desc: Slist init +* @tc.type: FUNC +* @tc.require: AR000DT1TK +*/ +HWTEST_F(HdfOsalSlistTest, SlistSearchTest001, TestSize.Level1) +{ + struct HdfSList list; + HdfSListInit(&list); + uint32_t insertData[] = {3, 1, 4, 5}; + int totalCount = (int)(sizeof(insertData)/ sizeof(insertData[0])); + for (int i = 0; i < totalCount; i++) { + struct TestList *testData = (struct TestList *)OsalMemAlloc(sizeof(struct TestList)); + testData->data = insertData[i]; + HdfSListAdd(&list, &testData->entry); + } + EXPECT_EQ(totalCount, HdfSListCount(&list)); + struct HdfSListNode *resultNode = HdfSListSearch(&list, 5, HdfTestSListSearchCompare); + EXPECT_TRUE(resultNode != NULL); + EXPECT_EQ(5, (int)(((struct TestList *)resultNode)->data)); + HdfSListFlush(&list, TestListDeleter); + EXPECT_EQ(0, HdfSListCount(&list)); +} + +/* +* @tc.name: SlistGetTest001 +* @tc.desc: Slist init +* @tc.type: FUNC +* @tc.require: AR000DT1TK +*/ +HWTEST_F(HdfOsalSlistTest, SlistGetTest001, TestSize.Level1) +{ + struct HdfSList list; + HdfSListInit(&list); + uint32_t insertData[] = {3, 1, 4, 5}; + int totalCount = (int)(sizeof(insertData)/ sizeof(insertData[0])); + for (int i = 0; i < totalCount; i++) { + struct TestList *testData = (struct TestList *)OsalMemAlloc(sizeof(struct TestList)); + testData->data = insertData[i]; + HdfSListAdd(&list, &testData->entry); + } + EXPECT_EQ(totalCount, HdfSListCount(&list)); + struct HdfSListNode *resultNode = HdfSListGetLast(&list); + EXPECT_TRUE(resultNode != NULL); + EXPECT_EQ(3, (int)(((struct TestList *)resultNode)->data)); + HdfSListFlush(&list, TestListDeleter); + EXPECT_EQ(0, HdfSListCount(&list)); + + for (int i = 0; i < totalCount; i++) { + struct TestList *testData = (struct TestList *)OsalMemAlloc(sizeof(struct TestList)); + testData->data = insertData[i]; + HdfSListAddTail(&list, &testData->entry); + } + EXPECT_EQ(totalCount, HdfSListCount(&list)); + resultNode = HdfSListGetLast(&list); + EXPECT_TRUE(resultNode != NULL); + EXPECT_EQ(5, (int)(((struct TestList *)resultNode)->data)); + HdfSListFlush(&list, TestListDeleter); + EXPECT_EQ(0, HdfSListCount(&list)); +} +} // namespace OHOS diff --git a/uhdf2/osal/test/unittest/common/sample_driver.c b/uhdf2/osal/test/unittest/common/sample_driver.c new file mode 100644 index 0000000..5979ff4 --- /dev/null +++ b/uhdf2/osal/test/unittest/common/sample_driver.c @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "sample_driver.h" +#include +#include "hdf_log.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" + +#define HDF_LOG_TAG sample_driver + +#ifndef INT32_MAX +#define INT32_MAX 0x7fffffff +#endif + +void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) +{ + (void)deviceObject; + return; +} + +int32_t SampleDriverRegisteDevice(struct HdfSBuf *data) +{ + if (data == NULL) { + return HDF_FAILURE; + } + + const char *moduleName = HdfSbufReadString(data); + if (moduleName == NULL) { + return HDF_FAILURE; + } + const char *serviceName = HdfSbufReadString(data); + if (serviceName == NULL) { + return HDF_FAILURE; + } + + struct HdfDeviceObject *devObj = HdfRegisteDevice(moduleName, serviceName); + if (devObj == NULL) { + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +int32_t SampleDriverUnregisteDevice(struct HdfSBuf *data) +{ + if (data == NULL) { + return HDF_FAILURE; + } + + const char *moduleName = HdfSbufReadString(data); + if (moduleName == NULL) { + return HDF_FAILURE; + } + const char *serviceName = HdfSbufReadString(data); + if (serviceName == NULL) { + return HDF_FAILURE; + } + HdfUnregisteDevice(moduleName, serviceName); + return HDF_SUCCESS; +} + +int32_t SampleDriverSendEvent(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *data, bool broadcast) +{ + int32_t ret = broadcast ? HdfDeviceSendEvent(client->device, id, data) : + HdfDeviceSendEventToClient(client, id, data); + HDF_LOGI("%s:report event done, broadcast = %d", __func__, broadcast); + return ret; +} + +int32_t SampleDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_SUCCESS; + if (reply == NULL || client == NULL) { + return HDF_FAILURE; + } + HDF_LOGI("%s:cmdId = %d", __func__, cmdId); + switch (cmdId) { + case SAMPLE_DRIVER_REGISTE_DEVICE: { + ret = SampleDriverRegisteDevice(data); + HdfSbufWriteInt32(reply, ret); + break; + } + case SAMPLE_DRIVER_UNREGISTE_DEVICE: + ret = SampleDriverUnregisteDevice(data); + HdfSbufWriteInt32(reply, ret); + break; + case SAMPLE_DRIVER_SENDEVENT_SINGLE_DEVICE: + ret = SampleDriverSendEvent(client, cmdId, data, false); + HdfSbufWriteInt32(reply, INT32_MAX); + break; + case SAMPLE_DRIVER_SENDEVENT_BROADCAST_DEVICE: + ret = SampleDriverSendEvent(client, cmdId, data, true); + HdfSbufWriteInt32(reply, INT32_MAX); + break; + default: + break; + } + + return ret; +} + +int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) +{ + HDF_LOGD("%s::enter!, deviceObject=%p", __func__, deviceObject); + if (deviceObject == NULL) { + return HDF_FAILURE; + } + static struct IDeviceIoService testService = { + .Dispatch = SampleDriverDispatch, + .Open = NULL, + .Release = NULL, + }; + deviceObject->service = &testService; + return HDF_SUCCESS; +} + +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); + diff --git a/uhdf2/osal/test/unittest/common/sample_driver.h b/uhdf2/osal/test/unittest/common/sample_driver.h new file mode 100644 index 0000000..4a915ad --- /dev/null +++ b/uhdf2/osal/test/unittest/common/sample_driver.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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 HDF_SAMPLE_DRIVER_H +#define HDF_SAMPLE_DRIVER_H + +#include "hdf_object.h" + +#define SAMPLE_SERVICE "sample_service" + +typedef enum { + SAMPLE_DRIVER_REGISTE_DEVICE = 0, + SAMPLE_DRIVER_UNREGISTE_DEVICE, + SAMPLE_DRIVER_SENDEVENT_SINGLE_DEVICE, + SAMPLE_DRIVER_SENDEVENT_BROADCAST_DEVICE, +} SAMPLE_DRIVER_CMDID; + +struct HdfDeviceObject *GetDeviceObject(void); + +#endif // HDF_MAIN_TEST_H + diff --git a/uhdf2/security/BUILD.gn b/uhdf2/security/BUILD.gn new file mode 100755 index 0000000..4bf2077 --- /dev/null +++ b/uhdf2/security/BUILD.gn @@ -0,0 +1,51 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") +group("uhdf_sec_pkg") { + deps = [ ":libhdf_sec" ] +} + +ohos_shared_library("libhdf_sec") { + include_dirs = [ + "//utils/native/base/include", + "//drivers/framework/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/core/sec/include", + "//drivers/framework/include/platform", + "$hdf_uhdf_path/security/include", + "$hdf_framework_path/include/config", + "$hdf_framework_path/ability/config/hcs_parser/include", + ] + + sources = [ "src/hdf_security.c" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/osal:libhdf_utils", + "$hdf_uhdf_path/pal:libhdf_pal", + "$hdf_uhdf_path/config:libhdf_hcs", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + # install_images = [ "vendor" ] + subsystem_name = "hdf" +} diff --git a/uhdf2/security/include/hdf_security.h b/uhdf2/security/include/hdf_security.h new file mode 100644 index 0000000..d221b48 --- /dev/null +++ b/uhdf2/security/include/hdf_security.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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 HDF_SECURITY_H +#define HDF_SECURITY_H +#include "hdf_map.h" +#include "device_resource_if.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +int32_t HdfRegisterAllDevSecId(const struct DeviceResourceNode *hostRoot); +int32_t HdfSetCurrentHostSecurity(const char *hostName, int32_t procId); +void HdfDelSecurity(const char *hostName); +Map *HdfSecGetHashMap(); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* HDF_SECURITY_H */ \ No newline at end of file diff --git a/uhdf2/security/src/hdf_security.c b/uhdf2/security/src/hdf_security.c new file mode 100644 index 0000000..71cc9b9 --- /dev/null +++ b/uhdf2/security/src/hdf_security.c @@ -0,0 +1,245 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_security.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "hcs_tree_if.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_sec.h" +#include "pal_if.h" +#include "securec.h" + +#define HDF_LOG_TAG hdf_security +#define HDF_SECURE_PATH "/dev/hdf_secure" +#define HDF_SEC_PERM_ELEM_NAME "permission" +#define HDF_SEC_HOST_NAME "hostName" +#define MAX_RANDOM_BYTE_LEN 8 +#define RAND_OPS_SUCCESS 1 +#define MAX_SEED_LEN 64 +#define HDF_TRANSFORM_STEP 2 +#define HDF_SECURE_INVALID_PERM 0 +#define HDF_SECURE_SET_ALL_ID 0 +#define HDF_SECURE_SET_CUR_ID 1 +#define HDF_SECURE_DEL_CUR_ID 2 + +#define BITS_PER_LONG 64 +#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) +#define BIT_WORD(nr) ((nr) / BITS_PER_LONG) + +const static char *g_hdfSecStrArray[PAL_MAX_TYPE] = { + [PAL_I2C_TYPE] = "i2c", + [PAL_SPI_TYPE] = "spi", + [PAL_GPIO_TYPE] = "gpio", + [PAL_PINCTRL_TYPE] = "pinctl", + [PAL_CLOCK_TYPE] = "clock", + [PAL_REGULATOR_TYPE] = "regulator", + [PAL_MIPI_TYPE] = "mipi", + [PAL_UART_TYPE] = "uart", + [PAL_SDIO_TYPE] = "sdio", + [PAL_MDIO_TYPE] = "mdio", + [PAL_APB_TYPE] = "apb", + [PAL_PCIE_TYPE] = "pcie", + [PAL_PCM_TYPE] = "pcm", + [PAL_I2S_TYPE] = "i2s", + [PAL_PWM_TYPE] = "pwm", + [PAL_DMA_TYPE] = "dma", + [PAL_EFUSE_TYPE] = "efuse", + [PAL_FLASH_TYPE] = "flash", + [PAL_EMMC_TYPE] = "emmc", + [PAL_RTC_TYPE] = "rtc", + [PAL_ADC_TYPE] = "adc", + [PAL_WDT_TYPE] = "wdt", + [PAL_I3C_TYPE] = "i3c", +}; + +static Map g_indexMap; + +Map *HdfSecGetHashMap() +{ + static Map hdfHashMap = { 0 }; + if (hdfHashMap.nodeSize == 0) { + HDF_LOGI("Init hdfHashMap"); + MapInit(&hdfHashMap); + } + return &hdfHashMap; +} + +static void InitStrToIndexMap() +{ + MapInit(&g_indexMap); + for (int i = 0; i < PAL_MAX_TYPE; ++i) { + MapSet(&g_indexMap, g_hdfSecStrArray[i], &i, sizeof(int *)); + } +} + +static void HdfSetBit(uint32_t nr, volatile uint64_t *addr) +{ + unsigned long mask = BIT_MASK(nr); + unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); + *p |= mask; +} + +static int32_t HdfGetRandom(int numBytes, uint8_t *random) +{ + if (RAND_bytes(random, numBytes) != RAND_OPS_SUCCESS) { + HDF_LOGE("%s: RAND_bytes failed", __FUNCTION__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static void HdfCalculatePerms(const char *permStr, volatile uint64_t *perms) +{ + uint32_t *result = NULL; + result = (uint32_t *)MapGet(&g_indexMap, permStr); + if (result == NULL) { + HDF_LOGE("%s: %s is not exist in map", __FUNCTION__, permStr); + return; + } + HdfSetBit(*result, perms); +} + +static int32_t HdfSecCheckParameters(const char *id) +{ + if (id == NULL) { + HDF_LOGE("HdfSecCheckParameters failed, id is null"); + return HDF_FAILURE; + } + uint32_t len = (uint32_t)strlen(id); + if (len >= (ID_MAX_SIZE - 1)) { + HDF_LOGE("HdfSecCheckParameters failed, id length is %u", len); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t HdfUpdateSecurityId(const char *id, const uint64_t perms, const int32_t isSetCurrentSecId) +{ + if (HdfSecCheckParameters(id) != HDF_SUCCESS) { + HDF_LOGE("%s check id failed", __FUNCTION__); + return HDF_FAILURE; + } + struct SecInfo secInfo = { + .secId = { 0 }, + .secMap = { 0 } + }; + int32_t ret = strncpy_s(secInfo.secId, ID_MAX_SIZE, id, ID_MAX_SIZE - 1); + if (ret != EOK) { + HDF_LOGE("%s strncpy_s id failed", __FUNCTION__); + return HDF_FAILURE; + } + secInfo.secMap[0] = perms; + + char path[PATH_MAX + 1] = { 0 }; + if (realpath(HDF_SECURE_PATH, path) == NULL) { + HDF_LOGE("file %s is invalid", HDF_SECURE_PATH); + return HDF_FAILURE; + } + int32_t fd = open(path, O_WRONLY); + if (fd < 0) { + HDF_LOGE("open %s failed, errno is %d", HDF_SECURE_PATH, errno); + return HDF_FAILURE; + } + + if (isSetCurrentSecId == 0) { + ret = ioctl(fd, HDF_SECURE_SET_INFO, &secInfo); + } else if (isSetCurrentSecId == 1) { + ret = ioctl(fd, HDF_SECURE_SET_CURRENT_ID, &secInfo); + } else { + ret = ioctl(fd, HDF_SECURE_DELETE_INFO, &secInfo); + } + + close(fd); + fd = -1; + if (ret != 0) { + HDF_LOGE("%s ioctl error, ret is %d", __FUNCTION__, ret); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t HdfRegisterSecurityId(const char *id, const uint64_t perm) +{ + return HdfUpdateSecurityId(id, perm, HDF_SECURE_SET_ALL_ID); +} + +int32_t HdfRegisterAllDevSecId(const struct DeviceResourceNode *hostRoot) +{ + if (hostRoot == NULL) { + HDF_LOGE("device_node linked list completed"); + return HDF_FAILURE; + } + + uint64_t random = 0; + int32_t ret = HdfGetRandom(MAX_RANDOM_BYTE_LEN, (uint8_t *)&random); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s HdfGetRandom failed", __FUNCTION__); + return HDF_FAILURE; + } + + InitStrToIndexMap(); + + struct DeviceResourceNode *driverRoot = hostRoot->child; + const char *permStr = NULL; + volatile uint64_t perms = HDF_SECURE_INVALID_PERM; + while (driverRoot != NULL) { + const struct DeviceResourceNode *deviceRoot = driverRoot->child; + while (deviceRoot != NULL) { + int32_t permNums = HcsGetElemNum(deviceRoot, HDF_SEC_PERM_ELEM_NAME); + HDF_LOGD("name is %s, permNums is %d", deviceRoot->name, permNums); + for (int i = 0; i < permNums; ++i) { + HcsGetStringArrayElem(deviceRoot, HDF_SEC_PERM_ELEM_NAME, i, &permStr, NULL); + HdfCalculatePerms(permStr, &perms); + } + deviceRoot = deviceRoot->sibling; + } + driverRoot = driverRoot->sibling; + } + if (perms != HDF_SECURE_INVALID_PERM) { + const char *hostName = NULL; + HcsGetString(hostRoot, HDF_SEC_HOST_NAME, &hostName, NULL); + ret = HdfRegisterSecurityId(hostName, perms); + if (ret != HDF_SUCCESS) { + HDF_LOGE("HdfCreateSecurityId failed for %s", hostRoot->name); + } + perms = HDF_SECURE_INVALID_PERM; + } + return HDF_SUCCESS; +} + +int32_t HdfSetCurrentHostSecurity(const char *hostName, int32_t procId) +{ + return HdfUpdateSecurityId(hostName, procId, HDF_SECURE_SET_CUR_ID); +} + +void HdfDelSecurity(const char *hostName) +{ + if (hostName == NULL) { + return; + } + int32_t ret = HdfUpdateSecurityId(hostName, 0, HDF_SECURE_DEL_CUR_ID); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s delete %s security failed", __func__, hostName); + } +} diff --git a/uhdf2/shared/include/dev_attribute_parcel.h b/uhdf2/shared/include/dev_attribute_parcel.h new file mode 100644 index 0000000..cd0892c --- /dev/null +++ b/uhdf2/shared/include/dev_attribute_parcel.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 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 HDF_ATTRIBUTE_PARCEL_H +#define HDF_ATTRIBUTE_PARCEL_H + +#include "hdf_device_info_full.h" +#include "hdf_sbuf.h" + +bool DeviceAttributeFullWrite(const struct HdfDeviceInfoFull *attribute, struct HdfSBuf *sbuf); +struct HdfDeviceInfoFull *DeviceAttributeFullRead(struct HdfSBuf *sbuf); + +#endif /* HDF_ATTRIBUTE_PARCEL_H */ \ No newline at end of file diff --git a/uhdf2/shared/include/hdf_device_info_full.h b/uhdf2/shared/include/hdf_device_info_full.h new file mode 100644 index 0000000..e26be29 --- /dev/null +++ b/uhdf2/shared/include/hdf_device_info_full.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 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 DEVICE_ATTRIBUTE_FULL_H +#define DEVICE_ATTRIBUTE_FULL_H + +#include "hdf_device_info.h" + +struct HdfDeviceInfoFull { + struct HdfDeviceInfo super; + void *deviceHandle; +}; + +struct HdfDeviceInfoFull *HdfDeviceInfoFullReinterpretCast(const struct HdfDeviceInfo *attribute); +struct HdfDeviceInfoFull *HdfDeviceInfoFullNewInstance(void); +void HdfDeviceInfoFullFreeInstance(struct HdfDeviceInfoFull *attribute); +void HdfDeviceInfoFullDelete(struct HdfSListNode *listEntry); + +#endif /* DEVICE_ATTRIBUTE_FULL_H */ diff --git a/uhdf2/shared/include/hdf_remote_service.h b/uhdf2/shared/include/hdf_remote_service.h new file mode 100644 index 0000000..887741e --- /dev/null +++ b/uhdf2/shared/include/hdf_remote_service.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2021 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 HDF_REMOTE_SERVICE_H +#define HDF_REMOTE_SERVICE_H + +#include "hdf_object.h" +#include "hdf_sbuf.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct HdfRemoteDispatcher { + int (*Dispatch)(struct HdfRemoteService *, int, struct HdfSBuf *, struct HdfSBuf *); +}; + +struct HdfDeathRecipient { + void (*OnRemoteDied)(struct HdfDeathRecipient *, struct HdfRemoteService *); +}; + +struct HdfRemoteService { + struct HdfObject object_; + struct HdfObject *target; + struct HdfRemoteDispatcher *dispatcher; + bool isHw; +}; + +struct HdfRemoteService *HdfRemoteServiceObtain( + struct HdfObject *object, struct HdfRemoteDispatcher *dispatcher); + +void HdfRemoteServiceRecycle(struct HdfRemoteService *service); + +void HdfRemoteServiceAddDeathRecipient(struct HdfRemoteService *service, struct HdfDeathRecipient *recipient); + +int HdfRemoteServiceRegister(int32_t serviceId, struct HdfRemoteService *service); + +struct HdfRemoteService *HdfRemoteServiceGet(int32_t serviceId); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* HDF_REMOTE_SERVICE_H */ diff --git a/uhdf2/shared/src/dev_attribute_parcel.c b/uhdf2/shared/src/dev_attribute_parcel.c new file mode 100644 index 0000000..b01c4e2 --- /dev/null +++ b/uhdf2/shared/src/dev_attribute_parcel.c @@ -0,0 +1,217 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "dev_attribute_parcel.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG dev_attribute_parcel + +#define ATTRIBUTE_PRIVATE_DATA_LENGTH_NULL 0 + +static bool DeviceAttributePrivateWrite(const struct HdfDeviceInfoFull *attribute, struct HdfSBuf *sbuf) +{ + bool flag; + uint8_t byteData; + int32_t wordData; + struct UsbPnpNotifyServiceInfo *info = (struct UsbPnpNotifyServiceInfo *)(attribute->super.private); + int32_t i; + + if (info == NULL) { + HDF_LOGE("Device attribute writePrivate private is NULL"); + return false; + } + + wordData = info->length; + flag = HdfSbufWriteInt32(sbuf, wordData); + if (flag == false) { + HDF_LOGE("Device attribute writePrivate length failed, flag=%{public}d", flag); + return false; + } + wordData = info->devNum; + flag = HdfSbufWriteInt32(sbuf, wordData); + if (flag == false) { + HDF_LOGE("Device attribute writePrivate devNum failed, flag=%{public}d", flag); + return false; + } + wordData = info->busNum; + flag = HdfSbufWriteInt32(sbuf, wordData); + if (flag == false) { + HDF_LOGE("Device attribute writePrivate busNum failed, flag=%{public}d", flag); + return false; + } + wordData = info->interfaceLength; + flag = HdfSbufWriteInt32(sbuf, wordData); + if (flag == false) { + HDF_LOGE("Device attribute writePrivate interfaceLength failed, flag=%{public}d", flag); + return false; + } + for (i = 0; i < info->interfaceLength; i++) { + byteData = info->interfaceNumber[i]; + flag = HdfSbufWriteUint8(sbuf, byteData); + if (flag == false) { + HDF_LOGE("Device attribute writePrivate interfaceNumber[%{public}d] failed, flag=%{public}d", flag, i); + return false; + } + } + + return true; +} + +static const void *DeviceAttributePrivateRead(struct HdfSBuf *sbuf) +{ + bool flag; + int32_t wordData; + uint8_t byteData; + int32_t i; + + struct UsbPnpNotifyServiceInfo *info = (struct UsbPnpNotifyServiceInfo *)OsalMemCalloc(sizeof(struct UsbPnpNotifyServiceInfo)); + if (info == NULL) { + HDF_LOGE("%{public}s: OsalMemCalloc failed, info is null", __func__); + return NULL; + } + + flag = HdfSbufReadInt32(sbuf, &wordData); + if (flag == false) { + HDF_LOGE("Device attribute readPrivate length failed, flag=%{public}d", flag); + return NULL; + } + info->length = wordData; + + if (info->length == ATTRIBUTE_PRIVATE_DATA_LENGTH_NULL) { + HDF_LOGW("%{public}s: readPrivate is NULL and info->length=%{public}d", __func__, info->length); + } else { + flag = HdfSbufReadInt32(sbuf, &wordData); + if (flag == false) { + HDF_LOGE("Device attribute readPrivate devNum failed, flag=%{public}d", flag); + return NULL; + } + info->devNum = wordData; + + flag = HdfSbufReadInt32(sbuf, &wordData); + if (flag == false) { + HDF_LOGE("Device attribute readPrivate busNum failed, flag=%{public}d", flag); + return NULL; + } + info->busNum = wordData; + + flag = HdfSbufReadInt32(sbuf, &wordData); + if (flag == false) { + HDF_LOGE("Device attribute readPrivate interfaceLength failed, flag=%{public}d", flag); + return NULL; + } + info->interfaceLength = wordData; + + for (i = 0; i < info->interfaceLength; i++) { + flag = HdfSbufReadUint8(sbuf, &byteData); + if (flag == false) { + HDF_LOGE("Device attribute readPrivate interfaceNumber[%{public}d] failed, flag=%{public}d", flag, i); + return NULL; + } + info->interfaceNumber[i] = byteData; + } + } + + return (const void *)info; +} + +bool DeviceAttributeFullWrite(const struct HdfDeviceInfoFull *attribute, struct HdfSBuf *sbuf) +{ + if (attribute == NULL || sbuf == NULL) { + return false; + } + + uint8_t ret = 1; + ret &= HdfSbufWriteInt32(sbuf, attribute->super.deviceId); + ret &= HdfSbufWriteInt32(sbuf, attribute->super.hostId); + ret &= HdfSbufWriteInt32(sbuf, attribute->super.policy); + ret &= HdfSbufWriteString(sbuf, attribute->super.svcName); + ret &= HdfSbufWriteString(sbuf, attribute->super.moduleName); + ret &= HdfSbufWriteString(sbuf, attribute->super.deviceMatchAttr); + if (attribute->super.private != NULL) { + ret &= DeviceAttributePrivateWrite(attribute, sbuf); + } else { + HDF_LOGW("%{public}s: Device attribute write private is NULL", __func__); + uint8_t privateLength = ATTRIBUTE_PRIVATE_DATA_LENGTH_NULL; + ret &= HdfSbufWriteUint8(sbuf, privateLength); + } + + if (ret == 0) { + HDF_LOGE("Device attribute write parcel failed"); + return false; + } + return true; +} + +struct HdfDeviceInfoFull *DeviceAttributeFullRead(struct HdfSBuf *sbuf) +{ + if (sbuf == NULL) { + return NULL; + } + + struct HdfDeviceInfoFull *attribute = HdfDeviceInfoFullNewInstance(); + if (attribute == NULL) { + HDF_LOGE("OsalMemCalloc failed, attribute is null"); + return NULL; + } + HdfSbufReadUint16(sbuf, &attribute->super.deviceId); + HdfSbufReadUint16(sbuf, &attribute->super.hostId); + HdfSbufReadUint16(sbuf, &attribute->super.policy); + do { + const char *svcName = HdfSbufReadString(sbuf); + if (svcName == NULL) { + HDF_LOGE("Read from parcel failed, svcName is null"); + break; + } + attribute->super.svcName = strdup(svcName); + if (attribute->super.svcName == NULL) { + HDF_LOGE("Read from parcel failed, strdup svcName fail"); + break; + } + const char *moduleName = HdfSbufReadString(sbuf); + if (moduleName == NULL) { + HDF_LOGE("Read from parcel failed, driverPath is null"); + break; + } + attribute->super.moduleName = strdup(moduleName); + if (attribute->super.moduleName == NULL) { + HDF_LOGE("Read from parcel failed, strdup moduleName fail"); + break; + } + + const char *deviceMatchAttr = HdfSbufReadString(sbuf); + if (deviceMatchAttr == NULL) { + HDF_LOGE("%{public}s: Read from parcel failed, deviceMatchAttr is null", __func__); + break; + } + attribute->super.deviceMatchAttr = strdup(deviceMatchAttr); + + attribute->super.private = DeviceAttributePrivateRead(sbuf); + if (attribute->super.private == NULL) { + HDF_LOGE("%{public}s: Read from parcel failed, private is null", __func__); + break; + } + + HDF_LOGD("%{public}s: AttributeRead moduleName=%{public}s, svcName=%{public}s, deviceMatchAttr=%{public}s, private=%{public}p, length=%{public}d", \ + __func__, moduleName, svcName, deviceMatchAttr, attribute->super.private, \ + ((struct UsbPnpNotifyServiceInfo *)(attribute->super.private))->length); + + return attribute; + } while (0); + + HdfDeviceInfoFullFreeInstance(attribute); + return NULL; +} diff --git a/uhdf2/shared/src/hdf_attribute_full.c b/uhdf2/shared/src/hdf_attribute_full.c new file mode 100644 index 0000000..0ccd373 --- /dev/null +++ b/uhdf2/shared/src/hdf_attribute_full.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include "hcs_parser.h" +#include "hcs_dm_parser.h" +#include "hcs_tree_if.h" +#include "hdf_attribute_manager.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG attribute_manager + +#ifdef __OHOS_STANDARD_SYS__ +#define HOST_CONFIG_PATH "/system/etc/hdfconfig" +#else +#define HOST_CONFIG_PATH "/system/etc/hdfconfig" +#endif +#define PRODUCT_PROPERTY "ro.build.product" +#define PRODUCT_NAME_MAX 128 + +static void StringLower(char *str, int strLen) +{ + for (int i = 0; i < strLen; ++i) { + str[i] = tolower(str[i]); + } +} + +static int GetProductName(char *name, int maxLen) +{ + (void)StringLower; + return strcpy_s(name, maxLen, "default"); +} + +const struct DeviceResourceNode *HdfGetRootNode(void) +{ + char productName[PRODUCT_NAME_MAX] = { 0 }; + char configPath[PATH_MAX] = { 0 }; + + int ret = GetProductName(productName, PRODUCT_NAME_MAX); + if (ret != HDF_SUCCESS) { + return NULL; + } + + ret = sprintf_s(configPath, PATH_MAX - 1, "%s/hdf_%s.hcb", HOST_CONFIG_PATH, productName); + if (ret < 0) { + HDF_LOGE("config path error"); + return NULL; + } + + const char *configFileName = configPath; + SetHcsBlobPath(configFileName); + const struct DeviceResourceNode *mgrRoot = HcsGetRootNode(); + return mgrRoot; +} + +bool HdfDeviceListAdd(const char *moduleName, const char *serviceName) +{ + (void)moduleName; + (void)serviceName; + return true; +} + +void HdfDeviceListDel(const char *moduleName, const char *serviceName) +{ + (void)moduleName; + (void)serviceName; +} \ No newline at end of file diff --git a/uhdf2/shared/src/hdf_device_info_full.c b/uhdf2/shared/src/hdf_device_info_full.c new file mode 100644 index 0000000..33bd376 --- /dev/null +++ b/uhdf2/shared/src/hdf_device_info_full.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "hdf_device_info_full.h" +#include +#include "osal_mem.h" + +struct HdfDeviceInfoFull *HdfDeviceInfoFullReinterpretCast(const struct HdfDeviceInfo *attribute) +{ + return (struct HdfDeviceInfoFull *)attribute; +} + +void HdfDeviceInfoFullConstruct(struct HdfDeviceInfoFull *attribute) +{ + HdfDeviceInfoConstruct(&attribute->super); + attribute->deviceHandle = NULL; +} + +struct HdfDeviceInfoFull* HdfDeviceInfoFullNewInstance() +{ + struct HdfDeviceInfoFull *fullAttribute = + (struct HdfDeviceInfoFull *)OsalMemCalloc(sizeof(struct HdfDeviceInfoFull)); + if (fullAttribute != NULL) { + HdfDeviceInfoFullConstruct(fullAttribute); + } + return fullAttribute; +} + +void HdfDeviceInfoFullFreeInstance(struct HdfDeviceInfoFull *attribute) +{ + if (attribute != NULL) { + if (attribute->super.moduleName != NULL) { + OsalMemFree((void *)attribute->super.moduleName); + } + if (attribute->super.svcName != NULL) { + OsalMemFree((void *)attribute->super.svcName); + } + if (attribute->super.deviceMatchAttr != NULL) { + OsalMemFree((void *)attribute->super.deviceMatchAttr); + } + if (attribute->super.private != NULL) { + OsalMemFree((void *)attribute->super.private); + } + if (attribute->deviceHandle != NULL) { + dlclose(attribute->deviceHandle); + } + OsalMemFree(attribute); + } +} + +void HdfDeviceInfoFullDelete(struct HdfSListNode *listEntry) +{ + struct HdfDeviceInfoFull *fullAttribute = (struct HdfDeviceInfoFull *)listEntry; + if (fullAttribute != NULL) { + HdfDeviceInfoFullFreeInstance(fullAttribute); + } +} + diff --git a/uhdf2/test/BUILD.gn b/uhdf2/test/BUILD.gn new file mode 100755 index 0000000..65b0163 --- /dev/null +++ b/uhdf2/test/BUILD.gn @@ -0,0 +1,17 @@ +# Copyright (c) 2021 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. + +group("hdf_test_uhdf") { + testonly = true + deps = [ "unittest/config:hdf_adapter_uhdf_test_config" ] +} diff --git a/uhdf2/test/resource/config/ohos_test.xml b/uhdf2/test/resource/config/ohos_test.xml new file mode 100755 index 0000000..23e3bfa --- /dev/null +++ b/uhdf2/test/resource/config/ohos_test.xml @@ -0,0 +1,22 @@ + + + + + + + + diff --git a/uhdf2/test/unittest/common/BUILD.gn b/uhdf2/test/unittest/common/BUILD.gn new file mode 100755 index 0000000..79f0c3f --- /dev/null +++ b/uhdf2/test/unittest/common/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2021 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") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("hdf_test_common_pub_config") { + visibility = [ ":*" ] + + include_dirs = [ "//drivers/framework/test/unittest/include" ] +} + +ohos_shared_library("libhdf_test_common") { + public_configs = [ ":hdf_test_common_pub_config" ] + sources = [ "//drivers/framework/test/unittest/common/hdf_common_test.c" ] + + include_dirs = [ "//drivers/framework/test/unittest/include" ] + + deps = [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//utils/native/base:utils", + ] + defines = [ "__OHOS__USER__" ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + subsystem_name = "hdf" + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/uhdf2/test/unittest/config/BUILD.gn b/uhdf2/test/unittest/config/BUILD.gn new file mode 100755 index 0000000..c0146f1 --- /dev/null +++ b/uhdf2/test/unittest/config/BUILD.gn @@ -0,0 +1,41 @@ +# Copyright (c) 2021 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/test.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +module_output_path = "hdf/config" +ohos_unittest("hdf_adapter_uhdf_test_config") { + module_out_path = module_output_path + include_dirs = [ "//drivers/framework/test/unittest/include" ] + + sources = [ "//drivers/framework/ability/config/test/unittest/common/hdf_config_test.cpp" ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + deps = [ + "$hdf_uhdf_path/osal:libhdf_utils", + "$hdf_uhdf_path/test/unittest/common:libhdf_test_common", + "//utils/native/base:utils", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/uhdf2/test/unittest/manager/BUILD.gn b/uhdf2/test/unittest/manager/BUILD.gn new file mode 100755 index 0000000..43d28d9 --- /dev/null +++ b/uhdf2/test/unittest/manager/BUILD.gn @@ -0,0 +1,127 @@ +# Copyright (c) 2021 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/test.gni") + +module_output_path = "hdf/manager" +ohos_unittest("hdf_adapter_uhdf_test_manager") { + module_out_path = module_output_path + include_dirs = [ + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/include/platform", + "//drivers/framework/include/core", + "//drivers/framework/core/common/include/host", + "//drivers/framework/core/shared/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/include/config", + "//drivers/framework/include", + "//drivers/framework/test/unittest/include", + "//drivers/framework/test/unittest/manager", + ] + + defines = [ "__USER__" ] + sources = [ "//drivers/framework/core/manager/test/unittest/common/hdf_lite_manager_test.cpp" ] + deps = [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/test/unittest/common:libhdf_test_common", + "//utils/native/base:utils", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} + +module_output_path = "hdf/ioservice" +ohos_unittest("hdf_adapter_uhdf_test_ioservice") { + module_out_path = module_output_path + include_dirs = [ + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/include/platform", + "//drivers/framework/include/core", + "//drivers/framework/core/common/include/host", + "//drivers/framework/core/shared/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/include/config", + "//drivers/framework/include", + "//drivers/framework/test/unittest/include", + "//drivers/framework/test/unittest/manager", + ] + + defines = [ "__USER__" ] + sources = [ "//drivers/framework/core/manager/test/unittest/common/hdf_ioservice_test.cpp" ] + deps = [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/test/unittest/common:libhdf_test_common", + "//utils/native/base:utils", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} +module_output_path = "hdf/sbuf" +ohos_unittest("hdf_adapter_uhdf_test_sbuf") { + module_out_path = module_output_path + include_dirs = [ + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/include/osal", + "//drivers/framework/include/utils", + "//drivers/framework/include", + "//drivers/framework/test/unittest/include", + "//drivers/framework/test/unittest/manager", + ] + + defines = [ "__USER__" ] + sources = [ + "//drivers/framework/core/manager/test/unittest/common/hdf_sbuf_test.cpp", + ] + deps = [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/test/unittest/common:libhdf_test_common", + "//utils/native/base:utils", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/uhdf2/test/unittest/osal/BUILD.gn b/uhdf2/test/unittest/osal/BUILD.gn new file mode 100755 index 0000000..549a3fd --- /dev/null +++ b/uhdf2/test/unittest/osal/BUILD.gn @@ -0,0 +1,56 @@ +# Copyright (c) 2021 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/test.gni") + +module_output_path = "hdf/osal" +ohos_unittest("hdf_adapter_uhdf_test_osal") { + module_out_path = module_output_path + + include_dirs = [ + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/include/platform", + "//drivers/framework/include/core", + "//drivers/framework/include/osal", + "//drivers/adapter/uhdf/posix/include", + "//drivers/framework/include/utils", + "//drivers/framework/include/config", + "//drivers/framework/include", + "//drivers/framework/test/unittest/include", + "//drivers/framework/test/unittest/osal", + ] + + defines = [ "__USER__" ] + sources = [ + "//drivers/framework/support/posix/test/unittest/common/hdf_osal_test.cpp", + ] + deps = [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/test/unittest/common:libhdf_test_common", + "//utils/native/base:utils", + ] + + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/uhdf2/test/unittest/platform/BUILD.gn b/uhdf2/test/unittest/platform/BUILD.gn new file mode 100755 index 0000000..e79a279 --- /dev/null +++ b/uhdf2/test/unittest/platform/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (c) 2021 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/test.gni") + +module_output_path = "hdf/platform" +ohos_unittest("hdf_adapter_uhdf_test_platform") { + module_out_path = module_output_path + include_dirs = [ + "//drivers/framework/ability/sbuf/include", + "//drivers/framework/include/platform", + "//drivers/framework/include/core", + "//drivers/framework/include/osal", + "//drivers/adapter/uhdf/posix/include", + "//drivers/framework/include/utils", + "//drivers/framework/include/config", + "//drivers/framework/include", + "//drivers/framework/test/unittest/include", + "//drivers/framework/test/unittest/platform/common", + ] + + sources = [ + "//drivers/framework/support/platform/test/unittest/common/hdf_emmc_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_gpio_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_i2c_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_pwm_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_rtc_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_sdio_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_spi_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_uart_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_watchdog_test.cpp", + "//drivers/framework/test/unittest/platform/common/i2c_test.c", + ] + + deps = [ + "//drivers/adapter/uhdf2/osal:libhdf_utils", + "//drivers/adapter/uhdf2/test/unittest/common:libhdf_test_common", + "//utils/native/base:utils", + ] + cflags = [ + "-Wall", + "-Wextra", + "-Werror", + "-fsigned-char", + "-fno-common", + "-fno-strict-aliasing", + ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } +} diff --git a/uhdf2/uhdf.gni b/uhdf2/uhdf.gni new file mode 100755 index 0000000..ef811b7 --- /dev/null +++ b/uhdf2/uhdf.gni @@ -0,0 +1,15 @@ +# Copyright (c) 2021 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.. + +hdf_framework_path = "//drivers/framework" +hdf_uhdf_path = "//drivers/adapter/uhdf2" -- Gitee