forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLEManagerImpl.h
432 lines (372 loc) · 16 KB
/
BLEManagerImpl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2018 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* Provides an implementation of the BLEManager singleton object
* for the ESP32 platform.
*/
#pragma once
#include <string>
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include "sdkconfig.h"
#if CONFIG_BT_BLUEDROID_ENABLED
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include "esp_gattc_api.h"
#endif
#include "esp_gatts_api.h"
#include <lib/core/CHIPCallback.h>
#elif CONFIG_BT_NIMBLE_ENABLED
/* min max macros in NimBLE can cause build issues with generic min max
* functions defined in CHIP.*/
#define min
#define max
#include "host/ble_hs.h"
#undef min
#undef max
/* GATT context */
struct ble_gatt_char_context
{
uint16_t conn_handle;
uint16_t attr_handle;
struct ble_gatt_access_ctxt * ctxt;
void * arg;
};
#endif
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER && CONFIG_BT_NIMBLE_ENABLED
#include "nimble/blecent.h"
#endif
#include "ble/Ble.h"
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include <ble/BleLayer.h>
#include <ble/BleUUID.h>
#include <platform/ESP32/ChipDeviceScanner.h>
#endif
#define MAX_SCAN_RSP_DATA_LEN 31
namespace chip {
namespace DeviceLayer {
namespace Internal {
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
void HandleIncomingBleConnection(Ble::BLEEndPoint * bleEP);
enum class BleScanState : uint8_t
{
kNotScanning,
kScanForDiscriminator,
kScanForAddress,
kConnecting,
};
struct BLEAdvConfig
{
char * mpBleName;
uint32_t mAdapterId;
uint8_t mMajor;
uint8_t mMinor;
uint16_t mVendorId;
uint16_t mProductId;
uint64_t mDeviceId;
uint8_t mPairingStatus;
uint8_t mType;
uint16_t mDuration;
const char * mpAdvertisingUUID;
};
struct BLEScanConfig
{
// If an active scan for connection is being performed
BleScanState mBleScanState = BleScanState::kNotScanning;
// If scanning by discriminator, what are we scanning for
SetupDiscriminator mDiscriminator;
// If scanning by address, what address are we searching for
std::string mAddress;
// Optional argument to be passed to callback functions provided by the BLE scan/connect requestor
void * mAppState = nullptr;
};
#endif
/**
* Concrete implementation of the BLEManager singleton object for the ESP32 platform.
*/
class BLEManagerImpl final : public BLEManager,
private Ble::BleLayer,
private Ble::BlePlatformDelegate,
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
private Ble::BleApplicationDelegate,
private Ble::BleConnectionDelegate,
private ChipDeviceScannerDelegate
#else
private Ble::BleApplicationDelegate
#endif
{
public:
uint8_t scanResponseBuffer[MAX_SCAN_RSP_DATA_LEN];
BLEManagerImpl() {}
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral);
#if CONFIG_BT_BLUEDROID_ENABLED
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t * param);
#endif
#endif
CHIP_ERROR ConfigureScanResponseData(ByteSpan data);
void ClearScanResponseData(void);
private:
chip::Optional<chip::ByteSpan> mScanResponse;
// Allow the BLEManager interface class to delegate method calls to
// the implementation methods provided by this class.
friend BLEManager;
// ===== Members that implement the BLEManager internal interface.
CHIP_ERROR _Init(void);
void _Shutdown();
bool _IsAdvertisingEnabled(void);
CHIP_ERROR _SetAdvertisingEnabled(bool val);
bool _IsAdvertising(void);
CHIP_ERROR _SetAdvertisingMode(BLEAdvertisingMode mode);
CHIP_ERROR _GetDeviceName(char * buf, size_t bufSize);
CHIP_ERROR _SetDeviceName(const char * deviceName);
uint16_t _NumConnections(void);
void _OnPlatformEvent(const ChipDeviceEvent * event);
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
void HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * event);
CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
#endif
::chip::Ble::BleLayer * _GetBleLayer(void);
// ===== Members that implement virtual methods on BlePlatformDelegate.
bool SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
const Ble::ChipBleUUID * charId) override;
bool UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId,
const Ble::ChipBleUUID * charId) override;
bool CloseConnection(BLE_CONNECTION_OBJECT conId) override;
uint16_t GetMTU(BLE_CONNECTION_OBJECT conId) const override;
bool SendIndication(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
System::PacketBufferHandle pBuf) override;
bool SendWriteRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
System::PacketBufferHandle pBuf) override;
bool SendReadRequest(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
System::PacketBufferHandle pBuf) override;
bool SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext, const Ble::ChipBleUUID * svcId,
const Ble::ChipBleUUID * charId) override;
// ===== Members that implement virtual methods on BleApplicationDelegate.
void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
// ===== Members that implement virtual methods on BleConnectionDelegate.
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
void NewConnection(chip::Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator) override;
void NewConnection(chip::Ble::BleLayer * bleLayer, void * appState, BLE_CONNECTION_OBJECT connObj) override{};
CHIP_ERROR CancelConnection() override;
// ===== Members that implement virtual methods on ChipDeviceScannerDelegate
#if CONFIG_BT_NIMBLE_ENABLED
virtual void OnDeviceScanned(const struct ble_hs_adv_fields & fields, const ble_addr_t & addr,
const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#elif CONFIG_BT_BLUEDROID_ENABLED
virtual void OnDeviceScanned(esp_ble_addr_type_t & addr_type, esp_bd_addr_t & addr,
const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#endif
void OnScanComplete() override;
#endif
// ===== Members for internal use by the following friends.
friend BLEManager & BLEMgr(void);
friend BLEManagerImpl & BLEMgrImpl(void);
static BLEManagerImpl sInstance;
// ===== Private members reserved for use by this class only.
enum class Flags : uint16_t
{
kAsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
kESPBLELayerInitialized = 0x0002, /**< The ESP BLE layer has been initialized. */
kAppRegistered = 0x0004, /**< The CHIPoBLE application has been registered with the ESP BLE layer. */
kAttrsRegistered = 0x0008, /**< The CHIPoBLE GATT attributes have been registered with the ESP BLE layer. */
kGATTServiceStarted = 0x0010, /**< The CHIPoBLE GATT service has been started. */
kAdvertisingConfigured = 0x0020, /**< CHIPoBLE advertising has been configured in the ESP BLE layer. */
kAdvertising = 0x0040, /**< The system is currently CHIPoBLE advertising. */
kControlOpInProgress = 0x0080, /**< An async control operation has been issued to the ESP BLE layer. */
kAdvertisingEnabled = 0x0100, /**< The application has enabled CHIPoBLE advertising. */
kFastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
};
enum
{
kMaxConnections = BLE_LAYER_NUM_BLE_ENDPOINTS,
kMaxDeviceNameLength = 16
};
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
BLEAdvConfig mBLEAdvConfig;
#endif
#if CONFIG_BT_NIMBLE_ENABLED
uint16_t mSubscribedConIds[kMaxConnections];
#endif
struct CHIPoBLEConState
{
System::PacketBufferHandle PendingIndBuf;
uint16_t ConId;
uint16_t MTU : 10;
uint16_t Allocated : 1;
uint16_t Subscribed : 1;
uint16_t Unused : 4;
void Set(uint16_t conId)
{
PendingIndBuf = nullptr;
ConId = conId;
MTU = 0;
Allocated = 1;
Subscribed = 0;
Unused = 0;
}
void Reset()
{
PendingIndBuf = nullptr;
ConId = BLE_CONNECTION_UNINITIALIZED;
MTU = 0;
Allocated = 0;
Subscribed = 0;
Unused = 0;
}
};
CHIPoBLEConState mCons[kMaxConnections];
CHIPoBLEServiceMode mServiceMode;
#if CONFIG_BT_BLUEDROID_ENABLED
esp_gatt_if_t mAppIf;
#elif CONFIG_BT_NIMBLE_ENABLED
uint16_t mNumGAPCons;
#endif
uint16_t mServiceAttrHandle;
uint16_t mRXCharAttrHandle;
uint16_t mTXCharAttrHandle;
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
uint16_t mC3CharAttrHandle;
#endif
uint16_t mTXCharCCCDAttrHandle;
BitFlags<Flags> mFlags;
char mDeviceName[kMaxDeviceNameLength + 1];
CHIP_ERROR MapBLEError(int bleErr);
void DriveBLEState(void);
CHIP_ERROR InitESPBleLayer(void);
void DeinitESPBleLayer(void);
CHIP_ERROR ConfigureAdvertisingData(void);
CHIP_ERROR StartAdvertising(void);
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
void CancelBleAdvTimeoutTimer(void);
static void BleAdvTimeoutHandler(System::Layer *, void *);
#if CONFIG_BT_BLUEDROID_ENABLED
void HandleGATTControlEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
void HandleGATTCommEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
void HandleRXCharWrite(esp_ble_gatts_cb_param_t * param);
void HandleTXCharRead(esp_ble_gatts_cb_param_t * param);
void HandleTXCharCCCDRead(esp_ble_gatts_cb_param_t * param);
void HandleTXCharCCCDWrite(esp_ble_gatts_cb_param_t * param);
void HandleTXCharConfirm(CHIPoBLEConState * conState, esp_ble_gatts_cb_param_t * param);
void HandleDisconnect(esp_ble_gatts_cb_param_t * param);
CHIPoBLEConState * GetConnectionState(uint16_t conId, bool allocate = false);
bool ReleaseConnectionState(uint16_t conId);
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
CHIP_ERROR HandleGAPConnect(esp_ble_gattc_cb_param_t p_data);
CHIP_ERROR HandleGAPCentralConnect(esp_ble_gattc_cb_param_t p_data);
static void HandleConnectFailed(CHIP_ERROR error);
static void ConnectDevice(esp_bd_addr_t & addr, esp_ble_addr_type_t addr_type, uint16_t timeout);
void HandleGAPConnectionFailed();
CHIP_ERROR HandleRXNotify(esp_ble_gattc_cb_param_t p_data);
#endif
static void HandleGATTEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
static void HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);
#elif CONFIG_BT_NIMBLE_ENABLED
CHIP_ERROR DeinitBLE();
static void ClaimBLEMemory(System::Layer *, void *);
void HandleRXCharRead(struct ble_gatt_char_context * param);
void HandleRXCharWrite(struct ble_gatt_char_context * param);
void HandleTXCharWrite(struct ble_gatt_char_context * param);
void HandleTXCharRead(struct ble_gatt_char_context * param);
void HandleTXCharCCCDRead(void * param);
void HandleTXCharCCCDWrite(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleTXComplete(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleGAPConnect(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleGAPPeripheralConnect(struct ble_gap_event * gapEvent);
CHIP_ERROR HandleGAPDisconnect(struct ble_gap_event * gapEvent);
CHIP_ERROR SetSubscribed(uint16_t conId);
bool UnsetSubscribed(uint16_t conId);
bool IsSubscribed(uint16_t conId);
static void ConnectDevice(const ble_addr_t & addr, uint16_t timeout);
CHIP_ERROR HandleGAPCentralConnect(struct ble_gap_event * gapEvent);
void HandleGAPConnectionFailed(struct ble_gap_event * gapEvent, CHIP_ERROR error);
static CHIP_ERROR bleprph_set_random_addr(void);
static void bleprph_host_task(void * param);
static void bleprph_on_sync(void);
static void bleprph_on_reset(int);
static const struct ble_gatt_svc_def CHIPoBLEGATTAttrs[];
static int ble_svr_gap_event(struct ble_gap_event * event, void * arg);
static int gatt_svr_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg);
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
static int gatt_svr_chr_access_additional_data(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt,
void * arg);
void HandleC3CharRead(struct ble_gatt_char_context * param);
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static int btshell_on_mtu(uint16_t conn_handle, const struct ble_gatt_error * error, uint16_t mtu, void * arg);
bool SubOrUnsubChar(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
bool subscribe);
static void OnGattDiscComplete(const struct peer * peer, int status, void * arg);
static void HandleConnectFailed(CHIP_ERROR error);
CHIP_ERROR HandleRXNotify(struct ble_gap_event * event);
#endif
#endif
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static void CancelConnect(void);
static void HandleConnectTimeout(chip::System::Layer *, void * context);
void InitiateScan(BleScanState scanType);
static void InitiateScan(intptr_t arg);
void HandleAdvertisementTimer(System::Layer * systemLayer, void * context);
void HandleAdvertisementTimer();
void CleanScanConfig();
BLEScanConfig mBLEScanConfig;
bool mIsCentral;
#endif
static void DriveBLEState(intptr_t arg);
};
/**
* Returns a reference to the public interface of the BLEManager singleton object.
*
* Internal components should use this to access features of the BLEManager object
* that are common to all platforms.
*/
inline BLEManager & BLEMgr(void)
{
return BLEManagerImpl::sInstance;
}
/**
* Returns the platform-specific implementation of the BLEManager singleton object.
*
* Internal components can use this to gain access to features of the BLEManager
* that are specific to the ESP32 platform.
*/
inline BLEManagerImpl & BLEMgrImpl(void)
{
return BLEManagerImpl::sInstance;
}
inline ::chip::Ble::BleLayer * BLEManagerImpl::_GetBleLayer()
{
return this;
}
inline bool BLEManagerImpl::_IsAdvertisingEnabled(void)
{
return mFlags.Has(Flags::kAdvertisingEnabled);
}
inline bool BLEManagerImpl::_IsAdvertising(void)
{
return mFlags.Has(Flags::kAdvertising);
}
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE