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  if (position + current_length() >= data.size()) {
74  return false;
75  }
76 
77  return true;
78  }
79 
80  /**
81  * Return the next advertising data element.
82  *
83  * @note Calling this function if there is no next element is undefined
84  * behavior.
85  */
87  {
88  element_t element = {
89  (ble::adv_data_type_t::type) data[position + TYPE_INDEX],
90  data.subspan(position + VALUE_INDEX, current_length() - (TYPE_SIZE))
91  };
92 
93  position += (DATA_SIZE_SIZE + current_length());
94  return element;
95  }
96 
97  /**
98  * Reset the parser.
99  */
100  void reset()
101  {
102  position = 0;
103  }
104 
105 private:
106  uint8_t current_length() const {
107  return data[position + DATA_SIZE_INDEX];
108  }
109 
111  size_t position;
112 };
113 
114 /**
115  * @}
116  * @}
117  */
118 
119 } // namespace ble
120 
121 #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.