Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / EFR32 / BLEManagerImpl.h
1 /*
2  *
3  *    Copyright (c) 2020-2021 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 "FreeRTOS.h"
29 #include "gatt_db.h"
30 #include "sl_bgapi.h"
31 #include "sl_bt_api.h"
32 #include "timers.h"
33
34 namespace chip {
35 namespace DeviceLayer {
36 namespace Internal {
37
38 using namespace chip::Ble;
39
40 /**
41  * Concrete implementation of the BLEManager singleton object for the EFR32 platforms.
42  */
43 class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate
44 {
45     // Allow the BLEManager interface class to delegate method calls to
46     // the implementation methods provided by this class.
47     friend BLEManager;
48
49     // ===== Members that implement the BLEManager internal interface.
50
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);
63
64     // ===== Members that implement virtual methods on BlePlatformDelegate.
65
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;
80
81     // ===== Members that implement virtual methods on BleApplicationDelegate.
82
83     void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
84
85     // ===== Members for internal use by the following friends.
86
87     friend BLEManager & BLEMgr(void);
88     friend BLEManagerImpl & BLEMgrImpl(void);
89
90     static BLEManagerImpl sInstance;
91
92     // ===== Private members reserved for use by this class only.
93
94     enum class Flags : uint16_t
95     {
96         kAdvertisingEnabled     = 0x0001,
97         kFastAdvertisingEnabled = 0x0002,
98         kAdvertising            = 0x0004,
99         kRestartAdvertising     = 0x0008,
100         kEFRBLEStackInitialized = 0x0010,
101         kDeviceNameSet          = 0x0020,
102     };
103
104     enum
105     {
106         kMaxConnections      = BLE_LAYER_NUM_BLE_ENDPOINTS,
107         kMaxDeviceNameLength = 16,
108         kUnusedIndex         = 0xFF,
109     };
110
111     struct CHIPoBLEConState
112     {
113         bd_addr address;
114         uint16_t mtu : 10;
115         uint16_t allocated : 1;
116         uint16_t subscribed : 1;
117         uint16_t unused : 4;
118         uint8_t connectionHandle;
119         uint8_t bondingHandle;
120     };
121
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;
129
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);
153 };
154
155 /**
156  * Returns a reference to the public interface of the BLEManager singleton object.
157  *
158  * Internal components should use this to access features of the BLEManager object
159  * that are common to all platforms.
160  */
161 inline BLEManager & BLEMgr(void)
162 {
163     return BLEManagerImpl::sInstance;
164 }
165
166 /**
167  * Returns the platform-specific implementation of the BLEManager singleton object.
168  *
169  * Internal components can use this to gain access to features of the BLEManager
170  * that are specific to the EFR32 platforms.
171  */
172 inline BLEManagerImpl & BLEMgrImpl(void)
173 {
174     return BLEManagerImpl::sInstance;
175 }
176
177 inline BleLayer * BLEManagerImpl::_GetBleLayer()
178 {
179     return this;
180 }
181
182 inline BLEManager::CHIPoBLEServiceMode BLEManagerImpl::_GetCHIPoBLEServiceMode(void)
183 {
184     return mServiceMode;
185 }
186
187 inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
188 {
189     return mFlags.Has(Flags::kAdvertisingEnabled);
190 }
191
192 } // namespace Internal
193 } // namespace DeviceLayer
194 } // namespace chip
195
196 #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE