Skip to content

Commit

Permalink
[android] Use specified PIN code in ECM (#15074)
Browse files Browse the repository at this point in the history
Android CHIPTool provides UI for specifying PIN code to be
used for the opened commissioning window, but then it
ignores the input value and uses random PIN code.

Use the right commissioning window option. Also change the
code to use enum instead of a raw uint8_t for clarity.

Signed-off-by: Damian Krolik <[email protected]>
  • Loading branch information
Damian-Nordic authored Feb 12, 2022
1 parent 2e7d65b commit cb432c8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OpenCommissioningWindowCommand : public CHIPCommand
mOnOpenCommissioningWindowCallback(OnOpenCommissioningWindowResponse, this)
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
AddArgument("option", 0, UINT8_MAX, &mCommissioningWindowOption);
AddArgument("option", 0, 2, &mCommissioningWindowOption);
AddArgument("timeout", 0, UINT16_MAX, &mTimeout);
AddArgument("iteration", chip::kPBKDFMinimumIterations, chip::kPBKDFMaximumIterations, &mIteration);
AddArgument("discriminator", 0, 4096, &mDiscriminator);
Expand All @@ -41,7 +41,7 @@ class OpenCommissioningWindowCommand : public CHIPCommand

private:
NodeId mNodeId;
uint8_t mCommissioningWindowOption;
ChipDeviceController::CommissioningWindowOption mCommissioningWindowOption;
uint16_t mTimeout;
uint32_t mIteration;
uint16_t mDiscriminator;
Expand Down
32 changes: 14 additions & 18 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,38 +443,34 @@ CHIP_ERROR DeviceController::ComputePASEVerifier(uint32_t iterations, uint32_t s
}

CHIP_ERROR DeviceController::OpenCommissioningWindowWithCallback(NodeId deviceId, uint16_t timeout, uint32_t iteration,
uint16_t discriminator, uint8_t option,
uint16_t discriminator, CommissioningWindowOption option,
chip::Callback::Callback<OnOpenCommissioningWindow> * callback,
bool readVIDPIDAttributes)
{
mSetupPayload = SetupPayload();

mSetupPayload.version = 0;
mSetupPayload.discriminator = discriminator;
mSetupPayload.rendezvousInformation = RendezvousInformationFlags(RendezvousInformationFlag::kOnNetwork);

mCommissioningWindowCallback = callback;
mDeviceWithCommissioningWindowOpen = deviceId;
mCommissioningWindowTimeout = timeout;
mCommissioningWindowIteration = iteration;

switch (option)
{
case 0:
mCommissioningWindowOption = CommissioningWindowOption::kOriginalSetupCode;
break;
case 1:
mCommissioningWindowOption = CommissioningWindowOption::kTokenWithRandomPIN;
case CommissioningWindowOption::kOriginalSetupCode:
case CommissioningWindowOption::kTokenWithRandomPIN:
break;
case 2:
mCommissioningWindowOption = CommissioningWindowOption::kTokenWithProvidedPIN;
case CommissioningWindowOption::kTokenWithProvidedPIN:
mSetupPayload.setUpPINCode = mSuggestedSetUpPINCode;
break;
default:
ChipLogError(Controller, "Invalid Pairing Window option");
return CHIP_ERROR_INVALID_ARGUMENT;
}

mSetupPayload.version = 0;
mSetupPayload.discriminator = discriminator;
mSetupPayload.rendezvousInformation = RendezvousInformationFlags(RendezvousInformationFlag::kOnNetwork);

mCommissioningWindowOption = option;
mCommissioningWindowCallback = callback;
mDeviceWithCommissioningWindowOpen = deviceId;
mCommissioningWindowTimeout = timeout;
mCommissioningWindowIteration = iteration;

if (callback != nullptr && mCommissioningWindowOption != CommissioningWindowOption::kOriginalSetupCode && readVIDPIDAttributes)
{
OperationalDeviceProxy * device = mCASESessionManager->FindExistingSession(GetPeerIdWithCommissioningWindowOpen());
Expand Down
7 changes: 4 additions & 3 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class DLL_EXPORT DeviceController : public SessionRecoveryDelegate
DeviceController();
virtual ~DeviceController() {}

enum class CommissioningWindowOption
enum class CommissioningWindowOption : uint8_t
{
kOriginalSetupCode = 0,
kTokenWithRandomPIN,
Expand Down Expand Up @@ -271,7 +271,7 @@ class DLL_EXPORT DeviceController : public SessionRecoveryDelegate
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error
*/
CHIP_ERROR OpenCommissioningWindow(NodeId deviceId, uint16_t timeout, uint32_t iteration, uint16_t discriminator,
uint8_t option, SetupPayload & payload)
CommissioningWindowOption option, SetupPayload & payload)
{
mSuggestedSetUpPINCode = payload.setUpPINCode;
ReturnErrorOnFailure(OpenCommissioningWindowWithCallback(deviceId, timeout, iteration, discriminator, option, nullptr));
Expand Down Expand Up @@ -302,7 +302,8 @@ class DLL_EXPORT DeviceController : public SessionRecoveryDelegate
* @return CHIP_ERROR CHIP_NO_ERROR on success, or corresponding error
*/
CHIP_ERROR OpenCommissioningWindowWithCallback(NodeId deviceId, uint16_t timeout, uint32_t iteration, uint16_t discriminator,
uint8_t option, Callback::Callback<OnOpenCommissioningWindow> * callback,
CommissioningWindowOption option,
Callback::Callback<OnOpenCommissioningWindow> * callback,
bool readVIDPIDAttributes = false);

#if CHIP_DEVICE_CONFIG_ENABLE_DNSSD
Expand Down
12 changes: 8 additions & 4 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,10 @@ JNI_METHOD(jboolean, openPairingWindow)(JNIEnv * env, jobject self, jlong handle
return false;
}

AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
err = wrapper->Controller()->OpenCommissioningWindow(chipDevice->GetDeviceId(), duration, 0, 0, 0, setupPayload);
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
DeviceController::CommissioningWindowOption option = DeviceController::CommissioningWindowOption::kOriginalSetupCode;

err = wrapper->Controller()->OpenCommissioningWindow(chipDevice->GetDeviceId(), duration, 0, 0, option, setupPayload);

if (err != CHIP_NO_ERROR)
{
Expand All @@ -560,8 +562,10 @@ JNI_METHOD(jboolean, openPairingWindowWithPIN)
return false;
}

AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
err = wrapper->Controller()->OpenCommissioningWindow(chipDevice->GetDeviceId(), duration, iteration, discriminator, 1,
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);
DeviceController::CommissioningWindowOption option = DeviceController::CommissioningWindowOption::kTokenWithProvidedPIN;

err = wrapper->Controller()->OpenCommissioningWindow(chipDevice->GetDeviceId(), duration, iteration, discriminator, option,
setupPayload);

if (err != CHIP_NO_ERROR)
Expand Down
6 changes: 4 additions & 2 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ChipError::StorageType pychip_DeviceController_PostTaskOnChipThread(ChipThreadTa

ChipError::StorageType pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl,
chip::NodeId nodeid, uint16_t timeout, uint32_t iteration,
uint16_t discriminator, uint8_t option);
uint16_t discriminator, uint8_t optionInt);

void pychip_DeviceController_PrintDiscoveredDevices(chip::Controller::DeviceCommissioner * devCtrl);
bool pychip_DeviceController_GetIPForDiscoveredDevice(chip::Controller::DeviceCommissioner * devCtrl, int idx, char * addrStr,
Expand Down Expand Up @@ -436,9 +436,11 @@ pychip_DeviceController_DiscoverCommissionableNodesCommissioningEnabled(chip::Co

ChipError::StorageType pychip_DeviceController_OpenCommissioningWindow(chip::Controller::DeviceCommissioner * devCtrl,
chip::NodeId nodeid, uint16_t timeout, uint32_t iteration,
uint16_t discriminator, uint8_t option)
uint16_t discriminator, uint8_t optionInt)
{
SetupPayload payload;
const auto option = static_cast<Controller::DeviceController::CommissioningWindowOption>(optionInt);

return devCtrl->OpenCommissioningWindow(nodeid, timeout, iteration, discriminator, option, payload).AsInteger();
}

Expand Down
7 changes: 4 additions & 3 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ - (BOOL)openPairingWindow:(uint64_t)deviceID duration:(NSUInteger)duration error
}

chip::SetupPayload setupPayload;
err = self.cppCommissioner->OpenCommissioningWindow(deviceID, (uint16_t) duration, 0, 0, 0, setupPayload);
err = self.cppCommissioner->OpenCommissioningWindow(deviceID, (uint16_t) duration, 0, 0,
chip::Controller::DeviceController::CommissioningWindowOption::kOriginalSetupCode, setupPayload);

if (err != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error(%s): Open Pairing Window failed", chip::ErrorStr(err));
Expand Down Expand Up @@ -555,8 +556,8 @@ - (NSString *)openPairingWindowWithPIN:(uint64_t)deviceID
setupPIN &= ((1 << chip::kSetupPINCodeFieldLengthInBits) - 1);
setupPayload.setUpPINCode = (uint32_t) setupPIN;

err = self.cppCommissioner->OpenCommissioningWindow(
deviceID, (uint16_t) duration, 1000, (uint16_t) discriminator, 2, setupPayload);
err = self.cppCommissioner->OpenCommissioningWindow(deviceID, (uint16_t) duration, 1000, (uint16_t) discriminator,
chip::Controller::DeviceController::CommissioningWindowOption::kTokenWithProvidedPIN, setupPayload);

if (err != CHIP_NO_ERROR) {
CHIP_LOG_ERROR("Error(%s): Open Pairing Window failed", chip::ErrorStr(err));
Expand Down

0 comments on commit cb432c8

Please sign in to comment.