Skip to content

Commit

Permalink
ICDHandler initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
thivya-amazon committed Dec 3, 2023
1 parent e6b7041 commit d792831
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 22 deletions.
31 changes: 20 additions & 11 deletions src/app/icd/ICDHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <lib/support/CodeUtils.h>
#include <messaging/Flags.h>
#include <protocols/Protocols.h>
#include <protocols/secure_channel/CheckinMessage.h>

#include <protocols/secure_channel/Constants.h>

namespace chip {
Expand All @@ -42,10 +42,12 @@ CheckInMessageHandler * CheckInMessageHandler::GetInstance()
return &sCheckInMessageHandler.get();
}

CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager)
CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage)
{
VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
mExchangeManager = exchangeManager;
VerifyOrReturnError(clientStorage != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
mExchangeManager = exchangeManager;
mICDClientStorage = static_cast<DefaultICDClientStorage *>(clientStorage);
ReturnErrorOnFailure(
exchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this));

Expand All @@ -54,7 +56,6 @@ CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeMana

void CheckInMessageHandler::Shutdown()
{
// TODO : If any timers are added in the future, they need to be cleared here
if (mExchangeManager)
{
mExchangeManager->UnregisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn);
Expand All @@ -73,16 +74,24 @@ CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHead
CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
System::PacketBufferHandle && payload)
{
// TODO : Pass the parsed payload to ICDClientManagement via callback
VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT);

Crypto::Aes128KeyHandle key;
chip::Protocols::SecureChannel::CounterType counter;
MutableByteSpan appData;
ByteSpan payloadByteSpan{ payload->Start(), payload->DataLength() };
chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(key, payloadByteSpan, counter, appData);

return CHIP_NO_ERROR;
auto * iterator = mICDClientStorage->IterateICDClientInfo();
CHIP_ERROR err;
uint32_t counter;
ICDClientInfo clientInfo;
while (iterator->Next(clientInfo))
{
err = mICDClientStorage->ProcessCheckInPayload(payloadByteSpan, clientInfo, &counter);
if (err == CHIP_NO_ERROR)
{
// TODO-1 : Check if the counter received is in range. If yes, proceed to TODO-2
// TODO-2 : Call the callback registered by the application to inform about the incoming checkin message
return err;
}
}
return err;
}

void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {}
Expand Down
4 changes: 3 additions & 1 deletion src/app/icd/ICDHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#pragma once

#include <client/DefaultICDClientStorage.h>
#include <lib/support/logging/CHIPLogging.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
Expand All @@ -50,7 +51,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi
*/
static CheckInMessageHandler * GetInstance(void);

CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager);
CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager, ICDClientStorage * clientStorage);
void Shutdown();

protected:
Expand All @@ -67,6 +68,7 @@ class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messagi
private:
Messaging::ExchangeManager * mExchangeManager = nullptr;
Messaging::ExchangeManager * GetExchangeManager(void) const { return mExchangeManager; }
DefaultICDClientStorage * mICDClientStorage = nullptr;
};

} // namespace app
Expand Down
1 change: 1 addition & 0 deletions src/app/icd/client/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ source_set("manager") {
"${chip_root}/src/app:app_config",
"${chip_root}/src/crypto",
"${chip_root}/src/lib/support",
"${chip_root}/src/protocols",
]
}
8 changes: 5 additions & 3 deletions src/app/icd/client/DefaultICDClientStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <lib/support/SafeInt.h>
#include <lib/support/logging/CHIPLogging.h>
#include <limits>
#include <protocols/secure_channel/CheckinMessage.h>

namespace {
// FabricIndex is uint8_t, the tlv size with anonymous tag is 1(control bytes) + 1(value) = 2
Expand Down Expand Up @@ -437,10 +438,11 @@ CHIP_ERROR DefaultICDClientStorage::DeleteAllEntries(FabricIndex fabricIndex)
return mpClientInfoStore->SyncDeleteKeyValue(DefaultStorageKeyAllocator::FabricICDClientInfoCounter(fabricIndex).KeyName());
}

CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo)
CHIP_ERROR DefaultICDClientStorage::ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter)
{
// TODO: Need to implement default decription code using CheckinMessage::ParseCheckinMessagePayload
return CHIP_NO_ERROR;
MutableByteSpan appData;
return chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(clientInfo.shared_key, payload, *counter,
appData);
}
} // namespace app
} // namespace chip
2 changes: 1 addition & 1 deletion src/app/icd/client/DefaultICDClientStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class DefaultICDClientStorage : public ICDClientStorage

CHIP_ERROR DeleteAllEntries(FabricIndex fabricIndex) override;

CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) override;
CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) override;

protected:
enum class ClientInfoTag : uint8_t
Expand Down
2 changes: 1 addition & 1 deletion src/app/icd/client/ICDClientStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ICDClientStorage
* @param[in] payload received checkIn Message payload
* @param[out] clientInfo retrieved matched clientInfo from storage
*/
virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo) = 0;
virtual CHIP_ERROR ProcessCheckInPayload(const ByteSpan & payload, ICDClientInfo & clientInfo, uint32_t * counter) = 0;
};
} // namespace app
} // namespace chip
6 changes: 3 additions & 3 deletions src/protocols/secure_channel/CheckinMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ CHIP_ERROR CheckinMessage::GenerateCheckinMessagePayload(Crypto::Aes128KeyHandle
return err;
}

CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, ByteSpan & payload, CounterType & counter,
MutableByteSpan & appData)
CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload,
CounterType & counter, MutableByteSpan & appData)
{
VerifyOrReturnError(payload.size() >= sMinPayloadSize, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(payload.size() <= (sMinPayloadSize + sMaxAppDataSize), CHIP_ERROR_INVALID_ARGUMENT);
Expand Down Expand Up @@ -92,7 +92,7 @@ CHIP_ERROR CheckinMessage::ParseCheckinMessagePayload(Crypto::Aes128KeyHandle &
return err;
}

size_t CheckinMessage::GetAppDataSize(ByteSpan & payload)
size_t CheckinMessage::GetAppDataSize(const ByteSpan & payload)
{
return (payload.size() <= sMinPayloadSize) ? 0 : payload.size() - sMinPayloadSize;
}
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/secure_channel/CheckinMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DLL_EXPORT CheckinMessage
* GetAppDataSize(payload) + sizeof(CounterType)
* @return CHIP_ERROR
*/
static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, ByteSpan & payload, CounterType & counter,
static CHIP_ERROR ParseCheckinMessagePayload(Crypto::Aes128KeyHandle & key, const ByteSpan & payload, CounterType & counter,
MutableByteSpan & appData);

static inline size_t GetCheckinPayloadSize(size_t appDataSize) { return appDataSize + sMinPayloadSize; }
Expand All @@ -76,7 +76,7 @@ class DLL_EXPORT CheckinMessage
* @param payload The undecrypted payload
* @return size_t size in byte of the application data from the payload
*/
static size_t GetAppDataSize(ByteSpan & payload);
static size_t GetAppDataSize(const ByteSpan & payload);

static constexpr uint16_t sMinPayloadSize =
CHIP_CRYPTO_AEAD_NONCE_LENGTH_BYTES + sizeof(CounterType) + CHIP_CRYPTO_AEAD_MIC_LENGTH_BYTES;
Expand Down

0 comments on commit d792831

Please sign in to comment.