Skip to content

Commit

Permalink
Use ScheduleLambda() instead of PlatformMgr().ScheduleWork to excute …
Browse files Browse the repository at this point in the history
…command (project-chip#35926)

* Use ScheduleLambda() instead of PlatformMgr().ScheduleWork to excute command

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
yufengwangca and restyled-commits authored Oct 7, 2024
1 parent ef0132b commit 45a68f8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 203 deletions.
11 changes: 1 addition & 10 deletions examples/fabric-admin/commands/fabric-sync/FabricSyncCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@

using namespace ::chip;

namespace {

void CheckFabricBridgeSynchronizationSupport(intptr_t ignored)
{
DeviceMgr().ReadSupportedDeviceCategories();
}

} // namespace

void FabricSyncAddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR err)
{
if (mBridgeNodeId != deviceId)
Expand Down Expand Up @@ -73,7 +64,7 @@ void FabricSyncAddBridgeCommand::OnCommissioningComplete(NodeId deviceId, CHIP_E
//
// Note: The Fabric-Admin MUST NOT send the RequestCommissioningApproval command
// if the remote Fabric-Bridge lacks Fabric Synchronization support.
DeviceLayer::PlatformMgr().ScheduleWork(CheckFabricBridgeSynchronizationSupport, 0);
DeviceLayer::SystemLayer().ScheduleLambda([]() { DeviceMgr().ReadSupportedDeviceCategories(); });
}
}
else
Expand Down
269 changes: 118 additions & 151 deletions examples/fabric-admin/device_manager/PairingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,98 +131,97 @@ CHIP_ERROR PairingManager::OpenCommissioningWindow(NodeId nodeId, EndpointId end
return CHIP_ERROR_INCORRECT_STATE;
}

auto params = Platform::MakeUnique<CommissioningWindowParams>();
params->nodeId = nodeId;
params->endpointId = endpointId;
params->commissioningWindowTimeout = commissioningTimeoutSec;
params->iteration = iterations;
params->discriminator = discriminator;
// Ensure salt and verifier sizes are valid
if (!salt.empty() && salt.size() > chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length)
{
ChipLogError(NotSpecified, "Salt size exceeds buffer capacity");
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

if (!salt.empty())
if (!verifier.empty() && verifier.size() > chip::Crypto::kSpake2p_VerifierSerialized_Length)
{
if (salt.size() > sizeof(params->saltBuffer))
{
ChipLogError(NotSpecified, "Salt size exceeds buffer capacity");
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
ChipLogError(NotSpecified, "Verifier size exceeds buffer capacity");
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

memcpy(params->saltBuffer, salt.data(), salt.size());
params->salt = ByteSpan(params->saltBuffer, salt.size());
if (!salt.empty())
{
memcpy(mSaltBuffer, salt.data(), salt.size());
mSalt = ByteSpan(mSaltBuffer, salt.size());
}
else
{
mSalt = ByteSpan();
}

if (!verifier.empty())
{
if (verifier.size() > sizeof(params->verifierBuffer))
{
ChipLogError(NotSpecified, "Verifier size exceeds buffer capacity");
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

memcpy(params->verifierBuffer, verifier.data(), verifier.size());
params->verifier = ByteSpan(params->verifierBuffer, verifier.size());
memcpy(mVerifierBuffer, verifier.data(), verifier.size());
mVerifier = ByteSpan(mVerifierBuffer, verifier.size());
}

// Schedule work on the Matter thread
return DeviceLayer::PlatformMgr().ScheduleWork(OnOpenCommissioningWindow, reinterpret_cast<intptr_t>(params.release()));
}

void PairingManager::OnOpenCommissioningWindow(intptr_t context)
{
Platform::UniquePtr<CommissioningWindowParams> params(reinterpret_cast<CommissioningWindowParams *>(context));
PairingManager & self = PairingManager::Instance();

if (self.mCommissioner == nullptr)
else
{
ChipLogError(NotSpecified, "Commissioner is null, cannot open commissioning window");
return;
mVerifier = ByteSpan();
}

self.mWindowOpener = Platform::MakeUnique<Controller::CommissioningWindowOpener>(self.mCommissioner);
return DeviceLayer::SystemLayer().ScheduleLambda([nodeId, endpointId, commissioningTimeoutSec, iterations, discriminator]() {
PairingManager & self = PairingManager::Instance();

if (!params->verifier.empty())
{
if (params->salt.empty())
if (self.mCommissioner == nullptr)
{
ChipLogError(NotSpecified, "Salt is required when verifier is set");
self.mWindowOpener.reset();
ChipLogError(NotSpecified, "Commissioner is null, cannot open commissioning window");
return;
}

CHIP_ERROR err =
self.mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams()
.SetNodeId(params->nodeId)
.SetEndpointId(params->endpointId)
.SetTimeout(params->commissioningWindowTimeout)
.SetIteration(params->iteration)
.SetDiscriminator(params->discriminator)
.SetVerifier(params->verifier)
.SetSalt(params->salt)
.SetCallback(&self.mOnOpenCommissioningWindowVerifierCallback));
if (err != CHIP_NO_ERROR)
self.mWindowOpener = Platform::MakeUnique<Controller::CommissioningWindowOpener>(self.mCommissioner);

if (!self.mVerifier.empty())
{
ChipLogError(NotSpecified, "Failed to open commissioning window with verifier: %s", ErrorStr(err));
self.mWindowOpener.reset();
if (self.mSalt.empty())
{
ChipLogError(NotSpecified, "Salt is required when verifier is set");
self.mWindowOpener.reset();
return;
}

// Open the commissioning window with verifier parameters
CHIP_ERROR err =
self.mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowVerifierParams()
.SetNodeId(nodeId)
.SetEndpointId(endpointId)
.SetTimeout(commissioningTimeoutSec)
.SetIteration(iterations)
.SetDiscriminator(discriminator)
.SetVerifier(self.mVerifier)
.SetSalt(self.mSalt)
.SetCallback(&self.mOnOpenCommissioningWindowVerifierCallback));
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to open commissioning window with verifier: %s", ErrorStr(err));
self.mWindowOpener.reset();
}
}
}
else
{
SetupPayload ignored;
CHIP_ERROR err = self.mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams()
.SetNodeId(params->nodeId)
.SetEndpointId(params->endpointId)
.SetTimeout(params->commissioningWindowTimeout)
.SetIteration(params->iteration)
.SetDiscriminator(params->discriminator)
.SetSetupPIN(NullOptional)
.SetSalt(NullOptional)
.SetCallback(&self.mOnOpenCommissioningWindowCallback),
ignored);
if (err != CHIP_NO_ERROR)
else
{
ChipLogError(NotSpecified, "Failed to open commissioning window with passcode: %s", ErrorStr(err));
self.mWindowOpener.reset();
SetupPayload ignored;
// Open the commissioning window with passcode parameters
CHIP_ERROR err = self.mWindowOpener->OpenCommissioningWindow(Controller::CommissioningWindowPasscodeParams()
.SetNodeId(nodeId)
.SetEndpointId(endpointId)
.SetTimeout(commissioningTimeoutSec)
.SetIteration(iterations)
.SetDiscriminator(discriminator)
.SetSetupPIN(NullOptional)
.SetSalt(NullOptional)
.SetCallback(&self.mOnOpenCommissioningWindowCallback),
ignored);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to open commissioning window with passcode: %s", ErrorStr(err));
self.mWindowOpener.reset();
}
}
}
});
}

