Download a stream of data to a peripheral over BLE.
Dependencies: BLE_API mbed nRF51822
A simple demonstration of downloading a stream onto a peripheral over BLE. There's a corresponding Python script to driver the client.
Revision 3:d58f3a5bd66c, committed 2014-09-02
- Comitter:
- rgrover1
- Date:
- Tue Sep 02 16:30:18 2014 +0000
- Parent:
- 2:4ca946e0ebdc
- Child:
- 4:29ae814ca55e
- Commit message:
- updating underlying libraries.
Changed in this revision
--- a/BLE_API.lib Fri Aug 29 10:41:56 2014 +0200 +++ b/BLE_API.lib Tue Sep 02 16:30:18 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#189ff241dae1 +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#ca826083980e
--- a/TransferService.cpp Fri Aug 29 10:41:56 2014 +0200 +++ b/TransferService.cpp Tue Sep 02 16:30:18 2014 +0000 @@ -12,20 +12,20 @@ static const uint8_t transferFileBlockUUID[16] = transfer_UUID(0xACE0); // Storage for the value of the characteristics -struct fileInfo_t { +typedef struct { uint16_t length; uint16_t crc16; -}; -static struct fileInfo_t fileInfo; -struct fileBlock_t { +} FileInfo_t; +static FileInfo_t fileInfo; +typedef struct { uint16_t blockNumber; uint8_t data[16]; -}; -static struct fileBlock_t fileBlock; +} FileBlock_t; +static FileBlock_t fileBlock; // Other things needed for operation -static BLEDevice* ble; -static Timer downloadTimer; +static BLEDevice* ble; +static Timer downloadTimer; static uint16_t expectingBlock = 0; @@ -40,8 +40,8 @@ static GattCharacteristic transferFileBlock(transferFileBlockUUID, (uint8_t *)&fileBlock, - sizeof(fileBlock_t), - sizeof(fileBlock_t), + sizeof(FileBlock_t), + sizeof(FileBlock_t), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); @@ -71,15 +71,18 @@ void refuseFile(); void sendFileDownloadedSuccessfully(); -void handleDataWritten(uint16_t handle) +void handleDataWritten(uint16_t handle, const GattCharacteristicWriteCBParams *params) { if (!ble) { return; } if (handle == transferFileInfo.getValueAttribute().getHandle()) { - uint16_t len = sizeof(fileInfo); - ble->readCharacteristicValue(handle, (uint8_t *) &fileInfo, &len); + if (params->len != sizeof(FileInfo_t)) { + DEBUG("invalid write into fileInfo characteristic\r\n"); + return; + } + memcpy(&fileInfo, params->data, params->len); if ((fileInfo.length == 0) && (fileInfo.crc16 == 0)) { // signal to cancel pending upload @@ -103,8 +106,11 @@ refuseFile(); } } else if (handle == transferFileBlock.getValueAttribute().getHandle()) { - uint16_t len = sizeof(fileBlock); - ble->readCharacteristicValue(handle, (uint8_t *) &fileBlock, &len); + if (params->len != sizeof(FileBlock_t)) { + DEBUG("invalid write into fileInfo characteristic\r\n"); + return; + } + memcpy(&fileBlock, params->data, params->len); if (fileBlock.blockNumber != expectingBlock) { DEBUG("Expected blk %u, not %u!\r\n", expectingBlock, fileBlock.blockNumber);
--- a/TransferService.h Fri Aug 29 10:41:56 2014 +0200 +++ b/TransferService.h Tue Sep 02 16:30:18 2014 +0000 @@ -2,11 +2,12 @@ #define _H_TRANSFERSERVICE_H #include "BLEDevice.h" +#include "GattCharacteristicWriteCBParams.h" namespace Transfer { void init(BLEDevice &ble); void reset(); - void handleDataWritten(uint16_t handle); + void handleDataWritten(uint16_t handle, const GattCharacteristicWriteCBParams *params); const uint8_t* getServiceUUIDp(); }
--- a/main.cpp Fri Aug 29 10:41:56 2014 +0200 +++ b/main.cpp Tue Sep 02 16:30:18 2014 +0000 @@ -30,16 +30,18 @@ void bluetoothInit(); -void disconnectionCallback(Gap::Handle_t handle) +void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { DEBUG("Disconnected\n\r"); ble.startAdvertising(); DEBUG("Advertising...\r\n"); } -void onConnectionCallback(Gap::Handle_t handle) +void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params) { DEBUG("____[ Connected ]______________________________________\r\n"); + DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n", + params->minConnectionInterval, params->maxConnectionInterval, params->slaveLatency, params->connectionSupervisionTimeout); connectionParams.minConnectionInterval = Config::minConnectionInterval; connectionParams.maxConnectionInterval = Config::maxConnectionInterval; @@ -57,10 +59,10 @@ DEBUG("Notifications enabled for %d\r\n", handle); } -void onDataWritten(Gap::Handle_t handle) +void onDataWritten(Gap::Handle_t handle, const GattCharacteristicWriteCBParams *params) { // bubble up to services, they will emit callbacks if handle matches - Transfer::handleDataWritten(handle); + Transfer::handleDataWritten(handle, params); } void bluetoothInit()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-src.lib Tue Sep 02 16:30:18 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-src/#1f0269907d8b
--- a/mbed.bld Fri Aug 29 10:41:56 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013 \ No newline at end of file
--- a/nRF51822.lib Fri Aug 29 10:41:56 2014 +0200 +++ b/nRF51822.lib Tue Sep 02 16:30:18 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#1e5c300cec7f +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#e861f2041469