|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "ICDCheckInSender.h" |
| 19 | + |
| 20 | +#include <system/SystemPacketBuffer.h> |
| 21 | + |
| 22 | +#include <protocols/secure_channel/CheckinMessage.h> |
| 23 | + |
| 24 | +#include <lib/dnssd/Resolver.h> |
| 25 | + |
| 26 | +namespace chip { |
| 27 | +namespace app { |
| 28 | + |
| 29 | +using namespace Protocols::SecureChannel; |
| 30 | + |
| 31 | +ICDCheckInSender::ICDCheckInSender(Messaging::ExchangeManager * exchangeManager) |
| 32 | +{ |
| 33 | + VerifyOrDie(exchangeManager != nullptr); |
| 34 | + mExchangeManager = exchangeManager; |
| 35 | + mAddressLookupHandle.SetListener(this); |
| 36 | +} |
| 37 | + |
| 38 | +void ICDCheckInSender::OnNodeAddressResolved(const PeerId & peerId, const AddressResolve::ResolveResult & result) |
| 39 | +{ |
| 40 | + mResolveInProgress = false; |
| 41 | + |
| 42 | + VerifyOrReturn(CHIP_NO_ERROR != SendCheckInMsg(result.address), |
| 43 | + ChipLogError(AppServer, "Failed to send the ICD Check-In message")); |
| 44 | +} |
| 45 | + |
| 46 | +void ICDCheckInSender::OnNodeAddressResolutionFailed(const PeerId & peerId, CHIP_ERROR reason) |
| 47 | +{ |
| 48 | + mResolveInProgress = false; |
| 49 | + ChipLogProgress(AppServer, "Node Address resolution failed for ICD Check-In with Node ID " ChipLogFormatX64, |
| 50 | + ChipLogValueX64(peerId.GetNodeId())); |
| 51 | +} |
| 52 | + |
| 53 | +CHIP_ERROR ICDCheckInSender::SendCheckInMsg(const Transport::PeerAddress & addr) |
| 54 | +{ |
| 55 | + System::PacketBufferHandle buffer = MessagePacketBuffer::New(CheckinMessage::sMinPayloadSize); |
| 56 | + |
| 57 | + VerifyOrReturnError(!buffer.IsNull(), CHIP_ERROR_NO_MEMORY); |
| 58 | + MutableByteSpan output{ buffer->Start(), buffer->DataLength() }; |
| 59 | + |
| 60 | + // TODO retrieve Check-in counter |
| 61 | + CounterType counter = 0; |
| 62 | + |
| 63 | + ReturnErrorOnFailure(CheckinMessage::GenerateCheckinMessagePayload(mKey, counter, ByteSpan(), output)); |
| 64 | + |
| 65 | + VerifyOrReturnError(mExchangeManager->GetSessionManager() != nullptr, CHIP_ERROR_INTERNAL); |
| 66 | + |
| 67 | + Optional<SessionHandle> session = |
| 68 | + mExchangeManager->GetSessionManager()->CreateUnauthenticatedSession(addr, GetDefaultMRPConfig()); |
| 69 | + VerifyOrReturnError(session.HasValue(), CHIP_ERROR_NO_MEMORY); |
| 70 | + |
| 71 | + // Using default MRP since we are not doing MRP in this context |
| 72 | + Messaging::ExchangeContext * exchangeContext = mExchangeManager->NewContext(session.Value(), nullptr); |
| 73 | + |
| 74 | + VerifyOrReturnError(exchangeContext != nullptr, CHIP_ERROR_NO_MEMORY); |
| 75 | + |
| 76 | + return exchangeContext->SendMessage(MsgType::ICD_CheckIn, std::move(buffer), Messaging::SendMessageFlags::kNoAutoRequestAck); |
| 77 | +} |
| 78 | + |
| 79 | +CHIP_ERROR ICDCheckInSender::RequestResolve(ICDMonitoringEntry & entry, FabricTable * fabricTable) |
| 80 | +{ |
| 81 | + VerifyOrReturnError(entry.IsValid(), CHIP_ERROR_INTERNAL); |
| 82 | + VerifyOrReturnError(fabricTable != nullptr, CHIP_ERROR_INTERNAL); |
| 83 | + const FabricInfo * fabricInfo = fabricTable->FindFabricWithIndex(entry.fabricIndex); |
| 84 | + PeerId peerId(fabricInfo->GetCompressedFabricId(), entry.checkInNodeID); |
| 85 | + |
| 86 | + AddressResolve::NodeLookupRequest request(peerId); |
| 87 | + |
| 88 | + memcpy(mKey.AsMutable<Crypto::Aes128KeyByteArray>(), entry.key.As<Crypto::Aes128KeyByteArray>(), |
| 89 | + sizeof(Crypto::Aes128KeyByteArray)); |
| 90 | + |
| 91 | + // TODO #30492 |
| 92 | + // Device must stay active during MDNS resolution |
| 93 | + CHIP_ERROR err = AddressResolve::Resolver::Instance().LookupNode(request, mAddressLookupHandle); |
| 94 | + |
| 95 | + if (err == CHIP_NO_ERROR) |
| 96 | + { |
| 97 | + mResolveInProgress = true; |
| 98 | + } |
| 99 | + |
| 100 | + return err; |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace app |
| 104 | +} // namespace chip |
0 commit comments