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