[BREAKING] Refactor NimBLEServer

* General code cleanup
* `NimBLEServerCallbacks::onMTUChanged` renamed to `NimBLEServerCallbacks::onMTUChange` to be consistent with the client callback.
* `NimBLEServer::getPeerIDInfo` renamed to `NimBLEServer::getPeerInfoByHandle` to better describe it's use.
* Use a std::array to store client connection handles instead of std::vector to reduce memory allocation.
* `NimBLEServer::disconnect` now returns `bool`, true = success,  instead of `int` to be consistent with the rest of the library.
This commit is contained in:
h2zero 2024-11-24 19:59:56 -07:00 committed by h2zero
parent c4c9f7913a
commit d9178cfa9b
5 changed files with 448 additions and 510 deletions

View file

@ -199,12 +199,7 @@ bool NimBLEAdvertising::start(uint32_t duration, const NimBLEAddress* dirAddr) {
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) # if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
NimBLEServer* pServer = NimBLEDevice::getServer(); NimBLEServer* pServer = NimBLEDevice::getServer();
if (pServer != nullptr) { if (pServer != nullptr) {
if (!pServer->m_gattsStarted) { pServer->start(); // make sure the GATT server is ready before advertising
pServer->start();
} else if (pServer->getConnectedCount() >= NIMBLE_MAX_CONNECTIONS) {
NIMBLE_LOGE(LOG_TAG, "Unable to advertise; Max connections reached");
return false;
}
} }
# endif # endif

View file

