Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Kevin Schoedel | a868187 | 2021-01-28 15:53:13 -0500 | [diff] [blame] | 3 | * Copyright (c) 2020-2021 Project CHIP Authors |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 4 | * 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 Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 24 | |
Arkadiusz Bokowy | 2a7ffdc | 2023-08-10 20:54:39 +0200 | [diff] [blame] | 25 | /** |
| 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 Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 34 | |
Boris Zbarsky | 444b50b | 2020-09-23 19:53:06 -0400 | [diff] [blame] | 35 | #include <type_traits> |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 36 | #include <utility> |
Boris Zbarsky | 444b50b | 2020-09-23 19:53:06 -0400 | [diff] [blame] | 37 | |
Arkadiusz Bokowy | 9966940 | 2024-04-18 11:31:44 +0200 | [diff] [blame] | 38 | #include <ble/Ble.h> |
Arkadiusz Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 39 | #include <lib/support/CHIPMemString.h> |
Arkadiusz Bokowy | 2a7ffdc | 2023-08-10 20:54:39 +0200 | [diff] [blame] | 40 | #include <lib/support/CodeUtils.h> |
| 41 | #include <lib/support/SafeInt.h> |
| 42 | #include <platform/CHIPDeviceLayer.h> |
| 43 | #include <platform/CommissionableDataProvider.h> |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 44 | |
Arkadiusz Bokowy | ac5b518 | 2023-10-16 19:06:29 +0200 | [diff] [blame] | 45 | #include "bluez/BluezEndpoint.h" |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 46 | |
simonhmorris1 | 2696bc9 | 2024-02-14 22:27:08 +0000 | [diff] [blame] | 47 | #if !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION |
| 48 | #include <platform/DeviceControlServer.h> |
| 49 | #endif |
| 50 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 51 | using namespace ::nl; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 52 | using namespace ::chip::Ble; |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 53 | |
| 54 | namespace chip { |
| 55 | namespace DeviceLayer { |
| 56 | namespace Internal { |
| 57 | |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 58 | namespace { |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 59 | |
rus084 | 48ee5e0 | 2023-12-06 16:41:54 +0300 | [diff] [blame] | 60 | static constexpr System::Clock::Timeout kNewConnectionScanTimeout = System::Clock::Seconds16(20); |
| 61 | static constexpr System::Clock::Timeout kConnectTimeout = System::Clock::Seconds16(20); |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 62 | static 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. |
| 68 | static 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); |
| 70 | static_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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 74 | |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 75 | } // namespace |
| 76 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 77 | BLEManagerImpl BLEManagerImpl::sInstance; |
| 78 | |
| 79 | CHIP_ERROR BLEManagerImpl::_Init() |
| 80 | { |
| 81 | CHIP_ERROR err; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 82 | |
Kevin Schoedel | 86eb040 | 2021-09-10 08:37:38 -0400 | [diff] [blame] | 83 | err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer()); |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 84 | SuccessOrExit(err); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 85 | |
| 86 | mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled; |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 87 | mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART && !mIsCentral); |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 88 | mFlags.Set(Flags::kFastAdvertisingEnabled, true); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 89 | |
| 90 | memset(mDeviceName, 0, sizeof(mDeviceName)); |
| 91 | |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 92 | DeviceLayer::SystemLayer().ScheduleLambda([this] { DriveBLEState(); }); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 93 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 94 | exit: |
| 95 | return err; |
| 96 | } |
| 97 | |
Michael Spang | 6387049 | 2022-06-28 08:41:08 -0400 | [diff] [blame] | 98 | void BLEManagerImpl::_Shutdown() |
Andrei Litvin | 7b71fb2 | 2021-11-05 02:16:47 -0400 | [diff] [blame] | 99 | { |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 100 | // 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 Bokowy | fab81b3 | 2023-11-03 14:10:21 +0100 | [diff] [blame] | 105 | mDeviceScanner.Shutdown(); |
Arkadiusz Bokowy | bc5ef90 | 2023-11-10 17:20:48 +0100 | [diff] [blame] | 106 | mBLEAdvertisement.Shutdown(); |
Arkadiusz Bokowy | c331f10 | 2023-11-14 17:18:01 +0100 | [diff] [blame] | 107 | mEndpoint.Shutdown(); |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 108 | |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 109 | mBluezObjectManager.Shutdown(); |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 110 | mFlags.Clear(Flags::kBluezManagerInitialized); |
Andrei Litvin | 7b71fb2 | 2021-11-05 02:16:47 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 113 | CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val) |
| 114 | { |
| 115 | CHIP_ERROR err = CHIP_NO_ERROR; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 116 | |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 117 | if (mFlags.Has(Flags::kAdvertisingEnabled) != val) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 118 | { |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 119 | mFlags.Set(Flags::kAdvertisingEnabled, val); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 122 | DeviceLayer::SystemLayer().ScheduleLambda([this] { DriveBLEState(); }); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 123 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 124 | return err; |
| 125 | } |
| 126 | |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 127 | CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode) |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 128 | { |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 129 | switch (mode) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 130 | { |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 131 | 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-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 139 | } |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 140 | mFlags.Set(Flags::kAdvertisingRefreshNeeded); |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 141 | DeviceLayer::SystemLayer().ScheduleLambda([this] { DriveBLEState(); }); |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 142 | return CHIP_NO_ERROR; |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | CHIP_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-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 152 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 153 | return CHIP_NO_ERROR; |
| 154 | } |
| 155 | |
| 156 | CHIP_ERROR BLEManagerImpl::_SetDeviceName(const char * deviceName) |
| 157 | { |
Song Guo | 308b2a4 | 2020-10-28 14:25:49 +0800 | [diff] [blame] | 158 | CHIP_ERROR err = CHIP_NO_ERROR; |
| 159 | |
| 160 | VerifyOrExit(mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported, err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); |
| 161 | |
Michael Spang | c76e82d | 2020-09-23 14:32:23 -0400 | [diff] [blame] | 162 | if (deviceName != nullptr && deviceName[0] != 0) |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 163 | { |
Song Guo | 308b2a4 | 2020-10-28 14:25:49 +0800 | [diff] [blame] | 164 | VerifyOrExit(strlen(deviceName) < kMaxDeviceNameLength, err = CHIP_ERROR_INVALID_ARGUMENT); |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 165 | strcpy(mDeviceName, deviceName); |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 166 | mFlags.Set(Flags::kUseCustomDeviceName); |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 167 | } |
| 168 | else |
| 169 | { |
Song Guo | 308b2a4 | 2020-10-28 14:25:49 +0800 | [diff] [blame] | 170 | uint16_t discriminator; |
Tennessee Carmel-Veilleux | 8f86c43 | 2022-03-11 15:49:56 -0500 | [diff] [blame] | 171 | SuccessOrExit(err = GetCommissionableDataProvider()->GetSetupDiscriminator(discriminator)); |
Song Guo | 308b2a4 | 2020-10-28 14:25:49 +0800 | [diff] [blame] | 172 | snprintf(mDeviceName, sizeof(mDeviceName), "%s%04u", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, discriminator); |
| 173 | mDeviceName[kMaxDeviceNameLength] = 0; |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 174 | mFlags.Clear(Flags::kUseCustomDeviceName); |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 175 | } |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 176 | |
Song Guo | 308b2a4 | 2020-10-28 14:25:49 +0800 | [diff] [blame] | 177 | exit: |
| 178 | return err; |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Michael Spang | b0e38c2 | 2020-09-30 14:20:50 -0400 | [diff] [blame] | 181 | uint16_t BLEManagerImpl::_NumConnections() |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 182 | { |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 183 | uint16_t numCons = 0; |
| 184 | return numCons; |
| 185 | } |
| 186 | |
Song Guo | 7e07597 | 2021-03-05 22:53:05 +0800 | [diff] [blame] | 187 | CHIP_ERROR BLEManagerImpl::ConfigureBle(uint32_t aAdapterId, bool aIsCentral) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 188 | { |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 189 | mAdapterId = aAdapterId; |
| 190 | mIsCentral = aIsCentral; |
| 191 | mpBLEAdvUUID = "0xFFF6"; |
Arkadiusz Bokowy | bc5ef90 | 2023-11-10 17:20:48 +0100 | [diff] [blame] | 192 | return CHIP_NO_ERROR; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) |
| 196 | { |
| 197 | switch (event->Type) |
| 198 | { |
| 199 | case DeviceEventType::kCHIPoBLESubscribe: |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 200 | HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 201 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 202 | ChipDeviceEvent connectionEvent{ .Type = DeviceEventType::kCHIPoBLEConnectionEstablished }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 203 | PlatformMgr().PostEventOrDie(&connectionEvent); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 204 | } |
| 205 | break; |
| 206 | |
| 207 | case DeviceEventType::kCHIPoBLEUnsubscribe: |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 208 | HandleUnsubscribeReceived(event->CHIPoBLEUnsubscribe.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 209 | break; |
| 210 | |
Kevin Schoedel | 59cb4f6 | 2020-12-09 13:55:52 -0500 | [diff] [blame] | 211 | case DeviceEventType::kCHIPoBLEWriteReceived: |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 212 | HandleWriteReceived(event->CHIPoBLEWriteReceived.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_1_UUID, |
Kevin Schoedel | bc4f841 | 2021-01-08 14:46:39 -0500 | [diff] [blame] | 213 | PacketBufferHandle::Adopt(event->CHIPoBLEWriteReceived.Data)); |
Kevin Schoedel | 59cb4f6 | 2020-12-09 13:55:52 -0500 | [diff] [blame] | 214 | break; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 215 | |
| 216 | case DeviceEventType::kCHIPoBLEIndicateConfirm: |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 217 | HandleIndicationConfirmation(event->CHIPoBLEIndicateConfirm.ConId, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 218 | break; |
| 219 | |
| 220 | case DeviceEventType::kCHIPoBLEConnectionError: |
| 221 | HandleConnectionError(event->CHIPoBLEConnectionError.ConId, event->CHIPoBLEConnectionError.Reason); |
| 222 | break; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 223 | case DeviceEventType::kServiceProvisioningChange: |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 224 | // Force the advertising configuration to be refreshed to reflect new provisioning state. |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 225 | mFlags.Clear(Flags::kAdvertisingConfigured); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 226 | |
| 227 | DriveBLEState(); |
yunhanw-google | 2413242 | 2020-09-08 12:33:16 -0700 | [diff] [blame] | 228 | break; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 229 | default: |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 230 | HandlePlatformSpecificBLEEvent(event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 231 | break; |
| 232 | } |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEvent) |
| 236 | { |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 237 | CHIP_ERROR err = CHIP_NO_ERROR; |
Jiacheng Guo | a79e3bd | 2021-04-28 03:11:52 +0800 | [diff] [blame] | 238 | ChipLogDetail(DeviceLayer, "HandlePlatformSpecificBLEEvent %d", apEvent->Type); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 239 | switch (apEvent->Type) |
| 240 | { |
Arkadiusz Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 241 | 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 Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 246 | mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled; |
| 247 | DriveBLEState(); |
Arkadiusz Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 248 | } |
| 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 Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 255 | // 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 Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 270 | } |
| 271 | break; |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 272 | case DeviceEventType::kPlatformLinuxBLECentralConnected: |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 273 | if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting) |
| 274 | { |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 275 | BleConnectionDelegate::OnConnectionComplete(mBLEScanConfig.mAppState, |
| 276 | apEvent->Platform.BLECentralConnected.mConnection); |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 277 | CleanScanConfig(); |
| 278 | } |
| 279 | break; |
| 280 | case DeviceEventType::kPlatformLinuxBLECentralConnectFailed: |
| 281 | if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting) |
| 282 | { |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 283 | BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, apEvent->Platform.BLECentralConnectFailed.mError); |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 284 | CleanScanConfig(); |
| 285 | } |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 286 | break; |
| 287 | case DeviceEventType::kPlatformLinuxBLEWriteComplete: |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 288 | HandleWriteConfirmation(apEvent->Platform.BLEWriteComplete.mConnection, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_1_UUID); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 289 | break; |
| 290 | case DeviceEventType::kPlatformLinuxBLESubscribeOpComplete: |
| 291 | if (apEvent->Platform.BLESubscribeOpComplete.mIsSubscribed) |
| 292 | HandleSubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &CHIP_BLE_SVC_ID, |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 293 | &Ble::CHIP_BLE_CHAR_2_UUID); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 294 | else |
| 295 | HandleUnsubscribeComplete(apEvent->Platform.BLESubscribeOpComplete.mConnection, &CHIP_BLE_SVC_ID, |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 296 | &Ble::CHIP_BLE_CHAR_2_UUID); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 297 | break; |
| 298 | case DeviceEventType::kPlatformLinuxBLEIndicationReceived: |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 299 | HandleIndicationReceived(apEvent->Platform.BLEIndicationReceived.mConnection, &CHIP_BLE_SVC_ID, &Ble::CHIP_BLE_CHAR_2_UUID, |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 300 | PacketBufferHandle::Adopt(apEvent->Platform.BLEIndicationReceived.mData)); |
| 301 | break; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 302 | case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete: |
Arkadiusz Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 303 | SuccessOrExit(err = apEvent->Platform.BLEPeripheralAdvStartComplete.mError); |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 304 | mFlags.Clear(Flags::kControlOpInProgress).Clear(Flags::kAdvertisingRefreshNeeded); |
Arkadiusz Bokowy | 003c478 | 2024-02-23 10:54:21 +0100 | [diff] [blame] | 305 | // 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 Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 312 | mFlags.Set(Flags::kAdvertising); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 313 | break; |
| 314 | case DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete: |
Arkadiusz Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 315 | SuccessOrExit(err = apEvent->Platform.BLEPeripheralAdvStopComplete.mError); |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 316 | mFlags.Clear(Flags::kControlOpInProgress).Clear(Flags::kAdvertisingRefreshNeeded); |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 317 | DeviceLayer::SystemLayer().CancelTimer(HandleAdvertisingTimer, this); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 318 | // Transition to the not Advertising state... |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 319 | if (mFlags.Has(Flags::kAdvertising)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 320 | { |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 321 | mFlags.Clear(Flags::kAdvertising); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 322 | ChipLogProgress(DeviceLayer, "CHIPoBLE advertising stopped"); |
| 323 | } |
| 324 | break; |
Arkadiusz Bokowy | 003c478 | 2024-02-23 10:54:21 +0100 | [diff] [blame] | 325 | case DeviceEventType::kPlatformLinuxBLEPeripheralAdvReleased: |
| 326 | // If the advertising was stopped due to a premature release, check if it needs to be restarted. |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 327 | mFlags.Clear(Flags::kAdvertising); |
Arkadiusz Bokowy | 003c478 | 2024-02-23 10:54:21 +0100 | [diff] [blame] | 328 | DriveBLEState(); |
| 329 | break; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 330 | case DeviceEventType::kPlatformLinuxBLEPeripheralRegisterAppComplete: |
Arkadiusz Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 331 | SuccessOrExit(err = apEvent->Platform.BLEPeripheralRegisterAppComplete.mError); |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 332 | mFlags.Clear(Flags::kControlOpInProgress); |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 333 | mFlags.Set(Flags::kAppRegistered); |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 334 | DriveBLEState(); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | exit: |
| 341 | if (err != CHIP_NO_ERROR) |
| 342 | { |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 343 | DisableBLEService(err); |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 344 | mFlags.Clear(Flags::kControlOpInProgress); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | uint16_t BLEManagerImpl::GetMTU(BLE_CONNECTION_OBJECT conId) const |
| 349 | { |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 350 | 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(); |
| 354 | exit: |
| 355 | return mtu; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId) |
| 359 | { |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 360 | bool result = false; |
| 361 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 362 | VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED, |
| 363 | ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__)); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 364 | VerifyOrExit(Ble::UUIDsMatch(svcId, &CHIP_BLE_SVC_ID), |
| 365 | ChipLogError(DeviceLayer, "SubscribeCharacteristic() called with invalid service ID")); |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 366 | VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_2_UUID), |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 367 | ChipLogError(DeviceLayer, "SubscribeCharacteristic() called with invalid characteristic ID")); |
| 368 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 369 | VerifyOrExit(conId->SubscribeCharacteristic() == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "SubscribeCharacteristic() failed")); |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 370 | result = true; |
| 371 | |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 372 | exit: |
| 373 | return result; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId) |
| 377 | { |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 378 | bool result = false; |
| 379 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 380 | VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED, |
| 381 | ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__)); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 382 | VerifyOrExit(Ble::UUIDsMatch(svcId, &CHIP_BLE_SVC_ID), |
| 383 | ChipLogError(DeviceLayer, "UnsubscribeCharacteristic() called with invalid service ID")); |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 384 | VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_2_UUID), |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 385 | ChipLogError(DeviceLayer, "UnsubscribeCharacteristic() called with invalid characteristic ID")); |
| 386 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 387 | VerifyOrExit(conId->UnsubscribeCharacteristic() == CHIP_NO_ERROR, |
| 388 | ChipLogError(DeviceLayer, "UnsubscribeCharacteristic() failed")); |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 389 | result = true; |
| 390 | |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 391 | exit: |
| 392 | return result; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId) |
| 396 | { |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 397 | bool result = false; |
| 398 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 399 | VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED, |
| 400 | ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__)); |
Damian Królik | 3b08358 | 2020-09-28 21:35:10 +0200 | [diff] [blame] | 401 | ChipLogProgress(DeviceLayer, "Closing BLE GATT connection (con %p)", conId); |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 402 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 403 | VerifyOrExit(conId->CloseConnection() == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "CloseConnection() failed")); |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 404 | result = true; |
| 405 | |
| 406 | exit: |
| 407 | return result; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, |
Kevin Schoedel | 316a6f5 | 2020-12-01 14:49:51 -0500 | [diff] [blame] | 411 | chip::System::PacketBufferHandle pBuf) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 412 | { |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 413 | bool result = false; |
| 414 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 415 | 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 Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 418 | result = true; |
| 419 | |
| 420 | exit: |
| 421 | return result; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | bool BLEManagerImpl::SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, |
Kevin Schoedel | 316a6f5 | 2020-12-01 14:49:51 -0500 | [diff] [blame] | 425 | chip::System::PacketBufferHandle pBuf) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 426 | { |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 427 | bool result = false; |
| 428 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 429 | VerifyOrExit(conId != BLE_CONNECTION_UNINITIALIZED, |
| 430 | ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__)); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 431 | VerifyOrExit(Ble::UUIDsMatch(svcId, &CHIP_BLE_SVC_ID), |
| 432 | ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid service ID")); |
Jakub Latusek | 4a8dc73 | 2024-06-05 14:35:44 +0200 | [diff] [blame^] | 433 | VerifyOrExit(Ble::UUIDsMatch(charId, &Ble::CHIP_BLE_CHAR_1_UUID), |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 434 | ChipLogError(DeviceLayer, "SendWriteRequest() called with invalid characteristic ID")); |
| 435 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 436 | VerifyOrExit(conId->SendWriteRequest(std::move(pBuf)) == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "SendWriteRequest() failed")); |
Arkadiusz Bokowy | 0e41b19 | 2022-11-04 15:09:34 +0100 | [diff] [blame] | 437 | result = true; |
| 438 | |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 439 | exit: |
| 440 | return result; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | bool BLEManagerImpl::SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId, |
Kevin Schoedel | 316a6f5 | 2020-12-01 14:49:51 -0500 | [diff] [blame] | 444 | chip::System::PacketBufferHandle pBuf) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 445 | { |
| 446 | ChipLogError(Ble, "SendReadRequest: Not implemented"); |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | bool 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ólik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 457 | void BLEManagerImpl::HandleNewConnection(BLE_CONNECTION_OBJECT conId) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 458 | { |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 459 | if (sInstance.mIsCentral) |
| 460 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 461 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLECentralConnected, |
| 462 | .Platform = { .BLECentralConnected = { .mConnection = conId } } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 463 | PlatformMgr().PostEventOrDie(&event); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 467 | void BLEManagerImpl::HandleConnectFailed(CHIP_ERROR error) |
| 468 | { |
| 469 | if (sInstance.mIsCentral) |
| 470 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 471 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLECentralConnectFailed, |
| 472 | .Platform = { .BLECentralConnectFailed = { .mError = error } } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 473 | PlatformMgr().PostEventOrDie(&event); |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 477 | void BLEManagerImpl::HandleWriteComplete(BLE_CONNECTION_OBJECT conId) |
| 478 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 479 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEWriteComplete, |
| 480 | .Platform = { .BLEWriteComplete = { .mConnection = conId } } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 481 | PlatformMgr().PostEventOrDie(&event); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | void BLEManagerImpl::HandleSubscribeOpComplete(BLE_CONNECTION_OBJECT conId, bool subscribed) |
| 485 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 486 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLESubscribeOpComplete, |
| 487 | .Platform = { .BLESubscribeOpComplete = { .mConnection = conId, .mIsSubscribed = subscribed } } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 488 | PlatformMgr().PostEventOrDie(&event); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void BLEManagerImpl::HandleTXCharChanged(BLE_CONNECTION_OBJECT conId, const uint8_t * value, size_t len) |
| 492 | { |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 493 | CHIP_ERROR err = CHIP_NO_ERROR; |
Kevin Schoedel | a868187 | 2021-01-28 15:53:13 -0500 | [diff] [blame] | 494 | System::PacketBufferHandle buf = System::PacketBufferHandle::NewWithData(value, len); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 495 | |
Jiacheng Guo | a79e3bd | 2021-04-28 03:11:52 +0800 | [diff] [blame] | 496 | ChipLogDetail(DeviceLayer, "Indication received, conn = %p", conId); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 497 | |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 498 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEIndicationReceived, |
| 499 | .Platform = { .BLEIndicationReceived = { .mConnection = conId } } }; |
| 500 | |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 501 | VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 502 | |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 503 | event.Platform.BLEIndicationReceived.mData = std::move(buf).UnsafeRelease(); |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 504 | PlatformMgr().PostEventOrDie(&event); |
Damian Królik | e393b5e | 2021-01-14 22:29:11 +0100 | [diff] [blame] | 505 | |
| 506 | exit: |
| 507 | if (err != CHIP_NO_ERROR) |
| 508 | ChipLogError(DeviceLayer, "HandleTXCharChanged() failed: %s", ErrorStr(err)); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Damian Królik | 3b08358 | 2020-09-28 21:35:10 +0200 | [diff] [blame] | 511 | void BLEManagerImpl::HandleRXCharWrite(BLE_CONNECTION_OBJECT conId, const uint8_t * value, size_t len) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 512 | { |
Kevin Schoedel | c6098f5 | 2020-11-18 12:40:00 -0500 | [diff] [blame] | 513 | CHIP_ERROR err = CHIP_NO_ERROR; |
| 514 | System::PacketBufferHandle buf; |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 515 | |
Kevin Schoedel | bd11f98 | 2021-01-26 13:34:29 -0500 | [diff] [blame] | 516 | // Copy the data to a packet buffer. |
Kevin Schoedel | a868187 | 2021-01-28 15:53:13 -0500 | [diff] [blame] | 517 | buf = System::PacketBufferHandle::NewWithData(value, len); |
Kevin Schoedel | c6098f5 | 2020-11-18 12:40:00 -0500 | [diff] [blame] | 518 | VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 519 | |
| 520 | // Post an event to the Chip queue to deliver the data into the Chip stack. |
| 521 | { |
Damian Królik | 3b08358 | 2020-09-28 21:35:10 +0200 | [diff] [blame] | 522 | ChipLogProgress(Ble, "Write request received debug %p", conId); |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 523 | ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEWriteReceived, |
| 524 | .CHIPoBLEWriteReceived = { .ConId = conId, .Data = std::move(buf).UnsafeRelease() } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 525 | PlatformMgr().PostEventOrDie(&event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | exit: |
| 529 | if (err != CHIP_NO_ERROR) |
| 530 | { |
| 531 | ChipLogError(DeviceLayer, "HandleRXCharWrite() failed: %s", ErrorStr(err)); |
| 532 | } |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Damian Królik | 3b08358 | 2020-09-28 21:35:10 +0200 | [diff] [blame] | 535 | void BLEManagerImpl::CHIPoBluez_ConnectionClosed(BLE_CONNECTION_OBJECT conId) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 536 | { |
| 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 Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 541 | ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEConnectionError, |
| 542 | .CHIPoBLEConnectionError = { .ConId = conId, .Reason = BLE_ERROR_REMOTE_DEVICE_DISCONNECTED } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 543 | PlatformMgr().PostEventOrDie(&event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Damian Królik | 3b08358 | 2020-09-28 21:35:10 +0200 | [diff] [blame] | 547 | void BLEManagerImpl::HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT conId) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 548 | { |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 549 | VerifyOrReturn(conId != BLE_CONNECTION_UNINITIALIZED, |
| 550 | ChipLogError(DeviceLayer, "BLE connection is not initialized in %s", __func__)); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 551 | |
| 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 Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 554 | ChipDeviceEvent event{ .Type = conId->IsNotifyAcquired() ? static_cast<uint16_t>(DeviceEventType::kCHIPoBLESubscribe) |
| 555 | : static_cast<uint16_t>(DeviceEventType::kCHIPoBLEUnsubscribe), |
| 556 | .CHIPoBLESubscribe = { .ConId = conId } }; |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 557 | PlatformMgr().PostEventOrDie(&event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 558 | |
Arkadiusz Bokowy | 9f37258 | 2023-10-19 21:12:59 +0200 | [diff] [blame] | 559 | ChipLogProgress(DeviceLayer, "CHIPoBLE %s received", |
| 560 | (event.Type == DeviceEventType::kCHIPoBLESubscribe) ? "subscribe" : "unsubscribe"); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Damian Królik | 3b08358 | 2020-09-28 21:35:10 +0200 | [diff] [blame] | 563 | void BLEManagerImpl::HandleTXComplete(BLE_CONNECTION_OBJECT conId) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 564 | { |
| 565 | // Post an event to the Chip queue to process the indicate confirmation. |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 566 | ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEIndicateConfirm, .CHIPoBLEIndicateConfirm = { .ConId = conId } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 567 | PlatformMgr().PostEventOrDie(&event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void 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 Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 575 | if (!mFlags.Has(Flags::kAsyncInitCompleted)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 576 | { |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 577 | mFlags.Set(Flags::kAsyncInitCompleted); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 578 | ExitNow(); |
| 579 | } |
| 580 | |
| 581 | // If there's already a control operation in progress, wait until it completes. |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 582 | VerifyOrExit(!mFlags.Has(Flags::kControlOpInProgress), /* */); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 583 | |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 584 | // Initializes the BlueZ BLE layer if needed. |
| 585 | if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 586 | { |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 587 | 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-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // Register the CHIPoBLE application with the Bluez BLE layer if needed. |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 606 | if (!mIsCentral && mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kAppRegistered)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 607 | { |
Arkadiusz Bokowy | c331f10 | 2023-11-14 17:18:01 +0100 | [diff] [blame] | 608 | SuccessOrExit(err = mEndpoint.RegisterGattApplication()); |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 609 | mFlags.Set(Flags::kControlOpInProgress); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 610 | ExitNow(); |
| 611 | } |
| 612 | |
| 613 | // If the application has enabled CHIPoBLE and BLE advertising... |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 614 | if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kAdvertisingEnabled)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 615 | { |
| 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 Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 618 | if (!mFlags.Has(Flags::kAdvertising) || mFlags.Has(Flags::kAdvertisingRefreshNeeded)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 619 | { |
Kamil Kasperczyk | 7254f30 | 2021-03-19 19:45:23 +0100 | [diff] [blame] | 620 | mFlags.Clear(Flags::kAdvertisingRefreshNeeded); |
| 621 | |
Arkadiusz Bokowy | bc5ef90 | 2023-11-10 17:20:48 +0100 | [diff] [blame] | 622 | // Configure advertising data if it hasn't been done yet. |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 623 | if (!mFlags.Has(Flags::kAdvertisingConfigured)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 624 | { |
Arkadiusz Bokowy | 3ae2ee2 | 2024-03-25 22:14:41 +0100 | [diff] [blame] | 625 | SuccessOrExit(err = mBLEAdvertisement.Init(mAdapter.get(), mpBLEAdvUUID, mDeviceName)); |
Arkadiusz Bokowy | bc5ef90 | 2023-11-10 17:20:48 +0100 | [diff] [blame] | 626 | mFlags.Set(Flags::kAdvertisingConfigured); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 629 | // 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-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
| 651 | // Otherwise stop advertising if needed... |
| 652 | else |
| 653 | { |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 654 | if (mFlags.Has(Flags::kAdvertising)) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 655 | { |
Arkadiusz Bokowy | c331f10 | 2023-11-14 17:18:01 +0100 | [diff] [blame] | 656 | SuccessOrExit(err = mBLEAdvertisement.Stop()); |
Kevin Schoedel | 8ea423a | 2021-03-11 11:49:18 -0500 | [diff] [blame] | 657 | mFlags.Set(Flags::kControlOpInProgress); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 658 | |
| 659 | ExitNow(); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | exit: |
| 664 | if (err != CHIP_NO_ERROR) |
| 665 | { |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 666 | DisableBLEService(err); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | void 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 Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 681 | DeviceLayer::SystemLayer().CancelTimer(HandleAdvertisingTimer, this); |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 682 | DeviceLayer::SystemLayer().CancelTimer(HandleConnectTimer, this); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 686 | void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) |
| 687 | { |
Andrei Litvin | d7a91fe | 2021-02-08 16:34:43 -0500 | [diff] [blame] | 688 | ChipLogProgress(Ble, "Got notification regarding chip connection closure"); |
simonhmorris1 | 90f36a4 | 2023-11-22 09:29:14 +0000 | [diff] [blame] | 689 | #if CHIP_DEVICE_CONFIG_ENABLE_WPA && !CHIP_DEVICE_CONFIG_SUPPORTS_CONCURRENT_CONNECTION |
simonhmorris1 | 2696bc9 | 2024-02-14 22:27:08 +0000 | [diff] [blame] | 690 | 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 | |
| 700 | void 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 | } |
simonhmorris1 | 90f36a4 | 2023-11-22 09:29:14 +0000 | [diff] [blame] | 707 | #endif |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 710 | BluezAdvertisement::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 | |
| 721 | void 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 743 | void BLEManagerImpl::InitiateScan(BleScanState scanType) |
| 744 | { |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 745 | CHIP_ERROR err = CHIP_ERROR_INCORRECT_STATE; |
| 746 | |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 747 | DriveBLEState(); |
| 748 | |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 749 | 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 753 | |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 754 | mBLEScanConfig.mBleScanState = scanType; |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 755 | |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 756 | err = mDeviceScanner.Init(mAdapter.get(), this); |
| 757 | VerifyOrExit(err == CHIP_NO_ERROR, { |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 758 | mBLEScanConfig.mBleScanState = BleScanState::kNotScanning; |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 759 | ChipLogError(Ble, "Failed to create BLE device scanner: %" CHIP_ERROR_FORMAT, err.Format()); |
| 760 | }); |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 761 | |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 762 | 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 Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 768 | err = DeviceLayer::SystemLayer().StartTimer(kNewConnectionScanTimeout, HandleScanTimer, this); |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 769 | 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 | |
| 775 | exit: |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 776 | if (err != CHIP_NO_ERROR) |
| 777 | { |
Song Guo | 87142e0 | 2021-04-21 09:00:00 +0800 | [diff] [blame] | 778 | BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, err); |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 782 | void BLEManagerImpl::HandleScanTimer(chip::System::Layer *, void * appState) |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 783 | { |
| 784 | auto * manager = static_cast<BLEManagerImpl *>(appState); |
| 785 | manager->OnScanError(CHIP_ERROR_TIMEOUT); |
| 786 | manager->mDeviceScanner.StopScan(); |
| 787 | } |
| 788 | |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 789 | void BLEManagerImpl::CleanScanConfig() |
| 790 | { |
| 791 | if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting) |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 792 | DeviceLayer::SystemLayer().CancelTimer(HandleConnectTimer, this); |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 793 | |
| 794 | mBLEScanConfig.mBleScanState = BleScanState::kNotScanning; |
| 795 | } |
| 796 | |
Boris Zbarsky | 79e8c9e | 2022-07-26 13:22:44 -0400 | [diff] [blame] | 797 | void BLEManagerImpl::NewConnection(BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator) |
Damian Królik | 880c5f3 | 2020-10-05 06:12:47 +0200 | [diff] [blame] | 798 | { |
| 799 | mBLEScanConfig.mDiscriminator = connDiscriminator; |
| 800 | mBLEScanConfig.mAppState = appState; |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 801 | |
| 802 | // Scan initiation performed async, to ensure that the BLE subsystem is initialized. |
Arkadiusz Bokowy | bd6c4ab | 2024-02-06 08:57:29 +0100 | [diff] [blame] | 803 | DeviceLayer::SystemLayer().ScheduleLambda([this] { InitiateScan(BleScanState::kScanForDiscriminator); }); |
Damian Królik | 880c5f3 | 2020-10-05 06:12:47 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Kevin Schoedel | 9c01258 | 2021-06-30 16:20:44 -0400 | [diff] [blame] | 806 | CHIP_ERROR BLEManagerImpl::CancelConnection() |
Vivien Nicolas | d914e71 | 2021-03-19 15:46:00 +0100 | [diff] [blame] | 807 | { |
tianfeng-yang | 08af5fa | 2023-07-10 15:19:10 +0800 | [diff] [blame] | 808 | if (mBLEScanConfig.mBleScanState == BleScanState::kConnecting) |
Arkadiusz Bokowy | c331f10 | 2023-11-14 17:18:01 +0100 | [diff] [blame] | 809 | mEndpoint.CancelConnect(); |
tianfeng-yang | 08af5fa | 2023-07-10 15:19:10 +0800 | [diff] [blame] | 810 | // If in discovery mode, stop scan. |
| 811 | else if (mBLEScanConfig.mBleScanState != BleScanState::kNotScanning) |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 812 | { |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 813 | DeviceLayer::SystemLayer().CancelTimer(HandleScanTimer, this); |
Arkadiusz Bokowy | fab81b3 | 2023-11-03 14:10:21 +0100 | [diff] [blame] | 814 | mDeviceScanner.StopScan(); |
Arkadiusz Bokowy | 22adb08 | 2024-03-29 17:57:31 +0100 | [diff] [blame] | 815 | } |
tianfeng-yang | 08af5fa | 2023-07-10 15:19:10 +0800 | [diff] [blame] | 816 | return CHIP_NO_ERROR; |
Vivien Nicolas | d914e71 | 2021-03-19 15:46:00 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Arkadiusz Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 819 | void BLEManagerImpl::NotifyBLEAdapterAdded(unsigned int aAdapterId, const char * aAdapterAddress) |
| 820 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 821 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEAdapterAdded, |
| 822 | .Platform = { .BLEAdapter = { .mAdapterId = aAdapterId } } }; |
Arkadiusz Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 823 | Platform::CopyString(event.Platform.BLEAdapter.mAdapterAddress, aAdapterAddress); |
| 824 | PlatformMgr().PostEventOrDie(&event); |
| 825 | } |
| 826 | |
| 827 | void BLEManagerImpl::NotifyBLEAdapterRemoved(unsigned int aAdapterId, const char * aAdapterAddress) |
| 828 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 829 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEAdapterRemoved, |
| 830 | .Platform = { .BLEAdapter = { .mAdapterId = aAdapterId } } }; |
Arkadiusz Bokowy | 6f7995b | 2024-03-27 18:17:31 +0100 | [diff] [blame] | 831 | Platform::CopyString(event.Platform.BLEAdapter.mAdapterAddress, aAdapterAddress); |
| 832 | PlatformMgr().PostEventOrDie(&event); |
| 833 | } |
| 834 | |
Arkadiusz Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 835 | void BLEManagerImpl::NotifyBLEPeripheralRegisterAppComplete(CHIP_ERROR error) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 836 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 837 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralRegisterAppComplete, |
| 838 | .Platform = { .BLEPeripheralRegisterAppComplete = { .mError = error } } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 839 | PlatformMgr().PostEventOrDie(&event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Arkadiusz Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 842 | void BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(CHIP_ERROR error) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 843 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 844 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete, |
| 845 | .Platform = { .BLEPeripheralAdvStartComplete = { .mError = error } } }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 846 | PlatformMgr().PostEventOrDie(&event); |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Arkadiusz Bokowy | f86633b | 2024-03-19 07:47:59 +0100 | [diff] [blame] | 849 | void BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(CHIP_ERROR error) |
yunhanw-google | e860aab | 2020-08-19 09:31:09 -0700 | [diff] [blame] | 850 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 851 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete, |
| 852 | .Platform = { .BLEPeripheralAdvStopComplete = { .mError = error } } }; |
Arkadiusz Bokowy | 003c478 | 2024-02-23 10:54:21 +0100 | [diff] [blame] | 853 | PlatformMgr().PostEventOrDie(&event); |
| 854 | } |
| 855 | |
| 856 | void BLEManagerImpl::NotifyBLEPeripheralAdvReleased() |
| 857 | { |
Matthew Swartwout | 6722693 | 2024-04-26 08:35:42 -0700 | [diff] [blame] | 858 | ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvReleased }; |
Kevin Schoedel | 6fd3811 | 2021-09-14 13:22:09 -0400 | [diff] [blame] | 859 | PlatformMgr().PostEventOrDie(&event); |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Arkadiusz Bokowy | 2a7ffdc | 2023-08-10 20:54:39 +0200 | [diff] [blame] | 862 | void BLEManagerImpl::OnDeviceScanned(BluezDevice1 & device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 863 | { |
Arkadiusz Bokowy | 48e3d65 | 2024-02-13 22:04:13 +0100 | [diff] [blame] | 864 | const char * address = bluez_device1_get_address(&device); |
| 865 | ChipLogProgress(Ble, "New device scanned: %s", address); |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 866 | |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 867 | if (mBLEScanConfig.mBleScanState == BleScanState::kScanForDiscriminator) |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 868 | { |
Arkadiusz Bokowy | 48e3d65 | 2024-02-13 22:04:13 +0100 | [diff] [blame] | 869 | 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 875 | ChipLogProgress(Ble, "Device discriminator match. Attempting to connect."); |
| 876 | } |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 877 | else if (mBLEScanConfig.mBleScanState == BleScanState::kScanForAddress) |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 878 | { |
Arkadiusz Bokowy | 48e3d65 | 2024-02-13 22:04:13 +0100 | [diff] [blame] | 879 | 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 883 | ChipLogProgress(Ble, "Device address match. Attempting to connect."); |
| 884 | } |
| 885 | else |
| 886 | { |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 887 | // Internal consistency error |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 888 | ChipLogError(Ble, "Unknown discovery type. Ignoring scanned device."); |
| 889 | return; |
| 890 | } |
| 891 | |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 892 | mBLEScanConfig.mBleScanState = BleScanState::kConnecting; |
Junior Martinez | de0dfcf | 2023-04-23 19:50:31 -0400 | [diff] [blame] | 893 | |
| 894 | chip::DeviceLayer::PlatformMgr().LockChipStack(); |
tianfeng-yang | 049fef0 | 2024-01-19 17:10:04 +0800 | [diff] [blame] | 895 | // 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 Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 898 | DeviceLayer::SystemLayer().CancelTimer(HandleScanTimer, this); |
tianfeng-yang | 049fef0 | 2024-01-19 17:10:04 +0800 | [diff] [blame] | 899 | mDeviceScanner.StopScan(); |
| 900 | // Stop scanning and then start connecting timer |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 901 | DeviceLayer::SystemLayer().StartTimer(kConnectTimeout, HandleConnectTimer, this); |
Junior Martinez | de0dfcf | 2023-04-23 19:50:31 -0400 | [diff] [blame] | 902 | chip::DeviceLayer::PlatformMgr().UnlockChipStack(); |
| 903 | |
Arkadiusz Bokowy | 48e3d65 | 2024-02-13 22:04:13 +0100 | [diff] [blame] | 904 | 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 908 | } |
| 909 | |
Arkadiusz Bokowy | e47f748 | 2024-04-23 13:43:11 +0200 | [diff] [blame] | 910 | void 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 917 | void BLEManagerImpl::OnScanComplete() |
| 918 | { |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 919 | switch (mBLEScanConfig.mBleScanState) |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 920 | { |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 921 | case BleScanState::kNotScanning: |
Damian Królik | 35806bc | 2021-03-24 16:47:48 +0100 | [diff] [blame] | 922 | ChipLogProgress(Ble, "Scan complete notification without an active scan."); |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 923 | 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 Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 931 | } |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 932 | } |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 933 | |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 934 | void BLEManagerImpl::OnScanError(CHIP_ERROR err) |
| 935 | { |
tianfeng-yang | c5bc3c1 | 2023-05-11 01:48:31 +0800 | [diff] [blame] | 936 | BleConnectionDelegate::OnConnectionError(mBLEScanConfig.mAppState, err); |
Jakub | d6a783e | 2023-02-28 18:07:45 +0100 | [diff] [blame] | 937 | ChipLogError(Ble, "BLE scan error: %" CHIP_ERROR_FORMAT, err.Format()); |
Andrei Litvin | 5ca7308 | 2021-02-25 11:53:45 -0500 | [diff] [blame] | 938 | } |
| 939 | |
Martin Turon | 0c03fa8 | 2020-05-26 15:43:11 -0700 | [diff] [blame] | 940 | } // namespace Internal |
| 941 | } // namespace DeviceLayer |
| 942 | } // namespace chip |