Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020 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 : // module header, comes first 20 : #include <controller/AbstractDnssdDiscoveryController.h> 21 : 22 : #include <lib/core/CHIPEncoding.h> 23 : #include <lib/support/logging/CHIPLogging.h> 24 : 25 : namespace chip { 26 : namespace Controller { 27 : 28 4 : void AbstractDnssdDiscoveryController::OnNodeDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData) 29 : { 30 4 : auto discoveredNodes = GetDiscoveredNodes(); 31 44 : for (auto & discoveredNode : discoveredNodes) 32 : { 33 40 : if (!discoveredNode.resolutionData.IsValid()) 34 : { 35 39 : continue; 36 : } 37 1 : if (strcmp(discoveredNode.resolutionData.hostName, nodeData.resolutionData.hostName) == 0 && 38 0 : discoveredNode.resolutionData.port == nodeData.resolutionData.port) 39 : { 40 0 : discoveredNode = nodeData; 41 0 : if (mDeviceDiscoveryDelegate != nullptr) 42 : { 43 0 : mDeviceDiscoveryDelegate->OnDiscoveredDevice(nodeData); 44 : } 45 4 : return; 46 : } 47 : } 48 : // Node not yet in the list 49 5 : for (auto & discoveredNode : discoveredNodes) 50 : { 51 5 : if (!discoveredNode.resolutionData.IsValid()) 52 : { 53 4 : discoveredNode = nodeData; 54 4 : if (mDeviceDiscoveryDelegate != nullptr) 55 : { 56 0 : mDeviceDiscoveryDelegate->OnDiscoveredDevice(nodeData); 57 : } 58 4 : return; 59 : } 60 : } 61 0 : ChipLogError(Discovery, "Failed to add discovered node with hostname %s- Insufficient space", nodeData.resolutionData.hostName); 62 : } 63 : 64 4 : CHIP_ERROR AbstractDnssdDiscoveryController::SetUpNodeDiscovery() 65 : { 66 4 : auto discoveredNodes = GetDiscoveredNodes(); 67 44 : for (auto & discoveredNode : discoveredNodes) 68 : { 69 40 : discoveredNode.Reset(); 70 : } 71 4 : return CHIP_NO_ERROR; 72 : } 73 : 74 31 : const Dnssd::DiscoveredNodeData * AbstractDnssdDiscoveryController::GetDiscoveredNode(int idx) 75 : { 76 : // TODO(cecille): Add assertion about main loop. 77 31 : auto discoveredNodes = GetDiscoveredNodes(); 78 31 : if (0 <= idx && idx < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES && discoveredNodes.data()[idx].resolutionData.IsValid()) 79 : { 80 10 : return discoveredNodes.data() + idx; 81 : } 82 21 : return nullptr; 83 : } 84 : 85 : } // namespace Controller 86 : } // namespace chip