Skip to content

Commit

Permalink
[chip-tool][ICD] Store ICDClientInfo when running icd registration in…
Browse files Browse the repository at this point in the history
…dependently (#33690)

* Store ICDClientInfo when running icd registration independently

* Restyled by clang-format

* address comments

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jun 13, 2024
1 parent 5496bd4 commit 6929668
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
39 changes: 36 additions & 3 deletions examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

#pragma once

#include <app/tests/suites/commands/interaction_model/InteractionModel.h>

#include "DataModelLogger.h"
#include "ModelCommand.h"
#include <app/tests/suites/commands/interaction_model/InteractionModel.h>

class ClusterCommand : public InteractionModelCommands, public ModelCommand, public chip::app::CommandSender::Callback
{
Expand Down Expand Up @@ -64,6 +63,17 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
return CHIP_NO_ERROR;
}

CHIP_ERROR SendCommand(chip::DeviceProxy * device, chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::CommandId commandId,
const chip::app::Clusters::IcdManagement::Commands::RegisterClient::Type & value)
{
ReturnErrorOnFailure(InteractionModelCommands::SendCommand(device, endpointId, clusterId, commandId, value));
mScopedNodeId = chip::ScopedNodeId(value.checkInNodeID, device->GetSecureSession().Value()->GetFabricIndex());
mMonitoredSubject = value.monitoredSubject;
memcpy(mICDSymmetricKey, value.key.data(), value.key.size());
return CHIP_NO_ERROR;
}

CHIP_ERROR SendCommand(chip::DeviceProxy * device, chip::EndpointId endpointId, chip::ClusterId clusterId,
chip::CommandId commandId,
const chip::app::Clusters::DiagnosticLogs::Commands::RetrieveLogsRequest::Type & value)
Expand Down Expand Up @@ -117,11 +127,32 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
mError = error;
return;
}
if ((path.mEndpointId == chip::kRootEndpointId) && (path.mClusterId == chip::app::Clusters::IcdManagement::Id) &&
(path.mCommandId == chip::app::Clusters::IcdManagement::Commands::RegisterClient::Id))
{
chip::TLV::TLVReader counterTlvReader;
counterTlvReader.Init(*data);
chip::app::Clusters::IcdManagement::Commands::RegisterClientResponse::DecodableType value;
CHIP_ERROR err = chip::app::DataModel::Decode(counterTlvReader, value);
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Failed to decode ICD counter: %" CHIP_ERROR_FORMAT, err.Format());
return;
}

chip::app::ICDClientInfo clientInfo;
clientInfo.peer_node = mScopedNodeId;
clientInfo.monitored_subject = mMonitoredSubject;
clientInfo.start_icd_counter = value.ICDCounter;

StoreICDEntryWithKey(clientInfo, chip::ByteSpan(mICDSymmetricKey));
}
}

if ((path.mEndpointId == chip::kRootEndpointId) && (path.mClusterId == chip::app::Clusters::IcdManagement::Id) &&
(path.mCommandId == chip::app::Clusters::IcdManagement::Commands::UnregisterClient::Id))
{
ModelCommand::ClearICDEntry(mScopedNodeId);
ClearICDEntry(mScopedNodeId);
}
}

Expand Down Expand Up @@ -223,6 +254,8 @@ class ClusterCommand : public InteractionModelCommands, public ModelCommand, pub
chip::ClusterId mClusterId;
chip::CommandId mCommandId;
chip::ScopedNodeId mScopedNodeId;
uint64_t mMonitoredSubject = static_cast<uint64_t>(0);
uint8_t mICDSymmetricKey[chip::Crypto::kAES_CCM128_Key_Length];
CHIP_ERROR mError = CHIP_NO_ERROR;
CustomArgument mPayload;
};
18 changes: 17 additions & 1 deletion examples/chip-tool/commands/clusters/ModelCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void ModelCommand::Shutdown()
CHIPCommand::Shutdown();
}

void ModelCommand::ClearICDEntry(const chip::ScopedNodeId & nodeId)
void ModelCommand::ClearICDEntry(const ScopedNodeId & nodeId)
{
CHIP_ERROR deleteEntryError = CHIPCommand::sICDClientStorage.DeleteEntry(nodeId);
if (deleteEntryError != CHIP_NO_ERROR)
Expand All @@ -85,6 +85,22 @@ void ModelCommand::ClearICDEntry(const chip::ScopedNodeId & nodeId)
}
}

void ModelCommand::StoreICDEntryWithKey(app::ICDClientInfo & clientInfo, ByteSpan key)
{
CHIP_ERROR err = CHIPCommand::sICDClientStorage.SetKey(clientInfo, key);
if (err == CHIP_NO_ERROR)
{
err = CHIPCommand::sICDClientStorage.StoreEntry(clientInfo);
}

if (err != CHIP_NO_ERROR)
{
CHIPCommand::sICDClientStorage.RemoveKey(clientInfo);
ChipLogError(chipTool, "Failed to persist symmetric key with error: %" CHIP_ERROR_FORMAT, err.Format());
return;
}
}

void ModelCommand::CheckPeerICDType()
{
if (mIsPeerLIT.HasValue())
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/clusters/ModelCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class ModelCommand : public CHIPCommand

virtual CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex) { return CHIP_ERROR_BAD_REQUEST; };

virtual void ClearICDEntry(const chip::ScopedNodeId & nodeId);

void ClearICDEntry(const chip::ScopedNodeId & nodeId);
void StoreICDEntryWithKey(chip::app::ICDClientInfo & clientinfo, chip::ByteSpan key);
void Shutdown() override;

protected:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ void ModelCommand::Shutdown()
mOnDeviceConnectionFailureCallback.Cancel();
}

void ModelCommand::ClearICDEntry(const chip::ScopedNodeId & nodeId)
void ModelCommand::ClearICDEntry(const ScopedNodeId & nodeId)
{
ChipLogError(chipTool, "ClearICDEntry is not implemented in tv-casting-app");
}

void ModelCommand::StoreICDEntryWithKey(app::ICDClientInfo & clientinfo, ByteSpan key)
{
ChipLogError(chipTool, "StoreICDEntryWithKey is not implemented in tv-casting-app");
}

bool ModelCommand::IsPeerLIT()
{
// Does not support tv-casting-app
Expand Down

0 comments on commit 6929668

Please sign in to comment.