How to union an array pointer? - c

I have the following struct definition:
typedef struct mb32_packet_t {
union {
struct {
uint16_t preamble;
uint8_t system_id;
uint8_t message_id;
uint8_t reserved;
uint32_t paylen;
};
uint8_t header[9];
};
uint8_t *payload;
uint16_t checksum;
} __attribute__((packed)) mb32_packet_t;
Now I would like to have another union, so that I can get an uint8_t body[] pointer to the entire packet object. Something like this:
typedef struct mb32_packet_t {
union {
struct {
union {
struct {
uint16_t preamble;
uint8_t system_id;
uint8_t message_id;
uint8_t reserved;
uint32_t paylen;
};
uint8_t header[9];
};
uint8_t *payload;
uint16_t checksum;
};
uint8_t body[?];
};
} __attribute__((packed)) mb32_packet_t;
The problem is that the payload field size is dynamically determined at runtime. Is there another way to accomplish this other than making payload fixed sized?
I basically want to send objects of this type through a network socket, so I need a uint8_t pointer that points to an object of this type. At the time of sending the object, I know the size of the entire object in bytes.

Introduction
The question is unclear, so I will discuss three apparent possibilities.
Fixed-length header followed by variable-length payload
A typical way to define a packet for a networking or messaging service is to have a fixed-length header followed by a variable-length payload. In modern C, the variable-length payload may be defined using a flexible array member, which is an array with no dimension at the end of a structure:
typedef struct
{
uint16_t preamble;
uint8_t system_id;
uint8_t message_id;
uint8_t reserved;
uint32_t paylen;
uint8_t payload[];
} mb32_packet_t;
Memory for such a structure is allocated use the base size provided by sizeof plus additional memory for the payload:
mb32_packet_t *MyPacket = malloc(sizeof *MyPacket + PayloadLength);
When you pass such an object to a routine that requires a char * or uint8_t * or similar type for its argument, you can simply convert the pointer:
SendMyMessage(…, (uint8_t *) MyPacket,…);
That cast, (uint8_t *) MyPacket, provides the pointer to the first byte of the packet requested in the question. There is no need to wedge another member into the structure or layer on a union or other declaration.
Prior to the introduction of flexible array members in C 1999, people would use one of two workarounds to create structures with variable amounts of data. One, they might just define a member array with one element and adjust the space calculations accordingly:
typedef struct
{
…
unsigned char payload[1];
} mb32_packet_t;
mb32_packet_t *MyPacket = malloc(sizeof *MyPacket + PayloadLength - 1);
Technically, that violated the C standard, since the structure contained an array of only one element even though more space was allocated for it. However, compilers were not as aggressive in their analysis of program semantics and their optimization as they are now, so it generally worked. So you may still see old code using that method.
Two, GCC had its own pre-standard implementation of flexible array members, just using an array dimension of zero instead of omitting a dimension:
typedef struct
{
…
unsigned char payload[0];
} mb32_packet_t;
Again, you may see old code using that, but new code should use the standard flexible array member.
Fixed-length header with pointer to variable-length payload
The payload-after-header form shown above is the form of packet I would most expect in a messaging packet, because it matches what the hardware has to put “on the wire” when sending bytes across a network: It writes the header bytes followed by the data bytes. So it is convenient to have them arranged that way in memory.
However, your code shows another option: The data is not in the packet but is pointed to by a pointer in the packet, with uint8_t *payload;. I would suspect that is a mistake, that the network or messaging service really wants a flexible array member, but you show it followed by another member, uint16_t checksum. A flexible array member must be the last member in a structure, so the fact that there is another member after the payload suggests this definition with a pointer may be correct for the messaging service you are working with.
However, if that is the case, it is not possible to get a pointer to the complete packet object, because the object is in two pieces. One contains the header, and the other, at some unrelated location in memory, contains the data.
As above, you can produce a uint8_t * pointer to the start of the packet with (uint8_t) MyPacket. If the messaging system knows about the pointer in the structure, that should work. If you have mistaken what the packet structure must be, it will fail.
Fixed-length header followed by fixed-length payload space
Code elsewhere on Stack Overflow shows a struct mb32_packet_t with a fixed amount of space for a payload:
typedef struct mb32_packet_t {
uint8_t compid;
uint8_t servid;
uint8_t payload[248];
uint8_t checksum;
} __attribute__((packed)) mb32_packet_s;
In this form, the packet is always a fixed size, although the amount of space used for the payload could vary. Again, you would obtain a uint8_t * pointer to the packet by a cast. There is no need for a special member for that.

