2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEClient.cpp
|
|
|
|
*
|
|
|
|
* Created: on Jan 26 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
* BLEClient.cpp
|
|
|
|
*
|
|
|
|
* Created on: Mar 22, 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 "NimBLEClient.h"
|
|
|
|
#include "NimBLEDevice.h"
|
|
|
|
#include "NimBLELog.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_set>
|
2022-01-15 04:45:24 +01:00
|
|
|
#include <climits>
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
2021-01-12 21:50:08 +01:00
|
|
|
#include "nimble/nimble_port.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#else
|
|
|
|
#include "nimble/porting/nimble/include/nimble/nimble_port.h"
|
|
|
|
#endif
|
2021-01-12 21:50:08 +01:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
static const char* LOG_TAG = "NimBLEClient";
|
|
|
|
static NimBLEClientCallbacks defaultCallbacks;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Design
|
|
|
|
* ------
|
|
|
|
* When we perform a getService() request, we are asking the BLE server to return each of the services
|
|
|
|
* that it exposes. For each service, we receive a callback which contains details
|
|
|
|
* of the exposed service including its UUID.
|
|
|
|
*
|
|
|
|
* The objects we will invent for a NimBLEClient will be as follows:
|
|
|
|
* * NimBLERemoteService - A model of a remote service.
|
|
|
|
* * NimBLERemoteCharacteristic - A model of a remote characteristic
|
|
|
|
* * NimBLERemoteDescriptor - A model of a remote descriptor.
|
|
|
|
*
|
|
|
|
* Since there is a hierarchical relationship here, we will have the idea that from a NimBLERemoteService will own
|
|
|
|
* zero or more remote characteristics and a NimBLERemoteCharacteristic will own zero or more remote NimBLEDescriptors.
|
|
|
|
*
|
2020-05-18 04:21:35 +02:00
|
|
|
* We will assume that a NimBLERemoteService contains a vector of owned characteristics
|
|
|
|
* and that a NimBLECharacteristic contains a vector of owned descriptors.
|
2020-03-30 01:44:20 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor, private - only callable by NimBLEDevice::createClient
|
|
|
|
* to ensure proper handling of the list of client objects.
|
|
|
|
*/
|
2020-07-30 20:31:17 +02:00
|
|
|
NimBLEClient::NimBLEClient(const NimBLEAddress &peerAddress) : m_peerAddress(peerAddress) {
|
2020-03-30 01:44:20 +02:00
|
|
|
m_pClientCallbacks = &defaultCallbacks;
|
|
|
|
m_conn_id = BLE_HS_CONN_HANDLE_NONE;
|
2020-05-14 06:03:56 +02:00
|
|
|
m_connectTimeout = 30000;
|
2020-06-22 06:07:01 +02:00
|
|
|
m_deleteCallbacks = false;
|
|
|
|
m_pTaskData = nullptr;
|
2021-01-12 21:52:28 +01:00
|
|
|
m_connEstablished = false;
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = 0;
|
2022-04-10 18:21:45 +02:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
m_phyMask = BLE_GAP_LE_PHY_1M_MASK |
|
|
|
|
BLE_GAP_LE_PHY_2M_MASK |
|
|
|
|
BLE_GAP_LE_PHY_CODED_MASK;
|
|
|
|
#endif
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
|
2020-04-14 03:13:51 +02:00
|
|
|
m_pConnParams.scan_window = 16; // Scan window in 0.625ms units (NimBLE Default)
|
2020-05-14 06:03:56 +02:00
|
|
|
m_pConnParams.itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN; // min_int = 0x10*1.25ms = 20ms
|
|
|
|
m_pConnParams.itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX; // max_int = 0x20*1.25ms = 40ms
|
|
|
|
m_pConnParams.latency = BLE_GAP_INITIAL_CONN_LATENCY; // number of packets allowed to skip (extends max interval)
|
|
|
|
m_pConnParams.supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT; // timeout = 400*10ms = 4000ms
|
2020-04-14 03:13:51 +02:00
|
|
|
m_pConnParams.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; // Minimum length of connection event in 0.625ms units
|
2020-05-14 06:03:56 +02:00
|
|
|
m_pConnParams.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; // Maximum length of connection event in 0.625ms units
|
2021-01-12 21:50:08 +01:00
|
|
|
|
2021-12-29 15:50:42 +01:00
|
|
|
memset(&m_dcTimer, 0, sizeof(m_dcTimer));
|
2021-01-12 21:50:08 +01:00
|
|
|
ble_npl_callout_init(&m_dcTimer, nimble_port_get_dflt_eventq(),
|
|
|
|
NimBLEClient::dcTimerCb, this);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // NimBLEClient
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Destructor, private - only callable by NimBLEDevice::deleteClient
|
|
|
|
* to ensure proper disconnect and removal from device list.
|
|
|
|
*/
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEClient::~NimBLEClient() {
|
|
|
|
// We may have allocated service references associated with this client.
|
2020-03-30 01:44:20 +02:00
|
|
|
// Before we are finished with the client, we must release resources.
|
2020-05-30 05:21:56 +02:00
|
|
|
deleteServices();
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-05-03 21:50:49 +02:00
|
|
|
if(m_deleteCallbacks && m_pClientCallbacks != &defaultCallbacks) {
|
2020-03-30 01:44:20 +02:00
|
|
|
delete m_pClientCallbacks;
|
|
|
|
}
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2022-02-15 05:14:14 +01:00
|
|
|
ble_npl_callout_deinit(&m_dcTimer);
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
} // ~NimBLEClient
|
|
|
|
|
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
/**
|
|
|
|
* @brief If we have asked to disconnect and the event does not
|
|
|
|
* occur within the supervision timeout + added delay, this will
|
|
|
|
* be called to reset the host in the case of a stalled controller.
|
|
|
|
*/
|
|
|
|
void NimBLEClient::dcTimerCb(ble_npl_event *event) {
|
2021-01-20 20:29:22 +01:00
|
|
|
/* NimBLEClient *pClient = (NimBLEClient*)event->arg;
|
2021-01-12 21:50:08 +01:00
|
|
|
NIMBLE_LOGC(LOG_TAG, "Timed out disconnecting from %s - resetting host",
|
|
|
|
std::string(pClient->getPeerAddress()).c_str());
|
2021-01-20 20:29:22 +01:00
|
|
|
*/
|
2021-01-12 21:50:08 +01:00
|
|
|
ble_hs_sched_reset(BLE_HS_ECONTROLLER);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Delete all service objects created by this client and clear the vector.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-30 05:21:56 +02:00
|
|
|
void NimBLEClient::deleteServices() {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> deleteServices");
|
2020-03-30 01:44:20 +02:00
|
|
|
// Delete all the services.
|
2020-05-18 04:21:35 +02:00
|
|
|
for(auto &it: m_servicesVector) {
|
|
|
|
delete it;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-18 04:21:35 +02:00
|
|
|
m_servicesVector.clear();
|
|
|
|
|
2020-05-30 05:21:56 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< deleteServices");
|
|
|
|
} // deleteServices
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delete service by UUID
|
|
|
|
* @param [in] uuid The UUID of the service to be deleted from the local database.
|
|
|
|
* @return Number of services left.
|
|
|
|
*/
|
|
|
|
size_t NimBLEClient::deleteService(const NimBLEUUID &uuid) {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> deleteService");
|
|
|
|
// Delete the requested service.
|
|
|
|
for(auto it = m_servicesVector.begin(); it != m_servicesVector.end(); ++it) {
|
|
|
|
if((*it)->getUUID() == uuid) {
|
|
|
|
delete *it;
|
|
|
|
m_servicesVector.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< deleteService");
|
|
|
|
|
|
|
|
return m_servicesVector.size();
|
|
|
|
} // deleteServices
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
2020-07-30 18:06:10 +02:00
|
|
|
/**
|
|
|
|
* @brief Connect to the BLE Server.
|
2022-07-31 19:00:12 +02:00
|
|
|
* @param [in] deleteAttributes If true this will delete any attribute objects this client may already\n
|
2020-07-30 18:06:10 +02:00
|
|
|
* have created and clears the vectors after successful connection.
|
|
|
|
* @return True on success.
|
|
|
|
*/
|
2022-07-31 19:00:12 +02:00
|
|
|
bool NimBLEClient::connect(bool deleteAttributes) {
|
|
|
|
return connect(m_peerAddress, deleteAttributes);
|
2020-07-30 18:04:34 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Connect to an advertising device.
|
|
|
|
* @param [in] device The device to connect to.
|
2022-07-31 19:00:12 +02:00
|
|
|
* @param [in] deleteAttributes If true this will delete any attribute objects this client may already\n
|
2020-07-09 03:27:26 +02:00
|
|
|
* have created and clears the vectors after successful connection.
|
|
|
|
* @return True on success.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-07-31 19:00:12 +02:00
|
|
|
bool NimBLEClient::connect(NimBLEAdvertisedDevice* device, bool deleteAttributes) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEAddress address(device->getAddress());
|
2022-07-31 19:00:12 +02:00
|
|
|
return connect(address, deleteAttributes);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Connect to the BLE Server.
|
|
|
|
* @param [in] address The address of the server.
|
2022-07-31 19:00:12 +02:00
|
|
|
* @param [in] deleteAttributes If true this will delete any attribute objects this client may already\n
|
2020-07-09 03:27:26 +02:00
|
|
|
* have created and clears the vectors after successful connection.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return True on success.
|
|
|
|
*/
|
2022-07-31 19:00:12 +02:00
|
|
|
bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttributes) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> connect(%s)", address.toString().c_str());
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(!NimBLEDevice::m_synced) {
|
|
|
|
NIMBLE_LOGC(LOG_TAG, "Host reset, wait for sync.");
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-20 20:28:07 +01:00
|
|
|
if(isConnected() || m_connEstablished || m_pTaskData != nullptr) {
|
2021-01-12 21:50:08 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Client busy, connected to %s, id=%d",
|
|
|
|
std::string(m_peerAddress).c_str(), getConnId());
|
2020-06-22 06:07:01 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-20 20:28:07 +01:00
|
|
|
ble_addr_t peerAddr_t;
|
|
|
|
memcpy(&peerAddr_t.val, address.getNative(),6);
|
|
|
|
peerAddr_t.type = address.getType();
|
|
|
|
if(ble_gap_conn_find_by_addr(&peerAddr_t, NULL) == 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "A connection to %s already exists",
|
|
|
|
address.toString().c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-30 18:09:27 +02:00
|
|
|
if(address == NimBLEAddress("")) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Invalid peer address;(NULL)");
|
|
|
|
return false;
|
2021-01-20 20:28:07 +01:00
|
|
|
} else {
|
2020-07-30 18:09:27 +02:00
|
|
|
m_peerAddress = address;
|
|
|
|
}
|
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};
|
2021-01-20 20:28:07 +01:00
|
|
|
m_pTaskData = &taskData;
|
2020-07-30 18:09:27 +02:00
|
|
|
int rc = 0;
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/* Try to connect the the advertiser. Allow 30 seconds (30000 ms) for
|
2020-05-14 06:03:56 +02:00
|
|
|
* timeout (default value of m_connectTimeout).
|
2020-03-30 01:44:20 +02:00
|
|
|
* Loop on BLE_HS_EBUSY if the scan hasn't stopped yet.
|
|
|
|
*/
|
2021-01-12 21:50:08 +01:00
|
|
|
do {
|
2022-04-10 18:21:45 +02:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
rc = ble_gap_ext_connect(NimBLEDevice::m_own_addr_type,
|
|
|
|
&peerAddr_t,
|
|
|
|
m_connectTimeout,
|
|
|
|
m_phyMask,
|
|
|
|
&m_pConnParams,
|
|
|
|
&m_pConnParams,
|
|
|
|
&m_pConnParams,
|
|
|
|
NimBLEClient::handleGapEvent,
|
|
|
|
this);
|
|
|
|
|
|
|
|
#else
|
2021-01-14 02:00:48 +01:00
|
|
|
rc = ble_gap_connect(NimBLEDevice::m_own_addr_type, &peerAddr_t,
|
2021-01-12 21:50:08 +01:00
|
|
|
m_connectTimeout, &m_pConnParams,
|
|
|
|
NimBLEClient::handleGapEvent, this);
|
2022-04-10 18:21:45 +02:00
|
|
|
#endif
|
2021-01-12 21:50:08 +01:00
|
|
|
switch (rc) {
|
|
|
|
case 0:
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
case BLE_HS_EBUSY:
|
2021-01-12 21:52:28 +01:00
|
|
|
// Scan was still running, stop it and try again
|
2021-01-12 21:50:08 +01:00
|
|
|
if (!NimBLEDevice::getScan()->stop()) {
|
2021-01-20 20:28:07 +01:00
|
|
|
rc = BLE_HS_EUNKNOWN;
|
2021-01-12 21:50:08 +01:00
|
|
|
}
|
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
case BLE_HS_EDONE:
|
2021-01-12 21:52:28 +01:00
|
|
|
// A connection to this device already exists, do not connect twice.
|
2021-01-12 21:50:08 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Already connected to device; addr=%s",
|
|
|
|
std::string(m_peerAddress).c_str());
|
2021-01-20 20:28:07 +01:00
|
|
|
break;
|
2021-01-12 21:50:08 +01:00
|
|
|
|
|
|
|
case BLE_HS_EALREADY:
|
2022-07-31 19:00:12 +02:00
|
|
|
// Already attempting to connect to this device, cancel the previous
|
2021-01-12 21:52:28 +01:00
|
|
|
// attempt and report failure here so we don't get 2 connections.
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Already attempting to connect to %s - cancelling",
|
|
|
|
std::string(m_peerAddress).c_str());
|
2021-01-12 21:50:08 +01:00
|
|
|
ble_gap_conn_cancel();
|
2021-01-20 20:28:07 +01:00
|
|
|
break;
|
2021-01-12 21:50:08 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Failed to connect to %s, rc=%d; %s",
|
|
|
|
std::string(m_peerAddress).c_str(),
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2021-01-20 20:28:07 +01:00
|
|
|
break;
|
2021-01-12 21:50:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (rc == BLE_HS_EBUSY);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = rc;
|
|
|
|
|
2021-01-20 20:28:07 +01:00
|
|
|
if(rc != 0) {
|
|
|
|
m_pTaskData = nullptr;
|
|
|
|
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-01-14 06:00:39 +01:00
|
|
|
// Wait for the connect timeout time +1 second for the connection to complete
|
|
|
|
if(ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(m_connectTimeout + 1000)) == pdFALSE) {
|
|
|
|
m_pTaskData = nullptr;
|
|
|
|
// If a connection was made but no response from MTU exchange; disconnect
|
|
|
|
if(isConnected()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connect timeout - no response");
|
|
|
|
disconnect();
|
|
|
|
} else {
|
|
|
|
// workaround; if the controller doesn't cancel the connection
|
2021-01-20 20:28:07 +01:00
|
|
|
// at the timeout, cancel it here.
|
2021-01-14 06:00:39 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connect timeout - cancelling");
|
|
|
|
ble_gap_conn_cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if(taskData.rc != 0){
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = taskData.rc;
|
2021-01-12 21:52:28 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connection failed; status=%d %s",
|
|
|
|
taskData.rc,
|
|
|
|
NimBLEUtils::returnCodeToString(taskData.rc));
|
|
|
|
// If the failure was not a result of a disconnection
|
|
|
|
// make sure we disconnect now to avoid dangling connections
|
|
|
|
if(isConnected()) {
|
2021-01-20 20:28:07 +01:00
|
|
|
disconnect();
|
2021-01-12 21:52:28 +01:00
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
return false;
|
2021-01-12 21:52:28 +01:00
|
|
|
} else {
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Connection established");
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2022-07-31 19:00:12 +02:00
|
|
|
if(deleteAttributes) {
|
2020-05-30 05:21:56 +02:00
|
|
|
deleteServices();
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:58:12 +01:00
|
|
|
m_connEstablished = true;
|
2020-05-14 06:03:56 +02:00
|
|
|
m_pClientCallbacks->onConnect(this);
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< connect()");
|
2021-01-14 18:21:25 +01:00
|
|
|
// Check if still connected before returning
|
|
|
|
return isConnected();
|
2020-03-30 01:44:20 +02:00
|
|
|
} // connect
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Initiate a secure connection (pair/bond) with the server.\n
|
|
|
|
* Called automatically when a characteristic or descriptor requires encryption or authentication to access it.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return True on success.
|
|
|
|
*/
|
|
|
|
bool NimBLEClient::secureConnection() {
|
2022-10-24 03:15:57 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> secureConnection()");
|
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-14 06:03:56 +02:00
|
|
|
|
2020-08-20 17:57:20 +02:00
|
|
|
int retryCount = 1;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-08-20 17:57:20 +02:00
|
|
|
do {
|
|
|
|
m_pTaskData = &taskData;
|
|
|
|
|
|
|
|
int rc = NimBLEDevice::startSecurity(m_conn_id);
|
2022-10-24 03:15:57 +02:00
|
|
|
if(rc != 0 && rc != BLE_HS_EALREADY){
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = rc;
|
2020-08-20 17:57:20 +02:00
|
|
|
m_pTaskData = nullptr;
|
|
|
|
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
|
2020-08-20 17:57:20 +02:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
2020-08-21 19:46:45 +02:00
|
|
|
} while (taskData.rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING) && retryCount--);
|
2020-06-22 06:07:01 +02:00
|
|
|
|
|
|
|
if(taskData.rc != 0){
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = taskData.rc;
|
2022-10-24 03:15:57 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "secureConnection: failed rc=%d", taskData.rc);
|
2020-03-30 01:44:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-10-24 03:15:57 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< secureConnection: success");
|
2020-03-30 01:44:20 +02:00
|
|
|
return true;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // secureConnection
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disconnect from the peer.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return Error code from NimBLE stack, 0 = success.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
int NimBLEClient::disconnect(uint8_t reason) {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> disconnect()");
|
|
|
|
int rc = 0;
|
2021-01-20 20:28:07 +01:00
|
|
|
if(isConnected()) {
|
|
|
|
// If the timer was already started, ignore this call.
|
|
|
|
if(ble_npl_callout_is_active(&m_dcTimer)) {
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Already disconnecting, timer started");
|
|
|
|
return BLE_HS_EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gap_conn_desc desc;
|
|
|
|
if(ble_gap_conn_find(m_conn_id, &desc) != 0){
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Connection ID not found");
|
|
|
|
return BLE_HS_EALREADY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a timer to detect a controller error in the event that it does
|
|
|
|
// not inform the stack when disconnection is complete.
|
|
|
|
// This is a common error in certain esp-idf versions.
|
2022-07-31 19:00:12 +02:00
|
|
|
// The disconnect timeout time is the supervision timeout time + 1 second.
|
|
|
|
// In the case that the event happens shortly after the supervision timeout
|
2021-01-20 20:28:07 +01:00
|
|
|
// we don't want to prematurely reset the host.
|
|
|
|
ble_npl_time_t ticks;
|
|
|
|
ble_npl_time_ms_to_ticks((desc.supervision_timeout + 100) * 10, &ticks);
|
|
|
|
ble_npl_callout_reset(&m_dcTimer, ticks);
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
rc = ble_gap_terminate(m_conn_id, reason);
|
2021-01-20 20:28:07 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
if(rc != BLE_HS_EALREADY) {
|
|
|
|
ble_npl_callout_stop(&m_dcTimer);
|
2021-01-12 21:50:08 +01:00
|
|
|
}
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2021-01-12 21:50:08 +01:00
|
|
|
} else {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Not connected to any peers");
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< disconnect()");
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = rc;
|
2020-04-14 03:13:51 +02:00
|
|
|
return rc;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // disconnect
|
|
|
|
|
|
|
|
|
2022-04-10 18:21:45 +02:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
/**
|
|
|
|
* @brief Set the PHY types to use when connecting to a server.
|
|
|
|
* @param [in] mask A bitmask indicating what PHYS to connect with.\n
|
|
|
|
* The available bits are:
|
|
|
|
* * 0x01 BLE_GAP_LE_PHY_1M_MASK
|
|
|
|
* * 0x02 BLE_GAP_LE_PHY_2M_MASK
|
|
|
|
* * 0x04 BLE_GAP_LE_PHY_CODED_MASK
|
|
|
|
*/
|
|
|
|
void NimBLEClient::setConnectPhy(uint8_t mask) {
|
|
|
|
m_phyMask = mask;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2022-07-31 19:00:12 +02:00
|
|
|
* @brief Set the connection parameters to use when connecting to a server.
|
2021-01-20 23:20:11 +01:00
|
|
|
* @param [in] minInterval The minimum connection interval in 1.25ms units.
|
|
|
|
* @param [in] maxInterval The maximum connection interval in 1.25ms units.
|
|
|
|
* @param [in] latency The number of packets allowed to skip (extends max interval).
|
|
|
|
* @param [in] timeout The timeout time in 10ms units before disconnecting.
|
|
|
|
* @param [in] scanInterval The scan interval to use when attempting to connect in 0.625ms units.
|
|
|
|
* @param [in] scanWindow The scan window to use when attempting to connect in 0.625ms units.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
|
2020-05-14 06:03:56 +02:00
|
|
|
uint16_t latency, uint16_t timeout,
|
2020-04-14 03:13:51 +02:00
|
|
|
uint16_t scanInterval, uint16_t scanWindow)/*,
|
|
|
|
uint16_t minConnTime, uint16_t maxConnTime)*/
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
m_pConnParams.scan_itvl = scanInterval;
|
|
|
|
m_pConnParams.scan_window = scanWindow;
|
|
|
|
m_pConnParams.itvl_min = minInterval;
|
|
|
|
m_pConnParams.itvl_max = maxInterval;
|
|
|
|
m_pConnParams.latency = latency;
|
|
|
|
m_pConnParams.supervision_timeout = timeout;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
// These are not used by NimBLE at this time - Must leave at defaults
|
2020-04-14 03:13:51 +02:00
|
|
|
//m_pConnParams->min_ce_len = minConnTime; // Minimum length of connection event in 0.625ms units
|
2020-05-14 06:03:56 +02:00
|
|
|
//m_pConnParams->max_ce_len = maxConnTime; // Maximum length of connection event in 0.625ms units
|
2020-07-09 03:27:26 +02:00
|
|
|
} // setConnectionParams
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Update the connection parameters:
|
|
|
|
* * Can only be used after a connection has been established.
|
2021-01-20 23:20:11 +01:00
|
|
|
* @param [in] minInterval The minimum connection interval in 1.25ms units.
|
|
|
|
* @param [in] maxInterval The maximum connection interval in 1.25ms units.
|
|
|
|
* @param [in] latency The number of packets allowed to skip (extends max interval).
|
|
|
|
* @param [in] timeout The timeout time in 10ms units before disconnecting.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-14 06:03:56 +02:00
|
|
|
void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
|
2020-04-14 03:13:51 +02:00
|
|
|
uint16_t latency, uint16_t timeout)
|
2020-05-14 06:03:56 +02:00
|
|
|
{
|
|
|
|
ble_gap_upd_params params;
|
|
|
|
|
|
|
|
params.latency = latency;
|
|
|
|
params.itvl_max = maxInterval;
|
|
|
|
params.itvl_min = minInterval;
|
|
|
|
params.supervision_timeout = timeout;
|
|
|
|
// These are not used by NimBLE at this time - Must leave at defaults
|
2020-04-14 03:13:51 +02:00
|
|
|
params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
|
|
|
|
params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
int rc = ble_gap_update_params(m_conn_id, ¶ms);
|
|
|
|
if(rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2020-07-09 03:27:26 +02:00
|
|
|
} // updateConnParams
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
2021-09-13 03:09:02 +02:00
|
|
|
/**
|
|
|
|
* @brief Request an update of the data packet length.
|
|
|
|
* * Can only be used after a connection has been established.
|
|
|
|
* @details Sends a data length update request to the server the client is connected to.
|
|
|
|
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
|
|
|
|
* The server needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
|
|
|
|
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
|
|
|
|
*/
|
|
|
|
void NimBLEClient::setDataLen(uint16_t tx_octets) {
|
2022-02-06 20:58:26 +01:00
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF) && !defined(ESP_IDF_VERSION) || \
|
|
|
|
(ESP_IDF_VERSION_MAJOR * 100 + ESP_IDF_VERSION_MINOR * 10 + ESP_IDF_VERSION_PATCH) < 432
|
2021-09-14 04:45:36 +02:00
|
|
|
return;
|
|
|
|
#else
|
2021-09-13 03:09:02 +02:00
|
|
|
uint16_t tx_time = (tx_octets + 14) * 8;
|
|
|
|
|
|
|
|
int rc = ble_gap_set_data_len(m_conn_id, tx_octets, tx_time);
|
|
|
|
if(rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
|
|
|
}
|
2021-09-14 04:45:36 +02:00
|
|
|
#endif
|
2021-09-13 03:09:02 +02:00
|
|
|
} // setDataLen
|
|
|
|
|
|
|
|
|
Add connection info class and access methods to server and client.
This adds the ability to access information about the current connection.
A new class was created to wrap the struct ble_gap_conn_desc with methods to retrieve the connection information.
Example server use:
```
for(auto i=0; i<pServer->getConnectedCount();i++) {
NimBLEConnInfo connInfo = pServer->getPeerInfo(i);
printf("Connected client %d info:\n", i);
printf("Peer address: %s\n", connInfo.getAddress().toString().c_str());
printf("Peer ID address: %s\n", connInfo.getIdAddress().toString().c_str());
printf("Handle: %u\n", connInfo.getConnHandle());
printf("Interval: %u\n", connInfo.getConnInterval());
printf("Timeout: %u\n", connInfo.getConnTimeout());
printf("Latency: %u\n", connInfo.getConnLatency());
printf("MTU: %u\n", connInfo.getMTU());
printf("Master: %s\n", connInfo.isMaster()? "true":"false");
printf("Slave: %s\n", connInfo.isSlave()? "true":"false");
printf("Bonded: %s\n", connInfo.isBonded()? "true":"false");
printf("Authenticated: %s\n", connInfo.isAuthenticated()? "true":"false");
printf("Encrypted: %s\n", connInfo.isEncrypted()? "true":"false");
printf("Encryption Key Size: %u\n", connInfo.getSecKeySize());
}
```
Example client use:
```
if (pClient->isConnected()) {
NimBLEConnInfo connInfo = pClient->getConnInfo();
printf("Connection info:\n");
printf("Peer address: %s\n", connInfo.getAddress().toString().c_str());
printf("Peer ID address: %s\n", connInfo.getIdAddress().toString().c_str());
printf("Handle: %u\n", connInfo.getConnHandle());
printf("Interval: %u\n", connInfo.getConnInterval());
printf("Timeout: %u\n", connInfo.getConnTimeout());
printf("Latency: %u\n", connInfo.getConnLatency());
printf("MTU: %u\n", connInfo.getMTU());
printf("Master: %s\n", connInfo.isMaster()? "true":"false");
printf("Slave: %s\n", connInfo.isSlave()? "true":"false");
printf("Bonded: %s\n", connInfo.isBonded()? "true":"false");
printf("Authenticated: %s\n", connInfo.isAuthenticated()? "true":"false");
printf("Encrypted: %s\n", connInfo.isEncrypted()? "true":"false");
printf("Encryption Key Size: %u\n", connInfo.getSecKeySize());
}
```
2021-05-07 17:02:43 +02:00
|
|
|
/**
|
|
|
|
* @brief Get detailed information about the current peer connection.
|
|
|
|
*/
|
|
|
|
NimBLEConnInfo NimBLEClient::getConnInfo() {
|
|
|
|
NimBLEConnInfo connInfo;
|
|
|
|
if (!isConnected()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Not connected");
|
|
|
|
} else {
|
|
|
|
int rc = ble_gap_conn_find(m_conn_id, &connInfo.m_desc);
|
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connection info not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return connInfo;
|
|
|
|
} // getConnInfo
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Set the timeout to wait for connection attempt to complete.
|
2022-08-27 03:32:01 +02:00
|
|
|
* @param [in] time The number of milliseconds before timeout.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-08-27 03:32:01 +02:00
|
|
|
void NimBLEClient::setConnectTimeout(uint32_t time) {
|
|
|
|
m_connectTimeout = time;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // setConnectTimeout
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the connection id for this client.
|
|
|
|
* @return The connection id.
|
|
|
|
*/
|
|
|
|
uint16_t NimBLEClient::getConnId() {
|
|
|
|
return m_conn_id;
|
|
|
|
} // getConnId
|
|
|
|
|
2024-06-04 18:37:24 +02:00
|
|
|
/**
|
|
|
|
* @brief Clear the connection information for this client.
|
|
|
|
* @note This is designed to be used to reset the connection information after
|
|
|
|
* calling setConnection(), and should not be used to disconnect from a
|
|
|
|
* peer. To disconnect from a peer, use disconnect().
|
|
|
|
*/
|
|
|
|
void NimBLEClient::clearConnection() {
|
|
|
|
m_conn_id = BLE_HS_CONN_HANDLE_NONE;
|
|
|
|
m_connEstablished = false;
|
|
|
|
m_peerAddress = NimBLEAddress();
|
|
|
|
} // clearConnection
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the connection information for this client.
|
|
|
|
* @param [in] connInfo The connection information.
|
|
|
|
* @return True on success.
|
|
|
|
* @note Sets the connection established flag to true.
|
|
|
|
* @note If the client is already connected to a peer, this will return false.
|
|
|
|
* @note This is designed to be used when a connection is made outside of the
|
|
|
|
* NimBLEClient class, such as when a connection is made by the
|
|
|
|
* NimBLEServer class and the client is passed the connection id. This use
|
|
|
|
* enables the GATT Server to read the name of the device that has
|
|
|
|
* connected to it.
|
|
|
|
*/
|
|
|
|
bool NimBLEClient::setConnection(NimBLEConnInfo &connInfo) {
|
|
|
|
if (isConnected() || m_connEstablished) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Already connected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_peerAddress = connInfo.getAddress();
|
|
|
|
m_conn_id = connInfo.getConnHandle();
|
|
|
|
m_connEstablished = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} // setConnection
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the connection information for this client.
|
|
|
|
* @param [in] conn_id The connection id.
|
|
|
|
* @note Sets the connection established flag to true.
|
|
|
|
* @note This is designed to be used when a connection is made outside of the
|
|
|
|
* NimBLEClient class, such as when a connection is made by the
|
|
|
|
* NimBLEServer class and the client is passed the connection id. This use
|
|
|
|
* enables the GATT Server to read the name of the device that has
|
|
|
|
* connected to it.
|
|
|
|
* @note If the client is already connected to a peer, this will return false.
|
|
|
|
* @note This will look up the peer address using the connection id.
|
|
|
|
*/
|
|
|
|
bool NimBLEClient::setConnection(uint16_t conn_id) {
|
|
|
|
// we weren't provided the peer address, look it up using ble_gap_conn_find
|
|
|
|
NimBLEConnInfo connInfo;
|
|
|
|
int rc = ble_gap_conn_find(m_conn_id, &connInfo.m_desc);
|
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connection info not found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return setConnection(connInfo);
|
|
|
|
} // setConnection
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrieve the address of the peer.
|
|
|
|
*/
|
|
|
|
NimBLEAddress NimBLEClient::getPeerAddress() {
|
|
|
|
return m_peerAddress;
|
2020-07-30 18:04:34 +02:00
|
|
|
} // getPeerAddress
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the peer address.
|
|
|
|
* @param [in] address The address of the peer that this client is
|
|
|
|
* connected or should connect to.
|
|
|
|
*/
|
|
|
|
void NimBLEClient::setPeerAddress(const NimBLEAddress &address) {
|
|
|
|
if(isConnected()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Cannot set peer address while connected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_peerAddress = address;
|
2020-07-30 18:07:52 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Peer address set: %s", std::string(m_peerAddress).c_str());
|
2020-07-30 18:04:34 +02:00
|
|
|
} // setPeerAddress
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Ask the BLE server for the RSSI value.
|
|
|
|
* @return The RSSI value.
|
|
|
|
*/
|
|
|
|
int NimBLEClient::getRssi() {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> getRssi()");
|
|
|
|
if (!isConnected()) {
|
2020-04-14 03:13:51 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "<< getRssi(): Not connected");
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
int8_t rssiValue = 0;
|
|
|
|
int rc = ble_gap_conn_rssi(m_conn_id, &rssiValue);
|
|
|
|
if(rc != 0) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Failed to read RSSI error code: %d, %s",
|
2020-03-30 01:44:20 +02:00
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = rc;
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
return rssiValue;
|
|
|
|
} // getRssi
|
|
|
|
|
|
|
|
|
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 service pointers.
|
|
|
|
* @return An iterator to the beginning of the vector of remote service pointers.
|
|
|
|
*/
|
|
|
|
std::vector<NimBLERemoteService*>::iterator NimBLEClient::begin() {
|
|
|
|
return m_servicesVector.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get iterator to the end of the vector of remote service pointers.
|
|
|
|
* @return An iterator to the end of the vector of remote service pointers.
|
|
|
|
*/
|
|
|
|
std::vector<NimBLERemoteService*>::iterator NimBLEClient::end() {
|
|
|
|
return m_servicesVector.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the service BLE Remote Service instance corresponding to the uuid.
|
|
|
|
* @param [in] uuid The UUID of the service being sought.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return A pointer to the service or nullptr if not found.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
NimBLERemoteService* NimBLEClient::getService(const char* uuid) {
|
|
|
|
return getService(NimBLEUUID(uuid));
|
|
|
|
} // getService
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the service object corresponding to the uuid.
|
|
|
|
* @param [in] uuid The UUID of the service being sought.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @return A pointer to the service or nullptr if not found.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str());
|
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
for(auto &it: m_servicesVector) {
|
|
|
|
if(it->getUUID() == uuid) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuid.toString().c_str());
|
2020-05-18 04:21:35 +02:00
|
|
|
return it;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2020-05-18 04:21:35 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
size_t prev_size = m_servicesVector.size();
|
|
|
|
if(retrieveServices(&uuid)) {
|
|
|
|
if(m_servicesVector.size() > prev_size) {
|
|
|
|
return m_servicesVector.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(retrieveServices(&uuid128)) {
|
|
|
|
if(m_servicesVector.size() > prev_size) {
|
|
|
|
return m_servicesVector.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(retrieveServices(&uuid16)) {
|
|
|
|
if(m_servicesVector.size() > prev_size) {
|
|
|
|
return m_servicesVector.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-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< getService: not found");
|
|
|
|
return nullptr;
|
|
|
|
} // getService
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Get a pointer to the vector of found services.
|
|
|
|
* @param [in] refresh If true the current services vector will be cleared and\n
|
|
|
|
* all services will be retrieved from the peripheral.\n
|
|
|
|
* If false the vector will be returned with the currently stored services.
|
|
|
|
* @return A pointer to the vector of available services.
|
2020-05-14 06:03:56 +02:00
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
std::vector<NimBLERemoteService*>* NimBLEClient::getServices(bool refresh) {
|
|
|
|
if(refresh) {
|
2020-05-30 05:21:56 +02:00
|
|
|
deleteServices();
|
2020-05-23 18:27:32 +02:00
|
|
|
|
|
|
|
if (!retrieveServices()) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Error: Failed to get services");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Found %d services", m_servicesVector.size());
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 04:21:35 +02:00
|
|
|
return &m_servicesVector;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // getServices
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Retrieves the full database of attributes that the peripheral has available.
|
2022-04-17 04:32:01 +02:00
|
|
|
* @return True if successful.
|
2020-05-23 18:27:32 +02:00
|
|
|
*/
|
2022-04-17 04:32:01 +02:00
|
|
|
bool NimBLEClient::discoverAttributes() {
|
|
|
|
deleteServices();
|
|
|
|
|
|
|
|
if (!retrieveServices()){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for(auto svc: m_servicesVector) {
|
|
|
|
if (!svc->retrieveCharacteristics()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto chr: svc->m_characteristicVector) {
|
|
|
|
if (!chr->retrieveDescriptors()) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-23 18:27:32 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-17 04:32:01 +02:00
|
|
|
|
|
|
|
return true;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // discoverAttributes
|
2020-05-23 18:27:32 +02:00
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Ask the remote %BLE server for its services.\n
|
|
|
|
* Here we ask the server for its set of services and wait until we have received them all.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @return true on success otherwise false if an error occurred
|
2020-05-14 06:03:56 +02:00
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
bool NimBLEClient::retrieveServices(const NimBLEUUID *uuid_filter) {
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* Design
|
|
|
|
* ------
|
|
|
|
* We invoke ble_gattc_disc_all_svcs. This will request a list of the services exposed by the
|
|
|
|
* peer BLE partner to be returned in the callback function provided.
|
|
|
|
*/
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> retrieveServices");
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
if(!isConnected()){
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Disconnected, could not retrieve services -aborting");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-22 06:07:01 +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};
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
if(uuid_filter == nullptr) {
|
2020-06-22 06:07:01 +02:00
|
|
|
rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, &taskData);
|
2020-05-23 18:27:32 +02:00
|
|
|
} else {
|
|
|
|
rc = ble_gattc_disc_svc_by_uuid(m_conn_id, &uuid_filter->getNative()->u,
|
2020-06-22 06:07:01 +02:00
|
|
|
NimBLEClient::serviceDiscoveredCB, &taskData);
|
2020-05-23 18:27:32 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_svcs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = 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-03-30 01:44:20 +02:00
|
|
|
// wait until we have all the services
|
2020-06-22 06:07:01 +02:00
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
2021-12-29 16:12:38 +01:00
|
|
|
m_lastErr = taskData.rc;
|
2020-06-22 06:07:01 +02:00
|
|
|
|
|
|
|
if(taskData.rc == 0){
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< retrieveServices");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Could not retrieve services");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // getServices
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief STATIC Callback for the service discovery API function.\n
|
2020-03-30 01:44:20 +02:00
|
|
|
* When a service is found or there is none left or there was an error
|
|
|
|
* the API will call this and report findings.
|
|
|
|
*/
|
|
|
|
int NimBLEClient::serviceDiscoveredCB(
|
2020-05-14 06:03:56 +02:00
|
|
|
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
|
|
|
const struct ble_gatt_svc *service, void *arg)
|
2020-03-30 01:44:20 +02:00
|
|
|
{
|
2020-05-23 18:27:32 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG,"Service Discovered >> status: %d handle: %d",
|
|
|
|
error->status, (error->status == 0) ? service->start_handle : -1);
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
ble_task_data_t *pTaskData = (ble_task_data_t*)arg;
|
|
|
|
NimBLEClient *client = (NimBLEClient*)pTaskData->pATT;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
// Make sure the service discovery is for this device
|
2020-06-22 06:07:01 +02:00
|
|
|
if(client->getConnId() != conn_handle){
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
if(error->status == 0) {
|
|
|
|
// Found a service - add it to the vector
|
|
|
|
NimBLERemoteService* pRemoteService = new NimBLERemoteService(client, service);
|
|
|
|
client->m_servicesVector.push_back(pRemoteService);
|
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
if(error->status == BLE_HS_EDONE) {
|
|
|
|
pTaskData->rc = 0;
|
|
|
|
} else {
|
2022-02-20 18:02:30 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "serviceDiscoveredCB() rc=%d %s",
|
2020-06-22 06:07:01 +02:00
|
|
|
error->status,
|
|
|
|
NimBLEUtils::returnCodeToString(error->status));
|
|
|
|
pTaskData->rc = error->status;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-06-22 06:07:01 +02:00
|
|
|
|
|
|
|
xTaskNotifyGive(pTaskData->task);
|
|
|
|
|
2022-02-20 18:02:30 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG,"<< Service Discovered");
|
2020-06-22 06:07:01 +02:00
|
|
|
return error->status;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the value of a specific characteristic associated with a specific service.
|
|
|
|
* @param [in] serviceUUID The service that owns the characteristic.
|
|
|
|
* @param [in] characteristicUUID The characteristic whose value we wish to read.
|
|
|
|
* @returns characteristic value or an empty string if not found
|
|
|
|
*/
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue NimBLEClient::getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID) {
|
2020-05-23 18:27:32 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> getValue: serviceUUID: %s, characteristicUUID: %s",
|
|
|
|
serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue ret;
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLERemoteService* pService = getService(serviceUUID);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(pService != nullptr) {
|
|
|
|
NimBLERemoteCharacteristic* pChar = pService->getCharacteristic(characteristicUUID);
|
|
|
|
if(pChar != nullptr) {
|
|
|
|
ret = pChar->readValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 18:02:30 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< getValue");
|
2020-03-30 01:44:20 +02:00
|
|
|
return ret;
|
|
|
|
} // getValue
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the value of a specific characteristic associated with a specific service.
|
|
|
|
* @param [in] serviceUUID The service that owns the characteristic.
|
|
|
|
* @param [in] characteristicUUID The characteristic whose value we wish to write.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] value The value to write to the characteristic.
|
2021-01-16 05:51:49 +01:00
|
|
|
* @param [in] response If true, uses write with response operation.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @returns true if successful otherwise false
|
|
|
|
*/
|
2020-05-23 18:27:32 +02:00
|
|
|
bool NimBLEClient::setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID,
|
2022-01-17 04:11:18 +01:00
|
|
|
const NimBLEAttValue &value, bool response)
|
2020-05-23 18:27:32 +02:00
|
|
|
{
|
|
|
|
NIMBLE_LOGD(LOG_TAG, ">> setValue: serviceUUID: %s, characteristicUUID: %s",
|
|
|
|
serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
bool ret = false;
|
|
|
|
NimBLERemoteService* pService = getService(serviceUUID);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
if(pService != nullptr) {
|
|
|
|
NimBLERemoteCharacteristic* pChar = pService->getCharacteristic(characteristicUUID);
|
|
|
|
if(pChar != nullptr) {
|
2020-12-20 17:57:26 +01:00
|
|
|
ret = pChar->writeValue(value, response);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "<< setValue");
|
|
|
|
return ret;
|
|
|
|
} // setValue
|
|
|
|
|
|
|
|
|
2020-09-16 06:09:30 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the remote characteristic with the specified handle.
|
|
|
|
* @param [in] handle The handle of the desired characteristic.
|
|
|
|
* @returns The matching remote characteristic, nullptr otherwise.
|
|
|
|
*/
|
|
|
|
NimBLERemoteCharacteristic* NimBLEClient::getCharacteristic(const uint16_t handle)
|
|
|
|
{
|
|
|
|
NimBLERemoteService *pService = nullptr;
|
|
|
|
for(auto it = m_servicesVector.begin(); it != m_servicesVector.end(); ++it) {
|
|
|
|
if ((*it)->getStartHandle() <= handle && handle <= (*it)->getEndHandle()) {
|
|
|
|
pService = *it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pService != nullptr) {
|
|
|
|
for (auto it = pService->begin(); it != pService->end(); ++it) {
|
|
|
|
if ((*it)->getHandle() == handle) {
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the current mtu of this connection.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @returns The MTU value.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
uint16_t NimBLEClient::getMTU() {
|
|
|
|
return ble_att_mtu(m_conn_id);
|
2020-07-09 03:27:26 +02:00
|
|
|
} // getMTU
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handle a received GAP event.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] event The event structure sent by the NimBLE stack.
|
|
|
|
* @param [in] arg A pointer to the client instance that registered for this callback.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2021-01-12 21:52:28 +01:00
|
|
|
/*STATIC*/
|
2022-01-17 04:11:18 +01:00
|
|
|
int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) {
|
2022-08-27 16:28:15 +02:00
|
|
|
NimBLEClient* pClient = (NimBLEClient*)arg;
|
2020-03-30 01:44:20 +02:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Got Client event %s", NimBLEUtils::gapEventToString(event->type));
|
|
|
|
|
|
|
|
switch(event->type) {
|
|
|
|
|
|
|
|
case BLE_GAP_EVENT_DISCONNECT: {
|
2021-01-12 21:56:29 +01:00
|
|
|
rc = event->disconnect.reason;
|
|
|
|
// If Host reset tell the device now before returning to prevent
|
|
|
|
// any errors caused by calling host functions before resyncing.
|
|
|
|
switch(rc) {
|
|
|
|
case BLE_HS_ECONTROLLER:
|
|
|
|
case BLE_HS_ETIMEOUT_HCI:
|
|
|
|
case BLE_HS_ENOTSYNCED:
|
|
|
|
case BLE_HS_EOS:
|
|
|
|
NIMBLE_LOGC(LOG_TAG, "Disconnect - host reset, rc=%d", rc);
|
|
|
|
NimBLEDevice::onReset(rc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Check that the event is for this client.
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->disconnect.conn.conn_handle) {
|
2021-01-12 21:56:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
// Stop the disconnect timer since we are now disconnected.
|
2022-08-27 16:28:15 +02:00
|
|
|
ble_npl_callout_stop(&pClient->m_dcTimer);
|
2021-01-12 21:50:08 +01:00
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
// Remove the device from ignore list so we will scan it again
|
2022-08-27 16:28:15 +02:00
|
|
|
NimBLEDevice::removeIgnored(pClient->m_peerAddress);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-20 20:28:07 +01:00
|
|
|
// No longer connected, clear the connection ID.
|
2022-08-27 16:28:15 +02:00
|
|
|
pClient->m_conn_id = BLE_HS_CONN_HANDLE_NONE;
|
2021-01-20 20:28:07 +01:00
|
|
|
|
2021-01-12 21:56:29 +01:00
|
|
|
// If we received a connected event but did not get established (no PDU)
|
2021-01-12 21:52:28 +01:00
|
|
|
// then a disconnect event will be sent but we should not send it to the
|
|
|
|
// app for processing. Instead we will ensure the task is released
|
|
|
|
// and report the error.
|
2022-08-27 16:28:15 +02:00
|
|
|
if(!pClient->m_connEstablished)
|
2021-01-12 21:52:28 +01:00
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:56:29 +01:00
|
|
|
NIMBLE_LOGI(LOG_TAG, "disconnect; reason=%d, %s",
|
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
pClient->m_connEstablished = false;
|
|
|
|
pClient->m_pClientCallbacks->onDisconnect(pClient, rc);
|
2020-06-22 06:07:01 +02:00
|
|
|
break;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // BLE_GAP_EVENT_DISCONNECT
|
|
|
|
|
|
|
|
case BLE_GAP_EVENT_CONNECT: {
|
2021-01-12 21:52:28 +01:00
|
|
|
// If we aren't waiting for this connection response
|
|
|
|
// we should drop the connection immediately.
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->isConnected() || pClient->m_pTaskData == nullptr) {
|
2021-01-12 21:50:08 +01:00
|
|
|
ble_gap_terminate(event->connect.conn_handle, BLE_ERR_REM_USER_CONN_TERM);
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
2021-01-12 21:50:08 +01:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
rc = event->connect.status;
|
|
|
|
if (rc == 0) {
|
2021-01-12 21:52:28 +01:00
|
|
|
NIMBLE_LOGI(LOG_TAG, "Connected event");
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
pClient->m_conn_id = event->connect.conn_handle;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
rc = ble_gattc_exchange_mtu(pClient->m_conn_id, NULL,NULL);
|
2020-03-30 01:44:20 +02:00
|
|
|
if(rc != 0) {
|
2021-01-12 21:52:28 +01:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "MTU exchange error; rc=%d %s",
|
2021-01-12 21:50:08 +01:00
|
|
|
rc, NimBLEUtils::returnCodeToString(rc));
|
2020-06-22 06:07:01 +02:00
|
|
|
break;
|
2020-05-14 06:03:56 +02:00
|
|
|
}
|
2020-06-22 06:07:01 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
// In the case of a multi-connecting device we ignore this device when
|
2020-06-22 06:07:01 +02:00
|
|
|
// scanning since we are already connected to it
|
2022-08-27 16:28:15 +02:00
|
|
|
NimBLEDevice::addIgnored(pClient->m_peerAddress);
|
2020-03-30 01:44:20 +02:00
|
|
|
} else {
|
2022-08-27 16:28:15 +02:00
|
|
|
pClient->m_conn_id = BLE_HS_CONN_HANDLE_NONE;
|
2020-06-22 06:07:01 +02:00
|
|
|
break;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2021-01-12 21:50:08 +01:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
} // BLE_GAP_EVENT_CONNECT
|
|
|
|
|
|
|
|
case BLE_GAP_EVENT_NOTIFY_RX: {
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->notify_rx.conn_handle)
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:58:12 +01:00
|
|
|
// If a notification comes before this flag is set we might
|
|
|
|
// access a vector while it is being cleared in connect()
|
2022-08-27 16:28:15 +02:00
|
|
|
if(!pClient->m_connEstablished) {
|
2021-01-12 21:58:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Notify Received for handle: %d",
|
2021-01-12 21:52:28 +01:00
|
|
|
event->notify_rx.attr_handle);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
for(auto &it: pClient->m_servicesVector) {
|
2020-06-22 06:07:01 +02:00
|
|
|
// Dont waste cycles searching services without this handle in its range
|
2020-05-18 04:21:35 +02:00
|
|
|
if(it->getEndHandle() < event->notify_rx.attr_handle) {
|
2020-03-30 01:44:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-05-18 04:21:35 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
auto cVector = &it->m_characteristicVector;
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "checking service %s for handle: %d",
|
2021-01-12 21:52:28 +01:00
|
|
|
it->getUUID().toString().c_str(),
|
|
|
|
event->notify_rx.attr_handle);
|
2020-05-23 18:27:32 +02:00
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
auto characteristic = cVector->cbegin();
|
|
|
|
for(; characteristic != cVector->cend(); ++characteristic) {
|
2020-05-23 18:27:32 +02:00
|
|
|
if((*characteristic)->m_handle == event->notify_rx.attr_handle)
|
|
|
|
break;
|
2020-05-18 04:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(characteristic != cVector->cend()) {
|
2021-01-12 21:52:28 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s",
|
|
|
|
(*characteristic)->toString().c_str());
|
2020-05-18 04:21:35 +02:00
|
|
|
|
2022-01-13 16:57:50 +01:00
|
|
|
uint32_t data_len = OS_MBUF_PKTLEN(event->notify_rx.om);
|
2022-01-17 04:11:18 +01:00
|
|
|
(*characteristic)->m_value.setValue(event->notify_rx.om->om_data, data_len);
|
2020-05-30 04:02:26 +02:00
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
if ((*characteristic)->m_notifyCallback != nullptr) {
|
2020-05-23 18:27:32 +02:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s",
|
2021-01-12 21:52:28 +01:00
|
|
|
(*characteristic)->toString().c_str());
|
2020-05-23 18:27:32 +02:00
|
|
|
(*characteristic)->m_notifyCallback(*characteristic, event->notify_rx.om->om_data,
|
2022-01-13 16:57:50 +01:00
|
|
|
data_len, !event->notify_rx.indication);
|
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
|
|
|
return 0;
|
|
|
|
} // BLE_GAP_EVENT_NOTIFY_RX
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
case BLE_GAP_EVENT_CONN_UPDATE_REQ:
|
2020-03-30 01:44:20 +02:00
|
|
|
case BLE_GAP_EVENT_L2CAP_UPDATE_REQ: {
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->conn_update_req.conn_handle){
|
2020-06-22 06:07:01 +02:00
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Peer requesting to update connection parameters");
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "MinInterval: %d, MaxInterval: %d, Latency: %d, Timeout: %d",
|
2021-01-12 21:52:28 +01:00
|
|
|
event->conn_update_req.peer_params->itvl_min,
|
|
|
|
event->conn_update_req.peer_params->itvl_max,
|
|
|
|
event->conn_update_req.peer_params->latency,
|
|
|
|
event->conn_update_req.peer_params->supervision_timeout);
|
2020-04-14 03:13:51 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
rc = pClient->m_pClientCallbacks->onConnParamsUpdateRequest(pClient,
|
2020-04-14 03:13:51 +02:00
|
|
|
event->conn_update_req.peer_params) ? 0 : BLE_ERR_CONN_PARMS;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
|
|
|
|
if(!rc && event->type == BLE_GAP_EVENT_CONN_UPDATE_REQ ) {
|
2022-08-27 16:28:15 +02:00
|
|
|
event->conn_update_req.self_params->itvl_min = pClient->m_pConnParams.itvl_min;
|
|
|
|
event->conn_update_req.self_params->itvl_max = pClient->m_pConnParams.itvl_max;
|
|
|
|
event->conn_update_req.self_params->latency = pClient->m_pConnParams.latency;
|
|
|
|
event->conn_update_req.self_params->supervision_timeout = pClient->m_pConnParams.supervision_timeout;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-04-14 03:13:51 +02:00
|
|
|
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "%s peer params", (rc == 0) ? "Accepted" : "Rejected");
|
2020-03-30 01:44:20 +02:00
|
|
|
return rc;
|
|
|
|
} // BLE_GAP_EVENT_CONN_UPDATE_REQ, BLE_GAP_EVENT_L2CAP_UPDATE_REQ
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
case BLE_GAP_EVENT_CONN_UPDATE: {
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->conn_update.conn_handle){
|
2020-06-22 06:07:01 +02:00
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
if(event->conn_update.status == 0) {
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "Connection parameters updated.");
|
|
|
|
} else {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Update connection parameters failed.");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} // BLE_GAP_EVENT_CONN_UPDATE
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
case BLE_GAP_EVENT_ENC_CHANGE: {
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->enc_change.conn_handle){
|
2020-06-22 06:07:01 +02:00
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-01-12 21:50:08 +01:00
|
|
|
if(event->enc_change.status == 0 ||
|
|
|
|
event->enc_change.status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING))
|
|
|
|
{
|
2022-08-27 16:28:15 +02:00
|
|
|
NimBLEConnInfo peerInfo;
|
|
|
|
rc = ble_gap_conn_find(event->enc_change.conn_handle, &peerInfo.m_desc);
|
2024-06-07 03:36:14 +02:00
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connection info not found");
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-08-21 19:46:45 +02:00
|
|
|
if (event->enc_change.status == (BLE_HS_ERR_HCI_BASE + BLE_ERR_PINKEY_MISSING)) {
|
2020-08-20 17:57:20 +02:00
|
|
|
// Key is missing, try deleting.
|
2022-08-27 16:28:15 +02:00
|
|
|
ble_store_util_delete_peer(&peerInfo.m_desc.peer_id_addr);
|
2020-03-30 01:44:20 +02:00
|
|
|
} else {
|
2022-08-27 16:28:15 +02:00
|
|
|
pClient->m_pClientCallbacks->onAuthenticationComplete(peerInfo);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
rc = event->enc_change.status;
|
|
|
|
break;
|
2020-03-30 01:44:20 +02:00
|
|
|
} //BLE_GAP_EVENT_ENC_CHANGE
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-06-12 17:26:10 +02:00
|
|
|
case BLE_GAP_EVENT_IDENTITY_RESOLVED: {
|
|
|
|
NimBLEConnInfo peerInfo;
|
|
|
|
rc = ble_gap_conn_find(event->identity_resolved.conn_handle, &peerInfo.m_desc);
|
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connection info not found");
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pClient->m_pClientCallbacks->onIdentity(peerInfo);
|
|
|
|
break;
|
|
|
|
} // BLE_GAP_EVENT_IDENTITY_RESOLVED
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
case BLE_GAP_EVENT_MTU: {
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->mtu.conn_handle){
|
2020-06-22 06:07:01 +02:00
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
NIMBLE_LOGI(LOG_TAG, "mtu update event; conn_handle=%d mtu=%d",
|
|
|
|
event->mtu.conn_handle,
|
|
|
|
event->mtu.value);
|
2020-06-22 06:07:01 +02:00
|
|
|
rc = 0;
|
|
|
|
break;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // BLE_GAP_EVENT_MTU
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
case BLE_GAP_EVENT_PASSKEY_ACTION: {
|
2020-06-22 06:07:01 +02:00
|
|
|
struct ble_sm_io pkey = {0,0};
|
2023-05-29 17:08:14 +02:00
|
|
|
(void)pkey; //warning: variable 'pkey' set but not used [-Wunused-but-set-variable]
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_conn_id != event->passkey.conn_handle)
|
2020-03-30 01:44:20 +02:00
|
|
|
return 0;
|
|
|
|
|
2024-06-07 03:36:14 +02:00
|
|
|
NimBLEConnInfo peerInfo;
|
|
|
|
rc = ble_gap_conn_find(event->passkey.conn_handle, &peerInfo.m_desc);
|
|
|
|
if (rc != 0) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Connection info not found");
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2024-06-07 03:36:14 +02:00
|
|
|
if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
|
2022-01-15 20:43:17 +01:00
|
|
|
NIMBLE_LOGD(LOG_TAG, "Passkey on device's display: %" PRIu32, event->passkey.params.numcmp);
|
2024-06-07 03:36:14 +02:00
|
|
|
pClient->m_pClientCallbacks->onConfirmPIN(peerInfo, event->passkey.params.numcmp);
|
2020-05-14 06:03:56 +02:00
|
|
|
//TODO: Handle out of band pairing
|
2020-03-30 01:44:20 +02:00
|
|
|
} else if (event->passkey.params.action == BLE_SM_IOACT_OOB) {
|
|
|
|
static uint8_t tem_oob[16] = {0};
|
|
|
|
pkey.action = event->passkey.params.action;
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
pkey.oob[i] = tem_oob[i];
|
|
|
|
}
|
|
|
|
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "ble_sm_inject_io result: %d", rc);
|
2020-05-14 06:03:56 +02:00
|
|
|
////////
|
2020-03-30 01:44:20 +02:00
|
|
|
} else if (event->passkey.params.action == BLE_SM_IOACT_INPUT) {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "Enter the passkey");
|
2024-06-07 03:36:14 +02:00
|
|
|
pClient->m_pClientCallbacks->onPassKeyEntry(peerInfo);
|
2020-03-30 01:44:20 +02:00
|
|
|
} else if (event->passkey.params.action == BLE_SM_IOACT_NONE) {
|
|
|
|
NIMBLE_LOGD(LOG_TAG, "No passkey action required");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} // BLE_GAP_EVENT_PASSKEY_ACTION
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
default: {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // Switch
|
2020-06-22 06:07:01 +02:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
if(pClient->m_pTaskData != nullptr) {
|
|
|
|
pClient->m_pTaskData->rc = rc;
|
|
|
|
if(pClient->m_pTaskData->task) {
|
|
|
|
xTaskNotifyGive(pClient->m_pTaskData->task);
|
2021-01-12 21:54:53 +01:00
|
|
|
}
|
2022-08-27 16:28:15 +02:00
|
|
|
pClient->m_pTaskData = nullptr;
|
2020-06-22 06:07:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // handleGapEvent
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Are we connected to a server?
|
|
|
|
* @return True if we are connected and false if we are not connected.
|
|
|
|
*/
|
|
|
|
bool NimBLEClient::isConnected() {
|
2021-01-12 21:50:08 +01:00
|
|
|
return m_conn_id != BLE_HS_CONN_HANDLE_NONE;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // isConnected
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Set the callbacks that will be invoked when events are received.
|
|
|
|
* @param [in] pClientCallbacks A pointer to a class to receive the event callbacks.
|
|
|
|
* @param [in] deleteCallbacks If true this will delete the callback class sent when the client is destructed.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
void NimBLEClient::setClientCallbacks(NimBLEClientCallbacks* pClientCallbacks, bool deleteCallbacks) {
|
|
|
|
if (pClientCallbacks != nullptr){
|
2020-05-14 06:03:56 +02:00
|
|
|
m_pClientCallbacks = pClientCallbacks;
|
|
|
|
} else {
|
|
|
|
m_pClientCallbacks = &defaultCallbacks;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
m_deleteCallbacks = deleteCallbacks;
|
|
|
|
} // setClientCallbacks
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return a string representation of this client.
|
|
|
|
* @return A string representation of this client.
|
|
|
|
*/
|
|
|
|
std::string NimBLEClient::toString() {
|
|
|
|
std::string res = "peer address: " + m_peerAddress.toString();
|
|
|
|
res += "\nServices:\n";
|
2020-05-18 04:21:35 +02:00
|
|
|
|
|
|
|
for(auto &it: m_servicesVector) {
|
|
|
|
res += it->toString() + "\n";
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
} // toString
|
|
|
|
|
|
|
|
|
2021-12-29 16:12:38 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the last error code reported by the NimBLE host
|
|
|
|
* @return int, the NimBLE error code.
|
|
|
|
*/
|
|
|
|
int NimBLEClient::getLastError() {
|
|
|
|
return m_lastErr;
|
|
|
|
} // getLastError
|
|
|
|
|
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onConnect: default");
|
|
|
|
}
|
|
|
|
|
2022-08-27 03:51:19 +02:00
|
|
|
void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient, int reason) {
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onDisconnect: default");
|
|
|
|
}
|
|
|
|
|
2020-04-14 03:13:51 +02:00
|
|
|
bool NimBLEClientCallbacks::onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onConnParamsUpdateRequest: default");
|
2020-04-14 03:13:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-07 03:36:14 +02:00
|
|
|
void NimBLEClientCallbacks::onPassKeyEntry(const NimBLEConnInfo& connInfo){
|
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onPassKeyEntry: default: 123456");
|
|
|
|
NimBLEDevice::injectPassKey(connInfo, 123456);
|
|
|
|
} //onPassKeyEntry
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2024-06-07 03:36:14 +02:00
|
|
|
void NimBLEClientCallbacks::onAuthenticationComplete(const NimBLEConnInfo& connInfo){
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onAuthenticationComplete: default");
|
|
|
|
}
|
2024-06-07 03:36:14 +02:00
|
|
|
|
2024-06-12 17:26:10 +02:00
|
|
|
void NimBLEClientCallbacks::onIdentity(const NimBLEConnInfo& connInfo){
|
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onIdentity: default");
|
|
|
|
} // onIdentity
|
|
|
|
|
2024-06-07 03:36:14 +02:00
|
|
|
void NimBLEClientCallbacks::onConfirmPIN(const NimBLEConnInfo& connInfo, uint32_t pin){
|
2020-03-30 01:44:20 +02:00
|
|
|
NIMBLE_LOGD("NimBLEClientCallbacks", "onConfirmPIN: default: true");
|
2024-06-07 03:36:14 +02:00
|
|
|
NimBLEDevice::injectConfirmPIN(connInfo, true);
|
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 */
|