BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

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