21
21
#include < iomanip>
22
22
23
23
#include " BLE.h"
24
- #include " BLERemoteDevice .h"
24
+ #include " BLEClient .h"
25
25
#include " BLEUtils.h"
26
26
#include " BLEXXXCharacteristic.h"
27
27
#include " GeneralUtils.h"
@@ -40,10 +40,11 @@ static EventGroupHandle_t g_eventGroup;
40
40
* Note that this address is binary and should not be directly printed. The value of the map is
41
41
* the BLEDevice object that this device represents.
42
42
*/
43
- static std::map<std::string, BLERemoteDevice > g_devices;
43
+ static std::map<std::string, BLEClient > g_devices;
44
44
45
45
BLEServer *BLE::m_bleServer = nullptr ;
46
46
BLEScan *BLE::m_pScan = nullptr ;
47
+ BLEClient *BLE::m_pClient = nullptr ;
47
48
48
49
BLE::BLE () {
49
50
}
@@ -53,6 +54,12 @@ BLE::~BLE() {
53
54
}
54
55
55
56
57
+ BLEClient* BLE::createClient () {
58
+ m_pClient = new BLEClient ();
59
+ return m_pClient;
60
+ }
61
+
62
+
56
63
/* *
57
64
* @brief Dump all the devices.
58
65
*
@@ -74,7 +81,7 @@ void BLE::dumpDevices() {
74
81
* @param [in] gatts_if
75
82
* @param [in] param
76
83
*/
77
- void gatt_server_event_handler (
84
+ static void gatt_server_event_handler (
78
85
esp_gatts_cb_event_t event,
79
86
esp_gatt_if_t gatts_if,
80
87
esp_ble_gatts_cb_param_t *param
@@ -112,13 +119,15 @@ static void gatt_client_event_handler(
112
119
BLEUtils::dumpGattClientEvent (event, gattc_if, param);
113
120
114
121
switch (event) {
122
+ /*
115
123
case ESP_GATTC_OPEN_EVT: {
116
- BLERemoteDevice *pDevice = BLEUtils::findByAddress (std::string ((char *)param->open .remote_bda , 6 ));
124
+ BLEClient *pDevice = BLEUtils::findByAddress(std::string((char *)param->open.remote_bda, 6));
117
125
BLEUtils::registerByConnId(param->open.conn_id, pDevice);
118
126
pDevice->dump();
119
127
pDevice->onConnected(param->open.status);
120
128
break;
121
129
}
130
+ */
122
131
123
132
/*
124
133
* The search_res field of the parameter has been populated. It contains:
@@ -129,17 +138,20 @@ static void gatt_client_event_handler(
129
138
* * uint8_t inst_id
130
139
* * bool is_primary
131
140
*/
141
+
142
+ /*
132
143
case ESP_GATTC_SEARCH_RES_EVT: {
133
- BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->search_res .conn_id );
144
+ BLEClient *pDevice = BLEUtils::findByConnId(param->search_res.conn_id);
134
145
pDevice->addService(param->search_res.srvc_id);
135
146
break;
136
147
}
137
148
138
149
case ESP_GATTC_SEARCH_CMPL_EVT: {
139
- BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->search_cmpl .conn_id );
150
+ BLEClient *pDevice = BLEUtils::findByConnId(param->search_cmpl.conn_id);
140
151
pDevice->onSearchComplete();
141
152
break;
142
153
}
154
+ */
143
155
144
156
/*
145
157
* The `get_char` field of the parameters has been populated. It contains:
@@ -149,15 +161,17 @@ static void gatt_client_event_handler(
149
161
* * esp_gatt_id_t char_id
150
162
* * esp_gatt_char_prop_t char_prop
151
163
*/
164
+ /*
152
165
case ESP_GATTC_GET_CHAR_EVT: {
153
166
if (param->get_char.status == ESP_GATT_OK) {
154
- BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->get_char .conn_id );
167
+ BLEClient *pDevice = BLEUtils::findByConnId(param->get_char.conn_id);
155
168
BLECharacteristicXXX characteristic(param->get_char.conn_id,
156
169
param->get_char.srvc_id, param->get_char.char_id, param->get_char.char_prop);
157
170
pDevice->onCharacteristic(characteristic);
158
171
}
159
172
break;
160
173
}
174
+ */
161
175
162
176
163
177
/*
@@ -171,17 +185,25 @@ static void gatt_client_event_handler(
171
185
* * uint16_t value_type
172
186
* * uint16_t value_len
173
187
*/
188
+ /*
174
189
case ESP_GATTC_READ_CHAR_EVT: {
175
190
if (param->read.status == ESP_GATT_OK) {
176
- BLERemoteDevice *pDevice = BLEUtils::findByConnId (param->read .conn_id );
191
+ BLEClient *pDevice = BLEUtils::findByConnId(param->read.conn_id);
177
192
std::string data = std::string((char *)param->read.value, param->read.value_len);
178
193
pDevice->onRead(data);
179
194
}
180
195
break;
181
196
}
197
+ */
182
198
183
- default :
184
- break ;
199
+ default : {
200
+ break ;
201
+ }
202
+ } // switch
203
+
204
+ // If we have a client registered, call it.
205
+ if (BLE::m_pClient != nullptr ) {
206
+ BLE::m_pClient->gattClientEventHandler (event, gattc_if, param);
185
207
}
186
208
187
209
} // gatt_event_handler
@@ -223,7 +245,7 @@ static void gap_event_handler(
223
245
/* *
224
246
* @brief Get the current set of known devices.
225
247
*/
226
- std::map<std::string, BLERemoteDevice > getDevices () {
248
+ std::map<std::string, BLEClient > getDevices () {
227
249
return g_devices;
228
250
} // getDevices
229
251
@@ -346,4 +368,6 @@ BLEScan* BLE::getScan() {
346
368
return m_pScan;
347
369
} // getScan
348
370
371
+
372
+
349
373
#endif // CONFIG_BT_ENABLED
0 commit comments