Add ==,!= operators to NimBLEAddress, pass parameters by const reference.

This commit is contained in:
h2zero 2020-05-10 07:21:46 -06:00
parent fba7e0fd68
commit f0191eb1e6
41 changed files with 217 additions and 183 deletions

View file

@ -14,9 +14,13 @@
#include "sdkconfig.h" #include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_BT_ENABLED)
#include <algorithm>
#include "NimBLEAddress.h" #include "NimBLEAddress.h"
#include "NimBLEUtils.h" #include "NimBLEUtils.h"
#include "NimBLELog.h"
static const char* LOG_TAG = "NimBLEAddress";
/************************************************* /*************************************************
NOTE: NimBLE addresses are in INVERSE ORDER! NOTE: NimBLE addresses are in INVERSE ORDER!
@ -43,26 +47,39 @@ NimBLEAddress::NimBLEAddress(ble_addr_t address) {
* *
* @param [in] stringAddress The hex representation of the address. * @param [in] stringAddress The hex representation of the address.
*/ */
NimBLEAddress::NimBLEAddress(std::string stringAddress) { NimBLEAddress::NimBLEAddress(const std::string &stringAddress) {
if (stringAddress.length() != 17) return; if (stringAddress.length() != 17) {
memset(m_address, 0, sizeof m_address); // "00:00:00:00:00:00" represents an invalid address
NIMBLE_LOGD(LOG_TAG, "Invalid address '%s'", stringAddress.c_str());
return;
}
int data[6]; int data[6];
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]); if(sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]) != 6) {
m_address[0] = (uint8_t) data[0]; memset(m_address, 0, sizeof m_address); // "00:00:00:00:00:00" represents an invalid address
m_address[1] = (uint8_t) data[1]; NIMBLE_LOGD(LOG_TAG, "Invalid address '%s'", stringAddress.c_str());
m_address[2] = (uint8_t) data[2]; }
m_address[3] = (uint8_t) data[3]; for(size_t index = 0; index < sizeof m_address; index++) {
m_address[4] = (uint8_t) data[4]; m_address[index] = data[index];
m_address[5] = (uint8_t) data[5]; }
} // BLEAddress } // BLEAddress
/** /**
* @brief Constructor for compatibility with bluedrioid esp library. * @brief Constructor for compatibility with bluedroid esp library.
* @param [in] uint8_t[6] or esp_bd_addr_t struct containing the address. * @param [in] uint8_t[6] or esp_bd_addr_t struct containing the address.
*/ */
NimBLEAddress::NimBLEAddress(uint8_t address[6]) { NimBLEAddress::NimBLEAddress(uint8_t address[6]) {
NimBLEUtils::memrcpy(m_address, address, 6); std::reverse_copy(address, address + sizeof m_address, m_address);
} // NimBLEAddress
/**
* @brief Constructor for address using a hex value. Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16"
* @param [in] uint64_t containing the address.
*/
NimBLEAddress::NimBLEAddress(const uint64_t &address) {
memcpy(m_address, &address, sizeof m_address);
} // NimBLEAddress } // NimBLEAddress
@ -71,8 +88,8 @@ NimBLEAddress::NimBLEAddress(uint8_t address[6]) {
* @param [in] otherAddress The other address to compare against. * @param [in] otherAddress The other address to compare against.
* @return True if the addresses are equal. * @return True if the addresses are equal.
*/ */
bool NimBLEAddress::equals(NimBLEAddress otherAddress) { bool NimBLEAddress::equals(const NimBLEAddress &otherAddress) const {
return memcmp(otherAddress.getNative(), m_address, 6) == 0; return *this == otherAddress;
} // equals } // equals
@ -80,7 +97,7 @@ bool NimBLEAddress::equals(NimBLEAddress otherAddress) {
* @brief Return the native representation of the address. * @brief Return the native representation of the address.
* @return The native representation of the address. * @return The native representation of the address.
*/ */
uint8_t *NimBLEAddress::getNative() { const uint8_t *NimBLEAddress::getNative() const {
return m_address; return m_address;
} // getNative } // getNative
@ -96,13 +113,23 @@ uint8_t *NimBLEAddress::getNative() {
* *
* @return The string representation of the address. * @return The string representation of the address.
*/ */
std::string NimBLEAddress::toString() { std::string NimBLEAddress::toString() const {
auto size = 18; return std::string(*this);
char *res = (char*)malloc(size);
snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[5], m_address[4], m_address[3], m_address[2], m_address[1], m_address[0]);
std::string ret(res);
free(res);
return ret;
} // toString } // toString
bool NimBLEAddress::operator ==(const NimBLEAddress & rhs) const {
return memcmp(rhs.m_address, m_address, sizeof m_address) == 0;
}
bool NimBLEAddress::operator !=(const NimBLEAddress & rhs) const {
return !this->operator==(rhs);
}
NimBLEAddress::operator std::string() const {
char buffer[18];
sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[5], m_address[4], m_address[3], m_address[2], m_address[1], m_address[0]);
return std::string(buffer);
}
#endif #endif

View file

@ -34,10 +34,15 @@ class NimBLEAddress {
public: public:
NimBLEAddress(ble_addr_t address); NimBLEAddress(ble_addr_t address);
NimBLEAddress(uint8_t address[6]); NimBLEAddress(uint8_t address[6]);
NimBLEAddress(std::string stringAddress); NimBLEAddress(const std::string &stringAddress);
bool equals(NimBLEAddress otherAddress); NimBLEAddress(const uint64_t &address);
uint8_t* getNative(); bool equals(const NimBLEAddress &otherAddress) const;
std::string toString(); const uint8_t* getNative() const;
std::string toString() const;
bool operator ==(const NimBLEAddress & rhs) const;
bool operator !=(const NimBLEAddress & rhs) const;
operator std::string() const;
private: private:
uint8_t m_address[6]; uint8_t m_address[6];

View file

@ -141,7 +141,7 @@ NimBLEUUID NimBLEAdvertisedDevice::getServiceUUID() { //TODO Remove it eventual
* @brief Check advertised serviced for existence required UUID * @brief Check advertised serviced for existence required UUID
* @return Return true if service is advertised * @return Return true if service is advertised
*/ */
bool NimBLEAdvertisedDevice::isAdvertisingService(NimBLEUUID uuid){ bool NimBLEAdvertisedDevice::isAdvertisingService(const NimBLEUUID &uuid){
for (int i = 0; i < m_serviceUUIDs.size(); i++) { for (int i = 0; i < m_serviceUUIDs.size(); i++) {
NIMBLE_LOGI(LOG_TAG, "Comparing UUIDS: %s %s", m_serviceUUIDs[i].toString().c_str(), uuid.toString().c_str()); NIMBLE_LOGI(LOG_TAG, "Comparing UUIDS: %s %s", m_serviceUUIDs[i].toString().c_str(), uuid.toString().c_str());
if (m_serviceUUIDs[i].equals(uuid)) return true; if (m_serviceUUIDs[i].equals(uuid)) return true;

View file

@ -54,7 +54,7 @@ public:
void setAddressType(uint8_t type); void setAddressType(uint8_t type);
bool isAdvertisingService(NimBLEUUID uuid); bool isAdvertisingService(const NimBLEUUID &uuid);
bool haveAppearance(); bool haveAppearance();
bool haveManufacturerData(); bool haveManufacturerData();
bool haveName(); bool haveName();

View file

@ -58,7 +58,7 @@ NimBLEAdvertising::NimBLEAdvertising() {
* @brief Add a service uuid to exposed list of services. * @brief Add a service uuid to exposed list of services.
* @param [in] serviceUUID The UUID of the service to expose. * @param [in] serviceUUID The UUID of the service to expose.
*/ */
void NimBLEAdvertising::addServiceUUID(NimBLEUUID serviceUUID) { void NimBLEAdvertising::addServiceUUID(const NimBLEUUID &serviceUUID) {
m_serviceUUIDs.push_back(serviceUUID); m_serviceUUIDs.push_back(serviceUUID);
} // addServiceUUID } // addServiceUUID
@ -393,7 +393,7 @@ void NimBLEAdvertising::onHostReset() {
* @brief Add data to the payload to be advertised. * @brief Add data to the payload to be advertised.
* @param [in] data The data to be added to the payload. * @param [in] data The data to be added to the payload.
*/ */
void NimBLEAdvertisementData::addData(std::string data) { void NimBLEAdvertisementData::addData(const std::string &data) {
if ((m_payload.length() + data.length()) > BLE_HS_ADV_MAX_SZ) { if ((m_payload.length() + data.length()) > BLE_HS_ADV_MAX_SZ) {
return; return;
} }
@ -420,7 +420,7 @@ void NimBLEAdvertisementData::setAppearance(uint16_t appearance) {
* @brief Set the complete services. * @brief Set the complete services.
* @param [in] uuid The single service to advertise. * @param [in] uuid The single service to advertise.
*/ */
void NimBLEAdvertisementData::setCompleteServices(NimBLEUUID uuid) { void NimBLEAdvertisementData::setCompleteServices(const NimBLEUUID &uuid) {
char cdata[2]; char cdata[2];
switch (uuid.bitSize()) { switch (uuid.bitSize()) {
case 16: { case 16: {
@ -482,7 +482,7 @@ void NimBLEAdvertisementData::setFlags(uint8_t flag) {
* @brief Set manufacturer specific data. * @brief Set manufacturer specific data.
* @param [in] data Manufacturer data. * @param [in] data Manufacturer data.
*/ */
void NimBLEAdvertisementData::setManufacturerData(std::string data) { void NimBLEAdvertisementData::setManufacturerData(const std::string &data) {
NIMBLE_LOGD("NimBLEAdvertisementData", ">> setManufacturerData"); NIMBLE_LOGD("NimBLEAdvertisementData", ">> setManufacturerData");
char cdata[2]; char cdata[2];
cdata[0] = data.length() + 1; cdata[0] = data.length() + 1;
@ -496,7 +496,7 @@ void NimBLEAdvertisementData::setManufacturerData(std::string data) {
* @brief Set the name. * @brief Set the name.
* @param [in] The complete name of the device. * @param [in] The complete name of the device.
*/ */
void NimBLEAdvertisementData::setName(std::string name) { void NimBLEAdvertisementData::setName(const std::string &name) {
NIMBLE_LOGD("NimBLEAdvertisementData", ">> setName: %s", name.c_str()); NIMBLE_LOGD("NimBLEAdvertisementData", ">> setName: %s", name.c_str());
char cdata[2]; char cdata[2];
cdata[0] = name.length() + 1; cdata[0] = name.length() + 1;
@ -510,7 +510,7 @@ void NimBLEAdvertisementData::setName(std::string name) {
* @brief Set the partial services. * @brief Set the partial services.
* @param [in] uuid The single service to advertise. * @param [in] uuid The single service to advertise.
*/ */
void NimBLEAdvertisementData::setPartialServices(NimBLEUUID uuid) { void NimBLEAdvertisementData::setPartialServices(const NimBLEUUID &uuid) {
char cdata[2]; char cdata[2];
switch (uuid.bitSize()) { switch (uuid.bitSize()) {
case 16: { case 16: {
@ -548,7 +548,7 @@ void NimBLEAdvertisementData::setPartialServices(NimBLEUUID uuid) {
* @param [in] uuid The UUID to set with the service data. Size of UUID will be used. * @param [in] uuid The UUID to set with the service data. Size of UUID will be used.
* @param [in] data The data to be associated with the service data advert. * @param [in] data The data to be associated with the service data advert.
*/ */
void NimBLEAdvertisementData::setServiceData(NimBLEUUID uuid, std::string data) { void NimBLEAdvertisementData::setServiceData(const NimBLEUUID &uuid, const std::string &data) {
char cdata[2]; char cdata[2];
switch (uuid.bitSize()) { switch (uuid.bitSize()) {
case 16: { case 16: {
@ -585,7 +585,7 @@ void NimBLEAdvertisementData::setServiceData(NimBLEUUID uuid, std::string data)
* @brief Set the short name. * @brief Set the short name.
* @param [in] The short name of the device. * @param [in] The short name of the device.
*/ */
void NimBLEAdvertisementData::setShortName(std::string name) { void NimBLEAdvertisementData::setShortName(const std::string &name) {
NIMBLE_LOGD("NimBLEAdvertisementData", ">> setShortName: %s", name.c_str()); NIMBLE_LOGD("NimBLEAdvertisementData", ">> setShortName: %s", name.c_str());
char cdata[2]; char cdata[2];
cdata[0] = name.length() + 1; cdata[0] = name.length() + 1;

View file

@ -47,14 +47,14 @@ class NimBLEAdvertisementData {
// //
public: public:
void setAppearance(uint16_t appearance); void setAppearance(uint16_t appearance);
void setCompleteServices(NimBLEUUID uuid); void setCompleteServices(const NimBLEUUID &uuid);
void setFlags(uint8_t); void setFlags(uint8_t);
void setManufacturerData(std::string data); void setManufacturerData(const std::string &data);
void setName(std::string name); void setName(const std::string &name);
void setPartialServices(NimBLEUUID uuid); void setPartialServices(const NimBLEUUID &uuid);
void setServiceData(NimBLEUUID uuid, std::string data); void setServiceData(const NimBLEUUID &uuid, const std::string &data);
void setShortName(std::string name); void setShortName(const std::string &name);
void addData(std::string data); // Add data to the payload. void addData(const std::string &data); // Add data to the payload.
std::string getPayload(); // Retrieve the current advert payload. std::string getPayload(); // Retrieve the current advert payload.
private: private:
@ -71,7 +71,7 @@ private:
class NimBLEAdvertising { class NimBLEAdvertising {
public: public:
NimBLEAdvertising(); NimBLEAdvertising();
void addServiceUUID(NimBLEUUID serviceUUID); void addServiceUUID(const NimBLEUUID &serviceUUID);
void addServiceUUID(const char* serviceUUID); void addServiceUUID(const char* serviceUUID);
void start(); void start();
void stop(); void stop();

View file

@ -58,7 +58,7 @@ int8_t NimBLEBeacon::getSignalPower() {
/** /**
* Set the raw data for the beacon record. * Set the raw data for the beacon record.
*/ */
void NimBLEBeacon::setData(std::string data) { void NimBLEBeacon::setData(const std::string &data) {
if (data.length() != sizeof(m_beaconData)) { if (data.length() != sizeof(m_beaconData)) {
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d", NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d",
data.length(), sizeof(m_beaconData)); data.length(), sizeof(m_beaconData));
@ -79,9 +79,10 @@ void NimBLEBeacon::setMinor(uint16_t minor) {
m_beaconData.minor = ENDIAN_CHANGE_U16(minor); m_beaconData.minor = ENDIAN_CHANGE_U16(minor);
} // setMinior } // setMinior
void NimBLEBeacon::setProximityUUID(NimBLEUUID uuid) { void NimBLEBeacon::setProximityUUID(const NimBLEUUID &uuid) {
uuid = uuid.to128(); NimBLEUUID temp_uuid = uuid;
memcpy(m_beaconData.proximityUUID, uuid.getNative()->u128.value, 16); temp_uuid.to128();
memcpy(m_beaconData.proximityUUID, temp_uuid.getNative()->u128.value, 16);
} // setProximityUUID } // setProximityUUID
void NimBLEBeacon::setSignalPower(int8_t signalPower) { void NimBLEBeacon::setSignalPower(int8_t signalPower) {

View file

@ -39,11 +39,11 @@ public:
uint16_t getManufacturerId(); uint16_t getManufacturerId();
NimBLEUUID getProximityUUID(); NimBLEUUID getProximityUUID();
int8_t getSignalPower(); int8_t getSignalPower();
void setData(std::string data); void setData(const std::string &data);
void setMajor(uint16_t major); void setMajor(uint16_t major);
void setMinor(uint16_t minor); void setMinor(uint16_t minor);
void setManufacturerId(uint16_t manufacturerId); void setManufacturerId(uint16_t manufacturerId);
void setProximityUUID(NimBLEUUID uuid); void setProximityUUID(const NimBLEUUID &uuid);
void setSignalPower(int8_t signalPower); void setSignalPower(int8_t signalPower);
}; // NimBLEBeacon }; // NimBLEBeacon

View file

@ -41,7 +41,7 @@ NimBLECharacteristic::NimBLECharacteristic(const char* uuid, uint16_t properties
* @param [in] uuid - UUID for the characteristic. * @param [in] uuid - UUID for the characteristic.
* @param [in] properties - Properties for the characteristic. * @param [in] properties - Properties for the characteristic.
*/ */
NimBLECharacteristic::NimBLECharacteristic(NimBLEUUID uuid, uint16_t properties, NimBLEService* pService) { NimBLECharacteristic::NimBLECharacteristic(const NimBLEUUID &uuid, uint16_t properties, NimBLEService* pService) {
m_uuid = uuid; m_uuid = uuid;
m_handle = NULL_HANDLE; m_handle = NULL_HANDLE;
m_properties = properties; m_properties = properties;
@ -99,7 +99,7 @@ NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const char* uuid, uint3
* @param [in] properties - The properties of the descriptor. * @param [in] properties - The properties of the descriptor.
* @return The new BLE descriptor. * @return The new BLE descriptor.
*/ */
NimBLEDescriptor* NimBLECharacteristic::createDescriptor(NimBLEUUID uuid, uint32_t properties, uint16_t max_len) { NimBLEDescriptor* NimBLECharacteristic::createDescriptor(const NimBLEUUID &uuid, uint32_t properties, uint16_t max_len) {
NimBLEDescriptor* pDescriptor = nullptr; NimBLEDescriptor* pDescriptor = nullptr;
if(uuid.equals(NimBLEUUID((uint16_t)0x2902))) { if(uuid.equals(NimBLEUUID((uint16_t)0x2902))) {
if(!(m_properties & BLE_GATT_CHR_F_NOTIFY) && !(m_properties & BLE_GATT_CHR_F_INDICATE)) { if(!(m_properties & BLE_GATT_CHR_F_NOTIFY) && !(m_properties & BLE_GATT_CHR_F_INDICATE)) {
@ -139,7 +139,7 @@ NimBLEDescriptor* NimBLECharacteristic::getDescriptorByUUID(const char* descript
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve. * @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
* @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned. * @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned.
*/ */
NimBLEDescriptor* NimBLECharacteristic::getDescriptorByUUID(NimBLEUUID descriptorUUID) { NimBLEDescriptor* NimBLECharacteristic::getDescriptorByUUID(const NimBLEUUID &descriptorUUID) {
return m_descriptorMap.getByUUID(descriptorUUID); return m_descriptorMap.getByUUID(descriptorUUID);
} // getDescriptorByUUID } // getDescriptorByUUID
@ -518,7 +518,7 @@ void NimBLECharacteristic::setWriteProperty(bool value) {
* @param [in] data The data to set for the characteristic. * @param [in] data The data to set for the characteristic.
* @param [in] length The length of the data in bytes. * @param [in] length The length of the data in bytes.
*/ */
void NimBLECharacteristic::setValue(uint8_t* data, size_t length) { void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) {
char* pHex = NimBLEUtils::buildHexData(nullptr, data, length); char* pHex = NimBLEUtils::buildHexData(nullptr, data, length);
NIMBLE_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str());
free(pHex); free(pHex);
@ -546,7 +546,7 @@ void NimBLECharacteristic::setValue(uint8_t* data, size_t length) {
* @param [in] Set the value of the characteristic. * @param [in] Set the value of the characteristic.
* @return N/A. * @return N/A.
*/ */
void NimBLECharacteristic::setValue(std::string value) { void NimBLECharacteristic::setValue(const std::string &value) {
setValue((uint8_t*)(value.data()), value.length()); setValue((uint8_t*)(value.data()), value.length());
} // setValue } // setValue

View file

@ -58,10 +58,10 @@ class NimBLECharacteristicCallbacks;
class NimBLEDescriptorMap { class NimBLEDescriptorMap {
public: public:
void setByUUID(const char* uuid, NimBLEDescriptor* pDescriptor); void setByUUID(const char* uuid, NimBLEDescriptor* pDescriptor);
void setByUUID(NimBLEUUID uuid, NimBLEDescriptor* pDescriptor); void setByUUID(const NimBLEUUID &uuid, NimBLEDescriptor* pDescriptor);
// void setByHandle(uint16_t handle, NimBLEDescriptor* pDescriptor); // void setByHandle(uint16_t handle, NimBLEDescriptor* pDescriptor);
NimBLEDescriptor* getByUUID(const char* uuid); NimBLEDescriptor* getByUUID(const char* uuid);
NimBLEDescriptor* getByUUID(NimBLEUUID uuid); NimBLEDescriptor* getByUUID(const NimBLEUUID &uuid);
// NimBLEDescriptor* getByHandle(uint16_t handle); // NimBLEDescriptor* getByHandle(uint16_t handle);
std::string toString(); std::string toString();
NimBLEDescriptor* getFirst(); NimBLEDescriptor* getFirst();
@ -87,13 +87,13 @@ public:
uint32_t properties = NIMBLE_PROPERTY::READ | uint32_t properties = NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE, NIMBLE_PROPERTY::WRITE,
uint16_t max_len = 100); uint16_t max_len = 100);
NimBLEDescriptor* createDescriptor(NimBLEUUID uuid, NimBLEDescriptor* createDescriptor(const NimBLEUUID &uuid,
uint32_t properties = NIMBLE_PROPERTY::READ | uint32_t properties = NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE, NIMBLE_PROPERTY::WRITE,
uint16_t max_len = 100); uint16_t max_len = 100);
NimBLEDescriptor* getDescriptorByUUID(const char* descriptorUUID); NimBLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
NimBLEDescriptor* getDescriptorByUUID(NimBLEUUID descriptorUUID); NimBLEDescriptor* getDescriptorByUUID(const NimBLEUUID &descriptorUUID);
NimBLEUUID getUUID(); NimBLEUUID getUUID();
std::string getValue(); std::string getValue();
uint8_t* getData(); uint8_t* getData();
@ -110,8 +110,8 @@ public:
void setWriteProperty(bool value); void setWriteProperty(bool value);
void setWriteNoResponseProperty(bool value); void setWriteNoResponseProperty(bool value);
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
void setValue(uint8_t* data, size_t size); void setValue(const uint8_t* data, size_t size);
void setValue(std::string value); void setValue(const std::string &value);
void setValue(uint16_t& data16); void setValue(uint16_t& data16);
void setValue(uint32_t& data32); void setValue(uint32_t& data32);
void setValue(int& data32); void setValue(int& data32);
@ -141,7 +141,7 @@ private:
NimBLECharacteristic(const char* uuid, uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE, NimBLECharacteristic(const char* uuid, uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
NimBLEService* pService = nullptr); NimBLEService* pService = nullptr);
NimBLECharacteristic(NimBLEUUID uuid, uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE, NimBLECharacteristic(const NimBLEUUID &uuid, uint16_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
NimBLEService* pService = nullptr); NimBLEService* pService = nullptr);
virtual ~NimBLECharacteristic(); virtual ~NimBLECharacteristic();

View file

@ -41,7 +41,7 @@ NimBLECharacteristic* NimBLECharacteristicMap::getByUUID(const char* uuid) {
* @param [in] UUID The UUID to look up the characteristic. * @param [in] UUID The UUID to look up the characteristic.
* @return The characteristic. * @return The characteristic.
*/ */
NimBLECharacteristic* NimBLECharacteristicMap::getByUUID(NimBLEUUID uuid) { NimBLECharacteristic* NimBLECharacteristicMap::getByUUID(const NimBLEUUID &uuid) {
for (auto &myPair : m_uuidMap) { for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) { if (myPair.first->getUUID().equals(uuid)) {
return myPair.first; return myPair.first;
@ -100,7 +100,7 @@ void NimBLECharacteristicMap::setByHandle(uint16_t handle, NimBLECharacteristic*
* @param [in] characteristic The characteristic to cache. * @param [in] characteristic The characteristic to cache.
* @return N/A. * @return N/A.
*/ */
void NimBLECharacteristicMap::setByUUID(NimBLECharacteristic* pCharacteristic, NimBLEUUID uuid) { void NimBLECharacteristicMap::setByUUID(NimBLECharacteristic* pCharacteristic, const NimBLEUUID &uuid) {
m_uuidMap.insert(std::pair<NimBLECharacteristic*, std::string>(pCharacteristic, uuid.toString())); m_uuidMap.insert(std::pair<NimBLECharacteristic*, std::string>(pCharacteristic, uuid.toString()));
} // setByUUID } // setByUUID

View file

@ -120,7 +120,7 @@ bool NimBLEClient::connect(NimBLEAdvertisedDevice* device, bool refreshServices)
* @param [in] address The address of the partner. * @param [in] address The address of the partner.
* @return True on success. * @return True on success.
*/ */
bool NimBLEClient::connect(NimBLEAddress address, uint8_t type, bool refreshServices) { bool NimBLEClient::connect(const NimBLEAddress &address, uint8_t type, bool refreshServices) {
NIMBLE_LOGD(LOG_TAG, ">> connect(%s)", address.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> connect(%s)", address.toString().c_str());
if(!NimBLEDevice::m_synced) { if(!NimBLEDevice::m_synced) {
@ -359,7 +359,7 @@ NimBLERemoteService* NimBLEClient::getService(const char* uuid) {
* @param [in] uuid The UUID of the service being sought. * @param [in] uuid The UUID of the service being sought.
* @return A reference to the Service or nullptr if don't know about it. * @return A reference to the Service or nullptr if don't know about it.
*/ */
NimBLERemoteService* NimBLEClient::getService(NimBLEUUID uuid) { NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> getService: uuid: %s", uuid.toString().c_str());
if (!m_haveServices) { if (!m_haveServices) {
@ -494,7 +494,7 @@ int NimBLEClient::serviceDiscoveredCB(
* @param [in] characteristicUUID The characteristic whose value we wish to read. * @param [in] characteristicUUID The characteristic whose value we wish to read.
* @returns characteristic value or an empty string if not found * @returns characteristic value or an empty string if not found
*/ */
std::string NimBLEClient::getValue(NimBLEUUID serviceUUID, NimBLEUUID characteristicUUID) { std::string NimBLEClient::getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID) {
NIMBLE_LOGD(LOG_TAG, ">> getValue: serviceUUID: %s, characteristicUUID: %s", serviceUUID.toString().c_str(), characteristicUUID.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> getValue: serviceUUID: %s, characteristicUUID: %s", serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
std::string ret = ""; std::string ret = "";
@ -518,7 +518,7 @@ std::string NimBLEClient::getValue(NimBLEUUID serviceUUID, NimBLEUUID characteri
* @param [in] characteristicUUID The characteristic whose value we wish to write. * @param [in] characteristicUUID The characteristic whose value we wish to write.
* @returns true if successful otherwise false * @returns true if successful otherwise false
*/ */
bool NimBLEClient::setValue(NimBLEUUID serviceUUID, NimBLEUUID characteristicUUID, std::string value) { bool NimBLEClient::setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value) {
NIMBLE_LOGD(LOG_TAG, ">> setValue: serviceUUID: %s, characteristicUUID: %s", serviceUUID.toString().c_str(), characteristicUUID.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> setValue: serviceUUID: %s, characteristicUUID: %s", serviceUUID.toString().c_str(), characteristicUUID.toString().c_str());
bool ret = false; bool ret = false;

View file

@ -34,15 +34,15 @@ class NimBLEAdvertisedDevice;
class NimBLEClient { class NimBLEClient {
public: public:
bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true); bool connect(NimBLEAdvertisedDevice* device, bool refreshServices = true);
bool connect(NimBLEAddress address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); // Connect to the remote BLE Server bool connect(const NimBLEAddress &address, uint8_t type = BLE_ADDR_PUBLIC, bool refreshServices = true); // Connect to the remote BLE Server
int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); // Disconnect from the remote BLE Server int disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); // Disconnect from the remote BLE Server
NimBLEAddress getPeerAddress(); // Get the address of the remote BLE Server NimBLEAddress getPeerAddress(); // Get the address of the remote BLE Server
int getRssi(); // Get the RSSI of the remote BLE Server int getRssi(); // Get the RSSI of the remote BLE Server
std::map<std::string, NimBLERemoteService*>* getServices(); // Get a map of the services offered by the remote BLE Server std::map<std::string, NimBLERemoteService*>* getServices(); // Get a map of the services offered by the remote BLE Server
NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server. NimBLERemoteService* getService(const char* uuid); // Get a reference to a specified service offered by the remote BLE server.
NimBLERemoteService* getService(NimBLEUUID uuid); // Get a reference to a specified service offered by the remote BLE server. NimBLERemoteService* getService(const NimBLEUUID &uuid); // Get a reference to a specified service offered by the remote BLE server.
std::string getValue(NimBLEUUID serviceUUID, NimBLEUUID characteristicUUID); // Get the value of a given characteristic at a given service. std::string getValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID); // Get the value of a given characteristic at a given service.
bool setValue(NimBLEUUID serviceUUID, NimBLEUUID characteristicUUID, std::string value); // Set the value of a given characteristic at a given service. bool setValue(const NimBLEUUID &serviceUUID, const NimBLEUUID &characteristicUUID, const std::string &value); // Set the value of a given characteristic at a given service.
bool isConnected(); // Return true if we are connected. bool isConnected(); // Return true if we are connected.
void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks = true); void setClientCallbacks(NimBLEClientCallbacks *pClientCallbacks, bool deleteCallbacks = true);
std::string toString(); // Return a string representation of this client. std::string toString(); // Return a string representation of this client.

View file

@ -187,7 +187,7 @@ void NimBLEDescriptor::setHandle(uint16_t handle) {
* @param [in] data The data to set for the descriptor. * @param [in] data The data to set for the descriptor.
* @param [in] length The length of the data in bytes. * @param [in] length The length of the data in bytes.
*/ */
void NimBLEDescriptor::setValue(uint8_t* data, size_t length) { void NimBLEDescriptor::setValue(const uint8_t* data, size_t length) {
if (length > BLE_ATT_ATTR_MAX_LEN) { if (length > BLE_ATT_ATTR_MAX_LEN) {
NIMBLE_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, BLE_ATT_ATTR_MAX_LEN); NIMBLE_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, BLE_ATT_ATTR_MAX_LEN);
return; return;
@ -201,7 +201,7 @@ void NimBLEDescriptor::setValue(uint8_t* data, size_t length) {
* @brief Set the value of the descriptor. * @brief Set the value of the descriptor.
* @param [in] value The value of the descriptor in string form. * @param [in] value The value of the descriptor in string form.
*/ */
void NimBLEDescriptor::setValue(std::string value) { void NimBLEDescriptor::setValue(const std::string &value) {
setValue((uint8_t*) value.data(), value.length()); setValue((uint8_t*) value.data(), value.length());
} // setValue } // setValue

View file

@ -50,8 +50,8 @@ public:
uint8_t* getValue(); // Get a pointer to the value of the descriptor. uint8_t* getValue(); // Get a pointer to the value of the descriptor.
// void setAccessPermissions(uint8_t perm); // Set the permissions of the descriptor. // void setAccessPermissions(uint8_t perm); // Set the permissions of the descriptor.
void setCallbacks(NimBLEDescriptorCallbacks* pCallbacks); // Set callbacks to be invoked for the descriptor. void setCallbacks(NimBLEDescriptorCallbacks* pCallbacks); // Set callbacks to be invoked for the descriptor.
void setValue(uint8_t* data, size_t size); // Set the value of the descriptor as a pointer to data. void setValue(const uint8_t* data, size_t size); // Set the value of the descriptor as a pointer to data.
void setValue(std::string value); // Set the value of the descriptor as a data buffer. void setValue(const std::string &value); // Set the value of the descriptor as a data buffer.
std::string toString(); // Convert the descriptor to a string representation. std::string toString(); // Convert the descriptor to a string representation.

View file

@ -32,7 +32,7 @@ NimBLEDescriptor* NimBLEDescriptorMap::getByUUID(const char* uuid) {
* @param [in] UUID The UUID to look up the descriptor. * @param [in] UUID The UUID to look up the descriptor.
* @return The descriptor. If not present, then nullptr is returned. * @return The descriptor. If not present, then nullptr is returned.
*/ */
NimBLEDescriptor* NimBLEDescriptorMap::getByUUID(NimBLEUUID uuid) { NimBLEDescriptor* NimBLEDescriptorMap::getByUUID(const NimBLEUUID &uuid) {
for (auto &myPair : m_uuidMap) { for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) { if (myPair.first->getUUID().equals(uuid)) {
return myPair.first; return myPair.first;
@ -71,7 +71,7 @@ void NimBLEDescriptorMap::setByUUID(const char* uuid, NimBLEDescriptor* pDescrip
* @param [in] characteristic The descriptor to cache. * @param [in] characteristic The descriptor to cache.
* @return N/A. * @return N/A.
*/ */
void NimBLEDescriptorMap::setByUUID(NimBLEUUID uuid, NimBLEDescriptor* pDescriptor) { void NimBLEDescriptorMap::setByUUID(const NimBLEUUID &uuid, NimBLEDescriptor* pDescriptor) {
m_uuidMap.insert(std::pair<NimBLEDescriptor*, std::string>(pDescriptor, uuid.toString())); m_uuidMap.insert(std::pair<NimBLEDescriptor*, std::string>(pDescriptor, uuid.toString()));
} // setByUUID } // setByUUID

View file

@ -202,7 +202,7 @@ void NimBLEDevice::stopAdvertising() {
* @param [in] a NimBLEAddress of the peer to search for. * @param [in] a NimBLEAddress of the peer to search for.
* @return A reference pointer to the client with the peer address. * @return A reference pointer to the client with the peer address.
*/ */
/* STATIC */NimBLEClient* NimBLEDevice::getClientByPeerAddress(NimBLEAddress peer_addr) { /* STATIC */NimBLEClient* NimBLEDevice::getClientByPeerAddress(const NimBLEAddress &peer_addr) {
for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) { for(auto it = m_cList.cbegin(); it != m_cList.cend(); ++it) {
if((*it)->getPeerAddress().equals(peer_addr)) { if((*it)->getPeerAddress().equals(peer_addr)) {
return (*it); return (*it);
@ -410,7 +410,7 @@ void NimBLEDevice::stopAdvertising() {
* @brief Initialize the %BLE environment. * @brief Initialize the %BLE environment.
* @param deviceName The device name of the device. * @param deviceName The device name of the device.
*/ */
/* STATIC */ void NimBLEDevice::init(std::string deviceName) { /* STATIC */ void NimBLEDevice::init(const std::string &deviceName) {
if(!initialized){ if(!initialized){
int rc=0; int rc=0;
esp_err_t errRc = ESP_OK; esp_err_t errRc = ESP_OK;
@ -612,7 +612,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) {
* @brief Check if the device address is on our ignore list. * @brief Check if the device address is on our ignore list.
* @return True if ignoring. * @return True if ignoring.
*/ */
/*STATIC*/ bool NimBLEDevice::isIgnored(NimBLEAddress address) { /*STATIC*/ bool NimBLEDevice::isIgnored(const NimBLEAddress &address) {
for(auto &it : m_ignoreList) { for(auto &it : m_ignoreList) {
if(it.equals(address)){ if(it.equals(address)){
return true; return true;
@ -627,7 +627,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) {
* @brief Add a device to the ignore list. * @brief Add a device to the ignore list.
* @param Address of the device we want to ignore. * @param Address of the device we want to ignore.
*/ */
/*STATIC*/ void NimBLEDevice::addIgnored(NimBLEAddress address) { /*STATIC*/ void NimBLEDevice::addIgnored(const NimBLEAddress &address) {
m_ignoreList.push_back(address); m_ignoreList.push_back(address);
} }
@ -636,7 +636,7 @@ void NimBLEDevice::setSecurityCallbacks(NimBLESecurityCallbacks* callbacks) {
* @brief Remove a device from the ignore list. * @brief Remove a device from the ignore list.
* @param Address of the device we want to remove from the list. * @param Address of the device we want to remove from the list.
*/ */
/*STATIC*/void NimBLEDevice::removeIgnored(NimBLEAddress address) { /*STATIC*/void NimBLEDevice::removeIgnored(const NimBLEAddress &address) {
for(auto it = m_ignoreList.begin(); it != m_ignoreList.end(); ++it) { for(auto it = m_ignoreList.begin(); it != m_ignoreList.end(); ++it) {
if((*it).equals(address)){ if((*it).equals(address)){
m_ignoreList.erase(it); m_ignoreList.erase(it);

View file

@ -76,7 +76,7 @@ extern "C" void ble_store_config_init(void);
class NimBLEDevice { class NimBLEDevice {
public: public:
static void init(std::string deviceName); // Initialize the local BLE environment. static void init(const std::string &deviceName); // Initialize the local BLE environment.
static void deinit(); static void deinit();
static bool getInitialized(); static bool getInitialized();
static NimBLEAddress getAddress(); static NimBLEAddress getAddress();
@ -98,14 +98,14 @@ public:
static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks); static void setSecurityCallbacks(NimBLESecurityCallbacks* pCallbacks);
static int setMTU(uint16_t mtu); static int setMTU(uint16_t mtu);
static uint16_t getMTU(); static uint16_t getMTU();
static bool isIgnored(NimBLEAddress address); static bool isIgnored(const NimBLEAddress &address);
static void addIgnored(NimBLEAddress address); static void addIgnored(const NimBLEAddress &address);
static void removeIgnored(NimBLEAddress address); static void removeIgnored(const NimBLEAddress &address);
static NimBLEAdvertising* getAdvertising(); static NimBLEAdvertising* getAdvertising();
static void startAdvertising(); static void startAdvertising();
static void stopAdvertising(); static void stopAdvertising();
static NimBLEClient* getClientByID(uint16_t conn_id); static NimBLEClient* getClientByID(uint16_t conn_id);
static NimBLEClient* getClientByPeerAddress(NimBLEAddress peer_addr); static NimBLEClient* getClientByPeerAddress(const NimBLEAddress &peer_addr);
static NimBLEClient* getDisconnectedClient(); static NimBLEClient* getDisconnectedClient();
static size_t getClientListSize(); static size_t getClientListSize();
static std::list<NimBLEClient*>* getClientList(); static std::list<NimBLEClient*>* getClientList();

View file

@ -116,7 +116,7 @@ std::string NimBLEEddystoneTLM::toString() {
/** /**
* Set the raw data for the beacon record. * Set the raw data for the beacon record.
*/ */
void NimBLEEddystoneTLM::setData(std::string data) { void NimBLEEddystoneTLM::setData(const std::string &data) {
if (data.length() != sizeof(m_eddystoneData)) { if (data.length() != sizeof(m_eddystoneData)) {
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d", NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d",
data.length(), sizeof(m_eddystoneData)); data.length(), sizeof(m_eddystoneData));
@ -125,7 +125,7 @@ void NimBLEEddystoneTLM::setData(std::string data) {
memcpy(&m_eddystoneData, data.data(), data.length()); memcpy(&m_eddystoneData, data.data(), data.length());
} // setData } // setData
void NimBLEEddystoneTLM::setUUID(NimBLEUUID l_uuid) { void NimBLEEddystoneTLM::setUUID(const NimBLEUUID &l_uuid) {
beaconUUID = l_uuid.getNative()->u16.value; beaconUUID = l_uuid.getNative()->u16.value;
} // setUUID } // setUUID

View file

@ -36,8 +36,8 @@ public:
uint32_t getCount(); uint32_t getCount();
uint32_t getTime(); uint32_t getTime();
std::string toString(); std::string toString();
void setData(std::string data); void setData(const std::string &data);
void setUUID(NimBLEUUID l_uuid); void setUUID(const NimBLEUUID &l_uuid);
void setVersion(uint8_t version); void setVersion(uint8_t version);
void setVolt(uint16_t volt); void setVolt(uint16_t volt);
void setTemp(float temp); void setTemp(float temp);

View file

@ -125,7 +125,7 @@ std::string NimBLEEddystoneURL::getDecodedURL() {
/** /**
* Set the raw data for the beacon record. * Set the raw data for the beacon record.
*/ */
void NimBLEEddystoneURL::setData(std::string data) { void NimBLEEddystoneURL::setData(const std::string &data) {
if (data.length() > sizeof(m_eddystoneData)) { if (data.length() > sizeof(m_eddystoneData)) {
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and max expected %d", NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and max expected %d",
data.length(), sizeof(m_eddystoneData)); data.length(), sizeof(m_eddystoneData));
@ -136,7 +136,7 @@ void NimBLEEddystoneURL::setData(std::string data) {
lengthURL = data.length() - (sizeof(m_eddystoneData) - sizeof(m_eddystoneData.url)); lengthURL = data.length() - (sizeof(m_eddystoneData) - sizeof(m_eddystoneData.url));
} // setData } // setData
void NimBLEEddystoneURL::setUUID(NimBLEUUID l_uuid) { void NimBLEEddystoneURL::setUUID(const NimBLEUUID &l_uuid) {
beaconUUID = l_uuid.getNative()->u16.value; beaconUUID = l_uuid.getNative()->u16.value;
} // setUUID } // setUUID
@ -144,7 +144,7 @@ void NimBLEEddystoneURL::setPower(int8_t advertisedTxPower) {
m_eddystoneData.advertisedTxPower = advertisedTxPower; m_eddystoneData.advertisedTxPower = advertisedTxPower;
} // setPower } // setPower
void NimBLEEddystoneURL::setURL(std::string url) { void NimBLEEddystoneURL::setURL(const std::string &url) {
if (url.length() > sizeof(m_eddystoneData.url)) { if (url.length() > sizeof(m_eddystoneData.url)) {
NIMBLE_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d", NIMBLE_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d",
url.length(), sizeof(m_eddystoneData.url)); url.length(), sizeof(m_eddystoneData.url));

View file

@ -33,10 +33,10 @@ public:
int8_t getPower(); int8_t getPower();
std::string getURL(); std::string getURL();
std::string getDecodedURL(); std::string getDecodedURL();
void setData(std::string data); void setData(const std::string &data);
void setUUID(NimBLEUUID l_uuid); void setUUID(const NimBLEUUID &l_uuid);
void setPower(int8_t advertisedTxPower); void setPower(int8_t advertisedTxPower);
void setURL(std::string url); void setURL(const std::string &url);
private: private:
uint16_t beaconUUID; uint16_t beaconUUID;

View file

@ -243,7 +243,7 @@ uint16_t NimBLERemoteCharacteristic::getDefHandle() {
* @param [in] uuid The UUID of the descriptor to find. * @param [in] uuid The UUID of the descriptor to find.
* @return The Remote descriptor (if present) or null if not present. * @return The Remote descriptor (if present) or null if not present.
*/ */
NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(NimBLEUUID uuid) { NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUUID &uuid) {
NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
std::string v = uuid.toString(); std::string v = uuid.toString();
for (auto &myPair : m_descriptorMap) { for (auto &myPair : m_descriptorMap) {
@ -489,7 +489,7 @@ std::string NimBLERemoteCharacteristic::toString() {
* @param [in] response Do we expect a response? * @param [in] response Do we expect a response?
* @return false if not connected or cant perform write for some reason. * @return false if not connected or cant perform write for some reason.
*/ */
bool NimBLERemoteCharacteristic::writeValue(std::string newValue, bool response) { bool NimBLERemoteCharacteristic::writeValue(const std::string &newValue, bool response) {
return writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response); return writeValue((uint8_t*)newValue.c_str(), strlen(newValue.c_str()), response);
} // writeValue } // writeValue
@ -514,7 +514,7 @@ bool NimBLERemoteCharacteristic::writeValue(uint8_t newValue, bool response) {
* @param [in] response Whether we require a response from the write. * @param [in] response Whether we require a response from the write.
* @return false if not connected or cant perform write for some reason. * @return false if not connected or cant perform write for some reason.
*/ */
bool NimBLERemoteCharacteristic::writeValue(uint8_t* data, size_t length, bool response) { bool NimBLERemoteCharacteristic::writeValue(const uint8_t* data, size_t length, bool response) {
NIMBLE_LOGD(LOG_TAG, ">> writeValue(), length: %d", length); NIMBLE_LOGD(LOG_TAG, ">> writeValue(), length: %d", length);

View file

@ -45,7 +45,7 @@ public:
bool canRead(); bool canRead();
bool canWrite(); bool canWrite();
bool canWriteNoResponse(); bool canWriteNoResponse();
NimBLERemoteDescriptor* getDescriptor(NimBLEUUID uuid); NimBLERemoteDescriptor* getDescriptor(const NimBLEUUID &uuid);
std::map<std::string, NimBLERemoteDescriptor*>* getDescriptors(); std::map<std::string, NimBLERemoteDescriptor*>* getDescriptors();
uint16_t getHandle(); uint16_t getHandle();
uint16_t getDefHandle(); uint16_t getDefHandle();
@ -55,8 +55,8 @@ public:
uint16_t readUInt16(); uint16_t readUInt16();
uint32_t readUInt32(); uint32_t readUInt32();
bool registerForNotify(notify_callback _callback, bool notifications = true, bool response = true); bool registerForNotify(notify_callback _callback, bool notifications = true, bool response = true);
bool writeValue(uint8_t* data, size_t length, bool response = false); bool writeValue(const uint8_t* data, size_t length, bool response = false);
bool writeValue(std::string newValue, bool response = false); bool writeValue(const std::string &newValue, bool response = false);
bool writeValue(uint8_t newValue, bool response = false); bool writeValue(uint8_t newValue, bool response = false);
std::string toString(); std::string toString();
uint8_t* readRawData(); uint8_t* readRawData();

View file

@ -236,7 +236,7 @@ int NimBLERemoteDescriptor::onWriteCB(uint16_t conn_handle,
* @param [in] length The length of the data to send. * @param [in] length The length of the data to send.
* @param [in] response True if we expect a response. * @param [in] response True if we expect a response.
*/ */
bool NimBLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool response) { bool NimBLERemoteDescriptor::writeValue(const uint8_t* data, size_t length, bool response) {
NIMBLE_LOGD(LOG_TAG, ">> Descriptor writeValue: %s", toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> Descriptor writeValue: %s", toString().c_str());
@ -317,7 +317,7 @@ bool NimBLERemoteDescriptor::writeValue(uint8_t* data, size_t length, bool respo
* @param [in] newValue The data to send to the remote descriptor. * @param [in] newValue The data to send to the remote descriptor.
* @param [in] response True if we expect a response. * @param [in] response True if we expect a response.
*/ */
bool NimBLERemoteDescriptor::writeValue(std::string newValue, bool response) { bool NimBLERemoteDescriptor::writeValue(const std::string &newValue, bool response) {
return writeValue((uint8_t*) newValue.data(), newValue.length(), response); return writeValue((uint8_t*) newValue.data(), newValue.length(), response);
} // writeValue } // writeValue

View file

@ -33,8 +33,8 @@ public:
uint16_t readUInt16(void); uint16_t readUInt16(void);
uint32_t readUInt32(void); uint32_t readUInt32(void);
std::string toString(void); std::string toString(void);
bool writeValue(uint8_t* data, size_t length, bool response = false); bool writeValue(const uint8_t* data, size_t length, bool response = false);
bool writeValue(std::string newValue, bool response = false); bool writeValue(const std::string &newValue, bool response = false);
bool writeValue(uint8_t newValue, bool response = false); bool writeValue(uint8_t newValue, bool response = false);

View file

@ -76,7 +76,7 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* u
* @param [in] uuid Characteristic uuid. * @param [in] uuid Characteristic uuid.
* @return Reference to the characteristic object, or nullptr if not found. * @return Reference to the characteristic object, or nullptr if not found.
*/ */
NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(NimBLEUUID uuid) { NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEUUID &uuid) {
if (m_haveCharacteristics) { if (m_haveCharacteristics) {
std::string v = uuid.toString(); std::string v = uuid.toString();
for (auto &myPair : m_characteristicMap) { for (auto &myPair : m_characteristicMap) {
@ -261,7 +261,7 @@ NimBLEUUID NimBLERemoteService::getUUID() {
* @param [in] characteristicUuid The characteristic to read. * @param [in] characteristicUuid The characteristic to read.
* @returns a string containing the value or an empty string if not found or error. * @returns a string containing the value or an empty string if not found or error.
*/ */
std::string NimBLERemoteService::getValue(NimBLEUUID characteristicUuid) { std::string NimBLERemoteService::getValue(const NimBLEUUID &characteristicUuid) {
NIMBLE_LOGD(LOG_TAG, ">> readValue: uuid: %s", characteristicUuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> readValue: uuid: %s", characteristicUuid.toString().c_str());
std::string ret = ""; std::string ret = "";
@ -282,7 +282,7 @@ std::string NimBLERemoteService::getValue(NimBLEUUID characteristicUuid) {
* @param [in] value The value to set. * @param [in] value The value to set.
* @returns true on success, false if not found or error * @returns true on success, false if not found or error
*/ */
bool NimBLERemoteService::setValue(NimBLEUUID characteristicUuid, std::string value) { bool NimBLERemoteService::setValue(const NimBLEUUID &characteristicUuid, const std::string &value) {
NIMBLE_LOGD(LOG_TAG, ">> setValue: uuid: %s", characteristicUuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> setValue: uuid: %s", characteristicUuid.toString().c_str());
bool ret = false; bool ret = false;

View file

@ -37,7 +37,7 @@ public:
// Public methods // Public methods
NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); // Get the specified characteristic reference. NimBLERemoteCharacteristic* getCharacteristic(const char* uuid); // Get the specified characteristic reference.
NimBLERemoteCharacteristic* getCharacteristic(NimBLEUUID uuid); // Get the specified characteristic reference. NimBLERemoteCharacteristic* getCharacteristic(const NimBLEUUID &uuid); // Get the specified characteristic reference.
// BLERemoteCharacteristic* getCharacteristic(uint16_t uuid); // Get the specified characteristic reference. // BLERemoteCharacteristic* getCharacteristic(uint16_t uuid); // Get the specified characteristic reference.
std::map<std::string, NimBLERemoteCharacteristic*>* getCharacteristics(); std::map<std::string, NimBLERemoteCharacteristic*>* getCharacteristics();
std::map<uint16_t, NimBLERemoteCharacteristic*>* getCharacteristicsByHandle(); // Get the characteristics map. std::map<uint16_t, NimBLERemoteCharacteristic*>* getCharacteristicsByHandle(); // Get the characteristics map.
@ -46,8 +46,8 @@ public:
NimBLEClient* getClient(void); // Get a reference to the client associated with this service. NimBLEClient* getClient(void); // Get a reference to the client associated with this service.
uint16_t getHandle(); // Get the handle of this service. uint16_t getHandle(); // Get the handle of this service.
NimBLEUUID getUUID(void); // Get the UUID of this service. NimBLEUUID getUUID(void); // Get the UUID of this service.
std::string getValue(NimBLEUUID characteristicUuid); // Get the value of a characteristic. std::string getValue(const NimBLEUUID &characteristicUuid); // Get the value of a characteristic.
bool setValue(NimBLEUUID characteristicUuid, std::string value); // Set the value of a characteristic. bool setValue(const NimBLEUUID &characteristicUuid, const std::string &value); // Set the value of a characteristic.
std::string toString(void); std::string toString(void);
private: private:

View file

@ -325,7 +325,7 @@ void NimBLEScan::stop() {
// delete peer device from cache after disconnecting, it is required in case we are connecting to devices with not public address // delete peer device from cache after disconnecting, it is required in case we are connecting to devices with not public address
void NimBLEScan::erase(NimBLEAddress address) { void NimBLEScan::erase(const NimBLEAddress &address) {
NIMBLE_LOGI(LOG_TAG, "erase device: %s", address.toString().c_str()); NIMBLE_LOGI(LOG_TAG, "erase device: %s", address.toString().c_str());
NimBLEAdvertisedDevice *advertisedDevice = m_scanResults.m_advertisedDevicesMap.find(address.toString())->second; NimBLEAdvertisedDevice *advertisedDevice = m_scanResults.m_advertisedDevicesMap.find(address.toString())->second;
m_scanResults.m_advertisedDevicesMap.erase(address.toString()); m_scanResults.m_advertisedDevicesMap.erase(address.toString());

View file

@ -62,7 +62,7 @@ public:
void stop(); void stop();
void clearResults(); void clearResults();
NimBLEScanResults getResults(); NimBLEScanResults getResults();
void erase(NimBLEAddress address); void erase(const NimBLEAddress &address);
private: private:

View file

@ -62,7 +62,7 @@ NimBLEService* NimBLEServer::createService(const char* uuid) {
* @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service. * @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service.
* @return A reference to the new service object. * @return A reference to the new service object.
*/ */
NimBLEService* NimBLEServer::createService(NimBLEUUID uuid, uint32_t numHandles, uint8_t inst_id) { NimBLEService* NimBLEServer::createService(const NimBLEUUID &uuid, uint32_t numHandles, uint8_t inst_id) {
NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str()); NIMBLE_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
// Check that a service with the supplied UUID does not already exist. // Check that a service with the supplied UUID does not already exist.
@ -95,7 +95,7 @@ NimBLEService* NimBLEServer::getServiceByUUID(const char* uuid) {
* @param [in] uuid The UUID of the new service. * @param [in] uuid The UUID of the new service.
* @return A reference to the service object. * @return A reference to the service object.
*/ */
NimBLEService* NimBLEServer::getServiceByUUID(NimBLEUUID uuid) { NimBLEService* NimBLEServer::getServiceByUUID(const NimBLEUUID &uuid) {
return m_serviceMap.getByUUID(uuid); return m_serviceMap.getByUUID(uuid);
} }

View file

@ -46,10 +46,10 @@ class NimBLEServiceMap {
public: public:
// NimBLEService* getByHandle(uint16_t handle); // NimBLEService* getByHandle(uint16_t handle);
NimBLEService* getByUUID(const char* uuid); NimBLEService* getByUUID(const char* uuid);
NimBLEService* getByUUID(NimBLEUUID uuid, uint8_t inst_id = 0); NimBLEService* getByUUID(const NimBLEUUID &uuid, uint8_t inst_id = 0);
// void setByHandle(uint16_t handle, NimBLEService* service); // void setByHandle(uint16_t handle, NimBLEService* service);
void setByUUID(const char* uuid, NimBLEService* service); void setByUUID(const char* uuid, NimBLEService* service);
void setByUUID(NimBLEUUID uuid, NimBLEService* service); void setByUUID(const NimBLEUUID &uuid, NimBLEService* service);
std::string toString(); std::string toString();
NimBLEService* getFirst(); NimBLEService* getFirst();
NimBLEService* getNext(); NimBLEService* getNext();
@ -70,7 +70,7 @@ class NimBLEServer {
public: public:
uint32_t getConnectedCount(); uint32_t getConnectedCount();
NimBLEService* createService(const char* uuid); NimBLEService* createService(const char* uuid);
NimBLEService* createService(NimBLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0); NimBLEService* createService(const NimBLEUUID &uuid, uint32_t numHandles=15, uint8_t inst_id=0);
NimBLEAdvertising* getAdvertising(); NimBLEAdvertising* getAdvertising();
void setCallbacks(NimBLEServerCallbacks* pCallbacks); void setCallbacks(NimBLEServerCallbacks* pCallbacks);
void startAdvertising(); void startAdvertising();
@ -78,7 +78,7 @@ public:
void start(); void start();
// void removeService(BLEService* service); // void removeService(BLEService* service);
NimBLEService* getServiceByUUID(const char* uuid); NimBLEService* getServiceByUUID(const char* uuid);
NimBLEService* getServiceByUUID(NimBLEUUID uuid); NimBLEService* getServiceByUUID(const NimBLEUUID &uuid);
int disconnect(uint16_t connID, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); int disconnect(uint16_t connID, uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
// bool connect(BLEAddress address); // bool connect(BLEAddress address);
void updateConnParams(uint16_t conn_handle, void updateConnParams(uint16_t conn_handle,

View file

@ -43,7 +43,7 @@ NimBLEService::NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer
* @param [in] uuid The UUID of the service. * @param [in] uuid The UUID of the service.
* @param [in] numHandles The maximum number of handles associated with the service. * @param [in] numHandles The maximum number of handles associated with the service.
*/ */
NimBLEService::NimBLEService(NimBLEUUID uuid, uint16_t numHandles, NimBLEServer* pServer) { NimBLEService::NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLEServer* pServer) {
m_uuid = uuid; m_uuid = uuid;
m_handle = NULL_HANDLE; m_handle = NULL_HANDLE;
m_pServer = pServer; m_pServer = pServer;
@ -247,7 +247,7 @@ NimBLECharacteristic* NimBLEService::createCharacteristic(const char* uuid, uint
* @param [in] properties - The properties of the characteristic. * @param [in] properties - The properties of the characteristic.
* @return The new BLE characteristic. * @return The new BLE characteristic.
*/ */
NimBLECharacteristic* NimBLEService::createCharacteristic(NimBLEUUID uuid, uint32_t properties) { NimBLECharacteristic* NimBLEService::createCharacteristic(const NimBLEUUID &uuid, uint32_t properties) {
NimBLECharacteristic* pCharacteristic = new NimBLECharacteristic(uuid, properties, this); NimBLECharacteristic* pCharacteristic = new NimBLECharacteristic(uuid, properties, this);
addCharacteristic(pCharacteristic); addCharacteristic(pCharacteristic);
//pCharacteristic->executeCreate(this); //pCharacteristic->executeCreate(this);
@ -260,7 +260,7 @@ NimBLECharacteristic* NimBLEService::getCharacteristic(const char* uuid) {
} }
NimBLECharacteristic* NimBLEService::getCharacteristic(NimBLEUUID uuid) { NimBLECharacteristic* NimBLEService::getCharacteristic(const NimBLEUUID &uuid) {
return m_characteristicMap.getByUUID(uuid); return m_characteristicMap.getByUUID(uuid);
} }

View file

@ -32,10 +32,10 @@ class NimBLECharacteristic;
class NimBLECharacteristicMap { class NimBLECharacteristicMap {
public: public:
void setByUUID(NimBLECharacteristic* pCharacteristic, const char* uuid); void setByUUID(NimBLECharacteristic* pCharacteristic, const char* uuid);
void setByUUID(NimBLECharacteristic* pCharacteristic, NimBLEUUID uuid); void setByUUID(NimBLECharacteristic* pCharacteristic, const NimBLEUUID &uuid);
void setByHandle(uint16_t handle, NimBLECharacteristic* pCharacteristic); void setByHandle(uint16_t handle, NimBLECharacteristic* pCharacteristic);
NimBLECharacteristic* getByUUID(const char* uuid); NimBLECharacteristic* getByUUID(const char* uuid);
NimBLECharacteristic* getByUUID(NimBLEUUID uuid); NimBLECharacteristic* getByUUID(const NimBLEUUID &uuid);
NimBLECharacteristic* getByHandle(uint16_t handle); NimBLECharacteristic* getByHandle(uint16_t handle);
NimBLECharacteristic* getFirst(); NimBLECharacteristic* getFirst();
NimBLECharacteristic* getNext(); NimBLECharacteristic* getNext();
@ -59,13 +59,13 @@ public:
uint32_t properties = NIMBLE_PROPERTY::READ | uint32_t properties = NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE); NIMBLE_PROPERTY::WRITE);
NimBLECharacteristic* createCharacteristic(NimBLEUUID uuid, NimBLECharacteristic* createCharacteristic(const NimBLEUUID &uuid,
uint32_t properties = NIMBLE_PROPERTY::READ | uint32_t properties = NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE); NIMBLE_PROPERTY::WRITE);
void dump(); void dump();
NimBLECharacteristic* getCharacteristic(const char* uuid); NimBLECharacteristic* getCharacteristic(const char* uuid);
NimBLECharacteristic* getCharacteristic(NimBLEUUID uuid); NimBLECharacteristic* getCharacteristic(const NimBLEUUID &uuid);
NimBLEUUID getUUID(); NimBLEUUID getUUID();
NimBLEServer* getServer(); NimBLEServer* getServer();
bool start(); bool start();
@ -76,7 +76,7 @@ public:
private: private:
NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer* pServer); NimBLEService(const char* uuid, uint16_t numHandles, NimBLEServer* pServer);
NimBLEService(NimBLEUUID uuid, uint16_t numHandles, NimBLEServer* pServer); NimBLEService(const NimBLEUUID &uuid, uint16_t numHandles, NimBLEServer* pServer);
friend class NimBLEServer; friend class NimBLEServer;
friend class NimBLEDevice; friend class NimBLEDevice;

View file

@ -31,7 +31,7 @@ NimBLEService* NimBLEServiceMap::getByUUID(const char* uuid) {
* @param [in] UUID The UUID to look up the service. * @param [in] UUID The UUID to look up the service.
* @return The characteristic. * @return The characteristic.
*/ */
NimBLEService* NimBLEServiceMap::getByUUID(NimBLEUUID uuid, uint8_t inst_id) { NimBLEService* NimBLEServiceMap::getByUUID(const NimBLEUUID &uuid, uint8_t inst_id) {
for (auto &myPair : m_uuidMap) { for (auto &myPair : m_uuidMap) {
if (myPair.first->getUUID().equals(uuid)) { if (myPair.first->getUUID().equals(uuid)) {
return myPair.first; return myPair.first;
@ -59,7 +59,7 @@ NimBLEService* NimBLEServiceMap::getByHandle(uint16_t handle) {
* @param [in] characteristic The service to cache. * @param [in] characteristic The service to cache.
* @return N/A. * @return N/A.
*/ */
void NimBLEServiceMap::setByUUID(NimBLEUUID uuid, NimBLEService* service) { void NimBLEServiceMap::setByUUID(const NimBLEUUID &uuid, NimBLEService* service) {
m_uuidMap.insert(std::pair<NimBLEService*, std::string>(service, uuid.toString())); m_uuidMap.insert(std::pair<NimBLEService*, std::string>(service, uuid.toString()));
} // setByUUID } // setByUUID

View file

@ -40,7 +40,7 @@ static const char* LOG_TAG = "NimBLEUUID";
* *
* @param [in] value The string to build a UUID from. * @param [in] value The string to build a UUID from.
*/ */
NimBLEUUID::NimBLEUUID(std::string value) { NimBLEUUID::NimBLEUUID(const std::string &value) {
m_valueSet = true; m_valueSet = true;
if (value.length() == 4) { if (value.length() == 4) {
m_uuid.u.type = BLE_UUID_TYPE_16; m_uuid.u.type = BLE_UUID_TYPE_16;
@ -78,7 +78,7 @@ static const char* LOG_TAG = "NimBLEUUID";
* @param [in] size The size of the data. * @param [in] size The size of the data.
* @param [in] msbFirst Is the MSB first in pData memory? * @param [in] msbFirst Is the MSB first in pData memory?
*/ */
NimBLEUUID::NimBLEUUID(uint8_t* pData, size_t size, bool msbFirst) { NimBLEUUID::NimBLEUUID(const uint8_t* pData, size_t size, bool msbFirst) {
/*** TODO: change this to use the Nimble function for various lenght UUIDs: /*** TODO: change this to use the Nimble function for various lenght UUIDs:
int ble_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len); int ble_uuid_init_from_buf(ble_uuid_any_t *uuid, const void *buf, size_t len);
***/ ***/
@ -126,8 +126,7 @@ NimBLEUUID::NimBLEUUID(uint32_t uuid) {
* *
* @param [in] uuid The native UUID. * @param [in] uuid The native UUID.
*/ */
NimBLEUUID::NimBLEUUID(const ble_uuid128_t* uuid) {
NimBLEUUID::NimBLEUUID(ble_uuid128_t* uuid) {
m_uuid.u.type = BLE_UUID_TYPE_128; m_uuid.u.type = BLE_UUID_TYPE_128;
memcpy(m_uuid.u128.value, uuid->value, 16); memcpy(m_uuid.u128.value, uuid->value, 16);
m_valueSet = true; m_valueSet = true;
@ -163,7 +162,7 @@ NimBLEUUID::NimBLEUUID() {
* @brief Get the number of bits in this uuid. * @brief Get the number of bits in this uuid.
* @return The number of bits in the UUID. One of 16, 32 or 128. * @return The number of bits in the UUID. One of 16, 32 or 128.
*/ */
uint8_t NimBLEUUID::bitSize() { uint8_t NimBLEUUID::bitSize() const {
if (!m_valueSet) return 0; if (!m_valueSet) return 0;
return m_uuid.u.type; return m_uuid.u.type;
} // bitSize } // bitSize
@ -175,7 +174,7 @@ uint8_t NimBLEUUID::bitSize() {
* @param [in] uuid The UUID to compare against. * @param [in] uuid The UUID to compare against.
* @return True if the UUIDs are equal and false otherwise. * @return True if the UUIDs are equal and false otherwise.
*/ */
bool NimBLEUUID::equals(NimBLEUUID uuid) { bool NimBLEUUID::equals(const NimBLEUUID &uuid) const {
return *this == uuid; return *this == uuid;
} }
@ -189,8 +188,7 @@ bool NimBLEUUID::equals(NimBLEUUID uuid) {
* NNNNNNNN * NNNNNNNN
* <UUID> * <UUID>
*/ */
NimBLEUUID NimBLEUUID::fromString(const std::string &_uuid) {
NimBLEUUID NimBLEUUID::fromString(std::string _uuid) {
uint8_t start = 0; uint8_t start = 0;
if (strstr(_uuid.c_str(), "0x") != nullptr) { // If the string starts with 0x, skip those characters. if (strstr(_uuid.c_str(), "0x") != nullptr) { // If the string starts with 0x, skip those characters.
start = 2; start = 2;
@ -215,7 +213,7 @@ NimBLEUUID NimBLEUUID::fromString(std::string _uuid) {
* *
* @return The native UUID value or NULL if not set. * @return The native UUID value or NULL if not set.
*/ */
ble_uuid_any_t* NimBLEUUID::getNative() { const ble_uuid_any_t* NimBLEUUID::getNative() const {
if (m_valueSet == false) { if (m_valueSet == false) {
NIMBLE_LOGD(LOG_TAG,"<< Return of un-initialized UUID!"); NIMBLE_LOGD(LOG_TAG,"<< Return of un-initialized UUID!");
return nullptr; return nullptr;
@ -230,7 +228,7 @@ ble_uuid_any_t* NimBLEUUID::getNative() {
* A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method * A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method
* will convert 16 or 32 bit representations to the full 128bit. * will convert 16 or 32 bit representations to the full 128bit.
*/ */
NimBLEUUID &NimBLEUUID::to128() { const NimBLEUUID &NimBLEUUID::to128() {
// If we either don't have a value or are already a 128 bit UUID, nothing further to do. // If we either don't have a value or are already a 128 bit UUID, nothing further to do.
if (!m_valueSet || m_uuid.u.type == BLE_UUID_TYPE_128) { if (!m_valueSet || m_uuid.u.type == BLE_UUID_TYPE_128) {
return *this; return *this;
@ -258,11 +256,12 @@ NimBLEUUID &NimBLEUUID::to128() {
* *
* @return A string representation of the UUID. * @return A string representation of the UUID.
*/ */
std::string NimBLEUUID::toString() { std::string NimBLEUUID::toString() const {
return std::string(*this); return std::string(*this);
} // toString } // toString
bool NimBLEUUID::operator ==(const NimBLEUUID & rhs) {
bool NimBLEUUID::operator ==(const NimBLEUUID & rhs) const {
if(m_valueSet && rhs.m_valueSet) { if(m_valueSet && rhs.m_valueSet) {
return ble_uuid_cmp(&m_uuid.u, &rhs.m_uuid.u) == 0; return ble_uuid_cmp(&m_uuid.u, &rhs.m_uuid.u) == 0;
} }
@ -270,10 +269,12 @@ bool NimBLEUUID::operator ==(const NimBLEUUID & rhs) {
return m_valueSet == rhs.m_valueSet; return m_valueSet == rhs.m_valueSet;
} }
bool NimBLEUUID::operator !=(const NimBLEUUID & rhs) {
bool NimBLEUUID::operator !=(const NimBLEUUID & rhs) const {
return !this->operator==(rhs); return !this->operator==(rhs);
} }
NimBLEUUID::operator std::string() const { NimBLEUUID::operator std::string() const {
if (!m_valueSet) return std::string(); // If we have no value, nothing to format. if (!m_valueSet) return std::string(); // If we have no value, nothing to format.

View file

@ -30,22 +30,22 @@
*/ */
class NimBLEUUID { class NimBLEUUID {
public: public:
NimBLEUUID(std::string uuid); NimBLEUUID(const std::string &uuid);
NimBLEUUID(uint16_t uuid); NimBLEUUID(uint16_t uuid);
NimBLEUUID(uint32_t uuid); NimBLEUUID(uint32_t uuid);
NimBLEUUID(ble_uuid128_t* uuid); NimBLEUUID(const ble_uuid128_t* uuid);
NimBLEUUID(uint8_t* pData, size_t size, bool msbFirst); NimBLEUUID(const uint8_t* pData, size_t size, bool msbFirst);
NimBLEUUID(uint32_t first, uint16_t second, uint16_t third, uint64_t fourth); NimBLEUUID(uint32_t first, uint16_t second, uint16_t third, uint64_t fourth);
NimBLEUUID(); NimBLEUUID();
uint8_t bitSize(); // Get the number of bits in this uuid. uint8_t bitSize() const; // Get the number of bits in this uuid.
bool equals(NimBLEUUID uuid); bool equals(const NimBLEUUID &uuid) const;
ble_uuid_any_t* getNative(); const ble_uuid_any_t* getNative() const;
NimBLEUUID & to128(); const NimBLEUUID & to128();
std::string toString(); std::string toString() const;
static NimBLEUUID fromString(std::string uuid); // Create a NimBLEUUID from a string static NimBLEUUID fromString(const std::string &uuid); // Create a NimBLEUUID from a string
bool operator ==(const NimBLEUUID & rhs); bool operator ==(const NimBLEUUID & rhs) const;
bool operator !=(const NimBLEUUID & rhs); bool operator !=(const NimBLEUUID & rhs) const;
operator std::string() const; operator std::string() const;
private: private:

View file

@ -33,7 +33,7 @@ static const char* LOG_TAG = "NimBLEUtils";
* @param [in] source The source of the copy * @param [in] source The source of the copy
* @param [in] size The number of bytes to copy * @param [in] size The number of bytes to copy
*/ */
void NimBLEUtils::memrcpy(uint8_t* target, uint8_t* source, uint32_t size) { void NimBLEUtils::memrcpy(uint8_t* target, const uint8_t* source, uint32_t size) {
assert(size > 0); assert(size > 0);
target += (size - 1); // Point target to the last byte of the target data target += (size - 1); // Point target to the last byte of the target data
while (size > 0) { while (size > 0) {
@ -394,7 +394,7 @@ const char* NimBLEUtils::advTypeToString(uint8_t advType) {
* @param [in] length The length of the data to convert. * @param [in] length The length of the data to convert.
* @return A pointer to the formatted buffer. * @return A pointer to the formatted buffer.
*/ */
char* NimBLEUtils::buildHexData(uint8_t* target, uint8_t* source, uint8_t length) { char* NimBLEUtils::buildHexData(uint8_t* target, const uint8_t* source, uint8_t length) {
// Guard against too much data. // Guard against too much data.
if (length > 100) length = 100; if (length > 100) length = 100;

View file

@ -25,10 +25,10 @@ class NimBLEUtils {
public: public:
static void dumpGapEvent(ble_gap_event *event, void *arg); static void dumpGapEvent(ble_gap_event *event, void *arg);
static const char* gapEventToString(uint8_t eventType); static const char* gapEventToString(uint8_t eventType);
static char* buildHexData(uint8_t* target, uint8_t* source, uint8_t length); static char* buildHexData(uint8_t* target, const uint8_t* source, uint8_t length);
static const char* advTypeToString(uint8_t advType); static const char* advTypeToString(uint8_t advType);
static const char* returnCodeToString(int rc); static const char* returnCodeToString(int rc);
static void memrcpy(uint8_t* target, uint8_t* source, uint32_t size); static void memrcpy(uint8_t* target, const uint8_t* source, uint32_t size);
static int checkConnParams(ble_gap_conn_params* params); static int checkConnParams(ble_gap_conn_params* params);
}; };

View file

@ -31,7 +31,7 @@ NimBLEValue::NimBLEValue() {
* The accumulation is a growing set of data that is added to until a commit or cancel. * The accumulation is a growing set of data that is added to until a commit or cancel.
* @param [in] part A message part being added. * @param [in] part A message part being added.
*/ */
void NimBLEValue::addPart(std::string part) { void NimBLEValue::addPart(const std::string &part) {
NIMBLE_LOGD(LOG_TAG, ">> addPart: length=%d", part.length()); NIMBLE_LOGD(LOG_TAG, ">> addPart: length=%d", part.length());
m_accumulation += part; m_accumulation += part;
} // addPart } // addPart
@ -43,7 +43,7 @@ void NimBLEValue::addPart(std::string part) {
* @param [in] pData A message part being added. * @param [in] pData A message part being added.
* @param [in] length The number of bytes being added. * @param [in] length The number of bytes being added.
*/ */
void NimBLEValue::addPart(uint8_t* pData, size_t length) { void NimBLEValue::addPart(const uint8_t* pData, size_t length) {
NIMBLE_LOGD(LOG_TAG, ">> addPart: length=%d", length); NIMBLE_LOGD(LOG_TAG, ">> addPart: length=%d", length);
m_accumulation += std::string((char*) pData, length); m_accumulation += std::string((char*) pData, length);
} // addPart } // addPart
@ -122,7 +122,7 @@ void NimBLEValue::setReadOffset(uint16_t readOffset) {
/** /**
* @brief Set the current value. * @brief Set the current value.
*/ */
void NimBLEValue::setValue(std::string value) { void NimBLEValue::setValue(const std::string &value) {
m_value = value; m_value = value;
} // setValue } // setValue
@ -132,7 +132,7 @@ void NimBLEValue::setValue(std::string value) {
* @param [in] pData The data for the current value. * @param [in] pData The data for the current value.
* @param [in] The length of the new current value. * @param [in] The length of the new current value.
*/ */
void NimBLEValue::setValue(uint8_t* pData, size_t length) { void NimBLEValue::setValue(const uint8_t* pData, size_t length) {
m_value = std::string((char*) pData, length); m_value = std::string((char*) pData, length);
} // setValue } // setValue

View file

@ -24,8 +24,8 @@
class NimBLEValue { class NimBLEValue {
public: public:
NimBLEValue(); NimBLEValue();
void addPart(std::string part); void addPart(const std::string &part);
void addPart(uint8_t* pData, size_t length); void addPart(const uint8_t* pData, size_t length);
void cancel(); void cancel();
void commit(); void commit();
uint8_t* getData(); uint8_t* getData();
@ -33,8 +33,8 @@ public:
uint16_t getReadOffset(); uint16_t getReadOffset();
std::string getValue(); std::string getValue();
void setReadOffset(uint16_t readOffset); void setReadOffset(uint16_t readOffset);
void setValue(std::string value); void setValue(const std::string &value);
void setValue(uint8_t* pData, size_t length); void setValue(const uint8_t* pData, size_t length);
private: private:
std::string m_accumulation; std::string m_accumulation;