简体   繁体   中英

Converting struct to pointer of member type

Hi I'm a student Electronics-ICT and I'm having some trouble with a I2C project. I'm using FreeRTOS as a scheduler. To pass data between tasks I use the BaseType_t xQueueSend(QueueHandle_t xQueue, const void *pvItemToQueue, TickType_t xTicksToWait); method that requires a item (in my case a struct). I have 3 struct types, 1 is the mainframe that has a payload field, the second one is a payload request (C-APDU format) and the last struct is a respond payload (R-APDU format).

Below a example of my structs.

// mainframe I2C
typedef struct I2CMainFrame
{
    //RF or I2C host to M24SR64-Y:  
    //C-APDU M24SR64-Y to RF or I2C host: R-APDU
    uint8_t DID;    
    uint8_t PCB;    // PCB field
    void *Payload;  // Payload
    uint8_t CRC1;   // 2 CRC bytes
    uint8_t CRC2;
}   I2CMainFrame_t;

// payload request
typedef struct PayloadSend
{
    uint8_t CLA;    // Class byte 0x00: standard command 0xA2: ST comman
    uint8_t INS;    // Instruction byte
    uint8_t P1;     // Param Byte 1
    uint8_t P2;     // Param Byte 2
    uint8_t LC;     // Number of bytes of the Data field
    uint8_t *Data;  // Data bytes
    uint8_t Le;     // Number of bytes to be read in the M24SR64-Y memory
} PayloadSend_t;

// payload response
typedef struct PayloadGet
{
    uint8_t *Data;  // Pointer to data
    uint8_t SW1;    // status byte 1
    uint8_t SW2;    // status byte 2
} PayloadGet_t;

The problem is when i want to acces the data. I need to pass a pointer to a methode that writes the data byte by byte on the I2C bus or that can calculate the CRC value for example:

void CalculateCRC(uint8_t *data, size_t szLen, uint8_t *outputBuffer);
void WriteDataOnI2CBus(uint8_t *data, size_t szLen);

Is it posible to do something like this? I Tried the following code:

I2C UART task file simplified

I2CMainFrame_t mainframe;
PayloadSend_t payload;

void rtosUartTask(void)
{
    //Fill payloaddata
    uint8 data[] = {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
    payload.CLA = 0x00;      payload.INS = 0xA4;     payload.P1 = 0x04;
    payload.P2 = 0x00;       payload.LC = 0x07;
    payload.Data = data;     payload.Le = 0x00;

    //fill mainframe data
    mainframe.DID = 0xAC; mainframe.PCB = 0x02;
    mainframe.Payload = &payload;

    //methode for passing struct to I2C task
    xQueueSend(I2CQueue, &mainframe,0);
}

I2C UART task file simplified

I2CMainFrame_t mainframe;

void rtosUartTask(void)
{
     //Task manager starts this method when there is a item in the queue
     xQueueReceive(I2CQueue, &mainframe, portMAX_DELAY);
     //This doesnt work
     uint8_t *pointerToStructMembers = &mainframe;
     WriteDataOnI2CBus(pointerToStructMembers, sizeof(mainframe));
}

Am I looking in the right direction here or should I try another approach?

uint8_t *pointerToStructMembers = &mainframe;

You cannot use typecast since I2CMainFrame contains pointer member void *Payload .

What you can try is serialize the mainframe as below.

Declare an array of uint8_t then individually copy the contents to it.

uint8_t bufferedStructMembers[sizeof(I2CMainFrame_t) + sizeof (PayloadSend_t) + ((PayloadSend_t *)(mainframe.Payload))->LC];


bufferedStructMembers[0] = mainframe.DID;    
bufferedStructMembers[1] = mainframe.PCB;
bufferedStructMembers[2] = ((PayloadSend_t *)(mainframe.Payload))->CLA;
bufferedStructMembers[3] = ((PayloadSend_t *)(mainframe.Payload))->INS;
bufferedStructMembers[4] = ((PayloadSend_t *)(mainframe.Payload))->P1;
bufferedStructMembers[5] =  ((PayloadSend_t *)(mainframe.Payload))->P2;
bufferedStructMembers[6] = ((PayloadSend_t *)(mainframe.Payload))->LC;
memcpy(&bufferedStructMembers[7], ((PayloadSend_t *)(mainframe.Payload))->Data, ((PayloadSend_t *)(mainframe.Payload))->LC);
bufferedStructMembers[((PayloadSend_t *)(mainframe.Payload))->LC+7] =  mainframe.Le;
bufferedStructMembers[((PayloadSend_t *)(mainframe.Payload))->LC+8] =  mainframe.CRC1;   // 2 CRC bytes
bufferedStructMembers[((PayloadSend_t *)(mainframe.Payload))->LC+9] =  mainframe.CRC2;


WriteDataOnI2CBus(bufferedStructMembers, sizeof(mainframe));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM