how to call c method which accepts structure as parameter from Micropython loading lib.so file

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Kalyan
Posts: 1
Joined: Mon Jun 05, 2017 7:28 am

how to call c method which accepts structure as parameter from Micropython loading lib.so file

Post by Kalyan » Mon Jun 05, 2017 7:47 am

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: how to call c method which accepts structure as parameter from Micropython loading lib.so file

Post by dhylands » Tue Jun 06, 2017 2:11 am

I know that you can pass a pointer to struct. I don't think ffi supports passing a struct by value.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: how to call c method which accepts structure as parameter from Micropython loading lib.so file

Post by stijn » Tue Jun 06, 2017 10:14 am

Kalyan wrote:I am getting segmentation failure when I am trying the same.
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 argument

Post Reply