2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLECharacteristic.h
|
|
|
|
*
|
|
|
|
* Created: on March 3, 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
* BLECharacteristic.h
|
|
|
|
*
|
|
|
|
* Created on: Jun 22, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MAIN_NIMBLECHARACTERISTIC_H_
|
|
|
|
#define MAIN_NIMBLECHARACTERISTIC_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_PERIPHERAL)
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "host/ble_hs.h"
|
2020-04-01 04:16:27 +02:00
|
|
|
/**** FIX COMPILATION ****/
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
/**************************/
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
typedef enum {
|
2020-05-14 06:03:56 +02:00
|
|
|
READ = BLE_GATT_CHR_F_READ,
|
2020-03-30 01:44:20 +02:00
|
|
|
READ_ENC = BLE_GATT_CHR_F_READ_ENC,
|
|
|
|
READ_AUTHEN = BLE_GATT_CHR_F_READ_AUTHEN,
|
2020-05-14 06:03:56 +02:00
|
|
|
READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,
|
2020-03-30 01:44:20 +02:00
|
|
|
WRITE = BLE_GATT_CHR_F_WRITE,
|
|
|
|
WRITE_NR = BLE_GATT_CHR_F_WRITE_NO_RSP,
|
|
|
|
WRITE_ENC = BLE_GATT_CHR_F_WRITE_ENC,
|
|
|
|
WRITE_AUTHEN = BLE_GATT_CHR_F_WRITE_AUTHEN,
|
|
|
|
WRITE_AUTHOR = BLE_GATT_CHR_F_WRITE_AUTHOR,
|
|
|
|
BROADCAST = BLE_GATT_CHR_F_BROADCAST,
|
|
|
|
NOTIFY = BLE_GATT_CHR_F_NOTIFY,
|
|
|
|
INDICATE = BLE_GATT_CHR_F_INDICATE
|
|
|
|
} NIMBLE_PROPERTY;
|
|
|
|
|
|
|
|
#include "NimBLEService.h"
|
|
|
|
#include "NimBLEDescriptor.h"
|
|
|
|
|
|
|
|
#include <string>
|
2020-06-08 02:42:28 +02:00
|
|
|
#include <vector>
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
class NimBLEService;
|
|
|
|
class NimBLEDescriptor;
|
|
|
|
class NimBLECharacteristicCallbacks;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The model of a %BLE Characteristic.
|
|
|
|
*
|
|
|
|
* A BLE Characteristic is an identified value container that manages a value. It is exposed by a BLE server and
|
|
|
|
* can be read and written to by a %BLE client.
|
|
|
|
*/
|
|
|
|
class NimBLECharacteristic {
|
|
|
|
public:
|
2021-02-08 16:28:32 +01:00
|
|
|
|
|
|
|
uint16_t getHandle();
|
|
|
|
NimBLEUUID getUUID();
|
|
|
|
std::string toString();
|
|
|
|
|
|
|
|
void setCallbacks(NimBLECharacteristicCallbacks* pCallbacks);
|
2021-05-20 06:21:05 +02:00
|
|
|
NimBLECharacteristicCallbacks*
|
|
|
|
getCallbacks();
|
2021-02-08 16:28:32 +01:00
|
|
|
|
|
|
|
void indicate();
|
|
|
|
void notify(bool is_notification = true);
|
|
|
|
size_t getSubscribedCount();
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEDescriptor* createDescriptor(const char* uuid,
|
2020-06-08 02:42:28 +02:00
|
|
|
uint32_t properties =
|
|
|
|
NIMBLE_PROPERTY::READ |
|
|
|
|
NIMBLE_PROPERTY::WRITE,
|
|
|
|
uint16_t max_len = 100);
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEDescriptor* createDescriptor(const NimBLEUUID &uuid,
|
2020-06-08 02:42:28 +02:00
|
|
|
uint32_t properties =
|
|
|
|
NIMBLE_PROPERTY::READ |
|
|
|
|
NIMBLE_PROPERTY::WRITE,
|
|
|
|
uint16_t max_len = 100);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
NimBLEDescriptor* getDescriptorByUUID(const char* uuid);
|
|
|
|
NimBLEDescriptor* getDescriptorByUUID(const NimBLEUUID &uuid);
|
2021-02-08 16:28:32 +01:00
|
|
|
NimBLEDescriptor* getDescriptorByHandle(uint16_t handle);
|
2020-07-02 01:32:41 +02:00
|
|
|
|
2021-02-08 16:28:32 +01:00
|
|
|
std::string getValue(time_t *timestamp = nullptr);
|
|
|
|
size_t getDataLength();
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief A template to convert the 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-07-02 01:32:41 +02:00
|
|
|
template<typename T>
|
|
|
|
T getValue(time_t *timestamp = nullptr, bool skipSizeCheck = false) {
|
|
|
|
std::string value = getValue();
|
|
|
|
if(!skipSizeCheck && value.size() < sizeof(T)) return T();
|
|
|
|
const char *pData = value.data();
|
|
|
|
return *((T *)pData);
|
|
|
|
}
|
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
void setValue(const uint8_t* data, size_t size);
|
|
|
|
void setValue(const std::string &value);
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Convenience template to set the characteristic value to <type\>val.
|
|
|
|
* @param [in] s The value to set.
|
|
|
|
*/
|
2020-07-02 01:32:41 +02:00
|
|
|
template<typename T>
|
2021-02-08 16:28:32 +01:00
|
|
|
void setValue(const T &s) {
|
2020-07-02 01:32:41 +02:00
|
|
|
setValue((uint8_t*)&s, sizeof(T));
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:28:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
private:
|
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
friend class NimBLEServer;
|
|
|
|
friend class NimBLEService;
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
NimBLECharacteristic(const char* uuid,
|
|
|
|
uint16_t properties =
|
|
|
|
NIMBLE_PROPERTY::READ |
|
|
|
|
NIMBLE_PROPERTY::WRITE,
|
|
|
|
NimBLEService* pService = nullptr);
|
|
|
|
NimBLECharacteristic(const NimBLEUUID &uuid,
|
|
|
|
uint16_t properties =
|
|
|
|
NIMBLE_PROPERTY::READ |
|
|
|
|
NIMBLE_PROPERTY::WRITE,
|
|
|
|
NimBLEService* pService = nullptr);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
~NimBLECharacteristic();
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEService* getService();
|
2020-06-22 04:26:16 +02:00
|
|
|
uint16_t getProperties();
|
2020-03-30 01:44:20 +02:00
|
|
|
void setSubscribe(struct ble_gap_event *event);
|
2020-05-14 06:03:56 +02:00
|
|
|
static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
|
2020-06-08 02:42:28 +02:00
|
|
|
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-06-08 02:42:28 +02:00
|
|
|
NimBLEUUID m_uuid;
|
|
|
|
uint16_t m_handle;
|
|
|
|
uint16_t m_properties;
|
|
|
|
NimBLECharacteristicCallbacks* m_pCallbacks;
|
|
|
|
NimBLEService* m_pService;
|
|
|
|
std::string m_value;
|
|
|
|
std::vector<NimBLEDescriptor*> m_dscVec;
|
|
|
|
portMUX_TYPE m_valMux;
|
2020-07-02 01:32:41 +02:00
|
|
|
time_t m_timestamp;
|
2020-07-28 05:11:38 +02:00
|
|
|
|
|
|
|
std::vector<std::pair<uint16_t, uint16_t>> m_subscribedVec;
|
2020-03-30 01:44:20 +02:00
|
|
|
}; // NimBLECharacteristic
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callbacks that can be associated with a %BLE characteristic to inform of events.
|
|
|
|
*
|
|
|
|
* When a server application creates a %BLE characteristic, we may wish to be informed when there is either
|
|
|
|
* a read or write request to the characteristic's value. An application can register a
|
|
|
|
* sub-classed instance of this class and will be notified when such an event happens.
|
|
|
|
*/
|
|
|
|
class NimBLECharacteristicCallbacks {
|
|
|
|
public:
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief An enum to provide the callback the status of the
|
|
|
|
* notification/indication, implemented for backward compatibility.
|
|
|
|
* @deprecated To be removed in the future as the NimBLE stack return code is also provided.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
typedef enum {
|
2020-05-14 06:03:56 +02:00
|
|
|
SUCCESS_INDICATE,
|
|
|
|
SUCCESS_NOTIFY,
|
|
|
|
ERROR_INDICATE_DISABLED,
|
|
|
|
ERROR_NOTIFY_DISABLED,
|
|
|
|
ERROR_GATT,
|
|
|
|
ERROR_NO_CLIENT,
|
|
|
|
ERROR_INDICATE_TIMEOUT,
|
|
|
|
ERROR_INDICATE_FAILURE
|
|
|
|
}Status;
|
|
|
|
|
|
|
|
virtual ~NimBLECharacteristicCallbacks();
|
|
|
|
virtual void onRead(NimBLECharacteristic* pCharacteristic);
|
2020-07-14 23:14:29 +02:00
|
|
|
virtual void onRead(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
|
2020-05-14 06:03:56 +02:00
|
|
|
virtual void onWrite(NimBLECharacteristic* pCharacteristic);
|
2020-07-14 23:14:29 +02:00
|
|
|
virtual void onWrite(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc);
|
2020-03-30 01:44:20 +02:00
|
|
|
virtual void onNotify(NimBLECharacteristic* pCharacteristic);
|
2020-05-14 06:03:56 +02:00
|
|
|
virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
|
2020-07-28 05:11:38 +02:00
|
|
|
virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, ble_gap_conn_desc* desc, uint16_t subValue);
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED */
|
2020-04-23 23:17:09 +02:00
|
|
|
#endif /*MAIN_NIMBLECHARACTERISTIC_H_*/
|