test

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers btle_gap.cpp Source File

btle_gap.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "common/common.h"
00017 
00018 #include "ble_gap.h"
00019 #include "ble_conn_params.h "
00020 
00021 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST;
00022 #if SDK_CONN_PARAMS_MODULE_ENABLE
00023 static void   error_callback(uint32_t nrf_error);
00024 #endif // SDK_CONN_PARAMS_MODULE_ENABLE
00025 
00026 /**************************************************************************/
00027 /*!
00028     @brief      Initialise GAP in the underlying SoftDevice
00029 
00030     @returns
00031 */
00032 /**************************************************************************/
00033 error_t btle_gap_init(void)
00034 {
00035     ble_gap_conn_params_t gap_conn_params = {0};
00036 
00037     gap_conn_params.min_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MIN_INTERVAL_MS);  // in 1.25ms units
00038     gap_conn_params.max_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MAX_INTERVAL_MS);  // in 1.25ms unit
00039     gap_conn_params.slave_latency     = CFG_GAP_CONNECTION_SLAVE_LATENCY;
00040     gap_conn_params.conn_sup_timeout  = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit
00041 
00042     ble_gap_conn_sec_mode_t sec_mode;
00043     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
00044 
00045     ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME)));
00046     ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE));
00047     ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params));
00048     ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL));
00049 
00050     /**
00051      * Call to conn_params_init() is not necessary; and so is disabled by default.
00052      * This API should be exposed to the user to be invoked when necessary.
00053      */
00054 #if SDK_CONN_PARAMS_MODULE_ENABLE
00055     /* Connection Parameters */
00056     enum {
00057         FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
00058         NEXT_UPDATE_DELAY  = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
00059         MAX_UPDATE_COUNT   = 3
00060     };
00061 
00062     ble_conn_params_init_t cp_init = {0};
00063 
00064     cp_init.p_conn_params                  = NULL;
00065     cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY;
00066     cp_init.next_conn_params_update_delay  = NEXT_UPDATE_DELAY;
00067     cp_init.max_conn_params_update_count   = MAX_UPDATE_COUNT;
00068     cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
00069     cp_init.disconnect_on_fail             = true;
00070     cp_init.evt_handler                    = NULL;
00071     cp_init.error_handler                  = error_callback;
00072 
00073     ASSERT_STATUS ( ble_conn_params_init(&cp_init));
00074 #endif // SDK_CONN_PARAMS_MODULE_ENABLE
00075 
00076     return ERROR_NONE;
00077 }
00078 
00079 /**************************************************************************/
00080 /*!
00081     @brief      Converts msecs to an integer representing 1.25ms units
00082 
00083     @param[in]  ms
00084                 The number of milliseconds to conver to 1.25ms units
00085 
00086     @returns    The number of 1.25ms units in the supplied number of ms
00087 */
00088 /**************************************************************************/
00089 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms)
00090 {
00091     return (interval_ms * 4) / 5;
00092 }
00093 
00094 #if SDK_CONN_PARAMS_MODULE_ENABLE
00095 static void error_callback(uint32_t nrf_error)
00096 {
00097     ASSERT_STATUS_RET_VOID( nrf_error );
00098 }
00099 #endif // SDK_CONN_PARAMS_MODULE_ENABLE