esp-nimble-cpp  1.3.2
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 #include "NimBLEAdvertising.h"
29 #include "NimBLEService.h"
30 #include "NimBLESecurity.h"
31 #include "NimBLEConnInfo.h"
32 
33 
34 class NimBLEService;
37 
38 
42 class NimBLEServer {
43 public:
44  size_t getConnectedCount();
45  NimBLEService* createService(const char* uuid);
47  void removeService(NimBLEService* service, bool deleteSvc = false);
48  void addService(NimBLEService* service);
50  void setCallbacks(NimBLEServerCallbacks* pCallbacks,
51  bool deleteCallbacks = true);
52  void startAdvertising();
53  void stopAdvertising();
54  void start();
55  NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0);
56  NimBLEService* getServiceByUUID(const NimBLEUUID &uuid, uint16_t instanceId = 0);
57  NimBLEService* getServiceByHandle(uint16_t handle);
58  int disconnect(uint16_t connID,
59  uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
60  void updateConnParams(uint16_t conn_handle,
61  uint16_t minInterval, uint16_t maxInterval,
62  uint16_t latency, uint16_t timeout);
63  void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
64  uint16_t getPeerMTU(uint16_t conn_id);
65  std::vector<uint16_t> getPeerDevices();
66  NimBLEConnInfo getPeerInfo(size_t index);
68  NimBLEConnInfo getPeerIDInfo(uint16_t id);
69  void advertiseOnDisconnect(bool);
70 
71 private:
72  NimBLEServer();
73  ~NimBLEServer();
74  friend class NimBLECharacteristic;
75  friend class NimBLEService;
76  friend class NimBLEDevice;
77  friend class NimBLEAdvertising;
78 
79  bool m_gattsStarted;
80  bool m_advertiseOnDisconnect;
81  bool m_svcChanged;
82  NimBLEServerCallbacks* m_pServerCallbacks;
83  bool m_deleteCallbacks;
84  uint16_t m_indWait[CONFIG_BT_NIMBLE_MAX_CONNECTIONS];
85  std::vector<uint16_t> m_connectedPeersVec;
86 
87 // uint16_t m_svcChgChrHdl; // Future use
88 
89  std::vector<NimBLEService*> m_svcVec;
90  std::vector<NimBLECharacteristic*> m_notifyChrVec;
91 
92  static int handleGapEvent(struct ble_gap_event *event, void *arg);
93  void serviceChanged();
94  void resetGATT();
95  bool setIndicateWait(uint16_t conn_handle);
96  void clearIndicateWait(uint16_t conn_handle);
97 }; // NimBLEServer
98 
99 
104 public:
105  virtual ~NimBLEServerCallbacks() {};
106 
112  virtual void onConnect(NimBLEServer* pServer);
113 
121  virtual void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
122 
128  virtual void onDisconnect(NimBLEServer* pServer);
129 
137  virtual void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
138 
145  virtual void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc);
146 
151  virtual uint32_t onPassKeyRequest();
152 
153  //virtual void onPassKeyNotify(uint32_t pass_key);
154  //virtual bool onSecurityRequest();
155 
161  virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);
162 
168  virtual bool onConfirmPIN(uint32_t pin);
169 }; // NimBLEServerCallbacks
170 
171 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL */
172 #endif /* MAIN_NIMBLESERVER_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
Perform and manage BLE advertising.
Definition: NimBLEAdvertising.h:85
The model of a BLE Characteristic.
Definition: NimBLECharacteristic.h:62
Connection information.
Definition: NimBLEConnInfo.h:9
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:92
Callbacks associated with the operation of a BLE server.
Definition: NimBLEServer.h:103
virtual void onDisconnect(NimBLEServer *pServer)
Handle a client disconnection. This is called when a client disconnects.
Definition: NimBLEServer.cpp:832
virtual bool onConfirmPIN(uint32_t pin)
Called when using numeric comparision for pairing.
Definition: NimBLEServer.cpp:861
virtual void onAuthenticationComplete(ble_gap_conn_desc *desc)
Called when the pairing procedure is complete.
Definition: NimBLEServer.cpp:858
virtual void onConnect(NimBLEServer *pServer)
Handle a client connection. This is called when a client connects.
Definition: NimBLEServer.cpp:822
virtual void onMTUChange(uint16_t MTU, ble_gap_conn_desc *desc)
Called when the connection MTU changes.
Definition: NimBLEServer.cpp:840
virtual uint32_t onPassKeyRequest()
Called when a client requests a passkey for pairing.
Definition: NimBLEServer.cpp:844
The model of a BLE server.
Definition: NimBLEServer.h:42
void removeService(NimBLEService *service, bool deleteSvc=false)
Remove a service from the server.
Definition: NimBLEServer.cpp:630
void setDataLen(uint16_t conn_handle, uint16_t tx_octets)
Request an update of the data packet length.
Definition: NimBLEServer.cpp:784
NimBLEService * getServiceByHandle(uint16_t handle)
Get a BLE Service by its handle.
Definition: NimBLEServer.cpp:133
void startAdvertising()
Start advertising.
Definition: NimBLEServer.cpp:724
NimBLEConnInfo getPeerInfo(size_t index)
Get the connection information of a connected peer by vector index.
Definition: NimBLEServer.cpp:273
void setCallbacks(NimBLEServerCallbacks *pCallbacks, bool deleteCallbacks=true)
Set the server callbacks.
Definition: NimBLEServer.cpp:602
int disconnect(uint16_t connID, uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
Disconnect the specified client with optional reason.
Definition: NimBLEServer.cpp:229
void advertiseOnDisconnect(bool)
Set the server to automatically start advertising when a client disconnects.
Definition: NimBLEServer.cpp:247
void addService(NimBLEService *service)
Adds a service which was either already created but removed from availability, or created and later a...
Definition: NimBLEServer.cpp:666
NimBLEService * getServiceByUUID(const char *uuid, uint16_t instanceId=0)
Get a BLE Service by its UUID.
Definition: NimBLEServer.cpp:104
size_t getConnectedCount()
Return the number of connected clients.
Definition: NimBLEServer.cpp:256
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:755
uint16_t getPeerMTU(uint16_t conn_id)
Get the MTU of the client.
Definition: NimBLEServer.cpp:741
NimBLEService * createService(const char *uuid)
Create a BLE Service.
Definition: NimBLEServer.cpp:70
NimBLEAdvertising * getAdvertising()
Retrieve the advertising object that can be used to advertise the existence of the server.
Definition: NimBLEServer.cpp:147
void stopAdvertising()
Stop advertising.
Definition: NimBLEServer.cpp:732
std::vector< uint16_t > getPeerDevices()
Get the vector of the connected client ID's.
Definition: NimBLEServer.cpp:264
NimBLEConnInfo getPeerIDInfo(uint16_t id)
Get the connection information of a connected peer by connection ID.
Definition: NimBLEServer.cpp:306
void start()
Start the GATT server. Required to be called after setup of all services and characteristics / descri...
Definition: NimBLEServer.cpp:168
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:33