Skip to content

Commit

Permalink
Replaced the use of std::set with IntrusiveList in ModeBase. (#28163)
Browse files Browse the repository at this point in the history
* Replaced the use of std::set with IntrusiveList in ModeBase.

* Removed the std::set exeption for ModeBase.

* Restyled by clang-format

* Refoctor from review.

* Added a way to gracefully shutdown the ModeBase dreived clusters and used it in the linux all-clusters-app.

* Restyled by whitespace

* Restyled by clang-format

* Implemented required ApplicationExit().

* Restyled by clang-format

* Modified the ModeBase example shutdowns to be safe if Shutdown is called twice and properly free the memory.

* Renamed ApplicationExit() to ApplicationShutdown()

* Apply documentation suggestions from code review

Co-authored-by: Boris Zbarsky <[email protected]>

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed Jul 26, 2023
1 parent 32351d4 commit 2434792
Show file tree
Hide file tree
Showing 33 changed files with 158 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class DishwasherModeDelegate : public ModeBase::Delegate
~DishwasherModeDelegate() override = default;
};

void Shutdown();

} // namespace DishwasherMode

} // namespace Clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class LaundryWasherModeDelegate : public ModeBase::Delegate
~LaundryWasherModeDelegate() override = default;
};

void Shutdown();

} // namespace LaundryWasherMode

} // namespace Clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class RvcRunModeDelegate : public ModeBase::Delegate
~RvcRunModeDelegate() override = default;
};

void Shutdown();

} // namespace RvcRunMode

namespace RvcCleanMode {
Expand Down Expand Up @@ -107,6 +109,8 @@ class RvcCleanModeDelegate : public ModeBase::Delegate
~RvcCleanModeDelegate() override = default;
};

void Shutdown();

} // namespace RvcCleanMode

} // namespace Clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class TccModeDelegate : public ModeBase::Delegate
~TccModeDelegate() override = default;
};

void Shutdown();

} // namespace RefrigeratorAndTemperatureControlledCabinetMode

} // namespace Clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,24 @@ CHIP_ERROR DishwasherModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List<Mo
return CHIP_NO_ERROR;
}

void DishwasherMode::Shutdown()
{
if (gDishwasherModeInstance != nullptr)
{
delete gDishwasherModeInstance;
gDishwasherModeInstance = nullptr;
}
if (gDishwasherModeDelegate != nullptr)
{
gDishwasherModeDelegate->~DishwasherModeDelegate();
}
}

void emberAfDishwasherModeClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
VerifyOrDie(gDishwasherModeDelegate == nullptr && gDishwasherModeInstance == nullptr);
gDishwasherModeDelegate = new DishwasherMode::DishwasherModeDelegate;
// todo use Clusters::XxxMode::Feature::kXxxx to set features.
gDishwasherModeInstance =
new ModeBase::Instance(gDishwasherModeDelegate, 0x1, DishwasherMode::Id, chip::to_underlying(Feature::kOnOff));
gDishwasherModeInstance->Init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ CHIP_ERROR LaundryWasherModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List
return CHIP_NO_ERROR;
}

void LaundryWasherMode::Shutdown()
{
if (gLaundryWasherModeInstance != nullptr)
{
delete gLaundryWasherModeInstance;
gLaundryWasherModeInstance = nullptr;
}
if (gLaundryWasherModeDelegate != nullptr)
{
gLaundryWasherModeDelegate->~LaundryWasherModeDelegate();
}
}

void emberAfLaundryWasherModeClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
Expand Down
26 changes: 26 additions & 0 deletions examples/all-clusters-app/all-clusters-common/src/rvc-modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ CHIP_ERROR RvcRunModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List<ModeTa
return CHIP_NO_ERROR;
}

void RvcRunMode::Shutdown()
{
if (gRvcRunModeInstance != nullptr)
{
delete gRvcRunModeInstance;
gRvcRunModeInstance = nullptr;
}
if (gRvcRunModeDelegate != nullptr)
{
gRvcRunModeDelegate->~RvcRunModeDelegate();
}
}

void emberAfRvcRunModeClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
Expand Down Expand Up @@ -158,6 +171,19 @@ CHIP_ERROR RvcCleanModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List<Mode
return CHIP_NO_ERROR;
}

void RvcCleanMode::Shutdown()
{
if (gRvcCleanModeInstance != nullptr)
{
delete gRvcCleanModeInstance;
gRvcCleanModeInstance = nullptr;
}
if (gRvcCleanModeDelegate != nullptr)
{
gRvcCleanModeDelegate->~RvcCleanModeDelegate();
}
}

void emberAfRvcCleanModeClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
Expand Down
13 changes: 13 additions & 0 deletions examples/all-clusters-app/all-clusters-common/src/tcc-mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ CHIP_ERROR TccModeDelegate::GetModeTagsByIndex(uint8_t modeIndex, List<ModeTagSt
return CHIP_NO_ERROR;
}

void RefrigeratorAndTemperatureControlledCabinetMode::Shutdown()
{
if (gTccModeInstance != nullptr)
{
delete gTccModeInstance;
gTccModeInstance = nullptr;
}
if (gTccModeDelegate != nullptr)
{
gTccModeDelegate->~TccModeDelegate();
}
}

void emberAfRefrigeratorAndTemperatureControlledCabinetModeClusterInitCallback(chip::EndpointId endpointId)
{
VerifyOrDie(endpointId == 1); // this cluster is only enabled for endpoint 1.
Expand Down
10 changes: 8 additions & 2 deletions examples/all-clusters-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* limitations under the License.
*/

#include "main-common.h"
#include "AllClustersCommandDelegate.h"
#include "WindowCoveringManager.h"
#include "dishwasher-mode.h"
Expand Down Expand Up @@ -191,8 +190,15 @@ void ApplicationInit()
app::Clusters::TemperatureControl::SetInstance(&sAppSupportedTemperatureLevelsDelegate);
}

