Solved

Assert unique values for a struct member during compile.

Posted on 2014-03-01
6
210 Views
Last Modified: 2014-03-03
I have an embedded application where I need to guarantee that a structure member has a unique value in a table of those structures. I need to ensure that the startingPosition is always unique.

const dataLogDefinition dataLogDefinitions PPAGE_E5 = {
		version:  PROTOCOL_VERSION,
		sizeOfEntry:  sizeof(dataLogField),
		entries: {
				 {.startingPosition = offsetof(CoreVar, IAT), .size = bitSize(CoreVar, IAT), .name = "IAT", .description = "Variable 1", 0,0},
				 {.startingPosition = offsetof(CoreVar, CHT), .size = bitSize(CoreVar, CHT), .name = "CHT", .description = "Variable 2", 0,0},
		}
};

Open in new window


Any ideas would be appreciated.
0
Comment
Question by:sean-keys
  • 3
  • 3
6 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 39897547
Hi Sean,

Is the table static?  How many items are in the table?  Is uniqueness enough or do they also need to be sequential, non-descending, random, etc.
0
 

Author Comment

by:sean-keys
ID: 39897563
Hi,

Yes the table is static. The length will be variable but within reason 20-30 entries. They do not need to be sequential, I just need to make sure something does not end up in there twice.
0
 

Author Comment

by:sean-keys
ID: 39897577
https://git.libreems.org/libreems-suite/libreems-firmware/blob/dataLogDefProtocol/src/main/dataLogDefinitions.c

needs to describe the data here:

https://git.libreems.org/libreems-suite/libreems-firmware/blob/dataLogDefProtocol/src/main/inc/containerTypes.h#L72


The definitions need to remain seperate because they reside in flash while the other resides in RAM, which I have much less of.

Thanks!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 39897697
Easy enough.  :)

Note the two sample lines:

                         {.startingPosition = offsetof(CoreVar, IAT), .size = bitSize(CoreVar, IAT), .name = "IAT", .description = "Variable 1", 0,0},
                         {.startingPosition = offsetof(CoreVar, CHT), .size = bitSize(CoreVar, CHT), .name = "CHT", .description = "Variable 2", 0,0},

The first line contains the string "IAT" and the constant IAT.  IAT is defined in the header file.
The second line has the same characteristics, except that the values are "CHT" and CHT.

I suggest that you simply enumerate the items that you need.


enum
{
  SP_NULL=0,      // zero
  SP_IAT,
  SP_CHT,
  SP_TPS,
  SP_EGO,
  SP_MAP,
  SP_AAP,
  SP_BRV,
  SP_MAT,
  SP_MAX
};
0
 

Author Closing Comment

by:sean-keys
ID: 39900630
I guess the "right" way to do this would be to have a script generate both C source files. It seems enum, is as good as it gets :)
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39900646
Well, for the specific application that you've shown, I don't think that a random/unique value is sufficient.  I believe that the value is also an index into the array of values returned from the PIC controller.  

You'll want to initialize the value to the correct index value.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now