Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C (Embedded) : Giving source file unique ID's

I'm working on a compact debug output/unit testing utility to be used on embedded systems.

I've created a system where I can output messages over a serial port to the PC in a compact manner. To save memory space/serial port bandwidth I've stripped message strings from the embedded system by giving them unique 16-bit ID's.

This was pretty straight forward because I put all messages in 1 list. A few macro's would put this into an enumeration:

projectdefs.h:

#define MESSAGE_TABLE(MSG) \
    MSG(HELLO, "Hello World!") \
    MSG(TEST, "Second message #ID 1") \
    MSG(TEST2, "Third message #ID 2")

messages.h:

#define MACRO_STR_CONCAT(a,b) a##b
#define MESSAGE_ENUM(codeName, str) MACRO_STR_CONCAT(MSG_, codeName)    

typedef enum messageNumbers_e {
    MESSAGE_TABLE(MESSAGE_ENUM),
    MESSAGE_COUNT
};

#define MESSAGE(codeName) messageSend(MACRO_STR_CONCAT(MSG_, codeName), __LINE__, file_number);

The only data transmitted over the serial port is the message ID, line number and file number (note; not string!).

The trouble I'm having is how can I assign an unique ID to every file using the C preprocessor/compiler. I don't want to store each filename string inside the embedded program. This uses (too much) memory or bandwidth on the serial port.

My idea is to define the constant file_number in each file with a macro. I would use this definition at the top of each source file:

#define ASSIGN_FILENUMBER() enum { file_number = __COUNTER__ };

However as each file is compiled separately this means the __COUNTER__ statement always starts at 0 when called and doesn't know the presence of other files or it's own ID.

Another consideration was to edit the MakeFile (script) and add file ID numbers there. However, this would tie the project build/functionality tightly to my IDE configuration, which is not desirable. Moreover, I am uncertain about the possibility with my current IDE's (XC16/XC32 compiler on Mplab X IDE or IAR Embedded Workbench).

I wonder if there is any other creative ways to have a standard C preprocessor take over task?

like image 500
Hans Avatar asked Apr 28 '13 12:04

Hans


People also ask

How do you create a unique identifier?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.

What is your unique ID number?

Your unique identifier is usually the last four digits of your SSN or your employee ID. This can change by group though, so check the instructions provided by your employer.

What is a unique identifier example?

Some agencies give people a 'unique identifier' instead of using their name. Examples are a driver's licence number, a passport number, a student ID number, or an IRD number.


2 Answers

I'm not sure that storing source code file names in the program is such a big deal for the memory. If you can have them, then one thing you could do is declare a variable like const char *fname = __FILE__; and then calculate some kind of checksum, hash or CRC and transmit this value instead of the file name. On the receiving side you can match the hash value to one of the the file names. You'll need to make sure that file name hashes don't collide, though.

Another approach would be to use your makefiles or perl or something to maintain a file counter and feed it as a macro into gcc, e.g. gcc [someparams1] -DFILENUMBER=%FILECOUNTER% somefile.c [someparams2], where %FILECOUNTER% is whatever expression that'll expand into the file counter variable's value as text. You might need to introduce a fixed order of source file compilation here.

You could combine the two methods and feed gcc with the hash instead of the counter, e.g. -DFILENAMEHASH=%HASH%, where %HASH% would expand to a numeric constant that your compiling scripts would generate based on the file name.

like image 85
Alexey Frunze Avatar answered Oct 09 '22 11:10

Alexey Frunze


Have you considered creating a 16-bit hash number based on whatever you get from FILE? It may not be perfect (read collisions), but if might be good enough for your needs. If acceptable, you would need an external lookup table so you could map your hash number ID to the file in question.

Hope this idea helps.

like image 23
Sparky Avatar answered Oct 09 '22 12:10

Sparky