Skip to content

Commit

Permalink
[ESP32] Add API to create user defined size of endpoint array (#27341)
Browse files Browse the repository at this point in the history
* [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 <bzbarsky@apple.com>

---------

Co-authored-by: Restyled.io <commits@restyled.io>
Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
  • Loading branch information
3 people authored and pull[bot] committed Oct 31, 2023
1 parent bd59cbd commit 2522944
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
7 changes: 7 additions & 0 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <common/Esp32AppServer.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <examples/platform/esp32/mode-support/static-supported-modes-manager.h>
#include <platform/ESP32/ESP32Utils.h>

#if CONFIG_HAVE_DISPLAY
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,31 @@ using SemanticTag = Structs::SemanticTagStruct::Type;
template <typename T>
using List = app::DataModel::List<T>;

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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,40 @@ 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,
const ModeOptionStructType ** dataPtr) const override;

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);
}
Expand Down

0 comments on commit 2522944

Please sign in to comment.