void PairingManager::OnOpenCommissioningWindowResponse(void * context, NodeId remoteId, CHIP_ERROR err, SetupPayload payload)
Expand Down Expand Up @@ -290,7 +289,7 @@ void PairingManager::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
if (err == CHIP_NO_ERROR)
{
// print to console
fprintf(stderr, "New device with Node ID: " ChipLogFormatX64 "has been successfully added.\n", ChipLogValueX64(nodeId));
fprintf(stderr, "New device with Node ID: " ChipLogFormatX64 " has been successfully added.\n", ChipLogValueX64(nodeId));

// mCommissioner has a lifetime that is the entire life of the application itself
// so it is safe to provide to StartDeviceSynchronization.
Expand Down Expand Up @@ -580,34 +579,24 @@ CHIP_ERROR PairingManager::PairDeviceWithCode(NodeId nodeId, const char * payloa
return CHIP_ERROR_INVALID_STRING_LENGTH;
}

auto params = Platform::MakeUnique<PairDeviceWithCodeParams>();
VerifyOrReturnError(params != nullptr, CHIP_ERROR_NO_MEMORY);

params->nodeId = nodeId;
Platform::CopyString(params->payloadBuffer, sizeof(params->payloadBuffer), payload);
Platform::CopyString(mOnboardingPayload, sizeof(mOnboardingPayload), payload);

// Schedule work on the Matter thread
return DeviceLayer::PlatformMgr().ScheduleWork(OnPairDeviceWithCode, reinterpret_cast<intptr_t>(params.release()));
}

void PairingManager::OnPairDeviceWithCode(intptr_t context)
{
Platform::UniquePtr<PairDeviceWithCodeParams> params(reinterpret_cast<PairDeviceWithCodeParams *>(context));
PairingManager & self = PairingManager::Instance();
return DeviceLayer::SystemLayer().ScheduleLambda([nodeId]() {
PairingManager & self = PairingManager::Instance();

self.InitPairingCommand();
self.InitPairingCommand();

CommissioningParameters commissioningParams = self.GetCommissioningParameters();
auto discoveryType = DiscoveryType::kDiscoveryNetworkOnly;
CommissioningParameters commissioningParams = self.GetCommissioningParameters();
auto discoveryType = DiscoveryType::kDiscoveryNetworkOnly;

self.mNodeId = params->nodeId;
self.mOnboardingPayload = params->payloadBuffer;
self.mNodeId = nodeId;

CHIP_ERROR err = self.mCommissioner->PairDevice(params->nodeId, params->payloadBuffer, commissioningParams, discoveryType);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair device with code, error: %s", ErrorStr(err));
}
CHIP_ERROR err = self.mCommissioner->PairDevice(nodeId, self.mOnboardingPayload, commissioningParams, discoveryType);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair device with code, error: %s", ErrorStr(err));
}
});
}

