esp-nimble-cpp/src/NimBLEClient.h

129 lines
5.4 KiB
C
Raw Normal View History

2020-03-30 01:44:20 +02:00
/*
* NimBLEClient.h
*
* Created: on Jan 26 2020
* Author H2zero
*
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"
#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 "NimBLEUUID.h"
2020-03-30 01:44:20 +02:00
#include "NimBLEAdvertisedDevice.h"
#include "NimBLERemoteService.h"
#include <vector>
2020-03-30 01:44:20 +02:00
#include <string>
typedef struct {
const NimBLEUUID *uuid;
const void *attribute;
} disc_filter_t;
2020-03-30 01:44:20 +02:00
class NimBLERemoteService;
class NimBLEClientCallbacks;
class NimBLEAdvertisedDevice;
/**
* @brief A model of a %BLE client.
*/
class NimBLEClient {
public:
bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true);
bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC,
bool refreshServices = true);
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
NimBLEAddress getPeerAddress();
int getRssi();
std::vector<NimBLERemoteService*>* getServices(bool refresh = false);
std::vector<NimBLERemoteService*>::iterator begin();
std::vector<NimBLERemoteService*>::iterator end();
NimBLERemoteService* getService(const char* uuid);
NimBLERemoteService* getService(const NimBLEUUID &uuid);
void deleteServices();
size_t deleteService(const NimBLEUUID &uuid);
std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID);
bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID,
const std::string &value);
bool isConnected();
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks,
bool deleteCallbacks = true);
std::string toString();
uint16_t getConnId();
uint16_t getMTU();
bool secureConnection();
void setConnectTimeout(uint8_t timeout);
void setConnectionParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout,
uint16_t scanInterval=16, uint16_t scanWindow=16);
void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout);
void discoverAttributes();
2020-03-30 01:44:20 +02:00
private:
NimBLEClient();
~NimBLEClient();
friend class NimBLEDevice;
friend class NimBLERemoteService;
2020-03-30 01:44:20 +02:00
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);
bool retrieveServices(const NimBLEUUID *uuid_filter = nullptr);
2020-03-30 01:44:20 +02:00
NimBLEAddress m_peerAddress = NimBLEAddress("");
uint16_t m_conn_id;
bool m_isConnected = false;
bool m_waitingToConnect =false;
bool m_deleteCallbacks = true;
int32_t m_connectTimeout;
2020-03-30 01:44:20 +02:00
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");
std::vector<NimBLERemoteService*> m_servicesVector;
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
}; // 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
};
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
2020-03-30 01:44:20 +02:00
#endif // CONFIG_BT_ENABLED
#endif /* MAIN_NIMBLECLIENT_H_ */