diff --git a/Software/PC_Application/LibreVNA-GUI/Device/device.cpp b/Software/PC_Application/LibreVNA-GUI/Device/device.cpp index 7ae0426..57e01dd 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/device.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/device.cpp @@ -70,6 +70,24 @@ void USBInBuffer::Callback(libusb_transfer *transfer) switch(transfer->status) { case LIBUSB_TRANSFER_COMPLETED: received_size += transfer->actual_length; + // Change/insert/delete random data to check the data handling for robustness +// srand((unsigned)time(0)); +// for(unsigned int i=0;iactual_length <<"total:" << received_size; inCallback = true; emit DataReceived(); diff --git a/Software/VNA_embedded/Application/Communication/Protocol.cpp b/Software/VNA_embedded/Application/Communication/Protocol.cpp index bfc8912..9a478f5 100644 --- a/Software/VNA_embedded/Application/Communication/Protocol.cpp +++ b/Software/VNA_embedded/Application/Communication/Protocol.cpp @@ -52,22 +52,23 @@ uint16_t Protocol::DecodeBuffer(uint8_t *buf, uint16_t len, PacketInfo *info) { } /* Evaluate frame size */ - uint16_t length = *(uint16_t*) &data[1]; - if(len < length) { + uint16_t length = data[1] | ((uint16_t) data[2] << 8); + + if(length > sizeof(PacketInfo) * 2 || length < 8) { + // larger than twice the maximum expected packet size or too small, probably an error, ignore + info->type = PacketType::None; + return 1; + } + + if(len < length) { /* The frame payload has not been completely received */ info->type = PacketType::None; return data - buf; } - if(length > sizeof(PacketInfo) * 2) { - // larger than twice the maximum expected packet size, probably an error, ignore - info->type = PacketType::None; - return 1; - } - /* The complete frame has been received, check checksum */ auto type = (PacketType) data[3]; - uint32_t crc = *(uint32_t*) &data[length - 4]; + uint32_t crc = (uint32_t) data[length-4] | ((uint32_t) data[length-3] << 8) | ((uint32_t) data[length-2] << 16) | ((uint32_t) data[length-1] << 24); if(type != PacketType::VNADatapoint) { uint32_t compare = CRC32(0, data, length - 4); if(crc != compare) {