テスト用

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Fri Sep 26 14:46:19 2014 +0100
Revision:
69:936d81c963fe
Parent:
65:98215c4f3a25
Release 0.2.1
=============

Features
~~~~~~~~

- Turn on the service-changed characteristic by default.

Bugfixes
~~~~~~~~

- Raised the maximum number of supported characteristics/descriptors to 24
(was 10 previously). We're beginning to find services like Device-
Information with several characteristics populated.

Compatibility
~~~~~~~~~~~~~

Works with 0.2.0 of BLE_API.

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