Nordic stack and drivers for the mbed BLE API

Dependents:   BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate writable_gatt ... more

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

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