Nordic stack and drivers for the mbed BLE API
Fork of nRF51822 by
btle/custom/custom_helper.cpp@0:eff01767de02, 2014-03-26 (annotated)
- Committer:
- bogdanm
- Date:
- Wed Mar 26 14:38:17 2014 +0000
- Revision:
- 0:eff01767de02
- Child:
- 6:bbb4357dc135
Initial import of the nRF51822 code
Who changed what in which revision?
User | Revision | Line number | New 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 | |
bogdanm | 0:eff01767de02 | 19 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 20 | /*! |
bogdanm | 0:eff01767de02 | 21 | @brief Adds the base UUID to the custom service. All UUIDs used |
bogdanm | 0:eff01767de02 | 22 | by this service are based on this 128-bit UUID. |
bogdanm | 0:eff01767de02 | 23 | |
bogdanm | 0:eff01767de02 | 24 | @note This UUID needs to be added to the SoftDevice stack before |
bogdanm | 0:eff01767de02 | 25 | adding the service's primary service via |
bogdanm | 0:eff01767de02 | 26 | 'sd_ble_gatts_service_add' |
bogdanm | 0:eff01767de02 | 27 | |
bogdanm | 0:eff01767de02 | 28 | @param[in] p_uuid_base A pointer to the 128-bit UUID array (8*16) |
bogdanm | 0:eff01767de02 | 29 | |
bogdanm | 0:eff01767de02 | 30 | @returns The UUID type. |
bogdanm | 0:eff01767de02 | 31 | A return value of 0 should be considered an error. |
bogdanm | 0:eff01767de02 | 32 | |
bogdanm | 0:eff01767de02 | 33 | @retval 0x00 BLE_UUID_TYPE_UNKNOWN |
bogdanm | 0:eff01767de02 | 34 | @retval 0x01 BLE_UUID_TYPE_BLE |
bogdanm | 0:eff01767de02 | 35 | @retval 0x02 BLE_UUID_TYPE_VENDOR_BEGIN |
bogdanm | 0:eff01767de02 | 36 | |
bogdanm | 0:eff01767de02 | 37 | @section EXAMPLE |
bogdanm | 0:eff01767de02 | 38 | @code |
bogdanm | 0:eff01767de02 | 39 | |
bogdanm | 0:eff01767de02 | 40 | // Take note that bytes 2/3 are blank since these are used to identify |
bogdanm | 0:eff01767de02 | 41 | // the primary service and individual characteristics |
bogdanm | 0:eff01767de02 | 42 | #define CFG_CUSTOM_UUID_BASE "\x6E\x40\x00\x00\xB5\xA3\xF3\x93\xE0\xA9\xE5\x0E\x24\xDC\xCA\x9E" |
bogdanm | 0:eff01767de02 | 43 | |
bogdanm | 0:eff01767de02 | 44 | uint8_t uuid_type = custom_add_uuid_base(CFG_CUSTOM_UUID_BASE); |
bogdanm | 0:eff01767de02 | 45 | ASSERT(uuid_type > 0, ERROR_NOT_FOUND); |
bogdanm | 0:eff01767de02 | 46 | |
bogdanm | 0:eff01767de02 | 47 | // We can now safely add the primary service and any characteristics |
bogdanm | 0:eff01767de02 | 48 | // for our custom service ... |
bogdanm | 0:eff01767de02 | 49 | |
bogdanm | 0:eff01767de02 | 50 | @endcode |
bogdanm | 0:eff01767de02 | 51 | */ |
bogdanm | 0:eff01767de02 | 52 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 53 | uint8_t custom_add_uuid_base(uint8_t const * const p_uuid_base) |
bogdanm | 0:eff01767de02 | 54 | { |
bogdanm | 0:eff01767de02 | 55 | ble_uuid128_t base_uuid; |
bogdanm | 0:eff01767de02 | 56 | uint8_t uuid_type = 0; |
bogdanm | 0:eff01767de02 | 57 | |
bogdanm | 0:eff01767de02 | 58 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
bogdanm | 0:eff01767de02 | 59 | for(uint8_t i=0; i<16; i++) |
bogdanm | 0:eff01767de02 | 60 | { |
bogdanm | 0:eff01767de02 | 61 | base_uuid.uuid128[i] = p_uuid_base[15-i]; |
bogdanm | 0:eff01767de02 | 62 | } |
bogdanm | 0:eff01767de02 | 63 | |
bogdanm | 0:eff01767de02 | 64 | ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); |
bogdanm | 0:eff01767de02 | 65 | |
bogdanm | 0:eff01767de02 | 66 | return uuid_type; |
bogdanm | 0:eff01767de02 | 67 | } |
bogdanm | 0:eff01767de02 | 68 | |
bogdanm | 0:eff01767de02 | 69 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 70 | /*! |
bogdanm | 0:eff01767de02 | 71 | |
bogdanm | 0:eff01767de02 | 72 | */ |
bogdanm | 0:eff01767de02 | 73 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 74 | error_t custom_decode_uuid_base(uint8_t const * const p_uuid_base, ble_uuid_t * p_uuid) |
bogdanm | 0:eff01767de02 | 75 | { |
bogdanm | 0:eff01767de02 | 76 | uint8_t uuid_base_le[16]; |
bogdanm | 0:eff01767de02 | 77 | |
bogdanm | 0:eff01767de02 | 78 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
bogdanm | 0:eff01767de02 | 79 | for(uint8_t i=0; i<16; i++) |
bogdanm | 0:eff01767de02 | 80 | { |
bogdanm | 0:eff01767de02 | 81 | uuid_base_le[i] = p_uuid_base[15-i]; |
bogdanm | 0:eff01767de02 | 82 | } |
bogdanm | 0:eff01767de02 | 83 | |
bogdanm | 0:eff01767de02 | 84 | ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid) ); |
bogdanm | 0:eff01767de02 | 85 | |
bogdanm | 0:eff01767de02 | 86 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 87 | } |
bogdanm | 0:eff01767de02 | 88 | |
bogdanm | 0:eff01767de02 | 89 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 90 | /*! |
bogdanm | 0:eff01767de02 | 91 | @brief Adds a new characteristic to the custom service, assigning |
bogdanm | 0:eff01767de02 | 92 | properties, a UUID add-on value, etc. |
bogdanm | 0:eff01767de02 | 93 | |
bogdanm | 0:eff01767de02 | 94 | @param[in] service_handle |
bogdanm | 0:eff01767de02 | 95 | @param[in] p_uuid The 16-bit value to add to the base UUID |
bogdanm | 0:eff01767de02 | 96 | for this characteristic (normally >1 |
bogdanm | 0:eff01767de02 | 97 | since 1 is typically used by the primary |
bogdanm | 0:eff01767de02 | 98 | service). |
bogdanm | 0:eff01767de02 | 99 | @param[in] char_props The characteristic properties, as |
bogdanm | 0:eff01767de02 | 100 | defined by ble_gatt_char_props_t |
bogdanm | 0:eff01767de02 | 101 | @param[in] max_length The maximum length of this characeristic |
bogdanm | 0:eff01767de02 | 102 | @param[in] p_char_handle |
bogdanm | 0:eff01767de02 | 103 | |
bogdanm | 0:eff01767de02 | 104 | @returns |
bogdanm | 0:eff01767de02 | 105 | @retval ERROR_NONE Everything executed normally |
bogdanm | 0:eff01767de02 | 106 | */ |
bogdanm | 0:eff01767de02 | 107 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 108 | error_t custom_add_in_characteristic(uint16_t service_handle, ble_uuid_t* p_uuid, uint8_t properties, |
bogdanm | 0:eff01767de02 | 109 | uint8_t *p_data, uint16_t min_length, uint16_t max_length, |
bogdanm | 0:eff01767de02 | 110 | ble_gatts_char_handles_t* p_char_handle) |
bogdanm | 0:eff01767de02 | 111 | { |
bogdanm | 0:eff01767de02 | 112 | /* Characteristic metadata */ |
bogdanm | 0:eff01767de02 | 113 | ble_gatts_attr_md_t cccd_md; |
bogdanm | 0:eff01767de02 | 114 | ble_gatt_char_props_t char_props; |
bogdanm | 0:eff01767de02 | 115 | |
bogdanm | 0:eff01767de02 | 116 | memcpy(&char_props, &properties, 1); |
bogdanm | 0:eff01767de02 | 117 | |
bogdanm | 0:eff01767de02 | 118 | if ( char_props.notify || char_props.indicate ) |
bogdanm | 0:eff01767de02 | 119 | { |
bogdanm | 0:eff01767de02 | 120 | /* Notification requires cccd */ |
bogdanm | 0:eff01767de02 | 121 | memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t) ); |
bogdanm | 0:eff01767de02 | 122 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
bogdanm | 0:eff01767de02 | 123 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
bogdanm | 0:eff01767de02 | 124 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); |
bogdanm | 0:eff01767de02 | 125 | } |
bogdanm | 0:eff01767de02 | 126 | |
bogdanm | 0:eff01767de02 | 127 | ble_gatts_char_md_t char_md = { 0 }; |
bogdanm | 0:eff01767de02 | 128 | |
bogdanm | 0:eff01767de02 | 129 | char_md.char_props = char_props; |
bogdanm | 0:eff01767de02 | 130 | char_md.p_cccd_md = (char_props.notify || char_props.indicate ) ? &cccd_md : NULL; |
bogdanm | 0:eff01767de02 | 131 | |
bogdanm | 0:eff01767de02 | 132 | /* Attribute declaration */ |
bogdanm | 0:eff01767de02 | 133 | ble_gatts_attr_md_t attr_md = { 0 }; |
bogdanm | 0:eff01767de02 | 134 | |
bogdanm | 0:eff01767de02 | 135 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
bogdanm | 0:eff01767de02 | 136 | attr_md.vlen = (min_length == max_length) ? 0 : 1; |
bogdanm | 0:eff01767de02 | 137 | |
bogdanm | 0:eff01767de02 | 138 | if ( char_props.read || char_props.notify || char_props.indicate ) |
bogdanm | 0:eff01767de02 | 139 | { |
bogdanm | 0:eff01767de02 | 140 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
bogdanm | 0:eff01767de02 | 141 | } |
bogdanm | 0:eff01767de02 | 142 | |
bogdanm | 0:eff01767de02 | 143 | if ( char_props.write ) |
bogdanm | 0:eff01767de02 | 144 | { |
bogdanm | 0:eff01767de02 | 145 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
bogdanm | 0:eff01767de02 | 146 | } |
bogdanm | 0:eff01767de02 | 147 | |
bogdanm | 0:eff01767de02 | 148 | ble_gatts_attr_t attr_char_value = { 0 }; |
bogdanm | 0:eff01767de02 | 149 | |
bogdanm | 0:eff01767de02 | 150 | attr_char_value.p_uuid = p_uuid; |
bogdanm | 0:eff01767de02 | 151 | attr_char_value.p_attr_md = &attr_md; |
bogdanm | 0:eff01767de02 | 152 | attr_char_value.init_len = min_length; |
bogdanm | 0:eff01767de02 | 153 | attr_char_value.max_len = max_length; |
bogdanm | 0:eff01767de02 | 154 | attr_char_value.p_value = p_data; |
bogdanm | 0:eff01767de02 | 155 | |
bogdanm | 0:eff01767de02 | 156 | |
bogdanm | 0:eff01767de02 | 157 | ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, |
bogdanm | 0:eff01767de02 | 158 | &char_md, |
bogdanm | 0:eff01767de02 | 159 | &attr_char_value, |
bogdanm | 0:eff01767de02 | 160 | p_char_handle) ); |
bogdanm | 0:eff01767de02 | 161 | |
bogdanm | 0:eff01767de02 | 162 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 163 | } |