C (Embedded): providing a unique identifier for the source file

I am working on a compact output / modulation debugging utility that will be used in embedded systems.

I created a system in which I can transfer messages via a serial port to a PC in a compact way. To save the memory / serial port throughput, I deleted the message lines from the embedded system by giving them unique 16-bit identifiers.

This was pretty straightforward because I put all the messages in 1 list. Several macros put this on the list:

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 through the serial port is the message identifier, line number and file number (note, not line!).

I am having a problem with how I can assign a unique identifier to each file using the C preprocessor / compiler. I do not want to store every line of the file name inside the firmware. It uses (too much) memory or bandwidth for the serial port.

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

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

However, since each file is compiled separately, this means that the __COUNTER__ operator always starts at 0 when called and does not know the presence of other files or its own identifier.

Another consideration was to edit the MakeFile (script) and add the file identification numbers. However, this will closely link the project / project functionality with my IDE configuration, which is undesirable. Moreover, I am not sure of the possibility with my current IDE (XC16 / XC32 compiler on the Mplab X IDE or IAR Embedded Workbench).

I wonder if there are any other creative ways for the standard C preprocessor to complete the task?

+8
c c-preprocessor embedded
source share
2 answers

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

Another approach would be to use your makefiles or perl or something to maintain a file counter and serve it as a macro in gcc, for example. gcc [someparams1] -DFILENUMBER=%FILECOUNTER% somefile.c [someparams2] , where %FILECOUNTER% is any expression that will expand into the value of the file counter variable as text. You may need to enter a fixed compilation order for the source file.

You can combine the two methods and serve gcc with a hash instead of a counter, for example. -DFILENAMEHASH=%HASH% , where %HASH% will expand to the numeric constant that your compilation scripts will generate based on the file name.

+6
source share

Have you considered creating a 16-bit hash number based on what you get from FILE ? It may not be ideal (read collisions), but if it may be enough for your needs. If this is acceptable, you will need an external lookup table so that you can match your hash number identifier with this file.

Hope this idea helps.

+1
source share

All Articles