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