I'm writing a firmware in which I would like to define functions "command" in multiple .c files, and automatically insert them in a list of function pointers. Subsequently from the serial port I want to recall one of these commands present in the array, and execute it with a list of arguments.
There is a firmware for 3d printers, on which I am relying, where such a thing is implemented.
https://github.com/KevinOConnor/klipper
The software structure used is this "remote procedure call" https://en.wikipedia.org/wiki/Remote_procedure_call
In this firmware the commands are implemented in the .c h file and there is no prototype in the .h
For example:
void command_set_digital_out(uint32_t *args)
{
gpio_out_setup(args[0], args[1]);
}
DECL_COMMAND(command_set_digital_out, "set_digital_out pin=%u value=%c");
below is a list of the definitions used
// Declare a function to run when the specified command is received
#define DECL_COMMAND_FLAGS(FUNC, FLAGS, MSG) \
DECL_CTR("DECL_COMMAND_FLAGS " __stringify(FUNC) " " \
__stringify(FLAGS) " " MSG)
#define DECL_COMMAND(FUNC, MSG) \
DECL_COMMAND_FLAGS(FUNC, 0, MSG)
// Declare a compile time request
#define DECL_CTR(REQUEST) \
static char __PASTE(_DECLS_, __LINE__)[] __attribute__((used)) \
__section(".compile_time_request") = (REQUEST)
#define __stringify_1(x) #x
#define __stringify(x) __stringify_1(x)
#define ___PASTE(a,b) a##b
#define __PASTE(a,b) ___PASTE(a,b)
Subsequently when the firmware acquires serial code it is able to recall these declared functions, as requested. How is it possible ?
I am not able to replicate this, in my firmware written for AVR in platformio, is there an easy and similar way to declare a list of functions dynamically?