CHIP_ERROR PairingManager::PairDevice(chip::NodeId nodeId, uint32_t setupPINCode, const char * deviceRemoteIp,
Expand All @@ -619,72 +608,50 @@ CHIP_ERROR PairingManager::PairDevice(chip::NodeId nodeId, uint32_t setupPINCode
return CHIP_ERROR_INVALID_STRING_LENGTH;
}

auto params = Platform::MakeUnique<PairDeviceParams>();
VerifyOrReturnError(params != nullptr, CHIP_ERROR_NO_MEMORY);

params->nodeId = nodeId;
params->setupPINCode = setupPINCode;
params->deviceRemotePort = deviceRemotePort;

Platform::CopyString(params->ipAddrBuffer, sizeof(params->ipAddrBuffer), deviceRemoteIp);

// Schedule work on the Matter thread
return DeviceLayer::PlatformMgr().ScheduleWork(OnPairDevice, reinterpret_cast<intptr_t>(params.release()));
}
Platform::CopyString(mRemoteIpAddr, sizeof(mRemoteIpAddr), deviceRemoteIp);

void PairingManager::OnPairDevice(intptr_t context)
{
Platform::UniquePtr<PairDeviceParams> params(reinterpret_cast<PairDeviceParams *>(context));
PairingManager & self = PairingManager::Instance();
return DeviceLayer::SystemLayer().ScheduleLambda([nodeId, setupPINCode, deviceRemotePort]() {
PairingManager & self = PairingManager::Instance();

self.InitPairingCommand();
self.mSetupPINCode = params->setupPINCode;
self.InitPairingCommand();
self.mSetupPINCode = setupPINCode;

Inet::IPAddress address;
Inet::InterfaceId interfaceId;
Inet::IPAddress address;
Inet::InterfaceId interfaceId;

if (!ParseAddressWithInterface(params->ipAddrBuffer, address, interfaceId))
{
ChipLogError(NotSpecified, "Invalid IP address: %s", params->ipAddrBuffer);
return;
}
if (!ParseAddressWithInterface(self.mRemoteIpAddr, address, interfaceId))
{
ChipLogError(NotSpecified, "Invalid IP address: %s", self.mRemoteIpAddr);
return;
}

CHIP_ERROR err = self.Pair(params->nodeId, Transport::PeerAddress::UDP(address, params->deviceRemotePort, interfaceId));
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair device, error: %s", ErrorStr(err));
}
CHIP_ERROR err = self.Pair(nodeId, Transport::PeerAddress::UDP(address, deviceRemotePort, interfaceId));
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to pair device, error: %s", ErrorStr(err));
}
});
}

CHIP_ERROR PairingManager::UnpairDevice(NodeId nodeId)
{
auto params = Platform::MakeUnique<UnpairDeviceParams>();
VerifyOrReturnError(params != nullptr, CHIP_ERROR_NO_MEMORY);

params->nodeId = nodeId;

// Schedule work on the Matter thread
return DeviceLayer::PlatformMgr().ScheduleWork(OnUnpairDevice, reinterpret_cast<intptr_t>(params.release()));
}

void PairingManager::OnUnpairDevice(intptr_t context)
{
Platform::UniquePtr<PairDeviceParams> params(reinterpret_cast<PairDeviceParams *>(context));
PairingManager & self = PairingManager::Instance();
return DeviceLayer::SystemLayer().ScheduleLambda([nodeId]() {
PairingManager & self = PairingManager::Instance();

self.InitPairingCommand();
self.InitPairingCommand();

self.mCurrentFabricRemover = Platform::MakeUnique<Controller::CurrentFabricRemover>(self.mCommissioner);
self.mCurrentFabricRemover = Platform::MakeUnique<Controller::CurrentFabricRemover>(self.mCommissioner);

if (!self.mCurrentFabricRemover)
{
ChipLogError(NotSpecified, "Failed to unpair device, mCurrentFabricRemover is null");
return;
}
if (!self.mCurrentFabricRemover)
{
ChipLogError(NotSpecified, "Failed to unpair device, mCurrentFabricRemover is null");
return;
}

CHIP_ERROR err = self.mCurrentFabricRemover->RemoveCurrentFabric(params->nodeId, &self.mCurrentFabricRemoveCallback);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to unpair device, error: %s", ErrorStr(err));
}
CHIP_ERROR err = self.mCurrentFabricRemover->RemoveCurrentFabric(nodeId, &self.mCurrentFabricRemoveCallback);
if (err != CHIP_NO_ERROR)
{
ChipLogError(NotSpecified, "Failed to unpair device, error: %s", ErrorStr(err));
}
});
}
Loading

0 comments on commit 45a68f8

Please sign in to comment.