fdsf
Fork of nRF51822 by
btle/custom/custom_helper.cpp@15:11b0c15979d1, 2014-05-30 (annotated)
- Committer:
- Rohit Grover
- Date:
- Fri May 30 08:44:28 2014 +0100
- Revision:
- 15:11b0c15979d1
- Parent:
- 13:b8251438f5c4
- Child:
- 18:465e0419c178
update the comment header about the UUID table.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:eff01767de02 | 1 | /* mbed Microcontroller Library |
bogdanm | 0:eff01767de02 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:eff01767de02 | 3 | * |
bogdanm | 0:eff01767de02 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:eff01767de02 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:eff01767de02 | 6 | * You may obtain a copy of the License at |
bogdanm | 0:eff01767de02 | 7 | * |
bogdanm | 0:eff01767de02 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:eff01767de02 | 9 | * |
bogdanm | 0:eff01767de02 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:eff01767de02 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:eff01767de02 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:eff01767de02 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:eff01767de02 | 14 | * limitations under the License. |
bogdanm | 0:eff01767de02 | 15 | */ |
bogdanm | 0:eff01767de02 | 16 | |
bogdanm | 0:eff01767de02 | 17 | #include "custom_helper.h" |
bogdanm | 0:eff01767de02 | 18 | |
Rohit Grover |
15:11b0c15979d1 | 19 | /* |
Rohit Grover |
15:11b0c15979d1 | 20 | * The current version of the soft-device doesn't handle duplicate 128-bit UUIDs |
Rohit Grover |
15:11b0c15979d1 | 21 | * very well. It is therefore necessary to filter away duplicates before |
Rohit Grover |
15:11b0c15979d1 | 22 | * passing long UUIDs to sd_ble_uuid_vs_add(). The following types and data |
Rohit Grover |
15:11b0c15979d1 | 23 | * structures involved in maintaining a local cache of 128-bit UUIDs. |
Rohit Grover |
10:68c0e6cabe07 | 24 | */ |
Rohit Grover |
10:68c0e6cabe07 | 25 | typedef struct { |
Rohit Grover |
10:68c0e6cabe07 | 26 | uint8_t uuid[UUID::LENGTH_OF_LONG_UUID]; |
Rohit Grover |
10:68c0e6cabe07 | 27 | uint8_t type; |
Rohit Grover |
10:68c0e6cabe07 | 28 | } converted_uuid_table_entry_t; |
Rohit Grover |
10:68c0e6cabe07 | 29 | static const unsigned UUID_TABLE_MAX_ENTRIES = 8; /* This is the maximum number |
Rohit Grover |
10:68c0e6cabe07 | 30 | * of 128-bit UUIDs with distinct bases that |
Rohit Grover |
10:68c0e6cabe07 | 31 | * we expect to be in use; increase this |
Rohit Grover |
10:68c0e6cabe07 | 32 | * limit if needed. */ |
Rohit Grover |
10:68c0e6cabe07 | 33 | static unsigned uuidTableEntries = 0; /* current usage of the table */ |
Rohit Grover |
10:68c0e6cabe07 | 34 | converted_uuid_table_entry_t convertedUUIDTable[UUID_TABLE_MAX_ENTRIES]; |
Rohit Grover |
10:68c0e6cabe07 | 35 | |
Rohit Grover |
10:68c0e6cabe07 | 36 | /** |
Rohit Grover |
9:3794dc9540f0 | 37 | * lookup the cache of previously converted 128-bit UUIDs to find a type value. |
Rohit Grover |
9:3794dc9540f0 | 38 | * @param uuid long UUID |
Rohit Grover |
9:3794dc9540f0 | 39 | * @param recoveredType the type field of the 3-byte nRF's uuid. |
Rohit Grover |
9:3794dc9540f0 | 40 | * @return true if a match is found. |
Rohit Grover |
9:3794dc9540f0 | 41 | */ |
Rohit Grover |
9:3794dc9540f0 | 42 | static bool |
Rohit Grover |
9:3794dc9540f0 | 43 | lookupConvertedUUIDTable(const uint8_t uuid[UUID::LENGTH_OF_LONG_UUID], |
Rohit Grover |
10:68c0e6cabe07 | 44 | uint8_t *recoveredType) |
Rohit Grover |
9:3794dc9540f0 | 45 | { |
Rohit Grover |
10:68c0e6cabe07 | 46 | unsigned i; |
Rohit Grover |
10:68c0e6cabe07 | 47 | for (i = 0; i < uuidTableEntries; i++) { |
Rohit Grover |
10:68c0e6cabe07 | 48 | if (memcmp(convertedUUIDTable[i].uuid, |
Rohit Grover |
10:68c0e6cabe07 | 49 | uuid, |
Rohit Grover |
10:68c0e6cabe07 | 50 | UUID::LENGTH_OF_LONG_UUID) == 0) { |
Rohit Grover |
10:68c0e6cabe07 | 51 | *recoveredType = convertedUUIDTable[i].type; |
Rohit Grover |
10:68c0e6cabe07 | 52 | return true; |
Rohit Grover |
10:68c0e6cabe07 | 53 | } |
Rohit Grover |
10:68c0e6cabe07 | 54 | } |
Rohit Grover |
10:68c0e6cabe07 | 55 | |
Rohit Grover |
9:3794dc9540f0 | 56 | return false; |
Rohit Grover |
9:3794dc9540f0 | 57 | } |
Rohit Grover |
9:3794dc9540f0 | 58 | |
Rohit Grover |
9:3794dc9540f0 | 59 | static void |
Rohit Grover |
9:3794dc9540f0 | 60 | addToConvertedUUIDTable(const uint8_t uuid[UUID::LENGTH_OF_LONG_UUID], |
Rohit Grover |
10:68c0e6cabe07 | 61 | uint8_t type) |
Rohit Grover |
9:3794dc9540f0 | 62 | { |
Rohit Grover |
10:68c0e6cabe07 | 63 | if (uuidTableEntries == UUID_TABLE_MAX_ENTRIES) { |
Rohit Grover |
10:68c0e6cabe07 | 64 | return; /* recovery needed; or at least the user should be |
Rohit Grover |
10:68c0e6cabe07 | 65 | * warned about this fact.*/ |
Rohit Grover |
10:68c0e6cabe07 | 66 | } |
Rohit Grover |
10:68c0e6cabe07 | 67 | |
Rohit Grover |
10:68c0e6cabe07 | 68 | memcpy(convertedUUIDTable[uuidTableEntries].uuid, |
Rohit Grover |
10:68c0e6cabe07 | 69 | uuid, |
Rohit Grover |
10:68c0e6cabe07 | 70 | UUID::LENGTH_OF_LONG_UUID); |
Rohit Grover |
10:68c0e6cabe07 | 71 | convertedUUIDTable[uuidTableEntries].type = type; |
Rohit Grover |
10:68c0e6cabe07 | 72 | uuidTableEntries++; |
Rohit Grover |
9:3794dc9540f0 | 73 | } |
Rohit Grover |
9:3794dc9540f0 | 74 | |
Rohit Grover |
9:3794dc9540f0 | 75 | /** |
Rohit Grover |
8:2214f1df6a6a | 76 | * The nRF transport has its own 3-byte representation of a UUID. If the user- |
Rohit Grover |
8:2214f1df6a6a | 77 | * specified UUID is 128-bits wide, then the UUID base needs to be added to the |
Rohit Grover |
8:2214f1df6a6a | 78 | * soft-device and converted to a 3-byte handle before being used further. This |
Rohit Grover |
8:2214f1df6a6a | 79 | * function is responsible for this translation of user-specified UUIDs into |
Rohit Grover |
8:2214f1df6a6a | 80 | * nRF's representation. |
Rohit Grover |
8:2214f1df6a6a | 81 | * |
Rohit Grover |
8:2214f1df6a6a | 82 | * @param[in] uuid |
Rohit Grover |
8:2214f1df6a6a | 83 | * user-specified UUID |
Rohit Grover |
8:2214f1df6a6a | 84 | * @return nRF |
Rohit Grover |
8:2214f1df6a6a | 85 | * 3-byte UUID (containing a type and 16-bit UUID) representation |
Rohit Grover |
8:2214f1df6a6a | 86 | * to be used with SVC calls. |
Rohit Grover |
8:2214f1df6a6a | 87 | */ |
Rohit Grover |
12:e151f55035b8 | 88 | ble_uuid_t custom_convert_to_nordic_uuid(const UUID &uuid) |
Rohit Grover |
8:2214f1df6a6a | 89 | { |
Rohit Grover |
10:68c0e6cabe07 | 90 | ble_uuid_t nordicUUID = { |
Rohit Grover |
13:b8251438f5c4 | 91 | .uuid = uuid.get16BitUUID(), |
Rohit Grover |
8:2214f1df6a6a | 92 | .type = BLE_UUID_TYPE_UNKNOWN /* to be set below */ |
Rohit Grover |
8:2214f1df6a6a | 93 | }; |
Rohit Grover |
8:2214f1df6a6a | 94 | |
Rohit Grover |
13:b8251438f5c4 | 95 | if (uuid.shortOrLong() == UUID::UUID_TYPE_SHORT) { |
Rohit Grover |
10:68c0e6cabe07 | 96 | nordicUUID.type = BLE_UUID_TYPE_BLE; |
Rohit Grover |
8:2214f1df6a6a | 97 | } else { |
Rohit Grover |
13:b8251438f5c4 | 98 | if (!lookupConvertedUUIDTable(uuid.getBaseUUID(), &nordicUUID.type)) { |
Rohit Grover |
13:b8251438f5c4 | 99 | nordicUUID.type = custom_add_uuid_base(uuid.getBaseUUID()); |
Rohit Grover |
13:b8251438f5c4 | 100 | addToConvertedUUIDTable(uuid.getBaseUUID(), nordicUUID.type); |
Rohit Grover |
9:3794dc9540f0 | 101 | } |
Rohit Grover |
8:2214f1df6a6a | 102 | } |
Rohit Grover |
8:2214f1df6a6a | 103 | |
Rohit Grover |
10:68c0e6cabe07 | 104 | return nordicUUID; |
Rohit Grover |
8:2214f1df6a6a | 105 | } |
Rohit Grover |
8:2214f1df6a6a | 106 | |
bogdanm | 0:eff01767de02 | 107 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 108 | /*! |
bogdanm | 0:eff01767de02 | 109 | @brief Adds the base UUID to the custom service. All UUIDs used |
bogdanm | 0:eff01767de02 | 110 | by this service are based on this 128-bit UUID. |
Rohit Grover |
6:bbb4357dc135 | 111 | |
bogdanm | 0:eff01767de02 | 112 | @note This UUID needs to be added to the SoftDevice stack before |
bogdanm | 0:eff01767de02 | 113 | adding the service's primary service via |
bogdanm | 0:eff01767de02 | 114 | 'sd_ble_gatts_service_add' |
bogdanm | 0:eff01767de02 | 115 | |
bogdanm | 0:eff01767de02 | 116 | @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) |
Rohit Grover |
6:bbb4357dc135 | 117 | |
bogdanm | 0:eff01767de02 | 118 | @returns The UUID type. |
bogdanm | 0:eff01767de02 | 119 | A return value of 0 should be considered an error. |
Rohit Grover |
6:bbb4357dc135 | 120 | |
bogdanm | 0:eff01767de02 | 121 | @retval 0x00 BLE_UUID_TYPE_UNKNOWN |
bogdanm | 0:eff01767de02 | 122 | @retval 0x01 BLE_UUID_TYPE_BLE |
bogdanm | 0:eff01767de02 | 123 | @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN |
Rohit Grover |
6:bbb4357dc135 | 124 | |
bogdanm | 0:eff01767de02 | 125 | @section EXAMPLE |
bogdanm | 0:eff01767de02 | 126 | @code |
bogdanm | 0:eff01767de02 | 127 | |
bogdanm | 0:eff01767de02 | 128 | // Take note that bytes 2/3 are blank since these are used to identify |
bogdanm | 0:eff01767de02 | 129 | // the primary service and individual characteristics |
bogdanm | 0:eff01767de02 | 130 | #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E" |
Rohit Grover |
6:bbb4357dc135 | 131 | |
bogdanm | 0:eff01767de02 | 132 | uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); |
bogdanm | 0:eff01767de02 | 133 | ASSERT(uuid_type > 0, ERROR_NOT_FOUND); |
Rohit Grover |
6:bbb4357dc135 | 134 | |
bogdanm | 0:eff01767de02 | 135 | // We can now safely add the primary service and any characteristics |
bogdanm | 0:eff01767de02 | 136 | // for our custom service ... |
Rohit Grover |
6:bbb4357dc135 | 137 | |
bogdanm | 0:eff01767de02 | 138 | @endcode |
bogdanm | 0:eff01767de02 | 139 | */ |
bogdanm | 0:eff01767de02 | 140 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 141 | uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base) |
bogdanm | 0:eff01767de02 | 142 | { |
Rohit Grover |
6:bbb4357dc135 | 143 | ble_uuid128_t base_uuid; |
Rohit Grover |
6:bbb4357dc135 | 144 | uint8_t uuid_type = 0; |
bogdanm | 0:eff01767de02 | 145 | |
Rohit Grover |
6:bbb4357dc135 | 146 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
Rohit Grover |
6:bbb4357dc135 | 147 | for (uint8_t i = 0; i<16; i++) { |
Rohit Grover |
6:bbb4357dc135 | 148 | base_uuid.uuid128[i] = p_uuid_base[15 - i]; |
Rohit Grover |
6:bbb4357dc135 | 149 | } |
bogdanm | 0:eff01767de02 | 150 | |
Rohit Grover |
6:bbb4357dc135 | 151 | ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); |
bogdanm | 0:eff01767de02 | 152 | |
Rohit Grover |
6:bbb4357dc135 | 153 | return uuid_type; |
bogdanm | 0:eff01767de02 | 154 | } |
bogdanm | 0:eff01767de02 | 155 | |
bogdanm | 0:eff01767de02 | 156 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 157 | /*! |
bogdanm | 0:eff01767de02 | 158 | |
bogdanm | 0:eff01767de02 | 159 | */ |
bogdanm | 0:eff01767de02 | 160 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 161 | error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, |
Rohit Grover |
6:bbb4357dc135 | 162 | ble_uuid_t *p_uuid) |
bogdanm | 0:eff01767de02 | 163 | { |
Rohit Grover |
6:bbb4357dc135 | 164 | uint8_t uuid_base_le[16]; |
bogdanm | 0:eff01767de02 | 165 | |
Rohit Grover |
6:bbb4357dc135 | 166 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
Rohit Grover |
6:bbb4357dc135 | 167 | for (uint8_t i = 0; i<16; i++) { |
Rohit Grover |
6:bbb4357dc135 | 168 | uuid_base_le[i] = p_uuid_base[15 - i]; |
Rohit Grover |
6:bbb4357dc135 | 169 | } |
bogdanm | 0:eff01767de02 | 170 | |
Rohit Grover |
6:bbb4357dc135 | 171 | ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid)); |
bogdanm | 0:eff01767de02 | 172 | |
Rohit Grover |
6:bbb4357dc135 | 173 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 174 | } |
bogdanm | 0:eff01767de02 | 175 | |
bogdanm | 0:eff01767de02 | 176 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 177 | /*! |
bogdanm | 0:eff01767de02 | 178 | @brief Adds a new characteristic to the custom service, assigning |
bogdanm | 0:eff01767de02 | 179 | properties, a UUID add-on value, etc. |
bogdanm | 0:eff01767de02 | 180 | |
bogdanm | 0:eff01767de02 | 181 | @param[in] service_handle |
bogdanm | 0:eff01767de02 | 182 | @param[in] p_uuid The 16-bit value to add to the base UUID |
bogdanm | 0:eff01767de02 | 183 | for this characteristic (normally >1 |
bogdanm | 0:eff01767de02 | 184 | since 1 is typically used by the primary |
bogdanm | 0:eff01767de02 | 185 | service). |
bogdanm | 0:eff01767de02 | 186 | @param[in] char_props The characteristic properties, as |
bogdanm | 0:eff01767de02 | 187 | defined by ble_gatt_char_props_t |
bogdanm | 0:eff01767de02 | 188 | @param[in] max_length The maximum length of this characeristic |
bogdanm | 0:eff01767de02 | 189 | @param[in] p_char_handle |
Rohit Grover |
6:bbb4357dc135 | 190 | |
bogdanm | 0:eff01767de02 | 191 | @returns |
bogdanm | 0:eff01767de02 | 192 | @retval ERROR_NONE Everything executed normally |
bogdanm | 0:eff01767de02 | 193 | */ |
bogdanm | 0:eff01767de02 | 194 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 195 | error_t custom_add_in_characteristic(uint16_t service_handle, |
Rohit Grover |
6:bbb4357dc135 | 196 | ble_uuid_t *p_uuid, |
Rohit Grover |
6:bbb4357dc135 | 197 | uint8_t properties, |
Rohit Grover |
6:bbb4357dc135 | 198 | uint8_t *p_data, |
Rohit Grover |
6:bbb4357dc135 | 199 | uint16_t min_length, |
Rohit Grover |
6:bbb4357dc135 | 200 | uint16_t max_length, |
Rohit Grover |
6:bbb4357dc135 | 201 | ble_gatts_char_handles_t *p_char_handle) |
bogdanm | 0:eff01767de02 | 202 | { |
Rohit Grover |
6:bbb4357dc135 | 203 | /* Characteristic metadata */ |
Rohit Grover |
6:bbb4357dc135 | 204 | ble_gatts_attr_md_t cccd_md; |
Rohit Grover |
6:bbb4357dc135 | 205 | ble_gatt_char_props_t char_props; |
bogdanm | 0:eff01767de02 | 206 | |
Rohit Grover |
6:bbb4357dc135 | 207 | memcpy(&char_props, &properties, 1); |
bogdanm | 0:eff01767de02 | 208 | |
Rohit Grover |
6:bbb4357dc135 | 209 | if (char_props.notify || char_props.indicate) { |
Rohit Grover |
6:bbb4357dc135 | 210 | /* Notification requires cccd */ |
Rohit Grover |
6:bbb4357dc135 | 211 | memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); |
Rohit Grover |
6:bbb4357dc135 | 212 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
Rohit Grover |
6:bbb4357dc135 | 213 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
Rohit Grover |
6:bbb4357dc135 | 214 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); |
Rohit Grover |
6:bbb4357dc135 | 215 | } |
bogdanm | 0:eff01767de02 | 216 | |
Rohit Grover |
6:bbb4357dc135 | 217 | ble_gatts_char_md_t char_md = {0}; |
bogdanm | 0:eff01767de02 | 218 | |
Rohit Grover |
6:bbb4357dc135 | 219 | char_md.char_props = char_props; |
Rohit Grover |
6:bbb4357dc135 | 220 | char_md.p_cccd_md = |
Rohit Grover |
6:bbb4357dc135 | 221 | (char_props.notify || char_props.indicate) ? &cccd_md : NULL; |
bogdanm | 0:eff01767de02 | 222 | |
Rohit Grover |
6:bbb4357dc135 | 223 | /* Attribute declaration */ |
Rohit Grover |
6:bbb4357dc135 | 224 | ble_gatts_attr_md_t attr_md = {0}; |
bogdanm | 0:eff01767de02 | 225 | |
Rohit Grover |
6:bbb4357dc135 | 226 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
Rohit Grover |
6:bbb4357dc135 | 227 | attr_md.vlen = (min_length == max_length) ? 0 : 1; |
bogdanm | 0:eff01767de02 | 228 | |
Rohit Grover |
6:bbb4357dc135 | 229 | if (char_props.read || char_props.notify || char_props.indicate) { |
Rohit Grover |
6:bbb4357dc135 | 230 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
Rohit Grover |
6:bbb4357dc135 | 231 | } |
Rohit Grover |
6:bbb4357dc135 | 232 | |
Rohit Grover |
6:bbb4357dc135 | 233 | if (char_props.write) { |
Rohit Grover |
6:bbb4357dc135 | 234 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
Rohit Grover |
6:bbb4357dc135 | 235 | } |
bogdanm | 0:eff01767de02 | 236 | |
Rohit Grover |
6:bbb4357dc135 | 237 | ble_gatts_attr_t attr_char_value = {0}; |
bogdanm | 0:eff01767de02 | 238 | |
Rohit Grover |
6:bbb4357dc135 | 239 | attr_char_value.p_uuid = p_uuid; |
Rohit Grover |
6:bbb4357dc135 | 240 | attr_char_value.p_attr_md = &attr_md; |
Rohit Grover |
6:bbb4357dc135 | 241 | attr_char_value.init_len = min_length; |
Rohit Grover |
6:bbb4357dc135 | 242 | attr_char_value.max_len = max_length; |
Rohit Grover |
6:bbb4357dc135 | 243 | attr_char_value.p_value = p_data; |
bogdanm | 0:eff01767de02 | 244 | |
bogdanm | 0:eff01767de02 | 245 | |
Rohit Grover |
6:bbb4357dc135 | 246 | ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, |
Rohit Grover |
6:bbb4357dc135 | 247 | &char_md, |
Rohit Grover |
6:bbb4357dc135 | 248 | &attr_char_value, |
Rohit Grover |
6:bbb4357dc135 | 249 | p_char_handle)); |
bogdanm | 0:eff01767de02 | 250 | |
Rohit Grover |
6:bbb4357dc135 | 251 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 252 | } |