Skip to content

Commit

Permalink
Modify from reviewer comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Dec 22, 2022
1 parent d15b236 commit 2f2312e
Show file tree
Hide file tree
Showing 16 changed files with 546 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.OpenCommissioningCallback
import chip.devicecontroller.UnpairDeviceCallback
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import com.google.chip.chiptool.clusterclient.AddressUpdateFragment
import kotlinx.android.synthetic.main.unpair_device_fragment.view.unpairDeviceBtn
Expand All @@ -33,23 +31,25 @@ class UnpairDeviceFragment : Fragment() {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.unpair_device_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment

unpairDeviceBtn.setOnClickListener { scope.launch { unpairDeviceClick() } }
}
}

inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onPairingDeleted(code: Int) {
Log.d(TAG, "onPairingDeleted : $code")
inner class ChipUnpairDeviceCallback : UnpairDeviceCallback {
override fun onError(status: Int, remoteDeviceId: Long) {
Log.d(TAG, "onError : $remoteDeviceId, $status")
}

override fun onSuccess(remoteDeviceId: Long) {
Log.d(TAG, "onSuccess : $remoteDeviceId")
}
}

private fun unpairDeviceClick() {
deviceController.unpairDevice(addressUpdateFragment.deviceId)
deviceController.unpairDeviceCallback(addressUpdateFragment.deviceId, ChipUnpairDeviceCallback())
}


Expand Down
21 changes: 19 additions & 2 deletions examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ CHIP_ERROR PairingCommand::PairWithMdns(NodeId remoteId)

CHIP_ERROR PairingCommand::Unpair(NodeId remoteId)
{
CHIP_ERROR err = CurrentCommissioner().UnpairDevice(remoteId);
return err;
mCurrentFabricRemover = Platform::MakeUnique<Controller::CurrentFabricRemover>(&CurrentCommissioner());
return mCurrentFabricRemover->CurrentFabricRemove(remoteId, &mCurrentFabricRemoveCallback);
}

void PairingCommand::OnStatusUpdate(DevicePairingDelegate::Status status)
Expand Down Expand Up @@ -264,3 +264,20 @@ void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData &
SetCommandExitStatus(err);
}
}

void PairingCommand::OnCurrentFabricRemove(void * context, NodeId remoteDeviceId, CHIP_ERROR err)
{
PairingCommand * command = reinterpret_cast<PairingCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnOpenCommissioningWindowCommand: context is null"));

if (err == CHIP_NO_ERROR)
{
ChipLogProgress(chipTool, "Device unpair completed with success: " ChipLogFormatX64, ChipLogValueX64(remoteDeviceId));
}
else
{
ChipLogProgress(chipTool, "Device unpair Failure: " ChipLogFormatX64 " %s", ChipLogValueX64(remoteDeviceId), ErrorStr(err));
}

command->SetCommandExitStatus(err);
}
9 changes: 8 additions & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class PairingCommand : public CHIPCommand,
chip::Dnssd::DiscoveryFilterType filterType = chip::Dnssd::DiscoveryFilterType::kNone) :
CHIPCommand(commandName, credIssuerCmds),
mPairingMode(mode), mNetworkType(networkType),
mFilterType(filterType), mRemoteAddr{ IPAddress::Any, chip::Inet::InterfaceId::Null() }
mFilterType(filterType), mRemoteAddr{ IPAddress::Any, chip::Inet::InterfaceId::Null() },
mCurrentFabricRemoveCallback(OnCurrentFabricRemove, this)
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);

Expand Down Expand Up @@ -184,4 +185,10 @@ class PairingCommand : public CHIPCommand,
char * mOnboardingPayload;
uint64_t mDiscoveryFilterCode;
char * mDiscoveryFilterInstanceName;

// For unpair
chip::Platform::UniquePtr<chip::Controller::CurrentFabricRemover> mCurrentFabricRemover;
chip::Callback::Callback<chip::Controller::OnCurrentFabricRemove> mCurrentFabricRemoveCallback;

