|
| 1 | +/* |
| 2 | + * BTAddress.cpp |
| 3 | + * |
| 4 | + * Created on: Jul 2, 2017 |
| 5 | + * Author: kolban |
| 6 | + * Ported on: Feb 5, 2021 |
| 7 | + * Author: Thomas M. (ArcticSnowSky) |
| 8 | + */ |
| 9 | +#include "sdkconfig.h" |
| 10 | +#if defined(CONFIG_BT_ENABLED) |
| 11 | + |
| 12 | +#include "BTAddress.h" |
| 13 | +#include <string> |
| 14 | +#include <sstream> |
| 15 | +#include <iomanip> |
| 16 | +#include <string.h> |
| 17 | +#include <stdio.h> |
| 18 | +#include <malloc.h> |
| 19 | +#ifdef ARDUINO_ARCH_ESP32 |
| 20 | +#include "esp32-hal-log.h" |
| 21 | +#endif |
| 22 | + |
| 23 | + |
| 24 | +/** |
| 25 | + * @brief Create an address from the native ESP32 representation. |
| 26 | + * @param [in] address The native representation. |
| 27 | + */ |
| 28 | +BTAddress::BTAddress(esp_bd_addr_t address) { |
| 29 | + memcpy(m_address, address, ESP_BD_ADDR_LEN); |
| 30 | +} // BTAddress |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * @brief Create an address from a hex string |
| 35 | + * |
| 36 | + * A hex string is of the format: |
| 37 | + * ``` |
| 38 | + * 00:00:00:00:00:00 |
| 39 | + * ``` |
| 40 | + * which is 17 characters in length. |
| 41 | + * |
| 42 | + * @param [in] stringAddress The hex representation of the address. |
| 43 | + */ |
| 44 | +BTAddress::BTAddress(std::string stringAddress) { |
| 45 | + if (stringAddress.length() != 17) return; |
| 46 | + |
| 47 | + int data[6]; |
| 48 | + sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]); |
| 49 | + m_address[0] = (uint8_t) data[0]; |
| 50 | + m_address[1] = (uint8_t) data[1]; |
| 51 | + m_address[2] = (uint8_t) data[2]; |
| 52 | + m_address[3] = (uint8_t) data[3]; |
| 53 | + m_address[4] = (uint8_t) data[4]; |
| 54 | + m_address[5] = (uint8_t) data[5]; |
| 55 | +} // BTAddress |
| 56 | + |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief Determine if this address equals another. |
| 60 | + * @param [in] otherAddress The other address to compare against. |
| 61 | + * @return True if the addresses are equal. |
| 62 | + */ |
| 63 | +bool BTAddress::equals(BTAddress otherAddress) { |
| 64 | + return memcmp(otherAddress.getNative(), m_address, 6) == 0; |
| 65 | +} // equals |
| 66 | + |
| 67 | + |
| 68 | +/** |
| 69 | + * @brief Return the native representation of the address. |
| 70 | + * @return The native representation of the address. |
| 71 | + */ |
| 72 | +esp_bd_addr_t *BTAddress::getNative() { |
| 73 | + return &m_address; |
| 74 | +} // getNative |
| 75 | + |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Convert a BT address to a string. |
| 79 | + * |
| 80 | + * A string representation of an address is in the format: |
| 81 | + * |
| 82 | + * ``` |
| 83 | + * xx:xx:xx:xx:xx:xx |
| 84 | + * ``` |
| 85 | + * |
| 86 | + * @return The string representation of the address. |
| 87 | + */ |
| 88 | +std::string BTAddress::toString() { |
| 89 | + auto size = 18; |
| 90 | + char *res = (char*)malloc(size); |
| 91 | + snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]); |
| 92 | + std::string ret(res); |
| 93 | + free(res); |
| 94 | + return ret; |
| 95 | +} // toString |
| 96 | +#endif |
0 commit comments