For with fix for disconnection notifications

Fork of nRF51822 by Nordic Semiconductor

Committer:
bogdanm
Date:
Wed Mar 26 14:38:17 2014 +0000
Revision:
0:eff01767de02
Child:
17:c3e11bb6cb72
Initial import of the nRF51822 code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:eff01767de02 1 /* mbed Microcontroller Library
bogdanm 0:eff01767de02 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 0:eff01767de02 3 *
bogdanm 0:eff01767de02 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 0:eff01767de02 5 * you may not use this file except in compliance with the License.
bogdanm 0:eff01767de02 6 * You may obtain a copy of the License at
bogdanm 0:eff01767de02 7 *
bogdanm 0:eff01767de02 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 0:eff01767de02 9 *
bogdanm 0:eff01767de02 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 0:eff01767de02 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 0:eff01767de02 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 0:eff01767de02 13 * See the License for the specific language governing permissions and
bogdanm 0:eff01767de02 14 * limitations under the License.
bogdanm 0:eff01767de02 15 */
bogdanm 0:eff01767de02 16 #include "common/common.h"
bogdanm 0:eff01767de02 17
bogdanm 0:eff01767de02 18 #include "app_timer.h"
bogdanm 0:eff01767de02 19 #include "ble_gap.h"
bogdanm 0:eff01767de02 20 #include "ble_conn_params.h"
bogdanm 0:eff01767de02 21
bogdanm 0:eff01767de02 22 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST;
bogdanm 0:eff01767de02 23 static void error_callback(uint32_t nrf_error);
bogdanm 0:eff01767de02 24
bogdanm 0:eff01767de02 25 /**************************************************************************/
bogdanm 0:eff01767de02 26 /*!
bogdanm 0:eff01767de02 27 @brief Initialise GAP in the underlying SoftDevice
bogdanm 0:eff01767de02 28
bogdanm 0:eff01767de02 29 @returns
bogdanm 0:eff01767de02 30 */
bogdanm 0:eff01767de02 31 /**************************************************************************/
bogdanm 0:eff01767de02 32 error_t btle_gap_init(void)
bogdanm 0:eff01767de02 33 {
bogdanm 0:eff01767de02 34 ble_gap_conn_params_t gap_conn_params = { 0 };
bogdanm 0:eff01767de02 35
bogdanm 0:eff01767de02 36 gap_conn_params.min_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MIN_INTERVAL_MS) ; // in 1.25ms unit
bogdanm 0:eff01767de02 37 gap_conn_params.max_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MAX_INTERVAL_MS) ; // in 1.25ms unit
bogdanm 0:eff01767de02 38 gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY ;
bogdanm 0:eff01767de02 39 gap_conn_params.conn_sup_timeout = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10 ; // in 10ms unit
bogdanm 0:eff01767de02 40
bogdanm 0:eff01767de02 41 ble_gap_conn_sec_mode_t sec_mode;
bogdanm 0:eff01767de02 42 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
bogdanm 0:eff01767de02 43
bogdanm 0:eff01767de02 44 ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME)) );
bogdanm 0:eff01767de02 45 ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE) );
bogdanm 0:eff01767de02 46 ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params) );
bogdanm 0:eff01767de02 47 ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL) );
bogdanm 0:eff01767de02 48
bogdanm 0:eff01767de02 49 /* Connection Parameters */
bogdanm 0:eff01767de02 50 enum {
bogdanm 0:eff01767de02 51 FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
bogdanm 0:eff01767de02 52 NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
bogdanm 0:eff01767de02 53 MAX_UPDATE_COUNT = 3
bogdanm 0:eff01767de02 54 };
bogdanm 0:eff01767de02 55
bogdanm 0:eff01767de02 56 ble_conn_params_init_t cp_init = { 0 };
bogdanm 0:eff01767de02 57
bogdanm 0:eff01767de02 58 cp_init.p_conn_params = NULL ;
bogdanm 0:eff01767de02 59 cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY ;
bogdanm 0:eff01767de02 60 cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY ;
bogdanm 0:eff01767de02 61 cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT ;
bogdanm 0:eff01767de02 62 cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID ;
bogdanm 0:eff01767de02 63 cp_init.disconnect_on_fail = true ;
bogdanm 0:eff01767de02 64 cp_init.evt_handler = NULL ;
bogdanm 0:eff01767de02 65 cp_init.error_handler = error_callback;
bogdanm 0:eff01767de02 66
bogdanm 0:eff01767de02 67 ASSERT_STATUS ( ble_conn_params_init(&cp_init) );
bogdanm 0:eff01767de02 68
bogdanm 0:eff01767de02 69 return ERROR_NONE;
bogdanm 0:eff01767de02 70 }
bogdanm 0:eff01767de02 71
bogdanm 0:eff01767de02 72 /**************************************************************************/
bogdanm 0:eff01767de02 73 /*!
bogdanm 0:eff01767de02 74 @brief Converts msecs to an integer representing 1.25ms units
bogdanm 0:eff01767de02 75
bogdanm 0:eff01767de02 76 @param[in] ms
bogdanm 0:eff01767de02 77 The number of milliseconds to conver to 1.25ms units
bogdanm 0:eff01767de02 78
bogdanm 0:eff01767de02 79 @returns The number of 1.25ms units in the supplied number of ms
bogdanm 0:eff01767de02 80 */
bogdanm 0:eff01767de02 81 /**************************************************************************/
bogdanm 0:eff01767de02 82 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms)
bogdanm 0:eff01767de02 83 {
bogdanm 0:eff01767de02 84 return (interval_ms * 4) / 5 ;
bogdanm 0:eff01767de02 85 }
bogdanm 0:eff01767de02 86
bogdanm 0:eff01767de02 87 static void error_callback(uint32_t nrf_error)
bogdanm 0:eff01767de02 88 {
bogdanm 0:eff01767de02 89 ASSERT_STATUS_RET_VOID( nrf_error );
bogdanm 0:eff01767de02 90 }