1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2024-12-27 09:58:21 +01:00
This commit is contained in:
lxsang 2019-05-08 20:55:23 +02:00
parent d5b15d20bd
commit 3eb4bd2ede
3 changed files with 63 additions and 12 deletions

View File

@ -1,4 +1,5 @@
local path = "/home/blackjack/workspace/ant-http/plugins/antd-lua-plugin/lib/ffi/example/libtest.so"
local path = "/Users/mrsang/Google Drive/ushare/cwp/ant-http/plugins/antd-lua-plugin/lib/ffi/example/libtest.so"
require("cif")
local lib = nil
local fn = nil
@ -52,16 +53,33 @@ if lib then
local buff = FFI.new(256)
FFI.call(FFI.atomic(FFI.type.VOID),{FFI.atomic(FFI.type.POINTER), FFI.atomic(FFI.type.POINTER)}, fn, {buff,"Hello world"})
echo(FFI.string(buff))
echo(tostring(FFI.bytearray(buff,5)[1]))
FFI.byteAtPut(buff,0, 11)
echo(tostring(FFI.byteAt(buff,0)))
end
local size = 1024
local struct_ptr = FFI.new(12)
local buff = FFI.new(5)
FFI.byteAtPut(buff,0,64)
FFI.byteAtPut(buff,1,65)
FFI.byteAtPut(buff,2,66)
FFI.byteAtPut(buff,3,67)
FFI.byteAtPut(buff,4,0)
FFI.byteAtPut(struct_ptr, 0, 10)
FFI.byteAtPut(struct_ptr, 1, 100)
FFI.byteAtPut(struct_ptr, 2, 0) -- pad
FFI.byteAtPut(struct_ptr, 3, 0) -- pad
FFI.byteAtPut(struct_ptr, 4, size & 0xFF)
FFI.byteAtPut(struct_ptr, 5, (size >> 8) & 0xFF)
FFI.byteAtPut(struct_ptr, 6, (size >> 16) & 0xFF)
FFI.byteAtPut(struct_ptr, 7, (size >> 24) & 0xFF)
FFI.atPutPtr(struct_ptr, 8, buff)
fn = FFI.lookup(lib, "buff")
fn = FFI.lookup(lib, "test_struct_ptr")
if(fn) then
local ptr = FFI.call(FFI.atomic(FFI.type.POINTER),{}, fn, {})
echo(FFI.string(ptr))
echo("calling test_struct_ptr")
FFI.call(FFI.atomic(FFI.type.SINT),{FFI.atomic(FFI.type.POINTER)}, fn, {struct_ptr})
end
FFI.unloadAll()

View File

@ -1,5 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
char data[] = {'h', 'e','l', 'l', 'o'};
typedef struct{
@ -16,6 +16,13 @@ typedef struct{
char d;
} test_t;
struct i2c_smbus_ioctl_data
{
char read_write ;
uint8_t command ;
int size ;
void *data ;
} ;
char greet(const char* msg, float num, int sint, char c)
{
printf("%s: '%f' '%d' '%c'\n", msg, num, sint, c);
@ -34,7 +41,9 @@ void test_string(char* buff, const char* a)
printf("%s\n", buff);
}
char * buff()
int test_struct_ptr(struct i2c_smbus_ioctl_data* data)
{
return data;
printf("Hello\n");
printf("rw %d cmd %d size %d %s\n", data->read_write, data->command, data->size, (char*)data->data);
return 1;
}

View File

@ -596,6 +596,29 @@ static int l_ffi_bytearray(lua_State* L)
memcpy(ba->data, ptr, size);
return 1;
}
struct i2c_smbus_ioctl_data
{
char read_write ;
uint8_t command ;
int size ;
void *data ;
} ;
static int l_ffi_ioctl(lua_State* L)
{
int fd = luaL_checknumber(L,1);
char rw = luaL_checknumber(L,2);
uint8_t command =luaL_checknumber(L,3);
int size = luaL_checknumber(L,4);
void* data = lua_touserdata(L,5);
struct i2c_smbus_ioctl_data args ;
args.read_write = rw ;
args.command = command ;
args.size = size ;
args.data = data ;
lua_pushnumber(L, ioctl (fd, 0x0720, &args));
return 1;
}
static const struct luaL_Reg _lib [] = {
{"dlopen", l_dlopen},
{"dlsym",l_dlsym},
@ -614,6 +637,7 @@ static const struct luaL_Reg _lib [] = {
// pointer to byte array
{"bytearray", l_ffi_bytearray},
{"free", l_ffi_free},
{"I2CIoctl", l_ffi_ioctl},
{NULL,NULL}
};