@@ -93,10 +93,6 @@ using namespace chip::Encoding;
93
93
#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
94
94
using namespace chip ::Protocols::UserDirectedCommissioning;
95
95
#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
100
96
101
97
DeviceController::DeviceController ()
102
98
{
@@ -473,6 +469,13 @@ DeviceCommissioner::DeviceCommissioner() :
473
469
mDeviceNOCChainCallback (OnDeviceNOCChainGeneration, this ), mSetUpCodePairer (this )
474
470
{}
475
471
472
+ DeviceCommissioner::~DeviceCommissioner ()
473
+ {
474
+ #if CHIP_DEVICE_CONFIG_ENABLE_WIFIPAF
475
+ SetChkObjValid ((void *)this , ObjChkAction::Clear, nullptr );
476
+ #endif
477
+ }
478
+
476
479
CHIP_ERROR DeviceCommissioner::Init (CommissionerInitParams params)
477
480
{
478
481
VerifyOrReturnError (params.operationalCredentialsDelegate != nullptr , CHIP_ERROR_INVALID_ARGUMENT);
@@ -829,9 +832,9 @@ CHIP_ERROR DeviceCommissioner::EstablishPASEConnection(NodeId remoteDeviceId, Re
829
832
ChipLogError (Controller, " Wi-Fi Management should have be started now." );
830
833
ExitNow (CHIP_ERROR_INTERNAL);
831
834
}
832
- DevCommPtr.reset (this , custom_del);
833
835
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 ,
835
838
OnWiFiPAFSubscribeComplete, OnWiFiPAFSubscribeError);
836
839
ExitNow (CHIP_NO_ERROR);
837
840
}
@@ -905,19 +908,66 @@ void DeviceCommissioner::OnDiscoveredDeviceOverBleError(void * appState, CHIP_ER
905
908
#endif // CONFIG_NETWORK_LAYER_BLE
906
909
907
910
#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
+
908
959
void DeviceCommissioner::OnWiFiPAFSubscribeComplete (void * appState)
909
960
{
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 !" );
915
966
return ;
916
967
}
917
- auto self = selfShrPtr. get () ;
968
+ auto self = (DeviceCommissioner*) appState ;
918
969
auto device = self->mDeviceInPASEEstablishment ;
919
970
920
- selfShrPtr.reset ();
921
971
if (nullptr != device && device->GetDeviceTransportType () == Transport::Type::kWiFiPAF )
922
972
{
923
973
ChipLogProgress (Controller, " WiFi-PAF: Subscription Completed, dev_id = %lu" , device->GetDeviceId ());
@@ -932,17 +982,16 @@ void DeviceCommissioner::OnWiFiPAFSubscribeComplete(void * appState)
932
982
933
983
void DeviceCommissioner::OnWiFiPAFSubscribeError (void * appState, CHIP_ERROR err)
934
984
{
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 !" );
940
990
return ;
941
991
}
942
- auto self = static_cast < DeviceCommissioner *>(appState) ;
992
+ auto self = ( DeviceCommissioner*) appState ;
943
993
auto device = self->mDeviceInPASEEstablishment ;
944
994
945
- selfShrPtr.reset ();
946
995
if (nullptr != device && device->GetDeviceTransportType () == Transport::Type::kWiFiPAF )
947
996
{
948
997
ChipLogError (Controller, " WiFi-PAF: Subscription Error, id = %lu, err = %" CHIP_ERROR_FORMAT, device->GetDeviceId (),
0 commit comments