Skip to content

Commit 92c6b1a

Browse files
Fetch next occupied credential index earlier in getCredentialStatusCommandHandler.
We want do to that before we actually fetch the credential we are trying to get the status of, since determining the next occupied credential index might stomp on data returned by emberAfPluginDoorLockGetCredential.
1 parent 83159c2 commit 92c6b1a

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

src/app/clusters/door-lock-server/door-lock-server.cpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,27 @@ void DoorLockServer::getCredentialStatusCommandHandler(chip::app::CommandHandler
860860
return;
861861
}
862862

863+
// Our response will need to include the index of the next occupied credential slot
864+
// after credentialIndex, if there is one.
865+
//
866+
// We want to figure this out before we call emberAfPluginDoorLockGetCredential, because to do
867+
// so we will also need to call emberAfPluginDoorLockGetCredential, and the
868+
// EmberAfPluginDoorLockCredentialInfo we get might be pointing into some application-static
869+
// buffers (for its credential data and whatnot).
870+
DataModel::Nullable<uint16_t> nextCredentialIndex;
871+
{
872+
uint16_t foundNextCredentialIndex;
873+
if (findOccupiedCredentialSlot(commandPath.mEndpointId, credentialType, static_cast<uint16_t>(credentialIndex + 1),
874+
foundNextCredentialIndex))
875+
{
876+
nextCredentialIndex.SetNonNull(foundNextCredentialIndex);
877+
}
878+
}
879+
863880
uint16_t maxNumberOfCredentials = 0;
864881
if (!credentialIndexValid(commandPath.mEndpointId, credentialType, credentialIndex, maxNumberOfCredentials))
865882
{
866-
sendGetCredentialResponse(commandObj, commandPath, credentialType, credentialIndex, 0, nullptr, false);
883+
sendGetCredentialResponse(commandObj, commandPath, credentialType, credentialIndex, nextCredentialIndex, 0, nullptr, false);
867884
return;
868885
}
869886

@@ -896,8 +913,8 @@ void DoorLockServer::getCredentialStatusCommandHandler(chip::app::CommandHandler
896913
}
897914
}
898915

899-
sendGetCredentialResponse(commandObj, commandPath, credentialType, credentialIndex, userIndexWithCredential, &credentialInfo,
900-
credentialExists);
916+
sendGetCredentialResponse(commandObj, commandPath, credentialType, credentialIndex, nextCredentialIndex,
917+
userIndexWithCredential, &credentialInfo, credentialExists);
901918
}
902919

903920
namespace {
@@ -918,9 +935,12 @@ bool IsAliroCredentialType(CredentialTypeEnum credentialType)
918935
void DoorLockServer::sendGetCredentialResponse(chip::app::CommandHandler * commandObj,
919936
const chip::app::ConcreteCommandPath & commandPath,
920937
CredentialTypeEnum credentialType, uint16_t credentialIndex,
921-
uint16_t userIndexWithCredential,
938+
DataModel::Nullable<uint16_t> nextCredentialIndex, uint16_t userIndexWithCredential,
922939
EmberAfPluginDoorLockCredentialInfo * credentialInfo, bool credentialExists)
923940
{
941+
// Important: We have to make sure nothing in this function calls
942+
// emberAfPluginDoorLockGetCredential, because that might stomp on the data
943+
// pointed to by credentialInfo.
924944
Commands::GetCredentialStatusResponse::Type response{ .credentialExists = credentialExists };
925945
if (credentialExists && !(nullptr == credentialInfo))
926946
{
@@ -949,19 +969,14 @@ void DoorLockServer::sendGetCredentialResponse(chip::app::CommandHandler * comma
949969
response.credentialData.Emplace(NullNullable);
950970
}
951971
}
952-
uint16_t nextCredentialIndex = 0;
953-
if (findOccupiedCredentialSlot(commandPath.mEndpointId, credentialType, static_cast<uint16_t>(credentialIndex + 1),
954-
nextCredentialIndex))
955-
{
956-
response.nextCredentialIndex.SetNonNull(nextCredentialIndex);
957-
}
972+
response.nextCredentialIndex = nextCredentialIndex;
958973
commandObj->AddResponse(commandPath, response);
959974

960975
ChipLogProgress(Zcl,
961976
"[GetCredentialStatus] Prepared credential status "
962977
"[endpointId=%d,credentialType=%u,credentialIndex=%d,userIndex=%d,nextCredentialIndex=%d]",
963978
commandPath.mEndpointId, to_underlying(credentialType), credentialIndex, userIndexWithCredential,
964-
nextCredentialIndex);
979+
nextCredentialIndex.ValueOr(0));
965980
}
966981

967982
void DoorLockServer::clearCredentialCommandHandler(

src/app/clusters/door-lock-server/door-lock-server.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ class DoorLockServer : public chip::app::AttributeAccessInterface
449449
uint16_t credentialIndex);
450450

451451
void sendGetCredentialResponse(chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,
452-
CredentialTypeEnum credentialType, uint16_t credentialIndex, uint16_t userIndexWithCredential,
452+
CredentialTypeEnum credentialType, uint16_t credentialIndex,
453+
chip::app::DataModel::Nullable<uint16_t> nextCredentialIndex, uint16_t userIndexWithCredential,
453454
EmberAfPluginDoorLockCredentialInfo * credentialInfo, bool credentialExists);
454455

455456
void clearCredentialCommandHandler(chip::app::CommandHandler * commandObj, const chip::app::ConcreteCommandPath & commandPath,

0 commit comments

Comments
 (0)