From 10147405f51bb3566bbd69dee81da12e1f92b62b Mon Sep 17 00:00:00 2001 From: Rohit Jadhav <69809379+jadhavrohit924@users.noreply.github.com> Date: Thu, 22 Jun 2023 13:16:53 +0530 Subject: [PATCH] [ESP32] Add API to create user defined size of endpoint array (#27341) * [ESP32] Add API to create user defined size of endpoint array * Restyled by clang-format * Address review comments * Update comment Co-authored-by: Boris Zbarsky --------- Co-authored-by: Restyled.io Co-authored-by: Boris Zbarsky --- examples/all-clusters-app/esp32/main/main.cpp | 7 ++++++ .../static-supported-modes-manager.cpp | 23 ++++++++++++++---- .../static-supported-modes-manager.h | 24 ++++++++++++++----- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index 4f4450c882767c..f3a593c2c53144 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #if CONFIG_HAVE_DISPLAY @@ -127,6 +128,12 @@ static void InitServer(intptr_t context) #if CONFIG_DEVICE_TYPE_M5STACK SetupPretendDevices(); #endif + err = app::Clusters::ModeSelect::StaticSupportedModesManager::getStaticSupportedModesManagerInstance().InitEndpointArray( + FIXED_ENDPOINT_COUNT); + if (err != CHIP_NO_ERROR) + { + ESP_LOGE(TAG, "Failed to initialize endpoint array for supported-modes, err:%" CHIP_ERROR_FORMAT, err.Format()); + } } extern "C" void app_main() diff --git a/examples/platform/esp32/mode-support/static-supported-modes-manager.cpp b/examples/platform/esp32/mode-support/static-supported-modes-manager.cpp index ed85f1068661f8..d06a8b8b7dc809 100644 --- a/examples/platform/esp32/mode-support/static-supported-modes-manager.cpp +++ b/examples/platform/esp32/mode-support/static-supported-modes-manager.cpp @@ -31,16 +31,31 @@ using SemanticTag = Structs::SemanticTagStruct::Type; template using List = app::DataModel::List; +SupportedModesManager::ModeOptionsProvider * StaticSupportedModesManager::epModeOptionsProviderList = nullptr; + const StaticSupportedModesManager StaticSupportedModesManager::instance = StaticSupportedModesManager(); -SupportedModesManager::ModeOptionsProvider StaticSupportedModesManager::epModeOptionsProviderList[FIXED_ENDPOINT_COUNT]; +int StaticSupportedModesManager::mSize = 0; -void StaticSupportedModesManager::InitEndpointArray() +CHIP_ERROR StaticSupportedModesManager::InitEndpointArray(int size) { - for (int i = 0; i < FIXED_ENDPOINT_COUNT; i++) + if (epModeOptionsProviderList != nullptr) + { + ChipLogError(Zcl, "Cannot allocate epModeOptionsProviderList"); + return CHIP_ERROR_INCORRECT_STATE; + } + mSize = size; + epModeOptionsProviderList = new SupportedModesManager::ModeOptionsProvider[mSize]; + if (epModeOptionsProviderList == nullptr) + { + ChipLogError(Zcl, "Failed to allocate memory to epModeOptionsProviderList"); + return CHIP_ERROR_NO_MEMORY; + } + for (int i = 0; i < mSize; i++) { epModeOptionsProviderList[i] = ModeOptionsProvider(); } + return CHIP_NO_ERROR; } SupportedModesManager::ModeOptionsProvider StaticSupportedModesManager::getModeOptionsProvider(EndpointId endpointId) const @@ -181,7 +196,7 @@ Status StaticSupportedModesManager::getModeOptionByMode(unsigned short endpointI const ModeSelect::SupportedModesManager * ModeSelect::getSupportedModesManager() { - return &StaticSupportedModesManager::instance; + return &StaticSupportedModesManager::getStaticSupportedModesManagerInstance(); } void StaticSupportedModesManager::FreeSupportedModes(EndpointId endpointId) const diff --git a/examples/platform/esp32/mode-support/static-supported-modes-manager.h b/examples/platform/esp32/mode-support/static-supported-modes-manager.h index 689c9d059f4ab7..985fb805cf682f 100644 --- a/examples/platform/esp32/mode-support/static-supported-modes-manager.h +++ b/examples/platform/esp32/mode-support/static-supported-modes-manager.h @@ -31,16 +31,28 @@ class StaticSupportedModesManager : public chip::app::Clusters::ModeSelect::Supp private: using ModeOptionStructType = Structs::ModeOptionStruct::Type; using SemanticTag = Structs::SemanticTagStruct::Type; + static int mSize; - static ModeOptionsProvider epModeOptionsProviderList[FIXED_ENDPOINT_COUNT]; - - void InitEndpointArray(); + static ModeOptionsProvider * epModeOptionsProviderList; void FreeSupportedModes(EndpointId endpointId) const; -public: static const StaticSupportedModesManager instance; +public: + // InitEndpointArray should be called only once in the application. Memory allocated to the + // epModeOptionsProviderList will be needed for the lifetime of the program, so it's never deallocated. + static CHIP_ERROR InitEndpointArray(int size); + + // DeInitEndpointArray should be called only when application need to reallocate memory of + // epModeOptionsProviderList ( Eg. Bridges ). + static void DeInitEndpointArray() + { + delete[] epModeOptionsProviderList; + epModeOptionsProviderList = nullptr; + mSize = 0; + } + SupportedModesManager::ModeOptionsProvider getModeOptionsProvider(EndpointId endpointId) const override; Protocols::InteractionModel::Status getModeOptionByMode(EndpointId endpointId, uint8_t mode, @@ -48,11 +60,11 @@ class StaticSupportedModesManager : public chip::app::Clusters::ModeSelect::Supp void CleanUp(EndpointId endpointId) const; - StaticSupportedModesManager() { InitEndpointArray(); } + StaticSupportedModesManager() {} ~StaticSupportedModesManager() { - for (int i = 0; i < FIXED_ENDPOINT_COUNT; i++) + for (int i = 0; i < mSize; i++) { FreeSupportedModes(i); }