From dbd9906ae4a97c5bb15f01837b219f0b5e586d1c Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Fri, 4 Oct 2024 12:41:54 -0400 Subject: [PATCH] Make use of DataModel::Provider device type resolution in the access control (#35862) * Add separate listing for device types * Make unit tests work * Restyle * Fixup include * Fixup include * Make use of DataModel::Provider when doing device type resolution * Add clang-tidy comments * Fix merge error: duplicate declaration * Fix a few more merge conflict markers --------- Co-authored-by: Andrei Litvin --- src/app/server/Server.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index 426e3c686572c0..e4db190c507608 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -19,8 +19,10 @@ #include +#include #include #include +#include #include #include #include @@ -81,6 +83,33 @@ using chip::Transport::TcpListenParameters; namespace { +#if CHIP_CONFIG_USE_DATA_MODEL_INTERFACE +class DeviceTypeResolver : public chip::Access::AccessControl::DeviceTypeResolver +{ +public: + bool IsDeviceTypeOnEndpoint(chip::DeviceTypeId deviceType, chip::EndpointId endpoint) override + { + chip::app::DataModel::Provider * model = chip::app::InteractionModelEngine::GetInstance()->GetDataModelProvider(); + + for (auto type = model->FirstDeviceType(endpoint); type.has_value(); type = model->NextDeviceType(endpoint, *type)) + { + if (type->deviceTypeId == deviceType) + { +#if CHIP_CONFIG_USE_EMBER_DATA_MODEL + VerifyOrDie(chip::app::IsDeviceTypeOnEndpoint(deviceType, endpoint)); +#endif // CHIP_CONFIG_USE_EMBER_DATA_MODEL + return true; + } + } +#if CHIP_CONFIG_USE_EMBER_DATA_MODEL + VerifyOrDie(!chip::app::IsDeviceTypeOnEndpoint(deviceType, endpoint)); +#endif // CHIP_CONFIG_USE_EMBER_DATA_MODEL + return false; + } +} sDeviceTypeResolver; +#else // CHIP_CONFIG_USE_DATA_MODEL_INTERFACE + +// Ember implementation of the device type resolver class DeviceTypeResolver : public chip::Access::AccessControl::DeviceTypeResolver { public: @@ -89,6 +118,7 @@ class DeviceTypeResolver : public chip::Access::AccessControl::DeviceTypeResolve return chip::app::IsDeviceTypeOnEndpoint(deviceType, endpoint); } } sDeviceTypeResolver; +#endif } // namespace