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}; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 48 | |
| 49 | // Get local bdaddr storage path from a system property. |
| 50 | if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) { |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 51 | ALOGD("%s: Trying %s", __func__, property); |
| 52 | |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 53 | int addr_fd = open(property, O_RDONLY); |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 54 | if (addr_fd != -1) { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 55 | char address[kStringLength + 1] = {0}; |
| 56 | int bytes_read = read(addr_fd, address, kStringLength); |
| 57 | if (bytes_read == -1) { |
| 58 | ALOGE("%s: Error reading address from %s: %s", __func__, property, |
| 59 | strerror(errno)); |
| 60 | } |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 61 | close(addr_fd); |
| 62 | |
| 63 | // Null terminate the string. |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 64 | address[kStringLength] = '\0'; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 65 | |
| 66 | // If the address is not all zeros, then use it. |
| 67 | const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0}; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 68 | if ((string_to_bytes(address, local_addr)) && |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 69 | (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 70 | ALOGD("%s: Got Factory BDA %s", __func__, address); |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 71 | return true; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 72 | } else { |
| 73 | ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property); |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // No BDADDR found in the file. Look for BDA in a factory property. |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 79 | if (property_get(FACTORY_BDADDR_PROPERTY, property, NULL) && |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 80 | string_to_bytes(property, local_addr)) { |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 81 | return true; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // No factory BDADDR found. Look for a previously stored BDA. |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 85 | if (property_get(PERSIST_BDADDR_PROPERTY, property, NULL) && |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 86 | string_to_bytes(property, local_addr)) { |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 87 | return true; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Myles Watson | 8d0d6f9 | 2017-05-08 10:25:11 -0700 | [diff] [blame] | 90 | return false; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace implementation |
| 94 | } // namespace V1_0 |
| 95 | } // namespace bluetooth |
| 96 | } // namespace hardware |
| 97 | } // namespace android |