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 
19 #include <cutils/properties.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <utils/Log.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace bluetooth {
28 namespace V1_0 {
29 namespace implementation {
30 
bytes_to_string(const uint8_t * addr,char * addr_str)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 
string_to_bytes(const char * addr_str,uint8_t * addr)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 
get_local_address(uint8_t * local_addr)46 bool BluetoothAddress::get_local_address(uint8_t* local_addr) {
47   char property[PROPERTY_VALUE_MAX] = {0};
48 
49   // Get local bdaddr storage path from a system property.
50   if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) {
51     ALOGD("%s: Trying %s", __func__, property);
52 
53     int addr_fd = open(property, O_RDONLY);
54     if (addr_fd != -1) {
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       }
61       close(addr_fd);
62 
63       // Null terminate the string.
64       address[kStringLength] = '\0';
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};
68       if ((string_to_bytes(address, local_addr)) &&
69           (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) {
70         ALOGD("%s: Got Factory BDA %s", __func__, address);
71         return true;
72       } else {
73         ALOGE("%s: Got Invalid BDA '%s' from %s", __func__, address, property);
74       }
75     }
76   }
77 
78   // No BDADDR found in the file. Look for BDA in a factory property.
79   if (property_get(FACTORY_BDADDR_PROPERTY, property, NULL) &&
80       string_to_bytes(property, local_addr)) {
81     return true;
82   }
83 
84   // No factory BDADDR found. Look for a previously stored BDA.
85   if (property_get(PERSIST_BDADDR_PROPERTY, property, NULL) &&
86       string_to_bytes(property, local_addr)) {
87     return true;
88   }
89 
90   return false;
91 }
92 
93 }  // namespace implementation
94 }  // namespace V1_0
95 }  // namespace bluetooth
96 }  // namespace hardware
97 }  // namespace android
98