Clone of the nRF51822 repository from github The correct home is https://github.com/lancaster-university/nRF51822
Dependencies: nrf51-sdk
Dependents: microbit-dal microbit-ble-open microbit-dal-eddystone microbit-dal-ble-accelerometer-example ... more
source/btle/custom/custom_helper.cpp@615:65ea2acfc6a2, 2016-04-06 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Wed Apr 06 22:38:43 2016 +0100
- Revision:
- 615:65ea2acfc6a2
- Parent:
- 557:e4218a32be51
- Child:
- 616:a8f9b022d8fd
Synchronized with git rev 60a7c0c0
Author: Rohit Grover
bring in the latest changes for BLE::init() where we allow <object, member> tuples for init callback.
Who changed what in which revision?
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 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
LancasterUniversity | 615:65ea2acfc6a2 | 152 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
LancasterUniversity | 615:65ea2acfc6a2 | 153 | base_uuid.uuid128[i] = p_uuid_base[UUID::LENGTH_OF_LONG_UUID - 1 - i]; |
LancasterUniversity | 615:65ea2acfc6a2 | 154 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 155 | |
LancasterUniversity | 615:65ea2acfc6a2 | 156 | ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); |
LancasterUniversity | 615:65ea2acfc6a2 | 157 | |
LancasterUniversity | 615:65ea2acfc6a2 | 158 | return uuid_type; |
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 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 166 | error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, |
LancasterUniversity | 615:65ea2acfc6a2 | 167 | ble_uuid_t *p_uuid) |
LancasterUniversity | 615:65ea2acfc6a2 | 168 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 169 | UUID::LongUUIDBytes_t uuid_base_le; |
LancasterUniversity | 615:65ea2acfc6a2 | 170 | |
LancasterUniversity | 615:65ea2acfc6a2 | 171 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
LancasterUniversity | 615:65ea2acfc6a2 | 172 | for (uint8_t i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
LancasterUniversity | 615:65ea2acfc6a2 | 173 | uuid_base_le[i] = p_uuid_base[UUID::LENGTH_OF_LONG_UUID - 1 - i]; |
LancasterUniversity | 615:65ea2acfc6a2 | 174 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 175 | |
LancasterUniversity | 615:65ea2acfc6a2 | 176 | ASSERT_STATUS( sd_ble_uuid_decode(UUID::LENGTH_OF_LONG_UUID, uuid_base_le, p_uuid)); |
LancasterUniversity | 615:65ea2acfc6a2 | 177 | |
LancasterUniversity | 615:65ea2acfc6a2 | 178 | return ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 179 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 180 | |
LancasterUniversity | 615:65ea2acfc6a2 | 181 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 182 | /*! |
LancasterUniversity | 615:65ea2acfc6a2 | 183 | @brief Adds a new characteristic to the custom service, assigning |
LancasterUniversity | 615:65ea2acfc6a2 | 184 | properties, a UUID add-on value, etc. |
LancasterUniversity | 615:65ea2acfc6a2 | 185 | |
LancasterUniversity | 615:65ea2acfc6a2 | 186 | @param[in] service_handle |
LancasterUniversity | 615:65ea2acfc6a2 | 187 | @param[in] p_uuid The 16-bit value to add to the base UUID |
LancasterUniversity | 615:65ea2acfc6a2 | 188 | for this characteristic (normally >1 |
LancasterUniversity | 615:65ea2acfc6a2 | 189 | since 1 is typically used by the primary |
LancasterUniversity | 615:65ea2acfc6a2 | 190 | service). |
LancasterUniversity | 615:65ea2acfc6a2 | 191 | @param[in] char_props The characteristic properties, as |
LancasterUniversity | 615:65ea2acfc6a2 | 192 | defined by ble_gatt_char_props_t |
LancasterUniversity | 615:65ea2acfc6a2 | 193 | @param[in] max_length The maximum length of this characeristic |
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 | 615:65ea2acfc6a2 | 205 | uint16_t min_length, |
LancasterUniversity | 615:65ea2acfc6a2 | 206 | uint16_t max_length, |
LancasterUniversity | 615:65ea2acfc6a2 | 207 | const uint8_t *userDescriptionDescriptorValuePtr, |
LancasterUniversity | 615:65ea2acfc6a2 | 208 | uint16_t userDescriptionDescriptorValueLen, |
LancasterUniversity | 615:65ea2acfc6a2 | 209 | bool readAuthorization, |
LancasterUniversity | 615:65ea2acfc6a2 | 210 | bool writeAuthorization, |
LancasterUniversity | 615:65ea2acfc6a2 | 211 | ble_gatts_char_handles_t *p_char_handle) |
LancasterUniversity | 615:65ea2acfc6a2 | 212 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 213 | /* Characteristic metadata */ |
LancasterUniversity | 615:65ea2acfc6a2 | 214 | ble_gatts_attr_md_t cccd_md; |
LancasterUniversity | 615:65ea2acfc6a2 | 215 | ble_gatt_char_props_t char_props; |
LancasterUniversity | 615:65ea2acfc6a2 | 216 | |
LancasterUniversity | 615:65ea2acfc6a2 | 217 | memcpy(&char_props, &properties, 1); |
LancasterUniversity | 615:65ea2acfc6a2 | 218 | |
LancasterUniversity | 615:65ea2acfc6a2 | 219 | if (char_props.notify || char_props.indicate) { |
LancasterUniversity | 615:65ea2acfc6a2 | 220 | /* Notification requires cccd */ |
LancasterUniversity | 615:65ea2acfc6a2 | 221 | memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); |
LancasterUniversity | 615:65ea2acfc6a2 | 222 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
LancasterUniversity | 615:65ea2acfc6a2 | 223 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 224 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 225 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 226 | |
LancasterUniversity | 615:65ea2acfc6a2 | 227 | ble_gatts_char_md_t char_md = {0}; |
LancasterUniversity | 615:65ea2acfc6a2 | 228 | |
LancasterUniversity | 615:65ea2acfc6a2 | 229 | char_md.char_props = char_props; |
LancasterUniversity | 615:65ea2acfc6a2 | 230 | char_md.p_cccd_md = |
LancasterUniversity | 615:65ea2acfc6a2 | 231 | (char_props.notify || char_props.indicate) ? &cccd_md : NULL; |
LancasterUniversity | 615:65ea2acfc6a2 | 232 | if ((userDescriptionDescriptorValueLen > 0) && (userDescriptionDescriptorValuePtr != NULL)) { |
LancasterUniversity | 615:65ea2acfc6a2 | 233 | char_md.p_char_user_desc = const_cast<uint8_t *>(userDescriptionDescriptorValuePtr); |
LancasterUniversity | 615:65ea2acfc6a2 | 234 | char_md.char_user_desc_max_size = userDescriptionDescriptorValueLen; |
LancasterUniversity | 615:65ea2acfc6a2 | 235 | char_md.char_user_desc_size = userDescriptionDescriptorValueLen; |
LancasterUniversity | 615:65ea2acfc6a2 | 236 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 237 | |
LancasterUniversity | 615:65ea2acfc6a2 | 238 | /* Attribute declaration */ |
LancasterUniversity | 615:65ea2acfc6a2 | 239 | ble_gatts_attr_md_t attr_md = {0}; |
LancasterUniversity | 615:65ea2acfc6a2 | 240 | |
LancasterUniversity | 615:65ea2acfc6a2 | 241 | attr_md.rd_auth = readAuthorization; |
LancasterUniversity | 615:65ea2acfc6a2 | 242 | attr_md.wr_auth = writeAuthorization; |
LancasterUniversity | 615:65ea2acfc6a2 | 243 | |
LancasterUniversity | 615:65ea2acfc6a2 | 244 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
LancasterUniversity | 615:65ea2acfc6a2 | 245 | attr_md.vlen = (min_length == max_length) ? 0 : 1; |
LancasterUniversity | 615:65ea2acfc6a2 | 246 | |
LancasterUniversity | 615:65ea2acfc6a2 | 247 | if (char_props.read || char_props.notify || char_props.indicate) { |
LancasterUniversity | 615:65ea2acfc6a2 | 248 | switch (requiredSecurity) { |
LancasterUniversity | 615:65ea2acfc6a2 | 249 | case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK : |
LancasterUniversity | 615:65ea2acfc6a2 | 250 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 251 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 252 | case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 253 | BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 254 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 255 | case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 256 | BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 257 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 258 | case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 259 | BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 260 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 261 | case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 262 | BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 263 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 264 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 265 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 266 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 267 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 268 | |
LancasterUniversity | 615:65ea2acfc6a2 | 269 | if (char_props.write || char_props.write_wo_resp) { |
LancasterUniversity | 615:65ea2acfc6a2 | 270 | switch (requiredSecurity) { |
LancasterUniversity | 615:65ea2acfc6a2 | 271 | case SecurityManager::SECURITY_MODE_ENCRYPTION_OPEN_LINK : |
LancasterUniversity | 615:65ea2acfc6a2 | 272 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 273 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 274 | case SecurityManager::SECURITY_MODE_ENCRYPTION_NO_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 275 | BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(&attr_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 276 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 277 | case SecurityManager::SECURITY_MODE_ENCRYPTION_WITH_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 278 | BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(&attr_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 279 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 280 | case SecurityManager::SECURITY_MODE_SIGNED_NO_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 281 | BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(&attr_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 282 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 283 | case SecurityManager::SECURITY_MODE_SIGNED_WITH_MITM : |
LancasterUniversity | 615:65ea2acfc6a2 | 284 | BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(&attr_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 285 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 286 | default: |
LancasterUniversity | 615:65ea2acfc6a2 | 287 | break; |
LancasterUniversity | 615:65ea2acfc6a2 | 288 | }; |
LancasterUniversity | 615:65ea2acfc6a2 | 289 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 290 | |
LancasterUniversity | 615:65ea2acfc6a2 | 291 | ble_gatts_attr_t attr_char_value = {0}; |
LancasterUniversity | 615:65ea2acfc6a2 | 292 | |
LancasterUniversity | 615:65ea2acfc6a2 | 293 | attr_char_value.p_uuid = p_uuid; |
LancasterUniversity | 615:65ea2acfc6a2 | 294 | attr_char_value.p_attr_md = &attr_md; |
LancasterUniversity | 615:65ea2acfc6a2 | 295 | attr_char_value.init_len = min_length; |
LancasterUniversity | 615:65ea2acfc6a2 | 296 | attr_char_value.max_len = max_length; |
LancasterUniversity | 615:65ea2acfc6a2 | 297 | attr_char_value.p_value = p_data; |
LancasterUniversity | 615:65ea2acfc6a2 | 298 | |
LancasterUniversity | 615:65ea2acfc6a2 | 299 | ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 300 | &char_md, |
LancasterUniversity | 615:65ea2acfc6a2 | 301 | &attr_char_value, |
LancasterUniversity | 615:65ea2acfc6a2 | 302 | p_char_handle)); |
LancasterUniversity | 615:65ea2acfc6a2 | 303 | |
LancasterUniversity | 615:65ea2acfc6a2 | 304 | return ERROR_NONE; |
LancasterUniversity | 615:65ea2acfc6a2 | 305 | } |
LancasterUniversity | 615:65ea2acfc6a2 | 306 | |
LancasterUniversity | 615:65ea2acfc6a2 | 307 | |
LancasterUniversity | 615:65ea2acfc6a2 | 308 | |
LancasterUniversity | 615:65ea2acfc6a2 | 309 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 310 | /*! |
LancasterUniversity | 615:65ea2acfc6a2 | 311 | @brief Adds a new descriptor to the custom service, assigning |
LancasterUniversity | 615:65ea2acfc6a2 | 312 | value, a UUID add-on value, etc. |
LancasterUniversity | 615:65ea2acfc6a2 | 313 | |
LancasterUniversity | 615:65ea2acfc6a2 | 314 | @param[in] char_handle |
LancasterUniversity | 615:65ea2acfc6a2 | 315 | @param[in] p_uuid The 16-bit value to add to the base UUID |
LancasterUniversity | 615:65ea2acfc6a2 | 316 | for this descriptor (normally >1 |
LancasterUniversity | 615:65ea2acfc6a2 | 317 | since 1 is typically used by the primary |
LancasterUniversity | 615:65ea2acfc6a2 | 318 | service). |
LancasterUniversity | 615:65ea2acfc6a2 | 319 | @param[in] max_length The maximum length of this descriptor |
LancasterUniversity | 615:65ea2acfc6a2 | 320 | |
LancasterUniversity | 615:65ea2acfc6a2 | 321 | @returns |
LancasterUniversity | 615:65ea2acfc6a2 | 322 | @retval ERROR_NONE Everything executed normally |
LancasterUniversity | 615:65ea2acfc6a2 | 323 | */ |
LancasterUniversity | 615:65ea2acfc6a2 | 324 | /**************************************************************************/ |
LancasterUniversity | 615:65ea2acfc6a2 | 325 | error_t custom_add_in_descriptor(uint16_t char_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 326 | ble_uuid_t *p_uuid, |
LancasterUniversity | 615:65ea2acfc6a2 | 327 | uint8_t *p_data, |
LancasterUniversity | 615:65ea2acfc6a2 | 328 | uint16_t min_length, |
LancasterUniversity | 615:65ea2acfc6a2 | 329 | uint16_t max_length, |
LancasterUniversity | 615:65ea2acfc6a2 | 330 | uint16_t *p_desc_handle) |
LancasterUniversity | 615:65ea2acfc6a2 | 331 | { |
LancasterUniversity | 615:65ea2acfc6a2 | 332 | /* Descriptor metadata */ |
LancasterUniversity | 615:65ea2acfc6a2 | 333 | ble_gatts_attr_md_t desc_md = {0}; |
LancasterUniversity | 615:65ea2acfc6a2 | 334 | |
LancasterUniversity | 615:65ea2acfc6a2 | 335 | desc_md.vloc = BLE_GATTS_VLOC_STACK; |
LancasterUniversity | 615:65ea2acfc6a2 | 336 | desc_md.vlen = (min_length == max_length) ? 0 : 1; |
LancasterUniversity | 615:65ea2acfc6a2 | 337 | |
LancasterUniversity | 615:65ea2acfc6a2 | 338 | /* Make it readable and writable */ |
LancasterUniversity | 615:65ea2acfc6a2 | 339 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.read_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 340 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&desc_md.write_perm); |
LancasterUniversity | 615:65ea2acfc6a2 | 341 | |
LancasterUniversity | 615:65ea2acfc6a2 | 342 | ble_gatts_attr_t attr_desc = {0}; |
LancasterUniversity | 615:65ea2acfc6a2 | 343 | |
LancasterUniversity | 615:65ea2acfc6a2 | 344 | attr_desc.p_uuid = p_uuid; |
LancasterUniversity | 615:65ea2acfc6a2 | 345 | attr_desc.p_attr_md = &desc_md; |
LancasterUniversity | 615:65ea2acfc6a2 | 346 | attr_desc.init_len = min_length; |
LancasterUniversity | 615:65ea2acfc6a2 | 347 | attr_desc.max_len = max_length; |
LancasterUniversity | 615:65ea2acfc6a2 | 348 | attr_desc.p_value = p_data; |
LancasterUniversity | 615:65ea2acfc6a2 | 349 | |
LancasterUniversity | 615:65ea2acfc6a2 | 350 | ASSERT_STATUS ( sd_ble_gatts_descriptor_add(char_handle, |
LancasterUniversity | 615:65ea2acfc6a2 | 351 | &attr_desc, |
LancasterUniversity | 615:65ea2acfc6a2 | 352 | p_desc_handle)); |
LancasterUniversity | 615:65ea2acfc6a2 | 353 | |
LancasterUniversity | 615:65ea2acfc6a2 | 354 | return ERROR_NONE; |
rgrover1 | 82:6c51cbe4bc12 | 355 | } |