I am having a problem in mapping/translating a callback function from C-DLL to Java using JNA.
on C header file following callback function is written:
// ! callback function header whenever a data report is received from a device
typedef void (FR_callback_func)(Data_t frame);
The structure of the above Data_t
is as follow:
// ! Carries information about one signal.
typedef struct
{
unsigned char index;
int isval;
unsigned short val;
int arr_Length;
unsigned char array[8];
} Data_t ;
The function in which Data_t
structure is getting called:
int getData(int val,Data_t *data);
Now I translated in my JAVA code which is as follows:
public interface device extends Library
{
public interface FR_callback_func extends Callback
{
void invoke(Data_t signal);
}
public class Data_t extends Structure implements com.sun.jna.Structure.ByReference
{
public static class ByReference extends Data_t implements Structure.ByReference { }
public byte index;
public int isval;
public short val;
public int arr_Length;
public byte[] array = new byte[8];
@Override
protected java.util.List<java.lang.String> getFieldOrder()
{
return Arrays.asList(new String[] {"index","isval","val","arr_Length","array"});
}
}
public int getData (int val,Data_t.ByReference data);
}
Then I tried to use it in my main function which is as follow:
public static void main(String[] args) throws IOException
{
Data_t .ByReference data_t = new Data_t .ByReference();
int data = 0;
int val = 0;
device h = (device) Native.load("Library", device.class);
data = h.getData (val, data_t);
}
My question is that am I translating the above C code correctly ? especially the callback function ? Since the C code can't be manipulated. Hence I have to translate the provided C-DLL code in JAVA.
Your advice will be highly appreciated.