advertise_data_parser.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /******************************************************************************
  2. *
  3. * Copyright 2017 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #pragma once
  19. #include <array>
  20. #include <vector>
  21. // Scan Response data from Traxxas
  22. static constexpr std::array<uint8_t, 18> trx_quirk{
  23. {0x14, 0x09, 0x54, 0xFF, 0xFF, 0x20, 0x42, 0x4C, 0x45, 0x05, 0x12, 0xFF,
  24. 0x00, 0xE8, 0x03, 0x02, 0x0A, 0x00}};
  25. class AdvertiseDataParser {
  26. // Return true if the packet is malformed, but should be considered valid for
  27. // compatibility with already existing devices
  28. static bool MalformedPacketQuirk(const std::vector<uint8_t>& ad,
  29. size_t position) {
  30. auto data_start = ad.begin() + position;
  31. // Traxxas - bad name length
  32. if ((ad.size() - position) >= 18 &&
  33. std::equal(data_start, data_start + 3, trx_quirk.begin()) &&
  34. std::equal(data_start + 5, data_start + 11, trx_quirk.begin() + 5) &&
  35. std::equal(data_start + 12, data_start + 18, trx_quirk.begin() + 12)) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. public:
  41. static void RemoveTrailingZeros(std::vector<uint8_t>& ad) {
  42. size_t position = 0;
  43. size_t ad_len = ad.size();
  44. while (position != ad_len) {
  45. uint8_t len = ad[position];
  46. // A field length of 0 would be invalid as it should at least contain the
  47. // EIR field type. However, some existing devices send zero padding at the
  48. // end of advertisement. If this is the case, cut the zero padding from
  49. // end of the packet. Otherwise i.e. gluing scan response to advertise
  50. // data will result in data with zero padding in the middle.
  51. if (len == 0) {
  52. ad.erase(ad.begin() + position, ad.end());
  53. return;
  54. }
  55. if (position + len >= ad_len) {
  56. return;
  57. }
  58. position += len + 1;
  59. }
  60. }
  61. /**
  62. * Return true if this |ad| represent properly formatted advertising data.
  63. */
  64. static bool IsValid(const std::vector<uint8_t>& ad) {
  65. size_t position = 0;
  66. size_t ad_len = ad.size();
  67. while (position != ad_len) {
  68. uint8_t len = ad[position];
  69. // A field length of 0 would be invalid as it should at least contain the
  70. // EIR field type. However, some existing devices send zero padding at the
  71. // end of advertisement. If this is the case, treat the packet as valid.
  72. if (len == 0) {
  73. for (size_t i = position + 1; i < ad_len; i++) {
  74. if (ad[i] != 0) return false;
  75. }
  76. return true;
  77. }
  78. // If the length of the current field would exceed the total data length,
  79. // then the data is badly formatted.
  80. if (position + len >= ad_len) {
  81. if (MalformedPacketQuirk(ad, position)) return true;
  82. return false;
  83. }
  84. position += len + 1;
  85. }
  86. return true;
  87. }
  88. /**
  89. * This function returns a pointer inside the |ad| array of length |ad_len|
  90. * where a field of |type| is located, together with its length in |p_length|
  91. */
  92. static const uint8_t* GetFieldByType(const uint8_t* ad, size_t ad_len,
  93. uint8_t type, uint8_t* p_length) {
  94. size_t position = 0;
  95. while (position != ad_len) {
  96. uint8_t len = ad[position];
  97. if (len == 0) break;
  98. if (position + len >= ad_len) break;
  99. uint8_t adv_type = ad[position + 1];
  100. if (adv_type == type) {
  101. /* length doesn't include itself */
  102. *p_length = len - 1; /* minus the length of type */
  103. return ad + position + 2;
  104. }
  105. position += len + 1; /* skip the length of data */
  106. }
  107. *p_length = 0;
  108. return NULL;
  109. }
  110. /**
  111. * This function returns a pointer inside the |adv| where a field of |type| is
  112. * located, together with it' length in |p_length|
  113. */
  114. static const uint8_t* GetFieldByType(std::vector<uint8_t> const& ad,
  115. uint8_t type, uint8_t* p_length) {
  116. return GetFieldByType(ad.data(), ad.size(), type, p_length);
  117. }
  118. };