Clone of the nRF51822 repository from github The correct home is https://github.com/lancaster-university/nRF51822

Dependencies:   nrf51-sdk

Dependents:   microbit-dal microbit-ble-open microbit-dal-eddystone microbit-dal-ble-accelerometer-example ... more

Committer:
Rohit Grover
Date:
Wed May 28 17:27:23 2014 +0100
Revision:
8:2214f1df6a6a
Parent:
6:bbb4357dc135
Child:
9:3794dc9540f0
convert_to_transport_uuid() now works with 128bit uuids

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:eff01767de02 1 /* mbed Microcontroller Library
bogdanm 0:eff01767de02 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 0:eff01767de02 3 *
bogdanm 0:eff01767de02 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 0:eff01767de02 5 * you may not use this file except in compliance with the License.
bogdanm 0:eff01767de02 6 * You may obtain a copy of the License at
bogdanm 0:eff01767de02 7 *
bogdanm 0:eff01767de02 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 0:eff01767de02 9 *
bogdanm 0:eff01767de02 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 0:eff01767de02 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 0:eff01767de02 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 0:eff01767de02 13 * See the License for the specific language governing permissions and
bogdanm 0:eff01767de02 14 * limitations under the License.
bogdanm 0:eff01767de02 15 */
bogdanm 0:eff01767de02 16
bogdanm 0:eff01767de02 17 #include "custom_helper.h"
bogdanm 0:eff01767de02 18
Rohit Grover 8:2214f1df6a6a 19 /**
Rohit Grover 8:2214f1df6a6a 20 * The nRF transport has its own 3-byte representation of a UUID. If the user-
Rohit Grover 8:2214f1df6a6a 21 * specified UUID is 128-bits wide, then the UUID base needs to be added to the
Rohit Grover 8:2214f1df6a6a 22 * soft-device and converted to a 3-byte handle before being used further. This
Rohit Grover 8:2214f1df6a6a 23 * function is responsible for this translation of user-specified UUIDs into
Rohit Grover 8:2214f1df6a6a 24 * nRF's representation.
Rohit Grover 8:2214f1df6a6a 25 *
Rohit Grover 8:2214f1df6a6a 26 * @param[in] uuid
Rohit Grover 8:2214f1df6a6a 27 * user-specified UUID
Rohit Grover 8:2214f1df6a6a 28 * @return nRF
Rohit Grover 8:2214f1df6a6a 29 * 3-byte UUID (containing a type and 16-bit UUID) representation
Rohit Grover 8:2214f1df6a6a 30 * to be used with SVC calls.
Rohit Grover 8:2214f1df6a6a 31 */
Rohit Grover 8:2214f1df6a6a 32 ble_uuid_t custom_convert_to_transport_uuid(const UUID &uuid)
Rohit Grover 8:2214f1df6a6a 33 {
Rohit Grover 8:2214f1df6a6a 34 ble_uuid_t transportUUID = {
Rohit Grover 8:2214f1df6a6a 35 .uuid = uuid.value,
Rohit Grover 8:2214f1df6a6a 36 .type = BLE_UUID_TYPE_UNKNOWN /* to be set below */
Rohit Grover 8:2214f1df6a6a 37 };
Rohit Grover 8:2214f1df6a6a 38
Rohit Grover 8:2214f1df6a6a 39 if (uuid.type == UUID::UUID_TYPE_SHORT) {
Rohit Grover 8:2214f1df6a6a 40 transportUUID.type = BLE_UUID_TYPE_BLE;
Rohit Grover 8:2214f1df6a6a 41 } else {
Rohit Grover 8:2214f1df6a6a 42 transportUUID.type = custom_add_uuid_base(uuid.base);
Rohit Grover 8:2214f1df6a6a 43 }
Rohit Grover 8:2214f1df6a6a 44
Rohit Grover 8:2214f1df6a6a 45 return transportUUID;
Rohit Grover 8:2214f1df6a6a 46 }
Rohit Grover 8:2214f1df6a6a 47
bogdanm 0:eff01767de02 48 /**************************************************************************/
bogdanm 0:eff01767de02 49 /*!
bogdanm 0:eff01767de02 50 @brief Adds the base UUID to the custom service. All UUIDs used
bogdanm 0:eff01767de02 51 by this service are based on this 128-bit UUID.
Rohit Grover 6:bbb4357dc135 52
bogdanm 0:eff01767de02 53 @note This UUID needs to be added to the SoftDevice stack before
bogdanm 0:eff01767de02 54 adding the service's primary service via
bogdanm 0:eff01767de02 55 'sd_ble_gatts_service_add'
bogdanm 0:eff01767de02 56
bogdanm 0:eff01767de02 57 @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16)
Rohit Grover 6:bbb4357dc135 58
bogdanm 0:eff01767de02 59 @returns The UUID type.
bogdanm 0:eff01767de02 60 A return value of 0 should be considered an error.
Rohit Grover 6:bbb4357dc135 61
bogdanm 0:eff01767de02 62 @retval 0x00 BLE_UUID_TYPE_UNKNOWN
bogdanm 0:eff01767de02 63 @retval 0x01 BLE_UUID_TYPE_BLE
bogdanm 0:eff01767de02 64 @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN
Rohit Grover 6:bbb4357dc135 65
bogdanm 0:eff01767de02 66 @section EXAMPLE
bogdanm 0:eff01767de02 67 @code
bogdanm 0:eff01767de02 68
bogdanm 0:eff01767de02 69 // Take note that bytes 2/3 are blank since these are used to identify
bogdanm 0:eff01767de02 70 // the primary service and individual characteristics
bogdanm 0:eff01767de02 71 #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E"
Rohit Grover 6:bbb4357dc135 72
bogdanm 0:eff01767de02 73 uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE);
bogdanm 0:eff01767de02 74 ASSERT(uuid_type > 0, ERROR_NOT_FOUND);
Rohit Grover 6:bbb4357dc135 75
bogdanm 0:eff01767de02 76 // We can now safely add the primary service and any characteristics
bogdanm 0:eff01767de02 77 // for our custom service ...
Rohit Grover 6:bbb4357dc135 78
bogdanm 0:eff01767de02 79 @endcode
bogdanm 0:eff01767de02 80 */
bogdanm 0:eff01767de02 81 /**************************************************************************/
Rohit Grover 6:bbb4357dc135 82 uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base)
bogdanm 0:eff01767de02 83 {
Rohit Grover 6:bbb4357dc135 84 ble_uuid128_t base_uuid;
Rohit Grover 6:bbb4357dc135 85 uint8_t uuid_type = 0;
bogdanm 0:eff01767de02 86
Rohit Grover 6:bbb4357dc135 87 /* Reverse the bytes since ble_uuid128_t is LSB */
Rohit Grover 6:bbb4357dc135 88 for (uint8_t i = 0; i<16; i++) {
Rohit Grover 6:bbb4357dc135 89 base_uuid.uuid128[i] = p_uuid_base[15 - i];
Rohit Grover 6:bbb4357dc135 90 }
bogdanm 0:eff01767de02 91
Rohit Grover 6:bbb4357dc135 92 ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0);
bogdanm 0:eff01767de02 93
Rohit Grover 6:bbb4357dc135 94 return uuid_type;
bogdanm 0:eff01767de02 95 }
bogdanm 0:eff01767de02 96
bogdanm 0:eff01767de02 97 /**************************************************************************/
bogdanm 0:eff01767de02 98 /*!
bogdanm 0:eff01767de02 99
bogdanm 0:eff01767de02 100 */
bogdanm 0:eff01767de02 101 /**************************************************************************/
Rohit Grover 6:bbb4357dc135 102 error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base,
Rohit Grover 6:bbb4357dc135 103 ble_uuid_t *p_uuid)
bogdanm 0:eff01767de02 104 {
Rohit Grover 6:bbb4357dc135 105 uint8_t uuid_base_le[16];
bogdanm 0:eff01767de02 106
Rohit Grover 6:bbb4357dc135 107 /* Reverse the bytes since ble_uuid128_t is LSB */
Rohit Grover 6:bbb4357dc135 108 for (uint8_t i = 0; i<16; i++) {
Rohit Grover 6:bbb4357dc135 109 uuid_base_le[i] = p_uuid_base[15 - i];
Rohit Grover 6:bbb4357dc135 110 }
bogdanm 0:eff01767de02 111
Rohit Grover 6:bbb4357dc135 112 ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid));
bogdanm 0:eff01767de02 113
Rohit Grover 6:bbb4357dc135 114 return ERROR_NONE;
bogdanm 0:eff01767de02 115 }
bogdanm 0:eff01767de02 116
bogdanm 0:eff01767de02 117 /**************************************************************************/
bogdanm 0:eff01767de02 118 /*!
bogdanm 0:eff01767de02 119 @brief Adds a new characteristic to the custom service, assigning
bogdanm 0:eff01767de02 120 properties, a UUID add-on value, etc.
bogdanm 0:eff01767de02 121
bogdanm 0:eff01767de02 122 @param[in] service_handle
bogdanm 0:eff01767de02 123 @param[in] p_uuid The 16-bit value to add to the base UUID
bogdanm 0:eff01767de02 124 for this characteristic (normally >1
bogdanm 0:eff01767de02 125 since 1 is typically used by the primary
bogdanm 0:eff01767de02 126 service).
bogdanm 0:eff01767de02 127 @param[in] char_props The characteristic properties, as
bogdanm 0:eff01767de02 128 defined by ble_gatt_char_props_t
bogdanm 0:eff01767de02 129 @param[in] max_length The maximum length of this characeristic
bogdanm 0:eff01767de02 130 @param[in] p_char_handle
Rohit Grover 6:bbb4357dc135 131
bogdanm 0:eff01767de02 132 @returns
bogdanm 0:eff01767de02 133 @retval ERROR_NONE Everything executed normally
bogdanm 0:eff01767de02 134 */
bogdanm 0:eff01767de02 135 /**************************************************************************/
Rohit Grover 6:bbb4357dc135 136 error_t custom_add_in_characteristic(uint16_t service_handle,
Rohit Grover 6:bbb4357dc135 137 ble_uuid_t *p_uuid,
Rohit Grover 6:bbb4357dc135 138 uint8_t properties,
Rohit Grover 6:bbb4357dc135 139 uint8_t *p_data,
Rohit Grover 6:bbb4357dc135 140 uint16_t min_length,
Rohit Grover 6:bbb4357dc135 141 uint16_t max_length,
Rohit Grover 6:bbb4357dc135 142 ble_gatts_char_handles_t *p_char_handle)
bogdanm 0:eff01767de02 143 {
Rohit Grover 6:bbb4357dc135 144 /* Characteristic metadata */
Rohit Grover 6:bbb4357dc135 145 ble_gatts_attr_md_t cccd_md;
Rohit Grover 6:bbb4357dc135 146 ble_gatt_char_props_t char_props;
bogdanm 0:eff01767de02 147
Rohit Grover 6:bbb4357dc135 148 memcpy(&char_props, &properties, 1);
bogdanm 0:eff01767de02 149
Rohit Grover 6:bbb4357dc135 150 if (char_props.notify || char_props.indicate) {
Rohit Grover 6:bbb4357dc135 151 /* Notification requires cccd */
Rohit Grover 6:bbb4357dc135 152 memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t));
Rohit Grover 6:bbb4357dc135 153 cccd_md.vloc = BLE_GATTS_VLOC_STACK;
Rohit Grover 6:bbb4357dc135 154 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
Rohit Grover 6:bbb4357dc135 155 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
Rohit Grover 6:bbb4357dc135 156 }
bogdanm 0:eff01767de02 157
Rohit Grover 6:bbb4357dc135 158 ble_gatts_char_md_t char_md = {0};
bogdanm 0:eff01767de02 159
Rohit Grover 6:bbb4357dc135 160 char_md.char_props = char_props;
Rohit Grover 6:bbb4357dc135 161 char_md.p_cccd_md =
Rohit Grover 6:bbb4357dc135 162 (char_props.notify || char_props.indicate) ? &cccd_md : NULL;
bogdanm 0:eff01767de02 163
Rohit Grover 6:bbb4357dc135 164 /* Attribute declaration */
Rohit Grover 6:bbb4357dc135 165 ble_gatts_attr_md_t attr_md = {0};
bogdanm 0:eff01767de02 166
Rohit Grover 6:bbb4357dc135 167 attr_md.vloc = BLE_GATTS_VLOC_STACK;
Rohit Grover 6:bbb4357dc135 168 attr_md.vlen = (min_length == max_length) ? 0 : 1;
bogdanm 0:eff01767de02 169
Rohit Grover 6:bbb4357dc135 170 if (char_props.read || char_props.notify || char_props.indicate) {
Rohit Grover 6:bbb4357dc135 171 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
Rohit Grover 6:bbb4357dc135 172 }
Rohit Grover 6:bbb4357dc135 173
Rohit Grover 6:bbb4357dc135 174 if (char_props.write) {
Rohit Grover 6:bbb4357dc135 175 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
Rohit Grover 6:bbb4357dc135 176 }
bogdanm 0:eff01767de02 177
Rohit Grover 6:bbb4357dc135 178 ble_gatts_attr_t attr_char_value = {0};
bogdanm 0:eff01767de02 179
Rohit Grover 6:bbb4357dc135 180 attr_char_value.p_uuid = p_uuid;
Rohit Grover 6:bbb4357dc135 181 attr_char_value.p_attr_md = &attr_md;
Rohit Grover 6:bbb4357dc135 182 attr_char_value.init_len = min_length;
Rohit Grover 6:bbb4357dc135 183 attr_char_value.max_len = max_length;
Rohit Grover 6:bbb4357dc135 184 attr_char_value.p_value = p_data;
bogdanm 0:eff01767de02 185
bogdanm 0:eff01767de02 186
Rohit Grover 6:bbb4357dc135 187 ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle,
Rohit Grover 6:bbb4357dc135 188 &char_md,
Rohit Grover 6:bbb4357dc135 189 &attr_char_value,
Rohit Grover 6:bbb4357dc135 190 p_char_handle));
bogdanm 0:eff01767de02 191
Rohit Grover 6:bbb4357dc135 192 return ERROR_NONE;
bogdanm 0:eff01767de02 193 }