Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 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 : /**
20 : * @file
21 : * Declaration of SetUp Code Pairer, a class that parses a given
22 : * setup code and uses the extracted informations to discover and
23 : * filter commissionables nodes, before initiating the pairing process.
24 : *
25 : */
26 :
27 : #pragma once
28 :
29 : #include <controller/DevicePairingDelegate.h>
30 : #include <lib/core/CHIPError.h>
31 : #include <lib/core/NodeId.h>
32 : #include <lib/support/DLLUtil.h>
33 : #include <lib/support/ThreadOperationalDataset.h>
34 : #include <platform/CHIPDeviceConfig.h>
35 : #include <protocols/secure_channel/RendezvousParameters.h>
36 : #include <setup_payload/ManualSetupPayloadParser.h>
37 : #include <setup_payload/QRCodeSetupPayloadParser.h>
38 :
39 : #if CONFIG_NETWORK_LAYER_BLE
40 : #include <ble/Ble.h>
41 : #endif // CONFIG_NETWORK_BLE
42 :
43 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
44 : #include <nfc/NFC.h>
45 : #endif // CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
46 :
47 : #if CHIP_SUPPORT_THREAD_MESHCOP
48 : #include <controller/ThreadMeshcopCommissionProxy.h>
49 : #endif // CHIP_SUPPORT_THREAD_MESHCOP
50 :
51 : #include <controller/DeviceDiscoveryDelegate.h>
52 :
53 : #include <deque>
54 : #include <optional>
55 : #include <vector>
56 :
57 : namespace chip {
58 :
59 : namespace Testing {
60 :
61 : class SetUpCodePairerTestAccess;
62 :
63 : } // namespace Testing
64 :
65 : namespace Controller {
66 :
67 : class DeviceCommissioner;
68 :
69 : /**
70 : * A class that represents a discovered device. This includes both the inputs to discovery (via the
71 : * RendezvousParameters super-class), and the outputs from discovery (the PeerAddress in
72 : * RendezvousParameters but also some of our members like mHostName, mInterfaceId,
73 : * mLongDiscriminator).
74 : */
75 : class SetUpCodePairerParameters : public RendezvousParameters
76 : {
77 : public:
78 2 : SetUpCodePairerParameters() = default;
79 : SetUpCodePairerParameters(const Dnssd::CommonResolutionData & data, std::optional<uint16_t> longDiscriminator, size_t index);
80 : #if CONFIG_NETWORK_LAYER_BLE
81 : SetUpCodePairerParameters(BLE_CONNECTION_OBJECT connObj, std::optional<uint16_t> longDiscriminator, bool connected = true);
82 : #endif // CONFIG_NETWORK_LAYER_BLE
83 : char mHostName[Dnssd::kHostNameMaxLength + 1] = {};
84 : Inet::InterfaceId mInterfaceId;
85 :
86 : // The long discriminator of the device that was actually discovered, if this is known. This
87 : // differs from the mSetupDiscriminator member of RendezvousParameters in that the latter may be
88 : // a short discriminator from a numeric setup code (which may match multiple devices), while
89 : // this member, if set, is always a long discriminator that was actually advertised by the
90 : // device represented by our PeerAddress.
91 : std::optional<uint16_t> mLongDiscriminator = std::nullopt;
92 : };
93 :
94 : enum class SetupCodePairerBehaviour : uint8_t
95 : {
96 : kCommission,
97 : kPaseOnly,
98 : };
99 :
100 : enum class DiscoveryType : uint8_t
101 : {
102 : kDiscoveryNetworkOnly,
103 : kDiscoveryNetworkOnlyWithoutPASEAutoRetry,
104 : kAll,
105 : };
106 :
107 : class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
108 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
109 : ,
110 : public Nfc::NFCReaderTransportDelegate
111 : #endif
112 : {
113 : friend class chip::Testing::SetUpCodePairerTestAccess;
114 :
115 : public:
116 : struct ThreadMeshcopCommissionParameters
117 : {
118 : Transport::PeerAddress mBorderAgentAddress;
119 : uint8_t mPSKcBuffer[Thread::kSizePSKc];
120 : };
121 17 : SetUpCodePairer(DeviceCommissioner * commissioner) : mCommissioner(commissioner) {}
122 21 : virtual ~SetUpCodePairer() {}
123 :
124 : CHIP_ERROR PairDevice(chip::NodeId remoteId, const char * setUpCode,
125 : SetupCodePairerBehaviour connectionType = SetupCodePairerBehaviour::kCommission,
126 : DiscoveryType discoveryType = DiscoveryType::kAll,
127 : Optional<Dnssd::CommonResolutionData> resolutionData = NullOptional);
128 :
129 : // Called by the DeviceCommissioner to notify that we have discovered a new device.
130 : void NotifyCommissionableDeviceDiscovered(const chip::Dnssd::DiscoveredNodeData & nodeData);
131 :
132 0 : void SetSystemLayer(System::Layer * systemLayer) { mSystemLayer = systemLayer; };
133 :
134 : #if CONFIG_NETWORK_LAYER_BLE
135 0 : void SetBleLayer(Ble::BleLayer * bleLayer) { mBleLayer = bleLayer; };
136 : #endif // CONFIG_NETWORK_LAYER_BLE
137 :
138 : #if CHIP_SUPPORT_THREAD_MESHCOP
139 0 : void SetThreadMeshcopCommissionParamsAndProxy(ThreadMeshcopCommissionParameters & meshcopCommissionParams,
140 : ThreadMeshcopCommissionProxy * proxy)
141 : {
142 0 : mThreadMeshcopCommissionProxy = proxy;
143 0 : mThreadMeshcopCommissionParams.SetValue(meshcopCommissionParams);
144 0 : }
145 : #endif
146 :
147 : // Stop ongoing discovery / pairing of the specified node, or of
148 : // whichever node we're pairing if kUndefinedNodeId is passed.
149 : bool StopPairing(NodeId remoteId = kUndefinedNodeId);
150 :
151 : private:
152 : // DevicePairingDelegate implementation.
153 : void OnStatusUpdate(DevicePairingDelegate::Status status) override;
154 : void OnPairingComplete(CHIP_ERROR error, const std::optional<RendezvousParameters> & rendezvousParameters,
155 : const std::optional<SetupPayload> & setupPayload) override;
156 : void OnPairingDeleted(CHIP_ERROR error) override;
157 : void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR error) override;
158 :
159 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
160 : // Nfc::NFCReaderTransportDelegate implementation
161 : void OnTagDiscovered(const chip::Nfc::NFCTag::Identifier & identifer) override;
162 : void OnTagDiscoveryFailed(CHIP_ERROR error) override;
163 : #endif
164 :
165 : CHIP_ERROR Connect();
166 : CHIP_ERROR StartDiscoveryOverBLE();
167 : CHIP_ERROR StopDiscoveryOverBLE();
168 : CHIP_ERROR StartDiscoveryOverDNSSD();
169 : CHIP_ERROR StopDiscoveryOverDNSSD();
170 : CHIP_ERROR StartDiscoveryOverWiFiPAF();
171 : CHIP_ERROR StopDiscoveryOverWiFiPAF();
172 : CHIP_ERROR StartDiscoveryOverNFC();
173 : CHIP_ERROR StopDiscoveryOverNFC();
174 : CHIP_ERROR StartDiscoveryOverThreadMeshcop();
175 : CHIP_ERROR StopDiscoveryOverThreadMeshcop();
176 :
177 : // Returns whether we have kicked off a new connection attempt.
178 : bool ConnectToDiscoveredDevice();
179 :
180 : // Stop attempts to discover more things to connect to, but keep trying to
181 : // connect to the ones we have already discovered.
182 : void StopAllDiscoveryAttempts();
183 :
184 : // Reset our mWaitingForDiscovery/mDiscoveredParameters state to indicate no
185 : // pending work.
186 : void ResetDiscoveryState();
187 :
188 : // Get ready to start PASE establishment via mCommissioner. Sets up
189 : // whatever state is needed for that.
190 : void ExpectPASEEstablishment();
191 :
192 : // PASE establishment by mCommissioner has completed: we either have a PASE
193 : // session now or we failed to set one up, but we are done waiting on
194 : // mCommissioner.
195 : void PASEEstablishmentComplete();
196 :
197 : // Called when PASE establishment fails.
198 : //
199 : // May start a new PASE establishment.
200 : //
201 : // Will return whether we might in fact have more rendezvous parameters to
202 : // try (e.g. because we started a new PASE establishment or are waiting on
203 : // more device discovery).
204 : //
205 : // The commissioner can use the return value to decide whether pairing has
206 : // actually failed or not.
207 : bool TryNextRendezvousParameters();
208 :
209 : // True if we are still waiting on discovery to possibly produce new
210 : // RendezvousParameters in the future.
211 : bool DiscoveryInProgress() const;
212 :
213 : // If there is nothing left to try (no PASE in progress, no queued discovered
214 : // parameters, no discovery in progress), notify the commissioner that pairing
215 : // has failed. err is used as the failure error only if no PASE attempt has
216 : // produced an error yet.
217 : void StopPairingIfTransportsExhausted(CHIP_ERROR err);
218 :
219 : // Not an enum class because we use this for indexing into arrays.
220 : enum TransportTypes
221 : {
222 : kBLETransport = 0,
223 : kIPTransport,
224 : kWiFiPAFTransport,
225 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
226 : kNFCTransport,
227 : #endif
228 : #if CHIP_SUPPORT_THREAD_MESHCOP
229 : kThreadMeshcopTransport,
230 : #endif
231 : kTransportTypeCount,
232 : };
233 :
234 : void NotifyCommissionableDeviceDiscovered(const chip::Dnssd::CommonResolutionData & resolutionData,
235 : std::optional<uint16_t> matchedLongDiscriminator);
236 :
237 : static void OnDeviceDiscoveredTimeoutCallback(System::Layer * layer, void * context);
238 :
239 : #if CONFIG_NETWORK_LAYER_BLE
240 : Ble::BleLayer * mBleLayer = nullptr;
241 : void OnDiscoveredDeviceOverBle(BLE_CONNECTION_OBJECT connObj, std::optional<uint16_t> matchedLongDiscriminator);
242 : void OnBLEDiscoveryError(CHIP_ERROR err);
243 : /////////// BLEConnectionDelegate Callbacks /////////
244 : static void OnDiscoveredDeviceOverBleSuccess(void * appState, BLE_CONNECTION_OBJECT connObj);
245 : static void OnDiscoveredDeviceWithDiscriminatorOverBleSuccess(void * appState, uint16_t matchedLongDiscriminator,
246 : BLE_CONNECTION_OBJECT connObj);
247 : static void OnDiscoveredDeviceOverBleError(void * appState, CHIP_ERROR err);
248 : #endif // CONFIG_NETWORK_LAYER_BLE
249 : #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
250 : void OnDiscoveredDeviceOverWifiPAF();
251 : void OnWifiPAFDiscoveryError(CHIP_ERROR err);
252 : static void OnWiFiPAFSubscribeComplete(void * appState);
253 : static void OnWiFiPAFSubscribeError(void * appState, CHIP_ERROR err);
254 : #endif
255 :
256 : bool NodeMatchesCurrentFilter(const Dnssd::DiscoveredNodeData & nodeData) const;
257 : static bool IdIsPresent(uint16_t vendorOrProductID);
258 :
259 : bool ShouldDiscoverUsing(RendezvousInformationFlag commissioningChannel) const;
260 :
261 : // kNotAvailable represents unavailable vendor/product ID values in setup payloads.
262 : static constexpr uint16_t kNotAvailable = 0;
263 :
264 : DeviceCommissioner * mCommissioner = nullptr;
265 : System::Layer * mSystemLayer = nullptr;
266 : chip::NodeId mRemoteId = kUndefinedNodeId;
267 : SetupCodePairerBehaviour mConnectionType = SetupCodePairerBehaviour::kCommission;
268 : DiscoveryType mDiscoveryType = DiscoveryType::kAll;
269 : std::vector<SetupPayload> mSetupPayloads;
270 :
271 : // The payload we are using for our current PASE connection attempt. Only
272 : // set while we are attempting PASE.
273 : std::optional<SetupPayload> mCurrentPASEPayload;
274 :
275 : // While we are trying to pair, we intercept the DevicePairingDelegate
276 : // notifications from mCommissioner. We want to make sure we send them on
277 : // to the original pairing delegate, if any.
278 : DevicePairingDelegate * mPairingDelegate = nullptr;
279 :
280 : // Boolean will be set to true if we currently have an async discovery
281 : // process happening via the relevant transport.
282 : bool mWaitingForDiscovery[kTransportTypeCount] = { false };
283 :
284 : // Double ended-queue of things we have discovered but not tried connecting to yet. The
285 : // general discovery/pairing process will terminate once this queue is empty
286 : // and all the booleans in mWaitingForDiscovery are false.
287 : std::deque<SetUpCodePairerParameters> mDiscoveredParameters;
288 :
289 : // Current thing we are trying to connect to over UDP. If a PASE connection fails with
290 : // a CHIP_ERROR_TIMEOUT, the discovered parameters will be used to ask the
291 : // mdns daemon to invalidate its caches.
292 : Optional<SetUpCodePairerParameters> mCurrentPASEParameters;
293 :
294 : // mWaitingForPASE is true if we have called either
295 : // EstablishPASEConnection or PairDevice on mCommissioner and are now just
296 : // waiting to see whether that works.
297 : bool mWaitingForPASE = false;
298 :
299 : // mLastPASEError is the error from the last OnPairingComplete call we got.
300 : CHIP_ERROR mLastPASEError = CHIP_NO_ERROR;
301 :
302 : #if CHIP_SUPPORT_THREAD_MESHCOP
303 : Optional<ThreadMeshcopCommissionParameters> mThreadMeshcopCommissionParams;
304 : ThreadMeshcopCommissionProxy * mThreadMeshcopCommissionProxy = nullptr;
305 : #endif
306 : };
307 :
308 : } // namespace Controller
309 : } // namespace chip
|