Correct error in creation of NimBLEAddress from six bytes in a std::string

* Add creation of a NimBLEAddress from a std::string of 6 exactly bytes.

* Add creation of a NimBLEAddress from an empty string.
This commit is contained in:
h2zero 2020-05-18 10:11:54 -06:00
parent 08fc2878e7
commit 32e1022e67
4 changed files with 13 additions and 2 deletions

View file

@ -48,6 +48,16 @@ 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(const std::string &stringAddress) { NimBLEAddress::NimBLEAddress(const std::string &stringAddress) {
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;
}
if (stringAddress.length() != 17) { if (stringAddress.length() != 17) {
memset(m_address, 0, sizeof m_address); // "00:00:00:00:00:00" represents an invalid address 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()); NIMBLE_LOGD(LOG_TAG, "Invalid address '%s'", stringAddress.c_str());

View file

@ -24,6 +24,7 @@
/**************************/ /**************************/
#include <string> #include <string>
#include <algorithm>
/** /**
* @brief A %BLE device address. * @brief A %BLE device address.

View file

@ -95,7 +95,7 @@ private:
bool m_haveTXPower; bool m_haveTXPower;
NimBLEAddress m_address = NimBLEAddress("\0\0\0\0\0\0"); NimBLEAddress m_address = NimBLEAddress("");
uint8_t m_advType; uint8_t m_advType;
uint16_t m_appearance; uint16_t m_appearance;
int m_deviceType; int m_deviceType;

View file

@ -73,7 +73,7 @@ private:
bool retrieveServices(); //Retrieve services from the server bool retrieveServices(); //Retrieve services from the server
// void onHostReset(); // void onHostReset();
NimBLEAddress m_peerAddress = NimBLEAddress("\0\0\0\0\0\0"); // The BD address of the remote server. NimBLEAddress m_peerAddress = NimBLEAddress(""); // The BD address of the remote server.
uint16_t m_conn_id; uint16_t m_conn_id;
bool m_haveServices = false; // Have we previously obtain the set of services from the remote server. bool m_haveServices = false; // Have we previously obtain the set of services from the remote server.
bool m_isConnected = false; // Are we currently connected. bool m_isConnected = false; // Are we currently connected.