8000 Updated BLERemoteCharacteristic/Descriptor to expose a setAuth method… · cipherz/arduino-esp32@296c3a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 296c3a1

Browse files
committed
Updated BLERemoteCharacteristic/Descriptor to expose a setAuth method to allow tweaking the authentication request type for that remotecharacteristic/descriptor without the need to add auth on each read/write.
1 parent ec5afd0 commit 296c3a1

File tree

4 files changed

+47
-26
lines changed

4 files changed

+47
-26
lines changed

libraries/BLE/src/BLERemoteCharacteristic.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(
4040
m_pRemoteService = pRemoteService;
4141
m_notifyCallback = nullptr;
4242
m_rawData = nullptr;
43+
m_auth = ESP_GATT_AUTH_REQ_NONE;
4344

4445
retrieveDescriptors(); // Get the descriptors for this characteristic
4546
log_v("<< BLERemoteCharacteristic");
@@ -355,8 +356,8 @@ BLEUUID BLERemoteCharacteristic::getUUID() {
355356
* @brief Read an unsigned 16 bit value
356357
* @return The unsigned 16 bit value.
357358
*/
358-
uint16_t BLERemoteCharacteristic::readUInt16(esp_gatt_auth_req_t auth) {
359-
std::string value = readValue(auth);
359+
uint16_t BLERemoteCharacteristic::readUInt16() {
360+
std::string value = readValue();
360361
if (value.length() >= 2) {
361362
return *(uint16_t*)(value.data());
362363
}
@@ -368,8 +369,8 @@ uint16_t BLERemoteCharacteristic::readUInt16(esp_gatt_auth_req_t auth) {
368369
* @brief Read an unsigned 32 bit value.
369370
* @return the unsigned 32 bit value.
370371
*/
371-
uint32_t BLERemoteCharacteristic::readUInt32(esp_gatt_auth_req_t auth) {
372-
std::string value = readValue(auth);
372+
uint32_t BLERemoteCharacteristic::readUInt32() {
373+
std::string value = readValue();
373374
if (value.length() >= 4) {
374375
return *(uint32_t*)(value.data());
375376
}
@@ -381,8 +382,8 @@ uint32_t BLERemoteCharacteristic::readUInt32(esp_gatt_auth_req_t auth) {
381382
* @brief Read a byte value
382383
* @return The value as a byte
383384
*/
384-
uint8_t BLERemoteCharacteristic::readUInt8(esp_gatt_auth_req_t auth) {
385-
std::string value = readValue(auth);
385+
uint8_t BLERemoteCharacteristic::readUInt8() {
386+
std::string value = readValue();
386387
if (value.length() >= 1) {
387388
return (uint8_t)value[0];
388389
}
@@ -393,8 +394,8 @@ uint8_t BLERemoteCharacteristic::readUInt8(esp_gatt_auth_req_t auth) {
393394
* @brief Read a float value.
394395
* @return the float value.
395396
*/
396-
float BLERemoteCharacteristic::readFloat(esp_gatt_auth_req_t auth) {
397-
std::string value = readValue(auth);
397+
float BLERemoteCharacteristic::readFloat() {
398+
std::string value = readValue();
398399
if (value.length() >= 4) {
399400
return *(float*)(value.data());
400401
}
@@ -405,7 +406,7 @@ float BLERemoteCharacteristic::readFloat(esp_gatt_auth_req_t auth) {
405406
* @brief Read the value of the remote characteristic.
406407
* @return The value of the remote characteristic.
407408
*/
408-
std::string BLERemoteCharacteristic::readValue(esp_gatt_auth_req_t auth) {
409+
std::string BLERemoteCharacteristic::readValue() {
409410
log_v(">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle());
410411

411412
// Check to see that we are connected.
@@ -423,7 +424,7 @@ std::string BLERemoteCharacteristic::readValue(esp_gatt_auth_req_t auth) {
423424
m_pRemoteService->getClient()->getGattcIf(),
424425
m_pRemoteService->getClient()->getConnId(), // The connection ID to the BLE server
425426
getHandle(), // The handle of this characteristic
426-
auth); // Security
427+
m_auth); // Security
427428

428429
if (errRc != ESP_OK) {
429430
log_e("esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -531,8 +532,8 @@ std::string BLERemoteCharacteristic::toString() {
531532
* @param [in] response Do we expect a response?
532533
* @return N/A.
533534
*/
534-
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response, esp_gatt_auth_req_t auth) {
535-
writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response, auth);
535+
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
536+
writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response);
536537
} // writeValue
537538

538539

@@ -544,8 +545,8 @@ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response, es
544545
* @param [in] response Whether we require a response from the write.
545546
* @return N/A.
546547
*/
547-
void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response, esp_gatt_auth_req_t auth) {
548-
writeValue(&newValue, 1, response, auth);
548+
void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) {
549+
writeValue(&newValue, 1, response);
549550
} // writeValue
550551

551552

@@ -555,7 +556,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response, esp_ga
555556
* @param [in] length The length of the data in the data buffer.
556557
* @param [in] response Whether we require a response from the write.
557558
*/
558-
void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response, esp_gatt_auth_req_t auth) {
559+
void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) {
559560
// writeValue(std::string((char*)data, length), response);
560561
log_v(">> writeValue(), length: %d", length);
561562

@@ -574,7 +575,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp
574575
length,
575576
data,
576577
response?ESP_GATT_WRITE_TYPE_RSP:ESP_GATT_WRITE_TYPE_NO_RSP,
577-
auth
578+
m_auth
578579
);
579580

580581
if (errRc != ESP_OK) {
@@ -595,4 +596,12 @@ uint8_t* BLERemoteCharacteristic::readRawData() {
595596
return m_rawData;
596597
}
597598

599+
/**
600+
* @brief Set authentication request type for characteristic
601+
* @param [in] auth Authentication request type.
602+
*/
603+
void BLERemoteCharacteristic::setAuth(esp_gatt_auth_req_t auth) {
604+
m_auth = auth;
605+
}
606+
598607
#endif /* CONFIG_BT_ENABLED */

libraries/BLE/src/BLERemoteCharacteristic.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ class BLERemoteCharacteristic {
4141
std::map<std::string, BLERemoteDescriptor*>* getDescriptors();
4242
uint16_t getHandle();
4343
BLEUUID getUUID();
44-
std::string readValue(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
45-
uint8_t readUInt8(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
46-
uint16_t readUInt16(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
47-
uint32_t readUInt32(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
48-
float readFloat(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
44+
std::string readValue();
45+
uint8_t readUInt8();
46+
uint16_t readUInt16();
47+
uint32_t readUInt32();
48+
float readFloat();
4949
void registerForNotify(notify_callback _callback, bool notifications = true);
50-
void writeValue(uint8_t* data, size_t length, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
51-
void writeValue(std::string newValue, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
52-
void writeValue(uint8_t newValue, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE);
50+
void writeValue(uint8_t* data, size_t length, bool response = false);
51+
void writeValue(std::string newValue, bool response = false);
52+
void writeValue(uint8_t newValue, bool response = false);
5353
std::string toString();
5454
uint8_t* readRawData();
55+
void setAuth(esp_gatt_auth_req_t auth);
5556

5657
private:
5758
BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
@@ -69,6 +70,7 @@ class BLERemoteCharacteristic {
6970
// Private properties
7071
BLEUUID m_uuid;
7172
esp_gatt_char_prop_t m_charProp;
73+
esp_gatt_auth_req_t m_auth;
7274
uint16_t m_handle;
7375
BLERemoteService* m_pRemoteService;
7476
FreeRTOS::Semaphore m_semaphoreReadCharEvt = FreeRTOS::Semaphore("ReadCharEvt");

libraries/BLE/src/BLERemoteDescriptor.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ BLERemoteDescriptor::BLERemoteDescriptor(
1919
m_handle = handle;
2020
m_uuid = uuid;
2121
m_pRemoteCharacteristic = pRemoteCharacteristic;
22+
m_auth = ESP_GATT_AUTH_REQ_NONE;
2223
}
2324

2425

@@ -65,7 +66,7 @@ std::string BLERemoteDescriptor::readValue() {
6566
m_pRemoteCharacteristic->getRemoteService()->getClient()->getGattcIf(),
6667
m_pRemoteCharacteristic->getRemoteService()->getClient()->getConnId(), // The connection ID to the BLE server
6768
getHandle(), // The handle of this characteristic
68-
ESP_GATT_AUTH_REQ_NONE); // Security
69+
m_auth); // Security
6970

7071
if (errRc != ESP_OK) {
7172
log_e("esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -143,7 +144,7 @@ void BLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response
143144
length, // Data length
144145
data, // Data
145146
response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP,
146-
ESP_GATT_AUTH_REQ_NONE
147+
m_auth
147148
);
148149
if (errRc != ESP_OK) {
149150
log_e("esp_ble_gattc_write_char_descr: %d", errRc);
@@ -171,5 +172,12 @@ void BLERemoteDescriptor::writeValue(uint8_t newValue, bool response) {
171172
writeValue(&newValue, 1, response);
172173
} // writeValue
173174

175+
/**
176+
* @brief Set authentication request type for characteristic
177+
* @param [in] auth Authentication request type.
178+
*/
179+
void BLERemoteDescriptor::setAuth(esp_gatt_auth_req_t auth) {
180+
m_auth = auth;
181+
}
174182

175183
#endif /* CONFIG_BT_ENABLED */

libraries/BLE/src/BLERemoteDescriptor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class BLERemoteDescriptor {
3434
void writeValue(uint8_t* data, size_t length, bool response = false);
3535
void writeValue(std::string newValue, bool response = false);
3636
void writeValue(uint8_t newValue, bool response = false);
37+
void setAuth(esp_gatt_auth_req_t auth);
3738

3839

3940
private:
@@ -48,6 +49,7 @@ class BLERemoteDescriptor {
4849
std::string m_value; // Last received value of the descriptor.
4950
BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated.
5051
FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt");
52+
esp_gatt_auth_req_t m_auth;
5153

5254

5355
};

0 commit comments

Comments
 (0)
0