Question for C only, C++ and vectors do not solve problem.
I have such structure:
typedef __packed struct Packet_s
{
U8 head;
U16 len;
U32 id;
U8 data;
U8 end;
U16 crc;
} Packet_t, *Packet_p;
(EDIT: U8 is uint8_t (unsigned char) and so on)
For example, I've received packet(hex):
24 0B 00 07 00 00 00 AA 0D 16 1C
where
head = 0x24
len = 0x0B 0x00
id = 0x07 0x00 0x00 0x00
data = 0xAA
end = 0x0D
crc = 0x16 0x1C
I can copy it from incoming buffer like this
U8 Buffer[SIZE]; // receives all bytes here
memcpy(&Packet, &Buffer, buffer_len);
and work futher with it.
Is it possible to use my structure if field "DATA" is longer than 1 byte?
How can I handle something like this?
24 0F 00 07 00 00 00 AA BB CC DD EE 0D BD 66
Length of packet will be always known (2 and 3 bytes have info about length).
EDIT: Under "handle" I mean that I want to do next:
if (isCRCmatch() )
{
if(Packet.id == SPECIAL_ID_1)
{
// do something
}
if(Packet.id == SPECIAL_ID_2)
{
// do other
}
if(Packet.data[0] == 0xAA)
{
// do one stuff
}
if(Packet.data[1] == 0xBB && Packet.id == SPECIAL_ID_3 )
{
// do another stuff
}
}
And also (if possible ofc) I would like to send "anwers" using same structure:
U8 rspData[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
SendResponce(Packet.id, rspData);
void SendResponce (U8 id_rsp, uint8_t* data_rsp)
{
Packet_t ResponceData;
uint16_t crc;
uint8_t *data;
ResponceData.head = 0x24;
ResponceData.len = sizeof(ResponceData); // HERE IS PROBLEM ONE
ResponceData.id = id_rsp;
ResponceData.data = *data_rsp; // HERE IS PROBLEM TWO
ResponceData.end = 0x0D; // symbol '\r'
data = (uint8_t*)&ResponceData;
crc = GetCrc(data, sizeof(ResponceData)-2); // last 2 bytes with crc
ResponceData.crc = crc;//(ack_crc >> 8 | ack_crc);
SendData((U8*)&ResponceData, sizeof(ResponceData)); // Send rsp packet
}
First problem - I cant get size of all structure automatically, since pointer will be always 4 bytes... Second problem - I sure that I will lose rsp data since I don't know where is end of it.