@ -62,9 +62,7 @@ bool NimBLEExtAdvertising::setInstanceData(uint8_t instId, NimBLEExtAdvertisemen
# if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) # if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
NimBLEServer* pServer = NimBLEDevice::getServer(); NimBLEServer* pServer = NimBLEDevice::getServer();
if (pServer != nullptr) { if (pServer != nullptr) {
if (!pServer->m_gattsStarted) { pServer->start(); // make sure the GATT server is ready before advertising
pServer->start();
}
} }
int rc = ble_gap_ext_adv_configure( int rc = ble_gap_ext_adv_configure(

File diff suppressed because it is too large Load diff

View file

@ -12,128 +12,127 @@
* Author: kolban * Author: kolban
*/ */
#ifndef MAIN_NIMBLESERVER_H_ #ifndef NIMBLE_CPP_SERVER_H_
#define MAIN_NIMBLESERVER_H_ #define NIMBLE_CPP_SERVER_H_
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
class NimBLEServer; # if defined(CONFIG_NIMBLE_CPP_IDF)
# include "host/ble_gap.h"
# else
# include "nimble/nimble/host/include/host/ble_gap.h"
# endif
/**** FIX COMPILATION ****/
# undef min
# undef max
/**************************/
# include <string>
# include <vector>
# include <array>
# define NIMBLE_ATT_REMOVE_HIDE 1
# define NIMBLE_ATT_REMOVE_DELETE 2
class NimBLEService;
class NimBLEServerCallbacks; class NimBLEServerCallbacks;
class NimBLEUUID;
#include "NimBLEUtils.h" class NimBLEConnInfo;
#include "NimBLEAddress.h" class NimBLEAddress;
#if CONFIG_BT_NIMBLE_EXT_ADV class NimBLEService;
#include "NimBLEExtAdvertising.h" class NimBLECharacteristic;
#else # if CONFIG_BT_NIMBLE_EXT_ADV
#include "NimBLEAdvertising.h" class NimBLEExtAdvertising;
#endif # else
#include "NimBLEService.h" class NimBLEAdvertising;
#include "NimBLECharacteristic.h" # endif
#include "NimBLEConnInfo.h"
#define NIMBLE_ATT_REMOVE_HIDE 1
#define NIMBLE_ATT_REMOVE_DELETE 2
#define onMtuChanged onMTUChange
/** /**
* @brief The model of a %BLE server. * @brief The model of a BLE server.
*/ */
class NimBLEServer { class NimBLEServer {
public: public:
size_t getConnectedCount(); void start();
uint8_t getConnectedCount() const;
bool disconnect(uint16_t connHandle, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
bool disconnect(const NimBLEConnInfo& connInfo, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
void setCallbacks(NimBLEServerCallbacks* pCallbacks, bool deleteCallbacks = true);
void updateConnParams(uint16_t connHandle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout) const;
NimBLEService* createService(const char* uuid); NimBLEService* createService(const char* uuid);
NimBLEService* createService(const NimBLEUUID &uuid); NimBLEService* createService(const NimBLEUUID& uuid);
NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0) const;
NimBLEService* getServiceByUUID(const NimBLEUUID& uuid, uint16_t instanceId = 0) const;
NimBLEService* getServiceByHandle(uint16_t handle) const;
void removeService(NimBLEService* service, bool deleteSvc = false); void removeService(NimBLEService* service, bool deleteSvc = false);
void addService(NimBLEService* service); void addService(NimBLEService* service);
void setCallbacks(NimBLEServerCallbacks* pCallbacks, uint16_t getPeerMTU(uint16_t connHandle) const;
bool deleteCallbacks = true); std::vector<uint16_t> getPeerDevices() const;
#if CONFIG_BT_NIMBLE_EXT_ADV NimBLEConnInfo getPeerInfo(uint8_t index) const;
NimBLEExtAdvertising* getAdvertising(); NimBLEConnInfo getPeerInfo(const NimBLEAddress& address) const;
bool startAdvertising(uint8_t inst_id, NimBLEConnInfo getPeerInfoByHandle(uint16_t connHandle) const;
int duration = 0, std::string getPeerName(const NimBLEConnInfo& connInfo) const;
int max_events = 0); void getPeerNameOnConnect(bool enable);
bool stopAdvertising(uint8_t inst_id); void advertiseOnDisconnect(bool enable);
void setDataLen(uint16_t connHandle, uint16_t tx_octets) const;
# if CONFIG_BT_NIMBLE_EXT_ADV
NimBLEExtAdvertising* getAdvertising() const;
bool startAdvertising(uint8_t instanceId, int duration = 0, int maxEvents = 0) const;
bool stopAdvertising(uint8_t instanceId) const;
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions); bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy); bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);
#endif # endif
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
NimBLEAdvertising* getAdvertising(); # if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
bool startAdvertising(uint32_t duration = 0); NimBLEAdvertising* getAdvertising() const;
#endif bool startAdvertising(uint32_t duration = 0) const;
bool stopAdvertising(); bool stopAdvertising() const;
void start(); # endif
NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0);
NimBLEService* getServiceByUUID(const NimBLEUUID &uuid, uint16_t instanceId = 0); private:
NimBLEService* getServiceByHandle(uint16_t handle); friend class NimBLEDevice;
int disconnect(uint16_t connID, friend class NimBLEService;
uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); friend class NimBLECharacteristic;
int disconnect(const NimBLEConnInfo &connInfo, # if CONFIG_BT_NIMBLE_EXT_ADV
uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); friend class NimBLEExtAdvertising;
void updateConnParams(uint16_t conn_handle, # else
uint16_t minInterval, uint16_t maxInterval, friend class NimBLEAdvertising;
uint16_t latency, uint16_t timeout); # endif
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
uint16_t getPeerMTU(uint16_t conn_id);
std::vector<uint16_t> getPeerDevices();
NimBLEConnInfo getPeerInfo(size_t index);
NimBLEConnInfo getPeerInfo(const NimBLEAddress& address);
NimBLEConnInfo getPeerIDInfo(uint16_t id);
std::string getPeerName(const NimBLEConnInfo& connInfo);
void getPeerNameOnConnect(bool enable);
#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
void advertiseOnDisconnect(bool);
#endif
private:
NimBLEServer(); NimBLEServer();
~NimBLEServer(); ~NimBLEServer();
friend class NimBLECharacteristic;
friend class NimBLEService;
friend class NimBLEDevice;
friend class NimBLEAdvertising;
#if CONFIG_BT_NIMBLE_EXT_ADV
friend class NimBLEExtAdvertising;
friend class NimBLEExtAdvertisementData;
#endif
bool m_gattsStarted; bool m_gattsStarted : 1;
#if !CONFIG_BT_NIMBLE_EXT_ADV bool m_getPeerNameOnConnect : 1;
bool m_advertiseOnDisconnect; bool m_svcChanged : 1;
#endif bool m_deleteCallbacks : 1;
bool m_getPeerNameOnConnect; # if !CONFIG_BT_NIMBLE_EXT_ADV
bool m_svcChanged; bool m_advertiseOnDisconnect : 1;
# endif
NimBLEServerCallbacks* m_pServerCallbacks; NimBLEServerCallbacks* m_pServerCallbacks;
bool m_deleteCallbacks;
uint16_t m_indWait[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
std::vector<uint16_t> m_connectedPeersVec;
// uint16_t m_svcChgChrHdl; // Future use
std::vector<NimBLEService*> m_svcVec; std::vector<NimBLEService*> m_svcVec;
std::vector<NimBLECharacteristic*> m_notifyChrVec; std::vector<NimBLECharacteristic*> m_notifyChrVec;
std::array<uint16_t, CONFIG_BT_NIMBLE_MAX_CONNECTIONS> m_connectedPeers;
std::array<uint16_t, CONFIG_BT_NIMBLE_MAX_CONNECTIONS> m_indWait;
static int handleGapEvent(struct ble_gap_event *event, void *arg); static int handleGapEvent(struct ble_gap_event* event, void* arg);
static int peerNameCB(uint16_t conn_handle, const struct ble_gatt_error *error, static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
struct ble_gatt_attr *attr, void *arg); static int peerNameCB(uint16_t connHandle, const ble_gatt_error* error, ble_gatt_attr* attr, void* arg);
std::string getPeerNameInternal(uint16_t conn_handle, int cb_type = -1); std::string getPeerNameImpl(uint16_t connHandle, int cb_type = -1) const;
void serviceChanged(); void serviceChanged();
void resetGATT(); void resetGATT();
bool setIndicateWait(uint16_t conn_handle); bool setIndicateWait(uint16_t connHandle);
void clearIndicateWait(uint16_t conn_handle); void clearIndicateWait(uint16_t connHandle);
static int handleGattEvent(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
}; // NimBLEServer }; // NimBLEServer
/** /**
* @brief Callbacks associated with the operation of a %BLE server. * @brief Callbacks associated with the operation of a %BLE server.
*/ */
class NimBLEServerCallbacks { class NimBLEServerCallbacks {
public: public:
virtual ~NimBLEServerCallbacks() {}; virtual ~NimBLEServerCallbacks() {};
/** /**
@ -232,5 +231,5 @@ public:
# endif # endif
}; // NimBLEServerCallbacks }; // NimBLEServerCallbacks
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */ #endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
#endif /* MAIN_NIMBLESERVER_H_ */ #endif // NIMBLE_CPP_SERVER_H_

View file

@ -17,8 +17,13 @@
#include "nimconfig.h" #include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL) #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
# include "NimBLEDevice.h"
# include "NimBLEService.h" # include "NimBLEService.h"
# if CONFIG_BT_NIMBLE_EXT_ADV
# include "NimBLEExtAdvertising.h"
# else
# include "NimBLEAdvertising.h"
# endif
# include "NimBLEDevice.h"
# include "NimBLEUtils.h" # include "NimBLEUtils.h"
# include "NimBLELog.h" # include "NimBLELog.h"