From c2b37d95e0924640e2c278c7bf439503ecad9d5f Mon Sep 17 00:00:00 2001 From: lj Date: Mon, 20 Jan 2020 08:40:13 -0500 Subject: [PATCH] =?UTF-8?q?Updated=20BLERemoteCharacteristic=20to=20exposr?= =?UTF-8?q?e=20esp=5Fgatt=5Fauth=5Freq=5Ft=20parame=E2=80=A6=20(#3531)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Updated BLERemoteCharacteristic to exposre esp_gatt_auth_req_t parameter for readValue and writeValue. * 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 | 13 +++++++++++-- libraries/BLE/src/BLERemoteCharacteristic.h | 2 ++ libraries/BLE/src/BLERemoteDescriptor.cpp | 12 ++++++++++-- libraries/BLE/src/BLERemoteDescriptor.h | 2 ++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/libraries/BLE/src/BLERemoteCharacteristic.cpp b/libraries/BLE/src/BLERemoteCharacteristic.cpp index fe455919..6a541389 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"); @@ -423,7 +424,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 + m_auth); // Security if (errRc != ESP_OK) { log_e("esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc)); @@ -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, - ESP_GATT_AUTH_REQ_NONE + 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 1d0db1c3..5ba0f2c6 100644 --- a/libraries/BLE/src/BLERemoteCharacteristic.h +++ b/libraries/BLE/src/BLERemoteCharacteristic.h @@ -52,6 +52,7 @@ public: 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 @@ private: // 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 54e59759..ad506aae 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 7bbc48f1..29efe573 100644 --- a/libraries/BLE/src/BLERemoteDescriptor.h +++ b/libraries/BLE/src/BLERemoteDescriptor.h @@ -34,6 +34,7 @@ public: 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 @@ private: 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; };