-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PairingCommand.cpp
403 lines (338 loc) · 13.1 KB
/
PairingCommand.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (c) 2020 Project CHIP Authors
* All rights reserved.
*
* 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.
*
*/
#include "PairingCommand.h"
#include "platform/PlatformManager.h"
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPSafeCasts.h>
#include <support/logging/CHIPLogging.h>
#include <setup_payload/ManualSetupPayloadParser.h>
#include <setup_payload/QRCodeSetupPayloadParser.h>
using namespace ::chip;
constexpr uint64_t kBreadcrumb = 0;
constexpr uint32_t kTimeoutMs = 6000;
CHIP_ERROR PairingCommand::Run()
{
CHIP_ERROR err = CHIP_NO_ERROR;
GetExecContext()->commissioner->RegisterDeviceAddressUpdateDelegate(this);
GetExecContext()->commissioner->RegisterPairingDelegate(this);
#if CONFIG_PAIR_WITH_RANDOM_ID
// Generate a random remote id so we don't end up reusing the same node id
// for different nodes.
//
// TODO: Ideally we'd just ask for an operational cert for the commissionnee
// and get the node from that, but the APIs are not set up that way yet.
NodeId randomId;
if (Controller::ExampleOperationalCredentialsIssuer::GetRandomOperationalNodeId(&randomId) == CHIP_NO_ERROR)
{
ChipLogProgress(Controller, "Generated random node id: 0x" ChipLogFormatX64, ChipLogValueX64(randomId));
if (GetExecContext()->storage->SetRemoteNodeId(randomId) == CHIP_NO_ERROR)
{
GetExecContext()->remoteId = randomId;
}
}
#endif // CONFIG_PAIR_WITH_RANDOM_ID
err = RunInternal(GetExecContext()->remoteId);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Init Failure! PairDevice: %s", ErrorStr(err)));
exit:
return err;
}
CHIP_ERROR PairingCommand::RunInternal(NodeId remoteId)
{
CHIP_ERROR err = CHIP_NO_ERROR;
mRemoteId = remoteId;
InitCallbacks();
switch (mPairingMode)
{
case PairingMode::None:
err = Unpair(remoteId);
break;
case PairingMode::Bypass:
err = PairWithoutSecurity(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort));
break;
case PairingMode::QRCode:
err = PairWithQRCode(remoteId);
break;
case PairingMode::ManualCode:
err = PairWithManualCode(remoteId);
break;
case PairingMode::Ble:
err = Pair(remoteId, PeerAddress::BLE());
break;
case PairingMode::OnNetwork:
case PairingMode::SoftAP:
err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort));
break;
case PairingMode::Ethernet:
err = Pair(remoteId, PeerAddress::UDP(mRemoteAddr.address, mRemotePort));
break;
}
return err;
}
CHIP_ERROR PairingCommand::PairWithQRCode(NodeId remoteId)
{
SetupPayload payload;
ReturnErrorOnFailure(QRCodeSetupPayloadParser(mOnboardingPayload).populatePayload(payload));
return PairWithCode(remoteId, payload);
}
CHIP_ERROR PairingCommand::PairWithManualCode(NodeId remoteId)
{
SetupPayload payload;
ReturnErrorOnFailure(ManualSetupPayloadParser(mOnboardingPayload).populatePayload(payload));
return PairWithCode(remoteId, payload);
}
CHIP_ERROR PairingCommand::PairWithCode(NodeId remoteId, SetupPayload payload)
{
chip::RendezvousInformationFlags rendezvousInformation = payload.rendezvousInformation;
ReturnErrorCodeIf(rendezvousInformation != RendezvousInformationFlag::kBLE, CHIP_ERROR_INVALID_ARGUMENT);
RendezvousParameters params = RendezvousParameters()
.SetSetupPINCode(payload.setUpPINCode)
.SetDiscriminator(payload.discriminator)
.SetPeerAddress(PeerAddress::BLE());
return GetExecContext()->commissioner->PairDevice(remoteId, params);
}
CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address)
{
RendezvousParameters params =
RendezvousParameters().SetSetupPINCode(mSetupPINCode).SetDiscriminator(mDiscriminator).SetPeerAddress(address);
return GetExecContext()->commissioner->PairDevice(remoteId, params);
}
CHIP_ERROR PairingCommand::PairWithoutSecurity(NodeId remoteId, PeerAddress address)
{
ChipSerializedDevice serializedTestDevice;
return GetExecContext()->commissioner->PairTestDeviceWithoutSecurity(remoteId, address, serializedTestDevice);
}
CHIP_ERROR PairingCommand::Unpair(NodeId remoteId)
{
CHIP_ERROR err = GetExecContext()->commissioner->UnpairDevice(remoteId);
SetCommandExitStatus(err);
return err;
}
void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
{
switch (status)
{
case DevicePairingDelegate::Status::SecurePairingSuccess:
ChipLogProgress(chipTool, "Secure Pairing Success");
break;
case DevicePairingDelegate::Status::SecurePairingFailed:
ChipLogError(chipTool, "Secure Pairing Failed");
break;
}
}
void PairingCommand::OnPairingComplete(CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Pairing Success");
err = SetupNetwork();
}
else
{
ChipLogProgress(chipTool, "Pairing Failure: %s", ErrorStr(err));
}
if (err != CHIP_NO_ERROR)
{
SetCommandExitStatus(err);
}
}
void PairingCommand::OnPairingDeleted(CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Pairing Deleted Success");
}
else
{
ChipLogProgress(chipTool, "Pairing Deleted Failure: %s", ErrorStr(err));
}
SetCommandExitStatus(err);
}
void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Device commissioning completed with success");
}
else
{
ChipLogProgress(chipTool, "Device commissioning Failure: %s", ErrorStr(err));
}
SetCommandExitStatus(err);
}
CHIP_ERROR PairingCommand::SetupNetwork()
{
CHIP_ERROR err = CHIP_NO_ERROR;
switch (mNetworkType)
{
case PairingNetworkType::None:
case PairingNetworkType::Ethernet:
// Nothing to do other than to resolve the device's operational address.
err = UpdateNetworkAddress();
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(chipTool, "Setup failure! Error calling UpdateNetworkAddress: %s", ErrorStr(err)));
break;
case PairingNetworkType::WiFi:
case PairingNetworkType::Thread:
err = GetExecContext()->commissioner->GetDevice(mRemoteId, &mDevice);
VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(chipTool, "Setup failure! No pairing for device: %" PRIu64, mRemoteId));
mCluster.Associate(mDevice, mEndpointId);
err = AddNetwork(mNetworkType);
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(chipTool, "Setup failure! Error calling AddWiFiNetwork: %s", ErrorStr(err)));
break;
}
exit:
return err;
}
void PairingCommand::InitCallbacks()
{
mOnAddThreadNetworkCallback =
new Callback::Callback<NetworkCommissioningClusterAddThreadNetworkResponseCallback>(OnAddNetworkResponse, this);
mOnAddWiFiNetworkCallback =
new Callback::Callback<NetworkCommissioningClusterAddWiFiNetworkResponseCallback>(OnAddNetworkResponse, this);
mOnEnableNetworkCallback =
new Callback::Callback<NetworkCommissioningClusterEnableNetworkResponseCallback>(OnEnableNetworkResponse, this);
mOnFailureCallback = new Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
}
void PairingCommand::Shutdown()
{
delete mOnAddThreadNetworkCallback;
delete mOnAddWiFiNetworkCallback;
delete mOnEnableNetworkCallback;
delete mOnFailureCallback;
}
CHIP_ERROR PairingCommand::AddNetwork(PairingNetworkType networkType)
{
return (networkType == PairingNetworkType::WiFi) ? AddWiFiNetwork() : AddThreadNetwork();
}
CHIP_ERROR PairingCommand::AddThreadNetwork()
{
Callback::Cancelable * successCallback = mOnAddThreadNetworkCallback->Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback->Cancel();
return mCluster.AddThreadNetwork(successCallback, failureCallback, mOperationalDataset, kBreadcrumb, kTimeoutMs);
}
CHIP_ERROR PairingCommand::AddWiFiNetwork()
{
Callback::Cancelable * successCallback = mOnAddWiFiNetworkCallback->Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback->Cancel();
return mCluster.AddWiFiNetwork(successCallback, failureCallback, mSSID, mPassword, kBreadcrumb, kTimeoutMs);
}
chip::ByteSpan PairingCommand::GetThreadNetworkId()
{
// For Thread devices the networkId is the extendedPanId and it is
// part of the dataset defined by OpenThread
Thread::OperationalDataset dataset;
if (dataset.Init(mOperationalDataset) != CHIP_NO_ERROR)
{
return ByteSpan();
}
if (dataset.GetExtendedPanId(mExtendedPanId) != CHIP_NO_ERROR)
{
return ByteSpan();
}
return ByteSpan(mExtendedPanId);
}
CHIP_ERROR PairingCommand::EnableNetwork()
{
Callback::Cancelable * successCallback = mOnEnableNetworkCallback->Cancel();
Callback::Cancelable * failureCallback = mOnFailureCallback->Cancel();
ByteSpan networkId;
if (mNetworkType == PairingNetworkType::WiFi)
{
networkId = mSSID;
}
else
{
networkId = GetThreadNetworkId();
}
if (networkId.empty())
{
return CHIP_ERROR_INVALID_ARGUMENT;
}
return mCluster.EnableNetwork(successCallback, failureCallback, networkId, kBreadcrumb, kTimeoutMs);
}
void PairingCommand::OnDefaultFailureResponse(void * context, uint8_t status)
{
ChipLogProgress(chipTool, "Default Failure Response: 0x%02x", status);
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
}
void PairingCommand::OnAddNetworkResponse(void * context, uint8_t errorCode, uint8_t * debugText)
{
ChipLogProgress(chipTool, "AddNetworkResponse");
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
// Normally, the errorCode should be checked, but the current codebase send a default response
// instead of the command specific response. So errorCode is not set correctly.
// if (EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_SUCCESS != errorCode)
// {
// ChipLogError(chipTool, "Setup failure. Error calling EnableNetwork: %d", errorCode);
// command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
// return;
// }
CHIP_ERROR err = command->EnableNetwork();
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Setup failure. Internal error calling EnableNetwork: %s", ErrorStr(err));
command->SetCommandExitStatus(err);
return;
}
// When the accessory is configured as a SoftAP and WiFi is configured to an other network
// there won't be any response since the WiFi network is changing.
// So returns early, assuming everything goes well
// and the device address will be updated once a command is issued.
if (command->mPairingMode == PairingMode::SoftAP && command->mNetworkType == PairingNetworkType::WiFi)
{
command->SetCommandExitStatus(CHIP_NO_ERROR);
}
}
void PairingCommand::OnEnableNetworkResponse(void * context, uint8_t errorCode, uint8_t * debugText)
{
ChipLogProgress(chipTool, "EnableNetworkResponse");
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
// Normally, the errorCode should be checked, but the current codebase send a default response
// instead of the command specific response. So errorCode is not set correctly.
// if (EMBER_ZCL_NETWORK_COMMISSIONING_ERROR_SUCCESS != errorCode)
// {
// ChipLogError(chipTool, "Setup failure. Error calling EnableNetwork: %d", errorCode);
// command->SetCommandExitStatus(CHIP_ERROR_INTERNAL);
// return;
// }
CHIP_ERROR err = command->UpdateNetworkAddress();
if (CHIP_NO_ERROR != err)
{
ChipLogError(chipTool, "Setup failure. Internal error calling UpdateNetworkAddress: %s", ErrorStr(err));
command->SetCommandExitStatus(err);
return;
}
}
CHIP_ERROR PairingCommand::UpdateNetworkAddress()
{
ChipLogProgress(chipTool, "Mdns: Updating NodeId: %" PRIx64 " FabricId: %" PRIx64 " ...", mRemoteId, mFabricId);
return GetExecContext()->commissioner->UpdateDevice(mRemoteId, mFabricId);
}
void PairingCommand::OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR err)
{
ChipLogProgress(chipTool, "OnAddressUpdateComplete: %s", ErrorStr(err));
if (err != CHIP_NO_ERROR)
{
// Set exit status only if the address update failed.
// Otherwise wait for OnCommissioningComplete() callback.
SetCommandExitStatus(err);
}
}