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.
Diff: TransferService.cpp
- Revision:
- 3:d58f3a5bd66c
- Parent:
- 2:4ca946e0ebdc
- Child:
- 4:29ae814ca55e
--- 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);