I would like to create a static (file scope) table of data pointer, data size and data version. The problem is that the data are in external files, but constants in the extern files.
Example:
file1.c
const unsigned char data1[] =
{
0x65, 0xF0, 0xA8, 0x5F, 0x5F,
0x5F, 0x5F, 0x31, 0x32, 0x2E,
0x31, 0xF1, 0x63, 0x4D, 0x43,
0x52, 0x45, 0x41, 0x54, 0x45,
0x44, 0x20, 0x42, 0x59, 0x3A,
0x20, 0x69, 0x73, 0x70, 0x56,
// ...
};
const unsigned int data1_size =
sizeof(data1) / sizeof(data1[0]);
const unsigned int data1_version = 1;
file2.c
const unsigned char data2[] =
{
0x20, 0x44, 0x61, 0x74, 0x61,
0x20, 0x52, 0x6F, 0x77, 0x20,
0x3D, 0x20, 0x34, 0x38, 0x12,
//...
};
const unsigned int data2_size =
sizeof(data2) / sizeof(data2[0]);
const unsigned int data2_version = 1;
main.c
struct Data_Info
{
unsigned char * data_ptr;
unsigned int data_size;
unsigned int data_version;
};
extern const unsigned char data1[];
extern const unsigned int data1_size;
extern const unsigned int data1_version;
extern const unsigned char data2[];
extern const unsigned int data2_size;
extern const unsigned int data2_version;
static struct Data_Info Data_Info_Table[] =
{
// How to set this up??
// The compiler wants constant expressions here,
// and the extern is not considered a constant expression
// This is what I tried:
{ data1, data1_size, data1_version},
{ data2, data2_size, data2_version},
};
int main(void)
{
return 0;
}
I am using Green Hills Compiler, 4.2.3. The exact error message:
error #28, expression must have a constant value.
See my earlier post:
C: External const ints in a array of const struct
Note: C++ tag is included since this also applies to C++
Have you tried? (where X is 0 or 1.)
struct Data_Info
{
unsigned char const * data_ptr;
unsigned int data_size;
unsigned int data_version;
};
fileX.h
extern const struct Data_Info data_infoX;
fileX.c
const struct Data_Info data_infoX = { dataX, dataX_size, dataX_version };
main.c
const struct Data_Info const * Data_Info_Table[] =
{
&data_info1,
&data_info2
};
The problem is that the compiler doesn't know the value to place into the data_size and data_version members of the field. There are a couple of ways you can get around this without too much fuss.
Approach 1:
#include "data1.c"
#include "data2.c"
...
static struct Data_Info Data_Info_Table[] =
{
{ data1, sizeof(data1), 1},
{ data2, sizeof(data2), 1},
};
Approach 2:
struct Data_Info
{
unsigned char * data_ptr;
unsigned int * data_size;
unsigned int * data_version;
};
...
static struct Data_Info Data_Info_Table[] =
{
{ data1, &data1_size, &data1_version},
{ data2, &data2_size, &data2_version},
};
A third approach, as already demonstrated by jyoung, is to allocate the members in the separate data files and include their addresses in the Data_Info_Table.
When the constants are defined, use the extern keyword.
I am not that much of a C expert, but the order of initialization across different translation units is most probably undefined (I know it is undefined in C++, and it would be a huge break of compatibility if the behavior differed in C), so even if it was allowed by the compiler it would be dangerous.
Now, adding the C++ tag is most probably not going to help much, as AFAIK this is not only dependent on the language, but also in the specific version of the language: C89/C99. In C++ it works if you move the initialization to the declaration of the constant in the header file. Again, I am no C expert.
Related
I'm trying to cast a 115bit data to a union of bitfields and getting a wrong result.
Setup
I have two types of data:
Configuration: data[62:0]
addr[113:107]
type[114]
RawBits: lowBits[63:0]
highBits[115:64]
So I defined the following bitfields and union:
typedef struct RawBits {
unsigned long int lowBits:64;
unsigned long int highBits:51;
unsigned long int reserved :13;
} __attribute__((packed))rawBits;
typedef struct Configuration {
unsigned long int data : 62;
unsigned long int addr : 7;
unsigned long int type : 1;
unsigned long int reserved : 58;
} __attribute__((packed))Configuration ;
typedef union Instruction {
RawBits bits;
Configuration configuration;
} __attribute__((packed))Instruction;
As data I used:
uint8_t configuration_test[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xfc, 0x00,
And to convert the buffer to the union type I used simple casting:
Instruction *instruction = (Instruction *)configuration_test;
Expected result
instruction->bits->lowBits = 0xfffffffffffffffc
instruction->bits->highBits = 0x00000000000001fc
instruction->bits->reserved = 0x0000000000000000
instruction->configuration->data = 0x3fffffffffffffff
instruction->configuration->addr = 0x000000000000007f
instruction->configuration->type = 0x0000000000000000
instruction->configuration->reserved = 0x0000000000000000
Real result
instruction->bits->lowBits = 0xfcffffffffffffff
instruction->bits->highBits = 0x0004010000000000
instruction->bits->reserved = 0x000000000000001f
instruction->configuration->data = 0x3cffffffffffffff
instruction->configuration->addr = 0x0000000000000003
instruction->configuration->type = 0x0000000000000000
instruction->configuration->reserved = 0x0003f00400000000
I use for my Arduino project OneWire library and Dallas one. This one defines an DeviceAddress type which can contains an OneWire device address. I would like to create an array to store my devices address and therefore be able to loop on them.
Following does not compile
DeviceAddress waterTempSensorAddress = { 0x28, 0xCA, 0x98, 0xCF, 0x05, 0x0, 0x0, 0x51 };
DeviceAddress heatWaterSystemTemSensorAddress = { 0x28, 0xC4, 0xA8, 0xCF, 0x05, 0x0, 0x0, 0xC6 };
DeviceAddress test[] = { waterTempSensorAddress, heatWaterSystemTemSensorAddress };
The error is :
pool_manager:62: error: array must be initialized with a brace-enclosed initializer
DeviceAddress test[] = { waterTempSensorAddress, heatWaterSystemTemSensorAddress };
^
Is it possible to declare an Arduino-like array for this? Should I consider using another structure?
Thanks,
It's not really custom type, it's just typedef uint8_t DeviceAddress[8]; and arrays can't be copy constructed unlike classes.
Basically you have two simple ways to do it:
// #1
DeviceAddress test[] = { { 0x28, 0xCA, 0x98, 0xCF, 0x05, 0x0, 0x0, 0x51 }, { 0x28, 0xC4, 0xA8, 0xCF, 0x05, 0x0, 0x0, 0xC6 } };
// and eventually you can define:
DeviceAddress *waterTempSensorAddress = test;
DeviceAddress *heatWaterSystemTemSensorAddress = test + 1;
But it's not so nice.
Another way is using pointers:
// #2
DeviceAddress waterTempSensorAddress = { 0x28, 0xCA, 0x98, 0xCF, 0x05, 0x0, 0x0, 0x51 };
DeviceAddress heatWaterSystemTemSensorAddress = { 0x28, 0xC4, 0xA8, 0xCF, 0x05, 0x0, 0x0, 0xC6 };
DeviceAddress * test2[] = { &waterTempSensorAddress, &heatWaterSystemTemSensorAddress };
The first one is easier to use, second one is little bit less readable:
void da(DeviceAddress const& addr) { /* .... */ }
void something() {
da(test[0]); // #1
da(*(test2[0])); // #2
da(test2[0][0]); // #2 (it's basically two dimensional array of DeviceAddress)
}
Anyway, it's all about the C++ basics.
Little bit harder way is to use container class.
I stumbled upon a neat trick that I've started using to write binary files into (flash) memory on arduino/esp8266 using a library someone posted to one of the esp8266 forums. I've been trying a number of ways to expand upon it. Most recently I've been minifying and compressing my web content files and compiling them in with sketches on my ESP.
The script he posted first uses the output of the unix command xxd -i to write the binary file into an array of hex. The second part uses a struct to combine the file details with a pointer to the array that you can reference from the code whenever the server gets a uri request that matches an entry in the array.
What I would like to do is create a second array of these things with 'default' tools already pre-compressed so I don't have to go through it every time and/or modify my script that builds the header file any time I create a new server sketch. Basically compress and xxd stuff like jquery.js, bootstrap.css and bootstrap.js (or more often their smaller counterparts like backbone or barekit)
Currently once a file is dumped to hex, for example:
FLASH_ARRAY(uint8_t, __js__simple_js,
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x4b, 0x2b,
0xcd, 0x4b, 0x2e, 0xc9, 0xcc, 0xcf, 0x53, 0xc8, 0xad, 0xf4, 0xcf, 0xf3,
0xc9, 0x4f, 0x4c, 0xd1, 0xd0, 0xac, 0x4e, 0xcc, 0x49, 0x2d, 0x2a, 0xd1,
0x50, 0x0a, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0xc4, 0x3c, 0x85, 0xfc,
0xbc, 0x1c, 0xa0, 0x94, 0x42, 0x6e, 0x6a, 0x71, 0x71, 0x62, 0x7a, 0xaa,
0x92, 0xa6, 0x75, 0x51, 0x6a, 0x49, 0x69, 0x51, 0x9e, 0x42, 0x49, 0x51,
0x69, 0x6a, 0x2d, 0x00, 0x16, 0xa6, 0x25, 0xe5, 0x43, 0x00, 0x00, 0x00);
The existing code added them all at once along with the struct definition:
struct t_websitefiles {
const char* path;
const char* mime;
const unsigned int len;
const char* enc;
const _FLASH_ARRAY<uint8_t>* content;
} files[] = {
{
.path = "/js/simple.js",
.mime = "application/javascript",
.len = 84,
.enc = "gzip",
.content = &__js__simple_js,
},
{
/* details for file2 ...*/
},
{
/* details for file3 ...*/
}
};
Building an array of the structs representing the various files.
My questions amount to noob questions regarding the language syntax. Can I assume that I can use an identical populated struct in the place of what is inside the curly brackets? For example, if I had a second header file with my regularly used libraries, and jquery was compressed in an array called 'default_files' at position 3, could I use something like &default_files[3] in the place of { /* definitions stuffs */ }. Such as:
struct t_websitefiles {
const char* path;
const char* mime;
const unsigned int len;
const char* enc;
const _FLASH_ARRAY<uint8_t>* content;
} files[] = {
{
.path = "/js/simple.js",
.mime = "application/javascript",
.len = 84,
.enc = "gzip",
.content = &__js__simple_js,
},
&default_files[1],
&default_files[3],
{
.path = "/text/readme.txt",
.mime = "text/text",
.len = 112,
.enc = "",
.content = &__text__readme_txt,
}
};
(I'm guessing based on what I've learned thus far it needs the & in front of it?)
I also assume rather than re-writing the struct definition twice,I could do it as a typedef and then just do:
t_websitefiles files[] = { {/*definitions*/},{ /*stuffs*/ } };
Is that correct? Any help is appreciated. It's hard sometimes to find details on the syntax for specific use cases in documentation covering basics. (I would just try it, but I'm not conveniently in front of a compiler at the moment nor do I have direct access to my codebase but want to work on it later when I might not have direct access to the net)
From what I understand, you want create an array of structs such contains both compound literals and items from another array, all defined in header information.
I don't think this is possible - or at least not in the exact way you suggest. I'll try and provide an alternative though.
Can I assume that I can use an identical populated struct in the place of what is inside the curly brackets?
No - you're mixing your types. 'files' is defined as an array of 'struct t_website'.
The code
struct t_websitefiles files[] = {
...
&default_files[1],
...
}
won't compile as you are mixing your types. files is defined as an array of struct t_websitefile, but &default_files[1] is a pointer. C makes a distinction between pointers and non-pointers. They are seperate types.
The obvious option that I can see to do what you want is to use pointers. This will allow you to define everything in header information.
struct t_websitefiles default_files[] = {
....
}
struct t_websitefiles files[] = {
....
}
// An array of pointers
struct t_websitefiles *files_combined[] = {
&files[0],
&files[1],
&default_files[0],
// Or whatever values you want here
...
}
// Example main, just iterates through the combined list
// of files
int main(int argc, char* argv[]) {
int i;
int files_combined_len = sizeof(files_combined)/sizeof(struct t_websitefiles);
for (i=0; i<files_combined_len; i++) {
printf("File %s\r\n", files_combined[i]->path);
}
return 0;
}
Hope this helps.
I am working on porting an application running on Arduino Mega to LPC824. The following piece of code is working differently for both the platforms.
/**
* Calculation of CMAC
*/
void cmac(const uint8_t* data, uint8_t dataLength) {
uint8_t trailer[1] = {0x80};
uint8_t bytes[_lenRnd];
uint8_t temp[_lenRnd];
memcpy(temp, data, dataLength);
concatArray(temp, dataLength, trailer, 1);
dataLength ++;
addPadding(temp, dataLength);
memcpy(bytes, _sk2, _lenRnd);
xorBytes(bytes,temp,_lenRnd);
aes128_ctx_t ctx;
aes128_init(_sessionkey, &ctx);
uint8_t* chain = aes128_enc_sendMode(bytes, _lenRnd, &ctx, _ivect);
Board_UARTPutSTR("chain\n\r");
printBytes(chain, 16, true);
memcpy(_ivect, chain, _lenRnd);
//memcpy(_ivect, aes128_enc_sendMode(bytes,_lenRnd,&ctx,_ivect), _lenRnd);
memcpy(_cmac,_ivect, _lenRnd);
Board_UARTPutSTR("Initialization vector\n\r");
printBytes(_ivect, 16, true);
}
I am expecting a value like {0x5d, 0xa8, 0x0f, 0x1f, 0x1c, 0x03, 0x7f, 0x16, 0x7e, 0xe5, 0xfd, 0xf3, 0x45, 0xb7, 0x73, 0xa2} for the chain variable. But the follow function is working differently. The print inside the function has the correct value which I want ({5d, 0xa8, 0x0f, 0x1f, 0x1c, 0x03, 0x7f, 0x16, 0x7e, 0xe5, 0xfd, 0xf3, 0x45, 0xb7, 0x73, 0xa2}).
But when the function returns chain is having a different value, compared to what I am expecting, I get the following value for chain {0x00, 0x20, 0x00, 0x10, 0x03, 0x01, 0x00, 0x00, 0xd5, 0x00, 0x00, 0x00, 0xd7, 0x00, 0x00, 0x00}
Inside the function, the result is correct. But it returns a wrong value to the function which called it. Why is it happening so ?
uint8_t* aes128_enc_sendMode(unsigned char* data, unsigned short len, aes128_ctx_t* key,
const unsigned char* iv) {
unsigned char tmp[16];
uint8_t chain[16];
unsigned char c;
unsigned char i;
memcpy(chain, iv, 16);
while (len >= 16) {
memcpy(tmp, data, 16);
//xorBytes(tmp,chain,16);
for (i = 0; i < 16; i++) {
tmp[i] = tmp[i] ^ chain[i];
}
aes128_enc(tmp, key);
for (i = 0; i < 16; i++) {
//c = data[i];
data[i] = tmp[i];
chain[i] = tmp[i];
}
len -= 16;
data += 16;
}
Board_UARTPutSTR("Chain!!!:");
printBytes(chain, 16, true);
return chain;
}
A good start with an issue like this is to delete as much as you can while reproducing the error, with a minimal code example the answer is typically clear. I have done that for you here.
uint8_t* aes128_enc_sendMode(void) {
uint8_t chain[16];
return chain;
}
The chain variable is a local to the function, it ceases to be defined once the function exists. Accessing a pointer to that variable causes undefined behaviour, don't do it.
In practice the pointer to the array still exists and points to an arbitrary block of memory. This block of memory is no longer reserved and can be overwritten at any time.
I suspect it works for the AVR because it is a simple 8 bit chip and that piece of memory was sitting unmolested by the time you used it. The ARM would have used greater optimisations, possibly running the full array on registers, so the data doesn't survive the transition.
tldr; You need to malloc() any arrays that you want to live past the function's exit. Be careful, malloc and embedded systems go together like diesel and styrofoam, it gets messy real quick.
I am attempting to initialise a GUID variable but I not sure this is how you are meant to do it. What I am especially confused about is how to store the last 12 hexadecimal digits in the char array(do I include the "-" character?)
How do I define/initialise a GUID variable?
bool TVManager::isMonitorDevice(GUID id)
{
// Class GUID for a Monitor is: {4d36e96e-e325-11ce-bfc1-08002be10318}
GUID monitorClassGuid;
char* a = "bfc1-08002be10318"; // do I store the "-" character?
monitorClassGuid.Data1 = 0x4d36e96e;
monitorClassGuid.Data2 = 0xe325;
monitorClassGuid.Data3 = 0x11ce;
monitorClassGuid.Data4 = a;
return (bool(id == monitorClassGuid));
}
The Data4 member is not a pointer, it's an array. You'd want:
monitorClassGuid.Data4 = { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };
To make your example work. You might find it easier to do all of the initialization along with the definition of your monitorClassGuid variable:
GUID monitorClassGuid = { 0x4d36e96e, 0xe325, 0x11c3, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
This question was asked long time ago, but maybe it helps somebody else.
You can use this code to initialize a GUID:
#include <combaseapi.h>;
GUID guid;
CLSIDFromString(L"{4d36e96e-e325-11ce-bfc1-08002be10318}", &guid);