mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2026-01-28 01:52:27 +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,7 +3,7 @@
|
||||
*
|
||||
* Created: on March 2, 2020
|
||||
* Author H2zero
|
||||
*
|
||||
*
|
||||
* Originally:
|
||||
*
|
||||
* BLEServer.cpp
|
||||
@@ -15,6 +15,9 @@
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include "nimconfig.h"
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
|
||||
#include "NimBLEServer.h"
|
||||
#include "NimBLE2902.h"
|
||||
#include "NimBLEUtils.h"
|
||||
@@ -32,10 +35,10 @@ static NimBLEServerCallbacks defaultCallbacks;
|
||||
* the BLEDevice class.
|
||||
*/
|
||||
NimBLEServer::NimBLEServer() {
|
||||
m_connId = BLE_HS_CONN_HANDLE_NONE;
|
||||
m_connId = BLE_HS_CONN_HANDLE_NONE;
|
||||
m_svcChgChrHdl = 0xffff;
|
||||
m_pServerCallbacks = &defaultCallbacks;
|
||||
m_gattsStarted = false;
|
||||
m_pServerCallbacks = &defaultCallbacks;
|
||||
m_gattsStarted = false;
|
||||
} // BLEServer
|
||||
|
||||
|
||||
@@ -48,7 +51,7 @@ NimBLEServer::NimBLEServer() {
|
||||
* @return A reference to the new service object.
|
||||
*/
|
||||
NimBLEService* NimBLEServer::createService(const char* uuid) {
|
||||
return createService(NimBLEUUID(uuid));
|
||||
return createService(NimBLEUUID(uuid));
|
||||
}
|
||||
|
||||
|
||||
@@ -63,20 +66,20 @@ NimBLEService* NimBLEServer::createService(const char* uuid) {
|
||||
* @return A reference to the new service object.
|
||||
*/
|
||||
NimBLEService* NimBLEServer::createService(const NimBLEUUID &uuid, uint32_t numHandles, uint8_t inst_id) {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
|
||||
NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
|
||||
|
||||
// Check that a service with the supplied UUID does not already exist.
|
||||
if (m_serviceMap.getByUUID(uuid) != nullptr) {
|
||||
NIMBLE_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
|
||||
uuid.toString().c_str());
|
||||
}
|
||||
// Check that a service with the supplied UUID does not already exist.
|
||||
if (m_serviceMap.getByUUID(uuid) != nullptr) {
|
||||
NIMBLE_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
|
||||
uuid.toString().c_str());
|
||||
}
|
||||
|
||||
NimBLEService* pService = new NimBLEService(uuid, numHandles, this);
|
||||
pService->m_instId = inst_id;
|
||||
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
|
||||
NimBLEService* pService = new NimBLEService(uuid, numHandles, this);
|
||||
pService->m_instId = inst_id;
|
||||
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< createService");
|
||||
return pService;
|
||||
NIMBLE_LOGD(LOG_TAG, "<< createService");
|
||||
return pService;
|
||||
} // createService
|
||||
|
||||
|
||||
@@ -86,7 +89,7 @@ NimBLEService* NimBLEServer::createService(const NimBLEUUID &uuid, uint32_t numH
|
||||
* @return A reference to the service object.
|
||||
*/
|
||||
NimBLEService* NimBLEServer::getServiceByUUID(const char* uuid) {
|
||||
return m_serviceMap.getByUUID(uuid);
|
||||
return m_serviceMap.getByUUID(uuid);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +99,7 @@ NimBLEService* NimBLEServer::getServiceByUUID(const char* uuid) {
|
||||
* @return A reference to the service object.
|
||||
*/
|
||||
NimBLEService* NimBLEServer::getServiceByUUID(const NimBLEUUID &uuid) {
|
||||
return m_serviceMap.getByUUID(uuid);
|
||||
return m_serviceMap.getByUUID(uuid);
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +109,7 @@ NimBLEService* NimBLEServer::getServiceByUUID(const NimBLEUUID &uuid) {
|
||||
* @return An advertising object.
|
||||
*/
|
||||
NimBLEAdvertising* NimBLEServer::getAdvertising() {
|
||||
return BLEDevice::getAdvertising();
|
||||
return BLEDevice::getAdvertising();
|
||||
}
|
||||
|
||||
|
||||
@@ -116,58 +119,58 @@ NimBLEAdvertising* NimBLEServer::getAdvertising() {
|
||||
* @return Client connection id.
|
||||
*/
|
||||
uint16_t NimBLEServer::getConnId() {
|
||||
return m_connId;
|
||||
return m_connId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Start the GATT server. Required to be called after setup of all
|
||||
* @brief Start the GATT server. Required to be called after setup of all
|
||||
* services and characteristics / descriptors for the NimBLE host to register them.
|
||||
*/
|
||||
void NimBLEServer::start() {
|
||||
if(m_gattsStarted) {
|
||||
NIMBLE_LOGW(LOG_TAG, "Gatt server already started");
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_gattsStarted) {
|
||||
NIMBLE_LOGW(LOG_TAG, "Gatt server already started");
|
||||
return;
|
||||
}
|
||||
|
||||
int rc = ble_gatts_start();
|
||||
if (rc != 0) {
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gatts_start; rc=%d, %s", rc,
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gatts_start; rc=%d, %s", rc,
|
||||
NimBLEUtils::returnCodeToString(rc));
|
||||
abort();
|
||||
}
|
||||
|
||||
#if CONFIG_LOG_DEFAULT_LEVEL > 3 || (ARDUINO_ARCH_ESP32 && CORE_DEBUG_LEVEL >= 4)
|
||||
|
||||
#if CONFIG_LOG_DEFAULT_LEVEL > 3 || (ARDUINO_ARCH_ESP32 && CORE_DEBUG_LEVEL >= 4)
|
||||
ble_gatts_show_local();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
ble_uuid16_t svc = {BLE_UUID_TYPE_16, 0x1801};
|
||||
ble_uuid16_t chr = {BLE_UUID_TYPE_16, 0x2a05};
|
||||
|
||||
rc = ble_gatts_find_chr(&svc.u, &chr.u, NULL, &m_svcChgChrHdl);
|
||||
|
||||
rc = ble_gatts_find_chr(&svc.u, &chr.u, NULL, &m_svcChgChrHdl);
|
||||
if(rc != 0) {
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gatts_find_chr: rc=%d, %s", rc,
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gatts_find_chr: rc=%d, %s", rc,
|
||||
NimBLEUtils::returnCodeToString(rc));
|
||||
abort();
|
||||
}
|
||||
|
||||
|
||||
NIMBLE_LOGI(LOG_TAG, "Service changed characterisic handle: %d", m_svcChgChrHdl);
|
||||
|
||||
|
||||
// Build a map of characteristics with Notify / Indicate capabilities for event handling
|
||||
uint8_t numSvcs = m_serviceMap.getRegisteredServiceCount();
|
||||
NimBLEService* pService = m_serviceMap.getFirst();
|
||||
|
||||
|
||||
for(int i = 0; i < numSvcs; i++) {
|
||||
uint8_t numChrs = pService->m_characteristicMap.getSize();
|
||||
NimBLECharacteristic* pChr = pService->m_characteristicMap.getFirst();
|
||||
|
||||
uint8_t numChrs = pService->m_characteristicMap.getSize();
|
||||
NimBLECharacteristic* pChr = pService->m_characteristicMap.getFirst();
|
||||
|
||||
if(pChr != nullptr) {
|
||||
for( int d = 0; d < numChrs; d++) {
|
||||
// if Notify / Indicate is enabled but we didn't create the descriptor
|
||||
// we do it now.
|
||||
if((pChr->m_properties & BLE_GATT_CHR_F_INDICATE) ||
|
||||
if((pChr->m_properties & BLE_GATT_CHR_F_INDICATE) ||
|
||||
(pChr->m_properties & BLE_GATT_CHR_F_NOTIFY)) {
|
||||
|
||||
|
||||
if(nullptr == pChr->getDescriptorByUUID("2902")) {
|
||||
pChr->createDescriptor("2902");
|
||||
}
|
||||
@@ -179,8 +182,8 @@ void NimBLEServer::start() {
|
||||
}
|
||||
pService = m_serviceMap.getNext();
|
||||
}
|
||||
|
||||
m_gattsStarted = true;
|
||||
|
||||
m_gattsStarted = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -192,13 +195,13 @@ void NimBLEServer::start() {
|
||||
*/
|
||||
int NimBLEServer::disconnect(uint16_t connId, uint8_t reason) {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> disconnect()");
|
||||
|
||||
|
||||
int rc = ble_gap_terminate(connId, reason);
|
||||
if(rc != 0){
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc,
|
||||
NIMBLE_LOGE(LOG_TAG, "ble_gap_terminate failed: rc=%d %s", rc,
|
||||
NimBLEUtils::returnCodeToString(rc));
|
||||
}
|
||||
|
||||
|
||||
return rc;
|
||||
NIMBLE_LOGD(LOG_TAG, "<< disconnect()");
|
||||
}
|
||||
@@ -209,7 +212,7 @@ int NimBLEServer::disconnect(uint16_t connId, uint8_t reason) {
|
||||
* @return The number of connected clients.
|
||||
*/
|
||||
uint32_t NimBLEServer::getConnectedCount() {
|
||||
return m_connectedServersMap.size();
|
||||
return m_connectedServersMap.size();
|
||||
} // getConnectedCount
|
||||
|
||||
|
||||
@@ -222,15 +225,15 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
*
|
||||
*/
|
||||
/*STATIC*/int NimBLEServer::handleGapEvent(struct ble_gap_event *event, void *arg) {
|
||||
NimBLEServer* server = (NimBLEServer*)arg;
|
||||
NIMBLE_LOGD(LOG_TAG, ">> handleGapEvent: %s",
|
||||
NimBLEUtils::gapEventToString(event->type));
|
||||
NimBLEServer* server = (NimBLEServer*)arg;
|
||||
NIMBLE_LOGD(LOG_TAG, ">> handleGapEvent: %s",
|
||||
NimBLEUtils::gapEventToString(event->type));
|
||||
int rc = 0;
|
||||
struct ble_gap_conn_desc desc;
|
||||
|
||||
switch(event->type) {
|
||||
|
||||
case BLE_GAP_EVENT_CONNECT: {
|
||||
|
||||
switch(event->type) {
|
||||
|
||||
case BLE_GAP_EVENT_CONNECT: {
|
||||
if (event->connect.status != 0) {
|
||||
/* Connection failed; resume advertising */
|
||||
NIMBLE_LOGC(LOG_TAG, "Connection failed");
|
||||
@@ -240,21 +243,21 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
else {
|
||||
server->m_connId = event->connect.conn_handle;
|
||||
server->addPeerDevice((void*)server, false, server->m_connId);
|
||||
|
||||
|
||||
ble_gap_conn_desc desc;
|
||||
rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
|
||||
assert(rc == 0);
|
||||
|
||||
|
||||
server->m_pServerCallbacks->onConnect(server);
|
||||
server->m_pServerCallbacks->onConnect(server, &desc);
|
||||
server->m_pServerCallbacks->onConnect(server, &desc);
|
||||
}
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_CONNECT
|
||||
|
||||
|
||||
} // BLE_GAP_EVENT_CONNECT
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_DISCONNECT: {
|
||||
// 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:
|
||||
@@ -264,22 +267,22 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
NIMBLE_LOGC(LOG_TAG, "Disconnect - host reset, rc=%d", event->disconnect.reason);
|
||||
NimBLEDevice::onReset(event->disconnect.reason);
|
||||
break;
|
||||
default:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
server->removePeerDevice(event->disconnect.conn.conn_handle, false);
|
||||
server->m_connId = BLE_HS_CONN_HANDLE_NONE;
|
||||
server->m_connId = BLE_HS_CONN_HANDLE_NONE;
|
||||
server->m_pServerCallbacks->onDisconnect(server);
|
||||
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_DISCONNECT
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_SUBSCRIBE: {
|
||||
NIMBLE_LOGI(LOG_TAG, "subscribe event; cur_notify=%d\n value handle; "
|
||||
"val_handle=%d\n",
|
||||
event->subscribe.cur_notify, event->subscribe.attr_handle);
|
||||
|
||||
|
||||
auto it = server->m_notifyChrMap.find(event->subscribe.attr_handle);
|
||||
if(it != server->m_notifyChrMap.cend()) {
|
||||
(*it).second->setSubscribe(event);
|
||||
@@ -287,7 +290,7 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_SUBSCRIBE
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_MTU: {
|
||||
NIMBLE_LOGI(LOG_TAG, "mtu update event; conn_handle=%d mtu=%d",
|
||||
event->mtu.conn_handle,
|
||||
@@ -295,7 +298,7 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
server->updatePeerMTU(event->mtu.conn_handle, event->mtu.value);
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_MTU
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_NOTIFY_TX: {
|
||||
if(event->notify_tx.indication && event->notify_tx.status != 0) {
|
||||
auto it = server->m_notifyChrMap.find(event->notify_tx.attr_handle);
|
||||
@@ -303,15 +306,15 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
(*it).second->m_semaphoreConfEvt.give(event->notify_tx.status);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_NOTIFY_TX
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_CONN_UPDATE: {
|
||||
NIMBLE_LOGD(LOG_TAG, "Connection parameters updated.");
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_CONN_UPDATE
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_REPEAT_PAIRING: {
|
||||
/* We already have a bond with the peer, but it is attempting to
|
||||
* establish a new secure link. This app sacrifices security for
|
||||
@@ -328,8 +331,8 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
*/
|
||||
return BLE_GAP_REPEAT_PAIRING_RETRY;
|
||||
} // BLE_GAP_EVENT_REPEAT_PAIRING
|
||||
|
||||
case BLE_GAP_EVENT_ENC_CHANGE: {
|
||||
|
||||
case BLE_GAP_EVENT_ENC_CHANGE: {
|
||||
rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
|
||||
if(rc != 0) {
|
||||
return BLE_ATT_ERR_INVALID_HANDLE;
|
||||
@@ -341,10 +344,10 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
} else {
|
||||
server->m_pServerCallbacks->onAuthenticationComplete(&desc);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_ENC_CHANGE
|
||||
|
||||
|
||||
case BLE_GAP_EVENT_PASSKEY_ACTION: {
|
||||
struct ble_sm_io pkey = {0};
|
||||
|
||||
@@ -359,7 +362,7 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
}
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "BLE_SM_IOACT_DISP; 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;
|
||||
@@ -370,11 +373,11 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
} else {
|
||||
pkey.numcmp_accept = server->m_pServerCallbacks->onConfirmPIN(event->passkey.params.numcmp);
|
||||
}
|
||||
|
||||
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "BLE_SM_IOACT_NUMCMP; 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;
|
||||
@@ -383,35 +386,35 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
}
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "BLE_SM_IOACT_OOB; 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();
|
||||
/////////////////////////////////////////////
|
||||
} else {
|
||||
pkey.passkey = server->m_pServerCallbacks->onPassKeyRequest();
|
||||
}
|
||||
}
|
||||
|
||||
rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
||||
NIMBLE_LOGD(LOG_TAG, "BLE_SM_IOACT_INPUT; 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");
|
||||
}
|
||||
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< handleGATTServerEvent");
|
||||
return 0;
|
||||
} // BLE_GAP_EVENT_PASSKEY_ACTION
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
NIMBLE_LOGD(LOG_TAG, "<< handleGATTServerEvent");
|
||||
NIMBLE_LOGD(LOG_TAG, "<< handleGATTServerEvent");
|
||||
return 0;
|
||||
} // handleGATTServerEvent
|
||||
|
||||
@@ -427,10 +430,10 @@ uint32_t NimBLEServer::getConnectedCount() {
|
||||
*/
|
||||
void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks) {
|
||||
if (pCallbacks != nullptr){
|
||||
m_pServerCallbacks = pCallbacks;
|
||||
} else {
|
||||
m_pServerCallbacks = &defaultCallbacks;
|
||||
}
|
||||
m_pServerCallbacks = pCallbacks;
|
||||
} else {
|
||||
m_pServerCallbacks = &defaultCallbacks;
|
||||
}
|
||||
} // setCallbacks
|
||||
|
||||
|
||||
@@ -439,9 +442,9 @@ void NimBLEServer::setCallbacks(NimBLEServerCallbacks* pCallbacks) {
|
||||
*/
|
||||
/*
|
||||
void BLEServer::removeService(BLEService* service) {
|
||||
service->stop();
|
||||
service->executeDelete();
|
||||
m_serviceMap.removeService(service);
|
||||
service->stop();
|
||||
service->executeDelete();
|
||||
m_serviceMap.removeService(service);
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -453,9 +456,9 @@ void BLEServer::removeService(BLEService* service) {
|
||||
* retrieving the advertising object and invoking start upon it.
|
||||
*/
|
||||
void NimBLEServer::startAdvertising() {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> startAdvertising");
|
||||
NimBLEDevice::startAdvertising();
|
||||
NIMBLE_LOGD(LOG_TAG, "<< startAdvertising");
|
||||
NIMBLE_LOGD(LOG_TAG, ">> startAdvertising");
|
||||
NimBLEDevice::startAdvertising();
|
||||
NIMBLE_LOGD(LOG_TAG, "<< startAdvertising");
|
||||
} // startAdvertising
|
||||
|
||||
|
||||
@@ -463,51 +466,51 @@ void NimBLEServer::startAdvertising() {
|
||||
* @brief Stop advertising.
|
||||
*/
|
||||
void NimBLEServer::stopAdvertising() {
|
||||
NIMBLE_LOGD(LOG_TAG, ">> stopAdvertising");
|
||||
NimBLEDevice::stopAdvertising();
|
||||
NIMBLE_LOGD(LOG_TAG, "<< stopAdvertising");
|
||||
NIMBLE_LOGD(LOG_TAG, ">> stopAdvertising");
|
||||
NimBLEDevice::stopAdvertising();
|
||||
NIMBLE_LOGD(LOG_TAG, "<< stopAdvertising");
|
||||
} // startAdvertising
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Allow to connect GATT server to peer device
|
||||
* Probably can be used in ANCS for iPhone
|
||||
*/
|
||||
/*
|
||||
bool BLEServer::connect(BLEAddress address) {
|
||||
esp_bd_addr_t addr;
|
||||
memcpy(&addr, address.getNative(), 6);
|
||||
// Perform the open connection request against the target BLE Server.
|
||||
m_semaphoreOpenEvt.take("connect");
|
||||
esp_err_t errRc = ::esp_ble_gatts_open(
|
||||
getGattsIf(),
|
||||
addr, // address
|
||||
1 // direct connection
|
||||
);
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return false;
|
||||
}
|
||||
esp_bd_addr_t addr;
|
||||
memcpy(&addr, address.getNative(), 6);
|
||||
// Perform the open connection request against the target BLE Server.
|
||||
m_semaphoreOpenEvt.take("connect");
|
||||
esp_err_t errRc = ::esp_ble_gatts_open(
|
||||
getGattsIf(),
|
||||
addr, // address
|
||||
1 // direct connection
|
||||
);
|
||||
if (errRc != ESP_OK) {
|
||||
ESP_LOGE(LOG_TAG, "esp_ble_gattc_open: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
|
||||
ESP_LOGD(LOG_TAG, "<< connect(), rc=%d", rc==ESP_GATT_OK);
|
||||
return rc == ESP_GATT_OK;
|
||||
uint32_t rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete.
|
||||
ESP_LOGD(LOG_TAG, "<< connect(), rc=%d", rc==ESP_GATT_OK);
|
||||
return rc == ESP_GATT_OK;
|
||||
} // connect
|
||||
*/
|
||||
|
||||
|
||||
void NimBLEServerCallbacks::onConnect(NimBLEServer* pServer) {
|
||||
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnect(): Default");
|
||||
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnect(): Default");
|
||||
} // onConnect
|
||||
|
||||
|
||||
void NimBLEServerCallbacks::onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc) {
|
||||
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnect(): Default");
|
||||
NIMBLE_LOGD("NimBLEServerCallbacks", "onConnect(): Default");
|
||||
} // onConnect
|
||||
|
||||
|
||||
void NimBLEServerCallbacks::onDisconnect(NimBLEServer* pServer) {
|
||||
NIMBLE_LOGD("NimBLEServerCallbacks", "onDisconnect(): Default");
|
||||
NIMBLE_LOGD("NimBLEServerCallbacks", "onDisconnect(): Default");
|
||||
} // onDisconnect
|
||||
|
||||
uint32_t NimBLEServerCallbacks::onPassKeyRequest(){
|
||||
@@ -533,14 +536,14 @@ bool NimBLEServerCallbacks::onConfirmPIN(uint32_t pin){
|
||||
|
||||
/* multi connect support */
|
||||
void NimBLEServer::updatePeerMTU(uint16_t conn_id, uint16_t mtu) {
|
||||
const std::map<uint16_t, conn_status_t>::iterator it = m_connectedServersMap.find(conn_id);
|
||||
if (it != m_connectedServersMap.end()) {
|
||||
it->second.mtu = mtu;
|
||||
}
|
||||
const std::map<uint16_t, conn_status_t>::iterator it = m_connectedServersMap.find(conn_id);
|
||||
if (it != m_connectedServersMap.end()) {
|
||||
it->second.mtu = mtu;
|
||||
}
|
||||
}
|
||||
|
||||
std::map<uint16_t, conn_status_t> NimBLEServer::getPeerDevices() {
|
||||
return m_connectedServersMap;
|
||||
return m_connectedServersMap;
|
||||
}
|
||||
|
||||
|
||||
@@ -558,17 +561,17 @@ uint16_t NimBLEServer::getPeerMTU(uint16_t conn_id) {
|
||||
}
|
||||
|
||||
void NimBLEServer::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
|
||||
conn_status_t status = {
|
||||
.peer_device = peer,
|
||||
.connected = true,
|
||||
.mtu = 23
|
||||
};
|
||||
conn_status_t status = {
|
||||
.peer_device = peer,
|
||||
.connected = true,
|
||||
.mtu = 23
|
||||
};
|
||||
|
||||
m_connectedServersMap.insert(std::pair<uint16_t, conn_status_t>(conn_id, status));
|
||||
m_connectedServersMap.insert(std::pair<uint16_t, conn_status_t>(conn_id, status));
|
||||
}
|
||||
|
||||
void NimBLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
|
||||
m_connectedServersMap.erase(conn_id);
|
||||
m_connectedServersMap.erase(conn_id);
|
||||
}
|
||||
/* multi connect support */
|
||||
|
||||
@@ -576,20 +579,20 @@ void NimBLEServer::removePeerDevice(uint16_t conn_id, bool _client) {
|
||||
/**
|
||||
* Update connection parameters can be called only after connection has been established
|
||||
*/
|
||||
void NimBLEServer::updateConnParams(uint16_t conn_handle,
|
||||
uint16_t minInterval, uint16_t maxInterval,
|
||||
void NimBLEServer::updateConnParams(uint16_t conn_handle,
|
||||
uint16_t minInterval, uint16_t maxInterval,
|
||||
uint16_t latency, uint16_t timeout,
|
||||
uint16_t minConnTime, uint16_t maxConnTime)
|
||||
uint16_t minConnTime, uint16_t maxConnTime)
|
||||
{
|
||||
ble_gap_upd_params params;
|
||||
ble_gap_upd_params params;
|
||||
|
||||
params.latency = latency;
|
||||
params.itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms
|
||||
params.itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms
|
||||
params.supervision_timeout = timeout; // timeout = 400*10ms = 4000ms
|
||||
params.latency = latency;
|
||||
params.itvl_max = maxInterval; // max_int = 0x20*1.25ms = 40ms
|
||||
params.itvl_min = minInterval; // min_int = 0x10*1.25ms = 20ms
|
||||
params.supervision_timeout = timeout; // timeout = 400*10ms = 4000ms
|
||||
params.min_ce_len = minConnTime; // Minimum length of connection event in 0.625ms units
|
||||
params.max_ce_len = maxConnTime; // Maximum length of connection event in 0.625ms units
|
||||
|
||||
|
||||
int rc = ble_gap_update_params(conn_handle, ¶ms);
|
||||
if(rc != 0) {
|
||||
NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
|
||||
@@ -602,7 +605,9 @@ void NimBLEServer::onHostReset() {
|
||||
for(auto it = m_notifyChrMap.cbegin(); it != m_notifyChrMap.cend(); ++it) {
|
||||
(*it).second->m_semaphoreConfEvt.give(0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
#endif // CONFIG_BT_ENABLED
|
||||
|
||||
Reference in New Issue
Block a user