Loading system/test/headless/property.h +10 −9 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ #include "include/hardware/bluetooth.h" #include "macros.h" #include "test/headless/log.h" #include "types/bluetooth/uuid.h" inline std::string bt_property_type_text(const ::bt_property_type_t type) { Loading Loading @@ -53,8 +52,10 @@ inline std::string bt_property_type_text(const ::bt_property_type_t type) { CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID); CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_MODEL_NUM); CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP); CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ADDR_TYPE); CASE_RETURN_TEXT(BT_PROPERTY_RESERVED_0x14); default: return base::StringPrintf("UNKNOWN[%d]", type); RETURN_UNKNOWN_TYPE_STRING(::bt_property_type_t, type); } } Loading Loading @@ -100,8 +101,8 @@ struct void_t : public bt_property_t { public: virtual std::string ToString() const override { return base::StringPrintf("Unimplemented property type:%d name:%s", type, bt_property_type_text(type).c_str()); return fmt::format("Unimplemented property type:{} name:{}", type, bt_property_type_text(type)); } }; Loading @@ -121,7 +122,7 @@ struct uuid_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("Number of uuids:%zu", get_uuids().size()); return fmt::format("Number of uuids:{}", get_uuids().size()); } private: Loading @@ -139,7 +140,7 @@ struct name_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("Name:%s", get_name().c_str()); return fmt::format("Name:{}", get_name()); } }; Loading @@ -157,7 +158,7 @@ struct bdaddr_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("bd_addr:%s", get_addr().ToString().c_str()); return fmt::format("bd_addr:{}", get_addr().ToString()); } }; Loading @@ -173,7 +174,7 @@ struct class_of_device_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("cod:0x%04x", get_class_of_device()); return fmt::format("cod:0x{:04x}", get_class_of_device()); } }; Loading @@ -189,7 +190,7 @@ struct type_of_device_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("tod:0x%04x", get_type_of_device()); return fmt::format("tod:0x{:04x}", get_type_of_device()); } }; Loading system/types/Android.bp +1 −2 Original line number Diff line number Diff line Loading @@ -43,7 +43,6 @@ cc_library_static { "bluetooth/uuid.cc", "raw_address.cc", ], static_libs: ["libchrome"], header_libs: ["libbluetooth-types-header"], export_header_lib_headers: ["libbluetooth-types-header"], apex_available: [ Loading @@ -56,9 +55,9 @@ cc_library_static { cc_test { name: "net_test_types", static_libs: [ "libbase", "libbluetooth-types", "libbluetooth_log", "libchrome", "libosi", // strlcpy ], test_suites: ["general-tests"], Loading system/types/bluetooth/uuid.cc +14 −6 Original line number Diff line number Diff line Loading @@ -18,10 +18,13 @@ #include "uuid.h" #include <base/rand_util.h> #include <base/strings/stringprintf.h> #include <string.h> #include <algorithm> #include <cstring> #include <iomanip> #include <ios> #include <sstream> namespace bluetooth { Loading Loading @@ -165,9 +168,14 @@ bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; } bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; } std::string Uuid::ToString() const { return base::StringPrintf( "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); std::stringstream uuid; uuid << std::hex << std::setfill('0'); for (size_t i = 0; i < 16; i++) { uuid << std::setw(2) << +uu[i]; if (i == 3 || i == 5 || i == 7 || i == 9) { uuid << "-"; } } return uuid.str(); } } // namespace bluetooth system/types/bt_transport.h +1 −4 Original line number Diff line number Diff line Loading @@ -16,8 +16,6 @@ #pragma once #include <base/strings/stringprintf.h> #include <string> #define BT_TRANSPORT_AUTO 0 Loading @@ -33,8 +31,7 @@ inline std::string bt_transport_text(const tBT_TRANSPORT& transport) { CASE_RETURN_TEXT(BT_TRANSPORT_AUTO); CASE_RETURN_TEXT(BT_TRANSPORT_BR_EDR); CASE_RETURN_TEXT(BT_TRANSPORT_LE); default: return base::StringPrintf("UNKNOWN[%hhu]", transport); } RETURN_UNKNOWN_TYPE_STRING(tBT_TRANSPORT, transport); } #endif system/types/raw_address.cc +43 −16 Original line number Diff line number Diff line Loading @@ -18,11 +18,12 @@ #include "raw_address.h" #include <base/strings/string_split.h> #include <base/strings/stringprintf.h> #include <stdint.h> #include <algorithm> #include <array> #include <iomanip> #include <sstream> #include <vector> static_assert(sizeof(RawAddress) == 6, "RawAddress must be 6 bytes long!"); Loading @@ -41,9 +42,15 @@ RawAddress::RawAddress(const std::array<uint8_t, kLength> mac) { std::string RawAddress::ToString() const { return ToColonSepHexString(); } std::string RawAddress::ToColonSepHexString() const { return base::StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", address[0], address[1], address[2], address[3], address[4], address[5]); std::stringstream addr; addr << std::hex << std::setfill('0'); for (size_t i = 0; i < 6; i++) { addr << std::setw(2) << +address[i]; if (i != 5) { addr << ":"; } } return addr.str(); } std::string RawAddress::ToStringForLogging() const { Loading @@ -54,7 +61,12 @@ std::string RawAddress::ToRedactedStringForLogging() const { if (*this == RawAddress::kAny || *this == RawAddress::kEmpty) { return ToStringForLogging(); } return base::StringPrintf("xx:xx:xx:xx:%02x:%02x", address[4], address[5]); std::stringstream addr; addr << std::hex << std::setfill('0'); addr << "xx:xx:xx:xx:"; addr << std::setw(2) << +address[4] << ":"; addr << std::setw(2) << +address[5]; return addr.str(); } std::array<uint8_t, RawAddress::kLength> RawAddress::ToArray() const { Loading @@ -67,19 +79,34 @@ bool RawAddress::FromString(const std::string& from, RawAddress& to) { RawAddress new_addr; if (from.length() != 17) return false; std::vector<std::string> byte_tokens = base::SplitString(from, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); std::istringstream stream(from); std::string token; int index = 0; while (getline(stream, token, ':')) { if (index >= 6) { return false; } if (byte_tokens.size() != 6) return false; if (token.length() != 2) { return false; } for (int i = 0; i < 6; i++) { const auto& token = byte_tokens[i]; char* temp = nullptr; new_addr.address[index] = std::strtol(token.c_str(), &temp, 16); if (temp == token.c_str()) { // string token is empty or has wrong format return false; } if (temp != (token.c_str() + token.size())) { // cannot parse whole string return false; } if (token.length() != 2) return false; index++; } char* temp = nullptr; new_addr.address[i] = strtol(token.c_str(), &temp, 16); if (*temp != '\0') return false; if (index != 6) { return false; } to = new_addr; Loading Loading
system/test/headless/property.h +10 −9 Original line number Diff line number Diff line Loading @@ -25,7 +25,6 @@ #include "include/hardware/bluetooth.h" #include "macros.h" #include "test/headless/log.h" #include "types/bluetooth/uuid.h" inline std::string bt_property_type_text(const ::bt_property_type_t type) { Loading Loading @@ -53,8 +52,10 @@ inline std::string bt_property_type_text(const ::bt_property_type_t type) { CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ASHA_TRUNCATED_HISYNCID); CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_MODEL_NUM); CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP); CASE_RETURN_TEXT(BT_PROPERTY_REMOTE_ADDR_TYPE); CASE_RETURN_TEXT(BT_PROPERTY_RESERVED_0x14); default: return base::StringPrintf("UNKNOWN[%d]", type); RETURN_UNKNOWN_TYPE_STRING(::bt_property_type_t, type); } } Loading Loading @@ -100,8 +101,8 @@ struct void_t : public bt_property_t { public: virtual std::string ToString() const override { return base::StringPrintf("Unimplemented property type:%d name:%s", type, bt_property_type_text(type).c_str()); return fmt::format("Unimplemented property type:{} name:{}", type, bt_property_type_text(type)); } }; Loading @@ -121,7 +122,7 @@ struct uuid_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("Number of uuids:%zu", get_uuids().size()); return fmt::format("Number of uuids:{}", get_uuids().size()); } private: Loading @@ -139,7 +140,7 @@ struct name_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("Name:%s", get_name().c_str()); return fmt::format("Name:{}", get_name()); } }; Loading @@ -157,7 +158,7 @@ struct bdaddr_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("bd_addr:%s", get_addr().ToString().c_str()); return fmt::format("bd_addr:{}", get_addr().ToString()); } }; Loading @@ -173,7 +174,7 @@ struct class_of_device_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("cod:0x%04x", get_class_of_device()); return fmt::format("cod:0x{:04x}", get_class_of_device()); } }; Loading @@ -189,7 +190,7 @@ struct type_of_device_t : public bt_property_t { } virtual std::string ToString() const override { return base::StringPrintf("tod:0x%04x", get_type_of_device()); return fmt::format("tod:0x{:04x}", get_type_of_device()); } }; Loading
system/types/Android.bp +1 −2 Original line number Diff line number Diff line Loading @@ -43,7 +43,6 @@ cc_library_static { "bluetooth/uuid.cc", "raw_address.cc", ], static_libs: ["libchrome"], header_libs: ["libbluetooth-types-header"], export_header_lib_headers: ["libbluetooth-types-header"], apex_available: [ Loading @@ -56,9 +55,9 @@ cc_library_static { cc_test { name: "net_test_types", static_libs: [ "libbase", "libbluetooth-types", "libbluetooth_log", "libchrome", "libosi", // strlcpy ], test_suites: ["general-tests"], Loading
system/types/bluetooth/uuid.cc +14 −6 Original line number Diff line number Diff line Loading @@ -18,10 +18,13 @@ #include "uuid.h" #include <base/rand_util.h> #include <base/strings/stringprintf.h> #include <string.h> #include <algorithm> #include <cstring> #include <iomanip> #include <ios> #include <sstream> namespace bluetooth { Loading Loading @@ -165,9 +168,14 @@ bool Uuid::operator==(const Uuid& rhs) const { return uu == rhs.uu; } bool Uuid::operator!=(const Uuid& rhs) const { return uu != rhs.uu; } std::string Uuid::ToString() const { return base::StringPrintf( "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); std::stringstream uuid; uuid << std::hex << std::setfill('0'); for (size_t i = 0; i < 16; i++) { uuid << std::setw(2) << +uu[i]; if (i == 3 || i == 5 || i == 7 || i == 9) { uuid << "-"; } } return uuid.str(); } } // namespace bluetooth
system/types/bt_transport.h +1 −4 Original line number Diff line number Diff line Loading @@ -16,8 +16,6 @@ #pragma once #include <base/strings/stringprintf.h> #include <string> #define BT_TRANSPORT_AUTO 0 Loading @@ -33,8 +31,7 @@ inline std::string bt_transport_text(const tBT_TRANSPORT& transport) { CASE_RETURN_TEXT(BT_TRANSPORT_AUTO); CASE_RETURN_TEXT(BT_TRANSPORT_BR_EDR); CASE_RETURN_TEXT(BT_TRANSPORT_LE); default: return base::StringPrintf("UNKNOWN[%hhu]", transport); } RETURN_UNKNOWN_TYPE_STRING(tBT_TRANSPORT, transport); } #endif
system/types/raw_address.cc +43 −16 Original line number Diff line number Diff line Loading @@ -18,11 +18,12 @@ #include "raw_address.h" #include <base/strings/string_split.h> #include <base/strings/stringprintf.h> #include <stdint.h> #include <algorithm> #include <array> #include <iomanip> #include <sstream> #include <vector> static_assert(sizeof(RawAddress) == 6, "RawAddress must be 6 bytes long!"); Loading @@ -41,9 +42,15 @@ RawAddress::RawAddress(const std::array<uint8_t, kLength> mac) { std::string RawAddress::ToString() const { return ToColonSepHexString(); } std::string RawAddress::ToColonSepHexString() const { return base::StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", address[0], address[1], address[2], address[3], address[4], address[5]); std::stringstream addr; addr << std::hex << std::setfill('0'); for (size_t i = 0; i < 6; i++) { addr << std::setw(2) << +address[i]; if (i != 5) { addr << ":"; } } return addr.str(); } std::string RawAddress::ToStringForLogging() const { Loading @@ -54,7 +61,12 @@ std::string RawAddress::ToRedactedStringForLogging() const { if (*this == RawAddress::kAny || *this == RawAddress::kEmpty) { return ToStringForLogging(); } return base::StringPrintf("xx:xx:xx:xx:%02x:%02x", address[4], address[5]); std::stringstream addr; addr << std::hex << std::setfill('0'); addr << "xx:xx:xx:xx:"; addr << std::setw(2) << +address[4] << ":"; addr << std::setw(2) << +address[5]; return addr.str(); } std::array<uint8_t, RawAddress::kLength> RawAddress::ToArray() const { Loading @@ -67,19 +79,34 @@ bool RawAddress::FromString(const std::string& from, RawAddress& to) { RawAddress new_addr; if (from.length() != 17) return false; std::vector<std::string> byte_tokens = base::SplitString(from, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); std::istringstream stream(from); std::string token; int index = 0; while (getline(stream, token, ':')) { if (index >= 6) { return false; } if (byte_tokens.size() != 6) return false; if (token.length() != 2) { return false; } for (int i = 0; i < 6; i++) { const auto& token = byte_tokens[i]; char* temp = nullptr; new_addr.address[index] = std::strtol(token.c_str(), &temp, 16); if (temp == token.c_str()) { // string token is empty or has wrong format return false; } if (temp != (token.c_str() + token.size())) { // cannot parse whole string return false; } if (token.length() != 2) return false; index++; } char* temp = nullptr; new_addr.address[i] = strtol(token.c_str(), &temp, 16); if (*temp != '\0') return false; if (index != 6) { return false; } to = new_addr; Loading