Skip to content

Commit

Permalink
[ICD] Store UserActiveModeTriggerHint and instruction into ICD storage (
Browse files Browse the repository at this point in the history
#30770)

* Store UserActiveModeTriggerBitmap and instruction into ICD storage

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 24, 2024
1 parent 3194834 commit 1541842
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 21 deletions.
32 changes: 30 additions & 2 deletions src/app/icd/client/DefaultICDClientStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,27 @@ CHIP_ERROR DefaultICDClientStorage::Load(FabricIndex fabricIndex, std::vector<IC
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kMonitoredSubject)));
ReturnErrorOnFailure(reader.Get(clientInfo.monitored_subject));

// Shared key
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kSharedKey)));
// UserActiveModeTriggerHint
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kUserActiveModeTriggerHint)));
ReturnErrorOnFailure(reader.Get(clientInfo.user_active_mode_trigger_hint));

// UserActiveModeTriggerInstruction
ReturnErrorOnFailure(reader.Next());
err = reader.Expect(TLV::ContextTag(ClientInfoTag::kUserActiveModeTriggerInstruction));
if (err == CHIP_NO_ERROR)
{
ReturnErrorOnFailure(
reader.GetString(clientInfo.user_active_mode_trigger_instruction, kUserActiveModeTriggerInstructionSize));
clientInfo.has_instruction = true;
// Shared key
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kSharedKey)));
}
else if (err == CHIP_ERROR_UNEXPECTED_TLV_ELEMENT)
{
err = reader.Expect(TLV::ContextTag(ClientInfoTag::kSharedKey));
}
ReturnErrorOnFailure(err);

ByteSpan buf;
ReturnErrorOnFailure(reader.Get(buf));
VerifyOrReturnError(buf.size() == sizeof(Crypto::Symmetric128BitsKeyByteArray), CHIP_ERROR_INTERNAL);
Expand Down Expand Up @@ -301,6 +320,15 @@ CHIP_ERROR DefaultICDClientStorage::SerializeToTlv(TLV::TLVWriter & writer, cons
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(ClientInfoTag::kStartICDCounter), clientInfo.start_icd_counter));
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(ClientInfoTag::kOffset), clientInfo.offset));
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(ClientInfoTag::kMonitoredSubject), clientInfo.monitored_subject));
ReturnErrorOnFailure(
writer.Put(TLV::ContextTag(ClientInfoTag::kUserActiveModeTriggerHint), clientInfo.user_active_mode_trigger_hint));
if (clientInfo.has_instruction)
{
ReturnErrorOnFailure(writer.PutString(TLV::ContextTag(ClientInfoTag::kUserActiveModeTriggerInstruction),
clientInfo.user_active_mode_trigger_instruction,
static_cast<uint32_t>(strlen(clientInfo.user_active_mode_trigger_instruction))));
}

ByteSpan buf(clientInfo.shared_key.As<Crypto::Symmetric128BitsKeyByteArray>());
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(ClientInfoTag::kSharedKey), buf));
ReturnErrorOnFailure(writer.EndContainer(ICDClientInfoContainerType));
Expand Down
17 changes: 10 additions & 7 deletions src/app/icd/client/DefaultICDClientStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ class DefaultICDClientStorage : public ICDClientStorage
protected:
enum class ClientInfoTag : uint8_t
{
kPeerNodeId = 1,
kFabricIndex = 2,
kStartICDCounter = 3,
kOffset = 4,
kMonitoredSubject = 5,
kSharedKey = 6
kPeerNodeId = 1,
kFabricIndex = 2,
kStartICDCounter = 3,
kOffset = 4,
kMonitoredSubject = 5,
kUserActiveModeTriggerHint = 6,
kUserActiveModeTriggerInstruction = 7,
kSharedKey = 8
};

enum class CounterTag : uint8_t
Expand Down Expand Up @@ -100,7 +102,8 @@ class DefaultICDClientStorage : public ICDClientStorage
{
// All the fields added together
return TLV::EstimateStructOverhead(sizeof(NodeId), sizeof(FabricIndex), sizeof(uint32_t), sizeof(uint32_t),
sizeof(uint64_t), sizeof(Crypto::Symmetric128BitsKeyByteArray));
sizeof(uint64_t), sizeof(uint32_t), kUserActiveModeTriggerInstructionSize,
sizeof(Crypto::Symmetric128BitsKeyByteArray));
}

