forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTROperationalBrowser.mm
154 lines (126 loc) · 4.87 KB
/
MTROperationalBrowser.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright (c) 2023 Project CHIP Authors
*
* 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.
*/
#import <Foundation/Foundation.h>
#import "MTRDeviceControllerFactory_Internal.h"
#import "MTRLogging_Internal.h"
#import "MTROperationalBrowser.h"
#include <cinttypes>
#include <lib/dnssd/ServiceNaming.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/LockTracker.h>
namespace {
constexpr const char * kBrowseDomains[] = {
"default.service.arpa.", // SRP
"local.",
};
constexpr char kOperationalType[] = "_matter._tcp";
}
MTROperationalBrowser::MTROperationalBrowser(MTRDeviceControllerFactory * aFactory, dispatch_queue_t aQueue)
: mDeviceControllerFactory(aFactory)
, mQueue(aQueue)
{
}
void MTROperationalBrowser::ControllerActivated()
{
assertChipStackLockedByCurrentThread();
if (mActiveControllerCount == 0) {
EnsureBrowse();
}
++mActiveControllerCount;
}
void MTROperationalBrowser::ControllerDeactivated()
{
assertChipStackLockedByCurrentThread();
if (mActiveControllerCount == 1) {
StopBrowse();
}
--mActiveControllerCount;
}
void MTROperationalBrowser::EnsureBrowse()
{
assertChipStackLockedByCurrentThread();
if (mInitialized) {
ChipLogProgress(Controller, "%p already has a persistent operational browse running", this);
return;
}
ChipLogProgress(Controller, "%p trying to start persistent operational browse", this);
auto err = DNSServiceCreateConnection(&mBrowseRef);
if (err != kDNSServiceErr_NoError) {
ChipLogError(Controller, "%p failed to create connection for persistent operational browse: %" PRId32, this, err);
return;
}
err = DNSServiceSetDispatchQueue(mBrowseRef, mQueue);
if (err != kDNSServiceErr_NoError) {
ChipLogError(Controller, "%p failed to set up dispatch queue properly for persistent operational browse: %" PRId32, this, err);
DNSServiceRefDeallocate(mBrowseRef);
return;
}
mInitialized = true;
for (auto domain : kBrowseDomains) {
auto browseRef = mBrowseRef; // Mandatory copy because of kDNSServiceFlagsShareConnection.
err = DNSServiceBrowse(&browseRef, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexAny, kOperationalType, domain, OnBrowse, this);
if (err != kDNSServiceErr_NoError) {
ChipLogError(Controller, "%p failed to start persistent operational browse for \"%s\" domain: %" PRId32, this, StringOrNullMarker(domain), err);
}
}
}
void MTROperationalBrowser::StopBrowse()
{
ChipLogProgress(Controller, "%p stopping persistent operational browse", this);
if (mInitialized) {
DNSServiceRefDeallocate(mBrowseRef);
mInitialized = false;
}
}
void MTROperationalBrowser::OnBrowse(DNSServiceRef aServiceRef, DNSServiceFlags aFlags, uint32_t aInterfaceId,
DNSServiceErrorType aError, const char * aName, const char * aType, const char * aDomain, void * aContext)
{
assertChipStackLockedByCurrentThread();
auto self = static_cast<MTROperationalBrowser *>(aContext);
// We only expect to get notified about our type/domain.
if (aError != kDNSServiceErr_NoError) {
ChipLogError(Controller, "Operational browse failure: %" PRId32, aError);
self->StopBrowse();
// We shouldn't really get callbacks under our destructor, but guard
// against it just in case.
if (!self->mIsDestroying) {
// Try to start a new browse, so we have one going.
self->EnsureBrowse();
}
return;
}
chip::PeerId peerId;
CHIP_ERROR err = chip::Dnssd::ExtractIdFromInstanceName(aName, &peerId);
if (err != CHIP_NO_ERROR) {
ChipLogError(Controller, "Invalid instance name: '%s'\n", aName);
return;
}
if (!(aFlags & kDNSServiceFlagsAdd)) {
// We mostly only care about new things appearing, but log it when things
// disappear.
MTR_LOG("Matter operational instance advertisement removed: '%s'\n", aName);
return;
}
ChipLogProgress(Controller, "Notifying controller factory about new operational instance: '%s'", aName);
[self->mDeviceControllerFactory operationalInstanceAdded:peerId];
}
MTROperationalBrowser::~MTROperationalBrowser()
{
assertChipStackLockedByCurrentThread();
mIsDestroying = true;
StopBrowse();
}