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
btle/custom/custom_helper.cpp@6:bbb4357dc135, 2014-05-28 (annotated)
- Committer:
- Rohit Grover
- Date:
- Wed May 28 12:55:18 2014 +0100
- Revision:
- 6:bbb4357dc135
- Parent:
- 0:eff01767de02
- Child:
- 8:2214f1df6a6a
running uncrustify against a few sources
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. |
Rohit Grover |
6:bbb4357dc135 | 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) |
Rohit Grover |
6:bbb4357dc135 | 29 | |
bogdanm | 0:eff01767de02 | 30 | @returns The UUID type. |
bogdanm | 0:eff01767de02 | 31 | A return value of 0 should be considered an error. |
Rohit Grover |
6:bbb4357dc135 | 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 |
Rohit Grover |
6:bbb4357dc135 | 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" |
Rohit Grover |
6:bbb4357dc135 | 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); |
Rohit Grover |
6:bbb4357dc135 | 46 | |
bogdanm | 0:eff01767de02 | 47 | // We can now safely add the primary service and any characteristics |
bogdanm | 0:eff01767de02 | 48 | // for our custom service ... |
Rohit Grover |
6:bbb4357dc135 | 49 | |
bogdanm | 0:eff01767de02 | 50 | @endcode |
bogdanm | 0:eff01767de02 | 51 | */ |
bogdanm | 0:eff01767de02 | 52 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 53 | uint8_t custom_add_uuid_base(uint8_t const *const p_uuid_base) |
bogdanm | 0:eff01767de02 | 54 | { |
Rohit Grover |
6:bbb4357dc135 | 55 | ble_uuid128_t base_uuid; |
Rohit Grover |
6:bbb4357dc135 | 56 | uint8_t uuid_type = 0; |
bogdanm | 0:eff01767de02 | 57 | |
Rohit Grover |
6:bbb4357dc135 | 58 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
Rohit Grover |
6:bbb4357dc135 | 59 | for (uint8_t i = 0; i<16; i++) { |
Rohit Grover |
6:bbb4357dc135 | 60 | base_uuid.uuid128[i] = p_uuid_base[15 - i]; |
Rohit Grover |
6:bbb4357dc135 | 61 | } |
bogdanm | 0:eff01767de02 | 62 | |
Rohit Grover |
6:bbb4357dc135 | 63 | ASSERT_INT( ERROR_NONE, sd_ble_uuid_vs_add( &base_uuid, &uuid_type ), 0); |
bogdanm | 0:eff01767de02 | 64 | |
Rohit Grover |
6:bbb4357dc135 | 65 | return uuid_type; |
bogdanm | 0:eff01767de02 | 66 | } |
bogdanm | 0:eff01767de02 | 67 | |
bogdanm | 0:eff01767de02 | 68 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 69 | /*! |
bogdanm | 0:eff01767de02 | 70 | |
bogdanm | 0:eff01767de02 | 71 | */ |
bogdanm | 0:eff01767de02 | 72 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 73 | error_t custom_decode_uuid_base(uint8_t const *const p_uuid_base, |
Rohit Grover |
6:bbb4357dc135 | 74 | ble_uuid_t *p_uuid) |
bogdanm | 0:eff01767de02 | 75 | { |
Rohit Grover |
6:bbb4357dc135 | 76 | uint8_t uuid_base_le[16]; |
bogdanm | 0:eff01767de02 | 77 | |
Rohit Grover |
6:bbb4357dc135 | 78 | /* Reverse the bytes since ble_uuid128_t is LSB */ |
Rohit Grover |
6:bbb4357dc135 | 79 | for (uint8_t i = 0; i<16; i++) { |
Rohit Grover |
6:bbb4357dc135 | 80 | uuid_base_le[i] = p_uuid_base[15 - i]; |
Rohit Grover |
6:bbb4357dc135 | 81 | } |
bogdanm | 0:eff01767de02 | 82 | |
Rohit Grover |
6:bbb4357dc135 | 83 | ASSERT_STATUS( sd_ble_uuid_decode(16, uuid_base_le, p_uuid)); |
bogdanm | 0:eff01767de02 | 84 | |
Rohit Grover |
6:bbb4357dc135 | 85 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 86 | } |
bogdanm | 0:eff01767de02 | 87 | |
bogdanm | 0:eff01767de02 | 88 | /**************************************************************************/ |
bogdanm | 0:eff01767de02 | 89 | /*! |
bogdanm | 0:eff01767de02 | 90 | @brief Adds a new characteristic to the custom service, assigning |
bogdanm | 0:eff01767de02 | 91 | properties, a UUID add-on value, etc. |
bogdanm | 0:eff01767de02 | 92 | |
bogdanm | 0:eff01767de02 | 93 | @param[in] service_handle |
bogdanm | 0:eff01767de02 | 94 | @param[in] p_uuid The 16-bit value to add to the base UUID |
bogdanm | 0:eff01767de02 | 95 | for this characteristic (normally >1 |
bogdanm | 0:eff01767de02 | 96 | since 1 is typically used by the primary |
bogdanm | 0:eff01767de02 | 97 | service). |
bogdanm | 0:eff01767de02 | 98 | @param[in] char_props The characteristic properties, as |
bogdanm | 0:eff01767de02 | 99 | defined by ble_gatt_char_props_t |
bogdanm | 0:eff01767de02 | 100 | @param[in] max_length The maximum length of this characeristic |
bogdanm | 0:eff01767de02 | 101 | @param[in] p_char_handle |
Rohit Grover |
6:bbb4357dc135 | 102 | |
bogdanm | 0:eff01767de02 | 103 | @returns |
bogdanm | 0:eff01767de02 | 104 | @retval ERROR_NONE Everything executed normally |
bogdanm | 0:eff01767de02 | 105 | */ |
bogdanm | 0:eff01767de02 | 106 | /**************************************************************************/ |
Rohit Grover |
6:bbb4357dc135 | 107 | error_t custom_add_in_characteristic(uint16_t service_handle, |
Rohit Grover |
6:bbb4357dc135 | 108 | ble_uuid_t *p_uuid, |
Rohit Grover |
6:bbb4357dc135 | 109 | uint8_t properties, |
Rohit Grover |
6:bbb4357dc135 | 110 | uint8_t *p_data, |
Rohit Grover |
6:bbb4357dc135 | 111 | uint16_t min_length, |
Rohit Grover |
6:bbb4357dc135 | 112 | uint16_t max_length, |
Rohit Grover |
6:bbb4357dc135 | 113 | ble_gatts_char_handles_t *p_char_handle) |
bogdanm | 0:eff01767de02 | 114 | { |
Rohit Grover |
6:bbb4357dc135 | 115 | /* Characteristic metadata */ |
Rohit Grover |
6:bbb4357dc135 | 116 | ble_gatts_attr_md_t cccd_md; |
Rohit Grover |
6:bbb4357dc135 | 117 | ble_gatt_char_props_t char_props; |
bogdanm | 0:eff01767de02 | 118 | |
Rohit Grover |
6:bbb4357dc135 | 119 | memcpy(&char_props, &properties, 1); |
bogdanm | 0:eff01767de02 | 120 | |
Rohit Grover |
6:bbb4357dc135 | 121 | if (char_props.notify || char_props.indicate) { |
Rohit Grover |
6:bbb4357dc135 | 122 | /* Notification requires cccd */ |
Rohit Grover |
6:bbb4357dc135 | 123 | memclr_( &cccd_md, sizeof(ble_gatts_attr_md_t)); |
Rohit Grover |
6:bbb4357dc135 | 124 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
Rohit Grover |
6:bbb4357dc135 | 125 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
Rohit Grover |
6:bbb4357dc135 | 126 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); |
Rohit Grover |
6:bbb4357dc135 | 127 | } |
bogdanm | 0:eff01767de02 | 128 | |
Rohit Grover |
6:bbb4357dc135 | 129 | ble_gatts_char_md_t char_md = {0}; |
bogdanm | 0:eff01767de02 | 130 | |
Rohit Grover |
6:bbb4357dc135 | 131 | char_md.char_props = char_props; |
Rohit Grover |
6:bbb4357dc135 | 132 | char_md.p_cccd_md = |
Rohit Grover |
6:bbb4357dc135 | 133 | (char_props.notify || char_props.indicate) ? &cccd_md : NULL; |
bogdanm | 0:eff01767de02 | 134 | |
Rohit Grover |
6:bbb4357dc135 | 135 | /* Attribute declaration */ |
Rohit Grover |
6:bbb4357dc135 | 136 | ble_gatts_attr_md_t attr_md = {0}; |
bogdanm | 0:eff01767de02 | 137 | |
Rohit Grover |
6:bbb4357dc135 | 138 | attr_md.vloc = BLE_GATTS_VLOC_STACK; |
Rohit Grover |
6:bbb4357dc135 | 139 | attr_md.vlen = (min_length == max_length) ? 0 : 1; |
bogdanm | 0:eff01767de02 | 140 | |
Rohit Grover |
6:bbb4357dc135 | 141 | if (char_props.read || char_props.notify || char_props.indicate) { |
Rohit Grover |
6:bbb4357dc135 | 142 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); |
Rohit Grover |
6:bbb4357dc135 | 143 | } |
Rohit Grover |
6:bbb4357dc135 | 144 | |
Rohit Grover |
6:bbb4357dc135 | 145 | if (char_props.write) { |
Rohit Grover |
6:bbb4357dc135 | 146 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); |
Rohit Grover |
6:bbb4357dc135 | 147 | } |
bogdanm | 0:eff01767de02 | 148 | |
Rohit Grover |
6:bbb4357dc135 | 149 | ble_gatts_attr_t attr_char_value = {0}; |
bogdanm | 0:eff01767de02 | 150 | |
Rohit Grover |
6:bbb4357dc135 | 151 | attr_char_value.p_uuid = p_uuid; |
Rohit Grover |
6:bbb4357dc135 | 152 | attr_char_value.p_attr_md = &attr_md; |
Rohit Grover |
6:bbb4357dc135 | 153 | attr_char_value.init_len = min_length; |
Rohit Grover |
6:bbb4357dc135 | 154 | attr_char_value.max_len = max_length; |
Rohit Grover |
6:bbb4357dc135 | 155 | attr_char_value.p_value = p_data; |
bogdanm | 0:eff01767de02 | 156 | |
bogdanm | 0:eff01767de02 | 157 | |
Rohit Grover |
6:bbb4357dc135 | 158 | ASSERT_STATUS ( sd_ble_gatts_characteristic_add(service_handle, |
Rohit Grover |
6:bbb4357dc135 | 159 | &char_md, |
Rohit Grover |
6:bbb4357dc135 | 160 | &attr_char_value, |
Rohit Grover |
6:bbb4357dc135 | 161 | p_char_handle)); |
bogdanm | 0:eff01767de02 | 162 | |
Rohit Grover |
6:bbb4357dc135 | 163 | return ERROR_NONE; |
bogdanm | 0:eff01767de02 | 164 | } |