Skip to content

Commit

Permalink
moveToLevelHandler can be called from a non-commmand, in that case em…
Browse files Browse the repository at this point in the history
…berAfCurrentCommand return null pointer (#14334)

Remove the use of emberAfCurrentCommand, moveToLevelHandler now returns a EmberAfStatus. The caller deals with sending the immediateResponse if need be
  • Loading branch information
jmartinez-silabs authored and pull[bot] committed Sep 13, 2023
1 parent e7e1e73 commit 5237681
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/app/clusters/level-control/level-control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ static EmberAfLevelControlState stateTable[EMBER_AF_LEVEL_CONTROL_CLUSTER_SERVER

static EmberAfLevelControlState * getState(EndpointId endpoint);

static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t level, uint16_t transitionTimeDs,
uint8_t optionMask, uint8_t optionOverride, uint16_t storedLevel);
static EmberAfStatus moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t level, uint16_t transitionTimeDs,
uint8_t optionMask, uint8_t optionOverride, uint16_t storedLevel);
static void moveHandler(CommandId commandId, uint8_t moveMode, uint8_t rate, uint8_t optionMask, uint8_t optionOverride);
static void stepHandler(CommandId commandId, uint8_t stepMode, uint8_t stepSize, uint16_t transitionTimeDs, uint8_t optionMask,
uint8_t optionOverride);
Expand Down Expand Up @@ -408,8 +408,12 @@ bool emberAfLevelControlClusterMoveToLevelCallback(app::CommandHandler * command

emberAfLevelControlClusterPrintln("%pMOVE_TO_LEVEL %x %2x %x %x", "RX level-control:", level, transitionTime, optionMask,
optionOverride);
moveToLevelHandler(commandPath.mEndpointId, Commands::MoveToLevel::Id, level, transitionTime, optionMask, optionOverride,
INVALID_STORED_LEVEL); // Don't revert to the stored level
EmberAfStatus status =
moveToLevelHandler(commandPath.mEndpointId, Commands::MoveToLevel::Id, level, transitionTime, optionMask, optionOverride,
INVALID_STORED_LEVEL); // Don't revert to the stored level

emberAfSendImmediateDefaultResponse(status);

return true;
}

Expand All @@ -421,8 +425,12 @@ bool emberAfLevelControlClusterMoveToLevelWithOnOffCallback(app::CommandHandler
auto & transitionTime = commandData.transitionTime;

emberAfLevelControlClusterPrintln("%pMOVE_TO_LEVEL_WITH_ON_OFF %x %2x", "RX level-control:", level, transitionTime);
moveToLevelHandler(commandPath.mEndpointId, Commands::MoveToLevelWithOnOff::Id, level, transitionTime, 0xFF, 0xFF,
INVALID_STORED_LEVEL); // Don't revert to the stored level
EmberAfStatus status =
moveToLevelHandler(commandPath.mEndpointId, Commands::MoveToLevelWithOnOff::Id, level, transitionTime, 0xFF, 0xFF,
INVALID_STORED_LEVEL); // Don't revert to the stored level

emberAfSendImmediateDefaultResponse(status);

return true;
}

Expand Down Expand Up @@ -495,8 +503,8 @@ bool emberAfLevelControlClusterStopWithOnOffCallback(app::CommandHandler * comma
return true;
}

static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t level, uint16_t transitionTimeDs,
uint8_t optionMask, uint8_t optionOverride, uint16_t storedLevel)
static EmberAfStatus moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t level, uint16_t transitionTimeDs,
uint8_t optionMask, uint8_t optionOverride, uint16_t storedLevel)
{
EmberAfLevelControlState * state = getState(endpoint);
EmberAfStatus status;
Expand All @@ -505,14 +513,12 @@ static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t

if (state == NULL)
{
status = EMBER_ZCL_STATUS_FAILURE;
goto send_default_response;
return EMBER_ZCL_STATUS_FAILURE;
}

if (!shouldExecuteIfOff(endpoint, commandId, optionMask, optionOverride))
{
status = EMBER_ZCL_STATUS_SUCCESS;
goto send_default_response;
return EMBER_ZCL_STATUS_SUCCESS;
}

// Cancel any currently active command before fiddling with the state.
Expand All @@ -522,7 +528,7 @@ static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
emberAfLevelControlClusterPrintln("ERR: reading current level %x", status);
goto send_default_response;
return status;
}

state->commandId = commandId;
Expand Down Expand Up @@ -554,8 +560,7 @@ static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t
}
if (currentLevel == state->moveToLevel)
{
status = EMBER_ZCL_STATUS_SUCCESS;
goto send_default_response;
return EMBER_ZCL_STATUS_SUCCESS;
}
state->increasing = true;
actualStepSize = static_cast<uint8_t>(state->moveToLevel - currentLevel);
Expand All @@ -580,7 +585,7 @@ static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
emberAfLevelControlClusterPrintln("ERR: reading on/off transition time %x", status);
goto send_default_response;
return status;
}

// Transition time comes in (or is stored, in the case of On/Off Transition
Expand Down Expand Up @@ -629,11 +634,7 @@ static void moveToLevelHandler(EndpointId endpoint, CommandId commandId, uint8_t
}
}

send_default_response:
if (emberAfCurrentCommand()->apsFrame->clusterId == LevelControl::Id)
{
emberAfSendImmediateDefaultResponse(status);
}
return status;
}

static void moveHandler(CommandId commandId, uint8_t moveMode, uint8_t rate, uint8_t optionMask, uint8_t optionOverride)
Expand Down

0 comments on commit 5237681

Please sign in to comment.