Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
menu "ESP-NimBLE-CPP configuration"

config NIMBLE_USING_ARDUINO_IOT
bool "Renames the enums READ and WRITE that conflit with the Arduino IoT lib"
default "n"
help
Enabling this option will change the enum NIMBLE_PROPERTY:READ to NIMBLE_PROPERTY:BLE_READ and NIMBLE_PROPERTY:WRITE to NIMBLE_PROPERTY:BLE_WRITE

config NIMBLE_USE_MAGIC_ENUM
bool "Enable if magic enum lib is set to get detailed text for Class of device property"
default "n"
help
Enable if magic enum lib is set to get detailed text for Class of device property

choice NIMBLE_CPP_LOG_LEVEL
prompt "NimBLE CPP log verbosity"
default NIMBLE_CPP_LOG_LEVEL_NONE
Expand Down
24 changes: 20 additions & 4 deletions src/NimBLEAdvertisementData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "NimBLEAdvertisementData.h"
#if (CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_BROADCASTER) && !MYNEWT_VAL(BLE_EXT_ADV)) || defined(_DOXYGEN_)
#if (CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER && !CONFIG_BT_NIMBLE_EXT_ADV) || defined(_DOXYGEN_)

# include "NimBLEDevice.h"
# include "NimBLEUtils.h"
Expand All @@ -31,15 +30,32 @@

static const char* LOG_TAG = "NimBLEAdvertisementData";

/**
* @brief Set the advertisement flags.
* @param [in] flag The flags to be set in the advertisement.
* * BLE_HS_ADV_F_DISC_LTD
* * BLE_HS_ADV_F_DISC_GEN
* * BLE_HS_ADV_F_BREDR_UNSUP - must always use with NimBLE
* A flag value of 0 will remove the flags from the advertisement.
*/
bool NimBLEAdvertisementData::setCODData(const NimClassOfDeviceType::bluetooth_cod_t cod) {
int dataLoc = getDataLocation(BLE_HS_ADV_TYPE_CLASS_OF_DEVICE);
if (dataLoc != -1) {
removeData(BLE_HS_ADV_TYPE_CLASS_OF_DEVICE);
}
return addData((const uint8_t*)((NimClassOfDeviceType::makeATT_Payload_CodeClassOfDevice(cod)).data()),5);
} // SetCOD

