thing
This commit is contained in:
@@ -1,136 +1,126 @@
|
||||
//
|
||||
// Created by bruno on 13. 7. 2026.
|
||||
//
|
||||
|
||||
#include "packet.h"
|
||||
#include "crc32.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "cpustatusui.h"
|
||||
#include "decoder.h"
|
||||
#include "protocol.h"
|
||||
|
||||
|
||||
#define MAGIC 0xCA55
|
||||
|
||||
|
||||
#define PACKET_SIZE 223
|
||||
|
||||
|
||||
uint8_t *build_stream(
|
||||
uint8_t *file,
|
||||
uint32_t size,
|
||||
uint32_t *outSize) {
|
||||
uint32_t packets =
|
||||
(size + PACKET_SIZE - 1)
|
||||
/
|
||||
PACKET_SIZE;
|
||||
|
||||
|
||||
/*
|
||||
estimate:
|
||||
|
||||
preamble 256 bytes
|
||||
sync 16 bytes
|
||||
packets:
|
||||
header 4
|
||||
payload 223
|
||||
crc 4
|
||||
*/
|
||||
|
||||
|
||||
uint32_t capacity =
|
||||
256 +
|
||||
16 +
|
||||
packets * (4 + 2 + 2 + 223 + 4);
|
||||
|
||||
|
||||
uint8_t *out =
|
||||
malloc(capacity);
|
||||
|
||||
|
||||
uint32_t pos = 0;
|
||||
|
||||
|
||||
/*
|
||||
preamble
|
||||
*/
|
||||
|
||||
for (int i = 0; i < 256; i++)
|
||||
out[pos++] = 0x55;
|
||||
|
||||
|
||||
/*
|
||||
sync
|
||||
*/
|
||||
|
||||
uint8_t sync[] =
|
||||
{
|
||||
0xD3,
|
||||
0x91,
|
||||
0xAC,
|
||||
0x55,
|
||||
0x77,
|
||||
0x22,
|
||||
0xF0,
|
||||
0x0F
|
||||
};
|
||||
void encoder_build_packet(
|
||||
Encoder *enc,
|
||||
uint8_t *data,
|
||||
uint8_t length)
|
||||
{
|
||||
uint32_t p=0;
|
||||
|
||||
|
||||
memcpy(
|
||||
out + pos,
|
||||
sync,
|
||||
sizeof(sync));
|
||||
&enc->packet[p],
|
||||
PACKET_MAGIC,
|
||||
MAGIC_SIZE);
|
||||
|
||||
pos += sizeof(sync);
|
||||
p+=MAGIC_SIZE;
|
||||
|
||||
|
||||
uint32_t offset = 0;
|
||||
enc->packet[p++]=length;
|
||||
|
||||
|
||||
for (uint32_t p = 0; p < packets; p++) {
|
||||
uint16_t magic = MAGIC;
|
||||
memcpy(
|
||||
&enc->packet[p],
|
||||
data,
|
||||
length);
|
||||
|
||||
uint16_t packet = p;
|
||||
p+=length;
|
||||
|
||||
|
||||
uint16_t len =
|
||||
size - offset > PACKET_SIZE ? PACKET_SIZE : size - offset;
|
||||
uint16_t crc =
|
||||
crc16(
|
||||
&enc->packet[0],
|
||||
p);
|
||||
|
||||
|
||||
memcpy(out + pos, &magic, 2);
|
||||
pos += 2;
|
||||
enc->packet[p++]=crc&0xff;
|
||||
enc->packet[p++]=crc>>8;
|
||||
|
||||
|
||||
memcpy(out + pos, &packet, 2);
|
||||
pos += 2;
|
||||
enc->packetSize=p;
|
||||
}
|
||||
|
||||
bool decoder_parse_packet(Decoder *dec)
|
||||
{
|
||||
if(dec->bufferSize < MAGIC_SIZE + 1 + 2)
|
||||
return false;
|
||||
|
||||
if(memcmp(dec->buffer,
|
||||
PACKET_MAGIC,
|
||||
MAGIC_SIZE))
|
||||
{
|
||||
printf("Bad magic\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t length =
|
||||
dec->buffer[MAGIC_SIZE];
|
||||
|
||||
uint32_t packetSize =
|
||||
MAGIC_SIZE +
|
||||
1 +
|
||||
length +
|
||||
2;
|
||||
|
||||
if(dec->bufferSize < packetSize)
|
||||
return false;
|
||||
|
||||
uint16_t received =
|
||||
dec->buffer[packetSize-2] |
|
||||
(dec->buffer[packetSize-1] << 8);
|
||||
|
||||
uint16_t calculated =
|
||||
crc16(dec->buffer,
|
||||
packetSize-2);
|
||||
|
||||
if(received != calculated)
|
||||
{
|
||||
printf("CRC FAIL\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
printf("PACKET OK len=%d\n", length);
|
||||
|
||||
printf("Payload:\n");
|
||||
|
||||
for(int i=0;i<length;i++)
|
||||
printf("%02X ",
|
||||
dec->buffer[MAGIC_SIZE+1+i]);
|
||||
|
||||
printf("\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t crc16(
|
||||
const uint8_t *data,
|
||||
uint32_t len)
|
||||
{
|
||||
uint16_t crc = 0xFFFF;
|
||||
|
||||
|
||||
memcpy(out + pos, &len, 2);
|
||||
pos += 2;
|
||||
for(uint32_t i=0;i<len;i++)
|
||||
{
|
||||
crc ^= data[i];
|
||||
|
||||
|
||||
memcpy(out + pos,
|
||||
file + offset,
|
||||
len);
|
||||
|
||||
pos += len;
|
||||
|
||||
|
||||
uint32_t crc =
|
||||
calcCRC32(
|
||||
file + offset,
|
||||
len);
|
||||
|
||||
|
||||
memcpy(out + pos, &crc, 4);
|
||||
|
||||
pos += 4;
|
||||
|
||||
|
||||
offset += len;
|
||||
for(int b=0;b<8;b++)
|
||||
{
|
||||
if(crc & 1)
|
||||
crc =
|
||||
(crc >> 1) ^ 0xA001;
|
||||
else
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
*outSize = pos;
|
||||
|
||||
return out;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
Reference in New Issue
Block a user