Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32] Add API to create user defined size of endpoint array #27341

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
jadhavrohit924 marked this conversation as resolved.
Show resolved Hide resolved
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