Hi,
I have an .xc task that is calling a .c function from wich I would like to communicate using interfaces (client side). How such implementation would look like? Could you provide an example?
Thanks
Interfaces in mixed .c and .xc implementation Topic is solved
-
- Member
- Posts: 8
- Joined: Mon Sep 29, 2014 9:36 am
-
- Experienced Member
- Posts: 104
- Joined: Fri Dec 11, 2009 8:29 pm
If you want to do it from the client side then the best thing to do is to write wrappers with a C prototype to the interface functions. For example suppose I have:
interface my_if {
void f(int x);
void g(char y);
};
You can write the following files:
--my_if_c_api.h---
#include <xccompat.h>
void my_if_f(CLIENT_INTERFACE(my_if, i), int x);
void my_if_g(CLIENT_INTERFACE(my_if,i), char y);
--my_if_c_api.xc--
#include <my_if_c_api.h>
void my_if_f(client interface my_if i, int x) { i.f(x); }
void my_if_g(client interface my_if i, char y) { i.g(y); }
----
You can then include my_if_c_api.h in C programs and use the functions from there.
There is currently no easy way of doing the server side of interface communication directly from C. The easiest thing is have the top level select in xC which then calls C functions e.g
// These are defined in a C file
extern void my_c_f_handler(int x);
extern void my_c_g_handler(int y);
void my_task(server my_if i) {
while (1) {
select {
case i.f(int x):
my_c_f_handler(x);
break;
case i.g(int y):
my_c_g_handler(y);
break;
}
}
}
I've attached a zip with a working example showing this technique.
interface my_if {
void f(int x);
void g(char y);
};
You can write the following files:
--my_if_c_api.h---
#include <xccompat.h>
void my_if_f(CLIENT_INTERFACE(my_if, i), int x);
void my_if_g(CLIENT_INTERFACE(my_if,i), char y);
--my_if_c_api.xc--
#include <my_if_c_api.h>
void my_if_f(client interface my_if i, int x) { i.f(x); }
void my_if_g(client interface my_if i, char y) { i.g(y); }
----
You can then include my_if_c_api.h in C programs and use the functions from there.
There is currently no easy way of doing the server side of interface communication directly from C. The easiest thing is have the top level select in xC which then calls C functions e.g
// These are defined in a C file
extern void my_c_f_handler(int x);
extern void my_c_g_handler(int y);
void my_task(server my_if i) {
while (1) {
select {
case i.f(int x):
my_c_f_handler(x);
break;
case i.g(int y):
my_c_g_handler(y);
break;
}
}
}
I've attached a zip with a working example showing this technique.
You do not have the required permissions to view the files attached to this post.