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