No changes

Fork of nRF51822 by Nordic Semiconductor

Committer:
galism
Date:
Sun Feb 26 03:15:43 2017 +0000
Revision:
639:abe58b2a2b5d
Parent:
557:e4218a32be51
No changes

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 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
vcoubard 557:e4218a32be51 152 base_uuid.uuid128[i] = p_uuid_base[i];
vcoubard 541:884f95bf5351 153 }
vcoubard 541:884f95bf5351 154
vcoubard 541:884f95bf5351 155 ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0);
vcoubard 541:884f95bf5351 156
vcoubard 541:884f95bf5351 157 return uuid_type;
vcoubard 541:884f95bf5351 158 }
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 error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base,
vcoubard 541:884f95bf5351 166 ble_uuid_t *p_uuid)
vcoubard 541:884f95bf5351 167 {
vcoubard 541:884f95bf5351 168 UUID::LongUUIDBytes_t uuid_base_le;
vcoubard 541:884f95bf5351 169
vcoubard 541:884f95bf5351 170 for (uint8_t i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
vcoubard 557:e4218a32be51 171 uuid_base_le[i] = p_uuid_base[i];
vcoubard 541:884f95bf5351 172 }
vcoubard 541:884f95bf5351 173
vcoubard 541:884f95bf5351 174 ASSERT_STATUS( sd_ble_uuid_decode(UUID::LENGTH_OF_LONG_UUID, uuid_base_le, p_uuid));
vcoubard 541:884f95bf5351 175
vcoubard 541:884f95bf5351 176 return ERROR_NONE;
vcoubard 541:884f95bf5351 177 }
vcoubard 541:884f95bf5351 178
vcoubard 541:884f95bf5351 179 /**************************************************************************/
vcoubard 541:884f95bf5351 180 /*!
vcoubard 541:884f95bf5351 181 @brief Adds a new characteristic to the custom service, assigning
vcoubard 541:884f95bf5351 182 properties, a UUID add-on value, etc.
vcoubard 541:884f95bf5351 183
vcoubard 541:884f95bf5351 184 @param[in] service_handle
vcoubard 541:884f95bf5351 185 @param[in] p_uuid The 16-bit value to add to the base UUID
vcoubard 541:884f95bf5351 186 for this characteristic (normally >1
vcoubard 541:884f95bf5351 187 since 1 is typically used by the primary
vcoubard 541:884f95bf5351 188 service).
vcoubard 541:884f95bf5351 189 @param[in] char_props The characteristic properties, as
vcoubard 541:884f95bf5351 190 defined by ble_gatt_char_props_t
vcoubard 541:884f95bf5351 191 @param[in] max_length The maximum length of this characeristic
vcoubard 553:2a413611e569 192 @param[in] has_variable_len Whether the characteristic data has
vcoubard 553:2a413611e569 193 variable length.
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 552:20b282c26f96 205 uint16_t length,
vcoubard 541:884f95bf5351 206 uint16_t max_length,
vcoubard 553:2a413611e569 207 bool has_variable_len,
vcoubard 541:884f95bf5351 208 const uint8_t *userDescriptionDescriptorValuePtr,
vcoubard 541:884f95bf5351 209 uint16_t userDescriptionDescriptorValueLen,
vcoubard 541:884f95bf5351 210 bool readAuthorization,
vcoubard 541:884f95bf5351 211 bool writeAuthorization,
vcoubard 541:884f95bf5351 212 ble_gatts_char_handles_t *p_char_handle)
vcoubard 541:884f95bf5351 213 {
vcoubard 541:884f95bf5351 214 /* Characteristic metadata */
vcoubard 541:884f95bf5351 215 ble_gatts_attr_md_t cccd_md;
vcoubard 541:884f95bf5351 216 ble_gatt_char_props_t char_props;
vcoubard 541:884f95bf5351 217
vcoubard 541:884f95bf5351 218 memcpy(&char_props, &properties, 1);
vcoubard 541:884f95bf5351 219
vcoubard 541:884f95bf5351 220 if (char_props.notify || char_props.indicate) {
vcoubard 541:884f95bf5351 221 /* Notification requires cccd */
vcoubard 541:884f95bf5351 222 memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t));
vcoubard 541:884f95bf5351 223 cccd_md.vloc = BLE_GATTS_VLOC_STACK;
vcoubard 541:884f95bf5351 224 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
vcoubard 541:884f95bf5351 225 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
vcoubard 541:884f95bf5351 226 }
vcoubard 541:884f95bf5351 227
vcoubard 541:884f95bf5351 228 ble_gatts_char_md_t char_md = {0};
vcoubard 541:884f95bf5351 229
vcoubard 541:884f95bf5351 230 char_md.char_props = char_props;
vcoubard 541:884f95bf5351 231 char_md.p_cccd_md =
vcoubard 541:884f95bf5351 232 (char_props.notify || char_props.indicate) ? &cccd_md : NULL;
vcoubard 541:884f95bf5351 233 if ((userDescriptionDescriptorValueLen > 0) && (userDescriptionDescriptorValuePtr != NULL)) {
vcoubard 541:884f95bf5351 234 char_md.p_char_user_desc = const_cast<uint8_t *>(userDescriptionDescriptorValuePtr);
vcoubard 541:884f95bf5351 235 char_md.char_user_desc_max_size = userDescriptionDescriptorValueLen;
vcoubard 541:884f95bf5351 236 char_md.char_user_desc_size = userDescriptionDescriptorValueLen;
vcoubard 541:884f95bf5351 237 }
vcoubard 541:884f95bf5351 238
vcoubard 541:884f95bf5351 239 /* Attribute declaration */
vcoubard 541:884f95bf5351 240 ble_gatts_attr_md_t attr_md = {0};
vcoubard 541:884f95bf5351 241
vcoubard 541:884f95bf5351 242 attr_md.rd_auth = readAuthorization;
vcoubard 541:884f95bf5351 243 attr_md.wr_auth = writeAuthorization;
vcoubard 541:884f95bf5351 244
vcoubard 541:884f95bf5351 245 attr_md.vloc = BLE_GATTS_VLOC_STACK;
vcoubard 552:20b282c26f96 246 /* Always set variable size */
vcoubard 553:2a413611e569 247 attr_md.vlen = has_variable_len;
vcoubard 541:884f95bf5351 248
vcoubard 541:884f95bf5351 249 if (char_props.read || char_props.notify || char_props.indicate) {
vcoubard 541:884f95bf5351 250 switch (requiredSecurity) {
vcoubard 541:884f95bf5351 251 case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
vcoubard 541:884f95bf5351 252 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
vcoubard 541:884f95bf5351 253 break;
vcoubard 541:884f95bf5351 254 case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
vcoubard 541:884f95bf5351 255 BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 256 break;
vcoubard 541:884f95bf5351 257 case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
vcoubard 541:884f95bf5351 258 BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 259 break;
vcoubard 541:884f95bf5351 260 case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
vcoubard 541:884f95bf5351 261 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 262 break;
vcoubard 541:884f95bf5351 263 case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
vcoubard 541:884f95bf5351 264 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm);
vcoubard 541:884f95bf5351 265 break;
vcoubard 541:884f95bf5351 266 default:
vcoubard 541:884f95bf5351 267 break;
vcoubard 541:884f95bf5351 268 };
vcoubard 541:884f95bf5351 269 }
vcoubard 541:884f95bf5351 270
vcoubard 541:884f95bf5351 271 if (char_props.write || char_props.write_wo_resp) {
vcoubard 541:884f95bf5351 272 switch (requiredSecurity) {
vcoubard 541:884f95bf5351 273 case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK :
vcoubard 541:884f95bf5351 274 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
vcoubard 541:884f95bf5351 275 break;
vcoubard 541:884f95bf5351 276 case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM :
vcoubard 541:884f95bf5351 277 BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 278 break;
vcoubard 541:884f95bf5351 279 case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM :
vcoubard 541:884f95bf5351 280 BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 281 break;
vcoubard 541:884f95bf5351 282 case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM :
vcoubard 541:884f95bf5351 283 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 284 break;
vcoubard 541:884f95bf5351 285 case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM :
vcoubard 541:884f95bf5351 286 BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.write_perm);
vcoubard 541:884f95bf5351 287 break;
vcoubard 541:884f95bf5351 288 default:
vcoubard 541:884f95bf5351 289 break;
vcoubard 541:884f95bf5351 290 };
vcoubard 541:884f95bf5351 291 }
vcoubard 541:884f95bf5351 292
vcoubard 541:884f95bf5351 293 ble_gatts_attr_t attr_char_value = {0};
vcoubard 541:884f95bf5351 294
vcoubard 541:884f95bf5351 295 attr_char_value.p_uuid = p_uuid;
vcoubard 541:884f95bf5351 296 attr_char_value.p_attr_md = &attr_md;
vcoubard 552:20b282c26f96 297 attr_char_value.init_len = length;
vcoubard 541:884f95bf5351 298 attr_char_value.max_len = max_length;
vcoubard 541:884f95bf5351 299 attr_char_value.p_value = p_data;
vcoubard 541:884f95bf5351 300
vcoubard 541:884f95bf5351 301 ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle,
vcoubard 541:884f95bf5351 302 &char_md,
vcoubard 541:884f95bf5351 303 &attr_char_value,
vcoubard 541:884f95bf5351 304 p_char_handle));
vcoubard 541:884f95bf5351 305
vcoubard 541:884f95bf5351 306 return ERROR_NONE;
vcoubard 541:884f95bf5351 307 }
vcoubard 541:884f95bf5351 308
vcoubard 541:884f95bf5351 309
vcoubard 541:884f95bf5351 310
vcoubard 541:884f95bf5351 311 /**************************************************************************/
vcoubard 541:884f95bf5351 312 /*!
vcoubard 541:884f95bf5351 313 @brief Adds a new descriptor to the custom service, assigning
vcoubard 541:884f95bf5351 314 value, a UUID add-on value, etc.
vcoubard 541:884f95bf5351 315
vcoubard 541:884f95bf5351 316 @param[in] char_handle
vcoubard 541:884f95bf5351 317 @param[in] p_uuid The 16-bit value to add to the base UUID
vcoubard 541:884f95bf5351 318 for this descriptor (normally >1
vcoubard 541:884f95bf5351 319 since 1 is typically used by the primary
vcoubard 541:884f95bf5351 320 service).
vcoubard 541:884f95bf5351 321 @param[in] max_length The maximum length of this descriptor
vcoubard 553:2a413611e569 322 @param[in] has_variable_len Whether the characteristic data has
vcoubard 553:2a413611e569 323 variable length.
vcoubard 541:884f95bf5351 324
vcoubard 541:884f95bf5351 325 @returns
vcoubard 541:884f95bf5351 326 @retval ERROR_NONE Everything executed normally
vcoubard 541:884f95bf5351 327 */
vcoubard 541:884f95bf5351 328 /**************************************************************************/
vcoubard 541:884f95bf5351 329 error_t custom_add_in_descriptor(uint16_t char_handle,
vcoubard 553:2a413611e569 330 ble_uuid_t *p_uuid,
vcoubard 553:2a413611e569 331 uint8_t *p_data,
vcoubard 553:2a413611e569 332 uint16_t length,
vcoubard 553:2a413611e569 333 uint16_t max_length,
vcoubard 553:2a413611e569 334 bool has_variable_len,
vcoubard 553:2a413611e569 335 uint16_t *p_desc_handle)
vcoubard 541:884f95bf5351 336 {
vcoubard 541:884f95bf5351 337 /* Descriptor metadata */
vcoubard 541:884f95bf5351 338 ble_gatts_attr_md_t desc_md = {0};
vcoubard 541:884f95bf5351 339
vcoubard 541:884f95bf5351 340 desc_md.vloc = BLE_GATTS_VLOC_STACK;
vcoubard 552:20b282c26f96 341 /* Always set variable size */
vcoubard 553:2a413611e569 342 desc_md.vlen = has_variable_len;
vcoubard 541:884f95bf5351 343
vcoubard 541:884f95bf5351 344 /* Make it readable and writable */
vcoubard 541:884f95bf5351 345 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.read_perm);
vcoubard 541:884f95bf5351 346 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.write_perm);
vcoubard 541:884f95bf5351 347
vcoubard 541:884f95bf5351 348 ble_gatts_attr_t attr_desc = {0};
vcoubard 541:884f95bf5351 349
vcoubard 541:884f95bf5351 350 attr_desc.p_uuid = p_uuid;
vcoubard 541:884f95bf5351 351 attr_desc.p_attr_md = &desc_md;
vcoubard 552:20b282c26f96 352 attr_desc.init_len = length;
vcoubard 541:884f95bf5351 353 attr_desc.max_len = max_length;
vcoubard 541:884f95bf5351 354 attr_desc.p_value = p_data;
vcoubard 541:884f95bf5351 355
vcoubard 541:884f95bf5351 356 ASSERT_STATUS ( sd_ble_gatts_descriptor_add(char_handle,
vcoubard 541:884f95bf5351 357 &attr_desc,
vcoubard 541:884f95bf5351 358 p_desc_handle));
vcoubard 541:884f95bf5351 359
vcoubard 541:884f95bf5351 360 return ERROR_NONE;
rgrover1 82:6c51cbe4bc12 361 }