void ApplicationExit()
void ApplicationShutdown()
{
// These may have been initialised via the emberAfXxxClusterInitCallback methods. We need to destroy them before shutdown.
Clusters::DishwasherMode::Shutdown();
Clusters::LaundryWasherMode::Shutdown();
Clusters::RvcCleanMode::Shutdown();
Clusters::RvcRunMode::Shutdown();
Clusters::RefrigeratorAndTemperatureControlledCabinetMode::Shutdown();

if (sChipNamedPipeCommands.Stop() != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to stop CHIP NamedPipeCommands");
Expand Down
21 changes: 0 additions & 21 deletions examples/all-clusters-app/linux/main-common.h

This file was deleted.

2 changes: 0 additions & 2 deletions examples/all-clusters-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "AppMain.h"
#include "AppOptions.h"
#include "binding-handler.h"
#include "main-common.h"

// Network commissioning
namespace {
Expand All @@ -35,7 +34,6 @@ int main(int argc, char * argv[])
LinuxDeviceOptions::GetInstance().dacProvider = AppOptions::GetDACProvider();

ChipLinuxAppMainLoop();
ApplicationExit();

return 0;
}
2 changes: 2 additions & 0 deletions examples/all-clusters-app/tizen/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ void ApplicationInit()
app::Clusters::TemperatureControl::SetInstance(&sAppSupportedTemperatureLevelsDelegate);
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
TizenServiceAppMain app;
Expand Down
2 changes: 2 additions & 0 deletions examples/all-clusters-minimal-app/linux/main-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ static Identify gIdentify1 = {

void ApplicationInit() {}

void ApplicationShutdown() {}

void emberAfLowPowerClusterInitCallback(EndpointId endpoint)
{
ChipLogProgress(Zcl, "TV Linux App: LowPower::SetDefaultDelegate");
Expand Down
2 changes: 2 additions & 0 deletions examples/all-clusters-minimal-app/tizen/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void ApplicationInit()
sEthernetNetworkCommissioningInstance.Init();
}

void ApplicationShutdown(){};

int main(int argc, char * argv[])
{
TizenServiceAppMain app;
Expand Down
2 changes: 2 additions & 0 deletions examples/bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ bool emberAfActionsClusterInstantActionCallback(app::CommandHandler * commandObj

void ApplicationInit() {}

void ApplicationShutdown() {}

const EmberAfDeviceType gBridgedOnOffDeviceTypes[] = { { DEVICE_TYPE_LO_ON_OFF_LIGHT, DEVICE_VERSION_DEFAULT },
{ DEVICE_TYPE_BRIDGED_NODE, DEVICE_VERSION_DEFAULT } };

Expand Down
2 changes: 2 additions & 0 deletions examples/chef/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ using namespace chip::app;

void ApplicationInit() {}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
Expand Down
2 changes: 2 additions & 0 deletions examples/contact-sensor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using namespace chip::app::Clusters;

void ApplicationInit() {}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);
Expand Down
2 changes: 2 additions & 0 deletions examples/dishwasher-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void ApplicationInit()
MatterOperationalStateServerInit();
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void ApplicationInit()
}
}

void ApplicationExit()
void ApplicationShutdown()
{
if (sChipNamedPipeCommands.Stop() != CHIP_NO_ERROR)
{
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/tizen/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void ApplicationInit()
#endif
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
TizenServiceAppMain app;
Expand Down
2 changes: 2 additions & 0 deletions examples/lock-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void ApplicationInit()
}
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);
Expand Down
2 changes: 2 additions & 0 deletions examples/ota-provider-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ void ApplicationInit()
chip::app::Clusters::OTAProvider::SetDelegate(kOtaProviderEndpoint, &gOtaProvider);
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, &cmdLineOptions) == 0);
Expand Down
2 changes: 2 additions & 0 deletions examples/ota-requestor-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ void ApplicationInit()
InitOTARequestor();
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, &cmdLineOptions, MakeOptional(kNetworkCommissioningEndpointSecondary)) == 0);
Expand Down
2 changes: 2 additions & 0 deletions examples/placeholder/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

void ApplicationInit() {}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv, AppOptions::GetOptions()) == 0);
Expand Down
2 changes: 2 additions & 0 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ void ChipLinuxAppMainLoop(AppMainLoopImplementation * impl)
}
gMainLoopImplementation = nullptr;

ApplicationShutdown();

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
ShutdownCommissioner();
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
Expand Down
3 changes: 3 additions & 0 deletions examples/platform/linux/AppMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@ CommissionerDiscoveryController * GetCommissionerDiscoveryController();

// For extra init calls, the function will be called right before running Matter main loop.
void ApplicationInit();

// For extra shutdown calls, the function will be called before any of the core Matter objects are shut down.
void ApplicationShutdown();
2 changes: 2 additions & 0 deletions examples/resource-monitoring-app/linux/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void ApplicationInit()
gActivatedCarbonFilterInstance.Init();
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
Expand Down
2 changes: 2 additions & 0 deletions examples/thermostat/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ static Identify gIdentify1 = {

void ApplicationInit() {}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{
VerifyOrDie(ChipLinuxAppInit(argc, argv) == 0);
Expand Down
2 changes: 2 additions & 0 deletions examples/tv-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void ApplicationInit()
// emberAfEndpointEnableDisable(3, false);
}

void ApplicationShutdown() {}

int main(int argc, char * argv[])
{

Expand Down
3 changes: 0 additions & 3 deletions scripts/tools/check_includes_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,4 @@
# Library meant for non-embedded
'src/tracing/json/json_tracing.cpp': {'string', 'sstream'},
'src/tracing/json/json_tracing.h': {'fstream'},

# Temporary solution util the OnOff server can provide a callback API for state change.
'src/app/clusters/mode-base-server/mode-base-server.h': {'set'}
}
Loading

0 comments on commit 2434792

Please sign in to comment.