2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLERemoteCharacteristic.cpp
|
|
|
|
*
|
|
|
|
* Created: on Jan 27 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLERemoteCharacteristic.cpp
|
|
|
|
*
|
|
|
|
* Created on: Mar 16, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLERemoteCharacteristic.h"
|
|
|
|
#include "NimBLEUtils.h"
|
|
|
|
#include "NimBLELog.h"
|
|
|
|
|
2022-01-15 04:45:24 +01:00
|
|
|
#include <climits>
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
static const char* LOG_TAG = "NimBLERemoteCharacteristic";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor.
|
|
|
|
* @param [in] reference to the service this characteristic belongs to.
|
|
|
|
* @param [in] ble_gatt_chr struct defined as:
|
|
|
|
* struct ble_gatt_chr {
|
|
|
|
* uint16_t def_handle;
|
|
|
|
* uint16_t val_handle;
|
|
|
|
* uint8_t properties;
|
|
|
|
* ble_uuid_any_t uuid;
|
|
|
|
* };
|
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
NimBLERemoteCharacteristic::NimBLERemoteCharacteristic(NimBLERemoteService *pRemoteService,
|
|
|
|
const struct ble_gatt_chr *chr)
|
|
|
|
{
|
2020-12-28 20:05:54 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> NimBLERemoteCharacteristic()");
|
2020-03-30 01:44:20 +02:00
|
|
|
switch (chr->uuid.u.type) {
|
2020-05-14 06:03:56 +02:00
|
|
|
case BLE_UUID_TYPE_16:
|
2020-03-30 01:44:20 +02:00
|
|
|
m_uuid = NimBLEUUID(chr->uuid.u16.value);
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
case BLE_UUID_TYPE_32:
|
2020-03-30 01:44:20 +02:00
|
|
|
m_uuid = NimBLEUUID(chr->uuid.u32.value);
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
case BLE_UUID_TYPE_128:
|
2020-03-30 01:44:20 +02:00
|
|
|
m_uuid = NimBLEUUID(const_cast<ble_uuid128_t*>(&chr->uuid.u128));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-05-30 04:49:46 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
m_handle = chr->val_handle;
|
|
|
|
m_defHandle = chr->def_handle;
|
2021-05-17 22:37:03 +02:00
|
|
|
m_endHandle = 0;
|
2020-05-23 18:27:32 +02:00
|
|
|
m_charProp = chr->properties;
|
|
|
|
m_pRemoteService = pRemoteService;
|
|
|
|
m_notifyCallback = nullptr;
|
2020-12-28 20:05:54 +01:00
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< NimBLERemoteCharacteristic(): %s", m_uuid.toString().c_str());
|
2020-03-30 01:44:20 +02:00
|
|
|
} // NimBLERemoteCharacteristic
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*@brief Destructor.
|
|
|
|
*/
|
|
|
|
NimBLERemoteCharacteristic::~NimBLERemoteCharacteristic() {
|
2020-06-22 06:07:01 +02:00
|
|
|
deleteDescriptors();
|
2020-03-30 01:44:20 +02:00
|
|
|
} // ~NimBLERemoteCharacteristic
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define BLE_GATT_CHR_PROP_BROADCAST 0x01
|
|
|
|
#define BLE_GATT_CHR_PROP_READ 0x02
|
|
|
|
#define BLE_GATT_CHR_PROP_WRITE_NO_RSP 0x04
|
|
|
|
#define BLE_GATT_CHR_PROP_WRITE 0x08
|
|
|
|
#define BLE_GATT_CHR_PROP_NOTIFY 0x10
|
|
|
|
#define BLE_GATT_CHR_PROP_INDICATE 0x20
|
|
|
|
#define BLE_GATT_CHR_PROP_AUTH_SIGN_WRITE 0x40
|
|
|
|
#define BLE_GATT_CHR_PROP_EXTENDED 0x80
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the characteristic support broadcasting?
|
|
|
|
* @return True if the characteristic supports broadcasting.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::canBroadcast() {
|
|
|
|
return (m_charProp & BLE_GATT_CHR_PROP_BROADCAST) != 0;
|
|
|
|
} // canBroadcast
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the characteristic support indications?
|
|
|
|
* @return True if the characteristic supports indications.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::canIndicate() {
|
|
|
|
return (m_charProp & BLE_GATT_CHR_PROP_INDICATE) != 0;
|
|
|
|
} // canIndicate
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the characteristic support notifications?
|
|
|
|
* @return True if the characteristic supports notifications.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::canNotify() {
|
|
|
|
return (m_charProp & BLE_GATT_CHR_PROP_NOTIFY) != 0;
|
|
|
|
} // canNotify
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the characteristic support reading?
|
|
|
|
* @return True if the characteristic supports reading.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::canRead() {
|
|
|
|
return (m_charProp & BLE_GATT_CHR_PROP_READ) != 0;
|
|
|
|
} // canRead
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the characteristic support writing?
|
|
|
|
* @return True if the characteristic supports writing.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::canWrite() {
|
|
|
|
return (m_charProp & BLE_GATT_CHR_PROP_WRITE) != 0;
|
|
|
|
} // canWrite
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Does the characteristic support writing with no response?
|
|
|
|
* @return True if the characteristic supports writing with no response.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::canWriteNoResponse() {
|
|
|
|
return (m_charProp & BLE_GATT_CHR_PROP_WRITE_NO_RSP) != 0;
|
|
|
|
} // canWriteNoResponse
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback used by the API when a descriptor is discovered or search complete.
|
|
|
|
*/
|
2020-05-14 06:03:56 +02:00
|
|
|
int NimBLERemoteCharacteristic::descriptorDiscCB(uint16_t conn_handle,
|
2020-03-30 01:44:20 +02:00
|
|
|
const struct ble_gatt_error *error,
|
2020-05-14 06:03:56 +02:00
|
|
|
uint16_t chr_val_handle,
|
2020-03-30 01:44:20 +02:00
|
|
|
const struct ble_gatt_dsc *dsc,
|
2020-05-14 06:03:56 +02:00
|
|
|
void *arg)
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2021-05-17 22:37:03 +02:00
|
|
|
int rc = error->status;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Descriptor Discovered >> status: %d handle: %d",
|
|
|
|
rc, (rc == 0) ? dsc->handle : -1);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
desc_filter_t *filter = (desc_filter_t*)arg;
|
|
|
|
const NimBLEUUID *uuid_filter = filter->uuid;
|
|
|
|
ble_task_data_t *pTaskData = (ble_task_data_t*)filter->task_data;
|
|
|
|
NimBLERemoteCharacteristic *characteristic = (NimBLERemoteCharacteristic*)pTaskData->pATT;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2021-05-17 22:37:03 +02:00
|
|
|
if (characteristic->getRemoteService()->getClient()->getConnId() != conn_handle){
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-05-17 22:37:03 +02:00
|
|
|
switch (rc) {
|
2020-03-30 01:44:20 +02:00
|
|
|
case 0: {
|
2021-05-17 22:37:03 +02:00
|
|
|
if (uuid_filter != nullptr) {
|
2024-07-13 04:42:53 +02:00
|
|
|
if (ble_uuid_cmp(uuid_filter->getBase(), &dsc->uuid.u) != 0) {
|
2020-05-23 18:27:32 +02:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
rc = BLE_HS_EDONE;
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 06:07:01 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLERemoteDescriptor* pNewRemoteDescriptor = new NimBLERemoteDescriptor(characteristic, dsc);
|
2020-05-18 04:21:35 +02:00
|
|
|
characteristic->m_descriptorVector.push_back(pNewRemoteDescriptor);
|
2020-03-30 01:44:20 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-05-23 18:27:32 +02:00
|
|
|
|
2021-05-17 22:37:03 +02:00
|
|
|
/* If rc == BLE_HS_EDONE, resume the task with a success error code and stop the discovery process.
|
2020-05-23 18:27:32 +02:00
|
|
|
* Else if rc == 0, just return 0 to continue the discovery until we get BLE_HS_EDONE.
|
2020-06-22 06:07:01 +02:00
|
|
|
* If we get any other error code tell the application to abort by returning non-zero in the rc.
|
2020-05-23 18:27:32 +02:00
|
|
|
*/
|
|
|
|
if (rc == BLE_HS_EDONE) {
|
2020-06-22 06:07:01 +02:00
|
|
|
pTaskData->rc = 0;
|
|
|
|
xTaskNotifyGive(pTaskData->task);
|
2020-05-23 18:27:32 +02:00
|
|
|
} else if(rc != 0) {
|
2020-06-22 06:07:01 +02:00
|
|
|
// Error; abort discovery.
|
|
|
|
pTaskData->rc = rc;
|
|
|
|
xTaskNotifyGive(pTaskData->task);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-06-22 06:07:01 +02:00
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG,"<< Descriptor Discovered. status: %d", pTaskData->rc);
|
2020-03-30 01:44:20 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
|
2021-05-17 22:37:03 +02:00
|
|
|
/**
|
|
|
|
* @brief callback from NimBLE when the next characteristic of the service is discovered.
|
|
|
|
*/
|
|
|
|
int NimBLERemoteCharacteristic::nextCharCB(uint16_t conn_handle,
|
|
|
|
const struct ble_gatt_error *error,
|
|
|
|
const struct ble_gatt_chr *chr, void *arg)
|
|
|
|
{
|
|
|
|
int rc = error->status;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Next Characteristic >> status: %d handle: %d",
|
|
|
|
rc, (rc == 0) ? chr->val_handle : -1);
|
|
|
|
|
|
|
|
ble_task_data_t *pTaskData = (ble_task_data_t*)arg;
|
|
|
|
NimBLERemoteCharacteristic *pChar = (NimBLERemoteCharacteristic*)pTaskData->pATT;
|
|
|
|
|
|
|
|
if (pChar->getRemoteService()->getClient()->getConnId() != conn_handle) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
pChar->m_endHandle = chr->def_handle - 1;
|
|
|
|
rc = BLE_HS_EDONE;
|
|
|
|
} else if (rc == BLE_HS_EDONE) {
|
|
|
|
pChar->m_endHandle = pChar->getRemoteService()->getEndHandle();
|
|
|
|
} else {
|
|
|
|
pTaskData->rc = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
xTaskNotifyGive(pTaskData->task);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Populate the descriptors (if any) for this characteristic.
|
|
|
|
* @param [in] the end handle of the characteristic, or the service, whichever comes first.
|
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filter) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str());
|
|
|
|
|
2021-05-17 22:37:03 +02:00
|
|
|
// If this is the last handle then there are no descriptors
|
|
|
|
if (m_handle == getRemoteService()->getEndHandle()) {
|
|
|
|
return true;
|
2021-01-14 02:04:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
int rc = 0;
|
2022-01-10 03:04:41 +01:00
|
|
|
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
|
|
|
|
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
|
2021-05-17 22:37:03 +02:00
|
|
|
|
|
|
|
// If we don't know the end handle of this characteristic retrieve the next one in the service
|
|
|
|
// The end handle is the next characteristic definition handle -1.
|
|
|
|
if (m_endHandle == 0) {
|
|
|
|
rc = ble_gattc_disc_all_chrs(getRemoteService()->getClient()->getConnId(),
|
|
|
|
m_handle,
|
|
|
|
getRemoteService()->getEndHandle(),
|
|
|
|
NimBLERemoteCharacteristic::nextCharCB,
|
|
|
|
&taskData);
|
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error getting end handle rc=%d", rc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-10 03:04:41 +01:00
|
|
|
#ifdef ulTaskNotifyValueClear
|
|
|
|
// Clear the task notification value to ensure we block
|
|
|
|
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
|
|
|
|
#endif
|
2021-05-17 22:37:03 +02:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
|
|
|
|
if (taskData.rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Could not retrieve end handle rc=%d", taskData.rc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 18:02:09 +01:00
|
|
|
if (m_handle == m_endHandle) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
desc_filter_t filter = {uuid_filter, &taskData};
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
rc = ble_gattc_disc_all_dscs(getRemoteService()->getClient()->getConnId(),
|
|
|
|
m_handle,
|
2021-05-17 22:37:03 +02:00
|
|
|
m_endHandle,
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLERemoteCharacteristic::descriptorDiscCB,
|
2020-05-23 18:27:32 +02:00
|
|
|
&filter);
|
2021-01-14 02:04:52 +01:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if (rc != 0) {
|
2021-05-17 22:37:03 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_dscs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
|
2020-03-30 01:44:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-01-10 03:04:41 +01:00
|
|
|
#ifdef ulTaskNotifyValueClear
|
|
|
|
// Clear the task notification value to ensure we block
|
|
|
|
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
|
|
|
|
#endif
|
2020-06-22 06:07:01 +02:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
|
2021-05-17 22:37:03 +02:00
|
|
|
if (taskData.rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Failed to retrieve descriptors; startHandle:%d endHandle:%d taskData.rc=%d",
|
|
|
|
m_handle, m_endHandle, taskData.rc);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", m_descriptorVector.size());
|
2021-05-17 22:37:03 +02:00
|
|
|
return (taskData.rc == 0);
|
2021-01-14 02:04:52 +01:00
|
|
|
} // retrieveDescriptors
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-23 18:27:32 +02:00
|
|
|
* @brief Get the descriptor instance with the given UUID that belongs to this characteristic.
|
|
|
|
* @param [in] uuid The UUID of the descriptor to find.
|
|
|
|
* @return The Remote descriptor (if present) or null if not present.
|
|
|
|
*/
|
|
|
|
NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUUID &uuid) {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
for(auto &it: m_descriptorVector) {
|
|
|
|
if(it->getUUID() == uuid) {
|
2020-12-28 23:40:01 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found the descriptor with uuid: %s", uuid.toString().c_str());
|
2020-05-23 18:27:32 +02:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t prev_size = m_descriptorVector.size();
|
|
|
|
if(retrieveDescriptors(&uuid)) {
|
|
|
|
if(m_descriptorVector.size() > prev_size) {
|
|
|
|
return m_descriptorVector.back();
|
|
|
|
}
|
2020-12-28 23:40:01 +01:00
|
|
|
|
2022-01-18 22:48:07 +01:00
|
|
|
// If the request was successful but 16/32 bit uuid not found
|
2020-12-28 23:40:01 +01:00
|
|
|
// try again with the 128 bit uuid.
|
|
|
|
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
|
|
|
|
uuid.bitSize() == BLE_UUID_TYPE_32)
|
|
|
|
{
|
|
|
|
NimBLEUUID uuid128(uuid);
|
|
|
|
uuid128.to128();
|
2022-02-15 04:18:18 +01:00
|
|
|
if(retrieveDescriptors(&uuid128)) {
|
|
|
|
if(m_descriptorVector.size() > prev_size) {
|
|
|
|
return m_descriptorVector.back();
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 22:48:07 +01:00
|
|
|
} else {
|
|
|
|
// If the request was successful but the 128 bit uuid not found
|
|
|
|
// try again with the 16 bit uuid.
|
|
|
|
NimBLEUUID uuid16(uuid);
|
|
|
|
uuid16.to16();
|
|
|
|
// if the uuid was 128 bit but not of the BLE base type this check will fail
|
|
|
|
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
|
2022-02-15 04:18:18 +01:00
|
|
|
if(retrieveDescriptors(&uuid16)) {
|
|
|
|
if(m_descriptorVector.size() > prev_size) {
|
|
|
|
return m_descriptorVector.back();
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 22:48:07 +01:00
|
|
|
}
|
2020-12-28 23:40:01 +01:00
|
|
|
}
|
2020-05-23 18:27:32 +02:00
|
|
|
}
|
2020-12-28 23:40:01 +01:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: Not found");
|
|
|
|
return nullptr;
|
|
|
|
} // getDescriptor
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Get a pointer to the vector of found descriptors.
|
|
|
|
* @param [in] refresh If true the current descriptor vector will be cleared and\n
|
|
|
|
* all descriptors for this characteristic retrieved from the peripheral.\n
|
|
|
|
* If false the vector will be returned with the currently stored descriptors
|
|
|
|
* of this characteristic.
|
|
|
|
* @return A pointer to the vector of descriptors for this characteristic.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
std::vector<NimBLERemoteDescriptor*>* NimBLERemoteCharacteristic::getDescriptors(bool refresh) {
|
|
|
|
if(refresh) {
|
2020-05-30 05:21:56 +02:00
|
|
|
deleteDescriptors();
|
2020-05-23 18:27:32 +02:00
|
|
|
|
|
|
|
if (!retrieveDescriptors()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error: Failed to get descriptors");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Found %d descriptor(s)", m_descriptorVector.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &m_descriptorVector;
|
|
|
|
} // getDescriptors
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
Add iterators to client remote attributes.
Add iterators for NimBLEScan: NimBLEadvertisedDevice, NimBLEClient: NimBLERemoteService, NimBLERemoteService: NimBLERemoteCharacteristic and NimBLERemoteCharacteristic: NimBLERemoteDescriptor
This is handy e.g. for showing every address of the advertised devices from a scan. To do so, first get a new scan and next:
```
for(auto pAdvertisedDevice: pBLEScan->getResults()) {
Serial.printf("Address is %s\n", std::string(pAdvertisedDevice->getAddress()).c_str());
}
```
Of course any other property of the advertised device can be shown (or looked up, if that is your use case)
Also this is handy e.g. for showing every UUID in a peripheral. To do so, first connect to a peripheral and next:
```
for(auto pService: *pClient) {
Serial.printf("Service UUID is %s\n", std::string(pService->getUUID()).c_str());
for(auto pCharacteristic: *pService) {
Serial.printf("Characteristic UUID is %s\n", std::string(pCharacteristic->getUUID()).c_str());
for(auto pDescriptor: *pCharacteristic) {
Serial.printf("Descriptor UUID is %s\n", std::string(pDescriptor->getUUID()).c_str());
}
}
}
```
Again of course any other property can be shown, or looked up.
2020-05-23 04:13:52 +02:00
|
|
|
/**
|
|
|
|
* @brief Get iterator to the beginning of the vector of remote descriptor pointers.
|
|
|
|
* @return An iterator to the beginning of the vector of remote descriptor pointers.
|
|
|
|
*/
|
|
|
|
std::vector<NimBLERemoteDescriptor*>::iterator NimBLERemoteCharacteristic::begin() {
|
|
|
|
return m_descriptorVector.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get iterator to the end of the vector of remote descriptor pointers.
|
|
|
|
* @return An iterator to the end of the vector of remote descriptor pointers.
|
|
|
|
*/
|
|
|
|
std::vector<NimBLERemoteDescriptor*>::iterator NimBLERemoteCharacteristic::end() {
|
|
|
|
return m_descriptorVector.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-05-23 18:27:32 +02:00
|
|
|
* @brief Get the handle for this characteristic.
|
|
|
|
* @return The handle for this characteristic.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
uint16_t NimBLERemoteCharacteristic::getHandle() {
|
|
|
|
return m_handle;
|
|
|
|
} // getHandle
|
2020-05-18 04:21:35 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the handle for this characteristics definition.
|
|
|
|
* @return The handle for this characteristic definition.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLERemoteCharacteristic::getDefHandle() {
|
|
|
|
return m_defHandle;
|
|
|
|
} // getDefHandle
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the remote service associated with this characteristic.
|
|
|
|
* @return The remote service associated with this characteristic.
|
|
|
|
*/
|
|
|
|
NimBLERemoteService* NimBLERemoteCharacteristic::getRemoteService() {
|
|
|
|
return m_pRemoteService;
|
|
|
|
} // getRemoteService
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the UUID for this characteristic.
|
|
|
|
* @return The UUID for this characteristic.
|
|
|
|
*/
|
|
|
|
NimBLEUUID NimBLERemoteCharacteristic::getUUID() {
|
|
|
|
return m_uuid;
|
|
|
|
} // getUUID
|
|
|
|
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the value of the remote characteristic.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] timestamp A pointer to a time_t struct to store the time the value was read.
|
2020-06-22 06:07:01 +02:00
|
|
|
* @return The value of the remote characteristic.
|
|
|
|
*/
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue NimBLERemoteCharacteristic::getValue(time_t *timestamp) {
|
2020-06-22 06:07:01 +02:00
|
|
|
if(timestamp != nullptr) {
|
2022-01-17 04:11:18 +01:00
|
|
|
*timestamp = m_value.getTimeStamp();
|
2020-06-22 06:07:01 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 04:11:18 +01:00
|
|
|
return m_value;
|
2022-08-27 04:15:25 +02:00
|
|
|
} // getValue
|
2020-07-02 01:32:41 +02:00
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Read the value of the remote characteristic.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] timestamp A pointer to a time_t struct to store the time the value was read.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return The value of the remote characteristic.
|
|
|
|
*/
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue NimBLERemoteCharacteristic::readValue(time_t *timestamp) {
|
2020-05-23 18:27:32 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> readValue(): uuid: %s, handle: %d 0x%.2x",
|
|
|
|
getUUID().toString().c_str(), getHandle(), getHandle());
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
NimBLEClient* pClient = getRemoteService()->getClient();
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue value;
|
2020-05-08 03:47:41 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if (!pClient->isConnected()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Disconnected");
|
2020-06-22 06:07:01 +02:00
|
|
|
return value;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
int rc = 0;
|
|
|
|
int retryCount = 1;
|
2022-01-10 03:04:41 +01:00
|
|
|
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
|
|
|
|
ble_task_data_t taskData = {this, cur_task, 0, &value};
|
2020-05-08 03:47:41 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
do {
|
2020-05-08 03:47:41 +02:00
|
|
|
rc = ble_gattc_read_long(pClient->getConnId(), m_handle, 0,
|
|
|
|
NimBLERemoteCharacteristic::onReadCB,
|
2020-06-22 06:07:01 +02:00
|
|
|
&taskData);
|
2020-03-30 01:44:20 +02:00
|
|
|
if (rc != 0) {
|
2020-05-08 03:47:41 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error: Failed to read characteristic; rc=%d, %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-06-22 06:07:01 +02:00
|
|
|
return value;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-01-10 03:04:41 +01:00
|
|
|
#ifdef ulTaskNotifyValueClear
|
|
|
|
// Clear the task notification value to ensure we block
|
|
|
|
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
|
|
|
|
#endif
|
2020-06-22 06:07:01 +02:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
rc = taskData.rc;
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
switch(rc){
|
|
|
|
case 0:
|
2020-05-08 03:47:41 +02:00
|
|
|
case BLE_HS_EDONE:
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
// Characteristic is not long-readable, return with what we have.
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_ATTR_NOT_LONG):
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Attribute not long");
|
2020-05-14 06:03:56 +02:00
|
|
|
rc = 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
break;
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN):
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHOR):
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_ENC):
|
|
|
|
if (retryCount && pClient->secureConnection())
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
/* Else falls through. */
|
2020-03-30 01:44:20 +02:00
|
|
|
default:
|
2020-06-22 06:07:01 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "<< readValue rc=%d", rc);
|
|
|
|
return value;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
} while(rc != 0 && retryCount--);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-01-17 04:11:18 +01:00
|
|
|
value.setTimeStamp();
|
2020-06-22 06:07:01 +02:00
|
|
|
m_value = value;
|
2020-05-30 04:02:26 +02:00
|
|
|
if(timestamp != nullptr) {
|
2022-01-17 04:11:18 +01:00
|
|
|
*timestamp = value.getTimeStamp();
|
2020-05-30 04:02:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< readValue length: %d rc=%d", value.length(), rc);
|
2020-05-30 04:02:26 +02:00
|
|
|
return value;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // readValue
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback for characteristic read operation.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return success == 0 or error code.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
|
|
|
|
const struct ble_gatt_error *error,
|
2020-05-14 06:03:56 +02:00
|
|
|
struct ble_gatt_attr *attr, void *arg)
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2020-06-22 06:07:01 +02:00
|
|
|
ble_task_data_t *pTaskData = (ble_task_data_t*)arg;
|
|
|
|
NimBLERemoteCharacteristic *characteristic = (NimBLERemoteCharacteristic*)pTaskData->pATT;
|
2020-05-08 03:47:41 +02:00
|
|
|
uint16_t conn_id = characteristic->getRemoteService()->getClient()->getConnId();
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-05-08 03:47:41 +02:00
|
|
|
if(conn_id != conn_handle) {
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-22 06:07:01 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGI(LOG_TAG, "Read complete; status=%d conn_handle=%d", error->status, conn_handle);
|
2020-05-08 03:47:41 +02:00
|
|
|
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue *valBuf = (NimBLEAttValue*)pTaskData->buf;
|
2020-06-22 06:07:01 +02:00
|
|
|
int rc = error->status;
|
2020-05-08 03:47:41 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
if(rc == 0) {
|
|
|
|
if(attr) {
|
2022-01-15 20:43:17 +01:00
|
|
|
uint16_t data_len = OS_MBUF_PKTLEN(attr->om);
|
2022-01-17 04:11:18 +01:00
|
|
|
if((valBuf->size() + data_len) > BLE_ATT_ATTR_MAX_LEN) {
|
2020-06-22 06:07:01 +02:00
|
|
|
rc = BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
|
|
|
|
} else {
|
2022-01-15 20:43:17 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Got %u bytes", data_len);
|
2022-01-17 04:11:18 +01:00
|
|
|
valBuf->append(attr->om->om_data, data_len);
|
2020-06-22 06:07:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-08 03:47:41 +02:00
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-06-22 06:07:01 +02:00
|
|
|
|
|
|
|
pTaskData->rc = rc;
|
|
|
|
xTaskNotifyGive(pTaskData->task);
|
|
|
|
|
|
|
|
return rc;
|
2022-08-27 04:15:25 +02:00
|
|
|
} // onReadCB
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-06-19 20:29:30 +02:00
|
|
|
* @brief Subscribe or unsubscribe for notifications or indications.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications.
|
|
|
|
* @param [in] notifyCallback A callback to be invoked for a notification.
|
2020-07-09 05:10:11 +02:00
|
|
|
* @param [in] response If write response required set this to true.
|
2020-07-09 03:27:26 +02:00
|
|
|
* If NULL is provided then no callback is performed.
|
2020-12-25 03:59:14 +01:00
|
|
|
* @return false if writing to the descriptor failed.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-10-24 03:16:33 +02:00
|
|
|
bool NimBLERemoteCharacteristic::setNotify(uint16_t val, notify_callback notifyCallback, bool response) {
|
2020-06-19 20:29:30 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> setNotify(): %s, %02x", toString().c_str(), val);
|
2020-12-28 20:05:54 +01:00
|
|
|
|
2020-12-25 03:59:14 +01:00
|
|
|
m_notifyCallback = notifyCallback;
|
2020-06-19 20:29:30 +02:00
|
|
|
|
|
|
|
NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902));
|
|
|
|
if(desc == nullptr) {
|
2020-12-25 03:59:14 +01:00
|
|
|
NIMBLE_LOGW(LOG_TAG, "<< setNotify(): Callback set, CCCD not found");
|
|
|
|
return true;
|
2020-06-19 20:29:30 +02:00
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-06-19 20:29:30 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setNotify()");
|
2022-10-24 03:16:33 +02:00
|
|
|
|
|
|
|
return desc->writeValue((uint8_t *)&val, 2, response);
|
2020-06-19 20:29:30 +02:00
|
|
|
} // setNotify
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
2020-06-19 20:29:30 +02:00
|
|
|
/**
|
|
|
|
* @brief Subscribe for notifications or indications.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] notifications If true, subscribe for notifications, false subscribe for indications.
|
|
|
|
* @param [in] notifyCallback A callback to be invoked for a notification.
|
2022-10-24 03:16:33 +02:00
|
|
|
* @param [in] response If true, require a write response from the descriptor write operation.
|
2020-07-09 03:27:26 +02:00
|
|
|
* If NULL is provided then no callback is performed.
|
2020-12-25 03:59:14 +01:00
|
|
|
* @return false if writing to the descriptor failed.
|
2020-06-19 20:29:30 +02:00
|
|
|
*/
|
2022-10-24 03:16:33 +02:00
|
|
|
bool NimBLERemoteCharacteristic::subscribe(bool notifications, notify_callback notifyCallback, bool response) {
|
2020-06-19 20:29:30 +02:00
|
|
|
if(notifications) {
|
2022-10-24 03:16:33 +02:00
|
|
|
return setNotify(0x01, notifyCallback, response);
|
2020-06-19 20:29:30 +02:00
|
|
|
} else {
|
2022-10-24 03:16:33 +02:00
|
|
|
return setNotify(0x02, notifyCallback, response);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-06-19 20:29:30 +02:00
|
|
|
} // subscribe
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unsubscribe for notifications or indications.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] response bool if true, require a write response from the descriptor write operation.
|
2020-12-25 03:59:14 +01:00
|
|
|
* @return false if writing to the descriptor failed.
|
2020-06-19 20:29:30 +02:00
|
|
|
*/
|
2022-10-24 03:16:33 +02:00
|
|
|
bool NimBLERemoteCharacteristic::unsubscribe(bool response) {
|
|
|
|
return setNotify(0x00, nullptr, response);
|
2020-06-19 20:29:30 +02:00
|
|
|
} // unsubscribe
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-05-18 04:21:35 +02:00
|
|
|
* @brief Delete the descriptors in the descriptor vector.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @details We maintain a vector called m_descriptorVector that contains pointers to NimBLERemoteDescriptors
|
2020-05-30 02:26:41 +02:00
|
|
|
* object references. Since we allocated these in this class, we are also responsible for deleting
|
2020-05-18 04:21:35 +02:00
|
|
|
* them. This method does just that.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-30 05:21:56 +02:00
|
|
|
void NimBLERemoteCharacteristic::deleteDescriptors() {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> deleteDescriptors");
|
2020-06-22 06:07:01 +02:00
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
for(auto &it: m_descriptorVector) {
|
|
|
|
delete it;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-18 04:21:35 +02:00
|
|
|
m_descriptorVector.clear();
|
2020-05-30 05:21:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< deleteDescriptors");
|
|
|
|
} // deleteDescriptors
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delete descriptor by UUID
|
|
|
|
* @param [in] uuid The UUID of the descriptor to be deleted.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return Number of descriptors left in the vector.
|
2020-05-30 05:21:56 +02:00
|
|
|
*/
|
|
|
|
size_t NimBLERemoteCharacteristic::deleteDescriptor(const NimBLEUUID &uuid) {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> deleteDescriptor");
|
2020-06-22 06:07:01 +02:00
|
|
|
|
2020-05-30 05:21:56 +02:00
|
|
|
for(auto it = m_descriptorVector.begin(); it != m_descriptorVector.end(); ++it) {
|
|
|
|
if((*it)->getUUID() == uuid) {
|
|
|
|
delete *it;
|
|
|
|
m_descriptorVector.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< deleteDescriptor");
|
|
|
|
|
|
|
|
return m_descriptorVector.size();
|
|
|
|
} // deleteDescriptor
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Convert a NimBLERemoteCharacteristic to a string representation;
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return a String representation.
|
|
|
|
*/
|
|
|
|
std::string NimBLERemoteCharacteristic::toString() {
|
|
|
|
std::string res = "Characteristic: uuid: " + m_uuid.toString();
|
|
|
|
char val[6];
|
|
|
|
res += ", handle: ";
|
|
|
|
snprintf(val, sizeof(val), "%d", getHandle());
|
|
|
|
res += val;
|
|
|
|
res += " 0x";
|
|
|
|
snprintf(val, sizeof(val), "%04x", getHandle());
|
|
|
|
res += val;
|
|
|
|
res += ", props: ";
|
|
|
|
res += " 0x";
|
|
|
|
snprintf(val, sizeof(val), "%02x", m_charProp);
|
|
|
|
res += val;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
for(auto &it: m_descriptorVector) {
|
|
|
|
res += "\n" + it->toString();
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
return res;
|
|
|
|
} // toString
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-17 04:11:18 +01:00
|
|
|
* @brief Write a new value to the remote characteristic from a std::vector<uint8_t>.
|
|
|
|
* @param [in] vec A std::vector<uint8_t> value to write to the remote characteristic.
|
|
|
|
* @param [in] response Whether we require a response from the write.
|
|
|
|
* @return false if not connected or otherwise cannot perform write.
|
|
|
|
*/
|
|
|
|
bool NimBLERemoteCharacteristic::writeValue(const std::vector<uint8_t>& vec, bool response) {
|
|
|
|
return writeValue((uint8_t*)&vec[0], vec.size(), response);
|
|
|
|
} // writeValue
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write a new value to the remote characteristic from a const char*.
|
|
|
|
* @param [in] char_s A character string to write to the remote characteristic.
|
|
|
|
* @param [in] response Whether we require a response from the write.
|
|
|
|
* @return false if not connected or otherwise cannot perform write.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-01-17 04:11:18 +01:00
|
|
|
bool NimBLERemoteCharacteristic::writeValue(const char* char_s, bool response) {
|
|
|
|
return writeValue((uint8_t*)char_s, strlen(char_s), response);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // writeValue
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-17 04:11:18 +01:00
|
|
|
* @brief Write a new value to the remote characteristic from a data buffer.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @param [in] data A pointer to a data buffer.
|
|
|
|
* @param [in] length The length of the data in the data buffer.
|
|
|
|
* @param [in] response Whether we require a response from the write.
|
2022-01-17 04:11:18 +01:00
|
|
|
* @return false if not connected or otherwise cannot perform write.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length, bool response) {
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> writeValue(), length: %d", length);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEClient* pClient = getRemoteService()->getClient();
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if (!pClient->isConnected()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Disconnected");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
int rc = 0;
|
|
|
|
int retryCount = 1;
|
|
|
|
uint16_t mtu = ble_att_mtu(pClient->getConnId()) - 3;
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
// Check if the data length is longer than we can write in one connection event.
|
2020-05-08 03:47:41 +02:00
|
|
|
// If so we must do a long write which requires a response.
|
|
|
|
if(length <= mtu && !response) {
|
2020-03-30 01:44:20 +02:00
|
|
|
rc = ble_gattc_write_no_rsp_flat(pClient->getConnId(), m_handle, data, length);
|
|
|
|
return (rc==0);
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-01-10 03:04:41 +01:00
|
|
|
TaskHandle_t cur_task = xTaskGetCurrentTaskHandle();
|
|
|
|
ble_task_data_t taskData = {this, cur_task, 0, nullptr};
|
2020-05-08 03:47:41 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
do {
|
2020-05-08 03:47:41 +02:00
|
|
|
if(length > mtu) {
|
|
|
|
NIMBLE_LOGI(LOG_TAG,"long write %d bytes", length);
|
2020-04-14 03:13:51 +02:00
|
|
|
os_mbuf *om = ble_hs_mbuf_from_flat(data, length);
|
|
|
|
rc = ble_gattc_write_long(pClient->getConnId(), m_handle, 0, om,
|
2020-05-08 03:47:41 +02:00
|
|
|
NimBLERemoteCharacteristic::onWriteCB,
|
2020-06-22 06:07:01 +02:00
|
|
|
&taskData);
|
2020-04-14 03:13:51 +02:00
|
|
|
} else {
|
2020-05-08 03:47:41 +02:00
|
|
|
rc = ble_gattc_write_flat(pClient->getConnId(), m_handle,
|
|
|
|
data, length,
|
|
|
|
NimBLERemoteCharacteristic::onWriteCB,
|
2020-06-22 06:07:01 +02:00
|
|
|
&taskData);
|
2020-05-08 03:47:41 +02:00
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error: Failed to write characteristic; rc=%d", rc);
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-01-10 03:04:41 +01:00
|
|
|
#ifdef ulTaskNotifyValueClear
|
|
|
|
// Clear the task notification value to ensure we block
|
|
|
|
ulTaskNotifyValueClear(cur_task, ULONG_MAX);
|
|
|
|
#endif
|
2020-06-22 06:07:01 +02:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
rc = taskData.rc;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
switch(rc){
|
|
|
|
case 0:
|
2020-05-08 03:47:41 +02:00
|
|
|
case BLE_HS_EDONE:
|
2020-05-14 06:03:56 +02:00
|
|
|
rc = 0;
|
2020-05-08 03:47:41 +02:00
|
|
|
break;
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_ATTR_NOT_LONG):
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Long write not supported by peer; Truncating length to %d", mtu);
|
|
|
|
retryCount++;
|
|
|
|
length = mtu;
|
2020-03-30 01:44:20 +02:00
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN):
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHOR):
|
|
|
|
case BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_ENC):
|
|
|
|
if (retryCount && pClient->secureConnection())
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
/* Else falls through. */
|
2020-03-30 01:44:20 +02:00
|
|
|
default:
|
2020-06-22 06:07:01 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "<< writeValue, rc: %d", rc);
|
2020-03-30 01:44:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while(rc != 0 && retryCount--);
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< writeValue, rc: %d", rc);
|
2020-04-14 03:13:51 +02:00
|
|
|
return (rc == 0);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // writeValue
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback for characteristic write operation.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return success == 0 or error code.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
int NimBLERemoteCharacteristic::onWriteCB(uint16_t conn_handle,
|
|
|
|
const struct ble_gatt_error *error,
|
2020-05-14 06:03:56 +02:00
|
|
|
struct ble_gatt_attr *attr, void *arg)
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2020-06-22 06:07:01 +02:00
|
|
|
ble_task_data_t *pTaskData = (ble_task_data_t*)arg;
|
|
|
|
NimBLERemoteCharacteristic *characteristic = (NimBLERemoteCharacteristic*)pTaskData->pATT;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(characteristic->getRemoteService()->getClient()->getConnId() != conn_handle){
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGI(LOG_TAG, "Write complete; status=%d conn_handle=%d", error->status, conn_handle);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
pTaskData->rc = error->status;
|
|
|
|
xTaskNotifyGive(pTaskData->task);
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
|