Home | History | Annotate | Download | only in default
      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 <android-base/logging.h>
     20 #include <cutils/properties.h>
     21 #include <fcntl.h>
     22 #include <utils/Log.h>
     23 
     24 namespace android {
     25 namespace hardware {
     26 namespace bluetooth {
     27 namespace V1_0 {
     28 namespace implementation {
     29 
     30 void 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 
     35 bool 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 
     45 bool BluetoothAddress::get_local_address(uint8_t* local_addr) {
     46   char property[PROPERTY_VALUE_MAX] = {0};
     47   bool valid_bda = false;
     48 
     49   // Get local bdaddr storage path from a system property.
     50   if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) {
     51     int addr_fd;
     52 
     53     ALOGD("%s: Trying %s", __func__, property);
     54 
     55     addr_fd = open(property, O_RDONLY);
     56     if (addr_fd != -1) {
     57       int bytes_read = read(addr_fd, property, kStringLength);
     58       CHECK(bytes_read == kStringLength);
     59       close(addr_fd);
     60 
     61       // Null terminate the string.
     62       property[kStringLength] = '\0';
     63 
     64       // If the address is not all zeros, then use it.
     65       const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0};
     66       if ((string_to_bytes(property, local_addr)) &&
     67           (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) {
     68         valid_bda = true;
     69         ALOGD("%s: Got Factory BDA %s", __func__, property);
     70       }
     71     }
     72   }
     73 
     74   // No BDADDR found in the file. Look for BDA in a factory property.
     75   if (!valid_bda && property_get(FACTORY_BDADDR_PROPERTY, property, NULL) &&
     76       string_to_bytes(property, local_addr)) {
     77     valid_bda = true;
     78   }
     79 
     80   // No factory BDADDR found. Look for a previously stored BDA.
     81   if (!valid_bda && property_get(PERSIST_BDADDR_PROPERTY, property, NULL) &&
     82       string_to_bytes(property, local_addr)) {
     83     valid_bda = true;
     84   }
     85 
     86   /* Generate new BDA if necessary */
     87   if (!valid_bda) {
     88     char bdstr[kStringLength + 1];
     89 
     90     /* No autogen BDA. Generate one now. */
     91     local_addr[0] = 0x22;
     92     local_addr[1] = 0x22;
     93     local_addr[2] = (uint8_t)rand();
     94     local_addr[3] = (uint8_t)rand();
     95     local_addr[4] = (uint8_t)rand();
     96     local_addr[5] = (uint8_t)rand();
     97 
     98     /* Convert to ascii, and store as a persistent property */
     99     bytes_to_string(local_addr, bdstr);
    100 
    101     ALOGE("%s: No preset BDA! Generating BDA: %s for prop %s", __func__,
    102           (char*)bdstr, PERSIST_BDADDR_PROPERTY);
    103     ALOGE("%s: This is a bug in the platform!  Please fix!", __func__);
    104 
    105     if (property_set(PERSIST_BDADDR_PROPERTY, (char*)bdstr) < 0) {
    106       ALOGE("%s: Failed to set random BDA in prop %s", __func__,
    107             PERSIST_BDADDR_PROPERTY);
    108       valid_bda = false;
    109     } else {
    110       valid_bda = true;
    111     }
    112   }
    113 
    114   return valid_bda;
    115 }
    116 
    117 }  // namespace implementation
    118 }  // namespace V1_0
    119 }  // namespace bluetooth
    120 }  // namespace hardware
    121 }  // namespace android
    122