#include "BLERemoteCharacteristic.h"
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <esp_gattc_api.h>
#include <esp_log.h>
#include <esp_err.h>
#include <sstream>
#include "BLEUtils.h"
#include "GeneralUtils.h"
#include "BLERemoteDescriptor.h"
static const char* LOG_TAG = "BLERemoteCharacteristic";
BLERemoteCharacteristic::BLERemoteCharacteristic(
uint16_t handle,
BLEUUID uuid,
esp_gatt_char_prop_t charProp,
BLERemoteService* pRemoteService) {
ESP_LOGD(LOG_TAG, ">> BLERemoteCharacteristic: handle: %d 0x%d, uuid: %s", handle, handle, uuid.toString().c_str());
m_handle = handle;
m_uuid = uuid;
m_charProp = charProp;
m_pRemoteService = pRemoteService;
m_notifyCallback = nullptr;
retrieveDescriptors();
ESP_LOGD(LOG_TAG, "<< BLERemoteCharacteristic");
}
BLERemoteCharacteristic::~BLERemoteCharacteristic() {
removeDescriptors();
}
bool BLERemoteCharacteristic::canBroadcast() {
return (m_charProp & ESP_GATT_CHAR_PROP_BIT_BROADCAST) != 0;
}
bool BLERemoteCharacteristic::canIndicate() {
return (m_charProp & ESP_GATT_CHAR_PROP_BIT_INDICATE) != 0;
}
bool BLERemoteCharacteristic::canNotify() {
return (m_charProp & ESP_GATT_CHAR_PROP_BIT_NOTIFY) != 0;
}
bool BLERemoteCharacteristic::canRead() {
return (m_charProp & ESP_GATT_CHAR_PROP_BIT_READ) != 0;
}
bool BLERemoteCharacteristic::canWrite() {
return (m_charProp & ESP_GATT_CHAR_PROP_BIT_WRITE) != 0;
}
bool BLERemoteCharacteristic::canWriteNoResponse() {
return (m_charProp & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) != 0;
}
void BLERemoteCharacteristic::gattClientEventHandler(
esp_gattc_cb_event_t event,
esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t* evtParam) {
switch(event) {
case ESP_GATTC_NOTIFY_EVT: {
if (evtParam->notify.handle != getHandle()) {
break;
}
if (m_notifyCallback != nullptr) {
ESP_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s", toString().c_str());
m_notifyCallback(
this,
evtParam->notify.value,
evtParam->notify.value_len,
evtParam->notify.is_notify
);
}
break;
}
case ESP_GATTC_READ_CHAR_EVT: {
if (evtParam->read.handle != getHandle()) {
break;
}
if (evtParam->read.status == ESP_GATT_OK) {
m_value = std::string((char*)evtParam->read.value, evtParam->read.value_len);
} else {
m_value = "";
}
m_semaphoreReadCharEvt.give();
break;
}
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
if (evtParam->reg_for_notify.handle != getHandle()) {
break;
}
m_semaphoreRegForNotifyEvt.give();
break;
}
case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
if (evtParam->unreg_for_notify.handle != getHandle()) {
break;
}
m_semaphoreRegForNotifyEvt.give();
break;
}
case ESP_GATTC_WRITE_CHAR_EVT: {
if (evtParam->write.handle != getHandle()) {
break;
}
m_semaphoreWriteCharEvt.give();
break;
}
default: {
break;
}
}
};
void BLERemoteCharacteristic::retrieveDescriptors() {
ESP_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str());
removeDescriptors();
uint16_t offset = 0;
esp_gattc_descr_elem_t result;
while(1) {
uint16_t count = 1;
esp_gatt_status_t status = ::esp_ble_gattc_get_all_descr(
getRemoteService()->getClient()->getGattcIf(),
getRemoteService()->getClient()->getConnId(),
getHandle(),
&result,
&count,
offset
);
if (status == ESP_GATT_INVALID_OFFSET) {
break;
}
if (status != ESP_GATT_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_all_descr: %s", BLEUtils::gattStatusToString(status).c_str());
break;
}
if (count == 0) {
break;
}
ESP_LOGD(LOG_TAG, "Found a descriptor: Handle: %d, UUID: %s", result.handle, BLEUUID(result.uuid).toString().c_str());
BLERemoteDescriptor *pNewRemoteDescriptor = new BLERemoteDescriptor(
result.handle,
BLEUUID(result.uuid),
this
);
m_descriptorMap.insert(std::pair<std::string, BLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor));
offset++;
}
ESP_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", offset);
}
std::map<std::string, BLERemoteDescriptor *>* BLERemoteCharacteristic::getDescriptors() {
return &m_descriptorMap;
}
uint16_t BLERemoteCharacteristic::getHandle() {
return m_handle;
}
BLERemoteDescriptor* BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) {
ESP_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
std::string v = uuid.toString();
for (auto &myPair : m_descriptorMap) {
if (myPair.first == v) {
ESP_LOGD(LOG_TAG, "<< getDescriptor: found");
return myPair.second;
}
}
ESP_LOGD(LOG_TAG, "<< getDescriptor: Not found");
return nullptr;
}
BLERemoteService* BLERemoteCharacteristic::getRemoteService() {
return m_pRemoteService;
}
BLEUUID BLERemoteCharacteristic::getUUID() {
return m_uuid;
}
uint16_t BLERemoteCharacteristic::readUInt16(void) {
std::string value = readValue();
if (value.length() >= 2) {
return *(uint16_t*)(value.data());
}
return 0;
}
uint32_t BLERemoteCharacteristic::readUInt32(void) {
std::string value = readValue();
if (value.length() >= 4) {
return *(uint32_t*)(value.data());
}
return 0;
}
uint8_t BLERemoteCharacteristic::readUInt8(void) {
std::string value = readValue();
if (value.length() >= 1) {
return (uint8_t)value[0];
}
return 0;
}
std::string BLERemoteCharacteristic::readValue() {
ESP_LOGD(LOG_TAG, ">> readValue(): uuid: %s, handle: %d 0x%.2x", getUUID().toString().c_str(), getHandle(), getHandle());
m_semaphoreReadCharEvt.take("readValue");
esp_err_t errRc = ::esp_ble_gattc_read_char(
m_pRemoteService->getClient()->getGattcIf(),
m_pRemoteService->getClient()->getConnId(),
getHandle(),
ESP_GATT_AUTH_REQ_NONE);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gattc_read_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return "";
}
m_semaphoreReadCharEvt.wait("readValue");
ESP_LOGD(LOG_TAG, "<< readValue(): length: %d", m_value.length());
return m_value;
}
void BLERemoteCharacteristic::registerForNotify(
void (*notifyCallback)(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify)) {
ESP_LOGD(LOG_TAG, ">> registerForNotify(): %s", toString().c_str());
m_notifyCallback = notifyCallback;
m_semaphoreRegForNotifyEvt.take("registerForNotify");
if (notifyCallback != nullptr) {
esp_err_t errRc = ::esp_ble_gattc_register_for_notify(
m_pRemoteService->getClient()->getGattcIf(),
*m_pRemoteService->getClient()->getPeerAddress().getNative(),
getHandle()
);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gattc_register_for_notify: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
}
else {
esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify(
m_pRemoteService->getClient()->getGattcIf(),
*m_pRemoteService->getClient()->getPeerAddress().getNative(),
getHandle()
);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gattc_unregister_for_notify: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
}
}
m_semaphoreRegForNotifyEvt.wait("registerForNotify");
ESP_LOGD(LOG_TAG, "<< registerForNotify()");
}
void BLERemoteCharacteristic::removeDescriptors() {
for (auto &myPair : m_descriptorMap) {
m_descriptorMap.erase(myPair.first);
delete myPair.second;
}
m_descriptorMap.clear();
}
std::string BLERemoteCharacteristic::toString() {
std::ostringstream ss;
ss << "Characteristic: uuid: " << m_uuid.toString() <<
", handle: " << getHandle() << " 0x" << std::hex << getHandle() <<
", props: " << BLEUtils::characteristicPropertiesToString(m_charProp);
return ss.str();
}
void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
ESP_LOGD(LOG_TAG, ">> writeValue(), length: %d", newValue.length());
m_semaphoreWriteCharEvt.take("writeValue");
esp_err_t errRc = ::esp_ble_gattc_write_char(
m_pRemoteService->getClient()->getGattcIf(),
m_pRemoteService->getClient()->getConnId(),
getHandle(),
newValue.length(),
(uint8_t*)newValue.data(),
response?ESP_GATT_WRITE_TYPE_RSP:ESP_GATT_WRITE_TYPE_NO_RSP,
ESP_GATT_AUTH_REQ_NONE
);
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gattc_write_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
m_semaphoreWriteCharEvt.wait("writeValue");
ESP_LOGD(LOG_TAG, "<< writeValue");
}
void BLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) {
writeValue(std::string(reinterpret_cast<char*>(&newValue), 1), response);
}
void BLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) {
writeValue(std::string((char *)data, length), response);
}
#endif