Changed the device name.
Dependents: BLE_Health_Thermometer_HeartRateMonitor
Fork of BLE_API_Native_IRC by
hw/nRF51822n/nRF51Gap.cpp@0:4c3097c65247, 2014-02-06 (annotated)
- Committer:
- ktownsend
- Date:
- Thu Feb 06 11:51:06 2014 +0000
- Revision:
- 0:4c3097c65247
- Child:
- 13:1800682b5703
Native mode drivers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ktownsend | 0:4c3097c65247 | 1 | /* mbed Microcontroller Library |
ktownsend | 0:4c3097c65247 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ktownsend | 0:4c3097c65247 | 3 | * |
ktownsend | 0:4c3097c65247 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ktownsend | 0:4c3097c65247 | 5 | * you may not use this file except in compliance with the License. |
ktownsend | 0:4c3097c65247 | 6 | * You may obtain a copy of the License at |
ktownsend | 0:4c3097c65247 | 7 | * |
ktownsend | 0:4c3097c65247 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ktownsend | 0:4c3097c65247 | 9 | * |
ktownsend | 0:4c3097c65247 | 10 | * Unless required by applicable law or agreed to in writing, software |
ktownsend | 0:4c3097c65247 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ktownsend | 0:4c3097c65247 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ktownsend | 0:4c3097c65247 | 13 | * See the License for the specific language governing permissions and |
ktownsend | 0:4c3097c65247 | 14 | * limitations under the License. |
ktownsend | 0:4c3097c65247 | 15 | */ |
ktownsend | 0:4c3097c65247 | 16 | |
ktownsend | 0:4c3097c65247 | 17 | #include "nRF51Gap.h" |
ktownsend | 0:4c3097c65247 | 18 | #include "mbed.h" |
ktownsend | 0:4c3097c65247 | 19 | |
ktownsend | 0:4c3097c65247 | 20 | #include "common/common.h" |
ktownsend | 0:4c3097c65247 | 21 | #include "ble_advdata.h" |
ktownsend | 0:4c3097c65247 | 22 | |
ktownsend | 0:4c3097c65247 | 23 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 24 | /*! |
ktownsend | 0:4c3097c65247 | 25 | @brief Sets the advertising parameters and payload for the device |
ktownsend | 0:4c3097c65247 | 26 | |
ktownsend | 0:4c3097c65247 | 27 | @param[in] params |
ktownsend | 0:4c3097c65247 | 28 | Basic advertising details, including the advertising |
ktownsend | 0:4c3097c65247 | 29 | delay, timeout and how the device should be advertised |
ktownsend | 0:4c3097c65247 | 30 | @params[in] advData |
ktownsend | 0:4c3097c65247 | 31 | The primary advertising data payload |
ktownsend | 0:4c3097c65247 | 32 | @params[in] scanResponse |
ktownsend | 0:4c3097c65247 | 33 | The optional Scan Response payload if the advertising |
ktownsend | 0:4c3097c65247 | 34 | type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED |
ktownsend | 0:4c3097c65247 | 35 | in \ref GapAdveritinngParams |
ktownsend | 0:4c3097c65247 | 36 | |
ktownsend | 0:4c3097c65247 | 37 | @returns \ref ble_error_t |
ktownsend | 0:4c3097c65247 | 38 | |
ktownsend | 0:4c3097c65247 | 39 | @retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 40 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 41 | |
ktownsend | 0:4c3097c65247 | 42 | @retval BLE_ERROR_BUFFER_OVERFLOW |
ktownsend | 0:4c3097c65247 | 43 | The proposed action would cause a buffer overflow. All |
ktownsend | 0:4c3097c65247 | 44 | advertising payloads must be <= 31 bytes, for example. |
ktownsend | 0:4c3097c65247 | 45 | |
ktownsend | 0:4c3097c65247 | 46 | @retval BLE_ERROR_NOT_IMPLEMENTED |
ktownsend | 0:4c3097c65247 | 47 | A feature was requested that is not yet supported in the |
ktownsend | 0:4c3097c65247 | 48 | nRF51 firmware or hardware. |
ktownsend | 0:4c3097c65247 | 49 | |
ktownsend | 0:4c3097c65247 | 50 | @retval BLE_ERROR_PARAM_OUT_OF_RANGE |
ktownsend | 0:4c3097c65247 | 51 | One of the proposed values is outside the valid range. |
ktownsend | 0:4c3097c65247 | 52 | |
ktownsend | 0:4c3097c65247 | 53 | @section EXAMPLE |
ktownsend | 0:4c3097c65247 | 54 | |
ktownsend | 0:4c3097c65247 | 55 | @code |
ktownsend | 0:4c3097c65247 | 56 | |
ktownsend | 0:4c3097c65247 | 57 | @endcode |
ktownsend | 0:4c3097c65247 | 58 | */ |
ktownsend | 0:4c3097c65247 | 59 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 60 | ble_error_t nRF51Gap::setAdvertisingData(GapAdvertisingData & advData, GapAdvertisingData & scanResponse) |
ktownsend | 0:4c3097c65247 | 61 | { |
ktownsend | 0:4c3097c65247 | 62 | /* Make sure we don't exceed the advertising payload length */ |
ktownsend | 0:4c3097c65247 | 63 | if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) |
ktownsend | 0:4c3097c65247 | 64 | { |
ktownsend | 0:4c3097c65247 | 65 | return BLE_ERROR_BUFFER_OVERFLOW; |
ktownsend | 0:4c3097c65247 | 66 | } |
ktownsend | 0:4c3097c65247 | 67 | |
ktownsend | 0:4c3097c65247 | 68 | /* Make sure we have a payload! */ |
ktownsend | 0:4c3097c65247 | 69 | if (advData.getPayloadLen() == 0) |
ktownsend | 0:4c3097c65247 | 70 | { |
ktownsend | 0:4c3097c65247 | 71 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
ktownsend | 0:4c3097c65247 | 72 | } |
ktownsend | 0:4c3097c65247 | 73 | |
ktownsend | 0:4c3097c65247 | 74 | /* Check the scan response payload limits */ |
ktownsend | 0:4c3097c65247 | 75 | //if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED)) |
ktownsend | 0:4c3097c65247 | 76 | //{ |
ktownsend | 0:4c3097c65247 | 77 | // /* Check if we're within the upper limit */ |
ktownsend | 0:4c3097c65247 | 78 | // if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) |
ktownsend | 0:4c3097c65247 | 79 | // { |
ktownsend | 0:4c3097c65247 | 80 | // return BLE_ERROR_BUFFER_OVERFLOW; |
ktownsend | 0:4c3097c65247 | 81 | // } |
ktownsend | 0:4c3097c65247 | 82 | // /* Make sure we have a payload! */ |
ktownsend | 0:4c3097c65247 | 83 | // if (advData.getPayloadLen() == 0) |
ktownsend | 0:4c3097c65247 | 84 | // { |
ktownsend | 0:4c3097c65247 | 85 | // return BLE_ERROR_PARAM_OUT_OF_RANGE; |
ktownsend | 0:4c3097c65247 | 86 | // } |
ktownsend | 0:4c3097c65247 | 87 | //} |
ktownsend | 0:4c3097c65247 | 88 | |
ktownsend | 0:4c3097c65247 | 89 | /* ToDo: Send advertising data! */ |
ktownsend | 0:4c3097c65247 | 90 | ASSERT( ERROR_NONE == sd_ble_gap_adv_data_set(advData.getPayload(), advData.getPayloadLen(), |
ktownsend | 0:4c3097c65247 | 91 | scanResponse.getPayload(), scanResponse.getPayloadLen()), BLE_ERROR_PARAM_OUT_OF_RANGE); |
ktownsend | 0:4c3097c65247 | 92 | |
ktownsend | 0:4c3097c65247 | 93 | /* ToDo: Perform some checks on the payload, for example the Scan Response can't */ |
ktownsend | 0:4c3097c65247 | 94 | /* contains a flags AD type, etc. */ |
ktownsend | 0:4c3097c65247 | 95 | |
ktownsend | 0:4c3097c65247 | 96 | return BLE_ERROR_NONE; |
ktownsend | 0:4c3097c65247 | 97 | } |
ktownsend | 0:4c3097c65247 | 98 | |
ktownsend | 0:4c3097c65247 | 99 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 100 | /*! |
ktownsend | 0:4c3097c65247 | 101 | @brief Starts the BLE HW, initialising any services that were |
ktownsend | 0:4c3097c65247 | 102 | added before this function was called. |
ktownsend | 0:4c3097c65247 | 103 | |
ktownsend | 0:4c3097c65247 | 104 | @note All services must be added before calling this function! |
ktownsend | 0:4c3097c65247 | 105 | |
ktownsend | 0:4c3097c65247 | 106 | @returns ble_error_t |
ktownsend | 0:4c3097c65247 | 107 | |
ktownsend | 0:4c3097c65247 | 108 | @retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 109 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 110 | |
ktownsend | 0:4c3097c65247 | 111 | @section EXAMPLE |
ktownsend | 0:4c3097c65247 | 112 | |
ktownsend | 0:4c3097c65247 | 113 | @code |
ktownsend | 0:4c3097c65247 | 114 | |
ktownsend | 0:4c3097c65247 | 115 | @endcode |
ktownsend | 0:4c3097c65247 | 116 | */ |
ktownsend | 0:4c3097c65247 | 117 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 118 | ble_error_t nRF51Gap::startAdvertising(GapAdvertisingParams & params) |
ktownsend | 0:4c3097c65247 | 119 | { |
ktownsend | 0:4c3097c65247 | 120 | /* Make sure we support the advertising type */ |
ktownsend | 0:4c3097c65247 | 121 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) |
ktownsend | 0:4c3097c65247 | 122 | { |
ktownsend | 0:4c3097c65247 | 123 | /* ToDo: This requires a propery security implementation, etc. */ |
ktownsend | 0:4c3097c65247 | 124 | return BLE_ERROR_NOT_IMPLEMENTED; |
ktownsend | 0:4c3097c65247 | 125 | } |
ktownsend | 0:4c3097c65247 | 126 | |
ktownsend | 0:4c3097c65247 | 127 | /* Check interval range */ |
ktownsend | 0:4c3097c65247 | 128 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) |
ktownsend | 0:4c3097c65247 | 129 | { |
ktownsend | 0:4c3097c65247 | 130 | /* Min delay is slightly longer for unconnectable devices */ |
ktownsend | 0:4c3097c65247 | 131 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || |
ktownsend | 0:4c3097c65247 | 132 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) |
ktownsend | 0:4c3097c65247 | 133 | { |
ktownsend | 0:4c3097c65247 | 134 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
ktownsend | 0:4c3097c65247 | 135 | } |
ktownsend | 0:4c3097c65247 | 136 | } |
ktownsend | 0:4c3097c65247 | 137 | else |
ktownsend | 0:4c3097c65247 | 138 | { |
ktownsend | 0:4c3097c65247 | 139 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || |
ktownsend | 0:4c3097c65247 | 140 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) |
ktownsend | 0:4c3097c65247 | 141 | { |
ktownsend | 0:4c3097c65247 | 142 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
ktownsend | 0:4c3097c65247 | 143 | } |
ktownsend | 0:4c3097c65247 | 144 | } |
ktownsend | 0:4c3097c65247 | 145 | |
ktownsend | 0:4c3097c65247 | 146 | /* Check timeout is zero for Connectable Directed */ |
ktownsend | 0:4c3097c65247 | 147 | if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && |
ktownsend | 0:4c3097c65247 | 148 | (params.getTimeout() != 0)) |
ktownsend | 0:4c3097c65247 | 149 | { |
ktownsend | 0:4c3097c65247 | 150 | /* Timeout must be 0 with this type, although we'll never get here */ |
ktownsend | 0:4c3097c65247 | 151 | /* since this isn't implemented yet anyway */ |
ktownsend | 0:4c3097c65247 | 152 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
ktownsend | 0:4c3097c65247 | 153 | } |
ktownsend | 0:4c3097c65247 | 154 | |
ktownsend | 0:4c3097c65247 | 155 | /* Check timeout for other advertising types */ |
ktownsend | 0:4c3097c65247 | 156 | if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && |
ktownsend | 0:4c3097c65247 | 157 | (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) |
ktownsend | 0:4c3097c65247 | 158 | { |
ktownsend | 0:4c3097c65247 | 159 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
ktownsend | 0:4c3097c65247 | 160 | } |
ktownsend | 0:4c3097c65247 | 161 | |
ktownsend | 0:4c3097c65247 | 162 | /* ToDo: Start Advertising */ |
ktownsend | 0:4c3097c65247 | 163 | ble_gap_adv_params_t adv_para = { 0 }; |
ktownsend | 0:4c3097c65247 | 164 | |
ktownsend | 0:4c3097c65247 | 165 | adv_para.type = params.getAdvertisingType() ; |
ktownsend | 0:4c3097c65247 | 166 | adv_para.p_peer_addr = NULL ; // Undirected advertisement |
ktownsend | 0:4c3097c65247 | 167 | adv_para.fp = BLE_GAP_ADV_FP_ANY ; |
ktownsend | 0:4c3097c65247 | 168 | adv_para.p_whitelist = NULL ; |
ktownsend | 0:4c3097c65247 | 169 | adv_para.interval = params.getInterval() ; // advertising interval (in units of 0.625 ms) |
ktownsend | 0:4c3097c65247 | 170 | adv_para.timeout = params.getTimeout() ; |
ktownsend | 0:4c3097c65247 | 171 | |
ktownsend | 0:4c3097c65247 | 172 | ASSERT( ERROR_NONE == sd_ble_gap_adv_start(&adv_para), BLE_ERROR_PARAM_OUT_OF_RANGE); |
ktownsend | 0:4c3097c65247 | 173 | |
ktownsend | 0:4c3097c65247 | 174 | state.advertising = 1; |
ktownsend | 0:4c3097c65247 | 175 | |
ktownsend | 0:4c3097c65247 | 176 | return BLE_ERROR_NONE; |
ktownsend | 0:4c3097c65247 | 177 | } |
ktownsend | 0:4c3097c65247 | 178 | |
ktownsend | 0:4c3097c65247 | 179 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 180 | /*! |
ktownsend | 0:4c3097c65247 | 181 | @brief Stops the BLE HW and disconnects from any devices |
ktownsend | 0:4c3097c65247 | 182 | |
ktownsend | 0:4c3097c65247 | 183 | @returns ble_error_t |
ktownsend | 0:4c3097c65247 | 184 | |
ktownsend | 0:4c3097c65247 | 185 | @retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 186 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 187 | |
ktownsend | 0:4c3097c65247 | 188 | @section EXAMPLE |
ktownsend | 0:4c3097c65247 | 189 | |
ktownsend | 0:4c3097c65247 | 190 | @code |
ktownsend | 0:4c3097c65247 | 191 | |
ktownsend | 0:4c3097c65247 | 192 | @endcode |
ktownsend | 0:4c3097c65247 | 193 | */ |
ktownsend | 0:4c3097c65247 | 194 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 195 | ble_error_t nRF51Gap::stopAdvertising(void) |
ktownsend | 0:4c3097c65247 | 196 | { |
ktownsend | 0:4c3097c65247 | 197 | /* ToDo: Stop Advertising */ |
ktownsend | 0:4c3097c65247 | 198 | |
ktownsend | 0:4c3097c65247 | 199 | /* ToDo: Check response */ |
ktownsend | 0:4c3097c65247 | 200 | wait(0.1); |
ktownsend | 0:4c3097c65247 | 201 | |
ktownsend | 0:4c3097c65247 | 202 | state.advertising = 0; |
ktownsend | 0:4c3097c65247 | 203 | |
ktownsend | 0:4c3097c65247 | 204 | return BLE_ERROR_NONE; |
ktownsend | 0:4c3097c65247 | 205 | } |
ktownsend | 0:4c3097c65247 | 206 | |
ktownsend | 0:4c3097c65247 | 207 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 208 | /*! |
ktownsend | 0:4c3097c65247 | 209 | @brief Disconnects if we are connected to a central device |
ktownsend | 0:4c3097c65247 | 210 | |
ktownsend | 0:4c3097c65247 | 211 | @returns ble_error_t |
ktownsend | 0:4c3097c65247 | 212 | |
ktownsend | 0:4c3097c65247 | 213 | @retval BLE_ERROR_NONE |
ktownsend | 0:4c3097c65247 | 214 | Everything executed properly |
ktownsend | 0:4c3097c65247 | 215 | |
ktownsend | 0:4c3097c65247 | 216 | @section EXAMPLE |
ktownsend | 0:4c3097c65247 | 217 | |
ktownsend | 0:4c3097c65247 | 218 | @code |
ktownsend | 0:4c3097c65247 | 219 | |
ktownsend | 0:4c3097c65247 | 220 | @endcode |
ktownsend | 0:4c3097c65247 | 221 | */ |
ktownsend | 0:4c3097c65247 | 222 | /**************************************************************************/ |
ktownsend | 0:4c3097c65247 | 223 | ble_error_t nRF51Gap::disconnect(void) |
ktownsend | 0:4c3097c65247 | 224 | { |
ktownsend | 0:4c3097c65247 | 225 | /* ToDo: Disconnect if we are connected to a central device */ |
ktownsend | 0:4c3097c65247 | 226 | |
ktownsend | 0:4c3097c65247 | 227 | state.advertising = 0; |
ktownsend | 0:4c3097c65247 | 228 | state.connected = 0; |
ktownsend | 0:4c3097c65247 | 229 | |
ktownsend | 0:4c3097c65247 | 230 | return BLE_ERROR_NONE; |
ktownsend | 0:4c3097c65247 | 231 | } |