blob: 34df752b3c78aa810c2a9e518a3f25f7fae601b4 [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>
22#include <utils/Log.h>
23
24namespace android {
25namespace hardware {
26namespace bluetooth {
27namespace V1_0 {
28namespace implementation {
29
30void BluetoothAddress::bytes_to_string(const uint8_t* addr, char* addr_str) {
31 sprintf(addr_str, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2],
32 addr[3], addr[4], addr[5]);
33}
34
35bool BluetoothAddress::string_to_bytes(const char* addr_str, uint8_t* addr) {
36 if (addr_str == NULL) return false;
37 if (strnlen(addr_str, kStringLength) != kStringLength) return false;
38 unsigned char trailing_char = '\0';
39
40 return (sscanf(addr_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx%1c",
41 &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5],
42 &trailing_char) == kBytes);
43}
44
45bool BluetoothAddress::get_local_address(uint8_t* local_addr) {
46 char property[PROPERTY_VALUE_MAX] = {0};
Myles Watson6a7d6222016-10-13 15:45:02 -070047
48 // Get local bdaddr storage path from a system property.
49 if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) {
Myles Watson6a7d6222016-10-13 15:45:02 -070050 ALOGD("%s: Trying %s", __func__, property);
51
Myles Watson8d0d6f92017-05-08 10:25:11 -070052 int addr_fd = open(property, O_RDONLY);
Myles Watson6a7d6222016-10-13 15:45:02 -070053 if (addr_fd != -1) {
Myles Watson3e272a72017-06-26 13:09:11 -070054 char address[kStringLength + 1] = {0};
55 int bytes_read = read(addr_fd, address, kStringLength);
56 if (bytes_read == -1) {
57 ALOGE("%s: Error reading address from %s: %s", __func__, property,
58 strerror(errno));
59 }
Myles Watson6a7d6222016-10-13 15:45:02 -070060 close(addr_fd);
61
62 // Null terminate the string.
Myles Watson3e272a72017-06-26 13:09:11 -070063 address[kStringLength] = '\0';
Myles Watson6a7d6222016-10-13 15:45:02 -070064
65 // If the address is not all zeros, then use it.
66 const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0};
Myles Watson3e272a72017-06-26 13:09:11 -070067 if ((string_to_bytes(address, local_addr)) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070068 (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) {
Myles Watson3e272a72017-06-26 13:09:11 -070069 ALOGD("%s: Got Factory BDA %s", __func__, address);
Myles Watson8d0d6f92017-05-08 10:25:11 -070070 return true;
Myles Watson3e272a72017-06-26 13:09:11 -070071 } else {
72 ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property);
Myles Watson6a7d6222016-10-13 15:45:02 -070073 }
74 }
75 }
76
77 // No BDADDR found in the file. Look for BDA in a factory property.
Myles Watson8d0d6f92017-05-08 10:25:11 -070078 if (property_get(FACTORY_BDADDR_PROPERTY, property, NULL) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070079 string_to_bytes(property, local_addr)) {
Myles Watson8d0d6f92017-05-08 10:25:11 -070080 return true;
Myles Watson6a7d6222016-10-13 15:45:02 -070081 }
82
83 // No factory BDADDR found. Look for a previously stored BDA.
Myles Watson8d0d6f92017-05-08 10:25:11 -070084 if (property_get(PERSIST_BDADDR_PROPERTY, property, NULL) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070085 string_to_bytes(property, local_addr)) {
Myles Watson8d0d6f92017-05-08 10:25:11 -070086 return true;
Myles Watson6a7d6222016-10-13 15:45:02 -070087 }
88
Myles Watson8d0d6f92017-05-08 10:25:11 -070089 return false;
Myles Watson6a7d6222016-10-13 15:45:02 -070090}
91
92} // namespace implementation
93} // namespace V1_0
94} // namespace bluetooth
95} // namespace hardware
96} // namespace android