From: Jakub Pawlowski Date: Fri, 14 Jul 2017 22:37:57 +0000 (-0700) Subject: Advertise data parser - allow zero padding at end of packet X-Git-Tag: android-x86-8.1-r1~29^2~2^2~10 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=efefc0bfc1ff25e46f836df01689b5b029c685fc;p=android-x86%2Fsystem-bt.git Advertise data parser - allow zero padding at end of packet Test: AdvertiseDataParserTest Bug: 63123881 Change-Id: I8be9e693de557951b1048840759b5658331e9b3b (cherry picked from commit 08b907b58b0fd8334268413f5e383c1a546f9b3a) --- diff --git a/stack/include/advertise_data_parser.h b/stack/include/advertise_data_parser.h index 24ee2b378..1c5f99f8f 100644 --- a/stack/include/advertise_data_parser.h +++ b/stack/include/advertise_data_parser.h @@ -33,8 +33,14 @@ class AdvertiseDataParser { uint8_t len = ad[position]; // A field length of 0 would be invalid as it should at least contain the - // EIR field type. - if (len == 0) return false; + // EIR field type. However, some existing devices send zero padding at the + // end of advertisement. If this is the case, treat the packet as valid. + if (len == 0) { + for (size_t i = position + 1; i < ad_len; i++) { + if (ad[i] != 0) return false; + } + return true; + } // If the length of the current field would exceed the total data length, // then the data is badly formatted. diff --git a/stack/test/ad_parser_unittest.cc b/stack/test/ad_parser_unittest.cc index 46ff3ec48..3202130a3 100644 --- a/stack/test/ad_parser_unittest.cc +++ b/stack/test/ad_parser_unittest.cc @@ -23,9 +23,9 @@ TEST(AdvertiseDataParserTest, IsValidEmpty) { const std::vector data0; EXPECT_TRUE(AdvertiseDataParser::IsValid(data0)); - // Single empty field not allowed. + // Single empty field allowed (treated as zero padding). const std::vector data1{0x00}; - EXPECT_FALSE(AdvertiseDataParser::IsValid(data1)); + EXPECT_TRUE(AdvertiseDataParser::IsValid(data1)); } TEST(AdvertiseDataParserTest, IsValidBad) { @@ -44,6 +44,16 @@ TEST(AdvertiseDataParserTest, IsValidBad) { // Two fields, second field empty. const std::vector data3{0x02, 0x02, 0x00, 0x01}; EXPECT_FALSE(AdvertiseDataParser::IsValid(data3)); + + // Non-zero padding at end of packet. + const std::vector data4{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, + 0x00, 0x00, 0xBA, 0xBA, 0x00, 0x00}; + EXPECT_FALSE(AdvertiseDataParser::IsValid(data1)); + + // Non-zero padding at end of packet. + const std::vector data5{0x03, 0x02, 0x01, 0x02, 0x02, + 0x03, 0x01, 0x00, 0xBA}; + EXPECT_FALSE(AdvertiseDataParser::IsValid(data1)); } TEST(AdvertiseDataParserTest, IsValidGood) { @@ -54,6 +64,18 @@ TEST(AdvertiseDataParserTest, IsValidGood) { // Two fields. const std::vector data1{0x03, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01}; EXPECT_TRUE(AdvertiseDataParser::IsValid(data1)); + + // Zero padding at end of packet. + const std::vector data2{0x03, 0x02, 0x01, 0x02, + 0x02, 0x03, 0x01, 0x00}; + EXPECT_TRUE(AdvertiseDataParser::IsValid(data2)); + + // zero padding at end of packet, sample data from real device + const std::vector data3{ + 0x10, 0x096, 0x85, 0x44, 0x32, 0x04, 0x74, 0x32, 0x03, 0x13, 0x93, + 0xa, 0x32, 0x39, 0x3a, 0x65, 0x32, 0x05, 0x12, 0x50, 0x00, 0x50, + 0x00, 0x02, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + EXPECT_TRUE(AdvertiseDataParser::IsValid(data3)); } TEST(AdvertiseDataParserTest, GetFieldByType) {