static constexpr size_t MaxICDCounterSize()
Expand Down
30 changes: 22 additions & 8 deletions src/app/icd/client/ICDClientInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,40 @@
#include <lib/support/CodeUtils.h>
#include <stddef.h>

namespace {
constexpr size_t kUserActiveModeTriggerInstructionSize = 128;
} // namespace

namespace chip {
namespace app {

struct ICDClientInfo
{
ScopedNodeId peer_node;
uint32_t start_icd_counter = 0;
uint32_t offset = 0;
uint64_t monitored_subject = static_cast<uint64_t>(0);
Crypto::Aes128KeyHandle shared_key = Crypto::Aes128KeyHandle();
uint32_t start_icd_counter = 0;
uint32_t offset = 0;
uint64_t monitored_subject = static_cast<uint64_t>(0);
uint32_t user_active_mode_trigger_hint = 0;
char user_active_mode_trigger_instruction[kUserActiveModeTriggerInstructionSize] = { 0 };
bool has_instruction = false;
Crypto::Aes128KeyHandle shared_key = Crypto::Aes128KeyHandle();

ICDClientInfo() {}
ICDClientInfo(const ICDClientInfo & other) { *this = other; }

ICDClientInfo & operator=(const ICDClientInfo & other)
{
peer_node = other.peer_node;
start_icd_counter = other.start_icd_counter;
offset = other.offset;
monitored_subject = other.monitored_subject;
peer_node = other.peer_node;
start_icd_counter = other.start_icd_counter;
offset = other.offset;
monitored_subject = other.monitored_subject;
user_active_mode_trigger_hint = other.user_active_mode_trigger_hint;
if (other.has_instruction)
{
memcpy(user_active_mode_trigger_instruction, other.user_active_mode_trigger_instruction,
kUserActiveModeTriggerInstructionSize);
}
has_instruction = other.has_instruction;
ByteSpan buf(other.shared_key.As<Crypto::Symmetric128BitsKeyByteArray>());
memcpy(shared_key.AsMutable<Crypto::Symmetric128BitsKeyByteArray>(), buf.data(),
sizeof(Crypto::Symmetric128BitsKeyByteArray));
Expand Down
16 changes: 12 additions & 4 deletions src/app/tests/TestDefaultICDClientStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ void TestClientInfoCount(nlTestSuite * apSuite, void * apContext)
// Write some ClientInfos and see the counts are correct
ICDClientInfo clientInfo1;
clientInfo1.peer_node = ScopedNodeId(nodeId1, fabricId);
char val[5] = "test";
ICDClientInfo clientInfo2;
clientInfo2.peer_node = ScopedNodeId(nodeId2, fabricId);
clientInfo2.peer_node = ScopedNodeId(nodeId2, fabricId);
clientInfo2.user_active_mode_trigger_hint = 1;
memcpy(clientInfo2.user_active_mode_trigger_instruction, val, sizeof(val));
clientInfo2.has_instruction = true;
ICDClientInfo clientInfo3;
clientInfo3.peer_node = ScopedNodeId(nodeId1, fabricId);
err = manager.SetKey(clientInfo1, ByteSpan(kKeyBuffer1));
clientInfo3.peer_node = ScopedNodeId(nodeId1, fabricId);
clientInfo3.user_active_mode_trigger_hint = 2;
err = manager.SetKey(clientInfo1, ByteSpan(kKeyBuffer1));
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
err = manager.StoreEntry(clientInfo1);
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
Expand All @@ -97,9 +102,12 @@ void TestClientInfoCount(nlTestSuite * apSuite, void * apContext)
ICDClientInfo clientInfo;
NL_TEST_ASSERT(apSuite, iterator->Next(clientInfo));
NL_TEST_ASSERT(apSuite, clientInfo.peer_node.GetNodeId() == nodeId2);
NL_TEST_ASSERT(apSuite, clientInfo.has_instruction);
NL_TEST_ASSERT(apSuite, strcmp(clientInfo.user_active_mode_trigger_instruction, val) == 0);
NL_TEST_ASSERT(apSuite, iterator->Next(clientInfo));
NL_TEST_ASSERT(apSuite, clientInfo.peer_node.GetNodeId() == nodeId1);

NL_TEST_ASSERT(apSuite, clientInfo.user_active_mode_trigger_hint == 2);
NL_TEST_ASSERT(apSuite, !clientInfo.has_instruction);
iterator->Release();

// Delete all and verify iterator counts 0
Expand Down

0 comments on commit 1541842

Please sign in to comment.