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

samples: matter: Added nullptr checks after memory allocation #19974

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions samples/matter/common/src/binding/binding_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Nrf::Matter

void BindingHandler::OnInvokeCommandSucces(BindingData *bindingData)
{
VerifyOrReturn(bindingData != nullptr, LOG_ERR("Invalid binding data"));
LOG_DBG("Binding command applied successfully!");

/* If session was recovered and communication works, reset flag to the initial state. */
Expand Down
114 changes: 66 additions & 48 deletions samples/matter/light_switch/src/shell_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,51 @@ namespace Unicast
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

static CHIP_ERROR OffCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

static CHIP_ERROR ToggleCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(false);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}
} /* namespace Unicast */

Expand Down Expand Up @@ -142,42 +151,51 @@ namespace Group
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::On::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR OffCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Off::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR ToggleCommandHandler(int argc, char **argv)
{
Nrf::Matter::BindingHandler::BindingData *data =
Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
if (data) {
data->EndpointId = LightSwitch::GetInstance().GetLightSwitchEndpointId();
data->CommandId = Clusters::OnOff::Commands::Toggle::Id;
data->ClusterId = Clusters::OnOff::Id;
data->InvokeCommandFunc = LightSwitch::SwitchChangedHandler;
data->IsGroup.SetValue(true);

Nrf::Matter::BindingHandler::RunBoundClusterAction(data);
return CHIP_NO_ERROR;
}
return CHIP_ERROR_NO_MEMORY;
}

} /* namespace Group */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ void TemperatureSensor::InternalMeasurement()
void TemperatureSensor::ExternalMeasurement()
{
Nrf::Matter::BindingHandler::BindingData *data = Platform::New<Nrf::Matter::BindingHandler::BindingData>();
data->ClusterId = Clusters::TemperatureMeasurement::Id;
data->EndpointId = mTemperatureMeasurementEndpointId;
data->InvokeCommandFunc = ExternalTemperatureMeasurementReadHandler;
BindingHandler::RunBoundClusterAction(data);
if (data) {
data->ClusterId = Clusters::TemperatureMeasurement::Id;
data->EndpointId = mTemperatureMeasurementEndpointId;
data->InvokeCommandFunc = ExternalTemperatureMeasurementReadHandler;
BindingHandler::RunBoundClusterAction(data);
}
}

void TemperatureSensor::ExternalTemperatureMeasurementReadHandler(const EmberBindingTableEntry &binding,
Expand Down