Skip to content

Commit 74e5a13

Browse files
bzbarsky-appleaustina-csa
authored andcommitted
Fix discriminator handling in chip-tool pairing commands. (project-chip#34115)
Not all commands initialize the discriminator value, and the ones that don't should not use it. Fixes project-chip#34096
1 parent a2effe3 commit 74e5a13

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

examples/chip-tool/commands/pairing/PairingCommand.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ CHIP_ERROR PairingCommand::PairWithCode(NodeId remoteId)
216216

217217
CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address)
218218
{
219-
auto params = RendezvousParameters().SetSetupPINCode(mSetupPINCode).SetDiscriminator(mDiscriminator).SetPeerAddress(address);
219+
VerifyOrDieWithMsg(mSetupPINCode.has_value(), chipTool, "Using mSetupPINCode in a mode when we have not gotten one");
220+
auto params = RendezvousParameters().SetSetupPINCode(mSetupPINCode.value()).SetPeerAddress(address);
221+
if (mDiscriminator.has_value())
222+
{
223+
params.SetDiscriminator(mDiscriminator.value());
224+
}
220225

221226
CHIP_ERROR err = CHIP_NO_ERROR;
222227
if (mPaseOnly.ValueOr(false))
@@ -236,9 +241,11 @@ CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndex(NodeId remoteId, uint16_t in
236241
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
237242
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
238243

244+
VerifyOrDieWithMsg(mSetupPINCode.has_value(), chipTool, "Using mSetupPINCode in a mode when we have not gotten one");
245+
239246
RendezvousParameters params;
240247
ReturnErrorOnFailure(GetDeviceScanner().Get(index, params));
241-
params.SetSetupPINCode(mSetupPINCode);
248+
params.SetSetupPINCode(mSetupPINCode.value());
242249

243250
CHIP_ERROR err = CHIP_NO_ERROR;
244251
if (mPaseOnly.ValueOr(false))
@@ -258,6 +265,10 @@ CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndex(NodeId remoteId, uint16_t in
258265

259266
CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndexWithCode(NodeId remoteId, uint16_t index)
260267
{
268+
// We might or might not have a setup code. We don't know yet, but if we
269+
// do, we'll emplace it at that point.
270+
mSetupPINCode.reset();
271+
261272
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
262273
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
263274

@@ -281,7 +292,7 @@ CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndexWithCode(NodeId remoteId, uin
281292
VerifyOrReturnError(payload.isValidManualCode(), CHIP_ERROR_INVALID_ARGUMENT);
282293
}
283294

284-
mSetupPINCode = payload.setUpPINCode;
295+
mSetupPINCode.emplace(payload.setUpPINCode);
285296
return PairWithMdnsOrBleByIndex(remoteId, index);
286297
}
287298

examples/chip-tool/commands/pairing/PairingCommand.h

+17-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <lib/support/Span.h>
2727
#include <lib/support/ThreadOperationalDataset.h>
2828

29+
#include <optional>
30+
2931
enum class PairingMode
3032
{
3133
None,
@@ -107,32 +109,32 @@ class PairingCommand : public CHIPCommand,
107109
break;
108110
case PairingMode::Ble:
109111
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
110-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
111-
AddArgument("discriminator", 0, 4096, &mDiscriminator);
112+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
113+
AddArgument("discriminator", 0, 4096, &mDiscriminator.emplace());
112114
break;
113115
case PairingMode::OnNetwork:
114116
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
115-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
117+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
116118
AddArgument("pase-only", 0, 1, &mPaseOnly);
117119
break;
118120
case PairingMode::SoftAP:
119121
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
120-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
121-
AddArgument("discriminator", 0, 4096, &mDiscriminator);
122+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
123+
AddArgument("discriminator", 0, 4096, &mDiscriminator.emplace());
122124
AddArgument("device-remote-ip", &mRemoteAddr);
123125
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
124126
AddArgument("pase-only", 0, 1, &mPaseOnly);
125127
break;
126128
case PairingMode::AlreadyDiscovered:
127129
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
128-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
130+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
129131
AddArgument("device-remote-ip", &mRemoteAddr);
130132
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
131133
AddArgument("pase-only", 0, 1, &mPaseOnly);
132134
break;
133135
case PairingMode::AlreadyDiscoveredByIndex:
134136
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
135-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
137+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
136138
AddArgument("index", 0, UINT16_MAX, &mIndex);
137139
AddArgument("pase-only", 0, 1, &mPaseOnly);
138140
break;
@@ -252,8 +254,14 @@ class PairingCommand : public CHIPCommand,
252254
mComplex_DSTOffsets;
253255

254256
uint16_t mRemotePort;
255-
uint16_t mDiscriminator;
256-
uint32_t mSetupPINCode;
257+
// mDiscriminator is only used for some situations, but in those situations
258+
// it's mandatory. Track whether we're actually using it; the cases that do
259+
// will emplace this optional.
260+
std::optional<uint16_t> mDiscriminator;
261+
// mSetupPINCode is only used for some situations, but in those situations
262+
// it's mandatory. Track whether we're actually using it; the cases that do
263+
// will emplace this optional.
264+
std::optional<uint32_t> mSetupPINCode;
257265
uint16_t mIndex;
258266
chip::ByteSpan mOperationalDataset;
259267
chip::ByteSpan mSSID;

0 commit comments

Comments
 (0)