84cfc6d0c9b80cf35c83cb89df625235bd46fa45
[platform/upstream/connectedhomeip.git] / src / platform / EFR32 / BLEManagerImpl.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 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 the Silicon Labs EFR32 platforms.
23  */
24
25 #pragma once
26 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
27
28 #include "gatt_db.h"
29 #include "sl_bgapi.h"
30 #include "sl_bt_api.h"
31
32 namespace chip {
33 namespace DeviceLayer {
34 namespace Internal {
35
36 using namespace chip::Ble;
37
38 /**
39  * Concrete implementation of the NetworkProvisioningServer singleton object for the EFR32 platforms.
40  */
41 class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate
42 {
43     // Allow the BLEManager interface class to delegate method calls to
44     // the implementation methods provided by this class.
45     friend BLEManager;
46
47     // ===== Members that implement the BLEManager internal interface.
48
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);
62
63     // ===== Members that implement virtual methods on BlePlatformDelegate.
64
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;
79
80     // ===== Members that implement virtual methods on BleApplicationDelegate.
81
82     void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
83
84     // ===== Members for internal use by the following friends.
85
86     friend BLEManager & BLEMgr(void);
87     friend BLEManagerImpl & BLEMgrImpl(void);
88
89     static BLEManagerImpl sInstance;
90
91     // ===== Private members reserved for use by this class only.
92
93     enum
94     {
95         kFlag_AdvertisingEnabled     = 0x0001,
96         kFlag_FastAdvertisingEnabled = 0x0002,
97         kFlag_Advertising            = 0x0004,
98         kFlag_RestartAdvertising     = 0x0008,
99         kFlag_EFRBLEStackInitialized = 0x0010,
100         kFlag_DeviceNameSet          = 0x0020,
101     };
102
103     enum
104     {
105         kMaxConnections      = BLE_LAYER_NUM_BLE_ENDPOINTS,
106         kMaxDeviceNameLength = 16,
107         kUnusedIndex         = 0xFF,
108     };
109
110     struct CHIPoBLEConState
111     {
112         bd_addr address;
113         uint16_t mtu : 10;
114         uint16_t allocated : 1;
115         uint16_t subscribed : 1;
116         uint16_t unused : 4;
117         uint8_t connectionHandle;
118         uint8_t bondingHandle;
119     };
120
121     CHIPoBLEConState mBleConnections[kMaxConnections];
122     uint8_t mIndConfId[kMaxConnections];
123     CHIPoBLEServiceMode mServiceMode;
124     uint16_t mFlags;
125     char mDeviceName[kMaxDeviceNameLength + 1];
126     // The advertising set handle allocated from Bluetooth stack.
127     uint8_t advertising_set_handle = 0xff;
128
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);
149 };
150
151 /**
152  * Returns a reference to the public interface of the BLEManager singleton object.
153  *
154  * Internal components should use this to access features of the BLEManager object
155  * that are common to all platforms.
156  */
157 inline BLEManager & BLEMgr(void)
158 {
159     return BLEManagerImpl::sInstance;
160 }
161
162 /**
163  * Returns the platform-specific implementation of the BLEManager singleton object.
164  *
165  * Internal components can use this to gain access to features of the BLEManager
166  * that are specific to the EFR32 platforms.
167  */
168 inline BLEManagerImpl & BLEMgrImpl(void)
169 {
170     return BLEManagerImpl::sInstance;
171 }
172
173 inline BleLayer * BLEManagerImpl::_GetBleLayer()
174 {
175     return this;
176 }
177
178 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
179 {
180     return mServiceMode;
181 }
182
183 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
184 {
185     return GetFlag(mFlags, kFlag_AdvertisingEnabled);
186 }
187
188 inline bool BLEManagerImpl::_IsFastAdvertisingEnabled(void)
189 {
190     return GetFlag(mFlags, kFlag_FastAdvertisingEnabled);
191 }
192
193 } // namespace Internal
194 } // namespace DeviceLayer
195 } // namespace chip
196
197 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE