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_
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
|
|
|
#if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
|
|
|
|
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>
|
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();
|
2020-05-30 04:02:26 +02:00
|
|
|
std::string readValue(time_t *timestamp = nullptr);
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief A 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>
|
|
|
|
*/
|
2020-05-30 04:02:26 +02:00
|
|
|
template<typename T>
|
|
|
|
T readValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
|
|
|
std::string value = readValue(timestamp);
|
|
|
|
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
|
|
|
const char *pData = value.data();
|
|
|
|
return *((T *)pData);
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:26:44 +02:00
|
|
|
uint8_t readUInt8() __attribute__ ((deprecated("Use template readValue<uint8_t>()")));
|
|
|
|
uint16_t readUInt16() __attribute__ ((deprecated("Use template readValue<uint16_t>()")));
|
|
|
|
uint32_t readUInt32() __attribute__ ((deprecated("Use template readValue<uint32_t>()")));
|
2020-07-02 01:32:41 +02:00
|
|
|
float readFloat() __attribute__ ((deprecated("Use template readValue<float>()")));
|
2020-05-30 02:26:41 +02:00
|
|
|
std::string getValue(time_t *timestamp = nullptr);
|
2020-05-30 04:02:26 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief A 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>
|
|
|
|
*/
|
2020-05-30 04:02:26 +02:00
|
|
|
template<typename T>
|
|
|
|
T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
|
|
|
std::string value = getValue(timestamp);
|
|
|
|
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
|
|
|
const char *pData = value.data();
|
|
|
|
return *((T *)pData);
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:29:30 +02:00
|
|
|
bool subscribe(bool notifications = true,
|
2020-07-09 05:10:11 +02:00
|
|
|
notify_callback notifyCallback = nullptr,
|
|
|
|
bool response = false);
|
|
|
|
bool unsubscribe(bool response = false);
|
2020-06-19 20:29:30 +02:00
|
|
|
bool registerForNotify(notify_callback notifyCallback,
|
2020-05-23 18:27:32 +02:00
|
|
|
bool notifications = true,
|
2020-07-02 01:26:44 +02:00
|
|
|
bool response = true)
|
|
|
|
__attribute__ ((deprecated("Use subscribe()/unsubscribe()")));
|
2020-05-23 18:27:32 +02:00
|
|
|
bool writeValue(const uint8_t* data,
|
|
|
|
size_t length,
|
|
|
|
bool response = false);
|
|
|
|
bool writeValue(const std::string &newValue,
|
|
|
|
bool response = false);
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Convenience template to set the remote characteristic value to <type\>val.
|
|
|
|
* @param [in] s The value to write.
|
|
|
|
* @param [in] response True == request write response.
|
|
|
|
*/
|
2020-07-02 01:32:41 +02:00
|
|
|
template<typename T>
|
|
|
|
bool writeValue(const T &s, bool response = false) {
|
|
|
|
return writeValue((uint8_t*)&s, sizeof(T), response);
|
|
|
|
}
|
|
|
|
|
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::string toString();
|
|
|
|
NimBLERemoteService* getRemoteService();
|
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
|
2020-07-09 05:10:11 +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;
|
|
|
|
std::string m_value;
|
|
|
|
notify_callback m_notifyCallback;
|
2020-05-30 02:26:41 +02:00
|
|
|
time_t m_timestamp;
|
2020-06-22 06:07:01 +02:00
|
|
|
portMUX_TYPE m_valMux;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
#endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED */
|
|
|
|
#endif /* COMPONENTS_NIMBLEREMOTECHARACTERISTIC_H_ */
|