Mistake on this page?
Report an issue in GitHub or email us
AdvertisingDataParser.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2020 ARM Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef BLE_GAP_ADVERTISINGDATAPARSER_H
20 #define BLE_GAP_ADVERTISINGDATAPARSER_H
21 
22 #include <cstdint>
23 #include "platform/Span.h"
24 
25 #include "ble/gap/AdvertisingDataTypes.h"
26 
27 namespace ble {
28 
29 /**
30  * @addtogroup ble
31  * @{
32  * @addtogroup gap
33  * @{
34  */
35 
36 /**
37  * Parse and iterate over advertising data
38  */
40 
41  enum {
42  DATA_SIZE_INDEX = 0,
43  TYPE_INDEX = 1,
44  VALUE_INDEX = 2,
45  TYPE_SIZE = 1,
46  DATA_SIZE_SIZE = 1
47  };
48 
49 public:
50  /**
51  * Representation of an Advertising Data element.
52  */
53  struct element_t {
54  adv_data_type_t type;
56  };
57 
58  /**
59  * Build a parser from an array of bytes.
60  * @param data The data to parse.
61  */
63  data(data),
64  position(0)
65  {
66  }
67 
68  /**
69  * Return if there is advertising data element left to parse.
70  */
71  bool hasNext() const
72  {
73  if (position >= (size_t) data.size()) {
74  return false;
75  }
76 
77  /* early termination of packet, no more meaningful octets */
78  if (current_length() == 0) {
79  return false;
80  }
81 
82  if (position + current_length() >= (size_t) data.size()) {
83  return false;
84  }
85 
86  return true;
87  }
88 
89  /**
90  * Return the next advertising data element.
91  *
92  * @note Calling this function if there is no next element is undefined
93  * behavior.
94  */
96  {
97  element_t element = {
98  (ble::adv_data_type_t::type) data[position + TYPE_INDEX],
99  data.subspan(position + VALUE_INDEX, current_length() - (TYPE_SIZE))
100  };
101 
102  position += (DATA_SIZE_SIZE + current_length());
103  return element;
104  }
105 
106  /**
107  * Reset the parser.
108  */
109  void reset()
110  {
111  position = 0;
112  }
113 
114 private:
115  uint8_t current_length() const {
116  return data[position + DATA_SIZE_INDEX];
117  }
118 
120  size_t position;
121 };
122 
123 /**
124  * @}
125  * @}
126  */
127 
128 } // namespace ble
129 
130 #endif //BLE_GAP_ADVERTISINGDATAPARSER_H
AdvertisingDataParser(mbed::Span< const uint8_t > data)
Build a parser from an array of bytes.
void reset()
Reset the parser.
Representation of an Advertising Data element.
bool hasNext() const
Return if there is advertising data element left to parse.
Parse and iterate over advertising data.
element_t next()
Return the next advertising data element.
Entry namespace for all BLE API definitions.
type
struct scoped enum wrapped by the class
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.