blob: 93a5469958a7df21729055039a5580d80fc7e6e3 [file] [log] [blame]
Myles Watson6a7d6222016-10-13 15:45:02 -07001//
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 Watson6a7d6222016-10-13 15:45:02 -070019#include <cutils/properties.h>
Myles Watson3e272a72017-06-26 13:09:11 -070020#include <errno.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070021#include <fcntl.h>
Jiyong Parkcb198f72017-07-10 13:27:05 +090022#include <unistd.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070023#include <utils/Log.h>
24
25namespace android {
26namespace hardware {
27namespace bluetooth {
28namespace V1_0 {
29namespace implementation {
30
31void 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
36bool 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
46bool BluetoothAddress::get_local_address(uint8_t* local_addr) {
47 char property[PROPERTY_VALUE_MAX] = {0};
Myles Watson6a7d6222016-10-13 15:45:02 -070048
49 // Get local bdaddr storage path from a system property.
50 if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) {
Myles Watson6a7d6222016-10-13 15:45:02 -070051 ALOGD("%s: Trying %s", __func__, property);
52
Myles Watson8d0d6f92017-05-08 10:25:11 -070053 int addr_fd = open(property, O_RDONLY);
Myles Watson6a7d6222016-10-13 15:45:02 -070054 if (addr_fd != -1) {
Myles Watson3e272a72017-06-26 13:09:11 -070055 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 Watson6a7d6222016-10-13 15:45:02 -070061 close(addr_fd);
62
63 // Null terminate the string.
Myles Watson3e272a72017-06-26 13:09:11 -070064 address[kStringLength] = '\0';
Myles Watson6a7d6222016-10-13 15:45:02 -070065
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 Watson3e272a72017-06-26 13:09:11 -070068 if ((string_to_bytes(address, local_addr)) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070069 (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) {
Myles Watson3e272a72017-06-26 13:09:11 -070070 ALOGD("%s: Got Factory BDA %s", __func__, address);
Myles Watson8d0d6f92017-05-08 10:25:11 -070071 return true;
Myles Watson3e272a72017-06-26 13:09:11 -070072 } else {
73 ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property);
Myles Watson6a7d6222016-10-13 15:45:02 -070074 }
75 }
76 }
77
78 // No BDADDR found in the file. Look for BDA in a factory property.
Myles Watson8d0d6f92017-05-08 10:25:11 -070079 if (property_get(FACTORY_BDADDR_PROPERTY, property, NULL) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070080 string_to_bytes(property, local_addr)) {
Myles Watson8d0d6f92017-05-08 10:25:11 -070081 return true;
Myles Watson6a7d6222016-10-13 15:45:02 -070082 }
83
84 // No factory BDADDR found. Look for a previously stored BDA.
Myles Watson8d0d6f92017-05-08 10:25:11 -070085 if (property_get(PERSIST_BDADDR_PROPERTY, property, NULL) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070086 string_to_bytes(property, local_addr)) {
Myles Watson8d0d6f92017-05-08 10:25:11 -070087 return true;
Myles Watson6a7d6222016-10-13 15:45:02 -070088 }
89
Myles Watson8d0d6f92017-05-08 10:25:11 -070090 return false;
Myles Watson6a7d6222016-10-13 15:45:02 -070091}
92
93} // namespace implementation
94} // namespace V1_0
95} // namespace bluetooth
96} // namespace hardware
97} // namespace android