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
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), BLE_ERROR_PARAM_OUT_OF_RANGE); 00172 00173 state.advertising = 1; 00174 00175 return BLE_ERROR_NONE; 00176 } 00177 00178 /**************************************************************************/ 00179 /*! 00180 @brief Stops the BLE HW and disconnects from any devices 00181 00182 @returns ble_error_t 00183 00184 @retval BLE_ERROR_NONE 00185 Everything executed properly 00186 00187 @section EXAMPLE 00188 00189 @code 00190 00191 @endcode 00192 */ 00193 /**************************************************************************/ 00194 ble_error_t nRF51Gap::stopAdvertising(void) 00195 { 00196 /* Stop Advertising */ 00197 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); 00198 00199 state.advertising = 0; 00200 00201 return BLE_ERROR_NONE; 00202 } 00203 00204 /**************************************************************************/ 00205 /*! 00206 @brief Disconnects if we are connected to a central device 00207 00208 @returns ble_error_t 00209 00210 @retval BLE_ERROR_NONE 00211 Everything executed properly 00212 00213 @section EXAMPLE 00214 00215 @code 00216 00217 @endcode 00218 */ 00219 /**************************************************************************/ 00220 ble_error_t nRF51Gap::disconnect(DisconnectionReason_t reason) 00221 { 00222 state.advertising = 0; 00223 state.connected = 0; 00224 00225 uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; 00226 switch (reason) { 00227 case REMOTE_USER_TERMINATED_CONNECTION: 00228 code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; 00229 break; 00230 case CONN_INTERVAL_UNACCEPTABLE: 00231 code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE; 00232 break; 00233 } 00234 00235 /* Disconnect if we are connected to a central device */ 00236 ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(m_connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE); 00237 00238 return BLE_ERROR_NONE; 00239 } 00240 00241 ble_error_t nRF51Gap::getPreferredConnectionParams(ConnectionParams_t *params) 00242 { 00243 ASSERT_INT(NRF_SUCCESS, 00244 sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)), 00245 BLE_ERROR_PARAM_OUT_OF_RANGE); 00246 00247 return BLE_ERROR_NONE; 00248 } 00249 00250 ble_error_t nRF51Gap::setPreferredConnectionParams(const ConnectionParams_t *params) 00251 { 00252 ASSERT_INT(NRF_SUCCESS, 00253 sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)), 00254 BLE_ERROR_PARAM_OUT_OF_RANGE); 00255 00256 return BLE_ERROR_NONE; 00257 } 00258 00259 ble_error_t nRF51Gap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams) 00260 { 00261 uint32_t rc; 00262 00263 rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams))); 00264 if (rc == NRF_SUCCESS) { 00265 return BLE_ERROR_NONE; 00266 } else { 00267 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00268 } 00269 } 00270 00271 /**************************************************************************/ 00272 /*! 00273 @brief Sets the 16-bit connection handle 00274 */ 00275 /**************************************************************************/ 00276 void nRF51Gap::setConnectionHandle(uint16_t con_handle) 00277 { 00278 m_connectionHandle = con_handle; 00279 } 00280 00281 /**************************************************************************/ 00282 /*! 00283 @brief Gets the 16-bit connection handle 00284 */ 00285 /**************************************************************************/ 00286 uint16_t nRF51Gap::getConnectionHandle(void) 00287 { 00288 return m_connectionHandle; 00289 } 00290 00291 /**************************************************************************/ 00292 /*! 00293 @brief Sets the BLE device address 00294 00295 @returns ble_error_t 00296 00297 @section EXAMPLE 00298 00299 @code 00300 00301 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; 00302 nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); 00303 00304 @endcode 00305 */ 00306 /**************************************************************************/ 00307 ble_error_t nRF51Gap::setAddress(addr_type_t type, const uint8_t address[6]) 00308 { 00309 if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { 00310 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00311 } 00312 00313 ble_gap_addr_t dev_addr; 00314 dev_addr.addr_type = type; 00315 memcpy(dev_addr.addr, address, 6); 00316 00317 ASSERT_INT(ERROR_NONE, 00318 sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &dev_addr), 00319 BLE_ERROR_PARAM_OUT_OF_RANGE); 00320 00321 00322 return BLE_ERROR_NONE; 00323 } 00324 00325 ble_error_t nRF51Gap::setDeviceName(const uint8_t *deviceName) 00326 { 00327 ble_gap_conn_sec_mode_t sec_mode; 00328 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed 00329 00330 if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == NRF_SUCCESS) { 00331 return BLE_ERROR_NONE; 00332 } else { 00333 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00334 } 00335 } 00336 00337 ble_error_t nRF51Gap::getDeviceName(uint8_t *deviceName, unsigned *lengthP) 00338 { 00339 if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) { 00340 return BLE_ERROR_NONE; 00341 } else { 00342 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00343 } 00344 } 00345 00346 ble_error_t nRF51Gap::setAppearance(uint16_t appearance) 00347 { 00348 if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) { 00349 return BLE_ERROR_NONE; 00350 } else { 00351 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00352 } 00353 } 00354 00355 ble_error_t nRF51Gap::getAppearance(uint16_t *appearanceP) 00356 { 00357 if (sd_ble_gap_appearance_get(appearanceP)) { 00358 return BLE_ERROR_NONE; 00359 } else { 00360 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00361 } 00362 }
Generated on Tue Jul 12 2022 13:52:31 by
