Skip to content

Commit e032c6e

Browse files
crlonxpwoody-apple
authored andcommitted
* Change to have the consistent naming of the new added variables
* Fix the problem to add tests in TestQRCode.cpp * Use a list to track the validation of the caller object. Signed-off-by: Lo,Chin-Ran <chin-ran.lo@nxp.com>
1 parent 19ccdba commit e032c6e

File tree

8 files changed

+107
-50
lines changed

8 files changed

+107
-50
lines changed

examples/platform/linux/AppMain.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ int ChipLinuxAppInit(int argc, char * const argv[], OptionSet * customOptions,
483483
ChipLogProgress(NotSpecified, "Wi-Fi Management started");
484484
DeviceLayer::ConnectivityManager::WiFiPAFAdvertiseParam args;
485485
args.enable = LinuxDeviceOptions::GetInstance().mWiFiPAF;
486-
args.ExtCmds = LinuxDeviceOptions::GetInstance().mWiFiPafExtCmds;
486+
args.ExtCmds = LinuxDeviceOptions::GetInstance().mWiFiPAFExtCmds;
487487
DeviceLayer::ConnectivityMgr().SetWiFiPAFAdvertisingEnabled(args);
488488
}
489489
}

examples/platform/linux/Options.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ bool HandleOption(const char * aProgram, OptionSet * aOptions, int aIdentifier,
604604
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
605605
case kDeviceOption_WiFi_PAF: {
606606
LinuxDeviceOptions::GetInstance().mWiFiPAF = true;
607-
LinuxDeviceOptions::GetInstance().mWiFiPafExtCmds = aValue;
607+
LinuxDeviceOptions::GetInstance().mWiFiPAFExtCmds = aValue;
608608
break;
609609
}
610610
#endif

examples/platform/linux/Options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct LinuxDeviceOptions
5151
bool mThread = false;
5252
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
5353
bool mWiFiPAF = false;
54-
const char * mWiFiPafExtCmds = nullptr;
54+
const char * mWiFiPAFExtCmds = nullptr;
5555
#endif
5656
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE || CHIP_DEVICE_ENABLE_PORT_PARAMS
5757
uint16_t securedDevicePort = CHIP_PORT;

src/controller/CHIPDeviceController.cpp

+69-20
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ using namespace chip::Encoding;
9393
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
9494
using namespace chip::Protocols::UserDirectedCommissioning;
9595
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
96-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
97-
static std::shared_ptr<DeviceCommissioner> DevCommPtr;
98-
void custom_del(DeviceCommissioner * p){};
99-
#endif
10096

10197
DeviceController::DeviceController()
10298
{
@@ -473,6 +469,13 @@ DeviceCommissioner::DeviceCommissioner() :
473469
mDeviceNOCChainCallback(OnDeviceNOCChainGeneration, this), mSetUpCodePairer(this)
474470
{}
475471

472+
DeviceCommissioner::~DeviceCommissioner()
473+
{
474+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
475+
SetChkObjValid((void *)this, ObjChkAction::Clear, nullptr);
476+
#endif
477+
}
478+
476479
CHIP_ERROR DeviceCommissioner::Init(CommissionerInitParams params)
477480
{
478481
VerifyOrReturnError(params.operationalCredentialsDelegate != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
@@ -829,9 +832,9 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re
829832
ChipLogError(Controller, "Wi-Fi Management should have be started now.");
830833
ExitNow(CHIP_ERROR_INTERNAL);
831834
}
832-
DevCommPtr.reset(this, custom_del);
833835
mRendezvousParametersForDeviceDiscoveredOverWiFiPAF = params;
834-
DeviceLayer::ConnectivityMgr().WiFiPAFConnect(params.GetSetupDiscriminator().value(), (void *) (&(DevCommPtr)),
836+
SetChkObjValid((void*)this, ObjChkAction::Set, nullptr);
837+
DeviceLayer::ConnectivityMgr().WiFiPAFConnect(params.GetSetupDiscriminator().value(), (void *) this,
835838
OnWiFiPAFSubscribeComplete, OnWiFiPAFSubscribeError);
836839
ExitNow(CHIP_NO_ERROR);
837840
}
@@ -905,19 +908,66 @@ void DeviceCommissioner::OnDiscoveredDeviceOverBleError(void * appState, CHIP_ER
905908
#endif // CONFIG_NETWORK_LAYER_BLE
906909

907910
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
911+
void DeviceCommissioner::SetChkObjValid(void * appObj, ObjChkAction action, bool* pIsObjValid)
912+
{
913+
static std::vector<void*> ObjVector;
914+
bool IsObjValid = false;
915+
916+
switch (action) {
917+
case ObjChkAction::Set: {
918+
for (auto lt=ObjVector.begin(); lt!=ObjVector.end(); lt++)
919+
{
920+
if (*lt == appObj) {
921+
IsObjValid = true;
922+
break;
923+
}
924+
}
925+
if (IsObjValid == false) {
926+
ObjVector.push_back(appObj);
927+
IsObjValid = true;
928+
}
929+
}
930+
break;
931+
case ObjChkAction::Check: {
932+
for (auto lt=ObjVector.begin(); lt!=ObjVector.end(); lt++)
933+
{
934+
if (*lt == appObj) {
935+
IsObjValid = true;
936+
break;
937+
}
938+
}
939+
}
940+
break;
941+
case ObjChkAction::Clear: {
942+
for (auto lt=ObjVector.begin(); lt!=ObjVector.end(); lt++)
943+
{
944+
if (*lt == appObj) {
945+
// Already existed in the list => Remove it
946+
ObjVector.erase(lt);
947+
break;
948+
}
949+
}
950+
}
951+
break;
952+
}
953+
if (pIsObjValid != nullptr) {
954+
*pIsObjValid = IsObjValid;
955+
}
956+
return;
957+
}
958+
908959
void DeviceCommissioner::OnWiFiPAFSubscribeComplete(void * appState)
909960
{
910-
std::weak_ptr<DeviceCommissioner> * caller = (std::weak_ptr<DeviceCommissioner> *) (appState);
911-
std::shared_ptr<DeviceCommissioner> selfShrPtr = caller->lock();
912-
if (!selfShrPtr)
913-
{
914-
ChipLogError(Controller, "DeviceCommissioner was destroyed unexpectedly!");
961+
bool isObjValid;
962+
SetChkObjValid(appState, ObjChkAction::Check, &isObjValid);
963+
if (isObjValid == false) {
964+
// The caller has been released.
965+
ChipLogError(Controller, "DeviceCommissioner has been destroyed!");
915966
return;
916967
}
917-
auto self = selfShrPtr.get();
968+
auto self = (DeviceCommissioner*) appState;
918969
auto device = self->mDeviceInPASEEstablishment;
919970

920-
selfShrPtr.reset();
921971
if (nullptr != device && device->GetDeviceTransportType() == Transport::Type::kWiFiPAF)
922972
{
923973
ChipLogProgress(Controller, "WiFi-PAF: Subscription Completed, dev_id = %lu", device->GetDeviceId());
@@ -932,17 +982,16 @@ void DeviceCommissioner::OnWiFiPAFSubscribeComplete(void * appState)
932982

933983
void DeviceCommissioner::OnWiFiPAFSubscribeError(void * appState, CHIP_ERROR err)
934984
{
935-
std::weak_ptr<DeviceCommissioner> * caller = (std::weak_ptr<DeviceCommissioner> *) (appState);
936-
std::shared_ptr<DeviceCommissioner> selfShrPtr = caller->lock();
937-
if (!selfShrPtr)
938-
{
939-
ChipLogError(Controller, "Err: DeviceCommissioner was destroyed unexpectedly!");
985+
bool isObjValid;
986+
SetChkObjValid(appState, ObjChkAction::Check, &isObjValid);
987+
if (isObjValid == false) {
988+
// The caller has been released.
989+
ChipLogError(Controller, "DeviceCommissioner has been destroyed!");
940990
return;
941991
}
942-
auto self = static_cast<DeviceCommissioner *>(appState);
992+
auto self = (DeviceCommissioner*) appState;
943993
auto device = self->mDeviceInPASEEstablishment;
944994

945-
selfShrPtr.reset();
946995
if (nullptr != device && device->GetDeviceTransportType() == Transport::Type::kWiFiPAF)
947996
{
948997
ChipLogError(Controller, "WiFi-PAF: Subscription Error, id = %lu, err = %" CHIP_ERROR_FORMAT, device->GetDeviceId(),

src/controller/CHIPDeviceController.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
468468
{
469469
public:
470470
DeviceCommissioner();
471-
~DeviceCommissioner() override {}
471+
~DeviceCommissioner() override;
472472

473473
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY // make this commissioner discoverable
474474
/**
@@ -821,6 +821,14 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
821821
return ExtendArmFailSafeInternal(proxy, step, armFailSafeTimeout, commandTimeout, onSuccess, onFailure,
822822
/* fireAndForget = */ true);
823823
}
824+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
825+
enum ObjChkAction{
826+
Set,
827+
Check,
828+
Clear
829+
};
830+
static void SetChkObjValid(void * appState, ObjChkAction action, bool * pIsObjValid);
831+
#endif
824832

825833
private:
826834
DevicePairingDelegate * mPairingDelegate = nullptr;

src/controller/SetUpCodePairer.cpp

+23-24
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ CHIP_ERROR GetPayload(const char * setUpCode, SetupPayload & payload)
6060
}
6161
} // namespace
6262

63+
SetUpCodePairer::~SetUpCodePairer()
64+
{
65+
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
66+
DeviceCommissioner::SetChkObjValid((void *)this, DeviceCommissioner::ObjChkAction::Clear, nullptr);
67+
#endif
68+
}
69+
6370
CHIP_ERROR SetUpCodePairer::PairDevice(NodeId remoteId, const char * setUpCode, SetupCodePairerBehaviour commission,
6471
DiscoveryType discoveryType, Optional<Dnssd::CommonResolutionData> resolutionData)
6572
{
@@ -253,21 +260,15 @@ CHIP_ERROR SetUpCodePairer::StopConnectOverSoftAP()
253260
return CHIP_NO_ERROR;
254261
}
255262

256-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
257-
static std::shared_ptr<SetUpCodePairer> SetUpCodeParserPtr;
258-
void custom_del(SetUpCodePairer * p){};
259-
#endif
260-
261263
CHIP_ERROR SetUpCodePairer::StartDiscoverOverWiFiPAF(SetupPayload & payload)
262264
{
263265
#if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
264266
ChipLogProgress(Controller, "Starting commissioning discovery over WiFiPAF");
265-
266267
VerifyOrReturnError(mCommissioner != nullptr, CHIP_ERROR_INCORRECT_STATE);
267268
mWaitingForDiscovery[kWiFiPAFTransport] = true;
268269

269-
SetUpCodeParserPtr.reset(this, custom_del);
270-
CHIP_ERROR err = DeviceLayer::ConnectivityMgr().WiFiPAFConnect(payload.discriminator, (void *) (&(SetUpCodeParserPtr)),
270+
DeviceCommissioner::SetChkObjValid((void*)this, DeviceCommissioner::ObjChkAction::Set, nullptr);
271+
CHIP_ERROR err = DeviceLayer::ConnectivityMgr().WiFiPAFConnect(payload.discriminator, (void *) this,
271272
OnWiFiPAFSubscribeComplete, OnWiFiPAFSubscribeError);
272273
if (err != CHIP_NO_ERROR)
273274
{
@@ -398,30 +399,28 @@ void SetUpCodePairer::OnWifiPAFDiscoveryError(CHIP_ERROR err)
398399

399400
void SetUpCodePairer::OnWiFiPAFSubscribeComplete(void * appState)
400401
{
401-
std::weak_ptr<SetUpCodePairer> * caller = (std::weak_ptr<SetUpCodePairer> *) (appState);
402-
std::shared_ptr<SetUpCodePairer> selfShrPtr = caller->lock();
403-
if (!selfShrPtr)
404-
{
405-
ChipLogError(Controller, "SetUpCodePairer is destroyed unexpectedly!");
402+
bool isObjValid;
403+
DeviceCommissioner::SetChkObjValid(appState, DeviceCommissioner::ObjChkAction::Check, &isObjValid);
404+
if (isObjValid == false) {
405+
// The caller has been released.
406+
ChipLogError(Controller, "SetUpCodePairer has been destroyed!");
406407
return;
407408
}
408-
selfShrPtr.get()->OnDiscoveredDeviceOverWifiPAF();
409-
// Release the ownership
410-
selfShrPtr.reset();
409+
auto self = (SetUpCodePairer*) appState;
410+
self->OnDiscoveredDeviceOverWifiPAF();
411411
}
412412

413413
void SetUpCodePairer::OnWiFiPAFSubscribeError(void * appState, CHIP_ERROR err)
414414
{
415-
std::weak_ptr<SetUpCodePairer> * caller = (std::weak_ptr<SetUpCodePairer> *) (appState);
416-
std::shared_ptr<SetUpCodePairer> selfShrPtr = caller->lock();
417-
if (!selfShrPtr)
418-
{
419-
ChipLogError(Controller, "Err: SetUpCodePairer was destroyed unexpectedly!");
415+
bool isObjValid;
416+
DeviceCommissioner::SetChkObjValid(appState, DeviceCommissioner::ObjChkAction::Check, &isObjValid);
417+
if (isObjValid == false) {
418+
// The caller has been released.
419+
ChipLogError(Controller, "SetUpCodePairer has been destroyed!");
420420
return;
421421
}
422-
selfShrPtr.get()->OnWifiPAFDiscoveryError(err);
423-
// Release the ownership
424-
selfShrPtr.reset();
422+
auto self = (SetUpCodePairer*) appState;
423+
self->OnWifiPAFDiscoveryError(err);
425424
}
426425
#endif
427426

src/controller/SetUpCodePairer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class DLL_EXPORT SetUpCodePairer : public DevicePairingDelegate
7777
{
7878
public:
7979
SetUpCodePairer(DeviceCommissioner * commissioner) : mCommissioner(commissioner) {}
80-
virtual ~SetUpCodePairer() {}
80+
~SetUpCodePairer();
8181

8282
CHIP_ERROR PairDevice(chip::NodeId remoteId, const char * setUpCode,
8383
SetupCodePairerBehaviour connectionType = SetupCodePairerBehaviour::kCommission,

src/setup_payload/tests/TestQRCode.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ TEST(TestQRCode, TestRendezvousFlags)
7474

7575
inPayload.rendezvousInformation.SetValue(RendezvousInformationFlags(
7676
RendezvousInformationFlag::kWiFiPAF, RendezvousInformationFlag::kSoftAP, RendezvousInformationFlag::kOnNetwork));
77+
EXPECT_TRUE(CheckWriteRead(inPayload));
7778

7879
inPayload.rendezvousInformation.SetValue(RendezvousInformationFlags(
7980
RendezvousInformationFlag::kWiFiPAF, RendezvousInformationFlag::kBLE, RendezvousInformationFlag::kOnNetwork));
81+
EXPECT_TRUE(CheckWriteRead(inPayload));
8082

8183
inPayload.rendezvousInformation.SetValue(
8284
RendezvousInformationFlags(RendezvousInformationFlag::kWiFiPAF, RendezvousInformationFlag::kBLE,
8385
RendezvousInformationFlag::kSoftAP, RendezvousInformationFlag::kOnNetwork));
84-
8586
EXPECT_TRUE(CheckWriteRead(inPayload));
8687
}
8788

0 commit comments

Comments
 (0)