blob: fd53e78ac7f550aa429a45723a16620df57b52c3 [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};
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 Watson3e272a72017-06-26 13:09:11 -070058 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 Watson6a7d6222016-10-13 15:45:02 -070064 close(addr_fd);
65
66 // Null terminate the string.
Myles Watson3e272a72017-06-26 13:09:11 -070067 address[kStringLength] = '\0';
Myles Watson6a7d6222016-10-13 15:45:02 -070068
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 Watson3e272a72017-06-26 13:09:11 -070071 if ((string_to_bytes(address, local_addr)) &&
Myles Watson6a7d6222016-10-13 15:45:02 -070072 (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) {
73 valid_bda = true;
Myles Watson3e272a72017-06-26 13:09:11 -070074 ALOGD("%s: Got Factory BDA %s", __func__, address);
75 } else {
76 ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property);
Myles Watson6a7d6222016-10-13 15:45:02 -070077 }
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 Watson11f10ba2017-02-03 15:17:54 -080093 /* 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 Watson6a7d6222016-10-13 15:45:02 -0700121 return valid_bda;
122}
123
124} // namespace implementation
125} // namespace V1_0
126} // namespace bluetooth
127} // namespace hardware
128} // namespace android