Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GapAdvertisingData.h Source File

GapAdvertisingData.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 MBED_GAP_ADVERTISING_DATA__LEGACY_H__
00018 #define MBED_GAP_ADVERTISING_DATA__LEGACY_H__
00019 
00020 #include <stdint.h>
00021 #include <string.h>
00022 
00023 #include "blecommon.h"
00024 
00025 /**
00026  * @addtogroup ble
00027  * @{
00028  * @addtogroup gap
00029  * @{
00030  */
00031 
00032 #define GAP_ADVERTISING_DATA_MAX_PAYLOAD        (31)
00033 
00034 /**
00035  * GAP advertising data builder.
00036  *
00037  * Advertising data are used by broadcaster or peripheral to advertise state
00038  * about the device. This class offers the function to add and update states present
00039  * in an advertisement payload.
00040  *
00041  * After construction, the advertising payload contained in the instance of
00042  * GapAdvertisingData is empty. Adding new states and named fields can be
00043  * achieved by invoking the function addData(), and updating existing state
00044  * involves calling the function updateData().
00045  *
00046  * Fields present in the payload can be retrieved by a call to the function
00047  * findField.
00048  *
00049  * This class includes shorthand for the most common fields:
00050  *   - FLAGS: addFlags().
00051  *   - APPEARANCE: addAppearance().
00052  *   - TX_POWER_LEVEL: addTxPower().
00053  *
00054  * @code
00055  *
00056  * Gap &gap;
00057  *
00058  * static const uint8_t device_name[] = "HRM";
00059  *
00060  * // construct an empty advertising payload
00061  * GapAdvertisingData advertising_data;
00062  *
00063  * // set the flags of the advertising device
00064  * advertising_data.addFlags(
00065  *    GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
00066  *    GapAdvertisingData::BREDR_NOT_SUPPORTED
00067  * );
00068  *
00069  * // set the advertised name of the device
00070  * advertising_data.addData(
00071  *     GapAdvertisingData::COMPLETE_LOCAL_NAME,
00072  *     device_name,
00073  *     sizeof(device_name)
00074  * );
00075  *
00076  * // update the advertising data of the gap payload
00077  * gap.setAdvertisingPayload(advertising_data);
00078  *
00079  * @endcode
00080  *
00081  * @note See Bluetooth Specification 4.0 (Vol. 3), Part C, Sections 11 and 18
00082  * for further information on advertising and scan response data.
00083  *
00084  * @par Advertising and Scan Response Payloads
00085  * Advertising data and scan response data are organized around a set of
00086  * data types called 'AD types' in Bluetooth 4.0 (see the Bluetooth Core
00087  * Specification v4.0, Vol. 3, Part C, Sections 11 and 18).
00088  *
00089  * @par
00090  * Each AD type has its own standardized assigned number, as
00091  * the Bluetooth SIG defines:
00092  * https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile.
00093  *
00094  * @par
00095  * For convenience, all appropriate AD types are encapsulated in
00096  * GapAdvertisingData::DataType.
00097  *
00098  * @par
00099  * Before the AD Types and their payload (if any) can be inserted into
00100  * the advertising or scan response frames, they need to be formatted as
00101  * follows:
00102  *
00103  * @li @c Record length (1 byte).
00104  * @li @c AD Type (1 byte).
00105  * @li @c AD payload (optional; only present if record length > 1).
00106  *
00107  * @par
00108  * This class takes care of properly formatting the payload, performs
00109  * some basic checks on the payload length and tries to avoid common
00110  * errors such as adding an exclusive AD field twice in the advertising
00111  * or scan response payload.
00112  *
00113  * @deprecated Use AdvertisingData instead.
00114  * This version provides the buffer backing for the advertising data
00115  * but it's only big enough for legacy advertising.
00116  */
00117 class GapAdvertisingData
00118 {
00119 public:
00120     /*!
00121      * List of standard Advertising Data types.
00122      *
00123      * These AD types are used to describe the capabilities of the peripheral
00124      * and are inserted inside the advertising or scan response payloads.
00125      *
00126      * @par Source
00127      *
00128      * @li @c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 11, 18.
00129      * @li @c https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile.
00130      */
00131     enum DataType_t  {
00132         /**
00133          * Flags, refer to GapAdvertisingData::Flags_t.
00134          */
00135         FLAGS = 0x01,
00136 
00137         /**
00138          * Incomplete list of 16-bit Service IDs.
00139          */
00140         INCOMPLETE_LIST_16BIT_SERVICE_IDS = 0x02,
00141 
00142         /**
00143          * Complete list of 16-bit Service IDs.
00144          */
00145         COMPLETE_LIST_16BIT_SERVICE_IDS = 0x03,
00146 
00147         /**
00148          * Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0).
00149          */
00150         INCOMPLETE_LIST_32BIT_SERVICE_IDS = 0x04,
00151 
00152         /**
00153          * Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0).
00154          */
00155         COMPLETE_LIST_32BIT_SERVICE_IDS = 0x05,
00156 
00157         /**
00158          * Incomplete list of 128-bit Service IDs.
00159          */
00160         INCOMPLETE_LIST_128BIT_SERVICE_IDS = 0x06,
00161 
00162         /**
00163          * Complete list of 128-bit Service IDs.
00164          */
00165         COMPLETE_LIST_128BIT_SERVICE_IDS = 0x07,
00166 
00167         /**
00168          * Shortened Local Name.
00169          */
00170         SHORTENED_LOCAL_NAME = 0x08,
00171 
00172         /**
00173          * Complete Local Name.
00174          */
00175         COMPLETE_LOCAL_NAME = 0x09,
00176 
00177         /**
00178          * TX Power Level (in dBm).
00179          */
00180         TX_POWER_LEVEL = 0x0A,
00181 
00182         /**
00183          * Device ID.
00184          */
00185         DEVICE_ID = 0x10,
00186 
00187         /**
00188          * Slave Connection Interval Range.
00189          */
00190         SLAVE_CONNECTION_INTERVAL_RANGE = 0x12,
00191 
00192         /**
00193          * List of 128-bit service UUIDs the device is looking for.
00194          */
00195         LIST_128BIT_SOLICITATION_IDS = 0x15,
00196 
00197         /**
00198          * Service Data.
00199          */
00200         SERVICE_DATA = 0x16,
00201 
00202         /**
00203          * Appearance, refer to GapAdvertisingData::Appearance_t.
00204          */
00205         APPEARANCE = 0x19,
00206 
00207         /**
00208          * Advertising Interval.
00209          */
00210         ADVERTISING_INTERVAL = 0x1A,
00211 
00212         /**
00213          * Manufacturer Specific Data.
00214          */
00215         MANUFACTURER_SPECIFIC_DATA = 0xFF
00216 
00217     };
00218 
00219     /**
00220      * Alias for GapAdvertisingData::DataType_t.
00221      *
00222      * @deprecated Future releases will drop this type alias.
00223      */
00224     typedef enum DataType_t  DataType;
00225 
00226     /**
00227      *  Enumeration of allowed flags for DataType_t::FLAGS.
00228      *
00229      *  @note DataType_t::FLAGS may contain several flags that the bitwise
00230      * and operator (ex.LE_GENERAL_DISCOVERABLE & BREDR_NOT_SUPPORTED) assembled.
00231      *
00232      *  @par Source
00233      *
00234      *  @li @c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 18.1.
00235      */
00236     enum Flags_t {
00237         /**
00238          * Peripheral device is discoverable for a limited period of time.
00239          */
00240         LE_LIMITED_DISCOVERABLE = 0x01,
00241 
00242         /**
00243          * Peripheral device is discoverable at any moment.
00244          */
00245         LE_GENERAL_DISCOVERABLE = 0x02,
00246 
00247         /**
00248          * Peripheral device is LE only and does not support Bluetooth Enhanced
00249          * DataRate.
00250          */
00251         BREDR_NOT_SUPPORTED = 0x04,
00252 
00253         /**
00254          * Not relevant - dual mode only.
00255          */
00256         SIMULTANEOUS_LE_BREDR_C = 0x08,
00257 
00258         /**
00259          * Not relevant - dual mode only.
00260          */
00261         SIMULTANEOUS_LE_BREDR_H = 0x10
00262 
00263     };
00264 
00265     /**
00266      * Alias for GapAdvertisingData::Flags_t.
00267      *
00268      * @deprecated  Future releases will drop this type alias.
00269      */
00270     typedef enum Flags_t Flags;
00271 
00272     /**
00273      *  Enumeration of values for the DataType_t::APPEARANCE.
00274      *
00275      *  These values describe the physical shape or appearance of the device.
00276      *
00277      *  @par Source
00278      *
00279      *  @li @c Bluetooth Core Specification Supplement, Part A, Section 1.12.
00280      *  @li @c Bluetooth Core Specification 4.0 (Vol. 3), Part C, Section 12.2.
00281      *  @li @c https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml.
00282      */
00283     enum Appearance_t {
00284         /**
00285          * Unknown or unspecified appearance type.
00286          */
00287         UNKNOWN = 0,
00288 
00289         /**
00290          * Generic Phone.
00291          */
00292         GENERIC_PHONE = 64,
00293 
00294         /**
00295          * Generic Computer.
00296          */
00297         GENERIC_COMPUTER = 128,
00298 
00299         /**
00300          * Generic Watch.
00301          */
00302         GENERIC_WATCH = 192,
00303 
00304         /**
00305          * Sports Watch.
00306          */
00307         WATCH_SPORTS_WATCH = 193,
00308 
00309         /**
00310          * Generic Clock.
00311          */
00312         GENERIC_CLOCK = 256,
00313 
00314         /**
00315          * Generic Display.
00316          */
00317         GENERIC_DISPLAY = 320,
00318 
00319         /**
00320          * Generic Remote Control.
00321          */
00322         GENERIC_REMOTE_CONTROL = 384,
00323 
00324         /**
00325          * Generic Eye Glasses.
00326          */
00327         GENERIC_EYE_GLASSES = 448,
00328 
00329         /**
00330          * Generic Tag.
00331          */
00332         GENERIC_TAG = 512,
00333 
00334         /**
00335          * Generic Keyring.
00336          */
00337         GENERIC_KEYRING = 576,
00338 
00339         /**
00340          * Generic Media Player.
00341          */
00342         GENERIC_MEDIA_PLAYER = 640,
00343 
00344         /**
00345          * Generic Bar Code Scanner.
00346          */
00347         GENERIC_BARCODE_SCANNER = 704,
00348 
00349         /**
00350          * Generic Thermometer.
00351          */
00352         GENERIC_THERMOMETER = 768,
00353 
00354         /**
00355          * Ear Thermometer.
00356          */
00357         THERMOMETER_EAR = 769,
00358 
00359         /**
00360          * Generic Heart Rate Sensor.
00361          */
00362         GENERIC_HEART_RATE_SENSOR = 832,
00363 
00364         /**
00365          * Belt Heart Rate Sensor.
00366          */
00367         HEART_RATE_SENSOR_HEART_RATE_BELT = 833,
00368 
00369         /**
00370          * Generic Blood Pressure.
00371          */
00372         GENERIC_BLOOD_PRESSURE = 896,
00373 
00374         /**
00375          * Arm Blood Pressure.
00376          */
00377         BLOOD_PRESSURE_ARM = 897,
00378 
00379         /**
00380          * Wrist Blood Pressure.
00381          */
00382         BLOOD_PRESSURE_WRIST = 898,
00383 
00384         /**
00385          * Human Interface Device (HID).
00386          */
00387         HUMAN_INTERFACE_DEVICE_HID = 960,
00388 
00389         /**
00390          * Keyboard.
00391          */
00392         KEYBOARD = 961,
00393 
00394         /**
00395          * Mouse.
00396          */
00397         MOUSE = 962,
00398 
00399         /**
00400          * Joystick.
00401          */
00402         JOYSTICK = 963,
00403 
00404         /**
00405          * Gamepad.
00406          */
00407         GAMEPAD = 964,
00408 
00409         /**
00410          * Digitizer Tablet.
00411          */
00412         DIGITIZER_TABLET = 965,
00413 
00414         /**
00415          * Card Reader.
00416          */
00417         CARD_READER = 966,
00418 
00419         /**
00420          * Digital Pen.
00421          */
00422         DIGITAL_PEN = 967,
00423 
00424         /**
00425          * Bar Code Scanner.
00426          */
00427         BARCODE_SCANNER = 968,
00428 
00429         /**
00430          * Generic Glucose Meter.
00431          */
00432         GENERIC_GLUCOSE_METER = 1024,
00433 
00434         /**
00435          * Generic Running/Walking Sensor.
00436          */
00437         GENERIC_RUNNING_WALKING_SENSOR = 1088,
00438 
00439         /**
00440          * In Shoe Running/Walking Sensor.
00441          */
00442         RUNNING_WALKING_SENSOR_IN_SHOE = 1089,
00443 
00444         /**
00445          * On Shoe Running/Walking Sensor.
00446          */
00447         RUNNING_WALKING_SENSOR_ON_SHOE = 1090,
00448 
00449         /**
00450          * On Hip Running/Walking Sensor.
00451          */
00452         RUNNING_WALKING_SENSOR_ON_HIP = 1091,
00453 
00454         /**
00455          * Generic Cycling.
00456          */
00457         GENERIC_CYCLING = 1152,
00458 
00459         /**
00460          * Cycling Computer.
00461          */
00462         CYCLING_CYCLING_COMPUTER = 1153,
00463 
00464         /**
00465          * Cycling Speed Sensor.
00466          */
00467         CYCLING_SPEED_SENSOR = 1154,
00468 
00469         /**
00470          * Cycling Cadence Sensor.
00471          */
00472         CYCLING_CADENCE_SENSOR = 1155,
00473 
00474         /**
00475          * Cycling Power Sensor.
00476          */
00477         CYCLING_POWER_SENSOR = 1156,
00478 
00479         /**
00480          * Cycling Speed and Cadence Sensor.
00481          */
00482         CYCLING_SPEED_AND_CADENCE_SENSOR = 1157,
00483 
00484         /**
00485          * Generic Pulse Oximeter.
00486          */
00487         PULSE_OXIMETER_GENERIC = 3136,
00488 
00489         /**
00490          * Fingertip Pulse Oximeter.
00491          */
00492         PULSE_OXIMETER_FINGERTIP = 3137,
00493 
00494         /**
00495          * Wrist Worn Pulse Oximeter.
00496          */
00497         PULSE_OXIMETER_WRIST_WORN = 3138,
00498 
00499         /**
00500          * Generic Weight Scale.
00501          */
00502         GENERIC_WEIGHT_SCALE = 3200,
00503 
00504         /**
00505          * Generic Outdoor.
00506          */
00507         OUTDOOR_GENERIC = 5184,
00508 
00509         /**
00510          * Outdoor Location Display Device.
00511          */
00512         OUTDOOR_LOCATION_DISPLAY_DEVICE = 5185,
00513 
00514         /**
00515          * Outdoor Location and Navigation Display Device.
00516          */
00517         OUTDOOR_LOCATION_AND_NAVIGATION_DISPLAY_DEVICE = 5186,
00518 
00519         /**
00520          * Outdoor Location Pod.
00521          */
00522         OUTDOOR_LOCATION_POD = 5187,
00523 
00524         /**
00525          * Outdoor Location and Navigation Pod.
00526          */
00527         OUTDOOR_LOCATION_AND_NAVIGATION_POD = 5188
00528 
00529     };
00530 
00531     /**
00532      * Alias for GapAdvertisingData::Appearance_t.
00533      *
00534      * @deprecated Future releases will drop this type alias.
00535      */
00536     typedef enum Appearance_t Appearance;
00537 
00538     /**
00539      * Construct a GapAdvertising instance with an empty payload.
00540      */
00541     GapAdvertisingData(void) :
00542         _payload(),
00543         _payloadLen(0),
00544         _appearance(GENERIC_TAG) {
00545     }
00546 
00547     /**
00548      * Adds a new field into the payload.
00549      *
00550      * If the supplied advertising data type is already present in the
00551      * advertising payload, then the value is updated.
00552      *
00553      * @param[in] advDataType The type of the field to add.
00554      * @param[in] payload Pointer to the value of the field to add.
00555      * @param[in] len Size in bytes of the value to add.
00556      *
00557      * @return BLE_ERROR_NONE on success.
00558      * @return BLE_ERROR_BUFFER_OVERFLOW if the new value causes the advertising
00559      * buffer to overflow.
00560      *
00561      * @note When the specified data type is INCOMPLETE_LIST_16BIT_SERVICE_IDS,
00562      * COMPLETE_LIST_16BIT_SERVICE_IDS, INCOMPLETE_LIST_32BIT_SERVICE_IDS,
00563      * COMPLETE_LIST_32BIT_SERVICE_IDS, INCOMPLETE_LIST_128BIT_SERVICE_IDS,
00564      * COMPLETE_LIST_128BIT_SERVICE_IDS or LIST_128BIT_SOLICITATION_IDS, the
00565      * supplied value is appended to the values present in the payload.
00566      */
00567     ble_error_t addData(DataType_t  advDataType, const uint8_t *payload, uint8_t len)
00568     {
00569         /* Find field */
00570         uint8_t* field = findField(advDataType);
00571 
00572         if (field) {
00573             /* Field type already exists, either add to field or replace */
00574             return addField(advDataType, payload, len, field);
00575         } else {
00576             /* Field doesn't exist, insert new */
00577             return appendField(advDataType, payload, len);
00578         }
00579     }
00580 
00581     /**
00582      * Update a specific field in the advertising payload.
00583      *
00584      * @param[in] advDataType The type of the field to update.
00585      * @param[in] payload Pointer to the updated value of the field.
00586      * @param[in] len Size of the new value in bytes.
00587      *
00588      * @return BLE_ERROR_NONE returned on success.
00589      * @return BLE_ERROR_UNSPECIFIED if the specified field is not found,
00590      * @return BLE_ERROR_BUFFER_OVERFLOW if the new value causes the
00591      * advertising buffer to overflow.
00592      */
00593     ble_error_t updateData(DataType_t  advDataType, const uint8_t *payload, uint8_t len)
00594     {
00595         /* Find field */
00596         uint8_t* field = findField(advDataType);
00597 
00598         if (field) {
00599             /* Field type already exists, replace field contents */
00600             return updateField(advDataType, payload, len, field);
00601         } else {
00602             /* field doesn't exist, return an error */
00603             return BLE_ERROR_UNSPECIFIED;
00604         }
00605     }
00606 
00607     /**
00608      * Add device appearance in the advertising payload.
00609      *
00610      * @param[in] appearance The appearance to advertise.
00611      *
00612      * @return BLE_ERROR_NONE on success.
00613      * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the
00614      * advertising buffer to overflow.
00615      *
00616      * @note This call is equivalent to calling addData() with
00617      * GapAdvertisingData::APPEARANCE as the field type.
00618      */
00619     ble_error_t addAppearance(Appearance appearance = GENERIC_TAG)
00620     {
00621         _appearance = appearance;
00622         return addData(GapAdvertisingData::APPEARANCE, (uint8_t *)&appearance, 2);
00623     }
00624 
00625     /**
00626      * Add BLE flags in the advertising payload.
00627      *
00628      * @param[in] flags Bitfield describing the capability of the device. See
00629      * allowed flags in Flags_t.
00630      *
00631      * @return BLE_ERROR_NONE on success.
00632      * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the
00633      * advertising buffer to overflow.
00634      *
00635      * @note This call is equivalent to calling addData() with
00636      * GapAdvertisingData::FLAGS as the field type.
00637      */
00638     ble_error_t addFlags(uint8_t flags = LE_GENERAL_DISCOVERABLE)
00639     {
00640         return addData(GapAdvertisingData::FLAGS, &flags, 1);
00641     }
00642 
00643     /**
00644      * Add the advertising TX in the advertising payload.
00645      *
00646      * @param[in] txPower Transmission power level in dB.
00647      *
00648      * @return BLE_ERROR_NONE on success.
00649      * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the
00650      * advertising buffer to overflow.
00651      *
00652      * @note This call is equivalent to calling addData() with
00653      * GapAdvertisingData::TX_POWER_LEVEL as the field type.
00654      */
00655     ble_error_t addTxPower(int8_t txPower)
00656     {
00657         /* To Do: Basic error checking to make sure txPower is in range. */
00658         return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t *)&txPower, 1);
00659     }
00660 
00661     /**
00662      * Clears the advertising data payload.
00663      *
00664      * @post getPayloadLen() returns 0.
00665      */
00666     void clear(void)
00667     {
00668         memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
00669         _payloadLen = 0;
00670     }
00671 
00672     /**
00673      * Get the pointer to the advertising payload bytes.
00674      *
00675      * @return A pointer to the payload.
00676      */
00677     const uint8_t *getPayload(void) const
00678     {
00679         return _payload;
00680     }
00681 
00682     /**
00683      * Get the payload length.
00684      *
00685      * @return The payload length in bytes.
00686      */
00687     uint8_t getPayloadLen(void) const
00688     {
00689         return _payloadLen;
00690     }
00691 
00692     /**
00693      * Get the appearance set.
00694      *
00695      * If no value has been set, this function returns GENERIC_TAG.
00696      *
00697      * @return The appearance value set for this device.
00698      */
00699     uint16_t getAppearance(void) const
00700     {
00701         return (uint16_t)_appearance;
00702     }
00703 
00704     /**
00705      * Search advertisement data for a specific field.
00706      *
00707      * @param[in] type The type of the field to find.
00708      *
00709      * @return A pointer to the first element in the field if found. The first
00710      * element being the length of the field followed by the value of the field.
00711      * @return NULL if the field is not present in the payload.
00712      */
00713     const uint8_t* findField(DataType_t  type) const
00714     {
00715         /* Scan through advertisement data */
00716         for (uint8_t idx = 0; idx < _payloadLen; ) {
00717             uint8_t fieldType = _payload[idx + 1];
00718 
00719             if (fieldType == type) {
00720                 return &_payload[idx];
00721             }
00722 
00723             /* Advance to next field */
00724             idx += _payload[idx] + 1;
00725         }
00726 
00727         /* Field not found */
00728         return NULL;
00729     }
00730 
00731 private:
00732     /**
00733      * Append advertising data based on the specified type.
00734      *
00735      * @param[in] advDataType Type of the new data.
00736      * @param[in] payload Pointer to the data to be appended to the advertising
00737      * payload.
00738      * @param[in] len Length of the data pointed to by @p payload.
00739      *
00740      * @return BLE_ERROR_NONE on success.
00741      * @return BLE_ERROR_BUFFER_OVERFLOW if the specified data would cause the
00742      * advertising buffer to overflow.
00743      */
00744     ble_error_t appendField(DataType advDataType, const uint8_t *payload, uint8_t len)
00745     {
00746         /* Make sure we don't exceed the 31-byte payload limit */
00747         if (_payloadLen + len + 2 > GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
00748             return BLE_ERROR_BUFFER_OVERFLOW;
00749         }
00750 
00751         /* Field length. */
00752         memset(&_payload[_payloadLen], len + 1, 1);
00753         _payloadLen++;
00754 
00755         /* Field ID. */
00756         memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
00757         _payloadLen++;
00758 
00759         /* Payload. */
00760         memcpy(&_payload[_payloadLen], payload, len);
00761         _payloadLen += len;
00762 
00763         return BLE_ERROR_NONE;
00764     }
00765 
00766     /**
00767      * Search advertisement data for a specific field.
00768      *
00769      * @param[in] type The type of the field to find.
00770      *
00771      * @return A pointer to the first element in the field if found. The first
00772      * element being the length of the field followed by the value of the field.
00773      * @return NULL if the field is not present in the payload.
00774      */
00775     uint8_t* findField(DataType_t  type)
00776     {
00777         return const_cast<uint8_t*>(
00778             static_cast<const GapAdvertisingData*>(this)->findField(type)
00779         );
00780     }
00781 
00782     /**
00783      * Update in place the value of a field in the advertising payload.
00784      *
00785      * @param[in] advDataType Type of the new data.
00786      * @param[in] payload Pointer to the data to be added to the advertising
00787      * payload.
00788      * @param[in] len Length of the data pointed to by @p payload.
00789      * @param[in] field Pointer to the field of type @p advDataType in the
00790      * advertising buffer.
00791      *
00792      * @note When the specified AD type is INCOMPLETE_LIST_16BIT_SERVICE_IDS,
00793      * COMPLETE_LIST_16BIT_SERVICE_IDS, INCOMPLETE_LIST_32BIT_SERVICE_IDS,
00794      * COMPLETE_LIST_32BIT_SERVICE_IDS, INCOMPLETE_LIST_128BIT_SERVICE_IDS,
00795      * COMPLETE_LIST_128BIT_SERVICE_IDS or LIST_128BIT_SOLICITATION_IDS, the
00796      * supplied value is appended to the values previously added to the
00797      * payload.
00798      *
00799      * @return BLE_ERROR_NONE on success.
00800      */
00801     ble_error_t addField(
00802         DataType_t  advDataType,
00803         const uint8_t *payload,
00804         uint8_t len,
00805         uint8_t* field
00806     ) {
00807         ble_error_t result = BLE_ERROR_BUFFER_OVERFLOW;
00808 
00809         switch(advDataType) {
00810             /* These fields have the new data appended if there is sufficient space. */
00811             case INCOMPLETE_LIST_16BIT_SERVICE_IDS:
00812             case COMPLETE_LIST_16BIT_SERVICE_IDS:
00813             case INCOMPLETE_LIST_32BIT_SERVICE_IDS:
00814             case COMPLETE_LIST_32BIT_SERVICE_IDS:
00815             case INCOMPLETE_LIST_128BIT_SERVICE_IDS:
00816             case COMPLETE_LIST_128BIT_SERVICE_IDS:
00817             case LIST_128BIT_SOLICITATION_IDS: {
00818                 /* Check if data fits */
00819                 if ((_payloadLen + len) <= GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
00820                     /*
00821                      * Make room for new field by moving the remainder of the
00822                      * advertisement payload "to the right" starting after the
00823                      * TYPE field.
00824                      */
00825                     uint8_t* end = &_payload[_payloadLen];
00826 
00827                     while (&field[1] < end) {
00828                         end[len] = *end;
00829                         end--;
00830                     }
00831 
00832                     /* Insert new data */
00833                     for (uint8_t idx = 0; idx < len; idx++) {
00834                         field[2 + idx] = payload[idx];
00835                     }
00836 
00837                     /* Increment lengths */
00838                     field[0] += len;
00839                     _payloadLen += len;
00840 
00841                     result = BLE_ERROR_NONE;
00842                 }
00843 
00844                 break;
00845             }
00846             /* These fields are overwritten with the new value */
00847             default: {
00848                 result = updateField(advDataType, payload, len, field);
00849 
00850                 break;
00851             }
00852         }
00853 
00854         return result;
00855     }
00856 
00857     /**
00858      * Update in place the value of a field in the advertising payload.
00859      *
00860      * @param[in] advDataType Type of the new data.
00861      * @param[in] payload Pointer to the data to be added to the advertising
00862      * payload.
00863      * @param[in] len Length of the data pointed to by @p payload.
00864      * @param[in] field Pointer to the field of type @p advDataType in the
00865      * advertising buffer.
00866      *
00867      * @return BLE_ERROR_NONE on success.
00868      */
00869     ble_error_t updateField(
00870         DataType_t  advDataType,
00871         const uint8_t *payload,
00872         uint8_t len,
00873         uint8_t* field
00874     ) {
00875         ble_error_t result = BLE_ERROR_BUFFER_OVERFLOW;
00876         uint8_t dataLength = field[0] - 1;
00877 
00878         /* New data has same length, do in-order replacement */
00879         if (len == dataLength) {
00880             for (uint8_t idx = 0; idx < dataLength; idx++) {
00881                 field[2 + idx] = payload[idx];
00882             }
00883 
00884             result = BLE_ERROR_NONE;
00885         } else {
00886             /* Check if data fits */
00887             if ((_payloadLen - dataLength + len) <= GAP_ADVERTISING_DATA_MAX_PAYLOAD) {
00888 
00889                 /* Remove old field */
00890                 while ((field + dataLength + 2) < &_payload[_payloadLen]) {
00891                     *field = field[dataLength + 2];
00892                     field++;
00893                 }
00894 
00895                 /* Reduce length */
00896                 _payloadLen -= dataLength + 2;
00897 
00898                 /* Add new field */
00899                 result = appendField(advDataType, payload, len);
00900             }
00901         }
00902 
00903         return result;
00904     }
00905 
00906     /**
00907      * Advertising data buffer.
00908      */
00909     uint8_t _payload[GAP_ADVERTISING_DATA_MAX_PAYLOAD];
00910 
00911     /**
00912      * Length of the data added to the advertising buffer.
00913      */
00914     uint8_t _payloadLen;
00915 
00916     /**
00917      * Appearance value.
00918      */
00919     uint16_t _appearance;
00920 };
00921 
00922 /**
00923  * @}
00924  * @}
00925  */
00926 
00927 
00928 #endif /* ifndef MBED_GAP_ADVERTISING_DATA__LEGACY_H__ */