use UART Service to loopback anything received on the TX characteristic onto the RX.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LoopbackUART by
Revision 12:bf690d81b1ea, committed 2015-04-15
- Comitter:
- dragosIQ
- Date:
- Wed Apr 15 14:00:45 2015 +0000
- Parent:
- 11:add794159852
- Commit message:
- c
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r add794159852 -r bf690d81b1ea main.cpp --- a/main.cpp Tue Dec 09 08:55:59 2014 +0000 +++ b/main.cpp Wed Apr 15 14:00:45 2015 +0000 @@ -17,68 +17,93 @@ #include "mbed.h" #include "BLEDevice.h" -#include "UARTService.h" - -#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; - * it will have an impact on code-size and power consumption. */ +#define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ +#define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ +#define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ -#if NEED_CONSOLE_OUTPUT -#define DEBUG(...) { printf(__VA_ARGS__); } -#else -#define DEBUG(...) /* nothing */ -#endif /* #if NEED_CONSOLE_OUTPUT */ +#define TXRX_BUF_LEN 20 + BLEDevice ble; DigitalOut led1(LED1); -UARTService *uartServicePtr; +// The Nordic UART Service +static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; +static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; +static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; +static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; + + +uint8_t txPayload[TXRX_BUF_LEN] = {0,}; +uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; + +GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + +GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + +GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; + +GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); + void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { - DEBUG("Disconnected!\n\r"); - DEBUG("Restarting the advertising process\n\r"); ble.startAdvertising(); } -void onDataWritten(const GattCharacteristicWriteCBParams *params) +void onDataWritten(const GattCharacteristicWriteCBParams *Handler) { - if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { - uint16_t bytesRead = params->len; - DEBUG("received %u bytes\n\r", bytesRead); - ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); + + uint8_t buf[TXRX_BUF_LEN]; + uint16_t bytesRead; + + if (Handler->charHandle == txCharacteristic.getValueAttribute().getHandle()) + { + ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead); + memset(txPayload, 0, TXRX_BUF_LEN); + memcpy(txPayload, buf, TXRX_BUF_LEN); + + } + } -void periodicCallback(void) +void switch_LED(void) { led1 = !led1; + ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), (const uint8_t *)"Salam de Sibiu!\n", sizeof("Salam de Sibiu!\n")-1); + } int main(void) { - led1 = 1; + Ticker ticker; - ticker.attach(periodicCallback, 1); - - DEBUG("Initialising the nRF51822\n\r"); + ticker.attach_us(switch_LED, 2000000); + ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(onDataWritten); - /* setup advertising */ + + // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, - (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); + (const uint8_t *)"sBeacon", sizeof("sBeacon") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, - (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); - + (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); + // 100ms; in multiples of 0.625ms. ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); - ble.startAdvertising(); - UARTService uartService(ble); - uartServicePtr = &uartService; + ble.addService(uartService); + + ble.startAdvertising(); + led1 = 1; + while (true) { ble.waitForEvent(); }