robo 8080
/
BLE_TEST_konashi
konashi/SBBLEのテスト
Fork of BLE_LoopbackUART by
Embed:
(wiki syntax)
Show/hide line numbers
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, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION), BLE_ERROR_PARAM_OUT_OF_RANGE); 00229 00230 return BLE_ERROR_NONE; 00231 } 00232 00233 ble_error_t nRF51Gap::getPreferredConnectionParams(ConnectionParams_t *params) 00234 { 00235 ASSERT_INT(NRF_SUCCESS, 00236 sd_ble_gap_ppcp_get(reinterpret_cast<ble_gap_conn_params_t *>(params)), 00237 BLE_ERROR_PARAM_OUT_OF_RANGE); 00238 00239 return BLE_ERROR_NONE; 00240 } 00241 00242 ble_error_t nRF51Gap::setPreferredConnectionParams(const ConnectionParams_t *params) 00243 { 00244 ASSERT_INT(NRF_SUCCESS, 00245 sd_ble_gap_ppcp_set(reinterpret_cast<const ble_gap_conn_params_t *>(params)), 00246 BLE_ERROR_PARAM_OUT_OF_RANGE); 00247 00248 return BLE_ERROR_NONE; 00249 } 00250 00251 ble_error_t nRF51Gap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *newParams) 00252 { 00253 uint32_t rc; 00254 00255 rc = sd_ble_gap_conn_param_update(handle, 00256 reinterpret_cast<ble_gap_conn_params_t *>(const_cast<ConnectionParams_t*>(newParams))); 00257 if (rc == NRF_SUCCESS) { 00258 return BLE_ERROR_NONE; 00259 } else { 00260 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00261 } 00262 } 00263 00264 /**************************************************************************/ 00265 /*! 00266 @brief Sets the 16-bit connection handle 00267 */ 00268 /**************************************************************************/ 00269 void nRF51Gap::setConnectionHandle(uint16_t con_handle) 00270 { 00271 m_connectionHandle = con_handle; 00272 } 00273 00274 /**************************************************************************/ 00275 /*! 00276 @brief Gets the 16-bit connection handle 00277 */ 00278 /**************************************************************************/ 00279 uint16_t nRF51Gap::getConnectionHandle(void) 00280 { 00281 return m_connectionHandle; 00282 } 00283 00284 /**************************************************************************/ 00285 /*! 00286 @brief Sets the BLE device address 00287 00288 @returns ble_error_t 00289 00290 @section EXAMPLE 00291 00292 @code 00293 00294 uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; 00295 nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); 00296 00297 @endcode 00298 */ 00299 /**************************************************************************/ 00300 ble_error_t nRF51Gap::setAddress(addr_type_t type, const uint8_t address[6]) 00301 { 00302 if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { 00303 return BLE_ERROR_PARAM_OUT_OF_RANGE; 00304 } 00305 00306 ble_gap_addr_t dev_addr; 00307 dev_addr.addr_type = type; 00308 memcpy(dev_addr.addr, address, 6); 00309 00310 ASSERT_INT(ERROR_NONE, 00311 sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &dev_addr), 00312 BLE_ERROR_PARAM_OUT_OF_RANGE); 00313 00314 00315 return BLE_ERROR_NONE; 00316 }
Generated on Tue Jul 12 2022 20:35:46 by 1.7.2