SG RFID nRF51822 fork

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Mar 20 10:31:37 2015 +0000
Revision:
99:bc524269c7c1
Parent:
98:f96993b65e90
Synchronized with git rev 2c5d9aec
Author: Rohit Grover
ble_conn_params: transfer use of app_timer to mbed::Ticker

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:eff01767de02 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
bogdanm 0:eff01767de02 2 *
bogdanm 0:eff01767de02 3 * The information contained herein is property of Nordic Semiconductor ASA.
bogdanm 0:eff01767de02 4 * Terms and conditions of usage are described in detail in NORDIC
bogdanm 0:eff01767de02 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
bogdanm 0:eff01767de02 6 *
bogdanm 0:eff01767de02 7 * Licensees are granted free, non-transferable use of the information. NO
bogdanm 0:eff01767de02 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
bogdanm 0:eff01767de02 9 * the file.
bogdanm 0:eff01767de02 10 *
bogdanm 0:eff01767de02 11 */
bogdanm 0:eff01767de02 12
bogdanm 0:eff01767de02 13 #include "ble_conn_params.h"
bogdanm 0:eff01767de02 14 #include <stdlib.h>
bogdanm 0:eff01767de02 15 #include "nordic_common.h"
bogdanm 0:eff01767de02 16 #include "ble_hci.h"
bogdanm 0:eff01767de02 17 #include "ble_srv_common.h"
bogdanm 0:eff01767de02 18 #include "app_util.h"
rgrover1 99:bc524269c7c1 19 #include "mbed.h"
bogdanm 0:eff01767de02 20
bogdanm 0:eff01767de02 21 static ble_conn_params_init_t m_conn_params_config; /**< Configuration as specified by the application. */
bogdanm 0:eff01767de02 22 static ble_gap_conn_params_t m_preferred_conn_params; /**< Connection parameters preferred by the application. */
bogdanm 0:eff01767de02 23 static uint8_t m_update_count; /**< Number of Connection Parameter Update messages that has currently been sent. */
bogdanm 0:eff01767de02 24 static uint16_t m_conn_handle; /**< Current connection handle. */
bogdanm 0:eff01767de02 25 static ble_gap_conn_params_t m_current_conn_params; /**< Connection parameters received in the most recent Connect event. */
rgrover1 99:bc524269c7c1 26 static Ticker m_conn_params_timer;
bogdanm 0:eff01767de02 27
bogdanm 0:eff01767de02 28 static bool m_change_param = false;
bogdanm 0:eff01767de02 29
bogdanm 0:eff01767de02 30 static bool is_conn_params_ok(ble_gap_conn_params_t * p_conn_params)
bogdanm 0:eff01767de02 31 {
bogdanm 0:eff01767de02 32 // Check if interval is within the acceptable range.
bogdanm 0:eff01767de02 33 // NOTE: Using max_conn_interval in the received event data because this contains
bogdanm 0:eff01767de02 34 // the client's connection interval.
Rohit Grover 42:120a96d10f90 35 if ((p_conn_params->max_conn_interval >= m_preferred_conn_params.min_conn_interval) &&
Rohit Grover 42:120a96d10f90 36 (p_conn_params->max_conn_interval <= m_preferred_conn_params.max_conn_interval)) {
bogdanm 0:eff01767de02 37 return true;
Rohit Grover 42:120a96d10f90 38 } else {
bogdanm 0:eff01767de02 39 return false;
bogdanm 0:eff01767de02 40 }
bogdanm 0:eff01767de02 41 }
bogdanm 0:eff01767de02 42
bogdanm 0:eff01767de02 43
rgrover1 99:bc524269c7c1 44 static void update_timeout_handler(void)
bogdanm 0:eff01767de02 45 {
rgrover1 99:bc524269c7c1 46 m_conn_params_timer.detach(); /* this is supposed to be a single-shot timer callback */
bogdanm 0:eff01767de02 47 if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
bogdanm 0:eff01767de02 48 {
bogdanm 0:eff01767de02 49 // Check if we have reached the maximum number of attempts
bogdanm 0:eff01767de02 50 m_update_count++;
bogdanm 0:eff01767de02 51 if (m_update_count <= m_conn_params_config.max_conn_params_update_count)
bogdanm 0:eff01767de02 52 {
bogdanm 0:eff01767de02 53 uint32_t err_code;
Rohit Grover 42:120a96d10f90 54
bogdanm 0:eff01767de02 55 // Parameters are not ok, send connection parameters update request.
bogdanm 0:eff01767de02 56 err_code = sd_ble_gap_conn_param_update(m_conn_handle, &m_preferred_conn_params);
bogdanm 0:eff01767de02 57 if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL))
bogdanm 0:eff01767de02 58 {
bogdanm 0:eff01767de02 59 m_conn_params_config.error_handler(err_code);
bogdanm 0:eff01767de02 60 }
bogdanm 0:eff01767de02 61 }
bogdanm 0:eff01767de02 62 else
bogdanm 0:eff01767de02 63 {
bogdanm 0:eff01767de02 64 m_update_count = 0;
Rohit Grover 42:120a96d10f90 65
bogdanm 0:eff01767de02 66 // Negotiation failed, disconnect automatically if this has been configured
bogdanm 0:eff01767de02 67 if (m_conn_params_config.disconnect_on_fail)
bogdanm 0:eff01767de02 68 {
bogdanm 0:eff01767de02 69 uint32_t err_code;
Rohit Grover 42:120a96d10f90 70
bogdanm 0:eff01767de02 71 err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
bogdanm 0:eff01767de02 72 if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL))
bogdanm 0:eff01767de02 73 {
bogdanm 0:eff01767de02 74 m_conn_params_config.error_handler(err_code);
bogdanm 0:eff01767de02 75 }
bogdanm 0:eff01767de02 76 }
Rohit Grover 42:120a96d10f90 77
bogdanm 0:eff01767de02 78 // Notify the application that the procedure has failed
bogdanm 0:eff01767de02 79 if (m_conn_params_config.evt_handler != NULL)
bogdanm 0:eff01767de02 80 {
bogdanm 0:eff01767de02 81 ble_conn_params_evt_t evt;
Rohit Grover 42:120a96d10f90 82
bogdanm 0:eff01767de02 83 evt.evt_type = BLE_CONN_PARAMS_EVT_FAILED;
bogdanm 0:eff01767de02 84 m_conn_params_config.evt_handler(&evt);
bogdanm 0:eff01767de02 85 }
bogdanm 0:eff01767de02 86 }
bogdanm 0:eff01767de02 87 }
bogdanm 0:eff01767de02 88 }
bogdanm 0:eff01767de02 89
bogdanm 0:eff01767de02 90
bogdanm 0:eff01767de02 91 uint32_t ble_conn_params_init(const ble_conn_params_init_t * p_init)
bogdanm 0:eff01767de02 92 {
bogdanm 0:eff01767de02 93 uint32_t err_code;
Rohit Grover 42:120a96d10f90 94
bogdanm 0:eff01767de02 95 m_conn_params_config = *p_init;
bogdanm 0:eff01767de02 96 m_change_param = false;
bogdanm 0:eff01767de02 97 if (p_init->p_conn_params != NULL)
bogdanm 0:eff01767de02 98 {
bogdanm 0:eff01767de02 99 m_preferred_conn_params = *p_init->p_conn_params;
Rohit Grover 42:120a96d10f90 100
bogdanm 0:eff01767de02 101 // Set the connection params in stack
bogdanm 0:eff01767de02 102 err_code = sd_ble_gap_ppcp_set(&m_preferred_conn_params);
bogdanm 0:eff01767de02 103 if (err_code != NRF_SUCCESS)
bogdanm 0:eff01767de02 104 {
bogdanm 0:eff01767de02 105 return err_code;
bogdanm 0:eff01767de02 106 }
bogdanm 0:eff01767de02 107 }
bogdanm 0:eff01767de02 108 else
bogdanm 0:eff01767de02 109 {
bogdanm 0:eff01767de02 110 // Fetch the connection params from stack
bogdanm 0:eff01767de02 111 err_code = sd_ble_gap_ppcp_get(&m_preferred_conn_params);
bogdanm 0:eff01767de02 112 if (err_code != NRF_SUCCESS)
bogdanm 0:eff01767de02 113 {
bogdanm 0:eff01767de02 114 return err_code;
bogdanm 0:eff01767de02 115 }
bogdanm 0:eff01767de02 116 }
bogdanm 0:eff01767de02 117
bogdanm 0:eff01767de02 118 m_conn_handle = BLE_CONN_HANDLE_INVALID;
bogdanm 0:eff01767de02 119 m_update_count = 0;
Rohit Grover 42:120a96d10f90 120
rgrover1 99:bc524269c7c1 121 return NRF_SUCCESS;
bogdanm 0:eff01767de02 122 }
bogdanm 0:eff01767de02 123
bogdanm 0:eff01767de02 124
bogdanm 0:eff01767de02 125 uint32_t ble_conn_params_stop(void)
bogdanm 0:eff01767de02 126 {
rgrover1 99:bc524269c7c1 127 m_conn_params_timer.detach();
rgrover1 99:bc524269c7c1 128 return NRF_SUCCESS;
bogdanm 0:eff01767de02 129 }
bogdanm 0:eff01767de02 130
bogdanm 0:eff01767de02 131
bogdanm 0:eff01767de02 132 static void conn_params_negotiation(void)
bogdanm 0:eff01767de02 133 {
bogdanm 0:eff01767de02 134 // Start negotiation if the received connection parameters are not acceptable
bogdanm 0:eff01767de02 135 if (!is_conn_params_ok(&m_current_conn_params))
bogdanm 0:eff01767de02 136 {
bogdanm 0:eff01767de02 137 uint32_t timeout_ticks;
Rohit Grover 42:120a96d10f90 138
bogdanm 0:eff01767de02 139 if (m_change_param)
bogdanm 0:eff01767de02 140 {
bogdanm 0:eff01767de02 141 // Notify the application that the procedure has failed
bogdanm 0:eff01767de02 142 if (m_conn_params_config.evt_handler != NULL)
bogdanm 0:eff01767de02 143 {
bogdanm 0:eff01767de02 144 ble_conn_params_evt_t evt;
Rohit Grover 42:120a96d10f90 145
bogdanm 0:eff01767de02 146 evt.evt_type = BLE_CONN_PARAMS_EVT_FAILED;
bogdanm 0:eff01767de02 147 m_conn_params_config.evt_handler(&evt);
bogdanm 0:eff01767de02 148 }
bogdanm 0:eff01767de02 149 }
bogdanm 0:eff01767de02 150 else
bogdanm 0:eff01767de02 151 {
bogdanm 0:eff01767de02 152 if (m_update_count == 0)
bogdanm 0:eff01767de02 153 {
bogdanm 0:eff01767de02 154 // First connection parameter update
bogdanm 0:eff01767de02 155 timeout_ticks = m_conn_params_config.first_conn_params_update_delay;
bogdanm 0:eff01767de02 156 }
bogdanm 0:eff01767de02 157 else
bogdanm 0:eff01767de02 158 {
bogdanm 0:eff01767de02 159 timeout_ticks = m_conn_params_config.next_conn_params_update_delay;
bogdanm 0:eff01767de02 160 }
bogdanm 0:eff01767de02 161
rgrover1 99:bc524269c7c1 162 m_conn_params_timer.attach(update_timeout_handler, timeout_ticks / 32768);
bogdanm 0:eff01767de02 163 }
bogdanm 0:eff01767de02 164 }
bogdanm 0:eff01767de02 165 else
bogdanm 0:eff01767de02 166 {
bogdanm 0:eff01767de02 167 // Notify the application that the procedure has succeded
bogdanm 0:eff01767de02 168 if (m_conn_params_config.evt_handler != NULL)
bogdanm 0:eff01767de02 169 {
bogdanm 0:eff01767de02 170 ble_conn_params_evt_t evt;
Rohit Grover 42:120a96d10f90 171
bogdanm 0:eff01767de02 172 evt.evt_type = BLE_CONN_PARAMS_EVT_SUCCEEDED;
bogdanm 0:eff01767de02 173 m_conn_params_config.evt_handler(&evt);
bogdanm 0:eff01767de02 174 }
bogdanm 0:eff01767de02 175 }
bogdanm 0:eff01767de02 176 m_change_param = false;
bogdanm 0:eff01767de02 177 }
bogdanm 0:eff01767de02 178
bogdanm 0:eff01767de02 179
bogdanm 0:eff01767de02 180 static void on_connect(ble_evt_t * p_ble_evt)
bogdanm 0:eff01767de02 181 {
bogdanm 0:eff01767de02 182 // Save connection parameters
bogdanm 0:eff01767de02 183 m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
bogdanm 0:eff01767de02 184 m_current_conn_params = p_ble_evt->evt.gap_evt.params.connected.conn_params;
bogdanm 0:eff01767de02 185 m_update_count = 0; // Connection parameter negotiation should re-start every connection
Rohit Grover 42:120a96d10f90 186
bogdanm 0:eff01767de02 187 // Check if we shall handle negotiation on connect
bogdanm 0:eff01767de02 188 if (m_conn_params_config.start_on_notify_cccd_handle == BLE_GATT_HANDLE_INVALID)
bogdanm 0:eff01767de02 189 {
bogdanm 0:eff01767de02 190 conn_params_negotiation();
bogdanm 0:eff01767de02 191 }
bogdanm 0:eff01767de02 192 }
bogdanm 0:eff01767de02 193
bogdanm 0:eff01767de02 194
bogdanm 0:eff01767de02 195 static void on_disconnect(ble_evt_t * p_ble_evt)
bogdanm 0:eff01767de02 196 {
bogdanm 0:eff01767de02 197 m_conn_handle = BLE_CONN_HANDLE_INVALID;
bogdanm 0:eff01767de02 198
bogdanm 0:eff01767de02 199 // Stop timer if running
bogdanm 0:eff01767de02 200 m_update_count = 0; // Connection parameters updates should happen during every connection
Rohit Grover 42:120a96d10f90 201
rgrover1 99:bc524269c7c1 202 m_conn_params_timer.detach();
bogdanm 0:eff01767de02 203 }
bogdanm 0:eff01767de02 204
bogdanm 0:eff01767de02 205
bogdanm 0:eff01767de02 206 static void on_write(ble_evt_t * p_ble_evt)
bogdanm 0:eff01767de02 207 {
bogdanm 0:eff01767de02 208 ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
bogdanm 0:eff01767de02 209
bogdanm 0:eff01767de02 210 // Check if this the correct CCCD
bogdanm 0:eff01767de02 211 if (
bogdanm 0:eff01767de02 212 (p_evt_write->handle == m_conn_params_config.start_on_notify_cccd_handle)
bogdanm 0:eff01767de02 213 &&
bogdanm 0:eff01767de02 214 (p_evt_write->len == 2)
Rohit Grover 65:98215c4f3a25 215 )
bogdanm 0:eff01767de02 216 {
bogdanm 0:eff01767de02 217 // Check if this is a 'start notification'
bogdanm 0:eff01767de02 218 if (ble_srv_is_notification_enabled(p_evt_write->data))
bogdanm 0:eff01767de02 219 {
bogdanm 0:eff01767de02 220 // Do connection parameter negotiation if necessary
bogdanm 0:eff01767de02 221 conn_params_negotiation();
bogdanm 0:eff01767de02 222 }
bogdanm 0:eff01767de02 223 else
bogdanm 0:eff01767de02 224 {
bogdanm 0:eff01767de02 225 // Stop timer if running
rgrover1 99:bc524269c7c1 226 m_conn_params_timer.detach();
bogdanm 0:eff01767de02 227 }
bogdanm 0:eff01767de02 228 }
bogdanm 0:eff01767de02 229 }
bogdanm 0:eff01767de02 230
bogdanm 0:eff01767de02 231
bogdanm 0:eff01767de02 232 static void on_conn_params_update(ble_evt_t * p_ble_evt)
bogdanm 0:eff01767de02 233 {
bogdanm 0:eff01767de02 234 // Copy the parameters
bogdanm 0:eff01767de02 235 m_current_conn_params = p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params;
bogdanm 0:eff01767de02 236
bogdanm 0:eff01767de02 237 conn_params_negotiation();
bogdanm 0:eff01767de02 238 }
bogdanm 0:eff01767de02 239
bogdanm 0:eff01767de02 240
bogdanm 0:eff01767de02 241 void ble_conn_params_on_ble_evt(ble_evt_t * p_ble_evt)
bogdanm 0:eff01767de02 242 {
bogdanm 0:eff01767de02 243 switch (p_ble_evt->header.evt_id)
bogdanm 0:eff01767de02 244 {
bogdanm 0:eff01767de02 245 case BLE_GAP_EVT_CONNECTED:
bogdanm 0:eff01767de02 246 on_connect(p_ble_evt);
bogdanm 0:eff01767de02 247 break;
Rohit Grover 42:120a96d10f90 248
bogdanm 0:eff01767de02 249 case BLE_GAP_EVT_DISCONNECTED:
bogdanm 0:eff01767de02 250 on_disconnect(p_ble_evt);
bogdanm 0:eff01767de02 251 break;
Rohit Grover 42:120a96d10f90 252
bogdanm 0:eff01767de02 253 case BLE_GATTS_EVT_WRITE:
bogdanm 0:eff01767de02 254 on_write(p_ble_evt);
bogdanm 0:eff01767de02 255 break;
Rohit Grover 42:120a96d10f90 256
bogdanm 0:eff01767de02 257 case BLE_GAP_EVT_CONN_PARAM_UPDATE:
bogdanm 0:eff01767de02 258 on_conn_params_update(p_ble_evt);
bogdanm 0:eff01767de02 259 break;
Rohit Grover 42:120a96d10f90 260
bogdanm 0:eff01767de02 261 default:
bogdanm 0:eff01767de02 262 // No implementation needed.
Rohit Grover 65:98215c4f3a25 263 break;
bogdanm 0:eff01767de02 264 }
bogdanm 0:eff01767de02 265 }
bogdanm 0:eff01767de02 266
bogdanm 0:eff01767de02 267 uint32_t ble_conn_params_change_conn_params(ble_gap_conn_params_t *new_params)
bogdanm 0:eff01767de02 268 {
bogdanm 0:eff01767de02 269 uint32_t err_code;
bogdanm 0:eff01767de02 270
bogdanm 0:eff01767de02 271 m_preferred_conn_params = *new_params;
bogdanm 0:eff01767de02 272 // Set the connection params in stack
bogdanm 0:eff01767de02 273 err_code = sd_ble_gap_ppcp_set(&m_preferred_conn_params);
bogdanm 0:eff01767de02 274 if (err_code == NRF_SUCCESS)
bogdanm 0:eff01767de02 275 {
bogdanm 0:eff01767de02 276 if (!is_conn_params_ok(&m_current_conn_params))
bogdanm 0:eff01767de02 277 {
bogdanm 0:eff01767de02 278 m_change_param = true;
bogdanm 0:eff01767de02 279 err_code = sd_ble_gap_conn_param_update(m_conn_handle, &m_preferred_conn_params);
bogdanm 0:eff01767de02 280 m_update_count = 1;
bogdanm 0:eff01767de02 281 }
bogdanm 0:eff01767de02 282 else
bogdanm 0:eff01767de02 283 {
bogdanm 0:eff01767de02 284 // Notify the application that the procedure has succeded
bogdanm 0:eff01767de02 285 if (m_conn_params_config.evt_handler != NULL)
bogdanm 0:eff01767de02 286 {
bogdanm 0:eff01767de02 287 ble_conn_params_evt_t evt;
Rohit Grover 42:120a96d10f90 288
bogdanm 0:eff01767de02 289 evt.evt_type = BLE_CONN_PARAMS_EVT_SUCCEEDED;
bogdanm 0:eff01767de02 290 m_conn_params_config.evt_handler(&evt);
bogdanm 0:eff01767de02 291 }
bogdanm 0:eff01767de02 292 err_code = NRF_SUCCESS;
bogdanm 0:eff01767de02 293 }
bogdanm 0:eff01767de02 294 }
bogdanm 0:eff01767de02 295 return err_code;
rgrover1 99:bc524269c7c1 296 }