WallbotBLE_Challenge ホワイトボード消し版

Dependencies:   mbed

Fork of BLE_WallbotBLE_Challenge by JKSoft

Committer:
jksoft
Date:
Wed Nov 12 02:40:34 2014 +0000
Revision:
0:76dfa9657d9d
????????

Who changed what in which revision?

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