2020-03-30 01:44:20 +02:00
/*
* NimBLEClient . h
*
* Created : on Jan 26 2020
* Author H2zero
2020-05-14 06:03:56 +02:00
*
2020-03-30 01:44:20 +02:00
* Originally :
* BLEClient . h
*
* Created on : Mar 22 , 2017
* Author : kolban
*/
# ifndef MAIN_NIMBLECLIENT_H_
# define MAIN_NIMBLECLIENT_H_
# include "sdkconfig.h"
2020-05-14 06:03:56 +02:00
# if defined(CONFIG_BT_ENABLED)
# include "nimconfig.h"
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
2020-03-30 01:44:20 +02:00
# include "NimBLEAddress.h"
# include "NimBLEAdvertisedDevice.h"
# include "NimBLERemoteService.h"
2020-05-18 04:21:35 +02:00
# include <vector>
2020-03-30 01:44:20 +02:00
# include <string>
class NimBLERemoteService ;
class NimBLEClientCallbacks ;
class NimBLEAdvertisedDevice ;
/**
* @ brief A model of a % BLE client .
*/
class NimBLEClient {
public :
Add iterators to client remote attributes.
Add iterators for NimBLEScan: NimBLEadvertisedDevice, NimBLEClient: NimBLERemoteService, NimBLERemoteService: NimBLERemoteCharacteristic and NimBLERemoteCharacteristic: NimBLERemoteDescriptor
This is handy e.g. for showing every address of the advertised devices from a scan. To do so, first get a new scan and next:
```
for(auto pAdvertisedDevice: pBLEScan->getResults()) {
Serial.printf("Address is %s\n", std::string(pAdvertisedDevice->getAddress()).c_str());
}
```
Of course any other property of the advertised device can be shown (or looked up, if that is your use case)
Also this is handy e.g. for showing every UUID in a peripheral. To do so, first connect to a peripheral and next:
```
for(auto pService: *pClient) {
Serial.printf("Service UUID is %s\n", std::string(pService->getUUID()).c_str());
for(auto pCharacteristic: *pService) {
Serial.printf("Characteristic UUID is %s\n", std::string(pCharacteristic->getUUID()).c_str());
for(auto pDescriptor: *pCharacteristic) {
Serial.printf("Descriptor UUID is %s\n", std::string(pDescriptor->getUUID()).c_str());
}
}
}
```
Again of course any other property can be shown, or looked up.
2020-05-23 04:13:52 +02:00
bool connect ( NimBLEAdvertisedDevice * device , bool refreshServices = true ) ;
bool connect ( const NimBLEAddress & address , uint8_t type = BLE_ADDR_PUBLIC , bool refreshServices = true ) ; // Connect to the remote BLE Server
int disconnect ( uint8_t reason = BLE_ERR_REM_USER_CONN_TERM ) ; // Disconnect from the remote BLE Server
NimBLEAddress getPeerAddress ( ) ; // Get the address of the remote BLE Server
int getRssi ( ) ; // Get the RSSI of the remote BLE Server
2020-05-18 04:21:35 +02:00
Add iterators to client remote attributes.
Add iterators for NimBLEScan: NimBLEadvertisedDevice, NimBLEClient: NimBLERemoteService, NimBLERemoteService: NimBLERemoteCharacteristic and NimBLERemoteCharacteristic: NimBLERemoteDescriptor
This is handy e.g. for showing every address of the advertised devices from a scan. To do so, first get a new scan and next:
```
for(auto pAdvertisedDevice: pBLEScan->getResults()) {
Serial.printf("Address is %s\n", std::string(pAdvertisedDevice->getAddress()).c_str());
}
```
Of course any other property of the advertised device can be shown (or looked up, if that is your use case)
Also this is handy e.g. for showing every UUID in a peripheral. To do so, first connect to a peripheral and next:
```
for(auto pService: *pClient) {
Serial.printf("Service UUID is %s\n", std::string(pService->getUUID()).c_str());
for(auto pCharacteristic: *pService) {
Serial.printf("Characteristic UUID is %s\n", std::string(pCharacteristic->getUUID()).c_str());
for(auto pDescriptor: *pCharacteristic) {
Serial.printf("Descriptor UUID is %s\n", std::string(pDescriptor->getUUID()).c_str());
}
}
}
```
Again of course any other property can be shown, or looked up.
2020-05-23 04:13:52 +02:00
std : : vector < NimBLERemoteService * > * getServices ( ) ; // Get a vector of the services offered by the remote BLE Server
std : : vector < NimBLERemoteService * > : : iterator begin ( ) ;
std : : vector < NimBLERemoteService * > : : iterator end ( ) ;
NimBLERemoteService * getService ( const char * uuid ) ; // Get a reference to a specified service offered by the remote BLE server.
NimBLERemoteService * getService ( const NimBLEUUID & uuid ) ; // Get a reference to a specified service offered by the remote BLE server.
std : : string getValue ( const NimBLEUUID & serviceUUID , const NimBLEUUID & characteristicUUID ) ; // Get the value of a given characteristic at a given service.
bool setValue ( const NimBLEUUID & serviceUUID , const NimBLEUUID & characteristicUUID , const std : : string & value ) ; // Set the value of a given characteristic at a given service.
bool isConnected ( ) ; // Return true if we are connected.
void setClientCallbacks ( NimBLEClientCallbacks * pClientCallbacks , bool deleteCallbacks = true ) ;
std : : string toString ( ) ; // Return a string representation of this client.
uint16_t getConnId ( ) ;
uint16_t getMTU ( ) ;
bool secureConnection ( ) ;
void setConnectTimeout ( uint8_t timeout ) ;
void setConnectionParams ( uint16_t minInterval , uint16_t maxInterval ,
2020-05-14 06:03:56 +02:00
uint16_t latency , uint16_t timeout ,
2020-04-14 03:13:51 +02:00
uint16_t scanInterval = 16 , uint16_t scanWindow = 16 ) ; // NimBLE default scan settings
Add iterators to client remote attributes.
Add iterators for NimBLEScan: NimBLEadvertisedDevice, NimBLEClient: NimBLERemoteService, NimBLERemoteService: NimBLERemoteCharacteristic and NimBLERemoteCharacteristic: NimBLERemoteDescriptor
This is handy e.g. for showing every address of the advertised devices from a scan. To do so, first get a new scan and next:
```
for(auto pAdvertisedDevice: pBLEScan->getResults()) {
Serial.printf("Address is %s\n", std::string(pAdvertisedDevice->getAddress()).c_str());
}
```
Of course any other property of the advertised device can be shown (or looked up, if that is your use case)
Also this is handy e.g. for showing every UUID in a peripheral. To do so, first connect to a peripheral and next:
```
for(auto pService: *pClient) {
Serial.printf("Service UUID is %s\n", std::string(pService->getUUID()).c_str());
for(auto pCharacteristic: *pService) {
Serial.printf("Characteristic UUID is %s\n", std::string(pCharacteristic->getUUID()).c_str());
for(auto pDescriptor: *pCharacteristic) {
Serial.printf("Descriptor UUID is %s\n", std::string(pDescriptor->getUUID()).c_str());
}
}
}
```
Again of course any other property can be shown, or looked up.
2020-05-23 04:13:52 +02:00
void updateConnParams ( uint16_t minInterval , uint16_t maxInterval ,
2020-05-14 06:03:56 +02:00
uint16_t latency , uint16_t timeout ) ;
2020-03-30 01:44:20 +02:00
private :
NimBLEClient ( ) ;
~ NimBLEClient ( ) ;
friend class NimBLEDevice ;
friend class NimBLERemoteService ;
static int handleGapEvent ( struct ble_gap_event * event , void * arg ) ;
static int serviceDiscoveredCB ( uint16_t conn_handle , const struct ble_gatt_error * error , const struct ble_gatt_svc * service , void * arg ) ;
void clearServices ( ) ; // Clear any existing services.
bool retrieveServices ( ) ; //Retrieve services from the server
// void onHostReset();
2020-05-18 18:11:54 +02:00
NimBLEAddress m_peerAddress = NimBLEAddress ( " " ) ; // The BD address of the remote server.
2020-03-30 01:44:20 +02:00
uint16_t m_conn_id ;
bool m_haveServices = false ; // Have we previously obtain the set of services from the remote server.
bool m_isConnected = false ; // Are we currently connected.
bool m_waitingToConnect = false ;
bool m_deleteCallbacks = true ;
2020-05-14 06:03:56 +02:00
int32_t m_connectTimeout ;
2020-03-30 01:44:20 +02:00
//uint16_t m_mtu = 23;
NimBLEClientCallbacks * m_pClientCallbacks = nullptr ;
FreeRTOS : : Semaphore m_semaphoreOpenEvt = FreeRTOS : : Semaphore ( " OpenEvt " ) ;
FreeRTOS : : Semaphore m_semaphoreSearchCmplEvt = FreeRTOS : : Semaphore ( " SearchCmplEvt " ) ;
FreeRTOS : : Semaphore m_semeaphoreSecEvt = FreeRTOS : : Semaphore ( " Security " ) ;
2020-05-18 04:21:35 +02:00
std : : vector < NimBLERemoteService * > m_servicesVector ;
2020-05-14 06:03:56 +02:00
2020-04-14 03:13:51 +02:00
private :
friend class NimBLEClientCallbacks ;
ble_gap_conn_params m_pConnParams ;
2020-03-30 01:44:20 +02:00
2020-05-14 06:03:56 +02:00
} ; // class NimBLEClient
2020-03-30 01:44:20 +02:00
/**
* @ brief Callbacks associated with a % BLE client .
*/
class NimBLEClientCallbacks {
public :
virtual ~ NimBLEClientCallbacks ( ) { } ;
2020-04-14 03:13:51 +02:00
virtual void onConnect ( NimBLEClient * pClient ) ;
virtual void onDisconnect ( NimBLEClient * pClient ) ;
virtual bool onConnParamsUpdateRequest ( NimBLEClient * pClient , const ble_gap_upd_params * params ) ;
virtual uint32_t onPassKeyRequest ( ) ;
virtual void onPassKeyNotify ( uint32_t pass_key ) ;
virtual bool onSecurityRequest ( ) ;
virtual void onAuthenticationComplete ( ble_gap_conn_desc * desc ) ;
virtual bool onConfirmPIN ( uint32_t pin ) ;
2020-03-30 01:44:20 +02:00
} ;
2020-05-14 06:03:56 +02:00
# endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
2020-03-30 01:44:20 +02:00
# endif // CONFIG_BT_ENABLED
# endif /* MAIN_NIMBLECLIENT_H_ */