cvb

Fork of nrf51-sdk by Lancaster University

Committer:
Jonathan Austin
Date:
Wed Apr 06 23:55:04 2016 +0100
Revision:
0:bc2961fa1ef0
Synchronized with git rev 90647e3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 0:bc2961fa1ef0 1 /*
Jonathan Austin 0:bc2961fa1ef0 2 * Copyright (c) Nordic Semiconductor ASA
Jonathan Austin 0:bc2961fa1ef0 3 * All rights reserved.
Jonathan Austin 0:bc2961fa1ef0 4 *
Jonathan Austin 0:bc2961fa1ef0 5 * Redistribution and use in source and binary forms, with or without modification,
Jonathan Austin 0:bc2961fa1ef0 6 * are permitted provided that the following conditions are met:
Jonathan Austin 0:bc2961fa1ef0 7 *
Jonathan Austin 0:bc2961fa1ef0 8 * 1. Redistributions of source code must retain the above copyright notice, this
Jonathan Austin 0:bc2961fa1ef0 9 * list of conditions and the following disclaimer.
Jonathan Austin 0:bc2961fa1ef0 10 *
Jonathan Austin 0:bc2961fa1ef0 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Jonathan Austin 0:bc2961fa1ef0 12 * list of conditions and the following disclaimer in the documentation and/or
Jonathan Austin 0:bc2961fa1ef0 13 * other materials provided with the distribution.
Jonathan Austin 0:bc2961fa1ef0 14 *
Jonathan Austin 0:bc2961fa1ef0 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Jonathan Austin 0:bc2961fa1ef0 16 * contributors to this software may be used to endorse or promote products
Jonathan Austin 0:bc2961fa1ef0 17 * derived from this software without specific prior written permission.
Jonathan Austin 0:bc2961fa1ef0 18 *
Jonathan Austin 0:bc2961fa1ef0 19 *
Jonathan Austin 0:bc2961fa1ef0 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Jonathan Austin 0:bc2961fa1ef0 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Jonathan Austin 0:bc2961fa1ef0 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Jonathan Austin 0:bc2961fa1ef0 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Jonathan Austin 0:bc2961fa1ef0 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Jonathan Austin 0:bc2961fa1ef0 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Jonathan Austin 0:bc2961fa1ef0 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Jonathan Austin 0:bc2961fa1ef0 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Jonathan Austin 0:bc2961fa1ef0 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Jonathan Austin 0:bc2961fa1ef0 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jonathan Austin 0:bc2961fa1ef0 30 *
Jonathan Austin 0:bc2961fa1ef0 31 */
Jonathan Austin 0:bc2961fa1ef0 32
Jonathan Austin 0:bc2961fa1ef0 33 #include "ble_advdata.h"
Jonathan Austin 0:bc2961fa1ef0 34 #include "nordic_common.h"
Jonathan Austin 0:bc2961fa1ef0 35 #include "nrf_error.h"
Jonathan Austin 0:bc2961fa1ef0 36 #include "ble_gap.h"
Jonathan Austin 0:bc2961fa1ef0 37 #include "ble_srv_common.h"
Jonathan Austin 0:bc2961fa1ef0 38 #include "app_util.h"
Jonathan Austin 0:bc2961fa1ef0 39
Jonathan Austin 0:bc2961fa1ef0 40 // NOTE: For now, Security Manager Out of Band Flags (OOB) are omitted from the advertising data.
Jonathan Austin 0:bc2961fa1ef0 41
Jonathan Austin 0:bc2961fa1ef0 42 // Types of LE Bluetooth Device Address AD type
Jonathan Austin 0:bc2961fa1ef0 43 #define AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC 0UL
Jonathan Austin 0:bc2961fa1ef0 44 #define AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM 1UL
Jonathan Austin 0:bc2961fa1ef0 45
Jonathan Austin 0:bc2961fa1ef0 46 static uint32_t tk_value_encode(ble_advdata_tk_value_t * p_tk_value,
Jonathan Austin 0:bc2961fa1ef0 47 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 48 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 49 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 50 {
Jonathan Austin 0:bc2961fa1ef0 51 int8_t i;
Jonathan Austin 0:bc2961fa1ef0 52
Jonathan Austin 0:bc2961fa1ef0 53 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 54 if (((*p_offset) + AD_TYPE_TK_VALUE_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 55 {
Jonathan Austin 0:bc2961fa1ef0 56 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 57 }
Jonathan Austin 0:bc2961fa1ef0 58
Jonathan Austin 0:bc2961fa1ef0 59 // Encode LE Role.
Jonathan Austin 0:bc2961fa1ef0 60 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_TK_VALUE_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 61 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 62 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SECURITY_MANAGER_TK_VALUE;
Jonathan Austin 0:bc2961fa1ef0 63 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 64
Jonathan Austin 0:bc2961fa1ef0 65 for (i = AD_TYPE_TK_VALUE_DATA_SIZE - 1; i >= 0; i--, (*p_offset)++)
Jonathan Austin 0:bc2961fa1ef0 66 {
Jonathan Austin 0:bc2961fa1ef0 67 p_encoded_data[*p_offset] = p_tk_value->tk[i];
Jonathan Austin 0:bc2961fa1ef0 68 }
Jonathan Austin 0:bc2961fa1ef0 69
Jonathan Austin 0:bc2961fa1ef0 70 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 71 }
Jonathan Austin 0:bc2961fa1ef0 72
Jonathan Austin 0:bc2961fa1ef0 73 static uint32_t le_role_encode(ble_advdata_le_role_t le_role,
Jonathan Austin 0:bc2961fa1ef0 74 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 75 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 76 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 77 {
Jonathan Austin 0:bc2961fa1ef0 78 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 79 if (((*p_offset) + AD_TYPE_LE_ROLE_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 80 {
Jonathan Austin 0:bc2961fa1ef0 81 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 82 }
Jonathan Austin 0:bc2961fa1ef0 83
Jonathan Austin 0:bc2961fa1ef0 84 // Encode LE Role.
Jonathan Austin 0:bc2961fa1ef0 85 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_LE_ROLE_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 86 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 87 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LE_ROLE;
Jonathan Austin 0:bc2961fa1ef0 88 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 89 switch(le_role)
Jonathan Austin 0:bc2961fa1ef0 90 {
Jonathan Austin 0:bc2961fa1ef0 91 case BLE_ADVDATA_ROLE_ONLY_PERIPH:
Jonathan Austin 0:bc2961fa1ef0 92 p_encoded_data[*p_offset] = 0;
Jonathan Austin 0:bc2961fa1ef0 93 break;
Jonathan Austin 0:bc2961fa1ef0 94 case BLE_ADVDATA_ROLE_ONLY_CENTRAL:
Jonathan Austin 0:bc2961fa1ef0 95 p_encoded_data[*p_offset] = 1;
Jonathan Austin 0:bc2961fa1ef0 96 break;
Jonathan Austin 0:bc2961fa1ef0 97 case BLE_ADVDATA_ROLE_BOTH_PERIPH_PREFERRED:
Jonathan Austin 0:bc2961fa1ef0 98 p_encoded_data[*p_offset] = 2;
Jonathan Austin 0:bc2961fa1ef0 99 break;
Jonathan Austin 0:bc2961fa1ef0 100 case BLE_ADVDATA_ROLE_BOTH_CENTRAL_PREFERRED:
Jonathan Austin 0:bc2961fa1ef0 101 p_encoded_data[*p_offset] = 3;
Jonathan Austin 0:bc2961fa1ef0 102 break;
Jonathan Austin 0:bc2961fa1ef0 103 default:
Jonathan Austin 0:bc2961fa1ef0 104 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 105 }
Jonathan Austin 0:bc2961fa1ef0 106 *p_offset += AD_TYPE_LE_ROLE_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 107
Jonathan Austin 0:bc2961fa1ef0 108 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 109 }
Jonathan Austin 0:bc2961fa1ef0 110
Jonathan Austin 0:bc2961fa1ef0 111 static uint32_t ble_device_addr_encode(uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 112 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 113 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 114 {
Jonathan Austin 0:bc2961fa1ef0 115 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 116 ble_gap_addr_t device_addr;
Jonathan Austin 0:bc2961fa1ef0 117
Jonathan Austin 0:bc2961fa1ef0 118 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 119 if (((*p_offset) + AD_TYPE_BLE_DEVICE_ADDR_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 120 {
Jonathan Austin 0:bc2961fa1ef0 121 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 122 }
Jonathan Austin 0:bc2961fa1ef0 123
Jonathan Austin 0:bc2961fa1ef0 124 // Get BLE address
Jonathan Austin 0:bc2961fa1ef0 125 err_code = sd_ble_gap_address_get(&device_addr);
Jonathan Austin 0:bc2961fa1ef0 126 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 127 {
Jonathan Austin 0:bc2961fa1ef0 128 return err_code;
Jonathan Austin 0:bc2961fa1ef0 129 }
Jonathan Austin 0:bc2961fa1ef0 130
Jonathan Austin 0:bc2961fa1ef0 131 // Encode LE Bluetooth Device Address
Jonathan Austin 0:bc2961fa1ef0 132 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE +
Jonathan Austin 0:bc2961fa1ef0 133 AD_TYPE_BLE_DEVICE_ADDR_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 134 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 135 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS;
Jonathan Austin 0:bc2961fa1ef0 136 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 137 memcpy(&p_encoded_data[*p_offset], &device_addr.addr[0], BLE_GAP_ADDR_LEN);
Jonathan Austin 0:bc2961fa1ef0 138 *p_offset += BLE_GAP_ADDR_LEN;
Jonathan Austin 0:bc2961fa1ef0 139 if(BLE_GAP_ADDR_TYPE_PUBLIC == device_addr.addr_type)
Jonathan Austin 0:bc2961fa1ef0 140 {
Jonathan Austin 0:bc2961fa1ef0 141 p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_PUBLIC;
Jonathan Austin 0:bc2961fa1ef0 142 }
Jonathan Austin 0:bc2961fa1ef0 143 else
Jonathan Austin 0:bc2961fa1ef0 144 {
Jonathan Austin 0:bc2961fa1ef0 145 p_encoded_data[*p_offset] = AD_TYPE_BLE_DEVICE_ADDR_TYPE_RANDOM;
Jonathan Austin 0:bc2961fa1ef0 146 }
Jonathan Austin 0:bc2961fa1ef0 147 *p_offset += AD_TYPE_BLE_DEVICE_ADDR_TYPE_SIZE;
Jonathan Austin 0:bc2961fa1ef0 148
Jonathan Austin 0:bc2961fa1ef0 149 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 150 }
Jonathan Austin 0:bc2961fa1ef0 151
Jonathan Austin 0:bc2961fa1ef0 152 static uint32_t name_encode(const ble_advdata_t * p_advdata,
Jonathan Austin 0:bc2961fa1ef0 153 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 154 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 155 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 156 {
Jonathan Austin 0:bc2961fa1ef0 157 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 158 uint16_t rem_adv_data_len;
Jonathan Austin 0:bc2961fa1ef0 159 uint16_t actual_length;
Jonathan Austin 0:bc2961fa1ef0 160 uint8_t adv_data_format;
Jonathan Austin 0:bc2961fa1ef0 161
Jonathan Austin 0:bc2961fa1ef0 162
Jonathan Austin 0:bc2961fa1ef0 163 // Validate parameters
Jonathan Austin 0:bc2961fa1ef0 164 if((BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) && (0 == p_advdata->short_name_len))
Jonathan Austin 0:bc2961fa1ef0 165 {
Jonathan Austin 0:bc2961fa1ef0 166 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 167 }
Jonathan Austin 0:bc2961fa1ef0 168
Jonathan Austin 0:bc2961fa1ef0 169 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 170 if ( (((*p_offset) + ADV_AD_DATA_OFFSET) > max_size) ||
Jonathan Austin 0:bc2961fa1ef0 171 ( (BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) &&
Jonathan Austin 0:bc2961fa1ef0 172 (((*p_offset) + ADV_AD_DATA_OFFSET + p_advdata->short_name_len) > max_size)))
Jonathan Austin 0:bc2961fa1ef0 173 {
Jonathan Austin 0:bc2961fa1ef0 174 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 175 }
Jonathan Austin 0:bc2961fa1ef0 176
Jonathan Austin 0:bc2961fa1ef0 177 rem_adv_data_len = max_size - (*p_offset) - ADV_AD_DATA_OFFSET;
Jonathan Austin 0:bc2961fa1ef0 178 actual_length = rem_adv_data_len;
Jonathan Austin 0:bc2961fa1ef0 179
Jonathan Austin 0:bc2961fa1ef0 180 // Get GAP device name and length
Jonathan Austin 0:bc2961fa1ef0 181 err_code = sd_ble_gap_device_name_get(&p_encoded_data[(*p_offset) + ADV_AD_DATA_OFFSET],
Jonathan Austin 0:bc2961fa1ef0 182 &actual_length);
Jonathan Austin 0:bc2961fa1ef0 183 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 184 {
Jonathan Austin 0:bc2961fa1ef0 185 return err_code;
Jonathan Austin 0:bc2961fa1ef0 186 }
Jonathan Austin 0:bc2961fa1ef0 187
Jonathan Austin 0:bc2961fa1ef0 188 // Check if device intend to use short name and it can fit available data size.
Jonathan Austin 0:bc2961fa1ef0 189 if ((p_advdata->name_type == BLE_ADVDATA_FULL_NAME) && (actual_length <= rem_adv_data_len))
Jonathan Austin 0:bc2961fa1ef0 190 {
Jonathan Austin 0:bc2961fa1ef0 191 // Complete device name can fit, setting Complete Name in Adv Data.
Jonathan Austin 0:bc2961fa1ef0 192 adv_data_format = BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME;
Jonathan Austin 0:bc2961fa1ef0 193 }
Jonathan Austin 0:bc2961fa1ef0 194 else
Jonathan Austin 0:bc2961fa1ef0 195 {
Jonathan Austin 0:bc2961fa1ef0 196 // Else short name needs to be used. Or application has requested use of short name.
Jonathan Austin 0:bc2961fa1ef0 197 adv_data_format = BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME;
Jonathan Austin 0:bc2961fa1ef0 198
Jonathan Austin 0:bc2961fa1ef0 199 // If application has set a preference on the short name size, it needs to be considered,
Jonathan Austin 0:bc2961fa1ef0 200 // else fit what can be fit.
Jonathan Austin 0:bc2961fa1ef0 201 if ((BLE_ADVDATA_SHORT_NAME == p_advdata->name_type) &&
Jonathan Austin 0:bc2961fa1ef0 202 (p_advdata->short_name_len <= rem_adv_data_len))
Jonathan Austin 0:bc2961fa1ef0 203 {
Jonathan Austin 0:bc2961fa1ef0 204 // Short name fits available size.
Jonathan Austin 0:bc2961fa1ef0 205 actual_length = p_advdata->short_name_len;
Jonathan Austin 0:bc2961fa1ef0 206 }
Jonathan Austin 0:bc2961fa1ef0 207 // Else whatever can fit the data buffer will be packed.
Jonathan Austin 0:bc2961fa1ef0 208 else
Jonathan Austin 0:bc2961fa1ef0 209 {
Jonathan Austin 0:bc2961fa1ef0 210 actual_length = rem_adv_data_len;
Jonathan Austin 0:bc2961fa1ef0 211 }
Jonathan Austin 0:bc2961fa1ef0 212 }
Jonathan Austin 0:bc2961fa1ef0 213
Jonathan Austin 0:bc2961fa1ef0 214 // There is only 1 byte intended to encode length which is (actual_length + ADV_AD_TYPE_FIELD_SIZE)
Jonathan Austin 0:bc2961fa1ef0 215 if(actual_length > (0x00FF - ADV_AD_TYPE_FIELD_SIZE))
Jonathan Austin 0:bc2961fa1ef0 216 {
Jonathan Austin 0:bc2961fa1ef0 217 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 218 }
Jonathan Austin 0:bc2961fa1ef0 219
Jonathan Austin 0:bc2961fa1ef0 220 // Complete name field in encoded data.
Jonathan Austin 0:bc2961fa1ef0 221 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + actual_length);
Jonathan Austin 0:bc2961fa1ef0 222 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 223 p_encoded_data[*p_offset] = adv_data_format;
Jonathan Austin 0:bc2961fa1ef0 224 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 225 *p_offset += actual_length;
Jonathan Austin 0:bc2961fa1ef0 226
Jonathan Austin 0:bc2961fa1ef0 227 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 228 }
Jonathan Austin 0:bc2961fa1ef0 229
Jonathan Austin 0:bc2961fa1ef0 230
Jonathan Austin 0:bc2961fa1ef0 231 static uint32_t appearance_encode(uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 232 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 233 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 234 {
Jonathan Austin 0:bc2961fa1ef0 235 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 236 uint16_t appearance;
Jonathan Austin 0:bc2961fa1ef0 237
Jonathan Austin 0:bc2961fa1ef0 238 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 239 if (((*p_offset) + AD_TYPE_APPEARANCE_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 240 {
Jonathan Austin 0:bc2961fa1ef0 241 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 242 }
Jonathan Austin 0:bc2961fa1ef0 243
Jonathan Austin 0:bc2961fa1ef0 244 // Get GAP appearance field.
Jonathan Austin 0:bc2961fa1ef0 245 err_code = sd_ble_gap_appearance_get(&appearance);
Jonathan Austin 0:bc2961fa1ef0 246 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 247 {
Jonathan Austin 0:bc2961fa1ef0 248 return err_code;
Jonathan Austin 0:bc2961fa1ef0 249 }
Jonathan Austin 0:bc2961fa1ef0 250
Jonathan Austin 0:bc2961fa1ef0 251 // Encode Length, AD Type and Appearance.
Jonathan Austin 0:bc2961fa1ef0 252 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_APPEARANCE_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 253 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 254 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_APPEARANCE;
Jonathan Austin 0:bc2961fa1ef0 255 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 256 *p_offset += uint16_encode(appearance, &p_encoded_data[*p_offset]);
Jonathan Austin 0:bc2961fa1ef0 257
Jonathan Austin 0:bc2961fa1ef0 258 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 259 }
Jonathan Austin 0:bc2961fa1ef0 260
Jonathan Austin 0:bc2961fa1ef0 261 static uint32_t flags_encode(int8_t flags,
Jonathan Austin 0:bc2961fa1ef0 262 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 263 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 264 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 265 {
Jonathan Austin 0:bc2961fa1ef0 266 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 267 if (((*p_offset) + AD_TYPE_FLAGS_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 268 {
Jonathan Austin 0:bc2961fa1ef0 269 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 270 }
Jonathan Austin 0:bc2961fa1ef0 271
Jonathan Austin 0:bc2961fa1ef0 272 // Encode flags.
Jonathan Austin 0:bc2961fa1ef0 273 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_FLAGS_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 274 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 275 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_FLAGS;
Jonathan Austin 0:bc2961fa1ef0 276 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 277 p_encoded_data[*p_offset] = flags;
Jonathan Austin 0:bc2961fa1ef0 278 *p_offset += AD_TYPE_FLAGS_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 279
Jonathan Austin 0:bc2961fa1ef0 280 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 281 }
Jonathan Austin 0:bc2961fa1ef0 282
Jonathan Austin 0:bc2961fa1ef0 283 static uint32_t sec_mgr_oob_flags_encode(uint8_t oob_flags,
Jonathan Austin 0:bc2961fa1ef0 284 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 285 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 286 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 287 {
Jonathan Austin 0:bc2961fa1ef0 288 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 289 if (((*p_offset) + AD_TYPE_OOB_FLAGS_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 290 {
Jonathan Austin 0:bc2961fa1ef0 291 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 292 }
Jonathan Austin 0:bc2961fa1ef0 293
Jonathan Austin 0:bc2961fa1ef0 294 // Encode flags.
Jonathan Austin 0:bc2961fa1ef0 295 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_OOB_FLAGS_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 296 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 297 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SECURITY_MANAGER_OOB_FLAGS;
Jonathan Austin 0:bc2961fa1ef0 298 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 299 p_encoded_data[*p_offset] = oob_flags;
Jonathan Austin 0:bc2961fa1ef0 300 *p_offset += AD_TYPE_OOB_FLAGS_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 301
Jonathan Austin 0:bc2961fa1ef0 302 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 303 }
Jonathan Austin 0:bc2961fa1ef0 304
Jonathan Austin 0:bc2961fa1ef0 305 static uint32_t tx_power_level_encode(int8_t tx_power_level,
Jonathan Austin 0:bc2961fa1ef0 306 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 307 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 308 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 309 {
Jonathan Austin 0:bc2961fa1ef0 310 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 311 if (((*p_offset) + AD_TYPE_TX_POWER_LEVEL_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 312 {
Jonathan Austin 0:bc2961fa1ef0 313 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 314 }
Jonathan Austin 0:bc2961fa1ef0 315
Jonathan Austin 0:bc2961fa1ef0 316 // Encode TX Power Level.
Jonathan Austin 0:bc2961fa1ef0 317 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE +
Jonathan Austin 0:bc2961fa1ef0 318 AD_TYPE_TX_POWER_LEVEL_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 319 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 320 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_TX_POWER_LEVEL;
Jonathan Austin 0:bc2961fa1ef0 321 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 322 p_encoded_data[*p_offset] = tx_power_level;
Jonathan Austin 0:bc2961fa1ef0 323 *p_offset += AD_TYPE_TX_POWER_LEVEL_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 324
Jonathan Austin 0:bc2961fa1ef0 325 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 326 }
Jonathan Austin 0:bc2961fa1ef0 327
Jonathan Austin 0:bc2961fa1ef0 328
Jonathan Austin 0:bc2961fa1ef0 329 static uint32_t uuid_list_sized_encode(const ble_advdata_uuid_list_t * p_uuid_list,
Jonathan Austin 0:bc2961fa1ef0 330 uint8_t adv_type,
Jonathan Austin 0:bc2961fa1ef0 331 uint8_t uuid_size,
Jonathan Austin 0:bc2961fa1ef0 332 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 333 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 334 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 335 {
Jonathan Austin 0:bc2961fa1ef0 336 int i;
Jonathan Austin 0:bc2961fa1ef0 337 bool is_heading_written = false;
Jonathan Austin 0:bc2961fa1ef0 338 uint16_t start_pos = *p_offset;
Jonathan Austin 0:bc2961fa1ef0 339 uint16_t length;
Jonathan Austin 0:bc2961fa1ef0 340
Jonathan Austin 0:bc2961fa1ef0 341 for (i = 0; i < p_uuid_list->uuid_cnt; i++)
Jonathan Austin 0:bc2961fa1ef0 342 {
Jonathan Austin 0:bc2961fa1ef0 343 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 344 uint8_t encoded_size;
Jonathan Austin 0:bc2961fa1ef0 345 ble_uuid_t uuid = p_uuid_list->p_uuids[i];
Jonathan Austin 0:bc2961fa1ef0 346
Jonathan Austin 0:bc2961fa1ef0 347 // Find encoded uuid size.
Jonathan Austin 0:bc2961fa1ef0 348 err_code = sd_ble_uuid_encode(&uuid, &encoded_size, NULL);
Jonathan Austin 0:bc2961fa1ef0 349 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 350 {
Jonathan Austin 0:bc2961fa1ef0 351 return err_code;
Jonathan Austin 0:bc2961fa1ef0 352 }
Jonathan Austin 0:bc2961fa1ef0 353
Jonathan Austin 0:bc2961fa1ef0 354 // Check size.
Jonathan Austin 0:bc2961fa1ef0 355 if (encoded_size == uuid_size)
Jonathan Austin 0:bc2961fa1ef0 356 {
Jonathan Austin 0:bc2961fa1ef0 357 uint8_t heading_bytes = (is_heading_written) ? 0 : ADV_AD_DATA_OFFSET;
Jonathan Austin 0:bc2961fa1ef0 358
Jonathan Austin 0:bc2961fa1ef0 359 // Check for buffer overflow
Jonathan Austin 0:bc2961fa1ef0 360 if (((*p_offset) + encoded_size + heading_bytes) > max_size)
Jonathan Austin 0:bc2961fa1ef0 361 {
Jonathan Austin 0:bc2961fa1ef0 362 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 363 }
Jonathan Austin 0:bc2961fa1ef0 364
Jonathan Austin 0:bc2961fa1ef0 365 if (!is_heading_written)
Jonathan Austin 0:bc2961fa1ef0 366 {
Jonathan Austin 0:bc2961fa1ef0 367 // Write AD structure heading.
Jonathan Austin 0:bc2961fa1ef0 368 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 369 p_encoded_data[*p_offset] = adv_type;
Jonathan Austin 0:bc2961fa1ef0 370 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 371 is_heading_written = true;
Jonathan Austin 0:bc2961fa1ef0 372 }
Jonathan Austin 0:bc2961fa1ef0 373
Jonathan Austin 0:bc2961fa1ef0 374 // Write UUID.
Jonathan Austin 0:bc2961fa1ef0 375 err_code = sd_ble_uuid_encode(&uuid, &encoded_size, &p_encoded_data[*p_offset]);
Jonathan Austin 0:bc2961fa1ef0 376 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 377 {
Jonathan Austin 0:bc2961fa1ef0 378 return err_code;
Jonathan Austin 0:bc2961fa1ef0 379 }
Jonathan Austin 0:bc2961fa1ef0 380 *p_offset += encoded_size;
Jonathan Austin 0:bc2961fa1ef0 381 }
Jonathan Austin 0:bc2961fa1ef0 382 }
Jonathan Austin 0:bc2961fa1ef0 383
Jonathan Austin 0:bc2961fa1ef0 384 if (is_heading_written)
Jonathan Austin 0:bc2961fa1ef0 385 {
Jonathan Austin 0:bc2961fa1ef0 386 // Write length.
Jonathan Austin 0:bc2961fa1ef0 387 length = (*p_offset) - (start_pos + ADV_LENGTH_FIELD_SIZE);
Jonathan Austin 0:bc2961fa1ef0 388 // There is only 1 byte intended to encode length
Jonathan Austin 0:bc2961fa1ef0 389 if(length > 0x00FF)
Jonathan Austin 0:bc2961fa1ef0 390 {
Jonathan Austin 0:bc2961fa1ef0 391 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 392 }
Jonathan Austin 0:bc2961fa1ef0 393 p_encoded_data[start_pos] = (uint8_t)length;
Jonathan Austin 0:bc2961fa1ef0 394 }
Jonathan Austin 0:bc2961fa1ef0 395
Jonathan Austin 0:bc2961fa1ef0 396 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 397 }
Jonathan Austin 0:bc2961fa1ef0 398
Jonathan Austin 0:bc2961fa1ef0 399
Jonathan Austin 0:bc2961fa1ef0 400 static uint32_t uuid_list_encode(const ble_advdata_uuid_list_t * p_uuid_list,
Jonathan Austin 0:bc2961fa1ef0 401 uint8_t adv_type_16,
Jonathan Austin 0:bc2961fa1ef0 402 uint8_t adv_type_128,
Jonathan Austin 0:bc2961fa1ef0 403 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 404 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 405 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 406 {
Jonathan Austin 0:bc2961fa1ef0 407 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 408
Jonathan Austin 0:bc2961fa1ef0 409 // Encode 16 bit UUIDs.
Jonathan Austin 0:bc2961fa1ef0 410 err_code = uuid_list_sized_encode(p_uuid_list,
Jonathan Austin 0:bc2961fa1ef0 411 adv_type_16,
Jonathan Austin 0:bc2961fa1ef0 412 sizeof(uint16_le_t),
Jonathan Austin 0:bc2961fa1ef0 413 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 414 p_offset,
Jonathan Austin 0:bc2961fa1ef0 415 max_size);
Jonathan Austin 0:bc2961fa1ef0 416 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 417 {
Jonathan Austin 0:bc2961fa1ef0 418 return err_code;
Jonathan Austin 0:bc2961fa1ef0 419 }
Jonathan Austin 0:bc2961fa1ef0 420
Jonathan Austin 0:bc2961fa1ef0 421 // Encode 128 bit UUIDs.
Jonathan Austin 0:bc2961fa1ef0 422 err_code = uuid_list_sized_encode(p_uuid_list,
Jonathan Austin 0:bc2961fa1ef0 423 adv_type_128,
Jonathan Austin 0:bc2961fa1ef0 424 sizeof(ble_uuid128_t),
Jonathan Austin 0:bc2961fa1ef0 425 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 426 p_offset,
Jonathan Austin 0:bc2961fa1ef0 427 max_size);
Jonathan Austin 0:bc2961fa1ef0 428 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 429 {
Jonathan Austin 0:bc2961fa1ef0 430 return err_code;
Jonathan Austin 0:bc2961fa1ef0 431 }
Jonathan Austin 0:bc2961fa1ef0 432
Jonathan Austin 0:bc2961fa1ef0 433 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 434 }
Jonathan Austin 0:bc2961fa1ef0 435
Jonathan Austin 0:bc2961fa1ef0 436
Jonathan Austin 0:bc2961fa1ef0 437 static uint32_t conn_int_check(const ble_advdata_conn_int_t *p_conn_int)
Jonathan Austin 0:bc2961fa1ef0 438 {
Jonathan Austin 0:bc2961fa1ef0 439 // Check Minimum Connection Interval.
Jonathan Austin 0:bc2961fa1ef0 440 if ((p_conn_int->min_conn_interval < 0x0006) ||
Jonathan Austin 0:bc2961fa1ef0 441 (
Jonathan Austin 0:bc2961fa1ef0 442 (p_conn_int->min_conn_interval > 0x0c80) &&
Jonathan Austin 0:bc2961fa1ef0 443 (p_conn_int->min_conn_interval != 0xffff)
Jonathan Austin 0:bc2961fa1ef0 444 )
Jonathan Austin 0:bc2961fa1ef0 445 )
Jonathan Austin 0:bc2961fa1ef0 446 {
Jonathan Austin 0:bc2961fa1ef0 447 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 448 }
Jonathan Austin 0:bc2961fa1ef0 449
Jonathan Austin 0:bc2961fa1ef0 450 // Check Maximum Connection Interval.
Jonathan Austin 0:bc2961fa1ef0 451 if ((p_conn_int->max_conn_interval < 0x0006) ||
Jonathan Austin 0:bc2961fa1ef0 452 (
Jonathan Austin 0:bc2961fa1ef0 453 (p_conn_int->max_conn_interval > 0x0c80) &&
Jonathan Austin 0:bc2961fa1ef0 454 (p_conn_int->max_conn_interval != 0xffff)
Jonathan Austin 0:bc2961fa1ef0 455 )
Jonathan Austin 0:bc2961fa1ef0 456 )
Jonathan Austin 0:bc2961fa1ef0 457 {
Jonathan Austin 0:bc2961fa1ef0 458 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 459 }
Jonathan Austin 0:bc2961fa1ef0 460
Jonathan Austin 0:bc2961fa1ef0 461 // Make sure Minimum Connection Interval is not bigger than Maximum Connection Interval.
Jonathan Austin 0:bc2961fa1ef0 462 if ((p_conn_int->min_conn_interval != 0xffff) &&
Jonathan Austin 0:bc2961fa1ef0 463 (p_conn_int->max_conn_interval != 0xffff) &&
Jonathan Austin 0:bc2961fa1ef0 464 (p_conn_int->min_conn_interval > p_conn_int->max_conn_interval)
Jonathan Austin 0:bc2961fa1ef0 465 )
Jonathan Austin 0:bc2961fa1ef0 466 {
Jonathan Austin 0:bc2961fa1ef0 467 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 468 }
Jonathan Austin 0:bc2961fa1ef0 469
Jonathan Austin 0:bc2961fa1ef0 470 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 471 }
Jonathan Austin 0:bc2961fa1ef0 472
Jonathan Austin 0:bc2961fa1ef0 473
Jonathan Austin 0:bc2961fa1ef0 474 static uint32_t conn_int_encode(const ble_advdata_conn_int_t * p_conn_int,
Jonathan Austin 0:bc2961fa1ef0 475 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 476 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 477 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 478 {
Jonathan Austin 0:bc2961fa1ef0 479 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 480
Jonathan Austin 0:bc2961fa1ef0 481 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 482 if (((*p_offset) + AD_TYPE_CONN_INT_SIZE) > max_size)
Jonathan Austin 0:bc2961fa1ef0 483 {
Jonathan Austin 0:bc2961fa1ef0 484 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 485 }
Jonathan Austin 0:bc2961fa1ef0 486
Jonathan Austin 0:bc2961fa1ef0 487 // Check parameters.
Jonathan Austin 0:bc2961fa1ef0 488 err_code = conn_int_check(p_conn_int);
Jonathan Austin 0:bc2961fa1ef0 489 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 490 {
Jonathan Austin 0:bc2961fa1ef0 491 return err_code;
Jonathan Austin 0:bc2961fa1ef0 492 }
Jonathan Austin 0:bc2961fa1ef0 493
Jonathan Austin 0:bc2961fa1ef0 494 // Encode Length and AD Type.
Jonathan Austin 0:bc2961fa1ef0 495 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_CONN_INT_DATA_SIZE);
Jonathan Austin 0:bc2961fa1ef0 496 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 497 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE;
Jonathan Austin 0:bc2961fa1ef0 498 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 499
Jonathan Austin 0:bc2961fa1ef0 500 // Encode Minimum and Maximum Connection Intervals.
Jonathan Austin 0:bc2961fa1ef0 501 *p_offset += uint16_encode(p_conn_int->min_conn_interval, &p_encoded_data[*p_offset]);
Jonathan Austin 0:bc2961fa1ef0 502 *p_offset += uint16_encode(p_conn_int->max_conn_interval, &p_encoded_data[*p_offset]);
Jonathan Austin 0:bc2961fa1ef0 503
Jonathan Austin 0:bc2961fa1ef0 504 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 505 }
Jonathan Austin 0:bc2961fa1ef0 506
Jonathan Austin 0:bc2961fa1ef0 507
Jonathan Austin 0:bc2961fa1ef0 508 static uint32_t manuf_specific_data_encode(const ble_advdata_manuf_data_t * p_manuf_sp_data,
Jonathan Austin 0:bc2961fa1ef0 509 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 510 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 511 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 512 {
Jonathan Austin 0:bc2961fa1ef0 513 uint32_t data_size = AD_TYPE_MANUF_SPEC_DATA_ID_SIZE + p_manuf_sp_data->data.size;
Jonathan Austin 0:bc2961fa1ef0 514
Jonathan Austin 0:bc2961fa1ef0 515 // Check for buffer overflow.
Jonathan Austin 0:bc2961fa1ef0 516 if (((*p_offset) + ADV_AD_DATA_OFFSET + data_size) > max_size)
Jonathan Austin 0:bc2961fa1ef0 517 {
Jonathan Austin 0:bc2961fa1ef0 518 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 519 }
Jonathan Austin 0:bc2961fa1ef0 520
Jonathan Austin 0:bc2961fa1ef0 521 // There is only 1 byte intended to encode length which is (data_size + ADV_AD_TYPE_FIELD_SIZE)
Jonathan Austin 0:bc2961fa1ef0 522 if(data_size > (0x00FF - ADV_AD_TYPE_FIELD_SIZE))
Jonathan Austin 0:bc2961fa1ef0 523 {
Jonathan Austin 0:bc2961fa1ef0 524 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 525 }
Jonathan Austin 0:bc2961fa1ef0 526
Jonathan Austin 0:bc2961fa1ef0 527 // Encode Length and AD Type.
Jonathan Austin 0:bc2961fa1ef0 528 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + data_size);
Jonathan Austin 0:bc2961fa1ef0 529 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 530 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
Jonathan Austin 0:bc2961fa1ef0 531 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 532
Jonathan Austin 0:bc2961fa1ef0 533 // Encode Company Identifier.
Jonathan Austin 0:bc2961fa1ef0 534 *p_offset += uint16_encode(p_manuf_sp_data->company_identifier, &p_encoded_data[*p_offset]);
Jonathan Austin 0:bc2961fa1ef0 535
Jonathan Austin 0:bc2961fa1ef0 536 // Encode additional manufacturer specific data.
Jonathan Austin 0:bc2961fa1ef0 537 if (p_manuf_sp_data->data.size > 0)
Jonathan Austin 0:bc2961fa1ef0 538 {
Jonathan Austin 0:bc2961fa1ef0 539 if (p_manuf_sp_data->data.p_data == NULL)
Jonathan Austin 0:bc2961fa1ef0 540 {
Jonathan Austin 0:bc2961fa1ef0 541 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 542 }
Jonathan Austin 0:bc2961fa1ef0 543 memcpy(&p_encoded_data[*p_offset], p_manuf_sp_data->data.p_data, p_manuf_sp_data->data.size);
Jonathan Austin 0:bc2961fa1ef0 544 *p_offset += p_manuf_sp_data->data.size;
Jonathan Austin 0:bc2961fa1ef0 545 }
Jonathan Austin 0:bc2961fa1ef0 546
Jonathan Austin 0:bc2961fa1ef0 547 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 548 }
Jonathan Austin 0:bc2961fa1ef0 549
Jonathan Austin 0:bc2961fa1ef0 550 // Implemented only for 16-bit UUIDs
Jonathan Austin 0:bc2961fa1ef0 551 static uint32_t service_data_encode(const ble_advdata_t * p_advdata,
Jonathan Austin 0:bc2961fa1ef0 552 uint8_t * p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 553 uint16_t * p_offset,
Jonathan Austin 0:bc2961fa1ef0 554 uint16_t max_size)
Jonathan Austin 0:bc2961fa1ef0 555 {
Jonathan Austin 0:bc2961fa1ef0 556 uint8_t i;
Jonathan Austin 0:bc2961fa1ef0 557
Jonathan Austin 0:bc2961fa1ef0 558 // Check parameter consistency.
Jonathan Austin 0:bc2961fa1ef0 559 if (p_advdata->p_service_data_array == NULL)
Jonathan Austin 0:bc2961fa1ef0 560 {
Jonathan Austin 0:bc2961fa1ef0 561 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 562 }
Jonathan Austin 0:bc2961fa1ef0 563
Jonathan Austin 0:bc2961fa1ef0 564 for (i = 0; i < p_advdata->service_data_count; i++)
Jonathan Austin 0:bc2961fa1ef0 565 {
Jonathan Austin 0:bc2961fa1ef0 566 ble_advdata_service_data_t * p_service_data;
Jonathan Austin 0:bc2961fa1ef0 567 uint32_t data_size;
Jonathan Austin 0:bc2961fa1ef0 568
Jonathan Austin 0:bc2961fa1ef0 569 p_service_data = &p_advdata->p_service_data_array[i];
Jonathan Austin 0:bc2961fa1ef0 570 // For now implemented only for 16-bit UUIDs
Jonathan Austin 0:bc2961fa1ef0 571 data_size = AD_TYPE_SERV_DATA_16BIT_UUID_SIZE + p_service_data->data.size;
Jonathan Austin 0:bc2961fa1ef0 572
Jonathan Austin 0:bc2961fa1ef0 573 // There is only 1 byte intended to encode length which is (data_size + ADV_AD_TYPE_FIELD_SIZE)
Jonathan Austin 0:bc2961fa1ef0 574 if(data_size > (0x00FF - ADV_AD_TYPE_FIELD_SIZE))
Jonathan Austin 0:bc2961fa1ef0 575 {
Jonathan Austin 0:bc2961fa1ef0 576 return NRF_ERROR_DATA_SIZE;
Jonathan Austin 0:bc2961fa1ef0 577 }
Jonathan Austin 0:bc2961fa1ef0 578
Jonathan Austin 0:bc2961fa1ef0 579 // Encode Length and AD Type.
Jonathan Austin 0:bc2961fa1ef0 580 p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + data_size);
Jonathan Austin 0:bc2961fa1ef0 581 *p_offset += ADV_LENGTH_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 582 p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SERVICE_DATA;
Jonathan Austin 0:bc2961fa1ef0 583 *p_offset += ADV_AD_TYPE_FIELD_SIZE;
Jonathan Austin 0:bc2961fa1ef0 584
Jonathan Austin 0:bc2961fa1ef0 585 // Encode service 16-bit UUID.
Jonathan Austin 0:bc2961fa1ef0 586 *p_offset += uint16_encode(p_service_data->service_uuid, &p_encoded_data[*p_offset]);
Jonathan Austin 0:bc2961fa1ef0 587
Jonathan Austin 0:bc2961fa1ef0 588 // Encode additional service data.
Jonathan Austin 0:bc2961fa1ef0 589 if (p_service_data->data.size > 0)
Jonathan Austin 0:bc2961fa1ef0 590 {
Jonathan Austin 0:bc2961fa1ef0 591 if (p_service_data->data.p_data == NULL)
Jonathan Austin 0:bc2961fa1ef0 592 {
Jonathan Austin 0:bc2961fa1ef0 593 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 594 }
Jonathan Austin 0:bc2961fa1ef0 595 memcpy(&p_encoded_data[*p_offset], p_service_data->data.p_data, p_service_data->data.size);
Jonathan Austin 0:bc2961fa1ef0 596 *p_offset += p_service_data->data.size;
Jonathan Austin 0:bc2961fa1ef0 597 }
Jonathan Austin 0:bc2961fa1ef0 598 }
Jonathan Austin 0:bc2961fa1ef0 599
Jonathan Austin 0:bc2961fa1ef0 600 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 601 }
Jonathan Austin 0:bc2961fa1ef0 602
Jonathan Austin 0:bc2961fa1ef0 603 uint32_t adv_data_encode(ble_advdata_t const * const p_advdata,
Jonathan Austin 0:bc2961fa1ef0 604 uint8_t * const p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 605 uint16_t * const p_len)
Jonathan Austin 0:bc2961fa1ef0 606 {
Jonathan Austin 0:bc2961fa1ef0 607 uint32_t err_code = NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 608 uint16_t max_size = *p_len;
Jonathan Austin 0:bc2961fa1ef0 609 *p_len = 0;
Jonathan Austin 0:bc2961fa1ef0 610
Jonathan Austin 0:bc2961fa1ef0 611 //Encode Security Manager OOB Flags
Jonathan Austin 0:bc2961fa1ef0 612 if (p_advdata->p_sec_mgr_oob_flags != NULL)
Jonathan Austin 0:bc2961fa1ef0 613 {
Jonathan Austin 0:bc2961fa1ef0 614 err_code = sec_mgr_oob_flags_encode(*p_advdata->p_sec_mgr_oob_flags,
Jonathan Austin 0:bc2961fa1ef0 615 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 616 p_len,
Jonathan Austin 0:bc2961fa1ef0 617 max_size);
Jonathan Austin 0:bc2961fa1ef0 618 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 619 {
Jonathan Austin 0:bc2961fa1ef0 620 return err_code;
Jonathan Austin 0:bc2961fa1ef0 621 }
Jonathan Austin 0:bc2961fa1ef0 622 }
Jonathan Austin 0:bc2961fa1ef0 623
Jonathan Austin 0:bc2961fa1ef0 624 // Encode Security Manager TK value
Jonathan Austin 0:bc2961fa1ef0 625 if (NULL != p_advdata->p_tk_value)
Jonathan Austin 0:bc2961fa1ef0 626 {
Jonathan Austin 0:bc2961fa1ef0 627 err_code = tk_value_encode(p_advdata->p_tk_value, p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 628 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 629 {
Jonathan Austin 0:bc2961fa1ef0 630 return err_code;
Jonathan Austin 0:bc2961fa1ef0 631 }
Jonathan Austin 0:bc2961fa1ef0 632 }
Jonathan Austin 0:bc2961fa1ef0 633
Jonathan Austin 0:bc2961fa1ef0 634 // Encode LE Role
Jonathan Austin 0:bc2961fa1ef0 635 if (BLE_ADVDATA_ROLE_NOT_PRESENT != p_advdata->le_role)
Jonathan Austin 0:bc2961fa1ef0 636 {
Jonathan Austin 0:bc2961fa1ef0 637 err_code = le_role_encode(p_advdata->le_role, p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 638 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 639 {
Jonathan Austin 0:bc2961fa1ef0 640 return err_code;
Jonathan Austin 0:bc2961fa1ef0 641 }
Jonathan Austin 0:bc2961fa1ef0 642 }
Jonathan Austin 0:bc2961fa1ef0 643
Jonathan Austin 0:bc2961fa1ef0 644 // Encode LE Bluetooth Device Address
Jonathan Austin 0:bc2961fa1ef0 645 if (p_advdata->include_ble_device_addr)
Jonathan Austin 0:bc2961fa1ef0 646 {
Jonathan Austin 0:bc2961fa1ef0 647 err_code = ble_device_addr_encode(p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 648 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 649 {
Jonathan Austin 0:bc2961fa1ef0 650 return err_code;
Jonathan Austin 0:bc2961fa1ef0 651 }
Jonathan Austin 0:bc2961fa1ef0 652 }
Jonathan Austin 0:bc2961fa1ef0 653
Jonathan Austin 0:bc2961fa1ef0 654 // Encode appearance.
Jonathan Austin 0:bc2961fa1ef0 655 if (p_advdata->include_appearance)
Jonathan Austin 0:bc2961fa1ef0 656 {
Jonathan Austin 0:bc2961fa1ef0 657 err_code = appearance_encode(p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 658 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 659 {
Jonathan Austin 0:bc2961fa1ef0 660 return err_code;
Jonathan Austin 0:bc2961fa1ef0 661 }
Jonathan Austin 0:bc2961fa1ef0 662 }
Jonathan Austin 0:bc2961fa1ef0 663
Jonathan Austin 0:bc2961fa1ef0 664 //Encode Flags
Jonathan Austin 0:bc2961fa1ef0 665 if(p_advdata->flags != 0 )
Jonathan Austin 0:bc2961fa1ef0 666 {
Jonathan Austin 0:bc2961fa1ef0 667 err_code = flags_encode(p_advdata->flags, p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 668 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 669 {
Jonathan Austin 0:bc2961fa1ef0 670 return err_code;
Jonathan Austin 0:bc2961fa1ef0 671 }
Jonathan Austin 0:bc2961fa1ef0 672 }
Jonathan Austin 0:bc2961fa1ef0 673
Jonathan Austin 0:bc2961fa1ef0 674 // Encode TX power level.
Jonathan Austin 0:bc2961fa1ef0 675 if (p_advdata->p_tx_power_level != NULL)
Jonathan Austin 0:bc2961fa1ef0 676 {
Jonathan Austin 0:bc2961fa1ef0 677 err_code = tx_power_level_encode(*p_advdata->p_tx_power_level,
Jonathan Austin 0:bc2961fa1ef0 678 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 679 p_len,
Jonathan Austin 0:bc2961fa1ef0 680 max_size);
Jonathan Austin 0:bc2961fa1ef0 681 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 682 {
Jonathan Austin 0:bc2961fa1ef0 683 return err_code;
Jonathan Austin 0:bc2961fa1ef0 684 }
Jonathan Austin 0:bc2961fa1ef0 685 }
Jonathan Austin 0:bc2961fa1ef0 686
Jonathan Austin 0:bc2961fa1ef0 687 // Encode 'more available' uuid list.
Jonathan Austin 0:bc2961fa1ef0 688 if (p_advdata->uuids_more_available.uuid_cnt > 0)
Jonathan Austin 0:bc2961fa1ef0 689 {
Jonathan Austin 0:bc2961fa1ef0 690 err_code = uuid_list_encode(&p_advdata->uuids_more_available,
Jonathan Austin 0:bc2961fa1ef0 691 BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE,
Jonathan Austin 0:bc2961fa1ef0 692 BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE,
Jonathan Austin 0:bc2961fa1ef0 693 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 694 p_len,
Jonathan Austin 0:bc2961fa1ef0 695 max_size);
Jonathan Austin 0:bc2961fa1ef0 696 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 697 {
Jonathan Austin 0:bc2961fa1ef0 698 return err_code;
Jonathan Austin 0:bc2961fa1ef0 699 }
Jonathan Austin 0:bc2961fa1ef0 700 }
Jonathan Austin 0:bc2961fa1ef0 701
Jonathan Austin 0:bc2961fa1ef0 702 // Encode 'complete' uuid list.
Jonathan Austin 0:bc2961fa1ef0 703 if (p_advdata->uuids_complete.uuid_cnt > 0)
Jonathan Austin 0:bc2961fa1ef0 704 {
Jonathan Austin 0:bc2961fa1ef0 705 err_code = uuid_list_encode(&p_advdata->uuids_complete,
Jonathan Austin 0:bc2961fa1ef0 706 BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE,
Jonathan Austin 0:bc2961fa1ef0 707 BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE,
Jonathan Austin 0:bc2961fa1ef0 708 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 709 p_len,
Jonathan Austin 0:bc2961fa1ef0 710 max_size);
Jonathan Austin 0:bc2961fa1ef0 711 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 712 {
Jonathan Austin 0:bc2961fa1ef0 713 return err_code;
Jonathan Austin 0:bc2961fa1ef0 714 }
Jonathan Austin 0:bc2961fa1ef0 715 }
Jonathan Austin 0:bc2961fa1ef0 716
Jonathan Austin 0:bc2961fa1ef0 717 // Encode 'solicited service' uuid list.
Jonathan Austin 0:bc2961fa1ef0 718 if (p_advdata->uuids_solicited.uuid_cnt > 0)
Jonathan Austin 0:bc2961fa1ef0 719 {
Jonathan Austin 0:bc2961fa1ef0 720 err_code = uuid_list_encode(&p_advdata->uuids_solicited,
Jonathan Austin 0:bc2961fa1ef0 721 BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT,
Jonathan Austin 0:bc2961fa1ef0 722 BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT,
Jonathan Austin 0:bc2961fa1ef0 723 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 724 p_len,
Jonathan Austin 0:bc2961fa1ef0 725 max_size);
Jonathan Austin 0:bc2961fa1ef0 726 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 727 {
Jonathan Austin 0:bc2961fa1ef0 728 return err_code;
Jonathan Austin 0:bc2961fa1ef0 729 }
Jonathan Austin 0:bc2961fa1ef0 730 }
Jonathan Austin 0:bc2961fa1ef0 731
Jonathan Austin 0:bc2961fa1ef0 732 // Encode Slave Connection Interval Range.
Jonathan Austin 0:bc2961fa1ef0 733 if (p_advdata->p_slave_conn_int != NULL)
Jonathan Austin 0:bc2961fa1ef0 734 {
Jonathan Austin 0:bc2961fa1ef0 735 err_code = conn_int_encode(p_advdata->p_slave_conn_int, p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 736 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 737 {
Jonathan Austin 0:bc2961fa1ef0 738 return err_code;
Jonathan Austin 0:bc2961fa1ef0 739 }
Jonathan Austin 0:bc2961fa1ef0 740 }
Jonathan Austin 0:bc2961fa1ef0 741
Jonathan Austin 0:bc2961fa1ef0 742 // Encode Manufacturer Specific Data.
Jonathan Austin 0:bc2961fa1ef0 743 if (p_advdata->p_manuf_specific_data != NULL)
Jonathan Austin 0:bc2961fa1ef0 744 {
Jonathan Austin 0:bc2961fa1ef0 745 err_code = manuf_specific_data_encode(p_advdata->p_manuf_specific_data,
Jonathan Austin 0:bc2961fa1ef0 746 p_encoded_data,
Jonathan Austin 0:bc2961fa1ef0 747 p_len,
Jonathan Austin 0:bc2961fa1ef0 748 max_size);
Jonathan Austin 0:bc2961fa1ef0 749 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 750 {
Jonathan Austin 0:bc2961fa1ef0 751 return err_code;
Jonathan Austin 0:bc2961fa1ef0 752 }
Jonathan Austin 0:bc2961fa1ef0 753 }
Jonathan Austin 0:bc2961fa1ef0 754
Jonathan Austin 0:bc2961fa1ef0 755 // Encode Service Data.
Jonathan Austin 0:bc2961fa1ef0 756 if (p_advdata->service_data_count > 0)
Jonathan Austin 0:bc2961fa1ef0 757 {
Jonathan Austin 0:bc2961fa1ef0 758 err_code = service_data_encode(p_advdata, p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 759 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 760 {
Jonathan Austin 0:bc2961fa1ef0 761 return err_code;
Jonathan Austin 0:bc2961fa1ef0 762 }
Jonathan Austin 0:bc2961fa1ef0 763 }
Jonathan Austin 0:bc2961fa1ef0 764
Jonathan Austin 0:bc2961fa1ef0 765 // Encode name. WARNING: it is encoded last on purpose since too long device name is truncated.
Jonathan Austin 0:bc2961fa1ef0 766 if (p_advdata->name_type != BLE_ADVDATA_NO_NAME)
Jonathan Austin 0:bc2961fa1ef0 767 {
Jonathan Austin 0:bc2961fa1ef0 768 err_code = name_encode(p_advdata, p_encoded_data, p_len, max_size);
Jonathan Austin 0:bc2961fa1ef0 769 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 770 {
Jonathan Austin 0:bc2961fa1ef0 771 return err_code;
Jonathan Austin 0:bc2961fa1ef0 772 }
Jonathan Austin 0:bc2961fa1ef0 773 }
Jonathan Austin 0:bc2961fa1ef0 774
Jonathan Austin 0:bc2961fa1ef0 775 return err_code;
Jonathan Austin 0:bc2961fa1ef0 776 }
Jonathan Austin 0:bc2961fa1ef0 777
Jonathan Austin 0:bc2961fa1ef0 778
Jonathan Austin 0:bc2961fa1ef0 779 static uint32_t advdata_check(const ble_advdata_t * p_advdata)
Jonathan Austin 0:bc2961fa1ef0 780 {
Jonathan Austin 0:bc2961fa1ef0 781 // Flags must be included in advertising data, and the BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED flag must be set.
Jonathan Austin 0:bc2961fa1ef0 782 if (
Jonathan Austin 0:bc2961fa1ef0 783 ((p_advdata->flags & BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) == 0)
Jonathan Austin 0:bc2961fa1ef0 784 )
Jonathan Austin 0:bc2961fa1ef0 785 {
Jonathan Austin 0:bc2961fa1ef0 786 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 787 }
Jonathan Austin 0:bc2961fa1ef0 788
Jonathan Austin 0:bc2961fa1ef0 789 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 790 }
Jonathan Austin 0:bc2961fa1ef0 791
Jonathan Austin 0:bc2961fa1ef0 792
Jonathan Austin 0:bc2961fa1ef0 793 static uint32_t srdata_check(const ble_advdata_t * p_srdata)
Jonathan Austin 0:bc2961fa1ef0 794 {
Jonathan Austin 0:bc2961fa1ef0 795 // Flags shall not be included in the scan response data.
Jonathan Austin 0:bc2961fa1ef0 796 if (p_srdata->flags)
Jonathan Austin 0:bc2961fa1ef0 797 {
Jonathan Austin 0:bc2961fa1ef0 798 return NRF_ERROR_INVALID_PARAM;
Jonathan Austin 0:bc2961fa1ef0 799 }
Jonathan Austin 0:bc2961fa1ef0 800
Jonathan Austin 0:bc2961fa1ef0 801 return NRF_SUCCESS;
Jonathan Austin 0:bc2961fa1ef0 802 }
Jonathan Austin 0:bc2961fa1ef0 803
Jonathan Austin 0:bc2961fa1ef0 804
Jonathan Austin 0:bc2961fa1ef0 805 uint32_t ble_advdata_set(const ble_advdata_t * p_advdata, const ble_advdata_t * p_srdata)
Jonathan Austin 0:bc2961fa1ef0 806 {
Jonathan Austin 0:bc2961fa1ef0 807 uint32_t err_code;
Jonathan Austin 0:bc2961fa1ef0 808 uint16_t len_advdata = BLE_GAP_ADV_MAX_SIZE;
Jonathan Austin 0:bc2961fa1ef0 809 uint16_t len_srdata = BLE_GAP_ADV_MAX_SIZE;
Jonathan Austin 0:bc2961fa1ef0 810 uint8_t encoded_advdata[BLE_GAP_ADV_MAX_SIZE];
Jonathan Austin 0:bc2961fa1ef0 811 uint8_t encoded_srdata[BLE_GAP_ADV_MAX_SIZE];
Jonathan Austin 0:bc2961fa1ef0 812 uint8_t * p_encoded_advdata;
Jonathan Austin 0:bc2961fa1ef0 813 uint8_t * p_encoded_srdata;
Jonathan Austin 0:bc2961fa1ef0 814
Jonathan Austin 0:bc2961fa1ef0 815 // Encode advertising data (if supplied).
Jonathan Austin 0:bc2961fa1ef0 816 if (p_advdata != NULL)
Jonathan Austin 0:bc2961fa1ef0 817 {
Jonathan Austin 0:bc2961fa1ef0 818 err_code = advdata_check(p_advdata);
Jonathan Austin 0:bc2961fa1ef0 819 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 820 {
Jonathan Austin 0:bc2961fa1ef0 821 return err_code;
Jonathan Austin 0:bc2961fa1ef0 822 }
Jonathan Austin 0:bc2961fa1ef0 823
Jonathan Austin 0:bc2961fa1ef0 824 err_code = adv_data_encode(p_advdata, encoded_advdata, &len_advdata);
Jonathan Austin 0:bc2961fa1ef0 825 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 826 {
Jonathan Austin 0:bc2961fa1ef0 827 return err_code;
Jonathan Austin 0:bc2961fa1ef0 828 }
Jonathan Austin 0:bc2961fa1ef0 829 p_encoded_advdata = encoded_advdata;
Jonathan Austin 0:bc2961fa1ef0 830 }
Jonathan Austin 0:bc2961fa1ef0 831 else
Jonathan Austin 0:bc2961fa1ef0 832 {
Jonathan Austin 0:bc2961fa1ef0 833 p_encoded_advdata = NULL;
Jonathan Austin 0:bc2961fa1ef0 834 len_advdata = 0;
Jonathan Austin 0:bc2961fa1ef0 835 }
Jonathan Austin 0:bc2961fa1ef0 836
Jonathan Austin 0:bc2961fa1ef0 837 // Encode scan response data (if supplied).
Jonathan Austin 0:bc2961fa1ef0 838 if (p_srdata != NULL)
Jonathan Austin 0:bc2961fa1ef0 839 {
Jonathan Austin 0:bc2961fa1ef0 840 err_code = srdata_check(p_srdata);
Jonathan Austin 0:bc2961fa1ef0 841 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 842 {
Jonathan Austin 0:bc2961fa1ef0 843 return err_code;
Jonathan Austin 0:bc2961fa1ef0 844 }
Jonathan Austin 0:bc2961fa1ef0 845
Jonathan Austin 0:bc2961fa1ef0 846 err_code = adv_data_encode(p_srdata, encoded_srdata, &len_srdata);
Jonathan Austin 0:bc2961fa1ef0 847 if (err_code != NRF_SUCCESS)
Jonathan Austin 0:bc2961fa1ef0 848 {
Jonathan Austin 0:bc2961fa1ef0 849 return err_code;
Jonathan Austin 0:bc2961fa1ef0 850 }
Jonathan Austin 0:bc2961fa1ef0 851 p_encoded_srdata = encoded_srdata;
Jonathan Austin 0:bc2961fa1ef0 852 }
Jonathan Austin 0:bc2961fa1ef0 853 else
Jonathan Austin 0:bc2961fa1ef0 854 {
Jonathan Austin 0:bc2961fa1ef0 855 p_encoded_srdata = NULL;
Jonathan Austin 0:bc2961fa1ef0 856 len_srdata = 0;
Jonathan Austin 0:bc2961fa1ef0 857 }
Jonathan Austin 0:bc2961fa1ef0 858
Jonathan Austin 0:bc2961fa1ef0 859 // Pass encoded advertising data and/or scan response data to the stack.
Jonathan Austin 0:bc2961fa1ef0 860 return sd_ble_gap_adv_data_set(p_encoded_advdata, len_advdata, p_encoded_srdata, len_srdata);
Jonathan Austin 0:bc2961fa1ef0 861 }