|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2020-2021 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <app/CASESessionManager.h> |
| 20 | + |
| 21 | +namespace chip { |
| 22 | + |
| 23 | +CHIP_ERROR CASESessionManager::FindOrEstablishSession(NodeId nodeId, Callback::Callback<OnDeviceConnected> * onConnection, |
| 24 | + Callback::Callback<OnDeviceConnectionFailure> * onFailure) |
| 25 | +{ |
| 26 | + Dnssd::ResolvedNodeData resolutionData; |
| 27 | + |
| 28 | + PeerId peerId = GetFabricInfo()->GetPeerIdForNode(nodeId); |
| 29 | + |
| 30 | + bool nodeIDWasResolved = (mConfig.dnsCache != nullptr && mConfig.dnsCache->Lookup(peerId, resolutionData) == CHIP_NO_ERROR); |
| 31 | + |
| 32 | + OperationalDeviceProxy * session = FindExistingSession(nodeId); |
| 33 | + if (session == nullptr) |
| 34 | + { |
| 35 | + // TODO - Implement LRU to evict least recently used session to handle mActiveSessions pool exhaustion |
| 36 | + if (nodeIDWasResolved) |
| 37 | + { |
| 38 | + session = mActiveSessions.CreateObject(mConfig.sessionInitParams, peerId, resolutionData); |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + session = mActiveSessions.CreateObject(mConfig.sessionInitParams, peerId); |
| 43 | + } |
| 44 | + |
| 45 | + if (session == nullptr) |
| 46 | + { |
| 47 | + onFailure->mCall(onFailure->mContext, nodeId, CHIP_ERROR_NO_MEMORY); |
| 48 | + return CHIP_ERROR_NO_MEMORY; |
| 49 | + } |
| 50 | + } |
| 51 | + else if (nodeIDWasResolved) |
| 52 | + { |
| 53 | + session->OnNodeIdResolved(resolutionData); |
| 54 | + } |
| 55 | + |
| 56 | + CHIP_ERROR err = session->Connect(onConnection, onFailure); |
| 57 | + if (err != CHIP_NO_ERROR) |
| 58 | + { |
| 59 | + ReleaseSession(session); |
| 60 | + } |
| 61 | + |
| 62 | + return err; |
| 63 | +} |
| 64 | + |
| 65 | +void CASESessionManager::ReleaseSession(NodeId nodeId) |
| 66 | +{ |
| 67 | + ReleaseSession(FindExistingSession(nodeId)); |
| 68 | +} |
| 69 | + |
| 70 | +CHIP_ERROR CASESessionManager::ResolveDeviceAddress(NodeId nodeId) |
| 71 | +{ |
| 72 | + return Dnssd::Resolver::Instance().ResolveNodeId(GetFabricInfo()->GetPeerIdForNode(nodeId), Inet::IPAddressType::kAny); |
| 73 | +} |
| 74 | + |
| 75 | +void CASESessionManager::OnNodeIdResolved(const Dnssd::ResolvedNodeData & nodeData) |
| 76 | +{ |
| 77 | + ChipLogProgress(Controller, "Address resolved for node: 0x" ChipLogFormatX64, ChipLogValueX64(nodeData.mPeerId.GetNodeId())); |
| 78 | + |
| 79 | + if (mConfig.dnsCache != nullptr) |
| 80 | + { |
| 81 | + LogErrorOnFailure(mConfig.dnsCache->Insert(nodeData)); |
| 82 | + } |
| 83 | + |
| 84 | + OperationalDeviceProxy * session = FindExistingSession(nodeData.mPeerId.GetNodeId()); |
| 85 | + VerifyOrReturn(session != nullptr, |
| 86 | + ChipLogDetail(Controller, "OnNodeIdResolved was called for a device with no active sessions, ignoring it.")); |
| 87 | + |
| 88 | + LogErrorOnFailure(session->UpdateDeviceData( |
| 89 | + session->ToPeerAddress(nodeData), nodeData.GetMrpRetryIntervalIdle().ValueOr(CHIP_CONFIG_MRP_DEFAULT_IDLE_RETRY_INTERVAL), |
| 90 | + nodeData.GetMrpRetryIntervalActive().ValueOr(CHIP_CONFIG_MRP_DEFAULT_ACTIVE_RETRY_INTERVAL))); |
| 91 | +} |
| 92 | + |
| 93 | +void CASESessionManager::OnNodeIdResolutionFailed(const PeerId & peer, CHIP_ERROR error) |
| 94 | +{ |
| 95 | + ChipLogError(Controller, "Error resolving node id: %s", ErrorStr(error)); |
| 96 | +} |
| 97 | + |
| 98 | +CHIP_ERROR CASESessionManager::GetPeerAddress(NodeId nodeId, Transport::PeerAddress & addr) |
| 99 | +{ |
| 100 | + if (mConfig.dnsCache != nullptr) |
| 101 | + { |
| 102 | + Dnssd::ResolvedNodeData resolutionData; |
| 103 | + ReturnErrorOnFailure(mConfig.dnsCache->Lookup(GetFabricInfo()->GetPeerIdForNode(nodeId), resolutionData)); |
| 104 | + addr = OperationalDeviceProxy::ToPeerAddress(resolutionData); |
| 105 | + return CHIP_NO_ERROR; |
| 106 | + } |
| 107 | + |
| 108 | + OperationalDeviceProxy * session = FindExistingSession(nodeId); |
| 109 | + VerifyOrReturnError(session != nullptr, CHIP_ERROR_NOT_CONNECTED); |
| 110 | + addr = session->GetPeerAddress(); |
| 111 | + return CHIP_NO_ERROR; |
| 112 | +} |
| 113 | + |
| 114 | +void CASESessionManager::OnNewConnection(SessionHandle sessionHandle, Messaging::ExchangeManager * mgr) |
| 115 | +{ |
| 116 | + // TODO Update the MRP params based on the MRP params extracted from CASE, when this is available. |
| 117 | +} |
| 118 | + |
| 119 | +void CASESessionManager::OnConnectionExpired(SessionHandle sessionHandle, Messaging::ExchangeManager * mgr) |
| 120 | +{ |
| 121 | + OperationalDeviceProxy * session = FindSession(sessionHandle); |
| 122 | + VerifyOrReturn(session != nullptr, |
| 123 | + ChipLogDetail(Controller, "OnConnectionExpired was called for unknown device, ignoring it.")); |
| 124 | + |
| 125 | + session->OnConnectionExpired(sessionHandle); |
| 126 | +} |
| 127 | + |
| 128 | +OperationalDeviceProxy * CASESessionManager::FindSession(SessionHandle session) |
| 129 | +{ |
| 130 | + OperationalDeviceProxy * foundSession = nullptr; |
| 131 | + mActiveSessions.ForEachActiveObject([&](auto * activeSession) { |
| 132 | + if (activeSession->MatchesSession(session)) |
| 133 | + { |
| 134 | + foundSession = activeSession; |
| 135 | + return false; |
| 136 | + } |
| 137 | + return true; |
| 138 | + }); |
| 139 | + |
| 140 | + return foundSession; |
| 141 | +} |
| 142 | + |
| 143 | +OperationalDeviceProxy * CASESessionManager::FindExistingSession(NodeId id) |
| 144 | +{ |
| 145 | + OperationalDeviceProxy * foundSession = nullptr; |
| 146 | + mActiveSessions.ForEachActiveObject([&](auto * activeSession) { |
| 147 | + if (activeSession->GetDeviceId() == id) |
| 148 | + { |
| 149 | + foundSession = activeSession; |
| 150 | + return false; |
| 151 | + } |
| 152 | + return true; |
| 153 | + }); |
| 154 | + |
| 155 | + return foundSession; |
| 156 | +} |
| 157 | + |
| 158 | +void CASESessionManager::ReleaseSession(OperationalDeviceProxy * session) |
| 159 | +{ |
| 160 | + if (session != nullptr) |
| 161 | + { |
| 162 | + mActiveSessions.ReleaseObject(session); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +} // namespace chip |
0 commit comments