Question

I have this union:

typedef union Message
{
    message_base            base;
    message_with_parameters  parameters;
    reply_message          reply;
    buffer_t                *buffer; // can't figure out what to put here
} message;

message_with_parameters has a message_base as the first field and reply_message has a message_with_parameters as as the first field which in turns has message_base as as the first field.
So basically I can access any of them and I'll still get all the data I need, however I am getting a buffer from my driver and now I want to serialize it into the message.
I already know that the pointer to the buffer is wrong as it won't correlate with my structs but I can't have a fixed size buffer.
Somewhere along the way I want to do this:

m->buffer = buff->payload;

And no matter what kind of data type I have, it will still serialize.
How can it be done?

EDIT:
Here are my structs:

typedef struct MessageBase
{
    uint32_t    u32DeviceID;
    uint32_t    u32CoreID;
    uint16_t    u16Class;
    uint16_t    u16CRC;
    uint8_t     u8OpCode;

    void (*states [MAX_OPCODES]) (void *);
} message_base;

typedef struct MessageWithParameters
{
    message_base    base_class;
    uint8_t         u8Param1;
    uint8_t         u8Param2;
} message_with_parameters;

typedef message_with_parameters reply_message;

typedef union Message
{
    message_base            base;
    message_with_parameters parameters;
    reply_message           reply;
} message;
Was it helpful?

Solution

its because the data in the buffer isn't part of the union.

buffer_t* buffer is a pointer, so the pointer is part of the union, not the data which it points at

you probablly want to do something like

 m =  (message*) buff->payload;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top