EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
bleHelper.cpp
00001 00002 #include "bleHelper.h" 00003 00004 BLEHelper::BLEHelper(BLEDevice* ble, UARTService* UuartServicePtr) 00005 { 00006 blePtr = ble; 00007 uartServicePtr = UuartServicePtr; 00008 } 00009 00010 00011 void BLEHelper::sendOverBLE(uint16_t cmd, uint8_t data[], uint16_t len, PacketStatus_e status) 00012 { 00013 if(len > 18) { 00014 // this is a problem since we can only send 20 bytes of payload and need 2 bytes for the command 00015 // so total of 18 can be sent in a single BLE transaction. 00016 } 00017 00018 uint8_t buf[len + 2]; // should set it to the max single packet size 00019 uint8_t offset = 0; 00020 uint16_t tmp = cmd | (uint16_t)status; 00021 memcpy(buf, &tmp, sizeof(uint16_t)); // command 00022 offset+=sizeof(uint16_t); 00023 memcpy(&buf[offset], &data[0], len); // data 00024 offset+= len; 00025 blePtr->updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset); 00026 } 00027 00028 void BLEHelper::sendPacketOverBLE(uint16_t cmd, uint8_t data[], uint16_t len) 00029 { 00030 uint8_t nPackets = (len / 19) + 1; 00031 uint8_t remainingPackets = nPackets; 00032 uint16_t offset = 0; 00033 bool first = true; 00034 const uint8_t maxDataPayload = 18; // max is 20 but we are sending a command of 2 bytes always 00035 00036 do { 00037 if(nPackets == 1) { 00038 // first and only 00039 packetStatus = PS_FIRST_AND_ONLY; 00040 sendOverBLE(cmd, &data[offset], len, packetStatus); 00041 remainingPackets = 0; 00042 } else if(remainingPackets == 1) { 00043 // last one 00044 packetStatus = PS_LAST; 00045 sendOverBLE(cmd, &data[offset], len-offset, packetStatus); 00046 remainingPackets = 0; 00047 } else if(remainingPackets > 1 && first == true) { 00048 // first and more to come 00049 packetStatus = PS_FIRST_AND_NOT_LAST; 00050 sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); 00051 offset += maxDataPayload; 00052 remainingPackets -= 1; 00053 first = false; 00054 } else if(remainingPackets > 1 && first == false) { 00055 // middle 00056 packetStatus = PS_MIDDLE; 00057 sendOverBLE(cmd, &data[offset], maxDataPayload, packetStatus); 00058 offset += maxDataPayload; 00059 remainingPackets -= 1; 00060 } 00061 00062 wait_ms(50); // 40ms delay seems to be a limit 00063 } while (remainingPackets > 0); 00064 } 00065
Generated on Sat Jul 16 2022 03:52:21 by
1.7.2
