esp-nimble-cpp/src/NimBLEServer.h

177 lines
6.2 KiB
C
Raw Normal View History

2020-03-30 01:44:20 +02:00
/*
* NimBLEServer.h
*
* Created: on March 2, 2020
* Author H2zero
*
2020-03-30 01:44:20 +02:00
* Originally:
*
* BLEServer.h
*
* Created on: Apr 16, 2017
* Author: kolban
*/
#ifndef MAIN_NIMBLESERVER_H_
#define MAIN_NIMBLESERVER_H_
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
#define NIMBLE_ATT_REMOVE_HIDE 1
#define NIMBLE_ATT_REMOVE_DELETE 2
2022-01-02 02:00:45 +01:00
#define onMtuChanged onMTUChange
#include "NimBLEUtils.h"
2020-03-30 01:44:20 +02:00
#include "NimBLEAddress.h"
#if CONFIG_BT_NIMBLE_EXT_ADV
#include "NimBLEExtAdvertising.h"
#else
2020-03-30 01:44:20 +02:00
#include "NimBLEAdvertising.h"
#endif
2020-03-30 01:44:20 +02:00
#include "NimBLEService.h"
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
#include "NimBLEConnInfo.h"
2020-03-30 01:44:20 +02:00
class NimBLEService;
class NimBLECharacteristic;
class NimBLEServerCallbacks;
/**
* @brief The model of a %BLE server.
*/
class NimBLEServer {
public:
size_t getConnectedCount();
NimBLEService* createService(const char* uuid);
NimBLEService* createService(const NimBLEUUID &uuid);
void removeService(NimBLEService* service, bool deleteSvc = false);
void addService(NimBLEService* service);
void setCallbacks(NimBLEServerCallbacks* pCallbacks,
bool deleteCallbacks = true);
#if CONFIG_BT_NIMBLE_EXT_ADV
NimBLEExtAdvertising* getAdvertising();
bool startAdvertising(uint8_t inst_id,
int duration = 0,
int max_events = 0);
bool stopAdvertising(uint8_t inst_id);
2022-08-27 20:38:53 +02:00
#endif
# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
NimBLEAdvertising* getAdvertising();
bool startAdvertising(uint32_t duration = 0);
#endif
bool stopAdvertising();
void start();
NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0);
NimBLEService* getServiceByUUID(const NimBLEUUID &uuid, uint16_t instanceId = 0);
NimBLEService* getServiceByHandle(uint16_t handle);
int disconnect(uint16_t connID,
uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
void updateConnParams(uint16_t conn_handle,
uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout);
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
uint16_t getPeerMTU(uint16_t conn_id);
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
std::vector<uint16_t> getPeerDevices();
NimBLEConnInfo getPeerInfo(size_t index);
NimBLEConnInfo getPeerInfo(const NimBLEAddress& address);
NimBLEConnInfo getPeerIDInfo(uint16_t id);
#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
void advertiseOnDisconnect(bool);
#endif
2020-03-30 01:44:20 +02:00
private:
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;
#if !CONFIG_BT_NIMBLE_EXT_ADV
bool m_advertiseOnDisconnect;
#endif
bool m_svcChanged;
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
2020-03-30 01:44:20 +02:00
std::vector<NimBLEService*> m_svcVec;
std::vector<NimBLECharacteristic*> m_notifyChrVec;
2020-03-30 01:44:20 +02:00
static int handleGapEvent(struct ble_gap_event *event, void *arg);
void serviceChanged();
void resetGATT();
bool setIndicateWait(uint16_t conn_handle);
void clearIndicateWait(uint16_t conn_handle);
2020-03-30 01:44:20 +02:00
}; // NimBLEServer
/**
* @brief Callbacks associated with the operation of a %BLE server.
*/
class NimBLEServerCallbacks {
public:
virtual ~NimBLEServerCallbacks() {};
2020-03-30 01:44:20 +02:00
/**
* @brief Handle a client connection.
* This is called when a client connects.
* @param [in] pServer A pointer to the %BLE server that received the client connection.
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
* about the peer connection parameters.
2020-03-30 01:44:20 +02:00
*/
virtual void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo);
2020-03-30 01:44:20 +02:00
/**
* @brief Handle a client disconnection.
* This is called when a client discconnects.
* @param [in] pServer A pointer to the %BLE server that received the client disconnection.
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
* about the peer connection parameters.
* @param [in] reason The reason code for the disconnection.
*/
virtual void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason);
/**
* @brief Called when the connection MTU changes.
* @param [in] MTU The new MTU value.
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
* about the peer connection parameters.
*/
virtual void onMTUChange(uint16_t MTU, NimBLEConnInfo& connInfo);
/**
* @brief Called when a client requests a passkey for pairing.
* @return The passkey to be sent to the client.
*/
virtual uint32_t onPassKeyRequest();
/**
* @brief Called when the pairing procedure is complete.
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
* about the peer connection parameters.
*/
virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo);
/**
* @brief Called when using numeric comparision for pairing.
* @param [in] pin The pin to compare with the client.
* @return True to accept the pin.
*/
virtual bool onConfirmPIN(uint32_t pin);
}; // NimBLEServerCallbacks
2020-03-30 01:44:20 +02:00
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
#endif /* MAIN_NIMBLESERVER_H_ */