Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_WallbotBLE_Challenge by
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 #if SDK_CONN_PARAMS_MODULE_ENABLE 00024 static void error_callback(uint32_t nrf_error); 00025 #endif // SDK_CONN_PARAMS_MODULE_ENABLE 00026 00027 /**************************************************************************/ 00028 /*! 00029 @brief Initialise GAP in the underlying SoftDevice 00030 00031 @returns 00032 */ 00033 /**************************************************************************/ 00034 error_t btle_gap_init(void) 00035 { 00036 ble_gap_conn_params_t gap_conn_params = {0}; 00037 00038 gap_conn_params.min_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MIN_INTERVAL_MS); // in 1.25ms units 00039 gap_conn_params.max_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MAX_INTERVAL_MS); // in 1.25ms unit 00040 gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY; 00041 gap_conn_params.conn_sup_timeout = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit 00042 00043 ble_gap_conn_sec_mode_t sec_mode; 00044 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed 00045 00046 ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME))); 00047 ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE)); 00048 ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params)); 00049 ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL)); 00050 00051 /** 00052 * Call to conn_params_init() is not necessary; and so is disabled by default. 00053 * This API should be exposed to the user to be invoked when necessary. 00054 */ 00055 #if SDK_CONN_PARAMS_MODULE_ENABLE 00056 /* Connection Parameters */ 00057 enum { 00058 FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), 00059 NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER), 00060 MAX_UPDATE_COUNT = 3 00061 }; 00062 00063 ble_conn_params_init_t cp_init = {0}; 00064 00065 cp_init.p_conn_params = NULL; 00066 cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY; 00067 cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY; 00068 cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT; 00069 cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID; 00070 cp_init.disconnect_on_fail = true; 00071 cp_init.evt_handler = NULL; 00072 cp_init.error_handler = error_callback; 00073 00074 ASSERT_STATUS ( ble_conn_params_init(&cp_init)); 00075 #endif // SDK_CONN_PARAMS_MODULE_ENABLE 00076 00077 return ERROR_NONE; 00078 } 00079 00080 /**************************************************************************/ 00081 /*! 00082 @brief Converts msecs to an integer representing 1.25ms units 00083 00084 @param[in] ms 00085 The number of milliseconds to conver to 1.25ms units 00086 00087 @returns The number of 1.25ms units in the supplied number of ms 00088 */ 00089 /**************************************************************************/ 00090 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) 00091 { 00092 return (interval_ms * 4) / 5; 00093 } 00094 00095 #if SDK_CONN_PARAMS_MODULE_ENABLE 00096 static void error_callback(uint32_t nrf_error) 00097 { 00098 ASSERT_STATUS_RET_VOID( nrf_error ); 00099 } 00100 #endif // SDK_CONN_PARAMS_MODULE_ENABLE
Generated on Tue Jul 12 2022 13:52:30 by
