konashi/SBBLEのテスト

Dependencies:   BLE_API mbed

Fork of BLE_LoopbackUART by Bluetooth Low Energy

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 "app_timer.h "
00019 #include "ble_gap.h"
00020 #include "ble_conn_params.h "
00021 
00022 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST;
00023 static void   error_callback(uint32_t nrf_error);
00024 
00025 /**************************************************************************/
00026 /*!
00027     @brief      Initialise GAP in the underlying SoftDevice
00028 
00029     @returns
00030 */
00031 /**************************************************************************/
00032 error_t btle_gap_init(void)
00033 {
00034     ble_gap_conn_params_t gap_conn_params = {0};
00035 
00036     gap_conn_params.min_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MIN_INTERVAL_MS);  // in 1.25ms units
00037     gap_conn_params.max_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MAX_INTERVAL_MS);  // in 1.25ms unit
00038     gap_conn_params.slave_latency     = CFG_GAP_CONNECTION_SLAVE_LATENCY;
00039     gap_conn_params.conn_sup_timeout  = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit
00040 
00041     ble_gap_conn_sec_mode_t sec_mode;
00042     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
00043 
00044     ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME)));
00045     ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE));
00046     ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params));
00047     ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL));
00048 
00049     /* Connection Parameters */
00050     enum {
00051         FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
00052         NEXT_UPDATE_DELAY  = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
00053         MAX_UPDATE_COUNT   = 3
00054     };
00055 
00056     ble_conn_params_init_t cp_init = {0};
00057 
00058     cp_init.p_conn_params                  = NULL;
00059     cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY;
00060     cp_init.next_conn_params_update_delay  = NEXT_UPDATE_DELAY;
00061     cp_init.max_conn_params_update_count   = MAX_UPDATE_COUNT;
00062     cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
00063     cp_init.disconnect_on_fail             = true;
00064     cp_init.evt_handler                    = NULL;
00065     cp_init.error_handler                  = error_callback;
00066 
00067     ASSERT_STATUS ( ble_conn_params_init(&cp_init));
00068 
00069     return ERROR_NONE;
00070 }
00071 
00072 /**************************************************************************/
00073 /*!
00074     @brief      Converts msecs to an integer representing 1.25ms units
00075 
00076     @param[in]  ms
00077                 The number of milliseconds to conver to 1.25ms units
00078 
00079     @returns    The number of 1.25ms units in the supplied number of ms
00080 */
00081 /**************************************************************************/
00082 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms)
00083 {
00084     return (interval_ms * 4) / 5;
00085 }
00086 
00087 static void error_callback(uint32_t nrf_error)
00088 {
00089     ASSERT_STATUS_RET_VOID( nrf_error );
00090 }