cvb

Fork of nrf51-sdk by Lancaster University

Committer:
tb942
Date:
Tue Aug 14 18:27:20 2018 +0000
Revision:
9:d3dd910b0f4b
Parent:
0:bc2961fa1ef0

        

Who changed what in which revision?

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