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.
No, since it has only room for 1
data
byte. But you can use a slightly modified version of your structure.Of course, you'd have to adapt the copying accordingly:
Regarding the added problems, it's necessary to pass the data length to
SendResponce()
:You can't have dynamic buffer in middle of a struct.
Another way to solve the problem is divide the struct to two pieces. For example (notice that
data
is here a flexible array member):Then use
Assuming that __packed allows to use unaligned access to members of __packed structs.
You should split your processing function into two distinct functions:
One which will discard everything until it finds the
head
byte. This byte usually is a constant byte, marking the start of a packet. This is done this way, in order to avoid start to read in the middle of a previously sent packet (think i.e. the startup order of the sender and the listener devices).Once it finds the start of the packet, it can read the header,
len
andid
and receive all the data storing it into yourBuffer
variable, until it reads theend
byte or there is a buffer overflow, in which case it would just discard the data and start again.NOTE that into the Buffer variable only should be written the actual data. All the other fields (len, id and so) can be stored in different variables, or in a struct containing only the
Packet information
, but no data. This way, you spit the application data from the transmission information.Note also that this function does not interpret the
id
field, nor thedata
field. It just sends this information to the other funciton, which will do the processing or discard if theid
or thedata
are not correct / known.Once the
end
byte is found, you can pass the information to the actualprocessing
function. Its header would be something like:and the call to it would be:
A more complete example could be:
For sending, just use the same format:
This function sould prepare the Packet header from
info
and read the data fromdata
.And finally, a word of caution: casting (or memcpy) a received packet directly into a struct is almost never a good idea. You have to take into account not only the zero holes (using the __packet attribute), but also the endianness and the data format representation of the sender and receiver systems, since if they are different, you would end up with the wrong values.