Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "bluetooth_address.h" |
| 18 | |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 19 | #include <cutils/properties.h> |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Jiyong Park | cb198f7 | 2017-07-10 13:27:05 +0900 | [diff] [blame] | 22 | #include <unistd.h> |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace bluetooth { |
| 28 | namespace V1_0 { |
| 29 | namespace implementation { |
| 30 | |
| 31 | void BluetoothAddress::bytes_to_string(const uint8_t* addr, char* addr_str) { |
| 32 | sprintf(addr_str, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], |
| 33 | addr[3], addr[4], addr[5]); |
| 34 | } |
| 35 | |
| 36 | bool BluetoothAddress::string_to_bytes(const char* addr_str, uint8_t* addr) { |
| 37 | if (addr_str == NULL) return false; |
| 38 | if (strnlen(addr_str, kStringLength) != kStringLength) return false; |
| 39 | unsigned char trailing_char = '\0'; |
| 40 | |
| 41 | return (sscanf(addr_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx%1c", |
| 42 | &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5], |
| 43 | &trailing_char) == kBytes); |
| 44 | } |
| 45 | |
| 46 | bool BluetoothAddress::get_local_address(uint8_t* local_addr) { |
| 47 | char property[PROPERTY_VALUE_MAX] = {0}; |
| 48 | bool valid_bda = false; |
| 49 | |
| 50 | // Get local bdaddr storage path from a system property. |
| 51 | if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) { |
| 52 | int addr_fd; |
| 53 | |
| 54 | ALOGD("%s: Trying %s", __func__, property); |
| 55 | |
| 56 | addr_fd = open(property, O_RDONLY); |
| 57 | if (addr_fd != -1) { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 58 | char address[kStringLength + 1] = {0}; |
| 59 | int bytes_read = read(addr_fd, address, kStringLength); |
| 60 | if (bytes_read == -1) { |
| 61 | ALOGE("%s: Error reading address from %s: %s", __func__, property, |
| 62 | strerror(errno)); |
| 63 | } |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 64 | close(addr_fd); |
| 65 | |
| 66 | // Null terminate the string. |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 67 | address[kStringLength] = '\0'; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 68 | |
| 69 | // If the address is not all zeros, then use it. |
| 70 | const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0}; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 71 | if ((string_to_bytes(address, local_addr)) && |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 72 | (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) { |
| 73 | valid_bda = true; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 74 | ALOGD("%s: Got Factory BDA %s", __func__, address); |
| 75 | } else { |
| 76 | ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property); |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // No BDADDR found in the file. Look for BDA in a factory property. |
| 82 | if (!valid_bda && property_get(FACTORY_BDADDR_PROPERTY, property, NULL) && |
| 83 | string_to_bytes(property, local_addr)) { |
| 84 | valid_bda = true; |
| 85 | } |
| 86 | |
| 87 | // No factory BDADDR found. Look for a previously stored BDA. |
| 88 | if (!valid_bda && property_get(PERSIST_BDADDR_PROPERTY, property, NULL) && |
| 89 | string_to_bytes(property, local_addr)) { |
| 90 | valid_bda = true; |
| 91 | } |
| 92 | |
Myles Watson | 11f10ba | 2017-02-03 15:17:54 -0800 | [diff] [blame] | 93 | /* Generate new BDA if necessary */ |
| 94 | if (!valid_bda) { |
| 95 | char bdstr[kStringLength + 1]; |
| 96 | |
| 97 | /* No autogen BDA. Generate one now. */ |
| 98 | local_addr[0] = 0x22; |
| 99 | local_addr[1] = 0x22; |
| 100 | local_addr[2] = (uint8_t)rand(); |
| 101 | local_addr[3] = (uint8_t)rand(); |
| 102 | local_addr[4] = (uint8_t)rand(); |
| 103 | local_addr[5] = (uint8_t)rand(); |
| 104 | |
| 105 | /* Convert to ascii, and store as a persistent property */ |
| 106 | bytes_to_string(local_addr, bdstr); |
| 107 | |
| 108 | ALOGE("%s: No preset BDA! Generating BDA: %s for prop %s", __func__, |
| 109 | (char*)bdstr, PERSIST_BDADDR_PROPERTY); |
| 110 | ALOGE("%s: This is a bug in the platform! Please fix!", __func__); |
| 111 | |
| 112 | if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) { |
| 113 | ALOGE("%s: Failed to set random BDA in prop %s", __func__, |
| 114 | PERSIST_BDADDR_PROPERTY); |
| 115 | valid_bda = false; |
| 116 | } else { |
| 117 | valid_bda = true; |
| 118 | } |
| 119 | } |
| 120 | |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 121 | return valid_bda; |
| 122 | } |
| 123 | |
| 124 | } // namespace implementation |
| 125 | } // namespace V1_0 |
| 126 | } // namespace bluetooth |
| 127 | } // namespace hardware |
| 128 | } // namespace android |