iOSのBLEコントローラアプリ「RCBController」と接続し、コントローラの操作を取得するサンプルプログラムです。 mbed HRM1017で動作を確認しています。 2014.08.20時点でのBLEライブラリに対応しました。

Dependencies:   BLE_API mbed

Fork of BLE_RCBController by Junichi Katsu

Committer:
jksoft
Date:
Wed Aug 20 13:41:01 2014 +0000
Revision:
4:ebda47d22091
Parent:
nRF51822/btle/custom/custom_helper.cpp@1:48f6e08a3ac2
?????????

Who changed what in which revision?

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