forked from h2zero/esp-nimble-cpp
Conditionally compile code for specific roles.
This allows NimBLE options in menuconfig to reduce code size based on the roles selected (scan/advertising/central/peripheral). Significant code space can be saved by removing unnecessary roles for the application.
This commit is contained in:
+94
-89
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* Created: on March 3, 2020
|
||||
* Author H2zero
|
||||
*
|
||||
*
|
||||
* Originally:
|
||||
* BLECharacteristic.h
|
||||
*
|
||||
@@ -16,6 +16,9 @@
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED)
|
||||
|
||||
#include "nimconfig.h"
|
||||
#if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
|
||||
#include "host/ble_hs.h"
|
||||
/**** FIX COMPILATION ****/
|
||||
#undef min
|
||||
@@ -23,10 +26,10 @@
|
||||
/**************************/
|
||||
|
||||
typedef enum {
|
||||
READ = BLE_GATT_CHR_F_READ,
|
||||
READ = BLE_GATT_CHR_F_READ,
|
||||
READ_ENC = BLE_GATT_CHR_F_READ_ENC,
|
||||
READ_AUTHEN = BLE_GATT_CHR_F_READ_AUTHEN,
|
||||
READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,
|
||||
READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,
|
||||
WRITE = BLE_GATT_CHR_F_WRITE,
|
||||
WRITE_NR = BLE_GATT_CHR_F_WRITE_NO_RSP,
|
||||
WRITE_ENC = BLE_GATT_CHR_F_WRITE_ENC,
|
||||
@@ -57,21 +60,21 @@ class NimBLECharacteristicCallbacks;
|
||||
*/
|
||||
class NimBLEDescriptorMap {
|
||||
public:
|
||||
void setByUUID(const char* uuid, NimBLEDescriptor* pDescriptor);
|
||||
void setByUUID(const NimBLEUUID &uuid, NimBLEDescriptor* pDescriptor);
|
||||
// void setByHandle(uint16_t handle, NimBLEDescriptor* pDescriptor);
|
||||
NimBLEDescriptor* getByUUID(const char* uuid);
|
||||
NimBLEDescriptor* getByUUID(const NimBLEUUID &uuid);
|
||||
// NimBLEDescriptor* getByHandle(uint16_t handle);
|
||||
std::string toString();
|
||||
NimBLEDescriptor* getFirst();
|
||||
NimBLEDescriptor* getNext();
|
||||
void setByUUID(const char* uuid, NimBLEDescriptor* pDescriptor);
|
||||
void setByUUID(const NimBLEUUID &uuid, NimBLEDescriptor* pDescriptor);
|
||||
// void setByHandle(uint16_t handle, NimBLEDescriptor* pDescriptor);
|
||||
NimBLEDescriptor* getByUUID(const char* uuid);
|
||||
NimBLEDescriptor* getByUUID(const NimBLEUUID &uuid);
|
||||
// NimBLEDescriptor* getByHandle(uint16_t handle);
|
||||
std::string toString();
|
||||
NimBLEDescriptor* getFirst();
|
||||
NimBLEDescriptor* getNext();
|
||||
uint8_t getSize();
|
||||
|
||||
|
||||
private:
|
||||
std::map<NimBLEDescriptor*, std::string> m_uuidMap;
|
||||
// std::map<uint16_t, BLEDescriptor*> m_handleMap;
|
||||
std::map<NimBLEDescriptor*, std::string>::iterator m_iterator;
|
||||
std::map<NimBLEDescriptor*, std::string> m_uuidMap;
|
||||
// std::map<uint16_t, BLEDescriptor*> m_handleMap;
|
||||
std::map<NimBLEDescriptor*, std::string>::iterator m_iterator;
|
||||
};
|
||||
|
||||
|
||||
@@ -83,85 +86,85 @@ private:
|
||||
*/
|
||||
class NimBLECharacteristic {
|
||||
public:
|
||||
NimBLEDescriptor* createDescriptor(const char* uuid,
|
||||
uint32_t properties = NIMBLE_PROPERTY::READ |
|
||||
NIMBLE_PROPERTY::WRITE,
|
||||
NimBLEDescriptor* createDescriptor(const char* uuid,
|
||||
uint32_t properties = NIMBLE_PROPERTY::READ |
|
||||
NIMBLE_PROPERTY::WRITE,
|
||||
uint16_t max_len = 100);
|
||||
NimBLEDescriptor* createDescriptor(const NimBLEUUID &uuid,
|
||||
uint32_t properties = NIMBLE_PROPERTY::READ |
|
||||
NIMBLE_PROPERTY::WRITE,
|
||||
NimBLEDescriptor* createDescriptor(const NimBLEUUID &uuid,
|
||||
uint32_t properties = NIMBLE_PROPERTY::READ |
|
||||
NIMBLE_PROPERTY::WRITE,
|
||||
uint16_t max_len = 100);
|
||||
|
||||
NimBLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
|
||||
NimBLEDescriptor* getDescriptorByUUID(const NimBLEUUID &descriptorUUID);
|
||||
NimBLEUUID getUUID();
|
||||
std::string getValue();
|
||||
uint8_t* getData();
|
||||
size_t getDataLength();
|
||||
|
||||
void indicate();
|
||||
void notify(bool is_notification = true);
|
||||
void setCallbacks(NimBLECharacteristicCallbacks* pCallbacks);
|
||||
NimBLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
|
||||
NimBLEDescriptor* getDescriptorByUUID(const NimBLEUUID &descriptorUUID);
|
||||
NimBLEUUID getUUID();
|
||||
std::string getValue();
|
||||
uint8_t* getData();
|
||||
size_t getDataLength();
|
||||
|
||||
void indicate();
|
||||
void notify(bool is_notification = true);
|
||||
void setCallbacks(NimBLECharacteristicCallbacks* pCallbacks);
|
||||
// Backward Compatibility - to be removed
|
||||
void setBroadcastProperty(bool value);
|
||||
void setIndicateProperty(bool value);
|
||||
void setNotifyProperty(bool value);
|
||||
void setReadProperty(bool value);
|
||||
void setIndicateProperty(bool value);
|
||||
void setNotifyProperty(bool value);
|
||||
void setReadProperty(bool value);
|
||||
void setWriteProperty(bool value);
|
||||
void setWriteNoResponseProperty(bool value);
|
||||
//////////////////////////////////////////////////////
|
||||
void setValue(const uint8_t* data, size_t size);
|
||||
void setValue(const std::string &value);
|
||||
void setValue(uint16_t& data16);
|
||||
void setValue(uint32_t& data32);
|
||||
void setValue(int& data32);
|
||||
void setValue(float& data32);
|
||||
void setValue(double& data64);
|
||||
void setWriteNoResponseProperty(bool value);
|
||||
//////////////////////////////////////////////////////
|
||||
void setValue(const uint8_t* data, size_t size);
|
||||
void setValue(const std::string &value);
|
||||
void setValue(uint16_t& data16);
|
||||
void setValue(uint32_t& data32);
|
||||
void setValue(int& data32);
|
||||
void setValue(float& data32);
|
||||
void setValue(double& data64);
|
||||
|
||||
std::string toString();
|
||||
uint16_t getHandle();
|
||||
// void setAccessPermissions(uint16_t perm);
|
||||
std::string toString();
|
||||
uint16_t getHandle();
|
||||
// void setAccessPermissions(uint16_t perm);
|
||||
|
||||
// Backward Compatibility - to be removed
|
||||
/* static const uint32_t PROPERTY_READ = 1<<0;
|
||||
static const uint32_t PROPERTY_WRITE = 1<<1;
|
||||
static const uint32_t PROPERTY_NOTIFY = 1<<2;
|
||||
static const uint32_t PROPERTY_BROADCAST = 1<<3;
|
||||
static const uint32_t PROPERTY_INDICATE = 1<<4;
|
||||
static const uint32_t PROPERTY_WRITE_NR = 1<<5;
|
||||
/* static const uint32_t PROPERTY_READ = 1<<0;
|
||||
static const uint32_t PROPERTY_WRITE = 1<<1;
|
||||
static const uint32_t PROPERTY_NOTIFY = 1<<2;
|
||||
static const uint32_t PROPERTY_BROADCAST = 1<<3;
|
||||
static const uint32_t PROPERTY_INDICATE = 1<<4;
|
||||
static const uint32_t PROPERTY_WRITE_NR = 1<<5;
|
||||
*/
|
||||
//////////////////////////////////////////////////////
|
||||
|
||||
|
||||
private:
|
||||
|
||||
friend class NimBLEServer;
|
||||
friend class NimBLEService;
|
||||
// friend class NimBLEDescriptor;
|
||||
// friend class NimBLECharacteristicMap;
|
||||
friend class NimBLEServer;
|
||||
friend class NimBLEService;
|
||||
// friend class NimBLEDescriptor;
|
||||
// friend class NimBLECharacteristicMap;
|
||||
|
||||
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);
|
||||
virtual ~NimBLECharacteristic();
|
||||
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);
|
||||
virtual ~NimBLECharacteristic();
|
||||
|
||||
NimBLEUUID m_uuid;
|
||||
NimBLEDescriptorMap m_descriptorMap;
|
||||
uint16_t m_handle;
|
||||
uint16_t m_properties;
|
||||
NimBLECharacteristicCallbacks* m_pCallbacks;
|
||||
NimBLEService* m_pService;
|
||||
NimBLEValue m_value;
|
||||
// uint16_t m_permissions;
|
||||
|
||||
void addDescriptor(NimBLEDescriptor* pDescriptor);
|
||||
NimBLEUUID m_uuid;
|
||||
NimBLEDescriptorMap m_descriptorMap;
|
||||
uint16_t m_handle;
|
||||
uint16_t m_properties;
|
||||
NimBLECharacteristicCallbacks* m_pCallbacks;
|
||||
NimBLEService* m_pService;
|
||||
NimBLEValue m_value;
|
||||
// uint16_t m_permissions;
|
||||
|
||||
void addDescriptor(NimBLEDescriptor* pDescriptor);
|
||||
NimBLEService* getService();
|
||||
uint8_t getProperties();
|
||||
void setSubscribe(struct ble_gap_event *event);
|
||||
static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
|
||||
static int handleGapEvent(uint16_t conn_handle, uint16_t attr_handle,
|
||||
struct ble_gatt_access_ctxt *ctxt, void *arg);
|
||||
|
||||
FreeRTOS::Semaphore m_semaphoreConfEvt = FreeRTOS::Semaphore("ConfEvt");
|
||||
FreeRTOS::Semaphore m_semaphoreConfEvt = FreeRTOS::Semaphore("ConfEvt");
|
||||
}; // NimBLECharacteristic
|
||||
|
||||
|
||||
@@ -175,21 +178,23 @@ private:
|
||||
class NimBLECharacteristicCallbacks {
|
||||
public:
|
||||
typedef enum {
|
||||
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);
|
||||
virtual void onWrite(NimBLECharacteristic* pCharacteristic);
|
||||
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);
|
||||
virtual void onWrite(NimBLECharacteristic* pCharacteristic);
|
||||
virtual void onNotify(NimBLECharacteristic* pCharacteristic);
|
||||
virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
|
||||
virtual void onStatus(NimBLECharacteristic* pCharacteristic, Status s, int code);
|
||||
};
|
||||
|
||||
#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
|
||||
#endif /* CONFIG_BT_ENABLED */
|
||||
#endif /*MAIN_NIMBLECHARACTERISTIC_H_*/
|
||||
|
||||
Reference in New Issue
Block a user