esp-nimble-cpp  1.4.1
NimBLEServer.h
1 /*
2  * NimBLEServer.h
3  *
4  * Created: on March 2, 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLEServer.h
10  *
11  * Created on: Apr 16, 2017
12  * Author: kolban
13  */
14 
15 #ifndef MAIN_NIMBLESERVER_H_
16 #define MAIN_NIMBLESERVER_H_
17 
18 #include "nimconfig.h"
19 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
20 
21 #define NIMBLE_ATT_REMOVE_HIDE 1
22 #define NIMBLE_ATT_REMOVE_DELETE 2
23 
24 #define onMtuChanged onMTUChange
25 
26 #include "NimBLEUtils.h"
27 #include "NimBLEAddress.h"
28 #if CONFIG_BT_NIMBLE_EXT_ADV
29 #include "NimBLEExtAdvertising.h"
30 #else
31 #include "NimBLEAdvertising.h"
32 #endif
33 #include "NimBLEService.h"
34 #include "NimBLESecurity.h"
35 #include "NimBLEConnInfo.h"
36 
37 
38 class NimBLEService;
41 
42 
46 class NimBLEServer {
47 public:
48  size_t getConnectedCount();
49  NimBLEService* createService(const char* uuid);
51  void removeService(NimBLEService* service, bool deleteSvc = false);
52  void addService(NimBLEService* service);
53  void setCallbacks(NimBLEServerCallbacks* pCallbacks,
54  bool deleteCallbacks = true);
55 #if CONFIG_BT_NIMBLE_EXT_ADV
57  bool startAdvertising(uint8_t inst_id,
58  int duration = 0,
59  int max_events = 0);
60  bool stopAdvertising(uint8_t inst_id);
61 #endif
62 # if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
64  bool startAdvertising();
65 #endif
66  bool stopAdvertising();
67  void start();
68  NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0);
69  NimBLEService* getServiceByUUID(const NimBLEUUID &uuid, uint16_t instanceId = 0);
70  NimBLEService* getServiceByHandle(uint16_t handle);
71  int disconnect(uint16_t connID,
72  uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
73  void updateConnParams(uint16_t conn_handle,
74  uint16_t minInterval, uint16_t maxInterval,
75  uint16_t latency, uint16_t timeout);
76  void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
77  uint16_t getPeerMTU(uint16_t conn_id);
78  std::vector<uint16_t> getPeerDevices();
79  NimBLEConnInfo getPeerInfo(size_t index);
81  NimBLEConnInfo getPeerIDInfo(uint16_t id);
82 #if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
83  void advertiseOnDisconnect(bool);
84 #endif
85 
86 private:
87  NimBLEServer();
88  ~NimBLEServer();
89  friend class NimBLECharacteristic;
90  friend class NimBLEService;
91  friend class NimBLEDevice;
92  friend class NimBLEAdvertising;
93 #if CONFIG_BT_NIMBLE_EXT_ADV
94  friend class NimBLEExtAdvertising;
95  friend class NimBLEExtAdvertisementData;
96 #endif
97 
98  bool m_gattsStarted;
99 #if !CONFIG_BT_NIMBLE_EXT_ADV
100  bool m_advertiseOnDisconnect;
101 #endif
102  bool m_svcChanged;
103  NimBLEServerCallbacks* m_pServerCallbacks;
104  bool m_deleteCallbacks;
105  uint16_t m_indWait[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
106  std::vector<uint16_t> m_connectedPeersVec;
107 
108 // uint16_t m_svcChgChrHdl; // Future use
109 
110  std::vector<NimBLEService*> m_svcVec;
111  std::vector<NimBLECharacteristic*> m_notifyChrVec;
112 
113  static int handleGapEvent(struct ble_gap_event *event, void *arg);
114  void serviceChanged();
115  void resetGATT();
116  bool setIndicateWait(uint16_t conn_handle);
117  void clearIndicateWait(uint16_t conn_handle);
118 }; // NimBLEServer
119 
120 
125 public:
126  virtual ~NimBLEServerCallbacks() {};
127 
133  virtual void onConnect(NimBLEServer* pServer);
134 
142  virtual void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
143 
149  virtual void onDisconnect(NimBLEServer* pServer);
150 
158  virtual void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
159 
166  virtual void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc);
167 
172  virtual uint32_t onPassKeyRequest();
173 
174  //virtual void onPassKeyNotify(uint32_t pass_key);
175  //virtual bool onSecurityRequest();
176 
182  virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);
183 
189  virtual bool onConfirmPIN(uint32_t pin);
190 }; // NimBLEServerCallbacks
191 
192 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
193 #endif /* MAIN_NIMBLESERVER_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:87
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:63
Connection information.
Definition: NimBLEConnInfo.h:9
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
Extended advertising class.
Definition: NimBLEExtAdvertising.h:92
Callbacks associated with the operation of a BLE server.
Definition: NimBLEServer.h:124
virtual void onDisconnect(NimBLEServer *pServer)
Handle a client disconnection. This is called when a client disconnects.
Definition: NimBLEServer.cpp:887
virtual bool onConfirmPIN(uint32_t pin)
Called when using numeric comparision for pairing.
Definition: NimBLEServer.cpp:916
virtual void onAuthenticationComplete(ble_gap_conn_desc *desc)
Called when the pairing procedure is complete.
Definition: NimBLEServer.cpp:913
virtual void onConnect(NimBLEServer *pServer)
Handle a client connection. This is called when a client connects.
Definition: NimBLEServer.cpp:877
virtual void onMTUChange(uint16_t MTU, ble_gap_conn_desc *desc)
Called when the connection MTU changes.
Definition: NimBLEServer.cpp:895
virtual uint32_t onPassKeyRequest()
Called when a client requests a passkey for pairing.
Definition: NimBLEServer.cpp:899
The model of a BLE server.
Definition: NimBLEServer.h:46
void removeService(NimBLEService *service, bool deleteSvc=false)
Remove a service from the server.
Definition: NimBLEServer.cpp:653
void setDataLen(uint16_t conn_handle, uint16_t tx_octets)
Request an update of the data packet length.
Definition: NimBLEServer.cpp:839
NimBLEService * getServiceByHandle(uint16_t handle)
Get a BLE Service by its handle.
Definition: NimBLEServer.cpp:135
NimBLEConnInfo getPeerInfo(size_t index)
Get the connection information of a connected peer by vector index.
Definition: NimBLEServer.cpp:287
void setCallbacks(NimBLEServerCallbacks *pCallbacks, bool deleteCallbacks=true)
Set the server callbacks.
Definition: NimBLEServer.cpp:625
int disconnect(uint16_t connID, uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
Disconnect the specified client with optional reason.
Definition: NimBLEServer.cpp:242
void advertiseOnDisconnect(bool)
Set the server to automatically start advertising when a client disconnects.
Definition: NimBLEServer.cpp:261
void addService(NimBLEService *service)
Adds a service which was either already created but removed from availability, or created and later a...
Definition: NimBLEServer.cpp:691
bool stopAdvertising()
Stop advertising.
Definition: NimBLEServer.cpp:787
NimBLEService * getServiceByUUID(const char *uuid, uint16_t instanceId=0)
Get a BLE Service by its UUID.
Definition: NimBLEServer.cpp:106
size_t getConnectedCount()
Return the number of connected clients.
Definition: NimBLEServer.cpp:270
void updateConnParams(uint16_t conn_handle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
Request an Update the connection parameters:
Definition: NimBLEServer.cpp:810
uint16_t getPeerMTU(uint16_t conn_id)
Get the MTU of the client.
Definition: NimBLEServer.cpp:796
NimBLEService * createService(const char *uuid)
Create a BLE Service.
Definition: NimBLEServer.cpp:72
NimBLEExtAdvertising * getAdvertising()
Retrieve the advertising object that can be used to advertise the existence of the server.
Definition: NimBLEServer.cpp:150
std::vector< uint16_t > getPeerDevices()
Get the vector of the connected client ID's.
Definition: NimBLEServer.cpp:278
NimBLEConnInfo getPeerIDInfo(uint16_t id)
Get the connection information of a connected peer by connection ID.
Definition: NimBLEServer.cpp:320
bool startAdvertising()
Start advertising.
Definition: NimBLEServer.cpp:777
void start()
Start the GATT server. Required to be called after setup of all services and characteristics / descri...
Definition: NimBLEServer.cpp:181
The model of a BLE service.
Definition: NimBLEService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37
#define CONFIG_BT_NIMBLE_MAX_CONNECTIONS
Un-comment to change the number of simultaneous connections (esp controller max is 9)
Definition: nimconfig.h:40