diff --git a/BUILD.gn b/BUILD.gn old mode 100755 new mode 100644 index d937682279dc3f5b7d341ec0c9617376b14721ea..896f53ee1a4601b3ddd7b9acd4caf23340f4954f --- a/BUILD.gn +++ b/BUILD.gn @@ -30,17 +30,18 @@ if (defined(ohos_lite)) { group("uhdf_entry") { deps = [ "//drivers/adapter/uhdf2/hcs:hdf_default.hcb", + "//drivers/adapter/uhdf2/hcs:hdf_devhost.cfg", "//drivers/adapter/uhdf2/hdi:libhdi", "//drivers/adapter/uhdf2/host:hdf_devhost", - "//drivers/adapter/uhdf2/host:hdf_devhost.rc", "//drivers/adapter/uhdf2/host:libhdf_host", "//drivers/adapter/uhdf2/host/test/unittest/sample_driver:libsample_driver", "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", "//drivers/adapter/uhdf2/manager:hdf_devmgr", - "//drivers/adapter/uhdf2/manager:hdf_devmgr.rc", + "//drivers/adapter/uhdf2/manager:hdf_devmgr.cfg", "//drivers/adapter/uhdf2/platform:libhdf_platform", "//drivers/adapter/uhdf2/utils:libhdf_utils", - "//drivers/peripheral/base:hdf_peripheral.rc", + "//drivers/framework/tools/hdf_dbg:hdf_dbg", + "//drivers/peripheral/base:hdf_peripheral.cfg", ] } group("uhdf_test_entry") { diff --git a/OAT.xml b/OAT.xml index 5cb0caf2b1abbc474945fb464e71e40ff2a20ace..4a9810f6848ddac8d935a0d7f818397cf7b4ee02 100644 --- a/OAT.xml +++ b/OAT.xml @@ -59,6 +59,9 @@ Note:If the text contains special characters, please escape them according to th + + + diff --git a/README.md b/README.md index c4b773b7960fd68700e72ed007ea12bc8c0e2f8d..b3cce1881f2c8060832194c7f46dae6f1b12bca2 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ This repository stores the code and compilation scripts for the kerner or user-m [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) -drivers\_adapter +[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README.md) diff --git a/README_zh.md b/README_zh.md index d70dade5d797d44b8acaa956bc2d77b427201d50..ebb4d89c413aeaada2038bf5dfadf1154e412cb7 100644 --- a/README_zh.md +++ b/README_zh.md @@ -24,7 +24,7 @@ [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README_zh.md) -drivers\_adapter +[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README_zh.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README_zh.md) diff --git a/bundle.json b/bundle.json old mode 100755 new mode 100644 diff --git a/khdf/liteos/Kconfig b/khdf/liteos/Kconfig old mode 100755 new mode 100644 index 8b9bd8fe75dce2e124b227943af17d3783d9696f..4d179b07788312a6d8949f4f71887d8de24c9a83 --- a/khdf/liteos/Kconfig +++ b/khdf/liteos/Kconfig @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -185,6 +185,13 @@ config DRIVERS_HDF_PLATFORM_WATCHDOG depends on DRIVERS_HDF_PLATFORM help Answer Y to enable HDF platform watchdog driver. + +config DRIVERS_HDF_PLATFORM_TRACE + bool "Enable HDF platform trace driver" + default n + depends on DRIVERS_HDF_PLATFORM + help + Answer Y to enable HDF platform trace driver. config DRIVERS_HDF_PLATFORM_TIMER bool "Enable HDF platform timer driver" @@ -193,6 +200,13 @@ config DRIVERS_HDF_PLATFORM_TIMER help Answer Y to enable HDF platform timer driver. +config DRIVERS_HDF_PLATFORM_CAN + bool "Enable HDF platform can bus driver" + default n + depends on DRIVERS_HDF_PLATFORM + help + Answer Y to enable HDF platform can bus driver. + config DRIVERS_HIEDMAC bool "Enable HIEDMAC" default n diff --git a/khdf/liteos/README.md b/khdf/liteos/README.md index 5ec1664ef9a6673323a59bc3db76051fe083e5f0..c7b2f65f850d58dec884939432dd5b20d98301fa 100644 --- a/khdf/liteos/README.md +++ b/khdf/liteos/README.md @@ -31,8 +31,8 @@ This repository stores the code and compilation scripts for the OpenHarmony driv [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) -[drivers\_adapter\_uhdf](https://gitee.com/openharmony/drivers_adapter_uhdf/blob/master/README.md) +[drivers\_adapter\_uhdf](https://gitee.com/openharmony/drivers_adapter/blob/master/uhdf/README.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_uhdf/blob/master/README.md) -drivers\_adapter\_khdf\_liteos +[drivers\_adapter\_khdf\_liteos](https://gitee.com/openharmony/drivers_adapter/blob/master/khdf/liteos/README.md) diff --git a/khdf/liteos/README_zh.md b/khdf/liteos/README_zh.md index 3a6f45373f11801b0e1a42ca2f95451730cf6d00..357d7cf3c706b560113945f192c8fe0f5e644327 100644 --- a/khdf/liteos/README_zh.md +++ b/khdf/liteos/README_zh.md @@ -31,8 +31,8 @@ [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README_zh.md) -[drivers\_adapter\_uhdf](https://gitee.com/openharmony/drivers_adapter_uhdf/blob/master/README_zh.md) +[drivers\_adapter\_uhdf](https://gitee.com/openharmony/drivers_adapter/blob/master/uhdf/README_zh.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README_zh.md) -drivers\_adapter\_khdf\_liteos +[drivers\_adapter\_khdf\_liteos](https://gitee.com/openharmony/drivers_adapter/blob/master/khdf/liteos/README_zh.md) diff --git a/khdf/liteos/hdf.gni b/khdf/liteos/hdf.gni index 9acc618df15f116e827054098b55f7e74f133c62..c76eb7039763cd68f896ab8271dccc04520ae8a7 100644 --- a/khdf/liteos/hdf.gni +++ b/khdf/liteos/hdf.gni @@ -31,6 +31,7 @@ import("//kernel/liteos_a/liteos.gni") HDF_FRAMEWORKS_PATH = "//drivers/framework" HDF_PERIPHERAL_PATH = "//drivers/peripheral" +HDF_ADAPTERS_PATH = "//drivers/adapter" template("hdf_driver") { if (defined(invoker.module_switch)) { diff --git a/khdf/liteos/hdf_lite.mk b/khdf/liteos/hdf_lite.mk old mode 100755 new mode 100644 index 7b203de96e3d325d8ae697fb9213974f045d075a..58f1bc10c4784e88ab3d67d73196ae8d0f5d995f --- a/khdf/liteos/hdf_lite.mk +++ b/khdf/liteos/hdf_lite.mk @@ -172,6 +172,8 @@ endif COMPANY_OF_SOC := $(patsubst "%",%,$(LOSCFG_DEVICE_COMPANY)) ifeq ($(COMPANY_OF_SOC), hisilicon) -include $(LITEOSTOPDIR)/../../device/soc/$(COMPANY_OF_SOC)/common/platform/lite.mk +else ifeq ($(COMPANY_OF_SOC), st) +-include $(LITEOSTOPDIR)/../../device/soc/$(COMPANY_OF_SOC)/common/platform/lite.mk else -include $(LITEOSTOPDIR)/../../device/$(COMPANY_OF_SOC)/drivers/lite.mk endif diff --git a/khdf/liteos/manager/src/devmgr_pm_reg.c b/khdf/liteos/manager/src/devmgr_pm_reg.c index 5ffd3a834b8a3ffa2c584b331d8cadd5142a53ca..c7d66a068163b8c82079da8a83c4a4781af718e4 100755 --- a/khdf/liteos/manager/src/devmgr_pm_reg.c +++ b/khdf/liteos/manager/src/devmgr_pm_reg.c @@ -38,7 +38,7 @@ UINT32 DevmgrPmSuspend(UINT32 mode) { if (mode != LOS_SYS_LIGHT_SLEEP) { - HDF_LOGI("%s: hdf Suspend %d", __func__, mode); + HDF_LOGI("%s: hdf Suspend %u", __func__, mode); } HDF_LOGI("%s hdf suspend start", __func__); diff --git a/khdf/liteos/model/audio/BUILD.gn b/khdf/liteos/model/audio/BUILD.gn index 1aa7f9b0a904bcaa49902fe132e0e392d60015a5..b8e341b9801920cde04f110c65be8bfa03ff0da0 100644 --- a/khdf/liteos/model/audio/BUILD.gn +++ b/khdf/liteos/model/audio/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -31,12 +31,11 @@ import("//drivers/adapter/khdf/liteos/hdf.gni") module_switch = defined(LOSCFG_DRIVERS_HDF_AUDIO) module_name = "hdf_audio_driver" hdf_driver(module_name) { - PERIPHERAL_ADUIO_ROOT = [] FRAMEWORKS_ADUIO_ROOT = "$HDF_FRAMEWORKS_PATH/model/audio" - PERIPHERAL_ADUIO_ROOT = "$HDF_PERIPHERAL_PATH/audio" + PERIPHERAL_ADUIO_ROOT = + "//device/board/hisilicon/hispark_taurus/audio_drivers" sources = [ - "$FRAMEWORKS_ADUIO_ROOT/common/src/audio_accessory_base.c", "$FRAMEWORKS_ADUIO_ROOT/common/src/audio_codec_base.c", "$FRAMEWORKS_ADUIO_ROOT/common/src/audio_dai_base.c", "$FRAMEWORKS_ADUIO_ROOT/common/src/audio_dma_base.c", @@ -69,32 +68,31 @@ hdf_driver(module_name) { if (defined(LOSCFG_DRIVERS_HDF_AUDIO_CODEC_HI3516)) { sources += [ - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/codec/src/hi3516_codec_adapter.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/codec/src/hi3516_codec_impl.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/codec/src/hi3516_codec_ops.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/dsp/src/dsp_adapter.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/dsp/src/dsp_ops.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/soc/src/hi3516_aiao_impl.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/soc/src/hi3516_dai_adapter.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/soc/src/hi3516_dai_ops.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/soc/src/hi3516_dma_adapter.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/soc/src/hi3516_dma_ops.c", + "$PERIPHERAL_ADUIO_ROOT/codec/hi3516/src/hi3516_codec_adapter.c", + "$PERIPHERAL_ADUIO_ROOT/codec/hi3516/src/hi3516_codec_impl.c", + "$PERIPHERAL_ADUIO_ROOT/codec/hi3516/src/hi3516_codec_ops.c", + "$PERIPHERAL_ADUIO_ROOT/dsp/src/dsp_adapter.c", + "$PERIPHERAL_ADUIO_ROOT/dsp/src/dsp_ops.c", + "$PERIPHERAL_ADUIO_ROOT/soc/src/hi3516_aiao_impl.c", + "$PERIPHERAL_ADUIO_ROOT/soc/src/hi3516_dai_adapter.c", + "$PERIPHERAL_ADUIO_ROOT/soc/src/hi3516_dai_ops.c", + "$PERIPHERAL_ADUIO_ROOT/soc/src/hi3516_dma_adapter.c", + "$PERIPHERAL_ADUIO_ROOT/soc/src/hi3516_dma_ops.c", ] include_dirs += [ - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/codec/include", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/soc/include", - "$PERIPHERAL_ADUIO_ROOT/chipsets/hi3516dv300/dsp/include", + "$PERIPHERAL_ADUIO_ROOT/codec/hi3516/include", + "$PERIPHERAL_ADUIO_ROOT/soc/include", + "$PERIPHERAL_ADUIO_ROOT/dsp/include", ] } if (defined(LOSCFG_DRIVERS_HDF_AUDIO_CODEC_TF9879)) { sources += [ - "$PERIPHERAL_ADUIO_ROOT/chipsets/tfa9879/accessory/src/tfa9879_accessory_adapter.c", - "$PERIPHERAL_ADUIO_ROOT/chipsets/tfa9879/accessory/src/tfa9879_accessory_impl.c", + "$PERIPHERAL_ADUIO_ROOT/codec/tfa9879/src/tfa9879_codec_adapter.c", + "$PERIPHERAL_ADUIO_ROOT/codec/tfa9879/src/tfa9879_codec_ops.c", ] - include_dirs += - [ "$PERIPHERAL_ADUIO_ROOT/chipsets/tfa9879/accessory/include" ] + include_dirs += [ "$PERIPHERAL_ADUIO_ROOT/codec/tfa9879/include" ] } cflags = [ diff --git a/khdf/liteos/model/audio/Makefile b/khdf/liteos/model/audio/Makefile index 482e942ddc9e7d6164719955320909ec2703f46c..edea03cb83332c8d04d26a0e92fe13e688e43f41 100644 --- a/khdf/liteos/model/audio/Makefile +++ b/khdf/liteos/model/audio/Makefile @@ -33,7 +33,7 @@ MODULE_NAME := hdf_audio_driver KHDF_AUDIO_BASE_ROOT_DIR = $(LITEOSTOPDIR)/../../ KHDF_AUDIO_ROOT_DIR = $(KHDF_AUDIO_BASE_ROOT_DIR)/drivers/framework/model/audio KHDF_FRAMEWORK_ROOT_DIR = $(KHDF_AUDIO_BASE_ROOT_DIR)/drivers/framework -KHDF_AUDIO_HI3516DV300_DIR = $(KHDF_AUDIO_BASE_ROOT_DIR)/drivers/peripheral/audio/chipsets/hi3516dv300 +KHDF_AUDIO_HI3516DV300_DIR = $(KHDF_AUDIO_BASE_ROOT_DIR)/device/board/hisilicon/hispark_taurus/audio_drivers LOCAL_INCLUDE := $(KHDF_AUDIO_BASE_ROOT_DIR)/third_party/bounds_checking_function/include \ $(KHDF_AUDIO_KHDF_ROOT_DIR)/osal/include \ @@ -47,8 +47,8 @@ LOCAL_INCLUDE := $(KHDF_AUDIO_BASE_ROOT_DIR)/third_party/bounds_checking_functio $(KHDF_FRAMEWORK_ROOT_DIR)/model/audio/sapm/include \ $(KHDF_FRAMEWORK_ROOT_DIR)/model/audio/dispatch/include \ $(KHDF_FRAMEWORK_ROOT_DIR)/model/audio/common/include \ - $(KHDF_AUDIO_HI3516DV300_DIR)/../tfa9879/accessory/include \ - $(KHDF_AUDIO_HI3516DV300_DIR)/codec/include \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/tfa9879/include \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/hi3516/include \ $(KHDF_AUDIO_HI3516DV300_DIR)/soc/include \ $(KHDF_AUDIO_HI3516DV300_DIR)/dsp/include \ $(LITEOSTOPDIR)/kernel/base/include @@ -56,7 +56,6 @@ LOCAL_INCLUDE := $(KHDF_AUDIO_BASE_ROOT_DIR)/third_party/bounds_checking_functio LOCAL_SRCS += $(KHDF_AUDIO_ROOT_DIR)/core/src/audio_core.c \ $(KHDF_AUDIO_ROOT_DIR)/core/src/audio_host.c \ $(KHDF_AUDIO_ROOT_DIR)/core/src/audio_parse.c \ - $(KHDF_AUDIO_ROOT_DIR)/common/src/audio_accessory_base.c \ $(KHDF_AUDIO_ROOT_DIR)/common/src/audio_codec_base.c \ $(KHDF_AUDIO_ROOT_DIR)/common/src/audio_platform_base.c \ $(KHDF_AUDIO_ROOT_DIR)/common/src/audio_dsp_base.c \ @@ -65,11 +64,11 @@ LOCAL_SRCS += $(KHDF_AUDIO_ROOT_DIR)/core/src/audio_core.c \ $(KHDF_AUDIO_ROOT_DIR)/sapm/src/audio_sapm.c \ $(KHDF_AUDIO_ROOT_DIR)/dispatch/src/audio_stream_dispatch.c \ $(KHDF_AUDIO_ROOT_DIR)/dispatch/src/audio_control_dispatch.c \ - $(KHDF_AUDIO_HI3516DV300_DIR)/../tfa9879/accessory/src/tfa9879_accessory_adapter.c \ - $(KHDF_AUDIO_HI3516DV300_DIR)/../tfa9879/accessory/src/tfa9879_accessory_impl.c \ - $(KHDF_AUDIO_HI3516DV300_DIR)/codec/src/hi3516_codec_adapter.c \ - $(KHDF_AUDIO_HI3516DV300_DIR)/codec/src/hi3516_codec_impl.c \ - $(KHDF_AUDIO_HI3516DV300_DIR)/codec/src/hi3516_codec_ops.c \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/tfa9879/src/tfa9879_codec_adapter.c \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/tfa9879/src/tfa9879_codec_ops.c \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/hi3516/src/hi3516_codec_adapter.c \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/hi3516/src/hi3516_codec_impl.c \ + $(KHDF_AUDIO_HI3516DV300_DIR)/codec/hi3516/src/hi3516_codec_ops.c \ $(KHDF_AUDIO_HI3516DV300_DIR)/dsp/src/dsp_adapter.c \ $(KHDF_AUDIO_HI3516DV300_DIR)/dsp/src/dsp_ops.c \ $(KHDF_AUDIO_HI3516DV300_DIR)/soc/src/hi3516_dai_adapter.c \ @@ -79,4 +78,4 @@ LOCAL_SRCS += $(KHDF_AUDIO_ROOT_DIR)/core/src/audio_core.c \ $(KHDF_AUDIO_HI3516DV300_DIR)/soc/src/hi3516_dma_adapter.c LOCAL_CFLAGS += -Wno-error -include $(HDF_DRIVER) \ No newline at end of file +include $(HDF_DRIVER) diff --git a/khdf/liteos/model/bus/usb/Kconfig b/khdf/liteos/model/bus/usb/Kconfig old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/bus/usb/Makefile b/khdf/liteos/model/bus/usb/Makefile old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/bus/usb/include/hdf_usb.h b/khdf/liteos/model/bus/usb/include/hdf_usb.h index 04973655a33b912f352fe04ffca22e3d902008cb..b8e60441b369107798d5dd5d6382bf118cb65a68 100644 --- a/khdf/liteos/model/bus/usb/include/hdf_usb.h +++ b/khdf/liteos/model/bus/usb/include/hdf_usb.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: diff --git a/khdf/liteos/model/display/Kconfig b/khdf/liteos/model/display/Kconfig old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/display/Makefile b/khdf/liteos/model/display/Makefile old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/input/Kconfig b/khdf/liteos/model/input/Kconfig old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/input/Makefile b/khdf/liteos/model/input/Makefile old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/misc/dsoftbus/BUILD.gn b/khdf/liteos/model/misc/dsoftbus/BUILD.gn index 314ac7ac4cd727da09673cb99adf24aacd8710be..6c5cb8712d179b4a8387062032c7ebaa8daf2626 100644 --- a/khdf/liteos/model/misc/dsoftbus/BUILD.gn +++ b/khdf/liteos/model/misc/dsoftbus/BUILD.gn @@ -48,7 +48,6 @@ hdf_driver(module_name) { include_dirs += [ "include", "//kernel/liteos_a/net/lwip-2.1/porting/include", - "//third_party/lwip/src/include/lwip", ] cflags += [ "-DENABLE_LWIP_MONITOR" ] } diff --git a/khdf/liteos/model/misc/dsoftbus/src/softbus_lwip_monitor.c b/khdf/liteos/model/misc/dsoftbus/src/softbus_lwip_monitor.c index 807d9d4f8f7db266f61810dbaf7ccb91927ecdd8..9330256d25eb1e87cb69a10abab63f7614aa0055 100644 --- a/khdf/liteos/model/misc/dsoftbus/src/softbus_lwip_monitor.c +++ b/khdf/liteos/model/misc/dsoftbus/src/softbus_lwip_monitor.c @@ -56,7 +56,7 @@ typedef struct { static void ReportEvent(const LwipMonitorReportInfo *reportInfo) { - struct HdfSBuf *data = data = HdfSbufObtainDefaultSize(); + struct HdfSBuf *data = HdfSbufObtainDefaultSize(); if (data == NULL) { dprintf("get sbuf fail\n"); @@ -77,9 +77,8 @@ static void NetifStatusCallback(struct netif *netif, netif_nsc_reason_t reason, LwipMonitorReportInfo reportInfo; bool needReport = true; - (void)args; - if (netif == NULL) { - dprintf("NetifStatusCallback: input netif is NULL\n"); + if (netif == NULL || args == NULL) { + dprintf("NetifStatusCallback: input args is NULL\n"); return; } dprintf("NetifStatusCallback(%s): nsc event: 0x%x\n", netif->full_name, (uint32_t)reason); diff --git a/khdf/liteos/model/misc/vibrator/BUILD.gn b/khdf/liteos/model/misc/vibrator/BUILD.gn index 3a5b13ba1bf9f90738efb6bd7980476ee94a2b61..6b0a52c8f3405c7abbeee6c4f482092d38810ed9 100644 --- a/khdf/liteos/model/misc/vibrator/BUILD.gn +++ b/khdf/liteos/model/misc/vibrator/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -32,6 +32,7 @@ module_switch = defined(LOSCFG_DRIVERS_HDF_VIBRATOR) module_name = "hdf_vibrator_driver" hdf_driver(module_name) { FRAMEWORKS_VIBRATOR_ROOT = "$HDF_FRAMEWORKS_PATH/model/misc/vibrator/driver" + PERIPHERAL_VIBRATOR_ROOT = "$HDF_PERIPHERAL_PATH/misc/vibrator" sources = [ "$FRAMEWORKS_VIBRATOR_ROOT/src/vibrator_driver.c", @@ -39,11 +40,11 @@ hdf_driver(module_name) { ] if (defined(LOSCFG_DRIVERS_HDF_VIBRATOR_LINEAR)) { - sources += [ "$FRAMEWORKS_VIBRATOR_ROOT/chipset/vibrator_linear_driver.c" ] + sources += [ "$PERIPHERAL_VIBRATOR_ROOT/chipset/vibrator_linear_driver.c" ] } include_dirs = [ "$FRAMEWORKS_VIBRATOR_ROOT/include", - "$FRAMEWORKS_VIBRATOR_ROOT/chipset", + "$PERIPHERAL_VIBRATOR_ROOT/chipset", ] } diff --git a/khdf/liteos/model/misc/vibrator/Makefile b/khdf/liteos/model/misc/vibrator/Makefile index ebc67f6f81f96ee3c954df58398aaefde353dce0..66cb379cc066ab78806539bbd2c7b09f57508fbf 100644 --- a/khdf/liteos/model/misc/vibrator/Makefile +++ b/khdf/liteos/model/misc/vibrator/Makefile @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -31,15 +31,16 @@ include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk MODULE_NAME := hdf_vibrator_driver FRAMEWORKS_VIBRATOR_ROOT = $(LITEOSTOPDIR)/../../drivers/framework/model/misc/vibrator/driver +PERIPHERAL_VIBRATOR_ROOT = $(LITEOSTOPDIR)/../../drivers/peripheral/misc/vibrator LOCAL_INCLUDE := $(FRAMEWORKS_VIBRATOR_ROOT)/include \ - $(FRAMEWORKS_VIBRATOR_ROOT)/chipset + $(PERIPHERAL_VIBRATOR_ROOT)/chipset LOCAL_SRCS += $(FRAMEWORKS_VIBRATOR_ROOT)/src/vibrator_driver.c \ $(FRAMEWORKS_VIBRATOR_ROOT)/src/vibrator_haptic.c ifeq ($(LOSCFG_DRIVERS_HDF_VIBRATOR_LINEAR), y) -LOCAL_SRCS += $(FRAMEWORKS_VIBRATOR_ROOT)/chipset/vibrator_linear_driver.c +LOCAL_SRCS += $(PERIPHERAL_VIBRATOR_ROOT)/chipset/vibrator_linear_driver.c endif include $(HDF_DRIVER) diff --git a/khdf/liteos/model/network/ethernet/BUILD.gn b/khdf/liteos/model/network/ethernet/BUILD.gn old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/network/ethernet/Kconfig b/khdf/liteos/model/network/ethernet/Kconfig old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/network/ethernet/Makefile b/khdf/liteos/model/network/ethernet/Makefile old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/network/wifi/BUILD.gn b/khdf/liteos/model/network/wifi/BUILD.gn index e2ba840da0fda410091cd8d91a808e322855e50e..d1043f88161b1375561278d4b5adabb2da507493 100644 --- a/khdf/liteos/model/network/wifi/BUILD.gn +++ b/khdf/liteos/model/network/wifi/BUILD.gn @@ -69,6 +69,7 @@ hdf_driver(module_name) { "$FRAMEWORKS_WIFI_ROOT/core/components/eapol", "$FRAMEWORKS_WIFI_ROOT/core/components/p2p", "$FRAMEWORKS_WIFI_ROOT/../common/netdevice", + "$FRAMEWORKS_WIFI_ROOT/core", "$FRAMEWORKS_WIFI_ROOT/core/module", "$HDF_FRAMEWORKS_PATH/include/wifi", "$FRAMEWORKS_WIFI_ROOT/bus", diff --git a/khdf/liteos/model/network/wifi/Makefile b/khdf/liteos/model/network/wifi/Makefile old mode 100755 new mode 100644 index 56903df2d9ac661e8e426f3aa445bb1f510b12ee..02f8cdab54f74f0910e8769e809d28288e912314 --- a/khdf/liteos/model/network/wifi/Makefile +++ b/khdf/liteos/model/network/wifi/Makefile @@ -58,6 +58,7 @@ WIFI_CFLAGS += -I $(INC_PATH) \ -I $(NETDEV_PATH) \ -I $(MODULE_PATH) \ -I $(INC_WIFI_PATH) \ + -I $(HDM_WIFI_ROOT)/core \ -I $(IBUS_PATH) diff --git a/khdf/liteos/model/network/wifi/platform/src/hdf_wlan_queue.c b/khdf/liteos/model/network/wifi/platform/src/hdf_wlan_queue.c index 153f9465158e2a1f9822baa0a2a9877e2d00a111..f3834015c3069037330c6dd7f98b0327f35e84b2 100644 --- a/khdf/liteos/model/network/wifi/platform/src/hdf_wlan_queue.c +++ b/khdf/liteos/model/network/wifi/platform/src/hdf_wlan_queue.c @@ -61,7 +61,7 @@ void DestroyQueue(HdfWlanQueue *queue) HdfWlanQueueImpl *queueImpl = (HdfWlanQueueImpl *)queue; uint32_t status = LOS_QueueDelete(queueImpl->queueID); if (status != LOS_OK) { - HDF_LOGE("Destroy message queue failed!err=%d", status); + HDF_LOGE("Destroy message queue failed!err=%u", status); } OsalMemFree(queue); } @@ -93,7 +93,7 @@ int32_t PushQueue(HdfWlanQueue *queue, void *context) uint32_t ret = LOS_QueueWriteCopy(queueImpl->queueID, &context, sizeof(void *), LOS_WAIT_FOREVER); if (ret != LOS_OK) { - HDF_LOGE("%s:Write queue failed!ret=%d", __func__, ret); + HDF_LOGE("%s:Write queue failed!ret=%u", __func__, ret); return HDF_FAILURE; } return HDF_SUCCESS; diff --git a/khdf/liteos/model/sensor/BUILD.gn b/khdf/liteos/model/sensor/BUILD.gn index f7e8a7babdd50d58e27855b7da58538aae015cd1..233039f5b7860174080a3293a23dff995cb97aee 100644 --- a/khdf/liteos/model/sensor/BUILD.gn +++ b/khdf/liteos/model/sensor/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -32,6 +32,7 @@ module_switch = defined(LOSCFG_DRIVERS_HDF_SENSOR) module_name = "hdf_sensor_driver" hdf_driver(module_name) { FRAMEWORKS_SENSOR_ROOT = "$HDF_FRAMEWORKS_PATH/model/sensor/driver" + PERIPHERAL_SENSOR_ROOT = "$HDF_PERIPHERAL_PATH/sensor" sources = [ "$FRAMEWORKS_SENSOR_ROOT/common/src/sensor_config_controller.c", "$FRAMEWORKS_SENSOR_ROOT/common/src/sensor_config_parser.c", @@ -43,14 +44,14 @@ hdf_driver(module_name) { sources += [ "$FRAMEWORKS_SENSOR_ROOT/accel/sensor_accel_driver.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_ACCEL_BMI160)) { - sources += [ "$FRAMEWORKS_SENSOR_ROOT/chipset/accel/accel_bmi160.c" ] + sources += [ "$PERIPHERAL_SENSOR_ROOT/chipset/accel/accel_bmi160.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_ALS)) { sources += [ "$FRAMEWORKS_SENSOR_ROOT/als/sensor_als_driver.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_ALS_BH1745)) { - sources += [ "$FRAMEWORKS_SENSOR_ROOT/chipset/als/als_bh1745.c" ] + sources += [ "$PERIPHERAL_SENSOR_ROOT/chipset/als/als_bh1745.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_BAROMETER)) { @@ -58,20 +59,20 @@ hdf_driver(module_name) { } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_BAROMETER_BMP180)) { sources += - [ "$FRAMEWORKS_SENSOR_ROOT/chipset/barometer/barometer_bmp180.c" ] + [ "$PERIPHERAL_SENSOR_ROOT/chipset/barometer/barometer_bmp180.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_PROXIMITY)) { sources += [ "$FRAMEWORKS_SENSOR_ROOT/proximity/sensor_proximity_driver.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_PROXIMITY_APDS9960)) { sources += - [ "$FRAMEWORKS_SENSOR_ROOT/chipset/proximity/proximity_apds9960.c" ] + [ "$PERIPHERAL_SENSOR_ROOT/chipset/proximity/proximity_apds9960.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_GYRO)) { sources += [ - "$FRAMEWORKS_SENSOR_ROOT/chipset/gyro/gyro_bmi160.c", "$FRAMEWORKS_SENSOR_ROOT/gyro/sensor_gyro_driver.c", + "$PERIPHERAL_SENSOR_ROOT/chipset/gyro/gyro_bmi160.c", ] } @@ -79,14 +80,14 @@ hdf_driver(module_name) { sources += [ "$FRAMEWORKS_SENSOR_ROOT/hall/sensor_hall_driver.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_HALL_AK8789)) { - sources += [ "$FRAMEWORKS_SENSOR_ROOT/chipset/hall/hall_ak8789.c" ] + sources += [ "$PERIPHERAL_SENSOR_ROOT/chipset/hall/hall_ak8789.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_MAGNETIC)) { sources += [ "$FRAMEWORKS_SENSOR_ROOT/magnetic/sensor_magnetic_driver.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_MAGNETIC_LSM303)) { - sources += [ "$FRAMEWORKS_SENSOR_ROOT/chipset/magnetic/magnetic_lsm303.c" ] + sources += [ "$PERIPHERAL_SENSOR_ROOT/chipset/magnetic/magnetic_lsm303.c" ] } if (defined(LOSCFG_DRIVERS_HDF_SENSOR_GRAVITY)) { @@ -97,18 +98,18 @@ hdf_driver(module_name) { "$FRAMEWORKS_SENSOR_ROOT/include", "$FRAMEWORKS_SENSOR_ROOT/common/include", "$FRAMEWORKS_SENSOR_ROOT/accel", - "$FRAMEWORKS_SENSOR_ROOT/chipset/accel", + "$PERIPHERAL_SENSOR_ROOT/chipset/accel", "$FRAMEWORKS_SENSOR_ROOT/als", - "$FRAMEWORKS_SENSOR_ROOT/chipset/als", + "$PERIPHERAL_SENSOR_ROOT/chipset/als", "$FRAMEWORKS_SENSOR_ROOT/barometer", - "$FRAMEWORKS_SENSOR_ROOT/chipset/barometer", + "$PERIPHERAL_SENSOR_ROOT/chipset/barometer", "$FRAMEWORKS_SENSOR_ROOT/gyro", - "$FRAMEWORKS_SENSOR_ROOT/chipset/gyro", + "$PERIPHERAL_SENSOR_ROOT/chipset/gyro", "$FRAMEWORKS_SENSOR_ROOT/hall", - "$FRAMEWORKS_SENSOR_ROOT/chipset/hall", + "$PERIPHERAL_SENSOR_ROOT/chipset/hall", "$FRAMEWORKS_SENSOR_ROOT/magnetic", - "$FRAMEWORKS_SENSOR_ROOT/chipset/magnetic", + "$PERIPHERAL_SENSOR_ROOT/chipset/magnetic", "$FRAMEWORKS_SENSOR_ROOT/proximity", - "$FRAMEWORKS_SENSOR_ROOT/chipset/proximity", + "$PERIPHERAL_SENSOR_ROOT/chipset/proximity", ] } diff --git a/khdf/liteos/model/sensor/Kconfig b/khdf/liteos/model/sensor/Kconfig old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/sensor/Makefile b/khdf/liteos/model/sensor/Makefile index 220b48514b2b9c44b53ca15f2cbdff485d9a1189..f604529a9d38249c2f55d18239006ee143781cd9 100644 --- a/khdf/liteos/model/sensor/Makefile +++ b/khdf/liteos/model/sensor/Makefile @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -31,21 +31,22 @@ include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk MODULE_NAME := hdf_sensor_driver FRAMEWORKS_SENSOR_ROOT = $(LITEOSTOPDIR)/../../drivers/framework/model/sensor/driver +PERIPHERAL_SENSOR_ROOT = $(LITEOSTOPDIR)/../../drivers/peripheral/sensor LOCAL_INCLUDE := $(FRAMEWORKS_SENSOR_ROOT)/include \ $(FRAMEWORKS_SENSOR_ROOT)/common/include \ $(FRAMEWORKS_SENSOR_ROOT)/accel \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/accel \ + $(PERIPHERAL_SENSOR_ROOT)/chipset/accel \ $(FRAMEWORKS_SENSOR_ROOT)/als \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/als \ + $(PERIPHERAL_SENSOR_ROOT)/chipset/als \ $(FRAMEWORKS_SENSOR_ROOT)/barometer \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/barometer \ + $(PERIPHERAL_SENSOR_ROOT)/chipset/barometer \ $(FRAMEWORKS_SENSOR_ROOT)/hall \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/hall \ + $(PERIPHERAL_SENSOR_ROOT)/chipset/hall \ $(FRAMEWORKS_SENSOR_ROOT)/magnetic \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/magnetic \ + $(PERIPHERAL_SENSOR_ROOT)/chipset/magnetic \ $(FRAMEWORKS_SENSOR_ROOT)/proximity \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/proximity + $(PERIPHERAL_SENSOR_ROOT)/chipset/proximity LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/common/src/sensor_config_controller.c \ $(FRAMEWORKS_SENSOR_ROOT)/common/src/sensor_config_parser.c \ @@ -54,32 +55,32 @@ LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/common/src/sensor_config_controller.c \ ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_ACCEL), y) LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/accel/sensor_accel_driver.c \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/accel/accel_bmi160.c + $(PERIPHERAL_SENSOR_ROOT)/chipset/accel/accel_bmi160.c endif ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_ALS), y) LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/als/sensor_als_driver.c \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/als/als_bh1745.c + $(PERIPHERAL_SENSOR_ROOT)/chipset/als/als_bh1745.c endif ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_BAROMETER), y) LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/barometer/sensor_barometer_driver.c \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/barometer/barometer_bmp180.c + $(PERIPHERAL_SENSOR_ROOT)/chipset/barometer/barometer_bmp180.c endif ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_HALL), y) LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/hall/sensor_hall_driver.c \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/hall/hall_ak8789.c + $(PERIPHERAL_SENSOR_ROOT)/chipset/hall/hall_ak8789.c endif ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_MAGNETIC), y) LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/magnetic/sensor_magnetic_driver.c \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/magnetic/magnetic_lsm303.c + $(PERIPHERAL_SENSOR_ROOT)/chipset/magnetic/magnetic_lsm303.c endif ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_PROXIMITY), y) LOCAL_SRCS += $(FRAMEWORKS_SENSOR_ROOT)/proximity/sensor_proximity_driver.c \ - $(FRAMEWORKS_SENSOR_ROOT)/chipset/proximity/proximity_apds9960.c + $(PERIPHERAL_SENSOR_ROOT)/chipset/proximity/proximity_apds9960.c endif ifeq ($(LOSCFG_DRIVERS_HDF_SENSOR_GRAVITY), y) diff --git a/khdf/liteos/model/storage/src/mmc/mmc_block_lite.c b/khdf/liteos/model/storage/src/mmc/mmc_block_lite.c index f986201bd35b696ac715448df3c70cbd99862055..95ce058aab95edbafd5451136ed6a5e5955222c1 100644 --- a/khdf/liteos/model/storage/src/mmc/mmc_block_lite.c +++ b/khdf/liteos/model/storage/src/mmc/mmc_block_lite.c @@ -91,8 +91,15 @@ static int LiteosBlockClose(FAR struct Vnode *vnode) static ssize_t LiteosBlockRead(FAR struct Vnode *vnode, FAR unsigned char *buf, unsigned long long secStart, unsigned int nSecs) { - size_t max = (size_t)(-1); - struct MmcBlock *mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; + size_t max; + struct MmcBlock *mb = NULL; + + if (vnode == NULL || vnode->data == NULL) { + HDF_LOGE("%s: vnode is NULL or data of vnode is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + max = (size_t)(-1); + mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; if (secStart >= max || nSecs >= max) { return HDF_ERR_INVALID_PARAM; @@ -108,8 +115,15 @@ static ssize_t LiteosBlockRead(FAR struct Vnode *vnode, FAR unsigned char *buf, static ssize_t LiteosBlockWrite(FAR struct Vnode *vnode, FAR const unsigned char *buf, unsigned long long secStart, unsigned int nSecs) { - size_t max = (size_t)(-1); - struct MmcBlock *mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; + size_t max; + struct MmcBlock *mb = NULL; + + if (vnode == NULL || vnode->data == NULL) { + HDF_LOGE("%s: vnode is NULL or data of vnode is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + max = (size_t)(-1); + mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; if (secStart >= max || nSecs >= max) { return HDF_ERR_INVALID_PARAM; @@ -124,8 +138,13 @@ static ssize_t LiteosBlockWrite(FAR struct Vnode *vnode, FAR const unsigned char static int LiteosBlockGeometry(FAR struct Vnode *vnode, FAR struct geometry *geometry) { - struct MmcBlock *mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; + struct MmcBlock *mb = NULL; + if (vnode == NULL || vnode->data == NULL) { + HDF_LOGE("%s: vnode is NULL or data of vnode is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; if (mb == NULL) { return HDF_ERR_INVALID_OBJECT; } @@ -146,6 +165,11 @@ static int32_t LiteosBlockSaveGeometry(FAR struct Vnode *vnode, unsigned long ar struct geometry gm; struct RtDeviceBlkGeometry rtGeo = {0}; + if (vnode == NULL || vnode->data == NULL) { + HDF_LOGE("%s: vnode is NULL or data of vnode is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + ret = LiteosBlockGeometry(vnode, &gm); if (ret != HDF_SUCCESS) { return ret; @@ -190,6 +214,11 @@ static int32_t LiteosBlockIoctl(FAR struct Vnode *vnode, int cmd, unsigned long uint32_t auSize; struct MmcBlock *mb = NULL; + if (vnode == NULL || vnode->data == NULL) { + HDF_LOGE("%s: vnode is NULL or data of vnode is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + mb = (struct MmcBlock *)((struct drv_data*)vnode->data)->priv; switch (cmd) { @@ -263,7 +292,7 @@ int32_t MmcBlockOsInit(struct MmcDevice *mmcDevice) } diskId = los_alloc_diskid_byname(mb->name); - if (!mb->removeable) { + if (!mb->removable) { info = &g_emmcInfo; info->sector_count = mb->capacity; } diff --git a/khdf/liteos/model/storage/src/mtd/mtd_char_lite.c b/khdf/liteos/model/storage/src/mtd/mtd_char_lite.c index e8fadcc937a1c64fc520e31b9093b1dac7c8cc3f..df9ed3e4d9618432af4f742ffb06cb1fd580de1c 100644 --- a/khdf/liteos/model/storage/src/mtd/mtd_char_lite.c +++ b/khdf/liteos/model/storage/src/mtd/mtd_char_lite.c @@ -54,8 +54,18 @@ struct MtdFileInfo { */ static int MtdCharOpen(FAR struct file *filep) { - struct drv_data *drv = (struct drv_data *)filep->f_vnode->data; + struct drv_data *drv = NULL; + + if (filep == NULL || filep->f_vnode == NULL || filep->f_vnode->data == NULL) { + HDF_LOGE("%s: filep is NULL or f_vnode of filep is NULL or data of f_vnode is NULL", __func__); + return -EINVAL; + } + drv = (struct drv_data *)filep->f_vnode->data; mtd_partition *partition = (mtd_partition *)drv->priv; + if (partition == NULL) { + HDF_LOGE("%s: partition is NULL", __func__); + return -EINVAL; + } struct MtdFileInfo *mfi = NULL; if (partition->user_num != 0) { // be opened @@ -117,6 +127,11 @@ static ssize_t MtdCharRead(FAR struct file *filep, FAR char *buffer, size_t bufl size_t partSize = (partition->end_block + 1 - partition->start_block) * blockSize; size_t partOobSize = (partSize >> mtdDevice->writeSizeShift) * mtdDevice->oobSize; + if (buffer == NULL) { + HDF_LOGE("%s: buffer is NULL", __func__); + return -EINVAL; + } + (void)LOS_MuxLock(&partition->lock, LOS_WAIT_FOREVER); if (ppos < 0 || ppos > partSize) { PRINTK("%s: current file offset:0x%x invalid\n", __func__, ppos); @@ -172,6 +187,10 @@ static ssize_t MtdCharWrite(FAR struct file *filep, FAR const char *buffer, size size_t partSize = (partition->end_block + 1 - partition->start_block) * blockSize; size_t partOobSize = (partSize >> mtdDevice->writeSizeShift) * mtdDevice->oobSize; + if (buffer == NULL) { + HDF_LOGE("%s: buffer is NULL", __func__); + return -EINVAL; + } (void)LOS_MuxLock(&partition->lock, LOS_WAIT_FOREVER); if (ppos < 0 || ppos > partSize) { @@ -282,8 +301,7 @@ static int MtdCharIoctlGetInfo(const mtd_partition *part, const struct MtdDevice size_t endAddr; (void)cmd; - - MtdCharGetMtdInfo(mtdDevice, &mtdInfo); + MtdCharGetMtdInfo(mtdDevice, &mtdInfo); startAddr = part->start_block * mtdDevice->eraseSize; endAddr = (part->end_block + 1) * mtdDevice->eraseSize; ret = LOS_CopyFromKernel((void *)(uintptr_t)arg, sizeof(mtdInfo), (void *)&mtdInfo, sizeof(mtdInfo)); @@ -356,21 +374,30 @@ static int MtdCharIoctl(FAR struct file *filep, int cmd, unsigned long arg) struct MtdDev *mtdDev = (struct MtdDev *)(partition->mtd_info); struct MtdDevice *mtdDevice = (struct MtdDevice *)mtdDev->priv; + if (mtdDevice == NULL || arg == 0) { + HDF_LOGE("%s: mtdDevice is NULL or arg is 0", __func__); + return -EINVAL; + } + (void)LOS_MuxLock(&partition->lock, LOS_WAIT_FOREVER); switch (cmd) { case MTD_IOC_GETINFO: { ret = MtdCharIoctlGetInfo(partition, mtdDevice, cmd, arg); + break; } case MTD_IOC_ERASE: case MTD_IOC_ERASE64: { ret = MtdCharIoctlErase(partition, mtdDevice, cmd, arg); + break; } case MTD_IOC_GETBADBLOCK: { ret = MtdCharIoctlGetBadBlock(partition, mtdDevice, cmd, arg); + break; } case MTD_IOC_SETBADBLOCK: { ret = MtdCharIoctlSetBadBlock(partition, mtdDevice, cmd, arg); + break; } case MTD_IOC_SETFILEMODE: mfi->mode = 0; @@ -392,6 +419,9 @@ static int MtdCharIoctl(FAR struct file *filep, int cmd, unsigned long arg) static ssize_t MtdCharMap(FAR struct file* filep, FAR LosVmMapRegion *region) { + (void)filep; + (void)region; + PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__); return 0; } @@ -438,6 +468,10 @@ int HdfMtdDevErase(struct MtdDev *mtdDev, UINT64 start, UINT64 len, UINT64 *fail if (mtdDev == NULL) { return HDF_ERR_INVALID_OBJECT; } + if (failAddr == NULL) { + HDF_LOGE("%s: failAddr is NULL", __func__); + return -EINVAL; + } ret = MtdDeviceErase((struct MtdDevice *)mtdDev->priv, start, len, &failPos); if (ret != HDF_SUCCESS) { *failAddr = failPos; diff --git a/khdf/liteos/model/storage/src/mtd/mtd_legacy_lite.c b/khdf/liteos/model/storage/src/mtd/mtd_legacy_lite.c index 2bd0b760105d1f38f0a991a90678ccc549889aec..616b7e35f0239528b044f4a8543d4476dae5c541 100644 --- a/khdf/liteos/model/storage/src/mtd/mtd_legacy_lite.c +++ b/khdf/liteos/model/storage/src/mtd/mtd_legacy_lite.c @@ -264,7 +264,8 @@ static int MtdDeviceLegacyNandRead(MtdInfoLegacy *mtd, off_t addr, size_t len, s { int ret; - if (mtd == NULL) { + if (mtd == NULL || mtd->priv == NULL || retlen == NULL || buf == NULL) { + HDF_LOGE("%s: function parameter is null", __func__); return -EINVAL; } @@ -277,7 +278,8 @@ static int MtdDeviceLegacyNandWrite(MtdInfoLegacy *mtd, off_t addr, size_t len, { int ret; - if (mtd == NULL) { + if (mtd == NULL || retlen == NULL) { + HDF_LOGE("%s: function parameter is null", __func__); return -EINVAL; } @@ -290,7 +292,8 @@ static int MtdDeviceLegacyNandReadOob(MtdInfoLegacy *mtd, off_t addr, size_t len { int ret; - if (mtd == NULL) { + if (mtd == NULL || retlen == NULL) { + HDF_LOGE("%s: function parameter is null", __func__); return -EINVAL; } @@ -303,7 +306,8 @@ static int MtdDeviceLegacyNandWriteOob(MtdInfoLegacy *mtd, off_t addr, size_t le { int ret; - if (mtd == NULL) { + if (mtd == NULL || retlen == NULL) { + HDF_LOGE("%s: function parameter is null", __func__); return -EINVAL; } diff --git a/khdf/liteos/model/usb/device/BUILD.gn b/khdf/liteos/model/usb/device/BUILD.gn old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/usb/device/Kconfig b/khdf/liteos/model/usb/device/Kconfig old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/usb/device/Makefile b/khdf/liteos/model/usb/device/Makefile old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/usb/host/include/usb_pnp_notify.h b/khdf/liteos/model/usb/host/include/usb_pnp_notify.h index 82661d788e4726074323cc9f79c078450b3df5e2..242123f41aa0a71bbcf115c2ba0af687572b5501 100755 --- a/khdf/liteos/model/usb/host/include/usb_pnp_notify.h +++ b/khdf/liteos/model/usb/host/include/usb_pnp_notify.h @@ -83,7 +83,7 @@ struct UsbInfoQueryPara { UsbInfoQueryParaType type; union { int32_t id; - uintptr_t usbDevAddr; + uint64_t usbDevAddr; struct { int32_t devNum; int32_t busNum; diff --git a/khdf/liteos/model/usb/host/src/usb_pnp_manager.c b/khdf/liteos/model/usb/host/src/usb_pnp_manager.c old mode 100755 new mode 100644 diff --git a/khdf/liteos/model/usb/host/src/usb_pnp_notify.c b/khdf/liteos/model/usb/host/src/usb_pnp_notify.c old mode 100755 new mode 100644 index f7982e9caec4ada5e4db00229db2046437689e8d..01dc4be5b7d5b26f7f0924bf07a0b17608414c44 --- a/khdf/liteos/model/usb/host/src/usb_pnp_notify.c +++ b/khdf/liteos/model/usb/host/src/usb_pnp_notify.c @@ -80,7 +80,7 @@ static bool UsbPnpNotifyFindDeviceList(struct usb_device *deviceObj, bool freeFl struct UsbPnpNotifyDeviceList, deviceList) { if (pnpNotifyDevicePos->device == deviceObj) { findFlag = true; - if (freeFlag == true) { + if (freeFlag) { DListRemove(&pnpNotifyDevicePos->deviceList); OsalMemFree(&pnpNotifyDevicePos); } @@ -96,29 +96,25 @@ static bool UsbPnpNotifyFindDeviceList(struct usb_device *deviceObj, bool freeFl static struct UsbPnpDeviceInfo *UsbPnpNotifyCreateInfo(void) { struct UsbPnpDeviceInfo *infoTemp = NULL; - unsigned char *ptr = NULL; - static int32_t idNum = 0; + static int32_t idNum = 1; - ptr = OsalMemCalloc(sizeof(struct UsbPnpDeviceInfo)); - if (ptr == NULL) { - HDF_LOGE("%s:%d OsalMemAlloc faile ", __func__, __LINE__); + infoTemp = (struct UsbPnpDeviceInfo *)OsalMemCalloc(sizeof(struct UsbPnpDeviceInfo)); + if (infoTemp == NULL) { + HDF_LOGE("%s:%d OsalMemAlloc failed ", __func__, __LINE__); return NULL; - } else { - infoTemp = (struct UsbPnpDeviceInfo *)ptr; - - if (idNum++ >= INT32_MAX) { - idNum = 0; - } - infoTemp->id = idNum; - OsalMutexInit(&infoTemp->lock); - infoTemp->status = USB_PNP_DEVICE_INIT_STATUS; - DListHeadInit(&infoTemp->list); - memset_s(infoTemp->interfaceRemoveStatus, USB_PNP_INFO_MAX_INTERFACES, - 0, sizeof(infoTemp->interfaceRemoveStatus)); - DListInsertTail(&infoTemp->list, &g_usbPnpInfoListHead); + } - return infoTemp; + if (idNum == INT32_MAX) { + idNum = 1; } + infoTemp->id = idNum; + OsalMutexInit(&infoTemp->lock); + infoTemp->status = USB_PNP_DEVICE_INIT_STATUS; + DListHeadInit(&infoTemp->list); + DListInsertTail(&infoTemp->list, &g_usbPnpInfoListHead); + idNum++; + + return infoTemp; } static struct UsbPnpDeviceInfo *UsbPnpNotifyFindInfo(struct UsbInfoQueryPara queryPara) @@ -128,7 +124,7 @@ static struct UsbPnpDeviceInfo *UsbPnpNotifyFindInfo(struct UsbInfoQueryPara que bool findFlag = false; if (DListIsEmpty(&g_usbPnpInfoListHead)) { - HDF_LOGE("%s:%d usb pnp list head is empty. ", __func__, __LINE__); + HDF_LOGE("%s:%d usb pnp list head is empty.", __func__, __LINE__); return NULL; } @@ -153,13 +149,13 @@ static struct UsbPnpDeviceInfo *UsbPnpNotifyFindInfo(struct UsbInfoQueryPara que break; } - if (findFlag == true) { + if (findFlag) { break; } } - if (findFlag == false) { - HDF_LOGE("%s:%d the usb pnp info to be find does not exist. ", __func__, __LINE__); + if (!findFlag) { + HDF_LOGE("%s:%d the usb pnp info to be find does not exist.", __func__, __LINE__); return NULL; } else { return infoPos; @@ -175,7 +171,7 @@ static HDF_STATUS UsbPnpNotifyDestroyInfo(struct UsbPnpDeviceInfo *deviceInfo) if (deviceInfo == NULL) { ret = HDF_FAILURE; - HDF_LOGE("%s:%d the deviceInfo is NULL, ret=%d ", __func__, __LINE__, ret); + HDF_LOGE("%s:%d the deviceInfo is NULL, ret = %d ", __func__, __LINE__, ret); return ret; } @@ -194,9 +190,9 @@ static HDF_STATUS UsbPnpNotifyDestroyInfo(struct UsbPnpDeviceInfo *deviceInfo) } } - if (findFlag == false) { + if (!findFlag) { ret = HDF_FAILURE; - HDF_LOGE("%s:%d the deviceInfoto be destroyed does not exist, ret=%d ", __func__, __LINE__, ret); + HDF_LOGE("%s:%d the deviceInfoto be destroyed does not exist, ret = %d ", __func__, __LINE__, ret); } return ret; @@ -206,7 +202,7 @@ static int32_t UsbPnpNotifyAddInitInfo(struct UsbPnpDeviceInfo *deviceInfo, unio { int32_t ret = HDF_SUCCESS; - deviceInfo->info.usbDevAddr = (uintptr_t)infoData.usbDev; + deviceInfo->info.usbDevAddr = (uint64_t)infoData.usbDev; deviceInfo->info.devNum = infoData.usbDev->address; deviceInfo->info.busNum = (int32_t)infoData.usbDev->port_no; @@ -226,8 +222,7 @@ static int32_t UsbPnpNotifyAddInitInfo(struct UsbPnpDeviceInfo *deviceInfo, unio deviceInfo->info.numInfos = infoData.usbDev->cdesc->bNumInterface; for (uint8_t i = 0; i < deviceInfo->info.numInfos; i++) { if (infoData.usbDev->ifaces[i].idesc == NULL) { - HDF_LOGE("%s:%d interface[%d].idesc is NULL", - __func__, __LINE__, i); + HDF_LOGE("%s:%d interface[%hhu].idesc is NULL", __func__, __LINE__, i); ret = HDF_ERR_INVALID_PARAM; goto OUT; } @@ -240,7 +235,7 @@ static int32_t UsbPnpNotifyAddInitInfo(struct UsbPnpDeviceInfo *deviceInfo, unio deviceInfo->info.interfaceInfo[i].interfaceNumber = infoData.usbDev->ifaces[i].idesc->bInterfaceNumber; - HDF_LOGI("%s:%d i=%d, interfaceInfo=0x%x-0x%x-0x%x-0x%x", + HDF_LOGI("%s:%d i = %hhu, interfaceInfo=0x%x-0x%x-0x%x-0x%x", __func__, __LINE__, i, deviceInfo->info.interfaceInfo[i].interfaceClass, deviceInfo->info.interfaceInfo[i].interfaceSubClass, deviceInfo->info.interfaceInfo[i].interfaceProtocol, @@ -279,7 +274,7 @@ static void UsbPnpNotifyAddInterfaceInitInfo(struct UsbPnpDeviceInfo *deviceInfo } else { for (i = 0, j = 0; i < deviceInfo->info.numInfos; i++) { if (deviceInfo->interfaceRemoveStatus[i] == true) { - HDF_LOGI("%s:%d j=%d deviceInfo->interfaceRemoveStatus[%d] is true!", __func__, __LINE__, j, i); + HDF_LOGI("%s:%d j = %hhu deviceInfo->interfaceRemoveStatus[%hhu] is true!", __func__, __LINE__, j, i); continue; } infoTable->interfaceInfo[j].interfaceClass = deviceInfo->info.interfaceInfo[i].interfaceClass; @@ -288,7 +283,7 @@ static void UsbPnpNotifyAddInterfaceInitInfo(struct UsbPnpDeviceInfo *deviceInfo infoTable->interfaceInfo[j].interfaceNumber = deviceInfo->info.interfaceInfo[i].interfaceNumber; j++; - HDF_LOGI("%s:%d i=%d, j=%d, interfaceInfo=0x%x-0x%x-0x%x-0x%x", + HDF_LOGI("%s:%d i = %d, j = %d, interfaceInfo=0x%x-0x%x-0x%x-0x%x", __func__, __LINE__, i, j - 1, infoTable->interfaceInfo[j - 1].interfaceClass, infoTable->interfaceInfo[j - 1].interfaceSubClass, infoTable->interfaceInfo[j - 1].interfaceProtocol, @@ -304,8 +299,8 @@ static int32_t UsbPnpNotifyInitInfo( int32_t ret = HDF_SUCCESS; const void *data = NULL; - if ((g_usbPnpNotifyCmdType == USB_PNP_NOTIFY_ADD_INTERFACE) - || (g_usbPnpNotifyCmdType == USB_PNP_NOTIFY_REMOVE_INTERFACE)) { + if ((g_usbPnpNotifyCmdType == USB_PNP_NOTIFY_ADD_INTERFACE) || + (g_usbPnpNotifyCmdType == USB_PNP_NOTIFY_REMOVE_INTERFACE)) { static struct UsbPnpNotifyMatchInfoTable infoTable; infoTable.usbDevAddr = deviceInfo->info.usbDevAddr; @@ -387,7 +382,7 @@ static int32_t UsbPnpNotifyGetDeviceInfo(void *eventData, union UsbPnpDeviceInfo } else if ((g_usbPnpNotifyCmdType == USB_PNP_NOTIFY_ADD_DEVICE) || (g_usbPnpNotifyCmdType == USB_PNP_NOTIFY_REMOVE_DEVICE)) { infoQueryPara.type = USB_INFO_DEVICE_ADDRESS_TYPE; - infoQueryPara.usbDevAddr = (uintptr_t)pnpInfoData->usbDev; + infoQueryPara.usbDevAddr = (uint64_t)pnpInfoData->usbDev; *deviceInfo = UsbPnpNotifyFindInfo(infoQueryPara); } else { *deviceInfo = UsbPnpNotifyCreateInfo(); @@ -470,7 +465,7 @@ static void TestReadPnpInfo(struct HdfSBuf *data) bool flag; flag = HdfSbufReadBuffer(data, (const void **)(&g_testUsbPnpInfo), &infoSize); - if ((flag == false) || (g_testUsbPnpInfo == NULL)) { + if ((!flag) || (g_testUsbPnpInfo == NULL)) { HDF_LOGE("%s: fail to read g_testUsbPnpInfo, flag=%d, g_testUsbPnpInfo=%px", \ __func__, flag, g_testUsbPnpInfo); return; @@ -536,8 +531,8 @@ static int32_t TestPnpNotifyHdfSendEvent(const struct HdfDeviceObject *deviceObj goto OUT; } - HDF_LOGI("%s: report one device information, %d usbDev=0x%x, devNum=%d, busNum=%d, infoTable=%d-0x%x-0x%x!", \ - __func__, g_usbPnpNotifyCmdType, (uint32_t)infoTable.usbDevAddr, infoTable.devNum, infoTable.busNum, \ + HDF_LOGI("%s: report one device information, %d usbDev=%llu, devNum=%d, busNum=%d, infoTable=%d-0x%x-0x%x!", \ + __func__, g_usbPnpNotifyCmdType, infoTable.usbDevAddr, infoTable.devNum, infoTable.busNum, \ infoTable.numInfos, infoTable.deviceInfo.vendorId, infoTable.deviceInfo.productId); ret = UsbPnpNotifySendEventLoader(data); @@ -559,6 +554,7 @@ static int32_t UsbPnpNotifyFirstReportDevice(struct HdfDeviceIoClient *client) { struct UsbPnpNotifyDeviceList *pnpNotifyDevicePos = NULL; struct UsbPnpNotifyDeviceList *pnpNotifyDeviceTemp = NULL; + int32_t ret; dprintf("%s:%d Enter!\n", __func__, __LINE__); @@ -570,7 +566,10 @@ static int32_t UsbPnpNotifyFirstReportDevice(struct HdfDeviceIoClient *client) OsalMutexLock(&g_usbPnpNotifyDevicelistLock); DLIST_FOR_EACH_ENTRY_SAFE(pnpNotifyDevicePos, pnpNotifyDeviceTemp, &g_usbPnpDeviceListHead, struct UsbPnpNotifyDeviceList, deviceList) { - UsbPnpNotifyHdfSendEvent(client->device, pnpNotifyDevicePos->device); + ret = UsbPnpNotifyHdfSendEvent(client->device, pnpNotifyDevicePos->device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d UsbPnpNotifyHdfSendEvent failed, ret=%{public}d", __func__, __LINE__, ret); + } } OsalMutexUnlock(&g_usbPnpNotifyDevicelistLock); @@ -677,12 +676,12 @@ static void UsbPnpNotifyAttachDevice(struct usb_device *udev) int32_t error; static int32_t listLockInit = 0; - if ((udev->ddesc.bDeviceClass == UICLASS_HUB) || UsbPnpNotifyFindDeviceList(udev, false) == true) { + if ((udev->ddesc.bDeviceClass == UICLASS_HUB) || UsbPnpNotifyFindDeviceList(udev, false)) { PRINTK("%s:%d findDeviceList is true!\n", __func__, __LINE__); return; } - if (0 == listLockInit) { + if (listLockInit == 0) { error = OsalMutexInit(&g_usbPnpNotifyDevicelistLock); if (error != HDF_SUCCESS) { HDF_LOGE("%s:%d error=%d!", __func__, __LINE__, error); @@ -708,9 +707,9 @@ static void UsbPnpNotifyDetachDevice(struct usb_device *udev) struct UsbInfoQueryPara infoQueryPara; struct UsbPnpDeviceInfo *deviceInfo = NULL; - if (UsbPnpNotifyFindDeviceList(udev, true) == true) { + if (UsbPnpNotifyFindDeviceList(udev, true)) { infoQueryPara.type = USB_INFO_DEVICE_ADDRESS_TYPE; - infoQueryPara.usbDevAddr = (uintptr_t)udev; + infoQueryPara.usbDevAddr = (uint64_t)udev; deviceInfo = UsbPnpNotifyFindInfo(infoQueryPara); if (deviceInfo == NULL) { PRINTK("%s:%d USB_DEVICE_REMOVE find info failed", __func__, __LINE__); @@ -770,7 +769,7 @@ struct usb_device *UsbPnpNotifyGetUsbDevice(struct UsbGetDevicePara paraData) break; } - if (findFlag == true) { + if (findFlag) { usbPnpDevice = pnpNotifyDevicePos->device; break; } @@ -789,7 +788,7 @@ static void UsbPnpNotifyReadPnpInfo(struct HdfSBuf *data) bool flag; flag = HdfSbufReadBuffer(data, (const void **)(&usbPnpInfo), &infoSize); - if ((flag == false) || (usbPnpInfo == NULL)) { + if ((!flag) || (usbPnpInfo == NULL)) { HDF_LOGE("%s:%d fail to read g_usbPnpInfo, flag=%d", __func__, __LINE__, flag); return; } @@ -849,7 +848,7 @@ static int32_t UsbPnpNotifyDriverRegisterDevice(struct HdfDeviceObject *device, ret = HdfDeviceObjectRegister(devObj); if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: failed to regitst device %s", __func__, moduleName); + HDF_LOGE("%s: failed to register device %s", __func__, moduleName); HdfDeviceObjectRelease(devObj); return ret; } @@ -857,7 +856,7 @@ static int32_t UsbPnpNotifyDriverRegisterDevice(struct HdfDeviceObject *device, ret = HdfDeviceObjectPublishService(devObj, serviceName, SERVICE_POLICY_CAPACITY, OSAL_S_IREAD | OSAL_S_IWRITE | OSAL_S_IRGRP | OSAL_S_IWGRP | OSAL_S_IROTH); if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: failed to regitst device %s", __func__, serviceName); + HDF_LOGE("%s: failed to register device %s", __func__, serviceName); HdfDeviceObjectRelease(devObj); } @@ -967,7 +966,7 @@ static int32_t UsbPnpNotifyBind(struct HdfDeviceObject *device) static int32_t UsbPnpNotifyInit(struct HdfDeviceObject *device) { static bool firstInitFlag = true; - HDF_STATUS ret; + int32_t ret; struct OsalThreadParam threadCfg; dprintf("%s:%d enter!\n", __func__, __LINE__); @@ -977,7 +976,7 @@ static int32_t UsbPnpNotifyInit(struct HdfDeviceObject *device) return HDF_ERR_INVALID_OBJECT; } - if (firstInitFlag == true) { + if (firstInitFlag) { firstInitFlag = false; DListHeadInit(&g_usbPnpInfoListHead); @@ -988,10 +987,14 @@ static int32_t UsbPnpNotifyInit(struct HdfDeviceObject *device) OsalMutexInit(&g_usbSendEventLock); - g_usbPnpThreadRunningFlag = true; - /* Creat thread to handle send usb interface information. */ - (void)memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg)); + ret = memset_s(&threadCfg, sizeof(threadCfg), 0, sizeof(threadCfg)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d memset_s failed", __func__, __LINE__); + return ret; + } + + g_usbPnpThreadRunningFlag = true; threadCfg.name = "LiteOS usb pnp notify handle kthread"; threadCfg.priority = OSAL_THREAD_PRI_HIGH; threadCfg.stackSize = USB_PNP_NOTIFY_REPORT_STACK_SIZE; diff --git a/khdf/liteos/model/usb/host/src/usb_test_pnp_notify.c b/khdf/liteos/model/usb/host/src/usb_test_pnp_notify.c old mode 100755 new mode 100644 diff --git a/khdf/liteos/ndk/BUILD.gn b/khdf/liteos/ndk/BUILD.gn old mode 100755 new mode 100644 diff --git a/khdf/liteos/network/src/netbuf_adapter.c b/khdf/liteos/network/src/netbuf_adapter.c index 6d9d2fe855a7570ef6e307217357182017443c95..176251b8004d49fcfc667e99a23bae10006583af 100644 --- a/khdf/liteos/network/src/netbuf_adapter.c +++ b/khdf/liteos/network/src/netbuf_adapter.c @@ -307,7 +307,7 @@ NetBuf *NetBufAlloc(uint32_t size) } (void)memset_s(nb, sizeof(NetBuf), 0, sizeof(NetBuf)); - (void)memset_s(data, sizeof(uint8_t), 0, sizeof(uint8_t)); + (void)memset_s(data, size, 0, size); nb->mem = data; nb->len = size; diff --git a/khdf/liteos/osal/include/osal_uaccess.h b/khdf/liteos/osal/include/osal_uaccess.h index f8a69a608d79fa8d28f1996f5862f9915776597a..dee351035c2506624d7dde627708adad1630b187 100644 --- a/khdf/liteos/osal/include/osal_uaccess.h +++ b/khdf/liteos/osal/include/osal_uaccess.h @@ -38,12 +38,12 @@ extern "C" { #endif /* __cplusplus */ -static inline size_t CopyToUser (void* to, const void* from, size_t len) +static inline size_t CopyToUser(void* to, const void* from, size_t len) { return LOS_ArchCopyToUser(to, from, len); } -static inline size_t CopyFromUser (void* to, const void* from, size_t len) +static inline size_t CopyFromUser(void* to, const void* from, size_t len) { return LOS_ArchCopyFromUser(to, from, len); } diff --git a/khdf/liteos/osal/src/osal_file.c b/khdf/liteos/osal/src/osal_file.c index 86bf1189c580569fff7cc741616d2077063e98cf..d5da1a14e461b45de1c61dd01d7ec7d28655a753 100644 --- a/khdf/liteos/osal/src/osal_file.c +++ b/khdf/liteos/osal/src/osal_file.c @@ -56,7 +56,7 @@ int32_t OsalFileOpen(OsalFile *file, const char *path, int flags, uint32_t right fd = open(pathName, flags, rights); if (fd < 0) { - HDF_LOGE("%s open file fail %d %d %d", __func__, flags, rights, errno); + HDF_LOGE("%s open file fail %d %u %d", __func__, flags, rights, errno); return HDF_FAILURE; } @@ -104,7 +104,7 @@ ssize_t OsalFileRead(OsalFile *file, void *buf, uint32_t length) ret = read((int32_t)(uintptr_t)file->realFile, buf, length); if (ret < 0) { - HDF_LOGE("%s read file fail %d %d", __func__, length, errno); + HDF_LOGE("%s read file fail %u %d", __func__, length, errno); return HDF_FAILURE; } diff --git a/khdf/liteos/osal/src/osal_firmware.c b/khdf/liteos/osal/src/osal_firmware.c index 9b578faa82f7f39499959726927151619a2e0ec5..3b90f7fb4c0ed6d46795092f136c97be20d3a35c 100644 --- a/khdf/liteos/osal/src/osal_firmware.c +++ b/khdf/liteos/osal/src/osal_firmware.c @@ -127,7 +127,7 @@ int32_t OsalReadFirmware(struct OsalFirmware *fwPara, struct OsalFwBlock *block) } fw = (struct OsalFwDesc *)fwPara->para; - if (fw->openFlag == false) { + if (!fw->openFlag) { HDF_LOGE("%s file do not open", __func__); return HDF_ERR_INVALID_PARAM; } @@ -153,7 +153,7 @@ int32_t OsalReadFirmware(struct OsalFirmware *fwPara, struct OsalFwBlock *block) readOffset = (uint32_t)lseek(fw->fd, fw->offset, SEEK_SET); if (readOffset != fw->offset) { - HDF_LOGE("%s seek offset fail %d %d", __func__, readOffset, fw->offset); + HDF_LOGE("%s seek offset fail %u %d", __func__, readOffset, fw->offset); return HDF_FAILURE; } readSize = (uint32_t)read(fw->fd, block->data, block->dataSize); diff --git a/khdf/liteos/osal/src/osal_irq.c b/khdf/liteos/osal/src/osal_irq.c index e2a19c641ba971a6c616651d1fae2314b7537a62..33f53ed184cbbdba30297daac0b95fe6ea35cbf7 100644 --- a/khdf/liteos/osal/src/osal_irq.c +++ b/khdf/liteos/osal/src/osal_irq.c @@ -49,7 +49,7 @@ int32_t OsalRegisterIrq(uint32_t irqId, uint32_t config, OsalIRQHandle handle, c irqParam.pName = name; ret = LOS_HwiCreate(irqId, 0, (HWI_MODE_T)config, (HWI_PROC_FUNC)handle, &irqParam); if (ret != LOS_OK) { - HDF_LOGE("%s %d register fail 0x%x", __func__, irqId, ret); + HDF_LOGE("%s %u register fail 0x%x", __func__, irqId, ret); return HDF_FAILURE; } HalIrqUnmask(irqId); @@ -63,7 +63,7 @@ int32_t OsalUnregisterIrq(uint32_t irqId, void *dev) HwiIrqParam irqParam; if (irqId >= OS_HWI_MAX_NUM) { - HDF_LOGE("invalid irq number %d\n", irqId); + HDF_LOGE("invalid irq number %u\n", irqId); return HDF_ERR_INVALID_PARAM; } @@ -71,7 +71,7 @@ int32_t OsalUnregisterIrq(uint32_t irqId, void *dev) irqParam.pDevId = dev; ret = LOS_HwiDelete(irqId, &irqParam); if (ret != LOS_OK) { - HDF_LOGE("irq %d unregister fail %d\n", irqId, ret); + HDF_LOGE("irq %u unregister fail %u\n", irqId, ret); return HDF_FAILURE; } @@ -81,7 +81,7 @@ int32_t OsalUnregisterIrq(uint32_t irqId, void *dev) int32_t OsalEnableIrq(uint32_t irqId) { if (irqId >= OS_HWI_MAX_NUM) { - HDF_LOGE("invalid irq number %d\n", irqId); + HDF_LOGE("invalid irq number %u\n", irqId); return HDF_ERR_INVALID_PARAM; } @@ -93,7 +93,7 @@ int32_t OsalEnableIrq(uint32_t irqId) int32_t OsalDisableIrq(uint32_t irqId) { if (irqId >= OS_HWI_MAX_NUM) { - HDF_LOGE("invalid irq number %d\n", irqId); + HDF_LOGE("invalid irq number %u\n", irqId); return HDF_ERR_INVALID_PARAM; } diff --git a/khdf/liteos/osal/src/osal_thread.c b/khdf/liteos/osal/src/osal_thread.c index 7897c58ad3303b9ac346c96be3541c55b0cfe7a6..b3e37f4d1b3a39657e43a196c675a45a3d15a157 100644 --- a/khdf/liteos/osal/src/osal_thread.c +++ b/khdf/liteos/osal/src/osal_thread.c @@ -174,7 +174,7 @@ int32_t OsalThreadSuspend(struct OsalThread *thread) bool flag = false; flag = OsalCheckPara(thread); - if (flag == false) { + if (!flag) { HDF_LOGE("%s invalid parameter %d\n", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } @@ -193,7 +193,7 @@ int32_t OsalThreadDestroy(struct OsalThread *thread) bool flag = false; flag = OsalCheckPara(thread); - if (flag == false) { + if (!flag) { HDF_LOGE("%s invalid parameter %d\n", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } @@ -210,7 +210,7 @@ int32_t OsalThreadResume(struct OsalThread *thread) bool flag = false; flag = OsalCheckPara(thread); - if (flag == false) { + if (!flag) { HDF_LOGE("%s invalid parameter %d\n", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } diff --git a/khdf/liteos/osal/src/osal_workqueue.c b/khdf/liteos/osal/src/osal_workqueue.c index e39c23ca933b2cf01139416280a52d57cd14e1d2..22348b006265ca72dc3cc363715a3fa1e7e7a800 100644 --- a/khdf/liteos/osal/src/osal_workqueue.c +++ b/khdf/liteos/osal/src/osal_workqueue.c @@ -170,7 +170,7 @@ bool HdfAddWork(HdfWorkQueue *queue, HdfWork *work) return queue_work(queue->realWorkQueue, &((struct WorkWrapper *)work->realWork)->work.work); } -bool HdfAddDelayedWork(HdfWorkQueue *queue, HdfWork *work, unsigned long ms) +bool HdfAddDelayedWork(HdfWorkQueue *queue, HdfWork *work, uint32_t ms) { if (queue == NULL || queue->realWorkQueue == NULL || work == NULL || work->realWork == NULL) { HDF_LOGE("%s invalid para", __func__); diff --git a/khdf/liteos/platform/BUILD.gn b/khdf/liteos/platform/BUILD.gn index d4b031930b4081fff7de31a37cbead2e8bf46fec..fdd264508e912ba3627f85f116e4b7f18e351d7b 100644 --- a/khdf/liteos/platform/BUILD.gn +++ b/khdf/liteos/platform/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -35,12 +35,20 @@ hdf_driver(module_name) { sources = [ "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_common.c", "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_device.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_dumper.c", "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_event.c", "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_manager.c", "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_queue.c", "src/plat_common.c", ] + if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_TRACE)) { + sources += [ + "$HDF_FRAMEWORKS_PATH/support/platform/src/fwk/platform_trace_transfer.c", + "src/platform_trace.c", + ] + } + if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_I2C)) { sources += [ "$HDF_FRAMEWORKS_PATH/support/platform/src/i2c/i2c_core.c", @@ -195,6 +203,18 @@ hdf_driver(module_name) { ] } + if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_CAN)) { + sources += [ + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_client.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_core.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_if.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_mail.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_manager.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_msg.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/can/can_service.c", + ] + } + public_configs = [ ":public" ] } diff --git a/khdf/liteos/platform/Makefile b/khdf/liteos/platform/Makefile index d9bb5507925b39323ed22012766e05a5376a2775..fb539e4c2cfe888caa918f54a29e9e4b45444fcf 100644 --- a/khdf/liteos/platform/Makefile +++ b/khdf/liteos/platform/Makefile @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -41,9 +41,15 @@ ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM), y) $(HDF_FRAMEWORKS)/support/platform/src/fwk/platform_event.c \ $(HDF_FRAMEWORKS)/support/platform/src/fwk/platform_manager.c \ $(HDF_FRAMEWORKS)/support/platform/src/fwk/platform_queue.c \ + $(HDF_FRAMEWORKS)/support/platform/src/fwk/platform_dumper.c \ $(HDF_FRAMEWORKS)/support/platform/src/fwk/platform_common.c endif +ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_TRACE), y) + LOCAL_SRCS += ./src/platform_trace.c \ + $(HDF_FRAMEWORKS)/support/platform/src/fwk/platform_trace_transfer.c +endif + ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_I2C), y) LOCAL_SRCS += $(HDF_FRAMEWORKS)/support/platform/src/i2c/i2c_if.c \ $(HDF_FRAMEWORKS)/support/platform/src/i2c/i2c_core.c @@ -163,4 +169,14 @@ ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_TIMER), y) $(HDF_FRAMEWORKS)/support/platform/src/timer/timer_core.c endif +ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_CAN), y) + LOCAL_SRCS += $(HDF_FRAMEWORKS)/support/platform/src/can/can_client.c \ + $(HDF_FRAMEWORKS)/support/platform/src/can/can_core.c \ + $(HDF_FRAMEWORKS)/support/platform/src/can/can_if.c \ + $(HDF_FRAMEWORKS)/support/platform/src/can/can_mail.c \ + $(HDF_FRAMEWORKS)/support/platform/src/can/can_manager.c \ + $(HDF_FRAMEWORKS)/support/platform/src/can/can_msg.c \ + $(HDF_FRAMEWORKS)/support/platform/src/can/can_service.c +endif + include $(HDF_DRIVER) diff --git a/khdf/liteos/platform/src/gpio_dev.c b/khdf/liteos/platform/src/gpio_dev.c index dd9f605eab049b1bd2cbaee08fef5cd52fad78e2..196efc00559078a1c87c90f51e08abbb2f2ce82f 100644 --- a/khdf/liteos/platform/src/gpio_dev.c +++ b/khdf/liteos/platform/src/gpio_dev.c @@ -62,7 +62,7 @@ static int GpioExecCmd(uint16_t gpio, struct GpioBitInfo *info, int cmd) HDF_LOGE("%s: set dir fail:%d", __func__, ret); return -1; } - PLAT_LOGV("%s: gpio:%u set dir:%u done!", __func__, gpio, info->direction); + PLAT_LOGV("%s: gpio:%hu set dir:%u done!", __func__, gpio, info->direction); break; case GPIO_GET_DIR: ret = GpioGetDir(gpio, &tmp); @@ -71,7 +71,7 @@ static int GpioExecCmd(uint16_t gpio, struct GpioBitInfo *info, int cmd) return -1; } info->direction = (unsigned char)tmp; - PLAT_LOGV("%s: gpio:%u get dir:%u done!", __func__, gpio, info->direction); + PLAT_LOGV("%s: gpio:%hu get dir:%u done!", __func__, gpio, info->direction); break; case GPIO_READ_BIT: ret = GpioRead(gpio, &tmp); @@ -80,7 +80,7 @@ static int GpioExecCmd(uint16_t gpio, struct GpioBitInfo *info, int cmd) return -1; } info->value = (unsigned char)tmp; - PLAT_LOGV("%s: gpio:%u read:%u done!", __func__, gpio, info->value); + PLAT_LOGV("%s: gpio:%hu read:%u done!", __func__, gpio, info->value); break; case GPIO_WRITE_BIT: ret = GpioWrite(gpio, info->value); @@ -88,7 +88,7 @@ static int GpioExecCmd(uint16_t gpio, struct GpioBitInfo *info, int cmd) HDF_LOGE("%s: write gpio fail:%d", __func__, ret); return -1; } - PLAT_LOGV("%s: gpio:%u write:%u done!", __func__, gpio, info->value); + PLAT_LOGV("%s: gpio:%hu write:%u done!", __func__, gpio, info->value); break; default: ret = -1; @@ -102,7 +102,14 @@ static int GpioIoctl(struct file *filep, int cmd, unsigned long arg) uint16_t bitNum; uint16_t gpio; struct GpioBitInfo info = {0}; - struct drv_data* drvData = (struct drv_data* )filep->f_vnode->data; + struct drv_data *drvData = NULL; + + if (filep == NULL || filep->f_vnode == NULL || filep->f_vnode->data == NULL) { + HDF_LOGE("%s: function parameter is null", __func__); + return HDF_ERR_INVALID_PARAM; + } + + drvData = (struct drv_data *)filep->f_vnode->data; bitNum = (uint16_t)(uintptr_t)drvData->priv; ret = LOS_CopyToKernel(&info, sizeof(struct GpioBitInfo), @@ -111,7 +118,7 @@ static int GpioIoctl(struct file *filep, int cmd, unsigned long arg) return -1; } gpio = info.groupnumber * bitNum + info.bitnumber; - PLAT_LOGV("%s: gn:%u, bn:%u, gpio:%u", __func__, info.groupnumber, info.bitnumber, gpio); + PLAT_LOGV("%s: gn:%u, bn:%u, gpio:%hu", __func__, info.groupnumber, info.bitnumber, gpio); ret = GpioExecCmd(gpio, &info, cmd); if (ret != 0) { diff --git a/khdf/liteos/platform/src/i2c_dev.c b/khdf/liteos/platform/src/i2c_dev.c index cf01a948e2aed8eb1b8bd1bc60fa883d8b94bb4c..dd1506d48e542f23635e3adef7a0340e26b704bb 100644 --- a/khdf/liteos/platform/src/i2c_dev.c +++ b/khdf/liteos/platform/src/i2c_dev.c @@ -348,8 +348,15 @@ static int I2cFsOpen(struct file *filep) { DevHandle handle = NULL; struct I2cClient *client = NULL; - struct drv_data* drvData = (struct drv_data* )filep->f_vnode->data; - int16_t id = (int16_t)(uintptr_t)drvData->priv; + struct drv_data *drvData = NULL; + int16_t id; + + if (filep == NULL || filep->f_vnode == NULL || filep->f_vnode->data == NULL) { + HDF_LOGE("%s: function parameter is null", __func__); + return -EINVAL; + } + drvData = (struct drv_data *)filep->f_vnode->data; + id = (int16_t)(uintptr_t)drvData->priv; handle = I2cOpen(id); if (handle == NULL) { diff --git a/khdf/liteos/platform/src/platform_trace.c b/khdf/liteos/platform/src/platform_trace.c new file mode 100644 index 0000000000000000000000000000000000000000..76cb2b6d0020537910d94b87320b994cf358d595 --- /dev/null +++ b/khdf/liteos/platform/src/platform_trace.c @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used + * to endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "platform_trace.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "los_trace.h" + +int32_t PlatformTraceStart(void) +{ +#ifdef LOSCFG_KERNEL_TRACE + uint32_t ret; + ret = LOS_TraceStart(); + if (ret != LOS_OK) { + HDF_LOGE("PlatformTraceStart error:%d", ret); + return HDF_FAILURE; + } +#endif + return HDF_SUCCESS; +} + +int32_t PlatformTraceStop(void) +{ +#ifdef LOSCFG_KERNEL_TRACE + LOS_TraceStop(); +#endif + return HDF_SUCCESS; +} + +void PlatformTraceReset(void) +{ +#ifdef LOSCFG_KERNEL_TRACE + LOS_TraceReset(); +#endif +} + +#ifdef LOSCFG_KERNEL_TRACE +static void TraceMeanPrint(void) +{ + uint32_t i = 0; + dprintf("********trace moudle and function explain:********\t"); + const struct PlatformTraceModuleExplain *explains = PlatformTraceModuleExplainGetAll(); + int32_t size = PlatformTraceModuleExplainCount(); + for (i = 0; i < size; i++) { + if (explains[i].moduleInfo < PLATFORM_TRACE_MODULE_MAX) { + dprintf("meaning of module num 0x%x is %s\t", explains[i].moduleInfo, explains[i].moduleMean); + } else if (explains[i].moduleInfo > PLATFORM_TRACE_MODULE_MAX) { + dprintf("meaning of function num 0x%x is %s\t", explains[i].moduleInfo, explains[i].moduleMean); + } + } +} +#endif + +void PlatformTraceInfoDump(void) +{ +#ifdef LOSCFG_KERNEL_TRACE + TraceMeanPrint(); + LOS_TraceRecordDump(FALSE); +#endif +} + +#define PLATFORM_TRACE_IDENTIFY 0x33 +void PlatformTraceAddUintMsg(int module, int moduleFun, uint infos[], uint8_t size) +{ +#ifdef LOSCFG_KERNEL_TRACE + if ((size <= 0) || (size > PLATFORM_TRACE_UINT_PARAM_SIZE_MAX)) { + HDF_LOGE("PlatformTraceAddUintMsg %hhu size illegal", size); + return; + } + if ((module < PLATFORM_TRACE_MODULE_I2S) || (module >= PLATFORM_TRACE_MODULE_MAX)) { + HDF_LOGE("PlatformTraceAddUintMsg %d module illegal", module); + return; + } + if ((moduleFun < PLATFORM_TRACE_MODULE_I2S_FUN) || (moduleFun >= PLATFORM_TRACE_MODULE_MAX_FUN)) { + HDF_LOGE("PlatformTraceAddUintMsg %d moduleFun illegal", moduleFun); + return; + } + + switch (size) { + case PLATFORM_TRACE_UINT_PARAM_SIZE_1: + LOS_TRACE_EASY(TRACE_SYS_FLAG, PLATFORM_TRACE_IDENTIFY, module, moduleFun, + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1]); + break; + case PLATFORM_TRACE_UINT_PARAM_SIZE_2: + LOS_TRACE_EASY(TRACE_SYS_FLAG, PLATFORM_TRACE_IDENTIFY, module, moduleFun, + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1], infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1]); + break; + case PLATFORM_TRACE_UINT_PARAM_SIZE_3: + LOS_TRACE_EASY(TRACE_SYS_FLAG, PLATFORM_TRACE_IDENTIFY, module, moduleFun, + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1], infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1], + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_3 - 1]); + break; + case PLATFORM_TRACE_UINT_PARAM_SIZE_4: + LOS_TRACE_EASY(TRACE_SYS_FLAG, PLATFORM_TRACE_IDENTIFY, module, moduleFun, + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1], infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1], + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_3 - 1], infos[PLATFORM_TRACE_UINT_PARAM_SIZE_4 - 1]); + break; + case PLATFORM_TRACE_UINT_PARAM_SIZE_MAX: + LOS_TRACE_EASY(TRACE_SYS_FLAG, PLATFORM_TRACE_IDENTIFY, module, moduleFun, + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_1 - 1], infos[PLATFORM_TRACE_UINT_PARAM_SIZE_2 - 1], + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_3 - 1], infos[PLATFORM_TRACE_UINT_PARAM_SIZE_4 - 1], + infos[PLATFORM_TRACE_UINT_PARAM_SIZE_MAX - 1]); + break; + default: + HDF_LOGE("PlatformTraceAddUintMsg %hhu size illegal", size); + break; + } +#endif +} + +void PlatformTraceAddMsg(const char *module, const char *moduleFun, const char *fmt, ...) +{ + // not support, return + return; +} diff --git a/khdf/liteos/platform/src/spi_dev.c b/khdf/liteos/platform/src/spi_dev.c index 7adb112a2a3b8f3fa9d9fc3724abbf8f296d3d27..9564ab1e6cf9a7673380e59688526ebf0b3419ea 100644 --- a/khdf/liteos/platform/src/spi_dev.c +++ b/khdf/liteos/platform/src/spi_dev.c @@ -163,7 +163,7 @@ static int32_t SpiDevGetCfg(struct SpiDev *dev, struct SpiCfg *mask, unsigned lo HDF_LOGI("%s: get mode 0x%x", __func__, tmp); } else if (mask->bitsPerWord == 1) { tmp = cfg.bitsPerWord; - HDF_LOGI("%s: get word %d", __func__, tmp); + HDF_LOGI("%s: get word %u", __func__, tmp); } else if (mask->maxSpeedHz == 1) { tmp = cfg.maxSpeedHz; HDF_LOGI("%s: get maxspeed %d", __func__, tmp); @@ -201,7 +201,7 @@ static int32_t SpiDevSetCfg(struct SpiDev *dev, struct SpiCfg *mask, unsigned lo HDF_LOGI("%s: set mode 0x%x", __func__, tmp); cfg.mode = tmp; } else if (mask->bitsPerWord == 1) { - HDF_LOGI("%s: set word %d", __func__, tmp); + HDF_LOGI("%s: set word %u", __func__, tmp); cfg.bitsPerWord = tmp; } else if (mask->maxSpeedHz == 1) { HDF_LOGI("%s: set maxspeed %d", __func__, tmp); @@ -285,7 +285,7 @@ static int32_t SpiDevRealTransfer(struct SpiDev *dev, struct SpiMsg *msg, struct kmsg[i].wbuf = wbuf + pos; kmsg[i].rbuf = rbuf + pos; kmsg[i].len = msg[i].len; - kmsg[i].csChange = msg[i].csChange; + kmsg[i].keepCs = msg[i].keepCs; kmsg[i].delayUs = msg[i].delayUs; kmsg[i].speed = msg[i].speed; pos += msg[i].len; @@ -420,7 +420,7 @@ static void SpiAddRemoveDev(struct SpiDev *dev, bool add) return; } if (add) { - HDF_LOGI("creat /dev/spidev%u.%u", dev->cntlr->busNum, dev->csNum); + HDF_LOGI("create /dev/spidev%u.%u", dev->cntlr->busNum, dev->csNum); if (register_driver(devName, &g_spiDevFops, HDF_SPI_FS_MODE, dev)) { HDF_LOGE("%s: gen /dev/spidev%u.%u", __func__, dev->cntlr->busNum, dev->csNum); } diff --git a/khdf/liteos/platform/src/uart_dev.c b/khdf/liteos/platform/src/uart_dev.c old mode 100755 new mode 100644 index d727fb6fdbe3e17214db7935d42489c91957715a..dd4d560ab45663a2b58c2613f9f52a3eec926f3c --- a/khdf/liteos/platform/src/uart_dev.c +++ b/khdf/liteos/platform/src/uart_dev.c @@ -47,7 +47,7 @@ static int32_t UartDevOpen(struct file *filep) } struct drv_data *drv = (struct drv_data *)filep->f_vnode->data; host = (struct UartHost *)drv->priv; - return UartHostInit(host); + return UartHostRequest(host); } static int32_t UartDevRelease(struct file *filep) { @@ -59,7 +59,7 @@ static int32_t UartDevRelease(struct file *filep) struct drv_data *drv = (struct drv_data *)filep->f_vnode->data; host = (struct UartHost *)drv->priv; - return UartHostDeinit(host); + return UartHostRelease(host); } static ssize_t UartDevRead(struct file *filep, char *buf, size_t count) @@ -226,7 +226,7 @@ static void UartAddRemoveDev(struct UartHost *host, bool add) return; } if (add) { - HDF_LOGI("creat /dev/uartdev-%d", host->num); + HDF_LOGI("create /dev/uartdev-%d", host->num); if (register_driver(devName, &g_uartDevFops, HDF_UART_FS_MODE, host)) { HDF_LOGE("%s: gen /dev/uartdev-%d fail!", __func__, host->num); OsalMemFree(devName); diff --git a/khdf/liteos/test/BUILD.gn b/khdf/liteos/test/BUILD.gn index b6f2934feba3e824ad7a261b4fdae92502c86485..553b67a8c8f2f08f597a7878ecf0444f62d83ca5 100755 --- a/khdf/liteos/test/BUILD.gn +++ b/khdf/liteos/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -82,12 +82,18 @@ hdf_driver(module_name) { sources += [ "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_device_test.c", "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_driver_test.c", + "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_dumper_test.c", "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_event_test.c", "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_manager_test.c", "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_queue_test.c", - "$HDF_TEST_FRAMWORK_ROOT/platform/entry/hdf_platform_core_entry_test.c", + "$HDF_TEST_FRAMWORK_ROOT/platform/entry/hdf_platform_entry_test.c", ] + if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_TRACE)) { + sources += + [ "$HDF_TEST_FRAMWORK_ROOT/platform/common/platform_trace_test.c" ] + } + if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_GPIO)) { sources += [ "$HDF_TEST_FRAMWORK_ROOT/platform/common/gpio_driver_test.c", @@ -242,6 +248,14 @@ hdf_driver(module_name) { "$HDF_TEST_FRAMWORK_ROOT/platform/entry/hdf_timer_entry_test.c", ] } + + if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_CAN)) { + sources += [ + "$HDF_ADAPTERS_PATH/platform/can/can_virtual.c", + "$HDF_TEST_FRAMWORK_ROOT/platform/common/can_test.c", + "$HDF_TEST_FRAMWORK_ROOT/platform/config/can_test_config.c", + ] + } } if (defined(LOSCFG_DRIVERS_HDF_SENSOR)) { diff --git a/khdf/liteos/test/Makefile b/khdf/liteos/test/Makefile index 54c14125ab00878d2d6472112df4cb47ddad674a..f979c606430b5d7bb2fee9967d976c948956dde6 100755 --- a/khdf/liteos/test/Makefile +++ b/khdf/liteos/test/Makefile @@ -1,4 +1,4 @@ -# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, # are permitted provided that the following conditions are met: @@ -31,6 +31,7 @@ include ./test_lite.mk MODULE_NAME := hdf_test HDF_TEST_FRAMWORK_ROOT = $(LITEOSTOPDIR)/../../drivers/framework/test/unittest +HDF_ADAPTERS_ROOT = $(LITEOSTOPDIR)/../../drivers/adapter LOCAL_SRCS := $(HDF_TEST_FRAMWORK_ROOT)/common/hdf_main_test.c \ $(HDF_TEST_FRAMWORK_ROOT)/manager/sample_driver_test.c \ @@ -65,9 +66,13 @@ ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM), y) LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_driver_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_event_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_queue_test.c +LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_dumper_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_device_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_manager_test.c -LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/entry/hdf_platform_core_entry_test.c +LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/entry/hdf_platform_entry_test.c +ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_TRACE), y) +LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/platform_trace_test.c +endif ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_GPIO), y) LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/gpio_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/gpio_driver_test.c @@ -159,6 +164,10 @@ LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/timer_driver_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/timer_test.c LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/entry/hdf_timer_entry_test.c endif +ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_CAN), y) +LOCAL_SRCS += $(HDF_TEST_FRAMWORK_ROOT)/platform/common/can_test.c +LOCAL_SRCS += $(HDF_ADAPTERS_ROOT)/platform/can/can_virtual.c +endif endif ifeq ($(LOSCFG_DRIVERS_HDF_PLATFORM_REGULATOR), y) diff --git a/khdf/liteos_m/Kconfig b/khdf/liteos_m/Kconfig index b6adead9b5d0453426709174df580716a8cb8a9c..80ba9281fb6e8e56163103cb93f7e348a4fc5142 100644 --- a/khdf/liteos_m/Kconfig +++ b/khdf/liteos_m/Kconfig @@ -35,7 +35,7 @@ config DRIVERS_HDF Answer Y to enable LiteOS support HDF. if DRIVERS_HDF config DRIVERS_HDF_CONFIG_MACRO - bool "Enable HDF platform driver" + bool "Enable HDF config macro parser" default n depends on DRIVERS_HDF help diff --git a/khdf/liteos_m/core/BUILD.gn b/khdf/liteos_m/core/BUILD.gn index 8b2c9b8f5f16bfe69d1a95df6a2ab7387830d9ff..9428feaf7acd50b898cbc9b5fc1695bfb6fad2cc 100644 --- a/khdf/liteos_m/core/BUILD.gn +++ b/khdf/liteos_m/core/BUILD.gn @@ -83,4 +83,14 @@ hdf_driver("hdf_core") { defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { deps = [ "//device/board/fnlink/shields" ] } + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/khdf/liteos_m/hdf.gni b/khdf/liteos_m/hdf.gni index b1b631bff14c4655c0eaa26c8bb78bc334d5c8cd..18a6ee47be06799fa9aef543c50479cf306e6e31 100644 --- a/khdf/liteos_m/hdf.gni +++ b/khdf/liteos_m/hdf.gni @@ -41,12 +41,30 @@ template("hdf_driver") { module_switch = true } - if (module_switch && defined(invoker.hcs_sources)) { + if (defined(invoker.macro_switch)) { + macro_switch = invoker.macro_switch + } else { + macro_switch = false + } + + if (!macro_switch && module_switch && defined(invoker.hcs_sources)) { hc_gen_hex("${target_name}_hc_gen_hex") { sources = invoker.hcs_sources } } + + if (macro_switch && module_switch && defined(invoker.hcs_sources)) { + hc_gen_macro("${target_name}_hc_gen_macro") { + sources = invoker.hcs_sources + outputs = [ "$target_gen_dir/hdf_config_macro.h" ] + } + } + kernel_module(target_name) { + if (defined(invoker.special_visibility)) { + visibility += invoker.special_visibility + } + forward_variables_from(invoker, "*", [ @@ -59,7 +77,8 @@ template("hdf_driver") { if (defined(invoker.visibility)) { visibility += invoker.visibility } - if (module_switch && defined(invoker.hcs_sources)) { + + if (module_switch && !macro_switch && defined(invoker.hcs_sources)) { hc_gen = ":${target_name}_hc_gen_hex" generated_sources = get_target_outputs(hc_gen) set_sources_assignment_filter([ "*.hcb" ]) @@ -74,10 +93,20 @@ template("hdf_driver") { deps = [ hc_gen ] } } + + if (module_switch && macro_switch && defined(invoker.hcs_sources)) { + hc_gen_macro = ":${target_name}_hc_gen_macro" + if (defined(invoker.deps)) { + deps += [ hc_gen_macro ] + } else { + deps = [ hc_gen_macro ] + } + } } } set_defaults("hdf_driver") { configs = [ "$HDFTOPDIR:hdf_config" ] visibility = [ "$HDFTOPDIR:*" ] + visibility += [ "//drivers/adapter/*" ] } diff --git a/khdf/liteos_m/hdf_macro.gni b/khdf/liteos_m/hdf_macro.gni deleted file mode 100755 index 6d6ee4b2523f9852b800343bddec9efe0ccc894d..0000000000000000000000000000000000000000 --- a/khdf/liteos_m/hdf_macro.gni +++ /dev/null @@ -1,85 +0,0 @@ -# -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -import("//drivers/framework/tools/hc-gen/hc_gen.gni") -import("//kernel/liteos_m/liteos.gni") - -HDF_FRAMEWORKS_PATH = "//drivers/framework" -HDF_PERIPHERAL_PATH = "//drivers/peripheral" -HDF_ADAPTER_PLATFORM_PATH = "//drivers/adapter/platform" - -template("hdf_driver") { - if (defined(invoker.module_switch)) { - forward_variables_from(invoker, [ "module_switch" ]) - } else { - module_switch = true - } - - if (module_switch && defined(invoker.hcs_sources)) { - hc_gen_macro("${target_name}_hc_gen_macro") { - sources = invoker.hcs_sources - outputs = invoker.outputs - } - } - kernel_module(target_name) { - forward_variables_from(invoker, - "*", - [ - "configs", - "visibility", - ]) - if (defined(invoker.configs)) { - configs += invoker.configs - } - if (defined(invoker.visibility)) { - visibility += invoker.visibility - } - if (module_switch && defined(invoker.hcs_sources)) { - hc_gen_macro = ":${target_name}_hc_gen_macro" - generated_include = get_path_info(get_target_outputs(hc_gen_macro), "dir") - if (defined(invoker.include_dirs)) { - include_dirs += generated_include - } else { - include_dirs = generated_include - } - if (defined(invoker.deps)) { - deps += [ hc_gen_macro ] - } else { - deps = [ hc_gen_macro ] - } - } - } -} - -set_defaults("hdf_driver") { - configs = [ "$HDFTOPDIR:hdf_config" ] - visibility = [ "$HDFTOPDIR:*" ] - visibility += [ "//device/soc/bestechnic/bes2600/liteos_m/*" ] - visibility += [ "//drivers/adapter/*" ] -} diff --git a/khdf/liteos_m/network/src/net_device_adapter.c b/khdf/liteos_m/network/src/net_device_adapter.c index 7888835d05cc50a8c7fa3b22f1de839a13df4228..55ab63e71303416ac155239e2a993591768f18b8 100644 --- a/khdf/liteos_m/network/src/net_device_adapter.c +++ b/khdf/liteos_m/network/src/net_device_adapter.c @@ -71,6 +71,7 @@ static int32_t LiteNetDevInit(struct NetDeviceImpl *netDeviceImpl) HDF_LOGI("%s Success!", __func__); return HDF_SUCCESS; } + static int32_t LiteNetDevDeInit(struct NetDeviceImpl *netDeviceImpl) { if (netDeviceImpl == NULL) { diff --git a/khdf/liteos_m/network/src/netbuf_adapter.c b/khdf/liteos_m/network/src/netbuf_adapter.c index 75bcc4b4440dda69088c27892c5eedbff76a9c55..c83308336a99320e5da4764cea216f000f89c082 100644 --- a/khdf/liteos_m/network/src/netbuf_adapter.c +++ b/khdf/liteos_m/network/src/netbuf_adapter.c @@ -28,6 +28,7 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "netbuf_adapter.h" #include "hdf_netbuf.h" #include "los_memory.h" #include "lwip/prot/ethernet.h" diff --git a/khdf/liteos_m/osal/src/osal_thread.c b/khdf/liteos_m/osal/src/osal_thread.c index af025da61feb074c537a60e7c1f74ac83e378514..d84b29fd525031841af65abc4df77c356e2bd135 100644 --- a/khdf/liteos_m/osal/src/osal_thread.c +++ b/khdf/liteos_m/osal/src/osal_thread.c @@ -167,7 +167,7 @@ int32_t OsalThreadSuspend(struct OsalThread *thread) bool flag = false; flag = OsalCheckPara(thread); - if (flag == false) { + if (!flag) { HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } @@ -186,7 +186,7 @@ int32_t OsalThreadDestroy(struct OsalThread *thread) bool flag = false; flag = OsalCheckPara(thread); - if (flag == false) { + if (!flag) { HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } @@ -203,7 +203,7 @@ int32_t OsalThreadResume(struct OsalThread *thread) bool flag = false; flag = OsalCheckPara(thread); - if (flag == false) { + if (!flag) { HDF_LOGE("%s invalid parameter %d", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } diff --git a/khdf/liteos_m/platform/BUILD.gn b/khdf/liteos_m/platform/BUILD.gn index 6228cc04900ac6206f6a2615904b42dcc4e144a7..7a33e4cb563bc137bbb60fd1027ccb54cf808931 100755 --- a/khdf/liteos_m/platform/BUILD.gn +++ b/khdf/liteos_m/platform/BUILD.gn @@ -64,7 +64,10 @@ hdf_driver("hdf_platform_lite") { } if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_PWM)) { - sources += [ "$HDF_FRAMEWORKS_PATH/support/platform/src/pwm/pwm_core.c" ] + sources += [ + "$HDF_FRAMEWORKS_PATH/support/platform/src/pwm/pwm_core.c", + "$HDF_FRAMEWORKS_PATH/support/platform/src/pwm/pwm_if.c", + ] } if (defined(LOSCFG_DRIVERS_HDF_PLATFORM_RTC)) { diff --git a/platform/BUILD.gn b/platform/BUILD.gn index 4b2029491a54f6e06f2a0487d16bb5bb92fd14c1..3aff7986ef77671ff54d6136a3a25bcd39e91a2e 100644 --- a/platform/BUILD.gn +++ b/platform/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") diff --git a/platform/can/can_virtual.c b/platform/can/can_virtual.c new file mode 100644 index 0000000000000000000000000000000000000000..1dbcf9994ab81a34c35f2bc20b2896bb8f702433 --- /dev/null +++ b/platform/can/can_virtual.c @@ -0,0 +1,367 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include + +#include "can/can_core.h" +#include "device_resource_if.h" +#include "hdf_log.h" +#include "osal_mem.h" + +#define HDF_LOG_TAG can_virtual_c +#define CAN_VIRTUAL_BUS_NUM_DFT 31 + +struct VirtualCanCntlr { + struct CanCntlr cntlr; + uint8_t workMode; + uint32_t bitRate; + uint32_t syncJumpWidth; + uint32_t timeSeg1; + uint32_t timeSeg2; + uint32_t prescaler; + int32_t busState; +}; + +enum VIRTUAL_CAN_SPEED { + CAN_SPEED_1M = 1000UL * 1000, /* 1 MBit/sec */ + CAN_SPEED_800K = 1000UL * 800, /* 800 kBit/sec */ + CAN_SPEED_500K = 1000UL * 500, /* 500 kBit/sec */ + CAN_SPEED_250K = 1000UL * 250, /* 250 kBit/sec */ + CAN_SPEED_125K = 1000UL * 125, /* 125 kBit/sec */ + CAN_SPEED_100K = 1000UL * 100, /* 100 kBit/sec */ + CAN_SPEED_50K = 1000UL * 50, /* 50 kBit/sec */ + CAN_SPEED_20K = 1000UL * 20, /* 20 kBit/sec */ + CAN_SPEED_10K = 1000UL * 10 /* 10 kBit/sec */ +}; + +static int32_t VirtualReadConfigFromHcs(struct VirtualCanCntlr *virtualCan) +{ + uint32_t value; + const struct DeviceResourceNode *node = NULL; + struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + + if (virtualCan == NULL) { + HDF_LOGE("%s: virtual cntlr is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + node = PlatformDeviceGetDrs(&virtualCan->cntlr.device); + if (node == NULL) { + HDF_LOGE("%s: properity node null", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (iface == NULL || iface->GetUint32 == NULL) { + HDF_LOGE("%s: face is invalid", __func__); + return HDF_ERR_NOT_SUPPORT; + } + + if (iface->GetUint32(node, "bus_num", &value, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read bus number failed", __func__); + return HDF_FAILURE; + } + virtualCan->cntlr.number = (int32_t)value; + + if (iface->GetUint8(node, "work_mode", &virtualCan->workMode, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read work mode failed", __func__); + return HDF_FAILURE; + } + + if (iface->GetUint32(node, "bit_rate", &virtualCan->bitRate, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read bit reate failed", __func__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t VirtualCanMsgLoopBack(struct VirtualCanCntlr *virtualCan, const struct CanMsg *msg) +{ + struct CanMsg *new = NULL; + + new = CanMsgObtain(); + if (new == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + + *new = *msg; // Yeah! this is loop back ... + + virtualCan->busState = CAN_BUS_READY; + return CanCntlrOnNewMsg(&virtualCan->cntlr, new); +} + +static int32_t VirtualCanSendMsg(struct CanCntlr *cntlr, const struct CanMsg *msg) +{ + struct VirtualCanCntlr *virtualCan = NULL; + + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + if (msg == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + virtualCan = (struct VirtualCanCntlr *)cntlr->device.priv; + if (virtualCan == NULL) { + HDF_LOGE("%s: private data is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + virtualCan->busState = CAN_BUS_BUSY; + + return VirtualCanMsgLoopBack(virtualCan, msg); +} + +struct VirtualSpeedConfigMap { + uint32_t speed; + uint32_t sjw; + uint32_t tsg1; + uint32_t tsg2; + uint32_t prescaler; +}; + +#define CAN_CLK 80M + +#define CAN_SJW_2TQ 2 +#define CAN_BS1_5TQ 5 +#define CAN_BS1_7TQ 7 +#define CAN_BS1_13TQ 13 +#define CAN_BS1_14TQ 14 +#define CAN_BS2_2TQ 2 +#define CAN_BS2_5TQ 5 + +/* + * S(bit rate) = 1 / ((1 + BS1 + BS2) * TQ) + * TQ = Prescaler / Fclk + * S(bit rate) = Fclk / (Prescaler * (1 + BS1 + BS2)) + */ + +static const struct VirtualSpeedConfigMap g_speedMaps[] = { + {CAN_SPEED_1M, CAN_SJW_2TQ, CAN_BS1_5TQ, CAN_BS2_2TQ, 10 }, + {CAN_SPEED_800K, CAN_SJW_2TQ, CAN_BS1_14TQ, CAN_BS2_5TQ, 5 }, + {CAN_SPEED_500K, CAN_SJW_2TQ, CAN_BS1_7TQ, CAN_BS2_2TQ, 16 }, + {CAN_SPEED_250K, CAN_SJW_2TQ, CAN_BS1_13TQ, CAN_BS2_2TQ, 20 }, + {CAN_SPEED_125K, CAN_SJW_2TQ, CAN_BS1_13TQ, CAN_BS2_2TQ, 40 }, + {CAN_SPEED_100K, CAN_SJW_2TQ, CAN_BS1_13TQ, CAN_BS2_2TQ, 50 }, + {CAN_SPEED_50K, CAN_SJW_2TQ, CAN_BS1_13TQ, CAN_BS2_2TQ, 100}, + {CAN_SPEED_20K, CAN_SJW_2TQ, CAN_BS1_13TQ, CAN_BS2_2TQ, 250}, + {CAN_SPEED_10K, CAN_SJW_2TQ, CAN_BS1_13TQ, CAN_BS2_2TQ, 500} +}; + +static int32_t VirtualCanSetBitRate(struct VirtualCanCntlr *virtualCan, uint32_t speed) +{ + int32_t i; + const struct VirtualSpeedConfigMap *cfgMap; + + for (i = 0; i < sizeof(g_speedMaps) / sizeof(g_speedMaps[0]); i++) { + if (g_speedMaps[i].speed == speed) { + break; + } + } + + if (i >= (sizeof(g_speedMaps) / sizeof(g_speedMaps[0]))) { + HDF_LOGE("%s: speed: %u not support", __func__, speed); + return HDF_ERR_NOT_SUPPORT; + } + + cfgMap = &g_speedMaps[i]; + virtualCan->syncJumpWidth = cfgMap->sjw; + virtualCan->timeSeg1 = cfgMap->tsg1; + virtualCan->timeSeg2 = cfgMap->tsg2; + virtualCan->prescaler = cfgMap->prescaler; + virtualCan->bitRate = speed; + return HDF_SUCCESS; +} + +static int32_t VirtualCanSetMode(struct VirtualCanCntlr *virtualCan, int32_t mode) +{ + switch (mode) { + case CAN_BUS_LOOPBACK: + virtualCan->workMode = CAN_BUS_LOOPBACK; + break; + default: + return HDF_ERR_NOT_SUPPORT; + } + return HDF_SUCCESS; +} + +static int32_t VirtualCanSetCfg(struct CanCntlr *cntlr, const struct CanConfig *cfg) +{ + int32_t ret; + struct VirtualCanCntlr *virtualCan = NULL; + + virtualCan = (struct VirtualCanCntlr *)cntlr->device.priv; + if (virtualCan == NULL) { + HDF_LOGE("%s: private data is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + virtualCan->busState = CAN_BUS_RESET; + if ((ret = VirtualCanSetBitRate(virtualCan, cfg->speed) != HDF_SUCCESS)) { + HDF_LOGE("%s: set speed failed", __func__); + return ret; + } + + if ((ret = VirtualCanSetMode(virtualCan, cfg->mode) != HDF_SUCCESS)) { + HDF_LOGE("%s: set mode failed", __func__); + return ret; + } + + virtualCan->busState = CAN_BUS_READY; + return HDF_SUCCESS; +} + +static int32_t VirtualCanGetCfg(struct CanCntlr *cntlr, struct CanConfig *cfg) +{ + struct VirtualCanCntlr *virtualCan = NULL; + + virtualCan = (struct VirtualCanCntlr *)cntlr->device.priv; + if (virtualCan == NULL) { + HDF_LOGE("%s: private data is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + cfg->speed = virtualCan->bitRate; + cfg->mode = virtualCan->workMode; + return HDF_SUCCESS; +} + +struct CanCntlrMethod g_virtualCanMethod = { + .sendMsg = VirtualCanSendMsg, + .setCfg = VirtualCanSetCfg, + .getCfg = VirtualCanGetCfg, +}; + +/* + * ! this function will not be invoked when polciy is 0 + * ! no need to make any changes here + */ +static int32_t HdfVirtualCanBind(struct HdfDeviceObject *device) +{ + return CanServiceBind(device); +} + +static void VirtualCanSetDefault(struct VirtualCanCntlr *virtualCan) +{ + virtualCan->cntlr.number = CAN_VIRTUAL_BUS_NUM_DFT; + virtualCan->workMode = CAN_BUS_LOOPBACK; + virtualCan->bitRate = CAN_SPEED_10K; +} + +static int32_t VirtualCanInit(struct VirtualCanCntlr *virtualCan) +{ + int32_t ret; + + HDF_LOGI("%s: enter", __func__); + if (virtualCan == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + VirtualCanSetDefault(virtualCan); + + ret = VirtualReadConfigFromHcs(virtualCan); + if (ret != HDF_SUCCESS) { + HDF_LOGW("VirtualUartInit: read hcs config failed"); + } + + if ((ret = VirtualCanSetBitRate(virtualCan, virtualCan->bitRate) != HDF_SUCCESS)) { + HDF_LOGE("%s: set bit rate failed", __func__); + return ret; + } + + if ((ret = VirtualCanSetMode(virtualCan, virtualCan->workMode) != HDF_SUCCESS)) { + HDF_LOGE("%s: set mode failed", __func__); + return ret; + } + + return HDF_SUCCESS; +} + +static int32_t HdfVirtualCanInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct VirtualCanCntlr *virtualCan = NULL; + + HDF_LOGI("%s: entry", __func__); + if (device == NULL) { + HDF_LOGE("%s: device is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + virtualCan = (struct VirtualCanCntlr *)OsalMemCalloc(sizeof(*virtualCan)); + if (virtualCan == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + + virtualCan->busState = CAN_BUS_RESET; + ret = VirtualCanInit(virtualCan); + if (ret != HDF_SUCCESS) { + OsalMemFree(virtualCan); + HDF_LOGE("%s: can init error: %d", __func__, ret); + return ret; + } + virtualCan->busState = CAN_BUS_READY; + + virtualCan->cntlr.ops = &g_virtualCanMethod; + virtualCan->cntlr.device.priv = virtualCan; + ret = CanCntlrAdd(&virtualCan->cntlr); + if (ret != HDF_SUCCESS) { + OsalMemFree(virtualCan); + HDF_LOGE("%s: add cntlr failed: %d", __func__, ret); + return ret; + } + + ret = CanCntlrSetHdfDev(&virtualCan->cntlr, device); + if (ret != HDF_SUCCESS) { + HDF_LOGW("%s: can controller attach failed:%d", __func__, ret); + } + + return HDF_SUCCESS; +} + +static int32_t VirtualCanDeinit(struct VirtualCanCntlr *virtualCan) +{ + (void)virtualCan; + return HDF_SUCCESS; +} + +static void HdfVirtualCanRelease(struct HdfDeviceObject *device) +{ + struct VirtualCanCntlr *virtualCan = NULL; + + HDF_LOGI("%s: entry", __func__); + if (device == NULL) { + HDF_LOGE("%s: device is null", __func__); + return; + } + + virtualCan = (struct VirtualCanCntlr *)CanCntlrFromHdfDev(device); + if (virtualCan == NULL) { + HDF_LOGE("%s: platform device not bind", __func__); + return; + } + + CanServiceRelease(device); + (void)CanCntlrDel(&virtualCan->cntlr); + VirtualCanDeinit(virtualCan); + OsalMemFree(virtualCan); +} + +static struct HdfDriverEntry g_hdfCanDriver = { + .moduleVersion = 1, + .moduleName = "can_driver_virtual", + .Bind = HdfVirtualCanBind, + .Init = HdfVirtualCanInit, + .Release = HdfVirtualCanRelease, +}; + +struct HdfDriverEntry *CanVirtualGetEntry(void) +{ + return &g_hdfCanDriver; +} + +HDF_INIT(g_hdfCanDriver); diff --git a/platform/gpio/BUILD.gn b/platform/gpio/BUILD.gn index 31b7bbfd9bf0bffe6198e986d55caaa3f352a5e7..090587dc1f1abb1476126a9eeb9c5d3a5de0bbcc 100644 --- a/platform/gpio/BUILD.gn +++ b/platform/gpio/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") @@ -40,9 +19,29 @@ hdf_driver(module_name) { if (defined(LOSCFG_SOC_COMPANY_GOODIX)) { sources += [ "gpio_gr5xx.c" ] } + if (defined(LOSCFG_SOC_COMPANY_ASRMICRO)) { + sources += [ "gpio_asr.c" ] + } + if (defined(LOSCFG_SOC_COMPANY_WINNERMICRO)) { + sources += [ "gpio_wm.c" ] + } + if (defined(LOSCFG_SOC_SERIES_STM32F4xx)) { + sources += [ "gpio_stm32f4xx.c" ] + } include_dirs = [ "." ] - if (defined(LOSCFG_SHIELD_V200ZR_EVB_T1)) { + if (defined(LOSCFG_SHIELD_V200ZR_EVB_T1) && + defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { deps = [ "//device/board/fnlink/shields" ] } + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/platform/gpio/gpio_asr.c b/platform/gpio/gpio_asr.c new file mode 100644 index 0000000000000000000000000000000000000000..24fe2b222aff5e893656dd8f8fb6fee88ae38243 --- /dev/null +++ b/platform/gpio/gpio_asr.c @@ -0,0 +1,477 @@ +/* + * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include "gpio_core.h" +#include "hdf_log.h" +#include "osal_irq.h" +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#endif +#include "duet_gpio.h" + +#define ASR_GPIO_TOTAL_NUM DUET_GPIO_TOTAL_NUM +#define asr_gpio_config_t duet_gpio_config_t +#define ASR_ANALOG_MODE DUET_ANALOG_MODE +#define ASR_IRQ_MODE DUET_IRQ_MODE +#define ASR_INPUT_PULL_UP DUET_INPUT_PULL_UP +#define ASR_INPUT_PULL_DOWN DUET_INPUT_PULL_DOWN +#define ASR_INPUT_HIGH_IMPEDANCE DUET_INPUT_HIGH_IMPEDANCE +#define ASR_OUTPUT_PUSH_PULL DUET_OUTPUT_PUSH_PULL +#define ASR_OUTPUT_OPEN_DRAIN_NO_PULL DUET_OUTPUT_OPEN_DRAIN_NO_PULL +#define ASR_OUTPUT_OPEN_DRAIN_PULL_UP DUET_OUTPUT_OPEN_DRAIN_PULL_UP + +#define asr_gpio_dev_t duet_gpio_dev_t +#define asr_gpio_irq_trigger_t duet_gpio_irq_trigger_t +#define asr_gpio_irq_handler_t duet_gpio_irq_handler_t +#define asr_gpio_cb_t duet_gpio_cb_t +#define asr_gpio_init duet_gpio_init +#define asr_gpio_output_high duet_gpio_output_high +#define asr_gpio_output_low duet_gpio_output_low +#define asr_gpio_output_toggle duet_gpio_output_toggle +#define asr_gpio_input_get duet_gpio_input_get +#define asr_gpio_enable_irq duet_gpio_enable_irq +#define asr_gpio_disable_irq duet_gpio_disable_irq +#define asr_gpio_clear_irq duet_gpio_clear_irq +#define asr_gpio_finalize duet_gpio_finalize + +#define HDF_LOG_TAG GPIO_ASR + +struct GpioResource { + uint32_t pin; + uint32_t realPin; + uint32_t config; + uint32_t pinNum; + uint32_t type; /**< Type of the input event EV_KEY */ + uint32_t code; /**< Specific code item of the input event KEY_POWER*/ + unsigned long physBase; +}; + +enum GpioDeviceState { + GPIO_DEVICE_UNINITIALIZED = 0x0u, + GPIO_DEVICE_INITIALIZED = 0x1u, +}; + +struct GpioDevice { + asr_gpio_dev_t dev; + struct GpioResource resource; +}; + +static struct GpioCntlr g_gpioCntlr; +struct OemGpioIrqHandler { + uint8_t port; + GpioIrqFunc func; + void *arg; +}; + +static asr_gpio_dev_t g_gpioPinMap[ASR_GPIO_TOTAL_NUM] = {0}; +static asr_gpio_irq_trigger_t g_gpioIrqCfg[ASR_GPIO_TOTAL_NUM] = {0}; + +static void OemGpioIrqHdl(uint16_t *arg) +{ + asr_gpio_dev_t *dev = NULL; + uint16_t gpio = (uint16_t)arg; + + dev = &g_gpioPinMap[gpio]; + if ((uint16_t)dev->port >= ASR_GPIO_TOTAL_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, (uint16_t)dev->port); + return; + } + GpioCntlrIrqCallback(&g_gpioCntlr, gpio); +} + +/* dispatch */ +int32_t GpioDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + if (client == NULL || client->device == NULL || data == NULL || reply == NULL) { + HDF_LOGE("%s: client or client->device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + return HDF_SUCCESS; +} + +/* dev api */ +static int32_t GpioDevWrite(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t val) +{ + (void)cntlr; + uint32_t ret = 0; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if (val) { + ret = asr_gpio_output_high(dev); + } else { + ret = asr_gpio_output_low(dev); + } + if (EIO == ret) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, dev->port); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val) +{ + (void)cntlr; + uint32_t ret = 0; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if ((cntlr == NULL) || (val == NULL)) { + return HDF_ERR_INVALID_PARAM; + } + ret = asr_gpio_input_get(dev, val); + if (EIO == ret) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, dev->port); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t dir) +{ + (void)cntlr; + uint32_t ret = 0; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if (cntlr == NULL) { + return HDF_ERR_INVALID_PARAM; + } + dev->config = (asr_gpio_config_t)dir; + ret = asr_gpio_init(dev); + if (EIO == ret) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, dev->port); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevGetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *dir) +{ + (void)cntlr; + uint32_t ret = 0; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if ((cntlr == NULL) || (dir == NULL)) { + return HDF_ERR_INVALID_PARAM; + } + if (dev->port >= ASR_GPIO_TOTAL_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, dev->port); + return HDF_ERR_NOT_SUPPORT; + } + *dir = (uint16_t)dev->config; + + return HDF_SUCCESS; +} + +static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mode) +{ + (void)cntlr; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + HDF_LOGE("%s %d, pin:%d", __func__, __LINE__, (uint16_t)dev->port); + if (dev->port >= ASR_GPIO_TOTAL_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, dev->port); + return HDF_ERR_NOT_SUPPORT; + } + + if ((mode == OSAL_IRQF_TRIGGER_RISING) || (mode == OSAL_IRQF_TRIGGER_FALLING) + || (mode == (OSAL_IRQF_TRIGGER_RISING | OSAL_IRQF_TRIGGER_FALLING))) { + g_gpioIrqCfg[gpio] = (asr_gpio_config_t)mode; + } else { + HDF_LOGE("%s %d, error mode:%d", __func__, __LINE__, mode); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevUnSetIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if (dev->port >= ASR_GPIO_TOTAL_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, dev->port); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevEnableIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if ((uint16_t)dev->port >= ASR_GPIO_TOTAL_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, (uint16_t)dev->port); + return HDF_ERR_NOT_SUPPORT; + } + + dev->config = ASR_IRQ_MODE; + asr_gpio_init(dev); + asr_gpio_enable_irq(dev, g_gpioIrqCfg[gpio], OemGpioIrqHdl, (void *)gpio); + HDF_LOGE("%s %d, pin:%d", __func__, __LINE__, (uint16_t)dev->port); + + return HDF_SUCCESS; +} + +static int32_t GpioDevDisableIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + asr_gpio_dev_t *dev = &g_gpioPinMap[gpio]; + + if ((uint16_t)dev->port >= ASR_GPIO_TOTAL_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, (uint16_t)dev->port); + return HDF_ERR_NOT_SUPPORT; + } + asr_gpio_disable_irq(dev); + + return HDF_SUCCESS; +} + +struct GpioMethod g_GpioCntlrMethod = { + .request = NULL, + .release = NULL, + .write = GpioDevWrite, + .read = GpioDevRead, + .setDir = GpioDevSetDir, + .getDir = GpioDevGetDir, + .toIrq = NULL, + .setIrq = GpioDevSetIrq, + .unsetIrq = GpioDevUnSetIrq, + .enableIrq = GpioDevEnableIrq, + .disableIrq = GpioDevDisableIrq, +}; + +static int InitGpioDevice(struct GpioDevice *device) +{ + uint32_t ret = 0; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + HDF_LOGE("%s %d, pin:%d, mode:%d", __func__, __LINE__, device->dev.port, device->dev.config); + ret = asr_gpio_init(&device->dev); + if (EIO == ret) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, device->dev.port); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define PLATFORM_GPIO_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), gpio_config) +static uint32_t GetGpioDeviceResource(struct GpioDevice *device) +{ + uint32_t relPin; + int32_t ret; + struct GpioResource *resource = NULL; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + resource->pinNum = HCS_PROP(PLATFORM_GPIO_CONFIG, pinNum); + uint32_t pins[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, pin)); + uint32_t realPins[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, realPin)); + uint32_t configs[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, config)); + for (size_t i = 0; i < resource->pinNum; i++) { + resource->pin = pins[i]; + resource->realPin = realPins[i]; + resource->config = configs[i]; + + relPin = resource->realPin;// / DECIMALNUM * OCTALNUM + resource->realPin % DECIMALNUM; + g_gpioPinMap[resource->pin].port = (uint8_t)relPin; + g_gpioPinMap[resource->pin].config = (asr_gpio_config_t)resource->config; + device->dev.config = (asr_gpio_config_t)resource->config; + resource->pin = relPin; + device->dev.port = relPin; + + ret = InitGpioDevice(device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("InitGpioDevice FAIL\r\n"); + return HDF_FAILURE; + } + } + + return HDF_SUCCESS; +} +#else +static uint32_t GetGpioDeviceResource( + struct GpioDevice *device, const struct DeviceResourceNode *resourceNode) +{ + uint32_t relPin; + int32_t ret; + struct GpioResource *resource = NULL; + struct DeviceResourceIface *dri = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid"); + return HDF_ERR_INVALID_OBJECT; + } + + if (dri->GetUint32(resourceNode, "pinNum", &resource->pinNum, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read pinNum fail"); + return HDF_FAILURE; + } + + for (size_t i = 0; i < resource->pinNum; i++) { + if (dri->GetUint32ArrayElem(resourceNode, "pin", i, &resource->pin, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read pin fail"); + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "realPin", i, &resource->realPin, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read realPin fail"); + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "config", i, &resource->config, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read config fail"); + return HDF_FAILURE; + } + + relPin = resource->realPin;// / DECIMALNUM * OCTALNUM + resource->realPin % DECIMALNUM; + g_gpioPinMap[resource->pin].port = (uint8_t)relPin; + g_gpioPinMap[resource->pin].config = (asr_gpio_config_t)resource->config; + device->dev.config = (asr_gpio_config_t)resource->config; + device->dev.port = relPin; + resource->pin = relPin; + + ret = InitGpioDevice(device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("InitGpioDevice FAIL\r\n"); + return HDF_FAILURE; + } + } + + return HDF_SUCCESS; +} +#endif + + +static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + + struct GpioDevice *gpioDevice = NULL; +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL) { +#else + if (device == NULL || device->property == NULL) { +#endif + HDF_LOGE("%s: param is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + gpioDevice = (struct GpioDevice *)OsalMemAlloc(sizeof(struct GpioDevice)); + if (gpioDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc gpioDevice error", __func__); + return HDF_ERR_MALLOC_FAIL; + } +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetGpioDeviceResource(gpioDevice); +#else + ret = GetGpioDeviceResource(gpioDevice, device->property); +#endif + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(gpioDevice); + return HDF_FAILURE; + } + + (void)OsalMemFree(gpioDevice); + gpioCntlr->count = gpioDevice->resource.pinNum; + + return HDF_SUCCESS; +} + +static int32_t GpioDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct GpioCntlr *gpioCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = PlatformDeviceBind(&g_gpioCntlr.device, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: bind hdf device failed:%d", __func__, ret); + return ret; + } + + gpioCntlr = GpioCntlrFromHdfDev(device); + if (gpioCntlr == NULL) { + HDF_LOGE("GpioCntlrFromHdfDev fail\r\n"); + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + ret = AttachGpioDevice(gpioCntlr, device); // GpioCntlr add GpioDevice to priv + if (ret != HDF_SUCCESS) { + HDF_LOGE("AttachGpioDevice fail\r\n"); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + gpioCntlr->ops = &g_GpioCntlrMethod; // register callback + ret = GpioCntlrAdd(gpioCntlr); + if (ret != HDF_SUCCESS) { + HDF_LOGE("GpioCntlrAdd fail %d\r\n", gpioCntlr->start); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static void GpioDriverRelease(struct HdfDeviceObject *device) +{ + struct GpioCntlr *gpioCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return; + } + + gpioCntlr = GpioCntlrFromHdfDev(device); + if (gpioCntlr == NULL) { + HDF_LOGE("GpioCntlrFromHdfDev fail\r\n"); + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + (void)OsalMemFree(gpioCntlr->priv); + gpioCntlr->count = 0; +} + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_GpioDriverEntry = { + .moduleVersion = 1, + .moduleName = "ASR_GPIO_MODULE_HDF", + .Init = GpioDriverInit, + .Release = GpioDriverRelease, +}; +HDF_INIT(g_GpioDriverEntry); diff --git a/platform/gpio/gpio_bes.c b/platform/gpio/gpio_bes.c old mode 100644 new mode 100755 index 4d1cc8fd730b3376cfffdf7cb7f80dbc41182c0a..5f93f7caa79b27b6e14b057c005b19e816375c3d --- a/platform/gpio/gpio_bes.c +++ b/platform/gpio/gpio_bes.c @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include #include "gpio_core.h" #include "hal_gpio.h" @@ -75,26 +69,10 @@ typedef int32_t (*oem_gpio_irq_handler_t)(uint16_t gpio, void *data); #define OCTALNUM 8 static struct GpioCntlr g_gpioCntlr; -struct OemGpioIrqHandler { - uint8_t port; - GpioIrqFunc func; - void *arg; -}; enum HAL_GPIO_PIN_T g_gpioPinReflectionMap[HAL_GPIO_PIN_LED_NUM] = {0}; -static struct HAL_GPIO_IRQ_CFG_T g_gpioIrqCfg[HAL_GPIO_PIN_LED_NUM] = {0}; -static struct HAL_GPIO_IRQ_CFG_T HalGpioGetIrqConfig(enum HAL_GPIO_PIN_T pin) -{ - struct HAL_GPIO_IRQ_CFG_T irqCfg; - - irqCfg.irq_enable = g_gpioIrqCfg[pin].irq_enable; - irqCfg.irq_debounce = g_gpioIrqCfg[pin].irq_debounce; - irqCfg.irq_type = g_gpioIrqCfg[pin].irq_type; - irqCfg.irq_polarity = g_gpioIrqCfg[pin].irq_polarity; - - return irqCfg; -} +static struct HAL_GPIO_IRQ_CFG_T g_gpioIrqCfg[HAL_GPIO_PIN_LED_NUM] = {0}; static void OemGpioIrqHdl(enum HAL_GPIO_PIN_T pin) { @@ -277,12 +255,10 @@ static uint32_t GetGpioDeviceResource( return HDF_FAILURE; } } - return HDF_SUCCESS; } #endif - static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObject *device) { int32_t ret; @@ -308,12 +284,12 @@ static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObj ret = GetGpioDeviceResource(gpioDevice, device->property); #endif if (ret != HDF_SUCCESS) { - (void)OsalMemFree(gpioDevice); + OsalMemFree(gpioDevice); return HDF_FAILURE; } gpioCntlr->count = gpioDevice->resource.pinNum; - + gpioCntlr->priv = (void *)gpioDevice; return HDF_SUCCESS; } @@ -369,7 +345,8 @@ static void GpioDriverRelease(struct HdfDeviceObject *device) return HDF_DEV_ERR_NO_DEVICE_SERVICE; } - (void)OsalMemFree(gpioCntlr->priv); + gpioCntlr->ops = NULL; + OsalMemFree(gpioCntlr->priv); gpioCntlr->count = 0; } @@ -394,7 +371,7 @@ static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val uint16_t value; uint16_t halGpio = g_gpioPinReflectionMap[gpio]; if ((enum HAL_GPIO_PIN_T)halGpio >= HAL_GPIO_PIN_LED_NUM) { - HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, halGpio); + HDF_LOGE("%s %d, error pin:%hu", __func__, __LINE__, halGpio); return HDF_ERR_NOT_SUPPORT; } @@ -409,7 +386,7 @@ static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t di (void)cntlr; uint16_t halGpio = g_gpioPinReflectionMap[gpio]; if ((enum HAL_GPIO_PIN_T)halGpio >= HAL_GPIO_PIN_LED_NUM) { - HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, halGpio); + HDF_LOGE("%s %d, error pin:%hu", __func__, __LINE__, halGpio); return HDF_ERR_NOT_SUPPORT; } @@ -424,7 +401,7 @@ static int32_t GpioDevGetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *d uint16_t value; uint16_t halGpio = g_gpioPinReflectionMap[gpio]; if ((enum HAL_GPIO_PIN_T)halGpio >= HAL_GPIO_PIN_LED_NUM) { - HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, gpio); + HDF_LOGE("%s %d, error pin:%hu", __func__, __LINE__, gpio); return HDF_ERR_NOT_SUPPORT; } @@ -448,10 +425,16 @@ static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mo } else if ((mode == OSAL_IRQF_TRIGGER_HIGH) || (mode == OSAL_IRQF_TRIGGER_LOW)) { g_gpioIrqCfg[pin].irq_type = HAL_GPIO_IRQ_TYPE_LEVEL_SENSITIVE; } else { - HDF_LOGE("%s %d, error mode:%d", __func__, __LINE__, mode); + HDF_LOGE("%s %d, error mode:%hu", __func__, __LINE__, mode); return HDF_ERR_NOT_SUPPORT; } + if (mode == OSAL_IRQF_TRIGGER_HIGH || mode == OSAL_IRQF_TRIGGER_RISING) { + mode = HAL_GPIO_IRQ_POLARITY_HIGH_RISING; + } else { + mode = HAL_GPIO_IRQ_POLARITY_LOW_FALLING; + } + g_gpioIrqCfg[pin].irq_polarity = mode; return HDF_SUCCESS; diff --git a/platform/gpio/gpio_gr5xx.c b/platform/gpio/gpio_gr5xx.c index 1ce446e7f78a0c3a3f5f3388362e7791999fc633..57463a43195c19d597fab640db8886f4a42b1ca1 100644 --- a/platform/gpio/gpio_gr5xx.c +++ b/platform/gpio/gpio_gr5xx.c @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 GOODIX. - * 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 + * Copyright (c) 2021-2022 GOODIX. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "app_io.h" #include "app_gpiote.h" #include "gpio/gpio_core.h" diff --git a/platform/gpio/gpio_stm32f4xx.c b/platform/gpio/gpio_stm32f4xx.c new file mode 100644 index 0000000000000000000000000000000000000000..60b4664073f35617161dfa3daf8f504f6eabfc51 --- /dev/null +++ b/platform/gpio/gpio_stm32f4xx.c @@ -0,0 +1,604 @@ +/* + * Copyright (c) 2022 Talkweb Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include "hal_gpio.h" +#include "hal_exti.h" +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#endif +#include "gpio_core.h" +#include "hdf_log.h" +#include "osal_irq.h" + +#define HDF_LOG_TAG gpio_stm_c + +static const uint16_t g_stmRealPinMaps[STM32_GPIO_PIN_MAX] = { + LL_GPIO_PIN_0, + LL_GPIO_PIN_1, + LL_GPIO_PIN_2, + LL_GPIO_PIN_3, + LL_GPIO_PIN_4, + LL_GPIO_PIN_5, + LL_GPIO_PIN_6, + LL_GPIO_PIN_7, + LL_GPIO_PIN_8, + LL_GPIO_PIN_9, + LL_GPIO_PIN_10, + LL_GPIO_PIN_11, + LL_GPIO_PIN_12, + LL_GPIO_PIN_13, + LL_GPIO_PIN_14, + LL_GPIO_PIN_15, +}; + +typedef struct { + uint32_t group; + uint32_t realPin; + uint32_t pin; +} GpioInflectInfo; + +GpioInflectInfo g_gpioPinsMap[STM32_GPIO_PIN_MAX * STM32_GPIO_GROUP_MAX] = {0}; + +static const GPIO_TypeDef* g_gpioxMaps[STM32_GPIO_GROUP_MAX] = { + GPIOA, + GPIOB, + GPIOC, + GPIOD, + GPIOE, + GPIOF, + GPIOG, + GPIOH, + GPIOI, +}; + +static const uint32_t g_gpioExitLineMap[STM32_GPIO_PIN_MAX] = { + LL_EXTI_LINE_0, + LL_EXTI_LINE_1, + LL_EXTI_LINE_2, + LL_EXTI_LINE_3, + LL_EXTI_LINE_4, + LL_EXTI_LINE_5, + LL_EXTI_LINE_6, + LL_EXTI_LINE_7, + LL_EXTI_LINE_8, + LL_EXTI_LINE_9, + LL_EXTI_LINE_10, + LL_EXTI_LINE_11, + LL_EXTI_LINE_12, + LL_EXTI_LINE_13, + LL_EXTI_LINE_14, + LL_EXTI_LINE_15, +}; + +typedef struct { + uint32_t pin; + uint32_t realPin; + uint32_t mode; + uint32_t group; + uint32_t pull; + uint32_t speed; + uint32_t outputType; + uint32_t alternate; +} GpioResource; + +enum GpioDeviceState { + GPIO_DEVICE_UNINITIALIZED = 0x0u, + GPIO_DEVICE_INITIALIZED = 0x1u, +}; + +typedef struct { + uint32_t pinNums; + GpioResource resource; + STM32_GPIO_GROUP group; /* gpio config */ +} GpioDevice; + +static struct GpioCntlr g_stmGpioCntlr; + +static HAL_GPIO_EXIT_CFG_T g_gpioExitCfg[STM32_GPIO_PIN_MAX * STM32_GPIO_GROUP_MAX] = {0}; + +static void OemGpioIrqHdl(uint32_t pin) +{ + GpioCntlrIrqCallback(&g_stmGpioCntlr, pin); + return; +} + +/* HdfDriverEntry method definitions */ +static int32_t GpioDriverInit(struct HdfDeviceObject *device); +static void GpioDriverRelease(struct HdfDeviceObject *device); + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_GpioDriverEntry = { + .moduleVersion = 1, + .moduleName = "ST_GPIO_MODULE_HDF", + .Init = GpioDriverInit, + .Release = GpioDriverRelease, +}; +HDF_INIT(g_GpioDriverEntry); + +/* GpioMethod method definitions */ +static int32_t GpioDevWrite(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t val); +static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val); +static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t dir); +static int32_t GpioDevGetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *dir); +static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mode); +static int32_t GpioDevUnSetIrq(struct GpioCntlr *cntlr, uint16_t gpio); +static int32_t GpioDevEnableIrq(struct GpioCntlr *cntlr, uint16_t gpio); +static int32_t GpioDevDisableIrq(struct GpioCntlr *cntlr, uint16_t gpio); + +/* GpioMethod definitions */ +struct GpioMethod g_GpioCntlrMethod = { + .request = NULL, + .release = NULL, + .write = GpioDevWrite, + .read = GpioDevRead, + .setDir = GpioDevSetDir, + .getDir = GpioDevGetDir, + .toIrq = NULL, + .setIrq = GpioDevSetIrq, + .unsetIrq = GpioDevUnSetIrq, + .enableIrq = GpioDevEnableIrq, + .disableIrq = GpioDevDisableIrq, +}; + +static void InitGpioClock(STM32_GPIO_GROUP group) +{ + switch (group) { + case STM32_GPIO_GROUP_A: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA); + break; + case STM32_GPIO_GROUP_B: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB); + break; + case STM32_GPIO_GROUP_C: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC); + break; + case STM32_GPIO_GROUP_D: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD); + break; + case STM32_GPIO_GROUP_E: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOE); + break; + case STM32_GPIO_GROUP_F: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF); + break; + case STM32_GPIO_GROUP_G: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOG); + break; + case STM32_GPIO_GROUP_H: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOH); + break; + case STM32_GPIO_GROUP_I: + LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOI); + break; + default: + break; + } +} + +static int32_t InitGpioDevice(GpioDevice* device) +{ + LL_GPIO_InitTypeDef gpioInitStruct = {0}; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + uint32_t halGpio = g_stmRealPinMaps[device->resource.realPin]; + if (halGpio > LL_GPIO_PIN_15 || halGpio < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, halGpio); + return HDF_ERR_NOT_SUPPORT; + } + + /* init clock */ + InitGpioClock(device->resource.group); + + GPIO_TypeDef* goiox = g_gpioxMaps[device->resource.group]; + if (device->resource.mode & LL_GPIO_MODE_OUTPUT) { + LL_GPIO_ResetOutputPin(goiox, halGpio); + } + + gpioInitStruct.Pin = halGpio; + gpioInitStruct.Mode = device->resource.mode; + gpioInitStruct.Pull = device->resource.pull; + gpioInitStruct.Speed = device->resource.speed; + gpioInitStruct.OutputType = device->resource.outputType; + gpioInitStruct.Alternate = device->resource.alternate; + LL_GPIO_Init(goiox, &gpioInitStruct); + + return HDF_SUCCESS; +} + +#ifndef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +static int32_t GpioParseHcs(const struct DeviceResourceIface *dri, + GpioDevice *device, const struct DeviceResourceNode *resourceNode) +{ + GpioResource *resource = NULL; + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + if (dri->GetUint32(resourceNode, "pinNum", &device->pinNums, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read pinNum fail"); + return HDF_FAILURE; + } + + for (size_t i = 0; i < device->pinNums; i++) { + if (dri->GetUint32ArrayElem(resourceNode, "pin", i, &resource->pin, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "realPin", i, &resource->realPin, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "mode", i, &resource->mode, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "speed", i, &resource->speed, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "pull", i, &resource->pull, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "output", i, &resource->outputType, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "group", i, &resource->group, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32ArrayElem(resourceNode, "alternate", i, &resource->alternate, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + g_gpioPinsMap[resource->pin].group = resource->group; + g_gpioPinsMap[resource->pin].realPin = resource->realPin; + g_gpioPinsMap[resource->pin].pin = resource->pin; + + if (InitGpioDevice(device) != HDF_SUCCESS) { + HDF_LOGE("InitGpioDevice FAIL\r\n"); + return HDF_FAILURE; + } + } + + return HDF_SUCCESS; +} +#endif + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define PLATFORM_GPIO_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), gpio_config) +static uint32_t GetGpioDeviceResource(GpioDevice *device) +{ + uint32_t relPin; + int32_t ret; + GpioResource *resource = NULL; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + device->pinNums = HCS_PROP(PLATFORM_GPIO_CONFIG, pinNum); + uint32_t pins[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, pin)); + uint32_t realPins[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, realPin)); + uint32_t groups[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, group)); + uint32_t modes[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, mode)); + uint32_t speeds[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, speed)); + uint32_t pulls[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, pull)); + uint32_t outputs[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, output)); + uint32_t alternates[] = HCS_ARRAYS(HCS_NODE(PLATFORM_GPIO_CONFIG, alternate)); + for (size_t i = 0; i < device->pinNums; i++) { + resource->pin = pins[i]; + resource->realPin = realPins[i]; + resource->group = groups[i]; + resource->mode = modes[i]; + resource->speed = speeds[i]; + resource->pull = pulls[i]; + resource->outputType = outputs[i]; + resource->alternate = alternates[i]; + g_gpioPinsMap[resource->pin].group = resource->group; + g_gpioPinsMap[resource->pin].realPin = resource->realPin; + g_gpioPinsMap[resource->pin].pin = resource->pin; + + if (InitGpioDevice(device) != HDF_SUCCESS) { + HDF_LOGE("InitGpioDevice FAIL\r\n"); + return HDF_FAILURE; + } + } + + return HDF_SUCCESS; +} +#else +static int32_t GetGpioDeviceResource(GpioDevice *device, const struct DeviceResourceNode *resourceNode) +{ + int32_t ret; + struct DeviceResourceIface *dri = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid"); + return HDF_ERR_INVALID_OBJECT; + } + + if (GpioParseHcs(dri, device, resourceNode) != HDF_SUCCESS) { + HDF_LOGE("gpio config parse hcs fail"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} +#endif + +static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + + GpioDevice *gpioDevice = NULL; +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL) { +#else + if (device == NULL || device->property == NULL) { +#endif + HDF_LOGE("%s: property is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + gpioDevice = (GpioDevice *)OsalMemAlloc(sizeof(GpioDevice)); + if (gpioDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc gpioDevice error", __func__); + return HDF_ERR_MALLOC_FAIL; + } + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetGpioDeviceResource(gpioDevice); +#else + ret = GetGpioDeviceResource(gpioDevice, device->property); +#endif + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(gpioDevice); + return HDF_FAILURE; + } + gpioCntlr->priv = gpioDevice; + gpioCntlr->count = gpioDevice->pinNums; + + return HDF_SUCCESS; +} + +static int32_t GpioDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct GpioCntlr *gpioCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = PlatformDeviceBind(&g_stmGpioCntlr.device, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: bind hdf device failed:%d", __func__, ret); + return ret; + } + + gpioCntlr = GpioCntlrFromHdfDev(device); + if (gpioCntlr == NULL) { + HDF_LOGE("GpioCntlrFromHdfDev fail\r\n"); + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + ret = AttachGpioDevice(gpioCntlr, device); /* GpioCntlr add GpioDevice to priv */ + if (ret != HDF_SUCCESS) { + HDF_LOGE("AttachGpioDevice fail\r\n"); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + gpioCntlr->ops = &g_GpioCntlrMethod; /* register callback */ + ret = GpioCntlrAdd(gpioCntlr); + if (ret != HDF_SUCCESS) { + HDF_LOGE("GpioCntlrAdd fail %d\r\n", gpioCntlr->start); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDriverBind(struct HdfDeviceObject *device) +{ + if (device == NULL) { + HDF_LOGE("device object is NULL\n"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static void GpioDriverRelease(struct HdfDeviceObject *device) +{ + struct GpioCntlr *gpioCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return; + } + + gpioCntlr = GpioCntlrFromHdfDev(device); + if (gpioCntlr == NULL) { + HDF_LOGE("%s: host is NULL", __func__); + return; + } + + gpioCntlr->count = 0; +} + +/* dev api */ +static int32_t GpioDevWrite(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t val) +{ + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + HDF_LOGE("%s %d ,write pin num %d", __func__, __LINE__, realPin); + GPIO_TypeDef* gpiox = g_gpioxMaps[g_gpioPinsMap[gpio].group]; + if (val) { + LL_GPIO_SetOutputPin(gpiox, pinReg); + } else { + LL_GPIO_ResetOutputPin(gpiox, pinReg); + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val) +{ + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + uint16_t value = 0; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + + GPIO_TypeDef* gpiox = g_gpioxMaps[g_gpioPinsMap[gpio].group]; + value = LL_GPIO_ReadInputPin(gpiox, pinReg); + *val = value; + + return HDF_SUCCESS; +} + +static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t dir) +{ + (void)cntlr; + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + uint16_t value = 0; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + GPIO_TypeDef* gpiox = g_gpioxMaps[g_gpioPinsMap[gpio].group]; + LL_GPIO_SetPinMode(gpiox, pinReg, dir); + + return HDF_SUCCESS; +} + +static int32_t GpioDevGetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *dir) +{ + (void)cntlr; + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + uint16_t value = 0; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + GPIO_TypeDef* gpiox = g_gpioxMaps[g_gpioPinsMap[gpio].group]; + value = LL_GPIO_GetPinMode(gpiox, pinReg); + *dir = value; + + return HDF_SUCCESS; +} + +static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mode) +{ + (void)cntlr; + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + + if (mode == OSAL_IRQF_TRIGGER_RISING) { + g_gpioExitCfg[gpio].trigger = LL_EXTI_TRIGGER_RISING; + } else if (mode == OSAL_IRQF_TRIGGER_FALLING) { + g_gpioExitCfg[gpio].trigger = LL_EXTI_TRIGGER_FALLING; + } else { + HDF_LOGE("%s %d, error mode:%d", __func__, __LINE__, mode); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevUnSetIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevEnableIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + LL_EXTI_InitConfig exitInitConfig = {0}; + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + + exitInitConfig.Exithandler = OemGpioIrqHdl; + exitInitConfig.Gpiox = g_gpioxMaps[g_gpioPinsMap[gpio].group]; + exitInitConfig.initType.Line_0_31 = g_gpioExitLineMap[g_gpioPinsMap[gpio].realPin]; + exitInitConfig.initType.LineCommand = ENABLE; + exitInitConfig.initType.Mode = LL_EXTI_MODE_IT; + exitInitConfig.PinReg = pinReg; + exitInitConfig.initType.Trigger = g_gpioExitCfg[gpio].trigger; + + LL_SETUP_EXTI(&exitInitConfig, g_gpioPinsMap[gpio].realPin, gpio, g_gpioPinsMap[gpio].group); + + return HDF_SUCCESS; +} + +static int32_t GpioDevDisableIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + LL_EXTI_InitConfig exitInitConfig = {0}; + uint16_t realPin = g_gpioPinsMap[gpio].realPin; + uint32_t pinReg = g_stmRealPinMaps[realPin]; + if (pinReg > LL_GPIO_PIN_15 || pinReg < LL_GPIO_PIN_0) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, realPin); + return HDF_ERR_NOT_SUPPORT; + } + + exitInitConfig.Exithandler = NULL; + exitInitConfig.Gpiox = g_gpioxMaps[g_gpioPinsMap[gpio].group]; + exitInitConfig.initType.Line_0_31 = g_gpioExitLineMap[g_gpioPinsMap[gpio].realPin]; + exitInitConfig.initType.LineCommand = DISABLE; + exitInitConfig.initType.Mode = LL_EXTI_MODE_IT; + exitInitConfig.PinReg = pinReg; + exitInitConfig.initType.Trigger = g_gpioExitCfg[gpio].trigger; + LL_SETUP_EXTI(&exitInitConfig, g_gpioPinsMap[gpio].realPin, gpio, g_gpioPinsMap[gpio].group); + + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/platform/gpio/gpio_wm.c b/platform/gpio/gpio_wm.c new file mode 100755 index 0000000000000000000000000000000000000000..4161e7403631c0321bf17c1dc6166f39738bd444 --- /dev/null +++ b/platform/gpio/gpio_wm.c @@ -0,0 +1,376 @@ +/* + * Copyright (c) 2022 Jiangsu Hoperun Software Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include "gpio_core.h" +#include "gpio_if.h" +#include "device_resource_if.h" +#include "osal_irq.h" +#include "hdf_log.h" +#include "wm_io.h" +#include "wm_gpio.h" + +#define DECIMALNUM 10 +#define OCTALNUM 8 + +#define HDF_LOG_TAG gpioDriver +#define WM_IO_MAX_GPIO_PIN_NUM 48 + +/* + * Pin configuration + */ +enum GPIO_CONFIG { + ANALOG_MODE, /* Used as a function pin, input and output analog */ + IRQ_MODE, /* Used to trigger interrupt */ + INPUT_PULL_UP, /* Input with an internal pull-up resistor - use with devices + that actively drive the signal low - e.g. button connected to ground */ + INPUT_PULL_DOWN, /* Input with an internal pull-down resistor - use with devices + that actively drive the signal high - e.g. button connected to a power rail */ + INPUT_HIGH_IMPEDANCE, /* Input - must always be driven, either actively or by an external pullup resistor */ + OUTPUT_PUSH_PULL, /* Output actively driven high and actively driven low - + must not be connected to other active outputs - e.g. LED output */ + OUTPUT_OPEN_DRAIN_NO_PULL, /* Output actively driven low but is high-impedance when set high - + can be connected to other open-drain/open-collector outputs. + Needs an external pull-up resistor */ + OUTPUT_OPEN_DRAIN_PULL_UP, /* Output actively driven low and is pulled high + with an internal resistor when set high - + can be connected to other open-drain/open-collector outputs. */ +}; + +struct GpioResource { + uint32_t groupNum; + uint32_t realPin; + uint32_t config; + uint32_t pinNum; +}; + +struct GpioDevice { + uint8_t port; /* gpio port */ + struct GpioResource resource; + enum GPIO_CONFIG config; /* gpio config */ +}; + +typedef void (* tls_gpio_pin_orq_handler)(enum tls_io_name pin); + +static struct GpioCntlr gpioCntlr; +struct wmGpioIrqHandler { + uint8_t port; +}; + +enum tls_io_name g_gpioPinReflectionMap[WM_IO_MAX_GPIO_PIN_NUM] = {0}; +static struct wmGpioIrqHandler g_wmGpioIrqHandler[WM_IO_MAX_GPIO_PIN_NUM] = {0}; +enum tls_gpio_irq_trig g_gpioIrqCfg[WM_IO_MAX_GPIO_PIN_NUM] = {0}; + + +GpioIrqFunc GpioIrqHdl() +{ + uint16_t ret; + + for (size_t i = 0; i < WM_IO_MAX_GPIO_PIN_NUM; i++) { + ret = tls_get_gpio_irq_status(g_wmGpioIrqHandler[i].port); + if ((enum tls_io_name)g_gpioPinReflectionMap[i] && (ret != 0)) { + GpioCntlrIrqCallback(&gpioCntlr, i); + return HDF_SUCCESS; + } + } +} + +/* dispatch */ +int32_t GpioDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + if (client == NULL || client->device == NULL || data == NULL || reply == NULL) { + HDF_LOGE("%s: client or client->device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + return HDF_SUCCESS; +} + +/* GpioMethod method definitions */ +static int32_t GpioDevWrite(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t val); +static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val); +static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t dir); +static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mode); +static int32_t GpioDevUnSetIrq(struct GpioCntlr *cntlr, uint16_t gpio); +static int32_t GpioDevEnableIrq(struct GpioCntlr *cntlr, uint16_t gpio); +static int32_t GpioDevDisableIrq(struct GpioCntlr *cntlr, uint16_t gpio); +/* GpioMethod definitions */ +struct GpioMethod g_GpioCntlrMethod = { + .request = NULL, + .release = NULL, + .write = GpioDevWrite, + .read = GpioDevRead, + .setDir = GpioDevSetDir, + .getDir = NULL, + .toIrq = NULL, + .setIrq = GpioDevSetIrq, + .unsetIrq = GpioDevUnSetIrq, + .enableIrq = GpioDevEnableIrq, + .disableIrq = GpioDevDisableIrq, +}; + +/* dev api */ +static int32_t GpioDevWrite(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t val) +{ + (void)cntlr; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error wmGpio:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + + tls_gpio_write((enum tls_io_name)wmGpio, val); + return HDF_SUCCESS; +} + +static int32_t GpioDevRead(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t *val) +{ + (void)cntlr; + uint16_t value; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error wmGpio:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + + value = tls_gpio_read((enum tls_io_name)wmGpio); + *val = value; + return HDF_SUCCESS; +} + +static int32_t GpioDevSetDir(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t dir) +{ + (void)cntlr; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error wmGpio:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + + switch (dir) { + case GPIO_DIR_OUT: + dir = WM_GPIO_DIR_OUTPUT; + break; + case GPIO_DIR_IN: + dir = WM_GPIO_DIR_INPUT; + break; + } + + tls_gpio_cfg((enum tls_io_name)wmGpio, (enum tls_gpio_dir)dir, WM_GPIO_ATTR_FLOATING); + + return HDF_SUCCESS; +} + +static int32_t GpioDevSetIrq(struct GpioCntlr *cntlr, uint16_t gpio, uint16_t mode) +{ + (void)cntlr; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error wmGpio:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + + g_wmGpioIrqHandler[wmGpio].port = gpio; + tls_gpio_isr_register((enum tls_io_name)wmGpio, (tls_gpio_irq_callback)GpioIrqHdl, NULL); + + switch (mode) { + case OSAL_IRQF_TRIGGER_RISING: + g_gpioIrqCfg[wmGpio] = WM_GPIO_IRQ_TRIG_RISING_EDGE; + break; + case OSAL_IRQF_TRIGGER_FALLING: + g_gpioIrqCfg[wmGpio] = WM_GPIO_IRQ_TRIG_FALLING_EDGE; + break; + case OSAL_IRQF_TRIGGER_HIGH: + g_gpioIrqCfg[wmGpio] = WM_GPIO_IRQ_TRIG_HIGH_LEVEL; + break; + case OSAL_IRQF_TRIGGER_LOW: + g_gpioIrqCfg[wmGpio] = WM_GPIO_IRQ_TRIG_LOW_LEVEL; + break; + } + + return HDF_SUCCESS; +} + +static int32_t GpioDevUnSetIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error wmGpio:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + return HDF_SUCCESS; +} + +static int32_t GpioDevEnableIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + + // config gpio interrupt + tls_gpio_irq_enable((enum tls_io_name)wmGpio, g_gpioIrqCfg[wmGpio]); + return HDF_SUCCESS; +} + +static int32_t GpioDevDisableIrq(struct GpioCntlr *cntlr, uint16_t gpio) +{ + (void)cntlr; + uint16_t wmGpio = g_gpioPinReflectionMap[gpio]; + if ((enum tls_io_name)wmGpio > WM_IO_MAX_GPIO_PIN_NUM) { + HDF_LOGE("%s %d, error pin:%d", __func__, __LINE__, wmGpio); + return HDF_ERR_NOT_SUPPORT; + } + + tls_gpio_irq_disable((enum tls_io_name)wmGpio); + return HDF_SUCCESS; +} + +static uint32_t GetGpioDeviceResource(struct GpioDevice *device, + const struct DeviceResourceNode *resourceNode) +{ + uint32_t relPin; + int32_t ret; + struct GpioResource *resource = NULL; + struct DeviceResourceIface *dri = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid"); + return HDF_ERR_INVALID_OBJECT; + } + + if (dri->GetUint32(resourceNode, "groupNum", &resource->groupNum, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read groupNum fail"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "pinNum", &resource->pinNum, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read pinNum fail"); + return HDF_FAILURE; + } + + for (size_t i = 0; i < resource->groupNum; i++) { + if (dri->GetUint32ArrayElem(resourceNode, "config", i, &resource->config, 0) != HDF_SUCCESS) { + HDF_LOGE("gpio config read config fail"); + return HDF_FAILURE; + } + } + device->config = resource->config; + return HDF_SUCCESS; +} + +static int32_t AttachGpioDevice(struct GpioCntlr *gpioCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + + struct GpioDevice *gpioDevice = NULL; + if (device == NULL || device->property == NULL) { + HDF_LOGE("%s: property is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + gpioDevice = (struct GpioDevice *)OsalMemAlloc(sizeof(struct GpioDevice)); + if (gpioDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc gpioDevice error", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = GetGpioDeviceResource(gpioDevice, device->property); + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(gpioDevice); + return HDF_FAILURE; + } + + gpioCntlr->count = gpioDevice->resource.pinNum; + + return HDF_SUCCESS; +} + +static int32_t GpioDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct GpioCntlr *gpioCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + gpioCntlr = GpioCntlrFromHdfDev(device); + if (gpioCntlr == NULL) { + HDF_LOGE("GpioCntlrFromHdfDev fail\r\n"); + return HDF_DEV_ERR_NO_DEVICE_SERVICE; + } + + ret = AttachGpioDevice(gpioCntlr, device); // GpioCntlr add GpioDevice to priv + if (ret != HDF_SUCCESS) { + HDF_LOGE("AttachGpioDevice fail\r\n"); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + gpioCntlr->ops = &g_GpioCntlrMethod; // register callback + ret = GpioCntlrAdd(gpioCntlr); + if (ret != HDF_SUCCESS) { + HDF_LOGE("GpioCntlrAdd fail %d\r\n", gpioCntlr->start); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t GpioDriverBind(struct HdfDeviceObject *device) +{ + if (device == NULL) { + HDF_LOGE("Sample device object is null!"); + return HDF_ERR_INVALID_PARAM; + } + + gpioCntlr.device.hdfDev = device; + device->service = gpioCntlr.device.hdfDev; + + return HDF_SUCCESS; +} + +static void GpioDriverRelease(struct HdfDeviceObject *device) +{ + struct GpioCntlr *gpioCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return; + } + + gpioCntlr = GpioCntlrFromHdfDev(device); + if (gpioCntlr == NULL) { + HDF_LOGE("%s: host is NULL", __func__); + return; + } + + gpioCntlr->ops = NULL; + OsalMemFree(gpioCntlr); +} + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_GpioDriverEntry = { + .moduleVersion = 1, + .moduleName = "WM_GPIO_MODULE_HDF", + .Bind = GpioDriverBind, + .Init = GpioDriverInit, + .Release = GpioDriverRelease, +}; +HDF_INIT(g_GpioDriverEntry); diff --git a/platform/i2c/BUILD.gn b/platform/i2c/BUILD.gn index 9db0f785b96b387b5888cfe5d01350bbfa52781e..7007f7d27c4c5932d86ddbff65a25f71bc21549f 100644 --- a/platform/i2c/BUILD.gn +++ b/platform/i2c/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") @@ -37,10 +16,26 @@ hdf_driver(module_name) { if (defined(LOSCFG_SOC_COMPANY_BESTECHNIC)) { sources += [ "i2c_bes.c" ] } + if (defined(LOSCFG_SOC_COMPANY_WINNERMICRO)) { + sources += [ "i2c_wm.c" ] + } + if (defined(LOSCFG_SOC_SERIES_STM32F4xx)) { + sources += [ "i2c_stm32f4xx.c" ] + } include_dirs = [ "." ] if (defined(LOSCFG_SHIELD_V200ZR_EVB_T1) && defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { deps = [ "//device/board/fnlink/shields" ] } + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/platform/i2c/i2c_bes.c b/platform/i2c/i2c_bes.c index 9ffa5b1fbabde2324796340e650e77361fbce625..39597f6601333a40bbffe095b5006c4ee704a9d2 100755 --- a/platform/i2c/i2c_bes.c +++ b/platform/i2c/i2c_bes.c @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "i2c_bes.h" #include #include @@ -54,25 +48,9 @@ struct I2cMethod g_i2cHostMethod = { .transfer = i2cHostTransfer, }; -int32_t InitI2cDevice(struct I2cDevice *device) +static void I2cDeviceIomuxInit(uint32_t i2cId, struct I2cResource *resource) { - int32_t ret = -1; - uint32_t i2cPort; - struct I2cResource *resource = NULL; - struct HAL_I2C_CONFIG_T *i2cConfig = NULL; - if (device == NULL) { - HDF_LOGE("device is NULL\r\n"); - return HDF_ERR_INVALID_PARAM; - } - - resource = &device->resource; - if (resource == NULL) { - HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_PARAM; - } - - i2cConfig = &device->i2cCfg; - if (i2cConfig == NULL) { + if (i2cId > HAL_I2C_ID_NUM || resource == NULL) { HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); return HDF_ERR_INVALID_PARAM; } @@ -82,63 +60,51 @@ int32_t InitI2cDevice(struct I2cDevice *device) {0, 0, HAL_IOMUX_PIN_VOLTAGE_VIO, HAL_IOMUX_PIN_PULLUP_ENABLE}, }; - device->port = resource->port; - i2cPort = device->port; - if (i2cPort > HAL_I2C_ID_NUM) { - HDF_LOGE("i2c port %u not support\r\n", i2cPort); - return HDF_ERR_NOT_SUPPORT; - } - - if (i2cPort == 0) { +#ifdef LOSCFG_SOC_SERIES_BES2600 + if (i2cId == 0) { pinMuxI2c[0].function = HAL_IOMUX_FUNC_I2C_M0_SCL; pinMuxI2c[1].function = HAL_IOMUX_FUNC_I2C_M0_SDA; } else { pinMuxI2c[0].function = HAL_IOMUX_FUNC_I2C_M1_SCL; pinMuxI2c[1].function = HAL_IOMUX_FUNC_I2C_M1_SDA; } - - if (OsalMutexInit(&device->mutex) != HDF_SUCCESS) { - HDF_LOGE("%s %d OsalMutexInit fail\r\n", __func__, __LINE__); - return HDF_FAILURE; - } - - if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { - HDF_LOGE("%s %d osMutexWait fail\r\n", __func__, __LINE__); - return HDF_ERR_TIMEOUT; +#elif defined (LOSCFG_SOC_SERIES_BES2700) + if (i2cId == 0) { + pinMuxI2c[0].function = HAL_IOMUX_FUNC_SYS_I2C_M0_SCL; + pinMuxI2c[1].function = HAL_IOMUX_FUNC_SYS_I2C_M0_SDA; + } else { + pinMuxI2c[0].function = HAL_IOMUX_FUNC_SYS_I2C_M1_SCL; + pinMuxI2c[1].function = HAL_IOMUX_FUNC_SYS_I2C_M1_SDA; } - +#endif pinMuxI2c[0].pin = resource->sclPin; pinMuxI2c[1].pin = resource->sdaPin; hal_iomux_init(pinMuxI2c, ARRAY_SIZE(pinMuxI2c)); - - ret = hal_i2c_open(i2cPort, i2cConfig); - if (ret == HDF_SUCCESS) { - HDF_LOGD("open %u i2c succ.\r\n", i2cPort); - } - OsalMutexUnlock(&device->mutex); - return ret; } -static int32_t HostRestI2cDevice(struct I2cDevice *device) +int32_t InitI2cDevice(struct I2cDevice *device) { - int32_t ret = -1; + int32_t ret; + uint32_t i2cPort; struct I2cResource *resource = NULL; struct HAL_I2C_CONFIG_T *i2cConfig = NULL; - uint32_t i2cPort; if (device == NULL) { - HDF_LOGE("%s %d device is null\r\n", __func__, __LINE__); + HDF_LOGE("device is NULL\r\n"); return HDF_ERR_INVALID_PARAM; } + resource = &device->resource; if (resource == NULL) { HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_OBJECT; + return HDF_ERR_INVALID_PARAM; } + i2cConfig = &device->i2cCfg; if (i2cConfig == NULL) { HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_OBJECT; + return HDF_ERR_INVALID_PARAM; } + device->port = resource->port; i2cPort = device->port; if (i2cPort > HAL_I2C_ID_NUM) { @@ -146,15 +112,28 @@ static int32_t HostRestI2cDevice(struct I2cDevice *device) return HDF_ERR_NOT_SUPPORT; } - hal_i2c_close(i2cPort); + if (OsalMutexInit(&device->mutex) != HDF_SUCCESS) { + HDF_LOGE("%s %d OsalMutexInit fail\r\n", __func__, __LINE__); + return HDF_FAILURE; + } + + if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { + HDF_LOGE("%s %d osMutexWait fail\r\n", __func__, __LINE__); + return HDF_ERR_TIMEOUT; + } + + I2cDeviceIomuxInit(i2cPort, resource); + ret = hal_i2c_open(i2cPort, i2cConfig); if (ret == HDF_SUCCESS) { HDF_LOGD("open %u i2c succ.\r\n", i2cPort); } + OsalMutexUnlock(&device->mutex); return ret; } + #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO -#define I2C_FIND_CONFIG(node, name, resource) \ +#define I2C_FIND_CONFIG(node, name, resource, result, tempPin) \ do { \ if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ resource->port = HCS_PROP(node, port); \ @@ -167,34 +146,35 @@ static int32_t HostRestI2cDevice(struct I2cDevice *device) resource->useDma = HCS_PROP(node, useDma); \ resource->useSync = HCS_PROP(node, useSync); \ resource->asMaster = HCS_PROP(node, asMaster); \ + result = HDF_SUCCESS; \ break; \ } \ } while (0) - #define PLATFORM_I2C_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), i2c_config) static uint32_t GetI2cDeviceResource(struct I2cDevice *device, const char *deviceMatchAttr) { - uint32_t tempPin = 0; + uint32_t tempPin; + int32_t result = HDF_FAILURE; struct I2cResource *resource = NULL; - if (device == NULL) { - HDF_LOGE("device or resourceNode is NULL\r\n"); + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL"); return HDF_ERR_INVALID_PARAM; } resource = &device->resource; - if (resource == NULL) { - HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); - return HDF_ERR_INVALID_OBJECT; +#if HCS_NODE_EXISTS(PLATFORM_I2C_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_I2C_CONFIG, I2C_FIND_CONFIG, deviceMatchAttr, resource, result, tempPin); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL", deviceMatchAttr); } - - HCS_FOREACH_CHILD_VARGS(PLATFORM_I2C_CONFIG, I2C_FIND_CONFIG, deviceMatchAttr, resource); - return HDF_SUCCESS; + return result; } #else static uint32_t GetI2cDeviceResource(struct I2cDevice *device, const struct DeviceResourceNode *resourceNode) { - uint32_t tempPin = 0; + uint32_t tempPin; struct I2cResource *resource = NULL; struct DeviceResourceIface *dri = NULL; if (device == NULL || resourceNode == NULL) { diff --git a/platform/i2c/i2c_bes.h b/platform/i2c/i2c_bes.h old mode 100755 new mode 100644 index b4c59455a21b022010d604a665a381ec7c73507a..404e8ff60c0cae6b752044989078ca4c890549d3 --- a/platform/i2c/i2c_bes.h +++ b/platform/i2c/i2c_bes.h @@ -1,16 +1,9 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ #ifndef _I2C_BES_H__ @@ -18,7 +11,9 @@ #include "hal_i2c.h" #include "hal_iomux.h" +#ifdef CHIP_BEST2003 #include "hal_iomux_best2003.h" +#endif #include "device_resource_if.h" #include "osal_mutex.h" #ifdef __cplusplus @@ -51,4 +46,4 @@ struct I2cDevice { } #endif -#endif \ No newline at end of file +#endif diff --git a/platform/i2c/i2c_stm32f4xx.c b/platform/i2c/i2c_stm32f4xx.c new file mode 100644 index 0000000000000000000000000000000000000000..f00f9d7ab9bc46592a84fd5254d027d685b6b509 --- /dev/null +++ b/platform/i2c/i2c_stm32f4xx.c @@ -0,0 +1,395 @@ +/* + * Copyright (c) 2022 Talkweb Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#endif +#include "hdf_device_desc.h" +#include "hdf_log.h" +#include "i2c_core.h" +#include "i2c_if.h" +#include "osal_mutex.h" +#include "hdf_base_hal.h" +#include "stm32f4xx_ll_i2c.h" + +#define HDF_LOG_TAG "hdf_i2c" + +typedef enum { + I2C_HANDLE_NULL = 0, + I2C_HANDLE_1 = 1, + I2C_HANDLE_2 = 2, + I2C_HANDLE_3 = 3, + I2C_HANDLE_MAX = I2C_HANDLE_3 +} I2C_HANDLE; + +struct RealI2cResource { + uint8_t port; + uint8_t devMode; + uint32_t devAddr; + uint32_t speed; + struct OsalMutex mutex; +}; + +static bool g_I2cEnableFlg[I2C_HANDLE_MAX] = {0}; + +static void HdfI2cInit(I2C_HANDLE i2cx, unsigned int i2cRate, unsigned int addr); +static void HdfI2cWrite(I2C_HANDLE i2cx, unsigned char devAddr, unsigned char *buf, unsigned int len); +static void HdfI2cRead(I2C_HANDLE i2cx, unsigned char devAddr, unsigned char *buf, unsigned int len); + +static int32_t I2cDriverBind(struct HdfDeviceObject *device); +static int32_t I2cDriverInit(struct HdfDeviceObject *device); +static void I2cDriverRelease(struct HdfDeviceObject *device); +static int32_t I2cDataTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count); + +struct HdfDriverEntry gI2cHdfDriverEntry = { + .moduleVersion = 1, + .moduleName = "HDF_I2C", + .Bind = I2cDriverBind, + .Init = I2cDriverInit, + .Release = I2cDriverRelease, +}; +HDF_INIT(gI2cHdfDriverEntry); + +struct I2cMethod gI2cHostMethod = { + .transfer = I2cDataTransfer, +}; + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define I2C_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->port = HCS_PROP(node, port); \ + resource->devMode = HCS_PROP(node, devMode); \ + resource->devAddr = HCS_PROP(node, devAddr); \ + resource->speed = HCS_PROP(node, speed); \ + result = HDF_SUCCESS; \ + } \ + } while (0) +#define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform) +#define PLATFORM_I2C_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), i2c_config) +static uint32_t GetI2cDeviceResource(struct RealI2cResource *i2cResource, const char *deviceMatchAttr) +{ + int32_t result = HDF_FAILURE; + struct RealI2cResource *resource = NULL; + if (i2cResource == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = i2cResource; +#if HCS_NODE_HAS_PROP(PLATFORM_CONFIG, i2c_config) + HCS_FOREACH_CHILD_VARGS(PLATFORM_I2C_CONFIG, I2C_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } else { + HdfI2cInit(i2cResource->port, i2cResource->speed, i2cResource->devAddr); + } + return result; +} +#else +static int32_t GetI2cDeviceResource(struct RealI2cResource *i2cResource, const struct DeviceResourceNode *resourceNode) +{ + if (i2cResource == NULL || resourceNode == NULL) { + HDF_LOGE("[%s]: param is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + struct DeviceResourceIface *dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint8 == NULL || dri->GetUint32 == NULL || dri->GetUint32Array == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + if (dri->GetUint8(resourceNode, "port", &i2cResource->port, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config port fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "devMode", &i2cResource->devMode, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config devMode fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "devAddr", &i2cResource->devAddr, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config devAddr fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "speed", &i2cResource->speed, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config speed fail\r\n"); + return HDF_FAILURE; + } + + HdfI2cInit(i2cResource->port, i2cResource->speed, i2cResource->devAddr); + + return HDF_SUCCESS; +} +#endif + +static int32_t AttachI2cDevice(struct I2cCntlr *host, struct HdfDeviceObject *device) +{ + int32_t ret = HDF_FAILURE; + + if (host == NULL || device == NULL) { + HDF_LOGE("[%s]: param is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + struct RealI2cResource *i2cResource = (struct RealI2cResource *)OsalMemAlloc(sizeof(struct RealI2cResource)); + if (i2cResource == NULL) { + HDF_LOGE("[%s]: OsalMemAlloc RealI2cResource fail\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + memset_s(i2cResource, sizeof(struct RealI2cResource), 0, sizeof(struct RealI2cResource)); +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetI2cDeviceResource(i2cResource, device->deviceMatchAttr); +#else + ret = GetI2cDeviceResource(i2cResource, device->property); +#endif + if (ret != HDF_SUCCESS) { + OsalMemFree(i2cResource); + return HDF_FAILURE; + } + + host->busId = i2cResource->port; + host->priv = i2cResource; + + return HDF_SUCCESS; +} + +static int32_t I2cDataTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count) +{ + if (cntlr == NULL || msgs == NULL || cntlr->priv == NULL) { + HDF_LOGE("[%s]: I2cDataTransfer param is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (count <= 0) { + HDF_LOGE("[%s]: I2cDataTransfer count err\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + struct RealI2cResource *device = (struct I2cDevice *)cntlr->priv; + if (device == NULL) { + HDF_LOGE("%s: I2cDevice is NULL\r\n", __func__); + return HDF_DEV_ERR_NO_DEVICE; + } + + struct I2cMsg *msg = NULL; + if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { + HDF_LOGE("[%s]: OsalMutexLock fail\r\n", __func__); + return HDF_ERR_TIMEOUT; + } + + for (int32_t i = 0; i < count; i++) { + msg = &msgs[i]; + if (msg->flags == I2C_FLAG_READ) { + HdfI2cRead(device->port, msg->addr, msg->buf, msg->len); + } else { + HdfI2cWrite(device->port, msg->addr, msg->buf, msg->len); + } + } + OsalMutexUnlock(&device->mutex); + + return count; +} + +static int32_t I2cDriverBind(struct HdfDeviceObject *device) +{ + if (device == NULL) { + HDF_LOGE("[%s]: I2c device is NULL\r\n", __func__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static int32_t I2cDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret = HDF_FAILURE; + struct I2cCntlr *host = NULL; + if (device == NULL) { + HDF_LOGE("[%s]: I2c device is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + host = (struct I2cCntlr *)OsalMemAlloc(sizeof(struct I2cCntlr)); + if (host == NULL) { + HDF_LOGE("[%s]: malloc host is NULL\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + memset_s(host, sizeof(struct I2cCntlr), 0, sizeof(struct I2cCntlr)); + host->ops = &gI2cHostMethod; + device->priv = (void *)host; + + ret = AttachI2cDevice(host, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("[%s]: AttachI2cDevice error, ret = %d\r\n", __func__, ret); + I2cDriverRelease(device); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + ret = I2cCntlrAdd(host); + if (ret != HDF_SUCCESS) { + I2cDriverRelease(device); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static void I2cDriverRelease(struct HdfDeviceObject *device) +{ + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return; + } + + struct I2cCntlr *i2cCntrl = device->priv; + if (i2cCntrl == NULL || i2cCntrl->priv == NULL) { + HDF_LOGE("%s: i2cCntrl is NULL\r\n", __func__); + return; + } + i2cCntrl->ops = NULL; + struct RealI2cResource *i2cDevice = (struct I2cDevice *)i2cCntrl->priv; + OsalMemFree(i2cCntrl); + + if (i2cDevice != NULL) { + OsalMutexDestroy(&i2cDevice->mutex); + OsalMemFree(i2cDevice); + } +} + +static I2C_TypeDef *GetLLI2cHandlerMatch(I2C_HANDLE i2cx) +{ + if (i2cx > I2C_HANDLE_MAX) { + printf("ERR: GetLLI2cClkMatch fail, param match fail\r\n"); + return NULL; + } + + switch (i2cx) { + case I2C_HANDLE_1: + return (I2C_TypeDef *)I2C1; + case I2C_HANDLE_2: + return (I2C_TypeDef *)I2C2; + case I2C_HANDLE_3: + return (I2C_TypeDef *)I2C3; + default: + printf("ERR: GetLLI2cClkMatch fail, handler match fail\r\n"); + return NULL; + } +} + +static bool EnableLLI2cClock(I2C_TypeDef *i2cx) +{ + if (i2cx == I2C1) { + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1); + return true; + } else if (i2cx == I2C2) { + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2); + return true; + } else if (i2cx == I2C3) { + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C3); + return true; + } else { + printf("EnableI2cClock fail, i2cx match fail\r\n"); + return false; + } +} + +static void HdfI2cInit(I2C_HANDLE i2cx, unsigned int i2cRate, unsigned int addr) +{ + LL_I2C_InitTypeDef I2C_InitStruct = {0}; + I2C_TypeDef *myI2c = GetLLI2cHandlerMatch(i2cx); + if (myI2c == NULL) { + return; + } + + EnableLLI2cClock(myI2c); + LL_I2C_DisableOwnAddress2(myI2c); + LL_I2C_DisableGeneralCall(myI2c); + LL_I2C_EnableClockStretching(myI2c); + I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C; + I2C_InitStruct.ClockSpeed = i2cRate; + I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2; + I2C_InitStruct.OwnAddress1 = addr; + I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK; + I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; + LL_I2C_Init(myI2c, &I2C_InitStruct); + LL_I2C_SetOwnAddress2(myI2c, 0); + + g_I2cEnableFlg[i2cx] = true; +} + +static void HdfI2cWrite(I2C_HANDLE i2cx, unsigned char devAddr, unsigned char *buf, unsigned int len) +{ + if (g_I2cEnableFlg[i2cx] != true) { + printf("I2C_WriteByte err, Please initialize first!"); + return; + } + + I2C_TypeDef *myI2c = GetLLI2cHandlerMatch(i2cx); + if (myI2c == NULL) { + return; + } + + while (LL_I2C_IsActiveFlag_BUSY(myI2c)); + + LL_I2C_GenerateStartCondition(myI2c); + while (LL_I2C_IsActiveFlag_SB(myI2c) == RESET); + + LL_I2C_TransmitData8(myI2c, (devAddr << 1)); + while (LL_I2C_IsActiveFlag_TXE(myI2c) == RESET); + + LL_I2C_ClearFlag_ADDR(myI2c); + while (LL_I2C_IsActiveFlag_TXE(myI2c) == RESET); + + for (unsigned int i = 0; i < len; i++) { + LL_I2C_TransmitData8(myI2c, buf[i]); + while (LL_I2C_IsActiveFlag_TXE(myI2c) == RESET); + } + + LL_I2C_GenerateStopCondition(myI2c); +} + +static void HdfI2cRead(I2C_HANDLE i2cx, unsigned char devAddr, unsigned char *buf, unsigned int len) +{ + if (g_I2cEnableFlg[i2cx] != true) { + printf("I2C_ReadByte err, Please initialize first!"); + return; + } + + I2C_TypeDef *myI2c = GetLLI2cHandlerMatch(i2cx); + if (myI2c == NULL) { + return; + } + + while (LL_I2C_IsActiveFlag_BUSY(myI2c)); + + LL_I2C_GenerateStartCondition(myI2c); + while (LL_I2C_IsActiveFlag_SB(myI2c) == RESET); + + LL_I2C_TransmitData8(myI2c, ((devAddr << 1) | 1)); + while ((LL_I2C_IsActiveFlag_ADDR(myI2c) == RESET) || (LL_I2C_IsActiveFlag_MSL(myI2c) == RESET) || + (LL_I2C_IsActiveFlag_BUSY(myI2c) == RESET)); + + for (unsigned int i = 0; i < len; i++) { + if (i < len - 1) { + LL_I2C_AcknowledgeNextData(myI2c, LL_I2C_ACK); + } else { + LL_I2C_AcknowledgeNextData(myI2c, LL_I2C_NACK); + } + while (LL_I2C_IsActiveFlag_RXNE(myI2c) == RESET); + buf[i] = LL_I2C_ReceiveData8(myI2c); + } + LL_I2C_GenerateStopCondition(myI2c); +} \ No newline at end of file diff --git a/platform/i2c/i2c_wm.c b/platform/i2c/i2c_wm.c new file mode 100755 index 0000000000000000000000000000000000000000..e87dd774db099dd70090fc43a4396bcf2a886f3e --- /dev/null +++ b/platform/i2c/i2c_wm.c @@ -0,0 +1,344 @@ +/* + * Copyright (c) 2022 Jiangsu Hoperun Software Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include "i2c_core.h" +#include "i2c_if.h" +#include "wm_i2c.h" +#include "wm_gpio_afsel.h" +#include "device_resource_if.h" +#include "osal_mutex.h" + +#define DEC_NUM 10 +#define GROUP_PIN_NUM 8 +#define I2C_INVALID_ADDR 0xFFFF +#define HAL_I2C_ID_NUM 1 + +struct I2cResource { + uint32_t port; + uint32_t sclPin; + uint32_t sdaPin; + uint32_t speed; +}; + +struct I2cDevice { + uint16_t devAddr; /**< slave device addr */ + uint32_t addressWidth; /**< Addressing mode: 7 bit or 10 bit */ + struct OsalMutex mutex; + uint32_t port; + struct I2cResource resource; +}; + +/* HdfDriverEntry method definitions */ +static int32_t i2cDriverBind(struct HdfDeviceObject *device); +static int32_t i2cDriverInit(struct HdfDeviceObject *device); +static void i2cDriverRelease(struct HdfDeviceObject *device); + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_i2cDriverEntry = { + .moduleVersion = 1, + .moduleName = "W800_I2C_MODULE_HDF", + .Bind = i2cDriverBind, + .Init = i2cDriverInit, + .Release = i2cDriverRelease, +}; + +// Initialize HdfDriverEntry +HDF_INIT(g_i2cDriverEntry); + +/* I2cHostMethod method definitions */ +static int32_t i2cHostTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count); + +struct I2cMethod g_i2cHostMethod = { + .transfer = i2cHostTransfer, +}; + +int32_t InitI2cDevice(struct I2cDevice *device) +{ + int32_t ret = -1; + uint32_t i2cPort; + struct I2cResource *resource = NULL; + + if (device == NULL) { + HDF_LOGE("device is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + resource = &device->resource; + device->port = resource->port; + i2cPort = device->port; + if (i2cPort >= HAL_I2C_ID_NUM) { + HDF_LOGE("i2c port %u not support\r\n", i2cPort); + return HDF_ERR_NOT_SUPPORT; + } + + if (OsalMutexInit(&device->mutex) != HDF_SUCCESS) { + HDF_LOGE("%s %d OsalMutexInit fail\r\n", __func__, __LINE__); + return HDF_FAILURE; + } + + if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { + HDF_LOGE("%s %d osMutexWait fail\r\n", __func__, __LINE__); + return HDF_ERR_TIMEOUT; + } + + if ((resource->sclPin == WM_IO_PA_01) && (resource->sdaPin == WM_IO_PA_04)) { + wm_i2c_scl_config(WM_IO_PA_01); + wm_i2c_sda_config(WM_IO_PA_04); + } else { + HDF_LOGE("%s %d scl sda pin fail\r\n", __func__, __LINE__); + OsalMutexUnlock(&device->mutex); + return HDF_ERR_INVALID_PARAM; + } + + tls_i2c_init(resource->speed); + OsalMutexUnlock(&device->mutex); + return HDF_SUCCESS; +} + +static int32_t HostRestI2cDevice(struct I2cDevice *device) +{ + int32_t ret = -1; + struct I2cResource *resource = NULL; + uint32_t i2cPort; + + if (device == NULL) { + HDF_LOGE("%s %d device is null\r\n", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + device->port = resource->port; + i2cPort = device->port; + if (i2cPort > HAL_I2C_ID_NUM) { + HDF_LOGE("i2c port %u not support\r\n", i2cPort); + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static uint32_t GetI2cDeviceResource(struct I2cDevice *device, + const struct DeviceResourceNode *resourceNode) +{ + uint32_t tempPin = 0; + struct I2cResource *resource = NULL; + struct DeviceResourceIface *dri = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("device or resourceNode is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + if (dri->GetUint32(resourceNode, "port", &resource->port, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config port fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "sclPin", &tempPin, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config sclPin fail\r\n"); + return HDF_FAILURE; + } + resource->sclPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); + + if (dri->GetUint32(resourceNode, "sdaPin", &tempPin, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config sdaPin fail\r\n"); + return HDF_FAILURE; + } + resource->sdaPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); + + if (dri->GetUint32(resourceNode, "speed", &resource->speed, 0) != HDF_SUCCESS) { + HDF_LOGE("i2c config speed fail\r\n"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t AttachI2cDevice(struct I2cCntlr *host, struct HdfDeviceObject *device) +{ + int32_t ret; + struct I2cDevice *i2cDevice = NULL; + struct I2cResource *resource = NULL; + + if (device == NULL || host == NULL) { + HDF_LOGE("%s: device or host is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + i2cDevice = (struct I2cDevice *)OsalMemAlloc(sizeof(struct I2cDevice)); + if (i2cDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc i2cDevice error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + (void)memset_s(i2cDevice, sizeof(struct I2cDevice), 0, sizeof(struct I2cDevice)); + ret = GetI2cDeviceResource(i2cDevice, device->property); + if (ret != HDF_SUCCESS) { + OsalMemFree(i2cDevice); + return HDF_FAILURE; + } + resource = &i2cDevice->resource; + if (resource == NULL) { + HDF_LOGE("%s %d: invalid parameter\r\n", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + + host->priv = i2cDevice; + host->busId = i2cDevice->port; + + return InitI2cDevice(i2cDevice); +} + +static int32_t i2cDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct I2cCntlr *host = NULL; + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + host = (struct I2cCntlr *)OsalMemAlloc(sizeof(struct I2cCntlr)); + if (host == NULL) { + HDF_LOGE("%s: host is NULL\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + (void)memset_s(host, sizeof(struct I2cCntlr), 0, sizeof(struct I2cCntlr)); + host->ops = &g_i2cHostMethod; + device->priv = (void *)host; + ret = AttachI2cDevice(host, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: attach error\r\n", __func__); + i2cDriverRelease(device); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + ret = I2cCntlrAdd(host); + if (ret != HDF_SUCCESS) { + i2cDriverRelease(device); + return HDF_FAILURE; + } + return ret; +} + +static int32_t i2cDriverBind(struct HdfDeviceObject *device) +{ + if (device == NULL) { + HDF_LOGE("%s: I2c device object is NULL\r\n", __func__); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +static void i2cDriverRelease(struct HdfDeviceObject *device) +{ + struct I2cCntlr *i2cCntrl = NULL; + struct I2cDevice *i2cDevice = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return; + } + i2cCntrl = device->priv; + if (i2cCntrl == NULL || i2cCntrl->priv == NULL) { + HDF_LOGE("%s: i2cCntrl is NULL\r\n", __func__); + return; + } + i2cCntrl->ops = NULL; + i2cDevice = (struct I2cDevice *)i2cCntrl->priv; + OsalMemFree(i2cCntrl); + + if (i2cDevice != NULL) { + OsalMutexDestroy(&i2cDevice->mutex); + OsalMemFree(i2cDevice); + } +} + +static int32_t i2c_send(struct I2cMsg *msg) +{ + uint16_t len; + uint8_t ifack, ifstop, ifstart; + + len = msg->len; + ifstop = 0; + if (msg->flags & I2C_FLAG_READ) { + ifack = msg->flags & I2C_FLAG_READ_NO_ACK ? 0 : 1; + for (int32_t j = 0; j < len; j++) { + if (((msg->flags & I2C_FLAG_STOP) && j) == (len - 1)) { + ifstop = 1; + } + msg->buf[j] = tls_i2c_read_byte(ifack, ifstop); + } + } else { + ifack = msg->flags & I2C_FLAG_IGNORE_NO_ACK ? 0 : 1; + for (int32_t j = 0; j < len; j++) { + if (((msg->flags & I2C_FLAG_NO_START) == 0) && (j == 0)) { + ifstart = 1; + } + tls_i2c_write_byte(msg->buf[j], ifstart); + if (ifack) { + tls_i2c_wait_ack(); + } + } + } +} + +static int32_t i2c_transfer(struct I2cDevice *device, struct I2cMsg *msgs, int16_t count) +{ + int ret; + struct I2cMsg *msg = NULL; + + uint32_t i2cPort; + if (device == NULL || msgs == NULL) { + HDF_LOGE("%s: device or msgs is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + i2cPort = (uint32_t)device->port; + if (i2cPort > HAL_I2C_ID_NUM) { + HDF_LOGE("i2c port %u not support\r\n", i2cPort); + return HDF_ERR_NOT_SUPPORT; + } + if (HDF_SUCCESS != OsalMutexLock(&device->mutex)) { + HDF_LOGE("%s %d OsalMutexTimedLock fail\r\n", __func__, __LINE__); + return HDF_ERR_TIMEOUT; + } + for (int32_t i = 0; i < count; i++) { + msg = &msgs[i]; + i2c_send(msg); + } + OsalMutexUnlock(&device->mutex); + return count; +} + +static int32_t i2cHostTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count) +{ + struct I2cDevice *device = NULL; + if (cntlr == NULL || msgs == NULL || cntlr->priv == NULL) { + HDF_LOGE("%s: I2cCntlr or msgs is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + device = (struct I2cDevice *)cntlr->priv; + if (device == NULL) { + HDF_LOGE("%s: I2cDevice is NULL\r\n", __func__); + return HDF_DEV_ERR_NO_DEVICE; + } + + return i2c_transfer(device, msgs, count); +} diff --git a/platform/pwm/BUILD.gn b/platform/pwm/BUILD.gn index a9ce95429560afc88dbf66c09bfd919c93675e6c..5daedb61b92061c3f3c8de593d2f9ea08a5e4859 100644 --- a/platform/pwm/BUILD.gn +++ b/platform/pwm/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") @@ -37,10 +16,26 @@ hdf_driver(module_name) { if (defined(LOSCFG_SOC_COMPANY_BESTECHNIC)) { sources += [ "pwm_bes.c" ] } + if (defined(LOSCFG_SOC_COMPANY_WINNERMICRO)) { + sources += [ "pwm_wm.c" ] + } + if (defined(LOSCFG_SOC_SERIES_STM32F4xx)) { + sources += [ "pwm_stm32f4xx.c" ] + } include_dirs = [ "." ] if (defined(LOSCFG_SHIELD_V200ZR_EVB_T1) && defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { deps = [ "//device/board/fnlink/shields" ] } + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/platform/pwm/pwm_bes.c b/platform/pwm/pwm_bes.c index 435298120ad7f20b940f3d5859eb2ede86f642d4..386e7809664d24555486cdc11c5d1bb10eb1055d 100755 --- a/platform/pwm/pwm_bes.c +++ b/platform/pwm/pwm_bes.c @@ -1,24 +1,17 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "pwm_bes.h" #include #include #include "hdf_device_desc.h" #include "hal_trace.h" #include "hal_timer.h" -#include "hal_iomux.h" #include "pwm_core.h" #include "hdf_log.h" #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO @@ -28,7 +21,13 @@ #include "device_resource_if.h" #endif +#if defined (LOSCFG_SOC_SERIES_BES2700) +#define PWM_MAX_FUNCTION 4 +#elif defined (LOSCFG_SOC_SERIES_BES2600) +#include "hal_iomux.h" #define PWM_MAX_FUNCTION 8 +#endif + #define UNTIL_NAN0SECONDS 1000000000 #define PERCENT 100 #define DEC_TEN 10 @@ -39,10 +38,12 @@ static uint32_t g_pwmFunction[PWM_MAX_FUNCTION] = { HAL_IOMUX_FUNC_PWM1, HAL_IOMUX_FUNC_PWM2, HAL_IOMUX_FUNC_PWM3, +#if defined (LOSCFG_SOC_SERIES_BES2600) HAL_IOMUX_FUNC_PWM4, HAL_IOMUX_FUNC_PWM5, HAL_IOMUX_FUNC_PWM6, HAL_IOMUX_FUNC_PWM7, +#endif }; static int32_t PwmDevSetConfig(struct PwmDev *pwm, struct PwmConfig *config); @@ -60,7 +61,7 @@ static int InitPwmDevice(struct PwmDev *host) struct PwmDevice *pwmDevice = NULL; struct PwmResource *resource = NULL; if (host == NULL || host->priv == NULL) { - HDF_LOGE("%s: invaild parameter\r\n", __func__); + HDF_LOGE("%s: invalid parameter\r\n", __func__); return HDF_ERR_INVALID_PARAM; } pwmDevice = (struct PwmDevice *)host->priv; @@ -88,8 +89,34 @@ static int InitPwmDevice(struct PwmDev *host) } #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO -static uint32_t GetPwmDeviceResource(struct PwmDevice *device) +#define PWM_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + tempPin = HCS_PROP(node, pwmPin); \ + resource->pwmPin = ((tempPin / DEC_TEN) * PIN_GROUP_NUM) + (tempPin % DEC_TEN); \ + resource->pwmId = HCS_PROP(node, pwmId); \ + result = HDF_SUCCESS; \ + break; \ + } \ + } while (0) +#define PLATFORM_PWM_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), pwm_config) +static uint32_t GetPwmDeviceResource(struct PwmDevice *device, const char *deviceMatchAttr) { + uint32_t tempPin; + int32_t result = HDF_FAILURE; + struct PwmResource *resource = NULL; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("%s: device or deviceMatchAttr is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; +#if HCS_NODE_EXISTS(PLATFORM_PWM_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_PWM_CONFIG, PWM_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } + return result; } #else static uint32_t GetPwmDeviceResource( @@ -148,7 +175,7 @@ static int32_t AttachPwmDevice(struct PwmDev *host, struct HdfDeviceObject *devi return HDF_ERR_MALLOC_FAIL; } #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO - ret = GetPwmDeviceResource(pwmDevice); + ret = GetPwmDeviceResource(pwmDevice, device->deviceMatchAttr); #else ret = GetPwmDeviceResource(pwmDevice, device->property); #endif diff --git a/platform/pwm/pwm_bes.h b/platform/pwm/pwm_bes.h old mode 100755 new mode 100644 index 0bc76f928f65b3594ba71b0c1fdd8822aaf30ab7..98af98e78b414e3edfb740aaf21cbe462eb6469f --- a/platform/pwm/pwm_bes.h +++ b/platform/pwm/pwm_bes.h @@ -1,23 +1,19 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #ifndef __PWM_BES_H__ #define __PWM_BES_H__ #include "hal_pwm.h" #include "hal_gpio.h" +#ifdef CHIP_BEST2003 #include "hal_iomux.h" +#endif #include "pwm_core.h" #ifdef __cplusplus @@ -39,4 +35,4 @@ struct PwmDevice { #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/platform/pwm/pwm_stm32f4xx.c b/platform/pwm/pwm_stm32f4xx.c new file mode 100644 index 0000000000000000000000000000000000000000..3ee1a83112c2f31e62558ed48934ad692d476a34 --- /dev/null +++ b/platform/pwm/pwm_stm32f4xx.c @@ -0,0 +1,591 @@ +/* + * Copyright (c) 2022 Talkweb Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#endif +#include "hdf_device_desc.h" +#include "pwm_core.h" +#include "hdf_log.h" +#include "hdf_base_hal.h" +#include "stm32f4xx_ll_tim.h" + +#define GPIO_STR_MAX_LENGTH 32 + +typedef enum { + PWM_CH1 = 0, + PWM_CH2, + PWM_CH3, + PWM_CH4, + PWM_CH_MAX +} PWM_CH; + +typedef enum { + PWM_TIM1 = 0, + PWM_TIM2, + PWM_TIM3, + PWM_TIM4, + PWM_TIM5, + PWM_TIM6, + PWM_TIM7, + PWM_TIM8, + PWM_TIM9, + PWM_TIM10, + PWM_TIM11, + PWM_TIM12, + PWM_TIM13, + PWM_TIM14, + PWM_TIM_MAX +} PWM_TIM; + +typedef struct { + PWM_CH pwmCh; + PWM_TIM pwmTim; + uint32_t prescaler; + uint32_t timPeroid; + uint32_t realHz; +} PwmResource; + +typedef struct { + LL_TIM_InitTypeDef timInitStruct; + LL_TIM_OC_InitTypeDef timOcInitStruct; +} PwmConfig; + +typedef struct { + struct IDeviceIoService ioService; + PwmConfig stPwmCfg; + struct PwmConfig *cfg; + PwmResource resource; +} PwmDevice; + +typedef struct { + uint32_t period; + uint32_t duty; + PWM_TIM pwmCh; + PWM_TIM pwmTim; +} PwmFreqArg; + +static TIM_TypeDef* g_stTimMap[PWM_TIM_MAX] = { + TIM1, + TIM2, + TIM3, + TIM4, + TIM5, + TIM6, + TIM7, + TIM8, + TIM9, + TIM10, + TIM11, + TIM12, + TIM13, + TIM14, +}; + +static uint32_t g_stChannelMap[PWM_CH_MAX] = { + LL_TIM_CHANNEL_CH1, + LL_TIM_CHANNEL_CH2, + LL_TIM_CHANNEL_CH3, + LL_TIM_CHANNEL_CH4, +}; + +static uint32_t g_stTimIrqMap[PWM_TIM_MAX] = { + TIM1_CC_IRQn, + TIM2_IRQn, + TIM3_IRQn, + TIM4_IRQn, + TIM5_IRQn, + TIM6_DAC_IRQn, + TIM7_IRQn, + TIM8_CC_IRQn, + TIM1_BRK_TIM9_IRQn, + TIM1_UP_TIM10_IRQn, + TIM1_TRG_COM_TIM11_IRQn, + TIM8_BRK_TIM12_IRQn, + TIM8_UP_TIM13_IRQn, + TIM8_TRG_COM_TIM14_IRQn, +}; + +static uint32_t g_stTimFreq[PWM_TIM_MAX] = { // tim2-tim7, tim12-tim14 is 84M,TIM1、TIM8~TIM11 is 168M + 168000000, + 84000000, + 84000000, + 84000000, + 84000000, + 84000000, + 84000000, + 168000000, + 168000000, + 168000000, + 168000000, + 84000000, + 84000000, +}; + +#define PER_SEC_NSEC 1000000000 + +static int32_t PwmDevSetConfig(struct PwmDev *pwm, struct PwmConfig *config); +static int32_t PwmDevOpen(struct PwmDev *pwm); +static int32_t PwmDevClose(struct PwmDev *pwm); + +struct PwmMethod g_pwmmethod = { + .setConfig = PwmDevSetConfig, + .open = PwmDevOpen, + .close = PwmDevClose, +}; + +static void InitPwmClock(PWM_TIM tim) +{ + switch (tim) { + case PWM_TIM1: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1); + break; + case PWM_TIM2: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); + break; + case PWM_TIM3: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3); + break; + case PWM_TIM4: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4); + break; + case PWM_TIM5: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM5); + break; + case PWM_TIM6: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM6); + break; + case PWM_TIM7: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM7); + break; + case PWM_TIM8: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM8); + break; + case PWM_TIM9: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM9); + break; + case PWM_TIM10: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM10); + break; + case PWM_TIM11: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM11); + break; + case PWM_TIM12: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM12); + break; + case PWM_TIM13: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM13); + break; + case PWM_TIM14: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM14); + break; + default: + break; + } +} + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define PWM_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + uint8_t tim = HCS_PROP(node, pwmTim); \ + uint8_t ch = HCS_PROP(node, pwmCh); \ + uint8_t prescaler = HCS_PROP(node, prescaler); \ + resource->pwmCh = ch; \ + resource->pwmTim = tim; \ + resource->prescaler = prescaler; \ + result = HDF_SUCCESS; \ + } \ + } while (0) +#define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform) +#define PLATFORM_PWM_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), pwm_config) +static uint32_t GetPwmDeviceResource(PwmDevice *device, const char *deviceMatchAttr) +{ + int32_t result = HDF_FAILURE; + PwmResource *resource = NULL; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("%s: device or deviceMatchAttr is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; +#if HCS_NODE_HAS_PROP(PLATFORM_CONFIG, pwm_config) + HCS_FOREACH_CHILD_VARGS(PLATFORM_PWM_CONFIG, PWM_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } + + return result; +} +#else +static int32_t GetPwmDeviceResource(PwmDevice *device, const struct DeviceResourceNode *resourceNode) +{ + struct DeviceResourceIface *dri = NULL; + PwmResource *resource = NULL; + + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("resource or device is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("resource is NULL\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint8 == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + if (dri->GetUint8(resourceNode, "pwmTim", &resource->pwmTim, 0) != HDF_SUCCESS) { + HDF_LOGE("read pwmPin fail\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + if (resource->pwmTim == PWM_TIM6 || resource->pwmTim == PWM_TIM7) { + HDF_LOGE("unsupport tim\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + if (dri->GetUint8(resourceNode, "pwmCh", &resource->pwmCh, 0) != HDF_SUCCESS) { + HDF_LOGE("read pwmCh fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "prescaler", &resource->prescaler, 0) != HDF_SUCCESS) { + HDF_LOGE("read prescaler fail\r\n"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} +#endif + +static int32_t AttachPwmDevice(struct PwmDev *host, struct HdfDeviceObject *device) +{ + int32_t ret; + PwmDevice *pwmDevice = NULL; +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL || host == NULL) { +#else + if (device == NULL || device->property == NULL || host == NULL) { +#endif + HDF_LOGE("%s: param is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + pwmDevice = (PwmDevice *)OsalMemAlloc(sizeof(PwmDevice)); + if (pwmDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc pwmDevice error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetPwmDeviceResource(pwmDevice, device->deviceMatchAttr); +#else + ret = GetPwmDeviceResource(pwmDevice, device->property); +#endif + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(pwmDevice); + return HDF_FAILURE; + } + host->priv = pwmDevice; + host->num = pwmDevice->resource.pwmTim; + + return HDF_SUCCESS; +} + +static int32_t PwmDriverBind(struct HdfDeviceObject *device); +static int32_t PwmDriverInit(struct HdfDeviceObject *device); +static void PwmDriverRelease(struct HdfDeviceObject *device); + +struct HdfDriverEntry g_pwmDriverEntry = { + .moduleVersion = 1, + .moduleName = "ST_HDF_PLATFORM_PWM", + .Bind = PwmDriverBind, + .Init = PwmDriverInit, + .Release = PwmDriverRelease, +}; +HDF_INIT(g_pwmDriverEntry); + +static int32_t PwmDriverBind(struct HdfDeviceObject *device) +{ + struct PwmDev *devService = NULL; + if (device == NULL) { + HDF_LOGE("hdfDevice object is null!\r\n"); + return HDF_FAILURE; + } + + devService = (struct PwmDev *)OsalMemCalloc(sizeof(struct PwmDev)); + if (devService == NULL) { + HDF_LOGE("malloc pwmDev failed\n"); + } + device->service = &devService->service; + devService->device = device; + + return HDF_SUCCESS; +} + +static int32_t PwmDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct PwmDev *host = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + host = (struct PwmDev *)device->service; + if (host == NULL) { + HDF_LOGE("%s: host is NULL\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = AttachPwmDevice(host, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s:attach error\r\n", __func__); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + host->method = &g_pwmmethod; + ret = PwmDeviceAdd(device, host); + if (ret != HDF_SUCCESS) { + PwmDeviceRemove(device, host); + OsalMemFree(host->device); + OsalMemFree(host); + return HDF_DEV_ERR_NO_DEVICE; + } + + return HDF_SUCCESS; +} + +static void PwmDriverRelease(struct HdfDeviceObject *device) +{ + struct PwmDev *host = NULL; + + if (device == NULL || device->service == NULL) { + HDF_LOGE("device is null\r\n"); + return; + } + + host = (struct PwmDev *)device->service; + if (host != NULL && host->device != NULL) { + host->method = NULL; + OsalMemFree(host->device); + OsalMemFree(host); + host->device = NULL; + host = NULL; + } + + device->service = NULL; + host = NULL; + + return; +} + +static int32_t InitPwmFreqAndPeriod(const struct PwmConfig *config, PwmFreqArg* arg, const PwmResource *resource) +{ + if (arg == NULL) { + HDF_LOGE("null ptr\r\n"); + return HDF_FAILURE; + } + + uint32_t freq = 0; + uint32_t period = 0; + uint32_t duty = 0; + uint32_t realHz = 0; + + if (config->period != 0) { + freq = (uint32_t)(PER_SEC_NSEC / config->period); + } else { + HDF_LOGE("invalid div\r\n"); + return HDF_FAILURE; + } + + realHz = (uint32_t)(((double)g_stTimFreq[arg->pwmTim]) / ((double)(resource->prescaler + 1))); + + if (freq != 0) { + period = (uint32_t)(realHz / freq); + } else { + HDF_LOGE("invalid div\r\n"); + return HDF_FAILURE; + } + + if (config->period != 0) { + duty = (uint32_t)(((double)config->duty / (double)config->period) * period); + } else { + HDF_LOGE("invalid div\r\n"); + return HDF_FAILURE; + } + + arg->period = period; + arg->duty = duty; + + return HDF_SUCCESS; +} + +static void InitTimPwm(const PwmFreqArg* arg, const struct PwmConfig *config, + PwmConfig *pwmCfg, const PwmResource *resource) +{ + if (arg == NULL) { + HDF_LOGE("null ptr\r\n"); + return; + } + + pwmCfg->timInitStruct.Autoreload = arg->period - 1; // if period is 1000 20KHz/1000=20Hz,period is 50ms + pwmCfg->timInitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; + pwmCfg->timInitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; + pwmCfg->timInitStruct.Prescaler = resource->prescaler; + LL_TIM_Init(g_stTimMap[arg->pwmTim], &pwmCfg->timInitStruct); + LL_TIM_EnableARRPreload(g_stTimMap[arg->pwmTim]); + LL_TIM_SetClockSource(g_stTimMap[arg->pwmTim], LL_TIM_CLOCKSOURCE_INTERNAL); + LL_TIM_SetTriggerOutput(g_stTimMap[arg->pwmTim], LL_TIM_TRGO_RESET); + LL_TIM_DisableMasterSlaveMode(g_stTimMap[arg->pwmTim]); + + pwmCfg->timOcInitStruct.OCMode = LL_TIM_OCMODE_PWM1; // PWM1 mode + pwmCfg->timOcInitStruct.OCState = LL_TIM_OCSTATE_DISABLE; + pwmCfg->timOcInitStruct.CompareValue = arg->duty; + if (config->polarity == PWM_NORMAL_POLARITY) { + pwmCfg->timOcInitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; + } else { + pwmCfg->timOcInitStruct.OCPolarity = LL_TIM_OCPOLARITY_LOW; + } + pwmCfg->timOcInitStruct.OCIdleState = LL_TIM_OCPOLARITY_LOW; + + LL_TIM_OC_Init(g_stTimMap[arg->pwmTim], g_stChannelMap[arg->pwmCh], &pwmCfg->timOcInitStruct); + LL_TIM_OC_DisableFast(g_stTimMap[arg->pwmTim], g_stChannelMap[arg->pwmCh]); + + if (arg->pwmTim == PWM_TIM1 || arg->pwmTim == PWM_TIM8) { + LL_TIM_BDTR_InitTypeDef bdtrInitStruct = {0}; + bdtrInitStruct.OSSRState = LL_TIM_OSSR_DISABLE; + bdtrInitStruct.OSSIState = LL_TIM_OSSI_DISABLE; + bdtrInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF; + bdtrInitStruct.DeadTime = 0; // dead area time 200ns 0x28 + bdtrInitStruct.BreakState = LL_TIM_BREAK_ENABLE; + bdtrInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH; + bdtrInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_ENABLE; + LL_TIM_BDTR_Init(g_stTimMap[arg->pwmTim], &bdtrInitStruct); + } + + NVIC_SetPriority(g_stTimIrqMap[arg->pwmTim], NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 1, 1)); + NVIC_EnableIRQ(g_stTimIrqMap[arg->pwmTim]); + + if (arg->pwmTim == PWM_TIM1 || arg->pwmTim == PWM_TIM8) { + LL_TIM_EnableAutomaticOutput(g_stTimMap[arg->pwmTim]); + LL_TIM_GenerateEvent_UPDATE(g_stTimMap[arg->pwmTim]); + } + LL_TIM_EnableCounter(g_stTimMap[arg->pwmTim]); + LL_TIM_CC_EnableChannel(g_stTimMap[arg->pwmTim], g_stChannelMap[arg->pwmCh]); + LL_TIM_OC_EnablePreload(g_stTimMap[arg->pwmTim], g_stChannelMap[arg->pwmCh]); + + return; +} + +static void DeInitTimPwm(PWM_TIM pwmId, PWM_CH pwmCh) +{ + LL_TIM_DeInit(g_stTimMap[pwmId]); + LL_TIM_OC_DisableClear(g_stTimMap[pwmId], g_stChannelMap[pwmCh]); + NVIC_DisableIRQ(g_stTimIrqMap[pwmId]); + + LL_TIM_DisableCounter(g_stTimMap[pwmId]); + LL_TIM_CC_DisableChannel(g_stTimMap[pwmId], g_stChannelMap[pwmCh]); + LL_TIM_OC_DisablePreload(g_stTimMap[pwmId], g_stChannelMap[pwmCh]); + + if (pwmId== PWM_TIM1 || pwmId == PWM_TIM8) { + LL_TIM_DisableAutomaticOutput(g_stTimMap[pwmId]); + } + + return; +} + +static int32_t PwmDevSetConfig(struct PwmDev *pwm, struct PwmConfig *config) +{ + PwmDevice *prvPwm = NULL; + PwmConfig *pwmCfg = NULL; + PWM_TIM pwmId; + PWM_CH pwmCh; + PwmResource *resource = NULL; + + if (pwm == NULL || config == NULL || (config->period > PER_SEC_NSEC)) { + HDF_LOGE("%s\r\n", __FUNCTION__); + return HDF_FAILURE; + } + + prvPwm = (struct PwmDevice *)PwmGetPriv(pwm); + if (prvPwm == NULL) { + return HDF_FAILURE; + } + resource = &prvPwm->resource; + if (resource == NULL) { + return HDF_FAILURE; + } + pwmCfg = &prvPwm->stPwmCfg; + if (pwmCfg == NULL) { + return HDF_FAILURE; + } + pwmId = prvPwm->resource.pwmTim; + pwmCh = prvPwm->resource.pwmCh; + + if (config->status == PWM_ENABLE_STATUS) { + PwmFreqArg arg = {0}; + arg.pwmCh = pwmCh; + arg.pwmTim = pwmId; + if (InitPwmFreqAndPeriod(config, &arg, resource) != HDF_SUCCESS) { + HDF_LOGE("calculate freq and period failed!\r\n"); + return HDF_FAILURE; + } + + InitPwmClock(pwmId); + InitTimPwm(&arg, config, pwmCfg, resource); + } else { + DeInitTimPwm(pwmId, pwmCh); + } + + return HDF_SUCCESS; +} + +static int32_t PwmDevOpen(struct PwmDev *pwm) +{ + if (pwm == NULL) { + HDF_LOGE("%s\r\n", __FUNCTION__); + return HDF_ERR_INVALID_PARAM; + } + + return HDF_SUCCESS; +} + +static int32_t PwmDevClose(struct PwmDev *pwm) +{ + PwmDevice *prvPwm = NULL; + PWM_TIM pwmId; + PWM_CH pwmCh; + if (pwm == NULL) { + HDF_LOGE("%s\r\n", __FUNCTION__); + return HDF_ERR_INVALID_PARAM; + } + prvPwm = (PwmDevice *)PwmGetPriv(pwm); + if (prvPwm == NULL) { + HDF_LOGE("%s\r\n", __FUNCTION__); + return HDF_DEV_ERR_NO_DEVICE; + } + + pwmId = prvPwm->resource.pwmTim; + pwmCh = prvPwm->resource.pwmCh; + LL_TIM_DeInit(g_stTimMap[pwmId]); + LL_TIM_OC_DisableClear(g_stTimMap[pwmId], g_stChannelMap[pwmCh]); + NVIC_DisableIRQ(g_stTimIrqMap[pwmId]); + + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/platform/pwm/pwm_wm.c b/platform/pwm/pwm_wm.c new file mode 100755 index 0000000000000000000000000000000000000000..1c45381c353adbd11cff87d01b1155f1d49f4c3f --- /dev/null +++ b/platform/pwm/pwm_wm.c @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2022 Jiangsu Hoperun Software Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include "device_resource_if.h" +#include "hdf_device_desc.h" +#include "pwm_core.h" +#include "hdf_log.h" +#include "wm_pwm.h" + +struct PwmResource { + uint32_t channel; + uint32_t freq; +}; + +struct PwmDevice { + struct IDeviceIoService ioService; + pwm_init_param pwmCfg; + struct PwmConfig *cfg; + struct PwmResource resource; +}; + +static int32_t PwmDevSetConfig(struct PwmDev *pwm, struct PwmConfig *config); + +struct PwmMethod g_pwmmethod = { + .setConfig = PwmDevSetConfig, +}; + +static int32_t PwmDevSetConfig(struct PwmDev *pwm, struct PwmConfig *config) +{ + struct PwmDevice *prvPwm = NULL; + pwm_init_param *pwmCfg = NULL; + uint32_t freq; + + if (pwm == NULL || config == NULL) { + HDF_LOGE("%s\r\n", __FUNCTION__); + return HDF_FAILURE; + } + + prvPwm = (struct PwmDevice *)PwmGetPriv(pwm); + if (prvPwm == NULL) { + return HDF_FAILURE; + } + pwmCfg = &prvPwm->pwmCfg; + pwmCfg->period = config->period; + pwmCfg->duty = config->duty; + pwmCfg->pnum = config->number; + pwmCfg->channel = prvPwm->resource.channel; + freq = prvPwm->resource.freq; + + if (config->status == PWM_ENABLE_STATUS) { + tls_pwm_init(pwmCfg->channel, freq, pwmCfg->duty, pwmCfg->pnum); + } + + return HDF_SUCCESS; +} + +static uint32_t GetPwmDeviceResource( + struct PwmDevice *device, const struct DeviceResourceNode *resourceNode) +{ + uint32_t tempPin = 0; + struct DeviceResourceIface *dri = NULL; + struct PwmResource *resource = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("resource or device is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("resource is NULL\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + if (dri->GetUint32(resourceNode, "channel", &resource->channel, 0) != HDF_SUCCESS) { + HDF_LOGE("read channel fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "freq", &resource->freq, 0) != HDF_SUCCESS) { + HDF_LOGE("read freq fail\r\n"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t AttachPwmDevice(struct PwmDev *host, struct HdfDeviceObject *device) +{ + int32_t ret; + struct PwmDevice *pwmDevice = NULL; + if (device == NULL || device->property == NULL || host == NULL) { + HDF_LOGE("%s: param is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + pwmDevice = (struct PwmDevice *)OsalMemAlloc(sizeof(struct PwmDevice)); + if (pwmDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc pwmDevice error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = GetPwmDeviceResource(pwmDevice, device->property); + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(pwmDevice); + return HDF_FAILURE; + } + + host->priv = pwmDevice; + + return HDF_SUCCESS; +} + +static int32_t PwmDriverBind(struct HdfDeviceObject *device) +{ + static struct PwmDev devService; + + if (device == NULL) { + HDF_LOGE("hdfDevice object is null!\r\n"); + return HDF_FAILURE; + } + + device->service = &devService.service; + devService.device = device; + HDF_LOGI("Enter %s\r\n", __func__); + + return HDF_SUCCESS; +} + +static int32_t PwmDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct PwmDev *host = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGI("Enter %s:\r\n", __func__); + + host = (struct PwmDev *)OsalMemAlloc(sizeof(struct PwmDev)); + if (host == NULL) { + HDF_LOGE("%s: host is NULL\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = AttachPwmDevice(host, device); + if (ret != HDF_SUCCESS) { + OsalMemFree(host); + HDF_LOGE("%s:attach error\r\n", __func__); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + host->method = &g_pwmmethod; + ret = PwmDeviceAdd(device, host); + if (ret != HDF_SUCCESS) { + PwmDeviceRemove(device, host); + OsalMemFree(host->device); + OsalMemFree(host); + return HDF_DEV_ERR_NO_DEVICE; + } + + HDF_LOGI("PwmDriverInit success!\r\n"); + return HDF_SUCCESS; +} + +static void PwmDriverRelease(struct HdfDeviceObject *device) +{ + struct PwmDev *host = NULL; + + if (device == NULL || device->service == NULL) { + HDF_LOGE("device is null\r\n"); + return; + } + + host = (struct PwmDev *)device->service; + if (host != NULL && host->device != NULL) { + host->method = NULL; + OsalMemFree(host->device); + OsalMemFree(host); + host->device = NULL; + host = NULL; + } + + device->service = NULL; + HDF_LOGI("PwmDriverRelease finish!!\r\n"); + return; +} + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_pwmDriverEntry = { + .moduleVersion = 1, + .moduleName = "WM_PWM_MODULE_HDF", + .Bind = PwmDriverBind, + .Init = PwmDriverInit, + .Release = PwmDriverRelease, +}; + +// Initialize HdfDriverEntry +HDF_INIT(g_pwmDriverEntry); diff --git a/platform/spi/BUILD.gn b/platform/spi/BUILD.gn index 49ec290b99f076ba8007d13bf5bcc0a05b3f59d2..b4558abfb0e2c5c8c0606a5db8f7994fbf63667f 100644 --- a/platform/spi/BUILD.gn +++ b/platform/spi/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") @@ -37,10 +16,26 @@ hdf_driver(module_name) { if (defined(LOSCFG_SOC_COMPANY_BESTECHNIC)) { sources += [ "spi_bes.c" ] } + if (defined(LOSCFG_SOC_COMPANY_WINNERMICRO)) { + sources += [ "spi_wm.c" ] + } + if (defined(LOSCFG_SOC_SERIES_STM32F4xx)) { + sources += [ "spi_stm32f4xx.c" ] + } include_dirs = [ "." ] if (defined(LOSCFG_SHIELD_V200ZR_EVB_T1) && defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { deps = [ "//device/board/fnlink/shields" ] } + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/platform/spi/spi_bes.c b/platform/spi/spi_bes.c index b1e92777096bd34dc49ee8522257cb3e6d7cc6b1..5a83d949b669c1464941a2aa8b66eedb9f68fd30 100755 --- a/platform/spi/spi_bes.c +++ b/platform/spi/spi_bes.c @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "spi_bes.h" #include #include @@ -43,10 +37,17 @@ static void Spi1DmaIrq(int error); static struct SPI_CTX_OBJ_T spiCtx[MAX_SPI_NUMBER] = { { .spiPinCS0 = 0, +#if defined (LOSCFG_SOC_SERIES_BES2700) + .spiFunDI0 = HAL_IOMUX_FUNC_SYS_SPI_DI0, + .spiFunCLK = HAL_IOMUX_FUNC_SYS_SPI_CLK, + .spiFunCS0 = HAL_IOMUX_FUNC_SYS_SPI_CS0, + .spiFunDIO = HAL_IOMUX_FUNC_SYS_SPI_DIO, +#elif defined (LOSCFG_SOC_SERIES_BES2600) .spiFunDI0 = HAL_IOMUX_FUNC_SPI_DI0, .spiFunCLK = HAL_IOMUX_FUNC_SPI_CLK, .spiFunCS0 = HAL_IOMUX_FUNC_SPI_CS0, .spiFunDIO = HAL_IOMUX_FUNC_SPI_DIO, +#endif .sem = { NULL }, .mutex = { NULL }, .SpiOpen = hal_spi_open, @@ -59,10 +60,17 @@ static struct SPI_CTX_OBJ_T spiCtx[MAX_SPI_NUMBER] = { }, { .spiPinCS0 = 0, +#if defined (LOSCFG_SOC_SERIES_BES2700) + .spiFunDI0 = HAL_IOMUX_FUNC_SYS_SPILCD_DI0, + .spiFunCLK = HAL_IOMUX_FUNC_SYS_SPILCD_CLK, + .spiFunCS0 = HAL_IOMUX_FUNC_SYS_SPILCD_CS0, + .spiFunDIO = HAL_IOMUX_FUNC_SYS_SPILCD_DIO, +#elif defined (LOSCFG_SOC_SERIES_BES2600) .spiFunDI0 = HAL_IOMUX_FUNC_SPILCD_DI0, .spiFunCLK = HAL_IOMUX_FUNC_SPILCD_CLK, .spiFunCS0 = HAL_IOMUX_FUNC_SPILCD_CS0, .spiFunDIO = HAL_IOMUX_FUNC_SPILCD_DIO, +#endif .sem = { NULL }, .mutex = { NULL }, .SpiOpen = hal_spilcd_open, @@ -113,12 +121,12 @@ static void SpiIomuxInit(struct SpiDevice *spiDevice) resource = &spiDevice->resource; if (resource == NULL) { HDF_LOGE("resource is null\r\n"); - return HDF_ERR_INVALID_OBJECT; + return; } spiDevCfg = &spiDevice->spiDevCfg; if (spiDevCfg == NULL) { HDF_LOGE("resource is null\r\n"); - return HDF_ERR_INVALID_OBJECT; + return; } spiDevCfg->rate = resource->speed; @@ -141,14 +149,55 @@ static void SpiIomuxInit(struct SpiDevice *spiDevice) pinMuxSpi[SPI_PIN_2].function = spiCtx[spiPort].spiFunCS0; pinMuxSpi[SPI_PIN_3].function = spiCtx[spiPort].spiFunDIO; - if (spiDevCfg->rate > MAX_SPI_SPEED) { - hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_1].pin, DRIVER_LEVEL); - hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_3].pin, DRIVER_LEVEL); - } + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_0].pin, DRIVER_LEVEL); + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_1].pin, DRIVER_LEVEL); + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_2].pin, DRIVER_LEVEL); + hal_iomux_set_io_drv(pinMuxSpi[SPI_PIN_3].pin, DRIVER_LEVEL); hal_iomux_init(pinMuxSpi, ARRAY_SIZE(pinMuxSpi)); } +static int32_t SpiDevCfgInit(struct HAL_SPI_CFG_T *spiDevCfg, struct SpiResource *resource) +{ + if (spiDevCfg == NULL || resource == NULL) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + switch (resource->mode) { + case SPI_WORK_MODE_0: + spiDevCfg->clk_delay_half = false; + spiDevCfg->clk_polarity = false; + break; + case SPI_WORK_MODE_1: + spiDevCfg->clk_delay_half = true; + spiDevCfg->clk_polarity = false; + break; + case SPI_WORK_MODE_2: + spiDevCfg->clk_delay_half = false; + spiDevCfg->clk_polarity = true; + break; + case SPI_WORK_MODE_3: + spiDevCfg->clk_delay_half = true; + spiDevCfg->clk_polarity = true; + break; + default: + spiDevCfg->clk_delay_half = true; + spiDevCfg->clk_polarity = true; + } + spiDevCfg->slave = 0; + if (resource->transmode == SPI_TRANSFER_DMA) { + spiDevCfg->dma_rx = true; + spiDevCfg->dma_tx = true; + } else { + spiDevCfg->dma_rx = false; + spiDevCfg->dma_tx = false; + } + spiDevCfg->cs = 0; + spiDevCfg->rx_bits = resource->dataSize; + spiDevCfg->tx_bits = resource->dataSize; + spiDevCfg->rx_frame_bits = 0; + return HDF_SUCCESS; +} /** * Spi send * @@ -166,7 +215,7 @@ static void SpiIomuxInit(struct SpiDevice *spiDevice) #endif int32_t HalSpiSend(struct SpiDevice *spiDevice, const uint8_t *data, uint16_t size, uint32_t timeOut) { - int32_t ret = 0; + int32_t ret; uint32_t spiId; uint32_t len = size; struct SpiResource *resource = NULL; @@ -188,7 +237,9 @@ int32_t HalSpiSend(struct SpiDevice *spiDevice, const uint8_t *data, uint16_t si HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); return HDF_ERR_TIMEOUT; } - +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif if (resource->transmode == SPI_TRANSFER_DMA) { ret = spiCtx[spiId].SpiDmaSend(data, len, spiCtx[spiId].SpiDmaIrq); if (OsalSemWait(&spiCtx[spiId].sem, timeOut) != HDF_SUCCESS) { @@ -223,11 +274,11 @@ OUT: * * @return 0 : on success, EIO : if the SPI device could not be initialised */ -int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, uint32_t timeOut) +static int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, uint32_t timeOut) { - int32_t ret = 0; - uint32_t len = size; - uint32_t remainder = 0; + int32_t ret; + int32_t len = (int32_t)size; + uint32_t remainder; int32_t status = HDF_FAILURE; uint8_t *cmd = NULL; uint32_t spiId; @@ -243,50 +294,46 @@ int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, ui HDF_LOGE("resource is null\r\n"); return HDF_ERR_INVALID_OBJECT; } - cmd = (uint8_t *)OsalMemAlloc(len); - if (cmd == NULL) { - HDF_LOGE("%s OsalMemAlloc size %ld error\r\n", __FUNCTION__, len); - return HDF_ERR_MALLOC_FAIL; - } - - memset_s(cmd, len, 0, len); + cmd = (uint8_t *)0x20020000; // 0x20020000 : non secure sram base. tx buf is useless, use sram instead of malloc. status = OsalMutexLock(&spiCtx[spiId].mutex); if (HDF_SUCCESS != status) { HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); - OsalMemFree(cmd); return HDF_ERR_TIMEOUT; } - remainder = len <= SPI_DMA_MAX ? len : SPI_DMA_MAX; - - if (resource->transmode == SPI_TRANSFER_DMA) { - ret = spiCtx[spiId].SpiDmaRecv(cmd, data, remainder, spiCtx[spiId].SpiDmaIrq); - if (OsalSemWait(&spiCtx[spiId].sem, timeOut) <= 0) { - HDF_LOGE("SPI Read timeOut!\r\n"); - goto OUT; +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif + do { + remainder = len <= SPI_DMA_MAX ? len : SPI_DMA_MAX; + if (resource->transmode == SPI_TRANSFER_DMA) { + ret = spiCtx[spiId].SpiDmaRecv(cmd, data, remainder, spiCtx[spiId].SpiDmaIrq); + if (OsalSemWait(&spiCtx[spiId].sem, timeOut) <= 0) { + HDF_LOGE("SPI Read timeOut!\r\n"); + goto OUT; + } + } else { + ret = spiCtx[spiId].SpiRecv(cmd, data, remainder); } - } else { - ret = spiCtx[spiId].SpiRecv(cmd, data, remainder); - } - len -= remainder; - data += remainder; + len -= remainder; + data += remainder; - if (ret) { - HDF_LOGE("spi tail fail %ld, size %ld\r\n", ret, len); - goto OUT; - } + if (ret != 0) { + HDF_LOGE("spi tail fail %ld, size %ld\r\n", ret, len); + goto OUT; + } + } while (len); OUT: OsalMutexUnlock(&spiCtx[spiId].mutex); - OsalMemFree(cmd); return ret; } #ifdef HalSpiSendRecv #undef HalSpiSendRecv #endif -int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t txSize, uint8_t *rxData, - uint16_t rxSize, uint32_t timeOut) +static int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t txSize, uint8_t *rxData, + uint16_t rxSize) { int32_t ret; int32_t status; @@ -300,13 +347,15 @@ int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t tx resource = &spiDevice->resource; status = OsalMutexLock(&spiCtx[spiId].mutex); if (HDF_SUCCESS != status) { - HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); + HDF_LOGE("%s OsalMutexLock error = 0x%X!\r\n", __func__, status); return HDF_ERR_TIMEOUT; } - +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif if (resource->transmode == SPI_TRANSFER_DMA) { ret = spiCtx[spiId].SpiDmaRecv(txData, rxData, rxSize, spiCtx[spiId].SpiDmaIrq); - if (OsalSemWait(&spiCtx[spiId].sem, timeOut) <= 0) { + if (OsalSemWait(&spiCtx[spiId].sem, TIMEOUT) <= 0) { HDF_LOGE("%s:SPI Read timeOut!\r\n", __func__); goto OUT; } @@ -315,7 +364,6 @@ int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t tx } if (ret) { HDF_LOGE("spi dma tail fail %d\r\n", ret); - goto OUT; } OUT: OsalMutexUnlock(&spiCtx[spiId].mutex); @@ -325,6 +373,7 @@ OUT: static int32_t InitSpiDevice(struct SpiDevice *spiDevice) { uint32_t spiPort; + int32_t ret; struct HAL_SPI_CFG_T *spiDevCfg = NULL; struct SpiResource *resource = NULL; if (spiDevice == NULL) { @@ -337,40 +386,11 @@ static int32_t InitSpiDevice(struct SpiDevice *spiDevice) spiPort = spiDevice->spiId; SpiIomuxInit(spiDevice); - - switch (resource->mode) { - case SPI_WORK_MODE_0: - spiDevCfg->clk_delay_half = false; - spiDevCfg->clk_polarity = false; - break; - case SPI_WORK_MODE_1: - spiDevCfg->clk_delay_half = true; - spiDevCfg->clk_polarity = false; - break; - case SPI_WORK_MODE_2: - spiDevCfg->clk_delay_half = false; - spiDevCfg->clk_polarity = true; - break; - case SPI_WORK_MODE_3: - spiDevCfg->clk_delay_half = true; - spiDevCfg->clk_polarity = true; - break; - default: - spiDevCfg->clk_delay_half = true; - spiDevCfg->clk_polarity = true; - } - spiDevCfg->slave = 0; - if (resource->transmode == SPI_TRANSFER_DMA) { - spiDevCfg->dma_rx = true; - spiDevCfg->dma_tx = true; - } else { - spiDevCfg->dma_rx = false; - spiDevCfg->dma_tx = false; + ret = SpiDevCfgInit(spiDevCfg, resource); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: SPI config init failed\r\n", __func__); + return HDF_FAILURE; } - spiDevCfg->cs = 0; - spiDevCfg->rx_bits = resource->dataSize; - spiDevCfg->tx_bits = resource->dataSize; - spiDevCfg->rx_frame_bits = 0; /* spi open move to hdf open api */ /* if cs use as gpio ,pull up at first */ if (spiCtx[spiPort].spiFunCS0 == HAL_IOMUX_FUNC_AS_GPIO) { @@ -395,8 +415,47 @@ static int32_t InitSpiDevice(struct SpiDevice *spiDevice) /* get spi config from hcs file */ #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO -static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice) +#define SPI_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->num = HCS_PROP(node, busNum); \ + resource->speed = HCS_PROP(node, speed); \ + resource->transmode = HCS_PROP(node, transmode); \ + resource->spiCsSoft = HCS_PROP(node, spiCsSoft); \ + resource->mode = HCS_PROP(node, mode); \ + resource->dataSize = HCS_PROP(node, dataSize); \ + resource->csNum = HCS_PROP(node, csNum); \ + tempPin = HCS_PROP(node, spiClkPin); \ + resource->spiClkPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + tempPin = HCS_PROP(node, spiMosiPin); \ + resource->spiMosiPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + tempPin = HCS_PROP(node, spiMisoPin); \ + resource->spiMisoPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + tempPin = HCS_PROP(node, spiCsPin); \ + resource->spiCsPin = ((tempPin / DEC_NUM) * GROUP_PIN_NUM) + (tempPin % DEC_NUM); \ + result = HDF_SUCCESS; \ + break; \ + } \ + } while (0) + +#define PLATFORM_SPI_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), spi_config) +static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const char *deviceMatchAttr) { + uint32_t tempPin; + int32_t result = HDF_FAILURE; + struct SpiResource *resource = NULL; + if (spiDevice == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &spiDevice->resource; +#if HCS_NODE_EXISTS(PLATFORM_SPI_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_SPI_CONFIG, SPI_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } + return result; } #else static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const struct DeviceResourceNode *resourceNode) @@ -489,6 +548,7 @@ static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const struct De return HDF_SUCCESS; } #endif + int32_t AttachSpiDevice(struct SpiCntlr *spiCntlr, struct HdfDeviceObject *device) { int32_t ret; @@ -508,7 +568,7 @@ int32_t AttachSpiDevice(struct SpiCntlr *spiCntlr, struct HdfDeviceObject *devic return HDF_ERR_MALLOC_FAIL; } #ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO - ret = GetSpiDeviceResource(spiDevice); + ret = GetSpiDeviceResource(spiDevice, device->deviceMatchAttr); #else ret = GetSpiDeviceResource(spiDevice, device->property); #endif @@ -693,6 +753,7 @@ static int32_t SpiDevGetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) return HDF_SUCCESS; } + static int32_t SpiDevSetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) { struct SpiDevice *spiDevice = NULL; @@ -723,11 +784,11 @@ static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, struct SpiDevice *spiDevice = NULL; struct HAL_SPI_CFG_T *spiDevCfg = NULL; struct SpiMsg *msg = NULL; + int32_t ret; if (spiCntlr == NULL || spiCntlr->priv == NULL) { HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); return HDF_ERR_INVALID_PARAM; } - HDF_LOGI("%s: %u Enter\r\n", __func__, spiId); spiDevice = (struct SpiDevice *)spiCntlr->priv; spiId = spiDevice->spiId; for (size_t i = 0; i < count; i++) { @@ -738,20 +799,23 @@ static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, } if ((msg->wbuf != NULL) && (msg->rbuf == NULL)) { - HalSpiSend(spiDevice, msg->wbuf, msg->len, TIMEOUT); + ret = HalSpiSend(spiDevice, msg->wbuf, msg->len, TIMEOUT); } if ((msg->rbuf != NULL) && (msg->wbuf == NULL)) { - HalSpiRecv(spiDevice, msg->rbuf, msg->len, TIMEOUT); + ret = HalSpiRecv(spiDevice, msg->rbuf, msg->len, TIMEOUT); } if ((msg->wbuf != NULL) && (msg->rbuf != NULL)) { - HalSpiSendRecv(spiDevice, msg->wbuf, msg->len, msg->rbuf, msg->len, TIMEOUT); + ret = HalSpiSendRecv(spiDevice, msg->wbuf, msg->len, msg->rbuf, msg->len); } /* pull pull up cs at the end */ - if (msg->csChange) { + if (msg->keepCs == 0) { hal_gpio_pin_set_dir(spiCtx[spiId].spiPinCS0, HAL_GPIO_DIR_OUT, 1); } + if (ret < 0) { + HDF_LOGE("%s send error!\r\n", __func__); + } DelayUs(msg->delayUs); } return HDF_SUCCESS; -} \ No newline at end of file +} diff --git a/platform/spi/spi_bes.h b/platform/spi/spi_bes.h old mode 100755 new mode 100644 index c274f10444b5349c9416b6c12d268264567b2af1..8cdf91633c0b2fd00a2b863791852121d4c7c5cd --- a/platform/spi/spi_bes.h +++ b/platform/spi/spi_bes.h @@ -1,24 +1,20 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #ifndef __SPI_BES_H__ #define __SPI_BES_H__ #include "hal_spi.h" #include "hal_gpio.h" #include "hal_iomux.h" +#ifdef CHIP_BEST2003 #include "hal_iomux_best2003.h" +#endif #include "osal_mutex.h" #include "osal_sem.h" @@ -79,4 +75,4 @@ struct SpiDevice { } #endif -#endif \ No newline at end of file +#endif diff --git a/platform/spi/spi_stm32f4xx.c b/platform/spi/spi_stm32f4xx.c new file mode 100644 index 0000000000000000000000000000000000000000..f630a1f5e69ce987ba9e73ea432e11b797e194cb --- /dev/null +++ b/platform/spi/spi_stm32f4xx.c @@ -0,0 +1,756 @@ +/* + * Copyright (c) 2022 Talkweb Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include "hal_gpio.h" +#include "hal_spi.h" +#include "osal_mutex.h" +#include "osal_sem.h" +#include "spi_core.h" +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#include "hdf_log.h" +#endif +#include "hdf_base_hal.h" + +#define BITWORD_EIGHT 8 +#define BITWORD_SIXTEEN 16 +#define GPIO_STR_MAX 32 +#define PER_MS_IN_SEC 1000 +typedef enum { + SPI_WORK_MODE_0, // CPOL = 0; CPHA = 0 + SPI_WORK_MODE_2, // CPOL = 1; CPHA = 0 + SPI_WORK_MODE_1, // CPOL = 0; CPHA = 1 + SPI_WORK_MODE_3, // CPOL = 1; CPHA = 1 + SPI_WORD_MODE_MAX, +} SPI_CLK_MODE; + +typedef enum { + SPI_TRANSFER_DMA, + SPI_TRANSFER_NORMAL, + SPI_TRANSFER_MAX, +} SPI_TRANS_MODE; + +typedef enum { + FULL_DUPLEX = 0, + SIMPLE_RX, + HALF_RX, + HALF_TX, + SPI_TRANS_DIR_MAX, +} SPI_TRANS_DIR; + +typedef enum { + SPI_SLAVE_MODE = 0, + SPI_MASTER_MODE, + SPI_MASTER_SLAVE_MAX, +} SPI_SLAVE_MASTER; + +typedef enum { + SPI_DATA_WIDTH_8 = 0, + SPI_DATA_WIDTH_16, + SPI_DATA_WIDTH_MAX, +} SPI_DATA_WIDTH; + +typedef enum { + SPI_NSS_SOFT_MODE = 0, + SPI_NSS_HARD_INPUT_MODE, + SPI_NSS_HARD_OUTPUT_MODE, + SPI_NSS_MODE_MAX, +} SPI_NSS; + +typedef enum { + BAUD_RATE_DIV2 = 0, + BAUD_RATE_DIV4, + BAUD_RATE_DIV8, + BAUD_RATE_DIV16, + BAUD_RATE_DIV32, + BAUD_RATE_DIV64, + BAUD_RATE_DIV128, + BAUD_RATE_DIV256, + BAUD_RATE_DIV_MAX, +} SPI_BAUD_RATE; + +typedef enum { + SPI_MSB_FIRST = 0, + SPI_LSB_FIRST, + SPI_MLSB_MAX, +} SPI_BYTE_ORDER; + +typedef enum { + CRC_DISABLE = 0, + CRC_ENABLE, + CRC_STATE_MAX, +} CRC_CALULATION; + +typedef enum { + SPI_PORT1 = 1, + SPI_PORT2, + SPI_PORT3, +} SPI_GROUPS; + +typedef enum { + SPI_PROTO_MOTOROLA = 0, + SPI_PROTO_TI, + SPI_PROTO_MAX, +} SPI_PROTO_STANDARD; + +typedef struct { + uint8_t busNum; + uint8_t csNum; + SPI_TRANS_DIR transDir; + SPI_TRANS_MODE transMode; + + SPI_SLAVE_MASTER smMode; + SPI_CLK_MODE clkMode; + SPI_DATA_WIDTH dataWidth; + SPI_NSS nss; + + SPI_BAUD_RATE baudRate; + SPI_BYTE_ORDER bitOrder; + CRC_CALULATION crcEnable; + SPI_GROUPS spix; + + STM32_GPIO_PIN csPin; + STM32_GPIO_GROUP csGroup; + SPI_PROTO_STANDARD standard; + uint8_t dummyByte; + + uint16_t crcPoly; +} SpiResource; + +#define HCS_UINT8_PARSE_NUM 16 +static const char *g_parseHcsMap[HCS_UINT8_PARSE_NUM] = { + "busNum", + "csNum", + "transDir", + "transMode", + "smMode", + "clkMode", + "dataWidth", + "nss", + "baudRate", + "bitOrder", + "crcEnable", + "spix", + "csPin", + "csGpiox", + "standard", + "dummyByte", +}; + +typedef struct { + struct OsalSem* sem; + struct OsalMutex* mutex; + SPI_TypeDef* spix; +} SPI_CONTEXT_T; + +typedef struct { + uint32_t spiId; + SpiResource resource; +} SpiDevice; + +static uint32_t g_transDirMaps[SPI_TRANS_DIR_MAX] = { + LL_SPI_FULL_DUPLEX, + LL_SPI_SIMPLEX_RX, + LL_SPI_HALF_DUPLEX_RX, + LL_SPI_HALF_DUPLEX_TX, +}; + +static uint32_t g_nssMaps[SPI_NSS_MODE_MAX] = { + LL_SPI_NSS_SOFT, + LL_SPI_NSS_HARD_INPUT, + LL_SPI_NSS_HARD_OUTPUT, +}; + +static uint32_t g_baudMaps[BAUD_RATE_DIV_MAX] = { + LL_SPI_BAUDRATEPRESCALER_DIV2, + LL_SPI_BAUDRATEPRESCALER_DIV4, + LL_SPI_BAUDRATEPRESCALER_DIV8, + LL_SPI_BAUDRATEPRESCALER_DIV16, + LL_SPI_BAUDRATEPRESCALER_DIV32, + LL_SPI_BAUDRATEPRESCALER_DIV64, + LL_SPI_BAUDRATEPRESCALER_DIV128, + LL_SPI_BAUDRATEPRESCALER_DIV256, +}; + +static SPI_TypeDef* g_spiGroupMaps[SPI_PORT3] = { + SPI1, + SPI2, + SPI3, +}; + +static void EnableSpiClock(uint32_t spiNum) +{ + switch (spiNum) { + case SPI_PORT1: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1); + break; + case SPI_PORT2: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2); + break; + case SPI_PORT3: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI3); + break; + default: + break; + } +} + +static SPI_CONTEXT_T spiContext[SPI_PORT3] = { + { + .sem = {NULL}, + .mutex = {NULL}, + .spix = {NULL}, + }, + { + .sem = {NULL}, + .mutex = {NULL}, + .spix = {NULL}, + }, + { + .sem = {NULL}, + .mutex = {NULL}, + .spix = {NULL}, + }, +}; + +int32_t HalSpiSend(SpiDevice *spiDevice, const uint8_t *data, uint16_t size) +{ + uint32_t spiId; + SpiResource *resource = NULL; + + if (spiDevice == NULL || data == NULL || size == 0) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + spiId = spiDevice->spiId; + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("resource is null\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + if (resource->transMode == SPI_TRANSFER_DMA) { + return HDF_ERR_INVALID_PARAM; // unsupport now + } else { + uint8_t readData; + while (size--) { + readData = LL_SPI_Transmit(spiContext[spiId].spix, *data); + data++; + } + } + + return HDF_SUCCESS; +} + +int32_t HalSpiRecv(SpiDevice *spiDevice, uint8_t *data, uint16_t size) +{ + uint32_t len = size; + uint8_t *cmd = NULL; + uint32_t spiId; + SpiResource *resource = NULL; + if (spiDevice == NULL || data == NULL || size == 0) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + spiId = spiDevice->spiId; + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("resource is null\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + cmd = (uint8_t *)OsalMemAlloc(len); + if (cmd == NULL) { + HDF_LOGE("%s OsalMemAlloc size %ld error\r\n", __FUNCTION__, len); + return HDF_ERR_MALLOC_FAIL; + } + + memset_s(cmd, len, resource->dummyByte, len); + + if (resource->transMode == SPI_TRANSFER_DMA) { + return HDF_ERR_INVALID_PARAM; // unsupport now + } else { + while (len--) { + *data = LL_SPI_Transmit(spiContext[spiId].spix, *cmd); + data++; + cmd++; + } + } + + OsalMemFree(cmd); + return HDF_SUCCESS; +} + +int32_t HalSpiSendRecv(SpiDevice *spiDevice, uint8_t *txData, + uint16_t txSize, uint8_t *rxData, uint16_t rxSize) +{ + uint32_t spiId; + SpiResource *resource = NULL; + if (spiDevice == NULL || txData == NULL || txSize == 0 || rxData == NULL || rxSize == 0) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + spiId = spiDevice->spiId; + resource = &spiDevice->resource; + + if (resource->transMode == SPI_TRANSFER_DMA) { + return HDF_ERR_INVALID_PARAM; // unsupport now + } else { + while (rxSize--) { + *rxData = LL_SPI_Transmit(spiContext[spiId].spix, *txData); + rxData++; + txData++; + } + } + + return HDF_SUCCESS; +} + +static void InitSpiInitStruct(LL_SPI_InitTypeDef *spiInitStruct, const SpiResource *resource) +{ + spiInitStruct->TransferDirection = g_transDirMaps[resource->transDir]; + if (resource->smMode == SPI_SLAVE_MODE) { + spiInitStruct->Mode = LL_SPI_MODE_SLAVE; + } else { + spiInitStruct->Mode = LL_SPI_MODE_MASTER; + } + + if (resource->dataWidth == SPI_DATA_WIDTH_8) { + spiInitStruct->DataWidth = LL_SPI_DATAWIDTH_8BIT; + } else { + spiInitStruct->DataWidth = LL_SPI_DATAWIDTH_16BIT; + } + + switch (resource->clkMode) { + case SPI_WORK_MODE_0: + spiInitStruct->ClockPolarity = LL_SPI_POLARITY_LOW; + spiInitStruct->ClockPhase = LL_SPI_PHASE_1EDGE; + break; + case SPI_WORK_MODE_1: + spiInitStruct->ClockPolarity = LL_SPI_POLARITY_HIGH; + spiInitStruct->ClockPhase = LL_SPI_PHASE_1EDGE; + break; + case SPI_WORK_MODE_2: + spiInitStruct->ClockPolarity = LL_SPI_POLARITY_LOW; + spiInitStruct->ClockPhase = LL_SPI_PHASE_2EDGE; + break; + case SPI_WORK_MODE_3: + spiInitStruct->ClockPolarity = LL_SPI_POLARITY_HIGH; + spiInitStruct->ClockPhase = LL_SPI_PHASE_2EDGE; + break; + default: + spiInitStruct->ClockPolarity = LL_SPI_POLARITY_LOW; + spiInitStruct->ClockPhase = LL_SPI_PHASE_1EDGE; + } + + spiInitStruct->NSS = g_nssMaps[resource->nss]; + spiInitStruct->BaudRate = g_baudMaps[resource->baudRate]; + + if (resource->bitOrder == SPI_MSB_FIRST) { + spiInitStruct->BitOrder = LL_SPI_MSB_FIRST; + } else { + spiInitStruct->BitOrder = LL_SPI_LSB_FIRST; + } + + if (resource->crcEnable == CRC_DISABLE) { + spiInitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE; + } else { + spiInitStruct->CRCCalculation = LL_SPI_CRCCALCULATION_ENABLE; + } + spiInitStruct->CRCPoly = resource->crcPoly; + + return; +} + +static int32_t InitSpiDevice(SpiDevice *spiDevice) +{ + uint32_t spiPort; + SpiResource *resource = NULL; + if (spiDevice == NULL) { + HDF_LOGE("%s: invalid parameter\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + LL_SPI_InitTypeDef spiInitStruct = {0}; + resource = &spiDevice->resource; + spiPort = spiDevice->spiId; + EnableSpiClock(spiPort + 1); + InitSpiInitStruct(&spiInitStruct, resource); + SPI_TypeDef* spix = g_spiGroupMaps[resource->spix]; + + spiContext[spiPort].spix = spix; + LL_SPI_Disable(spix); + uint8_t ret = LL_SPI_Init(spix, &spiInitStruct); + if (ret != 0) { + HDF_LOGE("HAL INIT SPI FAILED\r\n"); + return HDF_FAILURE; + } + if (resource->standard == SPI_PROTO_MOTOROLA) { + LL_SPI_SetStandard(spix, LL_SPI_PROTOCOL_MOTOROLA); + } else { + LL_SPI_SetStandard(spix, LL_SPI_PROTOCOL_TI); + } + + return HDF_SUCCESS; +} + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define SPI_FIND_CONFIG(node, name, resource, spiDevice) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->busNum = HCS_PROP(node, busNum); \ + spiDevice->spiId = resource->busNum; \ + resource->csNum = HCS_PROP(node, csNum); \ + resource->transDir = HCS_PROP(node, transDir); \ + resource->transMode = HCS_PROP(node, transMode); \ + resource->smMode = HCS_PROP(node, smMode); \ + resource->dataWidth = HCS_PROP(node, dataWidth); \ + resource->clkMode = HCS_PROP(node, clkMode); \ + resource->csNum = HCS_PROP(node, csNum); \ + resource->nss = HCS_PROP(node, nss); \ + resource->baudRate = HCS_PROP(node, baudRate); \ + resource->bitOrder = HCS_PROP(node, bitOrder); \ + resource->crcEnable = HCS_PROP(node, crcEnable); \ + resource->crcPoly = HCS_PROP(node, crcPoly); \ + resource->spix = HCS_PROP(node, spix); \ + resource->csPin = HCS_PROP(node, csPin); \ + resource->csGroup = HCS_PROP(node, csGpiox); \ + resource->standard = HCS_PROP(node, standard); \ + resource->dummyByte = HCS_PROP(node, dummyByte); \ + result = HDF_SUCCESS; \ + } \ + } while (0) + +#define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform) +#define PLATFORM_SPI_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), spi_config) +static int32_t GetSpiDeviceResource(SpiDevice *spiDevice, const char *deviceMatchAttr) +{ + int32_t result = HDF_FAILURE; + SpiResource *resource = NULL; + if (spiDevice == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &spiDevice->resource; +#if HCS_NODE_HAS_PROP(PLATFORM_CONFIG, spi_config) + HCS_FOREACH_CHILD_VARGS(PLATFORM_SPI_CONFIG, SPI_FIND_CONFIG, deviceMatchAttr, resource, spiDevice); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + } + return result; +} +#else +static int32_t GetSpiDeviceResource(SpiDevice *spiDevice, const struct DeviceResourceNode *resourceNode) +{ + struct DeviceResourceIface *dri = NULL; + if (spiDevice == NULL || resourceNode == NULL) { + HDF_LOGE("%s: PARAM is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + SpiResource *resource = NULL; + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); // open HDF + if (dri == NULL || dri->GetUint16 == NULL || dri->GetUint8 == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_PARAM; + } + uint8_t temp[HCS_UINT8_PARSE_NUM] = {0}; + for (int i = 0; i < HCS_UINT8_PARSE_NUM; i++) { + if (dri->GetUint8(resourceNode, g_parseHcsMap[i], &temp[i], 0) != HDF_SUCCESS) { + HDF_LOGE("get config %s failed\r\n", g_parseHcsMap[i]); + return HDF_FAILURE; + } + } + int ret = memcpy_s(resource, HCS_UINT8_PARSE_NUM, temp, HCS_UINT8_PARSE_NUM); + if (ret != 0) { + HDF_LOGE("memcpy failed\r\n"); + return HDF_FAILURE; + } + if (dri->GetUint16(resourceNode, "crcPoly", &resource->crcPoly, 0) != HDF_SUCCESS) { + HDF_LOGE("get config %s failed\r\n", "crcPoly"); + return HDF_FAILURE; + } + spiDevice->spiId = resource->busNum; + + return HDF_SUCCESS; +} +#endif + +int32_t AttachSpiDevice(struct SpiCntlr *spiCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + SpiDevice *spiDevice = NULL; + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (spiCntlr == NULL || device == NULL) { +#else + if (spiCntlr == NULL || device == NULL || device->property == NULL) { +#endif + HDF_LOGE("%s: property is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + spiDevice = (SpiDevice *)OsalMemAlloc(sizeof(SpiDevice)); + if (spiDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc spiDevice error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetSpiDeviceResource(spiDevice, device->deviceMatchAttr); +#else + ret = GetSpiDeviceResource(spiDevice, device->property); +#endif + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(spiDevice); + return HDF_FAILURE; + } + + spiCntlr->priv = spiDevice; + spiCntlr->busNum = spiDevice->spiId; + InitSpiDevice(spiDevice); + + return HDF_SUCCESS; +} +/* SPI Method */ +static int32_t SpiDevGetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg); +static int32_t SpiDevSetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg); +static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, uint32_t count); +static int32_t SpiDevOpen(struct SpiCntlr *spiCntlr); +static int32_t SpiDevClose(struct SpiCntlr *spiCntlr); + +struct SpiCntlrMethod g_twSpiCntlrMethod = { + .GetCfg = SpiDevGetCfg, + .SetCfg = SpiDevSetCfg, + .Transfer = SpiDevTransfer, + .Open = SpiDevOpen, + .Close = SpiDevClose, +}; + +/* HdfDriverEntry method definitions */ +static int32_t SpiDriverBind(struct HdfDeviceObject *device); +static int32_t SpiDriverInit(struct HdfDeviceObject *device); +static void SpiDriverRelease(struct HdfDeviceObject *device); + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_SpiDriverEntry = { + .moduleVersion = 1, + .moduleName = "ST_SPI_MODULE_HDF", + .Bind = SpiDriverBind, + .Init = SpiDriverInit, + .Release = SpiDriverRelease, +}; + +HDF_INIT(g_SpiDriverEntry); + +static int32_t SpiDriverBind(struct HdfDeviceObject *device) +{ + struct SpiCntlr *spiCntlr = NULL; + if (device == NULL) { + HDF_LOGE("Sample device object is null!\r\n"); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGI("Enter %s:\r\n", __func__); + spiCntlr = SpiCntlrCreate(device); + if (spiCntlr == NULL) { + HDF_LOGE("SpiCntlrCreate object is null!\r\n"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t SpiDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct SpiCntlr *spiCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + HDF_LOGI("Enter %s:", __func__); + spiCntlr = SpiCntlrFromDevice(device); + if (spiCntlr == NULL) { + HDF_LOGE("%s: spiCntlr is NULL", __func__); + return HDF_DEV_ERR_NO_DEVICE; + } + + ret = AttachSpiDevice(spiCntlr, device); // SpiCntlr add TWSpiDevice to priv + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: attach error\r\n", __func__); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + spiCntlr->method = &g_twSpiCntlrMethod; // register callback + + return ret; +} + +static void SpiDriverRelease(struct HdfDeviceObject *device) +{ + struct SpiCntlr *spiCntlr = NULL; + SpiDevice *spiDevice = NULL; + + HDF_LOGI("Enter %s\r\n", __func__); + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return; + } + + spiCntlr = SpiCntlrFromDevice(device); + if (spiCntlr == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return; + } + + spiDevice = (SpiDevice *)spiCntlr->priv; + OsalMemFree(spiDevice); + + return; +} + +static int32_t SpiDevOpen(struct SpiCntlr *spiCntlr) +{ + HDF_LOGI("Enter %s\r\n", __func__); + SpiDevice *spiDevice = NULL; + + spiDevice = (SpiDevice*)spiCntlr->priv; + SPI_TypeDef* spix = g_spiGroupMaps[spiDevice->resource.spix]; + LL_SPI_Enable(spix); + + return HDF_SUCCESS; +} + +static int32_t SpiDevClose(struct SpiCntlr *spiCntlr) +{ + SpiDevice *spiDevice = NULL; + + spiDevice = (SpiDevice*)spiCntlr->priv; + SPI_TypeDef* spix = g_spiGroupMaps[spiDevice->resource.spix]; + LL_SPI_Disable(spix); + + return HDF_SUCCESS; +} + +static int32_t SpiDevGetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) +{ + SpiDevice *spiDevice = NULL; + if (spiCntlr == NULL || spiCfg == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + spiDevice = (SpiDevice *)spiCntlr->priv; + if (spiDevice == NULL) { + return HDF_DEV_ERR_NO_DEVICE; + } + spiCfg->maxSpeedHz = spiDevice->resource.baudRate; + spiCfg->mode = spiDevice->resource.clkMode; + spiCfg->transferMode = spiDevice->resource.transMode; + spiCfg->bitsPerWord = spiDevice->resource.dataWidth; + + if (spiDevice->resource.dataWidth) { + spiCfg->bitsPerWord = BITWORD_SIXTEEN; + } else { + spiCfg->bitsPerWord = BITWORD_EIGHT; + } + + return HDF_SUCCESS; +} +static int32_t SpiDevSetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) +{ + SpiDevice *spiDevice = NULL; + if (spiCntlr == NULL || spiCfg == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + spiDevice = (SpiDevice *)spiCntlr->priv; + if (spiDevice == NULL) { + HDF_LOGE("%s: spiDevice is NULL\r\n", __func__); + return HDF_DEV_ERR_NO_DEVICE; + } + + spiDevice->resource.baudRate = spiCfg->maxSpeedHz; + spiDevice->resource.clkMode = spiCfg->mode; + spiDevice->resource.transMode = spiCfg->transferMode; + if (spiCfg->bitsPerWord == BITWORD_EIGHT) { + spiDevice->resource.dataWidth = SPI_DATA_WIDTH_8; + } else { + spiDevice->resource.dataWidth = SPI_DATA_WIDTH_16; + } + + return InitSpiDevice(spiDevice); +} + +static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, uint32_t count) +{ + SpiDevice *spiDevice = NULL; + uint32_t ticks = 0; + uint8_t singleCsChange = 0; + struct SpiMsg *msg = NULL; + if (spiCntlr == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + spiDevice = (SpiDevice *)spiCntlr->priv; + for (size_t i = 0; i < count; i++) { + msg = &spiMsg[i]; + LL_GPIO_ResetOutputPin(LL_GET_GPIOX(spiDevice->resource.csGroup), LL_GET_HAL_PIN(spiDevice->resource.csPin)); + + if ((msg->rbuf == NULL) && (msg->wbuf != NULL)) { + singleCsChange = msg->wbuf[0]; + + if (msg->len == 1) { + goto CS_DOWN; + } + HalSpiSend(spiDevice, msg->wbuf + 1, msg->len - 1); + } + + if ((msg->rbuf != NULL) && (msg->wbuf == NULL)) { + singleCsChange = msg->rbuf[0]; + + if (msg->len == 1) { + goto CS_DOWN; + } + HalSpiRecv(spiDevice, msg->rbuf + 1, msg->len - 1); + } + + if ((msg->wbuf != NULL) && (msg->rbuf != NULL)) { + HalSpiSendRecv(spiDevice, msg->wbuf, msg->len, msg->rbuf, msg->len); + } + + if (msg->keepCs == 0|| singleCsChange) { + LL_GPIO_SetOutputPin(LL_GET_GPIOX(spiDevice->resource.csGroup), LL_GET_HAL_PIN(spiDevice->resource.csPin)); + } + if (msg->delayUs > 0) { + ticks = (msg->delayUs / PER_MS_IN_SEC); + osDelay(ticks); + } + } + return HDF_SUCCESS; +CS_DOWN: + if (msg->keepCs == 0 || singleCsChange) { + LL_GPIO_SetOutputPin(LL_GET_GPIOX(spiDevice->resource.csGroup), LL_GET_HAL_PIN(spiDevice->resource.csPin)); + } + return HDF_SUCCESS; +} + diff --git a/platform/spi/spi_wm.c b/platform/spi/spi_wm.c new file mode 100755 index 0000000000000000000000000000000000000000..61cd9d943e0e9124ef87a0637533d590c50ab3bb --- /dev/null +++ b/platform/spi/spi_wm.c @@ -0,0 +1,508 @@ +/* + * Copyright (c) 2022 Jiangsu Hoperun Software Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include "hdf_log.h" +#include "spi_core.h" +#include "spi_if.h" +#include "device_resource_if.h" +#include "wm_gpio_afsel.h" +#include "osal_mutex.h" +#include "osal_sem.h" +#include "wm_hostspi.h" + +#define SPI_DMA_MAX 4095 +#define MAX_SPI_NUMBER 1 +#define TIMEOUT 1000 + +struct SpiResource { + uint32_t num; + uint32_t speed; + enum SpiTransferMode transmode; + uint32_t mode; // TLS_SPI_MODE_x + uint32_t dataSize; + uint32_t spiCsSoft; + uint32_t spiClkPin; + uint32_t spiMosiPin; + uint32_t spiMisoPin; + uint32_t spiCsPin; +}; + +struct SpiDevice { + uint32_t spiId; + struct SpiResource resource; + struct OsalMutex mutex; +}; + +static void SpiIomuxInit(struct SpiDevice *spiDevice) +{ + struct SpiResource *resource = NULL; + uint32_t spiPort; + HDF_LOGI("%s: Enter\r\n", __func__); + + if (spiDevice == NULL) { + HDF_LOGE("%s: invalid parameter\r\n", __func__); + return; + } + + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("resource is null\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + spiPort = spiDevice->spiId; + + if (resource->spiCsSoft) { + tls_gpio_cfg(WM_IO_PB_00 + resource->spiCsPin, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_PULLHIGH); + } + wm_spi_cs_config(WM_IO_PB_04); + wm_spi_ck_config(WM_IO_PB_02); + wm_spi_di_config(WM_IO_PB_03); + wm_spi_do_config(WM_IO_PB_05); +} + +/** + * Spi send + * + * @param[in] spiId the spi bus id + * @param[in] data spi send data + * @param[in] size spi send data size + * @param[in] timeOut timeOut in milisecond, set this value to HAL_WAIT_FOREVER + * if you want to wait forever + * + * @return 0 : on success, EIO : if the SPI device could not be initialised + */ +int32_t HalSpiSend(struct SpiDevice *spiDevice, const uint8_t *data, uint16_t size, uint32_t timeOut) +{ + int32_t ret = 0; + uint32_t spiId; + uint32_t len = size; + struct SpiResource *resource = NULL; + int32_t status = HDF_FAILURE; + + if (spiDevice == NULL || data == NULL || size == 0) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + spiId = spiDevice->spiId; + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("resource is null\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + status = OsalMutexLock(&spiDevice->mutex); + if (HDF_SUCCESS != status) { + HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); + return HDF_ERR_TIMEOUT; + } + ret = tls_spi_write(data, (uint32_t)size); + if (ret) { + HDF_LOGE("spi tail send fail %ld, size %ld\r\n", ret, len); + } + OsalMutexUnlock(&spiDevice->mutex); + return ret; +} + +/** + * SpiRecv + * + * @param[in] spiId the spi bus id + * @param[out] data spi recv data + * @param[in] size spi recv data size + * @param[in] timeOut timeOut in milisecond, set this value to HAL_WAIT_FOREVER + * if you want to wait forever + * + * @return 0 : on success, EIO : if the SPI device could not be initialised + */ +int32_t HalSpiRecv(struct SpiDevice *spiDevice, uint8_t *data, uint16_t size, uint32_t timeOut) +{ + int32_t ret = 0; + uint32_t len = size; + uint32_t remainder = 0; + int32_t status = HDF_FAILURE; + uint8_t *cmd = NULL; + uint32_t spiId; + struct SpiResource *resource = NULL; + if (spiDevice == NULL || data == NULL || size == 0) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + spiId = spiDevice->spiId; + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("resource is null\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + cmd = (uint8_t *)OsalMemAlloc(len); + if (cmd == NULL) { + HDF_LOGE("%s OsalMemAlloc size %ld error\r\n", __FUNCTION__, len); + return HDF_ERR_MALLOC_FAIL; + } + + memset_s(cmd, len, 0, len); + + status = OsalMutexLock(&spiDevice->mutex); + if (HDF_SUCCESS != status) { + HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); + OsalMemFree(cmd); + return HDF_ERR_TIMEOUT; + } + remainder = len <= SPI_DMA_MAX ? len : SPI_DMA_MAX; + + ret = tls_spi_read(data, remainder); + + len -= remainder; + data += remainder; + + if (ret) { + HDF_LOGE("spi tail fail %ld, size %ld\r\n", ret, len); + } + OsalMutexUnlock(&spiDevice->mutex); + OsalMemFree(cmd); + return ret; +} + +int32_t HalSpiSendRecv(struct SpiDevice *spiDevice, uint8_t *txData, uint16_t txSize, uint8_t *rxData, + uint16_t rxSize) +{ + int32_t ret; + int32_t status; + uint32_t spiId; + struct SpiResource *resource = NULL; + if (spiDevice == NULL || txData == NULL || txSize == 0 || rxData == NULL || rxSize == 0) { + HDF_LOGE("spi input para err\r\n"); + return HDF_ERR_INVALID_PARAM; + } + spiId = spiDevice->spiId; + resource = &spiDevice->resource; + status = OsalMutexLock(&spiDevice->mutex); + if (HDF_SUCCESS != status) { + HDF_LOGE("%s spi_mutex wait error = 0x%X!\r\n", __func__, status); + return HDF_ERR_TIMEOUT; + } + ret = tls_spi_xfer(txData, rxData, txSize, rxSize); + if (ret) { + HDF_LOGE("spi dma tail fail %d\r\n", ret); + } + OsalMutexUnlock(&spiDevice->mutex); + return ret; +} + +static int32_t InitSpiDevice(struct SpiDevice *spiDevice) +{ + uint32_t spiPort; + struct SpiResource *resource = NULL; + if (spiDevice == NULL) { + HDF_LOGE("%s: invalid parameter\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + resource = &spiDevice->resource; + spiPort = spiDevice->spiId; + + SpiIomuxInit(spiDevice); + + if (spiDevice->mutex.realMutex == NULL) { + if (OsalMutexInit(&spiDevice->mutex) != HDF_SUCCESS) { + HDF_LOGE("spi Mutex create failed!\r\n"); + return HDF_FAILURE; + } + } + tls_spi_init(); + return HDF_SUCCESS; +} + +/* get spi config from hcs file */ +static int32_t GetSpiDeviceResource(struct SpiDevice *spiDevice, const struct DeviceResourceNode *resourceNode) +{ + uint32_t relPin; + struct SpiResource *resource = NULL; + struct DeviceResourceIface *dri = NULL; + if (spiDevice == NULL || resourceNode == NULL) { + HDF_LOGE("%s: PARAM is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &spiDevice->resource; + if (resource == NULL) { + HDF_LOGE("%s: resource is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); // open HDF + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + if (dri->GetUint32(resourceNode, "num", &resource->num, 0) != HDF_SUCCESS) { + HDF_LOGE("spi config read num fail\r\n"); + return HDF_FAILURE; + } + spiDevice->spiId = resource->num; + + if (dri->GetUint32(resourceNode, "speed", &resource->speed, 0) != HDF_SUCCESS) { + HDF_LOGE("spi config read base fail\r\n"); + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "transmode", &resource->transmode, 0) != HDF_SUCCESS) { + HDF_LOGE("spi config read transmode fail\r\n"); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +int32_t AttachSpiDevice(struct SpiCntlr *spiCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + struct SpiDevice *spiDevice = NULL; + + if (spiCntlr == NULL || device == NULL || device->property == NULL) { + HDF_LOGE("%s: property is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + spiDevice = (struct SpiDevice *)OsalMemAlloc(sizeof(struct SpiDevice)); + if (spiDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc spiDevice error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + spiDevice->mutex.realMutex = NULL; + + ret = GetSpiDeviceResource(spiDevice, device->property); + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(spiDevice); + return HDF_FAILURE; + } + + spiCntlr->priv = spiDevice; + spiCntlr->busNum = spiDevice->spiId; + return InitSpiDevice(spiDevice); +} +/* SPI Method */ +static int32_t SpiDevGetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg); +static int32_t SpiDevSetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg); +static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, uint32_t count); +static int32_t SpiDevOpen(struct SpiCntlr *spiCntlr); +static int32_t SpiDevClose(struct SpiCntlr *spiCntlr); + +struct SpiCntlrMethod g_SpiCntlrMethod = { + .GetCfg = SpiDevGetCfg, + .SetCfg = SpiDevSetCfg, + .Transfer = SpiDevTransfer, + .Open = SpiDevOpen, + .Close = SpiDevClose, +}; + +/* HdfDriverEntry method definitions */ +static int32_t SpiDriverBind(struct HdfDeviceObject *device); +static int32_t SpiDriverInit(struct HdfDeviceObject *device); +static void SpiDriverRelease(struct HdfDeviceObject *device); + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_SpiDriverEntry = { + .moduleVersion = 1, + .moduleName = "W800_SPI_MODULE_HDF", + .Bind = SpiDriverBind, + .Init = SpiDriverInit, + .Release = SpiDriverRelease, +}; + +HDF_INIT(g_SpiDriverEntry); + +static int32_t SpiDriverBind(struct HdfDeviceObject *device) +{ + struct SpiCntlr *spiCntlr = NULL; + if (device == NULL) { + HDF_LOGE("Sample device object is null!\r\n"); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGI("Enter %s:\r\n", __func__); + spiCntlr = (struct SpiCntlr *)OsalMemAlloc(sizeof(struct SpiCntlr)); + if (spiCntlr == NULL) { + HDF_LOGE("%s: host is NULL\r\n", __func__); + return HDF_FAILURE; + } + device->service = &spiCntlr->service; + spiCntlr->device = device; + spiCntlr->priv = NULL; + return HDF_SUCCESS; +} + +static int32_t SpiDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct SpiCntlr *spiCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + HDF_LOGI("Enter %s:", __func__); + spiCntlr = SpiCntlrFromDevice(device); + if (spiCntlr == NULL) { + HDF_LOGE("%s: spiCntlr is NULL", __func__); + return HDF_DEV_ERR_NO_DEVICE; + } + + ret = AttachSpiDevice(spiCntlr, device); // SpiCntlr add SpiDevice to priv + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: attach error\r\n", __func__); + return HDF_DEV_ERR_ATTACHDEV_FAIL; + } + + spiCntlr->method = &g_SpiCntlrMethod; // register callback + + return ret; +} + +static void SpiDriverRelease(struct HdfDeviceObject *device) +{ + struct SpiCntlr *spiCntlr = NULL; + struct SpiDevice *spiDevice = NULL; + + HDF_LOGI("Enter %s\r\n", __func__); + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return; + } + + spiCntlr = SpiCntlrFromDevice(device); + if (spiCntlr == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return; + } + + spiDevice = (struct SpiDevice *)spiCntlr->priv; + if (spiDevice != NULL) { + OsalMemFree(spiDevice); + } + return; +} + +static int32_t SpiDevOpen(struct SpiCntlr *spiCntlr) +{ + HDF_LOGI("Enter %s\r\n", __func__); + int ret; + struct SpiDevice *spiDevice = NULL; + struct SpiResource *resource = NULL; + if (spiCntlr == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + spiDevice = (struct SpiDevice *)spiCntlr->priv; + if (spiDevice == NULL) { + HDF_LOGE("spi device is NULL\r\n"); + return HDF_DEV_ERR_NO_DEVICE; + } + resource = &spiDevice->resource; + + if (resource->transmode == SPI_DMA_TRANSFER) { + tls_spi_trans_type(SPI_USE_DMA_TRANSFER); + } + if (resource->dataSize == SPI_MASTER_FIFO_SIZE) { + tls_spi_trans_type(SPI_WORD_TRANSFER); + } + tls_spi_setup(resource->mode, TLS_SPI_CS_LOW, resource->speed); + return HDF_SUCCESS; +} + +static int32_t SpiDevClose(struct SpiCntlr *spiCntlr) +{ + int ret; + struct SpiDevice *spiDevice = NULL; + if (spiCntlr == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + spiDevice = (struct SpiDevice *)spiCntlr->priv; + if (spiDevice == NULL) { + HDF_LOGE("spi device is NULL\r\n"); + return HDF_DEV_ERR_NO_DEVICE; + } + return HDF_SUCCESS; +} + +static int32_t SpiDevGetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) +{ + struct SpiDevice *spiDevice = NULL; + if (spiCntlr == NULL || spiCfg == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + spiDevice = (struct SpiDevice *)spiCntlr->priv; + if (spiDevice == NULL) { + return HDF_DEV_ERR_NO_DEVICE; + } + spiCfg->maxSpeedHz = spiDevice->resource.speed; + spiCfg->mode = spiDevice->resource.mode; + spiCfg->transferMode = spiDevice->resource.transmode; + spiCfg->bitsPerWord = spiDevice->resource.dataSize; + + return HDF_SUCCESS; +} +static int32_t SpiDevSetCfg(struct SpiCntlr *spiCntlr, struct SpiCfg *spiCfg) +{ + struct SpiDevice *spiDevice = NULL; + if (spiCntlr == NULL || spiCfg == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + spiDevice = (struct SpiDevice *)spiCntlr->priv; + if (spiDevice == NULL) { + return HDF_DEV_ERR_NO_DEVICE; + } + spiDevice->resource.speed = spiCfg->maxSpeedHz; + spiDevice->resource.mode = spiCfg->mode; + spiDevice->resource.transmode = spiCfg->transferMode; + spiDevice->resource.dataSize = spiCfg->bitsPerWord; + return SpiDevOpen(spiCntlr); +} + +static int32_t SpiDevTransfer(struct SpiCntlr *spiCntlr, struct SpiMsg *spiMsg, uint32_t count) +{ + uint32_t spiId; + struct SpiDevice *spiDevice = NULL; + struct SpiMsg *msg = NULL; + if (spiCntlr == NULL || spiCntlr->priv == NULL) { + HDF_LOGE("%s: spiCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGI("%s: %u Enter\r\n", __func__, spiId); + spiDevice = (struct SpiDevice *)spiCntlr->priv; + spiId = spiDevice->spiId; + for (size_t i = 0; i < count; i++) { + msg = &spiMsg[i]; + /* pull down cs at first */ + if (spiDevice->resource.spiCsSoft) { + tls_gpio_write(WM_IO_PB_00 + spiDevice->resource.spiCsPin, 0); + } + + if ((msg->wbuf != NULL) && (msg->rbuf == NULL)) { + HalSpiSend(spiDevice, msg->wbuf, msg->len, TIMEOUT); + } + if ((msg->rbuf != NULL) && (msg->wbuf == NULL)) { + HalSpiRecv(spiDevice, msg->rbuf, msg->len, TIMEOUT); + } + if ((msg->wbuf != NULL) && (msg->rbuf != NULL)) { + HalSpiSendRecv(spiDevice, msg->wbuf, msg->len, msg->rbuf, msg->len); + } + + /* pull pull up cs at the end */ + if (msg->keepCs == 0 && spiDevice->resource.spiCsSoft) { + tls_gpio_write(WM_IO_PB_00 + spiDevice->resource.spiCsPin, 1); + } + } + return HDF_SUCCESS; +} diff --git a/platform/uart/BUILD.gn b/platform/uart/BUILD.gn index e7558566a37370544fc6282c2f13eceb951fc9ae..4d4b3711f5a7d757fa20291a40240a2ab306c0ff 100644 --- a/platform/uart/BUILD.gn +++ b/platform/uart/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") @@ -40,6 +19,29 @@ hdf_driver(module_name) { if (defined(LOSCFG_SOC_COMPANY_GOODIX)) { sources += [ "uart_gr5xx.c" ] } + if (defined(LOSCFG_SOC_COMPANY_ASRMICRO)) { + sources += [ "uart_asr.c" ] + } + if (defined(LOSCFG_SOC_COMPANY_WINNERMICRO)) { + sources += [ "uart_wm.c" ] + } + if (defined(LOSCFG_SOC_SERIES_STM32F4xx)) { + sources += [ "uart_stm32f4xx.c" ] + } include_dirs = [ "." ] + if (defined(LOSCFG_SHIELD_V200ZR_EVB_T1) && + defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + deps = [ "//device/board/fnlink/shields" ] + } + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/platform/uart/uart_asr.c b/platform/uart/uart_asr.c new file mode 100644 index 0000000000000000000000000000000000000000..d97ec440e6ae4984dcbde13fbb739d7e1a85479e --- /dev/null +++ b/platform/uart/uart_asr.c @@ -0,0 +1,695 @@ +/* + * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "uart/uart_core.h" +#include "device_resource_if.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "los_sem.h" +#include "osal_mem.h" +#include "duet_pinmux.h" +#include "duet_uart.h" + +#define ASR_UART_NUM DUET_UART_NUM +#define asr_uart_config_t duet_uart_config_t +#define asr_uart_dev_t duet_uart_dev_t +#define asr_uart_struct_init duet_uart_struct_init +#define asr_uart_dma_config duet_uart_dma_config +#define asr_uart_init duet_uart_init +#define asr_uart_send duet_uart_send +#define asr_uart_finalize duet_uart_finalize +#define asr_uart_start duet_uart_start +#define asr_uart_stop duet_uart_stop +#define asr_uart_set_callback duet_uart_set_callback +#define asr_uart_calc_baud duet_uart_calc_baud +#define asr_uart_interrupt_config duet_uart_interrupt_config +#define asr_uart_clear_interrupt duet_uart_clear_interrupt +#define asr_uart_get_raw_interrupt_status duet_uart_get_raw_interrupt_status +#define asr_uart_get_interrupt_status duet_uart_get_interrupt_status +#define asr_uart_get_flag_status duet_uart_get_flag_status +#define asr_uart_callback_func duet_uart_callback_func +#define asr_pinmux_config duet_pinmux_config + +#define HDF_LOG_TAG uart_asr + +#define DEFAULT_BAUDRATE 115200 +#define DEFAULT_DATABITS UART_ATTR_DATABIT_8 +#define DEFAULT_STOPBITS UART_ATTR_STOPBIT_1 +#define DEFAULT_PARITY UART_ATTR_PARITY_NONE +#define CONFIG_MAX_BAUDRATE 921600 +#define UART_STATE_NOT_OPENED 0 +#define UART_STATE_OPENING 1 +#define UART_STATE_USEABLE 2 +#define UART_STATE_SUSPENED 3 +#define UART_FLG_DMA_RX (1 << 0) +#define UART_FLG_DMA_TX (1 << 1) +#define UART_FLG_RD_BLOCK (1 << 2) +#define UART_TRANS_TIMEOUT 1000 +#define UART_RX_BUF_LEN 512 + +typedef int32_t (*app_uart_cfg_handler_t)(struct UartDriverData *udd); +struct UartResource { + uint32_t port; + uint32_t pin_tx_pin; + uint32_t pin_tx_mux; + uint32_t pin_rx_pin; + uint32_t pin_rx_mux; + uint32_t tx_rx; +}; + +struct UartDriverData { + asr_uart_dev_t params; + struct UartAttribute attr; + struct UartResource resource; + app_uart_cfg_handler_t config; + int32_t count; + int32_t state; + uint32_t flags; +}; + +static uint32_t g_uartTxMutex[ASR_UART_NUM]; +static uint32_t g_uartRxMutex[ASR_UART_NUM]; +static uint8_t *rx_buf[ASR_UART_NUM]; +static uint16_t rx_head[ASR_UART_NUM]; +static uint16_t rx_tail[ASR_UART_NUM]; + +static void Uart0Callback(char data); +static void Uart1Callback(char data); +static void Uart2Callback(char data); + +static const asr_uart_callback_func g_evtHandler[ASR_UART_NUM] = { + Uart0Callback, + Uart1Callback, + Uart2Callback +}; + +static void Uart0Callback(char data) +{ + uint8_t *dst = rx_buf[UART0_INDEX]; + if (dst) { + dst[rx_head[UART0_INDEX]++] = (uint8_t)data; + rx_head[UART0_INDEX] %= UART_RX_BUF_LEN; + } +} + +static void Uart1Callback(char data) +{ + uint8_t *dst = rx_buf[UART1_INDEX]; + if (dst) { + dst[rx_head[UART1_INDEX]++] = (uint8_t)data; + rx_head[UART1_INDEX] %= UART_RX_BUF_LEN; + } +} +static void Uart2Callback(char data) +{ + uint8_t *dst = rx_buf[UART2_INDEX]; + if (dst) { + dst[rx_head[UART2_INDEX]++] = (uint8_t)data; + rx_head[UART2_INDEX] %= UART_RX_BUF_LEN; + } +} + +static uint32_t GetUartDataBits(uint32_t attrDataBits) +{ + uint32_t dataBits; + + switch (attrDataBits) { + case UART_ATTR_DATABIT_5: + dataBits = DATA_5BIT; + break; + case UART_ATTR_DATABIT_6: + dataBits = DATA_6BIT; + break; + case UART_ATTR_DATABIT_7: + dataBits = DATA_7BIT; + break; + case UART_ATTR_DATABIT_8: + dataBits = DATA_8BIT; + break; + default: + dataBits = DATA_8BIT; + break; + } + + return dataBits; +} + +static uint32_t GetUartStopBits(uint32_t attrStopBits) +{ + uint32_t stopBits; + + switch (attrStopBits) { + case UART_ATTR_STOPBIT_1: + stopBits = STOP_1BIT; + break; + case UART_ATTR_STOPBIT_2: + stopBits = STOP_2BITS; + break; + default: + stopBits = STOP_1BIT; + break; + } + + return stopBits; +} + +static uint32_t GetUartParity(uint32_t attrParity) +{ + uint32_t parity; + + switch (attrParity) { + case UART_ATTR_PARITY_NONE: + parity = PARITY_NO; + break; + case UART_ATTR_PARITY_ODD: + parity = PARITY_ODD; + break; + case UART_ATTR_PARITY_EVEN: + parity = PARITY_EVEN; + break; + default: + parity = PARITY_NO; + break; + } + + return parity; +} + +static uint32_t GetUartFlowControl(uint32_t rts, uint32_t cts) +{ + uint32_t flow_control; + + if (!rts && !cts) { + flow_control = FLOW_CTRL_DISABLED; + } else if (rts && cts) { + flow_control = FLOW_CTRL_CTS_RTS; + } else if (rts) { + flow_control = FLOW_CTRL_RTS; + } else { + flow_control = FLOW_CTRL_CTS; + } + + return flow_control; +} + +static int32_t Asr582xUartConfig(struct UartDriverData *udd) +{ + uint32_t ret; + asr_uart_dev_t *params = NULL; + + if (udd == NULL) { + return HDF_FAILURE; + } + asr_pinmux_config(udd->resource.pin_tx_pin, udd->resource.pin_tx_mux); + asr_pinmux_config(udd->resource.pin_rx_pin, udd->resource.pin_rx_mux); + params = &udd->params; + params->port = udd->resource.port; + params->config.data_width = GetUartDataBits(udd->attr.dataBits); + params->config.stop_bits = GetUartStopBits(udd->attr.stopBits); + params->config.parity = GetUartParity(udd->attr.parity); + params->config.flow_control = GetUartFlowControl(udd->attr.rts, udd->attr.cts); + params->config.mode = udd->resource.tx_rx; + params->priv = (void *)g_evtHandler[udd->resource.port]; + + ret = asr_uart_init(params); + if (ret != 0) { + HDF_LOGE("%s , app uart init failed\r\n", __func__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size) +{ + int32_t ret; + uint32_t uwRet = 0; + uint32_t recv_len = 0; + struct UartDriverData *udd = NULL; + uint8_t port = 0; + uint8_t *src = NULL; + + if ((host == NULL) || (host->priv == NULL) || (data == NULL)) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + udd = (struct UartDriverData *)host->priv; + port = udd->resource.port; + src = rx_buf[port]; + if (udd->state != UART_STATE_USEABLE) { + HDF_LOGE("%s: uart_%d not useable", __func__, port); + return HDF_FAILURE; + } + + LOS_MuxPend(g_uartRxMutex[port], LOS_WAIT_FOREVER); + if (udd->flags & UART_FLG_RD_BLOCK) { + while (recv_len != size) { + if (rx_head[port] != rx_tail[port]) { + data[recv_len++] = src[rx_tail[port]++]; + rx_tail[port] %= UART_RX_BUF_LEN; + } + } + } else { + while ((recv_len != size) && (rx_head[port] != rx_tail[port])) { + data[recv_len++] = src[rx_tail[port]++]; + rx_tail[port] %= UART_RX_BUF_LEN; + } + } + LOS_MuxPost(g_uartRxMutex[port]); + + return recv_len; +} + +static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size) +{ + int32_t ret; + struct UartDriverData *udd = NULL; + + if ((host == NULL) || (host->priv == NULL) || (data == NULL)) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_USEABLE) { + HDF_LOGE("%s: uart_%d not useable", __func__, udd->resource.port); + return HDF_FAILURE; + } + + LOS_MuxPend(g_uartTxMutex[udd->resource.port], LOS_WAIT_FOREVER); + ret = asr_uart_send(&udd->params, data, size, UART_TRANS_TIMEOUT); + if (ret != 0) { + LOS_MuxPost(g_uartTxMutex[udd->resource.port]); + HDF_LOGE("%s: uart_%d send %d data failed", __func__, udd->resource.port, size); + return HDF_FAILURE; + } + LOS_MuxPost(g_uartTxMutex[udd->resource.port]); + + return HDF_SUCCESS; +} + +static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) +{ + struct UartDriverData *udd = NULL; + + if (host == NULL || host->priv == NULL || baudRate == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_USEABLE) { + HDF_LOGE("%s: uart_%d not useable", __func__, udd->resource.port); + return HDF_FAILURE; + } + *baudRate = udd->params.config.baud_rate; + + return HDF_SUCCESS; +} + +static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) +{ + struct UartDriverData *udd = NULL; + + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_USEABLE) { + HDF_LOGE("%s: uart_%d not useable", __func__, udd->resource.port); + return HDF_FAILURE; + } + if ((baudRate > 0) && (baudRate <= CONFIG_MAX_BAUDRATE)) { + udd->params.config.baud_rate = baudRate; + if (udd->config == NULL) { + HDF_LOGE("%s: not support", __func__); + return HDF_ERR_NOT_SUPPORT; + } + if (udd->config(udd) != HDF_SUCCESS) { + HDF_LOGE("%s: config baudrate %d failed", __func__, baudRate); + return HDF_FAILURE; + } + } else { + HDF_LOGE("%s: invalid baudrate, which is:%d", __func__, baudRate); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + struct UartDriverData *udd = NULL; + + if (host == NULL || host->priv == NULL || attribute == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_USEABLE) { + return HDF_FAILURE; + } + + *attribute = udd->attr; + + return HDF_SUCCESS; +} + +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + struct UartDriverData *udd = NULL; + + if (host == NULL || host->priv == NULL || attribute == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_USEABLE) { + HDF_LOGE("%s: uart_%d not useable", __func__, udd->resource.port); + return HDF_FAILURE; + } + + udd->attr = *attribute; + if (udd->config == NULL) { + HDF_LOGE("%s: not support", __func__); + return HDF_ERR_NOT_SUPPORT; + } + if (udd->config(udd) != HDF_SUCCESS) { + HDF_LOGE("%s: config failed", __func__); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode) +{ + struct UartDriverData *udd = NULL; + + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_USEABLE) { + HDF_LOGE("%s: uart_%d not useable", __func__, udd->resource.port); + return HDF_FAILURE; + } + if (UART_MODE_RD_BLOCK == mode) { + udd->flags |= UART_FLG_RD_BLOCK; + } else if (UART_MODE_RD_NONBLOCK == mode) { + udd->flags &= (~UART_FLG_RD_BLOCK); + } else { + HDF_LOGE("%s: uart_%d not support mode:%d", __func__, udd->resource.port, mode); + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static int32_t UartDevSemInit(uint32_t id) +{ + uint32_t uwRet = 0; + + uwRet = LOS_MuxCreate(&g_uartTxMutex[id]); + if (uwRet != LOS_OK) { + return HDF_FAILURE; + } + + uwRet = LOS_MuxCreate(&g_uartRxMutex[id]); + if (uwRet != LOS_OK) { + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} + +static void UartDevSemDeinit(uint32_t id) +{ + if (g_uartTxMutex[id] != 0) { + LOS_MuxDelete(g_uartTxMutex[id]); + } + + if (g_uartRxMutex[id] != 0) { + LOS_MuxDelete(g_uartRxMutex[id]); + } + + g_uartTxMutex[id] = 0; + g_uartRxMutex[id] = 0; +} + +static int32_t UartHostDevInit(struct UartHost *host) +{ + struct UartDriverData *udd = NULL; + uint32_t ret = 0; + uint8_t *ptxBuf = NULL; + + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + udd = (struct UartDriverData *)host->priv; + if (udd->resource.port >= ASR_UART_NUM) { + HDF_LOGE("%s: uart id is greater than the maximum", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (udd->state == UART_STATE_NOT_OPENED) { + udd->state = UART_STATE_OPENING; + + ptxBuf = (uint8_t *)OsalMemCalloc(UART_RX_BUF_LEN); + if (ptxBuf == NULL) { + HDF_LOGE("%s: alloc tx buffer failed", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = UartDevSemInit(udd->resource.port); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: uart semaphor init failed", __func__); + UartDevSemDeinit(udd->resource.port); + return HDF_FAILURE; + } + + rx_buf[udd->resource.port] = ptxBuf; + udd->config = Asr582xUartConfig; + + if (udd->config(udd) != HDF_SUCCESS) { + UartDevSemDeinit(udd->resource.port); + (void)OsalMemFree(rx_buf[udd->resource.port]); + rx_buf[udd->resource.port] = NULL; + return HDF_FAILURE; + } + } + + udd->state = UART_STATE_USEABLE; + udd->count++; + return HDF_SUCCESS; +} + +static int32_t UartHostDevDeinit(struct UartHost *host) +{ + struct UartDriverData *udd = NULL; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + udd = (struct UartDriverData *)host->priv; + if ((--udd->count) != 0) { + return HDF_SUCCESS; + } + asr_uart_finalize(&udd->params); + UartDevSemDeinit(udd->resource.port); + if (rx_buf[udd->resource.port] != NULL) { + (void)OsalMemFree(rx_buf[udd->resource.port]); + rx_buf[udd->resource.port] = NULL; + } + + udd->state = UART_STATE_NOT_OPENED; + return HDF_SUCCESS; +} + +struct UartHostMethod g_uartHostMethod = { + .Init = UartHostDevInit, + .Deinit = UartHostDevDeinit, + .Read = UartHostDevRead, + .Write = UartHostDevWrite, + .SetBaud = UartHostDevSetBaud, + .GetBaud = UartHostDevGetBaud, + .SetAttribute = UartHostDevSetAttribute, + .GetAttribute = UartHostDevGetAttribute, + .SetTransMode = UartHostDevSetTransMode, +}; + +static int32_t UartGetPinConfigFromHcs(struct UartDriverData *udd, const struct DeviceResourceNode *node) +{ + uint32_t resourceData; + struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + + if (iface == NULL || iface->GetUint32 == NULL) { + HDF_LOGE("%s: face is invalid", __func__); + return HDF_FAILURE; + } + + if (iface->GetUint32(node, "port", &resourceData, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read port fail", __func__); + return HDF_FAILURE; + } + udd->resource.port = resourceData; + + if (iface->GetUint32(node, "pin_tx_pin", &resourceData, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read pin_tx_pin fail", __func__); + return HDF_FAILURE; + } + udd->resource.pin_tx_pin = resourceData; + + if (iface->GetUint32(node, "pin_tx_mux", &resourceData, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read pin_tx_pin fail", __func__); + return HDF_FAILURE; + } + udd->resource.pin_tx_mux = resourceData; + + if (iface->GetUint32(node, "pin_rx_pin", &resourceData, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read pin_rx_pin fail", __func__); + return HDF_FAILURE; + } + udd->resource.pin_rx_pin = resourceData; + + if (iface->GetUint32(node, "pin_rx_mux", &resourceData, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read pin_rx_pin fail", __func__); + return HDF_FAILURE; + } + udd->resource.pin_rx_mux = resourceData; + + if (iface->GetUint32(node, "tx_rx", &resourceData, 0) != HDF_SUCCESS) { + HDF_LOGE("%s: read tx_rx fail", __func__); + return HDF_FAILURE; + } + udd->resource.tx_rx = resourceData; + + return HDF_SUCCESS; +} + +static int32_t UartDevAttach(struct UartHost *host, struct HdfDeviceObject *device) +{ + int32_t ret; + struct UartDriverData *udd = NULL; + + if (device->property == NULL) { + HDF_LOGE("%s: property is null", __func__); + return HDF_FAILURE; + } + udd = (struct UartDriverData *)OsalMemCalloc(sizeof(*udd)); + if (udd == NULL) { + HDF_LOGE("%s: OsalMemCalloc udd error", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = UartGetPinConfigFromHcs(udd, device->property); + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(udd); + return HDF_FAILURE; + } + + udd->state = UART_STATE_NOT_OPENED; + udd->config = NULL; + udd->count = 0; + + asr_uart_struct_init(&udd->params); + udd->params.port = udd->resource.port; + udd->params.config.baud_rate = DEFAULT_BAUDRATE; + udd->attr.dataBits = DEFAULT_DATABITS; + udd->attr.stopBits = DEFAULT_STOPBITS; + udd->attr.parity = DEFAULT_PARITY; + + host->priv = udd; + host->num = udd->resource.port; + + return HDF_SUCCESS; +} + +static void UartDevDetach(struct UartHost *host) +{ + struct UartDriverData *udd = NULL; + + if (host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return; + } + udd = (struct UartDriverData *)host->priv; + if (udd->state != UART_STATE_NOT_OPENED) { + HDF_LOGE("%s: uart driver data state invalid", __func__); + return; + } + + (void)OsalMemFree(udd); + host->priv = NULL; +} + +static int32_t HdfUartDeviceBind(struct HdfDeviceObject *device) +{ + if (device == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + return (UartHostCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS; +} + +int32_t HdfUartDeviceInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct UartHost *host = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is null", __func__); + return HDF_ERR_INVALID_OBJECT; + } + host = UartHostFromDevice(device); + if (host == NULL) { + HDF_LOGE("%s: host is null", __func__); + return HDF_FAILURE; + } + ret = UartDevAttach(host, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: attach error", __func__); + return HDF_FAILURE; + } + host->method = &g_uartHostMethod; + return ret; +} + +void HdfUartDeviceRelease(struct HdfDeviceObject *device) +{ + struct UartHost *host = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is null", __func__); + return; + } + host = UartHostFromDevice(device); + if (host == NULL) { + HDF_LOGE("%s: host is null", __func__); + return; + } + if (host->priv != NULL) { + UartDevDetach(host); + } + UartHostDestroy(host); +} + +struct HdfDriverEntry g_hdfUartDevice = { + .moduleVersion = 1, + .moduleName = "HDF_PLATFORM_UART", + .Bind = HdfUartDeviceBind, + .Init = HdfUartDeviceInit, + .Release = HdfUartDeviceRelease, +}; + +HDF_INIT(g_hdfUartDevice); diff --git a/platform/uart/uart_bes.c b/platform/uart/uart_bes.c index b17ad54861489384b52187fed2f3cb18d82fca1d..cc2a26af89f26276b20d7833aceb75787c4fbbec 100755 --- a/platform/uart/uart_bes.c +++ b/platform/uart/uart_bes.c @@ -1,39 +1,49 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "uart_bes.h" #include #include #include "hal_iomux.h" #include "hal_timer.h" +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else #include "device_resource_if.h" +#endif #include "hal_trace.h" #include "hal_cache.h" #include "hdf_log.h" -#define HDF_UART_TMO 1000 + +#define HDF_UART_TMO OSAL_WAIT_FOREVER #define HDF_LOG_TAG uartDev #define UART_FIFO_MAX_BUFFER 2048 #define UART_DMA_RING_BUFFER_SIZE 256 // mast be 2^n - +#ifdef LOSCFG_SOC_SERIES_BES2700 +#include "hal_location.h" +#define MAX_UART_NUMBER 2 +#define MAX_UART_ID HAL_UART_ID_1 +static SRAM_BSS_LOC unsigned char g_halUartBuf[UART_DMA_RING_BUFFER_SIZE]; +static SRAM_BSS_LOC unsigned char g_halUart1Buf[UART_DMA_RING_BUFFER_SIZE]; +static unsigned char *g_uartKfifoBuffer[MAX_UART_NUMBER] = { NULL, NULL }; +#elif defined (LOSCFG_SOC_SERIES_BES2600) +#define MAX_UART_NUMBER 3 +#define MAX_UART_ID HAL_UART_ID_2 static __SRAMBSS unsigned char g_halUartBuf[UART_DMA_RING_BUFFER_SIZE]; static __SRAMBSS unsigned char g_halUart1Buf[UART_DMA_RING_BUFFER_SIZE]; static __SRAMBSS unsigned char g_halUart2Buf[UART_DMA_RING_BUFFER_SIZE]; +static unsigned char *g_uartKfifoBuffer[MAX_UART_NUMBER] = {NULL, NULL, NULL}; +#endif -static struct UART_CTX_OBJ g_uartCtx[4] = {0}; -static unsigned char *g_uartKfifoBuffer[4] = {NULL, NULL, NULL, NULL}; +static struct UART_CTX_OBJ g_uartCtx[MAX_UART_NUMBER] = {0}; struct HAL_UART_CFG_T g_lowUartCfg = { // used for tgdb cli console .parity = HAL_UART_PARITY_NONE, @@ -50,21 +60,31 @@ struct HAL_UART_CFG_T g_lowUartCfg = { static void HalSetUartIomux(enum HAL_UART_ID_T uartId) { + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d not support!\r\n", __func__, uartId); + return; + } if (uartId == HAL_UART_ID_0) { hal_iomux_set_uart0(); - } else if (uartId == HAL_UART_ID_1) { + } + if (uartId == HAL_UART_ID_1) { hal_iomux_set_uart1(); - } else if (uartId == HAL_UART_ID_2) { + } +#ifdef LOSCFG_SOC_SERIES_BES2600 + if (uartId == HAL_UART_ID_2) { hal_iomux_set_uart2(); - } else { - hal_iomux_set_uart3(); } +#endif } -static void HalUartRxStart(uint32_t uartId) +static void HalUartStartDmaRx(uint32_t uartId) { + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d not support!\r\n", __func__, uartId); + return; + } struct HAL_DMA_DESC_T dmaDescRx; - unsigned int descCnt = 1; + uint32_t descCnt; union HAL_UART_IRQ_T mask; mask.reg = 0; @@ -73,58 +93,61 @@ static void HalUartRxStart(uint32_t uartId) mask.OE = 0; mask.PE = 0; mask.RT = 1; - + descCnt = 1; hal_uart_dma_recv_mask(uartId, g_uartCtx[uartId].buffer, UART_DMA_RING_BUFFER_SIZE, &dmaDescRx, &descCnt, &mask); } -static void UartRxHandler(enum HAL_UART_ID_T id, union HAL_UART_IRQ_T status) +static void UartRxHandler(enum HAL_UART_ID_T uartId, union HAL_UART_IRQ_T status) { - int32_t ret; - + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d not support!\r\n", __func__, uartId); + return; + } if (status.TX) { - ret = OsalSemPost(&g_uartCtx[id].txSem); - ASSERT(ret == HDF_SUCCESS, "%s: Failed to release write_sem: %d", __func__, ret); + if (OsalSemPost(&g_uartCtx[uartId].txSem) != HDF_SUCCESS) { + HDF_LOGE("%s OsalSemPost txSem failed!\r\n", __func__); + return; + } } if (status.RX || status.RT) { - ret = OsalSemPost(&g_uartCtx[id].rxSem); - ASSERT(ret == HDF_SUCCESS, "%s: Failed to release read_sem: %d", __func__, ret); + if (OsalSemPost(&g_uartCtx[uartId].rxSem) != HDF_SUCCESS) { + HDF_LOGE("%s OsalSemPost rxSem failed!\r\n", __func__); + return; + } } } static void UartDmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status) { - uint32_t len = 0; - uint32_t uartid = 0; - - len = kfifo_put(&g_uartCtx[uartid].fifo, g_uartCtx[uartid].buffer, xferSize); + uint32_t len; + len = kfifo_put(&g_uartCtx[HAL_UART_ID_0].fifo, g_uartCtx[HAL_UART_ID_0].buffer, xferSize); if (len < xferSize) { HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize); return; } - memset_s(g_uartCtx[uartid].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); - OsalSemPost(&g_uartCtx[uartid].rxSem); - HalUartRxStart(uartid); + (void)memset_s(g_uartCtx[HAL_UART_ID_0].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); + OsalSemPost(&g_uartCtx[HAL_UART_ID_0].rxSem); + HalUartStartDmaRx(HAL_UART_ID_0); } static void UartDmaTxHandler(uint32_t xferSize, int dmaError) { - OsalSemPost(&g_uartCtx[0].txSem); + OsalSemPost(&g_uartCtx[HAL_UART_ID_0].txSem); } static void Uart1DmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status) { - uint32_t len = 0; - uint32_t uartid = HAL_UART_ID_1; - len = kfifo_put(&g_uartCtx[uartid].fifo, g_uartCtx[uartid].buffer, xferSize); + uint32_t len; + len = kfifo_put(&g_uartCtx[HAL_UART_ID_1].fifo, g_uartCtx[HAL_UART_ID_1].buffer, xferSize); if (len < xferSize) { HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize); return; } - memset_s(g_uartCtx[uartid].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); - OsalSemPost(&g_uartCtx[uartid].rxSem); - HalUartRxStart(uartid); + memset_s(g_uartCtx[HAL_UART_ID_1].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); + OsalSemPost(&g_uartCtx[HAL_UART_ID_1].rxSem); + HalUartStartDmaRx(HAL_UART_ID_1); } static void Uart1DmaTxHandler(uint32_t xferSize, int dmaError) @@ -132,29 +155,34 @@ static void Uart1DmaTxHandler(uint32_t xferSize, int dmaError) OsalSemPost(&g_uartCtx[HAL_UART_ID_1].txSem); } +#ifdef LOSCFG_SOC_SERIES_BES2600 /* uart2 */ static void Uart2DmaRxHandler(uint32_t xferSize, int dmaError, union HAL_UART_IRQ_T status) { - uint32_t len = 0; - uint32_t uartid = HAL_UART_ID_2; - len = kfifo_put(&g_uartCtx[uartid].fifo, g_uartCtx[uartid].buffer, xferSize); + uint32_t len ; + len = kfifo_put(&g_uartCtx[HAL_UART_ID_2].fifo, g_uartCtx[HAL_UART_ID_2].buffer, xferSize); if (len < xferSize) { HDF_LOGE("%s ringbuf is full have %d need %d\r", __FUNCTION__, (int)len, (int)xferSize); return; } - memset_s(g_uartCtx[uartid].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); - OsalSemPost(&g_uartCtx[uartid].rxSem); - HalUartRxStart(uartid); + memset_s(g_uartCtx[HAL_UART_ID_2].buffer, UART_DMA_RING_BUFFER_SIZE, 0, UART_DMA_RING_BUFFER_SIZE); + OsalSemPost(&g_uartCtx[HAL_UART_ID_2].rxSem); + HalUartStartDmaRx(HAL_UART_ID_2); } static void Uart2DmaTxHandler(uint32_t xferSize, int dmaError) { OsalSemPost(&g_uartCtx[HAL_UART_ID_2].txSem); } +#endif static void HalUartStartRx(uint32_t uartId) { + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); + return; + } union HAL_UART_IRQ_T mask; mask.reg = 0; mask.RT = 1; @@ -166,20 +194,22 @@ static void HalUartStartRx(uint32_t uartId) static int32_t HalUartSend(uint32_t uartId, const void *data, uint32_t size, uint32_t timeOut) { - int32_t ret = HDF_FAILURE; + int32_t ret; struct HAL_DMA_DESC_T dmaSescTx; - unsigned int descCnt = 1; - + uint32_t descCnt; if (data == NULL || size == 0) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_INVALID_PARAM; } - if (uartId > HAL_UART_ID_2) { + if (uartId > MAX_UART_ID) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_NOT_SUPPORT; } - + descCnt = 1; +#ifdef LOSCFG_SOC_SERIES_BES2700 + hal_cache_sync_all(HAL_CACHE_ID_D_CACHE); +#endif hal_uart_dma_send(uartId, data, size, &dmaSescTx, &descCnt); OsalSemWait(&g_uartCtx[uartId].txSem, timeOut); @@ -189,19 +219,18 @@ static int32_t HalUartSend(uint32_t uartId, const void *data, uint32_t size, uin static int32_t HalUartRecv(uint8_t uartId, void *data, uint32_t expectSize, uint32_t *recvSize, uint32_t timeOut) { - int32_t ret = HDF_FAILURE; - uint32_t beginTime = 0; - uint32_t nowTime = 0; - uint32_t fifoPopLen = 0; + uint32_t beginTime; + uint32_t nowTime; + uint32_t fifoPopLen; uint32_t recvedLen = 0; - uint32_t expectLen = expectSize; + int32_t expectLen = (int32_t)expectSize; - if (data == NULL || expectSize == 0 || recvSize == NULL) { + if (data == NULL || expectLen == 0 || recvSize == NULL) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_INVALID_PARAM; } - if (uartId > HAL_UART_ID_2) { + if (uartId > MAX_UART_ID) { HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); return HDF_ERR_NOT_SUPPORT; } @@ -229,19 +258,31 @@ static int32_t HalUartRecv(uint8_t uartId, void *data, uint32_t expectSize, if (recvSize != NULL) { *recvSize = recvedLen; } - return HDF_SUCCESS; } -static void HalUartHandlerInit(struct UartDevice *device) +static int32_t InitUartCtxCfg(struct UartDevice *device) { uint32_t uartId; + struct HAL_UART_CFG_T *uartCfg = NULL; if (device == NULL) { HDF_LOGE("%s: INVALID PARAM", __func__); - return HDF_ERR_INVALID_PARAM; + return HDF_ERR_INVALID_OBJECT; + } + uartCfg = &device->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; } uartId = device->uartId; - HDF_LOGI("%s %ld\r\n", __func__, uartId); + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } + memset_s(&g_uartCtx[uartId], sizeof(struct UART_CTX_OBJ), 0, sizeof(struct UART_CTX_OBJ)); + g_uartCtx[uartId].txDMA = uartCfg->dma_tx; + g_uartCtx[uartId].rxDMA = uartCfg->dma_rx; + if (uartId == HAL_UART_ID_0) { g_uartCtx[uartId].UartDmaRxHandler = UartDmaRxHandler; g_uartCtx[uartId].UartDmaTxHandler = UartDmaTxHandler; @@ -253,13 +294,36 @@ static void HalUartHandlerInit(struct UartDevice *device) g_uartCtx[uartId].UartDmaTxHandler = Uart1DmaTxHandler; g_uartCtx[uartId].buffer = g_halUart1Buf; } - +#ifdef LOSCFG_SOC_SERIES_BES2600 if (uartId == HAL_UART_ID_2) { g_uartCtx[uartId].UartDmaRxHandler = Uart2DmaRxHandler; g_uartCtx[uartId].UartDmaTxHandler = Uart2DmaTxHandler; g_uartCtx[uartId].buffer = g_halUart2Buf; } +#endif + return HDF_SUCCESS; +} + +static void HalUartHandlerInit(struct UartDevice *device) +{ + uint32_t uartId; + int32_t ret; + struct HAL_UART_CFG_T *uartCfg = NULL; + if (device == NULL) { + HDF_LOGE("%s: INVALID PARAM!\r\n", __func__); + return; + } + uartId = device->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT!\r\n", __FILE__, __LINE__); + return; + } + ret = InitUartCtxCfg(device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s %d InitUartCtxCfg failed\r\n", __FILE__, __LINE__); + return; + } if (!g_uartKfifoBuffer[uartId]) { g_uartKfifoBuffer[uartId] = (char *)OsalMemAlloc(UART_FIFO_MAX_BUFFER); if (!g_uartKfifoBuffer[uartId]) { @@ -269,13 +333,19 @@ static void HalUartHandlerInit(struct UartDevice *device) kfifo_init(&g_uartCtx[uartId].fifo, g_uartKfifoBuffer[uartId], UART_FIFO_MAX_BUFFER); } - OsalSemInit(&g_uartCtx[uartId].rxSem, 0); - OsalSemInit(&g_uartCtx[uartId].txSem, 0); + if (OsalSemInit(&g_uartCtx[uartId].rxSem, 0) != HDF_SUCCESS) { + HDF_LOGE("UART rxsem init failed!"); + return; + } + if (OsalSemInit(&g_uartCtx[uartId].txSem, 0) != HDF_SUCCESS) { + HDF_LOGE("UART txsem init failed!"); + return; + } - if (g_uartCtx[uartId].rxDMA) { - HDF_LOGE("uart %ld start dma rx\r\n", uartId); + if (g_uartCtx[uartId].rxDMA == true) { + HDF_LOGI("uart %ld start dma rx\r\n", uartId); hal_uart_irq_set_dma_handler(uartId, g_uartCtx[uartId].UartDmaRxHandler, g_uartCtx[uartId].UartDmaTxHandler); - HalUartRxStart(uartId); + HalUartStartDmaRx(uartId); } else { HalUartStartRx(uartId); } @@ -290,13 +360,16 @@ static void UartStart(struct UartDevice *device) return; } uartId = device->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg = &device->config; if (uartCfg == NULL) { HDF_LOGE("%s: INVALID OBJECT", __func__); return; } hal_uart_open(uartId, uartCfg); - HDF_LOGI("%s %ld\r\n", __FUNCTION__, uartId); HalUartHandlerInit(device); } @@ -367,20 +440,15 @@ static int InitUartDevice(struct UartHost *host) HDF_LOGE("%s: INVALID OBJECT", __func__); return HDF_ERR_INVALID_OBJECT; } - uartDevice->uartId = resource->num; - uartCfg->parity = resource->parity; - uartCfg->stop = resource->stopBit; - uartCfg->data = resource->wLen; + uint32_t uartId = resource->num; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg->flow = HAL_UART_FLOW_CONTROL_NONE; - uartCfg->tx_level = HAL_UART_FIFO_LEVEL_1_2; + uartCfg->tx_level = HAL_UART_FIFO_LEVEL_1_8; uartCfg->rx_level = HAL_UART_FIFO_LEVEL_1_2; - uartCfg->baud = resource->baudRate; uartCfg->dma_rx_stop_on_err = false; - uartCfg->dma_rx = resource->rxDMA; - uartCfg->dma_tx = resource->txDMA; - - g_uartCtx[uartDevice->uartId].txDMA = resource->txDMA; - g_uartCtx[uartDevice->uartId].rxDMA = resource->rxDMA; if (!uartDevice->initFlag) { HDF_LOGE("uart %ld device init\r\n", uartDevice->uartId); @@ -388,10 +456,53 @@ static int InitUartDevice(struct UartHost *host) UartStart(uartDevice); uartDevice->initFlag = true; } - return HDF_SUCCESS; } - +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define UART_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->num = HCS_PROP(node, num); \ + resource->baudRate = HCS_PROP(node, baudRate); \ + resource->parity = HCS_PROP(node, parity); \ + resource->stopBit = HCS_PROP(node, stopBit); \ + resource->wLen = HCS_PROP(node, data); \ + resource->rxDMA = HCS_PROP(node, rxDMA); \ + resource->txDMA = HCS_PROP(node, txDMA); \ + result = HDF_SUCCESS; \ + break; \ + } \ + } while (0) + +#define PLATFORM_UART_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), uart_config) +static uint32_t GetUartDeviceResource(struct UartDevice *device, const char *deviceMatchAttr) +{ + struct UartResource *resource = NULL; + int32_t result = HDF_FAILURE; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; +#if HCS_NODE_EXISTS(PLATFORM_UART_CONFIG) + HCS_FOREACH_CHILD_VARGS(PLATFORM_UART_CONFIG, UART_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + return result; + } + // copy config + device->initFlag = false; + device->uartId = resource->num; + device->config.baud = resource->baudRate; + device->config.parity = resource->parity; + device->config.stop = resource->stopBit; + device->config.data = resource->wLen; + device->config.dma_rx = (resource->rxDMA == true) ? true : false; + device->config.dma_tx = (resource->txDMA == true) ? true : false; + return HDF_SUCCESS; +} +#else static uint32_t GetUartDeviceResource( struct UartDevice *device, const struct DeviceResourceNode *resourceNode) { @@ -437,6 +548,7 @@ static uint32_t GetUartDeviceResource( resource->rxDMA = dri->GetBool(resourceNode, "rxDMA"); // copy config + device->initFlag = false; device->uartId = resource->num; device->config.baud = resource->baudRate; device->config.parity = resource->parity; @@ -446,13 +558,16 @@ static uint32_t GetUartDeviceResource( device->config.dma_tx = resource->txDMA; return HDF_SUCCESS; } - +#endif static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device) { int32_t ret; struct UartDevice *uartDevice = NULL; - +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL || uartHost == NULL) { +#else if (uartHost == NULL || device == NULL || device->property == NULL) { +#endif HDF_LOGE("%s: property is NULL", __func__); return HDF_ERR_INVALID_PARAM; } @@ -462,8 +577,11 @@ static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObjec HDF_LOGE("%s: OsalMemCalloc uartDevice error", __func__); return HDF_ERR_MALLOC_FAIL; } - +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetUartDeviceResource(uartDevice, device->deviceMatchAttr); +#else ret = GetUartDeviceResource(uartDevice, device->property); +#endif if (ret != HDF_SUCCESS) { (void)OsalMemFree(uartDevice); return HDF_FAILURE; @@ -476,7 +594,7 @@ static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObjec static int32_t UartDriverBind(struct HdfDeviceObject *device) { - struct UartHost *devService; + struct UartHost *devService = NULL; if (device == NULL) { HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; @@ -560,8 +678,8 @@ static int32_t UartHostDevInit(struct UartHost *host) HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; } - InitUartDevice(host); - return HDF_SUCCESS; + + return InitUartDevice(host); } static int32_t UartHostDevDeinit(struct UartHost *host) @@ -590,7 +708,7 @@ static int32_t UartHostDevDeinit(struct UartHost *host) static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size) { struct UartDevice *device = NULL; - uint32_t portId; + uint32_t uartId; if (host == NULL || data == NULL || size == 0 || host->priv == NULL) { HDF_LOGE("%s: invalid parameter", __func__); @@ -603,15 +721,19 @@ static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t s return HDF_ERR_INVALID_OBJECT; } - portId = device->uartId; - if (g_uartCtx[portId].txDMA) { - HalUartSend(portId, data, size, HDF_UART_TMO); + uartId = device->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } + if (g_uartCtx[uartId].txDMA) { + HalUartSend(uartId, data, size, HDF_UART_TMO); } else { for (uint32_t idx = 0; idx < size; idx++) { - if (g_uartCtx[portId].isBlock) { - hal_uart_blocked_putc(portId, data[idx]); + if (g_uartCtx[uartId].isBlock) { + hal_uart_blocked_putc(uartId, data[idx]); } else { - hal_uart_putc(portId, data[idx]); + hal_uart_putc(uartId, data[idx]); } } } @@ -621,7 +743,7 @@ static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t s static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size) { - uint32_t recvSize = 0; + uint32_t recvSize; int32_t ret; uint32_t uartId; struct UartDevice *uartDevice = NULL; @@ -648,7 +770,9 @@ static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t si if (g_uartCtx[uartId].isBlock) { data[0] = hal_uart_blocked_getc(uartId); } else { - data[0] = hal_uart_getc(uartId); + if (hal_uart_readable(uartId) > 0) { + data[0] = hal_uart_getc(uartId); + } } ret = 1; } @@ -673,7 +797,10 @@ static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) return HDF_ERR_INVALID_OBJECT; } uartId = uartDevice->uartId; - + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg = &uartDevice->config; if (uartCfg == NULL) { HDF_LOGE("%s: device config is NULL", __func__); @@ -681,7 +808,7 @@ static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) } uartCfg->baud = baudRate; - hal_uart_reopen(uartId, uartCfg); + hal_uart_open(uartId, uartCfg); return HDF_SUCCESS; } @@ -702,6 +829,10 @@ static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) return HDF_ERR_INVALID_OBJECT; } uartId = uartDevice->uartId; + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } uartCfg = &uartDevice->config; if (uartCfg == NULL) { HDF_LOGE("%s: device is NULL", __func__); @@ -715,22 +846,14 @@ static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) return HDF_SUCCESS; } -static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) +static int32_t SetUartDevConfig(struct UartAttribute *attribute, struct UartDevice *uartDevice) { - HDF_LOGI("%s: Enter", __func__); - struct UartDevice *uartDevice = NULL; struct HAL_UART_CFG_T *uartCfg = NULL; uint32_t uartId; - if (host == NULL || attribute == NULL || host->priv == NULL) { + if (attribute == NULL || uartDevice == NULL) { HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; } - - uartDevice = (struct UartDevice *)host->priv; - if (uartDevice == NULL) { - HDF_LOGE("%s: device is NULL", __func__); - return HDF_ERR_INVALID_OBJECT; - } uartId = uartDevice->uartId; uartCfg = &uartDevice->config; if (uartCfg == NULL) { @@ -777,17 +900,15 @@ static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribu } else { uartCfg->flow = HAL_UART_FLOW_CONTROL_NONE; } - - hal_uart_reopen(uartId, uartCfg); - + hal_uart_open(uartId, uartCfg); return HDF_SUCCESS; } -static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) { HDF_LOGI("%s: Enter", __func__); struct UartDevice *uartDevice = NULL; - struct HAL_UART_CFG_T *uartCfg = NULL; + int ret; if (host == NULL || attribute == NULL || host->priv == NULL) { HDF_LOGE("%s: invalid parameter", __func__); return HDF_ERR_INVALID_PARAM; @@ -798,12 +919,21 @@ static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribu HDF_LOGE("%s: device is NULL", __func__); return HDF_ERR_INVALID_OBJECT; } - uartCfg = &uartDevice->config; - if (uartCfg == NULL) { - HDF_LOGE("%s: config is NULL", __func__); - return HDF_ERR_INVALID_OBJECT; + + ret = SetUartDevConfig(attribute, uartDevice); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: SetUartDevConfig error", __func__); + return HDF_FAILURE; } + return HDF_SUCCESS; +} +static int32_t GetUartDevConfig(struct UartAttribute *attribute, struct HAL_UART_CFG_T *uartCfg) +{ + if (attribute == NULL || uartCfg == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } switch (uartCfg->data) { case HAL_UART_DATA_BITS_8: attribute->dataBits = UART_ATTR_DATABIT_8; @@ -847,10 +977,33 @@ static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribu attribute->cts = 0; break; } - return HDF_SUCCESS; } +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + struct HAL_UART_CFG_T *uartCfg = NULL; + if (host == NULL || attribute == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartCfg = &uartDevice->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + return GetUartDevConfig(attribute, uartCfg); +} + static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode) { HDF_LOGI("%s: Enter", __func__); @@ -867,7 +1020,10 @@ static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode return HDF_ERR_INVALID_OBJECT; } uartId = uartDevice->uartId; - + if (uartId > MAX_UART_ID) { + HDF_LOGE("%s %d NOT SUPPORT \r\n", __FILE__, __LINE__); + return HDF_ERR_NOT_SUPPORT; + } switch (mode) { case UART_MODE_RD_BLOCK: g_uartCtx[uartId].isBlock = true; diff --git a/platform/uart/uart_bes.h b/platform/uart/uart_bes.h old mode 100755 new mode 100644 index 454e1015e6d8540df0150f61802dfaaf85d210e9..98a9bfe925cb38b45c1eab304e1574c4f4a1558d --- a/platform/uart/uart_bes.h +++ b/platform/uart/uart_bes.h @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #ifndef __UART_BES_H__ #define __UART_BES_H__ @@ -74,4 +68,4 @@ int32_t UartDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf } #endif -#endif \ No newline at end of file +#endif diff --git a/platform/uart/uart_gr5xx.c b/platform/uart/uart_gr5xx.c old mode 100755 new mode 100644 index 89621068553e7ec87114a2d8a84ca5df143ff75e..9c8005ff128ecc43b958145354749143ac001d71 --- a/platform/uart/uart_gr5xx.c +++ b/platform/uart/uart_gr5xx.c @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 GOODIX. - * 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 + * Copyright (c) 2021-2022 GOODIX. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "app_uart.h" #include "uart/uart_core.h" #include "uart_if.h" diff --git a/platform/uart/uart_stm32f4xx.c b/platform/uart/uart_stm32f4xx.c new file mode 100644 index 0000000000000000000000000000000000000000..e25dbe59ab9f50764307250d3d55492481cab4c7 --- /dev/null +++ b/platform/uart/uart_stm32f4xx.c @@ -0,0 +1,1093 @@ +/* + * Copyright (c) 2022 Talkweb Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include "hal_gpio.h" +#include "uart_if.h" +#include "stm32f4xx_ll_bus.h" +#include "uart_core.h" +#include "hal_usart.h" +#include "osal_sem.h" +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#endif +#include "hdf_log.h" + +#define HDF_UART_TMO 1000 +#define HDF_LOG_TAG uartDev +#define GPIO_MAX_LENGTH 32 +#define UART_FIFO_MAX_BUFFER 2048 +#define UART_DMA_RING_BUFFER_SIZE 256 // mast be 2^n + +#define UART_IRQ_PRO 5 +#define UART_DEV_SERVICE_NAME_PREFIX "HDF_PLATFORM_UART%d" +#define MAX_DEV_NAME_SIZE 32 +typedef enum { + USART_NUM_1 = 1, + USART_NUM_2, + USART_NUM_3, + USART_NUM_4, + USART_NUM_5, + USART_NUM_6, +} USART_NUM; + +typedef enum { + USART_DATAWIDTH_8 = 0, + USART_DATAWIDTH_9, + USART_DATAWIDTH_MAX, +} USART_DATAWIDTH; + +typedef enum { + USART_STOP_BIT_0_5 = 0, + USART_STOP_BIT_1, + USART_STOP_BIT_1_5, + USART_STOP_BIT_2, + USART_STOP_BIT_MAX, +} USART_STOP_BIT; + +typedef enum { + USART_PARITY_CHECK_NONE = 0, + USART_PARITY_CHECK_EVENT, + USART_PARITY_CHECK_ODD, + USART_PARITY_CHECK_MAX, +} USART_PARITY_CHECK; + +typedef enum { + USART_TRANS_DIR_NONE = 0, + USART_TRANS_DIR_RX, + USART_TRANS_DIR_TX, + USART_TRANS_DIR_RX_TX, + USART_TRANS_DIR_MAX, +} USART_TRANS_DIR; + +typedef enum { + USART_FLOW_CTRL_NONE = 0, + USART_FLOW_CTRL_RTS, + USART_FLOW_CTRL_CTS, + USART_FLOW_CTRL_RTS_CTS, + USART_FLOW_CTRL_MAX, +} USART_FLOW_CTRL; + +typedef enum { + USART_OVER_SIMPLING_16 = 0, + USART_OVER_SIMPLING_8, + USART_OVER_SIMPLING_MAX, +} USART_OVER_SIMPLING; + +typedef enum { + USART_TRANS_BLOCK = 0, // block + USART_TRANS_NOBLOCK, + USART_TRANS_TX_DMA, // TX DMA RX NORMAL + USART_TRANS_RX_DMA, // TX NORMAL RX DMA + USART_TRANS_TX_RX_DMA, // TX DMA RX DMA + USART_TRANS_MODE_MAX, +} USART_TRANS_MODE; + +typedef enum { + USART_IDLE_IRQ_DISABLE = 0, + USART_IDLE_IRQ_ENABLE, + USART_IDLE_IRQ_MAX, +} USART_IDLE_IRQ; + +typedef enum { + USART_232 = 0, + USART_485, + USART_TYPE_MAX, +} USART_TYPE; + +typedef struct { + USART_NUM num; + uint32_t baudRate; + USART_DATAWIDTH dataWidth; + USART_STOP_BIT stopBit; + USART_PARITY_CHECK parity; + USART_TRANS_DIR transDir; + USART_FLOW_CTRL flowCtrl; + USART_OVER_SIMPLING overSimpling; + USART_TRANS_MODE transMode; + USART_TYPE uartType; + STM32_GPIO_PIN dePin; + STM32_GPIO_GROUP deGroup; +} UartResource; + +typedef enum { + UART_DEVICE_UNINITIALIZED = 0x0u, + UART_DEVICE_INITIALIZED = 0x1u, +} UartDeviceState; + +typedef enum { + UART_ATTR_HDF_TO_NIOBE = 0, + UART_ATTR_NIOBE_TO_HDF, +} UART_ATTR_TRAN_TYPE; + +typedef void (*HWI_UART_IDLE_IRQ)(void); + +typedef struct { + bool txDMA; + bool rxDMA; + bool isBlock; + uint8_t dePin; + uint8_t deGroup; + uint8_t uartType; +} UsartContextObj; + +typedef struct { + struct IDeviceIoService ioService; + UartResource resource; + LL_USART_InitTypeDef initTypedef; + uint32_t uartId; + bool initFlag; + USART_TRANS_MODE transMode; +} UartDevice; + +static const uint32_t g_dataWidthMap[USART_DATAWIDTH_MAX] = { + LL_USART_DATAWIDTH_8B, + LL_USART_DATAWIDTH_9B, +}; + +static const uint32_t g_stopBitMap[USART_STOP_BIT_MAX] = { + LL_USART_STOPBITS_0_5, + LL_USART_STOPBITS_1, + LL_USART_STOPBITS_1_5, + LL_USART_STOPBITS_2, +}; + +static const uint32_t g_flowControlMap[USART_FLOW_CTRL_MAX] = { + LL_USART_HWCONTROL_NONE, + LL_USART_HWCONTROL_RTS, + LL_USART_HWCONTROL_CTS, + LL_USART_HWCONTROL_RTS_CTS, +}; + +static const uint32_t g_overSimplingMap[USART_OVER_SIMPLING_MAX] = { + LL_USART_OVERSAMPLING_16, + LL_USART_OVERSAMPLING_8, +}; + +static const uint32_t g_transDirMap[USART_TRANS_DIR_MAX] = { + LL_USART_DIRECTION_NONE, + LL_USART_DIRECTION_RX, + LL_USART_DIRECTION_TX, + LL_USART_DIRECTION_TX_RX, +}; + +static const uint32_t g_parityMap[USART_PARITY_CHECK_MAX] = { + LL_USART_PARITY_NONE, + LL_USART_PARITY_EVEN, + LL_USART_PARITY_ODD, +}; + +static const USART_TypeDef* g_usartRegMap[USART_NUM_6] = { + USART1, + USART2, + USART3, + UART4, + UART5, + USART6, +}; +static UsartContextObj g_uartCtx[USART_NUM_6] = {0}; + +static void USARTTxMode(STM32_GPIO_PIN gpioPin, STM32_GPIO_GROUP group) +{ + uint32_t pinReg = LL_GET_HAL_PIN(gpioPin); + GPIO_TypeDef* gpiox = NULL; + gpiox = LL_GET_GPIOX(group); + if (gpiox == NULL) { + return; + } + LL_GPIO_SetOutputPin(gpiox, pinReg); +} + +static void USARTRxMode(STM32_GPIO_PIN gpioPin, STM32_GPIO_GROUP group) +{ + uint32_t pinReg = LL_GET_HAL_PIN(gpioPin); + GPIO_TypeDef* gpiox = NULL; + gpiox = LL_GET_GPIOX(group); + if (gpiox == NULL) { + return; + } + LL_GPIO_ResetOutputPin(gpiox, pinReg); +} + +static const uint32_t g_exitIrqnMap[USART_NUM_6] = { + USART1_IRQn, + USART2_IRQn, + USART3_IRQn, + UART4_IRQn, + UART5_IRQn, + USART6_IRQn, +}; + +static void InitUsartClock(USART_NUM num) +{ + switch (num) { + case USART_NUM_1: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1); + break; + case USART_NUM_2: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2); + break; + case USART_NUM_3: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3); + break; + case USART_NUM_4: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4); + break; + case USART_NUM_5: + LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART5); + break; + case USART_NUM_6: + LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART6); + break; + default: + break; + } +} + +static void InitContextTransMode(UsartContextObj* ctx, USART_TRANS_MODE mode) +{ + switch (mode) { + case USART_TRANS_BLOCK: + ctx->isBlock = true; + ctx->rxDMA = false; + ctx->txDMA = false; + break; + case USART_TRANS_NOBLOCK: + ctx->isBlock = false; + ctx->rxDMA = false; + ctx->txDMA = false; + break; + case USART_TRANS_TX_DMA: // TX DMA RX NORMAL + ctx->isBlock = false; + ctx->rxDMA = false; + ctx->txDMA = true; + break; + case USART_TRANS_RX_DMA: // TX NORMAL RX DMA + ctx->isBlock = false; + ctx->rxDMA = true; + ctx->txDMA = false; + break; + case USART_TRANS_TX_RX_DMA: // TX DMA RX DMA + ctx->isBlock = false; + ctx->rxDMA = true; + ctx->txDMA = true; + break; + default: + break; + } +} + +static int32_t UartDriverBind(struct HdfDeviceObject *device); +static int32_t UartDriverInit(struct HdfDeviceObject *device); +static void UartDriverRelease(struct HdfDeviceObject *device); + +struct HdfDriverEntry g_UartDriverEntry = { + .moduleVersion = 1, + .moduleName = "ST_UART_MODULE_HDF", + .Bind = UartDriverBind, + .Init = UartDriverInit, + .Release = UartDriverRelease, +}; +HDF_INIT(g_UartDriverEntry); + +static int32_t UartHostDevInit(struct UartHost *host); +static int32_t UartHostDevDeinit(struct UartHost *host); +static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size); +static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate); +static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate); +static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size); +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute); +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute); +static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode); + +struct UartHostMethod g_uartHostMethod = { + .Init = UartHostDevInit, + .Deinit = UartHostDevDeinit, + .Read = UartHostDevRead, + .Write = UartHostDevWrite, + .SetBaud = UartHostDevSetBaud, + .GetBaud = UartHostDevGetBaud, + .SetAttribute = UartHostDevSetAttribute, + .GetAttribute = UartHostDevGetAttribute, + .SetTransMode = UartHostDevSetTransMode, +}; + +static int InitUartDevice(struct UartHost *host) +{ + uint8_t initRet = 0; + UartDevice *uartDevice = NULL; + LL_USART_InitTypeDef *initTypedef = NULL; + UartResource *resource = NULL; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + resource = &uartDevice->resource; + if (resource == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + initTypedef = &uartDevice->initTypedef; + if (initTypedef == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + InitContextTransMode(&g_uartCtx[uartDevice->uartId - 1], resource->transMode); + LL_USART_Disable(g_usartRegMap[uartDevice->uartId - 1]); + LL_USART_DeInit(g_usartRegMap[uartDevice->uartId - 1]); + InitUsartClock(uartDevice->uartId); + + if (!uartDevice->initFlag) { + initRet = LL_USART_Init(g_usartRegMap[uartDevice->uartId - 1], initTypedef); + if (initRet) { + HDF_LOGE("uart %ld device init failed\r\n", uartDevice->uartId); + return HDF_FAILURE; + } + LL_USART_ConfigAsyncMode(g_usartRegMap[uartDevice->uartId - 1]); + LL_USART_Enable(g_usartRegMap[uartDevice->uartId - 1]); + UART_IRQ_INIT(g_usartRegMap[uartDevice->uartId - 1], uartDevice->uartId, + g_exitIrqnMap[uartDevice->uartId - 1], g_uartCtx[uartDevice->uartId - 1].isBlock); + + uartDevice->initFlag = true; + } + + return HDF_SUCCESS; +} + +#ifndef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +static int32_t GetUartHcs(struct DeviceResourceIface *dri, + const struct DeviceResourceNode *resourceNode, UartResource *resource) +{ + if (dri->GetUint8(resourceNode, "num", &resource->num, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint32(resourceNode, "baudRate", &resource->baudRate, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "dataWidth", &resource->dataWidth, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "stopBit", &resource->stopBit, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "parity", &resource->parity, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "transDir", &resource->transDir, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "flowCtrl", &resource->flowCtrl, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "overSimpling", &resource->overSimpling, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "transMode", &resource->transMode, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "uartType", &resource->uartType, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "uartDePin", &resource->dePin, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + if (dri->GetUint8(resourceNode, "uartDeGroup", &resource->deGroup, 0) != HDF_SUCCESS) { + return HDF_FAILURE; + } + + return HDF_SUCCESS; +} +#endif + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define UART_FIND_CONFIG(node, name, resource) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + resource->num = HCS_PROP(node, num); \ + resource->baudRate = HCS_PROP(node, baudRate); \ + resource->dataWidth = HCS_PROP(node, dataWidth); \ + resource->stopBit = HCS_PROP(node, stopBit); \ + resource->parity = HCS_PROP(node, parity); \ + resource->transDir = HCS_PROP(node, transDir); \ + resource->flowCtrl = HCS_PROP(node, flowCtrl); \ + resource->overSimpling = HCS_PROP(node, overSimpling); \ + resource->transMode = HCS_PROP(node, transMode); \ + resource->uartType = HCS_PROP(node, uartType); \ + resource->dePin = HCS_PROP(node, uartDePin); \ + resource->deGroup = HCS_PROP(node, uartDeGroup); \ + result = HDF_SUCCESS; \ + } \ + } while (0) +#define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform) +#define PLATFORM_UART_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), uart_config) +static uint32_t GetUartDeviceResource(UartDevice *device, const char *deviceMatchAttr) +{ + UartResource *resource = NULL; + int32_t result = HDF_FAILURE; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; +#if HCS_NODE_HAS_PROP(PLATFORM_CONFIG, uart_config) + HCS_FOREACH_CHILD_VARGS(PLATFORM_UART_CONFIG, UART_FIND_CONFIG, deviceMatchAttr, resource); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + return result; + } + device->uartId = resource->num; + g_uartCtx[device->uartId - 1].dePin = resource->dePin; + g_uartCtx[device->uartId - 1].deGroup = resource->deGroup; + g_uartCtx[device->uartId - 1].uartType = resource->uartType; + device->initFlag = false; + device->initTypedef.BaudRate = resource->baudRate; + device->initTypedef.DataWidth = g_dataWidthMap[resource->dataWidth]; + device->initTypedef.HardwareFlowControl = g_flowControlMap[resource->flowCtrl]; + device->initTypedef.StopBits = g_stopBitMap[resource->stopBit]; + device->initTypedef.OverSampling = g_overSimplingMap[resource->overSimpling]; + device->initTypedef.TransferDirection = g_transDirMap[resource->transDir]; + device->initTypedef.Parity = g_parityMap[resource->parity]; + return HDF_SUCCESS; +} +#else +static int32_t GetUartDeviceResource(UartDevice *device, const struct DeviceResourceNode *resourceNode) +{ + struct DeviceResourceIface *dri = NULL; + UartResource *resource = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("%s: INVALID PARAM", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL || dri->GetUint8 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid"); + return HDF_ERR_INVALID_PARAM; + } + + if (GetUartHcs(dri, resourceNode, resource) != HDF_SUCCESS) { + HDF_LOGE("Get uart hcs failed\n"); + return HDF_ERR_INVALID_PARAM; + } + + device->uartId = resource->num; + g_uartCtx[device->uartId - 1].dePin = resource->dePin; + g_uartCtx[device->uartId - 1].deGroup = resource->deGroup; + g_uartCtx[device->uartId - 1].uartType = resource->uartType; + device->initFlag = false; + device->initTypedef.BaudRate = resource->baudRate; + device->initTypedef.DataWidth = g_dataWidthMap[resource->dataWidth]; + device->initTypedef.HardwareFlowControl = g_flowControlMap[resource->flowCtrl]; + device->initTypedef.StopBits = g_stopBitMap[resource->stopBit]; + device->initTypedef.OverSampling = g_overSimplingMap[resource->overSimpling]; + device->initTypedef.TransferDirection = g_transDirMap[resource->transDir]; + device->initTypedef.Parity = g_parityMap[resource->parity]; + + return HDF_SUCCESS; +} +#endif + +static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device) +{ + int32_t ret; + UartDevice *uartDevice = NULL; + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL || uartHost == NULL) { +#else + if (uartHost == NULL || device == NULL || device->property == NULL) { +#endif + HDF_LOGE("%s: property is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)OsalMemAlloc(sizeof(UartDevice)); + if (uartDevice == NULL) { + HDF_LOGE("%s: OsalMemCalloc uartDevice error", __func__); + return HDF_ERR_MALLOC_FAIL; + } +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetUartDeviceResource(uartDevice, device->deviceMatchAttr); +#else + ret = GetUartDeviceResource(uartDevice, device->property); +#endif + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(uartDevice); + return HDF_FAILURE; + } + + uartHost->priv = uartDevice; + + return HDF_SUCCESS; +} + +static int32_t UartDriverBind(struct HdfDeviceObject *device) +{ + struct UartHost *devService; + if (device == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return (UartHostCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS; +} + +static void UartDriverRelease(struct HdfDeviceObject *device) +{ + HDF_LOGI("Enter %s:", __func__); + uint32_t uartId; + struct UartHost *host = NULL; + UartDevice *uartDevice = NULL; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return; + } + + host = UartHostFromDevice(device); + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: host is NULL", __func__); + return; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return; + } + uartId = uartDevice->uartId; + host->method = NULL; + + OsalMemFree(uartDevice); +} + +static int32_t UartDriverInit(struct HdfDeviceObject *device) +{ + HDF_LOGI("Enter %s:", __func__); + int32_t ret; + struct UartHost *host = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + host = UartHostFromDevice(device); + if (host == NULL) { + HDF_LOGE("%s: host is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + ret = AttachUartDevice(host, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: attach error", __func__); + return HDF_FAILURE; + } + + host->method = &g_uartHostMethod; + + return ret; +} + +/* UartHostMethod implementations */ +static int32_t UartHostDevInit(struct UartHost *host) +{ + HDF_LOGI("%s: Enter\r\n", __func__); + if (host == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + return InitUartDevice(host); +} + +static int32_t UartHostDevDeinit(struct UartHost *host) +{ + HDF_LOGI("%s: Enter", __func__); + uint32_t uartId; + UartDevice *uartDevice = NULL; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + uartDevice->initFlag = false; + UART_IRQ_DEINIT(g_usartRegMap[uartDevice->uartId - 1], g_exitIrqnMap[uartId -1]); + LL_USART_Disable(g_usartRegMap[uartDevice->uartId - 1]); + + return LL_USART_DeInit(g_usartRegMap[uartId - 1]); +} + +static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size) +{ + UartDevice *device = NULL; + uint32_t portId; + + if (host == NULL || data == NULL || size == 0 || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + device = (UartDevice*)host->priv; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + portId = device->uartId; + USART_TypeDef* usartx = g_usartRegMap[portId - 1]; + if (g_uartCtx[portId - 1].uartType == USART_485) { + USARTTxMode(g_uartCtx[portId - 1].dePin, g_uartCtx[portId - 1].deGroup); + } + if (g_uartCtx[portId - 1].txDMA) { + HDF_LOGE("unsupport dma now\n"); + return HDF_ERR_INVALID_PARAM; + } else { + USART_TxData(usartx, data, size); + } + if (g_uartCtx[portId - 1].uartType == USART_485) { + USARTRxMode(g_uartCtx[portId - 1].dePin, g_uartCtx[portId - 1].deGroup); + } + + return HDF_SUCCESS; +} + +static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size) +{ + uint32_t recvSize = 0; + int32_t ret; + uint32_t uartId; + UartDevice *uartDevice = NULL; + if (host == NULL || data == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + uartId = uartDevice->uartId; + USART_TypeDef* usartx = g_usartRegMap[uartId - 1]; + if (g_uartCtx[uartId - 1].uartType == USART_485) { + USARTRxMode(g_uartCtx[uartId - 1].dePin, g_uartCtx[uartId - 1].deGroup); + } + + if (g_uartCtx[uartId - 1].rxDMA) { + HDF_LOGE("unsupport dma now\n"); + return HDF_ERR_INVALID_PARAM; + } else { + ret = USART_RxData(uartId, data, size, g_uartCtx[uartId - 1].isBlock); + } + + return ret; +} + +static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) +{ + HDF_LOGI("%s: Enter", __func__); + UartDevice *uartDevice = NULL; + LL_USART_InitTypeDef *uartInit = NULL; + + uint32_t uartId; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + uartId = uartDevice->uartId; + + uartInit = &uartDevice->initTypedef; + if (uartInit == NULL) { + HDF_LOGE("%s: device config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + uartInit->BaudRate = baudRate; + UartHostDevDeinit(host); + UartHostDevInit(host); + + return HDF_SUCCESS; +} + +static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) +{ + HDF_LOGI("%s: Enter", __func__); + UartDevice *uartDevice = NULL; + LL_USART_InitTypeDef *uartInit = NULL; + uint32_t uartId; + if (host == NULL || baudRate == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + uartInit = &uartDevice->initTypedef; + if (uartInit == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + *baudRate = uartInit->BaudRate; + + return HDF_SUCCESS; +} +static void TransDataWidth(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type) +{ + if (uartInit == NULL || attribute == NULL) { + HDF_LOGE("null ptr\r\n"); + return; + } + + if (type == UART_ATTR_NIOBE_TO_HDF) { + switch (uartInit->DataWidth) { // only support 9bit and 8 bit in niobe407 + case LL_USART_DATAWIDTH_8B: + attribute->dataBits = UART_ATTR_DATABIT_8; + break; + default: + attribute->dataBits = UART_ATTR_DATABIT_8; + break; + } + } else { + switch (attribute->dataBits) { + case UART_ATTR_DATABIT_8: + uartInit->DataWidth = LL_USART_DATAWIDTH_8B; + break; + default: + uartInit->DataWidth = LL_USART_DATAWIDTH_8B; + break; + } + } + + return; +} + +static void TransParity(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type) +{ + if (uartInit == NULL || attribute == NULL) { + HDF_LOGE("null ptr\r\n"); + return; + } + + if (type == UART_ATTR_NIOBE_TO_HDF) { + switch (uartInit->Parity) { + case LL_USART_PARITY_NONE: + attribute->parity = UART_ATTR_PARITY_NONE; + break; + case LL_USART_PARITY_EVEN: + attribute->parity = UART_ATTR_PARITY_EVEN; + break; + case LL_USART_PARITY_ODD: + attribute->parity = UART_ATTR_PARITY_ODD; + break; + default: + attribute->parity = UART_ATTR_PARITY_NONE; + break; + } + } else { + switch (attribute->parity) { + case UART_ATTR_PARITY_NONE: + uartInit->Parity = LL_USART_PARITY_NONE; + break; + case UART_ATTR_PARITY_EVEN: + uartInit->Parity = LL_USART_PARITY_EVEN; + break; + case UART_ATTR_PARITY_ODD: + uartInit->Parity = LL_USART_PARITY_ODD; + break; + default: + uartInit->Parity = LL_USART_PARITY_NONE; + break; + } + } + + return; +} + +static void TransStopbit(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type) +{ + if (uartInit == NULL || attribute == NULL) { + HDF_LOGE("null ptr\r\n"); + return; + } + + if (type == UART_ATTR_NIOBE_TO_HDF) { + switch (uartInit->StopBits) { + case LL_USART_STOPBITS_1: + attribute->stopBits = UART_ATTR_STOPBIT_1; + break; + case LL_USART_STOPBITS_1_5: + attribute->stopBits = UART_ATTR_STOPBIT_1P5; + break; + case LL_USART_STOPBITS_2: + attribute->stopBits = UART_ATTR_STOPBIT_2; + break; + default: + attribute->stopBits = UART_ATTR_PARITY_NONE; + break; + } + } else { + switch (attribute->stopBits) { + case UART_ATTR_STOPBIT_1: + uartInit->StopBits = LL_USART_STOPBITS_1; + break; + case UART_ATTR_STOPBIT_1P5: + uartInit->StopBits = LL_USART_STOPBITS_1_5; + break; + case UART_ATTR_STOPBIT_2: + uartInit->StopBits = LL_USART_STOPBITS_2; + break; + default: + uartInit->StopBits = LL_USART_STOPBITS_1; + break; + } + } + + return; +} + +static void TransFlowCtrl(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type) +{ + if (uartInit == NULL || attribute == NULL) { + HDF_LOGE("null ptr\r\n"); + return; + } + + if (type == UART_ATTR_NIOBE_TO_HDF) { + switch (uartInit->HardwareFlowControl) { + case LL_USART_HWCONTROL_NONE: + attribute->rts = 0; + attribute->cts = 0; + break; + case LL_USART_HWCONTROL_CTS: + attribute->rts = 0; + attribute->cts = 1; + break; + case LL_USART_HWCONTROL_RTS: + attribute->rts = 1; + attribute->cts = 0; + break; + case LL_USART_HWCONTROL_RTS_CTS: + attribute->rts = 1; + attribute->cts = 1; + break; + default: + attribute->rts = 0; + attribute->cts = 0; + break; + } + } else { + if (attribute->rts && attribute->cts) { + uartInit->HardwareFlowControl = LL_USART_HWCONTROL_RTS_CTS; + } else if (attribute->rts && !attribute->cts) { + uartInit->HardwareFlowControl = LL_USART_HWCONTROL_RTS; + } else if (!attribute->rts && attribute->cts) { + uartInit->HardwareFlowControl = LL_USART_HWCONTROL_CTS; + } else { + uartInit->HardwareFlowControl = LL_USART_HWCONTROL_NONE; + } + } + + return; +} + +static void TransDir(LL_USART_InitTypeDef *uartInit, struct UartAttribute *attribute, UART_ATTR_TRAN_TYPE type) +{ + if (uartInit == NULL || attribute == NULL) { + HDF_LOGE("null ptr\r\n"); + return; + } + if (type == UART_ATTR_NIOBE_TO_HDF) { + switch (uartInit->TransferDirection) { + case LL_USART_DIRECTION_NONE: + attribute->fifoRxEn = 0; + attribute->fifoTxEn = 0; + break; + case LL_USART_DIRECTION_RX: + attribute->fifoRxEn = 1; + attribute->fifoTxEn = 0; + break; + case LL_USART_DIRECTION_TX: + attribute->fifoRxEn = 0; + attribute->fifoTxEn = 1; + break; + case LL_USART_DIRECTION_TX_RX: + attribute->fifoRxEn = 1; + attribute->fifoTxEn = 1; + break; + default: + attribute->fifoRxEn = 1; + attribute->fifoTxEn = 1; + break; + } + } else { + if (attribute->fifoRxEn && attribute->fifoTxEn) { + uartInit->TransferDirection = LL_USART_DIRECTION_TX_RX; + } else if (attribute->fifoRxEn && !attribute->fifoTxEn) { + uartInit->TransferDirection = LL_USART_DIRECTION_RX; + } else if (!attribute->fifoRxEn && attribute->fifoTxEn) { + uartInit->TransferDirection = LL_USART_DIRECTION_TX; + } else { + uartInit->TransferDirection = LL_USART_DIRECTION_NONE; + } + } + + return; +} + +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + HDF_LOGI("%s: Enter", __func__); + UartDevice *uartDevice = NULL; + LL_USART_InitTypeDef *uartInit = NULL; + uint32_t uartId; + if (host == NULL || attribute == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + uartInit = &uartDevice->initTypedef; + if (uartInit == NULL) { + HDF_LOGE("%s: config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + TransDataWidth(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE); + TransParity(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE); + TransStopbit(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE); + TransFlowCtrl(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE); + TransDir(uartInit, attribute, UART_ATTR_HDF_TO_NIOBE); + + UartHostDevDeinit(host); + UartHostDevInit(host); + + return HDF_SUCCESS; +} + +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + HDF_LOGI("%s: Enter", __func__); + UartDevice *uartDevice = NULL; + LL_USART_InitTypeDef *uartInit = NULL; + if (host == NULL || attribute == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartInit = &uartDevice->initTypedef; + if (uartInit == NULL) { + HDF_LOGE("%s: uartInit is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + TransDataWidth(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF); + TransParity(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF); + TransStopbit(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF); + TransFlowCtrl(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF); + TransDir(uartInit, attribute, UART_ATTR_NIOBE_TO_HDF); + + return HDF_SUCCESS; +} + +static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode) +{ + HDF_LOGI("%s: Enter", __func__); + UartDevice *uartDevice = NULL; + uint32_t uartId; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + + switch (mode) { + case UART_MODE_RD_BLOCK: + g_uartCtx[uartId - 1].isBlock = true; + break; + case UART_MODE_RD_NONBLOCK: + g_uartCtx[uartId - 1].isBlock = false; + break; + case UART_MODE_DMA_RX_EN: + g_uartCtx[uartId - 1].rxDMA = true; + break; + case UART_MODE_DMA_RX_DIS: + g_uartCtx[uartId - 1].rxDMA = false; + break; + case UART_MODE_DMA_TX_EN: + g_uartCtx[uartId - 1].txDMA = true; + break; + case UART_MODE_DMA_TX_DIS: + g_uartCtx[uartId - 1].txDMA = false; + break; + default: + HDF_LOGE("%s: UartTransMode(%d) invalid", __func__, mode); + break; + } + + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/platform/uart/uart_wm.c b/platform/uart/uart_wm.c new file mode 100755 index 0000000000000000000000000000000000000000..5a9ce8bf10ac623850816e1dff09a9219135a701 --- /dev/null +++ b/platform/uart/uart_wm.c @@ -0,0 +1,704 @@ +/* + * Copyright (c) 2022 Jiangsu Hoperun Software Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ +#include +#include +#include "osal_time.h" +#include "osal_sem.h" +#include "device_resource_if.h" +#include "hdf_log.h" +#include "wm_gpio_afsel.h" +#include "uart_if.h" +#include "uart_core.h" +#include "wm_uart.h" + +#define HDF_UART_TMO 1000 +#define TMO_MS_UNIT_CONV (1000) +#define HDF_LOG_TAG uartDev + +#define UART_DEV_SERVICE_NAME_PREFIX "HDF_PLATFORM_UART%d" +#define MAX_DEV_NAME_SIZE 32 + +struct UartResource { + uint32_t num; /* UART port num */ + uint32_t baudRate; /* Default baudrate */ + uint32_t wLen; /* Default word length */ + uint32_t parity; /* Default parity */ + uint32_t stopBit; /* Default stop bits */ + bool txDMA; + bool rxDMA; +}; + +enum UartDeviceState { + UART_DEVICE_UNINITIALIZED = 0x0u, + UART_DEVICE_INITIALIZED = 0x1u, +}; + +struct UART_CTX_OBJ { + bool txDMA; + bool rxDMA; + bool isBlock; + struct OsalSem rxSem; + struct OsalSem txSem; +}; + +struct UartDevice { + struct IDeviceIoService ioService; + struct UartResource resource; + tls_uart_options_t config; + uint32_t uartId; + bool initFlag; + uint32_t transMode; +}; + +enum { + UART_READ = 0, + UART_WRITE +}; + +static struct UART_CTX_OBJ g_uartCtx[4] = {0}; +static unsigned char *g_uartKfifoBuffer[4] = {NULL, NULL, NULL, NULL}; + +static void HalSetUartIomux(int uartId) +{ + switch (uartId) { + case TLS_UART_0: + wm_uart0_tx_config(WM_IO_PB_19); + wm_uart0_rx_config(WM_IO_PB_20); + break; + case TLS_UART_1: + wm_uart1_tx_config(WM_IO_PB_06); + wm_uart1_rx_config(WM_IO_PB_07); + break; + case TLS_UART_2: + wm_uart2_tx_scio_config(WM_IO_PB_02); + wm_uart2_rx_config(WM_IO_PB_03); + break; + case TLS_UART_3: + wm_uart3_tx_config(WM_IO_PB_00); + wm_uart3_rx_config(WM_IO_PB_01); + break; + case TLS_UART_4: + wm_uart4_tx_config(WM_IO_PB_04); + wm_uart4_rx_config(WM_IO_PB_05); + break; + default: + HDF_LOGE("%s: uartId(%d) invalid", __func__, uartId); + break; + } +} + +static void dma_tx_cmpl_callback(uint32_t arg) +{ + uint32_t uartId = arg; + OsalSemPost(&g_uartCtx[uartId].txSem); +} + +static int32_t HalUartSend(uint32_t uartId, const uint8_t *data, uint32_t size, uint32_t timeOut) +{ + int32_t ret = HDF_FAILURE; + + if (data == NULL || size == 0) { + HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + tls_uart_dma_write(data, (uint16_t)size, dma_tx_cmpl_callback, (uint16_t)uartId); + ret = OsalSemWait(&g_uartCtx[uartId].txSem, timeOut); + + return ret; +} + +static int32_t HalUartRecv(uint8_t uartId, uint8_t *data, uint32_t expectSize, + uint32_t *recvSize, uint32_t timeOut) +{ + int32_t ret = HDF_FAILURE; + uint32_t fifoPopLen = 0; + uint32_t recvedLen = 0; + uint32_t expectLen = expectSize; + OsalTimespec hdfTs1 = { 0, 0 }; + OsalTimespec hdfTs2 = { 0, 0 }; + OsalTimespec hdfTsDiff = { 0, 0 }; + + if (data == NULL || expectSize == 0 || recvSize == NULL) { + HDF_LOGE("%s %d Invalid input \r\n", __FILE__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + OsalGetTime(&hdfTs1); + do { + fifoPopLen = tls_uart_read(uartId, (uint8_t *)data + recvedLen, expectLen); + recvedLen += fifoPopLen; + expectLen -= fifoPopLen; + if (recvedLen >= expectSize) { + break; + } + /* haven't get any data from fifo */ + if (recvedLen == 0) { + break; + } + /* if reaches here, it means need to wait for more data come */ + OsalMSleep(1); + /* time out break */ + OsalGetTime(&hdfTs2); + OsalDiffTime(&hdfTs1, &hdfTs2, &hdfTsDiff); + if ((uint32_t)(hdfTsDiff.sec * TMO_MS_UNIT_CONV + hdfTsDiff.usec / TMO_MS_UNIT_CONV) >= timeOut) { + break; + } + } while (1); + + if (recvSize != NULL) { + *recvSize = recvedLen; + } + return HDF_SUCCESS; +} + +static void HalUartHandlerInit(struct UartDevice *device) +{ + uint32_t uartId; + + if (device == NULL) { + HDF_LOGE("%s: INVALID PARAM", __func__); + return; + } + + uartId = device->uartId; + OsalSemInit(&g_uartCtx[uartId].rxSem, 0); + OsalSemInit(&g_uartCtx[uartId].txSem, 0); +} + +static void UartStart(struct UartDevice *device) +{ + uint32_t uartId; + tls_uart_options_t *uartCfg = NULL; + if (device == NULL) { + HDF_LOGE("%s: INVALID PARAM", __func__); + return; + } + uartId = device->uartId; + uartCfg = &device->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return; + } + if (WM_SUCCESS != tls_uart_port_init(uartId, uartCfg, 0)) { + HDF_LOGE("uart %d init error", uartId); + } + HDF_LOGI("%s %ld\r\n", __FUNCTION__, uartId); + HalUartHandlerInit(device); +} + +/* HdfDriverEntry method definitions */ +static int32_t UartDriverBind(struct HdfDeviceObject *device); +static int32_t UartDriverInit(struct HdfDeviceObject *device); +static void UartDriverRelease(struct HdfDeviceObject *device); + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_UartDriverEntry = { + .moduleVersion = 1, + .moduleName = "W800_UART_MODULE_HDF", + .Bind = UartDriverBind, + .Init = UartDriverInit, + .Release = UartDriverRelease, +}; + +/* Initialize HdfDriverEntry */ +HDF_INIT(g_UartDriverEntry); + +/* UartHostMethod method definitions */ +static int32_t UartHostDevInit(struct UartHost *host); +static int32_t UartHostDevDeinit(struct UartHost *host); +static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size); +static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate); +static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate); +static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size); +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute); +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute); +static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode); + +/* UartHostMethod definitions */ +struct UartHostMethod g_uartHostMethod = { + .Init = UartHostDevInit, + .Deinit = UartHostDevDeinit, + .Read = UartHostDevRead, + .Write = UartHostDevWrite, + .SetBaud = UartHostDevSetBaud, + .GetBaud = UartHostDevGetBaud, + .SetAttribute = UartHostDevSetAttribute, + .GetAttribute = UartHostDevGetAttribute, + .SetTransMode = UartHostDevSetTransMode, +}; + +static int InitUartDevice(struct UartHost *host) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + if (!uartDevice->initFlag) { + HDF_LOGI("uart %ld device init", uartDevice->uartId); + HalSetUartIomux(uartDevice->uartId); + UartStart(uartDevice); + uartDevice->initFlag = true; + } + + return HDF_SUCCESS; +} + +static uint32_t GetUartDeviceResource(struct UartDevice *device, const struct DeviceResourceNode *resourceNode) +{ + struct DeviceResourceIface *dri = NULL; + struct UartResource *resource = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("%s: INVALID PARAM", __func__); + return HDF_ERR_INVALID_PARAM; + } + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid"); + return HDF_ERR_INVALID_PARAM; + } + if (dri->GetUint32(resourceNode, "num", &resource->num, 0) != HDF_SUCCESS) { + HDF_LOGE("uart config read num fail"); + return HDF_FAILURE; + } + if (dri->GetUint32(resourceNode, "baudrate", &resource->baudRate, 0) != HDF_SUCCESS) { + HDF_LOGE("uart config read baudrate fail"); + return HDF_FAILURE; + } + if (dri->GetUint32(resourceNode, "parity", &resource->parity, 0) != HDF_SUCCESS) { + HDF_LOGE("uart config read parity fail"); + return HDF_FAILURE; + } + if (dri->GetUint32(resourceNode, "stopBit", &resource->stopBit, 0) != HDF_SUCCESS) { + HDF_LOGE("uart config read stopBit fail"); + return HDF_FAILURE; + } + + HDF_LOGI("%d, %d, %d, %d", resource->num, resource->baudRate, resource->parity); + + // copy config + device->uartId = resource->num; + device->config.baudrate = resource->baudRate; + device->config.paritytype = (TLS_UART_PMODE_T)resource->parity; + device->config.stopbits = (TLS_UART_STOPBITS_T)resource->stopBit; + device->config.charlength = TLS_UART_CHSIZE_8BIT; + device->config.flow_ctrl = TLS_UART_FLOW_CTRL_NONE; + return HDF_SUCCESS; +} + +static int32_t AttachUartDevice(struct UartHost *uartHost, struct HdfDeviceObject *device) +{ + int32_t ret; + struct UartDevice *uartDevice = NULL; + + if (uartHost == NULL || device == NULL || device->property == NULL) { + HDF_LOGE("%s: property is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)OsalMemAlloc(sizeof(struct UartDevice)); + if (uartDevice == NULL) { + HDF_LOGE("%s: OsalMemCalloc uartDevice error", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = GetUartDeviceResource(uartDevice, device->property); + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(uartDevice); + return HDF_FAILURE; + } + + uartHost->priv = uartDevice; + + return InitUartDevice(uartHost); +} + +static int32_t UartDriverBind(struct HdfDeviceObject *device) +{ + struct UartHost *devService; + if (device == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + devService = (struct UartHost *)OsalMemAlloc(sizeof(*devService)); + if (devService == NULL) { + HDF_LOGE("%s: OsalMemCalloc error", __func__); + return HDF_ERR_INVALID_OBJECT; + } + devService->device = device; + device->service = &(devService->service); + devService->priv = NULL; + devService->method = NULL; + return HDF_SUCCESS; +} + +static void UartDriverRelease(struct HdfDeviceObject *device) +{ + HDF_LOGI("Enter %s:", __func__); + uint32_t uartId; + struct UartHost *host = NULL; + struct UartDevice *uartDevice = NULL; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return; + } + + host = UartHostFromDevice(device); + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: host is NULL", __func__); + return; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return; + } + uartId = uartDevice->uartId; + host->method = NULL; + + OsalSemDestroy(&g_uartCtx[uartId].rxSem); + OsalSemDestroy(&g_uartCtx[uartId].txSem); + OsalMemFree(uartDevice); + OsalMemFree(host); +} + +static int32_t UartDriverInit(struct HdfDeviceObject *device) +{ + HDF_LOGI("Enter %s:", __func__); + int32_t ret; + struct UartHost *host = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + host = UartHostFromDevice(device); + if (host == NULL) { + HDF_LOGE("%s: host is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + ret = AttachUartDevice(host, device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: attach error", __func__); + return HDF_FAILURE; + } + + host->method = &g_uartHostMethod; + + return ret; +} + +/* UartHostMethod implementations */ +static int32_t UartHostDevInit(struct UartHost *host) +{ + HDF_LOGI("%s: Enter\r\n", __func__); + if (host == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + InitUartDevice(host); + return HDF_SUCCESS; +} + +static int32_t UartHostDevDeinit(struct UartHost *host) +{ + HDF_LOGI("%s: Enter", __func__); + uint32_t uartId; + struct UartDevice *uartDevice = NULL; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: INVALID OBJECT", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + uartDevice->initFlag = false; + + return HDF_SUCCESS; +} +static int32_t UartHostDevWrite(struct UartHost *host, uint8_t *data, uint32_t size) +{ + struct UartDevice *device = NULL; + uint32_t portId; + + if (host == NULL || data == NULL || size == 0 || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + device = (struct UartDevice *)host->priv; + if (device == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + portId = device->uartId; + + if (g_uartCtx[portId].txDMA) { + return HalUartSend(portId, data, size, HDF_UART_TMO); + } else { + if (tls_uart_write(portId, data, size) < 0) { + return HDF_FAILURE; + } + } + + return HDF_SUCCESS; +} + +static int32_t UartHostDevRead(struct UartHost *host, uint8_t *data, uint32_t size) +{ + uint32_t recvSize = 0; + int32_t ret; + uint32_t uartId; + struct UartDevice *uartDevice = NULL; + if (host == NULL || data == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + if (g_uartCtx[uartId].rxDMA) { + ret = HalUartRecv(uartId, data, size, &recvSize, HDF_UART_TMO); + if (ret != HDF_SUCCESS) { + HDF_LOGE("uart %ld recev error\r\n", uartId); + return ret; + } + ret = recvSize; + } else { + if (g_uartCtx[uartId].isBlock) { + while (tls_uart_try_read(uartId, 1) == 0); + ret = tls_uart_read(uartId, data, 1); + } else { + ret = tls_uart_read(uartId, data, 1); + } + ret = 1; + } + return ret; +} + +static int32_t UartHostDevSetBaud(struct UartHost *host, uint32_t baudRate) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + tls_uart_options_t *uartCfg = NULL; + uint32_t uartId; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + + uartCfg = &uartDevice->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: device config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartCfg->baudrate = baudRate; + + tls_uart_set_baud_rate(uartId, uartCfg->baudrate); + + return HDF_SUCCESS; +} + +static int32_t UartHostDevGetBaud(struct UartHost *host, uint32_t *baudRate) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + tls_uart_options_t *uartCfg = NULL; + uint32_t uartId; + if (host == NULL || baudRate == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + + uartCfg = &uartDevice->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + *baudRate = uartCfg->baudrate; + return HDF_SUCCESS; +} + +static int32_t UartHostDevSetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + struct UartDevice *uartDevice = NULL; + tls_uart_options_t *uartCfg = NULL; + uint32_t uartId; + if (host == NULL || attribute == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + uartCfg = &uartDevice->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + uartCfg->paritytype = attribute->parity; + tls_uart_set_parity(uartId, uartCfg->paritytype); + + switch (attribute->stopBits) { + case UART_ATTR_STOPBIT_1: + uartCfg->stopbits = TLS_UART_ONE_STOPBITS; + break; + case UART_ATTR_STOPBIT_2: + uartCfg->stopbits = TLS_UART_TWO_STOPBITS; + break; + default: + uartCfg->stopbits = TLS_UART_ONE_STOPBITS; + break; + } + tls_uart_set_stop_bits(uartId, uartCfg->stopbits); + + if (attribute->rts && attribute->cts) { + uartCfg->flow_ctrl = TLS_UART_FLOW_CTRL_HARDWARE; + } else { + uartCfg->flow_ctrl = TLS_UART_FLOW_CTRL_NONE; + } + tls_uart_set_fc_status(uartId, uartCfg->flow_ctrl); + return HDF_SUCCESS; +} + +static int32_t UartHostDevGetAttribute(struct UartHost *host, struct UartAttribute *attribute) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + tls_uart_options_t *uartCfg = NULL; + if (host == NULL || attribute == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartCfg = &uartDevice->config; + if (uartCfg == NULL) { + HDF_LOGE("%s: config is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + attribute->parity = uartCfg->paritytype; + + switch (uartCfg->stopbits) { + case TLS_UART_ONE_STOPBITS: + attribute->stopBits = UART_ATTR_STOPBIT_1; + break; + case TLS_UART_TWO_STOPBITS: + attribute->stopBits = UART_ATTR_STOPBIT_2; + break; + default: + attribute->stopBits = UART_ATTR_STOPBIT_1; + break; + } + + switch (uartCfg->flow_ctrl) { + case TLS_UART_FLOW_CTRL_HARDWARE: + attribute->rts = 1; + attribute->cts = 1; + break; + default: + attribute->rts = 0; + attribute->cts = 0; + break; + } + + return HDF_SUCCESS; +} + +static int32_t UartHostDevSetTransMode(struct UartHost *host, enum UartTransMode mode) +{ + HDF_LOGI("%s: Enter", __func__); + struct UartDevice *uartDevice = NULL; + uint32_t uartId; + if (host == NULL || host->priv == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + uartDevice = (struct UartDevice *)host->priv; + if (uartDevice == NULL) { + HDF_LOGE("%s: device is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + uartId = uartDevice->uartId; + + switch (mode) { + case UART_MODE_RD_BLOCK: + g_uartCtx[uartId].isBlock = true; + break; + case UART_MODE_RD_NONBLOCK: + g_uartCtx[uartId].isBlock = false; + break; + case UART_MODE_DMA_RX_EN: + g_uartCtx[uartId].rxDMA = true; + break; + case UART_MODE_DMA_RX_DIS: + g_uartCtx[uartId].rxDMA = false; + break; + case UART_MODE_DMA_TX_EN: + g_uartCtx[uartId].txDMA = true; + break; + case UART_MODE_DMA_TX_DIS: + g_uartCtx[uartId].txDMA = false; + break; + default: + HDF_LOGE("%s: UartTransMode(%d) invalid", __func__, mode); + break; + } + return HDF_SUCCESS; +} diff --git a/platform/watchdog/BUILD.gn b/platform/watchdog/BUILD.gn index 9dd31cf8608bf913d2d29a6d168b7121f58cf91a..1088512f116f6b94f6663e88f9436ae68cf70045 100644 --- a/platform/watchdog/BUILD.gn +++ b/platform/watchdog/BUILD.gn @@ -1,31 +1,10 @@ # -# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: +# This file is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. # -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other materials -# provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used -# to endorse or promote products derived from this software without specific prior written -# permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; -# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR -# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import("//drivers/adapter/khdf/liteos_m/hdf.gni") @@ -38,5 +17,23 @@ hdf_driver(module_name) { sources += [ "watchdog_bes.c" ] } + if (defined(LOSCFG_SOC_COMPANY_STMICROELECTRONICS)) { + sources += [ "watchdog_stm32f4xx.c" ] + } + + if (defined(LOSCFG_SOC_COMPANY_WINNERMICRO)) { + sources += [ "watchdog_wm.c" ] + } + include_dirs = [ "." ] + if (defined(LOSCFG_DRIVERS_HDF_CONFIG_MACRO)) { + HDF_HCS_BUILD_PATH = rebase_path( + "//device/board/$device_company/$device_name/liteos_m/hdf_config") + cmd = "if [ -f ${HDF_HCS_BUILD_PATH}/BUILD.gn ]; then echo true; else echo false; fi" + HAVE_HCS_BUILD_PATH = + exec_script("//build/lite/run_shell_cmd.py", [ cmd ], "value") + if (HAVE_HCS_BUILD_PATH) { + deps = [ "${HDF_HCS_BUILD_PATH}" ] + } + } } diff --git a/platform/watchdog/watchdog_bes.c b/platform/watchdog/watchdog_bes.c index d0e22d6ed3bec8093fb5ae46116c487a0a13d2e6..7071f41ca8b0b426d3ed4e4ea34ec19c74caf971 100755 --- a/platform/watchdog/watchdog_bes.c +++ b/platform/watchdog/watchdog_bes.c @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #include "watchdog_bes.h" #include #include @@ -53,7 +47,7 @@ static int InitWatchdogDevice(struct WatchdogDevice *watchdogDevice) struct WatchdogResource *resource = NULL; int32_t watchdogId; if (watchdogDevice == NULL) { - HDF_LOGE("%s: invaild parameter\r\n", __func__); + HDF_LOGE("%s: invalid parameter\r\n", __func__); return HDF_ERR_INVALID_PARAM; } @@ -188,7 +182,7 @@ static int32_t WatchdogDriverInit(struct HdfDeviceObject *device) } watchdogCntlr->ops = &g_WatchdogCntlrMethod; - + HDF_LOGE("WatchdogDriverInit success!\r\n"); return ret; } @@ -326,4 +320,4 @@ static int32_t WatchdogDevReleasePriv(struct WatchdogCntlr *watchdogCntlr) { (void)watchdogCntlr; return HDF_SUCCESS; -} \ No newline at end of file +} diff --git a/platform/watchdog/watchdog_bes.h b/platform/watchdog/watchdog_bes.h old mode 100755 new mode 100644 index 2f08bd2c172f8afd359fca3744bd1f6e048ac494..32dbe98b57b16196e3fde0335dc65afd981f8a05 --- a/platform/watchdog/watchdog_bes.h +++ b/platform/watchdog/watchdog_bes.h @@ -1,17 +1,11 @@ /* - * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. - * 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 + * Copyright (c) 2021-2022 Bestechnic (Shanghai) Co., Ltd. All rights reserved. * - * 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. + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. */ + #ifndef BES_WATCHDOG_H #define BES_WATCHDOG_H @@ -33,4 +27,4 @@ struct WatchdogDevice { #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/platform/watchdog/watchdog_stm32f4xx.c b/platform/watchdog/watchdog_stm32f4xx.c new file mode 100644 index 0000000000000000000000000000000000000000..934d43ccebd23ceae32916ac55ccf80215bbf4ab --- /dev/null +++ b/platform/watchdog/watchdog_stm32f4xx.c @@ -0,0 +1,379 @@ +/* + * Copyright (c) 2022 Talkweb Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#include "hcs_macro.h" +#include "hdf_config_macro.h" +#else +#include "device_resource_if.h" +#endif +#include "hdf_device_desc.h" +#include "hdf_log.h" +#include "watchdog_core.h" +#include "watchdog_if.h" + +#define WATCHDOG_MIN_TIMEOUT 1 +#define WATCHDOG_MAX_TIMEOUT 4096 +#define WATCHDOG_UPDATE_TIME (((6UL * 256UL * 1000UL) / LSI_VALUE) + ((LSI_STARTUP_TIME / 1000UL) + 1UL)) + +typedef struct { + int watchdogId; + int timeout; // Maximum interval between watchdog feeding, unit: ms +} WatchdogDeviceInfo; + +static IWDG_TypeDef *hdf_iwdg = NULL; +static int g_watchdogStart = 0; +static int g_watchdogTimeout = 0; + +static int32_t WatchdogDevStart(struct WatchdogCntlr *watchdogCntlr); +static int32_t WatchdogDevStop(struct WatchdogCntlr *watchdogCntlr); +static int32_t WatchdogDevSetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t seconds); +static int32_t WatchdogDevGetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t *seconds); +static int32_t WatchdogDevGetStatus(struct WatchdogCntlr *watchdogCntlr, uint32_t *status); +static int32_t WatchdogDevFeed(struct WatchdogCntlr *watchdogCntlr); + +struct WatchdogMethod g_WatchdogCntlrMethod = { + .getStatus = WatchdogDevGetStatus, + .setTimeout = WatchdogDevSetTimeout, + .getTimeout = WatchdogDevGetTimeout, + .start = WatchdogDevStart, + .stop = WatchdogDevStop, + .feed = WatchdogDevFeed, + .getPriv = NULL, + .releasePriv = NULL, +}; + +static int InitWatchdogDeviceInfo(WatchdogDeviceInfo *watchdogdeviceinfo) +{ + if (watchdogdeviceinfo == NULL) { + HDF_LOGE("%s: invaild parameter\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + return HDF_SUCCESS; +} + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO +#define WATCHDOG_FIND_CONFIG(node, name, device) \ + do { \ + if (strcmp(HCS_PROP(node, match_attr), name) == 0) { \ + device->watchdogId = HCS_PROP(node, id); \ + device->timeout = HCS_PROP(node, timeout); \ + result = HDF_SUCCESS; \ + } \ + } while (0) +#define PLATFORM_CONFIG HCS_NODE(HCS_ROOT, platform) +#define PLATFORM_WATCHDOG_CONFIG HCS_NODE(HCS_NODE(HCS_ROOT, platform), watchdog_config) +static uint32_t GetWatchdogDeviceInfoResource(WatchdogDeviceInfo *device, const char *deviceMatchAttr) +{ + int32_t result = HDF_FAILURE; + if (device == NULL || deviceMatchAttr == NULL) { + HDF_LOGE("device or deviceMatchAttr is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } +#if HCS_NODE_HAS_PROP(PLATFORM_CONFIG, watchdog_config) + HCS_FOREACH_CHILD_VARGS(PLATFORM_WATCHDOG_CONFIG, WATCHDOG_FIND_CONFIG, deviceMatchAttr, device); +#endif + if (result != HDF_SUCCESS) { + HDF_LOGE("resourceNode %s is NULL\r\n", deviceMatchAttr); + return result; + } + + return HDF_SUCCESS; +} +#else +static uint32_t GetWatchdogDeviceInfoResource(WatchdogDeviceInfo *device, const struct DeviceResourceNode *resourceNode) +{ + struct DeviceResourceIface *dri = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("resource or device is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + if (dri->GetUint32(resourceNode, "id", &device->watchdogId, 0) != HDF_SUCCESS) { + HDF_LOGE("read watchdogId fail\r\n"); + return HDF_FAILURE; + } + if (dri->GetUint32(resourceNode, "timeout", &device->timeout, 0) != HDF_SUCCESS) { + HDF_LOGE("read watchdogId fail\r\n"); + return HDF_FAILURE; + } + + HDF_LOGI("watchdogId = %d\n", device->watchdogId); + HDF_LOGI("timeout = %dms\n", device->timeout); + + return HDF_SUCCESS; +} +#endif + +static int32_t AttachWatchdogDeviceInfo(struct WatchdogCntlr *watchdogCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + WatchdogDeviceInfo *watchdogdeviceinfo = NULL; + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + if (device == NULL || device->deviceMatchAttr == NULL) { +#else + if (device == NULL || device->property == NULL) { +#endif + HDF_LOGE("%s: param is NULL\r\n", __func__); + return HDF_FAILURE; + } + + watchdogdeviceinfo = (WatchdogDeviceInfo *)OsalMemAlloc(sizeof(WatchdogDeviceInfo)); + if (watchdogdeviceinfo == NULL) { + HDF_LOGE("%s: OsalMemAlloc WatchdogDeviceInfo error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + +#ifdef LOSCFG_DRIVERS_HDF_CONFIG_MACRO + ret = GetWatchdogDeviceInfoResource(watchdogdeviceinfo, device->deviceMatchAttr); +#else + ret = GetWatchdogDeviceInfoResource(watchdogdeviceinfo, device->property); +#endif + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(watchdogdeviceinfo); + return HDF_FAILURE; + } + + (void)OsalMutexInit(&watchdogCntlr->lock); + + watchdogCntlr->priv = watchdogdeviceinfo; + watchdogCntlr->wdtId = watchdogdeviceinfo->watchdogId; + + return InitWatchdogDeviceInfo(watchdogdeviceinfo); +} +/* HdfDriverEntry method definitions */ +static int32_t WatchdogDriverBind(struct HdfDeviceObject *device); +static int32_t WatchdogDriverInit(struct HdfDeviceObject *device); +static void WatchdogDriverRelease(struct HdfDeviceObject *device); + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_watchdogDriverEntry = { + .moduleVersion = 1, + .moduleName = "ST_WATCHDOG_MODULE_HDF", + .Bind = WatchdogDriverBind, + .Init = WatchdogDriverInit, + .Release = WatchdogDriverRelease, +}; + +// Initialize HdfDriverEntry +HDF_INIT(g_watchdogDriverEntry); + +static int32_t WatchdogDriverBind(struct HdfDeviceObject *device) +{ + struct WatchdogCntlr *watchdogCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("hdfDevice object is null!\r\n"); + return HDF_FAILURE; + } + + watchdogCntlr = (struct WatchdogCntlr *)OsalMemAlloc(sizeof(struct WatchdogCntlr)); + if (watchdogCntlr == NULL) { + HDF_LOGE("%s: OsalMemAlloc watchdogCntlr error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + HDF_LOGI("Enter %s\r\n", __func__); + device->service = &watchdogCntlr->service; + watchdogCntlr->device = device; + watchdogCntlr->priv = NULL; + return HDF_SUCCESS; +} + +static int32_t WatchdogDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct WatchdogCntlr *watchdogCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGI("Enter %s:\r\n", __func__); + + watchdogCntlr = WatchdogCntlrFromDevice(device); + if (watchdogCntlr == NULL) { + HDF_LOGE("%s: watchdogCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = AttachWatchdogDeviceInfo(watchdogCntlr, device); + if (ret != HDF_SUCCESS) { + OsalMemFree(watchdogCntlr); + HDF_LOGE("%s:attach error\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + watchdogCntlr->ops = &g_WatchdogCntlrMethod; + + HDF_LOGI("WatchdogDriverInit success!\r\n"); + return ret; +} + +static void WatchdogDriverRelease(struct HdfDeviceObject *device) +{ + struct WatchdogCntlr *watchdogCntlr = NULL; + WatchdogDeviceInfo *watchdogdeviceinfo = NULL; + + if (device == NULL) { + HDF_LOGE("device is null\r\n"); + return; + } + + watchdogCntlr = WatchdogCntlrFromDevice(device); + if (watchdogCntlr == NULL || watchdogCntlr->priv == NULL) { + HDF_LOGE("%s: watchdogCntlr is NULL\r\n", __func__); + return; + } + + watchdogdeviceinfo = (WatchdogDeviceInfo *)watchdogCntlr->priv; + if (watchdogdeviceinfo != NULL) { + OsalMemFree(watchdogdeviceinfo); + } + return; +} + +static int32_t WatchdogDevStart(struct WatchdogCntlr *watchdogCntlr) +{ + WatchdogDeviceInfo *watchdogdeviceinfo = NULL; + int32_t watchdogId = 0; + int32_t timeout = 0; + unsigned long long tickstart = 0; + + if (watchdogCntlr == NULL || watchdogCntlr->priv == NULL) { + HDF_LOGE("%s: watchdogCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + watchdogdeviceinfo = (WatchdogDeviceInfo *)watchdogCntlr->priv; + if (watchdogdeviceinfo == NULL) { + HDF_LOGE("%s: OBJECT is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + watchdogId = watchdogdeviceinfo->watchdogId; + timeout = watchdogdeviceinfo->timeout; + + if (timeout < WATCHDOG_MIN_TIMEOUT) { + HDF_LOGW("%s: watchdog timeout must >= 1, set the timeout to 1ms\r\n", __func__); + timeout = WATCHDOG_MIN_TIMEOUT; + } + if (timeout > WATCHDOG_MAX_TIMEOUT) { + HDF_LOGW("%s: watchdog timeout must <= 1, set the timeout to 4096ms\r\n", __func__); + timeout = WATCHDOG_MAX_TIMEOUT; + } + + HDF_LOGI("%s: watchdog Started! timeout: %dms\r\n", __func__, timeout); + + hdf_iwdg = IWDG; // Point to watchdog register + hdf_iwdg->KR = IWDG_KEY_ENABLE; + hdf_iwdg->KR = IWDG_KEY_WRITE_ACCESS_ENABLE; + hdf_iwdg->PR = IWDG_PRESCALER_32; // 32 frequency division + hdf_iwdg->RLR = timeout - 1; // 32K crystal oscillator corresponds to 1-4096ms under 32 prescaled frequency + + tickstart = LOS_TickCountGet(); + // Wait for the register value to be updated and confirm that the watchdog is started successfully + while ((hdf_iwdg->SR & (IWDG_SR_RVU | IWDG_SR_PVU)) != 0x00u) { + if ((LOS_TickCountGet() - tickstart) > WATCHDOG_UPDATE_TIME) { + if ((hdf_iwdg->SR & (IWDG_SR_RVU | IWDG_SR_PVU)) != 0x00u) { + return HDF_FAILURE; + } + } + } + hdf_iwdg->KR = IWDG_KEY_RELOAD; // Reload initial value + + g_watchdogStart = 1; + return HDF_SUCCESS; +} + +static int32_t WatchdogDevStop(struct WatchdogCntlr *watchdogCntlr) +{ + HDF_LOGW("%s: WatchdogDevStop fail,because of soc not support!!\r\n", __func__); + return HDF_FAILURE; +} + +static int32_t WatchdogDevSetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t seconds) +{ + WatchdogDeviceInfo *watchdogdeviceinfo = NULL; + if (watchdogCntlr == NULL || watchdogCntlr->priv == NULL) { + HDF_LOGE("%s: watchdogCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + watchdogdeviceinfo = (WatchdogDeviceInfo *)watchdogCntlr->priv; + if (watchdogdeviceinfo == NULL) { + HDF_LOGE("%s: OBJECT is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + watchdogdeviceinfo->timeout = seconds; + + return HDF_SUCCESS; +} + +static int32_t WatchdogDevGetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t *seconds) +{ + WatchdogDeviceInfo *watchdogdeviceinfo = NULL; + if (watchdogCntlr == NULL || seconds == NULL) { + HDF_LOGE("%s: PARAM is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + watchdogdeviceinfo = (WatchdogDeviceInfo *)watchdogCntlr->priv; + if (watchdogdeviceinfo == NULL) { + HDF_LOGE("%s: OBJECT is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + *seconds = watchdogdeviceinfo->timeout; + return HDF_SUCCESS; +} + +static int32_t WatchdogDevGetStatus(struct WatchdogCntlr *watchdogCntlr, uint32_t *status) +{ + if (watchdogCntlr == NULL || status == NULL) { + HDF_LOGE("%s: PARAM is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + if (g_watchdogStart == 1) { + *status = WATCHDOG_START; + } else { + *status = WATCHDOG_STOP; + } + return HDF_SUCCESS; +} + +static int32_t WatchdogDevFeed(struct WatchdogCntlr *watchdogCntlr) +{ + (void)watchdogCntlr; + if (hdf_iwdg != NULL) + hdf_iwdg->KR = IWDG_KEY_RELOAD; // Reload initial value + return HDF_SUCCESS; +} + +static int32_t WatchdogDevGetPriv(struct WatchdogCntlr *watchdogCntlr) +{ + (void)watchdogCntlr; + return HDF_SUCCESS; +} + +static int32_t WatchdogDevReleasePriv(struct WatchdogCntlr *watchdogCntlr) +{ + (void)watchdogCntlr; + return HDF_SUCCESS; +} \ No newline at end of file diff --git a/platform/watchdog/watchdog_wm.c b/platform/watchdog/watchdog_wm.c new file mode 100755 index 0000000000000000000000000000000000000000..6a381a23234083932068471d547ff247a3ced896 --- /dev/null +++ b/platform/watchdog/watchdog_wm.c @@ -0,0 +1,259 @@ +/* + * Copyright (c) 2022 Jiangsu Hoperun Software Co., Ltd. + * + * This file is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include "device_resource_if.h" +#include "hdf_device_desc.h" +#include "hdf_log.h" +#include "watchdog_if.h" +#include "watchdog_core.h" +#include "wm_regs.h" +#include "wm_cpu.h" + +struct WatchdogResource { + int32_t watchdogId; +}; + +struct WatchdogDevice { + struct WatchdogResource resource; +}; + +static int g_watchdogStart; +static int g_watchdogTimeout; + +static int32_t WatchdogDevStart(struct WatchdogCntlr *watchdogCntlr); +static int32_t WatchdogDevSetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t seconds); +static int32_t WatchdogDevGetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t *seconds); +static int32_t WatchdogDevGetStatus(struct WatchdogCntlr *watchdogCntlr, uint32_t *status); +static int32_t WatchdogDevFeed(struct WatchdogCntlr *watchdogCntlr); + +struct WatchdogMethod g_WatchdogCntlrMethod = { + .getStatus = WatchdogDevGetStatus, + .setTimeout = WatchdogDevSetTimeout, + .getTimeout = WatchdogDevGetTimeout, + .start = WatchdogDevStart, + .stop = NULL, // WatchdogDevStop + .feed = WatchdogDevFeed, + .getPriv = NULL, // WatchdogDevGetPriv + .releasePriv = NULL, // WatchdogDevReleasePriv +}; + +static int32_t WatchdogDevStart(struct WatchdogCntlr *watchdogCntlr) +{ + tls_watchdog_start_cal_elapsed_time(); + g_watchdogStart = 1; + return HDF_SUCCESS; +} + +static int32_t WatchdogDevSetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t seconds) +{ + g_watchdogTimeout = seconds; + tls_watchdog_init(seconds); + return HDF_SUCCESS; +} + +static int32_t WatchdogDevGetTimeout(struct WatchdogCntlr *watchdogCntlr, uint32_t *seconds) +{ + if (watchdogCntlr == NULL || seconds == NULL) { + HDF_LOGE("%s: PARAM is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + g_watchdogTimeout = tls_watchdog_stop_cal_elapsed_time(); + + *seconds = g_watchdogTimeout; + return HDF_SUCCESS; +} + +static int32_t WatchdogDevGetStatus(struct WatchdogCntlr *watchdogCntlr, uint32_t *status) +{ + if (watchdogCntlr == NULL || status == NULL) { + HDF_LOGE("%s: PARAM is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + if (g_watchdogStart == 1) { + *status = WATCHDOG_START; + } else { + *status = WATCHDOG_STOP; + } + return HDF_SUCCESS; +} + +static int32_t WatchdogDevFeed(struct WatchdogCntlr *watchdogCntlr) +{ + tls_sys_clk sysclk; + + tls_sys_clk_get(&sysclk); + if (g_watchdogStart == 1) { + tls_sys_reset(); + } + return HDF_SUCCESS; +} + +static int InitWatchdogDevice(struct WatchdogDevice *watchdogDevice) +{ + struct WatchdogResource *resource = NULL; + if (watchdogDevice == NULL) { + HDF_LOGE("%s: invaild parameter\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + resource = &watchdogDevice->resource; + if (resource == NULL) { + HDF_LOGE("resource is NULL\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + return HDF_SUCCESS; +} + +static uint32_t GetWatchdogDeviceResource( + struct WatchdogDevice *device, const struct DeviceResourceNode *resourceNode) +{ + struct WatchdogResource *resource = NULL; + struct DeviceResourceIface *dri = NULL; + if (device == NULL || resourceNode == NULL) { + HDF_LOGE("resource or device is NULL\r\n"); + return HDF_ERR_INVALID_PARAM; + } + + resource = &device->resource; + if (resource == NULL) { + HDF_LOGE("resource is NULL\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + dri = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (dri == NULL || dri->GetUint32 == NULL) { + HDF_LOGE("DeviceResourceIface is invalid\r\n"); + return HDF_ERR_INVALID_OBJECT; + } + + return HDF_SUCCESS; +} + +static int32_t AttachWatchdogDevice(struct WatchdogCntlr *watchdogCntlr, struct HdfDeviceObject *device) +{ + int32_t ret; + struct WatchdogDevice *watchdogDevice = NULL; + + if (device == NULL || device->property == NULL) { + HDF_LOGE("%s: param is NULL\r\n", __func__); + return HDF_FAILURE; + } + + watchdogDevice = (struct WatchdogDevice *)OsalMemAlloc(sizeof(struct WatchdogDevice)); + if (watchdogDevice == NULL) { + HDF_LOGE("%s: OsalMemAlloc watchdogDevice error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + ret = GetWatchdogDeviceResource(watchdogDevice, device->property); + if (ret != HDF_SUCCESS) { + (void)OsalMemFree(watchdogDevice); + return HDF_FAILURE; + } + + watchdogCntlr->priv = watchdogDevice; + + return InitWatchdogDevice(watchdogDevice); +} + +/* HdfDriverEntry method definitions */ +static int32_t WatchdogDriverBind(struct HdfDeviceObject *device); +static int32_t WatchdogDriverInit(struct HdfDeviceObject *device); +static void WatchdogDriverRelease(struct HdfDeviceObject *device); + +static int32_t WatchdogDriverBind(struct HdfDeviceObject *device) +{ + struct WatchdogCntlr *watchdogCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("hdfDevice object is null!\r\n"); + return HDF_FAILURE; + } + + watchdogCntlr = (struct WatchdogCntlr *)OsalMemAlloc(sizeof(struct WatchdogCntlr)); + if (watchdogCntlr == NULL) { + HDF_LOGE("%s: OsalMemAlloc watchdogCntlr error\r\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + HDF_LOGI("Enter %s\r\n", __func__); + device->service = &watchdogCntlr->service; + watchdogCntlr->device = device; + watchdogCntlr->priv = NULL; + return HDF_SUCCESS; +} + +static int32_t WatchdogDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret; + struct WatchdogCntlr *watchdogCntlr = NULL; + + if (device == NULL) { + HDF_LOGE("%s: device is NULL\r\n", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGI("Enter %s:\r\n", __func__); + + watchdogCntlr = WatchdogCntlrFromDevice(device); + if (watchdogCntlr == NULL) { + HDF_LOGE("%s: watchdogCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = AttachWatchdogDevice(watchdogCntlr, device); + if (ret != HDF_SUCCESS) { + OsalMemFree(watchdogCntlr); + HDF_LOGE("%s:attach error\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + watchdogCntlr->ops = &g_WatchdogCntlrMethod; + + HDF_LOGE("WatchdogDriverInit success!\r\n"); + return ret; +} + +static void WatchdogDriverRelease(struct HdfDeviceObject *device) +{ + struct WatchdogCntlr *watchdogCntlr = NULL; + struct WatchdogDevice *watchdogDevice = NULL; + + if (device == NULL) { + HDF_LOGE("device is null\r\n"); + return; + } + + watchdogCntlr = WatchdogCntlrFromDevice(device); + if (watchdogCntlr == NULL || watchdogCntlr->priv == NULL) { + HDF_LOGE("%s: watchdogCntlr is NULL\r\n", __func__); + return HDF_ERR_INVALID_PARAM; + } + + watchdogDevice = (struct WatchdogDevice *)watchdogCntlr->priv; + if (watchdogDevice != NULL) { + OsalMemFree(watchdogDevice); + } + return; +} + +/* HdfDriverEntry definitions */ +struct HdfDriverEntry g_watchdogDriverEntry = { + .moduleVersion = 1, + .moduleName = "WM_WATCHDOG_MODULE_HDF", + .Bind = WatchdogDriverBind, + .Init = WatchdogDriverInit, + .Release = WatchdogDriverRelease, +}; + +// Initialize HdfDriverEntry +HDF_INIT(g_watchdogDriverEntry); diff --git a/uhdf/README.md b/uhdf/README.md index 7b5c9720e558ee72ea1c5583066013299bead893..3187078a4e6958c90260345140468ab0d1663ba2 100644 --- a/uhdf/README.md +++ b/uhdf/README.md @@ -24,9 +24,9 @@ This repository stores the code and compilation scripts for the OpenHarmony driv [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) -drivers\_adapter\_uhdf +[drivers\_adapter\_uhdf](https://gitee.com/openharmony/drivers_adapter/blob/master/uhdf/README_zh.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README.md) -[drivers\_adapter\_khdf\_liteos](https://gitee.com/openharmony/drivers_adapter_khdf_liteos/blob/master/README.md) +[drivers\_adapter\_khdf\_liteos](https://gitee.com/openharmony/drivers_adapter/blob/master/khdf/liteos/README.md) diff --git a/uhdf/README_zh.md b/uhdf/README_zh.md index 8e9d52f36e1ef29a97d54ac3e5f290416ede9cc6..3ea6ec1b771ec0fe8cdce0e6dc7b08ba121d8e80 100644 --- a/uhdf/README_zh.md +++ b/uhdf/README_zh.md @@ -24,9 +24,9 @@ [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README_zh.md) -drivers\_adapter\_uhdf +[drivers\_adapter\_uhdf](https://gitee.com/openharmony/drivers_adapter/blob/master/uhdf/README_zh.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README_zh.md) -[drivers\_adapter\_khdf\_liteos](https://gitee.com/openharmony/drivers_adapter_khdf_liteos/blob/master/README_zh.md) +[drivers\_adapter\_khdf\_liteos](https://gitee.com/openharmony/drivers_adapter/blob/master/khdf/liteos/README_zh.md) diff --git a/uhdf/platform/BUILD.gn b/uhdf/platform/BUILD.gn old mode 100755 new mode 100644 index bf7558220752e9bbee7704091bd6785d8997ce75..23ccc94f999d5c915b00788d721d97528620268d --- a/uhdf/platform/BUILD.gn +++ b/uhdf/platform/BUILD.gn @@ -52,6 +52,7 @@ ohos_shared_library("hdf_platform") { "$HDF_FRAMEWORKS/model/storage/src/mmc/mmc_if.c", "$HDF_FRAMEWORKS/support/platform/src/adc/adc_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/dac/dac_if_u.c", + "$HDF_FRAMEWORKS/support/platform/src/fwk/platform_listener_u.c", "$HDF_FRAMEWORKS/support/platform/src/gpio/gpio_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/hdmi/hdmi_if.c", "$HDF_FRAMEWORKS/support/platform/src/i2c/i2c_if_u.c", diff --git a/uhdf/posix/BUILD.gn b/uhdf/posix/BUILD.gn old mode 100755 new mode 100644 diff --git a/uhdf/test/unittest/platform/BUILD.gn b/uhdf/test/unittest/platform/BUILD.gn index 487859f9d216aa236cb68eb5eb680966ee293270..c71e01127cb380c09296b2edf8be06efb3f2e687 100644 --- a/uhdf/test/unittest/platform/BUILD.gn +++ b/uhdf/test/unittest/platform/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 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 @@ -35,28 +35,31 @@ if (board_name == "hi3516dv300" || board_name == "hispark_taurus") { if (ohos_kernel_type == "liteos_a") { defines += [ "HDF_LITEOS_TEST" ] } + if (ohos_kernel_type == "linux") { + defines += [ "_LINUX_USER_" ] + } sources = [ + "//drivers/framework/support/platform/test/unittest/common/hdf_adc_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_can_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_gpio_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_device_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_platform_dumper_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_event_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_manager_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_queue_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_platform_trace_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_pwm_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_regulator_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_rtc_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/adc_test.c", "//drivers/framework/test/unittest/platform/common/gpio_test.c", "//drivers/framework/test/unittest/platform/common/pwm_test.c", "//drivers/framework/test/unittest/platform/common/rtc_test.c", "//drivers/framework/test/unittest/platform/common/spi_test.c", - "//drivers/framework/test/unittest/platform/common/uart_test.c", - "//drivers/framework/test/unittest/platform/common/watchdog_test.c", ] if (ohos_kernel_type == "liteos_a") { sources += [ - "//drivers/framework/support/platform/test/unittest/common/hdf_adc_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_dac_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_i2s_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_i3c_test.cpp", @@ -64,9 +67,12 @@ if (board_name == "hi3516dv300" || board_name == "hispark_taurus") { "//drivers/framework/support/platform/test/unittest/common/hdf_pcie_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_pin_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_timer_test.cpp", - "//drivers/framework/test/unittest/platform/common/adc_test.c", + "//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/dac_test.c", "//drivers/framework/test/unittest/platform/common/timer_test.c", + "//drivers/framework/test/unittest/platform/common/uart_test.c", + "//drivers/framework/test/unittest/platform/common/watchdog_test.c", ] } if (storage_type == "emmc") { @@ -114,6 +120,7 @@ if (board_name == "hi3516dv300" || board_name == "hispark_taurus") { defines = [ "__USER__" ] sources = [ "//drivers/framework/support/platform/test/unittest/common/hdf_platform_device_test.cpp", + "//drivers/framework/support/platform/test/unittest/common/hdf_platform_dumper_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_event_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_manager_test.cpp", "//drivers/framework/support/platform/test/unittest/common/hdf_platform_queue_test.cpp", diff --git a/uhdf2/README.md b/uhdf2/README.md index c8efe7f483dff0ddaf8115d947a69407fc20f466..ce6dca91df297be5aad3969097177582960882cf 100644 --- a/uhdf2/README.md +++ b/uhdf2/README.md @@ -29,7 +29,7 @@ This repository stores the code and compilation scripts for the user-mode Hardwa [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) -drivers\_adapter +[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README.md) [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 index 67a3d90763ec9d1e8cfc32e3a9544eafc19aceb5..fff6ba756573cfb5bf351a74a877cc00611c01e3 100644 --- a/uhdf2/README_zh.md +++ b/uhdf2/README_zh.md @@ -29,7 +29,7 @@ [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README_zh.md) -drivers\_adapter +[drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README_zh.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README_zh.md) diff --git a/uhdf2/hcs/BUILD.gn b/uhdf2/hcs/BUILD.gn index 077f2c998088a093d4697cdff6e503672a42843e..ff65e837c1422c03da3d25a0085f5987a88f579b 100644 --- a/uhdf2/hcs/BUILD.gn +++ b/uhdf2/hcs/BUILD.gn @@ -16,18 +16,7 @@ import("//drivers/adapter/uhdf2/uhdf.gni") import("$hdf_framework_path/tools/hc-gen/hc_gen.gni") hc_gen("build_hcs") { - gadget = "acm" - if (product_name == "Hi3516DV300" || product_name == "watchos") { - gadget = "ecm_acm" - } hcs_source = "/hdf.hcs" - if (gadget == "acm") { - hcs_source = "/hdf.hcs" - } else if (gadget == "ecm") { - hcs_source = "/hdf_ecm.hcs" - } else if (gadget == "ecm_acm") { - hcs_source = "/hdf_ecm_acm.hcs" - } if (product_company == "openharmony") { hcs_config_path = "default" @@ -47,3 +36,25 @@ ohos_prebuilt_etc("hdf_default.hcb") { subsystem_name = "hdf" part_name = "device_driver_framework" } + +hc_gen_start_cfg("gen_start_cfg") { + hcs_source = "/hdf.hcs" + + if (product_company == "openharmony") { + hcs_config_path = "default" + } else { + hcs_config_path = "//vendor/$product_company/$product_name/hdf_config/uhdf/" + } + sources = [ rebase_path(hcs_config_path + hcs_source) ] + outputs = [ "$target_gen_dir/hdf_devhost.cfg" ] +} + +ohos_prebuilt_etc("hdf_devhost.cfg") { + deps = [ ":gen_start_cfg" ] + outputs = get_target_outputs(":gen_start_cfg") + source = outputs[0] + relative_install_dir = "init" + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "device_driver_framework" +} diff --git a/uhdf2/hcs/camera/hal/mpp/hispark_taurus/driver/mpp_config.hcs b/uhdf2/hcs/camera/hal/mpp/hispark_taurus/driver/mpp_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/mpp/hispark_taurus/hdi_impl/camera_host_config.hcs b/uhdf2/hcs/camera/hal/mpp/hispark_taurus/hdi_impl/camera_host_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/mpp/hispark_taurus/hdi_impl/stream_supported_config.hcs b/uhdf2/hcs/camera/hal/mpp/hispark_taurus/hdi_impl/stream_supported_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/mpp/hispark_taurus/pipeline_core/config.hcs b/uhdf2/hcs/camera/hal/mpp/hispark_taurus/pipeline_core/config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/mpp/hispark_taurus/pipeline_core/ipp_algo_config.hcs b/uhdf2/hcs/camera/hal/mpp/hispark_taurus/pipeline_core/ipp_algo_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/mpp/hispark_taurus/pipeline_core/params.hcs b/uhdf2/hcs/camera/hal/mpp/hispark_taurus/pipeline_core/params.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/v4l2/rpi4b/hdi_impl/camera_host_config.hcs b/uhdf2/hcs/camera/hal/v4l2/rpi4b/hdi_impl/camera_host_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/v4l2/rpi4b/hdi_impl/stream_supported_config.hcs b/uhdf2/hcs/camera/hal/v4l2/rpi4b/hdi_impl/stream_supported_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/v4l2/rpi4b/pipeline_core/config.hcs b/uhdf2/hcs/camera/hal/v4l2/rpi4b/pipeline_core/config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/v4l2/rpi4b/pipeline_core/ipp_algo_config.hcs b/uhdf2/hcs/camera/hal/v4l2/rpi4b/pipeline_core/ipp_algo_config.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/camera/hal/v4l2/rpi4b/pipeline_core/params.hcs b/uhdf2/hcs/camera/hal/v4l2/rpi4b/pipeline_core/params.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/default/device_info.hcs b/uhdf2/hcs/default/device_info.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hcs/default/hdf.hcs b/uhdf2/hcs/default/hdf.hcs old mode 100755 new mode 100644 diff --git a/uhdf2/hdi.gni b/uhdf2/hdi.gni old mode 100755 new mode 100644 index d664368b15f90ba5d49862fe8f28b961321c73fb..13132badfe295db996ebfc00ec938d1c5f380968 --- a/uhdf2/hdi.gni +++ b/uhdf2/hdi.gni @@ -22,12 +22,20 @@ template("hdi") { assert(defined(invoker.subsystem_name), "subsystem_name are must") assert(defined(invoker.part_name), "part_name are must") - root_package_mapping = "ohos.hdi:drivers/interface" + root_package = "ohos.hdi" + root_path = rebase_path("//drivers/interface") + if (defined(invoker.root)) { + package_path_map = string_split(invoker.root, ":") + root_package = package_path_map[0] + root_path = rebase_path(package_path_map[1]) + } + root_package_path = "${root_package}:${root_path}" + get_info_args = [ "-i", invoker.language, rebase_path(target_gen_dir), - root_package_mapping, + root_package_path, ] get_info_args += rebase_path(invoker.sources) files_info = exec_script("$hdf_fwk_path/tools/hdi-gen/build_hdi_files.py", @@ -50,9 +58,7 @@ template("hdi") { config("hdi_public_config") { include_dirs = [ - "$hdf_uhdf_path/include/hdi", "$hdf_uhdf_path/include/host", - "$hdf_uhdf_path/ipc/include", "${files_info.root_package_dir}", "${files_info.version_dir}", ] @@ -100,7 +106,7 @@ template("hdi") { } args += [ "-r", - root_package_mapping, + root_package_path, ] } @@ -121,13 +127,18 @@ template("hdi") { public_configs = [ ":hdi_public_config" ] - deps = [ + deps = [ "$hdi_gen" ] + + public_deps = [ "$hdf_uhdf_path/hdi:libhdi", - "$hdi_gen", "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", "//drivers/adapter/uhdf2/utils:libhdf_utils", ] + if (defined(invoker.sequenceable)) { + public_deps += invoker.sequenceable + } + if (is_standard_system) { external_deps = [ "hiviewdfx_hilog_native:libhilog", @@ -143,7 +154,7 @@ template("hdi") { install_images = [ system_base_dir ] subsystem_name = invoker.subsystem_name - part_name = invoker.part_name + part_name = invoker.part_name + "_interface" } } @@ -164,13 +175,18 @@ template("hdi") { public_configs = [ ":hdi_public_config" ] - deps = [ + deps = [ "$hdi_gen" ] + + public_deps = [ "$hdf_uhdf_path/hdi:libhdi", - "$hdi_gen", "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", "//drivers/adapter/uhdf2/utils:libhdf_utils", ] + if (defined(invoker.sequenceable)) { + public_deps += invoker.sequenceable + } + if (is_standard_system) { external_deps = [ "hiviewdfx_hilog_native:libhilog", @@ -180,7 +196,9 @@ template("hdi") { external_deps = [ "hilog:libhilog" ] } - if (invoker.language == "cpp") { + if (invoker.language == "c") { + deps += [ "$hdf_uhdf_path/host:libhdf_host" ] + } else if (invoker.language == "cpp") { external_deps += [ "ipc:ipc_single" ] } diff --git a/uhdf2/hdi/BUILD.gn b/uhdf2/hdi/BUILD.gn index c089d80ff549234763a4b14f8c25b3953c57daa3..1fba60251360eb4d57e6de4cfb5b2d32ae67f364 100644 --- a/uhdf2/hdi/BUILD.gn +++ b/uhdf2/hdi/BUILD.gn @@ -57,13 +57,15 @@ if (defined(ohos_lite)) { sources = [ "$hdf_framework_path/core/shared/src/service_status.c", "src/devmgr_client.c", - "src/hdf_load_hdi.c", "src/hdi_smq_syncer.cpp", + "src/hdi_support.cpp", "src/idevmgr_client.cpp", "src/iservmgr_client.cpp", + "src/object_collector.cpp", "src/servmgr_client.c", "src/servstat_listener.c", "src/servstat_listener_stub.cpp", + "src/stub_collector.cpp", ] deps = [ diff --git a/uhdf2/hdi/src/devmgr_client.c b/uhdf2/hdi/src/devmgr_client.c index d505a114d3ff0316ef16008eeabbfcb145076fd2..319fefce17fb9d14e7aa6be57beb9ea124ad389c 100644 --- a/uhdf2/hdi/src/devmgr_client.c +++ b/uhdf2/hdi/src/devmgr_client.c @@ -94,23 +94,26 @@ static int32_t DevmgrQueryDeviceInfo(struct HDIDeviceManager *iDevMgr, struct De HdfSbufRecycle(reply); return HDF_ERR_MALLOC_FAIL; } + do { + if (!HdfRemoteServiceWriteInterfaceToken(iDevMgr->remote, data)) { + ret = HDF_FAILURE; + break; + } + list->deviceCnt = 0; + DListHeadInit(&list->list); - 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; - } + HdfSbufWriteInt32(data, type); + ret = DeviceManagerHdiCall(iDevMgr, DEVMGR_SERVICE_QUERY_DEVICE, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("DevmgrProxyQueryDevice failed"); + break; + } - ret = HdfObtainDeviceInfo(list, reply); + ret = HdfObtainDeviceInfo(list, reply); + } while (0); -FINISHED: HdfSbufRecycle(reply); HdfSbufRecycle(data); - return ret; } @@ -165,7 +168,10 @@ int32_t DevmgrLoadDevice(struct HDIDeviceManager *iDevMgr, const char *serviceNa status = HDF_ERR_MALLOC_FAIL; break; } - + if (!HdfRemoteServiceWriteInterfaceToken(iDevMgr->remote, data)) { + status = HDF_FAILURE; + break; + } if (!HdfSbufWriteString(data, serviceName)) { HDF_LOGE("%{public}s: writing service name failed!", __func__); break; @@ -194,7 +200,10 @@ int32_t DevmgrUnloadDevice(struct HDIDeviceManager *iDevMgr, const char *service status = HDF_ERR_MALLOC_FAIL; break; } - + if (!HdfRemoteServiceWriteInterfaceToken(iDevMgr->remote, data)) { + status = HDF_FAILURE; + break; + } if (!HdfSbufWriteString(data, serviceName)) { HDF_LOGE("%{public}s: writing service name failed!", __func__); break; @@ -237,7 +246,11 @@ struct HDIDeviceManager *HDIDeviceManagerGet(void) HdfRemoteServiceRecycle(remote); return NULL; } - + if (!HdfRemoteServiceSetInterfaceDesc(remote, "HDI.IDeviceManager.V1_0")) { + HDF_LOGE("%{public}s: failed to init interface desc", __func__); + HdfRemoteServiceRecycle(remote); + return NULL; + } iDevMgr->remote = remote; HDIDeviceManagerConstruct(iDevMgr); return iDevMgr; diff --git a/uhdf2/hdi/src/hdi_support.cpp b/uhdf2/hdi/src/hdi_support.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df99214ad7ea4da8c4125a7ec20ae0f88f99ffee --- /dev/null +++ b/uhdf2/hdi/src/hdi_support.cpp @@ -0,0 +1,195 @@ +/* + * Copyright (c) 2022 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 "hdi_support.h" +#include +#include +#include +#include +#include +#include +#include + +#include "hdf_base.h" +#include "hdf_log.h" + +#define HDF_LOG_TAG load_hdi + +#ifdef __ARM64__ +#define HDI_SO_PATH HDF_LIBRARY_DIR "64" +#else +#define HDI_SO_PATH HDF_LIBRARY_DIR +#endif + +namespace { +constexpr size_t INTERFACE_MATCH_RESIZE = 4; +constexpr size_t INTERFACE_VERSION_MAJOR_INDEX = 1; +constexpr size_t INTERFACE_VERSION_MINOR_INDEX = 2; +constexpr size_t INTERFACE_NAME_INDEX = 3; +static const std::regex reInfDesc("[a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*\\." + "[V|v]([0-9]+)_([0-9]+)\\." + "([a-zA-Z_][a-zA-Z0-9_]*)"); +using HdiImplInstanceFunc = void *(*)(void); +using HdiImplReleaseFunc = void (*)(void *); +} // namespace + +static std::string TransFileName(const std::string &interfaceName) +{ + if (interfaceName.empty()) { + return interfaceName; + } + + std::string result; + for (size_t i = 0; i < interfaceName.size(); i++) { + char c = interfaceName[i]; + if (std::isupper(c) != 0) { + if (i > 1) { + result += '_'; + } + result += std::tolower(c); + } else { + result += c; + } + } + return result; +} + +struct HdiImpl { + HdiImpl() : handler(nullptr), constructor(nullptr), destructor(nullptr), useCount(0) {} + ~HdiImpl() = default; + void Unload() + { + if (handler != nullptr) { + dlclose(handler); + } + } + void *handler; + void *(*constructor)(void); + void (*destructor)(void *); + uint32_t useCount; +}; + +static std::map g_hdiConstructorMap; +static std::mutex g_loaderMutex; + +static int32_t ParseInterface( + const std::string &desc, std::string &interface, std::string &libpath, const char *serviceName) +{ + std::smatch result; + if (!std::regex_match(desc, result, reInfDesc)) { + return HDF_FAILURE; + } + + if (result.size() < INTERFACE_MATCH_RESIZE) { + return HDF_FAILURE; + } + + uint32_t versionMajor = std::stoul(result[INTERFACE_VERSION_MAJOR_INDEX]); + uint32_t versionMinor = std::stoul(result[INTERFACE_VERSION_MINOR_INDEX]); + std::string interfaceName = result[INTERFACE_NAME_INDEX]; + + interface = interfaceName[0] == 'I' ? interfaceName.substr(1) : interfaceName; + if (interface.empty()) { + return HDF_FAILURE; + } + char path[PATH_MAX + 1] = {0}; + char resolvedPath[PATH_MAX + 1] = {0}; + if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s/lib%s_%s_%u.%u.z.so", HDI_SO_PATH, + TransFileName(interface).c_str(), serviceName, versionMajor, versionMinor) < 0) { + HDF_LOGE("%{public}s snprintf_s failed", __func__); + return HDF_FAILURE; + } + + if (realpath(path, resolvedPath) == nullptr || strncmp(resolvedPath, HDI_SO_PATH, strlen(HDI_SO_PATH)) != 0) { + HDF_LOGE("%{public}s invalid hdi impl so name %{public}s", __func__, path); + return HDF_FAILURE; + } + libpath = path; + return HDF_SUCCESS; +} + +/* + * service name: xxx_service + * interface descriptor name: ohos.hdi.sample.v1_0.IFoo, the last two are version and interface base name + * interface: Foo + * versionMajor: 1 + * versionMinor: 0 + * library name: libfoo_xxx_service_1.0.z.so + * method name: FooImplGetInstance + */ +void *LoadHdiImpl(const char *desc, const char *serviceName) +{ + if (desc == nullptr || serviceName == nullptr || strlen(desc) == 0 || strlen(serviceName) == 0) { + HDF_LOGE("%{public}s invalid interface descriptor or service name", __func__); + return nullptr; + } + + std::string interfaceName; + std::string libpath; + if (ParseInterface(desc, interfaceName, libpath, serviceName) != HDF_SUCCESS) { + HDF_LOGE("failed to parse hdi interface info from '%{public}s'", desc); + return nullptr; + } + + std::lock_guard lock(g_loaderMutex); + auto constructor = g_hdiConstructorMap.find(libpath); + if (constructor != g_hdiConstructorMap.end()) { + return constructor->second.constructor(); + } + + HdiImpl hdiImpl; + hdiImpl.handler = dlopen(libpath.c_str(), RTLD_LAZY); + if (hdiImpl.handler == nullptr) { + HDF_LOGE("%{public}s dlopen failed %{public}s", __func__, dlerror()); + return nullptr; + } + std::string symName = interfaceName + "ImplGetInstance"; + hdiImpl.constructor = (HdiImplInstanceFunc)dlsym(hdiImpl.handler, symName.data()); + if (hdiImpl.constructor == nullptr) { + HDF_LOGE("%{public}s dlsym failed %{public}s", __func__, dlerror()); + hdiImpl.Unload(); + return nullptr; + } + std::string desSymName = interfaceName + "ImplRelease"; + hdiImpl.destructor = (HdiImplReleaseFunc)dlsym(hdiImpl.handler, desSymName.data()); + + void *implInstance = hdiImpl.constructor(); + if (implInstance == nullptr) { + HDF_LOGE("%{public}s no full hdi implementation in %{public}s", __func__, libpath.c_str()); + hdiImpl.Unload(); + } else { + g_hdiConstructorMap.emplace(std::make_pair(libpath, std::move(hdiImpl))); + } + return implInstance; +} + +void UnloadHdiImpl(const char *desc, const char *serviceName, void *impl) +{ + if (desc == nullptr || impl == nullptr) { + return; + } + + std::string interfaceName; + std::string libpath; + if (ParseInterface(desc, interfaceName, libpath, serviceName) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: failed to parse hdi interface info from '%{public}s'", __func__, desc); + return; + } + std::lock_guard lock(g_loaderMutex); + auto constructor = g_hdiConstructorMap.find(libpath); + if (constructor != g_hdiConstructorMap.end() && constructor->second.destructor != nullptr) { + constructor->second.destructor(impl); + } +} diff --git a/uhdf2/hdi/src/idevmgr_client.cpp b/uhdf2/hdi/src/idevmgr_client.cpp index 6b7c21a05b93e153c72c0cef45660bb83a540a32..643d81fbbed43fdf9021085e32bb2ba8a920ce48 100644 --- a/uhdf2/hdi/src/idevmgr_client.cpp +++ b/uhdf2/hdi/src/idevmgr_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -13,9 +13,12 @@ * limitations under the License. */ -#include #include #include +#include +#include +#include + #include "idevmgr_hdi.h" #include "iservmgr_hdi.h" @@ -25,21 +28,24 @@ namespace OHOS { namespace HDI { namespace DeviceManager { namespace V1_0 { -enum DevngrCmdId : uint32_t { +enum DevmgrCmdId : uint32_t { DEVMGR_SERVICE_ATTACH_DEVICE_HOST = 1, DEVMGR_SERVICE_ATTACH_DEVICE, DEVMGR_SERVICE_DETACH_DEVICE, DEVMGR_SERVICE_LOAD_DEVICE, DEVMGR_SERVICE_UNLOAD_DEVICE, DEVMGR_SERVICE_QUERY_DEVICE, + DEVMGR_SERVICE_LIST_ALL_DEVICE, }; -class DeviceManagerProxy : public IRemoteProxy { +class DeviceManagerProxy : public IProxyBroker { public: - explicit DeviceManagerProxy(const sptr& impl) : IRemoteProxy(impl) {} + explicit DeviceManagerProxy(const sptr &impl) : IProxyBroker(impl) {} ~DeviceManagerProxy() {} - virtual int32_t LoadDevice(const std::string &serviceName); - virtual int32_t UnloadDevice(const std::string &serviceName); + int32_t LoadDevice(const std::string &serviceName) override; + int32_t UnloadDevice(const std::string &serviceName) override; + int32_t ListAllDevice(std::vector &deviceInfos) override; + private: static inline BrokerDelegator delegator_; }; @@ -50,6 +56,9 @@ int32_t DeviceManagerProxy::LoadDevice(const std::string &serviceName) MessageParcel reply; MessageOption option; HDF_LOGI("load device: %{public}s", serviceName.data()); + if (!data.WriteInterfaceToken(GetDescriptor())) { + return HDF_FAILURE; + } if (!data.WriteCString(serviceName.data())) { return HDF_FAILURE; } @@ -67,6 +76,9 @@ int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName) MessageParcel reply; MessageOption option; HDF_LOGI("unload device: %{public}s", serviceName.data()); + if (!data.WriteInterfaceToken(DeviceManagerProxy::GetDescriptor())) { + return HDF_FAILURE; + } if (!data.WriteCString(serviceName.data())) { return HDF_FAILURE; } @@ -78,6 +90,47 @@ int32_t DeviceManagerProxy::UnloadDevice(const std::string &serviceName) return status; } +static void HdfDevMgrDbgFillDeviceInfo(std::vector &deviceInfos, MessageParcel &reply) +{ + while (true) { + uint32_t devId; + uint32_t devCnt; + struct HdiDevHostInfo info; + const char *hostName = reply.ReadCString(); + if (hostName == nullptr) { + break; + } + info.hostName = hostName; + info.hostId = reply.ReadUint32(); + devCnt = reply.ReadUint32(); + for (uint32_t i = 0; i < devCnt; i++) { + devId = reply.ReadUint32(); + info.devId.push_back(devId); + } + deviceInfos.push_back(info); + } + return; +} + +int32_t DeviceManagerProxy::ListAllDevice(std::vector &deviceInfos) +{ + MessageParcel data; + MessageParcel reply; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + return HDF_FAILURE; + } + + MessageOption option; + int status = Remote()->SendRequest(DEVMGR_SERVICE_LIST_ALL_DEVICE, data, reply, option); + if (status != HDF_SUCCESS) { + HDF_LOGE("list all device info failed, %{public}d", status); + } else { + HdfDevMgrDbgFillDeviceInfo(deviceInfos, reply); + } + return status; +} + sptr IDeviceManager::Get() { auto servmgr = ServiceManager::V1_0::IServiceManager::Get(); @@ -87,7 +140,7 @@ sptr IDeviceManager::Get() } sptr remote = servmgr->GetService("hdf_device_manager"); if (remote != nullptr) { - return iface_cast(remote); + return hdi_facecast(remote); } HDF_LOGE("hdf device manager not exist"); diff --git a/uhdf2/hdi/src/iservmgr_client.cpp b/uhdf2/hdi/src/iservmgr_client.cpp index 4dde8c5e57d045cd41069a27e0e0da18bf4f6ac8..8068390b46518b40dd4a03d4b5e4bf9e873e0ac6 100644 --- a/uhdf2/hdi/src/iservmgr_client.cpp +++ b/uhdf2/hdi/src/iservmgr_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -13,10 +13,13 @@ * limitations under the License. */ -#include -#include #include #include +#include +#include +#include +#include + #include "iservmgr_hdi.h" namespace OHOS { @@ -25,18 +28,20 @@ namespace ServiceManager { namespace V1_0 { constexpr int DEVICE_SERVICE_MANAGER_SA_ID = 5100; constexpr int DEVSVC_MANAGER_GET_SERVICE = 3; -constexpr int DEVSVC_MANAGER_REGISER_SVCLISTENER = 4; -constexpr int DEVSVC_MANAGER_UNREGISER_SVCLISTENER = 5; +constexpr int DEVSVC_MANAGER_REGISTER_SVCLISTENER = 4; +constexpr int DEVSVC_MANAGER_UNREGISTER_SVCLISTENER = 5; +constexpr int DEVSVC_MANAGER_LIST_ALL_SERVICE = 6; -class ServiceManagerProxy : public IRemoteProxy { +class ServiceManagerProxy : public IProxyBroker { public: - explicit ServiceManagerProxy(const sptr& impl) : IRemoteProxy(impl) {} + explicit ServiceManagerProxy(const sptr &impl) : IProxyBroker(impl) {} ~ServiceManagerProxy() {} - virtual sptr GetService(const char* serviceName) override; - virtual int32_t RegisterServiceStatusListener(::OHOS::sptr listener, - uint16_t deviceClass) override; - virtual int32_t UnregisterServiceStatusListener(::OHOS::sptr listener) override; + sptr GetService(const char *serviceName) override; + int32_t ListAllService(std::vector &serviceInfos) override; + int32_t RegisterServiceStatusListener(sptr listener, uint16_t deviceClass) override; + int32_t UnregisterServiceStatusListener(sptr listener) override; + private: static inline BrokerDelegator delegator_; }; @@ -50,26 +55,26 @@ sptr IServiceManager::Get() } sptr remote = saManager->GetSystemAbility(DEVICE_SERVICE_MANAGER_SA_ID); if (remote != nullptr) { - return iface_cast(remote); + return new ServiceManagerProxy(remote); } HDF_LOGE("failed to get sa hdf service manager"); return nullptr; } -int32_t ServiceManagerProxy::RegisterServiceStatusListener(::OHOS::sptr listener, - uint16_t deviceClass) +int32_t ServiceManagerProxy::RegisterServiceStatusListener( + ::OHOS::sptr listener, uint16_t deviceClass) { MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteUint16(deviceClass) || + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteUint16(deviceClass) || !data.WriteRemoteObject(listener->AsObject())) { return HDF_FAILURE; } - int status = Remote()->SendRequest(DEVSVC_MANAGER_REGISER_SVCLISTENER, data, reply, option); + int status = Remote()->SendRequest(DEVSVC_MANAGER_REGISTER_SVCLISTENER, data, reply, option); if (status) { HDF_LOGE("failed to register servstat listener, %{public}d", status); } @@ -82,22 +87,22 @@ int32_t ServiceManagerProxy::UnregisterServiceStatusListener(::OHOS::sptrAsObject())) { + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteRemoteObject(listener->AsObject())) { return HDF_FAILURE; } - int status = Remote()->SendRequest(DEVSVC_MANAGER_UNREGISER_SVCLISTENER, data, reply, option); + int status = Remote()->SendRequest(DEVSVC_MANAGER_UNREGISTER_SVCLISTENER, data, reply, option); if (status) { HDF_LOGE("failed to unregister servstat listener, %{public}d", status); } return status; } -sptr ServiceManagerProxy::GetService(const char* serviceName) +sptr ServiceManagerProxy::GetService(const char *serviceName) { MessageParcel data; MessageParcel reply; - if (!data.WriteCString(serviceName)) { + if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteCString(serviceName)) { return nullptr; } @@ -110,6 +115,41 @@ sptr ServiceManagerProxy::GetService(const char* serviceName) HDF_LOGD("get hdi service %{public}s success ", serviceName); return reply.ReadRemoteObject(); } + +static void HdfDevMgrDbgFillServiceInfo(std::vector &serviceInfos, MessageParcel &reply) +{ + while (true) { + HdiServiceInfo info; + const char *servName = reply.ReadCString(); + if (servName == nullptr) { + break; + } + info.serviceName = servName; + info.devClass = reply.ReadUint16(); + serviceInfos.push_back(info); + } + return; +} + +int32_t ServiceManagerProxy::ListAllService(std::vector &serviceInfos) +{ + MessageParcel data; + MessageParcel reply; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return HDF_FAILURE; + } + + MessageOption option; + int status = Remote()->SendRequest(DEVSVC_MANAGER_LIST_ALL_SERVICE, data, reply, option); + if (status != HDF_SUCCESS) { + HDF_LOGE("list all service info failed, %{public}d", status); + return status; + } else { + HdfDevMgrDbgFillServiceInfo(serviceInfos, reply); + } + HDF_LOGD("get all service info success"); + return status; +} } // namespace V1_0 } // namespace ServiceManager } // namespace HDI diff --git a/uhdf2/hdi/src/object_collector.cpp b/uhdf2/hdi/src/object_collector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e1c69acfeef2c05f3eca95346b6293999836c85 --- /dev/null +++ b/uhdf2/hdi/src/object_collector.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2022 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 "object_collector.h" +#include + +#include "hdf_base.h" +#include "hdf_log.h" +#include "osal_time.h" + +using namespace OHOS::HDI; + +ObjectCollector &ObjectCollector::GetInstance() +{ + static ObjectCollector mapper; + return mapper; +} + +bool ObjectCollector::ConstructorRegister(const std::u16string &interfaceName, const Constructor &constructor) +{ + if (interfaceName.empty()) { + return false; + } + std::lock_guard lock(mutex_); + constructorMapper_.insert({interfaceName, std::move(constructor)}); + return true; +} + +void ObjectCollector::ConstructorUnRegister(const std::u16string &interfaceName) +{ + std::lock_guard lock(mutex_); + constructorMapper_.erase(interfaceName); +} + +OHOS::sptr ObjectCollector::NewObjectLocked( + const OHOS::sptr &interface, const std::u16string &interfaceName) +{ + if (interface == nullptr) { + return nullptr; + } + auto constructor = constructorMapper_.find(interfaceName); + if (constructor != constructorMapper_.end()) { + return constructor->second(interface); + } + + return nullptr; +} + +OHOS::sptr ObjectCollector::NewObject( + const OHOS::sptr &interface, const std::u16string &interfaceName) +{ + std::lock_guard lock(mutex_); + return NewObjectLocked(interface, interfaceName); +} + +OHOS::sptr ObjectCollector::GetOrNewObject( + const OHOS::sptr &interface, const std::u16string &interfaceName) +{ + mutex_.lock(); +RETRY: + auto it = interfaceObjectCollector_.find(interface.GetRefPtr()); + if (it != interfaceObjectCollector_.end()) { + if (it->second->GetSptrRefCount() == 0) { + // may object is releasing, yield to sync + mutex_.unlock(); + OsalMSleep(1); + mutex_.lock(); + goto RETRY; + } + mutex_.unlock(); + return it->second; + } + sptr object = NewObjectLocked(interface, interfaceName); + interfaceObjectCollector_[interface.GetRefPtr()] = object.GetRefPtr(); + mutex_.unlock(); + return object; +} + +bool ObjectCollector::RemoveObject(const OHOS::sptr &interface) +{ + std::lock_guard lock(mutex_); + auto it = interfaceObjectCollector_.find(interface.GetRefPtr()); + if (it == interfaceObjectCollector_.end()) { + return false; + } + interfaceObjectCollector_.erase(it); + return true; +} \ No newline at end of file diff --git a/uhdf2/hdi/src/servmgr_client.c b/uhdf2/hdi/src/servmgr_client.c index 457185b1e8094466d5fa6310818fdf0f274b94b3..2a5c4e2dcd3041c3a0b1585e29d119781e821e2e 100644 --- a/uhdf2/hdi/src/servmgr_client.c +++ b/uhdf2/hdi/src/servmgr_client.c @@ -55,7 +55,8 @@ struct HdfRemoteService *HDIServMgrGetService(struct HDIServiceManager *iServMgr break; } - if (!HdfSbufWriteString(data, serviceName)) { + if (!HdfRemoteServiceWriteInterfaceToken(servMgrClient->remote, data) || + !HdfSbufWriteString(data, serviceName)) { break; } int status = ServiceManagerHdiCall(servMgrClient, DEVSVC_MANAGER_GET_SERVICE, data, reply); @@ -72,6 +73,7 @@ struct HdfRemoteService *HDIServMgrGetService(struct HDIServiceManager *iServMgr if (data != NULL) { HdfSbufRecycle(data); } + return service; } @@ -88,12 +90,13 @@ int32_t HDIServMgrRegisterServiceStatusListener(struct HDIServiceManager *self, return HDF_ERR_MALLOC_FAIL; } - if (!HdfSbufWriteUint16(data, deviceClass) || + if (!HdfRemoteServiceWriteInterfaceToken(servMgrClient->remote, data) || + !HdfSbufWriteUint16(data, deviceClass) || ServiceStatusListenerMarshalling(listener, data) != HDF_SUCCESS) { return HDF_FAILURE; } - int32_t ret = ServiceManagerHdiCall(servMgrClient, DEVSVC_MANAGER_REGISER_SVCLISTENER, data, NULL); + int32_t ret = ServiceManagerHdiCall(servMgrClient, DEVSVC_MANAGER_REGISTER_SVCLISTENER, data, NULL); if (ret != HDF_SUCCESS) { HDF_LOGE("failed to register hdi service listener"); } @@ -113,11 +116,12 @@ int32_t HDIServMgrUnregisterServiceStatusListener(struct HDIServiceManager *self return HDF_ERR_MALLOC_FAIL; } - if (ServiceStatusListenerMarshalling(listener, data) != HDF_SUCCESS) { + if (!HdfRemoteServiceWriteInterfaceToken(servMgrClient->remote, data) || + ServiceStatusListenerMarshalling(listener, data) != HDF_SUCCESS) { return HDF_FAILURE; } - int32_t ret = ServiceManagerHdiCall(servMgrClient, DEVSVC_MANAGER_UNREGISER_SVCLISTENER, data, NULL); + int32_t ret = ServiceManagerHdiCall(servMgrClient, DEVSVC_MANAGER_UNREGISTER_SVCLISTENER, data, NULL); if (ret != HDF_SUCCESS) { HDF_LOGE("failed to unregister hdi service listener"); } @@ -146,8 +150,13 @@ struct HDIServiceManager *HDIServiceManagerGet(void) HdfRemoteServiceRecycle(remote); return NULL; } - + if (!HdfRemoteServiceSetInterfaceDesc(remote, "HDI.IServiceManager.V1_0")) { + HDF_LOGE("%{public}s: failed to init interface desc", __func__); + HdfRemoteServiceRecycle(remote); + return NULL; + } iServMgrClient->remote = remote; + HDIServiceManagerConstruct(&iServMgrClient->iservmgr); return &iServMgrClient->iservmgr; } diff --git a/uhdf2/hdi/src/servstat_listener.c b/uhdf2/hdi/src/servstat_listener.c index 3fade05ee705291ad6ccd24b5e8794055a50cfcc..116e64fd4dd45149de33f48f513a9949a06cffe2 100644 --- a/uhdf2/hdi/src/servstat_listener.c +++ b/uhdf2/hdi/src/servstat_listener.c @@ -18,7 +18,7 @@ #include #include #include - +#define SERVSTAT_LISTENER_INTERFACE_DESCRIPTOR "HDI.IServiceStatusListener.V1_0" struct ServstatListenerStub { struct ServiceStatusListener listener; struct HdfRemoteService *remote; @@ -34,7 +34,10 @@ int ServstatListenerStubRemoteDispatch( if (cmdid != SERVIE_STATUS_LISTENER_NOTIFY) { return HDF_ERR_NOT_SUPPORT; } - + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("failed to check interface"); + return HDF_ERR_INVALID_PARAM; + } if (ServiceStatusUnMarshalling(&status, data) != HDF_SUCCESS) { return HDF_ERR_INVALID_PARAM; } @@ -75,6 +78,12 @@ struct ServiceStatusListener *HdiServiceStatusListenerNewInstance(void) OsalMemFree(stub); return NULL; } + if (!HdfRemoteServiceSetInterfaceDesc(stub->remote, SERVSTAT_LISTENER_INTERFACE_DESCRIPTOR)) { + HdfRemoteServiceRecycle(stub->remote); + stub->remote = NULL; + OsalMemFree(stub); + return NULL; + } return &stub->listener; } diff --git a/uhdf2/hdi/src/servstat_listener_stub.cpp b/uhdf2/hdi/src/servstat_listener_stub.cpp index 65096b62770f53a4f4c6304c59570417e17c7a48..0974e9602347734674c3e2b679f11c2a38fb7feb 100644 --- a/uhdf2/hdi/src/servstat_listener_stub.cpp +++ b/uhdf2/hdi/src/servstat_listener_stub.cpp @@ -17,7 +17,7 @@ #include "hdf_base.h" #include "hdf_log.h" #include "iservmgr_hdi.h" - +#define HDF_LOG_TAG servstat_listener namespace OHOS { namespace HDI { namespace ServiceManager { @@ -39,19 +39,26 @@ int32_t ServStatListenerStub::ServStatListenerStubOnReceive(MessageParcel& data, MessageParcel& reply, MessageOption& option) { ServiceStatus status; + if (data.ReadInterfaceToken() != GetDescriptor()) { + HDF_LOGI("failed to check interface token"); + return HDF_FAILURE; + } - status.serviceName = data.ReadCString(); + const char* name = data.ReadCString(); + status.serviceName = (name == nullptr) ? "" : name; if (status.serviceName.empty()) { HDF_LOGI("failed to read serviceName in ServiceStatus"); return HDF_FAILURE; } + if (!data.ReadUint16(status.deviceClass) || !data.ReadUint16(status.status)) { HDF_LOGI("failed to read deviceClass or status in ServiceStatus"); return HDF_FAILURE; } - status.info = data.ReadCString(); + const char* info = data.ReadCString(); + status.info = (info == nullptr) ? "" : info; OnReceive(status); return HDF_SUCCESS; diff --git a/uhdf2/hdi/src/stub_collector.cpp b/uhdf2/hdi/src/stub_collector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5219a8b46a90579472f64dd57601f459d271c80 --- /dev/null +++ b/uhdf2/hdi/src/stub_collector.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2022 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 "stub_collector.h" +#include +#include +#include + +static std::map g_constructorMap; +static std::map g_stubMap; +static std::mutex g_consMapLock; +static std::mutex g_stubMapLock; + +void StubConstructorRegister(const char *ifDesc, struct StubConstructor *constructor) +{ + if (ifDesc == nullptr || constructor == nullptr) { + return; + } + + const std::lock_guard lock(g_consMapLock); + if (g_constructorMap.find(ifDesc) != g_constructorMap.end()) { + HDF_LOGE("repeat registration stub constructor for if %{public}s", ifDesc); + return; + } + g_constructorMap.emplace(std::make_pair(ifDesc, constructor)); + return; +} + +void StubConstructorUnregister(const char *ifDesc, struct StubConstructor *constructor) +{ + if (ifDesc == nullptr || constructor == nullptr) { + return; + } + + const std::lock_guard lock(g_consMapLock); + g_constructorMap.erase(ifDesc); + return; +} + +struct HdfRemoteService **StubCollectorGetOrNewObject(const char *ifDesc, void *servPtr) +{ + if (ifDesc == nullptr || servPtr == nullptr) { + return nullptr; + } + const std::lock_guard stublock(g_stubMapLock); + auto stub = g_stubMap.find(servPtr); + if (stub != g_stubMap.end()) { + return stub->second; + } + + HDF_LOGI("g_constructorMap size %{public}zu", g_constructorMap.size()); + for (auto &consruct : g_constructorMap) { + HDF_LOGI("g_constructorMap it: %{public}s", consruct.first.c_str()); + } + + const std::lock_guard lock(g_consMapLock); + auto constructor = g_constructorMap.find(ifDesc); + if (constructor == g_constructorMap.end()) { + HDF_LOGE("no stub constructor for %{public}s", ifDesc); + return nullptr; + } + + if (constructor->second->constructor == nullptr) { + HDF_LOGE("no stub constructor method for %{public}s", ifDesc); + return nullptr; + } + + HdfRemoteService **stubObject = constructor->second->constructor(servPtr); + if (stubObject == nullptr) { + HDF_LOGE("failed to construct stub obj %{public}s", ifDesc); + return nullptr; + } + g_stubMap.insert(std::make_pair(servPtr, stubObject)); + return stubObject; +} + +void StubCollectorRemoveObject(const char *ifDesc, void *servPtr) +{ + if (ifDesc == nullptr || servPtr == nullptr) { + return; + } + + const std::lock_guard stublock(g_stubMapLock); + auto stub = g_stubMap.find(servPtr); + if (stub == g_stubMap.end()) { + return; + } + const std::lock_guard lock(g_consMapLock); + auto constructor = g_constructorMap.find(ifDesc); + if (constructor == g_constructorMap.end()) { + HDF_LOGE("no stub constructor for %{public}s", ifDesc); + return; + } + + if (constructor->second->destructor != nullptr) { + constructor->second->destructor(stub->second); + } + + g_stubMap.erase(servPtr); +} diff --git a/uhdf2/hdi/test/BUILD.gn b/uhdf2/hdi/test/BUILD.gn index 3a2f9db11dcf0a414979e5071e77d381cddf0497..f03ebace34dfa438a65997149702f0e6b5139ee7 100644 --- a/uhdf2/hdi/test/BUILD.gn +++ b/uhdf2/hdi/test/BUILD.gn @@ -14,7 +14,7 @@ import("//build/test.gni") import("//drivers/adapter/uhdf2/uhdf.gni") -module_output_path = "hdf/hdi" +module_output_path = "hdf/srvmgr" ###########################palTest########################### diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_client_c/BUILD.gn similarity index 50% rename from uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn rename to uhdf2/hdi/test/hdi_sample/sample_client_c/BUILD.gn index ea2da6b15ffb62c48948bab88e638ae79e619708..6b763403e5aae54b9554094a367e9621b23d5479 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_c/BUILD.gn +++ b/uhdf2/hdi/test/hdi_sample/sample_client_c/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -11,35 +11,28 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//build/ohos.gni") +import("//build/test.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.c", - "sample_service_stub.c", - ] +module_output_path = "hdf/hdi" + +ohos_unittest("HdiSampleTestC") { + module_out_path = module_output_path + sources = [ "sample_client_c_test.cpp" ] deps = [ - "//drivers/adapter/uhdf2/host:libhdf_host", + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/utils:libhdf_utils", + "unittest:libsample_client_c", "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", - "//drivers/adapter/uhdf2/utils:libhdf_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", ] - if (is_standard_system) { - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "utils_base:utils", - ] - } else { - external_deps = [ "hilog:libhilog" ] - } - - install_images = [ chipset_base_dir ] - subsystem_name = "hdf" - part_name = "device_driver_framework" + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] } 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 index 481b28f8fc29cda8b3e44fc854e32135cce8b8b7..95b3e04f535f7247ce5d83c3a66ac4505da004f7 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,323 +13,205 @@ * limitations under the License. */ -#include -#include -#include +#include #include #include +#include #include -#include "securec.h" +#include +#include +#include +#include + +#include "ifoo.h" #include "isample.h" +#include "isample_callback.h" using namespace OHOS; using namespace testing::ext; -#define HDF_LOG_TAG sample_client_c_test +#define HDF_LOG_TAG sample_client_c_test -constexpr const char *TEST_SERVICE_NAME = "sample_service_c"; +constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; -class SampleObjCTest : public testing::Test { +class SampleHdiCTest : public testing::Test { public: - static void SetUpTestCase(){} - static void TearDownTestCase(){} - void SetUp(){} - void TearDown(){} + static void SetUpTestCase() + { + auto devmgr = HDIDeviceManagerGet(); + if (devmgr != nullptr) { + devmgr->LoadDevice(devmgr, TEST_SERVICE_NAME); + HDIDeviceManagerRelease(devmgr); + } + } + static void TearDownTestCase() + { + auto devmgr = HDIDeviceManagerGet(); + if (devmgr != nullptr) { + devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME); + HDIDeviceManagerRelease(devmgr); + } + } + void SetUp() {} + void TearDown() {} }; -HWTEST_F(SampleObjCTest, SampleObjCTest_001, TestSize.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) +// IPC mode get interface object +HWTEST_F(SampleHdiCTest, HdiCTest001, TestSize.Level1) { - struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); + struct ISample *sampleService = ISampleGetInstance(TEST_SERVICE_NAME, false); + ASSERT_TRUE(sampleService != nullptr); - int32_t input = 10; - int32_t output; + struct IFoo *fooInterface = nullptr; + int ret = sampleService->GetInterface(sampleService, &fooInterface); + ASSERT_EQ(ret, 0); + ASSERT_NE(fooInterface, nullptr); - int32_t ec = sampleObj->IntTypeTest(sampleObj, input, &output); + ret = fooInterface->FooEvent(fooInterface, 1); + ASSERT_EQ(ret, 0); - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_EQ(input, output); + bool value = false; + ret = sampleService->PingTest(sampleService, true, &value); + ASSERT_EQ(ret, 0); + ASSERT_EQ(value, true); - HdiSampleRelease(sampleObj); + IFooRelease(true, fooInterface); + ISampleRelease(false, sampleService); } -HWTEST_F(SampleObjCTest, SampleObjCTest_005, TestSize.Level1) +// passthrough mode get interface object +HWTEST_F(SampleHdiCTest, HdiCTest002, TestSize.Level1) { - struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); + struct ISample *sampleService = ISampleGet(true); + ASSERT_TRUE(sampleService != nullptr); - int64_t input = 10; - int64_t output; + struct IFoo *fooInterface = nullptr; + int ret = sampleService->GetInterface(sampleService, &fooInterface); + ASSERT_EQ(ret, 0); + ASSERT_NE(fooInterface, nullptr); - int32_t ec = sampleObj->LongTypeTest(sampleObj, input, &output); + ret = fooInterface->FooEvent(fooInterface, 1); + ASSERT_EQ(ret, 0); - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_EQ(input, output); + bool value = false; + ret = sampleService->PingTest(sampleService, true, &value); + ASSERT_EQ(ret, 0); + ASSERT_EQ(value, true); - HdiSampleRelease(sampleObj); + IFooRelease(true, fooInterface); + ISampleRelease(true, sampleService); } -HWTEST_F(SampleObjCTest, SampleObjCTest_006, TestSize.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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); -} +struct SampleCallbackImpl { + struct ISampleCallback interface; + bool called; +}; -HWTEST_F(SampleObjCTest, SampleObjCTest_012, TestSize.Level1) +static int32_t SampleCallbackPingTest(struct ISampleCallback *self, int event) { - 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); + if (self == nullptr) { + return HDF_ERR_INVALID_PARAM; + } + struct SampleCallbackImpl *callback = CONTAINER_OF(self, struct SampleCallbackImpl, interface); + callback->called = true; + HDF_LOGI("%{public}s called, event = %{public}d", __func__, event); + return HDF_SUCCESS; } -HWTEST_F(SampleObjCTest, SampleObjCTest_013, TestSize.Level1) +// ipc mode set callback +HWTEST_F(SampleHdiCTest, HdiCTest003, TestSize.Level1) { - 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; + struct ISample *sampleService = ISampleGetInstance(TEST_SERVICE_NAME, false); + ASSERT_TRUE(sampleService != nullptr); - int32_t ec = sampleObj->ListTypeTest(sampleObj, input, inSize, &output, &outSize); + bool value = false; + int ret = sampleService->PingTest(sampleService, true, &value); + ASSERT_EQ(ret, 0); + ASSERT_EQ(value, true); - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_TRUE(output != nullptr); - ASSERT_EQ(outSize, inSize); + struct SampleCallbackImpl callback; + callback.interface.PingTest = SampleCallbackPingTest; + ret = sampleService->SetCallback(sampleService, &callback.interface); - for (uint32_t i = 0; i < outSize; i++) { - ASSERT_EQ(output[i], input[i]); + uint32_t retry = 0; + constexpr int WAIT_COUNT = 200; + while (!callback.called && retry < WAIT_COUNT) { + retry++; + OsalMSleep(1); } - if (input != nullptr) { - OsalMemFree(input); - } - if (output != nullptr) { - OsalMemFree(output); - } + ASSERT_TRUE(callback.called); + ISampleRelease(false, sampleService); } -HWTEST_F(SampleObjCTest, SampleObjCTest_014, TestSize.Level1) +// passthrougt mode set callback +HWTEST_F(SampleHdiCTest, HdiCTest004, TestSize.Level1) { - struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); + struct ISample *sampleService = ISampleGet(true); + ASSERT_TRUE(sampleService != nullptr); - uint32_t inSize = 5; - int8_t *input = (int8_t *)OsalMemAlloc(sizeof(int8_t) * inSize); - ASSERT_TRUE(input != nullptr); + bool value = false; + int ret = sampleService->PingTest(sampleService, true, &value); + ASSERT_EQ(ret, 0); + ASSERT_EQ(value, true); + struct SampleCallbackImpl callback; + callback.interface.PingTest = SampleCallbackPingTest; + ret = sampleService->SetCallback(sampleService, &callback.interface); + uint32_t retry = 0; - for (uint32_t i = 0; i < inSize; i++) { - input[i] = static_cast(i); + constexpr int WAIT_COUNT = 20; + while (!callback.called && retry < WAIT_COUNT) { + retry++; + OsalMSleep(1); } - 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); + ASSERT_TRUE(callback.called); + ISampleRelease(true, sampleService); +} - for (uint32_t i = 0; i < outSize; i++) { - ASSERT_EQ(output[i], input[i]); - } +struct SampleDeathRecipient { + struct HdfDeathRecipient recipient; + bool called; +}; - if (input != nullptr) { - OsalMemFree(input); - } - if (output != nullptr) { - OsalMemFree(output); +static void SampleOnRemoteDied(struct HdfDeathRecipient *deathRecipient, struct HdfRemoteService *remote) +{ + HDF_LOGI("sample service dead"); + if (deathRecipient == nullptr || remote == nullptr) { + return; } + struct SampleDeathRecipient *sampleRecipient = CONTAINER_OF(deathRecipient, struct SampleDeathRecipient, recipient); + sampleRecipient->called = true; } -HWTEST_F(SampleObjCTest, SampleObjCTest_015, TestSize.Level1) +// IPC mode add DeathRecipient +HWTEST_F(SampleHdiCTest, HdiCTest005, TestSize.Level1) { - struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); + struct ISample *sampleService = ISampleGetInstance(TEST_SERVICE_NAME, false); + ASSERT_TRUE(sampleService != nullptr); - struct StructSample input = { - .first = 1, - .second = 2, + struct SampleDeathRecipient deathRecipient = { + .recipient = { + .OnRemoteDied = SampleOnRemoteDied, + }, + .called = false, }; - 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.Level1) -{ - struct ISample *sampleObj = HdiSampleGet(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); - - enum EnumSample input = MEM_SECOND; - enum EnumSample output; + ASSERT_NE(sampleService->AsObject, nullptr); + struct HdfRemoteService *remote = sampleService->AsObject(sampleService); + ASSERT_NE(remote, nullptr); - int32_t ec = sampleObj->EnumTypeTest(sampleObj, input, &output); + HdfRemoteAdapterAddDeathRecipient(remote, &deathRecipient.recipient); + TearDownTestCase(); - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_EQ(input, output); - - HdiSampleRelease(sampleObj); -} + int retry = 0; + constexpr int WAIT_COUNT = 200; + while (!deathRecipient.called && retry < WAIT_COUNT) { + retry++; + OsalMSleep(1); + } + ASSERT_TRUE(deathRecipient.called); + HdfRemoteAdapterRemoveDeathRecipient(remote, &deathRecipient.recipient); + ISampleRelease(true, sampleService); +} \ 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 deleted file mode 100644 index 6aef4c06cb737046d196267316d81e2fce208378..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/isample.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * 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 deleted file mode 100644 index 68384d5b05e6944c67b545ab980e0246c8b3caf8..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/sample_proxy.c +++ /dev/null @@ -1,844 +0,0 @@ -/* - * 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[%{public}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[%{public}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/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ba35a9756c9de6ecca594262bbf7f0671aa8cee8 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/BUILD.gn @@ -0,0 +1,37 @@ +# Copyright (c) 2022 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" + +ohos_unittest("HdiSampleTestCC") { + module_out_path = module_output_path + sources = [ "sample_client_cpp_test.cpp" ] + + deps = [ + "$hdf_uhdf_path/hdi:libhdi", + "$hdf_uhdf_path/utils:libhdf_utils", + "unittest:libsample_client_cpp", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "samgr_standard:samgr_proxy", + "utils_base:utils", + ] +} 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 index 030eb93feae299274cd2b696ee62f125a517b765..7918135c29ac3ab4ac3128484617f50026303af5 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -12,289 +12,96 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include +#include +#include +#include +#include #include +#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; +using OHOS::IRemoteObject; +using OHOS::sptr; +using OHOS::wptr; +using OHOS::HDI::DeviceManager::V1_0::IDeviceManager; #define HDF_LOG_TAG sample_client_cpp_test -constexpr const char *TEST_SERVICE_NAME = "sample_service_cpp"; +constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; -class SampleObjCPPTest : public testing::Test { +class SampleHdiCppTest : public testing::Test { public: - static void SetUpTestCase(){} - static void TearDownTestCase(){} - void SetUp(){} - void TearDown(){} + static void SetUpTestCase() + { + auto devmgr = IDeviceManager::Get(); + if (devmgr != nullptr) { + devmgr->LoadDevice(TEST_SERVICE_NAME); + } + } + static void TearDownTestCase() + { + auto devmgr = IDeviceManager::Get(); + if (devmgr != nullptr) { + devmgr->UnloadDevice(TEST_SERVICE_NAME); + } + } + void SetUp() {} + void TearDown() {} }; -HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_001, TestSize.Level1) +// IPC mode get interface object +HWTEST_F(SampleHdiCppTest, HdiCppTest001, TestSize.Level1) { - sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); - - bool input = true; - bool output = false; - - int32_t ec = sampleObj->BooleanTypeTest(input, output); + OHOS::sptr sampleService = ISample::Get(TEST_SERVICE_NAME, false); + ASSERT_TRUE(sampleService != nullptr); + OHOS::sptr fooInterface = nullptr; + int ret = sampleService->GetInterface(fooInterface); + ASSERT_EQ(ret, 0); - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_EQ(output, input); + bool value = false; + ret = fooInterface->PingTest(true, value); + ASSERT_EQ(ret, 0); + ASSERT_EQ(value, true); } -HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_002, TestSize.Level1) +// passthrough mode get interface object +HWTEST_F(SampleHdiCppTest, HdiCppTest002, TestSize.Level1) { - sptr sampleObj = ISample::Get(TEST_SERVICE_NAME); - ASSERT_TRUE(sampleObj != nullptr); + OHOS::sptr sampleService = ISample::Get(true); + ASSERT_TRUE(sampleService != nullptr); + OHOS::sptr fooInterface = nullptr; + int ret = sampleService->GetInterface(fooInterface); + ASSERT_EQ(ret, 0); - int8_t input = 5; - int8_t output; - - int32_t ec = sampleObj->ByteTypeTest(input, output); - - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_EQ(input, output); + bool value = false; + ret = fooInterface->PingTest(true, value); + ASSERT_EQ(ret, 0); + ASSERT_EQ(value, true); } -HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_003, TestSize.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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.Level1) -{ - 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]); +class SampleDeathRecipient : public IRemoteObject::DeathRecipient { +public: + void OnRemoteDied(const wptr &object) override + { + HDF_LOGI("sample service dead"); } -} - -HWTEST_F(SampleObjCPPTest, SampleObjCPPTest_016, TestSize.Level1) -{ - 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.Level1) +// IPC mode add DeathRecipient +HWTEST_F(SampleHdiCppTest, HdiCppTest003, TestSize.Level1) { - 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); + sptr sampleService = ISample::Get(TEST_SERVICE_NAME, false); + ASSERT_TRUE(sampleService != nullptr); - ASSERT_EQ(ec, HDF_SUCCESS); - ASSERT_EQ(output, input); + const sptr recipient = new SampleDeathRecipient(); + sptr remote = OHOS::HDI::hdi_objcast(sampleService); + bool ret = remote->AddDeathRecipient(recipient); + ASSERT_EQ(ret, true); } 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 index bcd6a51f1644c2753911b272cda7136c635ca316..9d5b49b82d186d3384fea490b996aed0e2535dad 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -16,14 +16,25 @@ import("//drivers/adapter/uhdf2/uhdf.gni") group("sample_client_cpp") { deps = [ ":libsample_client_cpp" ] } +config("libsample_client_cpp_config") { + visibility = [ ":*" ] + include_dirs = [ + "./", + "$hdf_uhdf_path/hdi/test/hdi_sample/sample_service_cpp", + ] +} ohos_shared_library("libsample_client_cpp") { include_dirs = [ "$hdf_uhdf_path/manager/include", "$hdf_uhdf_path/include/hdi", + "$hdf_uhdf_path/hdi/test/hdi_sample", + ] + public_configs = [ ":libsample_client_cpp_config" ] + sources = [ + "foo_proxy.cpp", + "sample_proxy.cpp", ] - - sources = [ "sample_proxy.cpp" ] deps = [ "$hdf_uhdf_path/hdi:libhdi", @@ -32,18 +43,11 @@ ohos_shared_library("libsample_client_cpp") { "//drivers/adapter/uhdf2/utils:libhdf_utils", ] - if (is_standard_system) { - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_single", - "utils_base:utils", - ] - } else { - external_deps = [ - "hilog:libhilog", - "ipc:ipc_single", - ] - } + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "utils_base:utils", + ] install_images = [ chipset_base_dir ] subsystem_name = "hdf" diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/foo_proxy.cpp similarity index 32% rename from uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c rename to uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/foo_proxy.cpp index 3b64cf12e26d1818611c5e4ea9f2f4151d52d68f..dfad28f1cfcfd9655f2b8f8ec464f534259cce6f 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_driver.c +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/foo_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -15,55 +15,40 @@ #include #include -#include -#include +#include +#include -#include "isample.h" +#include "ifoo.h" -#define HDF_LOG_TAG sample_service_c +#include "foo_proxy.h" -static int32_t SampleServiceDispatch(struct HdfDeviceIoClient *client, int cmdId, - struct HdfSBuf *data, struct HdfSBuf *reply) +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +int32_t FooProxy::PingTest(const bool input, bool &output) { - 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!"); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(IFoo::GetDescriptor())) { + return HDF_FAILURE; + } + if (!data.WriteBool(input)) { + HDF_LOGE("%{public}s: write bool input failed!", __func__); return HDF_FAILURE; } - ioService->Dispatch = SampleServiceDispatch; - ioService->Open = NULL; - ioService->Release = NULL; - - deviceObject->service = ioService; - return HDF_SUCCESS; -} + int32_t ret = Remote()->SendRequest(CMD_FOO_PING, data, reply, option); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret); + return ret; + } + output = reply.ReadBool(); -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 +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/foo_proxy.h similarity index 38% rename from uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c rename to uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/foo_proxy.h index f7920dd453f9fd1c8817a7b9373780daf8b7ab5b..8b892c96ccda8d2725b7e6ce3661a558e2bb3160 100644 --- a/uhdf2/hdi/test/reg_virtual_device/unittest/sample_driver/hdf_virtual_driver.c +++ b/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/foo_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,42 +13,31 @@ * limitations under the License. */ -#include "hdf_log.h" -#include "hdf_base.h" -#include "hdf_device_desc.h" +#ifndef HDI_SAMPLE_CPP_CLIENT_H +#define HDI_SAMPLE_CPP_CLIENT_H -#define HDF_LOG_TAG hdf_virtual_driver +#include +#include -int32_t HdfVirtualDriverBind(struct HdfDeviceObject *para) -{ - HDF_LOGE("%{public}s", __func__); - static struct IDeviceIoService virtualService = { - .object.objectId = 1, - .Dispatch = NULL, - }; - para->service = &virtualService; - return 0; -} +#include "ifoo.h" -int32_t HdfVirtualDriverInit(struct HdfDeviceObject *para) -{ - (void)para; - HDF_LOGE("%{public}s", __func__); - return 0; -} +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +class FooProxy : public IProxyBroker { +public: + explicit FooProxy(const sptr &impl) : IProxyBroker(impl) {} + virtual ~FooProxy() = default; -void HdfVirtualDriverRelease(struct HdfDeviceObject *para) -{ - (void)para; + int32_t PingTest(const bool input, bool &output) override; - HDF_LOGE("%{public}s", __func__); -} - -struct HdfDriverEntry g_sampleDriverEntry = { - .moduleVersion = 1, - .Bind = HdfVirtualDriverBind, - .Init = HdfVirtualDriverInit, - .Release = HdfVirtualDriverRelease, - .moduleName = "virtual_driver", +private: + static inline BrokerDelegator delegator_; }; -HDF_INIT(g_sampleDriverEntry); \ No newline at end of file +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_SAMPLE_CPP_CLIENT_H \ 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 deleted file mode 100644 index 95733d8d9768a864f76cad355314a11d9c314e62..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/hdi_sample/sample_client_cpp/unittest/isample.h +++ /dev/null @@ -1,127 +0,0 @@ -/* - * 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("%{public}s: get %{public}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 index 7fd2e3d64ee4a6ffea5b4497ac9e58d9b8e191ac..3e2640b7257949f8bf1872031d05e6fb3d7e5129 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,409 +13,78 @@ * limitations under the License. */ -#include "sample_proxy.h" #include +#include +#include +#include +#include #include +#include + +#include "sample_proxy.h" 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) +sptr ISample::Get(const std::string &serviceName, bool isStub) { - 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; + if (!isStub) { + using namespace OHOS::HDI::ServiceManager::V1_0; + auto servMgr = IServiceManager::Get(); + if (servMgr == nullptr) { + return nullptr; } - } - - 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; + sptr remote = servMgr->GetService(serviceName.c_str()); + if (remote != nullptr) { + return hdi_facecast(remote); } - } - 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; + HDF_LOGE("%{public}s: get %{public}s failed!", __func__, serviceName.c_str()); + return nullptr; } - 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; + std::string desc = Str16ToStr8(ISample::GetDescriptor()); + void *impl = LoadHdiImpl(desc.data(), serviceName.c_str()); + if (impl == nullptr) { + HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); + return nullptr; } - return HDF_SUCCESS; + return reinterpret_cast(impl); } - -int32_t SampleProxy::ArrayTypeTest(const std::vector& input, std::vector& output) +sptr ISample::Get(bool isStub) { - 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; + return ISample::Get("sample_service", isStub); } -int32_t SampleProxy::StructTypeTest(const StructSample& input, StructSample& output) +int32_t SampleProxy::GetInterface(sptr &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; + data.WriteInterfaceToken(ISample::GetDescriptor()); + int32_t ret = Remote()->SendRequest(CMD_INTERFACE_TRANS_TEST, data, reply, option); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: SendRequest failed, error code is %{public}d", __func__, ret); + return ret; } - - 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; + auto remote = reply.ReadRemoteObject(); + if (remote == nullptr) { + HDF_LOGE("%{public}s: failed to read remote object", __func__); + return HDF_FAILURE; } - 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 = hdi_facecast(remote); + if (output == nullptr) { + HDF_LOGE("%{public}s: failed to cast foo proxy", __func__); + return HDF_FAILURE; } - output = (EnumSample)reply.ReadUint32(); - return HDF_SUCCESS; } -} // namespace V1_0 -} // namespace Sample -} // namespace HDI -} // namespace OHOS \ No newline at end of file +} // 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 index 4ddc29f9701469e058f5677c1dc1e4653e23cbfd..bd35983fd31d6a2ba3c32162edb2d6896a95497d 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -15,58 +15,27 @@ #ifndef HDI_SAMPLE_CLIENT_PROXY_CPP_INF_H #define HDI_SAMPLE_CLIENT_PROXY_CPP_INF_H -#include +#include + #include "isample.h" namespace OHOS { namespace HDI { namespace Sample { namespace V1_0 { -class SampleProxy : public IRemoteProxy { +class SampleProxy : public IProxyBroker { 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; + explicit SampleProxy(const sptr &impl) : IProxyBroker(impl) {} + virtual ~SampleProxy() = default; - virtual int32_t EnumTypeTest(const EnumSample& input, EnumSample& output) override; + int32_t GetInterface(sptr &output) override; private: static inline BrokerDelegator delegator_; }; -} // namespace V1_0 -} // namespace Sample -} // namespace HDI -} // namespace OHOS +} // 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/isample.h b/uhdf2/hdi/test/hdi_sample/sample_service_c/isample.h deleted file mode 100644 index 5842145ebd45c595b15517da8c4d59b22b643ee3..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/hdi_sample/sample_service_c/isample.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * 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_service.c b/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service.c deleted file mode 100644 index 332585b95549b01def9321d93f5c7f23f3e9a78c..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service.c +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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 deleted file mode 100644 index d64ec8b812f635ce2777418d89105c5dfc1b44b4..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/hdi_sample/sample_service_c/sample_service_stub.c +++ /dev/null @@ -1,488 +0,0 @@ -/* - * 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; - } - } -} diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn index ce5aef466d53f1095b6df53ac901070e9d6b41c1..781276df8b967b4d33564b9927a827c2e1076bb8 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -13,37 +13,70 @@ import("//build/ohos.gni") import("//drivers/adapter/uhdf2/uhdf.gni") -group("sample_service_cpp") { - deps = [ ":libsample_service_cpp" ] + +ohos_shared_library("libhdisample_driver_cpp") { + sources = [ "sample_driver.cpp" ] + + deps = [ ":libsample_stub_1.0" ] + + external_deps = [ + "device_driver_framework:libhdf_host", + "device_driver_framework:libhdf_ipc_adapter", + "device_driver_framework:libhdf_utils", + "device_driver_framework:libhdi", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "utils_base:utils", + ] + + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "device_driver_framework" } -ohos_shared_library("libsample_service_cpp") { +ohos_shared_library("libsample_stub_1.0") { sources = [ - "sample_driver.cpp", - "sample_service.cpp", + "foo_stub.cpp", "sample_service_stub.cpp", ] - deps = [ - "//drivers/adapter/uhdf2/host:libhdf_host", - "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", - "//drivers/adapter/uhdf2/utils:libhdf_utils", + external_deps = [ + "device_driver_framework:libhdf_utils", + "device_driver_framework:libhdi", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "utils_base:utils", ] - if (is_standard_system) { - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_single", - "utils_base:utils", - ] - } else { - external_deps = [ - "hilog:libhilog", - "ipc:ipc_single", - ] - } + install_images = [ chipset_base_dir ] + subsystem_name = "hdf" + part_name = "device_driver_framework" +} + +ohos_shared_library("libsample_service_1.0") { + sources = [ + "foo_service.cpp", + "sample_service.cpp", + "sample_service_stub.cpp", + ] + + external_deps = [ + "device_driver_framework:libhdf_utils", + "device_driver_framework:libhdi", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_single", + "utils_base:utils", + ] install_images = [ chipset_base_dir ] subsystem_name = "hdf" part_name = "device_driver_framework" } + +group("sample_service_cpp") { + deps = [ + ":libhdisample_driver_cpp", + ":libsample_service_1.0", + ":libsample_stub_1.0", + ] +} diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_service.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_service.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e21a5c6651286ef270c3be05e0be10167685bd27 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_service.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 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 "ifoo.h" + +#include "foo_service.h" + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +int32_t FooService::PingTest(bool input, bool &output) +{ + HDF_LOGI("FooService::PingTest, in=%{public}d", input); + output = input; + 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_service_cpp/foo_service.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_service.h new file mode 100644 index 0000000000000000000000000000000000000000..8992128d53d8e3abaacb860f16a7c23b5f411c86 --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_service.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 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_FOO_SERVICE_CPP_INF_H +#define HDI_FOO_SERVICE_CPP_INF_H + +#include +#include +#include + +#include "ifoo.h" + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +class FooService : public IFoo { +public: + int32_t PingTest(bool input, bool &output) override; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_FOO_SERVICE_CPP_INF_H diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fd075e91d38126f222657e6d25a046787a521fe --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2022 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 "ifoo.h" + +#include "foo_stub.h" + + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +FooStub::FooStub(const sptr serviceImpl) : IPCObjectStub(IFoo::GetDescriptor()), impl_(serviceImpl) {} + +FooStub::~FooStub() +{ + ObjectCollector::GetInstance().RemoveObject(impl_); +} + +int32_t FooStub::StubPingTest(OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) +{ + if (data.ReadInterfaceToken() != IFoo::GetDescriptor()) { + HDF_LOGE("failed to check interface token"); + return HDF_ERR_INVALID_PARAM; + } + bool input = data.ReadBool(); + bool output = false; + + if (impl_ == nullptr) { + HDF_LOGE("invalid service impl"); + return HDF_ERR_INVALID_OBJECT; + } + + int ret = impl_->PingTest(input, output); + if (ret != HDF_SUCCESS) { + return ret; + } + + reply.WriteBool(output); + return ret; +} + +int32_t FooStub::OnRemoteRequest( + uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) +{ + switch (code) { + case CMD_FOO_PING: + return StubPingTest(data, reply, option); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +sptr FooStub::ToInterface() +{ + return impl_; +} +} // 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/foo_stub.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..19d14d71646c475f84e4b8cb6bbb67b884ccd1ff --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/foo_stub.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 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_FOO_STUB_CPP_INF_H +#define HDI_FOO_STUB_CPP_INF_H + +#include +#include +#include + +#include "ifoo.h" + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +class FooStub : public OHOS::IPCObjectStub { +public: + explicit FooStub(const sptr serviceImpl); + virtual ~FooStub(); + + // function handle start + int32_t StubPingTest(OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option); + // function handle end + + // base functions + int32_t OnRemoteRequest( + uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option); + + sptr ToInterface(); + +private: + // Add constructor function to constructor map, only stub object need this + static inline ObjectDelegator objDelegator_; + + sptr impl_; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_FOO_STUB_CPP_INF_H diff --git a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h new file mode 100644 index 0000000000000000000000000000000000000000..06be1b5159465a8551c8d9831839e3908549deda --- /dev/null +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/ifoo.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 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_FOO_CPP_INF_H +#define HDI_FOO_CPP_INF_H + +#include + +namespace OHOS { +namespace HDI { +namespace Sample { +namespace V1_0 { +enum FooCommand { + CMD_FOO_PING = 1, +}; + +class IFoo : public HdiBase { +public: + DECLARE_HDI_DESCRIPTOR(u"OHOS.HDI.Sample.V1_0.IFoo"); + IFoo() = default; + virtual ~IFoo() = default; + + virtual int32_t PingTest(bool input, bool &output) = 0; +}; +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS + +#endif // HDI_FOO_CPP_INF_H \ 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 index c53b8386916b21bac2eae82f01d3859d62a0faa8..15f2e57ad0d8d1b957a78746878e29a0cfa7952e 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/isample.h +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/isample.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -20,6 +20,8 @@ #include #include #include +#include +#include "ifoo.h" namespace OHOS { namespace HDI { @@ -30,73 +32,23 @@ struct StructSample { int16_t second; }; -enum EnumSample { - MEM_FIRST, - MEM_SECOND, - MEM_THIRD, +enum SampleCmdId { + CMD_INTERFACE_TRANS_TEST, }; -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 { +class ISample : public HdiBase { 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; + DECLARE_HDI_DESCRIPTOR(u"OHOS.HDI.Sample.V1_0.ISample"); + ISample() = default; + virtual ~ISample() = default; + virtual int32_t GetInterface(sptr &output) = 0; - virtual int32_t EnumTypeTest(const EnumSample& input, EnumSample& output) const = 0; + static sptr Get(bool isStub = false); + static sptr Get(const std::string &serviceName, bool isStub = false); }; -} // namespace V1_0 -} // namespace Sample -} // namespace HDI -} // namespace OHOS +} // 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 index f12acad63af35c5fe709598210e50fb28cf793f8..eb5206e90117543f64ebe00d22107b090fb28f04 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_driver.cpp +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_driver.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -12,10 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include + #include -#include #include +#include +#include +#include + #include "sample_service_stub.h" #define HDF_LOG_TAG sample_service_cpp @@ -24,51 +27,77 @@ using namespace OHOS::HDI::Sample::V1_0; struct HdfSampleService { struct IDeviceIoService ioservice; - void *instance; + OHOS::sptr sampleStub; }; -static int32_t SampleServiceDispatch(struct HdfDeviceIoClient *client, int cmdId, - struct HdfSBuf *data, struct HdfSBuf *reply) +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); + struct HdfSampleService *hdfSampleService = + CONTAINER_OF(client->device->service, struct HdfSampleService, ioservice); + + OHOS::MessageParcel *dataParcel = nullptr; + OHOS::MessageParcel *replyParcel = nullptr; + OHOS::MessageOption option; + if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS || SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: failed trans sbuf to parcel", __func__); + return HDF_FAILURE; + } + return hdfSampleService->sampleStub->SendRequest(cmdId, *dataParcel, *replyParcel, option); } -int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) +static int HdfSampleDriverInit(struct HdfDeviceObject *deviceObject) { - HDF_LOGE("HdfSampleDriverInit enter, new hdi impl"); + (void)deviceObject; + HDF_LOGI("HdfSampleDriverInit enter, new hdi impl"); return HDF_SUCCESS; } -int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) +static int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) { HDF_LOGI("HdfSampleDriverBind enter!"); - - struct HdfSampleService *hdfSampleService = (struct HdfSampleService *)OsalMemAlloc( - sizeof(struct HdfSampleService)); + struct HdfSampleService *hdfSampleService = new HdfSampleService(); if (hdfSampleService == nullptr) { - HDF_LOGE("HdfSampleDriverBind OsalMemAlloc HdfSampleService failed!"); + HDF_LOGE("HdfSampleDriverBind: OsalMemAlloc HdfSampleService failed!"); + return HDF_FAILURE; + } + hdfSampleService->sampleStub = nullptr; + auto sampleImpl = ISample::Get(true); + if (sampleImpl == nullptr) { + HDF_LOGE("HdfSampleDriverBind: failed to get ISample implement"); + delete hdfSampleService; + return HDF_FAILURE; + } + + hdfSampleService->sampleStub = + OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(sampleImpl, ISample::GetDescriptor()); + if (hdfSampleService->sampleStub == nullptr) { + HDF_LOGE("HdfSampleDriverBind: failed to get ISample stub object"); + delete hdfSampleService; 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) +static void HdfSampleDriverRelease(struct HdfDeviceObject *deviceObject) { + HDF_LOGI("HdfSampleDriverRelease called"); + if (deviceObject->service == nullptr) { + HDF_LOGE("HdfSampleDriverRelease not initted"); + return; + } struct HdfSampleService *hdfSampleService = CONTAINER_OF(deviceObject->service, struct HdfSampleService, ioservice); - SampleStubRelease(hdfSampleService->instance); - OsalMemFree(hdfSampleService); + hdfSampleService->sampleStub = nullptr; + delete hdfSampleService; } -struct HdfDriverEntry g_sampleDriverEntry = { +static struct HdfDriverEntry g_sampleDriverEntry = { .moduleVersion = 1, .moduleName = "sample_service_cpp", .Bind = HdfSampleDriverBind, 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 index c9476952dc7677c994b7ea794655bc18b4a3d211..0fd3ac54fc6744a5bd7f7d43419226a6e8c5bb74 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.cpp +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,114 +13,26 @@ * limitations under the License. */ -#include "sample_service.h" #include +#include "foo_service.h" + +#include "sample_service.h" + namespace OHOS { namespace HDI { namespace Sample { namespace V1_0 { -int32_t SampleService::BooleanTypeTest(const bool input, bool& output) const +int32_t SampleService::GetInterface(sptr &output) { - output = input; + output = new FooService(); return 0; } -int32_t SampleService::ByteTypeTest(const int8_t input, int8_t& output) const +extern "C" +ISample *SampleImplGetInstance(void) { - 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; + return new SampleService(); } } // namespace V1_0 } // namespace Sample 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 index 6accfc2601bdde1f9795906706e19b7055226a74..9845a7a1943332de3b0f912b824017f11599faad 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.h +++ b/uhdf2/hdi/test/hdi_sample/sample_service_cpp/sample_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -24,46 +24,14 @@ namespace Sample { namespace V1_0 { class SampleService : public ISample { public: - SampleService(){} - virtual ~SampleService() override {} + SampleService() {} + ~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; + int32_t GetInterface(sptr &output) override; }; -} // namespace V1_0 -} // namespace Sample -} // namespace HDI -} // namespace OHOS +} // 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 index fbd84b4ca279dccc19ef1ba635b2203582bec17d..3905fc9f77e725f6d33134288124ed3155cd40b8 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,472 +13,93 @@ * limitations under the License. */ -#include "sample_service_stub.h" #include #include #include +#include +#include +#include + +#include "foo_service.h" + +#include "sample_service_stub.h" + +#define HDF_LOG_TAG sample_stub 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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteBool(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteInt8(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteInt16(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteInt32(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteInt64(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteFloat(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteDouble(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteString(output)) { - HDF_LOGE("%{public}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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteUint8(output)) { - HDF_LOGE("%{public}s: write result failed", __func__); - return HDF_ERR_INVALID_PARAM; - } - - return HDF_SUCCESS; -} - -int32_t SampleServiceStub::SampleStubUshortTypeTest(MessageParcel& data, - MessageParcel& reply, MessageOption& option) const +sptr ISample::Get(const std::string &serviceName, bool isStub) { - const uint16_t input = data.ReadUint16(); - uint16_t output; - - int32_t ec = service.UshortTypeTest(input, output); - if (ec != HDF_SUCCESS) { - HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; + if (!isStub) { + return nullptr; } - if (!reply.WriteUint16(output)) { - HDF_LOGE("%{public}s: write result failed", __func__); - return HDF_ERR_INVALID_PARAM; + std::string desc = Str16ToStr8(ISample::GetDescriptor()); + void *impl = LoadHdiImpl(desc.data(), serviceName.c_str()); + if (impl == nullptr) { + HDF_LOGE("failed to load hdi impl %{public}s", desc.data()); + return nullptr; } - return HDF_SUCCESS; + return reinterpret_cast(impl); } -int32_t SampleServiceStub::SampleStubUintTypeTest(MessageParcel& data, - MessageParcel& reply, MessageOption& option) const +sptr ISample::Get(bool isStub) { - const uint32_t input = data.ReadUint32(); - uint32_t output; - - int32_t ec = service.UintTypeTest(input, output); - if (ec != HDF_SUCCESS) { - HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteUint32(output)) { - HDF_LOGE("%{public}s: write result failed", __func__); - return HDF_ERR_INVALID_PARAM; - } - - return HDF_SUCCESS; + std::string defaultName = "sample_service"; + return ISample::Get(defaultName, isStub); } -int32_t SampleServiceStub::SampleStubUlongTypeTest(MessageParcel& data, - MessageParcel& reply, MessageOption& option) const +SampleServiceStub::SampleServiceStub(const sptr &serviceImpl) : + IPCObjectStub(ISample::GetDescriptor()), impl_(serviceImpl) { - const uint64_t input = data.ReadUint64(); - uint64_t output; - - int32_t ec = service.UlongTypeTest(input, output); - if (ec != HDF_SUCCESS) { - HDF_LOGE("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteUint64(output)) { - HDF_LOGE("%{public}s: write result failed", __func__); - return HDF_ERR_INVALID_PARAM; - } - - return HDF_SUCCESS; } -int32_t SampleServiceStub::SampleStubListTypeTest(MessageParcel& data, - MessageParcel& reply, MessageOption& option) const +SampleServiceStub::~SampleServiceStub() { - 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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteUint32(output.size())) { - HDF_LOGE("%{public}s: write result size failed", __func__); - return HDF_ERR_INVALID_PARAM; - } - - for (auto it : output) { - if (!reply.WriteInt8(it)) { - HDF_LOGE("%{public}s: write result failed", __func__); - return HDF_ERR_INVALID_PARAM; - } - } - - return HDF_SUCCESS; + ObjectCollector::GetInstance().RemoveObject(impl_); } -int32_t SampleServiceStub::SampleStubMapTypeTest(MessageParcel& data, - MessageParcel& reply, MessageOption& option) const +int32_t SampleServiceStub::SampleStubGetInterface( + 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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteUint32(output.size())) { - HDF_LOGE("%{public}s: write result size failed", __func__); + if (data.ReadInterfaceToken() != ISample::GetDescriptor()) { + HDF_LOGE("failed to check interface"); return HDF_ERR_INVALID_PARAM; } - - for (auto it : output) { - if (!reply.WriteInt8(it.first) || !reply.WriteInt8(it.second)) { - HDF_LOGE("%{public}s: write result size failed", __func__); - return HDF_ERR_INVALID_PARAM; - } + sptr interfaceObj; + auto ret = impl_->GetInterface(interfaceObj); + if (ret != HDF_SUCCESS || interfaceObj == nullptr) { + HDF_LOGE("%{public}s: call failed", __func__); + return HDF_FAILURE; } - 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("%{public}s: call failed, error code is %{public}d", __func__, ec); - return ec; - } - - if (!reply.WriteUint32(output.size())) { - HDF_LOGE("%{public}s: write result size failed", __func__); - return HDF_ERR_INVALID_PARAM; - } - - for (auto it : output) { - if (!reply.WriteInt8(it)) { - HDF_LOGE("%{public}s: write result failed", __func__); - return HDF_ERR_INVALID_PARAM; - } + sptr stubObject = ObjectCollector::GetInstance().GetOrNewObject(interfaceObj, IFoo::GetDescriptor()); + if (stubObject == nullptr) { + HDF_LOGE("%{public}s: failed to cast interface to stub object", __func__); + return HDF_ERR_INVALID_OBJECT; } + reply.WriteRemoteObject(stubObject); return HDF_SUCCESS; } -int32_t SampleServiceStub::SampleStubStructTypeTest(MessageParcel& data, - MessageParcel& reply, MessageOption& option) const +int32_t SampleServiceStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { - 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); + switch (code) { + case CMD_INTERFACE_TRANS_TEST: + return SampleStubGetInterface(data, reply, option); default: { - HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId); + HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, code); 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("%{public}s:invalid data sbuf object to dispatch", __func__); - return HDF_ERR_INVALID_PARAM; - } - - OHOS::MessageOption option; - return sampleStub->SampleServiceStubOnRemoteRequest(cmdId, *dataParcel, *replyParcel, option); -} +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS 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 index 8d76a631fe68e8214332e91244398ce9891c80a4..cff89dda70487ebd3ba2fb1c58230df719f29031 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -16,52 +16,35 @@ #ifndef HDI_SAMPLE_SERVICE_STUB_CPP_INF_H #define HDI_SAMPLE_SERVICE_STUB_CPP_INF_H -#include +#include #include +#include +#include #include + #include "sample_service.h" namespace OHOS { namespace HDI { namespace Sample { namespace V1_0 { -class SampleServiceStub { +class SampleServiceStub : public OHOS::IPCObjectStub { public: - SampleServiceStub(){} - virtual ~SampleServiceStub(){} + explicit SampleServiceStub(const sptr &serviceImpl); + virtual ~SampleServiceStub(); + + int32_t SampleStubGetInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option) const; - 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 OnRemoteRequest( +uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; - int32_t SampleServiceStubOnRemoteRequest(int cmdId, MessageParcel& data, MessageParcel& reply, - MessageOption& option) const; private: - SampleService service; + static inline ObjectDelegator objDelegator_; + sptr impl_; }; -} // 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); +} // namespace V1_0 +} // namespace Sample +} // namespace HDI +} // namespace OHOS #endif // HDI_SAMPLE_SERVICE_STUB_CPP_INF_H \ No newline at end of file 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 deleted file mode 100644 index e8dcac07354732c5847ed9f4fb45b98ba69c4566..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/reg_pnp_device/hdf_dev_reg_pnp_test.c +++ /dev/null @@ -1,326 +0,0 @@ -/* - * 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 deleted file mode 100644 index 1e56a14ba1b0c767fce0651a2d648b5608531d65..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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("%{public}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("%{public}s pnp get str is %{public}s", __func__, str); - HdfSbufWriteInt32(reply, 1); - break; - } - case HDF_PNP_MES_FAULT: { - // test to crash process - const char *str = NULL; - HDF_LOGE("%{public}s test to crash procss is %{public}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("%{public}s failed, cmd id is %{public}d", __func__, cmdId); - break; - } - } - return ret; -} - -int32_t HdfPnpDriverBind(struct HdfDeviceObject *para) -{ - HDF_LOGE("%{public}s", __func__); - static struct IDeviceIoService virtualService = { - .object.objectId = 1, - .Dispatch = PnpDriverDispatch, - }; - para->service = &virtualService; - return 0; -} - -int32_t HdfPnpDriverInit(struct HdfDeviceObject *para) -{ - HDF_LOGE("%{public}s enter!", __func__); - if (para == NULL) { - HDF_LOGE("%{public}s init failed, input is null", __func__); - return HDF_FAILURE; - } - DeviceHandle *palHandle = DeviceHandleCreate(PAL_GPIO_TYPE, NULL); - if (palHandle == NULL) { - HDF_LOGE("%{public}s %{public}d open gpio failed", __func__, __LINE__); - g_gpioFlg = 0; - } else { - HDF_LOGE("%{public}s %{public}d open gpio success", __func__, __LINE__); - DeviceHandleDestroy(palHandle); - g_gpioFlg = 1; - } - return 0; -} - -void HdfPnpDriverRelease(struct HdfDeviceObject *para) -{ - HDF_LOGE("%{public}s enter!", __func__); - if (para == NULL) { - HDF_LOGE("%{public}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_virtual_device/hdf_dev_reg_virtual_test.c b/uhdf2/hdi/test/reg_virtual_device/hdf_dev_reg_virtual_test.c deleted file mode 100644 index a7b00ecf4dd384d53c86c61f4cba7cb6cca6c1b6..0000000000000000000000000000000000000000 --- a/uhdf2/hdi/test/reg_virtual_device/hdf_dev_reg_virtual_test.c +++ /dev/null @@ -1,344 +0,0 @@ -/* - * 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] %{public}s line:%{public}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] %{public}s line:%{public}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] %{public}s test items: %{public}d", __func__, g_testItemCnt); - HDF_LOGE("[HDF_TEST] %{public}s test PASS: %{public}d", __func__, g_testItemCnt - g_testItemFailCnt); - HDF_LOGE("[HDF_TEST] %{public}s test FAIL: %{public}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] %{public}s line:%{public}d test 1 time use time:%{public}lu s %{public}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] %{public}s line:%{public}d test reg %{public}d time use time:%{public}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] %{public}s line:%{public}d test unreg %{public}d time use time:%{public}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] %{public}s line:%{public}d", func, line); - } else { - HDF_LOGE("[HDF_TEST] %{public}s line:%{public}d %{public}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] %{public}s line:%{public}d", __func__, __LINE__); - } else { - HDF_LOGE("[HDF_TEST] %{public}s line:%{public}d %{public}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] %{public}s %{public}s:%{public}d line:%{public}d", - __func__, debugInfo, list.deviceCnt, __LINE__); - DLIST_FOR_EACH_ENTRY(devNode, &list.list, struct DeviceInfoNode, node) { - HDF_LOGD("[HDF_TEST] %{public}s: %{public}s %{public}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] %{public}s find usable service failed %{public}d != %{public}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] %{public}s find unusable service failed %{public}d != %{public}d", - __func__, cnt, 0); - printf("[HDF_TEST] %s find unusable service failed %d != %d\r\n", __func__, cnt, 0); - } - } - } else { - HDF_LOGE("[HDF_TEST] %{public}s line:%{public}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/servmgr/service_manager_hdi_c_test.cpp b/uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp index d620383fa49ca289f3243ca77e4a8dbe2c3dfe7d..8a854b1f1889935824e851df91e1996e2ac46abd 100644 --- a/uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp +++ b/uhdf2/hdi/test/servmgr/service_manager_hdi_c_test.cpp @@ -22,18 +22,20 @@ #include #include #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; -constexpr int WAIT_LOAD_UNLOAD_TIME = 300; +static constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; +static constexpr const char *TEST_SERVICE_INTERFACE_DESC = "hdf.test.sampele_service"; +static constexpr int PAYLOAD_NUM = 1234; +static constexpr int WAIT_LOAD_UNLOAD_TIME = 300; class HdfServiceMangerHdiCTest : public testing::Test { public: static void SetUpTestCase() @@ -70,12 +72,17 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest002, TestSize.Level1) HDIServiceManagerRelease(servmgr); ASSERT_TRUE(sampleService != nullptr); + bool ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); + struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); ASSERT_TRUE(data != nullptr); ASSERT_TRUE(reply != nullptr); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); - bool ret = HdfSbufWriteString(data, "sample_service test call"); + ret = HdfSbufWriteString(data, "sample_service test call"); ASSERT_EQ(ret, true); int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_SERVICE_PING, data, reply); @@ -107,6 +114,8 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest003, TestSize.Level1) struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); HDIServiceManagerRelease(servmgr); ASSERT_TRUE(sampleService != nullptr); + bool ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); struct HdfRemoteService *callback = HdfRemoteServiceObtain(nullptr, &g_callbackDispatcher); ASSERT_NE(callback, nullptr); @@ -114,7 +123,8 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest003, TestSize.Level1) struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); ASSERT_TRUE(data != nullptr); ASSERT_TRUE(reply != nullptr); - + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); int32_t payload = PAYLOAD_NUM; HdfSbufWriteInt32(data, payload); HdfSbufWriteRemoteService(data, callback); @@ -135,18 +145,22 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest004, TestSize.Level1) struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); HDIServiceManagerRelease(servmgr); ASSERT_TRUE(sampleService != nullptr); + bool ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); ASSERT_TRUE(data != nullptr); ASSERT_TRUE(reply != nullptr); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); 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); + ret = HdfSbufReadInt32(reply, &result); ASSERT_TRUE(ret); int32_t expRes = PAYLOAD_NUM + PAYLOAD_NUM; @@ -164,14 +178,17 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest005, TestSize.Level1) struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); HDIServiceManagerRelease(servmgr); ASSERT_TRUE(sampleService != nullptr); + bool ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); 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); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); + struct DataBlock dataBlock = {1, 2, "dataBolck", 3}; + ret = DataBlockBlockMarshalling(&dataBlock, data); ASSERT_TRUE(ret); int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_STRUCT_TRANS, data, reply); @@ -198,6 +215,8 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest006, TestSize.Level1) struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); HDIServiceManagerRelease(servmgr); ASSERT_TRUE(sampleService != nullptr); + bool ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); @@ -209,8 +228,9 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest006, TestSize.Level1) for (int i = 0; i < buffersize; i++) { dataBuffer[i] = i; } - - bool ret = HdfSbufWriteUnpadBuffer(data, dataBuffer, sizeof(dataBuffer)); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); + ret = HdfSbufWriteUnpadBuffer(data, dataBuffer, sizeof(dataBuffer)); ASSERT_TRUE(ret); int status = sampleService->dispatcher->Dispatch(sampleService, SAMPLE_BUFFER_TRANS, data, reply); @@ -244,12 +264,16 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest007, TestSize.Level1) OsalMSleep(WAIT_LOAD_UNLOAD_TIME); sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); ASSERT_TRUE(sampleService != nullptr); + ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); ASSERT_TRUE(data != nullptr); ASSERT_TRUE(reply != nullptr); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); const char *newServName = "sample_driver_service2"; ret = HdfSbufWriteString(data, newServName); ASSERT_TRUE(ret); @@ -259,9 +283,13 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest007, TestSize.Level1) struct HdfRemoteService *sampleService2 = servmgr->GetService(servmgr, newServName); ASSERT_TRUE(sampleService != nullptr); + ret = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); HdfSbufFlush(data); HdfSbufFlush(reply); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); HdfSbufWriteInt32(data, PAYLOAD_NUM); HdfSbufWriteInt32(data, PAYLOAD_NUM); @@ -276,6 +304,8 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest007, TestSize.Level1) HdfRemoteServiceRecycle(sampleService2); HdfSbufFlush(data); + ret = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(ret, true); ret = HdfSbufWriteString(data, newServName); ASSERT_TRUE(ret); @@ -406,6 +436,8 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest009, TestSize.Level1) OsalMSleep(WAIT_LOAD_UNLOAD_TIME); sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME); ASSERT_TRUE(sampleService != nullptr); + bool res = HdfRemoteServiceSetInterfaceDesc(sampleService, TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(res, true); struct ServiceStatusData ssd; struct ServiceStatusListener *listener = HdiServiceStatusListenerNewInstance(); @@ -422,9 +454,10 @@ HWTEST_F(HdfServiceMangerHdiCTest, ServMgrTest009, TestSize.Level1) struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); ASSERT_TRUE(data != nullptr); ASSERT_TRUE(reply != nullptr); - - ret = HdfSbufWriteString(data, info.data()); - ASSERT_TRUE(ret); + res = HdfRemoteServiceWriteInterfaceToken(sampleService, data); + ASSERT_EQ(res, true); + res = HdfSbufWriteString(data, info.data()); + ASSERT_TRUE(res); ssd.callbacked = false; ssd.waitStatus = SERVIE_STATUS_CHANGE; diff --git a/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp b/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp index 15b2e9589ce696e90ff153f55eec197fb484acbd..be31636d9e357a19e2eb6985068e698a1a00312b 100644 --- a/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp +++ b/uhdf2/hdi/test/servmgr/service_manager_hdi_test.cpp @@ -22,8 +22,9 @@ #include #include #include +#include #include -#include "osal_time.h" + #include "sample_hdi.h" #define HDF_LOG_TAG service_manager_test_cpp @@ -39,11 +40,12 @@ using OHOS::HDI::ServiceManager::V1_0::IServiceManager; using OHOS::HDI::ServiceManager::V1_0::IServStatListener; using OHOS::HDI::ServiceManager::V1_0::ServiceStatus; using OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub; -constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; -constexpr int PAYLOAD_NUM = 1234; -constexpr int SMQ_TEST_QUEUE_SIZE = 10; -constexpr int SMQ_TEST_WAIT_TIME = 100; -constexpr int WAIT_LOAD_UNLOAD_TIME = 300; +static constexpr const char *TEST_SERVICE_NAME = "sample_driver_service"; +static constexpr const char16_t *TEST_SERVICE_INTERFACE_DESC = u"hdf.test.sampele_service"; +static constexpr int PAYLOAD_NUM = 1234; +static constexpr int SMQ_TEST_QUEUE_SIZE = 10; +static constexpr int SMQ_TEST_WAIT_TIME = 100; +static constexpr int WAIT_LOAD_UNLOAD_TIME = 300; class HdfServiceMangerHdiTest : public testing::Test { public: @@ -87,6 +89,8 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest002, TestSize.Level1) OHOS::MessageParcel data; OHOS::MessageParcel reply; + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); data.WriteCString("sample_service test call"); OHOS::MessageOption option; @@ -124,6 +128,8 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest003, TestSize.Level1) OHOS::MessageParcel data; OHOS::MessageParcel reply; int32_t payload = PAYLOAD_NUM; + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); data.WriteInt32(payload); data.WriteRemoteObject(callback); @@ -143,6 +149,8 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest004, TestSize.Level1) OHOS::MessageParcel data; OHOS::MessageParcel reply; + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); data.WriteInt32(PAYLOAD_NUM); data.WriteInt32(PAYLOAD_NUM); @@ -170,8 +178,9 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest006, TestSize.Level1) for (int i = 0; i < buffersize; i++) { dataBuffer[i] = i; } - - bool ret = data.WriteUnpadBuffer(dataBuffer, sizeof(dataBuffer)); + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); + ret = data.WriteUnpadBuffer(dataBuffer, sizeof(dataBuffer)); ASSERT_TRUE(ret); OHOS::MessageOption option; @@ -187,7 +196,7 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest006, TestSize.Level1) } /* - * Test device manager Load/UnLoad deivce and driver dynamic register device + * Test device manager Load/UnLoad device and driver dynamic register device */ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest007, TestSize.Level1) { @@ -210,7 +219,8 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest007, TestSize.Level1) OHOS::MessageParcel data; OHOS::MessageParcel reply; OHOS::MessageOption option; - + ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); const char *newServName = "sample_driver_service2"; ret = data.WriteCString(newServName); ASSERT_TRUE(ret); @@ -223,6 +233,8 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest007, TestSize.Level1) data.FlushBuffer(); reply.FlushBuffer(); + ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); data.WriteInt32(PAYLOAD_NUM); data.WriteInt32(PAYLOAD_NUM); @@ -236,6 +248,8 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest007, TestSize.Level1) data.FlushBuffer(); reply.FlushBuffer(); + ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); data.WriteCString(newServName); status = sampleService->SendRequest(SAMPLE_UNREGISTER_DEVICE, data, reply, option); @@ -254,9 +268,7 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest007, TestSize.Level1) class ServStatListener : public OHOS::HDI::ServiceManager::V1_0::ServStatListenerStub { public: using StatusCallback = std::function; - explicit ServStatListener(StatusCallback callback) : callback_(std::move(callback)) - { - } + explicit ServStatListener(StatusCallback callback) : callback_(std::move(callback)) {} ~ServStatListener() = default; void OnReceive(const ServiceStatus &status) override { @@ -367,6 +379,8 @@ void HdfServiceMangerHdiTest::TestSampleService(sptr& sampleServi OHOS::MessageParcel data; OHOS::MessageParcel reply; OHOS::MessageOption option; + bool res = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(res, true); std::string info = "foo"; data.WriteCString(info.data()); callbacked = false; @@ -489,11 +503,12 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest011, TestSize.Level1) OHOS::MessageParcel data; OHOS::MessageParcel reply; OHOS::MessageOption option; - std::unique_ptr> smq - = std::make_unique>(SMQ_TEST_QUEUE_SIZE, SmqType::SYNCED_SMQ); + std::unique_ptr> smq = + std::make_unique>(SMQ_TEST_QUEUE_SIZE, SmqType::SYNCED_SMQ); ASSERT_TRUE(smq->IsGood()); - - auto ret = smq->GetMeta()->Marshalling(data); + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); + ret = smq->GetMeta()->Marshalling(data); ASSERT_TRUE(ret); data.WriteUint32(1); @@ -502,7 +517,7 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest011, TestSize.Level1) constexpr int SEND_TIMES = 20; for (size_t i = 0; i < SEND_TIMES; i++) { - SampleSmqElement t = { 0 }; + SampleSmqElement t = {0}; t.data32 = i; t.data64 = i + 1; @@ -526,13 +541,13 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest012, TestSize.Level1) OHOS::MessageParcel data; OHOS::MessageParcel reply; OHOS::MessageOption option; - std::unique_ptr> smq - = std::make_unique>(SMQ_TEST_QUEUE_SIZE, SmqType::SYNCED_SMQ); + std::unique_ptr> smq = + std::make_unique>(SMQ_TEST_QUEUE_SIZE, SmqType::SYNCED_SMQ); ASSERT_TRUE(smq->IsGood()); - + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); constexpr uint32_t ELEMENT_SIZE = 2; - - auto ret = smq->GetMeta()->Marshalling(data); + ret = smq->GetMeta()->Marshalling(data); ASSERT_TRUE(ret); data.WriteUint32(ELEMENT_SIZE); @@ -546,7 +561,7 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest012, TestSize.Level1) t[0].data64 = i + 1; t[1].data32 = i + 1; t[1].data64 = i + 1; - HDF_LOGI("%{public}s:write smq message %{public}zu", __func__, i); + HDF_LOGI("%{public}s:write smq message %{public}d", __func__, i); auto status = smq->Write(&t[0], ELEMENT_SIZE, OHOS::MillisecToNanosec(SMQ_TEST_WAIT_TIME)); ASSERT_EQ(status, 0); } @@ -567,13 +582,13 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest013, TestSize.Level1) OHOS::MessageParcel reply; OHOS::MessageOption option; - std::unique_ptr> smq - = std::make_unique>(SMQ_TEST_QUEUE_SIZE, SmqType::UNSYNC_SMQ); + std::unique_ptr> smq = + std::make_unique>(SMQ_TEST_QUEUE_SIZE, SmqType::UNSYNC_SMQ); ASSERT_TRUE(smq->IsGood()); - + bool ret = data.WriteInterfaceToken(TEST_SERVICE_INTERFACE_DESC); + ASSERT_EQ(ret, true); constexpr uint32_t ELEMENT_SIZE = 2; - - auto ret = smq->GetMeta()->Marshalling(data); + ret = smq->GetMeta()->Marshalling(data); ASSERT_TRUE(ret); data.WriteUint32(ELEMENT_SIZE); auto status = sampleService->SendRequest(SAMPLE_TRANS_SMQ, data, reply, option); @@ -588,7 +603,7 @@ HWTEST_F(HdfServiceMangerHdiTest, ServMgrTest013, TestSize.Level1) t[0].data64 = i + 1; t[1].data32 = i + 1; t[1].data64 = i + 1; - HDF_LOGI("%{public}s:write smq message %{public}zu", __func__, i); + HDF_LOGI("%{public}s:write smq message %{public}d", __func__, i); status = smq->WriteNonBlocking(&t[0], ELEMENT_SIZE); ASSERT_EQ(status, 0); } diff --git a/uhdf2/host/BUILD.gn b/uhdf2/host/BUILD.gn index a18c2efe67a10e902f0d89ac40854aa890056f59..5a86414a3340138419a0c0f99c411893c10c0bcf 100644 --- a/uhdf2/host/BUILD.gn +++ b/uhdf2/host/BUILD.gn @@ -13,7 +13,6 @@ import("//build/ohos.gni") import("//drivers/adapter/uhdf2/uhdf.gni") - config("libhdf_host_pub_config") { visibility = [ ":*" ] @@ -152,11 +151,3 @@ if (defined(ohos_lite)) { part_name = "device_driver_framework" } } - -ohos_prebuilt_etc("hdf_devhost.rc") { - source = "hdf_devhost.cfg" - relative_install_dir = "init" - install_images = [ chipset_base_dir ] - subsystem_name = "hdf" - part_name = "device_driver_framework" -} diff --git a/uhdf2/host/devhost.c b/uhdf2/host/devhost.c index cdd021e2fcad25198a3ab99922a1331c182f7880..fbebfa40ee2e039488ad860d06544949517e3030 100644 --- a/uhdf2/host/devhost.c +++ b/uhdf2/host/devhost.c @@ -99,7 +99,7 @@ int main(int argc, char **argv) DevHostServiceFreeInstance(instance); HdfPowerManagerExit(); - + HDF_LOGD("hdf device host %{public}s %{public}d exit", hostName, hostId); return status; } diff --git a/uhdf2/host/hdf_devhost.cfg b/uhdf2/host/hdf_devhost.cfg deleted file mode 100755 index 7cadf0e54f40dfed89c53c1bef214be0b3137937..0000000000000000000000000000000000000000 --- a/uhdf2/host/hdf_devhost.cfg +++ /dev/null @@ -1,130 +0,0 @@ -{ - "jobs" : [{ - "name" : "post-fs-data", - "cmds" : [ - "start hdf_devhost" - ] - } - ], - "services" : [ - { - "name" : "blue_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "sample_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "system", - "gid" : ["system"] - }, - { - "name" : "usb_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "usbfnMaster_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "power_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "wifi_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "audio_hdi_server_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "camera_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "input_user_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "display_gralloc_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "codec_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "riladapter_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "sensor_dal_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "vibrator_dal_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "light_dal_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "wifi_c_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - }, - { - "name" : "disp_gralloc_host", - "dynamic" : true, - "path" : ["/vendor/bin/hdf_devhost"], - "uid" : "root", - "gid" : ["system"] - } - ] -} diff --git a/uhdf2/host/include/devhost_service_full.h b/uhdf2/host/include/devhost_service_full.h old mode 100755 new mode 100644 diff --git a/uhdf2/host/include/devhost_service_stub.h b/uhdf2/host/include/devhost_service_stub.h old mode 100755 new mode 100644 diff --git a/uhdf2/host/include/devsvc_manager_proxy.h b/uhdf2/host/include/devsvc_manager_proxy.h index 01d3ffcda84be072501c8ae60ab5da5a1bf7deb1..c8035107911f28396c79a2e0f8447921f5729666 100644 --- a/uhdf2/host/include/devsvc_manager_proxy.h +++ b/uhdf2/host/include/devsvc_manager_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -30,8 +30,9 @@ enum { DEVSVC_MANAGER_ADD_SERVICE = 1, DEVSVC_MANAGER_UPDATE_SERVICE, DEVSVC_MANAGER_GET_SERVICE, - DEVSVC_MANAGER_REGISER_SVCLISTENER, - DEVSVC_MANAGER_UNREGISER_SVCLISTENER, + DEVSVC_MANAGER_REGISTER_SVCLISTENER, + DEVSVC_MANAGER_UNREGISTER_SVCLISTENER, + DEVSVC_MANAGER_LIST_ALL_SERVICE, DEVSVC_MANAGER_LIST_SERVICE, DEVSVC_MANAGER_REMOVE_SERVICE, }; diff --git a/uhdf2/host/src/devhost_service_full.c b/uhdf2/host/src/devhost_service_full.c index 4d0f1a280d55ccbc51c47391075c43565a52bfb7..a01ab4b87487cab0e3a7ec739f62558011c3b331 100644 --- a/uhdf2/host/src/devhost_service_full.c +++ b/uhdf2/host/src/devhost_service_full.c @@ -158,6 +158,7 @@ static int DevHostServiceFullStartService(struct IDevHostService *service) int ret = DevmgrServiceClntAttachDeviceHost(hostService->hostId, service); if (ret != HDF_SUCCESS) { + HDF_LOGE("failed to start host service, attach host error %{public}d", ret); return ret; } diff --git a/uhdf2/host/src/devhost_service_stub.c b/uhdf2/host/src/devhost_service_stub.c index f71c4f603b1e236aa3087d54e17c2de9043557d1..a30edae3f191f9bf54de767fdd4dc6c67ff6bf27 100644 --- a/uhdf2/host/src/devhost_service_stub.c +++ b/uhdf2/host/src/devhost_service_stub.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -30,12 +30,52 @@ static void DevHostSetCurrentSecurec(const char *hostName) (void) hostName; } +static int DispatchAddDevice(struct IDevHostService *serviceIf, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + (void)reply; + if ((serviceIf == NULL) || (serviceIf->AddDevice == NULL)) { + HDF_LOGE("serviceIf or serviceIf->AddDevice is NULL"); + return HDF_FAILURE; + } + struct HdfDeviceInfo *attribute = DeviceAttributeDeserialize(data); + if (attribute == NULL) { + HDF_LOGE("Dispatch failed, attribute is null"); + return HDF_FAILURE; + } + HDF_LOGI("add device 0x%{public}x", attribute->deviceId); + int ret = serviceIf->AddDevice(serviceIf, attribute); + if (ret != HDF_SUCCESS) { + HDF_LOGE("Dispatch failed, add service failed and ret is %{public}d", ret); + } + DeviceSerializedAttributeRelease(attribute); + return ret; +} + +static int DispatchDelDevice(struct IDevHostService *serviceIf, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint32_t deviceId = 0; + (void)reply; + if ((serviceIf == NULL) || (serviceIf->DelDevice == NULL)) { + HDF_LOGE("serviceIf or serviceIf->DelDevice is NULL"); + return HDF_FAILURE; + } + if (!HdfSbufReadUint32(data, &deviceId)) { + HDF_LOGE("failed to del device, invalid device id"); + return HDF_FAILURE; + } + + HDF_LOGI("del device 0x%{public}x", deviceId); + int ret = serviceIf->DelDevice(serviceIf, deviceId); + if (ret != HDF_SUCCESS) { + HDF_LOGE("del service failed, ret is %{public}d", ret); + } + return ret; +} + static int DevHostServiceStubDispatch( struct HdfRemoteService *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) { - (void)reply; int ret = HDF_FAILURE; - uint32_t deviceId = 0; if (stub == NULL || data == NULL) { return ret; } @@ -45,38 +85,11 @@ static int DevHostServiceStubDispatch( 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 HdfDeviceInfo *attribute = DeviceAttributeDeserialize(data); - if (attribute == NULL) { - HDF_LOGE("Dispatch failed, attribute is null"); - break; - } - HDF_LOGI("add device 0x%{public}x", attribute->deviceId); - ret = serviceIf->AddDevice(serviceIf, attribute); - if (ret != HDF_SUCCESS) { - HDF_LOGE("Dispatch failed, add service failed and ret is %{public}d", ret); - } - DeviceSerializedAttributeRelease(attribute); + ret = DispatchAddDevice(serviceIf, data, reply); break; } case DEVHOST_SERVICE_DEL_DEVICE: { - if ((serviceIf == NULL) || (serviceIf->DelDevice == NULL)) { - HDF_LOGE("serviceIf or serviceIf->DelDevice is NULL"); - break; - } - if (!HdfSbufReadUint32(data, &deviceId)) { - HDF_LOGE("failed to del device, invalid device id"); - break; - } - - HDF_LOGI("del device 0x%{public}x", deviceId); - ret = serviceIf->DelDevice(serviceIf, deviceId); - if (ret != HDF_SUCCESS) { - HDF_LOGE("del service failed, ret is %{public}d", ret); - } + ret = DispatchDelDevice(serviceIf, data, reply); break; } default: { @@ -103,8 +116,11 @@ static void DevHostServiceStubConstruct(struct DevHostServiceStub *inst) struct HdfObject *DevHostServiceStubCreate(void) { - struct DevHostServiceStub *instance = - (struct DevHostServiceStub *)OsalMemCalloc(sizeof(struct DevHostServiceStub)); + static struct DevHostServiceStub *instance = NULL; + if (instance != NULL) { + return (struct HdfObject *)&instance->super; + } + instance = (struct DevHostServiceStub *)OsalMemCalloc(sizeof(struct DevHostServiceStub)); if (instance != NULL) { DevHostServiceStubConstruct(instance); return (struct HdfObject *)&instance->super; diff --git a/uhdf2/host/src/device_service_stub.c b/uhdf2/host/src/device_service_stub.c index 78267aa427ecf2e430f04b6337c0776f3664d9f7..9f39a5e7118fa21a9d52234edec100c0e563b68e 100644 --- a/uhdf2/host/src/device_service_stub.c +++ b/uhdf2/host/src/device_service_stub.c @@ -71,6 +71,11 @@ int DeviceServiceStubPublishService(struct HdfDeviceNode *service) } do { + if (service->interfaceDesc != NULL && + !HdfRemoteServiceSetInterfaceDesc(fullService->remote, service->interfaceDesc)) { + HDF_LOGE("failed to set device service interface desc"); + break; + } struct DevSvcManagerClnt *serviceManager = DevSvcManagerClntGetInstance(); if (serviceManager == NULL) { HDF_LOGE("device service stub failed to publish, svcmgr clnt invalid"); @@ -138,3 +143,12 @@ void DeviceServiceStubRelease(struct HdfObject *object) } } +bool HdfDeviceObjectCheckInterfaceDesc(struct HdfDeviceObject *dev, struct HdfSBuf *data) +{ + if (dev == NULL || data == NULL) { + return false; + } + struct HdfDeviceNode *devNode = CONTAINER_OF(dev, struct HdfDeviceNode, deviceObject); + struct DeviceServiceStub *instance = (struct DeviceServiceStub *)devNode; + return HdfRemoteServiceCheckInterfaceToken(instance->remote, data); +} \ No newline at end of file diff --git a/uhdf2/host/src/device_token_stub.c b/uhdf2/host/src/device_token_stub.c old mode 100755 new mode 100644 diff --git a/uhdf2/host/src/devmgr_service_proxy.c b/uhdf2/host/src/devmgr_service_proxy.c index 49def50ed1a096270f3e75b6d140192cad669668..23af0b7fc1daff8e6654ae4d332353f03c96c06f 100644 --- a/uhdf2/host/src/devmgr_service_proxy.c +++ b/uhdf2/host/src/devmgr_service_proxy.c @@ -15,9 +15,9 @@ #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 "devmgr_service_stub.h" #include "devsvc_manager_clnt.h" #include "hdf_base.h" #include "hdf_log.h" @@ -26,8 +26,7 @@ #define HDF_LOG_TAG devmgr_service_proxy -int DevmgrServiceProxyAttachDeviceHost( - struct IDevmgrService *inst, uint16_t hostId, struct IDevHostService *service) +int DevmgrServiceProxyAttachDeviceHost(struct IDevmgrService *inst, uint16_t hostId, struct IDevHostService *service) { int status = HDF_FAILURE; struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); @@ -38,15 +37,19 @@ int DevmgrServiceProxyAttachDeviceHost( struct DevHostServiceStub *hostStub = (struct DevHostServiceStub *)service; if ((serviceProxy->remote == NULL) || (data == NULL) || (reply == NULL)) { HDF_LOGE("DevmgrServiceProxyAttachDeviceHost failed, host id is %{public}u", hostId); - goto finished; + goto FINISHED; } remoteService = serviceProxy->remote; dipatcher = remoteService->dispatcher; - HdfSbufWriteInt32(data, hostId); - HdfSbufWriteRemoteService(data, hostStub->remote); + if (!HdfRemoteServiceWriteInterfaceToken(remoteService, data) || + !HdfSbufWriteInt32(data, hostId) || + HdfSbufWriteRemoteService(data, hostStub->remote) != HDF_SUCCESS) { + HDF_LOGE("failed to attach host, write sbuf error"); + goto FINISHED; + } status = dipatcher->Dispatch(remoteService, DEVMGR_SERVICE_ATTACH_DEVICE_HOST, data, reply); HDF_LOGI("Attach device host dispatch finish, status is %{public}d", status); -finished: +FINISHED: if (reply != NULL) { HdfSbufRecycle(reply); } @@ -56,22 +59,23 @@ finished: return status; } -int DevmgrServiceProxyAttachDevice( - struct IDevmgrService *inst, struct IHdfDeviceToken *token) +int DevmgrServiceProxyAttachDevice(struct IDevmgrService *inst, struct IHdfDeviceToken *token) { int status = HDF_FAILURE; struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfSBuf *reply = HdfSbufTypedObtain(SBUF_IPC); struct DevmgrServiceProxy *serviceProxy = (struct DevmgrServiceProxy *)inst; - if (serviceProxy == NULL || serviceProxy->remote == NULL || data == NULL || - reply == NULL || token == NULL) { + if (serviceProxy == NULL || serviceProxy->remote == NULL || data == NULL || reply == NULL || token == NULL) { HDF_LOGE("DevmgrServiceProxyAttachDevice failed"); - goto finished; + goto FINISHED; } struct HdfRemoteService *remoteService = serviceProxy->remote; - HdfSbufWriteInt32(data, token->devid); + if (!HdfRemoteServiceWriteInterfaceToken(remoteService, data) || !HdfSbufWriteInt32(data, token->devid)) { + goto FINISHED; + } + status = remoteService->dispatcher->Dispatch(remoteService, DEVMGR_SERVICE_ATTACH_DEVICE, data, reply); -finished: +FINISHED: if (reply != NULL) { HdfSbufRecycle(reply); } @@ -89,12 +93,15 @@ int DevmgrServiceProxyDetachDevice(struct IDevmgrService *inst, devid_t devid) struct DevmgrServiceProxy *serviceProxy = (struct DevmgrServiceProxy *)inst; if (serviceProxy == NULL || serviceProxy->remote == NULL || data == NULL || reply == NULL) { HDF_LOGE("DevmgrServiceProxyDetachDevice failed"); - goto finished; + goto FINISHED; } struct HdfRemoteService *remoteService = serviceProxy->remote; - HdfSbufWriteInt32(data, devid); + if (!HdfRemoteServiceWriteInterfaceToken(remoteService, data) || !HdfSbufWriteInt32(data, devid)) { + goto FINISHED; + } + status = remoteService->dispatcher->Dispatch(remoteService, DEVMGR_SERVICE_DETACH_DEVICE, data, reply); -finished: +FINISHED: if (reply != NULL) { HdfSbufRecycle(reply); } @@ -103,9 +110,8 @@ finished: } return status; } -int DevmgrServiceProxyLoadDevice(struct IDevmgrService *inst, const char *svcName) +int DevmgrServiceProxyLoadDevice(struct IDevmgrService *inst, const char *svcName) { - int status = HDF_FAILURE; struct DevmgrServiceProxy *serviceProxy = (struct DevmgrServiceProxy *)inst; if (serviceProxy == NULL || serviceProxy->remote == NULL || svcName == NULL) { HDF_LOGE("DevmgrServiceProxyLoadDevice failed"); @@ -114,17 +120,18 @@ int DevmgrServiceProxyLoadDevice(struct IDevmgrService *inst, const char *svcNa struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfRemoteService *remoteService = serviceProxy->remote; - HdfSbufWriteString(data, svcName); - - status = remoteService->dispatcher->Dispatch(remoteService, DEVMGR_SERVICE_LOAD_DEVICE, data, NULL); + if (!HdfRemoteServiceWriteInterfaceToken(remoteService, data) || !HdfSbufWriteString(data, svcName)) { + HdfSbufRecycle(data); + return HDF_FAILURE; + } + int status = remoteService->dispatcher->Dispatch(remoteService, DEVMGR_SERVICE_LOAD_DEVICE, data, NULL); HdfSbufRecycle(data); return status; } -int DevmgrServiceProxyUnLoadDevice(struct IDevmgrService *inst, const char *svcName) +int DevmgrServiceProxyUnLoadDevice(struct IDevmgrService *inst, const char *svcName) { - int status = HDF_FAILURE; struct DevmgrServiceProxy *serviceProxy = (struct DevmgrServiceProxy *)inst; if (serviceProxy == NULL || serviceProxy->remote == NULL || svcName == NULL) { HDF_LOGE("DevmgrServiceProxyLoadDevice failed"); @@ -133,9 +140,12 @@ int DevmgrServiceProxyUnLoadDevice(struct IDevmgrService *inst, const char *svc struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC); struct HdfRemoteService *remoteService = serviceProxy->remote; - HdfSbufWriteString(data, svcName); + if (!HdfRemoteServiceWriteInterfaceToken(remoteService, data) || !HdfSbufWriteString(data, svcName)) { + HdfSbufRecycle(data); + return HDF_FAILURE; + } - status = remoteService->dispatcher->Dispatch(remoteService, DEVMGR_SERVICE_UNLOAD_DEVICE, data, NULL); + int status = remoteService->dispatcher->Dispatch(remoteService, DEVMGR_SERVICE_UNLOAD_DEVICE, data, NULL); HdfSbufRecycle(data); return status; @@ -152,7 +162,6 @@ static void DevmgrServiceProxyConstruct(struct DevmgrServiceProxy *inst) pvtbl->StartService = NULL; } - static struct IDevmgrService *DevmgrServiceProxyObtain(struct HdfRemoteService *service) { if (service != NULL) { @@ -171,25 +180,31 @@ static struct IDevmgrService *DevmgrServiceProxyObtain(struct HdfRemoteService * struct HdfObject *DevmgrServiceProxyCreate(void) { 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!!!"); + if (instance != NULL) { + return (struct HdfObject *)instance; + } + 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; + } + struct HdfRemoteService *remote = + (struct HdfRemoteService *)serviceManagerIf->GetService(serviceManagerIf, DEVICE_MANAGER_SERVICE); + if (remote != NULL) { + if (!HdfRemoteServiceSetInterfaceDesc(remote, "HDI.IDeviceManager.V1_0")) { + HDF_LOGE("%{public}s: failed to init interface desc", __func__); + HdfRemoteServiceRecycle(remote); return NULL; - } else { - struct HdfRemoteService *remote = (struct HdfRemoteService *) - serviceManagerIf->GetService(serviceManagerIf, DEVICE_MANAGER_SERVICE); - if (remote != NULL) { - instance = DevmgrServiceProxyObtain(remote); - } } + instance = DevmgrServiceProxyObtain(remote); } + return (struct HdfObject *)instance; } diff --git a/uhdf2/host/src/devsvc_manager_proxy.c b/uhdf2/host/src/devsvc_manager_proxy.c index 9779693c3309725cff5bcf3b29161862ccf00dce..ccb9d3cfeb94ea4b6e347f533125ae99c3d0c138 100644 --- a/uhdf2/host/src/devsvc_manager_proxy.c +++ b/uhdf2/host/src/devsvc_manager_proxy.c @@ -14,6 +14,8 @@ */ #include "devsvc_manager_proxy.h" +#include "devhost_service.h" +#include "devhost_service_full.h" #include "device_service_stub.h" #include "devsvc_manager_stub.h" #include "hdf_base.h" @@ -23,8 +25,8 @@ #define HDF_LOG_TAG devsvc_manager_proxy -static int WriteServiceInfo(struct HdfSBuf *data, const char *svcName, - uint16_t devClass, struct HdfDeviceObject *service, const char *servInfo) +static int WriteServiceInfo( + struct HdfSBuf *data, const char *svcName, uint16_t devClass, struct HdfDeviceObject *service, const char *servInfo) { int ret = HDF_FAILURE; if (!HdfSbufWriteString(data, svcName)) { @@ -37,8 +39,8 @@ static int WriteServiceInfo(struct HdfSBuf *data, const char *svcName, return ret; } - struct HdfDeviceNode *devNode = HDF_SLIST_CONTAINER_OF( - struct HdfDeviceObject, service, struct HdfDeviceNode, deviceObject); + 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("%{public}s: device service is broken", __func__); @@ -58,8 +60,8 @@ static int WriteServiceInfo(struct HdfSBuf *data, const char *svcName, return HDF_SUCCESS; } -static int DevSvcManagerProxyAddService( struct IDevSvcManager *inst, const char *svcName, - uint16_t devClass, struct HdfDeviceObject *service, const char *servInfo) +static int DevSvcManagerProxyAddService(struct IDevSvcManager *inst, const char *svcName, uint16_t devClass, + struct HdfDeviceObject *service, const char *servInfo) { struct DevSvcManagerProxy *serviceProxy = (struct DevSvcManagerProxy *)inst; if (service == NULL || svcName == NULL) { @@ -84,11 +86,12 @@ static int DevSvcManagerProxyAddService( struct IDevSvcManager *inst, const c HDF_LOGE("Add service failed, failed to obtain sbuf"); break; } - if (WriteServiceInfo(data, svcName, devClass, service, servInfo) != HDF_SUCCESS) { + if (!HdfRemoteServiceWriteInterfaceToken(serviceProxy->remote, data) || + WriteServiceInfo(data, svcName, devClass, service, servInfo) != HDF_SUCCESS) { break; } - status = serviceProxy->remote->dispatcher->Dispatch( - serviceProxy->remote, DEVSVC_MANAGER_ADD_SERVICE, data, reply); + status = + serviceProxy->remote->dispatcher->Dispatch(serviceProxy->remote, DEVSVC_MANAGER_ADD_SERVICE, data, reply); HDF_LOGI("servmgr add service %{public}s, result is %{public}d", svcName, status); } while (0); @@ -101,8 +104,8 @@ static int DevSvcManagerProxyAddService( struct IDevSvcManager *inst, const c return status; } -static int DevSvcManagerProxyUpdateService( struct IDevSvcManager *inst, const char *svcName, - uint16_t devClass, struct HdfDeviceObject *service, const char *servInfo) +static int DevSvcManagerProxyUpdateService(struct IDevSvcManager *inst, const char *svcName, uint16_t devClass, + struct HdfDeviceObject *service, const char *servInfo) { struct DevSvcManagerProxy *serviceProxy = (struct DevSvcManagerProxy *)inst; if (service == NULL || svcName == NULL) { @@ -127,7 +130,8 @@ static int DevSvcManagerProxyUpdateService( struct IDevSvcManager *inst, cons HDF_LOGE("Add service failed, failed to obtain sbuf"); break; } - if (WriteServiceInfo(data, svcName, devClass, service, servInfo) != HDF_SUCCESS) { + if (!HdfRemoteServiceWriteInterfaceToken(serviceProxy->remote, data) || + WriteServiceInfo(data, svcName, devClass, service, servInfo) != HDF_SUCCESS) { break; } status = serviceProxy->remote->dispatcher->Dispatch( @@ -152,17 +156,21 @@ struct HdfObject *DevSvcManagerProxyGetService(struct IDevSvcManager *inst, cons 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: + do { + if ((serviceProxy->remote == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("Get service failed, serviceProxy->remote or data or reply is null"); + break; + } + dispatcher = serviceProxy->remote->dispatcher; + if (!HdfRemoteServiceWriteInterfaceToken(serviceProxy->remote, data) || !HdfSbufWriteString(data, svcName)) { + break; + } + status = dispatcher->Dispatch(serviceProxy->remote, DEVSVC_MANAGER_GET_SERVICE, data, reply); + if (status == HDF_SUCCESS) { + remoteService = HdfSbufReadRemoteService(reply); + } + } while (0); + if (reply != NULL) { HdfSbufRecycle(reply); } @@ -183,16 +191,21 @@ void DevSvcManagerProxyRemoveService(struct IDevSvcManager *inst, const char *sv 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 %{public}d", status); -FINISHED: + + do { + if ((serviceProxy->remote == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("Remove service failed, serviceProxy->remote or data or reply is null"); + break; + } + remoteService = serviceProxy->remote; + dispatcher = remoteService->dispatcher; + if (!HdfRemoteServiceWriteInterfaceToken(serviceProxy->remote, data) || !HdfSbufWriteString(data, svcName)) { + break; + } + int status = dispatcher->Dispatch(remoteService, DEVSVC_MANAGER_REMOVE_SERVICE, data, reply); + HDF_LOGW("Device service manager proxy remove service status is %{public}d", status); + } while (0); + if (reply != NULL) { HdfSbufRecycle(reply); } @@ -201,6 +214,23 @@ FINISHED: } } +static void DevSvcManagerProxyOnRemoteDied(struct HdfDeathRecipient *recipient, struct HdfRemoteService *service) +{ + struct IDevHostService *instance = DevHostServiceNewInstance(0, NULL); + + if (recipient == NULL || service == NULL || instance == NULL) { + HDF_LOGE("%{public}s parameter is null", __func__); + return; + } + + struct DevHostServiceFull *fullService = (struct DevHostServiceFull *)instance; + struct HdfMessageLooper *looper = &fullService->looper; + HDF_LOGW("%{public}s: DevSvcManager dead, host %{public}d stop", __func__, fullService->super.hostId); + if ((looper != NULL) && (looper->Stop != NULL)) { + looper->Stop(looper); + } +} + void DevSvcManagerProxyConstruct(struct DevSvcManagerProxy *inst, struct HdfRemoteService *remote) { inst->pvtbl.AddService = DevSvcManagerProxyAddService; @@ -208,12 +238,13 @@ void DevSvcManagerProxyConstruct(struct DevSvcManagerProxy *inst, struct HdfRemo inst->pvtbl.GetService = DevSvcManagerProxyGetService; inst->pvtbl.RemoveService = DevSvcManagerProxyRemoveService; inst->remote = remote; + inst->recipient.OnRemoteDied = DevSvcManagerProxyOnRemoteDied; + HdfRemoteServiceAddDeathRecipient(remote, &inst->recipient); } static struct IDevSvcManager *DevSvcManagerProxyObtain(struct HdfRemoteService *remote) { - struct DevSvcManagerProxy *instance = - (struct DevSvcManagerProxy *)OsalMemCalloc(sizeof(struct DevSvcManagerProxy)); + struct DevSvcManagerProxy *instance = (struct DevSvcManagerProxy *)OsalMemCalloc(sizeof(struct DevSvcManagerProxy)); if (instance != NULL) { DevSvcManagerProxyConstruct(instance, remote); } @@ -226,6 +257,11 @@ struct HdfObject *DevSvcManagerProxyCreate() if (instance == NULL) { struct HdfRemoteService *remote = HdfRemoteServiceGet(DEVICE_SERVICE_MANAGER_SA_ID); if (remote != NULL) { + if (!HdfRemoteServiceSetInterfaceDesc(remote, "HDI.IServiceManager.V1_0")) { + HDF_LOGE("%{public}s: failed to init interface desc", __func__); + HdfRemoteServiceRecycle(remote); + return NULL; + } instance = DevSvcManagerProxyObtain(remote); } } diff --git a/uhdf2/host/src/driver_loader_full.c b/uhdf2/host/src/driver_loader_full.c old mode 100755 new mode 100644 index 88187301fcad4cd62ff3677daec016d45d7430e0..29f9c04aa192a03f3e580d2d1deeb4cc24851de3 --- a/uhdf2/host/src/driver_loader_full.c +++ b/uhdf2/host/src/driver_loader_full.c @@ -90,11 +90,6 @@ void HdfDriverLoaderFullReclaimDriver(struct HdfDriver *driver) return; } - if (driver->priv != NULL) { - dlclose(driver->priv); - driver->priv = NULL; - } - OsalMemFree(driver); } diff --git a/uhdf2/host/src/hdf_device_thread.c b/uhdf2/host/src/hdf_device_thread.c index 9bb01a65015993ef480cdf4e2146dbe629690a8f..44514aeb52bf86dedd20a92e93510ccf9e968d83 100644 --- a/uhdf2/host/src/hdf_device_thread.c +++ b/uhdf2/host/src/hdf_device_thread.c @@ -49,7 +49,7 @@ int32_t DeviceThreadMessageHandler(struct HdfMessageTask *task, struct HdfMessag int DeviceThreadAttach(struct DeviceThread *inst, struct IHdfDevice *device, struct HdfDeviceNode *service) { - if (inst != NULL || device == NULL || service == NULL) { + if (inst == NULL || device == NULL || service == NULL) { return HDF_ERR_INVALID_PARAM; } diff --git a/uhdf2/host/src/hdf_pm_reg.c b/uhdf2/host/src/hdf_pm_reg.c old mode 100755 new mode 100644 diff --git a/uhdf2/host/test/BUILD.gn b/uhdf2/host/test/BUILD.gn old mode 100755 new mode 100644 diff --git a/uhdf2/host/test/unittest/sample_driver/sample_driver.cpp b/uhdf2/host/test/unittest/sample_driver/sample_driver.cpp index 410429d6d8fc79aed825abb4e6e7b2b19437edf7..d0f535f51419de79b36dfe308c134859cac96c2b 100644 --- a/uhdf2/host/test/unittest/sample_driver/sample_driver.cpp +++ b/uhdf2/host/test/unittest/sample_driver/sample_driver.cpp @@ -40,6 +40,11 @@ static int HdfSampleDriverBind(struct HdfDeviceObject *deviceObject) .Dispatch = SampleServiceDispatch, .Release = nullptr, }; + int ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, "hdf.test.sampele_service"); + if (ret != HDF_SUCCESS) { + HDF_LOGE("failed to set interface desc"); + return ret; + } deviceObject->service = &testService; return HDF_SUCCESS; } diff --git a/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.cpp b/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.cpp index fa59a49fad81e56b38a59d12b14bc5cdcd7d693d..48fb083dd4e2492708e714e9b4c322435d0aab13 100644 --- a/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.cpp +++ b/uhdf2/host/test/unittest/sample_driver/sample_hdi_service_stub.cpp @@ -14,9 +14,11 @@ */ #include +#include #include #include #include + #include "hdf_sbuf_ipc.h" #include "sample_hdi.h" @@ -24,7 +26,10 @@ using OHOS::HDI::Base::SharedMemQueueMeta; static int32_t SampleServiceStubPing(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) { - char *outInfo = NULL; + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } + char *outInfo = nullptr; const char *info = HdfSbufReadString(data); int32_t ret = SampleHdiImplInstance()->ping(client->device, info, &outInfo); @@ -39,7 +44,9 @@ static int32_t SampleServiceStubSum(struct HdfDeviceIoClient *client, struct Hdf int32_t parm0; int32_t parm1; int32_t result; - + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } if (!HdfSbufReadInt32(data, &parm0)) { HDF_LOGE("SampleHdi.sum: miss parm0"); return HDF_ERR_INVALID_PARAM; @@ -64,13 +71,16 @@ static int32_t SampleServiceStubSum(struct HdfDeviceIoClient *client, struct Hdf static int32_t SampleServiceStubCallback(struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) { int32_t code; + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } if (!HdfSbufReadInt32(data, &code)) { HDF_LOGE("SampleHdi.callback: miss parameter code"); return HDF_ERR_INVALID_PARAM; } struct HdfRemoteService *callback = HdfSbufReadRemoteService(data); - if (callback == NULL) { + if (callback == nullptr) { HDF_LOGE("SampleHdi.callback: miss parameter callback"); return HDF_ERR_INVALID_PARAM; } @@ -81,8 +91,11 @@ static int32_t SampleServiceStubStructTrans( struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) { HDF_LOGI("SampleServiceStubStructTrans: in"); + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } struct DataBlock *dataBlock = DataBlockBlockUnmarshalling(data); - if (dataBlock == NULL) { + if (dataBlock == nullptr) { HDF_LOGE("SampleServiceStubStructTrans: failed to read dataBlock"); return HDF_ERR_INVALID_PARAM; } @@ -102,10 +115,12 @@ static int32_t SampleServiceStubBufferTrans( struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) { HDF_LOGI("SampleServiceStubBufferTrans: in"); - + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } constexpr int SAMPLE_TEST_BUFFER_SIZE = 10; const uint8_t *buffer = HdfSbufReadUnpadBuffer(data, SAMPLE_TEST_BUFFER_SIZE); - if (buffer == NULL) { + if (buffer == nullptr) { HDF_LOGI("SampleServiceStubBufferTrans: read buffer failed"); return HDF_ERR_INVALID_PARAM; } @@ -123,9 +138,12 @@ static int32_t SampleServiceStubBufferTrans( static int32_t SampleServiceRegisterDevice( struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) { + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } (void)reply; const char *deviceName = HdfSbufReadString(data); - if (deviceName == NULL) { + if (deviceName == nullptr) { return HDF_ERR_INVALID_PARAM; } @@ -136,8 +154,11 @@ static int32_t SampleServiceUnregisterDevice( struct HdfDeviceIoClient *client, struct HdfSBuf *data, struct HdfSBuf *reply) { (void)reply; + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } const char *deviceName = HdfSbufReadString(data); - if (deviceName == NULL) { + if (deviceName == nullptr) { return HDF_ERR_INVALID_PARAM; } @@ -146,8 +167,11 @@ static int32_t SampleServiceUnregisterDevice( static int32_t SampleServiceUpdateDevice(struct HdfDeviceIoClient *client, struct HdfSBuf *data) { + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } const char *servInfo = HdfSbufReadString(data); - if (servInfo == NULL) { + if (servInfo == nullptr) { return HDF_ERR_INVALID_PARAM; } @@ -156,6 +180,9 @@ static int32_t SampleServiceUpdateDevice(struct HdfDeviceIoClient *client, struc static int32_t SampleServiceSmqTrans(struct HdfDeviceIoClient *client, struct HdfSBuf *data) { + if (!HdfDeviceObjectCheckInterfaceDesc(client->device, data)) { + return HDF_ERR_INVALID_PARAM; + } OHOS::MessageParcel *parcel = nullptr; if (SbufToParcel(data, &parcel) != HDF_SUCCESS) { return HDF_FAILURE; diff --git a/uhdf2/include/hdi/base/hdi_smq.h b/uhdf2/include/hdi/base/hdi_smq.h index 9fe894d0954d89cdccb3aced24d97ac218e3755a..345f9fc35b41e757603d43a21313420f49a94d93 100644 --- a/uhdf2/include/hdi/base/hdi_smq.h +++ b/uhdf2/include/hdi/base/hdi_smq.h @@ -99,7 +99,7 @@ SharedMemQueue::SharedMemQueue(uint32_t elementCount, SmqType type) : aligned } meta_ = std::make_shared>(elementCount, type); - HDF_LOGI("create SharedMemQueue, count=%{public}u, size=%{public}u", elementCount, meta_->GetSize()); + HDF_LOGI("create SharedMemQueue, count=%{public}u, size=%{public}zu", elementCount, meta_->GetSize()); int ashmemFd = AshmemCreate("hdi_smq", Align(meta_->GetSize(), PAGE_SIZE)); if (ashmemFd < 0) { HDF_LOGE("failed to create ashmem"); @@ -124,6 +124,7 @@ SharedMemQueue::~SharedMemQueue() UnMapMemZone(readOffset_, SharedMemQueueMeta::MemZoneType::MEMZONE_RPTR); } else { delete readOffset_; + readOffset_ = nullptr; } if (writeOffset_ != nullptr) { diff --git a/uhdf2/include/hdi/hdi_base.h b/uhdf2/include/hdi/hdi_base.h new file mode 100644 index 0000000000000000000000000000000000000000..c896db2f57b442ed69e0cc740ad3d324bb7447f1 --- /dev/null +++ b/uhdf2/include/hdi/hdi_base.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 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_BASE_INTERFACE_H +#define HDI_BASE_INTERFACE_H + +#include +#include + +namespace OHOS { +namespace HDI { +class HdiBase : virtual public OHOS::RefBase { +public: + HdiBase() = default; + virtual ~HdiBase() = default; +}; + +#define DECLARE_HDI_DESCRIPTOR(DESCRIPTOR) \ + const static inline std::u16string metaDescriptor_ = {DESCRIPTOR}; \ + const static inline std::u16string &GetDescriptor() \ + { \ + return metaDescriptor_; \ + } +} // namespace HDI +} // namespace OHOS + +#endif // HDI_BASE_INTERFACE_H diff --git a/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h b/uhdf2/include/hdi/hdi_support.h similarity index 72% rename from uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h rename to uhdf2/include/hdi/hdi_support.h index b5c3cc89669b77d6398e64dfdb7919361e22b3bc..b2a0ddae0e50d4399f54e1324201b1c7c189d93b 100644 --- a/uhdf2/hdi/test/reg_pnp_device/unittest/sample_driver/hdf_pnp_driver.h +++ b/uhdf2/include/hdi/hdi_support.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,25 +13,20 @@ * limitations under the License. */ -#ifndef HDF_PNP_DRIVER_H -#define HDF_PNP_DRIVER_H +#ifndef HDF_HDI_SUPPORT_H +#define HDF_HDI_SUPPORT_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, -}; - +void *LoadHdiImpl(const char *desc, const char *serviceName); +void UnloadHdiImpl(const char *desc, const char *serviceName, void *impl); #ifdef __cplusplus } #endif /* __cplusplus */ -#endif /* HDF_PNP_DRIVER_H */ +#endif /* HDF_HDI_SUPPORT_H */ diff --git a/uhdf2/include/hdi/idevmgr_hdi.h b/uhdf2/include/hdi/idevmgr_hdi.h index 6432506947dd2f923409ce1fbcefee0480fb5587..8e2abe08a2a8a2e270e97800d31483611c614405 100644 --- a/uhdf2/include/hdi/idevmgr_hdi.h +++ b/uhdf2/include/hdi/idevmgr_hdi.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -15,19 +15,28 @@ #ifndef HDI_DEVICE_MANAGER_HDI_INF_H #define HDI_DEVICE_MANAGER_HDI_INF_H -#include -#include +#include +#include namespace OHOS { namespace HDI { namespace DeviceManager { namespace V1_0 { -class IDeviceManager : public IRemoteBroker { +struct HdiDevHostInfo { + std::string hostName; + uint32_t hostId; + std::vector devId; +}; + +class IDeviceManager : public HdiBase { public: - DECLARE_INTERFACE_DESCRIPTOR(u"HDI.IDeviceManager.V1_0"); + DECLARE_HDI_DESCRIPTOR(u"HDI.IDeviceManager.V1_0"); + IDeviceManager() = default; + virtual ~IDeviceManager() = default; static ::OHOS::sptr Get(); virtual int32_t LoadDevice(const std::string &serviceName) = 0; virtual int32_t UnloadDevice(const std::string &serviceName) = 0; + virtual int32_t ListAllDevice(std::vector &deviceInfos) = 0; }; } // namespace V1_0 } // namespace DeviceManager diff --git a/uhdf2/include/hdi/iservmgr_hdi.h b/uhdf2/include/hdi/iservmgr_hdi.h index 5ce2c9e6fa1b9173d520356046e4af76448ff82a..1cb74144ef44d6c139fefab769f61a2060629e6e 100644 --- a/uhdf2/include/hdi/iservmgr_hdi.h +++ b/uhdf2/include/hdi/iservmgr_hdi.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -15,19 +15,28 @@ #ifndef HDI_ISERVICE_MANAGER_INF_H #define HDI_ISERVICE_MANAGER_INF_H -#include +#include +#include #include +#include + #include "iservstat_listener_hdi.h" namespace OHOS { namespace HDI { namespace ServiceManager { namespace V1_0 { -class IServiceManager : public IRemoteBroker { +struct HdiServiceInfo { + std::string serviceName; + uint16_t devClass; +}; + +class IServiceManager : public HdiBase { public: - DECLARE_INTERFACE_DESCRIPTOR(u"HDI.IServiceManager.V1_0"); + DECLARE_HDI_DESCRIPTOR(u"HDI.IServiceManager.V1_0"); static ::OHOS::sptr Get(); - virtual ::OHOS::sptr GetService(const char* serviceName) = 0; + virtual ::OHOS::sptr GetService(const char *serviceName) = 0; + virtual int32_t ListAllService(std::vector &serviceInfos) = 0; virtual int32_t RegisterServiceStatusListener(::OHOS::sptr listener, uint16_t deviceClass) = 0; virtual int32_t UnregisterServiceStatusListener(::OHOS::sptr listener) = 0; }; diff --git a/uhdf2/ipc/include/hdf_remote_adapter.h b/uhdf2/ipc/include/hdf_remote_adapter.h index 1931539d3afc23744533c5eed9712a3707fface4..44dfce49d4bca98b0e6b3c7a96b6b6000c7a32b5 100644 --- a/uhdf2/ipc/include/hdf_remote_adapter.h +++ b/uhdf2/ipc/include/hdf_remote_adapter.h @@ -18,8 +18,10 @@ #include #include -#include "hdf_remote_service.h" +#include + #include "hdf_remote_adapter_if.h" +#include "hdf_remote_service.h" #ifdef __cplusplus extern "C" { @@ -27,7 +29,7 @@ extern "C" { class HdfRemoteServiceStub : public OHOS::IPCObjectStub { public: - HdfRemoteServiceStub(struct HdfRemoteService *service); + explicit HdfRemoteServiceStub(struct HdfRemoteService *service); int OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option) override; ~HdfRemoteServiceStub(); @@ -48,9 +50,13 @@ private: struct HdfRemoteServiceHolder { HdfRemoteServiceHolder(); + ~HdfRemoteServiceHolder() = default; + + bool SetInterfaceDescriptor(const char *desc); struct HdfRemoteService service_; OHOS::sptr remote_; OHOS::sptr deathRecipient_; + std::u16string descriptor_; }; struct HdfRemoteService *HdfRemoteAdapterBind(OHOS::sptr binder); diff --git a/uhdf2/ipc/include/hdf_remote_adapter_if.h b/uhdf2/ipc/include/hdf_remote_adapter_if.h index 742c5844684941018d61f131196e5d57146a4f8f..27f044929e1078c6fe4f0fb613edaea71c6c420f 100644 --- a/uhdf2/ipc/include/hdf_remote_adapter_if.h +++ b/uhdf2/ipc/include/hdf_remote_adapter_if.h @@ -16,6 +16,7 @@ #ifndef HDF_REMOTE_ADAPATER_IF_H #define HDF_REMOTE_ADAPATER_IF_H +#include #include "hdf_remote_service.h" #ifdef __cplusplus @@ -38,6 +39,16 @@ void HdfRemoteAdapterAddDeathRecipient(struct HdfRemoteService *service, struct void HdfRemoteAdapterRemoveDeathRecipient(struct HdfRemoteService *service, struct HdfDeathRecipient *recipient); +bool HdfRemoteAdapterSetInterfaceDesc(struct HdfRemoteService *service, const char *descriptor); + +bool HdfRemoteAdapterWriteInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data); + +bool HdfRemoteAdapterCheckInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data); + +pid_t HdfRemoteGetCallingPid(void); + +pid_t HdfRemoteGetCallingUid(void); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/uhdf2/ipc/include/iproxy_broker.h b/uhdf2/ipc/include/iproxy_broker.h new file mode 100644 index 0000000000000000000000000000000000000000..182d32dc74c1246f9769bfd0591cce9a4ff893dd --- /dev/null +++ b/uhdf2/ipc/include/iproxy_broker.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022 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_PROXY_BROKER_H +#define HDI_PROXY_BROKER_H + +#include +#include +#include + +namespace OHOS { +namespace HDI { +template +class IProxyBroker : public OHOS::PeerHolder, public OHOS::IRemoteBroker, public INTERFACE { +public: + explicit IProxyBroker(const sptr &object); + virtual ~IProxyBroker() = default; + virtual sptr AsInterface(); + sptr AsObject() override; +}; + +template +IProxyBroker::IProxyBroker(const sptr &object) : PeerHolder(object) +{ +} + +template +sptr IProxyBroker::AsInterface() +{ + return this; +} + +template +sptr IProxyBroker::AsObject() +{ + return Remote(); +} + +template +inline sptr hdi_facecast(const sptr &object) +{ + const std::u16string descriptor = INTERFACE::GetDescriptor(); + BrokerRegistration ®istration = BrokerRegistration::Get(); + sptr broker = registration.NewInstance(descriptor, object); + INTERFACE *proxyBroker = static_cast *>(broker.GetRefPtr()); + return static_cast(proxyBroker); +} + +template +inline sptr hdi_objcast(const sptr &iface) +{ + IProxyBroker *brokerObject = static_cast *>(iface.GetRefPtr()); + return brokerObject->AsObject(); +} +} // namespace HDI +} // namespace OHOS + +#endif // HDI_PROXY_BROKER_H diff --git a/uhdf2/ipc/include/object_collector.h b/uhdf2/ipc/include/object_collector.h new file mode 100644 index 0000000000000000000000000000000000000000..b95746b3b97c40c30aa8d6e51c796e5dbbaa1e0c --- /dev/null +++ b/uhdf2/ipc/include/object_collector.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022 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_OBJECT_COLLECTOR_H +#define HDI_OBJECT_COLLECTOR_H + +#include +#include +#include +#include +#include +#include + +namespace OHOS { +namespace HDI { +class ObjectCollector { +public: + using Constructor = std::function(const sptr &interface)>; + + static ObjectCollector &GetInstance(); + + bool ConstructorRegister(const std::u16string &interfaceName, const Constructor &constructor); + void ConstructorUnRegister(const std::u16string &interfaceName); + sptr NewObject(const sptr &interface, const std::u16string &interfaceName); + sptr GetOrNewObject(const sptr &interface, const std::u16string &interfaceName); + bool RemoveObject(const sptr &interface); + +private: + ObjectCollector() = default; + sptr NewObjectLocked(const sptr &interface, const std::u16string &interfaceName); + std::map constructorMapper_; + std::map interfaceObjectCollector_; + std::mutex mutex_; +}; + +template +class ObjectDelegator { +public: + ObjectDelegator(); + ~ObjectDelegator(); + +private: + ObjectDelegator(const ObjectDelegator &) = delete; + ObjectDelegator(ObjectDelegator &&) = delete; + ObjectDelegator &operator=(const ObjectDelegator &) = delete; + ObjectDelegator &operator=(ObjectDelegator &&) = delete; +}; + +template +ObjectDelegator::ObjectDelegator() +{ + ObjectCollector::GetInstance().ConstructorRegister( + INTERFACE::GetDescriptor(), [](const sptr &interface) -> sptr { + return new OBJECT(static_cast(interface.GetRefPtr())); + }); +} + +template +ObjectDelegator::~ObjectDelegator() +{ + ObjectCollector::GetInstance().ConstructorUnRegister(INTERFACE::GetDescriptor()); +} +} // namespace HDI +} // namespace OHOS + +#endif // HDI_OBJECT_MAPPER_H diff --git a/uhdf2/include/hdi/hdf_load_hdi.h b/uhdf2/ipc/include/stub_collector.h similarity index 51% rename from uhdf2/include/hdi/hdf_load_hdi.h rename to uhdf2/ipc/include/stub_collector.h index 184e159d9c6674ef8a8ce5de9d53ea74a91633bb..46046de2324048cfe5540cc879231a74f6d5e4fd 100644 --- a/uhdf2/include/hdi/hdf_load_hdi.h +++ b/uhdf2/ipc/include/stub_collector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -13,35 +13,26 @@ * limitations under the License. */ -#ifndef HDF_LOAD_HDI_H -#define HDF_LOAD_HDI_H +#ifndef HDI_STUB_COLLECTOR_H +#define HDI_STUB_COLLECTOR_H -#include "hdf_base.h" +#include #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -struct HdiBase { - uint32_t moduleVersion; - const char *moduleName; - int (*OpenHdi)(); - int (*CloseHdi)(); +struct StubConstructor { + struct HdfRemoteService **(*constructor)(void *); + void (*destructor)(struct HdfRemoteService **); }; - -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) +void StubConstructorRegister(const char *ifDesc, struct StubConstructor *constructor); +void StubConstructorUnregister(const char *ifDesc, struct StubConstructor *constructor); +struct HdfRemoteService **StubCollectorGetOrNewObject(const char *ifDesc, void *servPtr); +void StubCollectorRemoveObject(const char *ifDesc, void *servPtr); #ifdef __cplusplus } #endif /* __cplusplus */ -#endif /* HDF_LOAD_HDI_H */ - +#endif // HDI_STUB_COLLECTOR_H diff --git a/uhdf2/ipc/src/hdf_remote_adapter.cpp b/uhdf2/ipc/src/hdf_remote_adapter.cpp index 3533a953ae5b4ed312ae429fd50db79ec81d899c..75448ec6a0204c12f8eee4fcca9364528315cf70 100644 --- a/uhdf2/ipc/src/hdf_remote_adapter.cpp +++ b/uhdf2/ipc/src/hdf_remote_adapter.cpp @@ -13,14 +13,17 @@ * limitations under the License. */ -#include "hdf_remote_adapter.h" #include #include #include #include + #include "hdf_log.h" #include "hdf_object_manager.h" #include "hdf_sbuf_ipc.h" +#include "hdf_remote_adapter.h" + +#define HDF_LOG_TAG hdf_remote_adapter static constexpr int32_t THREAD_POOL_BASE_THREAD_COUNT = 1; static int32_t g_remoteThreadMax = THREAD_POOL_BASE_THREAD_COUNT; @@ -128,6 +131,21 @@ HdfRemoteServiceHolder::HdfRemoteServiceHolder() : remote_(nullptr), deathRecipi service_.target = nullptr; } +bool HdfRemoteServiceHolder::SetInterfaceDescriptor(const char *desc) +{ + if (desc == nullptr) { + return false; + } + std::u16string newDesc = OHOS::Str8ToStr16(std::string(desc)); + if (newDesc.empty()) { + HDF_LOGE("failed to set interface des, error on cover str8 to str16, %{public}s", desc); + return false; + } + + descriptor_.assign(newDesc); + return true; +} + void HdfRemoteAdapterAddDeathRecipient( struct HdfRemoteService *service, struct HdfDeathRecipient *recipient) { @@ -273,6 +291,14 @@ struct HdfRemoteService *HdfRemoteAdapterGetSa(int32_t saId) return nullptr; } OHOS::sptr remote = saManager->GetSystemAbility(saId); + constexpr int32_t waitTimes = 50; + constexpr int32_t sleepInterval = 20000; + int32_t timeout = waitTimes; + while (remote == nullptr && (timeout-- > 0)) { + HDF_LOGD("waiting for saId %{public}d", saId); + usleep(sleepInterval); + remote = saManager->GetSystemAbility(saId); + } if (remote != nullptr) { return HdfRemoteAdapterBind(remote); } else { @@ -280,3 +306,73 @@ struct HdfRemoteService *HdfRemoteAdapterGetSa(int32_t saId) } return nullptr; } + +bool HdfRemoteAdapterSetInterfaceDesc(struct HdfRemoteService *service, const char *desc) +{ + if (service == nullptr || desc == nullptr) { + return false; + } + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + return holder->SetInterfaceDescriptor(desc); +} + +bool HdfRemoteAdapterWriteInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data) +{ + if (service == nullptr || data == nullptr) { + return false; + } + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + OHOS::MessageParcel *parcel = nullptr; + + if (SbufToParcel(data, &parcel) != HDF_SUCCESS) { + HDF_LOGE("failed to write interface token, SbufToParcel error"); + return false; + } + + if (holder->remote_ == nullptr) { + HDF_LOGE("failed to write interface token, holder->remote is nullptr"); + return false; + } + if (holder->descriptor_.empty()) { + HDF_LOGE("failed to write interface token, empty token"); + return false; + } + return parcel->WriteInterfaceToken(holder->descriptor_); +} + +bool HdfRemoteAdapterCheckInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data) +{ + if (service == nullptr || data == nullptr) { + return false; + } + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + if (holder == nullptr || holder->remote_ == nullptr) { + return false; + } + OHOS::MessageParcel *parcel = nullptr; + + if (SbufToParcel(data, &parcel) != HDF_SUCCESS) { + return false; + } + auto desc = parcel->ReadInterfaceToken(); + if (desc.empty()) { + HDF_LOGE("failed to check interface, empty token"); + return false; + } + if (holder->descriptor_ != desc) { + HDF_LOGE("calling unknown interface: %{public}s", OHOS::Str16ToStr8(desc).c_str()); + return false; + } + + return true; +} + +pid_t HdfRemoteGetCallingPid(void) +{ + return OHOS::IPCSkeleton::GetCallingPid(); +} + +pid_t HdfRemoteGetCallingUid(void) +{ + return OHOS::IPCSkeleton::GetCallingUid(); +} \ No newline at end of file diff --git a/uhdf2/ipc/src/hdf_remote_service.c b/uhdf2/ipc/src/hdf_remote_service.c index acb2fd4d788872e4470e60023b2bd7cfd14a3e55..952eec775a7417cabaf247fcba7975f383a51df8 100644 --- a/uhdf2/ipc/src/hdf_remote_service.c +++ b/uhdf2/ipc/src/hdf_remote_service.c @@ -52,3 +52,19 @@ int HdfRemoteServiceRegister(int32_t serviceId, struct HdfRemoteService *service { return HdfRemoteAdapterAddSa(serviceId, service); } + +bool HdfRemoteServiceSetInterfaceDesc(struct HdfRemoteService *service, const char *descriptor) +{ + return HdfRemoteAdapterSetInterfaceDesc(service, descriptor); +} + +bool HdfRemoteServiceWriteInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data) +{ + return HdfRemoteAdapterWriteInterfaceToken(service, data); +} + +bool HdfRemoteServiceCheckInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data) +{ + return HdfRemoteAdapterCheckInterfaceToken(service, data); +} + diff --git a/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp b/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp index 4babc245f665e235c1b17e03e3bd3bd6575f444e..534e70d938575ed80456db028508e6d3d839b26f 100644 --- a/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp +++ b/uhdf2/ipc/src/hdf_sbuf_impl_hipc.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -330,7 +330,7 @@ static const char16_t *SbufMParcelImplReadString16(struct HdfSBufImpl *sbuf) static int32_t SbufMParcelImplWriteRemoteService(struct HdfSBufImpl *sbuf, const struct HdfRemoteService *service) { if (sbuf == nullptr || service == nullptr) { - return false; + return HDF_ERR_INVALID_PARAM; } MessageParcel *parcel = MParcelCast(sbuf); const struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); @@ -457,20 +457,20 @@ extern "C" struct HdfSBufImpl *SbufObtainIpc(size_t capacity) class SbufAllocator : public Allocator { public: - virtual void *Realloc(void *data, size_t newSize) override + void *Realloc(void *data, size_t newSize) override { (void)data; (void)newSize; return nullptr; } - virtual void *Alloc(size_t size) override + void *Alloc(size_t size) override { (void)size; return nullptr; } - virtual void Dealloc(void *data) override + void Dealloc(void *data) override { (void)data; } diff --git a/uhdf2/manager/BUILD.gn b/uhdf2/manager/BUILD.gn index 3d55e5ec258c5e023297ae011476e19b363bf809..d6ef7c0e3b2a57442a80bff6c9fb9a176345abc9 100644 --- a/uhdf2/manager/BUILD.gn +++ b/uhdf2/manager/BUILD.gn @@ -64,15 +64,11 @@ ohos_executable("hdf_devmgr") { "$hdf_uhdf_path/utils:libhdf_utils", ] - if (is_standard_system) { - external_deps = [ - "hiviewdfx_hilog_native:libhilog", - "init:libbegetutil", - "utils_base:utils", - ] - } else { - external_deps = [ "hilog:libhilog" ] - } + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "init:libbegetutil", + "utils_base:utils", + ] cflags = [ "-Wall", @@ -80,18 +76,19 @@ ohos_executable("hdf_devmgr") { "-Werror", ] + if (build_selinux) { + external_deps += [ "selinux:libservice_checker" ] + cflags += [ "-DWITH_SELINUX" ] + } + install_enable = true install_images = [ chipset_base_dir ] subsystem_name = "hdf" part_name = "device_driver_framework" } -ohos_prebuilt_etc("hdf_devmgr.rc") { - if (use_musl) { - source = "hdf_devmgr.cfg" - } else { - source = "hdf_devmgr.rc" - } +ohos_prebuilt_etc("hdf_devmgr.cfg") { + source = "hdf_devmgr.cfg" relative_install_dir = "init" install_images = [ chipset_base_dir ] subsystem_name = "hdf" diff --git a/uhdf2/manager/hdf_devmgr.cfg b/uhdf2/manager/hdf_devmgr.cfg index f1090284f12227a89cd89c8853ca9534856ff4fe..89c79004e91420488a228a99e4017cb77720d4b5 100644 --- a/uhdf2/manager/hdf_devmgr.cfg +++ b/uhdf2/manager/hdf_devmgr.cfg @@ -2,9 +2,11 @@ "services" : [{ "name" : "hdf_devmgr", "path" : ["/vendor/bin/hdf_devmgr"], - "uid" : "root", - "gid" : ["system"], - "start-mode" : "boot" + "uid" : "hdf_devmgr", + "gid" : ["hdf_devmgr", "readproc"], + "start-mode" : "boot", + "secon" : "u:r:hdf_devmgr:s0", + "caps" : ["DAC_OVERRIDE", "DAC_READ_SEARCH","CAP_SYS_MODULE", "CAP_SYS_RAWIO"] } ] } diff --git a/uhdf2/manager/include/devhost_service_proxy.h b/uhdf2/manager/include/devhost_service_proxy.h old mode 100755 new mode 100644 diff --git a/uhdf2/manager/include/devmgr_service_stub.h b/uhdf2/manager/include/devmgr_service_stub.h old mode 100755 new mode 100644 index c0ac5432a5c75349a4dbbd83a1ceb8d7c9a4e784..45b02f2f6322e6ac72099546d03e92daa4790104 --- a/uhdf2/manager/include/devmgr_service_stub.h +++ b/uhdf2/manager/include/devmgr_service_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -35,6 +35,7 @@ enum { DEVMGR_SERVICE_LOAD_DEVICE, DEVMGR_SERVICE_UNLOAD_DEVICE, DEVMGR_SERVICE_QUERY_DEVICE, + DEVMGR_SERVICE_LIST_ALL_DEVICE, }; struct HdfObject *DevmgrServiceStubCreate(void); diff --git a/uhdf2/manager/src/devhost_service_proxy.c b/uhdf2/manager/src/devhost_service_proxy.c index ca4f50b1e63c9b431c00b17478a777bbad86dbcf..4e2a087245a5386ba36112e0803a66332fc35aa7 100644 --- a/uhdf2/manager/src/devhost_service_proxy.c +++ b/uhdf2/manager/src/devhost_service_proxy.c @@ -32,12 +32,12 @@ static int32_t DevHostServiceProxyOpsDevice( struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst; if (hostClnt->remote == NULL || data == NULL) { HDF_LOGE("Adding device failed, hostClnt->remote or data or reply is null"); - goto finished; + goto FINISHED; } DeviceAttributeSerialize(attribute, data); status = hostClnt->remote->dispatcher->Dispatch(hostClnt->remote, DEVHOST_SERVICE_ADD_DEVICE, data, NULL); -finished: +FINISHED: if (data != NULL) { HdfSbufRecycle(data); } @@ -58,13 +58,13 @@ static int32_t DevHostServiceProxyDelDevice( struct DevHostServiceProxy *hostClnt = (struct DevHostServiceProxy *)inst; if (hostClnt->remote == NULL || data == NULL) { HDF_LOGE("Del device failed, hostClnt->remote or data is null"); - goto finished; + goto FINISHED; } HdfSbufWriteUint32(data, devid); status = hostClnt->remote->dispatcher->Dispatch(hostClnt->remote, DEVHOST_SERVICE_DEL_DEVICE, data, NULL); -finished: +FINISHED: if (data != NULL) { HdfSbufRecycle(data); } diff --git a/uhdf2/manager/src/device_token_proxy.c b/uhdf2/manager/src/device_token_proxy.c old mode 100755 new mode 100644 diff --git a/uhdf2/manager/src/devmgr_query_device.c b/uhdf2/manager/src/devmgr_query_device.c index eaf3a8fe13107e5636f714a9de84d0a5f87dd31d..7231d23c506617b903405d0fa7fed6b0b5fb52ff 100644 --- a/uhdf2/manager/src/devmgr_query_device.c +++ b/uhdf2/manager/src/devmgr_query_device.c @@ -52,12 +52,12 @@ int DevFillQueryDeviceInfo(struct IDevmgrService *inst, struct HdfSBuf *data, st if ((deviceInfo->policy == SERVICE_POLICY_CAPACITY || deviceInfo->policy == SERVICE_POLICY_PUBLIC) && deviceInfo->status == status) { flag = HdfSbufWriteString(reply, deviceInfo->svcName); - if (flag == false) { + if (!flag) { HDF_LOGE("%{public}s writing string failed %{public}s", __func__, deviceInfo->svcName); return HDF_FAILURE; } flag = HdfSbufWriteInt32(reply, deviceInfo->deviceType); - if (flag == false) { + if (!flag) { HDF_LOGE("%{public}s writing int32 failed %{public}d", __func__, deviceInfo->deviceType); return HDF_FAILURE; } diff --git a/uhdf2/manager/src/devmgr_service_full.c b/uhdf2/manager/src/devmgr_service_full.c index 4c9aeb73290f463fbccce888b864839e51342130..45157efdde0c82bb911e36098e3738eb453497ea 100644 --- a/uhdf2/manager/src/devmgr_service_full.c +++ b/uhdf2/manager/src/devmgr_service_full.c @@ -56,6 +56,10 @@ static int32_t DevmgrServiceFullHandleDeviceHostDied(struct DevHostServiceClnt * if (g_hostMap.nodeSize == 0) { MapInit(&g_hostMap); } + // host is started at init phase by init module, and it respawn by init module + if (!HdfSListIsEmpty(&hostClnt->unloadDevInfos)) { + return 0; + } int *hostDieValue = (int *)MapGet(&g_hostMap, hostClnt->hostName); if (hostDieValue == NULL) { int hostDieNum = HOST_INIT_DIE_NUM; @@ -73,7 +77,7 @@ static int32_t DevmgrServiceFullHandleDeviceHostDied(struct DevHostServiceClnt * struct IDriverInstaller *installer = DriverInstallerGetInstance(); if (installer != NULL && installer->StartDeviceHost != NULL) { HDF_LOGI("%{public}s:%{public}d", __func__, __LINE__); - hostClnt->hostPid = installer->StartDeviceHost(hostClnt->hostId, hostClnt->hostName); + hostClnt->hostPid = installer->StartDeviceHost(hostClnt->hostId, hostClnt->hostName, true); return hostClnt->hostPid; } return INVALID_PID; diff --git a/uhdf2/manager/src/devmgr_service_stub.c b/uhdf2/manager/src/devmgr_service_stub.c index ba1c23905cd66b10d15e1db726ec0c168f96c00d..32deb180c0fdf028448094d06f561fa19d81423b 100644 --- a/uhdf2/manager/src/devmgr_service_stub.c +++ b/uhdf2/manager/src/devmgr_service_stub.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -82,6 +82,16 @@ static int32_t DevmgrServiceStubDispatchUnloadDevice(struct IDevmgrService *devm return devmgrSvc->UnloadDevice(devmgrSvc, serviceName); } +static int32_t DevmgrServiceStubDispatchListAllDevice(struct IDevmgrService *devmgrSvc, struct HdfSBuf *reply) +{ + if (reply == NULL) { + HDF_LOGE("%{public}s:service name is null", __func__); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGD("%{public}s:get all device info", __func__); + return devmgrSvc->ListAllDevice(devmgrSvc, reply); +} + int32_t DevmgrServiceStubDispatch(struct HdfRemoteService *stub, int code, struct HdfSBuf *data, struct HdfSBuf *reply) { int32_t ret = HDF_FAILURE; @@ -90,10 +100,13 @@ int32_t DevmgrServiceStubDispatch(struct HdfRemoteService *stub, int code, struc return HDF_ERR_INVALID_PARAM; } struct IDevmgrService *super = (struct IDevmgrService *)&serviceStub->super; - HDF_LOGE("%{public}s devmgr service stub dispatch cmd %{public}d", __func__, code); + if (!HdfRemoteServiceCheckInterfaceToken(serviceStub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token, code=%{public}d", __func__, code); + return HDF_ERR_INVALID_PARAM; + } + uint32_t hostId = 0; switch (code) { - case DEVMGR_SERVICE_ATTACH_DEVICE_HOST: { - uint32_t hostId = 0; + case DEVMGR_SERVICE_ATTACH_DEVICE_HOST: if (!HdfSbufReadUint32(data, &hostId)) { HDF_LOGE("invalid host id"); return HDF_FAILURE; @@ -102,27 +115,24 @@ int32_t DevmgrServiceStubDispatch(struct HdfRemoteService *stub, int code, struc struct IDevHostService *hostIf = DevHostServiceProxyObtain(hostId, service); ret = super->AttachDeviceHost(super, hostId, hostIf); break; - } - case DEVMGR_SERVICE_ATTACH_DEVICE: { + case DEVMGR_SERVICE_ATTACH_DEVICE: ret = DevmgrServiceStubDispatchAttachDevice(super, data); break; - } - case DEVMGR_SERVICE_DETACH_DEVICE: { + case DEVMGR_SERVICE_DETACH_DEVICE: ret = DevmgrServiceStubDispatchDetachDevice(super, data); break; - } - case DEVMGR_SERVICE_LOAD_DEVICE: { + case DEVMGR_SERVICE_LOAD_DEVICE: ret = DevmgrServiceStubDispatchLoadDevice(super, data); break; - } - case DEVMGR_SERVICE_UNLOAD_DEVICE: { + case DEVMGR_SERVICE_UNLOAD_DEVICE: ret = DevmgrServiceStubDispatchUnloadDevice(super, data); break; - } - case DEVMGR_SERVICE_QUERY_DEVICE: { + case DEVMGR_SERVICE_QUERY_DEVICE: ret = DevFillQueryDeviceInfo(super, data, reply); break; - } + case DEVMGR_SERVICE_LIST_ALL_DEVICE: + ret = DevmgrServiceStubDispatchListAllDevice(super, reply); + break; default: break; } @@ -154,7 +164,7 @@ static int32_t InstallModule(const char *module) } int32_t ret = syscall(SYS_finit_module, fd, "", 0); if (ret != 0) { - HDF_LOGE("failed to install module %{public}s", module); + HDF_LOGE("failed to install module %{public}s, %{public}d", module, ret); } close(fd); @@ -172,7 +182,7 @@ static int32_t MakeModulePath(char *buffer, const char *moduleName) char *path = realpath(temp, buffer); if (path == NULL || strncmp(path, HDF_MODULE_DIR, strlen(HDF_MODULE_DIR)) != 0) { - HDF_LOGI("driver module file is invalud: %{public}s", temp); + HDF_LOGE("driver module file is invalid: %{public}s", temp); return HDF_ERR_INVALID_PARAM; } @@ -238,23 +248,24 @@ int DevmgrServiceStubStartService(struct IDevmgrService *inst) return HDF_ERR_INVALID_OBJECT; } - int status = DevSvcManagerStartService(); - if (status != HDF_SUCCESS) { - return status; - } - struct HdfRemoteService *remoteService = HdfRemoteServiceObtain((struct HdfObject *)inst, &g_devmgrDispatcher); if (remoteService == NULL) { HDF_LOGI("failed to start devmgr, remoteService obtain err"); return HDF_ERR_MALLOC_FAIL; } + if (!HdfRemoteServiceSetInterfaceDesc(remoteService, "HDI.IDeviceManager.V1_0")) { + HDF_LOGE("%{public}s: failed to init interface desc", __func__); + HdfRemoteServiceRecycle(remoteService); + return HDF_FAILURE; + } struct HdfDeviceObject *deviceObject = OsalMemCalloc(sizeof(struct HdfDeviceObject)); if (deviceObject == NULL) { HdfRemoteServiceRecycle(remoteService); return HDF_ERR_MALLOC_FAIL; } deviceObject->service = (struct IDeviceIoService *)remoteService; - status = DevSvcManagerAddService(serviceManager, DEVICE_MANAGER_SERVICE, DEVICE_CLASS_DEFAULT, deviceObject, NULL); + int status = + DevSvcManagerAddService(serviceManager, DEVICE_MANAGER_SERVICE, DEVICE_CLASS_DEFAULT, deviceObject, NULL); if (status != HDF_SUCCESS) { HdfRemoteServiceRecycle(remoteService); OsalMemFree(deviceObject); @@ -262,9 +273,14 @@ int DevmgrServiceStubStartService(struct IDevmgrService *inst) } fullService->remote = remoteService; - DriverModuleLoadHelperInit(); - - return DevmgrServiceStartService((struct IDevmgrService *)&fullService->super); + (void)DriverModuleLoadHelperInit(); + status = DevmgrServiceStartService((struct IDevmgrService *)&fullService->super); + if (status != HDF_SUCCESS) { + HdfRemoteServiceRecycle(remoteService); + OsalMemFree(deviceObject); + return status; + } + return DevSvcManagerStartService(); } static void DevmgrServiceStubConstruct(struct DevmgrServiceStub *inst) diff --git a/uhdf2/manager/src/devsvc_manager_stub.c b/uhdf2/manager/src/devsvc_manager_stub.c index 1b42f969f7dee0229dbdff1dd96fb2b17e27626d..43c765408ec12c46abbfbaa97ae9255574f829fa 100644 --- a/uhdf2/manager/src/devsvc_manager_stub.c +++ b/uhdf2/manager/src/devsvc_manager_stub.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -14,20 +14,64 @@ */ #include "devsvc_manager_stub.h" + +#ifdef WITH_SELINUX +#include +#endif + #include "device_token_proxy.h" #include "devmgr_service_stub.h" #include "devsvc_listener_holder.h" #include "devsvc_manager_proxy.h" #include "hdf_cstring.h" #include "hdf_log.h" +#include "hdf_remote_adapter_if.h" #include "hdf_sbuf.h" #include "hdf_slist.h" #include "osal_mem.h" #define HDF_LOG_TAG devsvc_manager_stub -static struct HdfDeviceObject *ObtainServiceObject(struct DevSvcManagerStub *stub, - const char *name, struct HdfRemoteService *service) +static int32_t AddServicePermCheck(const char *servName) +{ +#ifdef WITH_SELINUX + pid_t callingPid = HdfRemoteGetCallingPid(); + if (HdfAddServiceCheck(callingPid, servName) != 0) { + HDF_LOGE("[selinux] %{public}d haven't \"add service\" permission to %{public}s", callingPid, servName); + return HDF_ERR_NOPERM; + } +#endif + return HDF_SUCCESS; +} + +static int32_t GetServicePermCheck(const char *servName) +{ +#ifdef WITH_SELINUX + pid_t callingPid = HdfRemoteGetCallingPid(); + if (HdfGetServiceCheck(callingPid, servName) != 0) { + HDF_LOGE("[selinux] %{public}d haven't \"get service\" permission to %{public}s", callingPid, servName); + return HDF_ERR_NOPERM; + } +#endif + + return HDF_SUCCESS; +} + +static int32_t ListServicePermCheck() +{ +#ifdef WITH_SELINUX + pid_t callingPid = HdfRemoteGetCallingPid(); + if (HdfListServiceCheck(callingPid) != 0) { + HDF_LOGE("[selinux] %{public}d haven't \"list service\" permission", callingPid); + return HDF_ERR_NOPERM; + } +#endif + + return HDF_SUCCESS; +} + +static struct HdfDeviceObject *ObtainServiceObject( + struct DevSvcManagerStub *stub, const char *name, struct HdfRemoteService *service) { struct HdfDeviceObject *serviceObject = OsalMemCalloc(sizeof(struct HdfDeviceObject)); if (serviceObject == NULL) { @@ -54,7 +98,7 @@ static void ReleaseServiceObject(struct DevSvcManagerStub *stub, struct HdfDevic HDF_LOGW("release service object has empty name, may broken object"); return; } - struct HdfRemoteService *serviceRemote = (struct HdfRemoteService *) serviceObject->service; + struct HdfRemoteService *serviceRemote = (struct HdfRemoteService *)serviceObject->service; HdfRemoteServiceRemoveDeathRecipient(serviceRemote, &stub->recipient); HdfRemoteServiceRecycle((struct HdfRemoteService *)serviceObject->service); OsalMemFree(serviceObject->priv); @@ -67,11 +111,20 @@ static int32_t DevSvcManagerStubAddService(struct IDevSvcManager *super, struct { int ret = HDF_FAILURE; struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } const char *name = HdfSbufReadString(data); if (name == NULL) { HDF_LOGE("%{public}s failed, name is null", __func__); return ret; } + ret = AddServicePermCheck(name); + if (ret != HDF_SUCCESS) { + return ret; + } + uint16_t devClass = DEVICE_CLASS_DEFAULT; if (!HdfSbufReadUint16(data, &devClass)) { HDF_LOGE("%{public}s failed, devClass invalid", __func__); @@ -104,11 +157,20 @@ static int32_t DevSvcManagerStubUpdateService(struct IDevSvcManager *super, stru { int ret = HDF_FAILURE; struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } const char *name = HdfSbufReadString(data); if (name == NULL) { HDF_LOGE("%{public}s failed, name is null", __func__); return ret; } + ret = AddServicePermCheck(name); + if (ret != HDF_SUCCESS) { + return ret; + } + uint16_t devClass = DEVICE_CLASS_DEFAULT; if (!HdfSbufReadUint16(data, &devClass)) { HDF_LOGE("%{public}s failed, devClass invalid", __func__); @@ -146,11 +208,20 @@ static int32_t DevSvcManagerStubUpdateService(struct IDevSvcManager *super, stru static int32_t DevSvcManagerStubGetService(struct IDevSvcManager *super, struct HdfSBuf *data, struct HdfSBuf *reply) { int ret = HDF_FAILURE; + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } const char *name = HdfSbufReadString(data); if (name == NULL) { HDF_LOGE("%{public}s failed, name is null", __func__); return ret; } + ret = GetServicePermCheck(name); + if (ret != HDF_SUCCESS) { + return ret; + } struct HdfRemoteService *remoteService = (struct HdfRemoteService *)super->GetService(super, name); if (remoteService != NULL) { ret = HDF_SUCCESS; @@ -163,13 +234,39 @@ static int32_t DevSvcManagerStubGetService(struct IDevSvcManager *super, struct return ret; } +static int32_t DevSvcManagerStubListAllService( + struct IDevSvcManager *super, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } + int ret = ListServicePermCheck(); + if (ret != HDF_SUCCESS) { + return ret; + } + super->ListAllService(super, reply); + + return HDF_SUCCESS; +} + static int32_t DevSvcManagerStubRemoveService(struct IDevSvcManager *super, struct HdfSBuf *data) { + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } const char *name = HdfSbufReadString(data); if (name == NULL) { HDF_LOGE("%{public}s failed, name is null", __func__); return HDF_FAILURE; } + int32_t ret = AddServicePermCheck(name); + if (ret != HDF_SUCCESS) { + return ret; + } struct HdfDeviceObject *serviceObject = super->GetObject(super, name); if (serviceObject == NULL) { HDF_LOGE("remove service %{public}s not exist", name); @@ -190,12 +287,16 @@ static int32_t DevSvcManagerStubRemoveService(struct IDevSvcManager *super, stru super->RemoveService(super, name); HDF_LOGI("service %{public}s removed", name); - struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; ReleaseServiceObject(stub, serviceObject); return HDF_SUCCESS; } static int32_t DevSvcManagerStubRegisterServListener(struct IDevSvcManager *super, struct HdfSBuf *data) { + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } uint16_t listenClass = DEVICE_CLASS_DEFAULT; if (!HdfSbufReadUint16(data, &listenClass)) { return HDF_ERR_INVALID_PARAM; @@ -205,8 +306,8 @@ static int32_t DevSvcManagerStubRegisterServListener(struct IDevSvcManager *supe return HDF_ERR_INVALID_PARAM; } - struct ServStatListenerHolder *listenerHolder - = ServStatListenerHolderCreate((uintptr_t)listenerRemote, listenClass); + struct ServStatListenerHolder *listenerHolder = + ServStatListenerHolderCreate((uintptr_t)listenerRemote, listenClass); if (listenerHolder == NULL) { return HDF_ERR_MALLOC_FAIL; } @@ -223,6 +324,11 @@ static int32_t DevSvcManagerStubRegisterServListener(struct IDevSvcManager *supe static int32_t DevSvcManagerStubUnregisterServListener(struct IDevSvcManager *super, struct HdfSBuf *data) { + struct DevSvcManagerStub *stub = (struct DevSvcManagerStub *)super; + if (!HdfRemoteServiceCheckInterfaceToken(stub->remote, data)) { + HDF_LOGE("%{public}s: invalid interface token", __func__); + return HDF_ERR_INVALID_PARAM; + } struct HdfRemoteService *listenerRemote = HdfSbufReadRemoteService(data); if (listenerRemote == NULL) { return HDF_ERR_INVALID_PARAM; @@ -249,6 +355,7 @@ int DevSvcManagerStubDispatch(struct HdfRemoteService *service, int code, struct return ret; } struct IDevSvcManager *super = (struct IDevSvcManager *)&stub->super; + HDF_LOGD("DevSvcManagerStubDispatch called: code=%{public}d", code); switch (code) { case DEVSVC_MANAGER_ADD_SERVICE: ret = DevSvcManagerStubAddService(super, data); @@ -262,12 +369,15 @@ int DevSvcManagerStubDispatch(struct HdfRemoteService *service, int code, struct case DEVSVC_MANAGER_REMOVE_SERVICE: ret = DevSvcManagerStubRemoveService(super, data); break; - case DEVSVC_MANAGER_REGISER_SVCLISTENER: + case DEVSVC_MANAGER_REGISTER_SVCLISTENER: ret = DevSvcManagerStubRegisterServListener(super, data); break; - case DEVSVC_MANAGER_UNREGISER_SVCLISTENER: + case DEVSVC_MANAGER_UNREGISTER_SVCLISTENER: ret = DevSvcManagerStubUnregisterServListener(super, data); break; + case DEVSVC_MANAGER_LIST_ALL_SERVICE: + ret = DevSvcManagerStubListAllService(super, data, reply); + break; default: HDF_LOGE("Unknown code : %{public}d", code); ret = HDF_FAILURE; @@ -277,8 +387,8 @@ int DevSvcManagerStubDispatch(struct HdfRemoteService *service, int code, struct void DevSvcManagerOnServiceDied(struct HdfDeathRecipient *recipient, struct HdfRemoteService *remote) { - struct DevSvcManagerStub *stub - = HDF_SLIST_CONTAINER_OF(struct HdfDeathRecipient, recipient, struct DevSvcManagerStub, recipient); + struct DevSvcManagerStub *stub = + HDF_SLIST_CONTAINER_OF(struct HdfDeathRecipient, recipient, struct DevSvcManagerStub, recipient); if (stub == NULL) { return; } @@ -320,11 +430,15 @@ int DevSvcManagerStubStart(struct IDevSvcManager *svcmgr) ServStatListenerHolderinit(); - static struct HdfRemoteDispatcher dispatcher = { .Dispatch = DevSvcManagerStubDispatch }; + static struct HdfRemoteDispatcher dispatcher = {.Dispatch = DevSvcManagerStubDispatch}; inst->remote = HdfRemoteServiceObtain((struct HdfObject *)inst, &dispatcher); if (inst->remote == NULL) { HDF_LOGE("failed to obtain device service manager remote service"); - return false; + return HDF_ERR_MALLOC_FAIL; + } + if (!HdfRemoteServiceSetInterfaceDesc(inst->remote, "HDI.IServiceManager.V1_0")) { + HDF_LOGE("%{public}s: failed to init interface desc", __func__); + return HDF_ERR_INVALID_OBJECT; } inst->recipient.OnRemoteDied = DevSvcManagerOnServiceDied; @@ -337,6 +451,7 @@ int DevSvcManagerStubStart(struct IDevSvcManager *svcmgr) HDF_LOGI("publish device service manager success"); inst->started = true; } + return ret; } diff --git a/uhdf2/manager/src/driver_installer_full.c b/uhdf2/manager/src/driver_installer_full.c index af1bbfa8845e909c8bb1e8cec204cd675a1a37d8..c234a54570ea2a8e1e583c43b80f0a154514f046 100644 --- a/uhdf2/manager/src/driver_installer_full.c +++ b/uhdf2/manager/src/driver_installer_full.c @@ -30,18 +30,13 @@ #define PARAM_CNT 2 static struct DriverInstaller *g_fullInstaller = NULL; -int DriverInstallerFullStartDeviceHost(uint32_t devHostId, const char* devHostName) +int DriverInstallerFullStartDeviceHost(uint32_t devHostId, const char* devHostName, bool dynamic) { - char hostIdStr[MAX_CMD_LEN] = {0}; - int ret; - - if (snprintf_s(hostIdStr, sizeof(hostIdStr), sizeof(hostIdStr) - 1, " %d", devHostId) < 0) { - HDF_LOGE("starting device host, snprintf_s failed"); - return HDF_FAILURE; + if (dynamic) { + int ret = ServiceControlWithExtra(devHostName, START, NULL, 0); + HDF_LOGD("%{public}s %{public}s %{public}d %{public}d", __func__, devHostName, devHostId, ret); } - const char *args[] = { hostIdStr, devHostName }; - ret = ServiceControlWithExtra(devHostName, START, args, PARAM_CNT); - HDF_LOGI("%{public}s %{public}s %{public}d %{public}d", __func__, devHostName, devHostId, ret); + return HDF_SUCCESS; } diff --git a/uhdf2/manager/src/servstat_listener_holder.c b/uhdf2/manager/src/servstat_listener_holder.c index 16dd5d2f6aa783c1910cee9777e7e3369bde5010..52928d519b20d9e4e6189b97620f15be589b896e 100644 --- a/uhdf2/manager/src/servstat_listener_holder.c +++ b/uhdf2/manager/src/servstat_listener_holder.c @@ -71,6 +71,11 @@ int32_t UServStatListenerHolderNotifyStatus(struct ServStatListenerHolder *holde return HDF_ERR_MALLOC_FAIL; } + if (!HdfRemoteServiceWriteInterfaceToken(holderInst->listenerRemote, data)) { + HDF_LOGE("failed to write interface token"); + HdfSbufRecycle(data); + return HDF_FAILURE; + } if (ServiceStatusMarshalling(status, data) != HDF_SUCCESS) { HDF_LOGE("failed to marshalling service status"); HdfSbufRecycle(data); @@ -129,6 +134,11 @@ struct ServStatListenerHolder *ServStatListenerHolderCreate(uintptr_t listener, return NULL; } struct HdfRemoteService *listenerRemote = (struct HdfRemoteService *)listener; + if (!HdfRemoteServiceSetInterfaceDesc(listenerRemote, "HDI.IServiceStatusListener.V1_0")) { + HDF_LOGE("failed to set interface desc to listener"); + OsalMemFree(holder); + return NULL; + } holder->holder.listenClass = listenClass; holder->holder.NotifyStatus = UServStatListenerHolderNotifyStatus; holder->holder.Recycle = UServStatListenerHolderRecycle; diff --git a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn b/uhdf2/model/display/sequenceable/buffer_handle/BUILD.gn similarity index 53% rename from uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn rename to uhdf2/model/display/sequenceable/buffer_handle/BUILD.gn index d470c792a089838a0f135ab3e3affb5af34260ed..aaf1db0b278a58d5e6ca35679f0915f93b1e6e2a 100644 --- a/uhdf2/hdi/test/hdi_sample/sample_client_c/unittest/BUILD.gn +++ b/uhdf2/model/display/sequenceable/buffer_handle/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 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 @@ -13,32 +13,26 @@ import("//build/ohos.gni") import("//drivers/adapter/uhdf2/uhdf.gni") -group("sample_client_c") { - deps = [ ":libsample_client_c" ] -} -ohos_shared_library("libsample_client_c") { +config("buffer_handle_sequenceable_config") { include_dirs = [ - "$hdf_uhdf_path/manager/include", - "$hdf_uhdf_path/include/hdi", + "./", + "//drivers/peripheral/base", ] +} - sources = [ "sample_proxy.c" ] +ohos_shared_library("libbuffer_handle_sequenceable_1.0") { + sources = [ "buffer_handle_sequenceable.cpp" ] - deps = [ - "$hdf_uhdf_path/hdi:libhdi", - "//drivers/adapter/uhdf2/host:libhdf_host", - "//drivers/adapter/uhdf2/ipc:libhdf_ipc_adapter", - "//drivers/adapter/uhdf2/utils:libhdf_utils", - ] + public_configs = [ ":buffer_handle_sequenceable_config" ] - if (is_standard_system) { - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] - } else { - external_deps = [ "hilog:libhilog" ] - } + external_deps = [ + "graphic_chipsetsdk:buffer_handle", + "ipc:ipc_single", + "utils_base:utils", + ] install_images = [ chipset_base_dir ] subsystem_name = "hdf" - part_name = "device_driver_framework" + part_name = "distributed_camera_device_driver" } diff --git a/uhdf2/model/display/sequenceable/buffer_handle/buffer_handle_sequenceable.cpp b/uhdf2/model/display/sequenceable/buffer_handle/buffer_handle_sequenceable.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fc898f87b69f56553bc19ab31877998c607fec9 --- /dev/null +++ b/uhdf2/model/display/sequenceable/buffer_handle/buffer_handle_sequenceable.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022 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 "buffer_handle_sequenceable.h" + +#include +#include "buffer_handle_parcel.h" + +namespace OHOS { +namespace HDI { +namespace Sequenceable { +namespace V1_0 { +BufferHandleSequenceable::BufferHandleSequenceable() : bufferHandle_(nullptr) +{ +} + +BufferHandleSequenceable::BufferHandleSequenceable(BufferHandle *bufferHandle) + : bufferHandle_(bufferHandle) +{ +} + +BufferHandleSequenceable::~BufferHandleSequenceable() +{ +} + +bool BufferHandleSequenceable::Marshalling(Parcel &parcel) const +{ + if (bufferHandle_ == nullptr) { + return false; + } + + MessageParcel &reply = static_cast(parcel); + return WriteBufferHandle(reply, *bufferHandle_); +} + +sptr BufferHandleSequenceable::Unmarshalling(Parcel &parcel) +{ + MessageParcel &data = static_cast(parcel); + BufferHandle *bufferHandle = ReadBufferHandle(data); + if (bufferHandle == nullptr) { + return nullptr; + } + sptr bufferHandleSeq = new BufferHandleSequenceable(bufferHandle); + return bufferHandleSeq; +} + +BufferHandle *BufferHandleSequenceable::GetBufferHandle() +{ + return bufferHandle_; +} + +void BufferHandleSequenceable::SetBufferHandle(BufferHandle *bufferHandle) +{ + bufferHandle_ = bufferHandle; +} +} // namespace V1_0 +} // namespace Sequenceable +} // namespace HDI +} // namespace OHOS \ No newline at end of file diff --git a/uhdf2/model/display/sequenceable/buffer_handle/buffer_handle_sequenceable.h b/uhdf2/model/display/sequenceable/buffer_handle/buffer_handle_sequenceable.h new file mode 100644 index 0000000000000000000000000000000000000000..7277dd279f107d5831d1ed23036407786025ae1b --- /dev/null +++ b/uhdf2/model/display/sequenceable/buffer_handle/buffer_handle_sequenceable.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 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 OHOS_HDI_BUFFER_HANDLE_SEQUENCE_H +#define OHOS_HDI_BUFFER_HANDLE_SEQUENCE_H + +#include + +#include "buffer_handle.h" + +namespace OHOS { +namespace HDI { +namespace Sequenceable { +namespace V1_0 { +using OHOS::Parcelable; +using OHOS::Parcel; +using OHOS::sptr; + +class BufferHandleSequenceable : public Parcelable { +public: + BufferHandleSequenceable(); + explicit BufferHandleSequenceable(BufferHandle *bufferHandle); + virtual ~BufferHandleSequenceable(); + + bool Marshalling(Parcel &parcel) const override; + static sptr Unmarshalling(Parcel &parcel); + + BufferHandle *GetBufferHandle(); + void SetBufferHandle(BufferHandle *bufferHandle); + +private: + BufferHandle *bufferHandle_; +}; +} // namespace V1_0 +} // namespace Sequenceable +} // namespace HDI +} // namespace OHOS +#endif \ No newline at end of file diff --git a/uhdf2/osal/src/osal_sysevent.c b/uhdf2/osal/src/osal_sysevent.c index 74e25551032e1461ee5afebdb9807de91e9864f1..318ea20376c1a1bf518342db8266d4a1a7b18ddc 100644 --- a/uhdf2/osal/src/osal_sysevent.c +++ b/uhdf2/osal/src/osal_sysevent.c @@ -102,20 +102,20 @@ static int OnKEventReceived( return HDF_FAILURE; } - receivedEvent->content = HdfSbufReadString(data); - + const char *eventContent = HdfSbufReadString(data); + eventContent = eventContent == NULL ? "" : eventContent; 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); + notifyNode, receivedEvent->eventClass, receivedEvent->eventid, eventContent); } } if (receivedEvent->syncToken != 0) { - FinishEvent(service, receivedEvent); + (void)FinishEvent(service, receivedEvent); } OsalMutexUnlock(¬ifier->mutex); diff --git a/uhdf2/platform/BUILD.gn b/uhdf2/platform/BUILD.gn index b7fad46664c1774972292b8373cff1a814500944..6f0747eb3424999ee255940cd44f84eb300c283b 100755 --- a/uhdf2/platform/BUILD.gn +++ b/uhdf2/platform/BUILD.gn @@ -34,13 +34,16 @@ ohos_shared_library("libhdf_platform") { public_configs = [ ":libhdf_platform_pub_config" ] sources = [ "$HDF_FRAMEWORKS/support/platform/src/adc/adc_if_u.c", + "$HDF_FRAMEWORKS/support/platform/src/fwk/platform_listener_u.c", "$HDF_FRAMEWORKS/support/platform/src/gpio/gpio_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/i2c/i2c_if_u.c", + "$HDF_FRAMEWORKS/support/platform/src/pwm/pwm_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/rtc/rtc_base.c", "$HDF_FRAMEWORKS/support/platform/src/rtc/rtc_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/spi/spi_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/timer/timer_if_u.c", "$HDF_FRAMEWORKS/support/platform/src/uart/uart_if_u.c", + "$HDF_FRAMEWORKS/support/platform/src/watchdog/watchdog_if_u.c", ] include_dirs = [ diff --git a/uhdf2/shared/include/hdf_remote_service.h b/uhdf2/shared/include/hdf_remote_service.h index cb191caa763afd2924e3e55314c40d57517b5dd4..ae71b25da8ff8b8973b839ccf9c529022d9ca56a 100644 --- a/uhdf2/shared/include/hdf_remote_service.h +++ b/uhdf2/shared/include/hdf_remote_service.h @@ -52,6 +52,12 @@ int HdfRemoteServiceRegister(int32_t serviceId, struct HdfRemoteService *service struct HdfRemoteService *HdfRemoteServiceGet(int32_t serviceId); +bool HdfRemoteServiceSetInterfaceDesc(struct HdfRemoteService *service, const char *descriptor); + +bool HdfRemoteServiceWriteInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data); + +bool HdfRemoteServiceCheckInterfaceToken(struct HdfRemoteService *service, struct HdfSBuf *data); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/uhdf2/shared/src/dev_attribute_serialize.c b/uhdf2/shared/src/dev_attribute_serialize.c index 65fb0210d79fdb4d029a80cb590ba14711e58e9c..a57565cf7c4e4dc8a370ee4d1be438e85cdbc65c 100644 --- a/uhdf2/shared/src/dev_attribute_serialize.c +++ b/uhdf2/shared/src/dev_attribute_serialize.c @@ -66,6 +66,7 @@ struct HdfDeviceInfo *DeviceAttributeDeserialize(struct HdfSBuf *sbuf) if (!HdfSbufReadUint32(sbuf, &attribute->deviceId) || !HdfSbufReadUint16(sbuf, &attribute->policy)) { HDF_LOGE("invalid deviceId or policy"); + DeviceSerializedAttributeRelease(attribute); return NULL; } diff --git a/uhdf2/test/BUILD.gn b/uhdf2/test/BUILD.gn index aef0d79785df9705b34e6855515197c99f643d05..ded2b8287b1481a5da331e4e5ec525f1e31078a1 100644 --- a/uhdf2/test/BUILD.gn +++ b/uhdf2/test/BUILD.gn @@ -14,6 +14,7 @@ group("hdf_test_uhdf") { testonly = true deps = [ + "fuzztest:hdf_platform_test", "unittest/config:hdf_adapter_uhdf_test_config", "unittest/manager:hdf_adapter_uhdf_test_ioservice", "unittest/manager:hdf_adapter_uhdf_test_manager", @@ -21,5 +22,6 @@ group("hdf_test_uhdf") { "unittest/manager:hdf_adapter_uhdf_test_sbuf", "unittest/osal:hdf_adapter_uhdf_test_osal", "unittest/osal:hdf_adapter_uhdf_test_osal_posix", + "//drivers/framework/test/fuzztest:hdf_framework_fuzztest", ] } diff --git a/uhdf2/test/fuzztest/BUILD.gn b/uhdf2/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..15b895a32ea13090a1e342c2bc4a0e18085a5186 --- /dev/null +++ b/uhdf2/test/fuzztest/BUILD.gn @@ -0,0 +1,13 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# +# HDF is dual licensed: you can use it either under the terms of +# the GPL, or the BSD license, at your option. +# See the LICENSE file in the root of this repository for complete details. + +group("hdf_platform_test") { + testonly = true + deps = [] + deps += [ + "//drivers/framework/support/platform/test/fuzztest:hdf_platform_fuzztest", + ] +} diff --git a/uhdf2/test/resource/audio/audiofile/audiorendertest.wav b/uhdf2/test/resource/audio/audiofile/audiorendertest.wav old mode 100755 new mode 100644 diff --git a/uhdf2/test/resource/wlan/ohos_test.xml b/uhdf2/test/resource/wlan/ohos_test.xml index c711d04b44d598b223180a3696e1e468a7849ceb..a7f87b4597445ffcba0522f7f8d344d15dcd1f52 100644 --- a/uhdf2/test/resource/wlan/ohos_test.xml +++ b/uhdf2/test/resource/wlan/ohos_test.xml @@ -14,7 +14,17 @@ limitations under the License. --> - + + + + + + + + +