diff options
| author | celtare21 <celtare21@gmail.com> | 2019-06-14 22:40:47 +0300 |
|---|---|---|
| committer | celtare21 <celtare21@gmail.com> | 2019-06-14 22:40:47 +0300 |
| commit | 5f9cfae56eb6cf7e9da01c6284a89f2a75341d83 (patch) | |
| tree | be2202896a821ad130be76802a47470a34b725e9 /bluetooth | |
| parent | d3a322dbe6b3f995ddc2012a99d245133410f6b5 (diff) | |
Revert "mata: Convert bluetooth@1.0-impl to read QC MAC address"
This reverts commit 585748e3c5c28e010d0facad306a39f112d47f6c.
Signed-off-by: celtare21 <celtare21@gmail.com>
Diffstat (limited to 'bluetooth')
| -rw-r--r-- | bluetooth/impl/bluetooth_address.cc | 75 | ||||
| -rw-r--r-- | bluetooth/impl/bluetooth_address.h | 24 |
2 files changed, 77 insertions, 22 deletions
diff --git a/bluetooth/impl/bluetooth_address.cc b/bluetooth/impl/bluetooth_address.cc index 4377b1c6..93a54699 100644 --- a/bluetooth/impl/bluetooth_address.cc +++ b/bluetooth/impl/bluetooth_address.cc @@ -16,44 +16,77 @@ #include "bluetooth_address.h" +#include <cutils/properties.h> #include <errno.h> #include <fcntl.h> -#include <string.h> #include <unistd.h> #include <utils/Log.h> -namespace { -constexpr char kNvPath[] = "/persist/bluetooth/.bt_nv.bin"; -constexpr size_t kNvPathSize = 9; -} - namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { +void BluetoothAddress::bytes_to_string(const uint8_t* addr, char* addr_str) { + sprintf(addr_str, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], + addr[3], addr[4], addr[5]); +} + +bool BluetoothAddress::string_to_bytes(const char* addr_str, uint8_t* addr) { + if (addr_str == NULL) return false; + if (strnlen(addr_str, kStringLength) != kStringLength) return false; + unsigned char trailing_char = '\0'; + + return (sscanf(addr_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx%1c", + &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5], + &trailing_char) == kBytes); +} + bool BluetoothAddress::get_local_address(uint8_t* local_addr) { - int addr_fd = open(kNvPath, O_RDONLY); - if (addr_fd != -1) { - char address[kNvPathSize] = {0}; - int bytes_read = read(addr_fd, address, kNvPathSize); - if (bytes_read == -1) { - ALOGE("%s: Error reading address from %s: %s", __func__, kNvPath, - strerror(errno)); + char property[PROPERTY_VALUE_MAX] = {0}; + + // Get local bdaddr storage path from a system property. + if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) { + ALOGD("%s: Trying %s", __func__, property); + + int addr_fd = open(property, O_RDONLY); + if (addr_fd != -1) { + char address[kStringLength + 1] = {0}; + int bytes_read = read(addr_fd, address, kStringLength); + if (bytes_read == -1) { + ALOGE("%s: Error reading address from %s: %s", __func__, property, + strerror(errno)); + } + close(addr_fd); + + // Null terminate the string. + address[kStringLength] = '\0'; + + // If the address is not all zeros, then use it. + const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0}; + if ((string_to_bytes(address, local_addr)) && + (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) { + ALOGD("%s: Got Factory BDA %s", __func__, address); + return true; + } else { + ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property); + } } - close(addr_fd); + } - // Swap into local_addr - local_addr[0] = address[8]; - local_addr[1] = address[7]; - local_addr[2] = address[6]; - local_addr[3] = address[5]; - local_addr[4] = address[4]; - local_addr[5] = address[3]; + // No BDADDR found in the file. Look for BDA in a factory property. + if (property_get(FACTORY_BDADDR_PROPERTY, property, NULL) && + string_to_bytes(property, local_addr)) { + return true; + } + // No factory BDADDR found. Look for a previously stored BDA. + if (property_get(PERSIST_BDADDR_PROPERTY, property, NULL) && + string_to_bytes(property, local_addr)) { return true; } + return false; } diff --git a/bluetooth/impl/bluetooth_address.h b/bluetooth/impl/bluetooth_address.h index 2a060c0b..94bf6161 100644 --- a/bluetooth/impl/bluetooth_address.h +++ b/bluetooth/impl/bluetooth_address.h @@ -16,7 +16,11 @@ #pragma once +#include <fcntl.h> + #include <cstdint> +#include <string> +#include <vector> namespace android { namespace hardware { @@ -24,10 +28,28 @@ namespace bluetooth { namespace V1_0 { namespace implementation { +// The property key stores the storage location of Bluetooth Device Address +static constexpr char PROPERTY_BT_BDADDR_PATH[] = "ro.bt.bdaddr_path"; + +// Check for a legacy address stored as a property. +static constexpr char PERSIST_BDADDR_PROPERTY[] = + "persist.service.bdroid.bdaddr"; + +// If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH and there +// is no available persistent bdaddr available from PERSIST_BDADDR_PROPERTY, +// use a factory set address. +static constexpr char FACTORY_BDADDR_PROPERTY[] = "ro.boot.btmacaddr"; + // Encapsulate handling for Bluetooth Addresses: class BluetoothAddress { public: - static constexpr size_t kBytes = 6; + // Conversion constants + static constexpr size_t kStringLength = sizeof("XX:XX:XX:XX:XX:XX") - 1; + static constexpr size_t kBytes = (kStringLength + 1) / 3; + + static void bytes_to_string(const uint8_t* addr, char* addr_str); + + static bool string_to_bytes(const char* addr_str, uint8_t* addr); static bool get_local_address(uint8_t* addr); }; |