This is possible, but not with a struct or union, because all parts of a struct or union need to have a known size. You can still use a struct for the header.
Because the body starts at a known location, there's a trick you can use to access it as if it was part of the structure. You can declare it with no size at all (a "flexible array member") or as 0 bytes (a GCC extension that predates the standard). The compiler will not allocate any space for it, but it will still let you use the name to refer to the end of the struct. The trick is that you can malloc extra bytes after the end of the struct, and then use body to refer to them.
typedef struct mb32_packet_t {
union {
struct {
uint16_t preamble;
uint8_t system_id;
uint8_t message_id;
uint8_t reserved;
uint32_t paylen;
};
uint8_t header[9];
};
uint8_t body[]; // flexible array member
} __attribute__((packed)) mb32_packet_t;
// This is not valid. The body is 0 bytes long, so the write is out of bounds.
mb32_packet_t my_packet;
my_packet.body[0] = 1;
// This is valid though!
mb32_packet_t *my_packet2 = malloc(sizeof(*my_packet2) + 50);
my_packet2->body[49] = 1;
// Alternative way to calculate size
mb32_packet_t *my_packet3 = malloc(offsetof(mb32_packet_t, body[50]));
my_packet3->body[49] = 1;
The flexible array member must be last. To access the checksum, you will need to allocate an extra 2 bytes, and use pointer arithmetic. Fortunately, this is just for the checksum, and not the entire header.
mb32_packet_t *my_packet = malloc(sizeof(*my_packet) + body_size + 2);
uint16_t *pchecksum = (uint16_t*)&my_packet.body[body_size];
// or
uint16_t *pchecksum = (uint16_t*)(my_packet.body + body_size);
After you fill in the header, body and checksum, then because they are contiguous in memory, a pointer to the header is also a pointer to the entire packet object.

I usually do it this way:
typedef struct
{
size_t payload_size;
double x;
char y[45];
/* another members */
unsigned char payload[];
}my_packet_t;
or if your compiler does not support FAMs
typedef struct
{
size_t payload_size;
double x;
char y[45];
/* another members */
unsigned char payload[0];
}my_packet_t;
So it the payload can be at the end of the header structure

Related

How can I pass a struct in a function with input argument unsigned 32 bit array?

I want to calculate the CRC value of some data in STM32 micro controller.
The HAL function to calculate the CRC has the following footprint:
uint32_t HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength);
My data are stored in a struct:
struct caldata_tag {
float K_P_Htng;
uint16_t K_I_Htng;
uint16_t K_D_Htng;
uint16_t K_P_Coolg; } caldata;
Who is the safest and appropriate way to pass the struct to the HAL_CRC_Calculate() function?
I am thinking about this :
#define U32BUFFERSIZE sizeof(struct caldata_tag)/sizeof(uint32_t)
uint32_t buffer[U32BUFFERSIZE];
uint32_t crcValue;
/* calculate the crc value of the data */
memcpy(buffer,&localStruct,U32BUFFERSIZE);
crcValue = HAL_CRC_Calculate(&CrcHandle,buffer,U32BUFFERSIZE);
but I am thinking that is an ugly way, could you tell me if it is ok? OR if you have a better idea?
Who is the safest and appropriate way to pass the struct to the HAL_CRC_Calculate()function?
Challenges:
HAL_CRC_Calculate() apparently wants to calculate the CRC based on multiples of uint32_t.
The size of struct caldata_tag may not be a multiple of the size of uint32_t.
struct caldata_tag may contain padding of an unknown state in caldata.
Use a union of struct caldata_tag and a large enough uint32_t array. Zero it, copy the members and then calculate the CRC.
I am thinking that is an ugly way, could you tell me if it is ok? OR if you have a better idea?
Form a helper function.
// Find the quotient of sizeof caldata_tag / sizeof(uint32_t), rounded up
#define U32BUFFERSIZE ((sizeof(struct caldata_tag) + sizeof(uint32_t) - 1)/sizeof(uint32_t))
uint32_t caldata_CRC(CRC_HandleTypeDef *hcrc, const struct caldata_tag *p) {
// u's size will be a multiple of sizeof uint32_t
union {
uint32_t u32[U32BUFFERSIZE];
struct caldata_tag tag;
} u = { {0} }; // zero every thing
// copy the members, not the padding
u.tag.K_P_Htng = p->K_P_Htng;
u.tag.K_I_Htng = p->K_I_Htng;
u.tag.K_D_Htng = p->K_D_Htng;
u.tag.K_P_Coolg = p->K_P_Coolg;
return HAL_CRC_Calculate(hcrc, u.u32, U32BUFFERSIZE);
}
Use
uint32_t crcValue = caldata_CRC(&CrcHandle, &caldata);
[Update]
Further research indicates that the BufferLength is a count of uint8_t, uint16_t, uint32_t depending on hcrc->InputDataFormat. OP has not provided that, yet if that can be set to uint8_t. then code only needs to worry about padding in struct caldata.
#define U8BUFFERSIZE sizeof(struct caldata_tag)
uint32_t caldata8_CRC(CRC_HandleTypeDef *hcrc, const struct caldata_tag *p) {
// u's size will be a multiple of sizeof uint32_t
union {
uint32_t u32[U32BUFFERSIZE];
struct caldata_tag tag;
} u = { {0} }; // zero every thing
// copy the members, not the padding
u.tag.K_P_Htng = p->K_P_Htng;
u.tag.K_I_Htng = p->K_I_Htng;
u.tag.K_D_Htng = p->K_D_Htng;
u.tag.K_P_Coolg = p->K_P_Coolg;
return HAL_CRC_Calculate(hcrc, u.u32, U8BUFFERSIZE);
}
If the compiler allows __attribute__((__packed__)), #sephiroth answer is a good way to go.
You can use a pointer that points directly to the beginning of the struct, without having to use the support buffer:
uint32_t *p = (uint32_t*)&localStruct;
There are 2 problems with this:
The first one is that you might get unexpected results if the compiler is doing padding on the struct; you can solve this by adding the (packed) attribute to the struct to tell the compiler not to do any padding
struct __attribute__((__packed__)) caldata_tag {
//...
}
The other problem is that your structure size isn't a multiple of 32, so it can't be represented in an array of uint32_t without having 16 random bits at the end of the last element. The same goes for your example, bur i think in this case you are discarding the last element of buffer because U32BUFFERSIZE should be equal to 2, so you are ignoring K_P_Coolg and the 16 random bits beside it when calculating the crc.
My suggestion when working with crc and stuff like that is using 8 bit buffers instead of 32 bit ones, as it completely eliminates the latter problem.
No workaround is needed. According to the documentation for the function HAL_CRC_Calculate: "By default, the API expects a uint32_t pointer as input buffer parameter. Input buffer pointers with other types simply need to be cast in uint32_t and the API will internally adjust its input data processing based on the handle field hcrc->InputDataFormat."
So set the field correctly in the first parameter and you can pass a pointer to bytes to the function.

Copying a struct into a byte array

I have a 1-byte pragma packed struct in C which I want to copy into a byte array for serialization purpose to be sent over a serial port.
#pragma pack(push, 1)
typedef struct {
uint8_t ck_a;
uint8_t ck_b;
} UBXChecksum_t ;
#pragma pack(pop)
What is the best way of serializing it into a byte array, should I just use memcpy()?
void writeStructToArray(const void* inStruct,
const uint16_t inLenStruct,
uint8_t* const outArray)
{
memcpy(outArray, inStruct, inLenStruct);
}
or better use byte-by-byte copying doing pointer typecasting?
void writeStructToArray(const void* inStruct,
const uint16_t inLenStruct,
uint8_t* const outArray)
{
for(uint16_t i = 0; i < inLenStruct; i++)
{
outArray[i] = ((uint8_t*)inStruct)[i];
}
}
As Kamil Cuk commented, your two proposals are nearly the same with some possible speed difference.
Another option would be to use a union:
typedef struct {
uint8_t ck_a;
uint8_t ck_b;
} UBXChecksum_t ;
union convert {
UBXChecksum_t checksum;
char buffer[sizeof UBXChecksum_t];
};
UBXChecksum_t checksum;
union convert converter;
converter.checksum = checksum;
passArrayToSomeFunction(converter.buffer, sizeof(converter.buffer));
You don't have to copy the data to convert it to an array. You could pass a pointer to the structure (if necessary casted to char* or void*) and the structure size to a function that sends the data to the serial port. Example:
typedef struct {
uint8_t ck_a;
uint8_t ck_b;
} UBXChecksum_t ;
int sendData(void *buf, size_t size);
UBXChecksum_t checksum;
/* ... */
int rc = sendData(&checksum, sizeof(checksum));
All these variants send the structure's internal representation as binary data. Normally "serializing" is understood as a way to convert the data into a platform-independent format.
Sending binary data structures works if the receiving system is of the same type and using the same compiler. You might get problems when the receiving system uses different byte order or different data type sizes.
In your case you have a structure of two uint8_t values, so the size is fixed and the byte order is not a problem.
It is OK to send binary data if the requirement for the structure is to match a specified binary data protocol and you are prepared to handle the byte order if necessary.
memcpy() will not consider endiannsess of the system. so if Sender is big endian and receiver is little endian then then will be a conflict in the receiver for the structure variable value.
With the second method you know how the byte stream is prepared at sender so at the receiving end also it can receive accordingly to make sure of the proper structure variable value.
If the endianness of the systems is same and endianness is not a concern then both the method will serve the purpose and memcpy() will be faster compare to the assigning the byte value in a loop.

