blob: 251f4f0d9261ddcb2e6047a50c3312d83fc7509a [file] [log] [blame]
Martin Turon0c03fa82020-05-26 15:43:11 -07001/*
2 *
Kevin Schoedela8681872021-01-28 15:53:13 -05003 * Copyright (c) 2020-2021 Project CHIP Authors
Martin Turon0c03fa82020-05-26 15:43:11 -07004 * Copyright (c) 2018 Nest Labs, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/**
20 * @file
21 * Provides an implementation of the BLEManager singleton object
22 * for Linux platforms.
23 */
Martin Turon0c03fa82020-05-26 15:43:11 -070024
Arkadiusz Bokowy2a7ffdc2023-08-10 20:54:39 +020025/**
26 * Note: BLEManager requires ConnectivityManager to be defined beforehand,
27 * otherwise we will face circular dependency between them. */
28#include <platform/ConnectivityManager.h>
29
30/**
31 * Note: Use public include for BLEManager which includes our local
32 * platform/<PLATFORM>/BLEManagerImpl.h after defining interface class. */
33#include "platform/internal/BLEManager.h"
Martin Turon0c03fa82020-05-26 15:43:11 -070034
Boris Zbarsky444b50b2020-09-23 19:53:06 -040035#include <type_traits>
Damian Królike393b5e2021-01-14 22:29:11 +010036#include <utility>
Boris Zbarsky444b50b2020-09-23 19:53:06 -040037
Arkadiusz Bokowy99669402024-04-18 11:31:44 +020038#include <ble/Ble.h>
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +010039#include <lib/support/CHIPMemString.h>
Arkadiusz Bokowy2a7ffdc2023-08-10 20:54:39 +020040#include <lib/support/CodeUtils.h>
41#include <lib/support/SafeInt.h>
42#include <platform/CHIPDeviceLayer.h>
43#include <platform/CommissionableDataProvider.h>
Martin Turon0c03fa82020-05-26 15:43:11 -070044
Arkadiusz Bokowyac5b5182023-10-16 19:06:29 +020045#include "bluez/BluezEndpoint.h"
yunhanw-googlee860aab2020-08-19 09:31:09 -070046
simonhmorris12696bc92024-02-14 22:27:08 +000047#if !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
48#include <platform/DeviceControlServer.h>
49#endif
50
Martin Turon0c03fa82020-05-26 15:43:11 -070051using namespace ::nl;
yunhanw-googlee860aab2020-08-19 09:31:09 -070052using namespace ::chip::Ble;
Martin Turon0c03fa82020-05-26 15:43:11 -070053
54namespace chip {
55namespace DeviceLayer {
56namespace Internal {
57
yunhanw-googlee860aab2020-08-19 09:31:09 -070058namespace {
Andrei Litvin5ca73082021-02-25 11:53:45 -050059
rus08448ee5e02023-12-06 16:41:54 +030060static constexpr System::Clock::Timeout kNewConnectionScanTimeout = System::Clock::Seconds16(20);
61static constexpr System::Clock::Timeout kConnectTimeout = System::Clock::Seconds16(20);
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +010062static constexpr System::Clock::Timeout kFastAdvertiseTimeout =
63 System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
64#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
65// The CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS specifies the transition time
66// starting from advertisement commencement. Since the extended advertisement timer is started after
67// the fast-to-slow transition, we have to subtract the time spent in fast advertising.
68static constexpr System::Clock::Timeout kSlowAdvertiseTimeout = System::Clock::Milliseconds32(
69 CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS - CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
70static_assert(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS >=
71 CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME,
72 "The extended advertising interval change time must be greater than the fast advertising interval change time");
73#endif
Andrei Litvin5ca73082021-02-25 11:53:45 -050074
yunhanw-googlee860aab2020-08-19 09:31:09 -070075} // namespace
76
Martin Turon0c03fa82020-05-26 15:43:11 -070077BLEManagerImpl BLEManagerImpl::sInstance;
78
79CHIP_ERROR BLEManagerImpl::_Init()
80{
81 CHIP_ERROR err;
yunhanw-googlee860aab2020-08-19 09:31:09 -070082
Kevin Schoedel86eb0402021-09-10 08:37:38 -040083 err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer());
Martin Turon0c03fa82020-05-26 15:43:11 -070084 SuccessOrExit(err);
yunhanw-googlee860aab2020-08-19 09:31:09 -070085
86 mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
Kevin Schoedel8ea423a2021-03-11 11:49:18 -050087 mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART && !mIsCentral);
Kamil Kasperczyk7254f302021-03-19 19:45:23 +010088 mFlags.Set(Flags::kFastAdvertisingEnabled, true);
yunhanw-googlee860aab2020-08-19 09:31:09 -070089
90 memset(mDeviceName, 0, sizeof(mDeviceName));
91
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +010092 DeviceLayer::SystemLayer().ScheduleLambda([this] { DriveBLEState(); });
yunhanw-googlee860aab2020-08-19 09:31:09 -070093
Martin Turon0c03fa82020-05-26 15:43:11 -070094exit:
95 return err;
96}
97
Michael Spang63870492022-06-28 08:41:08 -040098void BLEManagerImpl::_Shutdown()
Andrei Litvin7b71fb22021-11-05 02:16:47 -040099{
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200100 // Make sure that timers are stopped before shutting down the BLE layer.
101 DeviceLayer::SystemLayer().CancelTimer(HandleScanTimer, this);
102 DeviceLayer::SystemLayer().CancelTimer(HandleAdvertisingTimer, this);
103 DeviceLayer::SystemLayer().CancelTimer(HandleConnectTimer, this);
104
Arkadiusz Bokowyfab81b32023-11-03 14:10:21 +0100105 mDeviceScanner.Shutdown();
Arkadiusz Bokowybc5ef902023-11-10 17:20:48 +0100106 mBLEAdvertisement.Shutdown();
Arkadiusz Bokowyc331f102023-11-14 17:18:01 +0100107 mEndpoint.Shutdown();
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200108
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100109 mBluezObjectManager.Shutdown();
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200110 mFlags.Clear(Flags::kBluezManagerInitialized);
Andrei Litvin7b71fb22021-11-05 02:16:47 -0400111}
112
Martin Turon0c03fa82020-05-26 15:43:11 -0700113CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
114{
115 CHIP_ERROR err = CHIP_NO_ERROR;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700116
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500117 if (mFlags.Has(Flags::kAdvertisingEnabled) != val)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700118 {
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500119 mFlags.Set(Flags::kAdvertisingEnabled, val);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700120 }
121
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100122 DeviceLayer::SystemLayer().ScheduleLambda([this] { DriveBLEState(); });
yunhanw-googlee860aab2020-08-19 09:31:09 -0700123
Martin Turon0c03fa82020-05-26 15:43:11 -0700124 return err;
125}
126
Kamil Kasperczyk7254f302021-03-19 19:45:23 +0100127CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
Martin Turon0c03fa82020-05-26 15:43:11 -0700128{
Kamil Kasperczyk7254f302021-03-19 19:45:23 +0100129 switch (mode)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700130 {
Kamil Kasperczyk7254f302021-03-19 19:45:23 +0100131 case BLEAdvertisingMode::kFastAdvertising:
132 mFlags.Set(Flags::kFastAdvertisingEnabled, true);
133 break;
134 case BLEAdvertisingMode::kSlowAdvertising:
135 mFlags.Set(Flags::kFastAdvertisingEnabled, false);
136 break;
137 default:
138 return CHIP_ERROR_INVALID_ARGUMENT;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700139 }
Kamil Kasperczyk7254f302021-03-19 19:45:23 +0100140 mFlags.Set(Flags::kAdvertisingRefreshNeeded);
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100141 DeviceLayer::SystemLayer().ScheduleLambda([this] { DriveBLEState(); });
Kamil Kasperczyk7254f302021-03-19 19:45:23 +0100142 return CHIP_NO_ERROR;
Martin Turon0c03fa82020-05-26 15:43:11 -0700143}
144
145CHIP_ERROR BLEManagerImpl::_GetDeviceName(char * buf, size_t bufSize)
146{
147 if (strlen(mDeviceName) >= bufSize)
148 {
149 return CHIP_ERROR_BUFFER_TOO_SMALL;
150 }
151 strcpy(buf, mDeviceName);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700152
Martin Turon0c03fa82020-05-26 15:43:11 -0700153 return CHIP_NO_ERROR;
154}
155
156CHIP_ERROR BLEManagerImpl::_SetDeviceName(const char * deviceName)
157{
Song Guo308b2a42020-10-28 14:25:49 +0800158 CHIP_ERROR err = CHIP_NO_ERROR;
159
160 VerifyOrExit(mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported, err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
161
Michael Spangc76e82d2020-09-23 14:32:23 -0400162 if (deviceName != nullptr && deviceName[0] != 0)
Martin Turon0c03fa82020-05-26 15:43:11 -0700163 {
Song Guo308b2a42020-10-28 14:25:49 +0800164 VerifyOrExit(strlen(deviceName) < kMaxDeviceNameLength, err = CHIP_ERROR_INVALID_ARGUMENT);
Martin Turon0c03fa82020-05-26 15:43:11 -0700165 strcpy(mDeviceName, deviceName);
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500166 mFlags.Set(Flags::kUseCustomDeviceName);
Martin Turon0c03fa82020-05-26 15:43:11 -0700167 }
168 else
169 {
Song Guo308b2a42020-10-28 14:25:49 +0800170 uint16_t discriminator;
Tennessee Carmel-Veilleux8f86c432022-03-11 15:49:56 -0500171 SuccessOrExit(err = GetCommissionableDataProvider()->GetSetupDiscriminator(discriminator));
Song Guo308b2a42020-10-28 14:25:49 +0800172 snprintf(mDeviceName, sizeof(mDeviceName), "%s%04u", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, discriminator);
173 mDeviceName[kMaxDeviceNameLength] = 0;
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500174 mFlags.Clear(Flags::kUseCustomDeviceName);
Martin Turon0c03fa82020-05-26 15:43:11 -0700175 }
yunhanw-googlee860aab2020-08-19 09:31:09 -0700176
Song Guo308b2a42020-10-28 14:25:49 +0800177exit:
178 return err;
Martin Turon0c03fa82020-05-26 15:43:11 -0700179}
180
Michael Spangb0e38c22020-09-30 14:20:50 -0400181uint16_t BLEManagerImpl::_NumConnections()
Martin Turon0c03fa82020-05-26 15:43:11 -0700182{
yunhanw-googlee860aab2020-08-19 09:31:09 -0700183 uint16_t numCons = 0;
184 return numCons;
185}
186
Song Guo7e075972021-03-05 22:53:05 +0800187CHIP_ERROR BLEManagerImpl::ConfigureBle(uint32_t aAdapterId, bool aIsCentral)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700188{
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100189 mAdapterId = aAdapterId;
190 mIsCentral = aIsCentral;
191 mpBLEAdvUUID = "0xFFF6";
Arkadiusz Bokowybc5ef902023-11-10 17:20:48 +0100192 return CHIP_NO_ERROR;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700193}
194
195void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
196{
197 switch (event->Type)
198 {
199 case DeviceEventType::kCHIPoBLESubscribe:
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200200 HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700201 {
Matthew Swartwout67226932024-04-26 08:35:42 -0700202 ChipDeviceEvent connectionEvent{ .Type = DeviceEventType::kCHIPoBLEConnectionEstablished };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400203 PlatformMgr().PostEventOrDie(&connectionEvent);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700204 }
205 break;
206
207 case DeviceEventType::kCHIPoBLEUnsubscribe:
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200208 HandleUnsubscribeReceived(event->CHIPoBLEUnsubscribe.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700209 break;
210
Kevin Schoedel59cb4f62020-12-09 13:55:52 -0500211 case DeviceEventType::kCHIPoBLEWriteReceived:
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200212 HandleWriteReceived(event->CHIPoBLEWriteReceived.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_1_UUID,
Kevin Schoedelbc4f8412021-01-08 14:46:39 -0500213 PacketBufferHandle::Adopt(event->CHIPoBLEWriteReceived.Data));
Kevin Schoedel59cb4f62020-12-09 13:55:52 -0500214 break;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700215
216 case DeviceEventType::kCHIPoBLEIndicateConfirm:
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200217 HandleIndicationConfirmation(event->CHIPoBLEIndicateConfirm.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700218 break;
219
220 case DeviceEventType::kCHIPoBLEConnectionError:
221 HandleConnectionError(event->CHIPoBLEConnectionError.ConId, event->CHIPoBLEConnectionError.Reason);
222 break;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700223 case DeviceEventType::kServiceProvisioningChange:
yunhanw-googlee860aab2020-08-19 09:31:09 -0700224 // Force the advertising configuration to be refreshed to reflect new provisioning state.
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500225 mFlags.Clear(Flags::kAdvertisingConfigured);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700226
227 DriveBLEState();
yunhanw-google24132422020-09-08 12:33:16 -0700228 break;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700229 default:
Damian Królike393b5e2021-01-14 22:29:11 +0100230 HandlePlatformSpecificBLEEvent(event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700231 break;
232 }
yunhanw-googlee860aab2020-08-19 09:31:09 -0700233}
234
235void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEvent)
236{
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100237 CHIP_ERROR err = CHIP_NO_ERROR;
Jiacheng Guoa79e3bd2021-04-28 03:11:52 +0800238 ChipLogDetail(DeviceLayer, "HandlePlatformSpecificBLEEvent %d", apEvent->Type);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700239 switch (apEvent->Type)
240 {
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +0100241 case DeviceEventType::kPlatformLinuxBLEAdapterAdded:
242 ChipLogDetail(DeviceLayer, "BLE adapter added: id=%u address=%s", apEvent->Platform.BLEAdapter.mAdapterId,
243 apEvent->Platform.BLEAdapter.mAdapterAddress);
244 if (apEvent->Platform.BLEAdapter.mAdapterId == mAdapterId)
245 {
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200246 mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
247 DriveBLEState();
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +0100248 }
249 break;
250 case DeviceEventType::kPlatformLinuxBLEAdapterRemoved:
251 ChipLogDetail(DeviceLayer, "BLE adapter removed: id=%u address=%s", apEvent->Platform.BLEAdapter.mAdapterId,
252 apEvent->Platform.BLEAdapter.mAdapterAddress);
253 if (apEvent->Platform.BLEAdapter.mAdapterId == mAdapterId)
254 {
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200255 // Shutdown all BLE operations and release resources
256 mDeviceScanner.Shutdown();
257 mBLEAdvertisement.Shutdown();
258 mEndpoint.Shutdown();
259 // Drop reference to the adapter
260 mAdapter.reset();
261 // Clear all flags related to BlueZ BLE operations
262 mFlags.Clear(Flags::kBluezAdapterAvailable);
263 mFlags.Clear(Flags::kBluezBLELayerInitialized);
264 mFlags.Clear(Flags::kAdvertisingConfigured);
265 mFlags.Clear(Flags::kAppRegistered);
266 mFlags.Clear(Flags::kAdvertising);
267 CleanScanConfig();
268 // Indicate that the adapter is no longer available
269 err = BLE_ERROR_ADAPTER_UNAVAILABLE;
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +0100270 }
271 break;
Damian Królike393b5e2021-01-14 22:29:11 +0100272 case DeviceEventType::kPlatformLinuxBLECentralConnected:
Damian Królik35806bc2021-03-24 16:47:48 +0100273 if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting)
274 {
Song Guo87142e02021-04-21 09:00:00 +0800275 BleConnectionDelegate::OnConnectionComplete(mBLEScanConfig.mAppState,
276 apEvent->Platform.BLECentralConnected.mConnection);
Damian Królik35806bc2021-03-24 16:47:48 +0100277 CleanScanConfig();
278 }
279 break;
280 case DeviceEventType::kPlatformLinuxBLECentralConnectFailed:
281 if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting)
282 {
Song Guo87142e02021-04-21 09:00:00 +0800283 BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, apEvent->Platform.BLECentralConnectFailed.mError);
Damian Królik35806bc2021-03-24 16:47:48 +0100284 CleanScanConfig();
285 }
Damian Królike393b5e2021-01-14 22:29:11 +0100286 break;
287 case DeviceEventType::kPlatformLinuxBLEWriteComplete:
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200288 HandleWriteConfirmation(apEvent->Platform.BLEWriteComplete.mConnection, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_1_UUID);
Damian Królike393b5e2021-01-14 22:29:11 +0100289 break;
290 case DeviceEventType::kPlatformLinuxBLESubscribeOpComplete:
291 if (apEvent->Platform.BLESubscribeOpComplete.mIsSubscribed)
292 HandleSubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &CHIP_BLE_SVC_ID,
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200293 &Ble::CHIP_BLE_CHAR_2_UUID);
Damian Królike393b5e2021-01-14 22:29:11 +0100294 else
295 HandleUnsubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &CHIP_BLE_SVC_ID,
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200296 &Ble::CHIP_BLE_CHAR_2_UUID);
Damian Królike393b5e2021-01-14 22:29:11 +0100297 break;
298 case DeviceEventType::kPlatformLinuxBLEIndicationReceived:
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200299 HandleIndicationReceived(apEvent->Platform.BLEIndicationReceived.mConnection, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID,
Damian Królike393b5e2021-01-14 22:29:11 +0100300 PacketBufferHandle::Adopt(apEvent->Platform.BLEIndicationReceived.mData));
301 break;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700302 case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete:
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100303 SuccessOrExit(err = apEvent->Platform.BLEPeripheralAdvStartComplete.mError);
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100304 mFlags.Clear(Flags::kControlOpInProgress).Clear(Flags::kAdvertisingRefreshNeeded);
Arkadiusz Bokowy003c4782024-02-23 10:54:21 +0100305 // Do not restart the timer if it is still active. This is to avoid the timer from being restarted
306 // if the advertising is stopped due to a premature release.
307 if (!DeviceLayer::SystemLayer().IsTimerActive(HandleAdvertisingTimer, this))
308 {
309 // Start a timer to make sure that the fast advertising is stopped after specified timeout.
310 SuccessOrExit(err = DeviceLayer::SystemLayer().StartTimer(kFastAdvertiseTimeout, HandleAdvertisingTimer, this));
311 }
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100312 mFlags.Set(Flags::kAdvertising);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700313 break;
314 case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete:
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100315 SuccessOrExit(err = apEvent->Platform.BLEPeripheralAdvStopComplete.mError);
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100316 mFlags.Clear(Flags::kControlOpInProgress).Clear(Flags::kAdvertisingRefreshNeeded);
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100317 DeviceLayer::SystemLayer().CancelTimer(HandleAdvertisingTimer, this);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700318 // Transition to the not Advertising state...
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100319 if (mFlags.Has(Flags::kAdvertising))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700320 {
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100321 mFlags.Clear(Flags::kAdvertising);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700322 ChipLogProgress(DeviceLayer, "CHIPoBLE advertising stopped");
323 }
324 break;
Arkadiusz Bokowy003c4782024-02-23 10:54:21 +0100325 case DeviceEventType::kPlatformLinuxBLEPeripheralAdvReleased:
326 // If the advertising was stopped due to a premature release, check if it needs to be restarted.
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100327 mFlags.Clear(Flags::kAdvertising);
Arkadiusz Bokowy003c4782024-02-23 10:54:21 +0100328 DriveBLEState();
329 break;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700330 case DeviceEventType::kPlatformLinuxBLEPeripheralRegisterAppComplete:
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100331 SuccessOrExit(err = apEvent->Platform.BLEPeripheralRegisterAppComplete.mError);
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100332 mFlags.Clear(Flags::kControlOpInProgress);
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500333 mFlags.Set(Flags::kAppRegistered);
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100334 DriveBLEState();
yunhanw-googlee860aab2020-08-19 09:31:09 -0700335 break;
336 default:
337 break;
338 }
339
340exit:
341 if (err != CHIP_NO_ERROR)
342 {
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200343 DisableBLEService(err);
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500344 mFlags.Clear(Flags::kControlOpInProgress);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700345 }
346}
347
348uint16_t BLEManagerImpl::GetMTU(BLE_CONNECTION_OBJECT conId) const
349{
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200350 uint16_t mtu = 0;
351 VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED,
352 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
353 mtu = conId->GetMTU();
354exit:
355 return mtu;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700356}
357
358bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId)
359{
Damian Królike393b5e2021-01-14 22:29:11 +0100360 bool result = false;
361
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200362 VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED,
363 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
Damian Królike393b5e2021-01-14 22:29:11 +0100364 VerifyOrExit(Ble::UUIDsMatch(svcId, &CHIP_BLE_SVC_ID),
365 ChipLogError(DeviceLayer, "SubscribeCharacteristic() called with invalid service ID"));
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200366 VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_2_UUID),
Damian Królike393b5e2021-01-14 22:29:11 +0100367 ChipLogError(DeviceLayer, "SubscribeCharacteristic() called with invalid characteristic ID"));
368
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200369 VerifyOrExit(conId->SubscribeCharacteristic() == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "SubscribeCharacteristic() failed"));
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100370 result = true;
371
Damian Królike393b5e2021-01-14 22:29:11 +0100372exit:
373 return result;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700374}
375
376bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId)
377{
Damian Królike393b5e2021-01-14 22:29:11 +0100378 bool result = false;
379
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200380 VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED,
381 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
Damian Królike393b5e2021-01-14 22:29:11 +0100382 VerifyOrExit(Ble::UUIDsMatch(svcId, &CHIP_BLE_SVC_ID),
383 ChipLogError(DeviceLayer, "UnsubscribeCharacteristic() called with invalid service ID"));
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200384 VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_2_UUID),
Damian Królike393b5e2021-01-14 22:29:11 +0100385 ChipLogError(DeviceLayer, "UnsubscribeCharacteristic() called with invalid characteristic ID"));
386
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200387 VerifyOrExit(conId->UnsubscribeCharacteristic() == CHIP_NO_ERROR,
388 ChipLogError(DeviceLayer, "UnsubscribeCharacteristic() failed"));
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100389 result = true;
390
Damian Królike393b5e2021-01-14 22:29:11 +0100391exit:
392 return result;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700393}
394
395bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId)
396{
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100397 bool result = false;
398
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200399 VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED,
400 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
Damian Królik3b083582020-09-28 21:35:10 +0200401 ChipLogProgress(DeviceLayer, "Closing BLE GATT connection (con %p)", conId);
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100402
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200403 VerifyOrExit(conId->CloseConnection() == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "CloseConnection() failed"));
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100404 result = true;
405
406exit:
407 return result;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700408}
409
410bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
Kevin Schoedel316a6f52020-12-01 14:49:51 -0500411 chip::System::PacketBufferHandle pBuf)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700412{
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100413 bool result = false;
414
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200415 VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED,
416 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
417 VerifyOrExit(conId->SendIndication(std::move(pBuf)) == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "SendIndication() failed"));
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100418 result = true;
419
420exit:
421 return result;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700422}
423
424bool BLEManagerImpl::SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
Kevin Schoedel316a6f52020-12-01 14:49:51 -0500425 chip::System::PacketBufferHandle pBuf)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700426{
Damian Królike393b5e2021-01-14 22:29:11 +0100427 bool result = false;
428
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200429 VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED,
430 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
Damian Królike393b5e2021-01-14 22:29:11 +0100431 VerifyOrExit(Ble::UUIDsMatch(svcId, &CHIP_BLE_SVC_ID),
432 ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid service ID"));
Jakub Latusek4a8dc732024-06-05 14:35:44 +0200433 VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_1_UUID),
Damian Królike393b5e2021-01-14 22:29:11 +0100434 ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid characteristic ID"));
435
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200436 VerifyOrExit(conId->SendWriteRequest(std::move(pBuf)) == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "SendWriteRequest() failed"));
Arkadiusz Bokowy0e41b192022-11-04 15:09:34 +0100437 result = true;
438
Damian Królike393b5e2021-01-14 22:29:11 +0100439exit:
440 return result;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700441}
442
443bool BLEManagerImpl::SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
Kevin Schoedel316a6f52020-12-01 14:49:51 -0500444 chip::System::PacketBufferHandle pBuf)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700445{
446 ChipLogError(Ble, "SendReadRequest: Not implemented");
447 return true;
448}
449
450bool BLEManagerImpl::SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext,
451 const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId)
452{
453 ChipLogError(Ble, "SendReadRBluezonse: Not implemented");
454 return true;
455}
456
Damian Królike393b5e2021-01-14 22:29:11 +0100457void BLEManagerImpl::HandleNewConnection(BLE_CONNECTION_OBJECT conId)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700458{
Damian Królike393b5e2021-01-14 22:29:11 +0100459 if (sInstance.mIsCentral)
460 {
Matthew Swartwout67226932024-04-26 08:35:42 -0700461 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLECentralConnected,
462 .Platform = { .BLECentralConnected = { .mConnection = conId } } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400463 PlatformMgr().PostEventOrDie(&event);
Damian Królike393b5e2021-01-14 22:29:11 +0100464 }
465}
466
Damian Królik35806bc2021-03-24 16:47:48 +0100467void BLEManagerImpl::HandleConnectFailed(CHIP_ERROR error)
468{
469 if (sInstance.mIsCentral)
470 {
Matthew Swartwout67226932024-04-26 08:35:42 -0700471 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLECentralConnectFailed,
472 .Platform = { .BLECentralConnectFailed = { .mError = error } } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400473 PlatformMgr().PostEventOrDie(&event);
Damian Królik35806bc2021-03-24 16:47:48 +0100474 }
475}
476
Damian Królike393b5e2021-01-14 22:29:11 +0100477void BLEManagerImpl::HandleWriteComplete(BLE_CONNECTION_OBJECT conId)
478{
Matthew Swartwout67226932024-04-26 08:35:42 -0700479 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEWriteComplete,
480 .Platform = { .BLEWriteComplete = { .mConnection = conId } } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400481 PlatformMgr().PostEventOrDie(&event);
Damian Królike393b5e2021-01-14 22:29:11 +0100482}
483
484void BLEManagerImpl::HandleSubscribeOpComplete(BLE_CONNECTION_OBJECT conId, bool subscribed)
485{
Matthew Swartwout67226932024-04-26 08:35:42 -0700486 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLESubscribeOpComplete,
487 .Platform = { .BLESubscribeOpComplete = { .mConnection = conId, .mIsSubscribed = subscribed } } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400488 PlatformMgr().PostEventOrDie(&event);
Damian Królike393b5e2021-01-14 22:29:11 +0100489}
490
491void BLEManagerImpl::HandleTXCharChanged(BLE_CONNECTION_OBJECT conId, const uint8_t * value, size_t len)
492{
Damian Królike393b5e2021-01-14 22:29:11 +0100493 CHIP_ERROR err = CHIP_NO_ERROR;
Kevin Schoedela8681872021-01-28 15:53:13 -0500494 System::PacketBufferHandle buf = System::PacketBufferHandle::NewWithData(value, len);
Damian Królike393b5e2021-01-14 22:29:11 +0100495
Jiacheng Guoa79e3bd2021-04-28 03:11:52 +0800496 ChipLogDetail(DeviceLayer, "Indication received, conn = %p", conId);
Damian Królike393b5e2021-01-14 22:29:11 +0100497
Matthew Swartwout67226932024-04-26 08:35:42 -0700498 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEIndicationReceived,
499 .Platform = { .BLEIndicationReceived = { .mConnection = conId } } };
500
Damian Królike393b5e2021-01-14 22:29:11 +0100501 VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY);
Damian Królike393b5e2021-01-14 22:29:11 +0100502
Matthew Swartwout67226932024-04-26 08:35:42 -0700503 event.Platform.BLEIndicationReceived.mData = std::move(buf).UnsafeRelease();
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400504 PlatformMgr().PostEventOrDie(&event);
Damian Królike393b5e2021-01-14 22:29:11 +0100505
506exit:
507 if (err != CHIP_NO_ERROR)
508 ChipLogError(DeviceLayer, "HandleTXCharChanged() failed: %s", ErrorStr(err));
yunhanw-googlee860aab2020-08-19 09:31:09 -0700509}
510
Damian Królik3b083582020-09-28 21:35:10 +0200511void BLEManagerImpl::HandleRXCharWrite(BLE_CONNECTION_OBJECT conId, const uint8_t * value, size_t len)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700512{
Kevin Schoedelc6098f52020-11-18 12:40:00 -0500513 CHIP_ERROR err = CHIP_NO_ERROR;
514 System::PacketBufferHandle buf;
yunhanw-googlee860aab2020-08-19 09:31:09 -0700515
Kevin Schoedelbd11f982021-01-26 13:34:29 -0500516 // Copy the data to a packet buffer.
Kevin Schoedela8681872021-01-28 15:53:13 -0500517 buf = System::PacketBufferHandle::NewWithData(value, len);
Kevin Schoedelc6098f52020-11-18 12:40:00 -0500518 VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700519
520 // Post an event to the Chip queue to deliver the data into the Chip stack.
521 {
Damian Królik3b083582020-09-28 21:35:10 +0200522 ChipLogProgress(Ble, "Write request received debug %p", conId);
Matthew Swartwout67226932024-04-26 08:35:42 -0700523 ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEWriteReceived,
524 .CHIPoBLEWriteReceived = { .ConId = conId, .Data = std::move(buf).UnsafeRelease() } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400525 PlatformMgr().PostEventOrDie(&event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700526 }
527
528exit:
529 if (err != CHIP_NO_ERROR)
530 {
531 ChipLogError(DeviceLayer, "HandleRXCharWrite() failed: %s", ErrorStr(err));
532 }
yunhanw-googlee860aab2020-08-19 09:31:09 -0700533}
534
Damian Królik3b083582020-09-28 21:35:10 +0200535void BLEManagerImpl::CHIPoBluez_ConnectionClosed(BLE_CONNECTION_OBJECT conId)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700536{
537 ChipLogProgress(DeviceLayer, "Bluez notify CHIPoBluez connection disconnected");
538
539 // If this was a CHIPoBLE connection, post an event to deliver a connection error to the CHIPoBLE layer.
540 {
Matthew Swartwout67226932024-04-26 08:35:42 -0700541 ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEConnectionError,
542 .CHIPoBLEConnectionError = { .ConId = conId, .Reason = BLE_ERROR_REMOTE_DEVICE_DISCONNECTED } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400543 PlatformMgr().PostEventOrDie(&event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700544 }
545}
546
Damian Królik3b083582020-09-28 21:35:10 +0200547void BLEManagerImpl::HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT conId)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700548{
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200549 VerifyOrReturn(conId != BLE_CONNECTION_UNINITIALIZED,
550 ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__));
yunhanw-googlee860aab2020-08-19 09:31:09 -0700551
552 // Post an event to the Chip queue to process either a CHIPoBLE Subscribe or Unsubscribe based on
553 // whether the client is enabling or disabling indications.
Matthew Swartwout67226932024-04-26 08:35:42 -0700554 ChipDeviceEvent event{ .Type = conId->IsNotifyAcquired() ? static_cast<uint16_t>(DeviceEventType::kCHIPoBLESubscribe)
555 : static_cast<uint16_t>(DeviceEventType::kCHIPoBLEUnsubscribe),
556 .CHIPoBLESubscribe = { .ConId = conId } };
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200557 PlatformMgr().PostEventOrDie(&event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700558
Arkadiusz Bokowy9f372582023-10-19 21:12:59 +0200559 ChipLogProgress(DeviceLayer, "CHIPoBLE %s received",
560 (event.Type == DeviceEventType::kCHIPoBLESubscribe) ? "subscribe" : "unsubscribe");
yunhanw-googlee860aab2020-08-19 09:31:09 -0700561}
562
Damian Królik3b083582020-09-28 21:35:10 +0200563void BLEManagerImpl::HandleTXComplete(BLE_CONNECTION_OBJECT conId)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700564{
565 // Post an event to the Chip queue to process the indicate confirmation.
Matthew Swartwout67226932024-04-26 08:35:42 -0700566 ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEIndicateConfirm, .CHIPoBLEIndicateConfirm = { .ConId = conId } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400567 PlatformMgr().PostEventOrDie(&event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700568}
569
570void BLEManagerImpl::DriveBLEState()
571{
572 CHIP_ERROR err = CHIP_NO_ERROR;
573
574 // Perform any initialization actions that must occur after the Chip task is running.
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500575 if (!mFlags.Has(Flags::kAsyncInitCompleted))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700576 {
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500577 mFlags.Set(Flags::kAsyncInitCompleted);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700578 ExitNow();
579 }
580
581 // If there's already a control operation in progress, wait until it completes.
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500582 VerifyOrExit(!mFlags.Has(Flags::kControlOpInProgress), /* */);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700583
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100584 // Initializes the BlueZ BLE layer if needed.
585 if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700586 {
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100587 if (!mFlags.Has(Flags::kBluezManagerInitialized))
588 {
589 SuccessOrExit(err = mBluezObjectManager.Init());
590 mFlags.Set(Flags::kBluezManagerInitialized);
591 }
592 if (!mFlags.Has(Flags::kBluezAdapterAvailable))
593 {
594 mAdapter.reset(mBluezObjectManager.GetAdapter(mAdapterId));
595 VerifyOrExit(mAdapter, err = BLE_ERROR_ADAPTER_UNAVAILABLE);
596 mFlags.Set(Flags::kBluezAdapterAvailable);
597 }
598 if (!mFlags.Has(Flags::kBluezBLELayerInitialized))
599 {
600 SuccessOrExit(err = mEndpoint.Init(mAdapter.get(), mIsCentral));
601 mFlags.Set(Flags::kBluezBLELayerInitialized);
602 }
yunhanw-googlee860aab2020-08-19 09:31:09 -0700603 }
604
605 // Register the CHIPoBLE application with the Bluez BLE layer if needed.
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500606 if (!mIsCentral && mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kAppRegistered))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700607 {
Arkadiusz Bokowyc331f102023-11-14 17:18:01 +0100608 SuccessOrExit(err = mEndpoint.RegisterGattApplication());
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500609 mFlags.Set(Flags::kControlOpInProgress);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700610 ExitNow();
611 }
612
613 // If the application has enabled CHIPoBLE and BLE advertising...
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500614 if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kAdvertisingEnabled))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700615 {
616 // Start/re-start advertising if not already advertising, or if the advertising state of the
617 // Bluez BLE layer needs to be refreshed.
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500618 if (!mFlags.Has(Flags::kAdvertising) || mFlags.Has(Flags::kAdvertisingRefreshNeeded))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700619 {
Kamil Kasperczyk7254f302021-03-19 19:45:23 +0100620 mFlags.Clear(Flags::kAdvertisingRefreshNeeded);
621
Arkadiusz Bokowybc5ef902023-11-10 17:20:48 +0100622 // Configure advertising data if it hasn't been done yet.
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500623 if (!mFlags.Has(Flags::kAdvertisingConfigured))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700624 {
Arkadiusz Bokowy3ae2ee22024-03-25 22:14:41 +0100625 SuccessOrExit(err = mBLEAdvertisement.Init(mAdapter.get(), mpBLEAdvUUID, mDeviceName));
Arkadiusz Bokowybc5ef902023-11-10 17:20:48 +0100626 mFlags.Set(Flags::kAdvertisingConfigured);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700627 }
628
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100629 // Setup service data for advertising.
630 auto serviceDataFlags = BluezAdvertisement::kServiceDataNone;
631#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
632 if (mFlags.Has(Flags::kExtAdvertisingEnabled))
633 serviceDataFlags |= BluezAdvertisement::kServiceDataExtendedAnnouncement;
634#endif
635 SuccessOrExit(err = mBLEAdvertisement.SetupServiceData(serviceDataFlags));
636
637 // Set or update the advertising intervals.
638 SuccessOrExit(err = mBLEAdvertisement.SetIntervals(GetAdvertisingIntervals()));
639
640 if (!mFlags.Has(Flags::kAdvertising))
641 {
642 // Start advertising. This is an asynchronous step. BLE manager will be notified of
643 // advertising start completion via a call to NotifyBLEPeripheralAdvStartComplete.
644 SuccessOrExit(err = mBLEAdvertisement.Start());
645 mFlags.Set(Flags::kControlOpInProgress);
646 ExitNow();
647 }
yunhanw-googlee860aab2020-08-19 09:31:09 -0700648 }
649 }
650
651 // Otherwise stop advertising if needed...
652 else
653 {
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500654 if (mFlags.Has(Flags::kAdvertising))
yunhanw-googlee860aab2020-08-19 09:31:09 -0700655 {
Arkadiusz Bokowyc331f102023-11-14 17:18:01 +0100656 SuccessOrExit(err = mBLEAdvertisement.Stop());
Kevin Schoedel8ea423a2021-03-11 11:49:18 -0500657 mFlags.Set(Flags::kControlOpInProgress);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700658
659 ExitNow();
660 }
661 }
662
663exit:
664 if (err != CHIP_NO_ERROR)
665 {
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200666 DisableBLEService(err);
667 }
668}
669
670void BLEManagerImpl::DisableBLEService(CHIP_ERROR err)
671{
672 ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format());
673 mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled;
674 // Stop all timers if the error is other than BLE adapter unavailable. In case of BLE adapter
675 // beeing unavailable, we will keep timers running, as the adapter might become available in
676 // the nearest future (e.g. BlueZ restart due to crash). By doing that we will ensure that BLE
677 // adapter reappearance will not extend timeouts for the ongoing operations.
678 if (err != BLE_ERROR_ADAPTER_UNAVAILABLE)
679 {
680 DeviceLayer::SystemLayer().CancelTimer(HandleScanTimer, this);
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100681 DeviceLayer::SystemLayer().CancelTimer(HandleAdvertisingTimer, this);
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200682 DeviceLayer::SystemLayer().CancelTimer(HandleConnectTimer, this);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700683 }
684}
685
yunhanw-googlee860aab2020-08-19 09:31:09 -0700686void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId)
687{
Andrei Litvind7a91fe2021-02-08 16:34:43 -0500688 ChipLogProgress(Ble, "Got notification regarding chip connection closure");
simonhmorris190f36a42023-11-22 09:29:14 +0000689#if CHIP_DEVICE_CONFIG_ENABLE_WPA && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
simonhmorris12696bc92024-02-14 22:27:08 +0000690 if (mState == kState_NotInitialized)
691 {
692 // Close BLE GATT connections to disconnect BlueZ
693 CloseConnection(conId);
694 // In Non-Concurrent mode start the Wi-Fi, as BLE has been stopped
695 DeviceLayer::ConnectivityMgrImpl().StartNonConcurrentWiFiManagement();
696 }
697#endif // CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
698}
699
700void BLEManagerImpl::CheckNonConcurrentBleClosing()
701{
702#if CHIP_DEVICE_CONFIG_ENABLE_WPA && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION
703 if (mState == kState_Disconnecting)
704 {
705 DeviceLayer::DeviceControlServer::DeviceControlSvr().PostCloseAllBLEConnectionsToOperationalNetworkEvent();
706 }
simonhmorris190f36a42023-11-22 09:29:14 +0000707#endif
yunhanw-googlee860aab2020-08-19 09:31:09 -0700708}
709
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100710BluezAdvertisement::AdvertisingIntervals BLEManagerImpl::GetAdvertisingIntervals() const
711{
712 if (mFlags.Has(Flags::kFastAdvertisingEnabled))
713 return { CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN, CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX };
714#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
715 if (mFlags.Has(Flags::kExtAdvertisingEnabled))
716 return { CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN, CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX };
717#endif
718 return { CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN, CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX };
719}
720
721void BLEManagerImpl::HandleAdvertisingTimer(chip::System::Layer *, void * appState)
722{
723 auto * self = static_cast<BLEManagerImpl *>(appState);
724
725 if (self->mFlags.Has(Flags::kFastAdvertisingEnabled))
726 {
727 ChipLogDetail(DeviceLayer, "bleAdv Timeout : Start slow advertisement");
728 self->_SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
729#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
730 self->mFlags.Clear(Flags::kExtAdvertisingEnabled);
731 DeviceLayer::SystemLayer().StartTimer(kSlowAdvertiseTimeout, HandleAdvertisingTimer, self);
732 }
733 else
734 {
735 ChipLogDetail(DeviceLayer, "bleAdv Timeout : Start extended advertisement");
736 self->mFlags.Set(Flags::kExtAdvertisingEnabled);
737 // This will trigger advertising intervals update in the DriveBLEState() function.
738 self->_SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
739#endif
740 }
741}
742
Andrei Litvin5ca73082021-02-25 11:53:45 -0500743void BLEManagerImpl::InitiateScan(BleScanState scanType)
744{
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100745 CHIP_ERROR err = CHIP_ERROR_INCORRECT_STATE;
746
Andrei Litvin5ca73082021-02-25 11:53:45 -0500747 DriveBLEState();
748
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100749 VerifyOrExit(scanType != BleScanState::kNotScanning,
750 ChipLogError(Ble, "Invalid scan type requested: %d", to_underlying(scanType)));
751 VerifyOrExit(!mDeviceScanner.IsScanning(), ChipLogError(Ble, "BLE scan already in progress"));
752 VerifyOrExit(mFlags.Has(Flags::kBluezAdapterAvailable), err = BLE_ERROR_ADAPTER_UNAVAILABLE);
Andrei Litvin5ca73082021-02-25 11:53:45 -0500753
Damian Królik35806bc2021-03-24 16:47:48 +0100754 mBLEScanConfig.mBleScanState = scanType;
Andrei Litvin5ca73082021-02-25 11:53:45 -0500755
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100756 err = mDeviceScanner.Init(mAdapter.get(), this);
757 VerifyOrExit(err == CHIP_NO_ERROR, {
Damian Królik35806bc2021-03-24 16:47:48 +0100758 mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100759 ChipLogError(Ble, "Failed to create BLE device scanner: %" CHIP_ERROR_FORMAT, err.Format());
760 });
Andrei Litvin5ca73082021-02-25 11:53:45 -0500761
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100762 err = mDeviceScanner.StartScan();
763 VerifyOrExit(err == CHIP_NO_ERROR, {
764 mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
765 ChipLogError(Ble, "Failed to start BLE scan: %" CHIP_ERROR_FORMAT, err.Format());
766 });
767
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200768 err = DeviceLayer::SystemLayer().StartTimer(kNewConnectionScanTimeout, HandleScanTimer, this);
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100769 VerifyOrExit(err == CHIP_NO_ERROR, {
770 mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
771 mDeviceScanner.StopScan();
772 ChipLogError(Ble, "Failed to start BLE scan timeout: %" CHIP_ERROR_FORMAT, err.Format());
773 });
774
775exit:
Andrei Litvin5ca73082021-02-25 11:53:45 -0500776 if (err != CHIP_NO_ERROR)
777 {
Song Guo87142e02021-04-21 09:00:00 +0800778 BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, err);
Andrei Litvin5ca73082021-02-25 11:53:45 -0500779 }
780}
781
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200782void BLEManagerImpl::HandleScanTimer(chip::System::Layer *, void * appState)
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100783{
784 auto * manager = static_cast<BLEManagerImpl *>(appState);
785 manager->OnScanError(CHIP_ERROR_TIMEOUT);
786 manager->mDeviceScanner.StopScan();
787}
788
Damian Królik35806bc2021-03-24 16:47:48 +0100789void BLEManagerImpl::CleanScanConfig()
790{
791 if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting)
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200792 DeviceLayer::SystemLayer().CancelTimer(HandleConnectTimer, this);
Damian Królik35806bc2021-03-24 16:47:48 +0100793
794 mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
795}
796
Boris Zbarsky79e8c9e2022-07-26 13:22:44 -0400797void BLEManagerImpl::NewConnection(BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator)
Damian Królik880c5f32020-10-05 06:12:47 +0200798{
799 mBLEScanConfig.mDiscriminator = connDiscriminator;
800 mBLEScanConfig.mAppState = appState;
Andrei Litvin5ca73082021-02-25 11:53:45 -0500801
802 // Scan initiation performed async, to ensure that the BLE subsystem is initialized.
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100803 DeviceLayer::SystemLayer().ScheduleLambda([this] { InitiateScan(BleScanState::kScanForDiscriminator); });
Damian Królik880c5f32020-10-05 06:12:47 +0200804}
805
Kevin Schoedel9c012582021-06-30 16:20:44 -0400806CHIP_ERROR BLEManagerImpl::CancelConnection()
Vivien Nicolasd914e712021-03-19 15:46:00 +0100807{
tianfeng-yang08af5fa2023-07-10 15:19:10 +0800808 if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting)
Arkadiusz Bokowyc331f102023-11-14 17:18:01 +0100809 mEndpoint.CancelConnect();
tianfeng-yang08af5fa2023-07-10 15:19:10 +0800810 // If in discovery mode, stop scan.
811 else if (mBLEScanConfig.mBleScanState != BleScanState::kNotScanning)
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100812 {
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200813 DeviceLayer::SystemLayer().CancelTimer(HandleScanTimer, this);
Arkadiusz Bokowyfab81b32023-11-03 14:10:21 +0100814 mDeviceScanner.StopScan();
Arkadiusz Bokowy22adb082024-03-29 17:57:31 +0100815 }
tianfeng-yang08af5fa2023-07-10 15:19:10 +0800816 return CHIP_NO_ERROR;
Vivien Nicolasd914e712021-03-19 15:46:00 +0100817}
818
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +0100819void BLEManagerImpl::NotifyBLEAdapterAdded(unsigned int aAdapterId, const char * aAdapterAddress)
820{
Matthew Swartwout67226932024-04-26 08:35:42 -0700821 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEAdapterAdded,
822 .Platform = { .BLEAdapter = { .mAdapterId = aAdapterId } } };
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +0100823 Platform::CopyString(event.Platform.BLEAdapter.mAdapterAddress, aAdapterAddress);
824 PlatformMgr().PostEventOrDie(&event);
825}
826
827void BLEManagerImpl::NotifyBLEAdapterRemoved(unsigned int aAdapterId, const char * aAdapterAddress)
828{
Matthew Swartwout67226932024-04-26 08:35:42 -0700829 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEAdapterRemoved,
830 .Platform = { .BLEAdapter = { .mAdapterId = aAdapterId } } };
Arkadiusz Bokowy6f7995b2024-03-27 18:17:31 +0100831 Platform::CopyString(event.Platform.BLEAdapter.mAdapterAddress, aAdapterAddress);
832 PlatformMgr().PostEventOrDie(&event);
833}
834
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100835void BLEManagerImpl::NotifyBLEPeripheralRegisterAppComplete(CHIP_ERROR error)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700836{
Matthew Swartwout67226932024-04-26 08:35:42 -0700837 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralRegisterAppComplete,
838 .Platform = { .BLEPeripheralRegisterAppComplete = { .mError = error } } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400839 PlatformMgr().PostEventOrDie(&event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700840}
841
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100842void BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(CHIP_ERROR error)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700843{
Matthew Swartwout67226932024-04-26 08:35:42 -0700844 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete,
845 .Platform = { .BLEPeripheralAdvStartComplete = { .mError = error } } };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400846 PlatformMgr().PostEventOrDie(&event);
yunhanw-googlee860aab2020-08-19 09:31:09 -0700847}
848
Arkadiusz Bokowyf86633b2024-03-19 07:47:59 +0100849void BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(CHIP_ERROR error)
yunhanw-googlee860aab2020-08-19 09:31:09 -0700850{
Matthew Swartwout67226932024-04-26 08:35:42 -0700851 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete,
852 .Platform = { .BLEPeripheralAdvStopComplete = { .mError = error } } };
Arkadiusz Bokowy003c4782024-02-23 10:54:21 +0100853 PlatformMgr().PostEventOrDie(&event);
854}
855
856void BLEManagerImpl::NotifyBLEPeripheralAdvReleased()
857{
Matthew Swartwout67226932024-04-26 08:35:42 -0700858 ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvReleased };
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400859 PlatformMgr().PostEventOrDie(&event);
Martin Turon0c03fa82020-05-26 15:43:11 -0700860}
861
Arkadiusz Bokowy2a7ffdc2023-08-10 20:54:39 +0200862void BLEManagerImpl::OnDeviceScanned(BluezDevice1 & device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info)
Andrei Litvin5ca73082021-02-25 11:53:45 -0500863{
Arkadiusz Bokowy48e3d652024-02-13 22:04:13 +0100864 const char * address = bluez_device1_get_address(&device);
865 ChipLogProgress(Ble, "New device scanned: %s", address);
Andrei Litvin5ca73082021-02-25 11:53:45 -0500866
Damian Królik35806bc2021-03-24 16:47:48 +0100867 if (mBLEScanConfig.mBleScanState == BleScanState::kScanForDiscriminator)
Andrei Litvin5ca73082021-02-25 11:53:45 -0500868 {
Arkadiusz Bokowy48e3d652024-02-13 22:04:13 +0100869 auto isMatch = mBLEScanConfig.mDiscriminator.MatchesLongDiscriminator(info.GetDeviceDiscriminator());
870 VerifyOrReturn(
871 isMatch,
872 ChipLogError(Ble, "Skip connection: Device discriminator does not match: %u != %u", info.GetDeviceDiscriminator(),
873 mBLEScanConfig.mDiscriminator.IsShortDiscriminator() ? mBLEScanConfig.mDiscriminator.GetShortValue()
874 : mBLEScanConfig.mDiscriminator.GetLongValue()));
Andrei Litvin5ca73082021-02-25 11:53:45 -0500875 ChipLogProgress(Ble, "Device discriminator match. Attempting to connect.");
876 }
Damian Królik35806bc2021-03-24 16:47:48 +0100877 else if (mBLEScanConfig.mBleScanState == BleScanState::kScanForAddress)
Andrei Litvin5ca73082021-02-25 11:53:45 -0500878 {
Arkadiusz Bokowy48e3d652024-02-13 22:04:13 +0100879 auto isMatch = strcmp(address, mBLEScanConfig.mAddress.c_str()) == 0;
880 VerifyOrReturn(isMatch,
881 ChipLogError(Ble, "Skip connection: Device address does not match: %s != %s", address,
882 mBLEScanConfig.mAddress.c_str()));
Andrei Litvin5ca73082021-02-25 11:53:45 -0500883 ChipLogProgress(Ble, "Device address match. Attempting to connect.");
884 }
885 else
886 {
Jakubd6a783e2023-02-28 18:07:45 +0100887 // Internal consistency error
Andrei Litvin5ca73082021-02-25 11:53:45 -0500888 ChipLogError(Ble, "Unknown discovery type. Ignoring scanned device.");
889 return;
890 }
891
Damian Królik35806bc2021-03-24 16:47:48 +0100892 mBLEScanConfig.mBleScanState = BleScanState::kConnecting;
Junior Martinezde0dfcf2023-04-23 19:50:31 -0400893
894 chip::DeviceLayer::PlatformMgr().LockChipStack();
tianfeng-yang049fef02024-01-19 17:10:04 +0800895 // We StartScan in the ChipStack thread.
896 // StopScan should also be performed in the ChipStack thread.
897 // At the same time, the scan timer also needs to be canceled in the ChipStack thread.
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200898 DeviceLayer::SystemLayer().CancelTimer(HandleScanTimer, this);
tianfeng-yang049fef02024-01-19 17:10:04 +0800899 mDeviceScanner.StopScan();
900 // Stop scanning and then start connecting timer
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200901 DeviceLayer::SystemLayer().StartTimer(kConnectTimeout, HandleConnectTimer, this);
Junior Martinezde0dfcf2023-04-23 19:50:31 -0400902 chip::DeviceLayer::PlatformMgr().UnlockChipStack();
903
Arkadiusz Bokowy48e3d652024-02-13 22:04:13 +0100904 CHIP_ERROR err = mEndpoint.ConnectDevice(device);
905 VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Ble, "Device connection failed: %" CHIP_ERROR_FORMAT, err.Format()));
906
907 ChipLogProgress(Ble, "New device connected: %s", address);
Andrei Litvin5ca73082021-02-25 11:53:45 -0500908}
909
Arkadiusz Bokowye47f7482024-04-23 13:43:11 +0200910void BLEManagerImpl::HandleConnectTimer(chip::System::Layer *, void * appState)
911{
912 auto * manager = static_cast<BLEManagerImpl *>(appState);
913 manager->mEndpoint.CancelConnect();
914 BLEManagerImpl::HandleConnectFailed(CHIP_ERROR_TIMEOUT);
915}
916
Andrei Litvin5ca73082021-02-25 11:53:45 -0500917void BLEManagerImpl::OnScanComplete()
918{
Jakubd6a783e2023-02-28 18:07:45 +0100919 switch (mBLEScanConfig.mBleScanState)
Andrei Litvin5ca73082021-02-25 11:53:45 -0500920 {
Jakubd6a783e2023-02-28 18:07:45 +0100921 case BleScanState::kNotScanning:
Damian Królik35806bc2021-03-24 16:47:48 +0100922 ChipLogProgress(Ble, "Scan complete notification without an active scan.");
Jakubd6a783e2023-02-28 18:07:45 +0100923 break;
924 case BleScanState::kScanForAddress:
925 case BleScanState::kScanForDiscriminator:
926 mBLEScanConfig.mBleScanState = BleScanState::kNotScanning;
927 ChipLogProgress(Ble, "Scan complete. No matching device found.");
928 break;
929 case BleScanState::kConnecting:
930 break;
Andrei Litvin5ca73082021-02-25 11:53:45 -0500931 }
Jakubd6a783e2023-02-28 18:07:45 +0100932}
Andrei Litvin5ca73082021-02-25 11:53:45 -0500933
Jakubd6a783e2023-02-28 18:07:45 +0100934void BLEManagerImpl::OnScanError(CHIP_ERROR err)
935{
tianfeng-yangc5bc3c12023-05-11 01:48:31 +0800936 BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, err);
Jakubd6a783e2023-02-28 18:07:45 +0100937 ChipLogError(Ble, "BLE scan error: %" CHIP_ERROR_FORMAT, err.Format());
Andrei Litvin5ca73082021-02-25 11:53:45 -0500938}
939
Martin Turon0c03fa82020-05-26 15:43:11 -0700940} // namespace Internal
941} // namespace DeviceLayer
942} // namespace chip