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