7
7
#include " sdkconfig.h"
8
8
#if defined(CONFIG_BT_ENABLED)
9
9
#include < freertos/FreeRTOS.h>
10
+ #include < freertos/event_groups.h>
10
11
#include < freertos/task.h>
11
12
#include < esp_err.h>
12
13
#include < nvs_flash.h>
13
- #include < freertos/FreeRTOS.h>
14
- #include < freertos/event_groups.h>
15
- #include < bt.h> // ESP32 BLE
16
- #include < esp_bt_main.h> // ESP32 BLE
17
- #include < esp_gap_ble_api.h> // ESP32 BLE
18
- // ESP32 BLE
19
- #include < esp_gatts_api.h> // ESP32 BLE
20
- #include < esp_err.h> // ESP32 ESP-IDF
21
- #include < esp_log.h> // ESP32 ESP-IDF
22
- #include < map> // Part of C++ STL
23
- #include < sstream>
24
- #include < iomanip>
14
+ #include < bt.h> // ESP32 BLE
15
+ #include < esp_bt_main.h> // ESP32 BLE
16
+ #include < esp_gap_ble_api.h> // ESP32 BLE
17
+ #include < esp_gatts_api.h> // ESP32 BLE
18
+ #include < esp_gattc_api.h> // ESP32 BLE
19
+ #include < esp_err.h> // ESP32 ESP-IDF
20
+ #include < esp_log.h> // ESP32 ESP-IDF
21
+ #include < map> // Part of C++ Standard library
22
+ #include < sstream> // Part of C++ Standard library
23
+ #include < iomanip> // Part of C++ Standard library
25
24
26
25
#include " BLEDevice.h"
27
26
#include " BLEClient.h"
30
29
31
30
static const char * LOG_TAG = " BLEDevice" ;
32
31
33
- BLEServer *BLEDevice::m_bleServer = nullptr ;
34
- BLEScan *BLEDevice::m_pScan = nullptr ;
35
- BLEClient *BLEDevice::m_pClient = nullptr ;
36
-
37
- #include < esp_gattc_api.h>
38
32
33
+ /* *
34
+ * Singletons for the BLEDevice.
35
+ */
36
+ BLEServer* BLEDevice::m_pServer = nullptr ;
37
+ BLEScan* BLEDevice::m_pScan = nullptr ;
38
+ BLEClient* BLEDevice::m_pClient = nullptr ;
39
39
40
+ /* *
41
+ * @brief Create a new instance of a client.
42
+ * @return A new instance of the client.
43
+ */
40
44
BLEClient* BLEDevice::createClient () {
41
45
m_pClient = new BLEClient ();
42
46
return m_pClient;
43
47
} // createClient
44
48
49
+
50
+ /* *
51
+ * @brief Create a new instance of a server.
52
+ * @return A new instance of the server.
53
+ */
45
54
BLEServer* BLEDevice::createServer () {
46
- return new BLEServer ();
47
- }
55
+ m_pServer = new BLEServer ();
56
+ return m_pServer;
57
+ } // createServer
48
58
49
59
50
60
/* *
51
61
* @brief Handle GATT server events.
52
62
*
53
- * @param [in] event
54
- * @param [in] gatts_if
55
- * @param [in] param
63
+ * @param [in] event The event that has been newly received.
64
+ * @param [in] gatts_if The connection to the GATT interface.
65
+ * @param [in] param Parameters for the event.
56
66
*/
57
67
void BLEDevice::gattServerEventHandler (
58
68
esp_gatts_cb_event_t event,
59
69
esp_gatt_if_t gatts_if,
60
- esp_ble_gatts_cb_param_t * param
70
+ esp_ble_gatts_cb_param_t * param
61
71
) {
62
72
ESP_LOGD (LOG_TAG, " gattServerEventHandler [esp_gatt_if: %d] ... %s" ,
63
73
gatts_if,
64
74
BLEUtils::gattServerEventTypeToString (event).c_str ());
75
+
65
76
BLEUtils::dumpGattServerEvent (event, gatts_if, param);
66
- if (BLEDevice::m_bleServer != nullptr ) {
67
- BLEDevice::m_bleServer->handleGATTServerEvent (event, gatts_if, param);
77
+
78
+ if (BLEDevice::m_pServer != nullptr ) {
79
+ BLEDevice::m_pServer->handleGATTServerEvent (event, gatts_if, param);
68
80
}
69
81
} // gattServerEventHandler
70
82
@@ -73,10 +85,6 @@ void BLEDevice::gattServerEventHandler(
73
85
* @brief Handle GATT client events.
74
86
*
75
87
* Handler for the GATT client events.
76
- * * `ESP_GATTC_OPEN_EVT` – Invoked when a connection is opened.
77
- * * `ESP_GATTC_PREP_WRITE_EVT` – Response to write a characteristic.
78
- * * `ESP_GATTC_READ_CHAR_EVT` – Response to read a characteristic.
79
- * * `ESP_GATTC_REG_EVT` – Invoked when a GATT client has been registered.
80
88
*
81
89
* @param [in] event
82
90
* @param [in] gattc_if
@@ -90,12 +98,13 @@ void BLEDevice::gattClientEventHandler(
90
98
ESP_LOGD (LOG_TAG, " gattClientEventHandler [esp_gatt_if: %d] ... %s" ,
91
99
gattc_if, BLEUtils::gattClientEventTypeToString (event).c_str ());
92
100
BLEUtils::dumpGattClientEvent (event, gattc_if, param);
93
-
101
+ /*
94
102
switch(event) {
95
103
default: {
96
104
break;
97
105
}
98
106
} // switch
107
+ */
99
108
100
109
// If we have a client registered, call it.
101
110
if (BLEDevice::m_pClient != nullptr ) {
@@ -128,8 +137,8 @@ void BLEDevice::gapEventHandler(
128
137
}
129
138
} // switch
130
139
131
- if (BLEDevice::m_bleServer != nullptr ) {
132
- BLEDevice::m_bleServer ->handleGAPEvent (event, param);
140
+ if (BLEDevice::m_pServer != nullptr ) {
141
+ BLEDevice::m_pServer ->handleGAPEvent (event, param);
133
142
}
134
143
135
144
if (BLEDevice::m_pScan != nullptr ) {
@@ -138,6 +147,18 @@ void BLEDevice::gapEventHandler(
138
147
} // gapEventHandler
139
148
140
149
150
+ /* *
151
+ * @brief Retrieve the Scan object that we use for scanning.
152
+ * @return The scanning object reference.
153
+ */
154
+ BLEScan* BLEDevice::getScan () {
155
+ if (m_pScan == nullptr ) {
156
+ m_pScan = new BLEScan ();
157
+ }
158
+ return m_pScan;
159
+ } // getScan
160
+
161
+
141
162
/* *
142
163
* @brief Initialize the %BLE environment.
143
164
* @param deviceName The device name of the device.
@@ -209,18 +230,4 @@ void BLEDevice::init(std::string deviceName) {
209
230
} // init
210
231
211
232
212
-
213
- /* *
214
- * @brief Retrieve the Scan object that we use for scanning.
215
- * @return The scanning object reference.
216
- */
217
- BLEScan* BLEDevice::getScan () {
218
- if (m_pScan == nullptr ) {
219
- m_pScan = new BLEScan ();
220
- }
221
- return m_pScan;
222
- } // getScan
223
-
224
-
225
-
226
233
#endif // CONFIG_BT_ENABLED
0 commit comments