Loading bluetooth/1.0/default/Android.bp +21 −5 Original line number Diff line number Diff line Loading @@ -19,18 +19,34 @@ cc_library_shared { srcs: [ "async_fd_watcher.cc", "bluetooth_hci.cc", "bluetooth_address.cc", "vendor_interface.cc", ], shared_libs: [ "liblog", "android.hardware.bluetooth@1.0", "libbase", "libcutils", "libhardware", "libhwbinder", "libbase", "libcutils", "libutils", "libhidlbase", "libhidltransport", "android.hardware.bluetooth@1.0", "liblog", "libutils", ], } cc_test_host { name: "bluetooth-vendor-interface-unit-tests", srcs: [ "bluetooth_address.cc", "test/bluetooth_address_test.cc", "test/properties.cc", ], local_include_dirs: [ "test", ], shared_libs: [ "libbase", "liblog", ], } bluetooth/1.0/default/bluetooth_address.cc 0 → 100644 +93 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include "bluetooth_address.h" #include <android-base/logging.h> #include <cutils/properties.h> #include <fcntl.h> #include <utils/Log.h> namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { void BluetoothAddress::bytes_to_string(const uint8_t* addr, char* addr_str) { sprintf(addr_str, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); } bool BluetoothAddress::string_to_bytes(const char* addr_str, uint8_t* addr) { if (addr_str == NULL) return false; if (strnlen(addr_str, kStringLength) != kStringLength) return false; unsigned char trailing_char = '\0'; return (sscanf(addr_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx%1c", &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5], &trailing_char) == kBytes); } bool BluetoothAddress::get_local_address(uint8_t* local_addr) { char property[PROPERTY_VALUE_MAX] = {0}; bool valid_bda = false; // Get local bdaddr storage path from a system property. if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) { int addr_fd; ALOGD("%s: Trying %s", __func__, property); addr_fd = open(property, O_RDONLY); if (addr_fd != -1) { int bytes_read = read(addr_fd, property, kStringLength); CHECK(bytes_read == kStringLength); close(addr_fd); // Null terminate the string. property[kStringLength] = '\0'; // If the address is not all zeros, then use it. const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0}; if ((string_to_bytes(property, local_addr)) && (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) { valid_bda = true; ALOGD("%s: Got Factory BDA %s", __func__, property); } } } // No BDADDR found in the file. Look for BDA in a factory property. if (!valid_bda && property_get(FACTORY_BDADDR_PROPERTY, property, NULL) && string_to_bytes(property, local_addr)) { valid_bda = true; } // No factory BDADDR found. Look for a previously stored BDA. if (!valid_bda && property_get(PERSIST_BDADDR_PROPERTY, property, NULL) && string_to_bytes(property, local_addr)) { valid_bda = true; } return valid_bda; } } // namespace implementation } // namespace V1_0 } // namespace bluetooth } // namespace hardware } // namespace android bluetooth/1.0/default/bluetooth_address.h 0 → 100644 +61 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #pragma once #include <fcntl.h> #include <cstdint> #include <string> #include <vector> namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { // The property key stores the storage location of Bluetooth Device Address static constexpr char PROPERTY_BT_BDADDR_PATH[] = "ro.bt.bdaddr_path"; // Check for a legacy address stored as a property. static constexpr char PERSIST_BDADDR_PROPERTY[] = "persist.service.bdroid.bdaddr"; // If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH and there // is no available persistent bdaddr available from PERSIST_BDADDR_PROPERTY, // use a factory set address. static constexpr char FACTORY_BDADDR_PROPERTY[] = "ro.boot.btmacaddr"; // Encapsulate handling for Bluetooth Addresses: class BluetoothAddress { public: // Conversion constants static constexpr size_t kStringLength = sizeof("XX:XX:XX:XX:XX:XX") - 1; static constexpr size_t kBytes = (kStringLength + 1) / 3; static void bytes_to_string(const uint8_t* addr, char* addr_str); static bool string_to_bytes(const char* addr_str, uint8_t* addr); static bool get_local_address(uint8_t* addr); }; } // namespace implementation } // namespace V1_0 } // namespace bluetooth } // namespace hardware } // namespace android bluetooth/1.0/default/test/bluetooth_address_test.cc 0 → 100644 +246 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include <cutils/properties.h> #include <fcntl.h> #include <gtest/gtest.h> #include <string> #include <vector> using std::vector; #include "bluetooth_address.h" namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { constexpr char kTestAddr1[BluetoothAddress::kStringLength + 1] = "12:34:56:78:9a:bc"; constexpr uint8_t kTestAddr1_bytes[BluetoothAddress::kBytes] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}; constexpr char kZeros[BluetoothAddress::kStringLength + 1] = "00:00:00:00:00:00"; constexpr uint8_t kZeros_bytes[BluetoothAddress::kBytes] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; constexpr char kTestAddrBad1[BluetoothAddress::kStringLength + 1] = "bb:aa:dd:00:00:01"; constexpr uint8_t kTestAddrBad1_bytes[BluetoothAddress::kBytes] = { 0xbb, 0xaa, 0xdd, 0x00, 0x00, 0x01}; constexpr char kAddrPath[] = "/tmp/my_address_in_a_file.txt"; class BluetoothAddressTest : public ::testing::Test { public: BluetoothAddressTest() {} ~BluetoothAddressTest() {} void FileWriteString(const char* path, const char* string); }; void BluetoothAddressTest::FileWriteString(const char* path, const char* string) { int fd = open(path, O_CREAT | O_RDWR); EXPECT_TRUE(fd > 0) << "err = " << strerror(errno); size_t length = strlen(string); size_t bytes_written = write(fd, string, length); EXPECT_EQ(length, bytes_written) << strerror(errno); close(fd); } TEST_F(BluetoothAddressTest, string_to_bytes) { uint8_t addr[BluetoothAddress::kBytes]; // Malformed addresses EXPECT_FALSE(BluetoothAddress::string_to_bytes("", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("000000000000", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:0000", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0;", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("aB:cD:eF:Gh:iJ:Kl", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:000:00:00:0;", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:12;", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:123", addr)); // Reasonable addresses EXPECT_TRUE(BluetoothAddress::string_to_bytes("00:00:00:00:00:00", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("a5:a5:a5:a5:a5:a5", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("5A:5A:5A:5A:5A:5A", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("AA:BB:CC:DD:EE:FF", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("aa:bb:cc:dd:ee:ff", addr)); // Compare the output to known bytes uint8_t addrA[BluetoothAddress::kBytes]; uint8_t addrB[BluetoothAddress::kBytes]; // kTestAddr1 EXPECT_TRUE(BluetoothAddress::string_to_bytes(kTestAddr1, addrA)); EXPECT_TRUE(memcmp(addrA, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); // kZeros EXPECT_TRUE(BluetoothAddress::string_to_bytes(kZeros, addrB)); EXPECT_TRUE(memcmp(addrB, kZeros_bytes, BluetoothAddress::kBytes) == 0); // kTestAddr1 != kZeros EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kBytes) == 0); } TEST_F(BluetoothAddressTest, bytes_to_string) { char addrA[BluetoothAddress::kStringLength + 1] = ""; char addrB[BluetoothAddress::kStringLength + 1] = ""; // kTestAddr1 BluetoothAddress::bytes_to_string(kTestAddr1_bytes, addrA); EXPECT_TRUE(memcmp(addrA, kTestAddr1, BluetoothAddress::kStringLength) == 0); // kZeros BluetoothAddress::bytes_to_string(kZeros_bytes, addrB); EXPECT_TRUE(memcmp(addrB, kZeros, BluetoothAddress::kStringLength) == 0); // kTestAddr1 != kZeros EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kStringLength) == 0); } TEST_F(BluetoothAddressTest, property_set) { // Set the properties to empty strings. property_set(PERSIST_BDADDR_PROPERTY, ""); property_set(PROPERTY_BT_BDADDR_PATH, ""); property_set(FACTORY_BDADDR_PROPERTY, ""); // Get returns 0. char prop[PROP_VALUE_MAX] = ""; EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 0); // Set the properties to known strings. property_set(PERSIST_BDADDR_PROPERTY, "1"); property_set(PROPERTY_BT_BDADDR_PATH, "22"); property_set(FACTORY_BDADDR_PROPERTY, "333"); // Get returns the correct length. EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 1); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 2); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 3); // Set the properties to empty strings again. property_set(PERSIST_BDADDR_PROPERTY, ""); property_set(PROPERTY_BT_BDADDR_PATH, ""); property_set(FACTORY_BDADDR_PROPERTY, ""); // Get returns 0. EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 0); } TEST_F(BluetoothAddressTest, property_get) { // Set the properties to known strings. property_set(PERSIST_BDADDR_PROPERTY, PERSIST_BDADDR_PROPERTY); property_set(PROPERTY_BT_BDADDR_PATH, PROPERTY_BT_BDADDR_PATH); property_set(FACTORY_BDADDR_PROPERTY, FACTORY_BDADDR_PROPERTY); // Get returns the same strings. char prop[PROP_VALUE_MAX] = ""; EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(PERSIST_BDADDR_PROPERTY, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(PROPERTY_BT_BDADDR_PATH, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); // Set a property to a different known string. char prop2[PROP_VALUE_MAX] = "Erased"; property_set(PERSIST_BDADDR_PROPERTY, prop2); // Get returns the correct strings. EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(PROPERTY_BT_BDADDR_PATH, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); // Set another property to prop2. property_set(PROPERTY_BT_BDADDR_PATH, prop2); EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); // Set the third property to prop2. property_set(FACTORY_BDADDR_PROPERTY, prop2); EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); } TEST_F(BluetoothAddressTest, get_local_address) { EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, "") == 0); EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, "") == 0); uint8_t address[BluetoothAddress::kBytes]; // File contains a non-zero Address. FileWriteString(kAddrPath, kTestAddr1); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); // File contains a zero address. FileWriteString(kAddrPath, kZeros); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); EXPECT_FALSE(BluetoothAddress::get_local_address(address)); // Factory property contains an address. EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0); EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, kTestAddr1) == 0); EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); // Persistent property contains an address. memcpy(address, kTestAddrBad1_bytes, BluetoothAddress::kBytes); EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddr1) == 0); EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, "") == 0); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, "") == 0); EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); } } // namespace implementation } // namespace V1_0 } // namespace bluetooth } // namespace hardware } // namespace android bluetooth/1.0/default/test/properties.cc 0 → 100644 +79 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #define LOG_TAG "properties" #include <ctype.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <cutils/properties.h> #include <utils/Log.h> static const int MAX_PROPERTIES = 5; struct property { char key[PROP_KEY_MAX + 2]; char value[PROP_VALUE_MAX + 2]; }; int num_properties = 0; struct property properties[MAX_PROPERTIES]; // Find the correct entry. static int property_find(const char *key) { for (int i = 0; i < num_properties; i++) { if (strncmp(properties[i].key, key, PROP_KEY_MAX) == 0) { return i; } } return MAX_PROPERTIES; } int property_set(const char *key, const char *value) { if (strnlen(value, PROP_VALUE_MAX) > PROP_VALUE_MAX) return -1; // Check to see if the property exists. int prop_index = property_find(key); if (prop_index == MAX_PROPERTIES) { if (num_properties >= MAX_PROPERTIES) return -1; prop_index = num_properties; num_properties += 1; } // This is test code. Be nice and don't push the boundary cases! strncpy(properties[prop_index].key, key, PROP_KEY_MAX + 1); strncpy(properties[prop_index].value, value, PROP_VALUE_MAX + 1); return 0; } int property_get(const char *key, char *value, const char *default_value) { // This doesn't mock the behavior of default value if (default_value != NULL) ALOGE("%s: default_value is ignored!", __func__); // Check to see if the property exists. int prop_index = property_find(key); if (prop_index == MAX_PROPERTIES) return 0; int len = strlen(properties[prop_index].value); memcpy(value, properties[prop_index].value, len); value[len] = '\0'; return len; } Loading
bluetooth/1.0/default/Android.bp +21 −5 Original line number Diff line number Diff line Loading @@ -19,18 +19,34 @@ cc_library_shared { srcs: [ "async_fd_watcher.cc", "bluetooth_hci.cc", "bluetooth_address.cc", "vendor_interface.cc", ], shared_libs: [ "liblog", "android.hardware.bluetooth@1.0", "libbase", "libcutils", "libhardware", "libhwbinder", "libbase", "libcutils", "libutils", "libhidlbase", "libhidltransport", "android.hardware.bluetooth@1.0", "liblog", "libutils", ], } cc_test_host { name: "bluetooth-vendor-interface-unit-tests", srcs: [ "bluetooth_address.cc", "test/bluetooth_address_test.cc", "test/properties.cc", ], local_include_dirs: [ "test", ], shared_libs: [ "libbase", "liblog", ], }
bluetooth/1.0/default/bluetooth_address.cc 0 → 100644 +93 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include "bluetooth_address.h" #include <android-base/logging.h> #include <cutils/properties.h> #include <fcntl.h> #include <utils/Log.h> namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { void BluetoothAddress::bytes_to_string(const uint8_t* addr, char* addr_str) { sprintf(addr_str, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); } bool BluetoothAddress::string_to_bytes(const char* addr_str, uint8_t* addr) { if (addr_str == NULL) return false; if (strnlen(addr_str, kStringLength) != kStringLength) return false; unsigned char trailing_char = '\0'; return (sscanf(addr_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx%1c", &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5], &trailing_char) == kBytes); } bool BluetoothAddress::get_local_address(uint8_t* local_addr) { char property[PROPERTY_VALUE_MAX] = {0}; bool valid_bda = false; // Get local bdaddr storage path from a system property. if (property_get(PROPERTY_BT_BDADDR_PATH, property, NULL)) { int addr_fd; ALOGD("%s: Trying %s", __func__, property); addr_fd = open(property, O_RDONLY); if (addr_fd != -1) { int bytes_read = read(addr_fd, property, kStringLength); CHECK(bytes_read == kStringLength); close(addr_fd); // Null terminate the string. property[kStringLength] = '\0'; // If the address is not all zeros, then use it. const uint8_t zero_bdaddr[kBytes] = {0, 0, 0, 0, 0, 0}; if ((string_to_bytes(property, local_addr)) && (memcmp(local_addr, zero_bdaddr, kBytes) != 0)) { valid_bda = true; ALOGD("%s: Got Factory BDA %s", __func__, property); } } } // No BDADDR found in the file. Look for BDA in a factory property. if (!valid_bda && property_get(FACTORY_BDADDR_PROPERTY, property, NULL) && string_to_bytes(property, local_addr)) { valid_bda = true; } // No factory BDADDR found. Look for a previously stored BDA. if (!valid_bda && property_get(PERSIST_BDADDR_PROPERTY, property, NULL) && string_to_bytes(property, local_addr)) { valid_bda = true; } return valid_bda; } } // namespace implementation } // namespace V1_0 } // namespace bluetooth } // namespace hardware } // namespace android
bluetooth/1.0/default/bluetooth_address.h 0 → 100644 +61 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #pragma once #include <fcntl.h> #include <cstdint> #include <string> #include <vector> namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { // The property key stores the storage location of Bluetooth Device Address static constexpr char PROPERTY_BT_BDADDR_PATH[] = "ro.bt.bdaddr_path"; // Check for a legacy address stored as a property. static constexpr char PERSIST_BDADDR_PROPERTY[] = "persist.service.bdroid.bdaddr"; // If there is no valid bdaddr available from PROPERTY_BT_BDADDR_PATH and there // is no available persistent bdaddr available from PERSIST_BDADDR_PROPERTY, // use a factory set address. static constexpr char FACTORY_BDADDR_PROPERTY[] = "ro.boot.btmacaddr"; // Encapsulate handling for Bluetooth Addresses: class BluetoothAddress { public: // Conversion constants static constexpr size_t kStringLength = sizeof("XX:XX:XX:XX:XX:XX") - 1; static constexpr size_t kBytes = (kStringLength + 1) / 3; static void bytes_to_string(const uint8_t* addr, char* addr_str); static bool string_to_bytes(const char* addr_str, uint8_t* addr); static bool get_local_address(uint8_t* addr); }; } // namespace implementation } // namespace V1_0 } // namespace bluetooth } // namespace hardware } // namespace android
bluetooth/1.0/default/test/bluetooth_address_test.cc 0 → 100644 +246 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include <cutils/properties.h> #include <fcntl.h> #include <gtest/gtest.h> #include <string> #include <vector> using std::vector; #include "bluetooth_address.h" namespace android { namespace hardware { namespace bluetooth { namespace V1_0 { namespace implementation { constexpr char kTestAddr1[BluetoothAddress::kStringLength + 1] = "12:34:56:78:9a:bc"; constexpr uint8_t kTestAddr1_bytes[BluetoothAddress::kBytes] = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}; constexpr char kZeros[BluetoothAddress::kStringLength + 1] = "00:00:00:00:00:00"; constexpr uint8_t kZeros_bytes[BluetoothAddress::kBytes] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; constexpr char kTestAddrBad1[BluetoothAddress::kStringLength + 1] = "bb:aa:dd:00:00:01"; constexpr uint8_t kTestAddrBad1_bytes[BluetoothAddress::kBytes] = { 0xbb, 0xaa, 0xdd, 0x00, 0x00, 0x01}; constexpr char kAddrPath[] = "/tmp/my_address_in_a_file.txt"; class BluetoothAddressTest : public ::testing::Test { public: BluetoothAddressTest() {} ~BluetoothAddressTest() {} void FileWriteString(const char* path, const char* string); }; void BluetoothAddressTest::FileWriteString(const char* path, const char* string) { int fd = open(path, O_CREAT | O_RDWR); EXPECT_TRUE(fd > 0) << "err = " << strerror(errno); size_t length = strlen(string); size_t bytes_written = write(fd, string, length); EXPECT_EQ(length, bytes_written) << strerror(errno); close(fd); } TEST_F(BluetoothAddressTest, string_to_bytes) { uint8_t addr[BluetoothAddress::kBytes]; // Malformed addresses EXPECT_FALSE(BluetoothAddress::string_to_bytes("", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("000000000000", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:0000", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:00:00:00:0;", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("aB:cD:eF:Gh:iJ:Kl", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("00:00:000:00:00:0;", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:12;", addr)); EXPECT_FALSE(BluetoothAddress::string_to_bytes("12:34:56:78:90:123", addr)); // Reasonable addresses EXPECT_TRUE(BluetoothAddress::string_to_bytes("00:00:00:00:00:00", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("a5:a5:a5:a5:a5:a5", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("5A:5A:5A:5A:5A:5A", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("AA:BB:CC:DD:EE:FF", addr)); EXPECT_TRUE(BluetoothAddress::string_to_bytes("aa:bb:cc:dd:ee:ff", addr)); // Compare the output to known bytes uint8_t addrA[BluetoothAddress::kBytes]; uint8_t addrB[BluetoothAddress::kBytes]; // kTestAddr1 EXPECT_TRUE(BluetoothAddress::string_to_bytes(kTestAddr1, addrA)); EXPECT_TRUE(memcmp(addrA, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); // kZeros EXPECT_TRUE(BluetoothAddress::string_to_bytes(kZeros, addrB)); EXPECT_TRUE(memcmp(addrB, kZeros_bytes, BluetoothAddress::kBytes) == 0); // kTestAddr1 != kZeros EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kBytes) == 0); } TEST_F(BluetoothAddressTest, bytes_to_string) { char addrA[BluetoothAddress::kStringLength + 1] = ""; char addrB[BluetoothAddress::kStringLength + 1] = ""; // kTestAddr1 BluetoothAddress::bytes_to_string(kTestAddr1_bytes, addrA); EXPECT_TRUE(memcmp(addrA, kTestAddr1, BluetoothAddress::kStringLength) == 0); // kZeros BluetoothAddress::bytes_to_string(kZeros_bytes, addrB); EXPECT_TRUE(memcmp(addrB, kZeros, BluetoothAddress::kStringLength) == 0); // kTestAddr1 != kZeros EXPECT_FALSE(memcmp(addrA, addrB, BluetoothAddress::kStringLength) == 0); } TEST_F(BluetoothAddressTest, property_set) { // Set the properties to empty strings. property_set(PERSIST_BDADDR_PROPERTY, ""); property_set(PROPERTY_BT_BDADDR_PATH, ""); property_set(FACTORY_BDADDR_PROPERTY, ""); // Get returns 0. char prop[PROP_VALUE_MAX] = ""; EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 0); // Set the properties to known strings. property_set(PERSIST_BDADDR_PROPERTY, "1"); property_set(PROPERTY_BT_BDADDR_PATH, "22"); property_set(FACTORY_BDADDR_PROPERTY, "333"); // Get returns the correct length. EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 1); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 2); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 3); // Set the properties to empty strings again. property_set(PERSIST_BDADDR_PROPERTY, ""); property_set(PROPERTY_BT_BDADDR_PATH, ""); property_set(FACTORY_BDADDR_PROPERTY, ""); // Get returns 0. EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) == 0); } TEST_F(BluetoothAddressTest, property_get) { // Set the properties to known strings. property_set(PERSIST_BDADDR_PROPERTY, PERSIST_BDADDR_PROPERTY); property_set(PROPERTY_BT_BDADDR_PATH, PROPERTY_BT_BDADDR_PATH); property_set(FACTORY_BDADDR_PROPERTY, FACTORY_BDADDR_PROPERTY); // Get returns the same strings. char prop[PROP_VALUE_MAX] = ""; EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(PERSIST_BDADDR_PROPERTY, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(PROPERTY_BT_BDADDR_PATH, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); // Set a property to a different known string. char prop2[PROP_VALUE_MAX] = "Erased"; property_set(PERSIST_BDADDR_PROPERTY, prop2); // Get returns the correct strings. EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(PROPERTY_BT_BDADDR_PATH, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); // Set another property to prop2. property_set(PROPERTY_BT_BDADDR_PATH, prop2); EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(FACTORY_BDADDR_PROPERTY, prop) == 0); // Set the third property to prop2. property_set(FACTORY_BDADDR_PROPERTY, prop2); EXPECT_TRUE(property_get(PERSIST_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(PROPERTY_BT_BDADDR_PATH, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); EXPECT_TRUE(property_get(FACTORY_BDADDR_PROPERTY, prop, NULL) > 0); EXPECT_TRUE(strcmp(prop2, prop) == 0); } TEST_F(BluetoothAddressTest, get_local_address) { EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, "") == 0); EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, "") == 0); uint8_t address[BluetoothAddress::kBytes]; // File contains a non-zero Address. FileWriteString(kAddrPath, kTestAddr1); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); // File contains a zero address. FileWriteString(kAddrPath, kZeros); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, kAddrPath) == 0); EXPECT_FALSE(BluetoothAddress::get_local_address(address)); // Factory property contains an address. EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddrBad1) == 0); EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, kTestAddr1) == 0); EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); // Persistent property contains an address. memcpy(address, kTestAddrBad1_bytes, BluetoothAddress::kBytes); EXPECT_TRUE(property_set(PERSIST_BDADDR_PROPERTY, kTestAddr1) == 0); EXPECT_TRUE(property_set(FACTORY_BDADDR_PROPERTY, "") == 0); EXPECT_TRUE(property_set(PROPERTY_BT_BDADDR_PATH, "") == 0); EXPECT_TRUE(BluetoothAddress::get_local_address(address)); EXPECT_TRUE(memcmp(address, kTestAddr1_bytes, BluetoothAddress::kBytes) == 0); } } // namespace implementation } // namespace V1_0 } // namespace bluetooth } // namespace hardware } // namespace android
bluetooth/1.0/default/test/properties.cc 0 → 100644 +79 −0 Original line number Diff line number Diff line // // Copyright 2016 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #define LOG_TAG "properties" #include <ctype.h> #include <stdbool.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <cutils/properties.h> #include <utils/Log.h> static const int MAX_PROPERTIES = 5; struct property { char key[PROP_KEY_MAX + 2]; char value[PROP_VALUE_MAX + 2]; }; int num_properties = 0; struct property properties[MAX_PROPERTIES]; // Find the correct entry. static int property_find(const char *key) { for (int i = 0; i < num_properties; i++) { if (strncmp(properties[i].key, key, PROP_KEY_MAX) == 0) { return i; } } return MAX_PROPERTIES; } int property_set(const char *key, const char *value) { if (strnlen(value, PROP_VALUE_MAX) > PROP_VALUE_MAX) return -1; // Check to see if the property exists. int prop_index = property_find(key); if (prop_index == MAX_PROPERTIES) { if (num_properties >= MAX_PROPERTIES) return -1; prop_index = num_properties; num_properties += 1; } // This is test code. Be nice and don't push the boundary cases! strncpy(properties[prop_index].key, key, PROP_KEY_MAX + 1); strncpy(properties[prop_index].value, value, PROP_VALUE_MAX + 1); return 0; } int property_get(const char *key, char *value, const char *default_value) { // This doesn't mock the behavior of default value if (default_value != NULL) ALOGE("%s: default_value is ignored!", __func__); // Check to see if the property exists. int prop_index = property_find(key); if (prop_index == MAX_PROPERTIES) return 0; int len = strlen(properties[prop_index].value); memcpy(value, properties[prop_index].value, len); value[len] = '\0'; return len; }