From 4bf3365093234a9d52db01549fe246027a432693 Mon Sep 17 00:00:00 2001 From: Thivya Ashokkumar Date: Wed, 29 Nov 2023 15:26:42 -0800 Subject: [PATCH] ICDHandler initialization --- src/app/icd/ICDHandler.cpp | 93 ++++++++++++++++++++++++++++++++++++++ src/app/icd/ICDHandler.h | 73 ++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 src/app/icd/ICDHandler.cpp create mode 100644 src/app/icd/ICDHandler.h diff --git a/src/app/icd/ICDHandler.cpp b/src/app/icd/ICDHandler.cpp new file mode 100644 index 00000000000000..9516752f1d6653 --- /dev/null +++ b/src/app/icd/ICDHandler.cpp @@ -0,0 +1,93 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * All rights reserved. + * + * 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. + */ + +/** + * @file + * This file defines objects for a CHIP ICD handler which handles unsolicited checkin messages. + * + */ + +#include "ICDHandler.h" + +#include + +#include +#include +#include +#include +#include +#include + +namespace chip { +namespace app { + +static Global sCheckInMessageHandler; +CheckInMessageHandler * CheckInMessageHandler::GetInstance() +{ + return &sCheckInMessageHandler.get(); +} + +CHIP_ERROR CheckInMessageHandler::Init(Messaging::ExchangeManager * exchangeManager) +{ + VerifyOrReturnError(exchangeManager != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + mExchangeManager = exchangeManager; + ReturnErrorOnFailure( + exchangeManager->RegisterUnsolicitedMessageHandlerForType(Protocols::SecureChannel::MsgType::ICD_CheckIn, this)); + + return CHIP_NO_ERROR; +} + +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); + mExchangeManager = nullptr; + } +} +CHIP_ERROR CheckInMessageHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) +{ + // Return error for wrong message type + VerifyOrReturnError(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn), CHIP_ERROR_INVALID_ARGUMENT); + + newDelegate = this; + return CHIP_NO_ERROR; +} + +CHIP_ERROR CheckInMessageHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) +{ + // TODO : Pass the parsed payload to ICDClientManagement via callback + VerifyOrReturn(payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::ICD_CheckIn)); + + Crypto::Aes128KeyHandle key; + chip::Protocols::SecureChannel::CounterType counter; + MutableByteSpan appData; + uint8_t checkInPayload[chip::Protocols::SecureChannel::CheckinMessage::sMinPayloadSize]; + memcpy(&checkInPayload, payload->Start(), sizeof(checkInPayload)); + chip::ByteSpan payloadByteSpan(checkInPayload); + chip::Protocols::SecureChannel::CheckinMessage::ParseCheckinMessagePayload(key, payloadByteSpan, counter, appData); + + return CHIP_NO_ERROR; +} + +void CheckInMessageHandler::OnResponseTimeout(Messaging::ExchangeContext * ec) {} + +} // namespace app +} // namespace chip diff --git a/src/app/icd/ICDHandler.h b/src/app/icd/ICDHandler.h new file mode 100644 index 00000000000000..064d65c1957448 --- /dev/null +++ b/src/app/icd/ICDHandler.h @@ -0,0 +1,73 @@ +/* + * + * Copyright (c) 2020 Project CHIP Authors + * All rights reserved. + * + * 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. + */ + +/** + * @file + * This file defines objects for a CHIP CheckInMessage unsolicited + * handler + * + */ + +#pragma once + +#include +#include +#include + +namespace chip { +namespace app { +class CheckInMessageHandler : public Messaging::ExchangeDelegate, public Messaging::UnsolicitedMessageHandler +{ + class Callback + { + public: + virtual ~Callback() = default; + + // TODO : Include the callback message from ICDClientManagement + }; + +public: + /** + * @brief Retrieve the singleton CheckIn handler + * + * @return A pointer to the shared CheckIn handler + * + */ + static CheckInMessageHandler * GetInstance(void); + + CHIP_ERROR Init(Messaging::ExchangeManager * exchangeManager); + void Shutdown(); + +protected: + // ExchangeDelegate + CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader, + System::PacketBufferHandle && payload) override; + + // UnsolicitedMessageHandler + CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override; + + // TODO : Follow up to check if this really needs to be a pure virtual function in Exchange delegate + void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + +private: + Messaging::ExchangeManager * mExchangeManager = nullptr; + Messaging::ExchangeManager * GetExchangeManager(void) const { return mExchangeManager; } +}; + +} // namespace app +} // namespace chip