No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
vcoubard
Date:
Mon Jan 11 10:19:02 2016 +0000
Revision:
541:884f95bf5351
Parent:
535:f9ab1b333b44
Child:
552:20b282c26f96
Synchronized with git rev 60a7c0c0
Author: Rohit Grover
bring in the latest changes for BLE::init() where we allow <object, member> tuples for init callback.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 541:884f95bf5351 1 /* mbed Microcontroller Library
vcoubard 541:884f95bf5351 2 * Copyright (c) 2006-2013 ARM Limited
vcoubard 541:884f95bf5351 3 *
vcoubard 541:884f95bf5351 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 541:884f95bf5351 5 * you may not use this file except in compliance with the License.
vcoubard 541:884f95bf5351 6 * You may obtain a copy of the License at
vcoubard 541:884f95bf5351 7 *
vcoubard 541:884f95bf5351 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 541:884f95bf5351 9 *
vcoubard 541:884f95bf5351 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 541:884f95bf5351 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 541:884f95bf5351 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 541:884f95bf5351 13 * See the License for the specific language governing permissions and
vcoubard 541:884f95bf5351 14 * limitations under the License.
vcoubard 541:884f95bf5351 15 */
vcoubard 541:884f95bf5351 16
vcoubard 541:884f95bf5351 17 #include "custom_helper.h"
vcoubard 541:884f95bf5351 18
vcoubard 541:884f95bf5351 19 /*
vcoubard 541:884f95bf5351 20 * The current version of the soft-device doesn't handle duplicate 128-bit UUIDs
vcoubard 541:884f95bf5351 21 * very well. It is therefore necessary to filter away duplicates before
vcoubard 541:884f95bf5351 22 * passing long UUIDs to sd_ble_uuid_vs_add(). The following types and data
vcoubard 541:884f95bf5351 23 * structures involved in maintaining a local cache of 128-bit UUIDs.
vcoubard 541:884f95bf5351 24 */
vcoubard 541:884f95bf5351 25 typedef struct {
vcoubard 541:884f95bf5351 26 UUID::LongUUIDBytes_t uuid;
vcoubard 541:884f95bf5351 27 uint8_t type;
vcoubard 541:884f95bf5351 28 } converted_uuid_table_entry_t;
vcoubard 541:884f95bf5351 29 static const unsigned UUID_TABLE_MAX_ENTRIES = 4; /* This is the maximum number of 128-bit UUIDs with distinct bases that
vcoubard 541:884f95bf5351 30 * we expect to be in use; increase this limit if needed. */
vcoubard 541:884f95bf5351 31 static unsigned uuidTableEntries = 0; /* current usage of the table */
vcoubard 541:884f95bf5351 32 converted_uuid_table_entry_t convertedUUIDTable[UUID_TABLE_MAX_ENTRIES];
vcoubard 541:884f95bf5351 33
vcoubard 541:884f95bf5351 34 /**
vcoubard 541:884f95bf5351 35 * lookup the cache of previously converted 128-bit UUIDs to find a type value.
vcoubard 541:884f95bf5351 36 * @param uuid base 128-bit UUID
vcoubard 541:884f95bf5351 37 * @param recoveredType the type field of the 3-byte nRF's uuid.
vcoubard 541:884f95bf5351 38 * @return true if a match is found.
vcoubard 541:884f95bf5351 39 */
vcoubard 541:884f95bf5351 40 static bool
vcoubard 541:884f95bf5351 41 lookupConvertedUUIDTable(const UUID::LongUUIDBytes_t uuid, uint8_t *recoveredType)
vcoubard 541:884f95bf5351 42 {
vcoubard 541:884f95bf5351 43 unsigned i;
vcoubard 541:884f95bf5351 44 for (i = 0; i < uuidTableEntries; i++) {
vcoubard 541:884f95bf5351 45 unsigned byteIndex;
vcoubard 541:884f95bf5351 46 for (byteIndex = 0; byteIndex < UUID::LENGTH_OF_LONG_UUID; byteIndex++) {
vcoubard 541:884f95bf5351 47 /* Skip bytes 2 and 3, because they contain the shortUUID (16-bit) version of the
vcoubard 541:884f95bf5351 48 * long UUID; and we're comparing against the remainder. */
vcoubard 541:884f95bf5351 49 if ((byteIndex == 2) || (byteIndex == 3)) {
vcoubard 541:884f95bf5351 50 continue;
vcoubard 541:884f95bf5351 51 }
vcoubard 541:884f95bf5351 52
vcoubard 541:884f95bf5351 53 if (convertedUUIDTable[i].uuid[byteIndex] != uuid[byteIndex]) {
vcoubard 541:884f95bf5351 54 break;
vcoubard 541:884f95bf5351 55 }
vcoubard 541:884f95bf5351 56 }
vcoubard 541:884f95bf5351 57
vcoubard 541:884f95bf5351 58 if (byteIndex == UUID::LENGTH_OF_LONG_UUID) {
vcoubard 541:884f95bf5351 59 *recoveredType = convertedUUIDTable[i].type;
vcoubard 541:884f95bf5351 60 return true;
vcoubard 541:884f95bf5351 61 }
vcoubard 541:884f95bf5351 62 }
vcoubard 541:884f95bf5351 63
vcoubard 541:884f95bf5351 64 return false;
vcoubard 541:884f95bf5351 65 }
vcoubard 541:884f95bf5351 66
vcoubard 541:884f95bf5351 67 static void
vcoubard 541:884f95bf5351 68 addToConvertedUUIDTable(const UUID::LongUUIDBytes_t uuid, uint8_t type)
vcoubard 541:884f95bf5351 69 {
vcoubard 541:884f95bf5351 70 if (uuidTableEntries == UUID_TABLE_MAX_ENTRIES) {
vcoubard 541:884f95bf5351 71 return; /* recovery needed; or at least the user should be warned about this fact.*/
vcoubard 541:884f95bf5351 72 }
vcoubard 541:884f95bf5351 73
vcoubard 541:884f95bf5351 74 memcpy(convertedUUIDTable[uuidTableEntries].uuid, uuid, UUID::LENGTH_OF_LONG_UUID);
vcoubard 541:884f95bf5351 75 convertedUUIDTable[uuidTableEntries].uuid[2] = 0;
vcoubard 541:884f95bf5351 76 convertedUUIDTable[uuidTableEntries].uuid[3] = 0;
vcoubard 541:884f95bf5351 77 convertedUUIDTable[uuidTableEntries].type = type;
vcoubard 541:884f95bf5351 78 uuidTableEntries++;
vcoubard 541:884f95bf5351 79 }
vcoubard 541:884f95bf5351 80
vcoubard 541:884f95bf5351 81 /**
vcoubard 541:884f95bf5351 82 * The nRF transport has its own 3-byte representation of a UUID. If the user-
vcoubard 541:884f95bf5351 83 * specified UUID is 128-bits wide, then the UUID base needs to be added to the
vcoubard 541:884f95bf5351 84 * soft-device and converted to a 3-byte handle before being used further. This
vcoubard 541:884f95bf5351 85 * function is responsible for this translation of user-specified UUIDs into
vcoubard 541:884f95bf5351 86 * nRF's representation.
vcoubard 541:884f95bf5351 87 *
vcoubard 541:884f95bf5351 88 * @param[in] uuid
vcoubard 541:884f95bf5351 89 * user-specified UUID
vcoubard 541:884f95bf5351 90 * @return nRF
vcoubard 541:884f95bf5351 91 * 3-byte UUID (containing a type and 16-bit UUID) representation
vcoubard 541:884f95bf5351 92 * to be used with SVC calls.
vcoubard 541:884f95bf5351 93 */
vcoubard 541:884f95bf5351 94 ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid)
vcoubard 541:884f95bf5351 95 {
vcoubard 541:884f95bf5351 96 ble_uuid_t nordicUUID;
vcoubard 541:884f95bf5351 97 nordicUUID.uuid = uuid.getShortUUID();
vcoubard 541:884f95bf5351 98 nordicUUID.type = BLE_UUID_TYPE_UNKNOWN; /* to be set below */
vcoubard 541:884f95bf5351 99
vcoubard 541:884f95bf5351 100 if (uuid.shortOrLong() == UUID::UUID_TYPE_SHORT) {
vcoubard 541:884f95bf5351 101 nordicUUID.type = BLE_UUID_TYPE_BLE;
vcoubard 541:884f95bf5351 102 } else {
vcoubard 541:884f95bf5351 103 if (!lookupConvertedUUIDTable(uuid.getBaseUUID(), &nordicUUID.type)) {
vcoubard 541:884f95bf5351 104 nordicUUID.type = custom_add_uuid_base(uuid.getBaseUUID());
vcoubard 541:884f95bf5351 105 addToConvertedUUIDTable(uuid.getBaseUUID(), nordicUUID.type);
vcoubard 541:884f95bf5351 106 }
vcoubard 541:884f95bf5351 107 }
vcoubard 541:884f95bf5351 108
vcoubard 541:884f95bf5351 109 return nordicUUID;
vcoubard 541:884f95bf5351 110 }
vcoubard 541:884f95bf5351 111
vcoubard 541:884f95bf5351 112 /**************************************************************************/
vcoubard 541:884f95bf5351 113 /*!
vcoubard 541:884f95bf5351 114 @brief Adds the base UUID to the custom service. All UUIDs used
vcoubard 541:884f95bf5351 115 by this service are based on this 128-bit UUID.
vcoubard 541:884f95bf5351 116
vcoubard 541:884f95bf5351 117 @note This UUID needs to be added to the SoftDevice stack before
vcoubard 541:884f95bf5351 118 adding the service's primary service via
vcoubard 541:884f95bf5351 119 'sd_ble_gatts_service_add'
vcoubard 541:884f95bf5351 120
vcoubard 541:884f95bf5351 121 @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16)
vcoubard 541:884f95bf5351 122
vcoubard 541:884f95bf5351 123 @returns The UUID type.
vcoubard 541:884f95bf5351 124 A return value of 0 should be considered an error.
vcoubard 541:884f95bf5351 125
vcoubard 541:884f95bf5351 126 @retval 0x00 BLE_UUID_TYPE_UNKNOWN
vcoubard 541:884f95bf5351 127 @retval 0x01 BLE_UUID_TYPE_BLE
vcoubard 541:884f95bf5351 128 @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN
vcoubard 541:884f95bf5351 129
vcoubard 541:884f95bf5351 130 @section EXAMPLE
vcoubard 541:884f95bf5351 131 @code
vcoubard 541:884f95bf5351 132
vcoubard 541:884f95bf5351 133 // Take note that bytes 2/3 are blank since these are used to identify
vcoubard 541:884f95bf5351 134 // the primary service and individual characteristics
vcoubard 541:884f95bf5351 135 #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E"
vcoubard 541:884f95bf5351 136
vcoubard 541:884f95bf5351 137 uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE);
vcoubard 541:884f95bf5351 138 ASSERT(uuid_type > 0, ERROR_NOT_FOUND);
vcoubard 541:884f95bf5351 139
vcoubard 541:884f95bf5351 140 // We can now safely add the primary service and any characteristics
vcoubard 541:884f95bf5351 141 // for our custom service ...
vcoubard 541:884f95bf5351 142
vcoubard 541:884f95bf5351 143 @endcode
vcoubard 541:884f95bf5351 144 */
vcoubard 541:884f95bf5351 145 /**************************************************************************/
vcoubard 541:884f95bf5351 146 uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base)
vcoubard 541:884f95bf5351 147 {
vcoubard 541:884f95bf5351 148 ble_uuid128_t base_uuid;
vcoubard 541:884f95bf5351 149 uint8_t uuid_type = 0;
vcoubard 541:884f95bf5351 150
vcoubard 541:884f95bf5351 151 /* Reverse the bytes since ble_uuid128_t is LSB */
vcoubard 541:884f95bf5351 152 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
vcoubard 541:884f95bf5351 153 base_uuid.uuid128[i] = p_uuid_base[UUID::LENGTH_OF_LONG_UUID - 1 - i];
vcoubard 541:884f95bf5351 154 }
vcoubard 541:884f95bf5351 155
vcoubard 541:884f95bf5351 156 ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0);
vcoubard 541:884f95bf5351 157
vcoubard 541:884f95bf5351 158 return uuid_type;
vcoubard 541:884f95bf5351 159 }
vcoubard 541:884f95bf5351 160
vcoubard 541:884f95bf5351 161 /**************************************************************************/
vcoubard 541:884f95bf5351 162 /*!
vcoubard 541:884f95bf5351 163
vcoubard 541:884f95bf5351 164 */
vcoubard 541:884f95bf5351 165 /**************************************************************************/
vcoubard 541:884f95bf5351 166 error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base,
vcoubard 541:884f95bf5351 167 ble_uuid_t *p_uuid)
vcoubard 541:884f95bf5351 168 {
vcoubard 541:884f95bf5351 169 UUID::LongUUIDBytes_t uuid_base_le;
vcoubard 541:884f95bf5351 170
vcoubard 541:884f95bf5351 171 /* Reverse the bytes since ble_uuid128_t is LSB */
vcoubard 541:884f95bf5351 172 for (uint8_t i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
vcoubard 541:884f95bf5351 173 uuid_base_le[i] = p_uuid_base[UUID::LENGTH_OF_LONG_UUID - 1 - i];
vcoubard 541:884f95bf5351 174 }
vcoubard 541:884f95bf5351 175
vcoubard 541:884f95bf5351 176 ASSERT_STATUS( sd_ble_uuid_decode(UUID::LENGTH_OF_LONG_UUID, uuid_base_le, p_uuid));
vcoubard 541:884f95bf5351 177
vcoubard 541:884f95bf5351 178 return ERROR_NONE;
vcoubard 541:884f95bf5351 179 }
vcoubard 541:884f95bf5351 180
vcoubard 541:884f95bf5351 181 /**************************************************************************/
vcoubard 541:884f95bf5351 182 /*!
vcoubard 541:884f95bf5351 183 @brief Adds a new characteristic to the custom service, assigning
vcoubard 541:884f95bf5351 184 properties, a UUID add-on value, etc.
vcoubard 541:884f95bf5351 185
vcoubard 541:884f95bf5351 186 @param[in] service_handle
vcoubard 541:884f95bf5351 187 @param[in] p_uuid The 16-bit value to add to the base UUID
vcoubard 541:884f95bf5351 188 for this characteristic (normally >1
vcoubard 541:884f95bf5351 189 since 1 is typically used by the primary
vcoubard 541:884f95bf5351 190 service).
vcoubard 541:884f95bf5351 191 @param[in] char_props The characteristic properties, as
vcoubard 541:884f95bf5351 192 defined by ble_gatt_char_props_t
vcoubard 541:884f95bf5351 193 @param[in] max_length The maximum length of this characeristic
vcoubard 541:884f95bf5351 194 @param[out] p_char_handle
vcoubard 541:884f95bf5351 195
vcoubard 541:884f95bf5351 196 @returns
vcoubard 541:884f95bf5351 197 @retval ERROR_NONE Everything executed normally
vcoubard 541:884f95bf5351 198 */
vcoubard 541:884f95bf5351 199 /**************************************************************************/
vcoubard 541:884f95bf5351 200 error_t custom_add_in_characteristic(uint16_t service_handle,
vcoubard 541:884f95bf5351 201 ble_uuid_t *p_uuid,
vcoubard 541:884f95bf5351 202 uint8_t properties,
vcoubard 541:884f95bf5351 203 SecurityManager::SecurityMode_t requiredSecurity,
vcoubard 541:884f95bf5351 204 uint8_t *p_data,
vcoubard 541:884f95bf5351 205 uint16_t min_length,
vcoubard 541:884f95bf5351 206 uint16_t max_length,
vcoubard 541:884f95bf5351 207 const uint8_t *userDescriptionDescriptorValuePtr,
vcoubard 541:884f95bf5351 208 uint16_t userDescriptionDescriptorValueLen,
vcoubard 541:884f95bf5351 209 bool readAuthorization,
vcoubard 541:884f95bf5351 210 bool writeAuthorization,
vcoubard 541:884f95bf5351 211 ble_gatts_char_handles_t *p_char_handle)
vcoubard 541:884f95bf5351 212 {
vcoubard 541:884f95bf5351 213 /* Characteristic metadata */
vcoubard 541:884f95bf5351 214 ble_gatts_attr_md_t cccd_md;
vcoubard 541:884f95bf5351 215 ble_gatt_char_props_t char_props;
vcoubard 541:884f95bf5351 216
vcoubard 541:884f95bf5351 217 memcpy(&char_props, &properties, 1);
vcoubard 541:884f95bf5351 218
vcoubard 541:884f95bf5351 219 if (char_props.notify || char_props.indicate) {
vcoubard 541:884f95bf5351 220 /* Notification requires cccd */
vcoubard 541:884f95bf5351 221 memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t));
vcoubard 541:884f95bf5351 222 cccd_md.vloc = BLE_GATTS_VLOC_STACK;
vcoubard 541:884f95bf5351 223 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
vcoubard 541:884f95bf5351 224 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
vcoubard 541:884f95bf5351 225 }
vcoubard 541:884f95bf5351 226
vcoubard 541:884f95bf5351 227 ble_gatts_char_md_t char_md = {0};
vcoubard 541:884f95bf5351 228
vcoubard 541:884f95bf5351 229 char_md.char_props = char_props;
vcoubard 541:884f95bf5351 230 char_md.p_cccd_md =
vcoubard 541:884f95bf5351 231 (char_props.notify || char_props.indicate) ? &cccd_md : NULL;
vcoubard 541:884f95bf5351 232 if ((userDescriptionDescriptorValueLen > 0) && (userDescriptionDescriptorValuePtr != NULL)) {
vcoubard 541:884f95bf5351 233 char_md.p_char_user_desc = const_cast<uint8_t *>(userDescriptionDescriptorValuePtr);
vcoubard 541:884f95bf5351 234 char_md.char_user_desc_max_size = userDescriptionDescriptorValueLen;
vcoubard 541:884f95bf5351 235 char_md.char_user_desc_size = userDescriptionDescriptorValueLen;
vcoubard 541:884f95bf5351 236 }
vcoubard 541:884f95bf5351 237
vcoubard 541:884f95bf5351 238 /* Attribute declaration */
vcoubard 541:884f95bf5351 239 ble_gatts_attr_md_t attr_md = {0};
vcoubard 541:884f95bf5351 240
vcoubard 541:884f95bf5351 241 attr_md.rd_auth = readAuthorization;
vcoubard 541:884f95bf5351 242 attr_md.wr_auth = writeAuthorization;
vcoubard 541:884f95bf5351 243
vcoubard 541:884f95bf5351 244 attr_md.vloc = BLE_GATTS_VLOC_STACK;
vcoubard 541:884f95bf5351 245 attr_md.vlen = (min_length == max_length) ? 0 : 1;
vcoubard 541:884f95bf5351 246
vcoubard 541:884f95bf5351 247 if (char_props.read || char_props.notify || char_props.indicate) {
vcoubard 541:884f95bf5351 248 switch (requiredSecurity) {
vcoubard 541:884f95bf5351 249 case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
vcoubard 541:884f95bf5351 250 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
vcoubard 541:884f95bf5351 251 break;
vcoubard 541:884f95bf5351 252 case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
vcoubard 541:884f95bf5351 253 BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 254 break;
vcoubard 541:884f95bf5351 255 case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
vcoubard 541:884f95bf5351 256 BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 257 break;
vcoubard 541:884f95bf5351 258 case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
vcoubard 541:884f95bf5351 259 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 260 break;
vcoubard 541:884f95bf5351 261 case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
vcoubard 541:884f95bf5351 262 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 263 break;
vcoubard 541:884f95bf5351 264 default:
vcoubard 541:884f95bf5351 265 break;
vcoubard 541:884f95bf5351 266 };
vcoubard 541:884f95bf5351 267 }
vcoubard 541:884f95bf5351 268
vcoubard 541:884f95bf5351 269 if (char_props.write || char_props.write_wo_resp) {
vcoubard 541:884f95bf5351 270 switch (requiredSecurity) {
vcoubard 541:884f95bf5351 271 case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
vcoubard 541:884f95bf5351 272 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
vcoubard 541:884f95bf5351 273 break;
vcoubard 541:884f95bf5351 274 case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
vcoubard 541:884f95bf5351 275 BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 276 break;
vcoubard 541:884f95bf5351 277 case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
vcoubard 541:884f95bf5351 278 BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 279 break;
vcoubard 541:884f95bf5351 280 case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
vcoubard 541:884f95bf5351 281 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 282 break;
vcoubard 541:884f95bf5351 283 case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
vcoubard 541:884f95bf5351 284 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 285 break;
vcoubard 541:884f95bf5351 286 default:
vcoubard 541:884f95bf5351 287 break;
vcoubard 541:884f95bf5351 288 };
vcoubard 541:884f95bf5351 289 }
vcoubard 541:884f95bf5351 290
vcoubard 541:884f95bf5351 291 ble_gatts_attr_t attr_char_value = {0};
vcoubard 541:884f95bf5351 292
vcoubard 541:884f95bf5351 293 attr_char_value.p_uuid = p_uuid;
vcoubard 541:884f95bf5351 294 attr_char_value.p_attr_md = &attr_md;
vcoubard 541:884f95bf5351 295 attr_char_value.init_len = min_length;
vcoubard 541:884f95bf5351 296 attr_char_value.max_len = max_length;
vcoubard 541:884f95bf5351 297 attr_char_value.p_value = p_data;
vcoubard 541:884f95bf5351 298
vcoubard 541:884f95bf5351 299 ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle,
vcoubard 541:884f95bf5351 300 &char_md,
vcoubard 541:884f95bf5351 301 &attr_char_value,
vcoubard 541:884f95bf5351 302 p_char_handle));
vcoubard 541:884f95bf5351 303
vcoubard 541:884f95bf5351 304 return ERROR_NONE;
vcoubard 541:884f95bf5351 305 }
vcoubard 541:884f95bf5351 306
vcoubard 541:884f95bf5351 307
vcoubard 541:884f95bf5351 308
vcoubard 541:884f95bf5351 309 /**************************************************************************/
vcoubard 541:884f95bf5351 310 /*!
vcoubard 541:884f95bf5351 311 @brief Adds a new descriptor to the custom service, assigning
vcoubard 541:884f95bf5351 312 value, a UUID add-on value, etc.
vcoubard 541:884f95bf5351 313
vcoubard 541:884f95bf5351 314 @param[in] char_handle
vcoubard 541:884f95bf5351 315 @param[in] p_uuid The 16-bit value to add to the base UUID
vcoubard 541:884f95bf5351 316 for this descriptor (normally >1
vcoubard 541:884f95bf5351 317 since 1 is typically used by the primary
vcoubard 541:884f95bf5351 318 service).
vcoubard 541:884f95bf5351 319 @param[in] max_length The maximum length of this descriptor
vcoubard 541:884f95bf5351 320
vcoubard 541:884f95bf5351 321 @returns
vcoubard 541:884f95bf5351 322 @retval ERROR_NONE Everything executed normally
vcoubard 541:884f95bf5351 323 */
vcoubard 541:884f95bf5351 324 /**************************************************************************/
vcoubard 541:884f95bf5351 325 error_t custom_add_in_descriptor(uint16_t char_handle,
vcoubard 541:884f95bf5351 326 ble_uuid_t *p_uuid,
vcoubard 541:884f95bf5351 327 uint8_t *p_data,
vcoubard 541:884f95bf5351 328 uint16_t min_length,
vcoubard 541:884f95bf5351 329 uint16_t max_length,
vcoubard 541:884f95bf5351 330 uint16_t *p_desc_handle)
vcoubard 541:884f95bf5351 331 {
vcoubard 541:884f95bf5351 332 /* Descriptor metadata */
vcoubard 541:884f95bf5351 333 ble_gatts_attr_md_t desc_md = {0};
vcoubard 541:884f95bf5351 334
vcoubard 541:884f95bf5351 335 desc_md.vloc = BLE_GATTS_VLOC_STACK;
vcoubard 541:884f95bf5351 336 desc_md.vlen = (min_length == max_length) ? 0 : 1;
vcoubard 541:884f95bf5351 337
vcoubard 541:884f95bf5351 338 /* Make it readable and writable */
vcoubard 541:884f95bf5351 339 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.read_perm);
vcoubard 541:884f95bf5351 340 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.write_perm);
vcoubard 541:884f95bf5351 341
vcoubard 541:884f95bf5351 342 ble_gatts_attr_t attr_desc = {0};
vcoubard 541:884f95bf5351 343
vcoubard 541:884f95bf5351 344 attr_desc.p_uuid = p_uuid;
vcoubard 541:884f95bf5351 345 attr_desc.p_attr_md = &desc_md;
vcoubard 541:884f95bf5351 346 attr_desc.init_len = min_length;
vcoubard 541:884f95bf5351 347 attr_desc.max_len = max_length;
vcoubard 541:884f95bf5351 348 attr_desc.p_value = p_data;
vcoubard 541:884f95bf5351 349
vcoubard 541:884f95bf5351 350 ASSERT_STATUS ( sd_ble_gatts_descriptor_add(char_handle,
vcoubard 541:884f95bf5351 351 &attr_desc,
vcoubard 541:884f95bf5351 352 p_desc_handle));
vcoubard 541:884f95bf5351 353
vcoubard 541:884f95bf5351 354 return ERROR_NONE;
rgrover1 82:6c51cbe4bc12 355 }