8000 Sync - 2017-07-01 · programmer131/esp32-snippets@cc2c823 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc2c823

Browse files
committed
Sync - 2017-07-01
1 parent 7b5db7d commit cc2c823

File tree

6 files changed

+86
-27
lines changed

6 files changed

+86
-27
lines changed

cpp_utils/BLECharacteristic.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
static char LOG_TAG[] = "BLECharacteristic";
2020

21+
#define NULL_HANDLE (0xffff)
22+
2123
extern "C" {
2224
char *espToString(esp_err_t value);
2325
}
@@ -32,7 +34,7 @@ BLECharacteristic::BLECharacteristic(BLEUUID uuid, uint32_t properties) {
3234
m_value.attr_value = (uint8_t *)malloc(ESP_GATT_MAX_ATTR_LEN); // Allocate storage for the value
3335
m_value.attr_len = 0; // Initial length of actual data is none.
3436
m_value.attr_max_len = ESP_GATT_MAX_ATTR_LEN; // Maximum length of data.
35-
m_handle = -1;
37+
m_handle = NULL_HANDLE;
3638
m_properties = 0;
3739

3840
setBroadcastProperty((properties & PROPERTY_BROADCAST) !=0);
@@ -70,7 +72,7 @@ void BLECharacteristic::addDescriptor(BLEDescriptor* pDescriptor) {
7072
void BLECharacteristic::executeCreate(BLEService* pService) {
7173
ESP_LOGD(LOG_TAG, ">> executeCreate()");
7274

73-
if (m_handle != 0) {
75+
if (m_handle != NULL_HANDLE) {
7476
ESP_LOGE(LOG_TAG, "Characteristic already has a handle.");
7577
return;
7678
}
@@ -188,12 +190,11 @@ void BLECharacteristic::handleGATTServerEvent(
188190
if (param->write.handle == m_handle) {
189191
setValue(param->write.value, param->write.len);
190192

191-
ESP_LOGD(LOG_TAG, " - Write event: New value: handle: %.2x, uuid: %s",
193+
ESP_LOGD(LOG_TAG, " - Response to write event: New value: handle: %.2x, uuid: %s",
192194
getHandle(), getUUID().toString().c_str());
193195

194196
char *pHexData = BLEUtils::buildHexData(nullptr, param->write.value, param->write.len);
195-
BLEUtils::buildHexData((uint8_t*)pHexData, param->write.value, param->write.len);
196-
ESP_LOGD(LOG_TAG, " - Data: %d %s", param->write.len, pHexData);
197+
ESP_LOGD(LOG_TAG, " - Data: length: %d, data: %s", param->write.len, pHexData);
197198
free(pHexData);
198199

199200
if (param->write.need_rsp) {
@@ -212,7 +213,7 @@ void BLECharacteristic::handleGATTServerEvent(
212213
}
213214
} // Response needed
214215

215-
//onWrite(); // Invoke the onWrite callback handler.
216+
onWrite(); // Invoke the onWrite callback handler.
216217
} // Match on handles.
217218
break;
218219
} // ESP_GATTS_WRITE_EVT
@@ -233,7 +234,7 @@ void BLECharacteristic::handleGATTServerEvent(
233234
ESP_LOGD(LOG_TAG, "- Testing: 0x%.2x == 0x%.2x", param->read.handle, m_handle);
234235
if (param->read.handle == m_handle) {
235236

236-
//onRead(); // Invoke the read callback.
237+
onRead(); // Invoke the read callback.
237238

238239
// Here's an interesting thing. The read request has the option of saying whether we need a response
239240
// or not. What would it "mean" to receive a read request and NOT send a response back? That feels like
@@ -246,6 +247,11 @@ void BLECharacteristic::handleGATTServerEvent(
246247
rsp.attr_value.offset = 0;
247248
rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
248249
memcpy(rsp.attr_value.value, getValue(), rsp.attr_value.len);
250+
251+
char *pHexData = BLEUtils::buildHexData(nullptr, rsp.attr_value.value, rsp.attr_value.len);
252+
ESP_LOGD(LOG_TAG, " - Data: length: %d, data: %s", param->write.len, pHexData);
253+
free(pHexData);
254+
249255
esp_err_t errRc = ::esp_ble_gatts_send_response(
250256
gatts_if, param->read.conn_id,
251257
param->read.trans_id,
@@ -281,21 +287,16 @@ void BLECharacteristic::handleGATTServerEvent(
281287
*/
282288
void BLECharacteristic::indicate() {
283289

284-
/*
285290
char *pHexData = BLEUtils::buildHexData(nullptr, getValue(), getLength());
286291
ESP_LOGD(LOG_TAG, ">> indicate: length: %d, data: [%s]", getLength(), pHexData );
287-
ESP_LOGD(LOG_TAG, "A");
288292
free(pHexData);
289-
*/
293+
290294
assert(getService() != nullptr);
291-
ESP_LOGD(LOG_TAG, "A2");
292295
assert(getService()->getServer() != nullptr);
293-
ESP_LOGD(LOG_TAG, "B");
294296
esp_err_t errRc = ::esp_ble_gatts_send_indicate(
295297
getService()->getServer()->getGattsIf(),
296298
getService()->getServer()->getConnId(),
297299
getHandle(), getLength(), getValue(), false);
298-
ESP_LOGD(LOG_TAG, "C");
299300
if (errRc != ESP_OK) {
300301
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_send_indicate: rc=%d %s", errRc, espToString(errRc));
301302
return;

cpp_utils/BLEDescriptor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static char LOG_TAG[] = "BLEDescriptor";
2020
extern "C" {
2121
char *espToString(esp_err_t value);
2222
}
23-
23+
#define NULL_HANDLE (0xffff)
2424
/**
2525
* @brief BLEDescriptor constructor.
2626
*/
@@ -29,7 +29,7 @@ BLEDescriptor::BLEDescriptor(BLEUUID uuid) {
2929
m_value.attr_value = (uint8_t *)malloc(ESP_GATT_MAX_ATTR_LEN); // Allocate storage for the value.
3030
m_value.attr_len = 0;
3131
m_value.attr_max_len = ESP_GATT_MAX_ATTR_LEN;
32-
m_handle = -1;
32+
m_handle = NULL_HANDLE;
3333
m_pCharacteristic = nullptr; // No initial characteristic.
3434

3535
} // BLEDescriptor
@@ -50,7 +50,7 @@ BLEDescriptor::~BLEDescriptor() {
5050
void BLEDescriptor::executeCreate(BLECharacteristic* pCharacteristic) {
5151
ESP_LOGD(LOG_TAG, ">> executeCreate(): %s", toString().c_str());
5252

53-
if (m_handle != 0) {
53+
if (m_handle != NULL_HANDLE) {
5454
ESP_LOGE(LOG_TAG, "Descriptor already has a handle.");
5555
return;
5656
}

cpp_utils/BLEServer.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ BLEServer::BLEServer() {
3737
m_connId = -1;
3838
m_serializeMutex.setName("BLEServer");
3939
BLE::m_bleServer = this;
40+
createApp(0);
4041
} // BLEServer
4142

4243

@@ -124,7 +125,7 @@ void BLEServer::handleGATTServerEvent(
124125
// - bool is_connected
125126
case ESP_GATTS_CONNECT_EVT: {
126127
m_connId = param->connect.conn_id; // Save the connection id.
127-
//onConnection(); // Invoke the connection handler (may be over-ridden)
128+
onConnect(); // Invoke the connection handler (may be over-ridden)
128129
break;
129130
} // ESP_GATTS_CONNECT_EVT
130131

@@ -193,6 +194,7 @@ void BLEServer::handleGATTServerEvent(
193194

194195
// ESP_GATTS_DISCONNECT_EVT
195196
case ESP_GATTS_DISCONNECT_EVT: {
197+
onDisconnect();
196198
startAdvertising();
197199
break;
198200
} // ESP_GATTS_DISCONNECT_EVT
@@ -262,9 +264,14 @@ uint16_t BLEServer::getGattsIf() {
262264
return m_gatts_if;
263265
}
264266

265-
void BLEServer::onConnection() {
266-
ESP_LOGD(LOG_TAG, ">> onConnection: Default");
267-
ESP_LOGD(LOG_TAG, "<< onConnection");
267+
void BLEServer::onConnect() {
268+
ESP_LOGD(LOG_TAG, ">> onConnect: Default");
269+
ESP_LOGD(LOG_TAG, "<< onConnect");
270+
}
271+
272+
void BLEServer::onDisconnect() {
273+
ESP_LOGD(LOG_TAG, ">> onDisconnect: Default");
274+
ESP_LOGD(LOG_TAG, "<< onDisconnect");
268275
}
269276

270277
void BLEServer::createApp(uint16_t appId) {

cpp_utils/BLEServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class BLEServer {
3535
void startAdvertising();
3636
void createApp(uint16_t appId);
3737

38-
virtual void onConnection();
38+
virtual void onConnect();
39+
virtual void onDisconnect();
3940

4041
private:
4142
esp_ble_adv_data_t m_adv_data;

cpp_utils/BLEService.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ extern "C" {
2525
char *espToString(esp_err_t value);
2626
}
2727

28+
#define NULL_HANDLE (0xffff)
29+
2830
static char LOG_TAG[] = "BLEService"; // Tag for logging.
2931

3032
/**
@@ -33,7 +35,7 @@ static char LOG_TAG[] = "BLEService"; // Tag for logging.
3335
*/
3436
BLEService::BLEService(BLEUUID uuid) {
3537
m_uuid = uuid;
36-
m_handle = 0;
38+
m_handle = NULL_HANDLE;
3739
m_pServer = nullptr;
3840
m_serializeMutex.setName("BLEService");
3941
m_lastCreatedCharacteristic = nullptr;
@@ -132,6 +134,10 @@ void BLEService::start() {
132134
*/
133135
void BLEService::setHandle(uint16_t handle) {
134136
ESP_LOGD(LOG_TAG, ">> setHandle(0x%.2x)", handle);
137+
if (m_handle != NULL_HANDLE) {
138+
ESP_LOGE(LOG_TAG, "Handle is already set %.2x", m_handle);
139+
return;
140+
}
135141
m_handle = handle;
136142
ESP_LOGD(LOG_TAG, "<< setHandle()");
137143
} // setHandle
@@ -188,6 +194,10 @@ BLECharacteristic* BLEService::createCharacteristic(
188194
return pCharacteristic;
189195
} // createCharacteristic
190196

197+
198+
/**
199+
* @brief Handle a GATTS server event.
200+
*/
191201
void BLEService::handleGATTServerEvent(
192202
esp_gatts_cb_event_t event,
193203
esp_gatt_if_t gatts_if,

cpp_utils/BLEUUID.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,67 @@
88
#include <string.h>
99
#include <sstream>
1010
#include <iomanip>
11+
#include <stdio.h>
1112
#include "BLEUUID.h"
1213
static char TAG[] = "BLEUUID";
1314

15+
16+
/**
17+
* @brief Create a UUID from a string.
18+
* Create a UUID from a string. There will be two possible stories here. Either the string represents
19+
* a binary data field or the string represents a hex encoding of a UUID.
20+
* For the hex encoding, here is an example:
21+
* "beb5483e-36e1-4688-b7f5-ea07361b26a8"
22+
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
23+
* This has a length of 36 characters. We need to parse this into 16 bytes
24+
* @param [in] value The string to build a UUID from.
25+
*/
1426
BLEUUID::BLEUUID(std::string value) {
27+
m_valueSet = true;
1528
if (value.length() == 2) {
1629
m_uuid.len = ESP_UUID_LEN_16;
1730
m_uuid.uuid.uuid16 = value[0] | (value[1] << 8);
18-
m_valueSet = true;
1931
} else if (value.length() == 4) {
2032
m_uuid.len = ESP_UUID_LEN_32;
2133
m_uuid.uuid.uuid32 = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24);
22-
m_valueSet = true;
2334
} else if (value.length() == 16) {
2435
m_uuid.len = ESP_UUID_LEN_128;
2536
memcpy(m_uuid.uuid.uuid128, value.data(), 16);
26-
m_valueSet = true;
27-
} else {
37+
} else if (value.length() == 36) {
38+
// If the length of the string is 36 bytes then we will assume it is a long hex string in
39+
// UUID format.
40+
m_uuid.len = ESP_UUID_LEN_128;
41+
int vals[16];
42+
sscanf(value.c_str(), "%2x%2x%2x%2x-%2x%2x-%2x%2x-%2x%2x-%2x%2x%2x%2x%2x%2x",
43+
&vals[0],
44+
&vals[1],
45+
&vals[2],
46+
&vals[3],
47+
&vals[4],
48+
&vals[5],
49+
&vals[6],
50+
&vals[7],
51+
&vals[8],
52+
&vals[9],
53+
&vals[10],
54+
&vals[11],
55+
&vals[12],
56+
&vals[13],
57+
&vals[14],
58+
&vals[15]
59+
);
60+
61+
int i;
62+
for (i=0; i<16; i++) {
63+
m_uuid.uuid.uuid128[i] = vals[i];
64+
}
65+
}
66+
else {
2867
ESP_LOGE(TAG, "ERROR: UUID value not 2, 4 or 16 bytes");
2968
m_valueSet = false;
3069
}
31-
}
70+
} //BLEUUID(std::string)
71+
3272

3373
BLEUUID::BLEUUID(uint16_t uuid) {
3474
m_uuid.len = ESP_UUID_LEN_16;

0 commit comments

Comments
 (0)
0