mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-12-18 01:00:47 +01:00
[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:
parent
c4c9f7913a
commit
d9178cfa9b
5 changed files with 448 additions and 510 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
@ -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();
|
||||||
NimBLEService* createService(const char* uuid);
|
uint8_t getConnectedCount() const;
|
||||||
NimBLEService* createService(const NimBLEUUID &uuid);
|
bool disconnect(uint16_t connHandle, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
|
||||||
void removeService(NimBLEService* service, bool deleteSvc = false);
|
bool disconnect(const NimBLEConnInfo& connInfo, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
|
||||||
void addService(NimBLEService* service);
|
void setCallbacks(NimBLEServerCallbacks* pCallbacks, bool deleteCallbacks = true);
|
||||||
void setCallbacks(NimBLEServerCallbacks* pCallbacks,
|
void updateConnParams(uint16_t connHandle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout) const;
|
||||||
bool deleteCallbacks = true);
|
NimBLEService* createService(const char* uuid);
|
||||||
#if CONFIG_BT_NIMBLE_EXT_ADV
|
NimBLEService* createService(const NimBLEUUID& uuid);
|
||||||
NimBLEExtAdvertising* getAdvertising();
|
NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0) const;
|
||||||
bool startAdvertising(uint8_t inst_id,
|
NimBLEService* getServiceByUUID(const NimBLEUUID& uuid, uint16_t instanceId = 0) const;
|
||||||
int duration = 0,
|
NimBLEService* getServiceByHandle(uint16_t handle) const;
|
||||||
int max_events = 0);
|
void removeService(NimBLEService* service, bool deleteSvc = false);
|
||||||
bool stopAdvertising(uint8_t inst_id);
|
void addService(NimBLEService* service);
|
||||||
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
|
uint16_t getPeerMTU(uint16_t connHandle) const;
|
||||||
bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);
|
std::vector<uint16_t> getPeerDevices() const;
|
||||||
#endif
|
NimBLEConnInfo getPeerInfo(uint8_t index) const;
|
||||||
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
|
NimBLEConnInfo getPeerInfo(const NimBLEAddress& address) const;
|
||||||
NimBLEAdvertising* getAdvertising();
|
NimBLEConnInfo getPeerInfoByHandle(uint16_t connHandle) const;
|
||||||
bool startAdvertising(uint32_t duration = 0);
|
std::string getPeerName(const NimBLEConnInfo& connInfo) const;
|
||||||
#endif
|
void getPeerNameOnConnect(bool enable);
|
||||||
bool stopAdvertising();
|
void advertiseOnDisconnect(bool enable);
|
||||||
void start();
|
void setDataLen(uint16_t connHandle, uint16_t tx_octets) const;
|
||||||
NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0);
|
|
||||||
NimBLEService* getServiceByUUID(const NimBLEUUID &uuid, uint16_t instanceId = 0);
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
||||||
NimBLEService* getServiceByHandle(uint16_t handle);
|
NimBLEExtAdvertising* getAdvertising() const;
|
||||||
int disconnect(uint16_t connID,
|
bool startAdvertising(uint8_t instanceId, int duration = 0, int maxEvents = 0) const;
|
||||||
uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
|
bool stopAdvertising(uint8_t instanceId) const;
|
||||||
int disconnect(const NimBLEConnInfo &connInfo,
|
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
|
||||||
uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
|
bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);
|
||||||
void updateConnParams(uint16_t conn_handle,
|
# endif
|
||||||
uint16_t minInterval, uint16_t maxInterval,
|
|
||||||
uint16_t latency, uint16_t timeout);
|
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
|
||||||
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
|
NimBLEAdvertising* getAdvertising() const;
|
||||||
uint16_t getPeerMTU(uint16_t conn_id);
|
bool startAdvertising(uint32_t duration = 0) const;
|
||||||
std::vector<uint16_t> getPeerDevices();
|
bool stopAdvertising() const;
|
||||||
NimBLEConnInfo getPeerInfo(size_t index);
|
# endif
|
||||||
NimBLEConnInfo getPeerInfo(const NimBLEAddress& address);
|
|
||||||
NimBLEConnInfo getPeerIDInfo(uint16_t id);
|
private:
|
||||||
std::string getPeerName(const NimBLEConnInfo& connInfo);
|
friend class NimBLEDevice;
|
||||||
void getPeerNameOnConnect(bool enable);
|
friend class NimBLEService;
|
||||||
#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
|
friend class NimBLECharacteristic;
|
||||||
void advertiseOnDisconnect(bool);
|
# if CONFIG_BT_NIMBLE_EXT_ADV
|
||||||
#endif
|
friend class NimBLEExtAdvertising;
|
||||||
|
# else
|
||||||
|
friend class NimBLEAdvertising;
|
||||||
|
# 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;
|
||||||
NimBLEServerCallbacks* m_pServerCallbacks;
|
# endif
|
||||||
bool m_deleteCallbacks;
|
NimBLEServerCallbacks* m_pServerCallbacks;
|
||||||
uint16_t m_indWait[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
|
std::vector<NimBLEService*> m_svcVec;
|
||||||
std::vector<uint16_t> m_connectedPeersVec;
|
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;
|
||||||
|
|
||||||
// uint16_t m_svcChgChrHdl; // Future use
|
static int handleGapEvent(struct ble_gap_event* event, void* arg);
|
||||||
|
static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
|
||||||
std::vector<NimBLEService*> m_svcVec;
|
static int peerNameCB(uint16_t connHandle, const ble_gatt_error* error, ble_gatt_attr* attr, void* arg);
|
||||||
std::vector<NimBLECharacteristic*> m_notifyChrVec;
|
std::string getPeerNameImpl(uint16_t connHandle, int cb_type = -1) const;
|
||||||
|
void serviceChanged();
|
||||||
static int handleGapEvent(struct ble_gap_event *event, void *arg);
|
void resetGATT();
|
||||||
static int peerNameCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
bool setIndicateWait(uint16_t connHandle);
|
||||||
struct ble_gatt_attr *attr, void *arg);
|
void clearIndicateWait(uint16_t connHandle);
|
||||||
std::string getPeerNameInternal(uint16_t conn_handle, int cb_type = -1);
|
|
||||||
void serviceChanged();
|
|
||||||
void resetGATT();
|
|
||||||
bool setIndicateWait(uint16_t conn_handle);
|
|
||||||
void clearIndicateWait(uint16_t conn_handle);
|
|
||||||
|
|
||||||
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_
|
||||||
|
|
|
@ -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"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue