views:

70

answers:

1

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;
+2  A: 

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;
Keith Nicholas
I am getting only junk. Even if the values I input are correct.
the_drow
its hard to know what you are doing, I'm just making some guesses.you have a buffer of data which is a "Message", so you have defined a message union ( which I'm a little bit suspicious of, I'm not sure if all those fields are supposed to be a union? or whether it should be a struct with some of those as a union? ). therefore your data in your buffer, the first byte will be the first byte in whatever is in message base, and also it will be the first byte of reply_message, and also it will be the first byte of message_with parameters.
Keith Nicholas
@Keith Nicholas: See my edit for my structs.payload's type is void * in case you were wondering
the_drow
so, if you do m = (message*) buff->payloadthen is m->base.u32DeviceID something sensible?I'd be suspicious of the data in payload perhaps.thenhave a look at m->base
Keith Nicholas
void blah(){ unsigned char data[] = {1,2,3,4,5,6,7,8}; message* m = (message*) data; printf("%x\r\n", m->base.u32DeviceID);}that prints out "4030201" for me....
Keith Nicholas
that was using your structs copy pasted directly
Keith Nicholas
strangely enough it now works. turns out I assigned a pointer to a pointer somehow and that messed things up. I am accepting this as the right answer since you got me going on the right direction. Thanks.
the_drow