esp-nimble-cpp  1.3.2
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 "NimBLEAdvertisedDevice.h"
25 #include "NimBLERemoteService.h"
26 
27 #include <vector>
28 #include <string>
29 
34 
38 class NimBLEClient {
39 public:
40  bool connect(NimBLEAdvertisedDevice* device, bool deleteAttibutes = true);
41  bool connect(const NimBLEAddress &address, bool deleteAttibutes = true);
42  bool connect(bool deleteAttibutes = true);
43  int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
45  void setPeerAddress(const NimBLEAddress &address);
46  int getRssi();
47  std::vector<NimBLERemoteService*>* getServices(bool refresh = false);
48  std::vector<NimBLERemoteService*>::iterator begin();
49  std::vector<NimBLERemoteService*>::iterator end();
50  NimBLERemoteService* getService(const char* uuid);
52  void deleteServices();
53  size_t deleteService(const NimBLEUUID &uuid);
54  std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID);
55  bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID,
56  const std::string &value, bool response = false);
57  NimBLERemoteCharacteristic* getCharacteristic(const uint16_t handle);
58  bool isConnected();
59  void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks,
60  bool deleteCallbacks = true);
61  std::string toString();
62  uint16_t getConnId();
63  uint16_t getMTU();
64  bool secureConnection();
65  void setConnectTimeout(uint8_t timeout);
66  void setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
67  uint16_t latency, uint16_t timeout,
68  uint16_t scanInterval=16, uint16_t scanWindow=16);
69  void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
70  uint16_t latency, uint16_t timeout);
71  void setDataLen(uint16_t tx_octets);
72  void discoverAttributes();
74  int getLastError();
75 
76 private:
77  NimBLEClient(const NimBLEAddress &peerAddress);
78  ~NimBLEClient();
79 
80  friend class NimBLEDevice;
81  friend class NimBLERemoteService;
82 
83  static int handleGapEvent(struct ble_gap_event *event, void *arg);
84  static int serviceDiscoveredCB(uint16_t conn_handle,
85  const struct ble_gatt_error *error,
86  const struct ble_gatt_svc *service,
87  void *arg);
88  static void dcTimerCb(ble_npl_event *event);
89  bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr);
90 
91  NimBLEAddress m_peerAddress;
92  int m_lastErr;
93  uint16_t m_conn_id;
94  bool m_connEstablished;
95  bool m_deleteCallbacks;
96  int32_t m_connectTimeout;
97  NimBLEClientCallbacks* m_pClientCallbacks;
98  ble_task_data_t* m_pTaskData;
99  ble_npl_callout m_dcTimer;
100 
101  std::vector<NimBLERemoteService*> m_servicesVector;
102 
103 private:
104  friend class NimBLEClientCallbacks;
105  ble_gap_conn_params m_pConnParams;
106 
107 }; // class NimBLEClient
108 
109 
114 public:
115  virtual ~NimBLEClientCallbacks() {};
116 
121  virtual void onConnect(NimBLEClient* pClient);
122 
127  virtual void onDisconnect(NimBLEClient* pClient);
128 
135  virtual bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params);
136 
141  virtual uint32_t onPassKeyRequest();
142 
143  /*virtual void onPassKeyNotify(uint32_t pass_key);
144  virtual bool onSecurityRequest();*/
145 
151  virtual void onAuthenticationComplete(ble_gap_conn_desc* desc);
152 
158  virtual bool onConfirmPIN(uint32_t pin);
159 };
160 
161 #endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
162 #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
Callbacks associated with a BLE client.
Definition: NimBLEClient.h:113
virtual void onDisconnect(NimBLEClient *pClient)
Called when disconnected from the server.
Definition: NimBLEClient.cpp:1202
virtual void onAuthenticationComplete(ble_gap_conn_desc *desc)
Called when the pairing procedure is complete.
Definition: NimBLEClient.cpp:1224
virtual bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
Called when server requests to update the connection parameters.
Definition: NimBLEClient.cpp:1206
virtual void onConnect(NimBLEClient *pClient)
Called after client connects.
Definition: NimBLEClient.cpp:1198
virtual uint32_t onPassKeyRequest()
Called when server requests a passkey for pairing.
Definition: NimBLEClient.cpp:1211
virtual bool onConfirmPIN(uint32_t pin)
Called when using numeric comparision for pairing.
Definition: NimBLEClient.cpp:1227
A model of a BLE client.
Definition: NimBLEClient.h:38
NimBLEAddress getPeerAddress()
Retrieve the address of the peer.
Definition: NimBLEClient.cpp:520
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value, bool response=false)
Set the value of a specific characteristic associated with a specific service.
Definition: NimBLEClient.cpp:798
std::vector< NimBLERemoteService * >::iterator end()
Get iterator to the end of the vector of remote service pointers.
Definition: NimBLEClient.cpp:578
size_t deleteService(const NimBLEUUID &uuid)
Delete service by UUID.
Definition: NimBLEClient.cpp:134
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 paramaters to use when connecting to a server.
Definition: NimBLEClient.cpp:407
int getLastError()
Get the last error code reported by the NimBLE host.
Definition: NimBLEClient.cpp:1193
NimBLERemoteCharacteristic * getCharacteristic(const uint16_t handle)
Get the remote characteristic with the specified handle.
Definition: NimBLEClient.cpp:824
void setDataLen(uint16_t tx_octets)
Request an update of the data packet length.
Definition: NimBLEClient.cpp:466
void discoverAttributes()
Retrieves the full database of attributes that the peripheral has available.
Definition: NimBLEClient.cpp:655
void setConnectTimeout(uint8_t timeout)
Set the timeout to wait for connection attempt to complete.
Definition: NimBLEClient.cpp:503
NimBLEConnInfo getConnInfo()
Get detailed information about the current peer connection.
Definition: NimBLEClient.cpp:484
int disconnect(uint8_t reason=BLE_ERR_REM_USER_CONN_TERM)
Disconnect from the peer.
Definition: NimBLEClient.cpp:354
void deleteServices()
Delete all service objects created by this client and clear the vector.
Definition: NimBLEClient.cpp:117
bool secureConnection()
Initiate a secure connection (pair/bond) with the server. Called automatically when a characteristic ...
Definition: NimBLEClient.cpp:318
void setPeerAddress(const NimBLEAddress &address)
Set the peer address.
Definition: NimBLEClient.cpp:530
uint16_t getConnId()
Get the connection id for this client.
Definition: NimBLEClient.cpp:512
bool connect(NimBLEAdvertisedDevice *device, bool deleteAttibutes=true)
Connect to an advertising device.
Definition: NimBLEClient.cpp:168
std::vector< NimBLERemoteService * >::iterator begin()
Get iterator to the beginning of the vector of remote service pointers.
Definition: NimBLEClient.cpp:569
uint16_t getMTU()
Get the current mtu of this connection.
Definition: NimBLEClient.cpp:849
std::string toString()
Return a string representation of this client.
Definition: NimBLEClient.cpp:1177
std::vector< NimBLERemoteService * > * getServices(bool refresh=false)
Get a pointer to the vector of found services.
Definition: NimBLEClient.cpp:637
int getRssi()
Ask the BLE server for the RSSI value.
Definition: NimBLEClient.cpp:545
NimBLERemoteService * getService(const char *uuid)
Get the service BLE Remote Service instance corresponding to the uuid.
Definition: NimBLEClient.cpp:588
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks=true)
Set the callbacks that will be invoked when events are received.
Definition: NimBLEClient.cpp:1163
bool isConnected()
Are we connected to a server?
Definition: NimBLEClient.cpp:1153
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID)
Get the value of a specific characteristic associated with a specific service.
Definition: NimBLEClient.cpp:771
void updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout)
Update the connection parameters:
Definition: NimBLEClient.cpp:437
Connection information.
Definition: NimBLEConnInfo.h:9
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:92
A model of a remote BLE characteristic.
Definition: NimBLERemoteCharacteristic.h:43
A model of a remote BLE service.
Definition: NimBLERemoteService.h:34
A model of a BLE UUID.
Definition: NimBLEUUID.h:37