NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
NimBLEClient.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_CLIENT_H_
19#define NIMBLE_CPP_CLIENT_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
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# include "NimBLEAddress.h"
31
32# include <stdint.h>
33# include <vector>
34# include <string>
35
36class NimBLEAddress;
37class NimBLEUUID;
41class NimBLEAttValue;
43class NimBLEConnInfo;
44struct NimBLETaskData;
45
50 public:
51 bool connect(const NimBLEAdvertisedDevice* device,
52 bool deleteAttributes = true,
53 bool asyncConnect = false,
54 bool exchangeMTU = true);
55 bool connect(const NimBLEAddress& address, bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true);
56 bool connect(bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true);
57 bool disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
58 bool cancelConnect() const;
59 void setSelfDelete(bool deleteOnDisconnect, bool deleteOnConnectFail);
61 bool setPeerAddress(const NimBLEAddress& address);
62 int getRssi() const;
63 bool isConnected() const;
64 void setClientCallbacks(NimBLEClientCallbacks* pClientCallbacks, bool deleteCallbacks = true);
65 std::string toString() const;
66 uint16_t getConnHandle() const;
67 uint16_t getMTU() const;
68 bool exchangeMTU();
69 bool secureConnection(bool async = false) const;
70 void setConnectTimeout(uint32_t timeout);
71 bool setDataLen(uint16_t txOctets);
72 bool discoverAttributes();
74 int getLastError() const;
75 bool updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
76 void setConnectionParams(uint16_t minInterval,
77 uint16_t maxInterval,
78 uint16_t latency,
79 uint16_t timeout,
80 uint16_t scanInterval = 16,
81 uint16_t scanWindow = 16);
82 const std::vector<NimBLERemoteService*>& getServices(bool refresh = false);
83 std::vector<NimBLERemoteService*>::iterator begin();
84 std::vector<NimBLERemoteService*>::iterator end();
86 NimBLERemoteService* getService(const char* uuid);
88 void deleteServices();
89 size_t deleteService(const NimBLEUUID& uuid);
90 NimBLEAttValue getValue(const NimBLEUUID& serviceUUID, const NimBLEUUID& characteristicUUID);
91 bool setValue(const NimBLEUUID& serviceUUID,
92 const NimBLEUUID& characteristicUUID,
93 const NimBLEAttValue& value,
94 bool response = false);
95
96# if CONFIG_BT_NIMBLE_EXT_ADV
97 void setConnectPhy(uint8_t phyMask);
98 bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions = 0);
99 bool getPhy(uint8_t* txPhy, uint8_t* rxPhy);
100# endif
101
102 struct Config {
103 uint8_t deleteCallbacks : 1; // Delete the callback object when the client is deleted.
104 uint8_t deleteOnDisconnect : 1; // Delete the client when disconnected.
105 uint8_t deleteOnConnectFail : 1; // Delete the client when a connection attempt fails.
106 uint8_t asyncConnect : 1; // Connect asynchronously.
107 uint8_t exchangeMTU : 1; // Exchange MTU after connection.
108 };
109
110 Config getConfig() const;
111 void setConfig(Config config);
112
113 private:
114 NimBLEClient(const NimBLEAddress& peerAddress);
116 NimBLEClient(const NimBLEClient&) = delete;
117 NimBLEClient& operator=(const NimBLEClient&) = delete;
118
119 bool retrieveServices(const NimBLEUUID* uuidFilter = nullptr);
120 static int handleGapEvent(struct ble_gap_event* event, void* arg);
121 static int exchangeMTUCb(uint16_t conn_handle, const ble_gatt_error* error, uint16_t mtu, void* arg);
122 static int serviceDiscoveredCB(uint16_t connHandle,
123 const struct ble_gatt_error* error,
124 const struct ble_gatt_svc* service,
125 void* arg);
126
127 NimBLEAddress m_peerAddress;
128 mutable int m_lastErr;
129 int32_t m_connectTimeout;
130 mutable NimBLETaskData* m_pTaskData;
131 std::vector<NimBLERemoteService*> m_svcVec;
132 NimBLEClientCallbacks* m_pClientCallbacks;
133 uint16_t m_connHandle;
134 uint8_t m_terminateFailCount;
135 mutable uint8_t m_asyncSecureAttempt;
136 Config m_config;
137
138# if CONFIG_BT_NIMBLE_EXT_ADV
139 uint8_t m_phyMask;
140# endif
141 ble_gap_conn_params m_connParams;
142
143 friend class NimBLEDevice;
144 friend class NimBLEServer;
145}; // class NimBLEClient
146
151 public:
152 virtual ~NimBLEClientCallbacks() {};
153
158 virtual void onConnect(NimBLEClient* pClient);
159
165 virtual void onConnectFail(NimBLEClient* pClient, int reason);
166
172 virtual void onDisconnect(NimBLEClient* pClient, int reason);
173
180 virtual bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params);
181
186 virtual void onPassKeyEntry(NimBLEConnInfo& connInfo);
187
193 virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo);
194
200 virtual void onConfirmPasskey(NimBLEConnInfo& connInfo, uint32_t pin);
201
206 virtual void onIdentity(NimBLEConnInfo& connInfo);
207
214 virtual void onMTUChange(NimBLEClient* pClient, uint16_t MTU);
215
216# if CONFIG_BT_NIMBLE_EXT_ADV
228 virtual void onPhyUpdate(NimBLEClient* pClient, uint8_t txPhy, uint8_t rxPhy);
229# endif
230};
231
232#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
233#endif /* NIMBLE_CPP_CLIENT_H_ */
A BLE device address.
Definition NimBLEAddress.h:41
A representation of a BLE advertised device found by a scan.
Definition NimBLEAdvertisedDevice.h:45
A specialized container class to hold BLE attribute values.
Definition NimBLEAttValue.h:72
Callbacks associated with a BLE client.
Definition NimBLEClient.h:150
virtual void onPhyUpdate(NimBLEClient *pClient, uint8_t txPhy, uint8_t rxPhy)
Called when the PHY update procedure is complete.
Definition NimBLEClient.cpp:1302
virtual void onConfirmPasskey(NimBLEConnInfo &connInfo, uint32_t pin)
Called when using numeric comparision for pairing.
Definition NimBLEClient.cpp:1292
virtual bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
Called when server requests to update the connection parameters.
Definition NimBLEClient.cpp:1274
virtual void onMTUChange(NimBLEClient *pClient, uint16_t MTU)
Called when the connection MTU changes.
Definition NimBLEClient.cpp:1297
virtual void onConnect(NimBLEClient *pClient)
Called after client connects.
Definition NimBLEClient.cpp:1262
virtual void onPassKeyEntry(NimBLEConnInfo &connInfo)
Called when server requests a passkey for pairing.
Definition NimBLEClient.cpp:1279
virtual void onIdentity(NimBLEConnInfo &connInfo)
Called when the peer identity address is resolved.
Definition NimBLEClient.cpp:1288
virtual void onConnectFail(NimBLEClient *pClient, int reason)
Called when a connection attempt fails.
Definition NimBLEClient.cpp:1266
virtual void onDisconnect(NimBLEClient *pClient, int reason)
Called when disconnected from the server.
Definition NimBLEClient.cpp:1270
virtual void onAuthenticationComplete(NimBLEConnInfo &connInfo)
Called when the pairing procedure is complete.
Definition NimBLEClient.cpp:1284
A model of a BLE client.
Definition NimBLEClient.h:49
void setConnectPhy(uint8_t phyMask)
Set the PHY types to use when connecting to a server.
Definition NimBLEClient.cpp:408
std::vector< NimBLERemoteService * >::iterator end()
Get iterator to the end of the vector of remote service pointers.
Definition NimBLEClient.cpp:614
bool isConnected() const
Are we connected to a server?
Definition NimBLEClient.cpp:1218
size_t deleteService(const NimBLEUUID &uuid)
Delete a service by UUID from the local database to free resources.
Definition NimBLEClient.cpp:116
bool discoverAttributes()
Retrieves the full database of attributes that the peripheral has available.
Definition NimBLEClient.cpp:702
bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions=0)
Request a change to the PHY used for this peer connection.
Definition NimBLEClient.cpp:430
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout, uint16_t scanInterval=16, uint16_t scanWindow=16)
Set the connection parameters to use when connecting to a server.
Definition NimBLEClient.cpp:464
bool exchangeMTU()
Begin the MTU exchange process with the server.
Definition NimBLEClient.cpp:905
int getRssi() const
Ask the BLE server for the RSSI value.
Definition NimBLEClient.cpp:585
bool connect(const NimBLEAdvertisedDevice *device, bool deleteAttributes=true, bool asyncConnect=false, bool exchangeMTU=true)
Connect to an advertising device.
Definition NimBLEClient.cpp:155
bool secureConnection(bool async=false) const
Initiate a secure connection (pair/bond) with the server. Called automatically when a characteristic ...
Definition NimBLEClient.cpp:306
bool disconnect(uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
Disconnect from the peer.
Definition NimBLEClient.cpp:347
NimBLEAttValue getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID)
Get the value of a specific characteristic associated with a specific service.
Definition NimBLEClient.cpp:805
NimBLEAddress getPeerAddress() const
Retrieve the address of the peer.
Definition NimBLEClient.cpp:562
bool setDataLen(uint16_t txOctets)
Request an update of the data packet length.
Definition NimBLEClient.cpp:514
NimBLEConnInfo getConnInfo() const
Get detailed information about the current peer connection.
Definition NimBLEClient.cpp:533
void setSelfDelete(bool deleteOnDisconnect, bool deleteOnConnectFail)
Set or unset a flag to delete this client when disconnected or connection failed.
Definition NimBLEClient.cpp:378
void deleteServices()
Delete all service objects created by this client and clear the vector.
Definition NimBLEClient.cpp:102
NimBLERemoteCharacteristic * getCharacteristic(uint16_t handle)
Get the remote characteristic with the specified handle.
Definition NimBLEClient.cpp:859
bool getPhy(uint8_t *txPhy, uint8_t *rxPhy)
Get the PHY used for this peer connection.
Definition NimBLEClient.cpp:445
uint16_t getMTU() const
Get the current mtu of this connection.
Definition NimBLEClient.cpp:877
bool setPeerAddress(const NimBLEAddress &address)
Set the peer address.
Definition NimBLEClient.cpp:571
std::string toString() const
Return a string representation of this client.
Definition NimBLEClient.cpp:1241
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const NimBLEAttValue &value, bool response=false)
Set the value of a specific characteristic associated with a specific service.
Definition NimBLEClient.cpp:832
std::vector< NimBLERemoteService * >::iterator begin()
Get iterator to the beginning of the vector of remote service pointers.
Definition NimBLEClient.cpp:606
Config getConfig() const
Get a copy of the clients configuration.
Definition NimBLEClient.cpp:387
uint16_t getConnHandle() const
Get the connection handle for this client.
Definition NimBLEClient.cpp:554
bool updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
Update the connection parameters:
Definition NimBLEClient.cpp:488
void setConfig(Config config)
Set the client configuration options.
Definition NimBLEClient.cpp:395
const std::vector< NimBLERemoteService * > & getServices(bool refresh=false)
Get a pointer to the vector of found services.
Definition NimBLEClient.cpp:685
NimBLERemoteService * getService(const char *uuid)
Get the service BLE Remote Service instance corresponding to the uuid.
Definition NimBLEClient.cpp:623
int getLastError() const
Get the last error code reported by the NimBLE host.
Definition NimBLEClient.cpp:1258
void setConnectTimeout(uint32_t timeout)
Set the timeout to wait for connection attempt to complete.
Definition NimBLEClient.cpp:546
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks=true)
Set the callbacks that will be invoked when events are received.
Definition NimBLEClient.cpp:1227
bool cancelConnect() const
Cancel an ongoing connection attempt.
Definition NimBLEClient.cpp:362
Connection information.
Definition NimBLEConnInfo.h:32
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:109
A model of a remote BLE characteristic.
Definition NimBLERemoteCharacteristic.h:34
A model of a remote BLE service.
Definition NimBLERemoteService.h:34
The model of a BLE server.
Definition NimBLEServer.h:60
A model of a BLE UUID.
Definition NimBLEUUID.h:41
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:32
Connection parameters
Definition ble_gap.h:287
Definition ble_gap.h:510
Connection parameters update parameters.
Definition ble_gap.h:347