static void OnCurrentFabricRemove(void * context, NodeId remoteDeviceId, CHIP_ERROR status);
};
4 changes: 2 additions & 2 deletions src/controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ static_library("controller") {
"CommissioningDelegate.cpp",
"CommissioningWindowOpener.cpp",
"CommissioningWindowOpener.h",
"CurrentFabricRemover.cpp",
"CurrentFabricRemover.h",
"DeviceDiscoveryDelegate.h",
"DevicePairingDelegate.h",
"DeviceUnpair.cpp",
"DeviceUnpair.h",
"EmptyDataModelHandler.cpp",
"ExampleOperationalCredentialsIssuer.cpp",
"ExampleOperationalCredentialsIssuer.h",
Expand Down
24 changes: 4 additions & 20 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,35 +896,19 @@ CHIP_ERROR DeviceCommissioner::UnpairDevice(NodeId remoteDeviceId)
{
CHIP_ERROR err = CHIP_NO_ERROR;
MATTER_TRACE_EVENT_SCOPE("UnpairDevice", "DeviceCommissioner");
VerifyOrReturnError(mDeviceUnpair == nullptr, CHIP_ERROR_INCORRECT_STATE);

Platform::UniquePtr<DeviceUnpair> deviceUnpair(new DeviceUnpair());
deviceUnpair->RegisterCallback(this);
CommissioneeDeviceProxy * commissioneeDeviceProxy = nullptr;
if (CHIP_NO_ERROR == GetDeviceBeingCommissioned(remoteDeviceId, &commissioneeDeviceProxy))
if (mState == State::Initialized)
{
deviceUnpair->UnpairDevice(commissioneeDeviceProxy, remoteDeviceId);
err = AutoCurrentFabricRemover::RemoveCurrentFabric(this, remoteDeviceId);
}
else
else // the device is currently being paired
{
err = GetConnectedDevice(remoteDeviceId, &deviceUnpair->GetConnectedCallback(),
&deviceUnpair->GetConnectionFailureCallback());
err = StopPairing(remoteDeviceId);
}

mDeviceUnpair = std::move(deviceUnpair);

return err;
}

void DeviceCommissioner::OnDeviceUnpair(NodeId remoteDeviceId, CHIP_ERROR err)
{
mDeviceUnpair = nullptr;
if (mPairingDelegate != nullptr)
{
mPairingDelegate->OnPairingDeleted(err);
}
}

void DeviceCommissioner::RendezvousCleanup(CHIP_ERROR status)
{
if (mDeviceInPASEEstablishment != nullptr)
Expand Down
9 changes: 3 additions & 6 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
#include <controller/CHIPDeviceControllerSystemState.h>
#include <controller/CommissioneeDeviceProxy.h>
#include <controller/CommissioningDelegate.h>
#include <controller/CurrentFabricRemover.h>
#include <controller/DevicePairingDelegate.h>
#include <controller/DeviceUnpair.h>
#include <controller/OperationalCredentialsDelegate.h>
#include <controller/SetUpCodePairer.h>
#include <credentials/FabricTable.h>
Expand Down Expand Up @@ -371,8 +371,7 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
public Protocols::UserDirectedCommissioning::InstanceNameResolver,
#endif
public SessionEstablishmentDelegate,
public app::ClusterStateCache::Callback,
public DeviceUnpair::Callback
public app::ClusterStateCache::Callback
{
public:
DeviceCommissioner();
Expand Down Expand Up @@ -532,8 +531,6 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
*/
CHIP_ERROR UnpairDevice(NodeId remoteDeviceId);

void OnDeviceUnpair(NodeId remoteDeviceId, CHIP_ERROR err) override;

//////////// SessionEstablishmentDelegate Implementation ///////////////
void OnSessionEstablishmentError(CHIP_ERROR error) override;
void OnSessionEstablished(const SessionHandle & session) override;
Expand Down Expand Up @@ -917,7 +914,7 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
Platform::UniquePtr<Credentials::DeviceAttestationVerifier::AttestationDeviceInfo> mAttestationDeviceInfo;
Credentials::DeviceAttestationVerifier * mDeviceAttestationVerifier = nullptr;

Platform::UniquePtr<DeviceUnpair> mDeviceUnpair;
// Platform::UniquePtr<DeviceUnpair> mDeviceUnpair;
};

} // namespace Controller
Expand Down
190 changes: 190 additions & 0 deletions src/controller/CurrentFabricRemover.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
*
* Copyright (c) 2021 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 <controller/CurrentFabricRemover.h>

#include <app-common/zap-generated/cluster-objects.h>
#include <controller-clusters/zap-generated/CHIPClusters.h>

using namespace chip::app::Clusters;

namespace chip {
namespace Controller {

CHIP_ERROR CurrentFabricRemover::CurrentFabricRemove(NodeId remoteDeviceId, Callback::Callback<OnCurrentFabricRemove> * callback)
{
mRemoteDeviceId = remoteDeviceId;
mCurrentFabricRemoveCallback = callback;
mNextStep = Step::kReadCurrentFabricIndex;

return mCommissioner->GetConnectedDevice(remoteDeviceId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}

CHIP_ERROR CurrentFabricRemover::ReadCurrentFabricIndex(Messaging::ExchangeManager & exchangeMgr, SessionHandle & sessionHandle)
{
using TypeInfo = chip::app::Clusters::OperationalCredentials::Attributes::CurrentFabricIndex::TypeInfo;
OperationalCredentialsCluster cluster(exchangeMgr, sessionHandle, kRootEndpointId);

return cluster.ReadAttribute<TypeInfo>(this, OnSuccessReadCurrentFabricIndex, OnReadAttributeFailure);
}

CHIP_ERROR CurrentFabricRemover::SendRemoveFabricIndex(Messaging::ExchangeManager & exchangeMgr, SessionHandle & sessionHandle)
{
if (mFabricIndex == kUndefinedFabricIndex)
{
return CHIP_ERROR_INVALID_FABRIC_INDEX;
}
ChipLogProgress(Controller, "SendRemoveFabricIndex : %u", mFabricIndex);

OperationalCredentials::Commands::RemoveFabric::Type request;
request.fabricIndex = mFabricIndex;

OperationalCredentialsCluster cluster(exchangeMgr, sessionHandle, 0);

return cluster.InvokeCommand(request, this, OnSuccessRemoveFabric, OnCommandFailure);
}

void CurrentFabricRemover::OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr,
SessionHandle & sessionHandle)
{
CHIP_ERROR err = CHIP_NO_ERROR;
auto * self = static_cast<CurrentFabricRemover *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));

switch (self->mNextStep)
{
case Step::kReadCurrentFabricIndex: {
err = self->ReadCurrentFabricIndex(exchangeMgr, sessionHandle);
break;
}
case Step::kSendRemoveFabric: {
err = self->SendRemoveFabricIndex(exchangeMgr, sessionHandle);
break;
}
default:
err = CHIP_ERROR_INCORRECT_STATE;
break;
}

if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "%" CHIP_ERROR_FORMAT, err.Format());
FinishRemoveCurrentFabric(context, err);
}

