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