Skip to content

Commit 4ca1f29

Browse files
committed
samples: matter: lock: Extract lambda to separate function
Address SonarQube issue regarding too long lambda body. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent fa2d1fd commit 4ca1f29

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

samples/matter/lock/src/app_task.cpp

+48-45
Original file line numberDiff line numberDiff line change
@@ -176,54 +176,57 @@ void AppTask::UpdateClusterState(const BoltLockManager::StateData &stateData)
176176
return;
177177
}
178178

179-
SystemLayer().ScheduleLambda([stateData = stateDataCopy]() {
180-
DlLockState newLockState;
181-
182-
switch (stateData->state) {
183-
case BoltLockManager::State::kLockingCompleted:
184-
newLockState = DlLockState::kLocked;
185-
break;
186-
case BoltLockManager::State::kUnlockingCompleted:
187-
newLockState = DlLockState::kUnlocked;
188-
break;
189-
default:
190-
newLockState = DlLockState::kNotFullyLocked;
191-
break;
192-
}
179+
SystemLayer().ScheduleLambda([stateDataCopy]() {
180+
UpdateClusterStateHandler(*stateDataCopy);
181+
Platform::Delete(stateDataCopy);
182+
});
183+
}
184+
185+
void AppTask::UpdateClusterStateHandler(const BoltLockManager::StateData &stateData)
186+
{
187+
DlLockState newLockState;
188+
189+
switch (stateData.state) {
190+
case BoltLockManager::State::kLockingCompleted:
191+
newLockState = DlLockState::kLocked;
192+
break;
193+
case BoltLockManager::State::kUnlockingCompleted:
194+
newLockState = DlLockState::kUnlocked;
195+
break;
196+
default:
197+
newLockState = DlLockState::kNotFullyLocked;
198+
break;
199+
}
200+
201+
chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> currentLockState;
202+
chip::app::Clusters::DoorLock::Attributes::LockState::Get(kLockEndpointId, currentLockState);
203+
204+
if (currentLockState.IsNull()) {
205+
/* Initialize lock state with start value, but not invoke lock/unlock. */
206+
chip::app::Clusters::DoorLock::Attributes::LockState::Set(kLockEndpointId, newLockState);
207+
} else {
208+
LOG_INF("Updating LockState attribute");
193209

194-
chip::app::DataModel::Nullable<chip::app::Clusters::DoorLock::DlLockState> currentLockState;
195-
chip::app::Clusters::DoorLock::Attributes::LockState::Get(kLockEndpointId, currentLockState);
196-
197-
if (currentLockState.IsNull()) {
198-
/* Initialize lock state with start value, but not invoke lock/unlock. */
199-
chip::app::Clusters::DoorLock::Attributes::LockState::Set(kLockEndpointId, newLockState);
200-
} else {
201-
LOG_INF("Updating LockState attribute");
202-
203-
Nullable<uint16_t> userId;
204-
Nullable<List<const LockOpCredentials>> credentials;
205-
List<const LockOpCredentials> credentialList;
206-
207-
if (!stateData->validatePINResult.IsNull()) {
208-
userId = { stateData->validatePINResult.Value().userId };
209-
210-
/* `DoorLockServer::SetLockState` exptects list of `LockOpCredentials`,
211-
however in case of PIN validation it makes no sense to have more than one
212-
credential corresponding to validation result. For simplicity we wrap single
213-
credential in list here. */
214-
credentialList = { &stateData->validatePINResult.Value().credential, 1 };
215-
credentials = { credentialList };
216-
}
217-
218-
if (!DoorLockServer::Instance().SetLockState(kLockEndpointId, newLockState, stateData->source,
219-
userId, credentials, stateData->fabricIdx,
220-
stateData->nodeId)) {
221-
LOG_ERR("Failed to update LockState attribute");
222-
}
210+
Nullable<uint16_t> userId;
211+
Nullable<List<const LockOpCredentials>> credentials;
212+
List<const LockOpCredentials> credentialList;
213+
214+
if (!stateData.validatePINResult.IsNull()) {
215+
userId = { stateData.validatePINResult.Value().userId };
216+
217+
/* `DoorLockServer::SetLockState` exptects list of `LockOpCredentials`,
218+
however in case of PIN validation it makes no sense to have more than one
219+
credential corresponding to validation result. For simplicity we wrap single
220+
credential in list here. */
221+
credentialList = { &stateData.validatePINResult.Value().credential, 1 };
222+
credentials = { credentialList };
223223
}
224224

225-
Platform::Delete(stateData);
226-
});
225+
if (!DoorLockServer::Instance().SetLockState(kLockEndpointId, newLockState, stateData.source, userId,
226+
credentials, stateData.fabricIdx, stateData.nodeId)) {
227+
LOG_ERR("Failed to update LockState attribute");
228+
}
229+
}
227230
}
228231

229232
#ifdef CONFIG_CHIP_NUS

samples/matter/lock/src/app_task.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AppTask {
4141
static void LockActionEventHandler();
4242
static void ButtonEventHandler(Nrf::ButtonState state, Nrf::ButtonMask hasChanged);
4343
static void LockStateChanged(const BoltLockManager::StateData &stateData);
44+
static void UpdateClusterStateHandler(const BoltLockManager::StateData &stateData);
4445

4546
#ifdef CONFIG_THREAD_WIFI_SWITCHING
4647
static void SwitchTransportEventHandler();

0 commit comments

Comments
 (0)