3 * Copyright (c) 2020 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
30 #include "sl_bt_api.h"
33 namespace DeviceLayer {
36 using namespace chip::Ble;
39 * Concrete implementation of the NetworkProvisioningServer singleton object for the EFR32 platforms.
41 class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate
43 // Allow the BLEManager interface class to delegate method calls to
44 // the implementation methods provided by this class.
47 // ===== Members that implement the BLEManager internal interface.
49 CHIP_ERROR _Init(void);
50 CHIPoBLEServiceMode _GetCHIPoBLEServiceMode(void);
51 CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
52 bool _IsAdvertisingEnabled(void);
53 CHIP_ERROR _SetAdvertisingEnabled(bool val);
54 bool _IsFastAdvertisingEnabled(void);
55 CHIP_ERROR _SetFastAdvertisingEnabled(bool val);
56 bool _IsAdvertising(void);
57 CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
58 CHIP_ERROR _SetDeviceName(const char * deviceName);
59 uint16_t _NumConnections(void);
60 void _OnPlatformEvent(const ChipDeviceEvent * event);
61 BleLayer * _GetBleLayer(void);
63 // ===== Members that implement virtual methods on BlePlatformDelegate.
65 bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
66 const Ble::ChipBleUUID * charId) override;
67 bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
68 const Ble::ChipBleUUID * charId) override;
69 bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
70 uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
71 bool SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
72 System::PacketBufferHandle pBuf) override;
73 bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
74 System::PacketBufferHandle pBuf) override;
75 bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
76 System::PacketBufferHandle pBuf) override;
77 bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const Ble::ChipBleUUID * svcId,
78 const Ble::ChipBleUUID * charId) override;
80 // ===== Members that implement virtual methods on BleApplicationDelegate.
82 void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
84 // ===== Members for internal use by the following friends.
86 friend BLEManager & BLEMgr(void);
87 friend BLEManagerImpl & BLEMgrImpl(void);
89 static BLEManagerImpl sInstance;
91 // ===== Private members reserved for use by this class only.
95 kFlag_AdvertisingEnabled = 0x0001,
96 kFlag_FastAdvertisingEnabled = 0x0002,
97 kFlag_Advertising = 0x0004,
98 kFlag_RestartAdvertising = 0x0008,
99 kFlag_EFRBLEStackInitialized = 0x0010,
100 kFlag_DeviceNameSet = 0x0020,
105 kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS,
106 kMaxDeviceNameLength = 16,
110 struct CHIPoBLEConState
114 uint16_t allocated : 1;
115 uint16_t subscribed : 1;
117 uint8_t connectionHandle;
118 uint8_t bondingHandle;
121 CHIPoBLEConState mBleConnections[kMaxConnections];
122 uint8_t mIndConfId[kMaxConnections];
123 CHIPoBLEServiceMode mServiceMode;
125 char mDeviceName[kMaxDeviceNameLength + 1];
126 // The advertising set handle allocated from Bluetooth stack.
127 uint8_t advertising_set_handle = 0xff;
129 CHIP_ERROR MapBLEError(int bleErr);
130 void DriveBLEState(void);
131 CHIP_ERROR ConfigureAdvertisingData(void);
132 CHIP_ERROR StartAdvertising(void);
133 CHIP_ERROR StopAdvertising(void);
134 void UpdateMtu(volatile sl_bt_msg_t * evt);
135 void HandleBootEvent(void);
136 void HandleConnectEvent(volatile sl_bt_msg_t * evt);
137 void HandleConnectionCloseEvent(volatile sl_bt_msg_t * evt);
138 void HandleWriteEvent(volatile sl_bt_msg_t * evt);
139 void HandleTXCharCCCDWrite(volatile sl_bt_msg_t * evt);
140 void HandleRXCharWrite(volatile sl_bt_msg_t * evt);
141 void HandleTxConfirmationEvent(volatile sl_bt_msg_t * evt);
142 void HandleSoftTimerEvent(volatile sl_bt_msg_t * evt);
143 bool RemoveConnection(uint8_t connectionHandle);
144 void AddConnection(uint8_t connectionHandle, uint8_t bondingHandle);
145 CHIPoBLEConState * GetConnectionState(uint8_t conId, bool allocate = false);
146 uint8_t GetTimerHandle(uint8_t connectionHandle, bool allocate = false);
147 static void DriveBLEState(intptr_t arg);
148 static void bluetoothStackEventHandler(void * p_arg);
152 * Returns a reference to the public interface of the BLEManager singleton object.
154 * Internal components should use this to access features of the BLEManager object
155 * that are common to all platforms.
157 inline BLEManager & BLEMgr(void)
159 return BLEManagerImpl::sInstance;
163 * Returns the platform-specific implementation of the BLEManager singleton object.
165 * Internal components can use this to gain access to features of the BLEManager
166 * that are specific to the EFR32 platforms.
168 inline BLEManagerImpl & BLEMgrImpl(void)
170 return BLEManagerImpl::sInstance;
173 inline BleLayer * BLEManagerImpl::_GetBleLayer()
178 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
183 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
185 return GetFlag(mFlags, kFlag_AdvertisingEnabled);
188 inline bool BLEManagerImpl::_IsFastAdvertisingEnabled(void)
190 return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
193 } // namespace Internal
194 } // namespace DeviceLayer
197 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE