3 * Copyright (c) 2020-2021 Project CHIP Authors
4 * Copyright (c) 2019 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Provides an implementation of the BLEManager singleton object
22 * for the Silicon Labs EFR32 platforms.
26 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
31 #include "sl_bt_api.h"
35 namespace DeviceLayer {
38 using namespace chip::Ble;
41 * Concrete implementation of the BLEManager singleton object for the EFR32 platforms.
43 class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate
45 // Allow the BLEManager interface class to delegate method calls to
46 // the implementation methods provided by this class.
49 // ===== Members that implement the BLEManager internal interface.
51 CHIP_ERROR _Init(void);
52 CHIPoBLEServiceMode _GetCHIPoBLEServiceMode(void);
53 CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
54 bool _IsAdvertisingEnabled(void);
55 CHIP_ERROR _SetAdvertisingEnabled(bool val);
56 bool _IsAdvertising(void);
57 CHIP_ERROR _SetAdvertisingMode(BLEAdvertisingMode mode);
58 CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
59 CHIP_ERROR _SetDeviceName(const char * deviceName);
60 uint16_t _NumConnections(void);
61 void _OnPlatformEvent(const ChipDeviceEvent * event);
62 BleLayer * _GetBleLayer(void);
64 // ===== Members that implement virtual methods on BlePlatformDelegate.
66 bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
67 const Ble::ChipBleUUID * charId) override;
68 bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
69 const Ble::ChipBleUUID * charId) override;
70 bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
71 uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
72 bool SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
73 System::PacketBufferHandle pBuf) override;
74 bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
75 System::PacketBufferHandle pBuf) override;
76 bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
77 System::PacketBufferHandle pBuf) override;
78 bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const Ble::ChipBleUUID * svcId,
79 const Ble::ChipBleUUID * charId) override;
81 // ===== Members that implement virtual methods on BleApplicationDelegate.
83 void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
85 // ===== Members for internal use by the following friends.
87 friend BLEManager & BLEMgr(void);
88 friend BLEManagerImpl & BLEMgrImpl(void);
90 static BLEManagerImpl sInstance;
92 // ===== Private members reserved for use by this class only.
94 enum class Flags : uint16_t
96 kAdvertisingEnabled = 0x0001,
97 kFastAdvertisingEnabled = 0x0002,
98 kAdvertising = 0x0004,
99 kRestartAdvertising = 0x0008,
100 kEFRBLEStackInitialized = 0x0010,
101 kDeviceNameSet = 0x0020,
106 kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS,
107 kMaxDeviceNameLength = 16,
111 struct CHIPoBLEConState
115 uint16_t allocated : 1;
116 uint16_t subscribed : 1;
118 uint8_t connectionHandle;
119 uint8_t bondingHandle;
122 CHIPoBLEConState mBleConnections[kMaxConnections];
123 uint8_t mIndConfId[kMaxConnections];
124 CHIPoBLEServiceMode mServiceMode;
125 BitFlags<Flags> mFlags;
126 char mDeviceName[kMaxDeviceNameLength + 1];
127 // The advertising set handle allocated from Bluetooth stack.
128 uint8_t advertising_set_handle = 0xff;
130 CHIP_ERROR MapBLEError(int bleErr);
131 void DriveBLEState(void);
132 CHIP_ERROR ConfigureAdvertisingData(void);
133 CHIP_ERROR StartAdvertising(void);
134 CHIP_ERROR StopAdvertising(void);
135 void UpdateMtu(volatile sl_bt_msg_t * evt);
136 void HandleBootEvent(void);
137 void HandleConnectEvent(volatile sl_bt_msg_t * evt);
138 void HandleConnectionCloseEvent(volatile sl_bt_msg_t * evt);
139 void HandleWriteEvent(volatile sl_bt_msg_t * evt);
140 void HandleTXCharCCCDWrite(volatile sl_bt_msg_t * evt);
141 void HandleRXCharWrite(volatile sl_bt_msg_t * evt);
142 void HandleTxConfirmationEvent(volatile sl_bt_msg_t * evt);
143 void HandleSoftTimerEvent(volatile sl_bt_msg_t * evt);
144 bool RemoveConnection(uint8_t connectionHandle);
145 void AddConnection(uint8_t connectionHandle, uint8_t bondingHandle);
146 void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
147 void CancelBleAdvTimeoutTimer(void);
148 CHIPoBLEConState * GetConnectionState(uint8_t conId, bool allocate = false);
149 uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate = false);
150 static void DriveBLEState(intptr_t arg);
151 static void bluetoothStackEventHandler(void * p_arg);
152 static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
156 * Returns a reference to the public interface of the BLEManager singleton object.
158 * Internal components should use this to access features of the BLEManager object
159 * that are common to all platforms.
161 inline BLEManager & BLEMgr(void)
163 return BLEManagerImpl::sInstance;
167 * Returns the platform-specific implementation of the BLEManager singleton object.
169 * Internal components can use this to gain access to features of the BLEManager
170 * that are specific to the EFR32 platforms.
172 inline BLEManagerImpl & BLEMgrImpl(void)
174 return BLEManagerImpl::sInstance;
177 inline BleLayer * BLEManagerImpl::_GetBleLayer()
182 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
187 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
189 return mFlags.Has(Flags::kAdvertisingEnabled);
192 } // namespace Internal
193 } // namespace DeviceLayer
196 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE