SG RFID nRF51822 fork

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Fri Mar 13 16:56:45 2015 +0000
Revision:
98:f96993b65e90
Parent:
65:98215c4f3a25
Child:
99:bc524269c7c1
Synchronized with git rev 1d28ee86
Author: Rohit Grover
disable the connParameters module because it uses app_timer.

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