From ec5afd0ba8b2f33f896354b3548d884356043b2f Mon Sep 17 00:00:00 2001 From: lj Date: Fri, 29 Nov 2019 15:06:26 -0500 Subject: [PATCH 1/2] Updated BLERemoteCharacteristic to exposre esp_gatt_auth_req_t parameter for readValue and writeValue. --- libraries/BLE/src/BLERemoteCharacteristic.cpp | 32 +++++++++---------- libraries/BLE/src/BLERemoteCharacteristic.h | 16 +++++----- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/libraries/BLE/src/BLERemoteCharacteristic.cpp b/libraries/BLE/src/BLERemoteCharacteristic.cpp index fe4559199bd..046ec416dd7 100644 --- a/libraries/BLE/src/BLERemoteCharacteristic.cpp +++ b/libraries/BLE/src/BLERemoteCharacteristic.cpp @@ -355,8 +355,8 @@ BLEUUID BLERemoteCharacteristic::getUUID() { * @brief Read an unsigned 16 bit value * @return The unsigned 16 bit value. */ -uint16_t BLERemoteCharacteristic::readUInt16() { - std::string value = readValue(); +uint16_t BLERemoteCharacteristic::readUInt16(esp_gatt_auth_req_t auth) { + std::string value = readValue(auth); if (value.length() >= 2) { return *(uint16_t*)(value.data()); } @@ -368,8 +368,8 @@ uint16_t BLERemoteCharacteristic::readUInt16() { * @brief Read an unsigned 32 bit value. * @return the unsigned 32 bit value. */ -uint32_t BLERemoteCharacteristic::readUInt32() { - std::string value = readValue(); +uint32_t BLERemoteCharacteristic::readUInt32(esp_gatt_auth_req_t auth) { + std::string value = readValue(auth); if (value.length() >= 4) { return *(uint32_t*)(value.data()); } @@ -381,8 +381,8 @@ uint32_t BLERemoteCharacteristic::readUInt32() { * @brief Read a byte value * @return The value as a byte */ -uint8_t BLERemoteCharacteristic::readUInt8() { - std::string value = readValue(); +uint8_t BLERemoteCharacteristic::readUInt8(esp_gatt_auth_req_t auth) { + std::string value = readValue(auth); if (value.length() >= 1) { return (uint8_t)value[0]; } @@ -393,8 +393,8 @@ uint8_t BLERemoteCharacteristic::readUInt8() { * @brief Read a float value. * @return the float value. */ -float BLERemoteCharacteristic::readFloat() { - std::string value = readValue(); +float BLERemoteCharacteristic::readFloat(esp_gatt_auth_req_t auth) { + std::string value = readValue(auth); if (value.length() >= 4) { return *(float*)(value.data()); } @@ -405,7 +405,7 @@ float BLERemoteCharacteristic::readFloat() { * @brief Read the value of the remote characteristic. * @return The value of the remote characteristic. */ -std::string BLERemoteCharacteristic::readValue() { +std::string BLERemoteCharacteristic::readValue(esp_gatt_auth_req_t auth) { log_v(">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle()); // Check to see that we are connected. @@ -423,7 +423,7 @@ std::string BLERemoteCharacteristic::readValue() { m_pRemoteService->getClient()->getGattcIf(), m_pRemoteService->getClient()->getConnId(), // The connection ID to the BLE server getHandle(), // The handle of this characteristic - ESP_GATT_AUTH_REQ_NONE); // Security + auth); // Security if (errRc != ESP_OK) { log_e("esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); @@ -531,8 +531,8 @@ std::string BLERemoteCharacteristic::toString() { * @param [in] response Do we expect a response? * @return N/A. */ -void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) { - writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response); +void BLERemoteCharacteristic::writeValue(std::string newValue, bool response, esp_gatt_auth_req_t auth) { + writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response, auth); } // writeValue @@ -544,8 +544,8 @@ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) { * @param [in] response Whether we require a response from the write. * @return N/A. */ -void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) { - writeValue(&newValue, 1, response); +void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response, esp_gatt_auth_req_t auth) { + writeValue(&newValue, 1, response, auth); } // writeValue @@ -555,7 +555,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) { * @param [in] length The length of the data in the data buffer. * @param [in] response Whether we require a response from the write. */ -void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) { +void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response, esp_gatt_auth_req_t auth) { // writeValue(std::string((char*)data, length), response); log_v(">> writeValue(), length: %d", length); @@ -574,7 +574,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp length, data, response?ESP_GATT_WRITE_TYPE_RSP:ESP_GATT_WRITE_TYPE_NO_RSP, - ESP_GATT_AUTH_REQ_NONE + auth ); if (errRc != ESP_OK) { diff --git a/libraries/BLE/src/BLERemoteCharacteristic.h b/libraries/BLE/src/BLERemoteCharacteristic.h index 1d0db1c379d..0123e7d79fe 100644 --- a/libraries/BLE/src/BLERemoteCharacteristic.h +++ b/libraries/BLE/src/BLERemoteCharacteristic.h @@ -41,15 +41,15 @@ class BLERemoteCharacteristic { std::map* getDescriptors(); uint16_t getHandle(); BLEUUID getUUID(); - std::string readValue(); - uint8_t readUInt8(); - uint16_t readUInt16(); - uint32_t readUInt32(); - float readFloat(); + std::string readValue(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + uint8_t readUInt8(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + uint16_t readUInt16(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + uint32_t readUInt32(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + float readFloat(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); void registerForNotify(notify_callback _callback, bool notifications = true); - void writeValue(uint8_t* data, size_t length, bool response = false); - void writeValue(std::string newValue, bool response = false); - void writeValue(uint8_t newValue, bool response = false); + void writeValue(uint8_t* data, size_t length, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + void writeValue(std::string newValue, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + void writeValue(uint8_t newValue, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); std::string toString(); uint8_t* readRawData(); From 296c3a11eddfcc4725c6126cba64923ca65375fe Mon Sep 17 00:00:00 2001 From: lj Date: Sat, 30 Nov 2019 15:02:30 -0500 Subject: [PATCH 2/2] 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. --- libraries/BLE/src/BLERemoteCharacteristic.cpp | 41 +++++++++++-------- libraries/BLE/src/BLERemoteCharacteristic.h | 18 ++++---- libraries/BLE/src/BLERemoteDescriptor.cpp | 12 +++++- libraries/BLE/src/BLERemoteDescriptor.h | 2 + 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/libraries/BLE/src/BLERemoteCharacteristic.cpp b/libraries/BLE/src/BLERemoteCharacteristic.cpp index 046ec416dd7..6a5413893a1 100644 --- a/libraries/BLE/src/BLERemoteCharacteristic.cpp +++ b/libraries/BLE/src/BLERemoteCharacteristic.cpp @@ -40,6 +40,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic( m_pRemoteService = pRemoteService; m_notifyCallback = nullptr; m_rawData = nullptr; + m_auth = ESP_GATT_AUTH_REQ_NONE; retrieveDescriptors(); // Get the descriptors for this characteristic log_v("<< BLERemoteCharacteristic"); @@ -355,8 +356,8 @@ BLEUUID BLERemoteCharacteristic::getUUID() { * @brief Read an unsigned 16 bit value * @return The unsigned 16 bit value. */ -uint16_t BLERemoteCharacteristic::readUInt16(esp_gatt_auth_req_t auth) { - std::string value = readValue(auth); +uint16_t BLERemoteCharacteristic::readUInt16() { + std::string value = readValue(); if (value.length() >= 2) { return *(uint16_t*)(value.data()); } @@ -368,8 +369,8 @@ uint16_t BLERemoteCharacteristic::readUInt16(esp_gatt_auth_req_t auth) { * @brief Read an unsigned 32 bit value. * @return the unsigned 32 bit value. */ -uint32_t BLERemoteCharacteristic::readUInt32(esp_gatt_auth_req_t auth) { - std::string value = readValue(auth); +uint32_t BLERemoteCharacteristic::readUInt32() { + std::string value = readValue(); if (value.length() >= 4) { return *(uint32_t*)(value.data()); } @@ -381,8 +382,8 @@ uint32_t BLERemoteCharacteristic::readUInt32(esp_gatt_auth_req_t auth) { * @brief Read a byte value * @return The value as a byte */ -uint8_t BLERemoteCharacteristic::readUInt8(esp_gatt_auth_req_t auth) { - std::string value = readValue(auth); +uint8_t BLERemoteCharacteristic::readUInt8() { + std::string value = readValue(); if (value.length() >= 1) { return (uint8_t)value[0]; } @@ -393,8 +394,8 @@ uint8_t BLERemoteCharacteristic::readUInt8(esp_gatt_auth_req_t auth) { * @brief Read a float value. * @return the float value. */ -float BLERemoteCharacteristic::readFloat(esp_gatt_auth_req_t auth) { - std::string value = readValue(auth); +float BLERemoteCharacteristic::readFloat() { + std::string value = readValue(); if (value.length() >= 4) { return *(float*)(value.data()); } @@ -405,7 +406,7 @@ float BLERemoteCharacteristic::readFloat(esp_gatt_auth_req_t auth) { * @brief Read the value of the remote characteristic. * @return The value of the remote characteristic. */ -std::string BLERemoteCharacteristic::readValue(esp_gatt_auth_req_t auth) { +std::string BLERemoteCharacteristic::readValue() { log_v(">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle()); // Check to see that we are connected. @@ -423,7 +424,7 @@ std::string BLERemoteCharacteristic::readValue(esp_gatt_auth_req_t auth) { m_pRemoteService->getClient()->getGattcIf(), m_pRemoteService->getClient()->getConnId(), // The connection ID to the BLE server getHandle(), // The handle of this characteristic - auth); // Security + m_auth); // Security if (errRc != ESP_OK) { log_e("esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); @@ -531,8 +532,8 @@ std::string BLERemoteCharacteristic::toString() { * @param [in] response Do we expect a response? * @return N/A. */ -void BLERemoteCharacteristic::writeValue(std::string newValue, bool response, esp_gatt_auth_req_t auth) { - writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response, auth); +void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) { + writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response); } // writeValue @@ -544,8 +545,8 @@ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response, es * @param [in] response Whether we require a response from the write. * @return N/A. */ -void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response, esp_gatt_auth_req_t auth) { - writeValue(&newValue, 1, response, auth); +void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) { + writeValue(&newValue, 1, response); } // writeValue @@ -555,7 +556,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response, esp_ga * @param [in] length The length of the data in the data buffer. * @param [in] response Whether we require a response from the write. */ -void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response, esp_gatt_auth_req_t auth) { +void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) { // writeValue(std::string((char*)data, length), response); log_v(">> writeValue(), length: %d", length); @@ -574,7 +575,7 @@ void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool resp length, data, response?ESP_GATT_WRITE_TYPE_RSP:ESP_GATT_WRITE_TYPE_NO_RSP, - auth + m_auth ); if (errRc != ESP_OK) { @@ -595,4 +596,12 @@ uint8_t* BLERemoteCharacteristic::readRawData() { return m_rawData; } +/** + * @brief Set authentication request type for characteristic + * @param [in] auth Authentication request type. + */ +void BLERemoteCharacteristic::setAuth(esp_gatt_auth_req_t auth) { + m_auth = auth; +} + #endif /* CONFIG_BT_ENABLED */ diff --git a/libraries/BLE/src/BLERemoteCharacteristic.h b/libraries/BLE/src/BLERemoteCharacteristic.h index 0123e7d79fe..5ba0f2c6381 100644 --- a/libraries/BLE/src/BLERemoteCharacteristic.h +++ b/libraries/BLE/src/BLERemoteCharacteristic.h @@ -41,17 +41,18 @@ class BLERemoteCharacteristic { std::map* getDescriptors(); uint16_t getHandle(); BLEUUID getUUID(); - std::string readValue(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); - uint8_t readUInt8(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); - uint16_t readUInt16(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); - uint32_t readUInt32(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); - float readFloat(esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + std::string readValue(); + uint8_t readUInt8(); + uint16_t readUInt16(); + uint32_t readUInt32(); + float readFloat(); void registerForNotify(notify_callback _callback, bool notifications = true); - void writeValue(uint8_t* data, size_t length, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); - void writeValue(std::string newValue, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); - void writeValue(uint8_t newValue, bool response = false, esp_gatt_auth_req_t auth = ESP_GATT_AUTH_REQ_NONE); + void writeValue(uint8_t* data, size_t length, bool response = false); + void writeValue(std::string newValue, bool response = false); + void writeValue(uint8_t newValue, bool response = false); std::string toString(); uint8_t* readRawData(); + void setAuth(esp_gatt_auth_req_t auth); private: BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService); @@ -69,6 +70,7 @@ class BLERemoteCharacteristic { // Private properties BLEUUID m_uuid; esp_gatt_char_prop_t m_charProp; + esp_gatt_auth_req_t m_auth; uint16_t m_handle; BLERemoteService* m_pRemoteService; FreeRTOS::Semaphore m_semaphoreReadCharEvt = FreeRTOS::Semaphore("ReadCharEvt"); diff --git a/libraries/BLE/src/BLERemoteDescriptor.cpp b/libraries/BLE/src/BLERemoteDescriptor.cpp index 54e59759a04..ad506aae9b3 100644 --- a/libraries/BLE/src/BLERemoteDescriptor.cpp +++ b/libraries/BLE/src/BLERemoteDescriptor.cpp @@ -19,6 +19,7 @@ BLERemoteDescriptor::BLERemoteDescriptor( m_handle = handle; m_uuid = uuid; m_pRemoteCharacteristic = pRemoteCharacteristic; + m_auth = ESP_GATT_AUTH_REQ_NONE; } @@ -65,7 +66,7 @@ std::string BLERemoteDescriptor::readValue() { m_pRemoteCharacteristic->getRemoteService()->getClient()->getGattcIf(), m_pRemoteCharacteristic->getRemoteService()->getClient()->getConnId(), // The connection ID to the BLE server getHandle(), // The handle of this characteristic - ESP_GATT_AUTH_REQ_NONE); // Security + m_auth); // Security if (errRc != ESP_OK) { 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 length, // Data length data, // Data response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, - ESP_GATT_AUTH_REQ_NONE + m_auth ); if (errRc != ESP_OK) { log_e("esp_ble_gattc_write_char_descr: %d", errRc); @@ -171,5 +172,12 @@ void BLERemoteDescriptor::writeValue(uint8_t newValue, bool response) { writeValue(&newValue, 1, response); } // writeValue +/** + * @brief Set authentication request type for characteristic + * @param [in] auth Authentication request type. + */ +void BLERemoteDescriptor::setAuth(esp_gatt_auth_req_t auth) { + m_auth = auth; +} #endif /* CONFIG_BT_ENABLED */ diff --git a/libraries/BLE/src/BLERemoteDescriptor.h b/libraries/BLE/src/BLERemoteDescriptor.h index 7bbc48f12d9..29efe573bfe 100644 --- a/libraries/BLE/src/BLERemoteDescriptor.h +++ b/libraries/BLE/src/BLERemoteDescriptor.h @@ -34,6 +34,7 @@ class BLERemoteDescriptor { void writeValue(uint8_t* data, size_t length, bool response = false); void writeValue(std::string newValue, bool response = false); void writeValue(uint8_t newValue, bool response = false); + void setAuth(esp_gatt_auth_req_t auth); private: @@ -48,6 +49,7 @@ class BLERemoteDescriptor { std::string m_value; // Last received value of the descriptor. BLERemoteCharacteristic* m_pRemoteCharacteristic; // Reference to the Remote characteristic of which this descriptor is associated. FreeRTOS::Semaphore m_semaphoreReadDescrEvt = FreeRTOS::Semaphore("ReadDescrEvt"); + esp_gatt_auth_req_t m_auth; };