Skip to content

Commit

Permalink
Store UserActiveModeTriggerBitmap into ICD storage
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Dec 1, 2023
1 parent 44ea4eb commit 84dfc33
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/app/icd/client/DefaultICDClientStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ CHIP_ERROR DefaultICDClientStorage::Load(FabricIndex fabricIndex, std::vector<IC
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kMonitoredSubject)));
ReturnErrorOnFailure(reader.Get(clientInfo.monitored_subject));

// UserActiveModeTriggerBitmap
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kUserActiveModeTriggerBitmap)));
ReturnErrorOnFailure(reader.Get(clientInfo.user_active_mode_trigger_bitmap));

// Shared key
ReturnErrorOnFailure(reader.Next(TLV::ContextTag(ClientInfoTag::kSharedKey)));
ByteSpan buf;
Expand Down Expand Up @@ -299,6 +303,7 @@ 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::kUserActiveModeTriggerBitmap), clientInfo.user_active_mode_trigger_bitmap));
ByteSpan buf(clientInfo.shared_key.As<Crypto::Aes128KeyByteArray>());
ReturnErrorOnFailure(writer.Put(TLV::ContextTag(ClientInfoTag::kSharedKey), buf));
ReturnErrorOnFailure(writer.EndContainer(ICDClientInfoContainerType));
Expand Down
3 changes: 2 additions & 1 deletion src/app/icd/client/DefaultICDClientStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class DefaultICDClientStorage : public ICDClientStorage
kStartICDCounter = 3,
kOffset = 4,
kMonitoredSubject = 5,
kSharedKey = 6
kUserActiveModeTriggerBitmap = 6,
kSharedKey = 7
};

enum class CounterTag : uint8_t
Expand Down
2 changes: 2 additions & 0 deletions src/app/icd/client/ICDClientInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct ICDClientInfo
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_bitmap = 0;
Crypto::Aes128KeyHandle shared_key = Crypto::Aes128KeyHandle();

ICDClientInfo() {}
Expand All @@ -44,6 +45,7 @@ struct ICDClientInfo
start_icd_counter = other.start_icd_counter;
offset = other.offset;
monitored_subject = other.monitored_subject;
user_active_mode_trigger_bitmap = other.user_active_mode_trigger_bitmap;
ByteSpan buf(other.shared_key.As<Crypto::Aes128KeyByteArray>());
memcpy(shared_key.AsMutable<Crypto::Aes128KeyByteArray>(), buf.data(), sizeof(Crypto::Aes128KeyByteArray));
return *this;
Expand Down
95 changes: 95 additions & 0 deletions src/app/icd/client/ICDClientInfoPersistentStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "ICDClientInfo.h"
#include "ICDStorageKeyDelegate.h"
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPConfig.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/ScopedNodeId.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/CommonIterator.h>
#include <lib/support/StorageKeyName.h>
#include <stddef.h>

namespace chip {
namespace app {

/**
* The ICDClientStorage class is an abstract interface that defines the operations
* for storing, retrieving and deleting ICD client information in persistent storage.
*/
class ICDClientStorage
{
public:
using ICDClientInfoIterator = CommonIterator<ICDClientInfo>;

virtual ~ICDClientStorage() = default;

/**
* Iterate through persisted ICD Client Info
*
* @return A valid iterator on success. Use CommonIterator accessor to retrieve ICDClientInfo
*/
virtual ICDClientInfoIterator * IterateICDClientInfo() = 0;

/**
* Called during ICD device registration in commissioning, sdk provides the clientInfo and application
* provides raw key data, the shared key handle in clientInfo is updated based upon raw key data
*
* @param[inout] aICDClientInfo the ICD Client information to be updated with keyData and be saved
* @param[in] aKeyData raw key data provided by application
*/
virtual CHIP_ERROR SetKey(ICDClientInfo & aClientInfo, const ByteSpan aKeyData) = 0;

/**
* Store updated ICD ClientInfo to storage when ICD registration completes or check-in message
* comes.
*
* @param[in] aICDClientInfo the updated ICD Client Info.
*/
virtual CHIP_ERROR StoreEntry(ICDClientInfo & aICDClientInfo) = 0;
/**
* Delete ICD Client persistent information associated with the specified scoped node Id.
* when ICD device is unpaired/removed, the corresponding entry in ICD storage is removed.
* @param aPeerNodeId scoped node with peer node id and fabric index
*/
virtual CHIP_ERROR DeleteEntry(ScopedNodeId aPeerNodeId) = 0;

/**
* Remove all ICDClient persistent informations associated with the specified
* fabric index. If no entries for the fabric index exist, this is a no-op
* and is considered successful.
* When the whole fabric is removed, all entries from persistent storage in current fabric index is removed.
*
* @param[in] fabricIndex the index of the fabric for which to remove ICDClient persistent information
*/
virtual CHIP_ERROR DeleteAllEntries(FabricIndex aFabricIndex) = 0;

/**
* Validate received ICD Check-in message payload, return true when the key decription is successful
* Consumer has to provide keys and see whether it's the right key, then return the correponding ICDClientInfo
* @param[in] aPayload received checkIn Message payload
* @param[out] ICDClientInfo matched clientInfo
*/
virtual bool ValidateCheckInPayload(const ByteSpan & aPayload, ICDClientInfo & aClientInfo) = 0;
};
} // namespace app
} // namespace chip

0 comments on commit 84dfc33

Please sign in to comment.