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