Skip to content

Commit 27723d5

Browse files
Fix discriminator handling in chip-tool pairing commands.
Not all commands initialize the discriminator value, and the ones that don't should not use it. Fixes #34096
1 parent dd0c49b commit 27723d5

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
@@ -212,7 +212,12 @@ CHIP_ERROR PairingCommand::PairWithCode(NodeId remoteId)
212212

213213
CHIP_ERROR PairingCommand::Pair(NodeId remoteId, PeerAddress address)
214214
{
215-
auto params = RendezvousParameters().SetSetupPINCode(mSetupPINCode).SetDiscriminator(mDiscriminator).SetPeerAddress(address);
215+
VerifyOrDieWithMsg(mSetupPINCode.has_value(), chipTool, "Using mSetupPINCode in a mode when we have not gotten one");
216+
auto params = RendezvousParameters().SetSetupPINCode(mSetupPINCode.value()).SetPeerAddress(address);
217+
if (mDiscriminator.has_value())
218+
{
219+
params.SetDiscriminator(mDiscriminator.value());
220+
}
216221

217222
CHIP_ERROR err = CHIP_NO_ERROR;
218223
if (mPaseOnly.ValueOr(false))
@@ -232,9 +237,11 @@ CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndex(NodeId remoteId, uint16_t in
232237
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
233238
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
234239

240+
VerifyOrDieWithMsg(mSetupPINCode.has_value(), chipTool, "Using mSetupPINCode in a mode when we have not gotten one");
241+
235242
RendezvousParameters params;
236243
ReturnErrorOnFailure(GetDeviceScanner().Get(index, params));
237-
params.SetSetupPINCode(mSetupPINCode);
244+
params.SetSetupPINCode(mSetupPINCode.value());
238245

239246
CHIP_ERROR err = CHIP_NO_ERROR;
240247
if (mPaseOnly.ValueOr(false))
@@ -254,6 +261,10 @@ CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndex(NodeId remoteId, uint16_t in
254261

255262
CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndexWithCode(NodeId remoteId, uint16_t index)
256263
{
264+
// We might or might not have a setup code. We don't know yet, but if we
265+
// do, we'll emplace it at that point.
266+
mSetupPINCode.reset();
267+
257268
#if CHIP_DEVICE_LAYER_TARGET_DARWIN
258269
VerifyOrReturnError(IsInteractive(), CHIP_ERROR_INCORRECT_STATE);
259270

@@ -277,7 +288,7 @@ CHIP_ERROR PairingCommand::PairWithMdnsOrBleByIndexWithCode(NodeId remoteId, uin
277288
VerifyOrReturnError(payload.isValidManualCode(), CHIP_ERROR_INVALID_ARGUMENT);
278289
}
279290

280-
mSetupPINCode = payload.setUpPINCode;
291+
mSetupPINCode.emplace(payload.setUpPINCode);
281292
return PairWithMdnsOrBleByIndex(remoteId, index);
282293
}
283294

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,
@@ -101,32 +103,32 @@ class PairingCommand : public CHIPCommand,
101103
break;
102104
case PairingMode::Ble:
103105
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
104-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
105-
AddArgument("discriminator", 0, 4096, &mDiscriminator);
106+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
107+
AddArgument("discriminator", 0, 4096, &mDiscriminator.emplace());
106108
break;
107109
case PairingMode::OnNetwork:
108110
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
109-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
111+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
110112
AddArgument("pase-only", 0, 1, &mPaseOnly);
111113
break;
112114
case PairingMode::SoftAP:
113115
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
114-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
115-
AddArgument("discriminator", 0, 4096, &mDiscriminator);
116+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
117+
AddArgument("discriminator", 0, 4096, &mDiscriminator.emplace());
116118
AddArgument("device-remote-ip", &mRemoteAddr);
117119
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
118120
AddArgument("pase-only", 0, 1, &mPaseOnly);
119121
break;
120122
case PairingMode::AlreadyDiscovered:
121123
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
122-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
124+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
123125
AddArgument("device-remote-ip", &mRemoteAddr);
124126
AddArgument("device-remote-port", 0, UINT16_MAX, &mRemotePort);
125127
AddArgument("pase-only", 0, 1, &mPaseOnly);
126128
break;
127129
case PairingMode::AlreadyDiscoveredByIndex:
128130
AddArgument("skip-commissioning-complete", 0, 1, &mSkipCommissioningComplete);
129-
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode);
131+
AddArgument("setup-pin-code", 0, 134217727, &mSetupPINCode.emplace());
130132
AddArgument("index", 0, UINT16_MAX, &mIndex);
131133
AddArgument("pase-only", 0, 1, &mPaseOnly);
132134
break;
@@ -246,8 +248,14 @@ class PairingCommand : public CHIPCommand,
246248
mComplex_DSTOffsets;
247249

248250
uint16_t mRemotePort;
249-
uint16_t mDiscriminator;
250-
uint32_t mSetupPINCode;
251+
// mDiscriminator is only used for some situations, but in those situations
252+
// it's mandatory. Track whether we're actually using it; the cases that do
253+
// will emplace this optional.
254+
std::optional<uint16_t> mDiscriminator;
255+
// mSetupPINCode is only used for some situations, but in those situations
256+
// it's mandatory. Track whether we're actually using it; the cases that do
257+
// will emplace this optional.
258+
std::optional<uint32_t> mSetupPINCode;
251259
uint16_t mIndex;
252260
chip::ByteSpan mOperationalDataset;
253261
chip::ByteSpan mSSID;

0 commit comments

Comments
 (0)