Vikere
Vikere

Reputation: 1

function access in C

I am working on layered architecture. I came across a problem where I need to restrict access of some API from application layer.

Detail Description -

Application <---------> Middleware <-----------> MCAL.

Now, Since MCAL has its .h file include in Middleware & Middleware in turn has its .h files in application. So, it creates a room for application to directly access the MCAL API.

Can anyone help me with the easiest and best way of restricting MCAL API access from Application, but continue the access from Middleware.

Upvotes: 0

Views: 157

Answers (2)

little_birdie
little_birdie

Reputation: 5867

Well, first thing to understand, is that you cannot restrict programmer access to a C API that's linked into the program, just by manipulating the .h files. If the programmer wants to, they can just include the API .h file themselves and thus get the prototypes for the API functions.

But what you could do is.. split your middleware headers into two files. One header file would contain all the stuff that needs to be aware of the API, and this header file would include the API header file. The other header file, which is intended for the application developer, should contain only those functions declarations, data structures etc that are intended for use by the application developer, and this file would not include the API header file.

The middleware .c files would then include both headers, but the application code would include only the one for the published middleware API.

If you actually want to really prevent the application developer from using your API functions, then the way to do it would be to link your API and Middleware code as a library, and limit the symbols that are exported by the library. How to do this would be specific to the toolchain (compiler, linker etc) that you are using. It is not a feature of the C language itself, but something you can do with the tools that build the library. But here's a nice article from IBM that gives you a good idea of what's involved:

https://www.ibm.com/developerworks/aix/library/au-aix-symbol-visibility/index.html

Upvotes: 1

PareshDhandhukiya
PareshDhandhukiya

Reputation: 41

create two header file:

  1. middleware_private.h - include MCAL API here
  2. middleware.h - definition of middleware API's(do not include MCAL API headers in this file)

include middleware_private.h in middleware files. include middleware.h file in application layer files.

as middleware.h doesn't contain MCAL API header, your application layer code won't be able to access MCAL API directly.

Upvotes: 0

Related Questions