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"
#if defined(CONFIG_BT_ENABLED)
#include <algorithm>
#include "NimBLEAddress.h"
#include "NimBLEUtils.h"
#include "NimBLELog.h"
static const char* LOG_TAG = "NimBLEAddress";
/*************************************************
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.
*/
NimBLEAddress::NimBLEAddress(std::string stringAddress) {
if (stringAddress.length() != 17) return;
NimBLEAddress::NimBLEAddress(const std::string &stringAddress) {
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];
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]);
m_address[0] = (uint8_t) data[0];
m_address[1] = (uint8_t) data[1];
m_address[2] = (uint8_t) data[2];
m_address[3] = (uint8_t) data[3];
m_address[4] = (uint8_t) data[4];
m_address[5] = (uint8_t) data[5];
if(sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]) != 6) {
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());
}
for(size_t index = 0; index < sizeof m_address; index++) {
m_address[index] = data[index];
}
} // 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.
*/
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
@@ -71,8 +88,8 @@ NimBLEAddress::NimBLEAddress(uint8_t address[6]) {
* @param [in] otherAddress The other address to compare against.
* @return True if the addresses are equal.
*/
bool NimBLEAddress::equals(NimBLEAddress otherAddress) {
return memcmp(otherAddress.getNative(), m_address, 6) == 0;
bool NimBLEAddress::equals(const NimBLEAddress &otherAddress) const {
return *this == otherAddress;
} // equals
@@ -80,7 +97,7 @@ bool NimBLEAddress::equals(NimBLEAddress otherAddress) {
* @brief 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;
} // getNative
@@ -96,13 +113,23 @@ uint8_t *NimBLEAddress::getNative() {
*
* @return The string representation of the address.
*/
std::string NimBLEAddress::toString() {
auto size = 18;
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;
std::string NimBLEAddress::toString() const {
return std::string(*this);
} // 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