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