/**
* @brief Add data to the payload to be advertised.
* @param [in] data The data to be added to the payload.
* @param [in] length The size of data to be added to the payload.
*/
bool NimBLEAdvertisementData::addData(const uint8_t* data, size_t length) {
if (m_payload.size() + length > BLE_HS_ADV_MAX_SZ) {
NIMBLE_LOGE(LOG_TAG, "Data length exceeded");
return false;

NIMBLE_LOGE(LOG_TAG, "Data length exceeded %i mx lenth id %i",m_payload.size() + length, BLE_HS_ADV_MAX_SZ);
NIMBLE_LOGE(LOG_TAG, "Current data %s", NimBLEUtils::dataToHexString(m_payload.data(),m_payload.size()).c_str());
}

m_payload.insert(m_payload.end(), data, data + length);
Expand Down
8 changes: 4 additions & 4 deletions src/NimBLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ class NimBLEAdvertising {
void setMaxInterval(uint16_t maxInterval);
void setMinInterval(uint16_t minInterval);

bool setAdvertisementData(const NimBLEAdvertisementData& advertisementData);
bool setScanResponseData(const NimBLEAdvertisementData& advertisementData);
bool setAdvertisementData(const NimBLEAdvertisementData& advertisementData);
bool setScanResponseData(const NimBLEAdvertisementData& advertisementData);
const NimBLEAdvertisementData& getAdvertisementData();
const NimBLEAdvertisementData& getScanData();
void clearData();
bool refreshAdvertisingData();
void clearData();
bool refreshAdvertisingData();

bool addServiceUUID(const NimBLEUUID& serviceUUID);
bool addServiceUUID(const char* serviceUUID);
Expand Down
13 changes: 13 additions & 0 deletions src/NimBLECharacteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class NimBLE2904;
# include <string>
# include <vector>

// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# define WRITE BLE_WRITE
# define READ BLE_READ
# endif

/**
* @brief The model of a BLE Characteristic.
*
Expand Down Expand Up @@ -254,5 +260,12 @@ class NimBLECharacteristicCallbacks {
virtual void onSubscribe(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo& connInfo, uint16_t subValue);
};


// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# undef WRITE
# undef READ
# endif

#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
#endif // NIMBLE_CPP_CHARACTERISTIC_H_
13 changes: 13 additions & 0 deletions src/NimBLEDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

# include <string>

// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# define WRITE BLE_WRITE
# define READ BLE_READ
# endif

static const char* LOG_TAG = "NimBLEDescriptor";
static NimBLEDescriptorCallbacks defaultCallbacks;

Expand Down Expand Up @@ -147,4 +153,11 @@ void NimBLEDescriptorCallbacks::onWrite(NimBLEDescriptor* pDescriptor, NimBLECon
NIMBLE_LOGD("NimBLEDescriptorCallbacks", "onWrite: default");
} // onWrite


// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# undef WRITE
# undef READ
# endif

#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
37 changes: 17 additions & 20 deletions src/NimBLEExtAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,6 @@ bool NimBLEExtAdvertisement::setFlags(uint8_t flag) {
* @return True if successful.
*/
bool NimBLEExtAdvertisement::setManufacturerData(const uint8_t* data, size_t length) {
if (length > 0xFF - 1) {
NIMBLE_LOGE(LOG_TAG, "Manufacturer data too long!");
return false;
}

uint8_t header[2];
header[0] = length + 1;
header[1] = BLE_HS_ADV_TYPE_MFG_DATA;
Expand Down Expand Up @@ -657,11 +652,6 @@ bool NimBLEExtAdvertisement::setManufacturerData(const std::vector<uint8_t>& dat
* @return True if successful.
*/
bool NimBLEExtAdvertisement::setURI(const std::string& uri) {
if (uri.length() > 0xFF - 1) {
NIMBLE_LOGE(LOG_TAG, "URI too long!");
return false;
}

uint8_t header[2];
header[0] = uri.length() + 1;
header[1] = BLE_HS_ADV_TYPE_URI;
Expand All @@ -680,11 +670,6 @@ bool NimBLEExtAdvertisement::setURI(const std::string& uri) {
* @return True if successful.
*/
bool NimBLEExtAdvertisement::setName(const std::string& name, bool isComplete) {
if (name.length() > 0xFF - 1) {
NIMBLE_LOGE(LOG_TAG, "Name too long!");
return false;
}

uint8_t header[2];
header[0] = name.length() + 1;
header[1] = isComplete ? BLE_HS_ADV_TYPE_COMP_NAME : BLE_HS_ADV_TYPE_INCOMP_NAME;
Expand Down Expand Up @@ -932,12 +917,8 @@ bool NimBLEExtAdvertisement::setServices(bool complete, uint8_t size, const std:
*/
bool NimBLEExtAdvertisement::setServiceData(const NimBLEUUID& uuid, const uint8_t* data, size_t length) {
uint8_t uuidBytes = uuid.bitSize() / 8;
if (length + uuidBytes + 2 > 0xFF) {
NIMBLE_LOGE(LOG_TAG, "Service data too long!");
return false;
}
uint8_t sDataLen = 2 + uuidBytes + length;

uint8_t sDataLen = 2 + uuidBytes + length;
if (m_payload.size() + sDataLen > MYNEWT_VAL(BLE_EXT_ADV_MAX_SIZE)) {
return false;
}
Expand Down Expand Up @@ -1109,4 +1090,20 @@ std::string NimBLEExtAdvertisement::toString() const {
return str;
} // toString

/**
* @brief Set the advertisement flags.
* @param [in] flag The flags to be set in the advertisement.
* * BLE_HS_ADV_F_DISC_LTD
* * BLE_HS_ADV_F_DISC_GEN
* * BLE_HS_ADV_F_BREDR_UNSUP - must always use with NimBLE
* A flag value of 0 will remove the flags from the advertisement.
*/
bool NimBLEExtAdvertisement::setCODData(const NimClassOfDeviceType::bluetooth_cod_t cod) {
int dataLoc = getDataLocation(BLE_HS_ADV_TYPE_CLASS_OF_DEVICE);
if (dataLoc != -1) {
removeData(BLE_HS_ADV_TYPE_CLASS_OF_DEVICE);
}
return addData((const uint8_t*)((NimClassOfDeviceType::makeATT_Payload_CodeClassOfDevice(cod)).data()),5);
} // SetCOD

#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_BROADCASTER) && MYNEWT_VAL(BLE_EXT_ADV)
3 changes: 3 additions & 0 deletions src/NimBLEExtAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define NIMBLE_CPP_EXTADVERTISING_H_

#include "syscfg/syscfg.h"
#include "NimClassOfDeviceType.h"
#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_BROADCASTER) && MYNEWT_VAL(BLE_EXT_ADV)

# if defined(CONFIG_NIMBLE_CPP_IDF)
Expand Down Expand Up @@ -46,6 +47,8 @@ class NimBLEUUID;
class NimBLEExtAdvertisement {
public:
NimBLEExtAdvertisement(uint8_t priPhy = BLE_HCI_LE_PHY_1M, uint8_t secPhy = BLE_HCI_LE_PHY_1M);

bool setCODData(const NimClassOfDeviceType::bluetooth_cod_t cod);
bool setAppearance(uint16_t appearance);
bool addServiceUUID(const NimBLEUUID& serviceUUID);
bool addServiceUUID(const char* serviceUUID);
Expand Down
13 changes: 13 additions & 0 deletions src/NimBLEHIDDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
# include "NimBLEService.h"
# include "NimBLE2904.h"

// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# define WRITE BLE_WRITE
# define READ BLE_READ
# endif

static constexpr uint16_t deviceInfoSvcUuid = 0x180a;
static constexpr uint16_t hidSvcUuid = 0x1812;
static constexpr uint16_t batterySvcUuid = 0x180f;
Expand Down Expand Up @@ -340,4 +346,11 @@ NimBLEService* NimBLEHIDDevice::getBatteryService() {
return m_batterySvc;
} // getBatteryService


// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# undef WRITE
# undef READ
# endif

#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
18 changes: 12 additions & 6 deletions src/NimBLELocalValueAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@
/**************************/

typedef enum {
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,
WRITE = BLE_GATT_CHR_F_WRITE,
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
BLE_READ = BLE_GATT_CHR_F_READ,
BLE_WRITE = BLE_GATT_CHR_F_WRITE,
# else
READ = BLE_GATT_CHR_F_READ,
WRITE = BLE_GATT_CHR_F_WRITE,
# endif
READ_ENC = BLE_GATT_CHR_F_READ_ENC,
READ_AUTHEN = BLE_GATT_CHR_F_READ_AUTHEN,
READ_AUTHOR = BLE_GATT_CHR_F_READ_AUTHOR,

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,
Expand Down Expand Up @@ -101,7 +107,7 @@ class NimBLELocalValueAttribute : public NimBLELocalAttribute, public NimBLEValu
NimBLELocalValueAttribute(const NimBLEUUID& uuid,
uint16_t handle,
uint16_t maxLen,
uint16_t initLen = MYNEWT_VAL(NIMBLE_CPP_ATT_VALUE_INIT_LENGTH))
uint16_t initLen = CONFIG_NIMBLE_CPP_ATT_VALUE_INIT_LENGTH)
: NimBLELocalAttribute(uuid, handle), NimBLEValueAttribute(maxLen, initLen) {}
/**
* @brief Destroy the NimBLELocalValueAttribute object.
Expand Down
17 changes: 16 additions & 1 deletion src/NimBLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ NimBLEService* NimBLEServer::createService(const NimBLEUUID& uuid) {
m_svcVec.push_back(pService);
serviceChanged();

// added for debugging by synapse2
NIMBLE_LOGI(LOG_TAG, "Added serice to GATT table service %s", pService->toString().c_str());
return pService;
} // createService

Expand Down Expand Up @@ -189,7 +191,7 @@ void NimBLEServer::start() {
return;
}

# if MYNEWT_VAL(NIMBLE_CPP_LOG_LEVEL) >= 4
# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4
ble_gatts_show_local();
# endif

Expand Down Expand Up @@ -742,6 +744,17 @@ void NimBLEServer::removeService(NimBLEService* service, bool deleteSvc) {
# endif
} // removeService

/**
* @brief debug to get a string of teeh services database
*
*/
std::string NimBLEServer::toString(){

std::string rStr ="Nimble Server service count " + m_svcVec.size(); + "\n";

return rStr;
}

/**
* @brief Adds a service which was either already created but removed from availability,\n
* or created and later added to services list.
Expand Down Expand Up @@ -1032,4 +1045,6 @@ void NimBLEServerCallbacks::onPhyUpdate(NimBLEConnInfo& connInfo, uint8_t txPhy,
NIMBLE_LOGD("NimBLEServerCallbacks", "onPhyUpdate: default, txPhy: %d, rxPhy: %d", txPhy, rxPhy);
} // onPhyUpdate



#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
3 changes: 3 additions & 0 deletions src/NimBLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

# include <vector>
# include <array>
#include <string>

# define NIMBLE_ATT_REMOVE_HIDE 1
# define NIMBLE_ATT_REMOVE_DELETE 2
Expand Down Expand Up @@ -84,6 +85,8 @@ class NimBLEServer {
bool updatePhy(uint16_t connHandle, uint8_t txPhysMask, uint8_t rxPhysMask, uint16_t phyOptions);
bool getPhy(uint16_t connHandle, uint8_t* txPhy, uint8_t* rxPhy);

std::string toString();

# if MYNEWT_VAL(BLE_ROLE_CENTRAL)
NimBLEClient* getClient(uint16_t connHandle);
NimBLEClient* getClient(const NimBLEConnInfo& connInfo);
Expand Down
15 changes: 15 additions & 0 deletions src/NimBLEService.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@
#include "syscfg/syscfg.h"
#if CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)


class NimBLEService;

# include "NimBLEAttribute.h"
# include "NimBLEServer.h"
# include "NimBLECharacteristic.h"


/// updated as READ and WRITE conflict with ArduinoIOT libs
#if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
#define WRITE BLE_WRITE
#define READ BLE_READ
#endif


/**
* @brief The model of a BLE service.
*
Expand Down Expand Up @@ -69,5 +78,11 @@ class NimBLEService : public NimBLELocalAttribute {
ble_gatt_svc_def m_pSvcDef[2]{};
}; // NimBLEService

// updated as READ and WRITE conflict with ArduinoIOT libs
# if defined(CONFIG_NIMBLE_USING_ARDUINO_IOT)
# undef WRITE
# undef READ
# endif

#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_PERIPHERAL)
#endif // NIMBLE_CPP_SERVICE_H_
Loading