2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLERemoteCharacteristic.h
|
|
|
|
*
|
|
|
|
* Created: on Jan 27 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLERemoteCharacteristic.h
|
|
|
|
*
|
|
|
|
* Created on: Jul 8, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_
|
|
|
|
#define COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLERemoteService.h"
|
|
|
|
#include "NimBLERemoteDescriptor.h"
|
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
#include <vector>
|
2020-09-02 20:13:18 +02:00
|
|
|
#include <functional>
|
2022-01-17 04:11:18 +01:00
|
|
|
#include "NimBLELog.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
class NimBLERemoteService;
|
|
|
|
class NimBLERemoteDescriptor;
|
|
|
|
|
|
|
|
|
2020-09-02 20:13:18 +02:00
|
|
|
typedef std::function<void (NimBLERemoteCharacteristic* pBLERemoteCharacteristic,
|
|
|
|
uint8_t* pData, size_t length, bool isNotify)> notify_callback;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-06-22 06:07:01 +02:00
|
|
|
typedef struct {
|
|
|
|
const NimBLEUUID *uuid;
|
|
|
|
void *task_data;
|
|
|
|
} desc_filter_t;
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief A model of a remote %BLE characteristic.
|
|
|
|
*/
|
|
|
|
class NimBLERemoteCharacteristic {
|
|
|
|
public:
|
|
|
|
~NimBLERemoteCharacteristic();
|
|
|
|
|
|
|
|
// Public member functions
|
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 canBroadcast();
|
|
|
|
bool canIndicate();
|
|
|
|
bool canNotify();
|
|
|
|
bool canRead();
|
|
|
|
bool canWrite();
|
|
|
|
bool canWriteNoResponse();
|
|
|
|
std::vector<NimBLERemoteDescriptor*>::iterator begin();
|
|
|
|
std::vector<NimBLERemoteDescriptor*>::iterator end();
|
|
|
|
NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid);
|
2020-05-23 18:27:32 +02:00
|
|
|
std::vector<NimBLERemoteDescriptor*>* getDescriptors(bool refresh = false);
|
2020-05-30 05:21:56 +02:00
|
|
|
void deleteDescriptors();
|
|
|
|
size_t deleteDescriptor(const NimBLEUUID &uuid);
|
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
|
|
|
uint16_t getHandle();
|
|
|
|
uint16_t getDefHandle();
|
|
|
|
NimBLEUUID getUUID();
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue readValue(time_t *timestamp = nullptr);
|
|
|
|
std::string toString();
|
|
|
|
NimBLERemoteService* getRemoteService();
|
|
|
|
NimBLEAttValue getValue(time_t *timestamp = nullptr);
|
2020-06-19 20:29:30 +02:00
|
|
|
bool subscribe(bool notifications = true,
|
2022-10-24 03:16:33 +02:00
|
|
|
notify_callback notifyCallback = nullptr,
|
|
|
|
bool response = true);
|
|
|
|
bool unsubscribe(bool response = true);
|
2020-05-23 18:27:32 +02:00
|
|
|
bool writeValue(const uint8_t* data,
|
|
|
|
size_t length,
|
|
|
|
bool response = false);
|
2022-01-17 04:11:18 +01:00
|
|
|
bool writeValue(const std::vector<uint8_t>& v, bool response = false);
|
|
|
|
bool writeValue(const char* s, bool response = false);
|
|
|
|
|
|
|
|
|
|
|
|
/*********************** Template Functions ************************/
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
2022-01-17 04:11:18 +01:00
|
|
|
* @brief Template to set the remote characteristic value to <type\>val.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] s The value to write.
|
|
|
|
* @param [in] response True == request write response.
|
2022-01-17 04:11:18 +01:00
|
|
|
* @details Only used for non-arrays and types without a `c_str()` method.
|
2020-07-09 03:27:26 +02:00
|
|
|
*/
|
2020-07-02 01:32:41 +02:00
|
|
|
template<typename T>
|
2022-01-17 04:11:18 +01:00
|
|
|
#ifdef _DOXYGEN_
|
|
|
|
bool
|
|
|
|
#else
|
|
|
|
typename std::enable_if<!std::is_array<T>::value && !Has_c_str_len<T>::value, bool>::type
|
|
|
|
#endif
|
|
|
|
writeValue(const T& s, bool response = false) {
|
2020-07-02 01:32:41 +02:00
|
|
|
return writeValue((uint8_t*)&s, sizeof(T), response);
|
|
|
|
}
|
|
|
|
|
2022-01-17 04:11:18 +01:00
|
|
|
/**
|
|
|
|
* @brief Template to set the remote characteristic value to <type\>val.
|
|
|
|
* @param [in] s The value to write.
|
|
|
|
* @param [in] response True == request write response.
|
|
|
|
* @details Only used if the <type\> has a `c_str()` method.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
#ifdef _DOXYGEN_
|
|
|
|
bool
|
|
|
|
#else
|
|
|
|
typename std::enable_if<Has_c_str_len<T>::value, bool>::type
|
|
|
|
#endif
|
|
|
|
writeValue(const T& s, bool response = false) {
|
|
|
|
return writeValue((uint8_t*)s.c_str(), s.length(), response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Template to convert the remote characteristic data to <type\>.
|
|
|
|
* @tparam T The type to convert the data to.
|
|
|
|
* @param [in] timestamp A pointer to a time_t struct to store the time the value was read.
|
|
|
|
* @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is
|
|
|
|
* less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @details <b>Use:</b> <tt>getValue<type>(×tamp, skipSizeCheck);</tt>
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
|
|
|
if(!skipSizeCheck && m_value.size() < sizeof(T)) return T();
|
|
|
|
return *((T *)m_value.getValue(timestamp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Template to convert the remote characteristic data to <type\>.
|
|
|
|
* @tparam T The type to convert the data to.
|
|
|
|
* @param [in] timestamp A pointer to a time_t struct to store the time the value was read.
|
|
|
|
* @param [in] skipSizeCheck If true it will skip checking if the data size is less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @return The data converted to <type\> or NULL if skipSizeCheck is false and the data is
|
|
|
|
* less than <tt>sizeof(<type\>)</tt>.
|
|
|
|
* @details <b>Use:</b> <tt>readValue<type>(×tamp, skipSizeCheck);</tt>
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
|
|
|
NimBLEAttValue value = readValue();
|
|
|
|
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
|
|
|
return *((T *)value.getValue(timestamp));
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
NimBLERemoteCharacteristic(NimBLERemoteService *pRemoteservice, const struct ble_gatt_chr *chr);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-05-23 18:27:32 +02:00
|
|
|
friend class NimBLEClient;
|
|
|
|
friend class NimBLERemoteService;
|
|
|
|
friend class NimBLERemoteDescriptor;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
// Private member functions
|
2022-10-24 03:16:33 +02:00
|
|
|
bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true);
|
2020-05-23 18:27:32 +02:00
|
|
|
bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
|
|
|
|
static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
|
|
struct ble_gatt_attr *attr, void *arg);
|
|
|
|
static int onWriteCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
|
|
struct ble_gatt_attr *attr, void *arg);
|
2020-03-30 01:44:20 +02:00
|
|
|
static int descriptorDiscCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
2020-05-23 18:27:32 +02:00
|
|
|
uint16_t chr_val_handle, const struct ble_gatt_dsc *dsc,
|
|
|
|
void *arg);
|
2021-05-17 22:37:03 +02:00
|
|
|
static int nextCharCB(uint16_t conn_handle, const struct ble_gatt_error *error,
|
|
|
|
const struct ble_gatt_chr *chr, void *arg);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
// Private properties
|
|
|
|
NimBLEUUID m_uuid;
|
|
|
|
uint8_t m_charProp;
|
|
|
|
uint16_t m_handle;
|
|
|
|
uint16_t m_defHandle;
|
2021-05-17 22:37:03 +02:00
|
|
|
uint16_t m_endHandle;
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLERemoteService* m_pRemoteService;
|
2022-01-17 04:11:18 +01:00
|
|
|
NimBLEAttValue m_value;
|
2020-03-30 01:44:20 +02:00
|
|
|
notify_callback m_notifyCallback;
|
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
// We maintain a vector of descriptors owned by this characteristic.
|
|
|
|
std::vector<NimBLERemoteDescriptor*> m_descriptorVector;
|
2020-05-23 18:27:32 +02:00
|
|
|
}; // NimBLERemoteCharacteristic
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_CENTRAL */
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */
|