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 nRF51Gap &nRF51Gap::getInstance() { 00025 static nRF51Gap m_instance; 00026 return m_instance; 00027 } 00028 00029 /**************************************************************************/ 00030 /*! 00031 @brief Sets the advertising parameters and payload for the device 00032 00033 @param[in] params 00034 Basic advertising details, including the advertising 00035 delay, timeout and how the device should be advertised 00036 @params[in] advData 00037 The primary advertising data payload 00038 @params[in] scanResponse 00039 The optional Scan Response payload if the advertising 00040 type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED 00041 in \ref GapAdveritinngParams 00042 00043 @returns \ref ble_error_t 00044 00045 @retval BLE_ERROR_NONE 00046 Everything executed properly 00047 00048 @retval BLE_ERROR_BUFFER_OVERFLOW 00049 The proposed action would cause a buffer overflow. All 00050 advertising payloads must be <= 31 bytes, for example. 00051 00052 @retval BLE_ERROR_NOT_IMPLEMENTED 00053 A feature was requested that is not yet supported in the 00054 nRF51 firmware or hardware. 00055 00056 @retval BLE_ERROR_PARAM_OUT_OF_RANGE 00057 One of the proposed values is outside the valid range. 00058 00059 @section EXAMPLE 00060 00061 @code 00062 00063 @endcode 00064 */ 00065 /**************************************************************************/ 00066 ble_error_t nRF51Gap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) 00067 { 00068 /* Make sure we don't exceed the advertising payload length */ 00069 if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { 00070 return BLE_ERROR_BUFFER_OVERFLOW; 00071 } 00072 00073 /* Make sure we have a payload! */ 00074 if (advData.getPayloadLen() == 0) { 00075 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00076 } 00077 00078 /* Check the scan response payload limits */ 00079 //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)) 00080 //{ 00081 // /* Check if we're within the upper limit */ 00082 // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) 00083 // { 00084 // return BLE_ERROR_BUFFER_OVERFLOW; 00085 // } 00086 // /* Make sure we have a payload! */ 00087 // if (advData.getPayloadLen() == 0) 00088 // { 00089 // return BLE_ERROR_PARAM_OUT_OF_RANGE; 00090 // } 00091 //} 00092 00093 /* Send advertising data! */ 00094 ASSERT(ERROR_NONE == 00095 sd_ble_gap_adv_data_set(advData.getPayload(), 00096 advData.getPayloadLen(), 00097 scanResponse.getPayload(), 00098 scanResponse.getPayloadLen()), 00099 BLE_ERROR_PARAM_OUT_OF_RANGE); 00100 00101 /* Make sure the GAP Service appearance value is aligned with the 00102 *appearance from GapAdvertisingData */ 00103 ASSERT(ERROR_NONE == sd_ble_gap_appearance_set(advData.getAppearance()), 00104 BLE_ERROR_PARAM_OUT_OF_RANGE); 00105 00106 /* ToDo: Perform some checks on the payload, for example the Scan Response can't */ 00107 /* contains a flags AD type, etc. */ 00108 00109 return BLE_ERROR_NONE; 00110 } 00111 00112 /**************************************************************************/ 00113 /*! 00114 @brief Starts the BLE HW, initialising any services that were 00115 added before this function was called. 00116 00117 @note All services must be added before calling this function! 00118 00119 @returns ble_error_t 00120 00121 @retval BLE_ERROR_NONE 00122 Everything executed properly 00123 00124 @section EXAMPLE 00125 00126 @code 00127 00128 @endcode 00129 */ 00130 /**************************************************************************/ 00131 ble_error_t nRF51Gap::startAdvertising(const GapAdvertisingParams ¶ms) 00132 { 00133 /* Make sure we support the advertising type */ 00134 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { 00135 /* ToDo: This requires a propery security implementation, etc. */ 00136 return BLE_ERROR_NOT_IMPLEMENTED; 00137 } 00138 00139 /* Check interval range */ 00140 if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { 00141 /* Min delay is slightly longer for unconnectable devices */ 00142 if ((params.getInterval() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || 00143 (params.getInterval() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) { 00144 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00145 } 00146 } else { 00147 if ((params.getInterval() < GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MIN) || 00148 (params.getInterval() > GapAdvertisingParams::GAP_ADV_PARAMS_INTERVAL_MAX)) { 00149 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00150 } 00151 } 00152 00153 /* Check timeout is zero for Connectable Directed */ 00154 if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) { 00155 /* Timeout must be 0 with this type, although we'll never get here */ 00156 /* since this isn't implemented yet anyway */ 00157 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00158 } 00159 00160 /* Check timeout for other advertising types */ 00161 if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && 00162 (params.getTimeout() > GapAdvertisingParams::GAP_ADV_PARAMS_TIMEOUT_MAX)) { 00163 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00164 } 00165 00166 /* Start Advertising */ 00167 ble_gap_adv_params_t adv_para = {0}; 00168 00169 adv_para.type = params.getAdvertisingType(); 00170 adv_para.p_peer_addr = NULL; // Undirected advertisement 00171 adv_para.fp = BLE_GAP_ADV_FP_ANY; 00172 adv_para.p_whitelist = NULL; 00173 adv_para.interval = params.getInterval(); // advertising interval (in units of 0.625 ms) 00174 adv_para.timeout = params.getTimeout(); 00175 00176 ASSERT(ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE); 00177 00178 state.advertising = 1; 00179 00180 return BLE_ERROR_NONE; 00181 } 00182 00183 /**************************************************************************/ 00184 /*! 00185 @brief Stops the BLE HW and disconnects from any devices 00186 00187 @returns ble_error_t 00188 00189 @retval BLE_ERROR_NONE 00190 Everything executed properly 00191 00192 @section EXAMPLE 00193 00194 @code 00195 00196 @endcode 00197 */ 00198 /**************************************************************************/ 00199 ble_error_t nRF51Gap::stopAdvertising(void) 00200 { 00201 /* Stop Advertising */ 00202 ASSERT(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE); 00203 00204 state.advertising = 0; 00205 00206 return BLE_ERROR_NONE; 00207 } 00208 00209 /**************************************************************************/ 00210 /*! 00211 @brief Disconnects if we are connected to a central device 00212 00213 @returns ble_error_t 00214 00215 @retval BLE_ERROR_NONE 00216 Everything executed properly 00217 00218 @section EXAMPLE 00219 00220 @code 00221 00222 @endcode 00223 */ 00224 /**************************************************************************/ 00225 ble_error_t nRF51Gap::disconnect(DisconnectionReason_t reason) 00226 { 00227 state.advertising = 0; 00228 state.connected = 0; 00229 00230 uint8_t code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; 00231 switch (reason) { 00232 case REMOTE_USER_TERMINATED_CONNECTION: 00233 code = BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION; 00234 break; 00235 case CONN_INTERVAL_UNACCEPTABLE: 00236 code = BLE_HCI_CONN_INTERVAL_UNACCEPTABLE; 00237 break; 00238 } 00239 00240 /* Disconnect if we are connected to a central device */ 00241 ASSERT_INT(ERROR_NONE, sd_ble_gap_disconnect(m_connectionHandle, code), BLE_ERROR_PARAM_OUT_OF_RANGE); 00242 00243 return BLE_ERROR_NONE; 00244 } 00245 00246 ble_error_t nRF51Gap::getPreferredConnectionParams(ConnectionParams_t *params) 00247 { 00248 ASSERT_INT(NRF_SUCCESS, 00249 sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)), 00250 BLE_ERROR_PARAM_OUT_OF_RANGE); 00251 00252 return BLE_ERROR_NONE; 00253 } 00254 00255 ble_error_t nRF51Gap::setPreferredConnectionParams(const ConnectionParams_t *params) 00256 { 00257 ASSERT_INT(NRF_SUCCESS, 00258 sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)), 00259 BLE_ERROR_PARAM_OUT_OF_RANGE); 00260 00261 return BLE_ERROR_NONE; 00262 } 00263 00264 ble_error_t nRF51Gap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams) 00265 { 00266 uint32_t rc; 00267 00268 rc = sd_ble_gap_conn_param_update(handle, reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams))); 00269 if (rc == NRF_SUCCESS) { 00270 return BLE_ERROR_NONE; 00271 } else { 00272 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00273 } 00274 } 00275 00276 /**************************************************************************/ 00277 /*! 00278 @brief Sets the 16-bit connection handle 00279 */ 00280 /**************************************************************************/ 00281 void nRF51Gap::setConnectionHandle(uint16_t con_handle) 00282 { 00283 m_connectionHandle = con_handle; 00284 } 00285 00286 /**************************************************************************/ 00287 /*! 00288 @brief Gets the 16-bit connection handle 00289 */ 00290 /**************************************************************************/ 00291 uint16_t nRF51Gap::getConnectionHandle(void) 00292 { 00293 return m_connectionHandle; 00294 } 00295 00296 /**************************************************************************/ 00297 /*! 00298 @brief Sets the BLE device address 00299 00300 @returns ble_error_t 00301 00302 @section EXAMPLE 00303 00304 @code 00305 00306 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; 00307 nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); 00308 00309 @endcode 00310 */ 00311 /**************************************************************************/ 00312 ble_error_t nRF51Gap::setAddress(AddressType_t type, const address_t address) 00313 { 00314 if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { 00315 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00316 } 00317 00318 ble_gap_addr_t dev_addr; 00319 dev_addr.addr_type = type; 00320 memcpy(dev_addr.addr, address, ADDR_LEN); 00321 00322 ASSERT_INT(ERROR_NONE, sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &dev_addr), BLE_ERROR_PARAM_OUT_OF_RANGE); 00323 00324 return BLE_ERROR_NONE; 00325 } 00326 00327 ble_error_t nRF51Gap::getAddress(AddressType_t *typeP, address_t address) 00328 { 00329 ble_gap_addr_t dev_addr; 00330 if (sd_ble_gap_address_get(&dev_addr) != NRF_SUCCESS) { 00331 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00332 } 00333 00334 if (typeP != NULL) { 00335 *typeP = static_cast<AddressType_t>(dev_addr.addr_type); 00336 } 00337 if (address != NULL) { 00338 memcpy(address, dev_addr.addr, ADDR_LEN); 00339 } 00340 return BLE_ERROR_NONE; 00341 } 00342 00343 ble_error_t nRF51Gap::setDeviceName(const uint8_t *deviceName) 00344 { 00345 ble_gap_conn_sec_mode_t sec_mode; 00346 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed 00347 00348 if (sd_ble_gap_device_name_set(&sec_mode, deviceName, strlen((const char *)deviceName)) == 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::getDeviceName(uint8_t *deviceName, unsigned *lengthP) 00356 { 00357 if (sd_ble_gap_device_name_get(deviceName, (uint16_t *)lengthP) == NRF_SUCCESS) { 00358 return BLE_ERROR_NONE; 00359 } else { 00360 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00361 } 00362 } 00363 00364 ble_error_t nRF51Gap::setAppearance(uint16_t appearance) 00365 { 00366 if (sd_ble_gap_appearance_set(appearance) == NRF_SUCCESS) { 00367 return BLE_ERROR_NONE; 00368 } else { 00369 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00370 } 00371 } 00372 00373 ble_error_t nRF51Gap::getAppearance(uint16_t *appearanceP) 00374 { 00375 if (sd_ble_gap_appearance_get(appearanceP)) { 00376 return BLE_ERROR_NONE; 00377 } else { 00378 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00379 } 00380 }
Generated on Tue Jul 12 2022 19:22:46 by
1.7.2
