13
13
#include < esp_log.h>
14
14
#include < esp_err.h>
15
15
#include " BLECharacteristic.h"
16
+ #include " BLEService.h"
16
17
17
18
static char LOG_TAG[] = " BLECharacteristic" ;
18
19
10000
19
20
extern " C" {
20
21
char *espToString (esp_err_t value);
21
22
}
22
23
24
+ /* *
25
+ * @brief Construct a characteristic
26
+ * @param [in] uuid - UUID for the characteristic.
27
+ * @param [in] properties - Properties for the characteristic.
28
+ */
23
29
BLECharacteristic::BLECharacteristic (BLEUUID uuid, uint32_t properties) {
24
30
m_bleUUID = uuid;
25
31
m_value.attr_value = (uint8_t *)malloc (ESP_GATT_MAX_ATTR_LEN);
@@ -84,25 +90,27 @@ void BLECharacteristic::handleGATTServerEvent(
84
90
esp_gatt_if_t gatts_if,
85
91
esp_ble_gatts_cb_param_t *param) {
86
92
switch (event) {
93
+
87
94
// ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived.
88
95
//
89
96
// write:
90
- // - uint16_t conn_id
91
- // - uint16_t trans_id
97
+ // - uint16_t conn_id
98
+ // - uint16_t trans_id
92
99
// - esp_bd_addr_t bda
93
- // - uint16_t handle
94
- // - uint16_t offset
95
- // - bool need_rsp
96
- // - bool is_prep
97
- // - uint16_t len
98
- // - uint8_t *value
100
+ // - uint16_t handle
101
+ // - uint16_t offset
102
+ // - bool need_rsp
103
+ // - bool is_prep
104
+ // - uint16_t len
105
+ // - uint8_t *value
106
+ //
99
107
case ESP_GATTS_WRITE_EVT: {
100
108
if (param->write .handle == m_handle) {
101
109
setValue (param->write .value , param->write .len );
102
110
esp_gatt_rsp_t rsp;
103
- rsp.attr_value .len = getLength ();
104
- rsp.attr_value .handle = m_handle;
105
- rsp.attr_value .offset = 0 ;
111
+ rsp.attr_value .len = getLength ();
112
+ rsp.attr_value .handle = m_handle;
113
+ rsp.attr_value .offset = 0 ;
106
114
rsp.attr_value .auth_req = ESP_GATT_AUTH_REQ_NONE;
107
115
memcpy (rsp.attr_value .value , getValue (), rsp.attr_value .len );
108
116
esp_err_t errRc = ::esp_ble_gatts_send_response (
@@ -117,23 +125,23 @@ void BLECharacteristic::handleGATTServerEvent(
117
125
// ESP_GATTS_READ_EVT - A request to read the value of a characteristic has arrived.
118
126
//
119
127
// read:
120
- // - uint16_t conn_id
121
- // - uint32_t trans_id
128
+ // - uint16_t conn_id
129
+ // - uint32_t trans_id
122
130
// - esp_bd_addr_t bda
123
- // - uint16_t handle
124
- // - uint16_t offset
125
- // - bool is_long
126
- // - bool need_rsp
131
+ // - uint16_t handle
132
+ // - uint16_t offset
133
+ // - bool is_long
134
+ // - bool need_rsp
127
135
//
128
136
case ESP_GATTS_READ_EVT: {
129
- ESP_LOGD (LOG_TAG, " - Testing: %d == %d " , param->read .handle , m_handle);
137
+ ESP_LOGD (LOG_TAG, " - Testing: 0x%.2x == 0x%.2x " , param->read .handle , m_handle);
130
138
if (param->read .handle == m_handle) {
131
139
ESP_LOGD (LOG_TAG, " Sending a response (esp_ble_gatts_send_response)" );
132
140
if (param->read .need_rsp ) {
133
141
esp_gatt_rsp_t rsp;
134
- rsp.attr_value .len = getLength ();
135
- rsp.attr_value .handle = param->read .handle ;
136
- rsp.attr_value .offset = 0 ;
142
+ rsp.attr_value .len = getLength ();
143
+ rsp.attr_value .handle = param->read .handle ;
144
+ rsp.attr_value .offset = 0 ;
137
145
rsp.attr_value .auth_req = ESP_GATT_AUTH_REQ_NONE;
138
146
memcpy (rsp.attr_value .value , getValue (), rsp.attr_value .len );
139
147
esp_err_t errRc = ::esp_ble_gatts_send_response (
@@ -145,10 +153,21 @@ void BLECharacteristic::handleGATTServerEvent(
145
153
} // ESP_GATTS_READ_EVT
146
154
break ;
147
155
} // ESP_GATTS_READ_EVT
156
+
148
157
default : {
149
158
break ;
150
- }
151
- }// switch event
159
+ } // default
160
+
161
+ } // switch event
162
+
163
+ // Give each of the descriptors associated with this characteristic the opportunity to handle the
164
+ // event.
165
+ BLEDescriptor *pDescriptor = m_descriptorMap.getFirst ();
166
+ while (pDescriptor != nullptr ) {
167
+ pDescriptor->handleGATTServerEvent (event, gatts_if, param);
168
+ pDescriptor = m_descriptorMap.getNext ();
169
+ }
170
+
152
171
} // handleGATTServerEvent
153
172
154
173
@@ -158,7 +177,7 @@ void BLECharacteristic::handleGATTServerEvent(
158
177
* @return N/A
159
178
*/
160
179
void BLECharacteristic::setBroadcastProperty (bool value) {
161
- ESP_LOGD (LOG_TAG, " setBroadcastProperty(%d)" , value);
180
+ // ESP_LOGD(LOG_TAG, "setBroadcastProperty(%d)", value);
162
181
if (value) {
163
182
m_properties |= ESP_GATT_CHAR_PROP_BIT_BROADCAST;
164
183
} else {
@@ -175,7 +194,7 @@ void BLECharacteristic::setHandle(uint16_t handle) {
175
194
176
195
177
196
void BLECharacteristic::setIndicateProperty (bool value) {
178
- ESP_LOGD (LOG_TAG, " setIndicateProperty(%d)" , value);
197
+ // ESP_LOGD(LOG_TAG, "setIndicateProperty(%d)", value);
179
198
if (value) {
180
199
m_properties |= ESP_GATT_CHAR_PROP_BIT_INDICATE;
181
200
} else {
@@ -185,7 +204,7 @@ void BLECharacteristic::setIndicateProperty(bool value) {
185
204
186
205
187
206
void BLECharacteristic::setNotifyProperty (bool value) {
188
- ESP_LOGD (LOG_TAG, " setNotifyProperty(%d)" , value);
207
+ // ESP_LOGD(LOG_TAG, "setNotifyProperty(%d)", value);
189
208
if (value) {
190
209
m_properties |= ESP_GATT_CHAR_PROP_BIT_NOTIFY;
191
210
} else {
@@ -195,7 +214,7 @@ void BLECharacteristic::setNotifyProperty(bool value) {
195
214
196
215
197
216
void BLECharacteristic::setReadProperty (bool value) {
198
- ESP_LOGD (LOG_TAG, " setReadProperty(%d)" , value);
217
+ // ESP_LOGD(LOG_TAG, "setReadProperty(%d)", value);
199
218
if (value) {
200
219
m_properties |= ESP_GATT_CHAR_PROP_BIT_READ;
201
220
} else {
@@ -225,7 +244,7 @@ void BLECharacteristic::setValue(std::string value) {
225
244
226
245
227
246
void BLECharacteristic::setWriteNoResponseProperty (bool value) {
228
- ESP_LOGD (LOG_TAG, " setWriteNoResponseProperty(%d)" , value);
247
+ // ESP_LOGD(LOG_TAG, "setWriteNoResponseProperty(%d)", value);
229
248
if (value) {
230
249
m_properties |= ESP_GATT_CHAR_PROP_BIT_WRITE_NR;
231
250
} else {
@@ -235,7 +254,7 @@ void BLECharacteristic::setWriteNoResponseProperty(bool value) {
235
254
236
255
237
256
void BLECharacteristic::setWriteProperty (bool value) {
238
- ESP_LOGD (LOG_TAG, " setWriteProperty(%d)" , value);
257
+ // ESP_LOGD(LOG_TAG, "setWriteProperty(%d)", value);
239
258
if (value) {
240
259
m_properties |= ESP_GATT_CHAR_PROP_BIT_WRITE;
241
260
} else {
@@ -252,17 +271,63 @@ std::string BLECharacteristic::toString() {
252
271
std::stringstream stringstream;
253
272
stringstream << std::hex << std::setfill (' 0' );
254
273
stringstream << " UUID: " << m_bleUUID.toString () + " , handle: 0x" << std::setw (2 ) << m_handle;
255
- stringstream <<
256
- ((m_properties & ESP_GATT_CHAR_PROP_BIT_READ)?" Read " :" " ) <<
257
- ((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE)?" Write " :" " ) <<
258
- ((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?" WriteNoResponse " :" " ) <<
259
- ((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?" Broadcast " :" " ) <<
260
- ((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?" Notify " :" " ) <<
261
- ((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE)?" Indicate " :" " ) <<
262
- " \n " ;
274
+ stringstream << " " <<
275
+ ((m_properties & ESP_GATT_CHAR_PROP_BIT_READ)?" Read " :" " ) <<
276
+ ((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE)?" Write " :" " ) <<
277
+ ((m_properties & ESP_GATT_CHAR_PROP_BIT_WRITE_NR)?" WriteNoResponse " :" " ) <<
278
+ ((m_properties & ESP_GATT_CHAR_PROP_BIT_BROADCAST)?" Broadcast " :" " ) <<
279
+ ((m_properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY)?" Notify " :" " ) <<
280
+ ((m_properties & ESP_GATT_CHAR_PROP_BIT_INDICATE)?" Indicate " :" " );
263
281
return stringstream.str ();
264
282
} // toString
265
283
266
284
BLEService* BLECharacteristic::getService () {
267
285
return m_pService;
268
286
}
287
+
288
+
289
+ /* *
290
+ * @brief Register a new characteristic with the ESP runtime.
291
+ * @param [in] pService The service with which to associate this characteristic.
292
+ */
293
+ void BLECharacteristic::executeCreate (BLEService* pService) {
294
+ ESP_LOGD (LOG_TAG, " >> executeCreate()" );
295
+
296
+ if (m_handle != 0 ) {
297
+ ESP_LOGE (LOG_TAG, " Characteristic already has a handle." );
298
+ return ;
299
+ }
300
+
301
+ m_pService = pService; // Save the service for this characteristic.
302
+
303
+ ESP_LOGD (LOG_TAG, " Registering characteristic (esp_ble_gatts_add_char): uuid: %s, service: %s" ,
304
+ getUUID ().toString ().c_str (),
305
+ m_pService->toString ().c_str ());
306
+
307
+ // m_serializeMutex.take("addCharacteristic"); // Take the mutex, released by event ESP_GATTS_ADD_CHAR_EVT
308
+ esp_err_t errRc = ::esp_ble_gatts_add_char (
309
+ m_pService->getHandle (),
310
+ getUUID ().getNative (),
311
+ (esp_gatt_perm_t )(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE),
312
+ getProperties (),
313
+ &m_value,
314
+ NULL );
315
+
316
+ if (errRc != ESP_OK) {
317
+ ESP_LOGE (LOG_TAG, " << esp_ble_gatts_add_char: rc=%d %s" , errRc, espToString (errRc));
318
+ return ;
319
+ }
320
+
321
+ // Now that we have registered the characteristic, we must also register all the descriptors associated with this
322
+ // characteristic. We iterate through each of those and invoke the registration call to register them with the
323
+ // ESP environment.
324
+
325
+ BLEDescriptor *pDescriptor = m_descriptorMap.getFirst ();
326
+
327
+ while (pDescriptor != nullptr ) {
328
+ pDescriptor->executeCreate (this );
329
+ pDescriptor = m_descriptorMap.getNext ();
330
+ } // End while
331
+
332
+ ESP_LOGD (LOG_TAG, " << executeCreate()" );
333
+ } // executeCreate
0 commit comments