I am developing firmware for an embedded application with limited memory. I have a set of commands that need to be processed as they are received. Each team falls under different "buckets", and each "bucket" receives a number of valid team numbers. For this, I created two ENUMs, as shown below.
enum { BUCKET_1 = 0x100, // Range of 0x100 to 0x1FF BUCKET_2 = 0x200, // Range of 0x200 to 0x2FF BUCKET_3 = 0x300, // Range of 0x300 to 0x3FF ... ... BUCKET_N = 0xN00 // Range of 0xN00 to 0xNFF } cmd_buckets; enum { //BUCKET_1 commands CMD_BUCKET_1_START = BUCKET_1, BUCKET_1_CMD_1, BUCKET_1_CMD_2, BUCKET_1_CMD_3, BUCKET_1_CMD_4, //Add new commands above this line BUCKET_1_CMD_MAX, //BUCKET_2 commands CMD_BUCKET_2_START = BUCKET_2, BUCKET_2_CMD_1, BUCKET_2_CMD_2, BUCKET_2_CMD_3, //Add new commands above this line BUCKET_2_CMD_MAX, //BUCKET_3 commands ... ... ... //BUCKET_N commands CMD_BUCKET_N_START = BUCKET_N BUCKET_N_CMD_1, BUCKET_N_CMD_2, BUCKET_N_CMD_3, BUCKET_N_CMD_4, //Add new commands above this line BUCKET_N_CMD_MAX, }cmd_codes
When my command handler function receives a command code, it needs to check if the command is enabled before processing it. I plan to use a bitmap for this. Commands can be enabled or disabled during processing at runtime. I can use int for each group (giving me 32 commands per group, I understand that 0xN00 to 0xN20 are valid command codes and that other codes in the range are lost). Despite the fact that the command codes are wasted, the choice of design makes it easy to tell the group the command code when viewing raw data on the console.
Since many developers can add commands to the cmd_codes enumeration (even new buckets can be added as necessary to the cmd_buckets enumeration), I want to make sure that the number of command codes in each bucket does not exceed 32 (bitmap is int). I want to catch this at compile time, not run time. Besides checking each BUCKET_N_CMD_MAX value as shown below and throwing a compile-time error, is there a better solution?
#if (BUCKET_1_CMD_MAX > 0x20) #error ("Number of commands in BUCKET_1 exceeded 32") #endif #if (BUCKET_2_CMD_MAX > 0x20) #error ("Number of commands in BUCKET_2 exceeded 32") #endif #if (BUCKET_3_CMD_MAX > 0x20) #error ("Number of commands in BUCKET_3 exceeded 32") #endif ... ... ... #if (BUCKET_N_CMD_MAX > 0x20) #error ("Number of commands in BUCKET_N exceeded 32") #endif
Please also suggest if there is a more elegant way to develop it.
Thank you, I appreciate your time and patience.