Skip to content

Commit 2696bc9

Browse files
Non concurrent mode now sends connect network response as per spec (project-chip#31739)
* Update Non-concurrent mode to latest spec (project-chip#31660) * Handle unused variable (project-chip#31660) * Restyled by clang-format * Trigger from BTP, use regular member (project-chip#31660) * Fix override (project-chip#31660) * Restyled by whitespace * Restyled by clang-format * Use generic OperationalNetworkStarted (project-chip#31660) * Replace variable with mState (project-chip#31660) Use mState instead of mTerminateOnPacketTxComplete * Sorted namespace out (project-chip#31660) * Restyled by clang-format * Address review comments (project-chip#31660)ffffffffffffffffffff Close GATT so that BlueZ will disconnect. Add kOperationalNetworkStarted event type * Restyled by clang-format * Access BleLayer mState through function (project-chip#31660) * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 83cf341 commit 2696bc9

14 files changed

+97
-67
lines changed

src/app/clusters/network-commissioning/network-commissioning.cpp

+44-18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <lib/support/SortUtils.h>
3535
#include <lib/support/ThreadOperationalDataset.h>
3636
#include <platform/CHIPDeviceConfig.h>
37+
#include <platform/ConnectivityManager.h>
3738
#include <platform/DeviceControlServer.h>
3839
#include <platform/PlatformManager.h>
3940
#include <platform/internal/DeviceNetworkInfo.h>
@@ -135,6 +136,26 @@ void Instance::Shutdown()
135136
mpBaseDriver->Shutdown();
136137
}
137138

139+
#if !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
140+
void Instance::SendNonConcurrentConnectNetworkResponse()
141+
{
142+
auto commandHandleRef = std::move(mAsyncCommandHandle);
143+
auto commandHandle = commandHandleRef.Get();
144+
if (commandHandle == nullptr)
145+
{
146+
return;
147+
}
148+
149+
#if CONFIG_NETWORK_LAYER_BLE
150+
DeviceLayer::ConnectivityMgr().GetBleLayer()->IndicateBleClosing();
151+
#endif // CONFIG_NETWORK_LAYER_BLE
152+
ChipLogProgress(NetworkProvisioning, "Non-concurrent mode. Send ConnectNetworkResponse(Success)");
153+
Commands::ConnectNetworkResponse::Type response;
154+
response.networkingStatus = NetworkCommissioning::Status::kSuccess;
155+
commandHandle->AddResponse(mPath, response);
156+
}
157+
#endif // CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
158+
138159
void Instance::InvokeCommand(HandlerContext & ctxt)
139160
{
140161
if (mAsyncCommandHandle.Get() != nullptr)
@@ -177,12 +198,7 @@ void Instance::InvokeCommand(HandlerContext & ctxt)
177198

178199
case Commands::ConnectNetwork::Id: {
179200
VerifyOrReturn(mFeatureFlags.Has(Feature::kWiFiNetworkInterface) || mFeatureFlags.Has(Feature::kThreadNetworkInterface));
180-
#if CONFIG_NETWORK_LAYER_BLE && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
181-
// If commissionee does not support Concurrent Connections, request the BLE to be stopped.
182-
// Start the ConnectNetwork, but this will not complete until the BLE is off.
183-
ChipLogProgress(NetworkProvisioning, "Closing BLE connections due to non-concurrent mode");
184-
DeviceLayer::DeviceControlServer::DeviceControlSvr().PostCloseAllBLEConnectionsToOperationalNetworkEvent();
185-
#endif
201+
186202
HandleCommand<Commands::ConnectNetwork::DecodableType>(
187203
ctxt, [this](HandlerContext & ctx, const auto & req) { HandleConnectNetwork(ctx, req); });
188204
return;
@@ -708,18 +724,22 @@ void Instance::HandleConnectNetwork(HandlerContext & ctx, const Commands::Connec
708724
mAsyncCommandHandle = CommandHandler::Handle(&ctx.mCommandHandler);
709725
mCurrentOperationBreadcrumb = req.breadcrumb;
710726

711-
// In Non-concurrent mode postpone the final execution of ConnectNetwork until the operational
712-
// network has been fully brought up and kWiFiDeviceAvailable is delivered.
713-
// mConnectingNetworkIDLen and mConnectingNetworkID contains the received SSID
714727
#if CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
715728
mpWirelessDriver->ConnectNetwork(req.networkID, this);
729+
#else
730+
// In Non-concurrent mode postpone the final execution of ConnectNetwork until the operational
731+
// network has been fully brought up and kOperationalNetworkStarted is delivered.
732+
// mConnectingNetworkIDLen and mConnectingNetworkID contain the received SSID
733+
// As per spec, send the ConnectNetworkResponse(Success) prior to releasing the commissioning channel
734+
SendNonConcurrentConnectNetworkResponse();
716735
#endif
717736
}
718737

719738
void Instance::HandleNonConcurrentConnectNetwork()
720739
{
721740
ByteSpan nonConcurrentNetworkID = ByteSpan(mConnectingNetworkID, mConnectingNetworkIDLen);
722-
ChipLogProgress(NetworkProvisioning, "HandleNonConcurrentConnectNetwork() SSID=%s", mConnectingNetworkID);
741+
ChipLogProgress(NetworkProvisioning, "Non-concurrent mode, Connect to Network SSID=%.*s", mConnectingNetworkIDLen,
742+
mConnectingNetworkID);
723743
mpWirelessDriver->ConnectNetwork(nonConcurrentNetworkID, this);
724744
}
725745

@@ -826,13 +846,19 @@ void Instance::HandleQueryIdentity(HandlerContext & ctx, const Commands::QueryId
826846
void Instance::OnResult(Status commissioningError, CharSpan debugText, int32_t interfaceStatus)
827847
{
828848
auto commandHandleRef = std::move(mAsyncCommandHandle);
829-
auto commandHandle = commandHandleRef.Get();
849+
850+
// In Non-concurrent mode the commandHandle will be null here, the ConnectNetworkResponse
851+
// has already been sent and the BLE will have been stopped, however the other functionality
852+
// is still required
853+
#if CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
854+
auto commandHandle = commandHandleRef.Get();
830855
if (commandHandle == nullptr)
831856
{
832857
// When the platform shutted down, interaction model engine will invalidate all commandHandle to avoid dangling references.
833858
// We may receive the callback after it and should make it noop.
834859
return;
835860
}
861+
#endif // CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
836862

837863
Commands::ConnectNetworkResponse::Type response;
838864
response.networkingStatus = commissioningError;
@@ -856,13 +882,10 @@ void Instance::OnResult(Status commissioningError, CharSpan debugText, int32_t i
856882
memcpy(mLastNetworkID, mConnectingNetworkID, mLastNetworkIDLen);
857883
mLastNetworkingStatusValue.SetNonNull(commissioningError);
858884

859-
#if CONFIG_NETWORK_LAYER_BLE && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
860-
ChipLogProgress(NetworkProvisioning, "Non-concurrent mode, ConnectNetworkResponse will NOT be sent");
861-
// Do not send the ConnectNetworkResponse if in non-concurrent mode
862-
// Issue #30576 raised to modify CommandHandler to notify it if no response required
863-
#else
885+
#if CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
864886
commandHandle->AddResponse(mPath, response);
865-
#endif
887+
#endif // CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
888+
866889
if (commissioningError == Status::kSuccess)
867890
{
868891
CommitSavedBreadcrumb();
@@ -1063,8 +1086,11 @@ void Instance::OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event
10631086
{
10641087
this_->OnFailSafeTimerExpired();
10651088
}
1066-
else if (event->Type == DeviceLayer::DeviceEventType::kWiFiDeviceAvailable)
1089+
else if ((event->Type == DeviceLayer::DeviceEventType::kWiFiDeviceAvailable) ||
1090+
(event->Type == DeviceLayer::DeviceEventType::kOperationalNetworkStarted))
1091+
10671092
{
1093+
// In Non-Concurrent mode connect the operational channel, as BLE has been stopped
10681094
this_->HandleNonConcurrentConnectNetwork();
10691095
}
10701096
}

src/app/clusters/network-commissioning/network-commissioning.h

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class Instance : public CommandHandlerInterface,
7878
void OnCommissioningComplete();
7979
void OnFailSafeTimerExpired();
8080

81+
#if !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
82+
void SendNonConcurrentConnectNetworkResponse();
83+
#endif
8184
const BitFlags<Feature> mFeatureFlags;
8285

8386
DeviceLayer::NetworkCommissioning::Internal::WirelessDriver * const mpWirelessDriver;

src/app/server/CommissioningWindowManager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ void CommissioningWindowManager::OnPlatformEvent(const DeviceLayer::ChipDeviceEv
8989
#if CONFIG_NETWORK_LAYER_BLE
9090
else if (event->Type == DeviceLayer::DeviceEventType::kCloseAllBleConnections)
9191
{
92-
ChipLogProgress(AppServer, "Received kCloseAllBleConnections");
93-
mServer->GetBleLayerObject()->CloseAllBleConnections();
92+
ChipLogProgress(AppServer, "Received kCloseAllBleConnections:%d", static_cast<int>(event->Type));
93+
mServer->GetBleLayerObject()->Shutdown();
9494
}
9595
#endif
9696
}

src/ble/BLEEndPoint.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ CHIP_ERROR BLEEndPoint::DriveSending()
10401040
else
10411041
{
10421042
// Nothing to send!
1043+
mBle->mApplicationDelegate->CheckNonConcurrentBleClosing();
10431044
}
10441045
}
10451046

src/ble/BleApplicationDelegate.h

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class DLL_EXPORT BleApplicationDelegate
4242
// The application can use this callback to e.g. close the underlying BLE connection if it is no longer needed,
4343
// decrement the connection's refcount if it has one, or perform any other sort of cleanup as desired.
4444
virtual void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT connObj) = 0;
45+
46+
// Called to determine whether the BLE connection should be closed when in Non-concurrent mode if sending
47+
// ConnectNetworkResponse. The BTP will be in kState_Complete when all fragments have been sent.
48+
virtual void CheckNonConcurrentBleClosing() {}
4549
};
4650

4751
} /* namespace Ble */

src/ble/BleLayer.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ CHIP_ERROR BleLayer::Init(BlePlatformDelegate * platformDelegate, BleApplication
300300
return Init(platformDelegate, nullptr, appDelegate, systemLayer);
301301
}
302302

303+
void BleLayer::IndicateBleClosing()
304+
{
305+
mState = kState_Disconnecting;
306+
}
307+
303308
void BleLayer::Shutdown()
304309
{
305310
mState = kState_NotInitialized;

src/ble/BleLayer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ class DLL_EXPORT BleLayer
226226
enum
227227
{
228228
kState_NotInitialized = 0,
229-
kState_Initialized = 1
229+
kState_Initialized = 1,
230+
kState_Disconnecting = 2
230231
} mState; ///< [READ-ONLY] Current state
231232

232233
// This app state is not used by ble transport etc, it will be used by external ble implementation like Android
@@ -243,6 +244,7 @@ class DLL_EXPORT BleLayer
243244
chip::System::Layer * systemLayer);
244245
CHIP_ERROR Init(BlePlatformDelegate * platformDelegate, BleConnectionDelegate * connDelegate,
245246
BleApplicationDelegate * appDelegate, chip::System::Layer * systemLayer);
247+
void IndicateBleClosing();
246248
void Shutdown();
247249

248250
CHIP_ERROR CancelBleIncompleteConnection();

src/controller/CHIPDeviceController.cpp

+2-42
Original file line numberDiff line numberDiff line change
@@ -1636,27 +1636,6 @@ void OnBasicFailure(void * context, CHIP_ERROR error)
16361636
commissioner->CommissioningStageComplete(error);
16371637
}
16381638

1639-
static void NonConcurrentTimeout(void * context, CHIP_ERROR error)
1640-
{
1641-
if (error == CHIP_ERROR_TIMEOUT)
1642-
{
1643-
ChipLogProgress(Controller, "Non-concurrent mode: Expected NetworkResponse Timeout, do nothing");
1644-
}
1645-
else
1646-
{
1647-
ChipLogProgress(Controller, "Non-concurrent mode: Received failure response %" CHIP_ERROR_FORMAT, error.Format());
1648-
}
1649-
}
1650-
1651-
static void NonConcurrentNetworkResponse(void * context,
1652-
const NetworkCommissioning::Commands::ConnectNetworkResponse::DecodableType & data)
1653-
{
1654-
// In Non Concurrent mode the commissioning network should have been shut down and not sent the
1655-
// ConnectNetworkResponse. In case it does send it this handles the message
1656-
ChipLogError(Controller, "Non-concurrent Mode : Received Unexpected ConnectNetwork response, ignoring. Status=%u",
1657-
to_underlying(data.networkingStatus));
1658-
}
1659-
16601639
void DeviceCommissioner::CleanupCommissioning(DeviceProxy * proxy, NodeId nodeId, const CompletionStatus & completionStatus)
16611640
{
16621641
commissioningCompletionStatus = completionStatus;
@@ -3061,28 +3040,9 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio
30613040
request.breadcrumb.Emplace(breadcrumb);
30623041

30633042
CHIP_ERROR err = CHIP_NO_ERROR;
3064-
GeneralCommissioning::Attributes::SupportsConcurrentConnection::TypeInfo::Type supportsConcurrentConnection;
3065-
supportsConcurrentConnection = params.GetSupportsConcurrentConnection().Value();
30663043
ChipLogProgress(Controller, "SendCommand kWiFiNetworkEnable, supportsConcurrentConnection=%d",
3067-
supportsConcurrentConnection);
3068-
if (supportsConcurrentConnection)
3069-
{
3070-
err = SendCommand(proxy, request, OnConnectNetworkResponse, OnBasicFailure, endpoint, timeout);
3071-
}
3072-
else
3073-
{
3074-
// Concurrent Connections not allowed. Send the ConnectNetwork command but do not wait for the
3075-
// ConnectNetworkResponse on the Commissioning network as it will not be present. Log the expected timeout
3076-
// and run what would have been in the onConnectNetworkResponse callback.
3077-
err = SendCommand(proxy, request, NonConcurrentNetworkResponse, NonConcurrentTimeout, endpoint, NullOptional);
3078-
if (err == CHIP_NO_ERROR)
3079-
{
3080-
// As there will be no ConnectNetworkResponse, it is an implicit kSuccess so a default report is fine
3081-
CommissioningDelegate::CommissioningReport report;
3082-
CommissioningStageComplete(CHIP_NO_ERROR, report);
3083-
return;
3084-
}
3085-
}
3044+
params.GetSupportsConcurrentConnection().Value());
3045+
err = SendCommand(proxy, request, OnConnectNetworkResponse, OnBasicFailure, endpoint, timeout);
30863046

30873047
if (err != CHIP_NO_ERROR)
30883048
{

src/include/platform/CHIPDeviceEvent.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ enum PublicEventTypes
169169

170170
/**
171171
* When supportsConcurrentConnection = False, the ConnectNetwork command cannot start until
172-
* the BLE device is closed and the WiFi device has been started.
172+
* the BLE device is closed and the Operation Network device (e.g. WiFi) has been started.
173173
*/
174174
kWiFiDeviceAvailable,
175+
kOperationalNetworkStarted,
175176

176177
/**
177178
* Thread State Change

src/include/platform/DeviceControlServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DeviceControlServer final
3838
CHIP_ERROR PostConnectedToOperationalNetworkEvent(ByteSpan networkID);
3939
CHIP_ERROR PostCloseAllBLEConnectionsToOperationalNetworkEvent();
4040
CHIP_ERROR PostWiFiDeviceAvailableNetworkEvent();
41+
CHIP_ERROR PostOperationalNetworkStartedEvent();
4142
static DeviceControlServer & DeviceControlSvr();
4243

4344
private:

src/platform/DeviceControlServer.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,12 @@ CHIP_ERROR DeviceControlServer::PostWiFiDeviceAvailableNetworkEvent()
8787
return PlatformMgr().PostEvent(&event);
8888
}
8989

90+
CHIP_ERROR DeviceControlServer::PostOperationalNetworkStartedEvent()
91+
{
92+
ChipDeviceEvent event;
93+
event.Type = DeviceEventType::kOperationalNetworkStarted;
94+
return PlatformMgr().PostEvent(&event);
95+
}
96+
9097
} // namespace DeviceLayer
9198
} // namespace chip

src/platform/Linux/BLEManagerImpl.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343

4444
#include "bluez/BluezEndpoint.h"
4545

46+
#if !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
47+
#include <platform/DeviceControlServer.h>
48+
#endif
49+
4650
using namespace ::nl;
4751
using namespace ::chip::Ble;
4852

@@ -650,8 +654,23 @@ void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId)
650654
{
651655
ChipLogProgress(Ble, "Got notification regarding chip connection closure");
652656
#if CHIP_DEVICE_CONFIG_ENABLE_WPA && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
653-
// In Non-Concurrent mode start the Wi-Fi, as BLE has been stopped
654-
DeviceLayer::ConnectivityMgrImpl().StartNonConcurrentWiFiManagement();
657+
if (mState == kState_NotInitialized)
658+
{
659+
// Close BLE GATT connections to disconnect BlueZ
660+
CloseConnection(conId);
661+
// In Non-Concurrent mode start the Wi-Fi, as BLE has been stopped
662+
DeviceLayer::ConnectivityMgrImpl().StartNonConcurrentWiFiManagement();
663+
}
664+
#endif // CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
665+
}
666+
667+
void BLEManagerImpl::CheckNonConcurrentBleClosing()
668+
{
669+
#if CHIP_DEVICE_CONFIG_ENABLE_WPA && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
670+
if (mState == kState_Disconnecting)
671+
{
672+
DeviceLayer::DeviceControlServer::DeviceControlSvr().PostCloseAllBLEConnectionsToOperationalNetworkEvent();
673+
}
655674
#endif
656675
}
657676

src/platform/Linux/BLEManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class BLEManagerImpl final : public BLEManager,
133133
// ===== Members that implement virtual methods on BleApplicationDelegate.
134134

135135
void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
136+
void CheckNonConcurrentBleClosing() override;
136137

137138
// ===== Members that implement virtual methods on BleConnectionDelegate.
138139

src/platform/Linux/ConnectivityManagerImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ void ConnectivityManagerImpl::StartNonConcurrentWiFiManagement()
774774
{
775775
if (IsWiFiManagementStarted())
776776
{
777-
DeviceControlServer::DeviceControlSvr().PostWiFiDeviceAvailableNetworkEvent();
777+
DeviceControlServer::DeviceControlSvr().PostOperationalNetworkStartedEvent();
778778
ChipLogProgress(DeviceLayer, "Non-concurrent mode Wi-Fi Management Started.");
779779
return;
780780
}

0 commit comments

Comments
 (0)