ESP_IOT v2.5
IOT ESP Coding
NimBLEDevice.h
Go to the documentation of this file.
1#ifdef NOT_FOR_SHOW_ONLY
2/*
3 * NimBLEDevice.h
4 *
5 * Created: on Jan 24 2020
6 * Author H2zero
7 *
8 * Originally:
9 *
10 * BLEDevice.h
11 *
12 * Created on: Mar 16, 2017
13 * Author: kolban
14 */
15
16#ifndef MAIN_NIMBLEDEVICE_H_
17#define MAIN_NIMBLEDEVICE_H_
18
19#include "nimconfig.h"
20#if defined(CONFIG_BT_ENABLED)
21
22#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
23#include "NimBLEScan.h"
24#endif
25
26#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
27# if CONFIG_BT_NIMBLE_EXT_ADV
28# include "NimBLEExtAdvertising.h"
29# else
30# include "NimBLEAdvertising.h"
31# endif
32#endif
33
34#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
35#include "NimBLEClient.h"
36#endif
37
38#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
39#include "NimBLEServer.h"
40#endif
41
42#include "NimBLEUtils.h"
43#include "NimBLESecurity.h"
44#include "NimBLEAddress.h"
45
46#ifdef ESP_PLATFORM
47# include "esp_bt.h"
48#endif
49
50#include <map>
51#include <string>
52#include <list>
53
54#define BLEDevice NimBLEDevice
55#define BLEClient NimBLEClient
56#define BLERemoteService NimBLERemoteService
57#define BLERemoteCharacteristic NimBLERemoteCharacteristic
58#define BLERemoteDescriptor NimBLERemoteDescriptor
59#define BLEAdvertisedDevice NimBLEAdvertisedDevice
60#define BLEScan NimBLEScan
61#define BLEUUID NimBLEUUID
62#define BLESecurity NimBLESecurity
63#define BLESecurityCallbacks NimBLESecurityCallbacks
64#define BLEAddress NimBLEAddress
65#define BLEUtils NimBLEUtils
66#define BLEClientCallbacks NimBLEClientCallbacks
67#define BLEAdvertisedDeviceCallbacks NimBLEAdvertisedDeviceCallbacks
68#define BLEScanResults NimBLEScanResults
69#define BLEServer NimBLEServer
70#define BLEService NimBLEService
71#define BLECharacteristic NimBLECharacteristic
72#define BLEAdvertising NimBLEAdvertising
73#define BLEServerCallbacks NimBLEServerCallbacks
74#define BLECharacteristicCallbacks NimBLECharacteristicCallbacks
75#define BLEAdvertisementData NimBLEAdvertisementData
76#define BLEDescriptor NimBLEDescriptor
77#define BLE2902 NimBLE2902
78#define BLE2904 NimBLE2904
79#define BLEDescriptorCallbacks NimBLEDescriptorCallbacks
80#define BLEBeacon NimBLEBeacon
81#define BLEEddystoneTLM NimBLEEddystoneTLM
82#define BLEEddystoneURL NimBLEEddystoneURL
83
84#ifdef CONFIG_BT_NIMBLE_MAX_CONNECTIONS
85#define NIMBLE_MAX_CONNECTIONS CONFIG_BT_NIMBLE_MAX_CONNECTIONS
86#else
87#define NIMBLE_MAX_CONNECTIONS CONFIG_NIMBLE_MAX_CONNECTIONS
88#endif
89
90typedef int (*gap_event_handler)(ble_gap_event *event, void *arg);
91
92extern "C" void ble_store_config_init(void);
93
94/**
95 * @brief A model of a %BLE Device from which all the BLE roles are created.
96 */
97class NimBLEDevice {
98public:
99 static void init(const std::string &deviceName);
100 static void deinit(bool clearAll = false);
101 static bool getInitialized();
102 static NimBLEAddress getAddress();
103 static std::string toString();
104 static bool whiteListAdd(const NimBLEAddress & address);
105 static bool whiteListRemove(const NimBLEAddress & address);
106 static bool onWhiteList(const NimBLEAddress & address);
107 static size_t getWhiteListCount();
108 static NimBLEAddress getWhiteListAddress(size_t index);
109
110#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
111 static NimBLEScan* getScan();
112#endif
113
114#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
115 static NimBLEServer* createServer();
116 static NimBLEServer* getServer();
117#endif
118
119#ifdef ESP_PLATFORM
120 static void setPower(esp_power_level_t powerLevel, esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
121 static int getPower(esp_ble_power_type_t powerType=ESP_BLE_PWR_TYPE_DEFAULT);
122 static void setOwnAddrType(uint8_t own_addr_type, bool useNRPA=false);
123 static void setScanDuplicateCacheSize(uint16_t cacheSize);
124 static void setScanFilterMode(uint8_t type);
125#else
126 static void setPower(int dbm);
127 static int getPower();
128#endif
129
130 static void setCustomGapHandler(gap_event_handler handler);
131 static void setSecurityAuth(bool bonding, bool mitm, bool sc);
132 static void setSecurityAuth(uint8_t auth_req);
133 static void setSecurityIOCap(uint8_t iocap);
134 static void setSecurityInitKey(uint8_t init_key);
135 static void setSecurityRespKey(uint8_t init_key);
136 static void setSecurityPasskey(uint32_t pin);
137 static uint32_t getSecurityPasskey();
138 static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
139 static int startSecurity(uint16_t conn_id);
140 static int setMTU(uint16_t mtu);
141 static uint16_t getMTU();
142 static bool isIgnored(const NimBLEAddress &address);
143 static void addIgnored(const NimBLEAddress &address);
144 static void removeIgnored(const NimBLEAddress &address);
145
146#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
147# if CONFIG_BT_NIMBLE_EXT_ADV
148 static NimBLEExtAdvertising* getAdvertising();
149 static bool startAdvertising(uint8_t inst_id,
150 int duration = 0,
151 int max_events = 0);
152 static bool stopAdvertising(uint8_t inst_id);
153 static bool stopAdvertising();
154# else
155 static NimBLEAdvertising* getAdvertising();
156 static bool startAdvertising();
157 static bool stopAdvertising();
158# endif
159#endif
160
161#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
162 static NimBLEClient* createClient(NimBLEAddress peerAddress = NimBLEAddress(""));
163 static bool deleteClient(NimBLEClient* pClient);
164 static NimBLEClient* getClientByID(uint16_t conn_id);
165 static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
166 static NimBLEClient* getDisconnectedClient();
167 static size_t getClientListSize();
168 static std::list<NimBLEClient*>* getClientList();
169#endif
170
171#if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
172 static bool deleteBond(const NimBLEAddress &address);
173 static int getNumBonds();
174 static bool isBonded(const NimBLEAddress &address);
175 static void deleteAllBonds();
176 static NimBLEAddress getBondedAddress(int index);
177#endif
178
179private:
180#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
181 friend class NimBLEClient;
182#endif
183
184#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
185 friend class NimBLEScan;
186#endif
187
188#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
189 friend class NimBLEServer;
190 friend class NimBLECharacteristic;
191#endif
192
193#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
194 friend class NimBLEAdvertising;
195# if CONFIG_BT_NIMBLE_EXT_ADV
196 friend class NimBLEExtAdvertising;
197 friend class NimBLEExtAdvertisement;
198# endif
199#endif
200
201 static void onReset(int reason);
202 static void onSync(void);
203 static void host_task(void *param);
204 static bool m_synced;
205
206#if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
207 static NimBLEScan* m_pScan;
208#endif
209
210#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
211 static NimBLEServer* m_pServer;
212#endif
213
214#if defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
215# if CONFIG_BT_NIMBLE_EXT_ADV
216 static NimBLEExtAdvertising* m_bleAdvertising;
217# else
218 static NimBLEAdvertising* m_bleAdvertising;
219# endif
220#endif
221
222#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
223 static std::list <NimBLEClient*> m_cList;
224#endif
225 static std::list <NimBLEAddress> m_ignoreList;
226 static NimBLESecurityCallbacks* m_securityCallbacks;
227 static uint32_t m_passkey;
228 static ble_gap_event_listener m_listener;
229 static gap_event_handler m_customGapHandler;
230 static uint8_t m_own_addr_type;
231#ifdef ESP_PLATFORM
232 static uint16_t m_scanDuplicateSize;
233 static uint8_t m_scanFilterMode;
234#endif
235 static std::vector<NimBLEAddress> m_whiteList;
236};
237
238
239#endif // CONFIG_BT_ENABLED
240#endif // MAIN_NIMBLEDEVICE_H_
241
242#endif