changed low freq. clock source to IRC

Dependents:   BLE_ANCS_SDAPI_IRC

Fork of nRF51822 by Nordic Semiconductor

Committer:
ytsuboi
Date:
Sun Jul 06 13:08:48 2014 +0000
Revision:
37:8c9bf3bea9db
Parent:
0:eff01767de02
changed low freq. clock to IRC

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