Enable different drivers with API

Thread Starter

Lorenzo Ruscitti

Joined Nov 12, 2018
13
Hi everyone,

in my project, in C, I have different drivers for some devices I am using. I have made a recognition of the device that is connected to my logic board. Instead of merging the drivers into a single file and using #ifdef I was wondering if it was possible to put all the drivers in different files and through an API select a driver file I need.

Best regards
 

mckenney

Joined Nov 10, 2018
116
Sure. Define a "driver" as an array (or struct) of function pointers, each with a particular signature and operation associated with it. Then in each driver define a function for each of the entries and populate the array. Typical functions might be Init, Open, Close, Read, Write. In main(), pick one of the vectors and call the entries indirectly ("(*p->Open)()").

Function vectors (sometimes "transfer vectors") go back at least 50 years. To see an example, dig up a description of Unix device driver design. The Unix folk didn't invent it, but they probably documented it the best.
 
Top