HW layer for the Nucleo board, it only work with old BLE_API
Dependents: Hello_BLE F446RE-BLE
Fork of X_NUCLEO_IDB0XA1 by
BlueNRGGap.cpp@2:a2b623661316, 2014-07-21 (annotated)
- Committer:
- mridup
- Date:
- Mon Jul 21 08:28:47 2014 +0000
- Revision:
- 2:a2b623661316
- Child:
- 3:9c4c13795643
Latest Changes 21/07
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 2:a2b623661316 | 1 | /* mbed Microcontroller Library |
mridup | 2:a2b623661316 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mridup | 2:a2b623661316 | 3 | * |
mridup | 2:a2b623661316 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mridup | 2:a2b623661316 | 5 | * you may not use this file except in compliance with the License. |
mridup | 2:a2b623661316 | 6 | * You may obtain a copy of the License at |
mridup | 2:a2b623661316 | 7 | * |
mridup | 2:a2b623661316 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mridup | 2:a2b623661316 | 9 | * |
mridup | 2:a2b623661316 | 10 | * Unless required by applicable law or agreed to in writing, software |
mridup | 2:a2b623661316 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mridup | 2:a2b623661316 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mridup | 2:a2b623661316 | 13 | * See the License for the specific language governing permissions and |
mridup | 2:a2b623661316 | 14 | * limitations under the License. |
mridup | 2:a2b623661316 | 15 | */ |
mridup | 2:a2b623661316 | 16 | |
mridup | 2:a2b623661316 | 17 | #include "BlueNRGDevice.h" |
mridup | 2:a2b623661316 | 18 | #include "mbed.h" |
mridup | 2:a2b623661316 | 19 | |
mridup | 2:a2b623661316 | 20 | |
mridup | 2:a2b623661316 | 21 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 22 | /*! |
mridup | 2:a2b623661316 | 23 | @brief Sets the advertising parameters and payload for the device |
mridup | 2:a2b623661316 | 24 | |
mridup | 2:a2b623661316 | 25 | @param[in] params |
mridup | 2:a2b623661316 | 26 | Basic advertising details, including the advertising |
mridup | 2:a2b623661316 | 27 | delay, timeout and how the device should be advertised |
mridup | 2:a2b623661316 | 28 | @params[in] advData |
mridup | 2:a2b623661316 | 29 | The primary advertising data payload |
mridup | 2:a2b623661316 | 30 | @params[in] scanResponse |
mridup | 2:a2b623661316 | 31 | The optional Scan Response payload if the advertising |
mridup | 2:a2b623661316 | 32 | type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED |
mridup | 2:a2b623661316 | 33 | in \ref GapAdveritinngParams |
mridup | 2:a2b623661316 | 34 | |
mridup | 2:a2b623661316 | 35 | @returns \ref ble_error_t |
mridup | 2:a2b623661316 | 36 | |
mridup | 2:a2b623661316 | 37 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 38 | Everything executed properly |
mridup | 2:a2b623661316 | 39 | |
mridup | 2:a2b623661316 | 40 | @retval BLE_ERROR_BUFFER_OVERFLOW |
mridup | 2:a2b623661316 | 41 | The proposed action would cause a buffer overflow. All |
mridup | 2:a2b623661316 | 42 | advertising payloads must be <= 31 bytes, for example. |
mridup | 2:a2b623661316 | 43 | |
mridup | 2:a2b623661316 | 44 | @retval BLE_ERROR_NOT_IMPLEMENTED |
mridup | 2:a2b623661316 | 45 | A feature was requested that is not yet supported in the |
mridup | 2:a2b623661316 | 46 | nRF51 firmware or hardware. |
mridup | 2:a2b623661316 | 47 | |
mridup | 2:a2b623661316 | 48 | @retval BLE_ERROR_PARAM_OUT_OF_RANGE |
mridup | 2:a2b623661316 | 49 | One of the proposed values is outside the valid range. |
mridup | 2:a2b623661316 | 50 | |
mridup | 2:a2b623661316 | 51 | @section EXAMPLE |
mridup | 2:a2b623661316 | 52 | |
mridup | 2:a2b623661316 | 53 | @code |
mridup | 2:a2b623661316 | 54 | |
mridup | 2:a2b623661316 | 55 | @endcode |
mridup | 2:a2b623661316 | 56 | */ |
mridup | 2:a2b623661316 | 57 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 58 | ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) |
mridup | 2:a2b623661316 | 59 | { |
mridup | 2:a2b623661316 | 60 | /* Make sure we don't exceed the advertising payload length */ |
mridup | 2:a2b623661316 | 61 | if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { |
mridup | 2:a2b623661316 | 62 | return BLE_ERROR_BUFFER_OVERFLOW; |
mridup | 2:a2b623661316 | 63 | } |
mridup | 2:a2b623661316 | 64 | |
mridup | 2:a2b623661316 | 65 | /* Make sure we have a payload! */ |
mridup | 2:a2b623661316 | 66 | if (advData.getPayloadLen() == 0) { |
mridup | 2:a2b623661316 | 67 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 68 | } |
mridup | 2:a2b623661316 | 69 | |
mridup | 2:a2b623661316 | 70 | |
mridup | 2:a2b623661316 | 71 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 72 | } |
mridup | 2:a2b623661316 | 73 | |
mridup | 2:a2b623661316 | 74 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 75 | /*! |
mridup | 2:a2b623661316 | 76 | @brief Starts the BLE HW, initialising any services that were |
mridup | 2:a2b623661316 | 77 | added before this function was called. |
mridup | 2:a2b623661316 | 78 | |
mridup | 2:a2b623661316 | 79 | @note All services must be added before calling this function! |
mridup | 2:a2b623661316 | 80 | |
mridup | 2:a2b623661316 | 81 | @returns ble_error_t |
mridup | 2:a2b623661316 | 82 | |
mridup | 2:a2b623661316 | 83 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 84 | Everything executed properly |
mridup | 2:a2b623661316 | 85 | |
mridup | 2:a2b623661316 | 86 | @section EXAMPLE |
mridup | 2:a2b623661316 | 87 | |
mridup | 2:a2b623661316 | 88 | @code |
mridup | 2:a2b623661316 | 89 | |
mridup | 2:a2b623661316 | 90 | @endcode |
mridup | 2:a2b623661316 | 91 | */ |
mridup | 2:a2b623661316 | 92 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 93 | ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams ¶ms) |
mridup | 2:a2b623661316 | 94 | { |
mridup | 2:a2b623661316 | 95 | /* Make sure we support the advertising type */ |
mridup | 2:a2b623661316 | 96 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { |
mridup | 2:a2b623661316 | 97 | /* ToDo: This requires a propery security implementation, etc. */ |
mridup | 2:a2b623661316 | 98 | return BLE_ERROR_NOT_IMPLEMENTED; |
mridup | 2:a2b623661316 | 99 | } |
mridup | 2:a2b623661316 | 100 | |
mridup | 2:a2b623661316 | 101 | /* Check interval range */ |
mridup | 2:a2b623661316 | 102 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { |
mridup | 2:a2b623661316 | 103 | /* Min delay is slightly longer for unconnectable devices */ |
mridup | 2:a2b623661316 | 104 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || |
mridup | 2:a2b623661316 | 105 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { |
mridup | 2:a2b623661316 | 106 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 107 | } |
mridup | 2:a2b623661316 | 108 | } else { |
mridup | 2:a2b623661316 | 109 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || |
mridup | 2:a2b623661316 | 110 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { |
mridup | 2:a2b623661316 | 111 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 112 | } |
mridup | 2:a2b623661316 | 113 | } |
mridup | 2:a2b623661316 | 114 | |
mridup | 2:a2b623661316 | 115 | /* Check timeout is zero for Connectable Directed */ |
mridup | 2:a2b623661316 | 116 | if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) { |
mridup | 2:a2b623661316 | 117 | /* Timeout must be 0 with this type, although we'll never get here */ |
mridup | 2:a2b623661316 | 118 | /* since this isn't implemented yet anyway */ |
mridup | 2:a2b623661316 | 119 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 120 | } |
mridup | 2:a2b623661316 | 121 | |
mridup | 2:a2b623661316 | 122 | /* Check timeout for other advertising types */ |
mridup | 2:a2b623661316 | 123 | if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && |
mridup | 2:a2b623661316 | 124 | (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) { |
mridup | 2:a2b623661316 | 125 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 126 | } |
mridup | 2:a2b623661316 | 127 | |
mridup | 2:a2b623661316 | 128 | tBleStatus ret; |
mridup | 2:a2b623661316 | 129 | |
mridup | 2:a2b623661316 | 130 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'}; |
mridup | 2:a2b623661316 | 131 | |
mridup | 2:a2b623661316 | 132 | /* disable scan response */ |
mridup | 2:a2b623661316 | 133 | hci_le_set_scan_resp_data(0,NULL); |
mridup | 2:a2b623661316 | 134 | |
mridup | 2:a2b623661316 | 135 | /*aci_gap_set_discoverable(Advertising_Event_Type, Adv_min_intvl, Adv_Max_Intvl, Addr_Type, Adv_Filter_Policy, |
mridup | 2:a2b623661316 | 136 | Local_Name_Length, local_name, service_uuid_length, service_uuid_list, Slave_conn_intvl_min, Slave_conn_intvl_max);*/ |
mridup | 2:a2b623661316 | 137 | /*LINK_LAYER.H DESCRIBES THE ADVERTISING TYPES*/ |
mridup | 2:a2b623661316 | 138 | |
mridup | 2:a2b623661316 | 139 | ret = aci_gap_set_discoverable(params.getAdvertisingType(), params.getInterval(), 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
mridup | 2:a2b623661316 | 140 | 8, local_name, 0, NULL, 0, 0); |
mridup | 2:a2b623661316 | 141 | |
mridup | 2:a2b623661316 | 142 | |
mridup | 2:a2b623661316 | 143 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 144 | } |
mridup | 2:a2b623661316 | 145 | |
mridup | 2:a2b623661316 | 146 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 147 | /*! |
mridup | 2:a2b623661316 | 148 | @brief Stops the BLE HW and disconnects from any devices |
mridup | 2:a2b623661316 | 149 | |
mridup | 2:a2b623661316 | 150 | @returns ble_error_t |
mridup | 2:a2b623661316 | 151 | |
mridup | 2:a2b623661316 | 152 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 153 | Everything executed properly |
mridup | 2:a2b623661316 | 154 | |
mridup | 2:a2b623661316 | 155 | @section EXAMPLE |
mridup | 2:a2b623661316 | 156 | |
mridup | 2:a2b623661316 | 157 | @code |
mridup | 2:a2b623661316 | 158 | |
mridup | 2:a2b623661316 | 159 | @endcode |
mridup | 2:a2b623661316 | 160 | */ |
mridup | 2:a2b623661316 | 161 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 162 | ble_error_t BlueNRGGap::stopAdvertising(void) |
mridup | 2:a2b623661316 | 163 | { |
mridup | 2:a2b623661316 | 164 | |
mridup | 2:a2b623661316 | 165 | |
mridup | 2:a2b623661316 | 166 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 167 | } |
mridup | 2:a2b623661316 | 168 | |
mridup | 2:a2b623661316 | 169 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 170 | /*! |
mridup | 2:a2b623661316 | 171 | @brief Disconnects if we are connected to a central device |
mridup | 2:a2b623661316 | 172 | |
mridup | 2:a2b623661316 | 173 | @returns ble_error_t |
mridup | 2:a2b623661316 | 174 | |
mridup | 2:a2b623661316 | 175 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 176 | Everything executed properly |
mridup | 2:a2b623661316 | 177 | |
mridup | 2:a2b623661316 | 178 | @section EXAMPLE |
mridup | 2:a2b623661316 | 179 | |
mridup | 2:a2b623661316 | 180 | @code |
mridup | 2:a2b623661316 | 181 | |
mridup | 2:a2b623661316 | 182 | @endcode |
mridup | 2:a2b623661316 | 183 | */ |
mridup | 2:a2b623661316 | 184 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 185 | ble_error_t BlueNRGGap::disconnect(void) |
mridup | 2:a2b623661316 | 186 | { |
mridup | 2:a2b623661316 | 187 | |
mridup | 2:a2b623661316 | 188 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 189 | } |
mridup | 2:a2b623661316 | 190 | |
mridup | 2:a2b623661316 | 191 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 192 | /*! |
mridup | 2:a2b623661316 | 193 | @brief Sets the 16-bit connection handle |
mridup | 2:a2b623661316 | 194 | */ |
mridup | 2:a2b623661316 | 195 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 196 | void BlueNRGGap::setConnectionHandle(uint16_t con_handle) |
mridup | 2:a2b623661316 | 197 | { |
mridup | 2:a2b623661316 | 198 | m_connectionHandle = con_handle; |
mridup | 2:a2b623661316 | 199 | } |
mridup | 2:a2b623661316 | 200 | |
mridup | 2:a2b623661316 | 201 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 202 | /*! |
mridup | 2:a2b623661316 | 203 | @brief Gets the 16-bit connection handle |
mridup | 2:a2b623661316 | 204 | */ |
mridup | 2:a2b623661316 | 205 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 206 | uint16_t BlueNRGGap::getConnectionHandle(void) |
mridup | 2:a2b623661316 | 207 | { |
mridup | 2:a2b623661316 | 208 | return m_connectionHandle; |
mridup | 2:a2b623661316 | 209 | } |
mridup | 2:a2b623661316 | 210 | |
mridup | 2:a2b623661316 | 211 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 212 | /*! |
mridup | 2:a2b623661316 | 213 | @brief Sets the BLE device address |
mridup | 2:a2b623661316 | 214 | |
mridup | 2:a2b623661316 | 215 | @returns ble_error_t |
mridup | 2:a2b623661316 | 216 | |
mridup | 2:a2b623661316 | 217 | @section EXAMPLE |
mridup | 2:a2b623661316 | 218 | |
mridup | 2:a2b623661316 | 219 | @code |
mridup | 2:a2b623661316 | 220 | |
mridup | 2:a2b623661316 | 221 | uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; |
mridup | 2:a2b623661316 | 222 | nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); |
mridup | 2:a2b623661316 | 223 | |
mridup | 2:a2b623661316 | 224 | @endcode |
mridup | 2:a2b623661316 | 225 | */ |
mridup | 2:a2b623661316 | 226 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 227 | ble_error_t BlueNRGGap::setAddress(addr_type_t type, const uint8_t address[6]) |
mridup | 2:a2b623661316 | 228 | { |
mridup | 2:a2b623661316 | 229 | if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { |
mridup | 2:a2b623661316 | 230 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 231 | } |
mridup | 2:a2b623661316 | 232 | |
mridup | 2:a2b623661316 | 233 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 234 | } |