EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
bleHelper.cpp@9:afd6bd6e88bd, 2015-12-17 (annotated)
- Committer:
- Farshad
- Date:
- Thu Dec 17 01:12:17 2015 +0000
- Revision:
- 9:afd6bd6e88bd
- Parent:
- 7:8a23a257b66a
Added more comments.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Farshad | 7:8a23a257b66a | 1 | |
Farshad | 7:8a23a257b66a | 2 | #include "bleHelper.h" |
Farshad | 7:8a23a257b66a | 3 | |
Farshad | 7:8a23a257b66a | 4 | BLEHelper::BLEHelper(BLEDevice* ble, UARTService* UuartServicePtr) |
Farshad | 7:8a23a257b66a | 5 | { |
Farshad | 7:8a23a257b66a | 6 | blePtr = ble; |
Farshad | 7:8a23a257b66a | 7 | uartServicePtr = UuartServicePtr; |
Farshad | 7:8a23a257b66a | 8 | } |
Farshad | 7:8a23a257b66a | 9 | |
Farshad | 7:8a23a257b66a | 10 | |
Farshad | 7:8a23a257b66a | 11 | void BLEHelper::sendOverBLE(uint16_t cmd, uint8_t data[], uint16_t len, PacketStatus_e status) |
Farshad | 7:8a23a257b66a | 12 | { |
Farshad | 7:8a23a257b66a | 13 | if(len > 18) { |
Farshad | 9:afd6bd6e88bd | 14 | // this is a problem since we can only send 20 bytes of payload and need 2 bytes for the command |
Farshad | 9:afd6bd6e88bd | 15 | // so total of 18 can be sent in a single BLE transaction. |
Farshad | 7:8a23a257b66a | 16 | } |
Farshad | 7:8a23a257b66a | 17 | |
Farshad | 7:8a23a257b66a | 18 | uint8_t buf[len + 2]; // should set it to the max single packet size |
Farshad | 7:8a23a257b66a | 19 | uint8_t offset = 0; |
Farshad | 7:8a23a257b66a | 20 | uint16_t tmp = cmd | (uint16_t)status; |
Farshad | 7:8a23a257b66a | 21 | memcpy(buf, &tmp, sizeof(uint16_t)); // command |
Farshad | 7:8a23a257b66a | 22 | offset+=sizeof(uint16_t); |
Farshad | 7:8a23a257b66a | 23 | memcpy(&buf[offset], &data[0], len); // data |
Farshad | 7:8a23a257b66a | 24 | offset+= len; |
Farshad | 7:8a23a257b66a | 25 | blePtr->updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset); |
Farshad | 7:8a23a257b66a | 26 | } |
Farshad | 7:8a23a257b66a | 27 | |
Farshad | 7:8a23a257b66a | 28 | void BLEHelper::sendPacketOverBLE(uint16_t cmd, uint8_t data[], uint16_t len) |
Farshad | 7:8a23a257b66a | 29 | { |
Farshad | 7:8a23a257b66a | 30 | uint8_t nPackets = (len / 19) + 1; |
Farshad | 7:8a23a257b66a | 31 | uint8_t remainingPackets = nPackets; |
Farshad | 7:8a23a257b66a | 32 | uint16_t offset = 0; |
Farshad | 7:8a23a257b66a | 33 | bool first = true; |
Farshad | 7:8a23a257b66a | 34 | const uint8_t maxDataPayload = 18; // max is 20 but we are sending a command of 2 bytes always |
Farshad | 7:8a23a257b66a | 35 | |
Farshad | 7:8a23a257b66a | 36 | do { |
Farshad | 7:8a23a257b66a | 37 | if(nPackets == 1) { |
Farshad | 7:8a23a257b66a | 38 | // first and only |
Farshad | 7:8a23a257b66a | 39 | packetStatus = PS_FIRST_AND_ONLY; |
Farshad | 7:8a23a257b66a | 40 | sendOverBLE(cmd, &data[offset], len, packetStatus); |
Farshad | 7:8a23a257b66a | 41 | remainingPackets = 0; |
Farshad | 7:8a23a257b66a | 42 | } else if(remainingPackets == 1) { |
Farshad | 7:8a23a257b66a | 43 | // last one |
Farshad | 7:8a23a257b66a | 44 | packetStatus = PS_LAST; |
Farshad | 7:8a23a257b66a | 45 | sendOverBLE(cmd, &data[offset], len-offset, packetStatus); |
Farshad | 7:8a23a257b66a | 46 | remainingPackets = 0; |
Farshad | 7:8a23a257b66a | 47 | } else if(remainingPackets > 1 && first == true) { |
Farshad | 7:8a23a257b66a | 48 | // first and more to come |
Farshad | 7:8a23a257b66a | 49 | packetStatus = PS_FIRST_AND_NOT_LAST; |
Farshad | 7:8a23a257b66a | 50 | sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); |
Farshad | 7:8a23a257b66a | 51 | offset += maxDataPayload; |
Farshad | 7:8a23a257b66a | 52 | remainingPackets -= 1; |
Farshad | 7:8a23a257b66a | 53 | first = false; |
Farshad | 7:8a23a257b66a | 54 | } else if(remainingPackets > 1 && first == false) { |
Farshad | 7:8a23a257b66a | 55 | // middle |
Farshad | 7:8a23a257b66a | 56 | packetStatus = PS_MIDDLE; |
Farshad | 7:8a23a257b66a | 57 | sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); |
Farshad | 7:8a23a257b66a | 58 | offset += maxDataPayload; |
Farshad | 7:8a23a257b66a | 59 | remainingPackets -= 1; |
Farshad | 7:8a23a257b66a | 60 | } |
Farshad | 7:8a23a257b66a | 61 | |
Farshad | 7:8a23a257b66a | 62 | wait_ms(50); // 40ms delay seems to be a limit |
Farshad | 7:8a23a257b66a | 63 | } while (remainingPackets > 0); |
Farshad | 7:8a23a257b66a | 64 | } |
Farshad | 7:8a23a257b66a | 65 |