Added support for obtaining BLE device name by parsing advertising data.
Fork of BLE_BlueNRG by
BlueNRGGap.cpp@4:fa5b5693c1b5, 2014-07-22 (annotated)
- Committer:
- mridup
- Date:
- Tue Jul 22 09:20:20 2014 +0000
- Revision:
- 4:fa5b5693c1b5
- Parent:
- 3:9c4c13795643
- Child:
- 5:31dedfa19a12
Event Handling (Connection, disconnection) and debug support, SetAddress support.
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 | 3:9c4c13795643 | 20 | //Local Variables |
mridup | 3:9c4c13795643 | 21 | const uint8_t *device_name; |
mridup | 2:a2b623661316 | 22 | |
mridup | 2:a2b623661316 | 23 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 24 | /*! |
mridup | 2:a2b623661316 | 25 | @brief Sets the advertising parameters and payload for the device |
mridup | 2:a2b623661316 | 26 | |
mridup | 2:a2b623661316 | 27 | @param[in] params |
mridup | 2:a2b623661316 | 28 | Basic advertising details, including the advertising |
mridup | 2:a2b623661316 | 29 | delay, timeout and how the device should be advertised |
mridup | 2:a2b623661316 | 30 | @params[in] advData |
mridup | 2:a2b623661316 | 31 | The primary advertising data payload |
mridup | 2:a2b623661316 | 32 | @params[in] scanResponse |
mridup | 2:a2b623661316 | 33 | The optional Scan Response payload if the advertising |
mridup | 2:a2b623661316 | 34 | type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED |
mridup | 2:a2b623661316 | 35 | in \ref GapAdveritinngParams |
mridup | 2:a2b623661316 | 36 | |
mridup | 2:a2b623661316 | 37 | @returns \ref ble_error_t |
mridup | 2:a2b623661316 | 38 | |
mridup | 2:a2b623661316 | 39 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 40 | Everything executed properly |
mridup | 2:a2b623661316 | 41 | |
mridup | 2:a2b623661316 | 42 | @retval BLE_ERROR_BUFFER_OVERFLOW |
mridup | 2:a2b623661316 | 43 | The proposed action would cause a buffer overflow. All |
mridup | 2:a2b623661316 | 44 | advertising payloads must be <= 31 bytes, for example. |
mridup | 2:a2b623661316 | 45 | |
mridup | 2:a2b623661316 | 46 | @retval BLE_ERROR_NOT_IMPLEMENTED |
mridup | 2:a2b623661316 | 47 | A feature was requested that is not yet supported in the |
mridup | 2:a2b623661316 | 48 | nRF51 firmware or hardware. |
mridup | 2:a2b623661316 | 49 | |
mridup | 2:a2b623661316 | 50 | @retval BLE_ERROR_PARAM_OUT_OF_RANGE |
mridup | 2:a2b623661316 | 51 | One of the proposed values is outside the valid range. |
mridup | 2:a2b623661316 | 52 | |
mridup | 2:a2b623661316 | 53 | @section EXAMPLE |
mridup | 2:a2b623661316 | 54 | |
mridup | 2:a2b623661316 | 55 | @code |
mridup | 2:a2b623661316 | 56 | |
mridup | 2:a2b623661316 | 57 | @endcode |
mridup | 2:a2b623661316 | 58 | */ |
mridup | 2:a2b623661316 | 59 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 60 | ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) |
mridup | 2:a2b623661316 | 61 | { |
mridup | 2:a2b623661316 | 62 | /* Make sure we don't exceed the advertising payload length */ |
mridup | 2:a2b623661316 | 63 | if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { |
mridup | 2:a2b623661316 | 64 | return BLE_ERROR_BUFFER_OVERFLOW; |
mridup | 2:a2b623661316 | 65 | } |
mridup | 2:a2b623661316 | 66 | |
mridup | 2:a2b623661316 | 67 | /* Make sure we have a payload! */ |
mridup | 2:a2b623661316 | 68 | if (advData.getPayloadLen() == 0) { |
mridup | 2:a2b623661316 | 69 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 70 | } |
mridup | 2:a2b623661316 | 71 | |
mridup | 3:9c4c13795643 | 72 | //set the advData here in some local variable so that startAdvertising can use it. |
mridup | 3:9c4c13795643 | 73 | if (advData.getPayloadLen() > 0) { |
mridup | 3:9c4c13795643 | 74 | const uint8_t *payload = advData.getPayload(); |
mridup | 3:9c4c13795643 | 75 | device_name = advData.getPayload(); |
mridup | 3:9c4c13795643 | 76 | } |
mridup | 3:9c4c13795643 | 77 | |
mridup | 3:9c4c13795643 | 78 | |
mridup | 2:a2b623661316 | 79 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 80 | } |
mridup | 2:a2b623661316 | 81 | |
mridup | 2:a2b623661316 | 82 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 83 | /*! |
mridup | 2:a2b623661316 | 84 | @brief Starts the BLE HW, initialising any services that were |
mridup | 2:a2b623661316 | 85 | added before this function was called. |
mridup | 2:a2b623661316 | 86 | |
mridup | 2:a2b623661316 | 87 | @note All services must be added before calling this function! |
mridup | 2:a2b623661316 | 88 | |
mridup | 2:a2b623661316 | 89 | @returns ble_error_t |
mridup | 2:a2b623661316 | 90 | |
mridup | 2:a2b623661316 | 91 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 92 | Everything executed properly |
mridup | 2:a2b623661316 | 93 | |
mridup | 2:a2b623661316 | 94 | @section EXAMPLE |
mridup | 2:a2b623661316 | 95 | |
mridup | 2:a2b623661316 | 96 | @code |
mridup | 2:a2b623661316 | 97 | |
mridup | 2:a2b623661316 | 98 | @endcode |
mridup | 2:a2b623661316 | 99 | */ |
mridup | 2:a2b623661316 | 100 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 101 | ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams ¶ms) |
mridup | 2:a2b623661316 | 102 | { |
mridup | 2:a2b623661316 | 103 | /* Make sure we support the advertising type */ |
mridup | 2:a2b623661316 | 104 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { |
mridup | 2:a2b623661316 | 105 | /* ToDo: This requires a propery security implementation, etc. */ |
mridup | 2:a2b623661316 | 106 | return BLE_ERROR_NOT_IMPLEMENTED; |
mridup | 2:a2b623661316 | 107 | } |
mridup | 2:a2b623661316 | 108 | |
mridup | 2:a2b623661316 | 109 | /* Check interval range */ |
mridup | 2:a2b623661316 | 110 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { |
mridup | 2:a2b623661316 | 111 | /* Min delay is slightly longer for unconnectable devices */ |
mridup | 2:a2b623661316 | 112 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || |
mridup | 2:a2b623661316 | 113 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { |
mridup | 2:a2b623661316 | 114 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 115 | } |
mridup | 2:a2b623661316 | 116 | } else { |
mridup | 2:a2b623661316 | 117 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || |
mridup | 2:a2b623661316 | 118 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { |
mridup | 2:a2b623661316 | 119 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 120 | } |
mridup | 2:a2b623661316 | 121 | } |
mridup | 2:a2b623661316 | 122 | |
mridup | 2:a2b623661316 | 123 | /* Check timeout is zero for Connectable Directed */ |
mridup | 2:a2b623661316 | 124 | if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) { |
mridup | 2:a2b623661316 | 125 | /* Timeout must be 0 with this type, although we'll never get here */ |
mridup | 2:a2b623661316 | 126 | /* since this isn't implemented yet anyway */ |
mridup | 2:a2b623661316 | 127 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 128 | } |
mridup | 2:a2b623661316 | 129 | |
mridup | 2:a2b623661316 | 130 | /* Check timeout for other advertising types */ |
mridup | 2:a2b623661316 | 131 | if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && |
mridup | 2:a2b623661316 | 132 | (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) { |
mridup | 2:a2b623661316 | 133 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 134 | } |
mridup | 2:a2b623661316 | 135 | |
mridup | 2:a2b623661316 | 136 | tBleStatus ret; |
mridup | 2:a2b623661316 | 137 | |
mridup | 3:9c4c13795643 | 138 | //const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,'B','l','u','e','N','R','G'}; |
mridup | 3:9c4c13795643 | 139 | const char local_name[] = {AD_TYPE_COMPLETE_LOCAL_NAME,device_name[5],device_name[6],device_name[7],device_name[8], device_name[9], |
mridup | 4:fa5b5693c1b5 | 140 | device_name[10], device_name[11], device_name[12], device_name[13], device_name[14]}; |
mridup | 2:a2b623661316 | 141 | |
mridup | 2:a2b623661316 | 142 | /* disable scan response */ |
mridup | 2:a2b623661316 | 143 | hci_le_set_scan_resp_data(0,NULL); |
mridup | 2:a2b623661316 | 144 | |
mridup | 2:a2b623661316 | 145 | /*aci_gap_set_discoverable(Advertising_Event_Type, Adv_min_intvl, Adv_Max_Intvl, Addr_Type, Adv_Filter_Policy, |
mridup | 2:a2b623661316 | 146 | Local_Name_Length, local_name, service_uuid_length, service_uuid_list, Slave_conn_intvl_min, Slave_conn_intvl_max);*/ |
mridup | 2:a2b623661316 | 147 | /*LINK_LAYER.H DESCRIBES THE ADVERTISING TYPES*/ |
mridup | 2:a2b623661316 | 148 | |
mridup | 2:a2b623661316 | 149 | ret = aci_gap_set_discoverable(params.getAdvertisingType(), params.getInterval(), 0, PUBLIC_ADDR, NO_WHITE_LIST_USE, |
mridup | 4:fa5b5693c1b5 | 150 | 11 /*Length of the local_name[] array*/, local_name, 0, NULL, 0, 0); |
mridup | 4:fa5b5693c1b5 | 151 | state.advertising = 1; |
mridup | 2:a2b623661316 | 152 | |
mridup | 2:a2b623661316 | 153 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 154 | } |
mridup | 2:a2b623661316 | 155 | |
mridup | 2:a2b623661316 | 156 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 157 | /*! |
mridup | 2:a2b623661316 | 158 | @brief Stops the BLE HW and disconnects from any devices |
mridup | 2:a2b623661316 | 159 | |
mridup | 2:a2b623661316 | 160 | @returns ble_error_t |
mridup | 2:a2b623661316 | 161 | |
mridup | 2:a2b623661316 | 162 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 163 | Everything executed properly |
mridup | 2:a2b623661316 | 164 | |
mridup | 2:a2b623661316 | 165 | @section EXAMPLE |
mridup | 2:a2b623661316 | 166 | |
mridup | 2:a2b623661316 | 167 | @code |
mridup | 2:a2b623661316 | 168 | |
mridup | 2:a2b623661316 | 169 | @endcode |
mridup | 2:a2b623661316 | 170 | */ |
mridup | 2:a2b623661316 | 171 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 172 | ble_error_t BlueNRGGap::stopAdvertising(void) |
mridup | 2:a2b623661316 | 173 | { |
mridup | 2:a2b623661316 | 174 | |
mridup | 2:a2b623661316 | 175 | |
mridup | 2:a2b623661316 | 176 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 177 | } |
mridup | 2:a2b623661316 | 178 | |
mridup | 2:a2b623661316 | 179 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 180 | /*! |
mridup | 2:a2b623661316 | 181 | @brief Disconnects if we are connected to a central device |
mridup | 2:a2b623661316 | 182 | |
mridup | 2:a2b623661316 | 183 | @returns ble_error_t |
mridup | 2:a2b623661316 | 184 | |
mridup | 2:a2b623661316 | 185 | @retval BLE_ERROR_NONE |
mridup | 2:a2b623661316 | 186 | Everything executed properly |
mridup | 2:a2b623661316 | 187 | |
mridup | 2:a2b623661316 | 188 | @section EXAMPLE |
mridup | 2:a2b623661316 | 189 | |
mridup | 2:a2b623661316 | 190 | @code |
mridup | 2:a2b623661316 | 191 | |
mridup | 2:a2b623661316 | 192 | @endcode |
mridup | 2:a2b623661316 | 193 | */ |
mridup | 2:a2b623661316 | 194 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 195 | ble_error_t BlueNRGGap::disconnect(void) |
mridup | 2:a2b623661316 | 196 | { |
mridup | 2:a2b623661316 | 197 | |
mridup | 2:a2b623661316 | 198 | return BLE_ERROR_NONE; |
mridup | 2:a2b623661316 | 199 | } |
mridup | 2:a2b623661316 | 200 | |
mridup | 2:a2b623661316 | 201 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 202 | /*! |
mridup | 2:a2b623661316 | 203 | @brief Sets the 16-bit connection handle |
mridup | 2:a2b623661316 | 204 | */ |
mridup | 2:a2b623661316 | 205 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 206 | void BlueNRGGap::setConnectionHandle(uint16_t con_handle) |
mridup | 2:a2b623661316 | 207 | { |
mridup | 2:a2b623661316 | 208 | m_connectionHandle = con_handle; |
mridup | 2:a2b623661316 | 209 | } |
mridup | 2:a2b623661316 | 210 | |
mridup | 2:a2b623661316 | 211 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 212 | /*! |
mridup | 2:a2b623661316 | 213 | @brief Gets the 16-bit connection handle |
mridup | 2:a2b623661316 | 214 | */ |
mridup | 2:a2b623661316 | 215 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 216 | uint16_t BlueNRGGap::getConnectionHandle(void) |
mridup | 2:a2b623661316 | 217 | { |
mridup | 2:a2b623661316 | 218 | return m_connectionHandle; |
mridup | 2:a2b623661316 | 219 | } |
mridup | 2:a2b623661316 | 220 | |
mridup | 2:a2b623661316 | 221 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 222 | /*! |
mridup | 2:a2b623661316 | 223 | @brief Sets the BLE device address |
mridup | 2:a2b623661316 | 224 | |
mridup | 2:a2b623661316 | 225 | @returns ble_error_t |
mridup | 2:a2b623661316 | 226 | |
mridup | 2:a2b623661316 | 227 | @section EXAMPLE |
mridup | 2:a2b623661316 | 228 | |
mridup | 2:a2b623661316 | 229 | @code |
mridup | 2:a2b623661316 | 230 | |
mridup | 2:a2b623661316 | 231 | uint8_t device_address[6] = { 0xca, 0xfe, 0xf0, 0xf0, 0xf0, 0xf0 }; |
mridup | 2:a2b623661316 | 232 | nrf.getGap().setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, device_address); |
mridup | 2:a2b623661316 | 233 | |
mridup | 2:a2b623661316 | 234 | @endcode |
mridup | 2:a2b623661316 | 235 | */ |
mridup | 2:a2b623661316 | 236 | /**************************************************************************/ |
mridup | 2:a2b623661316 | 237 | ble_error_t BlueNRGGap::setAddress(addr_type_t type, const uint8_t address[6]) |
mridup | 2:a2b623661316 | 238 | { |
mridup | 4:fa5b5693c1b5 | 239 | tBleStatus ret; |
mridup | 4:fa5b5693c1b5 | 240 | |
mridup | 2:a2b623661316 | 241 | if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { |
mridup | 2:a2b623661316 | 242 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 243 | } |
mridup | 4:fa5b5693c1b5 | 244 | |
mridup | 4:fa5b5693c1b5 | 245 | ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET, CONFIG_DATA_PUBADDR_LEN, (const tHalUint8*)address); |
mridup | 4:fa5b5693c1b5 | 246 | |
mridup | 4:fa5b5693c1b5 | 247 | //if (ret==BLE_STATUS_SUCCESS) |
mridup | 2:a2b623661316 | 248 | return BLE_ERROR_NONE; |
mridup | 4:fa5b5693c1b5 | 249 | |
mridup | 4:fa5b5693c1b5 | 250 | //return BLE_ERROR_PARAM_OUT_OF_RANGE; |
mridup | 2:a2b623661316 | 251 | } |