Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: nRF51822
Source/BLE_Stuff.cpp@27:bb7247a1704e, 2019-04-17 (annotated)
- Committer:
- sgetz7908
- Date:
- Wed Apr 17 18:19:30 2019 +0000
- Revision:
- 27:bb7247a1704e
- Parent:
- 24:761c30334cf4
- Child:
- 30:76b51e525c40
Works acording to Merck flow diagram.;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sgetz7908 | 23:7ca590427f0e | 1 | |
| sgetz7908 | 23:7ca590427f0e | 2 | /// @file BLE_Stuff.cpp |
| sgetz7908 | 23:7ca590427f0e | 3 | #include <events/mbed_events.h> |
| sgetz7908 | 23:7ca590427f0e | 4 | #include <mbed.h> |
| sgetz7908 | 23:7ca590427f0e | 5 | #include "ble/BLE.h" |
| sgetz7908 | 23:7ca590427f0e | 6 | #include "ble/Gap.h" |
| sgetz7908 | 23:7ca590427f0e | 7 | #include "ble/services/UARTService.h" |
| sgetz7908 | 23:7ca590427f0e | 8 | |
| sgetz7908 | 23:7ca590427f0e | 9 | #include "BLE_Stuff.h" |
| sgetz7908 | 23:7ca590427f0e | 10 | #include "infoService.h" |
| sgetz7908 | 23:7ca590427f0e | 11 | #include "hw.h" |
| sgetz7908 | 23:7ca590427f0e | 12 | #include "log.h" |
| sgetz7908 | 23:7ca590427f0e | 13 | #include "main.h" |
| sgetz7908 | 23:7ca590427f0e | 14 | #include "log.h" |
| sgetz7908 | 23:7ca590427f0e | 15 | #include "nrf_soc.h" |
| sgetz7908 | 23:7ca590427f0e | 16 | |
| sgetz7908 | 23:7ca590427f0e | 17 | extern EventQueue eventQueue; //(/* event count */ 16 * EVENTS_EVENT_SIZE); |
| sgetz7908 | 23:7ca590427f0e | 18 | UARTService *uartServicePtr = NULL; |
| sgetz7908 | 23:7ca590427f0e | 19 | |
| sgetz7908 | 23:7ca590427f0e | 20 | LowPowerTimeout bleInactivity_timeout; |
| sgetz7908 | 23:7ca590427f0e | 21 | |
| sgetz7908 | 23:7ca590427f0e | 22 | |
| sgetz7908 | 23:7ca590427f0e | 23 | int adv_timeout_ID = 0; |
| sgetz7908 | 23:7ca590427f0e | 24 | |
| sgetz7908 | 23:7ca590427f0e | 25 | int adv_state = false; |
| sgetz7908 | 23:7ca590427f0e | 26 | char dev_name[15] = ""; |
| sgetz7908 | 23:7ca590427f0e | 27 | |
| sgetz7908 | 23:7ca590427f0e | 28 | volatile int batt_voltage = 0; // actual battery voltage * 100 |
| sgetz7908 | 23:7ca590427f0e | 29 | |
| sgetz7908 | 23:7ca590427f0e | 30 | bool m_isConnected = false; |
| sgetz7908 | 23:7ca590427f0e | 31 | |
| sgetz7908 | 23:7ca590427f0e | 32 | void process_cmd(char * cmd); |
| sgetz7908 | 23:7ca590427f0e | 33 | |
| sgetz7908 | 23:7ca590427f0e | 34 | |
| sgetz7908 | 23:7ca590427f0e | 35 | /// Called by bleInactivity_timeout |
| sgetz7908 | 23:7ca590427f0e | 36 | void disconnect(void) |
| sgetz7908 | 23:7ca590427f0e | 37 | { |
| sgetz7908 | 23:7ca590427f0e | 38 | bleInactivity_timeout.detach(); |
| sgetz7908 | 23:7ca590427f0e | 39 | #if UART_DEBUGGING==0 |
| sgetz7908 | 23:7ca590427f0e | 40 | BLE &ble = BLE::Instance(); |
| sgetz7908 | 23:7ca590427f0e | 41 | ble.gap().disconnect(Gap::REMOTE_DEV_TERMINATION_DUE_TO_POWER_OFF); // force disconnect |
| sgetz7908 | 23:7ca590427f0e | 42 | #endif |
| sgetz7908 | 23:7ca590427f0e | 43 | } |
| sgetz7908 | 23:7ca590427f0e | 44 | |
| sgetz7908 | 23:7ca590427f0e | 45 | void reset_ble_activity_timer(void) |
| sgetz7908 | 23:7ca590427f0e | 46 | { |
| sgetz7908 | 23:7ca590427f0e | 47 | bleInactivity_timeout.detach(); |
| sgetz7908 | 23:7ca590427f0e | 48 | bleInactivity_timeout.attach(&disconnect, BLE_INACTIVITY_TIMEOUT); |
| sgetz7908 | 23:7ca590427f0e | 49 | } |
| sgetz7908 | 23:7ca590427f0e | 50 | |
| sgetz7908 | 23:7ca590427f0e | 51 | /// Called when the Bluetooth connection is disconnected. |
| sgetz7908 | 23:7ca590427f0e | 52 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| sgetz7908 | 23:7ca590427f0e | 53 | { |
| sgetz7908 | 23:7ca590427f0e | 54 | m_isConnected = false; |
| sgetz7908 | 23:7ca590427f0e | 55 | bleInactivity_timeout.detach(); |
| sgetz7908 | 23:7ca590427f0e | 56 | BLE &ble = BLE::Instance(); |
| sgetz7908 | 23:7ca590427f0e | 57 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); |
| sgetz7908 | 23:7ca590427f0e | 58 | if(adv_state) ble.gap().startAdvertising(); |
| sgetz7908 | 23:7ca590427f0e | 59 | } |
| sgetz7908 | 23:7ca590427f0e | 60 | |
| sgetz7908 | 23:7ca590427f0e | 61 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
| sgetz7908 | 23:7ca590427f0e | 62 | { |
| sgetz7908 | 23:7ca590427f0e | 63 | m_isConnected = true; |
| sgetz7908 | 23:7ca590427f0e | 64 | BLE &ble = BLE::Instance(); |
| sgetz7908 | 23:7ca590427f0e | 65 | ble.gap().setAdvertisingInterval(CONN_INTERVAL); |
| sgetz7908 | 23:7ca590427f0e | 66 | reset_ble_activity_timer(); |
| sgetz7908 | 23:7ca590427f0e | 67 | } |
| sgetz7908 | 23:7ca590427f0e | 68 | |
| sgetz7908 | 23:7ca590427f0e | 69 | bool isConnected(void) |
| sgetz7908 | 23:7ca590427f0e | 70 | { |
| sgetz7908 | 23:7ca590427f0e | 71 | return m_isConnected; |
| sgetz7908 | 23:7ca590427f0e | 72 | } |
| sgetz7908 | 23:7ca590427f0e | 73 | |
| sgetz7908 | 23:7ca590427f0e | 74 | /// Called to write a string to the Bluetooth UART. |
| sgetz7908 | 23:7ca590427f0e | 75 | // used for writting to the BLE UART |
| sgetz7908 | 23:7ca590427f0e | 76 | void BLE_UART_xmit(const char * str) |
| sgetz7908 | 23:7ca590427f0e | 77 | { |
| sgetz7908 | 23:7ca590427f0e | 78 | if (uartServicePtr != NULL) { |
| sgetz7908 | 23:7ca590427f0e | 79 | uartServicePtr->writeString(str); |
| sgetz7908 | 23:7ca590427f0e | 80 | } |
| sgetz7908 | 23:7ca590427f0e | 81 | } |
| sgetz7908 | 23:7ca590427f0e | 82 | |
| sgetz7908 | 23:7ca590427f0e | 83 | void BLE_UART_xmit(int i) |
| sgetz7908 | 23:7ca590427f0e | 84 | { |
| sgetz7908 | 23:7ca590427f0e | 85 | BLE_UART_xmit(uli2a(i)); |
| sgetz7908 | 23:7ca590427f0e | 86 | } |
| sgetz7908 | 23:7ca590427f0e | 87 | |
| sgetz7908 | 23:7ca590427f0e | 88 | char cmd_str[21]; |
| sgetz7908 | 23:7ca590427f0e | 89 | |
| sgetz7908 | 23:7ca590427f0e | 90 | /// Called when data is rcv'd from connected BLE device |
| sgetz7908 | 23:7ca590427f0e | 91 | void onDataWritten(const GattWriteCallbackParams *params) |
| sgetz7908 | 23:7ca590427f0e | 92 | { |
| sgetz7908 | 23:7ca590427f0e | 93 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
| sgetz7908 | 23:7ca590427f0e | 94 | uint16_t bytesRead = params->len; |
| sgetz7908 | 23:7ca590427f0e | 95 | strncpy(cmd_str, (const char *)params->data, bytesRead); |
| sgetz7908 | 24:761c30334cf4 | 96 | cmd_str[bytesRead] = 0; // add end of string char |
| sgetz7908 | 23:7ca590427f0e | 97 | |
| sgetz7908 | 23:7ca590427f0e | 98 | // Start process: process_cmd(cmd_str) as regular non-irq routine |
| sgetz7908 | 23:7ca590427f0e | 99 | eventQueue.call(process_cmd, cmd_str); |
| sgetz7908 | 23:7ca590427f0e | 100 | // the dispatch method executes events |
| sgetz7908 | 23:7ca590427f0e | 101 | //queue.dispatch(); |
| sgetz7908 | 23:7ca590427f0e | 102 | |
| sgetz7908 | 23:7ca590427f0e | 103 | //ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); // send back whatever is sent to us |
| sgetz7908 | 23:7ca590427f0e | 104 | reset_ble_activity_timer(); |
| sgetz7908 | 23:7ca590427f0e | 105 | } |
| sgetz7908 | 23:7ca590427f0e | 106 | } |
| sgetz7908 | 23:7ca590427f0e | 107 | |
| sgetz7908 | 27:bb7247a1704e | 108 | |
| sgetz7908 | 27:bb7247a1704e | 109 | /// Called when data is rcv'd from connected BLE device |
| sgetz7908 | 27:bb7247a1704e | 110 | void dataReadCallback(const GattReadCallbackParams *params) |
| sgetz7908 | 27:bb7247a1704e | 111 | { |
| sgetz7908 | 27:bb7247a1704e | 112 | //if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getRXCharacteristicHandle())) { |
| sgetz7908 | 27:bb7247a1704e | 113 | // Start process: process as regular non-irq routine |
| sgetz7908 | 27:bb7247a1704e | 114 | eventQueue.call(dataWasRead); |
| sgetz7908 | 27:bb7247a1704e | 115 | //} |
| sgetz7908 | 27:bb7247a1704e | 116 | } |
| sgetz7908 | 27:bb7247a1704e | 117 | |
| sgetz7908 | 27:bb7247a1704e | 118 | |
| sgetz7908 | 23:7ca590427f0e | 119 | /// Turn on or off advertising |
| sgetz7908 | 23:7ca590427f0e | 120 | // When turned on, it will only advertise for a limited time, or until turned off. |
| sgetz7908 | 23:7ca590427f0e | 121 | void set_radio(int state) |
| sgetz7908 | 23:7ca590427f0e | 122 | { |
| sgetz7908 | 23:7ca590427f0e | 123 | BLE &ble = BLE::Instance(); |
| sgetz7908 | 23:7ca590427f0e | 124 | adv_state = state; |
| sgetz7908 | 23:7ca590427f0e | 125 | |
| sgetz7908 | 23:7ca590427f0e | 126 | if(state) { |
| sgetz7908 | 24:761c30334cf4 | 127 | //adv_state = true; |
| sgetz7908 | 23:7ca590427f0e | 128 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); |
| sgetz7908 | 23:7ca590427f0e | 129 | ble.gap().startAdvertising(); |
| sgetz7908 | 23:7ca590427f0e | 130 | } else { |
| sgetz7908 | 24:761c30334cf4 | 131 | //adv_state = false; |
| sgetz7908 | 23:7ca590427f0e | 132 | ble.gap().stopAdvertising(); |
| sgetz7908 | 23:7ca590427f0e | 133 | disconnect(); |
| sgetz7908 | 23:7ca590427f0e | 134 | } |
| sgetz7908 | 23:7ca590427f0e | 135 | } |
| sgetz7908 | 23:7ca590427f0e | 136 | |
| sgetz7908 | 23:7ca590427f0e | 137 | #if 0 |
| sgetz7908 | 23:7ca590427f0e | 138 | void stop_radio_and_wait(void) |
| sgetz7908 | 23:7ca590427f0e | 139 | { |
| sgetz7908 | 23:7ca590427f0e | 140 | set_radio(false); |
| sgetz7908 | 23:7ca590427f0e | 141 | |
| sgetz7908 | 23:7ca590427f0e | 142 | // If radio is active, wait for it to become inactive. |
| sgetz7908 | 23:7ca590427f0e | 143 | //wait(0.5); |
| sgetz7908 | 23:7ca590427f0e | 144 | while (isConnected())// || isRadioActive()) |
| sgetz7908 | 23:7ca590427f0e | 145 | { |
| sgetz7908 | 23:7ca590427f0e | 146 | // Do nothing (just wait for radio to become inactive). |
| sgetz7908 | 23:7ca590427f0e | 147 | sd_app_evt_wait(); |
| sgetz7908 | 23:7ca590427f0e | 148 | } |
| sgetz7908 | 23:7ca590427f0e | 149 | } |
| sgetz7908 | 23:7ca590427f0e | 150 | #endif |
| sgetz7908 | 23:7ca590427f0e | 151 | |
| sgetz7908 | 27:bb7247a1704e | 152 | void authorize_client_write(GattWriteAuthCallbackParams *e) |
| sgetz7908 | 27:bb7247a1704e | 153 | { |
| sgetz7908 | 27:bb7247a1704e | 154 | e->authorizationReply = AUTH_CALLBACK_REPLY_SUCCESS; |
| sgetz7908 | 27:bb7247a1704e | 155 | } |
| sgetz7908 | 27:bb7247a1704e | 156 | |
| sgetz7908 | 23:7ca590427f0e | 157 | ble_init_status_t ble_init_status = BLE_INIT_IN_PROGRESS; |
| sgetz7908 | 23:7ca590427f0e | 158 | |
| sgetz7908 | 23:7ca590427f0e | 159 | /// Called to setup the Bluetooth link. |
| sgetz7908 | 23:7ca590427f0e | 160 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| sgetz7908 | 23:7ca590427f0e | 161 | { |
| sgetz7908 | 23:7ca590427f0e | 162 | BLE& ble = params->ble; |
| sgetz7908 | 23:7ca590427f0e | 163 | ble_error_t error = params->error; |
| sgetz7908 | 23:7ca590427f0e | 164 | |
| sgetz7908 | 23:7ca590427f0e | 165 | if (error != BLE_ERROR_NONE) { |
| sgetz7908 | 23:7ca590427f0e | 166 | ble_init_status = BLE_INIT_ERROR; |
| sgetz7908 | 23:7ca590427f0e | 167 | return; |
| sgetz7908 | 23:7ca590427f0e | 168 | } |
| sgetz7908 | 23:7ca590427f0e | 169 | |
| sgetz7908 | 23:7ca590427f0e | 170 | /* Ensure that it is the default instance of BLE */ |
| sgetz7908 | 23:7ca590427f0e | 171 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| sgetz7908 | 23:7ca590427f0e | 172 | return; |
| sgetz7908 | 23:7ca590427f0e | 173 | } |
| sgetz7908 | 23:7ca590427f0e | 174 | |
| sgetz7908 | 23:7ca590427f0e | 175 | ble.gap().onDisconnection(disconnectionCallback); |
| sgetz7908 | 23:7ca590427f0e | 176 | ble.gap().onConnection(connectionCallback); |
| sgetz7908 | 23:7ca590427f0e | 177 | ble.gattServer().onDataWritten(onDataWritten); |
| sgetz7908 | 23:7ca590427f0e | 178 | |
| sgetz7908 | 23:7ca590427f0e | 179 | //ble.gattServer().onDataRead( dataReadCallback ); //const DataReadCallback_t & callback) |
| sgetz7908 | 23:7ca590427f0e | 180 | //ble.onDataRead( dataReadCallback); |
| sgetz7908 | 23:7ca590427f0e | 181 | |
| sgetz7908 | 23:7ca590427f0e | 182 | strcpy(dev_name, DEV_NAME); |
| sgetz7908 | 27:bb7247a1704e | 183 | strcat(dev_name, char2hex(NRF_FICR->DEVICEADDR[0] & 0xfff, 3)); |
| sgetz7908 | 23:7ca590427f0e | 184 | int len = strlen(dev_name); |
| sgetz7908 | 23:7ca590427f0e | 185 | |
| sgetz7908 | 23:7ca590427f0e | 186 | ble.gap().setDeviceName((std::uint8_t *)dev_name); |
| sgetz7908 | 23:7ca590427f0e | 187 | |
| sgetz7908 | 23:7ca590427f0e | 188 | /* Setup primary service */ |
| sgetz7908 | 23:7ca590427f0e | 189 | uartServicePtr = new UARTService(ble); |
| sgetz7908 | 23:7ca590427f0e | 190 | |
| sgetz7908 | 23:7ca590427f0e | 191 | /* setup advertising */ |
| sgetz7908 | 23:7ca590427f0e | 192 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
| sgetz7908 | 23:7ca590427f0e | 193 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| sgetz7908 | 23:7ca590427f0e | 194 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
| sgetz7908 | 23:7ca590427f0e | 195 | (const uint8_t *)dev_name, len); |
| sgetz7908 | 23:7ca590427f0e | 196 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
| sgetz7908 | 23:7ca590427f0e | 197 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
| sgetz7908 | 23:7ca590427f0e | 198 | |
| sgetz7908 | 23:7ca590427f0e | 199 | |
| sgetz7908 | 23:7ca590427f0e | 200 | ble.gap().setAdvertisingInterval(ADV_INTERVAL); |
| sgetz7908 | 23:7ca590427f0e | 201 | ble.gap().setAdvertisingTimeout(BLE_ADVERTISING_DURATION); |
| sgetz7908 | 23:7ca590427f0e | 202 | ble.gap().setTxPower(-20); // -30, -20, -16, -12, -8, -4, 0, and 4 dBm |
| sgetz7908 | 23:7ca590427f0e | 203 | |
| sgetz7908 | 23:7ca590427f0e | 204 | ble_init_status = BLE_INIT_OK; |
| sgetz7908 | 23:7ca590427f0e | 205 | |
| sgetz7908 | 23:7ca590427f0e | 206 | set_radio(true); |
| sgetz7908 | 23:7ca590427f0e | 207 | } |
| sgetz7908 | 23:7ca590427f0e | 208 | |
| sgetz7908 | 23:7ca590427f0e | 209 | /// Process BLE Events |
| sgetz7908 | 23:7ca590427f0e | 210 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
| sgetz7908 | 23:7ca590427f0e | 211 | BLE &ble = BLE::Instance(); |
| sgetz7908 | 23:7ca590427f0e | 212 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
| sgetz7908 | 23:7ca590427f0e | 213 | } |
| sgetz7908 | 23:7ca590427f0e | 214 | |
| sgetz7908 | 23:7ca590427f0e | 215 | /// Initialize the BLE Stack |
| sgetz7908 | 23:7ca590427f0e | 216 | // Returns 0 if OK |
| sgetz7908 | 23:7ca590427f0e | 217 | ble_init_status_t Init_BLE_Stuff(void) |
| sgetz7908 | 23:7ca590427f0e | 218 | { |
| sgetz7908 | 23:7ca590427f0e | 219 | ble_init_status = BLE_INIT_IN_PROGRESS; |
| sgetz7908 | 23:7ca590427f0e | 220 | BLE &ble = BLE::Instance(); |
| sgetz7908 | 23:7ca590427f0e | 221 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
| sgetz7908 | 23:7ca590427f0e | 222 | ble.init(bleInitComplete); |
| sgetz7908 | 23:7ca590427f0e | 223 | |
| sgetz7908 | 23:7ca590427f0e | 224 | // Wait for initialization to complete. This is necessary because the |
| sgetz7908 | 23:7ca590427f0e | 225 | // BLE object is used after this. |
| sgetz7908 | 23:7ca590427f0e | 226 | while(ble_init_status == BLE_INIT_IN_PROGRESS) { wait(0.05);} |
| sgetz7908 | 23:7ca590427f0e | 227 | // set up BLE Information services in infoService.cpp |
| sgetz7908 | 23:7ca590427f0e | 228 | //ble.gattServer().addService(infoServicePtr); |
| sgetz7908 | 23:7ca590427f0e | 229 | return ble_init_status; |
| sgetz7908 | 23:7ca590427f0e | 230 | } |