Files
esp-nimble-cpp/src/NimBLEClient.h
T

234 lines
9.5 KiB
C++
Raw Normal View History

2020-03-29 17:44:20 -06:00
/*
2025-02-24 17:53:24 -07:00
* Copyright 2020-2025 Ryan Powell <ryan@nable-embedded.io> and
2024-12-12 19:21:03 -07:00
* esp-nimble-cpp, NimBLE-Arduino contributors.
2020-03-29 17:44:20 -06:00
*
2024-12-12 19:21:03 -07:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2024-12-12 19:21:03 -07:00
* http://www.apache.org/licenses/LICENSE-2.0
2020-03-29 17:44:20 -06:00
*
2024-12-12 19:21:03 -07:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2020-03-29 17:44:20 -06:00
*/
2024-11-02 19:00:07 -06:00
#ifndef NIMBLE_CPP_CLIENT_H_
#define NIMBLE_CPP_CLIENT_H_
2020-03-29 17:44:20 -06:00
#include "nimconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
2020-03-29 17:44:20 -06:00
2024-11-02 19:00:07 -06:00
# if defined(CONFIG_NIMBLE_CPP_IDF)
# include "host/ble_gap.h"
# else
# include "nimble/nimble/host/include/host/ble_gap.h"
# endif
2020-03-29 17:44:20 -06:00
2024-11-02 19:00:07 -06:00
# include "NimBLEAddress.h"
2020-03-29 17:44:20 -06:00
2024-11-02 19:00:07 -06:00
# include <stdint.h>
# include <vector>
# include <string>
class NimBLEAddress;
class NimBLEUUID;
2020-03-29 17:44:20 -06:00
class NimBLERemoteService;
class NimBLERemoteCharacteristic;
2020-03-29 17:44:20 -06:00
class NimBLEAdvertisedDevice;
2024-11-02 19:00:07 -06:00
class NimBLEAttValue;
2024-07-26 14:47:36 -06:00
class NimBLEClientCallbacks;
2024-11-02 19:00:07 -06:00
class NimBLEConnInfo;
2024-11-10 13:31:37 -07:00
struct NimBLETaskData;
2020-03-29 17:44:20 -06:00
/**
2024-11-02 19:00:07 -06:00
* @brief A model of a BLE client.
2020-03-29 17:44:20 -06:00
*/
class NimBLEClient {
2024-11-02 19:00:07 -06:00
public:
bool connect(const NimBLEAdvertisedDevice* device,
bool deleteAttributes = true,
bool asyncConnect = false,
bool exchangeMTU = true);
bool connect(const NimBLEAddress& address, bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true);
bool connect(bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true);
2024-11-02 19:00:07 -06:00
bool disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
bool cancelConnect() const;
void setSelfDelete(bool deleteOnDisconnect, bool deleteOnConnectFail);
2024-11-02 19:00:07 -06:00
NimBLEAddress getPeerAddress() const;
bool setPeerAddress(const NimBLEAddress& address);
int getRssi() const;
bool isConnected() const;
void setClientCallbacks(NimBLEClientCallbacks* pClientCallbacks, bool deleteCallbacks = true);
std::string toString() const;
uint16_t getConnHandle() const;
uint16_t getMTU() const;
bool exchangeMTU();
2024-11-18 14:02:18 -05:00
bool secureConnection(bool async = false) const;
2024-11-02 19:00:07 -06:00
void setConnectTimeout(uint32_t timeout);
bool setDataLen(uint16_t txOctets);
bool discoverAttributes();
NimBLEConnInfo getConnInfo() const;
int getLastError() const;
bool updateConnParams(uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);
void setConnectionParams(uint16_t minInterval,
uint16_t maxInterval,
uint16_t latency,
uint16_t timeout,
uint16_t scanInterval = 16,
uint16_t scanWindow = 16);
const std::vector<NimBLERemoteService*>& getServices(bool refresh = false);
2020-05-22 20:13:52 -06:00
std::vector<NimBLERemoteService*>::iterator begin();
std::vector<NimBLERemoteService*>::iterator end();
2024-11-02 19:00:07 -06:00
NimBLERemoteCharacteristic* getCharacteristic(uint16_t handle);
NimBLERemoteService* getService(const char* uuid);
2024-11-02 19:00:07 -06:00
NimBLERemoteService* getService(const NimBLEUUID& uuid);
void deleteServices();
2024-11-02 19:00:07 -06:00
size_t deleteService(const NimBLEUUID& uuid);
NimBLEAttValue getValue(const NimBLEUUID& serviceUUID, const NimBLEUUID& characteristicUUID);
bool setValue(const NimBLEUUID& serviceUUID,
const NimBLEUUID& characteristicUUID,
const NimBLEAttValue& value,
bool response = false);
2024-11-02 19:00:07 -06:00
# if CONFIG_BT_NIMBLE_EXT_ADV
2024-11-28 12:50:24 -07:00
void setConnectPhy(uint8_t phyMask);
bool updatePhy(uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions = 0);
bool getPhy(uint8_t* txPhy, uint8_t* rxPhy);
2024-11-02 19:00:07 -06:00
# endif
struct Config {
uint8_t deleteCallbacks : 1; // Delete the callback object when the client is deleted.
uint8_t deleteOnDisconnect : 1; // Delete the client when disconnected.
uint8_t deleteOnConnectFail : 1; // Delete the client when a connection attempt fails.
uint8_t asyncConnect : 1; // Connect asynchronously.
uint8_t exchangeMTU : 1; // Exchange MTU after connection.
};
Config getConfig() const;
void setConfig(Config config);
2024-11-02 19:00:07 -06:00
private:
NimBLEClient(const NimBLEAddress& peerAddress);
2020-03-29 17:44:20 -06:00
~NimBLEClient();
2024-11-02 19:00:07 -06:00
NimBLEClient(const NimBLEClient&) = delete;
NimBLEClient& operator=(const NimBLEClient&) = delete;
2020-03-29 17:44:20 -06:00
2024-11-02 19:00:07 -06:00
bool retrieveServices(const NimBLEUUID* uuidFilter = nullptr);
static int handleGapEvent(struct ble_gap_event* event, void* arg);
static int exchangeMTUCb(uint16_t conn_handle, const ble_gatt_error* error, uint16_t mtu, void* arg);
2024-11-02 19:00:07 -06:00
static int serviceDiscoveredCB(uint16_t connHandle,
const struct ble_gatt_error* error,
const struct ble_gatt_svc* service,
void* arg);
2020-03-29 17:44:20 -06:00
2024-11-02 19:00:07 -06:00
NimBLEAddress m_peerAddress;
mutable int m_lastErr;
int32_t m_connectTimeout;
2024-11-10 13:31:37 -07:00
mutable NimBLETaskData* m_pTaskData;
2024-11-02 19:00:07 -06:00
std::vector<NimBLERemoteService*> m_svcVec;
NimBLEClientCallbacks* m_pClientCallbacks;
uint16_t m_connHandle;
uint8_t m_terminateFailCount;
2024-11-18 14:02:18 -05:00
mutable uint8_t m_asyncSecureAttempt;
Config m_config;
2024-11-02 19:00:07 -06:00
# if CONFIG_BT_NIMBLE_EXT_ADV
uint8_t m_phyMask;
# endif
ble_gap_conn_params m_connParams;
2020-03-29 17:44:20 -06:00
2024-11-02 19:00:07 -06:00
friend class NimBLEDevice;
friend class NimBLEServer;
}; // class NimBLEClient
2020-03-29 17:44:20 -06:00
/**
* @brief Callbacks associated with a %BLE client.
*/
class NimBLEClientCallbacks {
2024-11-02 19:00:07 -06:00
public:
2020-03-29 17:44:20 -06:00
virtual ~NimBLEClientCallbacks() {};
2020-07-08 19:27:26 -06:00
/**
* @brief Called after client connects.
* @param [in] pClient A pointer to the connecting client object.
2020-07-08 19:27:26 -06:00
*/
2020-04-13 19:13:51 -06:00
virtual void onConnect(NimBLEClient* pClient);
2020-07-08 19:27:26 -06:00
/**
* @brief Called when a connection attempt fails.
* @param [in] pClient A pointer to the connecting client object.
* @param [in] reason Contains the reason code for the connection failure.
*/
virtual void onConnectFail(NimBLEClient* pClient, int reason);
2020-07-08 19:27:26 -06:00
/**
* @brief Called when disconnected from the server.
* @param [in] pClient A pointer to the calling client object.
2022-08-27 12:38:53 -06:00
* @param [in] reason Contains the reason code for the disconnection.
2020-07-08 19:27:26 -06:00
*/
virtual void onDisconnect(NimBLEClient* pClient, int reason);
2020-07-08 19:27:26 -06:00
/**
* @brief Called when server requests to update the connection parameters.
* @param [in] pClient A pointer to the calling client object.
* @param [in] params A pointer to the struct containing the connection parameters requested.
2022-07-31 11:00:12 -06:00
* @return True to accept the parameters.
2020-07-08 19:27:26 -06:00
*/
2020-04-13 19:13:51 -06:00
virtual bool onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params);
2020-07-08 19:27:26 -06:00
/**
* @brief Called when server requests a passkey for pairing.
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.
2020-07-08 19:27:26 -06:00
*/
2024-07-26 14:47:36 -06:00
virtual void onPassKeyEntry(NimBLEConnInfo& connInfo);
2020-07-08 19:27:26 -06:00
/**
* @brief Called when the pairing procedure is complete.
2022-08-27 12:38:53 -06:00
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.\n
2020-07-08 19:27:26 -06:00
* This can be used to check the status of the connection encryption/pairing.
*/
2024-07-26 14:47:36 -06:00
virtual void onAuthenticationComplete(NimBLEConnInfo& connInfo);
2020-07-08 19:27:26 -06:00
/**
* @brief Called when using numeric comparision for pairing.
* @param [in] connInfo A reference to a NimBLEConnInfo instance containing the peer info.
2020-07-08 19:27:26 -06:00
* @param [in] pin The pin to compare with the server.
*/
2024-11-02 19:00:07 -06:00
virtual void onConfirmPasskey(NimBLEConnInfo& connInfo, uint32_t pin);
2024-06-12 09:26:10 -06:00
/**
* @brief Called when the peer identity address is resolved.
* @param [in] connInfo A reference to a NimBLEConnInfo instance with information
*/
2024-07-26 14:47:36 -06:00
virtual void onIdentity(NimBLEConnInfo& connInfo);
/**
* @brief Called when the connection MTU changes.
* @param [in] pClient A pointer to the client that the MTU change is associated with.
* @param [in] MTU The new MTU value.
* about the peer connection parameters.
*/
virtual void onMTUChange(NimBLEClient* pClient, uint16_t MTU);
2024-11-28 12:50:24 -07:00
# if CONFIG_BT_NIMBLE_EXT_ADV
/**
* @brief Called when the PHY update procedure is complete.
* @param [in] pClient A pointer to the client whose PHY was updated.
* about the peer connection parameters.
* @param [in] txPhy The transmit PHY.
* @param [in] rxPhy The receive PHY.
* Possible values:
* * BLE_GAP_LE_PHY_1M
* * BLE_GAP_LE_PHY_2M
* * BLE_GAP_LE_PHY_CODED
*/
virtual void onPhyUpdate(NimBLEClient* pClient, uint8_t txPhy, uint8_t rxPhy);
# endif
2020-03-29 17:44:20 -06:00
};
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
2024-11-02 19:00:07 -06:00
#endif /* NIMBLE_CPP_CLIENT_H_ */