テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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