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