2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEServer.h
|
|
|
|
*
|
|
|
|
* Created: on March 2, 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
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_
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-07-31 04:56:52 +02:00
|
|
|
#define NIMBLE_ATT_REMOVE_HIDE 1
|
|
|
|
#define NIMBLE_ATT_REMOVE_DELETE 2
|
|
|
|
|
2022-01-02 02:00:45 +01:00
|
|
|
#define onMtuChanged onMTUChange
|
|
|
|
|
2020-06-11 16:06:16 +02:00
|
|
|
#include "NimBLEUtils.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAddress.h"
|
2022-04-10 18:21:45 +02:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
#include "NimBLEExtAdvertising.h"
|
|
|
|
#else
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAdvertising.h"
|
2022-04-10 18:21:45 +02:00
|
|
|
#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-06-11 16:06:16 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
class NimBLEService;
|
|
|
|
class NimBLECharacteristic;
|
|
|
|
class NimBLEServerCallbacks;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The model of a %BLE server.
|
|
|
|
*/
|
|
|
|
class NimBLEServer {
|
|
|
|
public:
|
2020-06-08 02:42:28 +02:00
|
|
|
size_t getConnectedCount();
|
|
|
|
NimBLEService* createService(const char* uuid);
|
2021-12-29 16:12:07 +01:00
|
|
|
NimBLEService* createService(const NimBLEUUID &uuid);
|
2020-07-14 05:24:07 +02:00
|
|
|
void removeService(NimBLEService* service, bool deleteSvc = false);
|
|
|
|
void addService(NimBLEService* service);
|
2020-07-29 04:57:33 +02:00
|
|
|
void setCallbacks(NimBLEServerCallbacks* pCallbacks,
|
|
|
|
bool deleteCallbacks = true);
|
2022-04-10 18:21:45 +02:00
|
|
|
#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_)
|
2022-04-10 18:21:45 +02:00
|
|
|
NimBLEAdvertising* getAdvertising();
|
2022-08-27 03:32:01 +02:00
|
|
|
bool startAdvertising(uint32_t duration = 0);
|
2022-04-10 18:21:45 +02:00
|
|
|
#endif
|
|
|
|
bool stopAdvertising();
|
2020-06-08 02:42:28 +02:00
|
|
|
void start();
|
2021-02-08 16:28:32 +01:00
|
|
|
NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0);
|
|
|
|
NimBLEService* getServiceByUUID(const NimBLEUUID &uuid, uint16_t instanceId = 0);
|
|
|
|
NimBLEService* getServiceByHandle(uint16_t handle);
|
2020-06-08 02:42:28 +02:00
|
|
|
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);
|
2021-09-13 03:09:02 +02:00
|
|
|
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
|
2020-06-08 02:42:28 +02:00
|
|
|
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);
|
2022-04-10 18:21:45 +02:00
|
|
|
#if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
|
2020-06-08 02:42:28 +02:00
|
|
|
void advertiseOnDisconnect(bool);
|
2022-04-10 18:21:45 +02:00
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
NimBLEServer();
|
2020-07-14 05:24:07 +02:00
|
|
|
~NimBLEServer();
|
2020-06-08 02:42:28 +02:00
|
|
|
friend class NimBLECharacteristic;
|
2021-07-31 04:56:52 +02:00
|
|
|
friend class NimBLEService;
|
2020-06-08 02:42:28 +02:00
|
|
|
friend class NimBLEDevice;
|
|
|
|
friend class NimBLEAdvertising;
|
2022-04-10 18:21:45 +02:00
|
|
|
#if CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
friend class NimBLEExtAdvertising;
|
|
|
|
friend class NimBLEExtAdvertisementData;
|
|
|
|
#endif
|
2020-06-08 02:42:28 +02:00
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
bool m_gattsStarted;
|
2022-04-10 18:21:45 +02:00
|
|
|
#if !CONFIG_BT_NIMBLE_EXT_ADV
|
2020-06-08 02:42:28 +02:00
|
|
|
bool m_advertiseOnDisconnect;
|
2022-04-10 18:21:45 +02:00
|
|
|
#endif
|
2020-07-14 05:24:07 +02:00
|
|
|
bool m_svcChanged;
|
2020-06-08 02:42:28 +02:00
|
|
|
NimBLEServerCallbacks* m_pServerCallbacks;
|
2020-07-29 04:57:33 +02:00
|
|
|
bool m_deleteCallbacks;
|
2021-05-17 22:08:02 +02:00
|
|
|
uint16_t m_indWait[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
|
2020-06-08 02:42:28 +02:00
|
|
|
std::vector<uint16_t> m_connectedPeersVec;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
// uint16_t m_svcChgChrHdl; // Future use
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
std::vector<NimBLEService*> m_svcVec;
|
|
|
|
std::vector<NimBLECharacteristic*> m_notifyChrVec;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
static int handleGapEvent(struct ble_gap_event *event, void *arg);
|
2021-07-31 04:56:52 +02:00
|
|
|
void serviceChanged();
|
2020-07-14 05:24:07 +02:00
|
|
|
void resetGATT();
|
2021-05-17 22:08:02 +02:00
|
|
|
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-07-09 03:27:26 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +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.
|
2022-08-27 16:28:15 +02:00
|
|
|
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
|
|
|
|
* about the peer connection parameters.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2022-08-27 16:28:15 +02:00
|
|
|
virtual void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo);
|
2020-07-09 03:27:26 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-12-05 03:03:52 +01: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.
|
2022-08-27 16:28:15 +02:00
|
|
|
* @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.
|
2020-12-05 03:03:52 +01:00
|
|
|
*/
|
2022-08-27 16:28:15 +02:00
|
|
|
virtual void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason);
|
2020-12-05 03:03:52 +01:00
|
|
|
|
2022-08-27 16:28:15 +02:00
|
|
|
/**
|
2021-07-20 05:47:59 +02:00
|
|
|
* @brief Called when the connection MTU changes.
|
|
|
|
* @param [in] MTU The new MTU value.
|
2022-08-27 16:28:15 +02:00
|
|
|
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
|
|
|
|
* about the peer connection parameters.
|
2021-07-20 05:47:59 +02:00
|
|
|
*/
|
2022-08-27 16:28:15 +02:00
|
|
|
virtual void onMTUChange(uint16_t MTU, NimBLEConnInfo& connInfo);
|
2021-07-20 05:47:59 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @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.
|
2022-08-27 16:28:15 +02:00
|
|
|
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
|
|
|
|
* about the peer connection parameters.
|
2020-07-09 03:27:26 +02:00
|
|
|
*/
|
2022-08-27 16:28:15 +02:00
|
|
|
virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo);
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
2020-06-08 02:42:28 +02:00
|
|
|
}; // NimBLEServerCallbacks
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
|
2020-05-10 15:21:46 +02:00
|
|
|
#endif /* MAIN_NIMBLESERVER_H_ */
|