BLE lib
Fork of Nucleo_BLE_BlueNRG by
BlueNRGGap.cpp@5:beb2c7544ce4, 2015-02-08 (annotated)
- Committer:
- vijaynvr
- Date:
- Sun Feb 08 14:25:58 2015 +0000
- Revision:
- 5:beb2c7544ce4
- Parent:
- 0:a948f5f3904c
working;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sjallouli | 0:a948f5f3904c | 1 | /* mbed Microcontroller Library |
sjallouli | 0:a948f5f3904c | 2 | * Copyright (c) 2006-2013 ARM Limited |
sjallouli | 0:a948f5f3904c | 3 | * |
sjallouli | 0:a948f5f3904c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sjallouli | 0:a948f5f3904c | 5 | * you may not use this file except in compliance with the License. |
sjallouli | 0:a948f5f3904c | 6 | * You may obtain a copy of the License at |
sjallouli | 0:a948f5f3904c | 7 | * |
sjallouli | 0:a948f5f3904c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sjallouli | 0:a948f5f3904c | 9 | * |
sjallouli | 0:a948f5f3904c | 10 | * Unless required by applicable law or agreed to in writing, software |
sjallouli | 0:a948f5f3904c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sjallouli | 0:a948f5f3904c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sjallouli | 0:a948f5f3904c | 13 | * See the License for the specific language governing permissions and |
sjallouli | 0:a948f5f3904c | 14 | * limitations under the License. |
sjallouli | 0:a948f5f3904c | 15 | */ |
sjallouli | 0:a948f5f3904c | 16 | |
sjallouli | 0:a948f5f3904c | 17 | #include "BlueNRGDevice.h" |
sjallouli | 0:a948f5f3904c | 18 | #include "mbed.h" |
sjallouli | 0:a948f5f3904c | 19 | #include "Payload.h" |
sjallouli | 0:a948f5f3904c | 20 | #include "Utils.h" |
sjallouli | 0:a948f5f3904c | 21 | |
sjallouli | 0:a948f5f3904c | 22 | //Local Variables |
sjallouli | 0:a948f5f3904c | 23 | const char *local_name = NULL; |
sjallouli | 0:a948f5f3904c | 24 | uint8_t local_name_length = 0; |
sjallouli | 0:a948f5f3904c | 25 | const uint8_t *scan_response_payload = NULL; |
sjallouli | 0:a948f5f3904c | 26 | uint8_t scan_rsp_length = 0; |
sjallouli | 0:a948f5f3904c | 27 | uint8_t servUuidlength = 0; |
sjallouli | 0:a948f5f3904c | 28 | uint8_t* servUuidData = NULL; |
sjallouli | 0:a948f5f3904c | 29 | |
sjallouli | 0:a948f5f3904c | 30 | uint32_t advtInterval = 0; |
sjallouli | 0:a948f5f3904c | 31 | |
sjallouli | 0:a948f5f3904c | 32 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 33 | /*! |
sjallouli | 0:a948f5f3904c | 34 | @brief Sets the advertising parameters and payload for the device. |
sjallouli | 0:a948f5f3904c | 35 | Note: Some data types give error when their adv data is updated using aci_gap_update_adv_data() API |
sjallouli | 0:a948f5f3904c | 36 | |
sjallouli | 0:a948f5f3904c | 37 | @param[in] params |
sjallouli | 0:a948f5f3904c | 38 | Basic advertising details, including the advertising |
sjallouli | 0:a948f5f3904c | 39 | delay, timeout and how the device should be advertised |
sjallouli | 0:a948f5f3904c | 40 | @params[in] advData |
sjallouli | 0:a948f5f3904c | 41 | The primary advertising data payload |
sjallouli | 0:a948f5f3904c | 42 | @params[in] scanResponse |
sjallouli | 0:a948f5f3904c | 43 | The optional Scan Response payload if the advertising |
sjallouli | 0:a948f5f3904c | 44 | type is set to \ref GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED |
sjallouli | 0:a948f5f3904c | 45 | in \ref GapAdveritinngParams |
sjallouli | 0:a948f5f3904c | 46 | |
sjallouli | 0:a948f5f3904c | 47 | @returns \ref ble_error_t |
sjallouli | 0:a948f5f3904c | 48 | |
sjallouli | 0:a948f5f3904c | 49 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 50 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 51 | |
sjallouli | 0:a948f5f3904c | 52 | @retval BLE_ERROR_BUFFER_OVERFLOW |
sjallouli | 0:a948f5f3904c | 53 | The proposed action would cause a buffer overflow. All |
sjallouli | 0:a948f5f3904c | 54 | advertising payloads must be <= 31 bytes, for example. |
sjallouli | 0:a948f5f3904c | 55 | |
sjallouli | 0:a948f5f3904c | 56 | @retval BLE_ERROR_NOT_IMPLEMENTED |
sjallouli | 0:a948f5f3904c | 57 | A feature was requested that is not yet supported in the |
sjallouli | 0:a948f5f3904c | 58 | nRF51 firmware or hardware. |
sjallouli | 0:a948f5f3904c | 59 | |
sjallouli | 0:a948f5f3904c | 60 | @retval BLE_ERROR_PARAM_OUT_OF_RANGE |
sjallouli | 0:a948f5f3904c | 61 | One of the proposed values is outside the valid range. |
sjallouli | 0:a948f5f3904c | 62 | |
sjallouli | 0:a948f5f3904c | 63 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 64 | |
sjallouli | 0:a948f5f3904c | 65 | @code |
sjallouli | 0:a948f5f3904c | 66 | |
sjallouli | 0:a948f5f3904c | 67 | @endcode |
sjallouli | 0:a948f5f3904c | 68 | */ |
sjallouli | 0:a948f5f3904c | 69 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 70 | ble_error_t BlueNRGGap::setAdvertisingData(const GapAdvertisingData &advData, const GapAdvertisingData &scanResponse) |
sjallouli | 0:a948f5f3904c | 71 | { |
sjallouli | 0:a948f5f3904c | 72 | DEBUG("BlueNRGGap::setAdvertisingData\n\r"); |
sjallouli | 0:a948f5f3904c | 73 | /* Make sure we don't exceed the advertising payload length */ |
sjallouli | 0:a948f5f3904c | 74 | if (advData.getPayloadLen() > GAP_ADVERTISING_DATA_MAX_PAYLOAD) { |
sjallouli | 0:a948f5f3904c | 75 | return BLE_ERROR_BUFFER_OVERFLOW; |
sjallouli | 0:a948f5f3904c | 76 | } |
sjallouli | 0:a948f5f3904c | 77 | |
sjallouli | 0:a948f5f3904c | 78 | /* Make sure we have a payload! */ |
sjallouli | 0:a948f5f3904c | 79 | if (advData.getPayloadLen() <= 0) { |
sjallouli | 0:a948f5f3904c | 80 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 81 | } else { |
sjallouli | 0:a948f5f3904c | 82 | PayloadPtr loadPtr(advData.getPayload(), advData.getPayloadLen()); |
sjallouli | 0:a948f5f3904c | 83 | for(uint8_t index=0; index<loadPtr.getPayloadUnitCount(); index++) { |
sjallouli | 0:a948f5f3904c | 84 | PayloadUnit unit = loadPtr.getUnitAtIndex(index); |
sjallouli | 0:a948f5f3904c | 85 | |
sjallouli | 0:a948f5f3904c | 86 | DEBUG("adData[%d].length=%d\n\r", index,(uint8_t)(*loadPtr.getUnitAtIndex(index).getLenPtr())); |
sjallouli | 0:a948f5f3904c | 87 | DEBUG("adData[%d].AdType=0x%x\n\r", index,(uint8_t)(*loadPtr.getUnitAtIndex(index).getAdTypePtr())); |
sjallouli | 0:a948f5f3904c | 88 | |
sjallouli | 0:a948f5f3904c | 89 | switch(*loadPtr.getUnitAtIndex(index).getAdTypePtr()) { |
sjallouli | 0:a948f5f3904c | 90 | case GapAdvertisingData::FLAGS: /* ref *Flags */ |
sjallouli | 0:a948f5f3904c | 91 | { |
sjallouli | 0:a948f5f3904c | 92 | //Check if Flags are OK. BlueNRG only supports LE Mode. |
sjallouli | 0:a948f5f3904c | 93 | uint8_t *flags = loadPtr.getUnitAtIndex(index).getDataPtr(); |
sjallouli | 0:a948f5f3904c | 94 | if((*flags & GapAdvertisingData::BREDR_NOT_SUPPORTED) != GapAdvertisingData::BREDR_NOT_SUPPORTED) { |
sjallouli | 0:a948f5f3904c | 95 | DEBUG("BlueNRG does not support BR/EDR Mode"); |
sjallouli | 0:a948f5f3904c | 96 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 97 | } |
sjallouli | 0:a948f5f3904c | 98 | |
sjallouli | 0:a948f5f3904c | 99 | break; |
sjallouli | 0:a948f5f3904c | 100 | } |
sjallouli | 0:a948f5f3904c | 101 | case GapAdvertisingData::INCOMPLETE_LIST_16BIT_SERVICE_IDS: /**< Incomplete list of 16-bit Service IDs */ |
sjallouli | 0:a948f5f3904c | 102 | { |
sjallouli | 0:a948f5f3904c | 103 | break; |
sjallouli | 0:a948f5f3904c | 104 | } |
sjallouli | 0:a948f5f3904c | 105 | case GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS: /**< Complete list of 16-bit Service IDs */ |
sjallouli | 0:a948f5f3904c | 106 | { |
sjallouli | 0:a948f5f3904c | 107 | DEBUG("Advertising type: COMPLETE_LIST_16BIT_SERVICE_IDS\n\r"); |
sjallouli | 0:a948f5f3904c | 108 | DEBUG("Advertising type: COMPLETE_LIST_16BIT_SERVICE_IDS\n"); |
sjallouli | 0:a948f5f3904c | 109 | #if 0 |
sjallouli | 0:a948f5f3904c | 110 | int err = aci_gap_update_adv_data(*loadPtr.getUnitAtIndex(index).getLenPtr(), loadPtr.getUnitAtIndex(index).getAdTypePtr()); |
sjallouli | 0:a948f5f3904c | 111 | if(BLE_STATUS_SUCCESS!=err) { |
sjallouli | 0:a948f5f3904c | 112 | DEBUG("error occurred while adding adv data\n"); |
sjallouli | 0:a948f5f3904c | 113 | return BLE_ERROR_PARAM_OUT_OF_RANGE; // no other suitable error code is available |
sjallouli | 0:a948f5f3904c | 114 | } |
sjallouli | 0:a948f5f3904c | 115 | #endif |
sjallouli | 0:a948f5f3904c | 116 | break; |
sjallouli | 0:a948f5f3904c | 117 | } |
sjallouli | 0:a948f5f3904c | 118 | case GapAdvertisingData::INCOMPLETE_LIST_32BIT_SERVICE_IDS: /**< Incomplete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ |
sjallouli | 0:a948f5f3904c | 119 | { |
sjallouli | 0:a948f5f3904c | 120 | break; |
sjallouli | 0:a948f5f3904c | 121 | } |
sjallouli | 0:a948f5f3904c | 122 | case GapAdvertisingData::COMPLETE_LIST_32BIT_SERVICE_IDS: /**< Complete list of 32-bit Service IDs (not relevant for Bluetooth 4.0) */ |
sjallouli | 0:a948f5f3904c | 123 | { |
sjallouli | 0:a948f5f3904c | 124 | break; |
sjallouli | 0:a948f5f3904c | 125 | } |
sjallouli | 0:a948f5f3904c | 126 | case GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS: /**< Incomplete list of 128-bit Service IDs */ |
sjallouli | 0:a948f5f3904c | 127 | { |
sjallouli | 0:a948f5f3904c | 128 | break; |
sjallouli | 0:a948f5f3904c | 129 | } |
sjallouli | 0:a948f5f3904c | 130 | case GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS: /**< Complete list of 128-bit Service IDs */ |
sjallouli | 0:a948f5f3904c | 131 | { |
sjallouli | 0:a948f5f3904c | 132 | break; |
sjallouli | 0:a948f5f3904c | 133 | } |
sjallouli | 0:a948f5f3904c | 134 | case GapAdvertisingData::SHORTENED_LOCAL_NAME: /**< Shortened Local Name */ |
sjallouli | 0:a948f5f3904c | 135 | { |
sjallouli | 0:a948f5f3904c | 136 | break; |
sjallouli | 0:a948f5f3904c | 137 | } |
sjallouli | 0:a948f5f3904c | 138 | case GapAdvertisingData::COMPLETE_LOCAL_NAME: /**< Complete Local Name */ |
sjallouli | 0:a948f5f3904c | 139 | { |
sjallouli | 0:a948f5f3904c | 140 | DEBUG("Advertising type: COMPLETE_LOCAL_NAME\n\r"); |
sjallouli | 0:a948f5f3904c | 141 | loadPtr.getUnitAtIndex(index).printDataAsString(); |
sjallouli | 0:a948f5f3904c | 142 | local_name_length = *loadPtr.getUnitAtIndex(index).getLenPtr()-1; |
sjallouli | 0:a948f5f3904c | 143 | local_name = (const char*)loadPtr.getUnitAtIndex(index).getAdTypePtr(); |
sjallouli | 0:a948f5f3904c | 144 | //COMPLETE_LOCAL_NAME is only advertising device name. Gatt Device Name is not the same.(Must be set right after GAP/GATT init?) |
sjallouli | 0:a948f5f3904c | 145 | |
sjallouli | 0:a948f5f3904c | 146 | DEBUG("device_name length=%d\n\r", local_name_length); |
sjallouli | 0:a948f5f3904c | 147 | break; |
sjallouli | 0:a948f5f3904c | 148 | } |
sjallouli | 0:a948f5f3904c | 149 | case GapAdvertisingData::TX_POWER_LEVEL: /**< TX Power Level (in dBm) */ |
sjallouli | 0:a948f5f3904c | 150 | { |
sjallouli | 0:a948f5f3904c | 151 | DEBUG("Advertising type: TX_POWER_LEVEL\n\r"); |
sjallouli | 0:a948f5f3904c | 152 | int8_t dbm = *loadPtr.getUnitAtIndex(index).getDataPtr(); |
sjallouli | 0:a948f5f3904c | 153 | int8_t enHighPower = 0; |
sjallouli | 0:a948f5f3904c | 154 | int8_t paLevel = 0; |
sjallouli | 0:a948f5f3904c | 155 | int8_t dbmActuallySet = getHighPowerAndPALevelValue(dbm, enHighPower, paLevel); |
sjallouli | 0:a948f5f3904c | 156 | DEBUG("dbm=%d, dbmActuallySet=%d\n\r", dbm, dbmActuallySet); |
sjallouli | 0:a948f5f3904c | 157 | DEBUG("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel); |
sjallouli | 0:a948f5f3904c | 158 | aci_hal_set_tx_power_level(enHighPower, paLevel); |
sjallouli | 0:a948f5f3904c | 159 | break; |
sjallouli | 0:a948f5f3904c | 160 | } |
sjallouli | 0:a948f5f3904c | 161 | case GapAdvertisingData::DEVICE_ID: /**< Device ID */ |
sjallouli | 0:a948f5f3904c | 162 | { |
sjallouli | 0:a948f5f3904c | 163 | break; |
sjallouli | 0:a948f5f3904c | 164 | } |
sjallouli | 0:a948f5f3904c | 165 | case GapAdvertisingData::SLAVE_CONNECTION_INTERVAL_RANGE: /**< Slave :Connection Interval Range */ |
sjallouli | 0:a948f5f3904c | 166 | { |
sjallouli | 0:a948f5f3904c | 167 | break; |
sjallouli | 0:a948f5f3904c | 168 | } |
sjallouli | 0:a948f5f3904c | 169 | case GapAdvertisingData::SERVICE_DATA: /**< Service Data */ |
sjallouli | 0:a948f5f3904c | 170 | { |
sjallouli | 0:a948f5f3904c | 171 | break; |
sjallouli | 0:a948f5f3904c | 172 | } |
sjallouli | 0:a948f5f3904c | 173 | case GapAdvertisingData::APPEARANCE: |
sjallouli | 0:a948f5f3904c | 174 | { |
sjallouli | 0:a948f5f3904c | 175 | /* |
sjallouli | 0:a948f5f3904c | 176 | Tested with GapAdvertisingData::GENERIC_PHONE. |
sjallouli | 0:a948f5f3904c | 177 | for other appearances BLE Scanner android app is not behaving properly |
sjallouli | 0:a948f5f3904c | 178 | */ |
sjallouli | 0:a948f5f3904c | 179 | DEBUG("Advertising type: APPEARANCE\n\r"); |
sjallouli | 0:a948f5f3904c | 180 | const char *deviceAppearance = NULL; |
sjallouli | 0:a948f5f3904c | 181 | deviceAppearance = (const char*)loadPtr.getUnitAtIndex(index).getDataPtr(); // to be set later when startAdvertising() is called |
sjallouli | 0:a948f5f3904c | 182 | |
sjallouli | 0:a948f5f3904c | 183 | uint8_t Appearance[2]; |
sjallouli | 0:a948f5f3904c | 184 | uint16_t devP = (uint16_t)*deviceAppearance; |
sjallouli | 0:a948f5f3904c | 185 | STORE_LE_16(Appearance, (uint16_t)devP); |
sjallouli | 0:a948f5f3904c | 186 | |
sjallouli | 0:a948f5f3904c | 187 | DEBUG("input: deviceAppearance= 0x%x 0x%x..., strlen(deviceAppearance)=%d\n\r", Appearance[1], Appearance[0], (uint8_t)*loadPtr.getUnitAtIndex(index).getLenPtr()-1); /**< \ref Appearance */ |
sjallouli | 0:a948f5f3904c | 188 | |
sjallouli | 0:a948f5f3904c | 189 | aci_gatt_update_char_value(g_gap_service_handle, g_appearance_char_handle, 0, 2, (tHalUint8 *)deviceAppearance);//not using array Appearance[2] |
sjallouli | 0:a948f5f3904c | 190 | break; |
sjallouli | 0:a948f5f3904c | 191 | } |
sjallouli | 0:a948f5f3904c | 192 | case GapAdvertisingData::ADVERTISING_INTERVAL: /**< Advertising Interval */ |
sjallouli | 0:a948f5f3904c | 193 | { |
sjallouli | 0:a948f5f3904c | 194 | advtInterval = (uint16_t)(*loadPtr.getUnitAtIndex(index).getDataPtr()); |
sjallouli | 0:a948f5f3904c | 195 | DEBUG("advtInterval=%d\n\r", advtInterval); |
sjallouli | 0:a948f5f3904c | 196 | break; |
sjallouli | 0:a948f5f3904c | 197 | } |
sjallouli | 0:a948f5f3904c | 198 | case GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA: /**< Manufacturer Specific Data */ |
sjallouli | 0:a948f5f3904c | 199 | { |
sjallouli | 0:a948f5f3904c | 200 | break; |
sjallouli | 0:a948f5f3904c | 201 | } |
sjallouli | 0:a948f5f3904c | 202 | |
sjallouli | 0:a948f5f3904c | 203 | } |
sjallouli | 0:a948f5f3904c | 204 | } |
sjallouli | 0:a948f5f3904c | 205 | //Set the SCAN_RSP Payload |
sjallouli | 0:a948f5f3904c | 206 | scan_response_payload = scanResponse.getPayload(); |
sjallouli | 0:a948f5f3904c | 207 | scan_rsp_length = scanResponse.getPayloadLen(); |
sjallouli | 0:a948f5f3904c | 208 | } |
sjallouli | 0:a948f5f3904c | 209 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 210 | } |
sjallouli | 0:a948f5f3904c | 211 | |
sjallouli | 0:a948f5f3904c | 212 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 213 | /*! |
sjallouli | 0:a948f5f3904c | 214 | @brief Starts the BLE HW, initialising any services that were |
sjallouli | 0:a948f5f3904c | 215 | added before this function was called. |
sjallouli | 0:a948f5f3904c | 216 | |
sjallouli | 0:a948f5f3904c | 217 | @note All services must be added before calling this function! |
sjallouli | 0:a948f5f3904c | 218 | |
sjallouli | 0:a948f5f3904c | 219 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 220 | |
sjallouli | 0:a948f5f3904c | 221 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 222 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 223 | |
sjallouli | 0:a948f5f3904c | 224 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 225 | |
sjallouli | 0:a948f5f3904c | 226 | @code |
sjallouli | 0:a948f5f3904c | 227 | |
sjallouli | 0:a948f5f3904c | 228 | @endcode |
sjallouli | 0:a948f5f3904c | 229 | */ |
sjallouli | 0:a948f5f3904c | 230 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 231 | ble_error_t BlueNRGGap::startAdvertising(const GapAdvertisingParams ¶ms) |
sjallouli | 0:a948f5f3904c | 232 | { |
sjallouli | 0:a948f5f3904c | 233 | /* Make sure we support the advertising type */ |
sjallouli | 0:a948f5f3904c | 234 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) { |
sjallouli | 0:a948f5f3904c | 235 | /* ToDo: This requires a propery security implementation, etc. */ |
sjallouli | 0:a948f5f3904c | 236 | return BLE_ERROR_NOT_IMPLEMENTED; |
sjallouli | 0:a948f5f3904c | 237 | } |
sjallouli | 0:a948f5f3904c | 238 | |
sjallouli | 0:a948f5f3904c | 239 | /* Check interval range */ |
sjallouli | 0:a948f5f3904c | 240 | if (params.getAdvertisingType() == GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED) { |
sjallouli | 0:a948f5f3904c | 241 | /* Min delay is slightly longer for unconnectable devices */ |
sjallouli | 0:a948f5f3904c | 242 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN_NONCON) || |
sjallouli | 0:a948f5f3904c | 243 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { |
sjallouli | 0:a948f5f3904c | 244 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 245 | } |
sjallouli | 0:a948f5f3904c | 246 | } else { |
sjallouli | 0:a948f5f3904c | 247 | if ((params.getInterval() < GAP_ADV_PARAMS_INTERVAL_MIN) || |
sjallouli | 0:a948f5f3904c | 248 | (params.getInterval() > GAP_ADV_PARAMS_INTERVAL_MAX)) { |
sjallouli | 0:a948f5f3904c | 249 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 250 | } |
sjallouli | 0:a948f5f3904c | 251 | } |
sjallouli | 0:a948f5f3904c | 252 | |
sjallouli | 0:a948f5f3904c | 253 | /* Check timeout is zero for Connectable Directed */ |
sjallouli | 0:a948f5f3904c | 254 | if ((params.getAdvertisingType() == GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && (params.getTimeout() != 0)) { |
sjallouli | 0:a948f5f3904c | 255 | /* Timeout must be 0 with this type, although we'll never get here */ |
sjallouli | 0:a948f5f3904c | 256 | /* since this isn't implemented yet anyway */ |
sjallouli | 0:a948f5f3904c | 257 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 258 | } |
sjallouli | 0:a948f5f3904c | 259 | |
sjallouli | 0:a948f5f3904c | 260 | /* Check timeout for other advertising types */ |
sjallouli | 0:a948f5f3904c | 261 | if ((params.getAdvertisingType() != GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED) && |
sjallouli | 0:a948f5f3904c | 262 | (params.getTimeout() > GAP_ADV_PARAMS_TIMEOUT_MAX)) { |
sjallouli | 0:a948f5f3904c | 263 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 264 | } |
sjallouli | 0:a948f5f3904c | 265 | |
sjallouli | 0:a948f5f3904c | 266 | tBleStatus ret; |
sjallouli | 0:a948f5f3904c | 267 | const LongUUIDBytes_t HRM_SERVICE_UUID_128 = {0x18, 0x0D}; |
sjallouli | 0:a948f5f3904c | 268 | /* set scan response data */ |
sjallouli | 0:a948f5f3904c | 269 | hci_le_set_scan_resp_data(scan_rsp_length, scan_response_payload); /*int hci_le_set_scan_resp_data(uint8_t length, const uint8_t data[]);*/ |
sjallouli | 0:a948f5f3904c | 270 | |
sjallouli | 0:a948f5f3904c | 271 | /*aci_gap_set_discoverable(Advertising_Event_Type, Adv_min_intvl, Adv_Max_Intvl, Addr_Type, Adv_Filter_Policy, |
sjallouli | 0:a948f5f3904c | 272 | Local_Name_Length, local_name, service_uuid_length, service_uuid_list, Slave_conn_intvl_min, Slave_conn_intvl_max);*/ |
sjallouli | 0:a948f5f3904c | 273 | /*LINK_LAYER.H DESCRIBES THE ADVERTISING TYPES*/ |
sjallouli | 0:a948f5f3904c | 274 | |
sjallouli | 0:a948f5f3904c | 275 | char* name = NULL; |
sjallouli | 0:a948f5f3904c | 276 | uint8_t nameLen = 0; |
sjallouli | 0:a948f5f3904c | 277 | if(local_name!=NULL) { |
sjallouli | 0:a948f5f3904c | 278 | name = (char*)local_name; |
sjallouli | 0:a948f5f3904c | 279 | DEBUG("name=%s\n\r", name); |
sjallouli | 0:a948f5f3904c | 280 | nameLen = local_name_length; |
sjallouli | 0:a948f5f3904c | 281 | } else { |
sjallouli | 0:a948f5f3904c | 282 | char str[] = "ST_BLE_DEV"; |
sjallouli | 0:a948f5f3904c | 283 | name = new char[strlen(str)+1]; |
sjallouli | 0:a948f5f3904c | 284 | name[0] = AD_TYPE_COMPLETE_LOCAL_NAME; |
sjallouli | 0:a948f5f3904c | 285 | strcpy(name+1, str); |
sjallouli | 0:a948f5f3904c | 286 | nameLen = strlen(name); |
sjallouli | 0:a948f5f3904c | 287 | DEBUG("nameLen=%d\n\r", nameLen); |
sjallouli | 0:a948f5f3904c | 288 | DEBUG("name=%s\n\r", name); |
sjallouli | 0:a948f5f3904c | 289 | } |
sjallouli | 0:a948f5f3904c | 290 | |
sjallouli | 0:a948f5f3904c | 291 | |
sjallouli | 0:a948f5f3904c | 292 | advtInterval = params.getInterval(); // set advtInterval in case it is not already set by user application |
sjallouli | 0:a948f5f3904c | 293 | ret = aci_gap_set_discoverable(params.getAdvertisingType(), // Advertising_Event_Type |
sjallouli | 0:a948f5f3904c | 294 | 0, // Adv_Interval_Min |
sjallouli | 0:a948f5f3904c | 295 | advtInterval, // Adv_Interval_Max |
sjallouli | 0:a948f5f3904c | 296 | PUBLIC_ADDR, // Address_Type |
sjallouli | 0:a948f5f3904c | 297 | NO_WHITE_LIST_USE, // Adv_Filter_Policy |
sjallouli | 0:a948f5f3904c | 298 | nameLen, //local_name_length, // Local_Name_Length |
sjallouli | 0:a948f5f3904c | 299 | (const char*)name, //local_name, // Local_Name |
sjallouli | 0:a948f5f3904c | 300 | servUuidlength, //Service_Uuid_Length |
sjallouli | 0:a948f5f3904c | 301 | servUuidData, //Service_Uuid_List |
sjallouli | 0:a948f5f3904c | 302 | 0, // Slave_Conn_Interval_Min |
sjallouli | 0:a948f5f3904c | 303 | 0); // Slave_Conn_Interval_Max |
sjallouli | 0:a948f5f3904c | 304 | |
sjallouli | 0:a948f5f3904c | 305 | state.advertising = 1; |
sjallouli | 0:a948f5f3904c | 306 | |
sjallouli | 0:a948f5f3904c | 307 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 308 | } |
sjallouli | 0:a948f5f3904c | 309 | |
sjallouli | 0:a948f5f3904c | 310 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 311 | /*! |
sjallouli | 0:a948f5f3904c | 312 | @brief Stops the BLE HW and disconnects from any devices |
sjallouli | 0:a948f5f3904c | 313 | |
sjallouli | 0:a948f5f3904c | 314 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 315 | |
sjallouli | 0:a948f5f3904c | 316 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 317 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 318 | |
sjallouli | 0:a948f5f3904c | 319 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 320 | |
sjallouli | 0:a948f5f3904c | 321 | @code |
sjallouli | 0:a948f5f3904c | 322 | |
sjallouli | 0:a948f5f3904c | 323 | @endcode |
sjallouli | 0:a948f5f3904c | 324 | */ |
sjallouli | 0:a948f5f3904c | 325 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 326 | ble_error_t BlueNRGGap::stopAdvertising(void) |
sjallouli | 0:a948f5f3904c | 327 | { |
sjallouli | 0:a948f5f3904c | 328 | tBleStatus ret; |
sjallouli | 0:a948f5f3904c | 329 | |
sjallouli | 0:a948f5f3904c | 330 | if(state.advertising == 1) { |
sjallouli | 0:a948f5f3904c | 331 | //Set non-discoverable to stop advertising |
sjallouli | 0:a948f5f3904c | 332 | ret = aci_gap_set_non_discoverable(); |
sjallouli | 0:a948f5f3904c | 333 | |
sjallouli | 0:a948f5f3904c | 334 | if (ret != BLE_STATUS_SUCCESS){ |
sjallouli | 0:a948f5f3904c | 335 | DEBUG("Error in stopping advertisement!!\n\r") ; |
sjallouli | 0:a948f5f3904c | 336 | return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value |
sjallouli | 0:a948f5f3904c | 337 | //FIXME: Define Error values equivalent to BlueNRG Error Codes. |
sjallouli | 0:a948f5f3904c | 338 | } |
sjallouli | 0:a948f5f3904c | 339 | DEBUG("Advertisement stopped!!\n\r") ; |
sjallouli | 0:a948f5f3904c | 340 | //Set GapState_t::advertising state |
sjallouli | 0:a948f5f3904c | 341 | state.advertising = 0; |
sjallouli | 0:a948f5f3904c | 342 | } |
sjallouli | 0:a948f5f3904c | 343 | |
sjallouli | 0:a948f5f3904c | 344 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 345 | } |
sjallouli | 0:a948f5f3904c | 346 | |
sjallouli | 0:a948f5f3904c | 347 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 348 | /*! |
sjallouli | 0:a948f5f3904c | 349 | @brief Disconnects if we are connected to a central device |
sjallouli | 0:a948f5f3904c | 350 | |
sjallouli | 0:a948f5f3904c | 351 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 352 | |
sjallouli | 0:a948f5f3904c | 353 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 354 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 355 | |
sjallouli | 0:a948f5f3904c | 356 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 357 | |
sjallouli | 0:a948f5f3904c | 358 | @code |
sjallouli | 0:a948f5f3904c | 359 | |
sjallouli | 0:a948f5f3904c | 360 | @endcode |
sjallouli | 0:a948f5f3904c | 361 | */ |
sjallouli | 0:a948f5f3904c | 362 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 363 | ble_error_t BlueNRGGap::disconnect(Gap::DisconnectionReason_t reason) |
sjallouli | 0:a948f5f3904c | 364 | { |
sjallouli | 0:a948f5f3904c | 365 | tBleStatus ret; |
sjallouli | 0:a948f5f3904c | 366 | //For Reason codes check BlueTooth HCI Spec |
sjallouli | 0:a948f5f3904c | 367 | |
sjallouli | 0:a948f5f3904c | 368 | if(m_connectionHandle != BLE_CONN_HANDLE_INVALID) { |
sjallouli | 0:a948f5f3904c | 369 | ret = aci_gap_terminate(m_connectionHandle, 0x16);//0x16 Connection Terminated by Local Host. |
sjallouli | 0:a948f5f3904c | 370 | |
sjallouli | 0:a948f5f3904c | 371 | if (ret != BLE_STATUS_SUCCESS){ |
sjallouli | 0:a948f5f3904c | 372 | DEBUG("Error in GAP termination!!\n\r") ; |
sjallouli | 0:a948f5f3904c | 373 | return BLE_ERROR_PARAM_OUT_OF_RANGE ; //Not correct Error Value |
sjallouli | 0:a948f5f3904c | 374 | //FIXME: Define Error values equivalent to BlueNRG Error Codes. |
sjallouli | 0:a948f5f3904c | 375 | } |
sjallouli | 0:a948f5f3904c | 376 | |
sjallouli | 0:a948f5f3904c | 377 | //DEBUG("Disconnected from localhost!!\n\r") ; |
sjallouli | 0:a948f5f3904c | 378 | m_connectionHandle = BLE_CONN_HANDLE_INVALID; |
sjallouli | 0:a948f5f3904c | 379 | } |
sjallouli | 0:a948f5f3904c | 380 | |
sjallouli | 0:a948f5f3904c | 381 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 382 | } |
sjallouli | 0:a948f5f3904c | 383 | |
sjallouli | 0:a948f5f3904c | 384 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 385 | /*! |
sjallouli | 0:a948f5f3904c | 386 | @brief Sets the 16-bit connection handle |
sjallouli | 0:a948f5f3904c | 387 | */ |
sjallouli | 0:a948f5f3904c | 388 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 389 | void BlueNRGGap::setConnectionHandle(uint16_t con_handle) |
sjallouli | 0:a948f5f3904c | 390 | { |
sjallouli | 0:a948f5f3904c | 391 | m_connectionHandle = con_handle; |
sjallouli | 0:a948f5f3904c | 392 | } |
sjallouli | 0:a948f5f3904c | 393 | |
sjallouli | 0:a948f5f3904c | 394 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 395 | /*! |
sjallouli | 0:a948f5f3904c | 396 | @brief Gets the 16-bit connection handle |
sjallouli | 0:a948f5f3904c | 397 | */ |
sjallouli | 0:a948f5f3904c | 398 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 399 | uint16_t BlueNRGGap::getConnectionHandle(void) |
sjallouli | 0:a948f5f3904c | 400 | { |
sjallouli | 0:a948f5f3904c | 401 | return m_connectionHandle; |
sjallouli | 0:a948f5f3904c | 402 | } |
sjallouli | 0:a948f5f3904c | 403 | |
sjallouli | 0:a948f5f3904c | 404 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 405 | /*! |
sjallouli | 0:a948f5f3904c | 406 | @brief Sets the BLE device address. SetAddress will reset the BLE |
sjallouli | 0:a948f5f3904c | 407 | device and re-initialize BTLE. Will not start advertising. |
sjallouli | 0:a948f5f3904c | 408 | |
sjallouli | 0:a948f5f3904c | 409 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 410 | |
sjallouli | 0:a948f5f3904c | 411 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 412 | |
sjallouli | 0:a948f5f3904c | 413 | @code |
sjallouli | 0:a948f5f3904c | 414 | |
sjallouli | 0:a948f5f3904c | 415 | @endcode |
sjallouli | 0:a948f5f3904c | 416 | */ |
sjallouli | 0:a948f5f3904c | 417 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 418 | ble_error_t BlueNRGGap::setAddress(addr_type_t type, const uint8_t address[6]) |
sjallouli | 0:a948f5f3904c | 419 | { |
sjallouli | 0:a948f5f3904c | 420 | tBleStatus ret; |
sjallouli | 0:a948f5f3904c | 421 | |
sjallouli | 0:a948f5f3904c | 422 | if (type > ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE) { |
sjallouli | 0:a948f5f3904c | 423 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 424 | } |
sjallouli | 0:a948f5f3904c | 425 | |
sjallouli | 0:a948f5f3904c | 426 | //copy address to bdAddr[6] |
sjallouli | 0:a948f5f3904c | 427 | for(int i=0; i<BDADDR_SIZE; i++) { |
sjallouli | 0:a948f5f3904c | 428 | bdaddr[i] = address[i]; |
sjallouli | 0:a948f5f3904c | 429 | //DEBUG("i[%d]:0x%x\n\r",i,bdaddr[i]); |
sjallouli | 0:a948f5f3904c | 430 | } |
sjallouli | 0:a948f5f3904c | 431 | |
sjallouli | 0:a948f5f3904c | 432 | if(!isSetAddress) isSetAddress = true; |
sjallouli | 0:a948f5f3904c | 433 | |
sjallouli | 0:a948f5f3904c | 434 | //Re-Init the BTLE Device with SetAddress as true |
sjallouli | 0:a948f5f3904c | 435 | //if(BlueNRGDevice::getIsInitialized())//Re-init only initialization is already done |
sjallouli | 0:a948f5f3904c | 436 | btle_init(isSetAddress); |
sjallouli | 0:a948f5f3904c | 437 | |
sjallouli | 0:a948f5f3904c | 438 | //if (ret==BLE_STATUS_SUCCESS) |
sjallouli | 0:a948f5f3904c | 439 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 440 | } |
sjallouli | 0:a948f5f3904c | 441 | |
sjallouli | 0:a948f5f3904c | 442 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 443 | /*! |
sjallouli | 0:a948f5f3904c | 444 | @brief Returns boolean if the address of the device has been set |
sjallouli | 0:a948f5f3904c | 445 | or not |
sjallouli | 0:a948f5f3904c | 446 | |
sjallouli | 0:a948f5f3904c | 447 | @returns bool |
sjallouli | 0:a948f5f3904c | 448 | |
sjallouli | 0:a948f5f3904c | 449 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 450 | |
sjallouli | 0:a948f5f3904c | 451 | @code |
sjallouli | 0:a948f5f3904c | 452 | |
sjallouli | 0:a948f5f3904c | 453 | @endcode |
sjallouli | 0:a948f5f3904c | 454 | */ |
sjallouli | 0:a948f5f3904c | 455 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 456 | bool BlueNRGGap::getIsSetAddress() |
sjallouli | 0:a948f5f3904c | 457 | { |
sjallouli | 0:a948f5f3904c | 458 | return isSetAddress; |
sjallouli | 0:a948f5f3904c | 459 | } |
sjallouli | 0:a948f5f3904c | 460 | |
sjallouli | 0:a948f5f3904c | 461 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 462 | /*! |
sjallouli | 0:a948f5f3904c | 463 | @brief Returns the address of the device if set |
sjallouli | 0:a948f5f3904c | 464 | |
sjallouli | 0:a948f5f3904c | 465 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 466 | |
sjallouli | 0:a948f5f3904c | 467 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 468 | |
sjallouli | 0:a948f5f3904c | 469 | @code |
sjallouli | 0:a948f5f3904c | 470 | |
sjallouli | 0:a948f5f3904c | 471 | @endcode |
sjallouli | 0:a948f5f3904c | 472 | */ |
sjallouli | 0:a948f5f3904c | 473 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 474 | tHalUint8* BlueNRGGap::getAddress() |
sjallouli | 0:a948f5f3904c | 475 | { |
sjallouli | 0:a948f5f3904c | 476 | if(isSetAddress) |
sjallouli | 0:a948f5f3904c | 477 | return bdaddr; |
sjallouli | 0:a948f5f3904c | 478 | else return NULL; |
sjallouli | 0:a948f5f3904c | 479 | } |
sjallouli | 0:a948f5f3904c | 480 | |
sjallouli | 0:a948f5f3904c | 481 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 482 | /*! |
sjallouli | 0:a948f5f3904c | 483 | @brief obtains preferred connection params |
sjallouli | 0:a948f5f3904c | 484 | |
sjallouli | 0:a948f5f3904c | 485 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 486 | |
sjallouli | 0:a948f5f3904c | 487 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 488 | |
sjallouli | 0:a948f5f3904c | 489 | @code |
sjallouli | 0:a948f5f3904c | 490 | |
sjallouli | 0:a948f5f3904c | 491 | @endcode |
sjallouli | 0:a948f5f3904c | 492 | */ |
sjallouli | 0:a948f5f3904c | 493 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 494 | ble_error_t BlueNRGGap::getPreferredConnectionParams(ConnectionParams_t *params) |
sjallouli | 0:a948f5f3904c | 495 | { |
sjallouli | 0:a948f5f3904c | 496 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 497 | } |
sjallouli | 0:a948f5f3904c | 498 | |
sjallouli | 0:a948f5f3904c | 499 | |
sjallouli | 0:a948f5f3904c | 500 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 501 | /*! |
sjallouli | 0:a948f5f3904c | 502 | @brief sets preferred connection params |
sjallouli | 0:a948f5f3904c | 503 | |
sjallouli | 0:a948f5f3904c | 504 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 505 | |
sjallouli | 0:a948f5f3904c | 506 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 507 | |
sjallouli | 0:a948f5f3904c | 508 | @code |
sjallouli | 0:a948f5f3904c | 509 | |
sjallouli | 0:a948f5f3904c | 510 | @endcode |
sjallouli | 0:a948f5f3904c | 511 | */ |
sjallouli | 0:a948f5f3904c | 512 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 513 | ble_error_t BlueNRGGap::setPreferredConnectionParams(const ConnectionParams_t *params) |
sjallouli | 0:a948f5f3904c | 514 | { |
sjallouli | 0:a948f5f3904c | 515 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 516 | } |
sjallouli | 0:a948f5f3904c | 517 | |
sjallouli | 0:a948f5f3904c | 518 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 519 | /*! |
sjallouli | 0:a948f5f3904c | 520 | @brief updates preferred connection params |
sjallouli | 0:a948f5f3904c | 521 | |
sjallouli | 0:a948f5f3904c | 522 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 523 | |
sjallouli | 0:a948f5f3904c | 524 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 525 | |
sjallouli | 0:a948f5f3904c | 526 | @code |
sjallouli | 0:a948f5f3904c | 527 | |
sjallouli | 0:a948f5f3904c | 528 | @endcode |
sjallouli | 0:a948f5f3904c | 529 | */ |
sjallouli | 0:a948f5f3904c | 530 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 531 | ble_error_t BlueNRGGap::updateConnectionParams(Handle_t handle, const ConnectionParams_t *params) |
sjallouli | 0:a948f5f3904c | 532 | { |
sjallouli | 0:a948f5f3904c | 533 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 534 | } |
sjallouli | 0:a948f5f3904c | 535 | |
sjallouli | 0:a948f5f3904c | 536 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 537 | /*! |
sjallouli | 0:a948f5f3904c | 538 | @brief sets device name characteristic |
sjallouli | 0:a948f5f3904c | 539 | |
sjallouli | 0:a948f5f3904c | 540 | @param[in] deviceName |
sjallouli | 0:a948f5f3904c | 541 | pointer to device name to be set |
sjallouli | 0:a948f5f3904c | 542 | |
sjallouli | 0:a948f5f3904c | 543 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 544 | |
sjallouli | 0:a948f5f3904c | 545 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 546 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 547 | |
sjallouli | 0:a948f5f3904c | 548 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 549 | |
sjallouli | 0:a948f5f3904c | 550 | @code |
sjallouli | 0:a948f5f3904c | 551 | |
sjallouli | 0:a948f5f3904c | 552 | @endcode |
sjallouli | 0:a948f5f3904c | 553 | */ |
sjallouli | 0:a948f5f3904c | 554 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 555 | ble_error_t BlueNRGGap::setDeviceName(const uint8_t *deviceName) |
sjallouli | 0:a948f5f3904c | 556 | { |
sjallouli | 0:a948f5f3904c | 557 | int ret; |
sjallouli | 0:a948f5f3904c | 558 | uint8_t nameLen = 0; |
sjallouli | 0:a948f5f3904c | 559 | |
sjallouli | 0:a948f5f3904c | 560 | DeviceName = (uint8_t *)deviceName; |
sjallouli | 0:a948f5f3904c | 561 | //DEBUG("SetDeviceName=%s\n\r", DeviceName); |
sjallouli | 0:a948f5f3904c | 562 | |
sjallouli | 0:a948f5f3904c | 563 | nameLen = strlen((const char*)DeviceName); |
sjallouli | 0:a948f5f3904c | 564 | //DEBUG("DeviceName Size=%d\n\r", nameLen); |
sjallouli | 0:a948f5f3904c | 565 | |
sjallouli | 0:a948f5f3904c | 566 | ret = aci_gatt_update_char_value(g_gap_service_handle, |
sjallouli | 0:a948f5f3904c | 567 | g_device_name_char_handle, |
sjallouli | 0:a948f5f3904c | 568 | 0, |
sjallouli | 0:a948f5f3904c | 569 | nameLen, |
sjallouli | 0:a948f5f3904c | 570 | (tHalUint8 *)DeviceName); |
sjallouli | 0:a948f5f3904c | 571 | |
sjallouli | 0:a948f5f3904c | 572 | if(ret){ |
sjallouli | 0:a948f5f3904c | 573 | DEBUG("device set name failed\n\r"); |
sjallouli | 0:a948f5f3904c | 574 | return BLE_ERROR_PARAM_OUT_OF_RANGE;//TODO:Wrong error code |
sjallouli | 0:a948f5f3904c | 575 | } |
sjallouli | 0:a948f5f3904c | 576 | |
sjallouli | 0:a948f5f3904c | 577 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 578 | } |
sjallouli | 0:a948f5f3904c | 579 | |
sjallouli | 0:a948f5f3904c | 580 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 581 | /*! |
sjallouli | 0:a948f5f3904c | 582 | @brief gets device name characteristic |
sjallouli | 0:a948f5f3904c | 583 | |
sjallouli | 0:a948f5f3904c | 584 | @param[in] deviceName |
sjallouli | 0:a948f5f3904c | 585 | pointer to device name |
sjallouli | 0:a948f5f3904c | 586 | |
sjallouli | 0:a948f5f3904c | 587 | |
sjallouli | 0:a948f5f3904c | 588 | @param[in] lengthP |
sjallouli | 0:a948f5f3904c | 589 | pointer to device name length |
sjallouli | 0:a948f5f3904c | 590 | |
sjallouli | 0:a948f5f3904c | 591 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 592 | |
sjallouli | 0:a948f5f3904c | 593 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 594 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 595 | |
sjallouli | 0:a948f5f3904c | 596 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 597 | |
sjallouli | 0:a948f5f3904c | 598 | @code |
sjallouli | 0:a948f5f3904c | 599 | |
sjallouli | 0:a948f5f3904c | 600 | @endcode |
sjallouli | 0:a948f5f3904c | 601 | */ |
sjallouli | 0:a948f5f3904c | 602 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 603 | ble_error_t BlueNRGGap::getDeviceName(uint8_t *deviceName, unsigned *lengthP) |
sjallouli | 0:a948f5f3904c | 604 | { |
sjallouli | 0:a948f5f3904c | 605 | int ret; |
sjallouli | 0:a948f5f3904c | 606 | |
sjallouli | 0:a948f5f3904c | 607 | if(DeviceName==NULL) |
sjallouli | 0:a948f5f3904c | 608 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 609 | |
sjallouli | 0:a948f5f3904c | 610 | strcpy((char*)deviceName, (const char*)DeviceName); |
sjallouli | 0:a948f5f3904c | 611 | //DEBUG("GetDeviceName=%s\n\r", deviceName); |
sjallouli | 0:a948f5f3904c | 612 | |
sjallouli | 0:a948f5f3904c | 613 | *lengthP = strlen((const char*)DeviceName); |
sjallouli | 0:a948f5f3904c | 614 | //DEBUG("DeviceName Size=%d\n\r", *lengthP); |
sjallouli | 0:a948f5f3904c | 615 | |
sjallouli | 0:a948f5f3904c | 616 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 617 | } |
sjallouli | 0:a948f5f3904c | 618 | |
sjallouli | 0:a948f5f3904c | 619 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 620 | /*! |
sjallouli | 0:a948f5f3904c | 621 | @brief sets device appearance characteristic |
sjallouli | 0:a948f5f3904c | 622 | |
sjallouli | 0:a948f5f3904c | 623 | @param[in] appearance |
sjallouli | 0:a948f5f3904c | 624 | device appearance |
sjallouli | 0:a948f5f3904c | 625 | |
sjallouli | 0:a948f5f3904c | 626 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 627 | |
sjallouli | 0:a948f5f3904c | 628 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 629 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 630 | |
sjallouli | 0:a948f5f3904c | 631 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 632 | |
sjallouli | 0:a948f5f3904c | 633 | @code |
sjallouli | 0:a948f5f3904c | 634 | |
sjallouli | 0:a948f5f3904c | 635 | @endcode |
sjallouli | 0:a948f5f3904c | 636 | */ |
sjallouli | 0:a948f5f3904c | 637 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 638 | ble_error_t BlueNRGGap::setAppearance(uint16_t appearance) |
sjallouli | 0:a948f5f3904c | 639 | { |
sjallouli | 0:a948f5f3904c | 640 | /* |
sjallouli | 0:a948f5f3904c | 641 | Tested with GapAdvertisingData::GENERIC_PHONE. |
sjallouli | 0:a948f5f3904c | 642 | for other appearances BLE Scanner android app is not behaving properly |
sjallouli | 0:a948f5f3904c | 643 | */ |
sjallouli | 0:a948f5f3904c | 644 | //char deviceAppearance[2]; |
sjallouli | 0:a948f5f3904c | 645 | STORE_LE_16(deviceAppearance, appearance); |
sjallouli | 0:a948f5f3904c | 646 | DEBUG("input: incoming = %d deviceAppearance= 0x%x 0x%x\n\r", appearance, deviceAppearance[1], deviceAppearance[0]); |
sjallouli | 0:a948f5f3904c | 647 | |
sjallouli | 0:a948f5f3904c | 648 | aci_gatt_update_char_value(g_gap_service_handle, g_appearance_char_handle, 0, 2, (tHalUint8 *)deviceAppearance); |
sjallouli | 0:a948f5f3904c | 649 | |
sjallouli | 0:a948f5f3904c | 650 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 651 | } |
sjallouli | 0:a948f5f3904c | 652 | |
sjallouli | 0:a948f5f3904c | 653 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 654 | /*! |
sjallouli | 0:a948f5f3904c | 655 | @brief gets device appearance |
sjallouli | 0:a948f5f3904c | 656 | |
sjallouli | 0:a948f5f3904c | 657 | @param[in] appearance |
sjallouli | 0:a948f5f3904c | 658 | pointer to device appearance value |
sjallouli | 0:a948f5f3904c | 659 | |
sjallouli | 0:a948f5f3904c | 660 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 661 | |
sjallouli | 0:a948f5f3904c | 662 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 663 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 664 | |
sjallouli | 0:a948f5f3904c | 665 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 666 | |
sjallouli | 0:a948f5f3904c | 667 | @code |
sjallouli | 0:a948f5f3904c | 668 | |
sjallouli | 0:a948f5f3904c | 669 | @endcode |
sjallouli | 0:a948f5f3904c | 670 | */ |
sjallouli | 0:a948f5f3904c | 671 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 672 | ble_error_t BlueNRGGap::getAppearance(uint16_t *appearanceP) |
sjallouli | 0:a948f5f3904c | 673 | { |
sjallouli | 0:a948f5f3904c | 674 | uint16_t devP; |
sjallouli | 0:a948f5f3904c | 675 | if(!appearanceP) return BLE_ERROR_PARAM_OUT_OF_RANGE; |
sjallouli | 0:a948f5f3904c | 676 | devP = ((uint16_t)(0x0000|deviceAppearance[0])) | (((uint16_t)(0x0000|deviceAppearance[1]))<<8); |
sjallouli | 0:a948f5f3904c | 677 | strcpy((char*)appearanceP, (const char*)&devP); |
sjallouli | 0:a948f5f3904c | 678 | |
sjallouli | 0:a948f5f3904c | 679 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 680 | } |