BLE lib
Fork of Nucleo_BLE_BlueNRG by
BlueNRGDevice.cpp@0:a948f5f3904c, 2014-12-19 (annotated)
- Committer:
- sjallouli
- Date:
- Fri Dec 19 18:56:07 2014 +0000
- Revision:
- 0:a948f5f3904c
BLE_BlueNRG for Nucleo board
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 "mbed.h" |
sjallouli | 0:a948f5f3904c | 18 | #include "BlueNRGDevice.h" |
sjallouli | 0:a948f5f3904c | 19 | #include "BlueNRGGap.h" |
sjallouli | 0:a948f5f3904c | 20 | #include "BlueNRGGattServer.h" |
sjallouli | 0:a948f5f3904c | 21 | |
sjallouli | 0:a948f5f3904c | 22 | #include "btle.h" |
sjallouli | 0:a948f5f3904c | 23 | #include "Utils.h" |
sjallouli | 0:a948f5f3904c | 24 | #include "osal.h" |
sjallouli | 0:a948f5f3904c | 25 | |
sjallouli | 0:a948f5f3904c | 26 | /** |
sjallouli | 0:a948f5f3904c | 27 | * The singleton which represents the BlueNRG transport for the BLEDevice. |
sjallouli | 0:a948f5f3904c | 28 | */ |
sjallouli | 0:a948f5f3904c | 29 | static BlueNRGDevice deviceInstance; |
sjallouli | 0:a948f5f3904c | 30 | |
sjallouli | 0:a948f5f3904c | 31 | /** |
sjallouli | 0:a948f5f3904c | 32 | * BLE-API requires an implementation of the following function in order to |
sjallouli | 0:a948f5f3904c | 33 | * obtain its transport handle. |
sjallouli | 0:a948f5f3904c | 34 | */ |
sjallouli | 0:a948f5f3904c | 35 | BLEDeviceInstanceBase * |
sjallouli | 0:a948f5f3904c | 36 | createBLEDeviceInstance(void) |
sjallouli | 0:a948f5f3904c | 37 | { |
sjallouli | 0:a948f5f3904c | 38 | return (&deviceInstance); |
sjallouli | 0:a948f5f3904c | 39 | } |
sjallouli | 0:a948f5f3904c | 40 | |
sjallouli | 0:a948f5f3904c | 41 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 42 | /*! |
sjallouli | 0:a948f5f3904c | 43 | @brief Constructor |
sjallouli | 0:a948f5f3904c | 44 | */ |
sjallouli | 0:a948f5f3904c | 45 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 46 | BlueNRGDevice::BlueNRGDevice(void) |
sjallouli | 0:a948f5f3904c | 47 | { |
sjallouli | 0:a948f5f3904c | 48 | isInitialized = false; |
sjallouli | 0:a948f5f3904c | 49 | } |
sjallouli | 0:a948f5f3904c | 50 | |
sjallouli | 0:a948f5f3904c | 51 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 52 | /*! |
sjallouli | 0:a948f5f3904c | 53 | @brief Destructor |
sjallouli | 0:a948f5f3904c | 54 | */ |
sjallouli | 0:a948f5f3904c | 55 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 56 | BlueNRGDevice::~BlueNRGDevice(void) |
sjallouli | 0:a948f5f3904c | 57 | { |
sjallouli | 0:a948f5f3904c | 58 | } |
sjallouli | 0:a948f5f3904c | 59 | |
sjallouli | 0:a948f5f3904c | 60 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 61 | /*! |
sjallouli | 0:a948f5f3904c | 62 | @brief Initialises anything required to start using BLE |
sjallouli | 0:a948f5f3904c | 63 | |
sjallouli | 0:a948f5f3904c | 64 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 65 | |
sjallouli | 0:a948f5f3904c | 66 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 67 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 68 | |
sjallouli | 0:a948f5f3904c | 69 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 70 | |
sjallouli | 0:a948f5f3904c | 71 | @code |
sjallouli | 0:a948f5f3904c | 72 | |
sjallouli | 0:a948f5f3904c | 73 | @endcode |
sjallouli | 0:a948f5f3904c | 74 | */ |
sjallouli | 0:a948f5f3904c | 75 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 76 | ble_error_t BlueNRGDevice::init(void) |
sjallouli | 0:a948f5f3904c | 77 | { |
sjallouli | 0:a948f5f3904c | 78 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
sjallouli | 0:a948f5f3904c | 79 | btle_init(BlueNRGGap::getInstance().getIsSetAddress()); |
sjallouli | 0:a948f5f3904c | 80 | |
sjallouli | 0:a948f5f3904c | 81 | isInitialized = true; |
sjallouli | 0:a948f5f3904c | 82 | |
sjallouli | 0:a948f5f3904c | 83 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 84 | } |
sjallouli | 0:a948f5f3904c | 85 | |
sjallouli | 0:a948f5f3904c | 86 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 87 | /*! |
sjallouli | 0:a948f5f3904c | 88 | @brief Resets the BLE HW, removing any existing services and |
sjallouli | 0:a948f5f3904c | 89 | characteristics |
sjallouli | 0:a948f5f3904c | 90 | |
sjallouli | 0:a948f5f3904c | 91 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 92 | |
sjallouli | 0:a948f5f3904c | 93 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 94 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 95 | |
sjallouli | 0:a948f5f3904c | 96 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 97 | |
sjallouli | 0:a948f5f3904c | 98 | @code |
sjallouli | 0:a948f5f3904c | 99 | |
sjallouli | 0:a948f5f3904c | 100 | @endcode |
sjallouli | 0:a948f5f3904c | 101 | */ |
sjallouli | 0:a948f5f3904c | 102 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 103 | ble_error_t BlueNRGDevice::reset(void) |
sjallouli | 0:a948f5f3904c | 104 | { |
sjallouli | 0:a948f5f3904c | 105 | wait(0.5); |
sjallouli | 0:a948f5f3904c | 106 | |
sjallouli | 0:a948f5f3904c | 107 | /* Reset BlueNRG SPI interface */ |
sjallouli | 0:a948f5f3904c | 108 | BlueNRG_RST(); |
sjallouli | 0:a948f5f3904c | 109 | |
sjallouli | 0:a948f5f3904c | 110 | /* Wait for the radio to come back up */ |
sjallouli | 0:a948f5f3904c | 111 | wait(1); |
sjallouli | 0:a948f5f3904c | 112 | |
sjallouli | 0:a948f5f3904c | 113 | isInitialized = false; |
sjallouli | 0:a948f5f3904c | 114 | |
sjallouli | 0:a948f5f3904c | 115 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 116 | } |
sjallouli | 0:a948f5f3904c | 117 | |
sjallouli | 0:a948f5f3904c | 118 | void BlueNRGDevice::waitForEvent(void) |
sjallouli | 0:a948f5f3904c | 119 | { |
sjallouli | 0:a948f5f3904c | 120 | HCI_Process();//Send App Events?? |
sjallouli | 0:a948f5f3904c | 121 | |
sjallouli | 0:a948f5f3904c | 122 | } |
sjallouli | 0:a948f5f3904c | 123 | |
sjallouli | 0:a948f5f3904c | 124 | |
sjallouli | 0:a948f5f3904c | 125 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 126 | /*! |
sjallouli | 0:a948f5f3904c | 127 | @brief get GAP version |
sjallouli | 0:a948f5f3904c | 128 | |
sjallouli | 0:a948f5f3904c | 129 | @returns char * |
sjallouli | 0:a948f5f3904c | 130 | |
sjallouli | 0:a948f5f3904c | 131 | @retval pointer to version string |
sjallouli | 0:a948f5f3904c | 132 | |
sjallouli | 0:a948f5f3904c | 133 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 134 | |
sjallouli | 0:a948f5f3904c | 135 | @code |
sjallouli | 0:a948f5f3904c | 136 | |
sjallouli | 0:a948f5f3904c | 137 | @endcode |
sjallouli | 0:a948f5f3904c | 138 | */ |
sjallouli | 0:a948f5f3904c | 139 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 140 | const char *BlueNRGDevice::getVersion(void) |
sjallouli | 0:a948f5f3904c | 141 | { |
sjallouli | 0:a948f5f3904c | 142 | char *version = new char[6]; |
sjallouli | 0:a948f5f3904c | 143 | memcpy((void *)version, "1.0.0", 5); |
sjallouli | 0:a948f5f3904c | 144 | return version; |
sjallouli | 0:a948f5f3904c | 145 | } |
sjallouli | 0:a948f5f3904c | 146 | |
sjallouli | 0:a948f5f3904c | 147 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 148 | /*! |
sjallouli | 0:a948f5f3904c | 149 | @brief get init state |
sjallouli | 0:a948f5f3904c | 150 | |
sjallouli | 0:a948f5f3904c | 151 | @returns bool |
sjallouli | 0:a948f5f3904c | 152 | |
sjallouli | 0:a948f5f3904c | 153 | @retval |
sjallouli | 0:a948f5f3904c | 154 | |
sjallouli | 0:a948f5f3904c | 155 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 156 | |
sjallouli | 0:a948f5f3904c | 157 | @code |
sjallouli | 0:a948f5f3904c | 158 | |
sjallouli | 0:a948f5f3904c | 159 | @endcode |
sjallouli | 0:a948f5f3904c | 160 | */ |
sjallouli | 0:a948f5f3904c | 161 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 162 | bool BlueNRGDevice::getIsInitialized(void) |
sjallouli | 0:a948f5f3904c | 163 | { |
sjallouli | 0:a948f5f3904c | 164 | return isInitialized; |
sjallouli | 0:a948f5f3904c | 165 | } |
sjallouli | 0:a948f5f3904c | 166 | |
sjallouli | 0:a948f5f3904c | 167 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 168 | /*! |
sjallouli | 0:a948f5f3904c | 169 | @brief get reference to GAP object |
sjallouli | 0:a948f5f3904c | 170 | |
sjallouli | 0:a948f5f3904c | 171 | @returns Gap& |
sjallouli | 0:a948f5f3904c | 172 | |
sjallouli | 0:a948f5f3904c | 173 | @retval reference to gap object |
sjallouli | 0:a948f5f3904c | 174 | |
sjallouli | 0:a948f5f3904c | 175 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 176 | |
sjallouli | 0:a948f5f3904c | 177 | @code |
sjallouli | 0:a948f5f3904c | 178 | |
sjallouli | 0:a948f5f3904c | 179 | @endcode |
sjallouli | 0:a948f5f3904c | 180 | */ |
sjallouli | 0:a948f5f3904c | 181 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 182 | Gap &BlueNRGDevice::getGap() |
sjallouli | 0:a948f5f3904c | 183 | { |
sjallouli | 0:a948f5f3904c | 184 | return BlueNRGGap::getInstance(); |
sjallouli | 0:a948f5f3904c | 185 | } |
sjallouli | 0:a948f5f3904c | 186 | |
sjallouli | 0:a948f5f3904c | 187 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 188 | /*! |
sjallouli | 0:a948f5f3904c | 189 | @brief get reference to GATT server object |
sjallouli | 0:a948f5f3904c | 190 | |
sjallouli | 0:a948f5f3904c | 191 | @returns GattServer& |
sjallouli | 0:a948f5f3904c | 192 | |
sjallouli | 0:a948f5f3904c | 193 | @retval reference to GATT server object |
sjallouli | 0:a948f5f3904c | 194 | |
sjallouli | 0:a948f5f3904c | 195 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 196 | |
sjallouli | 0:a948f5f3904c | 197 | @code |
sjallouli | 0:a948f5f3904c | 198 | |
sjallouli | 0:a948f5f3904c | 199 | @endcode |
sjallouli | 0:a948f5f3904c | 200 | */ |
sjallouli | 0:a948f5f3904c | 201 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 202 | GattServer &BlueNRGDevice::getGattServer() |
sjallouli | 0:a948f5f3904c | 203 | { |
sjallouli | 0:a948f5f3904c | 204 | return BlueNRGGattServer::getInstance(); |
sjallouli | 0:a948f5f3904c | 205 | } |
sjallouli | 0:a948f5f3904c | 206 | |
sjallouli | 0:a948f5f3904c | 207 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 208 | /*! |
sjallouli | 0:a948f5f3904c | 209 | @brief set Tx power level |
sjallouli | 0:a948f5f3904c | 210 | |
sjallouli | 0:a948f5f3904c | 211 | @returns ble_error_t |
sjallouli | 0:a948f5f3904c | 212 | |
sjallouli | 0:a948f5f3904c | 213 | @retval BLE_ERROR_NONE |
sjallouli | 0:a948f5f3904c | 214 | Everything executed properly |
sjallouli | 0:a948f5f3904c | 215 | |
sjallouli | 0:a948f5f3904c | 216 | @section EXAMPLE |
sjallouli | 0:a948f5f3904c | 217 | |
sjallouli | 0:a948f5f3904c | 218 | @code |
sjallouli | 0:a948f5f3904c | 219 | |
sjallouli | 0:a948f5f3904c | 220 | @endcode |
sjallouli | 0:a948f5f3904c | 221 | */ |
sjallouli | 0:a948f5f3904c | 222 | /**************************************************************************/ |
sjallouli | 0:a948f5f3904c | 223 | ble_error_t BlueNRGDevice::setTxPower(int8_t txPower) |
sjallouli | 0:a948f5f3904c | 224 | { |
sjallouli | 0:a948f5f3904c | 225 | int8_t enHighPower = 0; |
sjallouli | 0:a948f5f3904c | 226 | int8_t paLevel = 0; |
sjallouli | 0:a948f5f3904c | 227 | int8_t dbmActuallySet = getHighPowerAndPALevelValue(txPower, enHighPower, paLevel); |
sjallouli | 0:a948f5f3904c | 228 | DEBUG("txPower=%d, dbmActuallySet=%d\n\r", txPower, dbmActuallySet); |
sjallouli | 0:a948f5f3904c | 229 | DEBUG("enHighPower=%d, paLevel=%d\n\r", enHighPower, paLevel); |
sjallouli | 0:a948f5f3904c | 230 | aci_hal_set_tx_power_level(enHighPower, paLevel); |
sjallouli | 0:a948f5f3904c | 231 | return BLE_ERROR_NONE; |
sjallouli | 0:a948f5f3904c | 232 | } |