|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 Ryan Powell <[email protected]> and |
| 3 | + * esp-nimble-cpp, NimBLE-Arduino contributors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "sdkconfig.h" |
| 19 | +#if defined(CONFIG_NIMBLE_ENABLED) |
| 20 | + |
| 21 | +#include "BLEAddress.h" |
| 22 | + |
| 23 | +#include <algorithm> |
| 24 | + |
| 25 | +#ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_EXCLUDE_DELIMITER |
| 26 | +#define NIMBLE_CPP_ADDR_DELIMITER "" |
| 27 | +#else |
| 28 | +#define NIMBLE_CPP_ADDR_DELIMITER ":" |
| 29 | +#endif |
| 30 | + |
| 31 | +#ifdef CONFIG_NIMBLE_CPP_ADDR_FMT_UPPERCASE |
| 32 | +#define NIMBLE_CPP_ADDR_FMT "%02X%s%02X%s%02X%s%02X%s%02X%s%02X" |
| 33 | +#else |
| 34 | +#define NIMBLE_CPP_ADDR_FMT "%02x%s%02x%s%02x%s%02x%s%02x%s%02x" |
| 35 | +#endif |
| 36 | + |
| 37 | +static const char *LOG_TAG = "BLEAddress"; |
| 38 | + |
| 39 | +/************************************************* |
| 40 | + * NOTE: BLE address bytes are in INVERSE ORDER! |
| 41 | + * We will accommodate that fact in these methods. |
| 42 | + *************************************************/ |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Create an address from the native BLE representation. |
| 46 | + * @param [in] address The native BLE address. |
| 47 | + */ |
| 48 | +BLEAddress::BLEAddress(ble_addr_t address) : ble_addr_t{address} {} |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Create an address from a hex string. |
| 52 | + * |
| 53 | + * A hex string is of the format: |
| 54 | + * ``` |
| 55 | + * 00:00:00:00:00:00 |
| 56 | + * ``` |
| 57 | + * which is 17 characters in length. |
| 58 | + * @param [in] addr The hex string representation of the address. |
| 59 | + * @param [in] type The type of the address, should be one of: |
| 60 | + * * BLE_ADDR_PUBLIC (0) |
| 61 | + * * BLE_ADDR_RANDOM (1) |
| 62 | + */ |
| 63 | +BLEAddress::BLEAddress(const std::string &addr, uint8_t type) { |
| 64 | + this->type = type; |
| 65 | + |
| 66 | + if (addr.length() == BLE_DEV_ADDR_LEN) { |
| 67 | + std::reverse_copy(addr.data(), addr.data() + BLE_DEV_ADDR_LEN, this->val); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (addr.length() == 17) { |
| 72 | + std::string mac{addr}; |
| 73 | + mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end()); |
| 74 | + uint64_t address = std::stoull(mac, nullptr, 16); |
| 75 | + memcpy(this->val, &address, sizeof(this->val)); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + *this = BLEAddress{}; |
| 80 | + log_e(LOG_TAG, "Invalid address '%s'", addr.c_str()); |
| 81 | +} // BLEAddress |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief Constructor for compatibility with bluedroid esp library using native ESP representation. |
| 85 | + * @param [in] address A uint8_t[6] or esp_bd_addr_t containing the address. |
| 86 | + * @param [in] type The type of the address should be one of: |
| 87 | + * * BLE_ADDR_PUBLIC (0) |
| 88 | + * * BLE_ADDR_RANDOM (1) |
| 89 | + */ |
| 90 | +BLEAddress::BLEAddress(const uint8_t address[BLE_DEV_ADDR_LEN], uint8_t type) { |
| 91 | + std::reverse_copy(address, address + BLE_DEV_ADDR_LEN, this->val); |
| 92 | + this->type = type; |
| 93 | +} // BLEAddress |
| 94 | + |
| 95 | +/** |
| 96 | + * @brief Constructor for address using a hex value.\n |
| 97 | + * Use the same byte order, so use 0xa4c1385def16 for "a4:c1:38:5d:ef:16" |
| 98 | + * @param [in] address uint64_t containing the address. |
| 99 | + * @param [in] type The type of the address should be one of: |
| 100 | + * * BLE_ADDR_PUBLIC (0) |
| 101 | + * * BLE_ADDR_RANDOM (1) |
| 102 | + */ |
| 103 | +BLEAddress::BLEAddress(const uint64_t &address, uint8_t type) { |
| 104 | + memcpy(this->val, &address, sizeof(this->val)); |
| 105 | + this->type = type; |
| 106 | +} // BLEAddress |
| 107 | + |
| 108 | +/** |
| 109 | + * @brief Determine if this address equals another. |
| 110 | + * @param [in] otherAddress The other address to compare against. |
| 111 | + * @return True if the addresses are equal. |
| 112 | + */ |
| 113 | +bool BLEAddress::equals(const BLEAddress &otherAddress) const { |
| 114 | + return *this == otherAddress; |
| 115 | +} // equals |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Get the BLE base struct of the address. |
| 119 | + * @return A read only reference to the BLE base struct of the address. |
| 120 | + */ |
| 121 | +const ble_addr_t *BLEAddress::getBase() const { |
| 122 | + return reinterpret_cast<const ble_addr_t *>(this); |
| 123 | +} // getBase |
| 124 | + |
| 125 | +/** |
| 126 | + * @brief Get the address type. |
| 127 | + * @return The address type. |
| 128 | + */ |
| 129 | +uint8_t BLEAddress::getType() const { |
| 130 | + return this->type; |
| 131 | +} // getType |
| 132 | + |
| 133 | +/** |
| 134 | + * @brief Get the address value. |
| 135 | + * @return A read only reference to the address value. |
| 136 | + */ |
| 137 | +const uint8_t *BLEAddress::getVal() const { |
| 138 | + return this->val; |
| 139 | +} // getVal |
| 140 | + |
| 141 | +/** |
| 142 | + * @brief Determine if this address is a Resolvable Private Address. |
| 143 | + * @return True if the address is a RPA. |
| 144 | + */ |
| 145 | +bool BLEAddress::isRpa() const { |
| 146 | + return BLE_ADDR_IS_RPA(this); |
| 147 | +} // isRpa |
| 148 | + |
| 149 | +/** |
| 150 | + * @brief Determine if this address is a Non-Resolvable Private Address. |
| 151 | + * @return True if the address is a NRPA. |
| 152 | + */ |
| 153 | +bool BLEAddress::isNrpa() const { |
| 154 | + return BLE_ADDR_IS_NRPA(this); |
| 155 | +} // isNrpa |
| 156 | + |
| 157 | +/** |
| 158 | + * @brief Determine if this address is a Static Address. |
| 159 | + * @return True if the address is a Static Address. |
| 160 | + */ |
| 161 | +bool BLEAddress::isStatic() const { |
| 162 | + return BLE_ADDR_IS_STATIC(this); |
| 163 | +} // isStatic |
| 164 | + |
| 165 | +/** |
| 166 | + * @brief Determine if this address is a Public Address. |
| 167 | + * @return True if the address is a Public Address. |
| 168 | + */ |
| 169 | +bool BLEAddress::isPublic() const { |
| 170 | + return this->type == BLE_ADDR_PUBLIC; |
| 171 | +} // isPublic |
| 172 | + |
| 173 | +/** |
| 174 | + * @brief Determine if this address is a NULL Address. |
| 175 | + * @return True if the address is a NULL Address. |
| 176 | + */ |
| 177 | +bool BLEAddress::isNull() const { |
| 178 | + return *this == BLEAddress{}; |
| 179 | +} // isNull |
| 180 | + |
| 181 | +/** |
| 182 | + * @brief Convert a BLE address to a string. |
| 183 | + * @return The string representation of the address. |
| 184 | + * @deprecated Use std::string() operator instead. |
| 185 | + */ |
| 186 | +std::string BLEAddress::toString() const { |
| 187 | + return std::string(*this); |
| 188 | +} // toString |
| 189 | + |
| 190 | +/** |
| 191 | + * @brief Reverse the byte order of the address. |
| 192 | + * @return A reference to this address. |
| 193 | + */ |
| 194 | +const BLEAddress &BLEAddress::reverseByteOrder() { |
| 195 | + std::reverse(this->val, this->val + BLE_DEV_ADDR_LEN); |
| 196 | + return *this; |
| 197 | +} // reverseByteOrder |
| 198 | + |
| 199 | +/** |
| 200 | + * @brief Convenience operator to check if this address is equal to another. |
| 201 | + */ |
| 202 | +bool BLEAddress::operator==(const BLEAddress &rhs) const { |
| 203 | + if (this->type != rhs.type) { |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + return memcmp(rhs.val, this->val, sizeof(this->val)) == 0; |
| 208 | +} // operator == |
| 209 | + |
| 210 | +/** |
| 211 | + * @brief Convenience operator to check if this address is not equal to another. |
| 212 | + */ |
| 213 | +bool BLEAddress::operator!=(const BLEAddress &rhs) const { |
| 214 | + return !this->operator==(rhs); |
| 215 | +} // operator != |
| 216 | + |
| 217 | +/** |
| 218 | + * @brief Convenience operator to convert this address to string representation. |
| 219 | + * @details This allows passing BLEAddress to functions that accept std::string and/or it's methods as a parameter. |
| 220 | + */ |
| 221 | +BLEAddress::operator std::string() const { |
| 222 | + char buffer[18]; |
| 223 | + snprintf( |
| 224 | + buffer, sizeof(buffer), NIMBLE_CPP_ADDR_FMT, this->val[5], NIMBLE_CPP_ADDR_DELIMITER, this->val[4], NIMBLE_CPP_ADDR_DELIMITER, this->val[3], |
| 225 | + NIMBLE_CPP_ADDR_DELIMITER, this->val[2], NIMBLE_CPP_ADDR_DELIMITER, this->val[1], NIMBLE_CPP_ADDR_DELIMITER, this->val[0] |
| 226 | + ); |
| 227 | + return std::string{buffer}; |
| 228 | +} // operator std::string |
| 229 | + |
| 230 | +/** |
| 231 | + * @brief Convenience operator to convert the native address representation to uint_64. |
| 232 | + */ |
| 233 | +BLEAddress::operator uint64_t() const { |
| 234 | + uint64_t address = 0; |
| 235 | + memcpy(&address, this->val, sizeof(this->val)); |
| 236 | + return address; |
| 237 | +} // operator uint64_t |
| 238 | + |
| 239 | +#endif |
0 commit comments