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