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_
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
|
|
|
|
2021-07-31 04:56:52 +02:00
|
|
|
#define NIMBLE_ATT_REMOVE_HIDE 1
|
|
|
|
#define NIMBLE_ATT_REMOVE_DELETE 2
|
|
|
|
|
2020-06-11 16:06:16 +02:00
|
|
|
#include "NimBLEUtils.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAddress.h"
|
|
|
|
#include "NimBLEAdvertising.h"
|
|
|
|
#include "NimBLEService.h"
|
|
|
|
#include "NimBLESecurity.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);
|
|
|
|
NimBLEService* createService(const NimBLEUUID &uuid, uint32_t numHandles=15,
|
|
|
|
uint8_t inst_id=0);
|
2020-07-14 05:24:07 +02:00
|
|
|
void removeService(NimBLEService* service, bool deleteSvc = false);
|
|
|
|
void addService(NimBLEService* service);
|
2020-06-08 02:42:28 +02:00
|
|
|
NimBLEAdvertising* getAdvertising();
|
2020-07-29 04:57:33 +02:00
|
|
|
void setCallbacks(NimBLEServerCallbacks* pCallbacks,
|
|
|
|
bool deleteCallbacks = true);
|
2020-06-08 02:42:28 +02:00
|
|
|
void startAdvertising();
|
|
|
|
void stopAdvertising();
|
|
|
|
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);
|
|
|
|
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);
|
2020-06-08 02:42:28 +02:00
|
|
|
void advertiseOnDisconnect(bool);
|
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;
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
bool m_gattsStarted;
|
2020-06-08 02:42:28 +02:00
|
|
|
bool m_advertiseOnDisconnect;
|
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.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
virtual void onConnect(NimBLEServer* pServer);
|
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.
|
|
|
|
* @param [in] desc A pointer to the connection description structure containig information
|
|
|
|
* about the connection parameters.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
virtual void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
|
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 disconnection.
|
|
|
|
* This is called when a client disconnects.
|
2020-03-30 01:44:20 +02:00
|
|
|
* @param [in] pServer A reference to the %BLE server that received the existing client disconnection.
|
|
|
|
*/
|
|
|
|
virtual void onDisconnect(NimBLEServer* pServer);
|
2020-05-14 06:03:56 +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.
|
|
|
|
* @param [in] desc A pointer to the connection description structure containig information
|
|
|
|
* about the connection.
|
|
|
|
*/
|
|
|
|
virtual void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
|
|
|
|
|
2021-07-20 05:47:59 +02:00
|
|
|
/**
|
|
|
|
* @brief Called when the connection MTU changes.
|
|
|
|
* @param [in] MTU The new MTU value.
|
|
|
|
* @param [in] desc A pointer to the connection description structure containig information
|
|
|
|
* about the connection.
|
|
|
|
*/
|
|
|
|
virtual void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc);
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
//virtual void onPassKeyNotify(uint32_t pass_key);
|
|
|
|
//virtual bool onSecurityRequest();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Called when the pairing procedure is complete.
|
|
|
|
* @param [in] desc A pointer to the struct containing the connection information.\n
|
|
|
|
* This can be used to check the status of the connection encryption/pairing.
|
|
|
|
*/
|
|
|
|
virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED */
|
2020-05-10 15:21:46 +02:00
|
|
|
#endif /* MAIN_NIMBLESERVER_H_ */
|