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 nRF51822 by
nRF51Gap.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 00017 #include "nRF51Gap.h" 00018 #include "mbed.h" 00019 00020 #include "common/common.h " 00021 #include "ble_advdata.h " 00022 #include "ble_hci.h" 00023 00024 /**************************************************************************/ 00025 /*! 00026 @brief Sets the advertising parameters and payload for the device 00027 00028 @param[in] params 00029 Basic advertising details, including the advertising 00030 delay, timeout and how the device should be advertised 00031 @params[in] advData 00032 The primary advertising data payload 00033 @params[in] scanResponse 00034 The optional Scan Response payload if the advertising 00035 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED 00036 in \ref GapAdveritinngParams 00037 00038 @returns \ref ble_error_t 00039 00040 @retval BLE_ERROR_NONE 00041 Everything executed properly 00042 00043 @retval BLE_ERROR_BUFFER_OVERFLOW 00044 The proposed action would cause a buffer overflow. All 00045 advertising payloads must be <= 31 bytes, for example. 00046 00047 @retval BLE_ERROR_NOT_IMPLEMENTED 00048 A feature was requested that is not yet supported in the 00049 nRF51 firmware or hardware. 00050 00051 @retval BLE_ERROR_PARAM_OUT_OF_RANGE 00052 One of the proposed values is outside the valid range. 00053 00054 @section EXAMPLE 00055 00056 @code 00057 00058 @endcode 00059 */ 00060 /**************************************************************************/ 00061 ble_error_t nRF51Gap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) 00062 { 00063 /* Make sure we don't exceed the advertising payload length */ 00064 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { 00065 return BLE_ERROR_BUFFER_OVERFLOW; 00066 } 00067 00068 /* Make sure we have a payload! */ 00069 if (advData.getPayloadLen() == 0) { 00070 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00071 } 00072 00073 /* Check the scan response payload limits */ 00074 //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)) 00075 //{ 00076 // /* Check if we're within the upper limit */ 00077 // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) 00078 // { 00079 // return BLE_ERROR_BUFFER_OVERFLOW; 00080 // } 00081 // /* Make sure we have a payload! */ 00082 // if (advData.getPayloadLen() == 0) 00083 // { 00084 // return BLE_ERROR_PARAM_OUT_OF_RANGE; 00085 // } 00086 //} 00087 00088 /* Send advertising data! */ 00089 ASSERT(ERROR_NONE == 00090 sd_ble_gap_adv_data_set(advData.getPayload(), 00091 advData.getPayloadLen(), 00092 scanResponse.getPayload(), 00093 scanResponse.getPayloadLen()), 00094 BLE_ERROR_PARAM_OUT_OF_RANGE); 00095 00096 /* Make sure the GAP Service appearance value is aligned with the 00097 *appearance from GapAdvertisingData */ 00098 ASSERT(ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), 00099 BLE_ERROR_PARAM_OUT_OF_RANGE); 00100 00101 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */ 00102 /* contains a flags AD type, etc. */ 00103 00104 return BLE_ERROR_NONE; 00105 } 00106 00107 /**************************************************************************/ 00108 /*! 00109 @brief Starts the BLE HW, initialising any services that were 00110 added before this function was called. 00111 00112 @note All services must be added before calling this function! 00113 00114 @returns ble_error_t 00115 00116 @retval BLE_ERROR_NONE 00117 Everything executed properly 00118 00119 @section EXAMPLE 00120 00121 @code 00122 00123 @endcode 00124 */ 00125 /**************************************************************************/ 00126 ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams ¶ms) 00127 { 00128 /* Make sure we support the advertising type */ 00129 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { 00130 /* ToDo: This requires a propery security implementation, etc. */ 00131 return BLE_ERROR_NOT_IMPLEMENTED; 00132 } 00133 00134 /* Check interval range */ 00135 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { 00136 /* Min delay is slightly longer for unconnectable devices */ 00137 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || 00138 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { 00139 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00140 } 00141 } else { 00142 if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || 00143 (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { 00144 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00145 } 00146 } 00147 00148 /* Check timeout is zero for Connectable Directed */ 00149 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) { 00150 /* Timeout must be 0 with this type, although we'll never get here */ 00151 /* since this isn't implemented yet anyway */ 00152 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00153 } 00154 00155 /* Check timeout for other advertising types */ 00156 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && 00157 (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) { 00158 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00159 } 00160 00161 /* Start Advertising */ 00162 ble_gap_adv_params_t adv_para = {0}; 00163 00164 adv_para.type = params.getAdvertisingType(); 00165 adv_para.p_peer_addr = NULL; // Undirected advertisement 00166 adv_para.fp = BLE_GAP_ADV_FP_ANY; 00167 adv_para.p_whitelist = NULL; 00168 adv_para.interval = params.getInterval(); // advertising interval (in units of 0.625 ms) 00169 adv_para.timeout = params.getTimeout(); 00170 00171 ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), 00172 BLE_ERROR_PARAM_OUT_OF_RANGE); 00173 00174 state.advertising = 1; 00175 00176 return BLE_ERROR_NONE; 00177 } 00178 00179 /**************************************************************************/ 00180 /*! 00181 @brief Stops the BLE HW and disconnects from any devices 00182 00183 @returns ble_error_t 00184 00185 @retval BLE_ERROR_NONE 00186 Everything executed properly 00187 00188 @section EXAMPLE 00189 00190 @code 00191 00192 @endcode 00193 */ 00194 /**************************************************************************/ 00195 ble_error_t nRF51Gap::stopAdvertising(void) 00196 { 00197 /* Stop Advertising */ 00198 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); 00199 00200 state.advertising = 0; 00201 00202 return BLE_ERROR_NONE; 00203 } 00204 00205 /**************************************************************************/ 00206 /*! 00207 @brief Disconnects if we are connected to a central device 00208 00209 @returns ble_error_t 00210 00211 @retval BLE_ERROR_NONE 00212 Everything executed properly 00213 00214 @section EXAMPLE 00215 00216 @code 00217 00218 @endcode 00219 */ 00220 /**************************************************************************/ 00221 ble_error_t nRF51Gap::disconnect(void) 00222 { 00223 state.advertising = 0; 00224 state.connected = 0; 00225 00226 /* Disconnect if we are connected to a central device */ 00227 ASSERT_INT(ERROR_NONE, 00228 sd_ble_gap_disconnect(m_connectionHandle, 00229 BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION), 00230 BLE_ERROR_PARAM_OUT_OF_RANGE); 00231 00232 return BLE_ERROR_NONE; 00233 } 00234 00235 /**************************************************************************/ 00236 /*! 00237 @brief Sets the 16-bit connection handle 00238 */ 00239 /**************************************************************************/ 00240 void nRF51Gap::setConnectionHandle(uint16_t con_handle) 00241 { 00242 m_connectionHandle = con_handle; 00243 } 00244 00245 /**************************************************************************/ 00246 /*! 00247 @brief Gets the 16-bit connection handle 00248 */ 00249 /**************************************************************************/ 00250 uint16_t nRF51Gap::getConnectionHandle(void) 00251 { 00252 return m_connectionHandle; 00253 } 00254 00255 /**************************************************************************/ 00256 /*! 00257 @brief Sets the BLE device address 00258 00259 @returns ble_error_t 00260 00261 @section EXAMPLE 00262 00263 @code 00264 00265 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; 00266 nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); 00267 00268 @endcode 00269 */ 00270 /**************************************************************************/ 00271 ble_error_t nRF51Gap::setAddress(addr_type_t type, const uint8_t address[6]) 00272 { 00273 if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { 00274 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00275 } 00276 00277 ble_gap_addr_t dev_addr; 00278 dev_addr.addr_type = type; 00279 memcpy(dev_addr.addr, address, 6); 00280 00281 ASSERT_INT(ERROR_NONE, 00282 sd_ble_gap_address_set(&dev_addr), 00283 BLE_ERROR_PARAM_OUT_OF_RANGE); 00284 00285 return BLE_ERROR_NONE; 00286 }
Generated on Tue Jul 12 2022 19:00:52 by
