2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEAddress.cpp
|
|
|
|
*
|
|
|
|
* Created: on Jan 24 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEAddress.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jul 2, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
2021-09-07 05:14:43 +02:00
|
|
|
#include "nimconfig.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
|
|
|
|
2020-05-10 15:21:46 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAddress.h"
|
|
|
|
#include "NimBLEUtils.h"
|
2020-05-10 15:21:46 +02:00
|
|
|
#include "NimBLELog.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-05-10 15:21:46 +02:00
|
|
|
static const char* LOG_TAG = "NimBLEAddress";
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/*************************************************
|
2020-07-09 03:27:26 +02:00
|
|
|
* NOTE: NimBLE address bytes are in INVERSE ORDER!
|
|
|
|
* We will accomodate that fact in these methods.
|
2020-03-30 01:44:20 +02:00
|
|
|
*************************************************/
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Create an address from the native NimBLE representation.
|
|
|
|
* @param [in] address The native NimBLE address.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
|
|
|
NimBLEAddress::NimBLEAddress(ble_addr_t address) {
|
|
|
|
memcpy(m_address, address.val, 6);
|
2020-07-30 22:57:47 +02:00
|
|
|
m_addrType = address.type;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // NimBLEAddress
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
2020-09-02 22:52:34 +02:00
|
|
|
/**
|
|
|
|
* @brief Create a blank address, i.e. 00:00:00:00:00:00, type 0.
|
|
|
|
*/
|
|
|
|
NimBLEAddress::NimBLEAddress() {
|
|
|
|
NimBLEAddress("");
|
|
|
|
} // NimBLEAddress
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Create an address from a hex string
|
|
|
|
*
|
|
|
|
* A hex string is of the format:
|
|
|
|
* ```
|
|
|
|
* 00:00:00:00:00:00
|
|
|
|
* ```
|
|
|
|
* which is 17 characters in length.
|
|
|
|
*
|
2020-07-09 03:27:26 +02:00
|
|
|
* @param [in] stringAddress The hex string representation of the address.
|
2020-07-30 22:57:47 +02:00
|
|
|
* @param [in] type The type of the address.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-07-30 22:57:47 +02:00
|
|
|
NimBLEAddress::NimBLEAddress(const std::string &stringAddress, uint8_t type) {
|
|
|
|
m_addrType = type;
|
|
|
|
|
2020-05-18 18:11:54 +02:00
|
|
|
if (stringAddress.length() == 0) {
|
|
|
|
memset(m_address, 0, 6);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stringAddress.length() == 6) {
|
|
|
|
std::reverse_copy(stringAddress.data(), stringAddress.data() + 6, m_address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:21:46 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
int data[6];
|
2020-05-10 15:21:46 +02:00
|
|
|
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];
|
|
|
|
}
|
2020-07-09 03:27:26 +02:00
|
|
|
} // NimBLEAddress
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Constructor for compatibility with bluedroid esp library using native ESP representation.
|
|
|
|
* @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address.
|
2020-07-30 22:57:47 +02:00
|
|
|
* @param [in] type The type of the address.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-07-30 22:57:47 +02:00
|
|
|
NimBLEAddress::NimBLEAddress(uint8_t address[6], uint8_t type) {
|
2020-05-10 15:21:46 +02:00
|
|
|
std::reverse_copy(address, address + sizeof m_address, m_address);
|
2020-07-30 22:57:47 +02:00
|
|
|
m_addrType = type;
|
2020-05-10 15:21:46 +02:00
|
|
|
} // NimBLEAddress
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Constructor for address using a hex value.\n
|
|
|
|
* Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16"
|
|
|
|
* @param [in] address uint64_t containing the address.
|
2020-07-30 22:57:47 +02:00
|
|
|
* @param [in] type The type of the address.
|
2020-05-10 15:21:46 +02:00
|
|
|
*/
|
2020-07-30 22:57:47 +02:00
|
|
|
NimBLEAddress::NimBLEAddress(const uint64_t &address, uint8_t type) {
|
2020-05-10 15:21:46 +02:00
|
|
|
memcpy(m_address, &address, sizeof m_address);
|
2020-07-30 22:57:47 +02:00
|
|
|
m_addrType = type;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // NimBLEAddress
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Determine if this address equals another.
|
|
|
|
* @param [in] otherAddress The other address to compare against.
|
|
|
|
* @return True if the addresses are equal.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
bool NimBLEAddress::equals(const NimBLEAddress &otherAddress) const {
|
|
|
|
return *this == otherAddress;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // equals
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Get the native representation of the address.
|
|
|
|
* @return a pointer to the uint8_t[6] array of the address.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
const uint8_t *NimBLEAddress::getNative() const {
|
2020-03-30 01:44:20 +02:00
|
|
|
return m_address;
|
|
|
|
} // getNative
|
|
|
|
|
|
|
|
|
2020-07-30 22:57:47 +02:00
|
|
|
/**
|
|
|
|
* @brief Get the address type.
|
|
|
|
* @return The address type.
|
|
|
|
*/
|
|
|
|
uint8_t NimBLEAddress::getType() const {
|
|
|
|
return m_addrType;
|
|
|
|
} // getType
|
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
|
|
|
* @brief Convert a BLE address to a string.
|
|
|
|
*
|
|
|
|
* A string representation of an address is in the format:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* xx:xx:xx:xx:xx:xx
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @return The string representation of the address.
|
2020-07-09 03:27:26 +02:00
|
|
|
* @deprecated Use std::string() operator instead.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
std::string NimBLEAddress::toString() const {
|
|
|
|
return std::string(*this);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // toString
|
2020-05-10 15:21:46 +02:00
|
|
|
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
/**
|
|
|
|
* @brief Convienience operator to check if this address is equal to another.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
bool NimBLEAddress::operator ==(const NimBLEAddress & rhs) const {
|
|
|
|
return memcmp(rhs.m_address, m_address, sizeof m_address) == 0;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // operator ==
|
2020-05-10 15:21:46 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convienience operator to check if this address is not equal to another.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
bool NimBLEAddress::operator !=(const NimBLEAddress & rhs) const {
|
|
|
|
return !this->operator==(rhs);
|
2020-07-09 03:27:26 +02:00
|
|
|
} // operator !=
|
2020-05-10 15:21:46 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convienience operator to convert this address to string representation.
|
|
|
|
* @details This allows passing NimBLEAddress to functions
|
|
|
|
* that accept std::string and/or or it's methods as a parameter.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
NimBLEAddress::operator std::string() const {
|
2020-08-03 15:33:11 +02:00
|
|
|
char buffer[18];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%02x:%02x:%02x:%02x:%02x:%02x",
|
2020-07-30 22:57:47 +02:00
|
|
|
m_address[5], m_address[4], m_address[3],
|
2020-08-03 15:33:11 +02:00
|
|
|
m_address[2], m_address[1], m_address[0]);
|
2020-05-10 15:21:46 +02:00
|
|
|
return std::string(buffer);
|
2020-07-09 03:27:26 +02:00
|
|
|
} // operator std::string
|
2020-05-10 15:21:46 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convienience operator to convert the native address representation to uint_64.
|
|
|
|
*/
|
2020-06-19 20:32:57 +02:00
|
|
|
NimBLEAddress::operator uint64_t() const {
|
|
|
|
uint64_t address = 0;
|
|
|
|
memcpy(&address, m_address, sizeof m_address);
|
|
|
|
return address;
|
2020-07-09 03:27:26 +02:00
|
|
|
} // operator uint64_t
|
2020-06-19 20:32:57 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif
|