Using C++ variadic templates to initialize variable-length arrays in static structs - arrays

I've been writing some code that deals with USB at the packet level. One "hangup" I've run into is that with USB, you have some structs that contain arrays of other structs (not pointers):
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} PACKED endp_descriptor_t;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
endp_descriptor_t endpoints[];
} PACKED int_descriptor_t;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
int_descriptor_t interfaces[];
} PACKED cfg_descriptor_t;
The cfg_descriptor_t contains zero or more int_descriptor_ts, and each int_descriptor_t contains zero or more endp_descriptor_ts.
I'm working on a microcontroller with fairly limited space (256K flash, 64K RAM), so I wanted to avoid dynamic allocation as much as possible and define these structs at compile time:
static cfg_descriptor_t cfg_descriptor = {
.bLength = 9,
.bDescriptorType = 2, //config descriptor
.wTotalLength = 18,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80, //bus powered, no remote wakeup
.bMaxPower = 250, //x2 = 500mA (maximum allowed by spec)
.interfaces = {
{
.bLength = 9,
.bDescriptorType = 4, //interface
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 1,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0x0,
.bInterfaceProtocol = 0x0,
.iInterface = 0,
.endpoints = {
{
.bLength = 7,
.bDescriptorType = 5, //endpoint
.bEndpointAddress = 0x81,
.bmAttributes = 0x03,
.wMaxPacketSize = 64,
.bInterval = 10,
}
}
}
}
};
Unfortunately, it seems gcc doesn't understand what I'm trying to do here, and complains about too many initializers for ‘int_descriptor_t [0]’ - i.e. it apparently can't determine the length of the array from the initializers. (Maybe I've just made some simple mistake here?)
I thought I might take advantage of C++11's variadic templates for this:
template<typename... interfaces>
struct PACKED x_cfg_descriptor_t {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
interfaces...;
};
but this doesn't work either: expected unqualified-id before ‘...’ token on the interfaces... line.
I've seen some examples of variable-sized tuples using variadic templates, but I couldn't quite understand what they were doing, and they suggested that the actual layout in memory would be in reverse order of the template parameters. (I think recursive templates are involved...)
So the question is: can I define a static struct instance with a variable number of members at compile time?

Well, I didn't exactly answer the question, but I found a much better method: move the arrays out of the structs, and define one large struct:
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bEndpointAddress;
uint8_t bmAttributes;
uint16_t wMaxPacketSize;
uint8_t bInterval;
} PACKED endp_descriptor_t;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint8_t bInterfaceNumber;
uint8_t bAlternateSetting;
uint8_t bNumEndpoints;
uint8_t bInterfaceClass;
uint8_t bInterfaceSubClass;
uint8_t bInterfaceProtocol;
uint8_t iInterface;
//endp_descriptor_t endpoints[];
} PACKED int_descriptor_t;
typedef struct {
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t wTotalLength;
uint8_t bNumInterfaces;
uint8_t bConfigurationValue;
uint8_t iConfiguration;
uint8_t bmAttributes;
uint8_t bMaxPower;
//int_descriptor_t interfaces[];
} PACKED cfg_descriptor_t;
static struct {
cfg_descriptor_t cfg;
int_descriptor_t iface[1];
endp_descriptor_t endp[1];
} PACKED cfg_descriptor = {
.cfg = {
.bLength = 9,
.bDescriptorType = 2, //config descriptor
.wTotalLength = 18,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = 0x80, //bus powered, no remote wakeup
.bMaxPower = 250, //x2 = 500mA (maximum allowed by spec)
},
.iface = {
{ //iface[0]
.bLength = 9,
.bDescriptorType = 4, //interface
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 1,
.bInterfaceClass = 0xFF,
.bInterfaceSubClass = 0x0,
.bInterfaceProtocol = 0x0,
.iInterface = 0,
},
},
.endp = {
{ //endp[0]
.bLength = 7,
.bDescriptorType = 5, //endpoint
.bEndpointAddress = 0x81,
.bmAttributes = 0x03,
.wMaxPacketSize = 64,
.bInterval = 10,
}
}
};
Using PACKED (which expands to __attribute__((__packed__))) ensures the memory layout is still what USB expects, and this allows to define any number of endpoints and interfaces per configuration descriptor. The downside is that I don't see a way to have this in an external file, besides defining a typedef struct for every configuration descriptor (and having to update it in two different places) or exporting it as a void* (and being unable to address individual fields).
This doesn't really answer the question about whether variadic templates can be used for this purpose, though...