How to cast a struct onto a different struct member

How can I have a variable point to a member of a different struct? This is what I'm trying to do, but the third line fails.
volatile uint8_t tx_message_buffer[sizeof(MESSAGE)];
struct MESSAGE *tx_message = (MESSAGE *)tx_message_buffer;
struct PAYLOAD *tx_payload = (PAYLOAD *)tx_message->payload;
Here are the struct definitions.
#define MSG_MAX_PAYLOAD_LENGTH 64
typedef struct PAYLOAD {
uint8_t descriptor;
uint8_t parameters[MSG_MAX_PAYLOAD_LENGTH-1];
};
typedef struct MESSAGE {
uint8_t address;
uint8_t length;
PAYLOAD payload;
uint8_t checksum;
};
This code has many problems.
As pointed out in other answers, you cannot set a pointer to point at a PAYLOAD payload; member, you need to point at its address, &tx_message->payload.
typedef struct PAYLOAD {} should be typedef struct {} PAYLOAD.
(MESSAGE *)tx_message_buffer is a completely wild cast, which invokes several cases of poorly defined behavior. First of all, you should never cast away volatile qualifiers. But also, as soon as you de-reference this struct you will violate strict aliasing and invoke undefined behavior. Anything can happen.
To solve these pointer bugs, you can do something similar to this:
typedef struct {
uint8_t address;
uint8_t length;
PAYLOAD payload;
uint8_t checksum;
} MESSAGE;
typedef union {
MESSAGE message;
uint8_t tx_message_buffer[sizeof(MESSAGE)];
} message_something;
This code is valid and well-defined.
Using a struct to represent a data protocol is bad practice, as you must ensure that the struct contains no padding at all. The memory layout in your MESSAGE struct is by no means guaranteed to correspond to the memory layout of the data protocol. The struct may have padding bytes to suit the alignment requirements of the specific CPU.
Disabling padding with non-standard C such as #pragma pack(1) may or may not be sufficient, depending on your portability requirements. To achieve full portability, you may have to write serialization/deserialization routines.
You have a bigger issue in your code: the cast on the second line is not valid, because the storage for struct MESSAGE may generally have different alignment requirements than char[] array. For example, changing the type of descriptor to uint32_t could force an even-address location for the entire struct on some platforms.
Doing it the other way around would be valid, through, because you are allowed to convert any object pointer to char *:
volatile struct MESSAGE tx_message;
volatile uint8_t *tx_message_buffer = (char*)tx_message;
The third line fails because you did not take a pointer of PAYLOAD struct:
struct PAYLOAD tx_payload = &tx_message.payload;
There is no need to cast the result, because tx_message.payload is already of the correct type.
By using,
PAYLOAD payload;
You are getting a variable not a pointer. Meaning that
message->payload;
Is not a pointer.
You need to use a pointer.
PAYLOAD * payload;
Or get the address of the struct
&message->payload;

Writing structs to a socket

