Lightly modified version of the BLE stack, that doesn't bring up a DFUService by default... as we have our own.
Fork of BLE_API by
public/GapAdvertisingData.h@403:f24e0ea73138, 2015-05-21 (annotated)
- Committer:
- finneyj
- Date:
- Thu May 21 09:34:46 2015 +0000
- Revision:
- 403:f24e0ea73138
- Parent:
- 331:10f190629734
Disabled autogenerated DFU Service
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 260:ea7f9f14cc15 | 1 | /* mbed Microcontroller Library |
rgrover1 | 260:ea7f9f14cc15 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 260:ea7f9f14cc15 | 3 | * |
rgrover1 | 260:ea7f9f14cc15 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 260:ea7f9f14cc15 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 260:ea7f9f14cc15 | 6 | * You may obtain a copy of the License at |
rgrover1 | 260:ea7f9f14cc15 | 7 | * |
rgrover1 | 260:ea7f9f14cc15 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 260:ea7f9f14cc15 | 9 | * |
rgrover1 | 260:ea7f9f14cc15 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 260:ea7f9f14cc15 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 260:ea7f9f14cc15 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 260:ea7f9f14cc15 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 260:ea7f9f14cc15 | 14 | * limitations under the License. |
rgrover1 | 260:ea7f9f14cc15 | 15 | */ |
rgrover1 | 260:ea7f9f14cc15 | 16 | |
rgrover1 | 260:ea7f9f14cc15 | 17 | #ifndef __GAP_ADVERTISING_DATA_H__ |
rgrover1 | 260:ea7f9f14cc15 | 18 | #define __GAP_ADVERTISING_DATA_H__ |
rgrover1 | 260:ea7f9f14cc15 | 19 | |
rgrover1 | 331:10f190629734 | 20 | #include <string.h> |
rgrover1 | 331:10f190629734 | 21 | |
rgrover1 | 260:ea7f9f14cc15 | 22 | #include "blecommon.h" |
rgrover1 | 260:ea7f9f14cc15 | 23 | |
rgrover1 | 260:ea7f9f14cc15 | 24 | #define GAP_ADVERTISING_DATA_MAX_PAYLOAD (31) |
rgrover1 | 260:ea7f9f14cc15 | 25 | |
rgrover1 | 260:ea7f9f14cc15 | 26 | /**************************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 27 | /*! |
rgrover1 | 260:ea7f9f14cc15 | 28 | \brief |
rgrover1 | 260:ea7f9f14cc15 | 29 | This class provides several helper functions to generate properly |
rgrover1 | 260:ea7f9f14cc15 | 30 | formatted GAP Advertising and Scan Response data payloads |
rgrover1 | 260:ea7f9f14cc15 | 31 | |
rgrover1 | 260:ea7f9f14cc15 | 32 | \note |
rgrover1 | 260:ea7f9f14cc15 | 33 | See Bluetooth Specification 4.0 (Vol. 3), Part C, Section 11 and 18 |
rgrover1 | 260:ea7f9f14cc15 | 34 | for further information on Advertising and Scan Response data. |
rgrover1 | 260:ea7f9f14cc15 | 35 | |
rgrover1 | 260:ea7f9f14cc15 | 36 | \par Advertising and Scan Response Payloads |
rgrover1 | 260:ea7f9f14cc15 | 37 | Advertising data and Scan Response data are organized around a set of |
rgrover1 | 260:ea7f9f14cc15 | 38 | data types called 'AD types' in Bluetooth 4.0 (see the Bluetooth Core |
rgrover1 | 260:ea7f9f14cc15 | 39 | Specification v4.0, Vol. 3, Part C, Sections 11 and 18). |
rgrover1 | 260:ea7f9f14cc15 | 40 | |
rgrover1 | 260:ea7f9f14cc15 | 41 | \par |
rgrover1 | 260:ea7f9f14cc15 | 42 | Each AD type has it's own standardized 'assigned number', as defined |
rgrover1 | 260:ea7f9f14cc15 | 43 | by the Bluetooth SIG: |
rgrover1 | 260:ea7f9f14cc15 | 44 | https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile |
rgrover1 | 260:ea7f9f14cc15 | 45 | |
rgrover1 | 260:ea7f9f14cc15 | 46 | \par |
rgrover1 | 260:ea7f9f14cc15 | 47 | For convenience sake, all appropriate AD types have been encapsulated |
rgrover1 | 260:ea7f9f14cc15 | 48 | into GapAdvertisingData::DataType. |
rgrover1 | 260:ea7f9f14cc15 | 49 | |
rgrover1 | 260:ea7f9f14cc15 | 50 | \par |
rgrover1 | 260:ea7f9f14cc15 | 51 | Before the AD Types and their payload (if any) can be inserted into |
rgrover1 | 260:ea7f9f14cc15 | 52 | the Advertising or Scan Response frames, they need to be formatted as |
rgrover1 | 260:ea7f9f14cc15 | 53 | follows: |
rgrover1 | 260:ea7f9f14cc15 | 54 | |
rgrover1 | 260:ea7f9f14cc15 | 55 | \li \c Record length (1 byte) |
rgrover1 | 260:ea7f9f14cc15 | 56 | \li \c AD Type (1 byte) |
rgrover1 | 260:ea7f9f14cc15 | 57 | \li \c AD payload (optional, only present if record length > 1) |
rgrover1 | 260:ea7f9f14cc15 | 58 | |
rgrover1 | 260:ea7f9f14cc15 | 59 | \par |
rgrover1 | 260:ea7f9f14cc15 | 60 | This class takes care of properly formatting the payload, performs |
rgrover1 | 260:ea7f9f14cc15 | 61 | some basic checks on the payload length, and tries to avoid common |
rgrover1 | 260:ea7f9f14cc15 | 62 | errors like adding an exclusive AD field twice in the Advertising |
rgrover1 | 260:ea7f9f14cc15 | 63 | or Scan Response payload. |
rgrover1 | 260:ea7f9f14cc15 | 64 | |
rgrover1 | 260:ea7f9f14cc15 | 65 | \par EXAMPLE |
rgrover1 | 260:ea7f9f14cc15 | 66 | |
rgrover1 | 260:ea7f9f14cc15 | 67 | \code |
rgrover1 | 260:ea7f9f14cc15 | 68 | |
rgrover1 | 260:ea7f9f14cc15 | 69 | // ToDo |
rgrover1 | 260:ea7f9f14cc15 | 70 | |
rgrover1 | 260:ea7f9f14cc15 | 71 | \endcode |
rgrover1 | 260:ea7f9f14cc15 | 72 | */ |
rgrover1 | 260:ea7f9f14cc15 | 73 | /**************************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 74 | class GapAdvertisingData |
rgrover1 | 260:ea7f9f14cc15 | 75 | { |
rgrover1 | 260:ea7f9f14cc15 | 76 | public: |
rgrover1 | 260:ea7f9f14cc15 | 77 | /**********************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 78 | /*! |
rgrover1 | 260:ea7f9f14cc15 | 79 | \brief |
rgrover1 | 260:ea7f9f14cc15 | 80 | A list of Advertising Data types commonly used by peripherals. |
rgrover1 | 260:ea7f9f14cc15 | 81 | These AD types are used to describe the capabilities of the |
rgrover1 | 260:ea7f9f14cc15 | 82 | peripheral, and get inserted inside the advertising or scan |
rgrover1 | 260:ea7f9f14cc15 | 83 | response payloads. |
rgrover1 | 260:ea7f9f14cc15 | 84 | |
rgrover1 | 260:ea7f9f14cc15 | 85 | \par Source |
rgrover1 | 260:ea7f9f14cc15 | 86 | \li \c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18 |
rgrover1 | 260:ea7f9f14cc15 | 87 | \li \c https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile |
rgrover1 | 260:ea7f9f14cc15 | 88 | */ |
rgrover1 | 260:ea7f9f14cc15 | 89 | /**********************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 90 | enum DataType { |
rgrover1 | 260:ea7f9f14cc15 | 91 | FLAGS = 0x01, /**< \ref *Flags */ |
rgrover1 | 260:ea7f9f14cc15 | 92 | INCOMPLETE_LIST_16BIT_SERVICE_IDS = 0x02, /**< Incomplete list of 16-bit Service IDs */ |
rgrover1 | 260:ea7f9f14cc15 | 93 | COMPLETE_LIST_16BIT_SERVICE_IDS = 0x03, /**< Complete list of 16-bit Service IDs */ |
rgrover1 | 260:ea7f9f14cc15 | 94 | INCOMPLETE_LIST_32BIT_SERVICE_IDS = 0x04, /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ |
rgrover1 | 260:ea7f9f14cc15 | 95 | COMPLETE_LIST_32BIT_SERVICE_IDS = 0x05, /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ |
rgrover1 | 260:ea7f9f14cc15 | 96 | INCOMPLETE_LIST_128BIT_SERVICE_IDS = 0x06, /**< Incomplete list of 128-bit Service IDs */ |
rgrover1 | 260:ea7f9f14cc15 | 97 | COMPLETE_LIST_128BIT_SERVICE_IDS = 0x07, /**< Complete list of 128-bit Service IDs */ |
rgrover1 | 260:ea7f9f14cc15 | 98 | SHORTENED_LOCAL_NAME = 0x08, /**< Shortened Local Name */ |
rgrover1 | 260:ea7f9f14cc15 | 99 | COMPLETE_LOCAL_NAME = 0x09, /**< Complete Local Name */ |
rgrover1 | 260:ea7f9f14cc15 | 100 | TX_POWER_LEVEL = 0x0A, /**< TX Power Level (in dBm) */ |
rgrover1 | 260:ea7f9f14cc15 | 101 | DEVICE_ID = 0x10, /**< Device ID */ |
rgrover1 | 260:ea7f9f14cc15 | 102 | SLAVE_CONNECTION_INTERVAL_RANGE = 0x12, /**< Slave Connection Interval Range */ |
rgrover1 | 260:ea7f9f14cc15 | 103 | SERVICE_DATA = 0x16, /**< Service Data */ |
rgrover1 | 260:ea7f9f14cc15 | 104 | APPEARANCE = 0x19, /**< \ref Appearance */ |
rgrover1 | 260:ea7f9f14cc15 | 105 | ADVERTISING_INTERVAL = 0x1A, /**< Advertising Interval */ |
rgrover1 | 260:ea7f9f14cc15 | 106 | MANUFACTURER_SPECIFIC_DATA = 0xFF /**< Manufacturer Specific Data */ |
rgrover1 | 260:ea7f9f14cc15 | 107 | }; |
rgrover1 | 260:ea7f9f14cc15 | 108 | |
rgrover1 | 260:ea7f9f14cc15 | 109 | /**********************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 110 | /*! |
rgrover1 | 260:ea7f9f14cc15 | 111 | \brief |
rgrover1 | 260:ea7f9f14cc15 | 112 | A list of values for the FLAGS AD Type |
rgrover1 | 260:ea7f9f14cc15 | 113 | |
rgrover1 | 260:ea7f9f14cc15 | 114 | \note |
rgrover1 | 260:ea7f9f14cc15 | 115 | You can use more than one value in the FLAGS AD Type (ex. |
rgrover1 | 260:ea7f9f14cc15 | 116 | LE_GENERAL_DISCOVERABLE and BREDR_NOT_SUPPORTED). |
rgrover1 | 260:ea7f9f14cc15 | 117 | |
rgrover1 | 260:ea7f9f14cc15 | 118 | \par Source |
rgrover1 | 260:ea7f9f14cc15 | 119 | \li \c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 18.1 |
rgrover1 | 260:ea7f9f14cc15 | 120 | */ |
rgrover1 | 260:ea7f9f14cc15 | 121 | /**********************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 122 | enum Flags { |
rgrover1 | 260:ea7f9f14cc15 | 123 | LE_LIMITED_DISCOVERABLE = 0x01, /**< *Peripheral device is discoverable for a limited period of time */ |
rgrover1 | 260:ea7f9f14cc15 | 124 | LE_GENERAL_DISCOVERABLE = 0x02, /**< Peripheral device is discoverable at any moment */ |
rgrover1 | 260:ea7f9f14cc15 | 125 | BREDR_NOT_SUPPORTED = 0x04, /**< Peripheral device is LE only */ |
rgrover1 | 260:ea7f9f14cc15 | 126 | SIMULTANEOUS_LE_BREDR_C = 0x08, /**< Not relevant - central mode only */ |
rgrover1 | 260:ea7f9f14cc15 | 127 | SIMULTANEOUS_LE_BREDR_H = 0x10 /**< Not relevant - central mode only */ |
rgrover1 | 260:ea7f9f14cc15 | 128 | }; |
rgrover1 | 260:ea7f9f14cc15 | 129 | |
rgrover1 | 260:ea7f9f14cc15 | 130 | /**********************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 131 | /*! |
rgrover1 | 260:ea7f9f14cc15 | 132 | \brief |
rgrover1 | 260:ea7f9f14cc15 | 133 | A list of values for the APPEARANCE AD Type, which describes the |
rgrover1 | 260:ea7f9f14cc15 | 134 | physical shape or appearance of the device |
rgrover1 | 260:ea7f9f14cc15 | 135 | |
rgrover1 | 260:ea7f9f14cc15 | 136 | \par Source |
rgrover1 | 260:ea7f9f14cc15 | 137 | \li \c Bluetooth Core Specification Supplement, Part A, Section 1.12 |
rgrover1 | 260:ea7f9f14cc15 | 138 | \li \c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 12.2 |
rgrover1 | 260:ea7f9f14cc15 | 139 | \li \c https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml |
rgrover1 | 260:ea7f9f14cc15 | 140 | */ |
rgrover1 | 260:ea7f9f14cc15 | 141 | /**********************************************************************/ |
rgrover1 | 260:ea7f9f14cc15 | 142 | enum Appearance { |
rgrover1 | 260:ea7f9f14cc15 | 143 | UNKNOWN = 0, /**< Unknown of unspecified appearance type */ |
rgrover1 | 260:ea7f9f14cc15 | 144 | GENERIC_PHONE = 64, /**< Generic Phone */ |
rgrover1 | 260:ea7f9f14cc15 | 145 | GENERIC_COMPUTER = 128, /**< Generic Computer */ |
rgrover1 | 260:ea7f9f14cc15 | 146 | GENERIC_WATCH = 192, /**< Generic Watch */ |
rgrover1 | 260:ea7f9f14cc15 | 147 | WATCH_SPORTS_WATCH = 193, /**< Sports Watch */ |
rgrover1 | 260:ea7f9f14cc15 | 148 | GENERIC_CLOCK = 256, /**< Generic Clock */ |
rgrover1 | 260:ea7f9f14cc15 | 149 | GENERIC_DISPLAY = 320, /**< Generic Display */ |
rgrover1 | 260:ea7f9f14cc15 | 150 | GENERIC_REMOTE_CONTROL = 384, /**< Generic Remote Control */ |
rgrover1 | 260:ea7f9f14cc15 | 151 | GENERIC_EYE_GLASSES = 448, /**< Generic Eye Glasses */ |
rgrover1 | 260:ea7f9f14cc15 | 152 | GENERIC_TAG = 512, /**< Generic Tag */ |
rgrover1 | 260:ea7f9f14cc15 | 153 | GENERIC_KEYRING = 576, /**< Generic Keyring */ |
rgrover1 | 260:ea7f9f14cc15 | 154 | GENERIC_MEDIA_PLAYER = 640, /**< Generic Media Player */ |
rgrover1 | 260:ea7f9f14cc15 | 155 | GENERIC_BARCODE_SCANNER = 704, /**< Generic Barcode Scanner */ |
rgrover1 | 260:ea7f9f14cc15 | 156 | GENERIC_THERMOMETER = 768, /**< Generic Thermometer */ |
rgrover1 | 260:ea7f9f14cc15 | 157 | THERMOMETER_EAR = 769, /**< Ear Thermometer */ |
rgrover1 | 260:ea7f9f14cc15 | 158 | GENERIC_HEART_RATE_SENSOR = 832, /**< Generic Heart Rate Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 159 | HEART_RATE_SENSOR_HEART_RATE_BELT = 833, /**< Belt Heart Rate Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 160 | GENERIC_BLOOD_PRESSURE = 896, /**< Generic Blood Pressure */ |
rgrover1 | 260:ea7f9f14cc15 | 161 | BLOOD_PRESSURE_ARM = 897, /**< Arm Blood Pressure */ |
rgrover1 | 260:ea7f9f14cc15 | 162 | BLOOD_PRESSURE_WRIST = 898, /**< Wrist Blood Pressure */ |
rgrover1 | 260:ea7f9f14cc15 | 163 | HUMAN_INTERFACE_DEVICE_HID = 960, /**< Human Interface Device (HID) */ |
rgrover1 | 260:ea7f9f14cc15 | 164 | KEYBOARD = 961, /**< Keyboard */ |
rgrover1 | 260:ea7f9f14cc15 | 165 | MOUSE = 962, /**< Mouse */ |
rgrover1 | 260:ea7f9f14cc15 | 166 | JOYSTICK = 963, /**< Joystick */ |
rgrover1 | 260:ea7f9f14cc15 | 167 | GAMEPAD = 964, /**< Gamepad */ |
rgrover1 | 260:ea7f9f14cc15 | 168 | DIGITIZER_TABLET = 965, /**< Digitizer Tablet */ |
rgrover1 | 260:ea7f9f14cc15 | 169 | CARD_READER = 966, /**< Card Read */ |
rgrover1 | 260:ea7f9f14cc15 | 170 | DIGITAL_PEN = 967, /**< Digital Pen */ |
rgrover1 | 260:ea7f9f14cc15 | 171 | BARCODE_SCANNER = 968, /**< Barcode Scanner */ |
rgrover1 | 260:ea7f9f14cc15 | 172 | GENERIC_GLUCOSE_METER = 1024, /**< Generic Glucose Meter */ |
rgrover1 | 260:ea7f9f14cc15 | 173 | GENERIC_RUNNING_WALKING_SENSOR = 1088, /**< Generic Running/Walking Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 174 | RUNNING_WALKING_SENSOR_IN_SHOE = 1089, /**< In Shoe Running/Walking Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 175 | RUNNING_WALKING_SENSOR_ON_SHOE = 1090, /**< On Shoe Running/Walking Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 176 | RUNNING_WALKING_SENSOR_ON_HIP = 1091, /**< On Hip Running/Walking Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 177 | GENERIC_CYCLING = 1152, /**< Generic Cycling */ |
rgrover1 | 260:ea7f9f14cc15 | 178 | CYCLING_CYCLING_COMPUTER = 1153, /**< Cycling Computer */ |
rgrover1 | 260:ea7f9f14cc15 | 179 | CYCLING_SPEED_SENSOR = 1154, /**< Cycling Speed Senspr */ |
rgrover1 | 260:ea7f9f14cc15 | 180 | CYCLING_CADENCE_SENSOR = 1155, /**< Cycling Cadence Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 181 | CYCLING_POWER_SENSOR = 1156, /**< Cycling Power Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 182 | CYCLING_SPEED_AND_CADENCE_SENSOR = 1157, /**< Cycling Speed and Cadence Sensor */ |
rgrover1 | 260:ea7f9f14cc15 | 183 | PULSE_OXIMETER_GENERIC = 3136, /**< Generic Pulse Oximeter */ |
rgrover1 | 260:ea7f9f14cc15 | 184 | PULSE_OXIMETER_FINGERTIP = 3137, /**< Fingertip Pulse Oximeter */ |
rgrover1 | 260:ea7f9f14cc15 | 185 | PULSE_OXIMETER_WRIST_WORN = 3138, /**< Wrist Worn Pulse Oximeter */ |
rgrover1 | 260:ea7f9f14cc15 | 186 | OUTDOOR_GENERIC = 5184, /**< Generic Outdoor */ |
rgrover1 | 260:ea7f9f14cc15 | 187 | OUTDOOR_LOCATION_DISPLAY_DEVICE = 5185, /**< Outdoor Location Display Device */ |
rgrover1 | 260:ea7f9f14cc15 | 188 | OUTDOOR_LOCATION_AND_NAVIGATION_DISPLAY_DEVICE = 5186, /**< Outdoor Location and Navigation Display Device */ |
rgrover1 | 260:ea7f9f14cc15 | 189 | OUTDOOR_LOCATION_POD = 5187, /**< Outdoor Location Pod */ |
rgrover1 | 260:ea7f9f14cc15 | 190 | OUTDOOR_LOCATION_AND_NAVIGATION_POD = 5188 /**< Outdoor Location and Navigation Pod */ |
rgrover1 | 260:ea7f9f14cc15 | 191 | }; |
rgrover1 | 260:ea7f9f14cc15 | 192 | |
rgrover1 | 331:10f190629734 | 193 | GapAdvertisingData(void) : _payload(), _payloadLen(0), _appearance(GENERIC_TAG) { |
rgrover1 | 331:10f190629734 | 194 | /* empty */ |
rgrover1 | 331:10f190629734 | 195 | } |
rgrover1 | 331:10f190629734 | 196 | |
rgrover1 | 331:10f190629734 | 197 | /** |
rgrover1 | 331:10f190629734 | 198 | * Adds advertising data based on the specified AD type (see DataType) |
rgrover1 | 331:10f190629734 | 199 | * |
rgrover1 | 331:10f190629734 | 200 | * @param advDataType The Advertising 'DataType' to add |
rgrover1 | 331:10f190629734 | 201 | * @param payload Pointer to the payload contents |
rgrover1 | 331:10f190629734 | 202 | * @param len Size of the payload in bytes |
rgrover1 | 331:10f190629734 | 203 | * |
rgrover1 | 331:10f190629734 | 204 | * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the |
rgrover1 | 331:10f190629734 | 205 | * advertising buffer to overflow, else BLE_ERROR_NONE. |
rgrover1 | 331:10f190629734 | 206 | */ |
rgrover1 | 331:10f190629734 | 207 | ble_error_t addData(DataType advDataType, const uint8_t *payload, uint8_t len) |
rgrover1 | 331:10f190629734 | 208 | { |
rgrover1 | 331:10f190629734 | 209 | /* ToDo: Check if an AD type already exists and if the existing */ |
rgrover1 | 331:10f190629734 | 210 | /* value is exclusive or not (flags, etc.) */ |
rgrover1 | 331:10f190629734 | 211 | |
rgrover1 | 331:10f190629734 | 212 | /* Make sure we don't exceed the 31 byte payload limit */ |
rgrover1 | 331:10f190629734 | 213 | if (_payloadLen + len + 2 > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { |
rgrover1 | 331:10f190629734 | 214 | return BLE_ERROR_BUFFER_OVERFLOW; |
rgrover1 | 331:10f190629734 | 215 | } |
rgrover1 | 331:10f190629734 | 216 | |
rgrover1 | 331:10f190629734 | 217 | /* Field length */ |
rgrover1 | 331:10f190629734 | 218 | memset(&_payload[_payloadLen], len + 1, 1); |
rgrover1 | 331:10f190629734 | 219 | _payloadLen++; |
rgrover1 | 331:10f190629734 | 220 | |
rgrover1 | 331:10f190629734 | 221 | /* Field ID */ |
rgrover1 | 331:10f190629734 | 222 | memset(&_payload[_payloadLen], (uint8_t)advDataType, 1); |
rgrover1 | 331:10f190629734 | 223 | _payloadLen++; |
rgrover1 | 331:10f190629734 | 224 | |
rgrover1 | 331:10f190629734 | 225 | /* Payload */ |
rgrover1 | 331:10f190629734 | 226 | memcpy(&_payload[_payloadLen], payload, len); |
rgrover1 | 331:10f190629734 | 227 | _payloadLen += len; |
rgrover1 | 331:10f190629734 | 228 | |
rgrover1 | 331:10f190629734 | 229 | return BLE_ERROR_NONE; |
rgrover1 | 331:10f190629734 | 230 | } |
rgrover1 | 331:10f190629734 | 231 | |
rgrover1 | 331:10f190629734 | 232 | /** |
rgrover1 | 331:10f190629734 | 233 | * Helper function to add APPEARANCE data to the advertising payload |
rgrover1 | 331:10f190629734 | 234 | * |
rgrover1 | 331:10f190629734 | 235 | * @param appearance |
rgrover1 | 331:10f190629734 | 236 | * The APPEARANCE value to add |
rgrover1 | 331:10f190629734 | 237 | * |
rgrover1 | 331:10f190629734 | 238 | * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the |
rgrover1 | 331:10f190629734 | 239 | * advertising buffer to overflow, else BLE_ERROR_NONE. |
rgrover1 | 331:10f190629734 | 240 | */ |
rgrover1 | 331:10f190629734 | 241 | ble_error_t addAppearance(Appearance appearance = GENERIC_TAG) { |
rgrover1 | 331:10f190629734 | 242 | _appearance = appearance; |
rgrover1 | 331:10f190629734 | 243 | return addData(GapAdvertisingData::APPEARANCE, (uint8_t *)&appearance, 2); |
rgrover1 | 331:10f190629734 | 244 | } |
rgrover1 | 260:ea7f9f14cc15 | 245 | |
rgrover1 | 331:10f190629734 | 246 | /** |
rgrover1 | 331:10f190629734 | 247 | * Helper function to add FLAGS data to the advertising payload. |
rgrover1 | 331:10f190629734 | 248 | * @param flags |
rgrover1 | 331:10f190629734 | 249 | * LE_LIMITED_DISCOVERABLE |
rgrover1 | 331:10f190629734 | 250 | * The peripheral is discoverable for a limited period of time. |
rgrover1 | 331:10f190629734 | 251 | * LE_GENERAL_DISCOVERABLE |
rgrover1 | 331:10f190629734 | 252 | * The peripheral is permanently discoverable. |
rgrover1 | 331:10f190629734 | 253 | * BREDR_NOT_SUPPORTED |
rgrover1 | 331:10f190629734 | 254 | * This peripheral is a Bluetooth Low Energy only device (no EDR support). |
rgrover1 | 331:10f190629734 | 255 | * |
rgrover1 | 331:10f190629734 | 256 | * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the |
rgrover1 | 331:10f190629734 | 257 | * advertising buffer to overflow, else BLE_ERROR_NONE. |
rgrover1 | 331:10f190629734 | 258 | */ |
rgrover1 | 331:10f190629734 | 259 | ble_error_t addFlags(uint8_t flags = LE_GENERAL_DISCOVERABLE) { |
rgrover1 | 331:10f190629734 | 260 | return addData(GapAdvertisingData::FLAGS, &flags, 1); |
rgrover1 | 331:10f190629734 | 261 | } |
rgrover1 | 331:10f190629734 | 262 | |
rgrover1 | 331:10f190629734 | 263 | /** |
rgrover1 | 331:10f190629734 | 264 | * Helper function to add TX_POWER_LEVEL data to the advertising payload |
rgrover1 | 331:10f190629734 | 265 | * |
rgrover1 | 331:10f190629734 | 266 | * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the |
rgrover1 | 331:10f190629734 | 267 | * advertising buffer to overflow, else BLE_ERROR_NONE. |
rgrover1 | 331:10f190629734 | 268 | */ |
rgrover1 | 331:10f190629734 | 269 | ble_error_t addTxPower(int8_t txPower) { |
rgrover1 | 331:10f190629734 | 270 | /* ToDo: Basic error checking to make sure txPower is in range */ |
rgrover1 | 331:10f190629734 | 271 | return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t *)&txPower, 1); |
rgrover1 | 331:10f190629734 | 272 | } |
rgrover1 | 331:10f190629734 | 273 | |
rgrover1 | 331:10f190629734 | 274 | /** |
rgrover1 | 331:10f190629734 | 275 | * Clears the payload and resets the payload length counter |
rgrover1 | 331:10f190629734 | 276 | */ |
rgrover1 | 331:10f190629734 | 277 | void clear(void) { |
rgrover1 | 331:10f190629734 | 278 | memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD); |
rgrover1 | 331:10f190629734 | 279 | _payloadLen = 0; |
rgrover1 | 331:10f190629734 | 280 | } |
rgrover1 | 331:10f190629734 | 281 | |
rgrover1 | 331:10f190629734 | 282 | /** |
rgrover1 | 331:10f190629734 | 283 | * Returns a pointer to the the current payload |
rgrover1 | 331:10f190629734 | 284 | */ |
rgrover1 | 331:10f190629734 | 285 | const uint8_t *getPayload(void) const { |
rgrover1 | 331:10f190629734 | 286 | return (_payloadLen > 0) ? _payload : NULL; |
rgrover1 | 331:10f190629734 | 287 | } |
rgrover1 | 331:10f190629734 | 288 | |
rgrover1 | 331:10f190629734 | 289 | /** |
rgrover1 | 331:10f190629734 | 290 | * Returns the current payload length (0..31 bytes) |
rgrover1 | 331:10f190629734 | 291 | */ |
rgrover1 | 331:10f190629734 | 292 | uint8_t getPayloadLen(void) const { |
rgrover1 | 331:10f190629734 | 293 | return _payloadLen; |
rgrover1 | 331:10f190629734 | 294 | } |
rgrover1 | 331:10f190629734 | 295 | |
rgrover1 | 331:10f190629734 | 296 | /** |
rgrover1 | 331:10f190629734 | 297 | * Returns the 16-bit appearance value for this device |
rgrover1 | 331:10f190629734 | 298 | */ |
rgrover1 | 331:10f190629734 | 299 | uint16_t getAppearance(void) const { |
rgrover1 | 331:10f190629734 | 300 | return (uint16_t)_appearance; |
rgrover1 | 331:10f190629734 | 301 | } |
rgrover1 | 260:ea7f9f14cc15 | 302 | |
rgrover1 | 260:ea7f9f14cc15 | 303 | private: |
rgrover1 | 260:ea7f9f14cc15 | 304 | uint8_t _payload[GAP_ADVERTISING_DATA_MAX_PAYLOAD]; |
rgrover1 | 260:ea7f9f14cc15 | 305 | uint8_t _payloadLen; |
rgrover1 | 260:ea7f9f14cc15 | 306 | uint16_t _appearance; |
rgrover1 | 260:ea7f9f14cc15 | 307 | }; |
rgrover1 | 260:ea7f9f14cc15 | 308 | |
rgrover1 | 260:ea7f9f14cc15 | 309 | #endif // ifndef __GAP_ADVERTISING_DATA_H__ |