Related

Attempting to allocate a union to a void* in a struct

The goal is to create a structure that contains 3 fields. One of those fields needs to be a uint32_t union but the flags will be different for each element. I believe the best way to do this is to create a separate union for each peripheral and assign it to a void pointer in the parent struct (somehow).
The Current Plan:
//--------------Register for AAA
typedef union
{
struct
{
uint32_t spare : 32; //< Bits 0-31 not used
}Bits;
uint32_t FullData;
}ABC_EMPTY_DATA;
ABC_EMPTY_DATA AAA_Data;
//--------------Register for BBB
typedef union
{
struct
{
uint32_t use32BitColor : 1; //< Bit0
uint32_t enableTimer : 1; //< Bit1
uint32_t fooTheBar : 1; //< Bit2
uint32_t spare : 29; //< Bits 3-31 not used
}Bits;
uint32_t FullData;
}ABC_BBB_DATA;
ABC_EMPTY_DATA BBB_Data;
//--------------Register for CCC
typedef union
{
struct
{
uint32_t useInternalFrameTick : 1; //< Bit0
uint32_t useExternalFrameTick : 1; //< Bit1
uint32_t spare : 30; //< Bits 2-31 not used
}Bits;
uint32_t FullData;
}ABC_CCC_DATA;
ABC_CCC_DATA CCC_Data;
//-------The Peripherals with register element
typedef struct
{
char *Name;
unsigned int HexCmd;
void *Data; //Connect to corasponding XXXX_Data
}ABC_dataStructs;
//----- Initializing Peripherals
typedef struct
{
ABC_dataStructs AAA = {.Name = "AAA", .HexCmd = 0x00, .Data = (void*) &AAA_Data};
ABC_dataStructs BBB = {.Name = "BBB", .HexCmd = 0x00, .Data = (void*) &BBB_Data};
ABC_dataStructs CCC = {.Name = "CCC", .HexCmd = 0x00, .Data = (void*) &CCC_Data};
}ABC_CommandList;
void main(){
ABC_CommandList everything;
everything.CCC.FullData = 0x000F;
everything.BBB.use32BitColor = 0;
int myint = everything.AAA.HexCmd;
}
Currently, I am having initializeing the values within the struct
ABC_dataStructs AAA = {.Name = "AAA", .HexCmd = 0x00, .Data = (void*) &AAA_Data};
is invalid
Also and more importantly the assigning of the union is not working as intended. The current result only allows for everything.AAA.Data without being able to assign/read individual bits.
I also tried to explain the intent of the SW in order to avoid an "XY problem" and if I am approaching this wrong or you think of something better let me know. Thank You.
Note: C struct, union pointer to struct has another approach of putting structs in the unions instead of unions to the structs, but it doesn't help with my main issue of assigning these to a void*
The problem you're having with the initialization is that you're defining a type and attempting to initialize it as if it's a variable at the same time. You need to first define the struct, then initialize an instance of it:
typedef struct
{
ABC_dataStructs AAA;
ABC_dataStructs BBB;
ABC_dataStructs CCC;
}ABC_CommandList;
void main(){
ABC_CommandList everything = {
.AAA = {.Name = "AAA", .HexCmd = 0x00, .Data = &AAA_Data},
.BBB = {.Name = "BBB", .HexCmd = 0x00, .Data = &BBB_Data},
.CCC = {.Name = "CCC", .HexCmd = 0x00, .Data = &CCC_Data},
};
...
Note also that it's not required to cast to/from a void *.
Then the problem here:
everything.CCC.FullData = 0x000F;
everything.BBB.use32BitColor = 0;
Is that CCC and BBB are of type ABC_dataStructs, and that type does not have fields named FullData or use32BitColor. You would need to cast the Data member to the proper pointer type, then dereference that:
((ABC_CCC_DATA *)everything.CCC.Data)->FullData = 0x000F;
((ABC_BBB_DATA *)everything.BBB.Data)->use32BitColor = 0;
All that being said, there's a better way of modeling this data. What you really want is a union of the 3 register types. Then ABC_dataStructs would contain that union along with a separate field which flags which union to use.
typedef struct
{
uint32_t spare : 32; //< Bits 0-31 not used
} ABC_EMPTY_DATA;
typedef struct
{
uint32_t use32BitColor : 1; //< Bit0
uint32_t enableTimer : 1; //< Bit1
uint32_t fooTheBar : 1; //< Bit2
uint32_t spare : 29; //< Bits 3-31 not used
} ABC_BBB_DATA;
typedef struct
{
uint32_t useInternalFrameTick : 1; //< Bit0
uint32_t useExternalFrameTick : 1; //< Bit1
uint32_t spare : 30; //< Bits 2-31 not used
} ABC_CCC_DATA;
typedef struct
{
char *Name;
unsigned int HexCmd;
union {
ABC_EMPTY_DATA AAA;
ABC_BBB_DATA BBB;
ABC_CCC_DATA CCC;
uint32_t FullData;
};
}ABC_dataStructs;
Then you can define the following:
ABC_CommandList everything = {
.AAA = {.Name = "AAA", .HexCmd = 0x00, .AAA = {}},
.BBB = {.Name = "BBB", .HexCmd = 0x00, .BBB = { .use32BitColor = 0 }},
.CCC = {.Name = "CCC", .HexCmd = 0x00, .FullData = 0x000F },
};

What are for square brackets in an addressed-of pointer?

I've been searching for a while what are for square-brackets in an addressed-of pointer, but I continue without understanding it. Here are the lines of the function, where "id" variable is an uint32_t pointer that has been previously passed as an argument.
#define LIST_STARTED (0x0001) /*defined out of the function*/
#define LIST_FIRST (0x0002) /*defined out of the function*/
uint32_t *pointer = id;
uint16_t *flags = &((uint16_t *)pointer)[0];
uint16_t *index = &((uint16_t *)pointer)[1];
bool started = *flags & LIST_STARTED;
bool first = *flags & LIST_FIRST;
if (!started){
/* does something */
*flags = MSEC_PRM_MGMT_LIST_STARTED;
return true;
}
if (!first){
/* does something */
*flags |= MSEC_PRM_MGMT_LIST_FIRST;
*index = 1;
return true;
}
if (*index == final_index)
/* does something */
return false;
*index += 1;
I understand what the logic of the function is, but I don't understand what do the following lines. I put "all" the code above in case it helps you.
uint16_t *flags = &((uint16_t *)pointer)[0];
uint16_t *index = &((uint16_t *)pointer)[1];
I would appreciate if someone could help me!
Thank you!
I edit to say that this C code works fine in an Embedded System, I'm not modifying it, I was just watching its behaviour.
The following code tries to read a uint32_t object as an array of two uint16_t objects:
uint32_t *id = ...;
uint32_t *pointer = id;
uint16_t *flags = &((uint16_t *)pointer)[0];
uint16_t *index = &((uint16_t *)pointer)[1];
and that is undefined behaviour to read a uint32_t object as 2 uint16_t objects through flags and index pointers because that breaks strict aliasing rules.
The correct way is:
uint16_t flags = *id; // The lowest order bits of id.
uint16_t index = *id >> 16; // The highest order bits of id.
In the above assignments of uint32_t to uint16_t it truncates the highest order bits of id.
uint32_t *pointer = id;
uint16_t *flags = &((uint16_t *)pointer)[0];
it is an equivalent of.
uint32_t *pointer = id;
uint16_t *flags = (uint16_t *)pointer;
The definition:
uint16_t *index = &((uint16_t *)pointer)[1];
Is an equivalent of:
uint16_t *temp = (uint16_t *)pointer;
uint16_t *index = temp + 1;
//or
uint16_t *index = &temp[1];
This is called: pointer punning and it is considered dangerous and not portable.
You can use unions for safe punning (at least when using gcc or its derivatives)
typedef union
{
uint64_t u64;
uint32_t u32[2];
uint16_t u16[4];
uint8_t u8[8];
struct
{
uint8_t n1: 4;
uint8_t n2: 4;
}u4[8];
}union_pune_t;
uint16_t foo16(uint32_t *p32)
{
union_pune_t *d64 = (void *)p32;
return d64 -> u16[1];
}
uint8_t foo8(uint32_t *p32)
{
union_pune_t *d64 = (void *)p32;
return d64 -> u8[5];
}
uint8_t foon4(uint32_t *p32)
{
union_pune_t *d64 = (void *)p32;
return d64 -> u4[9].n2;
}

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));

