3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Provides an implementation of the BLEManager singleton object
21 * for the Linux platforms.
26 #include <ble/BleLayer.h>
27 #include <platform/internal/BLEManager.h>
29 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
31 #include "bluez/ChipDeviceScanner.h"
32 #include "bluez/Types.h"
35 namespace DeviceLayer {
38 void HandleIncomingBleConnection(Ble::BLEEndPoint * bleEP);
49 uint8_t mPairingStatus;
52 const char * mpAdvertisingUUID;
55 enum class BleScanState
58 kScanForDiscriminator,
64 // If a active scan for connection is being performed
65 BleScanState bleScanState = BleScanState::kNotScanning;
67 // If scanning by discriminator, what are we scanning for
68 uint16_t mDiscriminator = 0;
70 // If scanning by address, what address are we searching for
73 // Optional argument to be passed to callback functions provided by the BLE scan/connect requestor
74 void * mAppState = nullptr;
78 * Concrete implementation of the BLEManagerImpl singleton object for the Linux platforms.
80 class BLEManagerImpl final : public BLEManager,
81 private Ble::BleLayer,
82 private Ble::BlePlatformDelegate,
83 private Ble::BleApplicationDelegate,
84 private Ble::BleConnectionDelegate,
85 private ChipDeviceScannerDelegate
87 // Allow the BLEManager interface class to delegate method calls to
88 // the implementation methods provided by this class.
92 CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral);
95 static void HandleNewConnection(BLE_CONNECTION_OBJECT conId);
96 static void HandleWriteComplete(BLE_CONNECTION_OBJECT conId);
97 static void HandleSubscribeOpComplete(BLE_CONNECTION_OBJECT conId, bool subscribed);
98 static void HandleTXCharChanged(BLE_CONNECTION_OBJECT conId, const uint8_t * value, size_t len);
99 static void HandleRXCharWrite(BLE_CONNECTION_OBJECT user_data, const uint8_t * value, size_t len);
100 static void CHIPoBluez_ConnectionClosed(BLE_CONNECTION_OBJECT user_data);
101 static void HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT user_data);
102 static void HandleTXComplete(BLE_CONNECTION_OBJECT user_data);
103 static bool WoBLEz_TimerCb(BLE_CONNECTION_OBJECT user_data);
105 static void NotifyBLEPeripheralRegisterAppComplete(bool aIsSuccess, void * apAppstate);
106 static void NotifyBLEPeripheralAdvConfiguredComplete(bool aIsSuccess, void * apAppstate);
107 static void NotifyBLEPeripheralAdvStartComplete(bool aIsSuccess, void * apAppstate);
108 static void NotifyBLEPeripheralAdvStopComplete(bool aIsSuccess, void * apAppstate);
111 // ===== Members that implement the BLEManager internal interface.
114 CHIPoBLEServiceMode _GetCHIPoBLEServiceMode();
115 CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
116 bool _IsAdvertisingEnabled();
117 CHIP_ERROR _SetAdvertisingEnabled(bool val);
118 bool _IsFastAdvertisingEnabled();
119 CHIP_ERROR _SetFastAdvertisingEnabled(bool val);
120 bool _IsAdvertising();
121 CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
122 CHIP_ERROR _SetDeviceName(const char * deviceName);
123 uint16_t _NumConnections();
125 void _OnPlatformEvent(const ChipDeviceEvent * event);
126 void HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * event);
127 BleLayer * _GetBleLayer();
129 // ===== Members that implement virtual methods on BlePlatformDelegate.
131 bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
132 const Ble::ChipBleUUID * charId) override;
133 bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
134 const Ble::ChipBleUUID * charId) override;
135 bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
136 uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
137 bool SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
138 System::PacketBufferHandle pBuf) override;
139 bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
140 System::PacketBufferHandle pBuf) override;
141 bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
142 System::PacketBufferHandle pBuf) override;
143 bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const Ble::ChipBleUUID * svcId,
144 const Ble::ChipBleUUID * charId) override;
146 // ===== Members that implement virtual methods on BleApplicationDelegate.
148 void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
150 // ===== Members that implement virtual methods on BleConnectionDelegate.
152 void NewConnection(BleLayer * bleLayer, void * appState, uint16_t connDiscriminator) override;
154 // ===== Members that implement virtual methods on ChipDeviceScannerDelegate
155 void OnDeviceScanned(BluezDevice1 * device, const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
156 void OnScanComplete() override;
158 // ===== Members for internal use by the following friends.
160 friend BLEManager & BLEMgr();
161 friend BLEManagerImpl & BLEMgrImpl();
163 static BLEManagerImpl sInstance;
165 // ===== Private members reserved for use by this class only.
168 kFlag_AsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
169 kFlag_BluezBLELayerInitialized = 0x0002, /**< The Bluez layer has been initialized. */
170 kFlag_AppRegistered = 0x0004, /**< The CHIPoBLE application has been registered with the Bluez layer. */
171 kFlag_AdvertisingConfigured = 0x0008, /**< CHIPoBLE advertising has been configured in the Bluez layer. */
172 kFlag_Advertising = 0x0010, /**< The system is currently CHIPoBLE advertising. */
173 kFlag_ControlOpInProgress = 0x0020, /**< An async control operation has been issued to the ESP BLE layer. */
174 kFlag_AdvertisingEnabled = 0x0040, /**< The application has enabled CHIPoBLE advertising. */
175 kFlag_FastAdvertisingEnabled = 0x0080, /**< The application has enabled fast advertising. */
176 kFlag_UseCustomDeviceName = 0x0100, /**< The application has configured a custom BLE device name. */
177 kFlag_AdvertisingRefreshNeeded = 0x0200, /**< The advertising configuration/state in BLE layer needs to be updated. */
182 kMaxConnections = 1, // TODO: right max connection
183 kMaxDeviceNameLength = 20, // TODO: right-size this
184 kMaxAdvertismentDataSetSize = 31 // TODO: verify this
187 CHIP_ERROR StartBLEAdvertising();
188 CHIP_ERROR StopBLEAdvertising();
190 void DriveBLEState();
191 static void DriveBLEState(intptr_t arg);
193 void InitiateScan(BleScanState scanType);
194 static void InitiateScan(intptr_t arg);
196 CHIPoBLEServiceMode mServiceMode;
197 BLEAdvConfig mBLEAdvConfig;
198 BLEScanConfig mBLEScanConfig;
200 char mDeviceName[kMaxDeviceNameLength + 1];
201 bool mIsCentral = false;
202 BluezEndpoint * mpEndpoint = nullptr;
203 std::unique_ptr<ChipDeviceScanner> mDeviceScanner;
207 * Returns a reference to the public interface of the BLEManager singleton object.
209 * Internal components should use this to access features of the BLEManager object
210 * that are common to all platforms.
212 inline BLEManager & BLEMgr()
214 return BLEManagerImpl::sInstance;
218 * Returns the platform-specific implementation of the BLEManager singleton object.
220 * Internal components can use this to gain access to features of the BLEManager
221 * that are specific to the Linux platforms.
223 inline BLEManagerImpl & BLEMgrImpl()
225 return BLEManagerImpl::sInstance;
228 inline Ble::BleLayer * BLEManagerImpl::_GetBleLayer()
233 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode()
238 inline bool BLEManagerImpl::_IsAdvertisingEnabled()
240 return GetFlag(mFlags, kFlag_AdvertisingEnabled);
243 inline bool BLEManagerImpl::_IsFastAdvertisingEnabled()
245 return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
248 inline bool BLEManagerImpl::_IsAdvertising()
250 return GetFlag(mFlags, kFlag_Advertising);
253 } // namespace Internal
254 } // namespace DeviceLayer
257 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE