Copy of nRF51822 library
Fork of nRF51822 by
btle/custom/custom_helper.cpp@9:3794dc9540f0, 2014-05-28 (annotated)
- Committer:
- Rohit Grover
- Date:
- Wed May 28 17:46:58 2014 +0100
- Revision:
- 9:3794dc9540f0
- Parent:
- 8:2214f1df6a6a
- Child:
- 10:68c0e6cabe07
add skeleton for converted table for 128-bit UUIDs
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 |
8:2214f1df6a6a | 19 | /** |
Rohit Grover |
9:3794dc9540f0 | 20 | * lookup the cache of previously converted 128-bit UUIDs to find a type value. |
Rohit Grover |
9:3794dc9540f0 | 21 | * @param uuid long UUID |
Rohit Grover |
9:3794dc9540f0 | 22 | * @param recoveredType the type field of the 3-byte nRF's uuid. |
Rohit Grover |
9:3794dc9540f0 | 23 | * @return true if a match is found. |
Rohit Grover |
9:3794dc9540f0 | 24 | */ |
Rohit Grover |
9:3794dc9540f0 | 25 | static bool |
Rohit Grover |
9:3794dc9540f0 | 26 | lookupConvertedUUIDTable(const uint8_t uuid[UUID::LENGTH_OF_LONG_UUID], |
Rohit Grover |
9:3794dc9540f0 | 27 | uint8_t *recoveredType) |
Rohit Grover |
9:3794dc9540f0 | 28 | { |
Rohit Grover |
9:3794dc9540f0 | 29 | return false; |
Rohit Grover |
9:3794dc9540f0 | 30 | } |
Rohit Grover |
9:3794dc9540f0 | 31 | |
Rohit Grover |
9:3794dc9540f0 | 32 | static void |
Rohit Grover |
9:3794dc9540f0 | 33 | addToConvertedUUIDTable(const uint8_t uuid[UUID::LENGTH_OF_LONG_UUID], |
Rohit Grover |
9:3794dc9540f0 | 34 | uint8_t recoveredType) |
Rohit Grover |
9:3794dc9540f0 | 35 | { |
Rohit Grover |
9:3794dc9540f0 | 36 | /* empty for now */ |
Rohit Grover |
9:3794dc9540f0 | 37 | } |
Rohit Grover |
9:3794dc9540f0 | 38 | |
Rohit Grover |
9:3794dc9540f0 | 39 | /** |
Rohit Grover |
8:2214f1df6a6a | 40 | * The nRF transport has its own 3-byte representation of a UUID. If the user- |
Rohit Grover |
8:2214f1df6a6a | 41 | * specified UUID is 128-bits wide, then the UUID base needs to be added to the |
Rohit Grover |
8:2214f1df6a6a | 42 | * soft-device and converted to a 3-byte handle before being used further. This |
Rohit Grover |
8:2214f1df6a6a | 43 | * function is responsible for this translation of user-specified UUIDs into |
Rohit Grover |
8:2214f1df6a6a | 44 | * nRF's representation. |
Rohit Grover |
8:2214f1df6a6a | 45 | * |
Rohit Grover |
8:2214f1df6a6a | 46 | * @param[in] uuid |
Rohit Grover |
8:2214f1df6a6a | 47 | * user-specified UUID |
Rohit Grover |
8:2214f1df6a6a | 48 | * @return nRF |
Rohit Grover |
8:2214f1df6a6a | 49 | * 3-byte UUID (containing a type and 16-bit UUID) representation |
Rohit Grover |
8:2214f1df6a6a | 50 | * to be used with SVC calls. |
Rohit Grover |
8:2214f1df6a6a | 51 | */ |
Rohit Grover |
8:2214f1df6a6a | 52 | ble_uuid_t custom_convert_to_transport_uuid(const UUID &uuid) |
Rohit Grover |
8:2214f1df6a6a | 53 | { |
Rohit Grover |
8:2214f1df6a6a | 54 | ble_uuid_t transportUUID = { |
Rohit Grover |
8:2214f1df6a6a | 55 | .uuid = uuid.value, |
Rohit Grover |
8:2214f1df6a6a | 56 | .type = BLE_UUID_TYPE_UNKNOWN /* to be set below */ |
Rohit Grover |
8:2214f1df6a6a | 57 | }; |
Rohit Grover |
8:2214f1df6a6a | 58 | |
Rohit Grover |
8:2214f1df6a6a | 59 | if (uuid.type == UUID::UUID_TYPE_SHORT) { |
Rohit Grover |
8:2214f1df6a6a | 60 | transportUUID.type = BLE_UUID_TYPE_BLE; |
Rohit Grover |
8:2214f1df6a6a | 61 | } else { |
Rohit Grover |
9:3794dc9540f0 | 62 | if (!lookupConvertedUUIDTable(uuid.base, &transportUUID.type)) { |
Rohit Grover |
9:3794dc9540f0 | 63 | transportUUID.type = custom_add_uuid_base(uuid.base); |
Rohit Grover |
9:3794dc9540f0 | 64 | addToConvertedUUIDTable(uuid.base, transportUUID.type); |
Rohit Grover |
9:3794dc9540f0 | 65 | } |
Rohit Grover |
8:2214f1df6a6a | 66 | } |
Rohit Grover |
8:2214f1df6a6a | 67 | |
Rohit Grover |
8:2214f1df6a6a | 68 | return transportUUID; |
Rohit Grover |
8:2214f1df6a6a | 69 | } |
Rohit Grover |
8:2214f1df6a6a | 70 | |
bogdanm | 0:eff01767de02 | 71 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 72 | /*! |
bogdanm | 0:eff01767de02 | 73 | @brief Adds the base UUID to the custom service. All UUIDs used |
bogdanm | 0:eff01767de02 | 74 | by this service are based on this 128-bit UUID. |
Rohit Grover |
6:bbb4357dc135 | 75 | |
bogdanm | 0:eff01767de02 | 76 | @note This UUID needs to be added to the SoftDevice stack before |
bogdanm | 0:eff01767de02 | 77 | adding the service's primary service via |
bogdanm | 0:eff01767de02 | 78 | 'sd_ble_gatts_service_add' |
bogdanm | 0:eff01767de02 | 79 | |
bogdanm | 0:eff01767de02 | 80 | @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) |
Rohit Grover |
6:bbb4357dc135 | 81 | |
bogdanm | 0:eff01767de02 | 82 | @returns The UUID type. |
bogdanm | 0:eff01767de02 | 83 | A return value of 0 should be considered an error. |
Rohit Grover |
6:bbb4357dc135 | 84 | |
bogdanm | 0:eff01767de02 | 85 | @retval 0x00 BLE_UUID_TYPE_UNKNOWN |
bogdanm | 0:eff01767de02 | 86 | @retval 0x01 BLE_UUID_TYPE_BLE |
bogdanm | 0:eff01767de02 | 87 | @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN |
Rohit Grover |
6:bbb4357dc135 | 88 | |
bogdanm | 0:eff01767de02 | 89 | @section EXAMPLE |
bogdanm | 0:eff01767de02 | 90 | @code |
bogdanm | 0:eff01767de02 | 91 | |
bogdanm | 0:eff01767de02 | 92 | // Take note that bytes 2/3 are blank since these are used to identify |
bogdanm | 0:eff01767de02 | 93 | // the primary service and individual characteristics |
bogdanm | 0:eff01767de02 | 94 | #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 | 95 | |
bogdanm | 0:eff01767de02 | 96 | uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); |
bogdanm | 0:eff01767de02 | 97 | ASSERT(uuid_type > 0, ERROR_NOT_FOUND); |
Rohit Grover |
6:bbb4357dc135 | 98 | |
bogdanm | 0:eff01767de02 | 99 | // We can now safely add the primary service and any characteristics |
bogdanm | 0:eff01767de02 | 100 | // for our custom service ... |
Rohit Grover |
6:bbb4357dc135 | 101 | |
bogdanm | 0:eff01767de02 | 102 | @endcode |
bogdanm | 0:eff01767de02 | 103 | */ |
bogdanm | 0:eff01767de02 | 104 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 105 | uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base) |
bogdanm | 0:eff01767de02 | 106 | { |
Rohit Grover |
6:bbb4357dc135 | 107 | ble_uuid128_t base_uuid; |
Rohit Grover |
6:bbb4357dc135 | 108 | uint8_t uuid_type = 0; |
bogdanm | 0:eff01767de02 | 109 | |
Rohit Grover |
6:bbb4357dc135 | 110 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
Rohit Grover |
6:bbb4357dc135 | 111 | for (uint8_t i = 0; i<16; i++) { |
Rohit Grover |
6:bbb4357dc135 | 112 | base_uuid.uuid128[i] = p_uuid_base[15 - i]; |
Rohit Grover |
6:bbb4357dc135 | 113 | } |
bogdanm | 0:eff01767de02 | 114 | |
Rohit Grover |
6:bbb4357dc135 | 115 | ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); |
bogdanm | 0:eff01767de02 | 116 | |
Rohit Grover |
6:bbb4357dc135 | 117 | return uuid_type; |
bogdanm | 0:eff01767de02 | 118 | } |
bogdanm | 0:eff01767de02 | 119 | |
bogdanm | 0:eff01767de02 | 120 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 121 | /*! |
bogdanm | 0:eff01767de02 | 122 | |
bogdanm | 0:eff01767de02 | 123 | */ |
bogdanm | 0:eff01767de02 | 124 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 125 | error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, |
Rohit Grover |
6:bbb4357dc135 | 126 | ble_uuid_t *p_uuid) |
bogdanm | 0:eff01767de02 | 127 | { |
Rohit Grover |
6:bbb4357dc135 | 128 | uint8_t uuid_base_le[16]; |
bogdanm | 0:eff01767de02 | 129 | |
Rohit Grover |
6:bbb4357dc135 | 130 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
Rohit Grover |
6:bbb4357dc135 | 131 | for (uint8_t i = 0; i<16; i++) { |
Rohit Grover |
6:bbb4357dc135 | 132 | uuid_base_le[i] = p_uuid_base[15 - i]; |
Rohit Grover |
6:bbb4357dc135 | 133 | } |
bogdanm | 0:eff01767de02 | 134 | |
Rohit Grover |
6:bbb4357dc135 | 135 | ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid)); |
bogdanm | 0:eff01767de02 | 136 | |
Rohit Grover |
6:bbb4357dc135 | 137 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 138 | } |
bogdanm | 0:eff01767de02 | 139 | |
bogdanm | 0:eff01767de02 | 140 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 141 | /*! |
bogdanm | 0:eff01767de02 | 142 | @brief Adds a new characteristic to the custom service, assigning |
bogdanm | 0:eff01767de02 | 143 | properties, a UUID add-on value, etc. |
bogdanm | 0:eff01767de02 | 144 | |
bogdanm | 0:eff01767de02 | 145 | @param[in] service_handle |
bogdanm | 0:eff01767de02 | 146 | @param[in] p_uuid The 16-bit value to add to the base UUID |
bogdanm | 0:eff01767de02 | 147 | for this characteristic (normally >1 |
bogdanm | 0:eff01767de02 | 148 | since 1 is typically used by the primary |
bogdanm | 0:eff01767de02 | 149 | service). |
bogdanm | 0:eff01767de02 | 150 | @param[in] char_props The characteristic properties, as |
bogdanm | 0:eff01767de02 | 151 | defined by ble_gatt_char_props_t |
bogdanm | 0:eff01767de02 | 152 | @param[in] max_length The maximum length of this characeristic |
bogdanm | 0:eff01767de02 | 153 | @param[in] p_char_handle |
Rohit Grover |
6:bbb4357dc135 | 154 | |
bogdanm | 0:eff01767de02 | 155 | @returns |
bogdanm | 0:eff01767de02 | 156 | @retval ERROR_NONE Everything executed normally |
bogdanm | 0:eff01767de02 | 157 | */ |
bogdanm | 0:eff01767de02 | 158 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 159 | error_t custom_add_in_characteristic(uint16_t service_handle, |
Rohit Grover |
6:bbb4357dc135 | 160 | ble_uuid_t *p_uuid, |
Rohit Grover |
6:bbb4357dc135 | 161 | uint8_t properties, |
Rohit Grover |
6:bbb4357dc135 | 162 | uint8_t *p_data, |
Rohit Grover |
6:bbb4357dc135 | 163 | uint16_t min_length, |
Rohit Grover |
6:bbb4357dc135 | 164 | uint16_t max_length, |
Rohit Grover |
6:bbb4357dc135 | 165 | ble_gatts_char_handles_t *p_char_handle) |
bogdanm | 0:eff01767de02 | 166 | { |
Rohit Grover |
6:bbb4357dc135 | 167 | /* Characteristic metadata */ |
Rohit Grover |
6:bbb4357dc135 | 168 | ble_gatts_attr_md_t cccd_md; |
Rohit Grover |
6:bbb4357dc135 | 169 | ble_gatt_char_props_t char_props; |
bogdanm | 0:eff01767de02 | 170 | |
Rohit Grover |
6:bbb4357dc135 | 171 | memcpy(&char_props, &properties, 1); |
bogdanm | 0:eff01767de02 | 172 | |
Rohit Grover |
6:bbb4357dc135 | 173 | if (char_props.notify || char_props.indicate) { |
Rohit Grover |
6:bbb4357dc135 | 174 | /* Notification requires cccd */ |
Rohit Grover |
6:bbb4357dc135 | 175 | memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); |
Rohit Grover |
6:bbb4357dc135 | 176 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
Rohit Grover |
6:bbb4357dc135 | 177 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
Rohit Grover |
6:bbb4357dc135 | 178 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); |
Rohit Grover |
6:bbb4357dc135 | 179 | } |
bogdanm | 0:eff01767de02 | 180 | |
Rohit Grover |
6:bbb4357dc135 | 181 | ble_gatts_char_md_t char_md = {0}; |
bogdanm | 0:eff01767de02 | 182 | |
Rohit Grover |
6:bbb4357dc135 | 183 | char_md.char_props = char_props; |
Rohit Grover |
6:bbb4357dc135 | 184 | char_md.p_cccd_md = |
Rohit Grover |
6:bbb4357dc135 | 185 | (char_props.notify || char_props.indicate) ? &cccd_md : NULL; |
bogdanm | 0:eff01767de02 | 186 | |
Rohit Grover |
6:bbb4357dc135 | 187 | /* Attribute declaration */ |
Rohit Grover |
6:bbb4357dc135 | 188 | ble_gatts_attr_md_t attr_md = {0}; |
bogdanm | 0:eff01767de02 | 189 | |
Rohit Grover |
6:bbb4357dc135 | 190 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
Rohit Grover |
6:bbb4357dc135 | 191 | attr_md.vlen = (min_length == max_length) ? 0 : 1; |
bogdanm | 0:eff01767de02 | 192 | |
Rohit Grover |
6:bbb4357dc135 | 193 | if (char_props.read || char_props.notify || char_props.indicate) { |
Rohit Grover |
6:bbb4357dc135 | 194 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
Rohit Grover |
6:bbb4357dc135 | 195 | } |
Rohit Grover |
6:bbb4357dc135 | 196 | |
Rohit Grover |
6:bbb4357dc135 | 197 | if (char_props.write) { |
Rohit Grover |
6:bbb4357dc135 | 198 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
Rohit Grover |
6:bbb4357dc135 | 199 | } |
bogdanm | 0:eff01767de02 | 200 | |
Rohit Grover |
6:bbb4357dc135 | 201 | ble_gatts_attr_t attr_char_value = {0}; |
bogdanm | 0:eff01767de02 | 202 | |
Rohit Grover |
6:bbb4357dc135 | 203 | attr_char_value.p_uuid = p_uuid; |
Rohit Grover |
6:bbb4357dc135 | 204 | attr_char_value.p_attr_md = &attr_md; |
Rohit Grover |
6:bbb4357dc135 | 205 | attr_char_value.init_len = min_length; |
Rohit Grover |
6:bbb4357dc135 | 206 | attr_char_value.max_len = max_length; |
Rohit Grover |
6:bbb4357dc135 | 207 | attr_char_value.p_value = p_data; |
bogdanm | 0:eff01767de02 | 208 | |
bogdanm | 0:eff01767de02 | 209 | |
Rohit Grover |
6:bbb4357dc135 | 210 | ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, |
Rohit Grover |
6:bbb4357dc135 | 211 | &char_md, |
Rohit Grover |
6:bbb4357dc135 | 212 | &attr_char_value, |
Rohit Grover |
6:bbb4357dc135 | 213 | p_char_handle)); |
bogdanm | 0:eff01767de02 | 214 | |
Rohit Grover |
6:bbb4357dc135 | 215 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 216 | } |