Nordic stack and drivers for the mbed BLE API
Fork of nRF51822 by
TARGET_MCU_NRF51822/sdk/source/ble/common/ble_srv_common.c@640:c90ae1400bf2, 2016-09-14 (annotated)
- Committer:
- Vincent Coubard
- Date:
- Wed Sep 14 14:39:43 2016 +0100
- Revision:
- 640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Vincent Coubard |
640:c90ae1400bf2 | 1 | /* |
Vincent Coubard |
640:c90ae1400bf2 | 2 | * Copyright (c) Nordic Semiconductor ASA |
Vincent Coubard |
640:c90ae1400bf2 | 3 | * All rights reserved. |
Vincent Coubard |
640:c90ae1400bf2 | 4 | * |
Vincent Coubard |
640:c90ae1400bf2 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
Vincent Coubard |
640:c90ae1400bf2 | 6 | * are permitted provided that the following conditions are met: |
Vincent Coubard |
640:c90ae1400bf2 | 7 | * |
Vincent Coubard |
640:c90ae1400bf2 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
Vincent Coubard |
640:c90ae1400bf2 | 9 | * list of conditions and the following disclaimer. |
Vincent Coubard |
640:c90ae1400bf2 | 10 | * |
Vincent Coubard |
640:c90ae1400bf2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
Vincent Coubard |
640:c90ae1400bf2 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
Vincent Coubard |
640:c90ae1400bf2 | 13 | * other materials provided with the distribution. |
Vincent Coubard |
640:c90ae1400bf2 | 14 | * |
Vincent Coubard |
640:c90ae1400bf2 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
Vincent Coubard |
640:c90ae1400bf2 | 16 | * contributors to this software may be used to endorse or promote products |
Vincent Coubard |
640:c90ae1400bf2 | 17 | * derived from this software without specific prior written permission. |
Vincent Coubard |
640:c90ae1400bf2 | 18 | * |
Vincent Coubard |
640:c90ae1400bf2 | 19 | * |
Vincent Coubard |
640:c90ae1400bf2 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
Vincent Coubard |
640:c90ae1400bf2 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
Vincent Coubard |
640:c90ae1400bf2 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Vincent Coubard |
640:c90ae1400bf2 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
Vincent Coubard |
640:c90ae1400bf2 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
Vincent Coubard |
640:c90ae1400bf2 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
Vincent Coubard |
640:c90ae1400bf2 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
Vincent Coubard |
640:c90ae1400bf2 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
Vincent Coubard |
640:c90ae1400bf2 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
Vincent Coubard |
640:c90ae1400bf2 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Vincent Coubard |
640:c90ae1400bf2 | 30 | * |
Vincent Coubard |
640:c90ae1400bf2 | 31 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 32 | |
Vincent Coubard |
640:c90ae1400bf2 | 33 | /* Attention! |
Vincent Coubard |
640:c90ae1400bf2 | 34 | * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile |
Vincent Coubard |
640:c90ae1400bf2 | 35 | * qualification listings, this section of source code must not be modified. |
Vincent Coubard |
640:c90ae1400bf2 | 36 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 37 | |
Vincent Coubard |
640:c90ae1400bf2 | 38 | #include "ble_srv_common.h" |
Vincent Coubard |
640:c90ae1400bf2 | 39 | #include <string.h> |
Vincent Coubard |
640:c90ae1400bf2 | 40 | #include "nordic_common.h" |
Vincent Coubard |
640:c90ae1400bf2 | 41 | #include "app_error.h" |
Vincent Coubard |
640:c90ae1400bf2 | 42 | #include "nrf_ble.h" |
Vincent Coubard |
640:c90ae1400bf2 | 43 | |
Vincent Coubard |
640:c90ae1400bf2 | 44 | uint8_t ble_srv_report_ref_encode(uint8_t * p_encoded_buffer, |
Vincent Coubard |
640:c90ae1400bf2 | 45 | const ble_srv_report_ref_t * p_report_ref) |
Vincent Coubard |
640:c90ae1400bf2 | 46 | { |
Vincent Coubard |
640:c90ae1400bf2 | 47 | uint8_t len = 0; |
Vincent Coubard |
640:c90ae1400bf2 | 48 | |
Vincent Coubard |
640:c90ae1400bf2 | 49 | p_encoded_buffer[len++] = p_report_ref->report_id; |
Vincent Coubard |
640:c90ae1400bf2 | 50 | p_encoded_buffer[len++] = p_report_ref->report_type; |
Vincent Coubard |
640:c90ae1400bf2 | 51 | |
Vincent Coubard |
640:c90ae1400bf2 | 52 | APP_ERROR_CHECK_BOOL(len == BLE_SRV_ENCODED_REPORT_REF_LEN); |
Vincent Coubard |
640:c90ae1400bf2 | 53 | return len; |
Vincent Coubard |
640:c90ae1400bf2 | 54 | } |
Vincent Coubard |
640:c90ae1400bf2 | 55 | |
Vincent Coubard |
640:c90ae1400bf2 | 56 | |
Vincent Coubard |
640:c90ae1400bf2 | 57 | void ble_srv_ascii_to_utf8(ble_srv_utf8_str_t * p_utf8, char * p_ascii) |
Vincent Coubard |
640:c90ae1400bf2 | 58 | { |
Vincent Coubard |
640:c90ae1400bf2 | 59 | p_utf8->length = (uint16_t)strlen(p_ascii); |
Vincent Coubard |
640:c90ae1400bf2 | 60 | p_utf8->p_str = (uint8_t *)p_ascii; |
Vincent Coubard |
640:c90ae1400bf2 | 61 | } |
Vincent Coubard |
640:c90ae1400bf2 | 62 | |
Vincent Coubard |
640:c90ae1400bf2 | 63 | |
Vincent Coubard |
640:c90ae1400bf2 | 64 | /**@brief Function for setting security requirements of a characteristic. |
Vincent Coubard |
640:c90ae1400bf2 | 65 | * |
Vincent Coubard |
640:c90ae1400bf2 | 66 | * @param[in] level required security level. |
Vincent Coubard |
640:c90ae1400bf2 | 67 | * @param[out] p_perm Characteristic security requirements. |
Vincent Coubard |
640:c90ae1400bf2 | 68 | * |
Vincent Coubard |
640:c90ae1400bf2 | 69 | * @return encoded security level and security mode. |
Vincent Coubard |
640:c90ae1400bf2 | 70 | */ |
Vincent Coubard |
640:c90ae1400bf2 | 71 | static inline void set_security_req(security_req_t level, ble_gap_conn_sec_mode_t * p_perm) |
Vincent Coubard |
640:c90ae1400bf2 | 72 | { |
Vincent Coubard |
640:c90ae1400bf2 | 73 | |
Vincent Coubard |
640:c90ae1400bf2 | 74 | |
Vincent Coubard |
640:c90ae1400bf2 | 75 | BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 76 | switch (level) |
Vincent Coubard |
640:c90ae1400bf2 | 77 | { |
Vincent Coubard |
640:c90ae1400bf2 | 78 | case SEC_NO_ACCESS: |
Vincent Coubard |
640:c90ae1400bf2 | 79 | BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 80 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 81 | case SEC_OPEN: |
Vincent Coubard |
640:c90ae1400bf2 | 82 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 83 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 84 | case SEC_JUST_WORKS: |
Vincent Coubard |
640:c90ae1400bf2 | 85 | BLE_GAP_CONN_SEC_MODE_SET_ENC_NO_MITM(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 86 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 87 | case SEC_MITM: |
Vincent Coubard |
640:c90ae1400bf2 | 88 | BLE_GAP_CONN_SEC_MODE_SET_ENC_WITH_MITM(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 89 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 90 | case SEC_SIGNED: |
Vincent Coubard |
640:c90ae1400bf2 | 91 | BLE_GAP_CONN_SEC_MODE_SET_SIGNED_NO_MITM(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 92 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 93 | case SEC_SIGNED_MITM: |
Vincent Coubard |
640:c90ae1400bf2 | 94 | BLE_GAP_CONN_SEC_MODE_SET_SIGNED_WITH_MITM(p_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 95 | break; |
Vincent Coubard |
640:c90ae1400bf2 | 96 | } |
Vincent Coubard |
640:c90ae1400bf2 | 97 | return; |
Vincent Coubard |
640:c90ae1400bf2 | 98 | } |
Vincent Coubard |
640:c90ae1400bf2 | 99 | |
Vincent Coubard |
640:c90ae1400bf2 | 100 | |
Vincent Coubard |
640:c90ae1400bf2 | 101 | uint32_t characteristic_add(uint16_t service_handle, |
Vincent Coubard |
640:c90ae1400bf2 | 102 | ble_add_char_params_t * p_char_props, |
Vincent Coubard |
640:c90ae1400bf2 | 103 | ble_gatts_char_handles_t * p_char_handle) |
Vincent Coubard |
640:c90ae1400bf2 | 104 | { |
Vincent Coubard |
640:c90ae1400bf2 | 105 | ble_gatts_char_md_t char_md; |
Vincent Coubard |
640:c90ae1400bf2 | 106 | ble_gatts_attr_t attr_char_value; |
Vincent Coubard |
640:c90ae1400bf2 | 107 | ble_uuid_t char_uuid; |
Vincent Coubard |
640:c90ae1400bf2 | 108 | ble_gatts_attr_md_t attr_md; |
Vincent Coubard |
640:c90ae1400bf2 | 109 | ble_gatts_attr_md_t user_descr_attr_md; |
Vincent Coubard |
640:c90ae1400bf2 | 110 | ble_gatts_attr_md_t cccd_md; |
Vincent Coubard |
640:c90ae1400bf2 | 111 | |
Vincent Coubard |
640:c90ae1400bf2 | 112 | if (p_char_props->uuid_type == 0) |
Vincent Coubard |
640:c90ae1400bf2 | 113 | { |
Vincent Coubard |
640:c90ae1400bf2 | 114 | char_uuid.type = BLE_UUID_TYPE_BLE; |
Vincent Coubard |
640:c90ae1400bf2 | 115 | } |
Vincent Coubard |
640:c90ae1400bf2 | 116 | else |
Vincent Coubard |
640:c90ae1400bf2 | 117 | { |
Vincent Coubard |
640:c90ae1400bf2 | 118 | char_uuid.type = p_char_props->uuid_type; |
Vincent Coubard |
640:c90ae1400bf2 | 119 | } |
Vincent Coubard |
640:c90ae1400bf2 | 120 | char_uuid.uuid = p_char_props->uuid; |
Vincent Coubard |
640:c90ae1400bf2 | 121 | |
Vincent Coubard |
640:c90ae1400bf2 | 122 | memset(&attr_md, 0, sizeof(ble_gatts_attr_md_t)); |
Vincent Coubard |
640:c90ae1400bf2 | 123 | set_security_req(p_char_props->read_access, &attr_md.read_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 124 | set_security_req(p_char_props->write_access, & attr_md.write_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 125 | attr_md.rd_auth = (p_char_props->is_defered_read ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 126 | attr_md.wr_auth = (p_char_props->is_defered_write ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 127 | attr_md.vlen = (p_char_props->is_var_len ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 128 | attr_md.vloc = (p_char_props->is_value_user ? BLE_GATTS_VLOC_USER : BLE_GATTS_VLOC_STACK); |
Vincent Coubard |
640:c90ae1400bf2 | 129 | |
Vincent Coubard |
640:c90ae1400bf2 | 130 | |
Vincent Coubard |
640:c90ae1400bf2 | 131 | memset(&char_md, 0, sizeof(ble_gatts_char_md_t)); |
Vincent Coubard |
640:c90ae1400bf2 | 132 | if ((p_char_props->char_props.notify == 1)||(p_char_props->char_props.indicate == 1)) |
Vincent Coubard |
640:c90ae1400bf2 | 133 | { |
Vincent Coubard |
640:c90ae1400bf2 | 134 | |
Vincent Coubard |
640:c90ae1400bf2 | 135 | memset(&cccd_md, 0, sizeof(cccd_md)); |
Vincent Coubard |
640:c90ae1400bf2 | 136 | set_security_req(p_char_props->cccd_write_access, &cccd_md.write_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 137 | BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 138 | |
Vincent Coubard |
640:c90ae1400bf2 | 139 | cccd_md.vloc = BLE_GATTS_VLOC_STACK; |
Vincent Coubard |
640:c90ae1400bf2 | 140 | |
Vincent Coubard |
640:c90ae1400bf2 | 141 | char_md.p_cccd_md = &cccd_md; |
Vincent Coubard |
640:c90ae1400bf2 | 142 | } |
Vincent Coubard |
640:c90ae1400bf2 | 143 | char_md.char_props = p_char_props->char_props; |
Vincent Coubard |
640:c90ae1400bf2 | 144 | |
Vincent Coubard |
640:c90ae1400bf2 | 145 | memset(&attr_char_value, 0, sizeof(ble_gatts_attr_t)); |
Vincent Coubard |
640:c90ae1400bf2 | 146 | attr_char_value.p_uuid = &char_uuid; |
Vincent Coubard |
640:c90ae1400bf2 | 147 | attr_char_value.p_attr_md = &attr_md; |
Vincent Coubard |
640:c90ae1400bf2 | 148 | attr_char_value.max_len = p_char_props->max_len; |
Vincent Coubard |
640:c90ae1400bf2 | 149 | if (p_char_props->p_init_value != NULL) |
Vincent Coubard |
640:c90ae1400bf2 | 150 | { |
Vincent Coubard |
640:c90ae1400bf2 | 151 | attr_char_value.init_len = p_char_props->init_len; |
Vincent Coubard |
640:c90ae1400bf2 | 152 | attr_char_value.p_value = p_char_props->p_init_value; |
Vincent Coubard |
640:c90ae1400bf2 | 153 | } |
Vincent Coubard |
640:c90ae1400bf2 | 154 | if (p_char_props->p_user_descr != NULL) |
Vincent Coubard |
640:c90ae1400bf2 | 155 | { |
Vincent Coubard |
640:c90ae1400bf2 | 156 | memset(&user_descr_attr_md, 0, sizeof(ble_gatts_attr_md_t)); |
Vincent Coubard |
640:c90ae1400bf2 | 157 | char_md.char_user_desc_max_size = p_char_props->p_user_descr->max_size; |
Vincent Coubard |
640:c90ae1400bf2 | 158 | char_md.char_user_desc_size = p_char_props->p_user_descr->size; |
Vincent Coubard |
640:c90ae1400bf2 | 159 | char_md.p_char_user_desc = p_char_props->p_user_descr->p_char_user_desc; |
Vincent Coubard |
640:c90ae1400bf2 | 160 | |
Vincent Coubard |
640:c90ae1400bf2 | 161 | char_md.p_user_desc_md = &user_descr_attr_md; |
Vincent Coubard |
640:c90ae1400bf2 | 162 | |
Vincent Coubard |
640:c90ae1400bf2 | 163 | set_security_req(p_char_props->p_user_descr->read_access, &user_descr_attr_md.read_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 164 | set_security_req(p_char_props->p_user_descr->write_access, &user_descr_attr_md.write_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 165 | |
Vincent Coubard |
640:c90ae1400bf2 | 166 | user_descr_attr_md.rd_auth = (p_char_props->p_user_descr->is_defered_read ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 167 | user_descr_attr_md.wr_auth = (p_char_props->p_user_descr->is_defered_write ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 168 | user_descr_attr_md.vlen = (p_char_props->p_user_descr->is_var_len ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 169 | user_descr_attr_md.vloc = (p_char_props->p_user_descr->is_value_user ? BLE_GATTS_VLOC_USER : BLE_GATTS_VLOC_STACK); |
Vincent Coubard |
640:c90ae1400bf2 | 170 | } |
Vincent Coubard |
640:c90ae1400bf2 | 171 | if (p_char_props->p_presentation_format != NULL) |
Vincent Coubard |
640:c90ae1400bf2 | 172 | { |
Vincent Coubard |
640:c90ae1400bf2 | 173 | char_md.p_char_pf = p_char_props->p_presentation_format; |
Vincent Coubard |
640:c90ae1400bf2 | 174 | } |
Vincent Coubard |
640:c90ae1400bf2 | 175 | return sd_ble_gatts_characteristic_add(service_handle, |
Vincent Coubard |
640:c90ae1400bf2 | 176 | &char_md, |
Vincent Coubard |
640:c90ae1400bf2 | 177 | &attr_char_value, |
Vincent Coubard |
640:c90ae1400bf2 | 178 | p_char_handle); |
Vincent Coubard |
640:c90ae1400bf2 | 179 | } |
Vincent Coubard |
640:c90ae1400bf2 | 180 | |
Vincent Coubard |
640:c90ae1400bf2 | 181 | |
Vincent Coubard |
640:c90ae1400bf2 | 182 | uint32_t descriptor_add(uint16_t char_handle, |
Vincent Coubard |
640:c90ae1400bf2 | 183 | ble_add_descr_params_t * p_descr_props, |
Vincent Coubard |
640:c90ae1400bf2 | 184 | uint16_t * p_descr_handle) |
Vincent Coubard |
640:c90ae1400bf2 | 185 | { |
Vincent Coubard |
640:c90ae1400bf2 | 186 | ble_gatts_attr_t descr_params; |
Vincent Coubard |
640:c90ae1400bf2 | 187 | ble_uuid_t desc_uuid; |
Vincent Coubard |
640:c90ae1400bf2 | 188 | ble_gatts_attr_md_t attr_md; |
Vincent Coubard |
640:c90ae1400bf2 | 189 | |
Vincent Coubard |
640:c90ae1400bf2 | 190 | memset(&descr_params, 0, sizeof(descr_params)); |
Vincent Coubard |
640:c90ae1400bf2 | 191 | if (p_descr_props->uuid_type == 0) |
Vincent Coubard |
640:c90ae1400bf2 | 192 | { |
Vincent Coubard |
640:c90ae1400bf2 | 193 | desc_uuid.type = BLE_UUID_TYPE_BLE; |
Vincent Coubard |
640:c90ae1400bf2 | 194 | } |
Vincent Coubard |
640:c90ae1400bf2 | 195 | else |
Vincent Coubard |
640:c90ae1400bf2 | 196 | { |
Vincent Coubard |
640:c90ae1400bf2 | 197 | desc_uuid.type = p_descr_props->uuid_type; |
Vincent Coubard |
640:c90ae1400bf2 | 198 | } |
Vincent Coubard |
640:c90ae1400bf2 | 199 | desc_uuid.uuid = p_descr_props->uuid; |
Vincent Coubard |
640:c90ae1400bf2 | 200 | descr_params.p_uuid = &desc_uuid; |
Vincent Coubard |
640:c90ae1400bf2 | 201 | |
Vincent Coubard |
640:c90ae1400bf2 | 202 | set_security_req(p_descr_props->read_access, &attr_md.read_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 203 | set_security_req(p_descr_props->write_access,&attr_md.write_perm); |
Vincent Coubard |
640:c90ae1400bf2 | 204 | |
Vincent Coubard |
640:c90ae1400bf2 | 205 | attr_md.rd_auth = (p_descr_props->is_defered_read ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 206 | attr_md.wr_auth = (p_descr_props->is_defered_write ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 207 | attr_md.vlen = (p_descr_props->is_var_len ? 1 : 0); |
Vincent Coubard |
640:c90ae1400bf2 | 208 | attr_md.vloc = (p_descr_props->is_value_user ? BLE_GATTS_VLOC_USER : BLE_GATTS_VLOC_STACK); |
Vincent Coubard |
640:c90ae1400bf2 | 209 | descr_params.p_attr_md = &attr_md; |
Vincent Coubard |
640:c90ae1400bf2 | 210 | |
Vincent Coubard |
640:c90ae1400bf2 | 211 | descr_params.init_len = p_descr_props->init_len; |
Vincent Coubard |
640:c90ae1400bf2 | 212 | descr_params.init_offs = p_descr_props->init_offs; |
Vincent Coubard |
640:c90ae1400bf2 | 213 | descr_params.max_len = p_descr_props->max_len; |
Vincent Coubard |
640:c90ae1400bf2 | 214 | descr_params.p_value = p_descr_props->p_value; |
Vincent Coubard |
640:c90ae1400bf2 | 215 | |
Vincent Coubard |
640:c90ae1400bf2 | 216 | return sd_ble_gatts_descriptor_add(char_handle, &descr_params, p_descr_handle); |
Vincent Coubard |
640:c90ae1400bf2 | 217 | } |