From 976870b17f5816da2f3fecd7aa272df4cfd5b105 Mon Sep 17 00:00:00 2001 From: surenyi Date: Sat, 17 Nov 2018 16:06:45 +0800 Subject: [PATCH] add luaopen_libfdt() Signed-off-by: surenyi --- packages/vsky/libdsp/lua/lualib/kit.c | 6 + packages/vsky/libdsp/lua/lualib/luafdt.c | 272 +++++++++++++++++++++++ packages/vsky/libdsp/lua/lualib/script.h | 4 + packages/vsky/libdsp/package.bld | 10 +- 4 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 packages/vsky/libdsp/lua/lualib/luafdt.c diff --git a/packages/vsky/libdsp/lua/lualib/kit.c b/packages/vsky/libdsp/lua/lualib/kit.c index 3ce9b6e..964c100 100644 --- a/packages/vsky/libdsp/lua/lualib/kit.c +++ b/packages/vsky/libdsp/lua/lualib/kit.c @@ -544,6 +544,7 @@ lua_State *script_create() luaL_openlibs(L); luaopen_dspkits(L); + luaopen_libfdt(L); return L; } @@ -551,3 +552,8 @@ void script_close(lua_State *L) { lua_close(L); } + +void script_add_global(lua_State *L, struct luaL_Reg funcs[], const char *name) +{ + __add_funcs(L, funcs, name); +} diff --git a/packages/vsky/libdsp/lua/lualib/luafdt.c b/packages/vsky/libdsp/lua/lualib/luafdt.c new file mode 100644 index 0000000..d32fdde --- /dev/null +++ b/packages/vsky/libdsp/lua/lualib/luafdt.c @@ -0,0 +1,272 @@ +#include +#include +#include "libfdt.h" +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +#include "auxiliar.h" +#include "script.h" + +struct fdt_priv { + void *blob; + char buf[0]; +}; + +#define FDT_CLASS_NAME "fdt{class}" +#define FDT_GROUP_NAME "fdt{any}" + +static int meth_strerror(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int err = luaL_checkinteger(L, 2); + + lua_pushstring(L, fdt_strerror(err)); + return 1; +} + +static int meth_del_node(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + + lua_pushinteger(L, fdt_del_node(fdt->blob, off)); + return 1; +} + +static int meth_add_subnode(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + lua_pushinteger(L, fdt_add_subnode(fdt->blob, off, s)); + return 1; +} + +static int meth_del_prop(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + lua_pushinteger(L, fdt_delprop(fdt->blob, off, s)); + return 1; +} + +static int meth_append_string(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + const char *v; + size_t len; + int r; + + v = luaL_checklstring(L, 4, &len); + + r = fdt_appendprop(fdt->blob, off, s, v, len + 1); + lua_pushinteger(L, r); + + return 1; +} + +static int meth_append_prop(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + const char *v; + size_t len; + int r; + + v = luaL_checklstring(L, 4, &len); + + r = fdt_appendprop(fdt->blob, off, s, v, len); + lua_pushinteger(L, r); + + return 1; +} + +static int meth_append_u32(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + uint32_t v = luaL_checkinteger(L, 4); + + lua_pushinteger(L, fdt_appendprop_u32(fdt->blob, off, s, v)); + return 1; +} + +static int meth_append_u64(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + uint64_t v = luaL_checkinteger(L, 4); + + lua_pushinteger(L, fdt_appendprop_u64(fdt->blob, off, s, v)); + return 1; +} + +static int meth_set_prop_empty(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + lua_pushinteger(L, fdt_setprop(fdt->blob, off, s, NULL, 0)); + return 1; +} + +static int meth_set_prop_string(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + size_t len; + const char *v = luaL_checklstring(L, 4, &len); + + lua_pushinteger(L, fdt_setprop(fdt->blob, off, s, v, len + 1)); + return 1; +} + +static int meth_set_prop(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + size_t len; + const char *v = luaL_checklstring(L, 4, &len); + + lua_pushinteger(L, fdt_setprop(fdt->blob, off, s, v, len)); + return 1; +} + +static int meth_set_prop_cell(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + uint32_t v = luaL_checkinteger(L, 4); + lua_pushinteger(L, fdt_setprop_cell(fdt->blob, off, s, v)); + return 1; +} + +static int meth_set_prop_u64(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + uint64_t v = luaL_checkinteger(L, 4); + lua_pushinteger(L, fdt_setprop_u64(fdt->blob, off, s, v)); + return 1; +} + +static int meth_set_name(lua_State *L) +{ + struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); + int off = luaL_checkinteger(L, 2); + const char *s = luaL_checkstring(L, 3); + lua_pushinteger(L, fdt_set_name(L, off, s)); + return 1; +} + +static luaL_Reg meths[] = { + {"setName", meth_set_name}, + {"setProperty", meth_set_prop}, + {"setPropertyCell", meth_set_prop_cell}, + {"setPropertyU32", meth_set_prop_cell}, + {"setPropertyU64", meth_set_prop_u64}, + {"setPropertyString", meth_set_prop_string}, + {"setPropertyEmpty", meth_set_prop_empty}, + {"appendProperty", meth_append_prop}, + {"appendCell", meth_append_u32}, + {"appendU32", meth_append_u32}, + {"appendU64", meth_append_u64}, + {"appendString",meth_append_string}, + {"delProperty", meth_del_prop}, + {"addSubnode", meth_add_subnode}, + {"delNode", meth_del_node}, + {"strerror", meth_strerror}, + {"__tostring", auxiliar_tostring}, + {NULL, NULL}, +}; + +static int __fdt_open_address(lua_State *L) +{ + struct fdt_priv *fdt; + uint32_t addr = luaL_checkinteger(L, 1); + int size = luaL_checkinteger(L, 2); + + fdt = lua_newuserdata(L, sizeof *fdt); + if (fdt == NULL) { + return 0; + } + auxiliar_setclass(L, FDT_CLASS_NAME, -1); + fdt->blob = (void *)(addr); + return 1; +} + +static int __fdt_open(lua_State *L) +{ + struct fdt_priv *fdt; + const char *s; + size_t len; + + s = luaL_checklstring(L, 1, &len); + if (!s) { + return 0; + } + fdt = lua_newuserdata(L, sizeof *fdt + len); + if (fdt == NULL) { + return 0; + } + auxiliar_setclass(L, FDT_CLASS_NAME, -1); + memcpy(fdt->buf, s, len); + fdt->blob = fdt->buf; + return 1; +} + +static int __fdt_open_file(lua_State *L) +{ + struct fdt_priv *fdt; + FILE *fp; + size_t len; + const char *name = luaL_checkstring(L, 1); + + if (!name) { + return 0; + } + fp = fopen(name, "rb"); + if (!fp) { + return 0; + } + fseek(fp, 0L, SEEK_END); + len = ftell(fp); + fseek(fp, 0L, SEEK_SET); + fdt = lua_newuserdata(L, sizeof *fdt + len); + if (fdt == NULL) { + fclose(fp); + return 0; + } + auxiliar_setclass(L, FDT_CLASS_NAME, -1); + fread(fdt->buf, 1, len, fp); + fclose(fp); + fdt->blob = fdt->buf; + return 1; +} + +int luaopen_libfdt(lua_State *L) +{ + static luaL_Reg funcs[] = { + {"open", __fdt_open}, + {"openWithFile", __fdt_open_file}, + {"openWithAddress", __fdt_open_address}, + {NULL, NULL} + }; + + auxiliar_newclass(L, FDT_CLASS_NAME, meths); + auxiliar_add2group(L, FDT_CLASS_NAME, FDT_GROUP_NAME); + + script_add_global(L, funcs, "fdt"); + return 0; +} + diff --git a/packages/vsky/libdsp/lua/lualib/script.h b/packages/vsky/libdsp/lua/lualib/script.h index d83fc3e..20e25f9 100644 --- a/packages/vsky/libdsp/lua/lualib/script.h +++ b/packages/vsky/libdsp/lua/lualib/script.h @@ -14,12 +14,16 @@ struct repl_object { }; struct lua_State; +struct luaL_Reg; struct lua_State *script_create(); void script_close(lua_State *L); int script_repl(struct lua_State *L, struct repl_object *ro); +void script_add_global(lua_State *L, struct luaL_Reg funcs[], const char *name); + int luaopen_dspkits(struct lua_State *L); +int luaopen_libfdt(struct lua_State *L); #ifdef __cplusplus } diff --git a/packages/vsky/libdsp/package.bld b/packages/vsky/libdsp/package.bld index 2c91f43..61bde2c 100644 --- a/packages/vsky/libdsp/package.bld +++ b/packages/vsky/libdsp/package.bld @@ -12,6 +12,13 @@ Pkg.otherFiles = [ "board.xdt", "Serial.xdc", "Printer.xdc", + "lua/5.3.5/lua.h", + "lua/5.3.5/lauxlib.h", + "lua/5.3.5/lualib.h", + "lua/5.3.5/luaconf.h", + "libfdt/fdt.h", + "libfdt/libfdt.h", + "libfdt/libfdt_env.h" ]; var drvFiles = [ @@ -35,13 +42,11 @@ var drvFiles = [ ]; var emacFiles = [ - /* "resource/resource_mgr.c", "resource/osal.c", "srio/srio_device.c", "srio/srio_func.c", "emac/nimu_eth.c", - */ ]; var elfFiles = [ @@ -112,6 +117,7 @@ var lualibFiles = [ "lua/lualib/auxiliar.c", "lua/lualib/kit.c", "lua/lualib/repl.c", + "lua/lualib/luafdt.c", "lua/cjson/fpconv.c", "lua/cjson/strbuf.c", "lua/cjson/lua_cjson.c",