AndroidのBLEラジコンプロポアプリ「BLEPropo」と接続し、RCサーボとDCモータを制御するプログラムです。 mbed HRM1017で動作を確認しています。 BLEPropo → https://github.com/lipoyang/BLEPropo
Fork of BLE_RCBController2 by
BLEを使ったAndroid用ラジコンプロポアプリ「BLEPropo」に対応するmbed HRM1017用ファームウェアです。
BLEPropoは、GitHubにて公開中。
https://github.com/lipoyang/BLEPropo
ラジコンは、mbed HRM1017とRCサーボやDCモータを組み合わせて作ります。
HRM1017/nordic/ble/ble_conn_params.cpp@7:505a9a98b776, 2015-03-14 (annotated)
- Committer:
- lipoyang
- Date:
- Sat Mar 14 11:51:38 2015 +0000
- Revision:
- 7:505a9a98b776
- Parent:
- 4:ebda47d22091
- adjust servo center position
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 1:48f6e08a3ac2 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
jksoft | 1:48f6e08a3ac2 | 2 | * |
jksoft | 1:48f6e08a3ac2 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
jksoft | 1:48f6e08a3ac2 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
jksoft | 1:48f6e08a3ac2 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
jksoft | 1:48f6e08a3ac2 | 6 | * |
jksoft | 1:48f6e08a3ac2 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
jksoft | 1:48f6e08a3ac2 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
jksoft | 1:48f6e08a3ac2 | 9 | * the file. |
jksoft | 1:48f6e08a3ac2 | 10 | * |
jksoft | 1:48f6e08a3ac2 | 11 | */ |
jksoft | 1:48f6e08a3ac2 | 12 | |
jksoft | 1:48f6e08a3ac2 | 13 | #include "ble_conn_params.h" |
jksoft | 1:48f6e08a3ac2 | 14 | #include <stdlib.h> |
jksoft | 1:48f6e08a3ac2 | 15 | #include "nordic_common.h" |
jksoft | 1:48f6e08a3ac2 | 16 | #include "ble_hci.h" |
jksoft | 1:48f6e08a3ac2 | 17 | #include "app_timer.h" |
jksoft | 1:48f6e08a3ac2 | 18 | #include "ble_srv_common.h" |
jksoft | 1:48f6e08a3ac2 | 19 | #include "app_util.h" |
jksoft | 1:48f6e08a3ac2 | 20 | |
jksoft | 1:48f6e08a3ac2 | 21 | |
jksoft | 1:48f6e08a3ac2 | 22 | static ble_conn_params_init_t m_conn_params_config; /**< Configuration as specified by the application. */ |
jksoft | 1:48f6e08a3ac2 | 23 | static ble_gap_conn_params_t m_preferred_conn_params; /**< Connection parameters preferred by the application. */ |
jksoft | 1:48f6e08a3ac2 | 24 | static uint8_t m_update_count; /**< Number of Connection Parameter Update messages that has currently been sent. */ |
jksoft | 1:48f6e08a3ac2 | 25 | static uint16_t m_conn_handle; /**< Current connection handle. */ |
jksoft | 1:48f6e08a3ac2 | 26 | static ble_gap_conn_params_t m_current_conn_params; /**< Connection parameters received in the most recent Connect event. */ |
jksoft | 1:48f6e08a3ac2 | 27 | static app_timer_id_t m_conn_params_timer_id; /**< Connection parameters timer. */ |
jksoft | 1:48f6e08a3ac2 | 28 | |
jksoft | 1:48f6e08a3ac2 | 29 | static bool m_change_param = false; |
jksoft | 1:48f6e08a3ac2 | 30 | |
jksoft | 1:48f6e08a3ac2 | 31 | static bool is_conn_params_ok(ble_gap_conn_params_t * p_conn_params) |
jksoft | 1:48f6e08a3ac2 | 32 | { |
jksoft | 1:48f6e08a3ac2 | 33 | // Check if interval is within the acceptable range. |
jksoft | 1:48f6e08a3ac2 | 34 | // NOTE: Using max_conn_interval in the received event data because this contains |
jksoft | 1:48f6e08a3ac2 | 35 | // the client's connection interval. |
jksoft | 1:48f6e08a3ac2 | 36 | if ((p_conn_params->max_conn_interval >= m_preferred_conn_params.min_conn_interval) && |
jksoft | 1:48f6e08a3ac2 | 37 | (p_conn_params->max_conn_interval <= m_preferred_conn_params.max_conn_interval)) { |
jksoft | 1:48f6e08a3ac2 | 38 | return true; |
jksoft | 1:48f6e08a3ac2 | 39 | } else { |
jksoft | 1:48f6e08a3ac2 | 40 | return false; |
jksoft | 1:48f6e08a3ac2 | 41 | } |
jksoft | 1:48f6e08a3ac2 | 42 | } |
jksoft | 1:48f6e08a3ac2 | 43 | |
jksoft | 1:48f6e08a3ac2 | 44 | |
jksoft | 1:48f6e08a3ac2 | 45 | static void update_timeout_handler(void * p_context) |
jksoft | 1:48f6e08a3ac2 | 46 | { |
jksoft | 1:48f6e08a3ac2 | 47 | UNUSED_PARAMETER(p_context); |
jksoft | 1:48f6e08a3ac2 | 48 | |
jksoft | 1:48f6e08a3ac2 | 49 | if (m_conn_handle != BLE_CONN_HANDLE_INVALID) |
jksoft | 1:48f6e08a3ac2 | 50 | { |
jksoft | 1:48f6e08a3ac2 | 51 | // Check if we have reached the maximum number of attempts |
jksoft | 1:48f6e08a3ac2 | 52 | m_update_count++; |
jksoft | 1:48f6e08a3ac2 | 53 | if (m_update_count <= m_conn_params_config.max_conn_params_update_count) |
jksoft | 1:48f6e08a3ac2 | 54 | { |
jksoft | 1:48f6e08a3ac2 | 55 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 56 | |
jksoft | 1:48f6e08a3ac2 | 57 | // Parameters are not ok, send connection parameters update request. |
jksoft | 1:48f6e08a3ac2 | 58 | err_code = sd_ble_gap_conn_param_update(m_conn_handle, &m_preferred_conn_params); |
jksoft | 1:48f6e08a3ac2 | 59 | if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) |
jksoft | 1:48f6e08a3ac2 | 60 | { |
jksoft | 1:48f6e08a3ac2 | 61 | m_conn_params_config.error_handler(err_code); |
jksoft | 1:48f6e08a3ac2 | 62 | } |
jksoft | 1:48f6e08a3ac2 | 63 | } |
jksoft | 1:48f6e08a3ac2 | 64 | else |
jksoft | 1:48f6e08a3ac2 | 65 | { |
jksoft | 1:48f6e08a3ac2 | 66 | m_update_count = 0; |
jksoft | 1:48f6e08a3ac2 | 67 | |
jksoft | 1:48f6e08a3ac2 | 68 | // Negotiation failed, disconnect automatically if this has been configured |
jksoft | 1:48f6e08a3ac2 | 69 | if (m_conn_params_config.disconnect_on_fail) |
jksoft | 1:48f6e08a3ac2 | 70 | { |
jksoft | 1:48f6e08a3ac2 | 71 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 72 | |
jksoft | 1:48f6e08a3ac2 | 73 | err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE); |
jksoft | 1:48f6e08a3ac2 | 74 | if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) |
jksoft | 1:48f6e08a3ac2 | 75 | { |
jksoft | 1:48f6e08a3ac2 | 76 | m_conn_params_config.error_handler(err_code); |
jksoft | 1:48f6e08a3ac2 | 77 | } |
jksoft | 1:48f6e08a3ac2 | 78 | } |
jksoft | 1:48f6e08a3ac2 | 79 | |
jksoft | 1:48f6e08a3ac2 | 80 | // Notify the application that the procedure has failed |
jksoft | 1:48f6e08a3ac2 | 81 | if (m_conn_params_config.evt_handler != NULL) |
jksoft | 1:48f6e08a3ac2 | 82 | { |
jksoft | 1:48f6e08a3ac2 | 83 | ble_conn_params_evt_t evt; |
jksoft | 1:48f6e08a3ac2 | 84 | |
jksoft | 1:48f6e08a3ac2 | 85 | evt.evt_type = BLE_CONN_PARAMS_EVT_FAILED; |
jksoft | 1:48f6e08a3ac2 | 86 | m_conn_params_config.evt_handler(&evt); |
jksoft | 1:48f6e08a3ac2 | 87 | } |
jksoft | 1:48f6e08a3ac2 | 88 | } |
jksoft | 1:48f6e08a3ac2 | 89 | } |
jksoft | 1:48f6e08a3ac2 | 90 | } |
jksoft | 1:48f6e08a3ac2 | 91 | |
jksoft | 1:48f6e08a3ac2 | 92 | |
jksoft | 1:48f6e08a3ac2 | 93 | uint32_t ble_conn_params_init(const ble_conn_params_init_t * p_init) |
jksoft | 1:48f6e08a3ac2 | 94 | { |
jksoft | 1:48f6e08a3ac2 | 95 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 96 | |
jksoft | 1:48f6e08a3ac2 | 97 | m_conn_params_config = *p_init; |
jksoft | 1:48f6e08a3ac2 | 98 | m_change_param = false; |
jksoft | 1:48f6e08a3ac2 | 99 | if (p_init->p_conn_params != NULL) |
jksoft | 1:48f6e08a3ac2 | 100 | { |
jksoft | 1:48f6e08a3ac2 | 101 | m_preferred_conn_params = *p_init->p_conn_params; |
jksoft | 1:48f6e08a3ac2 | 102 | |
jksoft | 1:48f6e08a3ac2 | 103 | // Set the connection params in stack |
jksoft | 1:48f6e08a3ac2 | 104 | err_code = sd_ble_gap_ppcp_set(&m_preferred_conn_params); |
jksoft | 1:48f6e08a3ac2 | 105 | if (err_code != NRF_SUCCESS) |
jksoft | 1:48f6e08a3ac2 | 106 | { |
jksoft | 1:48f6e08a3ac2 | 107 | return err_code; |
jksoft | 1:48f6e08a3ac2 | 108 | } |
jksoft | 1:48f6e08a3ac2 | 109 | } |
jksoft | 1:48f6e08a3ac2 | 110 | else |
jksoft | 1:48f6e08a3ac2 | 111 | { |
jksoft | 1:48f6e08a3ac2 | 112 | // Fetch the connection params from stack |
jksoft | 1:48f6e08a3ac2 | 113 | err_code = sd_ble_gap_ppcp_get(&m_preferred_conn_params); |
jksoft | 1:48f6e08a3ac2 | 114 | if (err_code != NRF_SUCCESS) |
jksoft | 1:48f6e08a3ac2 | 115 | { |
jksoft | 1:48f6e08a3ac2 | 116 | return err_code; |
jksoft | 1:48f6e08a3ac2 | 117 | } |
jksoft | 1:48f6e08a3ac2 | 118 | } |
jksoft | 1:48f6e08a3ac2 | 119 | |
jksoft | 1:48f6e08a3ac2 | 120 | m_conn_handle = BLE_CONN_HANDLE_INVALID; |
jksoft | 1:48f6e08a3ac2 | 121 | m_update_count = 0; |
jksoft | 1:48f6e08a3ac2 | 122 | |
jksoft | 1:48f6e08a3ac2 | 123 | return app_timer_create(&m_conn_params_timer_id, |
jksoft | 1:48f6e08a3ac2 | 124 | APP_TIMER_MODE_SINGLE_SHOT, |
jksoft | 1:48f6e08a3ac2 | 125 | update_timeout_handler); |
jksoft | 1:48f6e08a3ac2 | 126 | } |
jksoft | 1:48f6e08a3ac2 | 127 | |
jksoft | 1:48f6e08a3ac2 | 128 | |
jksoft | 1:48f6e08a3ac2 | 129 | uint32_t ble_conn_params_stop(void) |
jksoft | 1:48f6e08a3ac2 | 130 | { |
jksoft | 1:48f6e08a3ac2 | 131 | return app_timer_stop(m_conn_params_timer_id); |
jksoft | 1:48f6e08a3ac2 | 132 | } |
jksoft | 1:48f6e08a3ac2 | 133 | |
jksoft | 1:48f6e08a3ac2 | 134 | |
jksoft | 1:48f6e08a3ac2 | 135 | static void conn_params_negotiation(void) |
jksoft | 1:48f6e08a3ac2 | 136 | { |
jksoft | 1:48f6e08a3ac2 | 137 | // Start negotiation if the received connection parameters are not acceptable |
jksoft | 1:48f6e08a3ac2 | 138 | if (!is_conn_params_ok(&m_current_conn_params)) |
jksoft | 1:48f6e08a3ac2 | 139 | { |
jksoft | 1:48f6e08a3ac2 | 140 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 141 | uint32_t timeout_ticks; |
jksoft | 1:48f6e08a3ac2 | 142 | |
jksoft | 1:48f6e08a3ac2 | 143 | if (m_change_param) |
jksoft | 1:48f6e08a3ac2 | 144 | { |
jksoft | 1:48f6e08a3ac2 | 145 | // Notify the application that the procedure has failed |
jksoft | 1:48f6e08a3ac2 | 146 | if (m_conn_params_config.evt_handler != NULL) |
jksoft | 1:48f6e08a3ac2 | 147 | { |
jksoft | 1:48f6e08a3ac2 | 148 | ble_conn_params_evt_t evt; |
jksoft | 1:48f6e08a3ac2 | 149 | |
jksoft | 1:48f6e08a3ac2 | 150 | evt.evt_type = BLE_CONN_PARAMS_EVT_FAILED; |
jksoft | 1:48f6e08a3ac2 | 151 | m_conn_params_config.evt_handler(&evt); |
jksoft | 1:48f6e08a3ac2 | 152 | } |
jksoft | 1:48f6e08a3ac2 | 153 | } |
jksoft | 1:48f6e08a3ac2 | 154 | else |
jksoft | 1:48f6e08a3ac2 | 155 | { |
jksoft | 1:48f6e08a3ac2 | 156 | if (m_update_count == 0) |
jksoft | 1:48f6e08a3ac2 | 157 | { |
jksoft | 1:48f6e08a3ac2 | 158 | // First connection parameter update |
jksoft | 1:48f6e08a3ac2 | 159 | timeout_ticks = m_conn_params_config.first_conn_params_update_delay; |
jksoft | 1:48f6e08a3ac2 | 160 | } |
jksoft | 1:48f6e08a3ac2 | 161 | else |
jksoft | 1:48f6e08a3ac2 | 162 | { |
jksoft | 1:48f6e08a3ac2 | 163 | timeout_ticks = m_conn_params_config.next_conn_params_update_delay; |
jksoft | 1:48f6e08a3ac2 | 164 | } |
jksoft | 1:48f6e08a3ac2 | 165 | |
jksoft | 1:48f6e08a3ac2 | 166 | err_code = app_timer_start(m_conn_params_timer_id, timeout_ticks, NULL); |
jksoft | 1:48f6e08a3ac2 | 167 | if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) |
jksoft | 1:48f6e08a3ac2 | 168 | { |
jksoft | 1:48f6e08a3ac2 | 169 | m_conn_params_config.error_handler(err_code); |
jksoft | 1:48f6e08a3ac2 | 170 | } |
jksoft | 1:48f6e08a3ac2 | 171 | } |
jksoft | 1:48f6e08a3ac2 | 172 | } |
jksoft | 1:48f6e08a3ac2 | 173 | else |
jksoft | 1:48f6e08a3ac2 | 174 | { |
jksoft | 1:48f6e08a3ac2 | 175 | // Notify the application that the procedure has succeded |
jksoft | 1:48f6e08a3ac2 | 176 | if (m_conn_params_config.evt_handler != NULL) |
jksoft | 1:48f6e08a3ac2 | 177 | { |
jksoft | 1:48f6e08a3ac2 | 178 | ble_conn_params_evt_t evt; |
jksoft | 1:48f6e08a3ac2 | 179 | |
jksoft | 1:48f6e08a3ac2 | 180 | evt.evt_type = BLE_CONN_PARAMS_EVT_SUCCEEDED; |
jksoft | 1:48f6e08a3ac2 | 181 | m_conn_params_config.evt_handler(&evt); |
jksoft | 1:48f6e08a3ac2 | 182 | } |
jksoft | 1:48f6e08a3ac2 | 183 | } |
jksoft | 1:48f6e08a3ac2 | 184 | m_change_param = false; |
jksoft | 1:48f6e08a3ac2 | 185 | } |
jksoft | 1:48f6e08a3ac2 | 186 | |
jksoft | 1:48f6e08a3ac2 | 187 | |
jksoft | 1:48f6e08a3ac2 | 188 | static void on_connect(ble_evt_t * p_ble_evt) |
jksoft | 1:48f6e08a3ac2 | 189 | { |
jksoft | 1:48f6e08a3ac2 | 190 | // Save connection parameters |
jksoft | 1:48f6e08a3ac2 | 191 | m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle; |
jksoft | 1:48f6e08a3ac2 | 192 | m_current_conn_params = p_ble_evt->evt.gap_evt.params.connected.conn_params; |
jksoft | 1:48f6e08a3ac2 | 193 | m_update_count = 0; // Connection parameter negotiation should re-start every connection |
jksoft | 1:48f6e08a3ac2 | 194 | |
jksoft | 1:48f6e08a3ac2 | 195 | // Check if we shall handle negotiation on connect |
jksoft | 1:48f6e08a3ac2 | 196 | if (m_conn_params_config.start_on_notify_cccd_handle == BLE_GATT_HANDLE_INVALID) |
jksoft | 1:48f6e08a3ac2 | 197 | { |
jksoft | 1:48f6e08a3ac2 | 198 | conn_params_negotiation(); |
jksoft | 1:48f6e08a3ac2 | 199 | } |
jksoft | 1:48f6e08a3ac2 | 200 | } |
jksoft | 1:48f6e08a3ac2 | 201 | |
jksoft | 1:48f6e08a3ac2 | 202 | |
jksoft | 1:48f6e08a3ac2 | 203 | static void on_disconnect(ble_evt_t * p_ble_evt) |
jksoft | 1:48f6e08a3ac2 | 204 | { |
jksoft | 1:48f6e08a3ac2 | 205 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 206 | |
jksoft | 1:48f6e08a3ac2 | 207 | m_conn_handle = BLE_CONN_HANDLE_INVALID; |
jksoft | 1:48f6e08a3ac2 | 208 | |
jksoft | 1:48f6e08a3ac2 | 209 | // Stop timer if running |
jksoft | 1:48f6e08a3ac2 | 210 | m_update_count = 0; // Connection parameters updates should happen during every connection |
jksoft | 1:48f6e08a3ac2 | 211 | |
jksoft | 1:48f6e08a3ac2 | 212 | err_code = app_timer_stop(m_conn_params_timer_id); |
jksoft | 1:48f6e08a3ac2 | 213 | if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) |
jksoft | 1:48f6e08a3ac2 | 214 | { |
jksoft | 1:48f6e08a3ac2 | 215 | m_conn_params_config.error_handler(err_code); |
jksoft | 1:48f6e08a3ac2 | 216 | } |
jksoft | 1:48f6e08a3ac2 | 217 | } |
jksoft | 1:48f6e08a3ac2 | 218 | |
jksoft | 1:48f6e08a3ac2 | 219 | |
jksoft | 1:48f6e08a3ac2 | 220 | static void on_write(ble_evt_t * p_ble_evt) |
jksoft | 1:48f6e08a3ac2 | 221 | { |
jksoft | 1:48f6e08a3ac2 | 222 | ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write; |
jksoft | 1:48f6e08a3ac2 | 223 | |
jksoft | 1:48f6e08a3ac2 | 224 | // Check if this the correct CCCD |
jksoft | 1:48f6e08a3ac2 | 225 | if ( |
jksoft | 1:48f6e08a3ac2 | 226 | (p_evt_write->handle == m_conn_params_config.start_on_notify_cccd_handle) |
jksoft | 1:48f6e08a3ac2 | 227 | && |
jksoft | 1:48f6e08a3ac2 | 228 | (p_evt_write->len == 2) |
jksoft | 1:48f6e08a3ac2 | 229 | ) |
jksoft | 1:48f6e08a3ac2 | 230 | { |
jksoft | 1:48f6e08a3ac2 | 231 | // Check if this is a 'start notification' |
jksoft | 1:48f6e08a3ac2 | 232 | if (ble_srv_is_notification_enabled(p_evt_write->data)) |
jksoft | 1:48f6e08a3ac2 | 233 | { |
jksoft | 1:48f6e08a3ac2 | 234 | // Do connection parameter negotiation if necessary |
jksoft | 1:48f6e08a3ac2 | 235 | conn_params_negotiation(); |
jksoft | 1:48f6e08a3ac2 | 236 | } |
jksoft | 1:48f6e08a3ac2 | 237 | else |
jksoft | 1:48f6e08a3ac2 | 238 | { |
jksoft | 1:48f6e08a3ac2 | 239 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 240 | |
jksoft | 1:48f6e08a3ac2 | 241 | // Stop timer if running |
jksoft | 1:48f6e08a3ac2 | 242 | err_code = app_timer_stop(m_conn_params_timer_id); |
jksoft | 1:48f6e08a3ac2 | 243 | if ((err_code != NRF_SUCCESS) && (m_conn_params_config.error_handler != NULL)) |
jksoft | 1:48f6e08a3ac2 | 244 | { |
jksoft | 1:48f6e08a3ac2 | 245 | m_conn_params_config.error_handler(err_code); |
jksoft | 1:48f6e08a3ac2 | 246 | } |
jksoft | 1:48f6e08a3ac2 | 247 | } |
jksoft | 1:48f6e08a3ac2 | 248 | } |
jksoft | 1:48f6e08a3ac2 | 249 | } |
jksoft | 1:48f6e08a3ac2 | 250 | |
jksoft | 1:48f6e08a3ac2 | 251 | |
jksoft | 1:48f6e08a3ac2 | 252 | static void on_conn_params_update(ble_evt_t * p_ble_evt) |
jksoft | 1:48f6e08a3ac2 | 253 | { |
jksoft | 1:48f6e08a3ac2 | 254 | // Copy the parameters |
jksoft | 1:48f6e08a3ac2 | 255 | m_current_conn_params = p_ble_evt->evt.gap_evt.params.conn_param_update.conn_params; |
jksoft | 1:48f6e08a3ac2 | 256 | |
jksoft | 1:48f6e08a3ac2 | 257 | conn_params_negotiation(); |
jksoft | 1:48f6e08a3ac2 | 258 | } |
jksoft | 1:48f6e08a3ac2 | 259 | |
jksoft | 1:48f6e08a3ac2 | 260 | |
jksoft | 1:48f6e08a3ac2 | 261 | void ble_conn_params_on_ble_evt(ble_evt_t * p_ble_evt) |
jksoft | 1:48f6e08a3ac2 | 262 | { |
jksoft | 1:48f6e08a3ac2 | 263 | switch (p_ble_evt->header.evt_id) |
jksoft | 1:48f6e08a3ac2 | 264 | { |
jksoft | 1:48f6e08a3ac2 | 265 | case BLE_GAP_EVT_CONNECTED: |
jksoft | 1:48f6e08a3ac2 | 266 | on_connect(p_ble_evt); |
jksoft | 1:48f6e08a3ac2 | 267 | break; |
jksoft | 1:48f6e08a3ac2 | 268 | |
jksoft | 1:48f6e08a3ac2 | 269 | case BLE_GAP_EVT_DISCONNECTED: |
jksoft | 1:48f6e08a3ac2 | 270 | on_disconnect(p_ble_evt); |
jksoft | 1:48f6e08a3ac2 | 271 | break; |
jksoft | 1:48f6e08a3ac2 | 272 | |
jksoft | 1:48f6e08a3ac2 | 273 | case BLE_GATTS_EVT_WRITE: |
jksoft | 1:48f6e08a3ac2 | 274 | on_write(p_ble_evt); |
jksoft | 1:48f6e08a3ac2 | 275 | break; |
jksoft | 1:48f6e08a3ac2 | 276 | |
jksoft | 1:48f6e08a3ac2 | 277 | case BLE_GAP_EVT_CONN_PARAM_UPDATE: |
jksoft | 1:48f6e08a3ac2 | 278 | on_conn_params_update(p_ble_evt); |
jksoft | 1:48f6e08a3ac2 | 279 | break; |
jksoft | 1:48f6e08a3ac2 | 280 | |
jksoft | 1:48f6e08a3ac2 | 281 | default: |
jksoft | 1:48f6e08a3ac2 | 282 | // No implementation needed. |
jksoft | 1:48f6e08a3ac2 | 283 | break; |
jksoft | 1:48f6e08a3ac2 | 284 | } |
jksoft | 1:48f6e08a3ac2 | 285 | } |
jksoft | 1:48f6e08a3ac2 | 286 | |
jksoft | 1:48f6e08a3ac2 | 287 | uint32_t ble_conn_params_change_conn_params(ble_gap_conn_params_t *new_params) |
jksoft | 1:48f6e08a3ac2 | 288 | { |
jksoft | 1:48f6e08a3ac2 | 289 | uint32_t err_code; |
jksoft | 1:48f6e08a3ac2 | 290 | |
jksoft | 1:48f6e08a3ac2 | 291 | m_preferred_conn_params = *new_params; |
jksoft | 1:48f6e08a3ac2 | 292 | // Set the connection params in stack |
jksoft | 1:48f6e08a3ac2 | 293 | err_code = sd_ble_gap_ppcp_set(&m_preferred_conn_params); |
jksoft | 1:48f6e08a3ac2 | 294 | if (err_code == NRF_SUCCESS) |
jksoft | 1:48f6e08a3ac2 | 295 | { |
jksoft | 1:48f6e08a3ac2 | 296 | if (!is_conn_params_ok(&m_current_conn_params)) |
jksoft | 1:48f6e08a3ac2 | 297 | { |
jksoft | 1:48f6e08a3ac2 | 298 | m_change_param = true; |
jksoft | 1:48f6e08a3ac2 | 299 | err_code = sd_ble_gap_conn_param_update(m_conn_handle, &m_preferred_conn_params); |
jksoft | 1:48f6e08a3ac2 | 300 | m_update_count = 1; |
jksoft | 1:48f6e08a3ac2 | 301 | } |
jksoft | 1:48f6e08a3ac2 | 302 | else |
jksoft | 1:48f6e08a3ac2 | 303 | { |
jksoft | 1:48f6e08a3ac2 | 304 | // Notify the application that the procedure has succeded |
jksoft | 1:48f6e08a3ac2 | 305 | if (m_conn_params_config.evt_handler != NULL) |
jksoft | 1:48f6e08a3ac2 | 306 | { |
jksoft | 1:48f6e08a3ac2 | 307 | ble_conn_params_evt_t evt; |
jksoft | 1:48f6e08a3ac2 | 308 | |
jksoft | 1:48f6e08a3ac2 | 309 | evt.evt_type = BLE_CONN_PARAMS_EVT_SUCCEEDED; |
jksoft | 1:48f6e08a3ac2 | 310 | m_conn_params_config.evt_handler(&evt); |
jksoft | 1:48f6e08a3ac2 | 311 | } |
jksoft | 1:48f6e08a3ac2 | 312 | err_code = NRF_SUCCESS; |
jksoft | 1:48f6e08a3ac2 | 313 | } |
jksoft | 1:48f6e08a3ac2 | 314 | } |
jksoft | 1:48f6e08a3ac2 | 315 | return err_code; |
jksoft | 1:48f6e08a3ac2 | 316 | } |