esp-nimble-cpp  1.4.1
NimBLEClient.h
1 /*
2  * NimBLEClient.h
3  *
4  * Created: on Jan 26 2020
5  * Author H2zero
6  *
7  * Originally:
8  * BLEClient.h
9  *
10  * Created on: Mar 22, 2017
11  * Author: kolban
12  */
13 
14 #ifndef MAIN_NIMBLECLIENT_H_
15 #define MAIN_NIMBLECLIENT_H_
16 
17 #include "nimconfig.h"
18 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
19 
20 #include "NimBLEAddress.h"
21 #include "NimBLEUUID.h"
22 #include "NimBLEUtils.h"
23 #include "NimBLEConnInfo.h"
24 #include "NimBLEAttValue.h"
25 #include "NimBLEAdvertisedDevice.h"
26 #include "NimBLERemoteService.h"
27 
28 #include <vector>
29 #include <string>
30 
35 
39 class NimBLEClient {
40 public:
41  bool connect(NimBLEAdvertisedDevice* device, bool deleteAttributes = true);
42  bool connect(const NimBLEAddress &address, bool deleteAttributes = true);
43  bool connect(bool deleteAttributes = true);
44  int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
46  void setPeerAddress(const NimBLEAddress &address);
47  int getRssi();
48  std::vector<NimBLERemoteService*>* getServices(bool refresh = false);
49  std::vector<NimBLERemoteService*>::iterator begin();
50  std::vector<NimBLERemoteService*>::iterator end();
51  NimBLERemoteService* getService(const char* uuid);
53  void deleteServices();
54  size_t deleteService(const NimBLEUUID &uuid);
55  NimBLEAttValue getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID);
56  bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID,
57  const NimBLEAttValue &value, bool response = false);
58  NimBLERemoteCharacteristic* getCharacteristic(const uint16_t handle);
59  bool isConnected();
60  void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks,
61  bool deleteCallbacks = true);
62  std::string toString();
63  uint16_t getConnId();
64  uint16_t getMTU();
65  bool secureConnection();
66  void setConnectTimeout(uint8_t timeout);
67  void setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
68  uint16_t latency, uint16_t timeout,
69  uint16_t scanInterval=16, uint16_t scanWindow=16);
70  void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
71  uint16_t latency, uint16_t timeout);
72  void setDataLen(uint16_t tx_octets);
73  bool discoverAttributes();
75  int getLastError();
76 #if CONFIG_BT_NIMBLE_EXT_ADV
77  void setConnectPhy(uint8_t mask);
78 #endif
79 
80 private:
81  NimBLEClient(const NimBLEAddress &peerAddress);
82  ~NimBLEClient();
83 
84  friend class NimBLEDevice;
85  friend class NimBLERemoteService;
86 
87  static int handleGapEvent(struct ble_gap_event *event, void *arg);
88  static int serviceDiscoveredCB(uint16_t conn_handle,
89  const struct ble_gatt_error *error,
90  const struct ble_gatt_svc *service,
91  void *arg);
92  static void dcTimerCb(ble_npl_event *event);
93  bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr);
94 
95  NimBLEAddress m_peerAddress;
96  int m_lastErr;
97  uint16_t m_conn_id;
98  bool m_connEstablished;
99  bool m_deleteCallbacks;
100  int32_t m_connectTimeout;
101  NimBLEClientCallbacks* m_pClientCallbacks;
102  ble_task_data_t* m_pTaskData;
103  ble_npl_callout m_dcTimer;
104 #if CONFIG_BT_NIMBLE_EXT_ADV
105  uint8_t m_phyMask;
106 #endif
107 
108  std::vector<NimBLERemoteService*> m_servicesVector;
109 
110 private:
111  friend class NimBLEClientCallbacks;
112  ble_gap_conn_params m_pConnParams;
113 
114 }; // class NimBLEClient
115 
116 
121 public:
122  virtual ~NimBLEClientCallbacks() {};
123 
128  virtual void onConnect(NimBLEClient* pClient);
129 
134  virtual void onDisconnect(NimBLEClient* pClient);
135 
142  virtual bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params);
143 
148  virtual uint32_t onPassKeyRequest();
149 
150  /*virtual void onPassKeyNotify(uint32_t pass_key);
151  virtual bool onSecurityRequest();*/
152 
158  virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);
159 
165  virtual bool onConfirmPIN(uint32_t pin);
166 };
167 
168 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
169 #endif /* MAIN_NIMBLECLIENT_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
A representation of a BLE advertised device found by a scan.
Definition: NimBLEAdvertisedDevice.h:42
A specialized container class to hold BLE attribute values.
Definition: NimBLEAttValue.h:61
Callbacks associated with a BLE client.
Definition: NimBLEClient.h:120
virtual void onDisconnect(NimBLEClient *pClient)
Called when disconnected from the server.
Definition: NimBLEClient.cpp:1269
virtual void onAuthenticationComplete(ble_gap_conn_desc *desc)
Called when the pairing procedure is complete.
Definition: NimBLEClient.cpp:1291
virtual bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
Called when server requests to update the connection parameters.
Definition: NimBLEClient.cpp:1273
virtual void onConnect(NimBLEClient *pClient)
Called after client connects.
Definition: NimBLEClient.cpp:1265
virtual uint32_t onPassKeyRequest()
Called when server requests a passkey for pairing.
Definition: NimBLEClient.cpp:1278
virtual bool onConfirmPIN(uint32_t pin)
Called when using numeric comparision for pairing.
Definition: NimBLEClient.cpp:1294
A model of a BLE client.
Definition: NimBLEClient.h:39
NimBLEAddress getPeerAddress()
Retrieve the address of the peer.
Definition: NimBLEClient.cpp:558
std::vector< NimBLERemoteService * >::iterator end()
Get iterator to the end of the vector of remote service pointers.
Definition: NimBLEClient.cpp:616
size_t deleteService(const NimBLEUUID &uuid)
Delete service by UUID.
Definition: NimBLEClient.cpp:141
bool discoverAttributes()
Retrieves the full database of attributes that the peripheral has available.
Definition: NimBLEClient.cpp:711
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:445
int getLastError()
Get the last error code reported by the NimBLE host.
Definition: NimBLEClient.cpp:1260
NimBLERemoteCharacteristic * getCharacteristic(const uint16_t handle)
Get the remote characteristic with the specified handle.
Definition: NimBLEClient.cpp:895
void setDataLen(uint16_t tx_octets)
Request an update of the data packet length.
Definition: NimBLEClient.cpp:504
NimBLEAttValue getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID)
Get the value of a specific characteristic associated with a specific service.
Definition: NimBLEClient.cpp:842
void setConnectPhy(uint8_t mask)
Set the PHY types to use when connecting to a server.
Definition: NimBLEClient.cpp:430
void setConnectTimeout(uint8_t timeout)
Set the timeout to wait for connection attempt to complete.
Definition: NimBLEClient.cpp:541
NimBLEConnInfo getConnInfo()
Get detailed information about the current peer connection.
Definition: NimBLEClient.cpp:522
int disconnect(uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
Disconnect from the peer.
Definition: NimBLEClient.cpp:377
void deleteServices()
Delete all service objects created by this client and clear the vector.
Definition: NimBLEClient.cpp:124
bool secureConnection()
Initiate a secure connection (pair/bond) with the server. Called automatically when a characteristic ...
Definition: NimBLEClient.cpp:338
void setPeerAddress(const NimBLEAddress &address)
Set the peer address.
Definition: NimBLEClient.cpp:568
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:869
uint16_t getConnId()
Get the connection id for this client.
Definition: NimBLEClient.cpp:550
std::vector< NimBLERemoteService * >::iterator begin()
Get iterator to the beginning of the vector of remote service pointers.
Definition: NimBLEClient.cpp:607
uint16_t getMTU()
Get the current mtu of this connection.
Definition: NimBLEClient.cpp:920
std::string toString()
Return a string representation of this client.
Definition: NimBLEClient.cpp:1244
std::vector< NimBLERemoteService * > * getServices(bool refresh=false)
Get a pointer to the vector of found services.
Definition: NimBLEClient.cpp:692
int getRssi()
Ask the BLE server for the RSSI value.
Definition: NimBLEClient.cpp:583
NimBLERemoteService * getService(const char *uuid)
Get the service BLE Remote Service instance corresponding to the uuid.
Definition: NimBLEClient.cpp:626
bool connect(NimBLEAdvertisedDevice *device, bool deleteAttributes=true)
Connect to an advertising device.
Definition: NimBLEClient.cpp:175
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks=true)
Set the callbacks that will be invoked when events are received.
Definition: NimBLEClient.cpp:1230
bool isConnected()
Are we connected to a server?
Definition: NimBLEClient.cpp:1220
void updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
Update the connection parameters:
Definition: NimBLEClient.cpp:475
Connection information.
Definition: NimBLEConnInfo.h:9
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:44
A model of a remote BLE service.
Definition: NimBLERemoteService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37