I work with embedded software and have encountered (as well as copied from this model) an interface I never learned about in college (new grad). It allows a code base to make a library
function
call without knowing what library
is compiled with it, assuming they both have the same function
. The example below only shows a radio1, but assume there is a radio2, radio3, etc that is handled via the makefile
.
In a compiled library
we do the following. In a .c source file, the function
declarations and the interface struct
:
// function bodies that do things
void radio1_init_radio(void) {}
void radio1_calibrate_radio(void) {}
// populate global tune interface
const struct tune_if_t tune_if =
{
.init_radio = radio1_init_radio,
.calibrate_radio = radio1_calibrate_radio
}
Then, in a .h I specify the function pointers
:
struct tune_if_t
{
void (*init_radio) (void);
void (*calibrate_radio) (void);
};
extern const struct tune_if_t tune_if;
This allows the system outside of the library to call a function
without knowing which radio library is compiled and linked with it:
int main( void )
{
// interface calls
tune_if.init_radio();
tune_if.calibrate_radio();
return 0;
}
My questions are:
Is there a technical term for this? It's kind of hard for me to explain to another engineer quickly at the moment.
Is this the best approach to accomplishing this type of behavior?
Are there any issues with this approach?