esp-nimble-cpp 2.0.2
Loading...
Searching...
No Matches
NimBLEServer.h
1/*
2 * Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_SERVER_H_
19#define NIMBLE_CPP_SERVER_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
23
24# if defined(CONFIG_NIMBLE_CPP_IDF)
25# include "host/ble_gap.h"
26# else
27# include "nimble/nimble/host/include/host/ble_gap.h"
28# endif
29
30/**** FIX COMPILATION ****/
31# undef min
32# undef max
33/**************************/
34
35# include <vector>
36# include <array>
37
38# define NIMBLE_ATT_REMOVE_HIDE 1
39# define NIMBLE_ATT_REMOVE_DELETE 2
40
41class NimBLEService;
43class NimBLEUUID;
44class NimBLEConnInfo;
45class NimBLEAddress;
46class NimBLEService;
48# if CONFIG_BT_NIMBLE_EXT_ADV
50# else
52# endif
53# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
54class NimBLEClient;
55# endif
56
61 public:
62 void start();
63 uint8_t getConnectedCount() const;
64 bool disconnect(uint16_t connHandle, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
65 bool disconnect(const NimBLEConnInfo& connInfo, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM) const;
66 void setCallbacks(NimBLEServerCallbacks* pCallbacks, bool deleteCallbacks = true);
67 void updateConnParams(uint16_t connHandle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout) const;
68 NimBLEService* createService(const char* uuid);
70 NimBLEService* getServiceByUUID(const char* uuid, uint16_t instanceId = 0) const;
71 NimBLEService* getServiceByUUID(const NimBLEUUID& uuid, uint16_t instanceId = 0) const;
72 NimBLEService* getServiceByHandle(uint16_t handle) const;
73 void removeService(NimBLEService* service, bool deleteSvc = false);
74 void addService(NimBLEService* service);
75 uint16_t getPeerMTU(uint16_t connHandle) const;
76 std::vector<uint16_t> getPeerDevices() const;
77 NimBLEConnInfo getPeerInfo(uint8_t index) const;
78 NimBLEConnInfo getPeerInfo(const NimBLEAddress& address) const;
79 NimBLEConnInfo getPeerInfoByHandle(uint16_t connHandle) const;
80 void advertiseOnDisconnect(bool enable);
81 void setDataLen(uint16_t connHandle, uint16_t tx_octets) const;
82
83# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
84 NimBLEClient* getClient(uint16_t connHandle);
85 NimBLEClient* getClient(const NimBLEConnInfo& connInfo);
86 void deleteClient();
87# endif
88
89# if CONFIG_BT_NIMBLE_EXT_ADV
91 bool startAdvertising(uint8_t instanceId, int duration = 0, int maxEvents = 0) const;
92 bool stopAdvertising(uint8_t instanceId) const;
93 bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
94 bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);
95# endif
96
97# if !CONFIG_BT_NIMBLE_EXT_ADV || defined(_DOXYGEN_)
99 bool startAdvertising(uint32_t duration = 0) const;
100 bool stopAdvertising() const;
101# endif
102
103 private:
104 friend class NimBLEDevice;
105 friend class NimBLEService;
106 friend class NimBLECharacteristic;
107# if CONFIG_BT_NIMBLE_EXT_ADV
108 friend class NimBLEExtAdvertising;
109# else
110 friend class NimBLEAdvertising;
111# endif
112
113 NimBLEServer();
115
116 bool m_gattsStarted : 1;
117 bool m_svcChanged : 1;
118 bool m_deleteCallbacks : 1;
119# if !CONFIG_BT_NIMBLE_EXT_ADV
120 bool m_advertiseOnDisconnect : 1;
121# endif
122 NimBLEServerCallbacks* m_pServerCallbacks;
123 std::vector<NimBLEService*> m_svcVec;
124 std::array<uint16_t, CONFIG_BT_NIMBLE_MAX_CONNECTIONS> m_connectedPeers;
125
126# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
127 NimBLEClient* m_pClient{nullptr};
128# endif
129
130 static int handleGapEvent(struct ble_gap_event* event, void* arg);
131 static int handleGattEvent(uint16_t connHandle, uint16_t attrHandle, ble_gatt_access_ctxt* ctxt, void* arg);
132 void serviceChanged();
133 void resetGATT();
134
135}; // NimBLEServer
136
141 public:
142 virtual ~NimBLEServerCallbacks() {};
143
151 virtual void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo);
152
161 virtual void onDisconnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, int reason);
162
169 virtual void onMTUChange(uint16_t MTU, NimBLEConnInfo& connInfo);
170
175 virtual uint32_t onPassKeyDisplay();
176
183 virtual void onConfirmPassKey(NimBLEConnInfo& connInfo, uint32_t pin);
184
190 virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo);
191
196 virtual void onIdentity(NimBLEConnInfo& connInfo);
197
204 virtual void onConnParamsUpdate(NimBLEConnInfo& connInfo);
205
206# if CONFIG_BT_NIMBLE_EXT_ADV
218 virtual void onPhyUpdate(NimBLEConnInfo& connInfo, uint8_t txPhy, uint8_t rxPhy);
219# endif
220}; // NimBLEServerCallbacks
221
222#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_PERIPHERAL
223#endif // NIMBLE_CPP_SERVER_H_
A BLE device address.
Definition NimBLEAddress.h:41
Perform and manage BLE advertising.
Definition NimBLEAdvertising.h:52
The model of a BLE Characteristic.
Definition NimBLECharacteristic.h:40
A model of a BLE client.
Definition NimBLEClient.h:49
Connection information.
Definition NimBLEConnInfo.h:32
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:109
Extended advertising class.
Definition NimBLEExtAdvertising.h:111
Callbacks associated with the operation of a BLE server.
Definition NimBLEServer.h:140
virtual void onConnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo)
Handle a client connection. This is called when a client connects.
Definition NimBLEServer.cpp:987
virtual uint32_t onPassKeyDisplay()
Called when a client requests a passkey for pairing (display).
Definition NimBLEServer.cpp:999
virtual void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason)
Handle a client disconnection. This is called when a client disconnects.
Definition NimBLEServer.cpp:991
virtual void onConfirmPassKey(NimBLEConnInfo &connInfo, uint32_t pin)
Called when using numeric comparision for pairing.
Definition NimBLEServer.cpp:1004
virtual void onPhyUpdate(NimBLEConnInfo &connInfo, uint8_t txPhy, uint8_t rxPhy)
Called when the PHY update procedure is complete.
Definition NimBLEServer.cpp:1022
virtual void onAuthenticationComplete(NimBLEConnInfo &connInfo)
Called when the pairing procedure is complete.
Definition NimBLEServer.cpp:1013
virtual void onConnParamsUpdate(NimBLEConnInfo &connInfo)
Called when connection parameters are updated following a request to update via NimBLEServer::updateC...
Definition NimBLEServer.cpp:1017
virtual void onMTUChange(uint16_t MTU, NimBLEConnInfo &connInfo)
Called when the connection MTU changes.
Definition NimBLEServer.cpp:995
virtual void onIdentity(NimBLEConnInfo &connInfo)
Called when the peer identity address is resolved.
Definition NimBLEServer.cpp:1009
The model of a BLE server.
Definition NimBLEServer.h:60
void removeService(NimBLEService *service, bool deleteSvc=false)
Remove a service from the server.
Definition NimBLEServer.cpp:708
NimBLEConnInfo getPeerInfoByHandle(uint16_t connHandle) const
Get the connection information of a connected peer by connection handle.
Definition NimBLEServer.cpp:337
NimBLEService * getServiceByHandle(uint16_t handle) const
Get a BLE Service by its handle.
Definition NimBLEServer.cpp:137
NimBLEConnInfo getPeerInfo(uint8_t index) const
Get the connection information of a connected peer by vector index.
Definition NimBLEServer.cpp:299
std::vector< uint16_t > getPeerDevices() const
Get a vector of the connected client handles.
Definition NimBLEServer.cpp:283
bool stopAdvertising() const
Stop advertising.
Definition NimBLEServer.cpp:877
NimBLEService * getServiceByUUID(const char *uuid, uint16_t instanceId=0) const
Get a BLE Service by its UUID.
Definition NimBLEServer.cpp:108
NimBLEClient * getClient(uint16_t connHandle)
Create a client instance from the connection handle.
Definition NimBLEServer.cpp:946
uint16_t getPeerMTU(uint16_t connHandle) const
Get the MTU value of a client connection.
Definition NimBLEServer.cpp:887
uint8_t getConnectedCount() const
Return the number of connected clients.
Definition NimBLEServer.cpp:268
void setCallbacks(NimBLEServerCallbacks *pCallbacks, bool deleteCallbacks=true)
Set the server callbacks.
Definition NimBLEServer.cpp:680
void updateConnParams(uint16_t connHandle, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout) const
Request an Update the connection parameters:
Definition NimBLEServer.cpp:900
void addService(NimBLEService *service)
Adds a service which was either already created but removed from availability, or created and later a...
Definition NimBLEServer.cpp:745
bool getPhy(uint16_t connHandle, uint8_t *txPhy, uint8_t *rxPhy)
Get the PHY used for a peer connection.
Definition NimBLEServer.cpp:851
NimBLEExtAdvertising * getAdvertising() const
Retrieve the advertising object that can be used to advertise the existence of the server.
Definition NimBLEServer.cpp:152
void deleteClient()
Delete the NimBLEClient instance that was created with getClient()
Definition NimBLEServer.cpp:978
NimBLEService * createService(const char *uuid)
Create a BLE Service.
Definition NimBLEServer.cpp:85
bool startAdvertising(uint8_t instanceId, int duration=0, int maxEvents=0) const
Start advertising.
Definition NimBLEServer.cpp:803
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions)
Request an update to the PHY used for a peer connection.
Definition NimBLEServer.cpp:835
bool disconnect(uint16_t connHandle, uint8_t reason=BLE_ERR_REM_USER_CONN_TERM) const
Disconnect the specified client with optional reason.
Definition NimBLEServer.cpp:234
void start()
Start the GATT server.
Definition NimBLEServer.cpp:182
void advertiseOnDisconnect(bool enable)
Set the server to automatically start advertising when a client disconnects.
Definition NimBLEServer.cpp:259
void setDataLen(uint16_t connHandle, uint16_t tx_octets) const
Request an update of the data packet length.
Definition NimBLEServer.cpp:924
The model of a BLE service.
Definition NimBLEService.h:34
A model of a BLE UUID.
Definition NimBLEUUID.h:41