Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AdvertisingDataParser.h Source File

AdvertisingDataParser.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef BLE_GAP_ADVERTISINGDATAPARSER_H
00018 #define BLE_GAP_ADVERTISINGDATAPARSER_H
00019 
00020 #include <stdint.h>
00021 #include "ble/gap/AdvertisingDataTypes.h"
00022 
00023 namespace ble {
00024 
00025 /**
00026  * @addtogroup ble
00027  * @{
00028  * @addtogroup gap
00029  * @{
00030  */
00031 
00032 /**
00033  * Parse and iterate over advertising data
00034  */
00035 class AdvertisingDataParser {
00036 
00037     enum {
00038         DATA_SIZE_INDEX = 0,
00039         TYPE_INDEX = 1,
00040         VALUE_INDEX = 2,
00041         TYPE_SIZE = 1,
00042         DATA_SIZE_SIZE = 1
00043     };
00044 
00045 public:
00046     /**
00047      * Representation of an Advertising Data element.
00048      */
00049     struct element_t {
00050         adv_data_type_t  type;
00051         mbed::Span<const uint8_t>  value;
00052     };
00053 
00054     /**
00055      * Build a parser from an array of bytes.
00056      * @param data The data to parse.
00057      */
00058     AdvertisingDataParser(mbed::Span<const uint8_t>  data) :
00059         data(data),
00060         position(0)
00061     {
00062     }
00063 
00064     /**
00065      * Return if there is advertising data element left to parse.
00066      */
00067     bool hasNext() const
00068     {
00069         if (position >= data.size()) {
00070             return false;
00071         }
00072         
00073         /* early termination of packet, no more meaningful octets */
00074         if (current_length() == 0) {
00075             return false;
00076         }
00077 
00078         if (position + current_length() >= data.size()) {
00079             return false;
00080         }
00081 
00082         return true;
00083     }
00084 
00085     /**
00086      * Return the next advertising data element.
00087      *
00088      * @note Calling this function if there is no next element is undefined
00089      * behavior.
00090      */
00091     element_t next()
00092     {
00093         element_t element = {
00094             (ble::adv_data_type_t::type) data[position + TYPE_INDEX],
00095             data.subspan(position + VALUE_INDEX, current_length() - (TYPE_SIZE))
00096         };
00097 
00098         position += (DATA_SIZE_SIZE + current_length());
00099         return element;
00100     }
00101 
00102     /**
00103      * Reset the parser.
00104      */
00105     void reset()
00106     {
00107         position = 0;
00108     }
00109 
00110 private:
00111     uint8_t current_length() const {
00112         return data[position + DATA_SIZE_INDEX];
00113     }
00114 
00115     mbed::Span<const uint8_t>  data;
00116     size_t position;
00117 };
00118 
00119 /**
00120  * @}
00121  * @}
00122  */
00123 
00124 } // namespace ble
00125 
00126 #endif //BLE_GAP_ADVERTISINGDATAPARSER_H