Commit d48a0e8f authored by Archie Pusaka's avatar Archie Pusaka Committed by Chromium LUCI CQ

Fix ble_scan_parser endianness when parsing UUIDs

In Bluetooth, the UUIDs are given in little endian order. Therefore,
the byte array must be reversed before it is encoded into strings.

Bug: None
Change-Id: I0aeca5a1159566b674638ce3451a33665a5382ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2561960Reviewed-by: default avatarSonny Sasaka <sonnysasaka@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@google.com>
Commit-Queue: Archie Pusaka <apusaka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#834576}
parent 8005329e
......@@ -141,7 +141,8 @@ std::string BleScanParserImpl::ParseUuid(base::span<const uint8_t> bytes,
return std::string();
}
std::string uuid = base::HexEncode(bytes.data(), bytes.size());
std::vector<uint8_t> reversed(bytes.rbegin(), bytes.rend());
std::string uuid = base::HexEncode(reversed.data(), reversed.size());
switch (format) {
case UuidFormat::kFormat16Bit:
......
......@@ -22,7 +22,7 @@ TEST(BleScanParserImplTest, ParseBadUuidLengthReturnsEmptyString) {
TEST(BleScanParserImplTest, Parse16BitUuid) {
const uint8_t kUuid16[] = {0xab, 0xcd};
const char kExpected[] = "0000ABCD-0000-1000-8000-00805F9B34FB";
const char kExpected[] = "0000CDAB-0000-1000-8000-00805F9B34FB";
std::string actual =
BleScanParserImpl::ParseUuid(kUuid16, UuidFormat::kFormat16Bit);
EXPECT_STREQ(kExpected, actual.c_str());
......@@ -30,7 +30,7 @@ TEST(BleScanParserImplTest, Parse16BitUuid) {
TEST(BleScanParserImplTest, Parse32BitUuid) {
const uint8_t kUuid32[] = {0xab, 0xcd, 0xef, 0x01};
const char kExpected[] = "ABCDEF01-0000-1000-8000-00805F9B34FB";
const char kExpected[] = "01EFCDAB-0000-1000-8000-00805F9B34FB";
std::string actual =
BleScanParserImpl::ParseUuid(kUuid32, UuidFormat::kFormat32Bit);
EXPECT_STREQ(kExpected, actual.c_str());
......@@ -39,7 +39,7 @@ TEST(BleScanParserImplTest, Parse32BitUuid) {
TEST(BleScanParserImplTest, Parse128BitUuid) {
const uint8_t kUuid128[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89};
const char kExpected[] = "ABCDEF01-2345-6789-ABCD-EF0123456789";
const char kExpected[] = "89674523-01EF-CDAB-8967-452301EFCDAB";
std::string actual =
BleScanParserImpl::ParseUuid(kUuid128, UuidFormat::kFormat128Bit);
EXPECT_STREQ(kExpected, actual.c_str());
......@@ -47,14 +47,14 @@ TEST(BleScanParserImplTest, Parse128BitUuid) {
TEST(BleScanParserImplTest, Parse16BitServiceUuids) {
std::vector<device::BluetoothUUID> expected = {
device::BluetoothUUID("0000ABCD-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("0000EF01-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00002345-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00006789-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("0000ABCD-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("0000EF01-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00002345-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00006789-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("0000CDAB-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("000001EF-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00004523-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00008967-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("0000CDAB-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("000001EF-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00004523-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("00008967-0000-1000-8000-00805F9B34FB"),
};
const uint8_t kUuids[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
......@@ -68,10 +68,10 @@ TEST(BleScanParserImplTest, Parse16BitServiceUuids) {
TEST(BleScanParserImplTest, Parse32BitServiceUuids) {
std::vector<device::BluetoothUUID> expected = {
device::BluetoothUUID("ABCDEF01-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("23456789-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("ABCDEF01-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("23456789-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("01EFCDAB-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("89674523-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("01EFCDAB-0000-1000-8000-00805F9B34FB"),
device::BluetoothUUID("89674523-0000-1000-8000-00805F9B34FB"),
};
const uint8_t kUuids[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
......@@ -85,8 +85,8 @@ TEST(BleScanParserImplTest, Parse32BitServiceUuids) {
TEST(BleScanParserImplTest, Parse128BitServiceUuids) {
std::vector<device::BluetoothUUID> expected = {
device::BluetoothUUID("ABCDEF01-2345-6789-ABCD-EF0123456789"),
device::BluetoothUUID("23456789-ABCD-EF01-ABCD-EF0123456789"),
device::BluetoothUUID("89674523-01EF-CDAB-8967-452301EFCDAB"),
device::BluetoothUUID("89674523-01EF-CDAB-01EF-CDAB89674523"),
};
const uint8_t kUuids[] = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
......@@ -123,18 +123,18 @@ TEST(BleScanParserImplTest, ParseBleAdvertisingScan) {
// Advertising flag = 0x42
0x02, 0x01, 0x42,
// 16-bit service UUIDs 0000abcd-... and 0000ef01-...
0x05, 0x02, 0xab, 0xcd, 0xef, 0x01,
0x05, 0x02, 0xcd, 0xab, 0x01, 0xef,
// TX power = 0x1b
0x02, 0x0a, 0x1b,
// 32-bit service UUIDs abcdef01-... and 23456789-...
0x09, 0x05, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
0x09, 0x05, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23,
// Local name 'Steve'
0x06, 0x08, 0x53, 0x74, 0x65, 0x76, 0x65,
// 128-bit service UUID abcdef01-2345-6789-abcd-ef0123456789
0x11, 0x06, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd,
0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
0x11, 0x06, 0x89, 0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x67,
0x45, 0x23, 0x01, 0xef, 0xcd, 0xab,
// Service data map 0000dcab-... => { 0xa1, 0xb2, 0xc3, 0xd4, 0xe5 }
0x08, 0x16, 0xdc, 0xab, 0xa1, 0xb2, 0xc3, 0xd4, 0xe5,
0x08, 0x16, 0xab, 0xdc, 0xa1, 0xb2, 0xc3, 0xd4, 0xe5,
// Manufacturer data map 0xd00d => { 0x1a, 0x2b, 0x3c, 0x4d }
0x07, 0xff, 0x0d, 0xd0, 0x1a, 0x2b, 0x3c, 0x4d};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment