Hi,
Is it possible to pass a uctypes structure/ pointer to c function from micropython and the C call is available by loading lib.so using ffi.open
I am getting segmentation failure when I am trying the same.
here is the sample code which I am trying
demo.c
#include <stdio.h>
struct s {
int a;
int b;
char* c;
};
int getss(struct s ss)
{
printf("print the structure\n");
printf ("a is : %d \n", ss.a);
printf ("b is : %d \n", ss.b);
printf ("string is %s \n", ss.c);
ss.a = 1;
ss.b = ss.a+1;
ss.c = "SKK\n";
printf("return ss.\n");
return ss.a;
}
and demo.py
import uctypes,ffi
struct_s = {"ptr2": (uctypes.PTR | 0, {
"a": (uctypes.INT32 | 0),
"b": (uctypes.INT32 | 4),
"c": (uctypes.ARRAY | 8, uctypes.INT8 | 10)
})
}
buf = b"12345678abcd"
stru = uctypes.struct(uctypes.addressof(buf), struct_s)
l = ffi.open('./demo.so')
gs = l.func('v', 'getss', 's')
stru.ptr2[0].a = 100
print("value is ", stru.ptr2[0].a)
stru.ptr2[0].b = 200
stru.ptr2[0].c[0] = 0x1122334455
x = gs(stru)
print (x)
print ("after call")
print ( stru.a, stru.b)
Need help.
Thanks,
kalyan.G
how to call c method which accepts structure as parameter from Micropython loading lib.so file
Re: how to call c method which accepts structure as parameter from Micropython loading lib.so file
I know that you can pass a pointer to struct. I don't think ffi supports passing a struct by value.
Re: how to call c method which accepts structure as parameter from Micropython loading lib.so file
In cases like this you can usually figure out the problem by running micropython with the debugger attached: put a breakpoint in getss and inspect the argumentKalyan wrote:I am getting segmentation failure when I am trying the same.