So I have some structs containing data that I want to send to another process using a unix socket. This process may not be compiled using the same compiler version, or even be written in C for that matter. This is the struct (note that some stuff is commented out):
struct nested_struct {
uint8_t a;
uint8_t b;
uint16_t c;
} */__attribute__((packed))*/;
struct my_struct {
uint32_t num_nested_structs;
/* uint8_t padding[3];*/
uint8_t x;
uint16_t y;
uint16_t z;
struct nested_struct nested[];
} /*__attribute__((packed))*/;
For convenience and performance, I'd like to get away with something like
write(socket, &data.x, data.num_nested_structs * sizeof(struct nested_struct) + 5)
or something -- but I doubt this would be safe, given that struct my_struct is not nicely aligned. But how about if we un-comment the packed attribute? This feels like it should work, but I've read that referencing fields in __packed__ structs by address can be dangerous.
What if we instead uncomment the uint8_t padding[3]; field? Now both structs are word size-aligned (on a system with WORD_BIT = 32). Is it safe to assume that the compiler won't add any padding in this case? If so, is this enough to ensure that accessing 5 + 4*num_nested_structs bytes of memory starting from &my_struct.x is safe?

fixed length structure with variable length reserved space

In the embedded world we often have data structures that are passed around via fixed-length buffers. These are relatively easy to handle using something like this:
#define TOTAL_BUFFER_LENGTH 4096
struct overlay {
uint16_t field1;
uint16_t field2;
uint8_t array1[ARY1_LEN];
};
static_assert(sizeof(struct overlay) <= TOTAL_BUFFER_LENGTH);
struct overlay* overlay = malloc(TOTAL_BUFFER_LENGTH);
That is, we use a data structure as an overlay to allow easy access to the part of the buffer that is currently being used.
We have a number of buffer formats, however, that also use the last few bytes of the buffer to store things like checksums. We currently use constructions like this:
struct overlay {
uint16_t field1;
uint16_t field2;
uint8_t array1[ARY1_LEN];
char reserved[TOTAL_BUFFER_LENGTH -
sizeof(uint16_t) - sizeof(uint16_t) -
(sizeof(uint8_t) * ARY1_LEN) -
sizeof(uint32_t)];
uint32_t crc;
};
As ugly as this looks for this simple data structure, it's an absolute monstrosity when the structure grows to have dozens of fields. It's also a maintainability nightmare, as adding or removing a structure field means that the size calculation for reserved must be updated at the same time.
When the end of the structure only contains one item (like a checksum), then we sometimes use a helper function for reading/writing the value. That keeps the data structure clean and maintainable, but it doesn't scale well when the end of the buffer has multiple fields.
It would help greatly if we could do something like this instead:
struct overlay {
uint16_t field1;
uint16_t field2;
uint8_t array1[ARY1_LEN];
char reserved[TOTAL_BUFFER_LENGTH -
offsetof(struct overlay, reserved) -
sizeof(uint32_t)];
uint32_t crc;
};
Unfortunately, offsetof only works on complete object types and since this is in the middle of the definition of struct overlay, that type isn't yet complete.
Is there a cleaner, more maintainable way to do this sort of thing? I essentially need a fixed-length structure with fields at the beginning and at the end, with the remaining space in the middle reserved/unused.
In your situation, I think I'd probably do things this way:
typedef struct overlay_head
{
uint16_t field1;
uint16_t field2;
uint8_t array1[ARY1_LEN];
} overlay_head;
typedef struct overlay_tail
{
uint32_t crc;
} overlay_tail;
enum { OVERLAY_RSVD = TOTAL_BUFFER_LENGTH - sizeof(overlay_head)
- sizeof(overlay_tail) };
typedef struct overlay
{
overlay_head h;
uint8_t reserved[OVERLAY_RSVD];
overlay_tail t;
} overlay;
You can then work almost as before, except that where you used to write p->field1
you now write p->h.field1, and where you used to write p->crc you now write p->t.crc.
Note that this handles arbitrarily large tail structures quite effectively, as long as the head and tail both fit inside the overall size.
You could define a structure that simply has the buffer with a CRC field at the end:
struct checked_buffer {
char data[TOTAL_BUFFER_LENGTH - sizeof(uint32_t)];
uint32_t crc;
};
and then place your "overlays" on its data field. You're presumably already casting pointers to "convert" a raw buffer's char* into an overlay*, so it shouldn't be a big deal to cast from overlay* to checked_buffer* when you want to access the CRC field.
But if you want to have a field in a consistent position across a bunch of structures, it'd be easier to put it at the beginning of each structure. That way you can declare it directly in each structure without needing to do anything strange, and you don't need any pointer casts to access it.
How about that?
union a256
{
struct
{
int field_a;
int field_b;
char name[16];
//
int crcshadow;
};
struct
{
char buff[256-sizeof(int)];
int crc;
};
} ;
static_assert(offsetof(a256, crcshadow) < offsetof(a256, crc), "data too big");
The first struct contains data, the second define fixed size for this union.

Resources