mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2026-01-28 10:02:19 +01:00
Conditionally compile code for specific roles.
This allows NimBLE options in menuconfig to reduce code size based on the roles selected (scan/advertising/central/peripheral). Significant code space can be saved by removing unnecessary roles for the application.
This commit is contained in:
@@ -3,17 +3,20 @@
|
||||
*
|
||||
* Created: on Jan 26 2020
|
||||
* Author H2zero
|
||||
*
|
||||
*
|
||||
* Originally:
|
||||
* BLEClient.cpp
|
||||
*
|
||||
* Created on: Mar 22, 2017
|
||||
* Author: kolban
|
||||
*/
|
||||
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include "nimconfig.h"
|
||||
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
||||
|
||||
#include "NimBLEClient.h"
|
||||
#include "NimBLEUtils.h"
|
||||
#include "NimBLEDevice.h"
|
||||
@@ -52,16 +55,16 @@ NimBLEClient::NimBLEClient()
|
||||
m_conn_id = BLE_HS_CONN_HANDLE_NONE;
|
||||
m_haveServices = false;
|
||||
m_isConnected = false;
|
||||
m_connectTimeout = 30000;
|
||||
|
||||
m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
|
||||
m_connectTimeout = 30000;
|
||||
|
||||
m_pConnParams.scan_itvl = 16; // Scan interval in 0.625ms units (NimBLE Default)
|
||||
m_pConnParams.scan_window = 16; // Scan window in 0.625ms units (NimBLE Default)
|
||||
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
|
||||
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
|
||||
m_pConnParams.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN; // Minimum length of connection event in 0.625ms units
|
||||
m_pConnParams.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; // Maximum length of connection event in 0.625ms units
|
||||
m_pConnParams.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN; // Maximum length of connection event in 0.625ms units
|
||||
} // NimBLEClient
|
||||
|
||||
|
||||
@@ -69,11 +72,11 @@ NimBLEClient::NimBLEClient()
|
||||
* @brief Destructor, private - only callable by NimBLEDevice::deleteClient
|
||||
* to ensure proper disconnect and removal from device list.
|
||||
*/
|
||||
NimBLEClient::~NimBLEClient() {
|
||||
// We may have allocated service references associated with this client.
|
||||
NimBLEClient::~NimBLEClient() {
|
||||
// We may have allocated service references associated with this client.
|
||||
// Before we are finished with the client, we must release resources.
|
||||
clearServices();
|
||||
|
||||
|
||||
if(m_deleteCallbacks && m_pClientCallbacks != &defaultCallbacks) {
|
||||
delete m_pClientCallbacks;
|
||||
}
|
||||
@@ -101,10 +104,10 @@ void NimBLEClient::clearServices() {
|
||||
*/
|
||||
/*
|
||||
void NimBLEClient::onHostReset() {
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add overloaded function to ease connect to peer device with not public address
|
||||
*/
|
||||
@@ -122,12 +125,12 @@ bool NimBLEClient::connect(NimBLEAdvertisedDevice* device, bool refreshServices)
|
||||
*/
|
||||
bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refreshServices) {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> connect(%s)", address.toString().c_str());
|
||||
|
||||
|
||||
if(!NimBLEDevice::m_synced) {
|
||||
NIMBLE_LOGC(LOG_TAG, "Host reset, wait for sync.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if(ble_gap_conn_active()) {
|
||||
NIMBLE_LOGE(LOG_TAG, "Connection in progress - must wait.");
|
||||
return false;
|
||||
@@ -135,15 +138,15 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
|
||||
|
||||
int rc = 0;
|
||||
m_peerAddress = address;
|
||||
|
||||
|
||||
ble_addr_t peerAddrt;
|
||||
memcpy(&peerAddrt.val, address.getNative(),6);
|
||||
peerAddrt.type = type;
|
||||
|
||||
|
||||
m_semaphoreOpenEvt.take("connect");
|
||||
|
||||
|
||||
/** Try to connect the the advertiser. Allow 30 seconds (30000 ms) for
|
||||
* timeout (default value of m_connectTimeout).
|
||||
* timeout (default value of m_connectTimeout).
|
||||
* Loop on BLE_HS_EBUSY if the scan hasn't stopped yet.
|
||||
*/
|
||||
do{
|
||||
@@ -153,21 +156,21 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}while(rc == BLE_HS_EBUSY);
|
||||
|
||||
|
||||
if (rc != 0 && rc != BLE_HS_EDONE) {
|
||||
NIMBLE_LOGE(LOG_TAG, "Error: Failed to connect to device; addr_type=%d "
|
||||
"addr=%s, rc=%d; %s",
|
||||
type,
|
||||
type,
|
||||
m_peerAddress.toString().c_str(),
|
||||
rc, NimBLEUtils::returnCodeToString(rc));
|
||||
|
||||
|
||||
m_semaphoreOpenEvt.give();
|
||||
m_waitingToConnect = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
m_waitingToConnect = true;
|
||||
|
||||
|
||||
rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
|
||||
|
||||
if(rc != 0){
|
||||
@@ -178,7 +181,7 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
|
||||
NIMBLE_LOGD(LOG_TAG, "Refreshing Services for: (%s)", address.toString().c_str());
|
||||
clearServices();
|
||||
}
|
||||
|
||||
|
||||
if (!m_haveServices) {
|
||||
if (!retrieveServices()) {
|
||||
// error getting services, make sure we disconnect and release any resources before returning
|
||||
@@ -191,8 +194,8 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
|
||||
}
|
||||
}
|
||||
|
||||
m_pClientCallbacks->onConnect(this);
|
||||
|
||||
m_pClientCallbacks->onConnect(this);
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< connect()");
|
||||
return true;
|
||||
} // connect
|
||||
@@ -204,23 +207,23 @@ bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refr
|
||||
* @return True on success.
|
||||
*/
|
||||
bool NimBLEClient::secureConnection() {
|
||||
|
||||
|
||||
m_semeaphoreSecEvt.take("secureConnection");
|
||||
|
||||
|
||||
int rc = NimBLEDevice::startSecurity(m_conn_id);
|
||||
if(rc != 0){
|
||||
m_semeaphoreSecEvt.give();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
rc = m_semeaphoreSecEvt.wait("secureConnection");
|
||||
if(rc != 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Disconnect from the peer.
|
||||
@@ -240,7 +243,7 @@ int NimBLEClient::disconnect(uint8_t reason) {
|
||||
// the device can be found again.
|
||||
NimBLEDevice::removeIgnored(m_peerAddress);
|
||||
}
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< disconnect()");
|
||||
return rc;
|
||||
} // disconnect
|
||||
@@ -250,23 +253,23 @@ int NimBLEClient::disconnect(uint8_t reason) {
|
||||
* @brief Set the connection paramaters to use when connecting to a server.
|
||||
*/
|
||||
void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
|
||||
uint16_t latency, uint16_t timeout,
|
||||
uint16_t latency, uint16_t timeout,
|
||||
uint16_t scanInterval, uint16_t scanWindow)/*,
|
||||
uint16_t minConnTime, uint16_t maxConnTime)*/
|
||||
{
|
||||
|
||||
m_pConnParams.scan_itvl = scanInterval; // Scan interval in 0.625ms units
|
||||
m_pConnParams.scan_itvl = scanInterval; // Scan interval in 0.625ms units
|
||||
m_pConnParams.scan_window = scanWindow; // Scan window in 0.625ms units
|
||||
m_pConnParams.itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms
|
||||
m_pConnParams.itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms
|
||||
m_pConnParams.latency = latency; // number of packets allowed to skip (extends max interval)
|
||||
m_pConnParams.supervision_timeout = timeout; // timeout = 400*10ms = 4000ms
|
||||
|
||||
// These are not used by NimBLE at this time - Must leave at defaults
|
||||
m_pConnParams.itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms
|
||||
m_pConnParams.itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms
|
||||
m_pConnParams.latency = latency; // number of packets allowed to skip (extends max interval)
|
||||
m_pConnParams.supervision_timeout = timeout; // timeout = 400*10ms = 4000ms
|
||||
|
||||
// These are not used by NimBLE at this time - Must leave at defaults
|
||||
//m_pConnParams->min_ce_len = minConnTime; // Minimum length of connection event in 0.625ms units
|
||||
//m_pConnParams->max_ce_len = maxConnTime; // Maximum length of connection event in 0.625ms units
|
||||
|
||||
int rc = NimBLEUtils::checkConnParams(&m_pConnParams);
|
||||
//m_pConnParams->max_ce_len = maxConnTime; // Maximum length of connection event in 0.625ms units
|
||||
|
||||
int rc = NimBLEUtils::checkConnParams(&m_pConnParams);
|
||||
assert(rc == 0 && "Invalid Connection parameters");
|
||||
}
|
||||
|
||||
@@ -274,24 +277,24 @@ void NimBLEClient::setConnectionParams(uint16_t minInterval, uint16_t maxInterva
|
||||
/**
|
||||
* Update connection parameters can be called only after connection has been established
|
||||
*/
|
||||
void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
|
||||
void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
|
||||
uint16_t latency, uint16_t timeout)
|
||||
{
|
||||
ble_gap_upd_params params;
|
||||
{
|
||||
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
|
||||
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
|
||||
params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
|
||||
params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -331,15 +334,15 @@ int NimBLEClient::getRssi() {
|
||||
NIMBLE_LOGE(LOG_TAG, "<< getRssi(): Not connected");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int8_t rssiValue = 0;
|
||||
int rc = ble_gap_conn_rssi(m_conn_id, &rssiValue);
|
||||
if(rc != 0) {
|
||||
NIMBLE_LOGE(LOG_TAG, "Failed to read RSSI error code: %d, %s",
|
||||
NIMBLE_LOGE(LOG_TAG, "Failed to read RSSI error code: %d, %s",
|
||||
rc, NimBLEUtils::returnCodeToString(rc));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
return rssiValue;
|
||||
} // getRssi
|
||||
|
||||
@@ -371,7 +374,7 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
|
||||
NIMBLE_LOGD(LOG_TAG, "<< getService: found the service with uuid: %s", uuid.toString().c_str());
|
||||
return myPair.second;
|
||||
}
|
||||
}
|
||||
}
|
||||
NIMBLE_LOGD(LOG_TAG, "<< getService: not found");
|
||||
return nullptr;
|
||||
} // getService
|
||||
@@ -379,7 +382,7 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
|
||||
|
||||
/**
|
||||
* @Get a pointer to the map of found services.
|
||||
*/
|
||||
*/
|
||||
std::map<std::string, NimBLERemoteService*>* NimBLEClient::getServices() {
|
||||
return &m_servicesMap;
|
||||
}
|
||||
@@ -391,7 +394,7 @@ std::map<std::string, NimBLERemoteService*>* NimBLEClient::getServices() {
|
||||
* services and wait until we have received them all.
|
||||
* We then ask for the characteristics for each service found and their desciptors.
|
||||
* @return true on success otherwise false if an error occurred
|
||||
*/
|
||||
*/
|
||||
bool NimBLEClient::retrieveServices() {
|
||||
/**
|
||||
* Design
|
||||
@@ -399,25 +402,25 @@ bool NimBLEClient::retrieveServices() {
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, ">> retrieveServices");
|
||||
|
||||
|
||||
if(!m_isConnected){
|
||||
NIMBLE_LOGE(LOG_TAG, "Disconnected, could not retrieve services -aborting");
|
||||
return false;
|
||||
}
|
||||
|
||||
m_semaphoreSearchCmplEvt.take("retrieveServices");
|
||||
|
||||
|
||||
int rc = ble_gattc_disc_all_svcs(m_conn_id, NimBLEClient::serviceDiscoveredCB, this);
|
||||
|
||||
|
||||
if (rc != 0) {
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_svcs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
|
||||
m_haveServices = false;
|
||||
m_semaphoreSearchCmplEvt.give();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// wait until we have all the services
|
||||
// If sucessful, remember that we now have services.
|
||||
m_haveServices = (m_semaphoreSearchCmplEvt.wait("retrieveServices") == 0);
|
||||
@@ -428,7 +431,7 @@ bool NimBLEClient::retrieveServices() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< retrieveServices");
|
||||
return true;
|
||||
}
|
||||
@@ -440,14 +443,14 @@ bool NimBLEClient::retrieveServices() {
|
||||
|
||||
|
||||
/**
|
||||
* @brief STATIC Callback for the service discovery API function.
|
||||
* @brief STATIC Callback for the service discovery API function.
|
||||
* 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(
|
||||
uint16_t conn_handle,
|
||||
uint16_t conn_handle,
|
||||
const struct ble_gatt_error *error,
|
||||
const struct ble_gatt_svc *service, void *arg)
|
||||
const struct ble_gatt_svc *service, void *arg)
|
||||
{
|
||||
NIMBLE_LOGD(LOG_TAG,"Service Discovered >> status: %d handle: %d", error->status, conn_handle);
|
||||
NimBLEClient *peer = (NimBLEClient*)arg;
|
||||
@@ -466,7 +469,7 @@ int NimBLEClient::serviceDiscoveredCB(
|
||||
break;
|
||||
}
|
||||
case BLE_HS_EDONE:{
|
||||
// All services discovered; start discovering characteristics.
|
||||
// All services discovered; start discovering characteristics.
|
||||
|
||||
//NIMBLE_LOGD(LOG_TAG,"Giving search semaphore - completed");
|
||||
peer->m_semaphoreSearchCmplEvt.give(0);
|
||||
@@ -481,7 +484,7 @@ int NimBLEClient::serviceDiscoveredCB(
|
||||
|
||||
if (rc != 0) {
|
||||
// pass non-zero to semaphore on error to indicate an error finding services
|
||||
peer->m_semaphoreSearchCmplEvt.give(1);
|
||||
peer->m_semaphoreSearchCmplEvt.give(1);
|
||||
}
|
||||
NIMBLE_LOGD(LOG_TAG,"<< Service Discovered. status: %d", rc);
|
||||
return rc;
|
||||
@@ -496,10 +499,10 @@ int NimBLEClient::serviceDiscoveredCB(
|
||||
*/
|
||||
std::string NimBLEClient::getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID) {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> getValue: serviceUUID: %s, characteristicUUID: %s", serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
|
||||
|
||||
|
||||
std::string ret = "";
|
||||
NimBLERemoteService* pService = getService(serviceUUID);
|
||||
|
||||
|
||||
if(pService != nullptr) {
|
||||
NimBLERemoteCharacteristic* pChar = pService->getCharacteristic(characteristicUUID);
|
||||
if(pChar != nullptr) {
|
||||
@@ -520,17 +523,17 @@ std::string NimBLEClient::getValue(const NimBLEUUID &serviceUUID, const NimBLEUU
|
||||
*/
|
||||
bool NimBLEClient::setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value) {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> setValue: serviceUUID: %s, characteristicUUID: %s", serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
|
||||
|
||||
|
||||
bool ret = false;
|
||||
NimBLERemoteService* pService = getService(serviceUUID);
|
||||
|
||||
|
||||
if(pService != nullptr) {
|
||||
NimBLERemoteCharacteristic* pChar = pService->getCharacteristic(characteristicUUID);
|
||||
if(pChar != nullptr) {
|
||||
ret = pChar->writeValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< setValue");
|
||||
return ret;
|
||||
} // setValue
|
||||
@@ -552,8 +555,8 @@ uint16_t NimBLEClient::getMTU() {
|
||||
* @param [in] arg = pointer to the client instance
|
||||
*/
|
||||
/*STATIC*/ int NimBLEClient::handleGapEvent(struct ble_gap_event *event, void *arg) {
|
||||
|
||||
NimBLEClient* client = (NimBLEClient*)arg;
|
||||
|
||||
NimBLEClient* client = (NimBLEClient*)arg;
|
||||
//struct ble_gap_conn_desc desc;
|
||||
//struct ble_hs_adv_fields fields;
|
||||
int rc;
|
||||
@@ -566,22 +569,22 @@ uint16_t NimBLEClient::getMTU() {
|
||||
case BLE_GAP_EVENT_DISCONNECT: {
|
||||
if(!client->m_isConnected)
|
||||
return 0;
|
||||
|
||||
|
||||
if(client->m_conn_id != event->disconnect.conn.conn_handle)
|
||||
return 0;
|
||||
|
||||
|
||||
client->m_isConnected = false;
|
||||
client->m_waitingToConnect=false;
|
||||
// Remove the device from ignore list so we will scan it again
|
||||
NimBLEDevice::removeIgnored(client->m_peerAddress);
|
||||
|
||||
|
||||
NIMBLE_LOGI(LOG_TAG, "disconnect; reason=%d, %s", event->disconnect.reason,
|
||||
NimBLEUtils::returnCodeToString(event->disconnect.reason));
|
||||
//print_conn_desc(&event->disconnect.conn);
|
||||
//MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
|
||||
// If Host reset tell the device now before returning to prevent
|
||||
|
||||
// If Host reset tell the device now before returning to prevent
|
||||
// any errors caused by calling host functions before resyncing.
|
||||
switch(event->disconnect.reason) {
|
||||
case BLE_HS_ETIMEOUT_HCI:
|
||||
@@ -591,19 +594,19 @@ uint16_t NimBLEClient::getMTU() {
|
||||
NIMBLE_LOGC(LOG_TAG, "Disconnect - host reset, rc=%d", event->disconnect.reason);
|
||||
NimBLEDevice::onReset(event->disconnect.reason);
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
//client->m_conn_id = BLE_HS_CONN_HANDLE_NONE;
|
||||
|
||||
// Indicate a non-success return value to any semaphores waiting
|
||||
|
||||
// Indicate a non-success return value to any semaphores waiting
|
||||
client->m_semaphoreOpenEvt.give(1);
|
||||
client->m_semaphoreSearchCmplEvt.give(1);
|
||||
client->m_semeaphoreSecEvt.give(1);
|
||||
|
||||
|
||||
client->m_pClientCallbacks->onDisconnect(client);
|
||||
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_DISCONNECT
|
||||
|
||||
@@ -611,26 +614,26 @@ uint16_t NimBLEClient::getMTU() {
|
||||
|
||||
if(!client->m_waitingToConnect)
|
||||
return 0;
|
||||
|
||||
|
||||
//if(client->m_conn_id != BLE_HS_CONN_HANDLE_NONE)
|
||||
// return 0;
|
||||
|
||||
|
||||
client->m_waitingToConnect=false;
|
||||
|
||||
|
||||
if (event->connect.status == 0) {
|
||||
client->m_isConnected = true;
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "Connection established");
|
||||
|
||||
|
||||
client->m_conn_id = event->connect.conn_handle;
|
||||
|
||||
|
||||
// rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
// assert(rc == 0);
|
||||
// print_conn_desc(&desc);
|
||||
// MODLOG_DFLT(INFO, "\n");
|
||||
|
||||
|
||||
// In the case of a multiconnecting device we ignore this device when
|
||||
|
||||
// In the case of a multiconnecting device we ignore this device when
|
||||
// scanning since we are already connected to it
|
||||
NimBLEDevice::addIgnored(client->m_peerAddress);
|
||||
|
||||
@@ -638,9 +641,9 @@ uint16_t NimBLEClient::getMTU() {
|
||||
if(rc != 0) {
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gattc_exchange_mtu: rc=%d %s",rc,
|
||||
NimBLEUtils::returnCodeToString(rc));
|
||||
// if error getting mtu indicate a connection error.
|
||||
// if error getting mtu indicate a connection error.
|
||||
client->m_semaphoreOpenEvt.give(rc);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Connection attempt failed
|
||||
NIMBLE_LOGE(LOG_TAG, "Error: Connection failed; status=%d %s",
|
||||
@@ -654,11 +657,11 @@ uint16_t NimBLEClient::getMTU() {
|
||||
case BLE_GAP_EVENT_NOTIFY_RX: {
|
||||
if(client->m_conn_id != event->notify_rx.conn_handle)
|
||||
return 0;
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "Notify Recieved for handle: %d",event->notify_rx.attr_handle);
|
||||
if(!client->m_haveServices)
|
||||
return 0;
|
||||
|
||||
|
||||
for(auto &sPair : client->m_servicesMap){
|
||||
// Dont waste cycles searching services without this handle in their range
|
||||
if(sPair.second->getEndHandle() < event->notify_rx.attr_handle) {
|
||||
@@ -669,20 +672,20 @@ uint16_t NimBLEClient::getMTU() {
|
||||
auto characteristic = cMap->find(event->notify_rx.attr_handle);
|
||||
if(characteristic != cMap->end()) {
|
||||
NIMBLE_LOGD(LOG_TAG, "Got Notification for characteristic %s", characteristic->second->toString().c_str());
|
||||
|
||||
|
||||
if (characteristic->second->m_notifyCallback != nullptr) {
|
||||
NIMBLE_LOGD(LOG_TAG, "Invoking callback for notification on characteristic %s", characteristic->second->toString().c_str());
|
||||
characteristic->second->m_notifyCallback(characteristic->second, event->notify_rx.om->om_data, event->notify_rx.om->om_len, !event->notify_rx.indication);
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_NOTIFY_RX
|
||||
|
||||
case BLE_GAP_EVENT_CONN_UPDATE_REQ:
|
||||
|
||||
case BLE_GAP_EVENT_CONN_UPDATE_REQ:
|
||||
case BLE_GAP_EVENT_L2CAP_UPDATE_REQ: {
|
||||
if(client->m_conn_id != event->conn_update_req.conn_handle){
|
||||
return 0; //BLE_HS_ENOTCONN BLE_ATT_ERR_INVALID_HANDLE
|
||||
@@ -696,7 +699,7 @@ uint16_t NimBLEClient::getMTU() {
|
||||
|
||||
rc = client->m_pClientCallbacks->onConnParamsUpdateRequest(client,
|
||||
event->conn_update_req.peer_params) ? 0 : BLE_ERR_CONN_PARMS;
|
||||
|
||||
|
||||
|
||||
if(!rc && event->type == BLE_GAP_EVENT_CONN_UPDATE_REQ ) {
|
||||
event->conn_update_req.self_params->itvl_min = client->m_pConnParams.itvl_min;
|
||||
@@ -708,7 +711,7 @@ uint16_t NimBLEClient::getMTU() {
|
||||
NIMBLE_LOGD(LOG_TAG, "%s peer params", (rc == 0) ? "Accepted" : "Rejected");
|
||||
return rc;
|
||||
} // BLE_GAP_EVENT_CONN_UPDATE_REQ, BLE_GAP_EVENT_L2CAP_UPDATE_REQ
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_CONN_UPDATE: {
|
||||
if(client->m_conn_id != event->conn_update.conn_handle){
|
||||
return 0; //BLE_HS_ENOTCONN BLE_ATT_ERR_INVALID_HANDLE
|
||||
@@ -720,28 +723,28 @@ uint16_t NimBLEClient::getMTU() {
|
||||
}
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_CONN_UPDATE
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_ENC_CHANGE: {
|
||||
if(client->m_conn_id != event->enc_change.conn_handle){
|
||||
return 0; //BLE_HS_ENOTCONN BLE_ATT_ERR_INVALID_HANDLE
|
||||
}
|
||||
|
||||
|
||||
if(event->enc_change.status == 0) {
|
||||
struct ble_gap_conn_desc desc;
|
||||
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
|
||||
|
||||
if(NimBLEDevice::m_securityCallbacks != nullptr) {
|
||||
NimBLEDevice::m_securityCallbacks->onAuthenticationComplete(&desc);
|
||||
} else {
|
||||
client->m_pClientCallbacks->onAuthenticationComplete(&desc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
client->m_semeaphoreSecEvt.give(event->enc_change.status);
|
||||
return 0;
|
||||
} //BLE_GAP_EVENT_ENC_CHANGE
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_MTU: {
|
||||
if(client->m_conn_id != event->mtu.conn_handle){
|
||||
return 0; //BLE_HS_ENOTCONN BLE_ATT_ERR_INVALID_HANDLE
|
||||
@@ -749,15 +752,15 @@ uint16_t NimBLEClient::getMTU() {
|
||||
NIMBLE_LOGI(LOG_TAG, "mtu update event; conn_handle=%d mtu=%d",
|
||||
event->mtu.conn_handle,
|
||||
event->mtu.value);
|
||||
client->m_semaphoreOpenEvt.give(0);
|
||||
client->m_semaphoreOpenEvt.give(0);
|
||||
//client->m_mtu = event->mtu.value;
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_MTU
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_PASSKEY_ACTION: {
|
||||
struct ble_sm_io pkey = {0};
|
||||
|
||||
if(client->m_conn_id != event->passkey.conn_handle)
|
||||
|
||||
if(client->m_conn_id != event->passkey.conn_handle)
|
||||
return 0;
|
||||
|
||||
if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
|
||||
@@ -765,7 +768,7 @@ uint16_t NimBLEClient::getMTU() {
|
||||
pkey.passkey = NimBLEDevice::m_passkey; // This is the passkey to be entered on peer
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "ble_sm_inject_io result: %d", rc);
|
||||
|
||||
|
||||
} else if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
|
||||
NIMBLE_LOGD(LOG_TAG, "Passkey on device's display: %d", event->passkey.params.numcmp);
|
||||
pkey.action = event->passkey.params.action;
|
||||
@@ -779,8 +782,8 @@ uint16_t NimBLEClient::getMTU() {
|
||||
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "ble_sm_inject_io result: %d", rc);
|
||||
|
||||
//TODO: Handle out of band pairing
|
||||
|
||||
//TODO: Handle out of band pairing
|
||||
} else if (event->passkey.params.action == BLE_SM_IOACT_OOB) {
|
||||
static uint8_t tem_oob[16] = {0};
|
||||
pkey.action = event->passkey.params.action;
|
||||
@@ -789,11 +792,11 @@ uint16_t NimBLEClient::getMTU() {
|
||||
}
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "ble_sm_inject_io result: %d", rc);
|
||||
////////
|
||||
////////
|
||||
} else if (event->passkey.params.action == BLE_SM_IOACT_INPUT) {
|
||||
NIMBLE_LOGD(LOG_TAG, "Enter the passkey");
|
||||
pkey.action = event->passkey.params.action;
|
||||
|
||||
|
||||
// Compatibility only - Do not use, should be removed the in future
|
||||
if(NimBLEDevice::m_securityCallbacks != nullptr) {
|
||||
pkey.passkey = NimBLEDevice::m_securityCallbacks->onPassKeyRequest();
|
||||
@@ -804,14 +807,14 @@ uint16_t NimBLEClient::getMTU() {
|
||||
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "ble_sm_inject_io result: %d", rc);
|
||||
|
||||
|
||||
} 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
|
||||
|
||||
|
||||
default: {
|
||||
return 0;
|
||||
}
|
||||
@@ -833,10 +836,10 @@ bool NimBLEClient::isConnected() {
|
||||
*/
|
||||
void NimBLEClient::setClientCallbacks(NimBLEClientCallbacks* pClientCallbacks, bool deleteCallbacks) {
|
||||
if (pClientCallbacks != nullptr){
|
||||
m_pClientCallbacks = pClientCallbacks;
|
||||
} else {
|
||||
m_pClientCallbacks = &defaultCallbacks;
|
||||
}
|
||||
m_pClientCallbacks = pClientCallbacks;
|
||||
} else {
|
||||
m_pClientCallbacks = &defaultCallbacks;
|
||||
}
|
||||
m_deleteCallbacks = deleteCallbacks;
|
||||
} // setClientCallbacks
|
||||
|
||||
@@ -865,7 +868,7 @@ void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient) {
|
||||
}
|
||||
|
||||
bool NimBLEClientCallbacks::onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params) {
|
||||
NIMBLE_LOGD("NimBLEClientCallbacks", "onConnParamsUpdateRequest: default");
|
||||
NIMBLE_LOGD("NimBLEClientCallbacks", "onConnParamsUpdateRequest: default");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -890,4 +893,5 @@ bool NimBLEClientCallbacks::onConfirmPIN(uint32_t pin){
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
||||
#endif // CONFIG_BT_ENABLED
|
||||
|
||||
Reference in New Issue
Block a user