I would use a combination of structures to create a register and a couple of functions to handle them.
In fpga_register.h you have something like
#define FPGA_READ = 1; #define FPGA_WRITE = 2; typedef struct register_t { char permissions; } FPGARegister; FPGARegister* fpga_init(void* address, char permissions); int fpga_write(FPGARegister* register, void* value); int fpga_read(FPGARegister* register, void* value);
using READ and WRITE in xor to express permissions.
Than in fpga_register.c you have to define a new structure
typedef struct register_t2 { char permissions; void * address; } FPGARegisterReal;
so that you return a pointer to it instead of a pointer to FPGARegister on fpga_init .
Then on fpga_read and fpga_write you check permissions and
- if the operation is allowed, drop
FPGARegister from the argument in FPGARegisterReal , perform the required action (set or read the value) and return the success code - If the operation is not allowed, just return the error code
Thus, no one, including the header file, will be able to access the FPGARegisterReal structure and, therefore, will not have direct access to the register address. Obviously, you could hack it, but I am absolutely sure that such deliberate hacks are not your real problems.
Giacomo Tesio Apr 17 '13 at 0:56 2013-04-17 00:56
source share