return;
}

void CurrentFabricRemover::OnDeviceConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR err)
{
ChipLogProgress(Controller, "OnDeviceConnectionFailureFn: %s", err.AsString());

auto * self = static_cast<CurrentFabricRemover *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));

FinishRemoveCurrentFabric(context, err);
return;
}

void CurrentFabricRemover::OnSuccessReadCurrentFabricIndex(void * context, uint8_t fabricIndex)
{
auto * self = static_cast<CurrentFabricRemover *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));
self->mFabricIndex = fabricIndex;
self->mNextStep = Step::kSendRemoveFabric;
CHIP_ERROR err = self->mCommissioner->GetConnectedDevice(self->mRemoteDeviceId, &self->mOnDeviceConnectedCallback,
&self->mOnDeviceConnectionFailureCallback);
if (err != CHIP_NO_ERROR)
{
FinishRemoveCurrentFabric(context, err);
}
return;
}

void CurrentFabricRemover::OnReadAttributeFailure(void * context, CHIP_ERROR err)
{
ChipLogProgress(Controller, "OnReadAttributeFailure %s", err.AsString());

auto * self = static_cast<CurrentFabricRemover *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));

FinishRemoveCurrentFabric(context, err);
return;
}

void CurrentFabricRemover::OnSuccessRemoveFabric(void * context,
const OperationalCredentials::Commands::NOCResponse::DecodableType & data)
{
auto * self = static_cast<CurrentFabricRemover *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));

FinishRemoveCurrentFabric(context, CHIP_NO_ERROR);
return;
}

void CurrentFabricRemover::OnCommandFailure(void * context, CHIP_ERROR err)
{
ChipLogProgress(Controller, "OnCommandFailure %s", err.AsString());

auto * self = static_cast<CurrentFabricRemover *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Device connected callback with null context. Ignoring"));

FinishRemoveCurrentFabric(context, err);
return;
}

void CurrentFabricRemover::FinishRemoveCurrentFabric(void * context, CHIP_ERROR err)
{
ChipLogError(Controller, "Remove Current Fabric Result : %" CHIP_ERROR_FORMAT, err.Format());
auto * self = static_cast<CurrentFabricRemover *>(context);
self->mNextStep = Step::kAcceptRemoveFabricStart;
if (self->mCurrentFabricRemoveCallback != nullptr)
{
self->mCurrentFabricRemoveCallback->mCall(self->mCurrentFabricRemoveCallback->mContext, self->mRemoteDeviceId, err);
}
}

AutoCurrentFabricRemover::AutoCurrentFabricRemover(DeviceCommissioner * commissioner) :
CurrentFabricRemover(commissioner), mOnRemoveCurrentFabricCallback(OnRemoveCurrentFabric, this)
{}

CHIP_ERROR AutoCurrentFabricRemover::RemoveCurrentFabric(DeviceCommissioner * commissoner, NodeId remoteDeviceId)
{
// Not using Platform::New because we want to keep our constructor private.
auto * remover = new (std::nothrow) AutoCurrentFabricRemover(commissoner);
if (remover == nullptr)
{
return CHIP_ERROR_NO_MEMORY;
}

CHIP_ERROR err = remover->CurrentFabricRemover::CurrentFabricRemove(remoteDeviceId, &remover->mOnRemoveCurrentFabricCallback);
if (err != CHIP_NO_ERROR)
{
delete remover;
}
// Else will clean up when the callback is called.
return err;
}

void AutoCurrentFabricRemover::OnRemoveCurrentFabric(void * context, NodeId remoteDeviceId, CHIP_ERROR status)
{
auto * self = static_cast<AutoCurrentFabricRemover *>(context);
delete self;
}
} // namespace Controller
} // namespace chip
Loading

0 comments on commit 2f2312e

Please sign in to comment.