テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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