Why does this memory copy of a struct not copy bytes to byte stream as expected?

I need to serialise a struct and I am trying to do this using memcpy. But it is not working. I can tell by looking at the byte stream - I see garbage characters. Why?
Also I get runtime error:
Run-Time Check Failure #2 - Stack around the variable 'addresses' was corrupted.
What is happening and how can I fix this?
I am using #pragma pack(push, 1) which I thought would mean there would be no padding of the structs.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#pragma pack(push, 1) /* padding has to be disabled for casting to struct to work at other end */
typedef struct {
uint8_t start_char;
uint8_t msg_type;
uint8_t length;
} MSG_HEADER;
typedef struct {
uint8_t denomination[6];
uint8_t path;
uint8_t min_level;
uint16_t max_level;
uint16_t weight;
uint8_t address;
} CONFIG_DATA;
typedef struct {
MSG_HEADER header;
uint8_t clear_type;
CONFIG_DATA config_data[12];
uint8_t system_algorithm;
uint8_t max_transaction;
} MSG_CONFIGURATION;
#pragma pack(pop) /* only affect this file */
typedef struct {
unsigned char data[256];
size_t length;
int msg_type;
} TCHU_MESSAGE;
enum DRM_MESSAGE_TYPE {
CONFIG, CLEAR_COUNT, DISPENSE, CANCEL_TRANSACTION };
void TestCopy()
{
MSG_CONFIGURATION config;
config.clear_type = 0;
config.system_algorithm = 0;
config.max_transaction = 17;
const int NumItems = 12;
const uint16_t maxLevel = 300;
static const char* denoms[] = { "GB005A","GB005B","GB010A","GB010B",
"GB020A","GB050A","GB050B","GB100A",
"GB100B","GB200A", "EU100A", "EU100B" };
const uint8_t addresses[] = { 0, 0, 5, 5, 0, 7, 7, 8, 8, 9, 0, 0 };
const uint8_t sorting_paths[] = { 5, 5, 4, 4, 5, 2, 2, 1, 1, 3, 0, 0 };
for(int i = 0; i < NumItems; ++i) {
memcpy(config.config_data[i].denomination, denoms[i], 6);
config.config_data[i].address = addresses[i];
config.config_data[i].path = sorting_paths[i];
config.config_data[i].min_level = 3;
config.config_data[i].max_level = maxLevel;
config.config_data[i].weight = 1000;
}
config.header.start_char = 1;
config.header.msg_type = 2;
config.header.length = sizeof(MSG_CONFIGURATION);
TCHU_MESSAGE tchu_msg = {0};
// why does the memcpy not work? How can I get it to work?
memcpy(tchu_msg.data, &config+sizeof(MSG_HEADER), sizeof(MSG_CONFIGURATION) - sizeof(MSG_HEADER));
printf("sizeof(MSG_HEADER) = %u\n", sizeof(MSG_HEADER));
printf("sizeof(MSG_CONFIGURATION) = %u\n", sizeof(MSG_CONFIGURATION));
// get garbage in copyconfig
MSG_CONFIGURATION copyconfig;
memcpy(&copyconfig+sizeof(MSG_HEADER), tchu_msg.data, sizeof(MSG_CONFIGURATION) - sizeof(MSG_HEADER));
if(copyconfig.header.start_char != config.header.start_char)
{
// we get to here
printf("mismatch between original and copy\n");
}
}
int main() {
TestCopy();
// I also get Run-Time Check Failure #2 - Stack around the variable 'addresses' was corrupted.
// when program ends
}
My compiler instantly told me what was wrong:
warning: '__builtin___memcpy_chk' will always overflow destination buffer [-Wbuiltin-memcpy-chk-size]
memcpy(&copyconfig+sizeof(MSG_HEADER), tchu_msg.data, sizeof(MSG_CONFIGURATION) - sizeof(MSG_HEADER));
Why is that? Well, let's look at the destination:
&copyconfig + sizeof(MSG_HEADER)
That means "Take the address of copyconfig, treat it as an array, and take the Nth object where N is sizeof(MSG_HEADER). I think you thought it would add N bytes, but it actually adds N instances of MSG_CONFIGURATION. Instead, use this:
&copyconfig.header + 1
That is, "Take the address of copyconfig.header and go to just beyond it."
You could equally do this:
(char*)&copyconfig + sizeof(MSG_HEADER)
Because the size of one char is one byte. Or, since your struct is packed:
&copyconfig.clear_type
Because that's the address of the first byte you actually want to copy into.
For more details, read: Pointer Arithmetic .

Initialize C array with struct

Is it possible to initialise an array of uint8_t with a struct?
What I want to achieve is something similar to:
#define BIGGER_THAN_STRUCT 1024
struct Device {
uint32_t address;
uint32_t id;
};
const uint8_t bytes[BIGGER_THAN_STRUCT] = (struct Device) {
.address = 123,
.id = 456,
};
The reason I want to do this it to get an easy overlay view of the content I write to the byte array. I simply want an easy interface to whatever first bytes I need for the information that is displayed by the structure.
If thats not possible, what's the closest thing to it?
The standard way in C to overlay data types is by using unions:
#include <stdio.h>
#include <stdint.h>
#define BIGGER_THAN_STRUCT 1024
struct Device {
uint32_t address;
uint32_t id;
};
union Memory {
uint8_t bytes[BIGGER_THAN_STRUCT];
struct Device devices[BIGGER_THAN_STRUCT/sizeof(struct Device)];
};
const union Memory memory = {
.devices = {
{ .address = 123, .id = 30 },
{ .address = 111, .id = 89 }
}
};
int main(void)
{
unsigned i;
for (i = 0; i < 16; i++)
printf("%d ", memory.bytes[i]);
putchar('\n');
return 0;
}
,
$ ./a
123 0 0 0 30 0 0 0 111 0 0 0 89 0 0 0
Beside approaching this via a union (as proposed by hdante here https://stackoverflow.com/a/27462808/694576) instead of trying:
const uint8_t bytes[BIGGER_THAN_STRUCT] = (struct Device) {
.address = 123,
.id = 456,
};
do quick and dirty:
uint8_t bytes[BIGGER_THAN_STRUCT] = {0};
*((struct Device *) bytes) = ((struct Device) {
.address = 123,
.id = 456,
});
or better do:
struct Device dev = {
.address = 123,
.id = 456,
};
uint8_t bytes[BIGGER_THAN_STRUCT] = {0};
...
size_t size_dev = sizeof dev;
memcpy(bytes, &dev, size_dev);
Then inspect array bytes up to the size_dev - 1th element.
This will do it then
static const uint8_t buffer[BIGGER_THAN_STRUCT] = {
0x7b, 0x00, 0x00, 0x00,
0xc8, 0x01, 0x00, 0x00
};
I think you maybe want to do something like that, even if the copy is not that necessary as b_sample is exactly what you need.
#include <stdio.h>
#include <stdint.h>
typedef struct Device dev;
struct Device {
uint32_t address;
uint32_t id;
};
int main(void) {
//create an instance `sample.address=123` and `sample.id=456`
dev sample = (dev) { 123, 456 };
//convert dev pointer to byte pointer, so you loop through bytes
uint8_t* b_sample = (uint8_t *)(&sample);
//buffer for copy
uint8_t* bytes[1024];
int size = (int)(sizeof(dev)/sizeof(uint8_t)), i;
for(i = 0; i < size; i++) {
bytes[i] = b_sample[i];
//see what values you copy
printf("%x ", bytes[i]);
}
return 0;
}
Demo: http://codepad.org/wE8dbBV1
If you want to divide the struct into uint16_t segments you can safely replace all uint8_t with uint16_t
Usually, when I have to work with bag of structured bytes, I create a "view" struct/class that gives me some higher-level interface to a block of memory. Manual pointer arithmetic is usually too error prone to be repeated. Create a "memory view" structure and unit-test it properly.
struct memory_view {
uint32_t *addr;
uint32_t *id;
};
void view_init(struct memory_view *view, void *buf, size_t bufsz) {
// TODO: validate buffer size using bufsz here
view->addr = (uint32_t*)buf;
view->id = (uint32_t*)(buf + sizeof(uint32_t));
}
struct memory_view view;
uint8_t buffer[LARGE_NUMBER];
view_init(&view, buffer, LARGE_NUMBER);
*view->addr = 0xDEADBEEF;
*view->id = 0xCAFEBABE;
You can see a similar technique in device drivers, when structures are initialized to access different hardware registers located in some memory region.
You could also get a buffer pointer, cast it to structure and try using this memory block as it were a structure. Doable, but memory alignment can bite you hard. Such code may or may not work, depending on compiler and system architecture.
Does this:
#define BIGGER_THAN_STRUCT 1024
struct Device {
uint32_t address;
uint32_t id;
};
struct DeviceAndData {
struct Device d;
char filler[BIGGER_THAN_STRUCT - sizeof(Device)];
};
const struct DeviceAndData bytes_pre = { .d = { .address = 123, .id = 456 } };
const uint8_t* bytes = (uint8_t*)&bytes_pre;
do the trick? :)
There is an alternative to using a uint8_t byte[] array. You can also make use of a struct utilizing a bitfield for each addr and id. You (may/may not) find it more convenient, but it does provide an easy way to keep the offset information associated with any give addr/id pair.
I don't believe there is a way to directly make use of struct type Designated Initializers to fill the uint8_t byte array. I think the closest full initialization would be with memcpy. I've included that in the example below. Note, there is nothing that prevents you from filling the uint8_t byte array with memcpy, but then you have to track the offset within the uint8_t byte array to accurately point to any given byte in either addr or id for any given element. This is where the bitfield makes things a little easier. You get a one-to-one correlation between the struct Device index and the uibitfield index with a1..4 and b1..4 being the bytes within each addr and id, respectively.
A version using the uint8_t array is shown below this version.
Here is a short example with test data in an array of struct Device:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
typedef struct /* bitfield corresponding to struct Device */
{
unsigned int a1 : 8,
a2 : 8,
a3 : 8,
a4 : 8;
unsigned int b1 : 8,
b2 : 8,
b3 : 8,
b4 : 8;
} uibitfield;
struct Device { /* original struct Device */
uint32_t addr;
uint32_t id;
};
int main () {
/* test data in an array of struct Device */
struct Device dev[] = { {0x4009f0, 0}, {0x4009f1, 1}, {0x4009f2, 2}, {0x4009f3, 3},
{0x4009f4, 4}, {0x4009f5, 5}, {0x4009f6, 6}, {0x4009f7, 7},
{0x4009f8, 8}, {0x4009f9, 9}, {0x4009fa, 10}, {0x4009fb, 11},
{0x4009fc, 12}, {0x4009fd, 13}, {0x4009fe, 14}, {0x4009ff, 15},
{0x400a00, 16}, {0x400a01, 17}, {0x400a02, 18}, {0x400a03, 19} };
int it = 0; /* general iterator */
size_t sz = sizeof (dev)/sizeof (*dev); /* size of array */
/* create validate and fill bitfield array */
uibitfield *bytes = calloc (sz, sizeof (*bytes));
if (!bytes) {
fprintf (stderr, "error: allocation failed.\n");
return 1;
}
memcpy (bytes, dev, sz * sizeof (dev));
/* print bytes in each addr & id in dev */
for (it = 0; it < sz; it++)
printf ("\n addr[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n id[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
it, (bytes + it)->a1, (bytes + it)->a2, (bytes + it)->a3, (bytes + it)->a4,
it, (bytes + it)->b1, (bytes + it)->b2, (bytes + it)->b3, (bytes + it)->b4);
printf ("\n");
return 0;
}
output:
$ ./bin/memview
addr[ 0]: 0xf0, 0x09, 0x40, 0x00
id[ 0]: 0x00, 0x00, 0x00, 0x00
addr[ 1]: 0xf1, 0x09, 0x40, 0x00
id[ 1]: 0x01, 0x00, 0x00, 0x00
addr[ 2]: 0xf2, 0x09, 0x40, 0x00
id[ 2]: 0x02, 0x00, 0x00, 0x00
addr[ 3]: 0xf3, 0x09, 0x40, 0x00
id[ 3]: 0x03, 0x00, 0x00, 0x00
addr[ 4]: 0xf4, 0x09, 0x40, 0x00
id[ 4]: 0x04, 0x00, 0x00, 0x00
(snip)
Note: it was a unclear how you would be using/filling struct Device and how much of an initial peek you wanted at the data in stuct Device, so this is just intended as an example of viewing the data.
using a uint8_t byte array:
If you do want to use the `uint8_t array, the changes needed are minimal:
/* using a uint8_t byte array */
uint8_t *bytearr = calloc (sz * 4, sizeof (*bytearr));
if (!bytearr) {
fprintf (stderr, "error: allocation failed.\n");
return 1;
}
memcpy (bytearr, dev, sz * sizeof (dev));
/* print bytes in each addr & id in dev using uint8_t array */
for (it = 0; it < sz * 4; it+=8)
printf ("\n addr[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n id[%2d]: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
it, bytearr[it], bytearr[it+1], bytearr[it+2], bytearr[it+3],
it, bytearr[it+4], bytearr[it+5], bytearr[it+6], bytearr[it+7]);
output is the same

Resources