From ecbee6c83a7f86f4113839d97f65fcf439a0c9a8 Mon Sep 17 00:00:00 2001 From: sam <30084950+lunaboards-dev@users.noreply.github.com> Date: Sat, 29 May 2021 19:19:29 -0400 Subject: [PATCH] Static UUIDs. --- include/luares.h | 3 ++ include/lupi.h | 4 +++ src/c/event/event.c | 4 +-- src/c/lnative.c | 15 +++++++++ src/c/modules.c | 1 + src/lua/core/drive.lua | 2 +- src/lua/core/eeprom.lua | 2 +- src/lua/core/fbgpu.lua | 6 ++-- src/lua/core/filesystem.lua | 2 +- src/lua/core/gpio.lua | 2 +- src/lua/core/init.lua | 8 +++-- src/lua/core/internet.lua | 2 +- src/lua/core/serial.lua | 2 +- src/lua/core/textgpu.lua | 6 ++-- src/lua/core/uuidmgr.lua | 63 +++++++++++++++++++++++++++++++++++++ src/lua/core/winapigpu.lua | 6 ++-- 16 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 src/lua/core/uuidmgr.lua diff --git a/include/luares.h b/include/luares.h index 5f3d109..018bcec 100644 --- a/include/luares.h +++ b/include/luares.h @@ -4,6 +4,7 @@ extern char lua_boot[]; extern char lua_component[]; extern char lua_computer[]; extern char lua_debug[]; +extern char lua_drive[]; extern char lua_eeprom[]; extern char lua_fbgpu[]; extern char lua_filesystem[]; @@ -12,9 +13,11 @@ extern char lua_gpudetect[]; extern char lua_init[]; extern char lua_internet[]; extern char lua_sandbox[]; +extern char lua_serial[]; extern char lua_textgpu[]; extern char lua_util_buffer[]; extern char lua_util_color[]; extern char lua_util_random[]; +extern char lua_uuidmgr[]; extern char lua_winapigpu[]; #endif diff --git a/include/lupi.h b/include/lupi.h index 40377d4..638522a 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -22,9 +22,13 @@ typedef unsigned short ushort; lua_State* getL(); +char * get_kbid(); // ugly hack + void run_init(int argc, char **argv); void lupi_init(); void luanative_start(lua_State *L); +void serial_start(lua_State *L); +void blk_start(lua_State *L); void fb_start(lua_State *L); void setup_modules(lua_State *L); void termutils_start(lua_State *L); diff --git a/src/c/event/event.c b/src/c/event/event.c index aaab3f0..a7ba3ee 100644 --- a/src/c/event/event.c +++ b/src/c/event/event.c @@ -35,7 +35,7 @@ static void handleStdin(evutil_socket_t fd, short what, void *eventc) { lua_getglobal(L, "pushEvent"); lua_pushstring(L, "key_down"); - lua_pushstring(L, "TODO:SetThisUuid");/* Also in textgpu.lua */ + lua_pushstring(L, get_kbid());/* Also in textgpu.lua */ lua_pushnumber(L, buf); lua_pushnumber(L, -1); lua_pushstring(L, "root"); @@ -43,7 +43,7 @@ static void handleStdin(evutil_socket_t fd, short what, void *eventc) { lua_getglobal(L, "pushEvent"); lua_pushstring(L, "key_up"); - lua_pushstring(L, "TODO:SetThisUuid"); + lua_pushstring(L, get_kbid()); lua_pushnumber(L, buf); lua_pushnumber(L, -1); lua_pushstring(L, "root"); diff --git a/src/c/lnative.c b/src/c/lnative.c index 875815b..0e2f699 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -476,6 +476,20 @@ static int l_debug (lua_State *L) { } #endif +char * kbid = "TODO:SetThisUuid"; + +static int l_setkbid(lua_State *L) { + size_t len = 0; + char * id = luaL_checklstring(L, 1, &len); + kbid = malloc(len+1); + memcpy(kbid, id, len); + return 0; +} + +char * get_kbid() { + return kbid; +} + void luanative_start(lua_State *L) { struct timeval tp; @@ -511,6 +525,7 @@ void luanative_start(lua_State *L) { {"pull", l_pull}, {"platform", l_platform}, {"isinit", l_isinit}, + {"setkbid", l_setkbid}, #ifdef DEBUG {"debug", l_debug}, #endif diff --git a/src/c/modules.c b/src/c/modules.c index 841fa1f..22035c5 100644 --- a/src/c/modules.c +++ b/src/c/modules.c @@ -27,6 +27,7 @@ void setup_modules(lua_State *L) { pushstuple(L, "winapigpu", lua_winapigpu); pushstuple(L, "color", lua_util_color); pushstuple(L, "random", lua_util_random); + pushstuple(L, "uuidmgr", lua_uuidmgr); pushstuple(L, "buffer", lua_util_buffer); pushstuple(L, "eepromDefault", res_eepromDefault); diff --git a/src/lua/core/drive.lua b/src/lua/core/drive.lua index f2e6668..f3ec1bb 100644 --- a/src/lua/core/drive.lua +++ b/src/lua/core/drive.lua @@ -48,7 +48,7 @@ function drive.register() return blk.size(disk) end - modules.component.api.register("Block:"..ent, "drive", component) + modules.component.api.register(modules.uuidmgr.lookup("drive", ent), "drive", component) else lprint("Can't open blkdev: "..ent..": "..disk) end diff --git a/src/lua/core/eeprom.lua b/src/lua/core/eeprom.lua index ec01b81..53c5df1 100644 --- a/src/lua/core/eeprom.lua +++ b/src/lua/core/eeprom.lua @@ -62,7 +62,7 @@ function eeprom.register() function component.makeReadonly() return false, "Method stub" end - modules.component.api.register(nil, "eeprom", component) + modules.component.api.register(modules.uuidmgr.lookup("eeprom", "lmao"), "eeprom", component) end return eeprom diff --git a/src/lua/core/fbgpu.lua b/src/lua/core/fbgpu.lua index 0cd3268..bedeb61 100644 --- a/src/lua/core/fbgpu.lua +++ b/src/lua/core/fbgpu.lua @@ -165,9 +165,9 @@ function fbgpu.start() termutils.init() write("\x1b[?25l") --Disable cursor - modules.component.api.register(nil, "gpu", gpu) - screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c - modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) + modules.component.api.register(modules.uuidmgr.lookup("gpu", "fbgpu"), "gpu", gpu) + screenAddr = modules.component.api.register(modules.uuidmgr.lookup("screen", "lmao"), "screen", {getKeyboards = function() return {modules.uuidmgr.lookup("keyboard", "lmao")} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + modules.component.api.register(modules.uuidmgr.lookup("keyboard", "lmao"), "keyboard", {}) deadhooks[#deadhooks + 1] = function() write("\x1b[?25h") --Enable cursor on quit diff --git a/src/lua/core/filesystem.lua b/src/lua/core/filesystem.lua index 2214488..70d6924 100644 --- a/src/lua/core/filesystem.lua +++ b/src/lua/core/filesystem.lua @@ -156,7 +156,7 @@ function filesystem.register(basePath, uuid) checkArg(1, value, "number") return value --TODO: Implement, use real labels end - return modules.component.api.register(uuid, "filesystem", fs) + return modules.component.api.register(modules.uuidmgr.lookup("filesystem", basePath), "filesystem", fs) end return filesystem diff --git a/src/lua/core/gpio.lua b/src/lua/core/gpio.lua index 31f72b1..5373c85 100644 --- a/src/lua/core/gpio.lua +++ b/src/lua/core/gpio.lua @@ -68,7 +68,7 @@ gpio.register = function () end return _read("/sys/class/gpio/gpio" .. pin .. "/value") end - return modules.component.api.register(uuid, "gpio", component) + return modules.component.api.register(modules.uuidmgr.lookup("gpio", "lmao"), "gpio", component) end return gpio diff --git a/src/lua/core/init.lua b/src/lua/core/init.lua index faba5ba..8e06cbb 100644 --- a/src/lua/core/init.lua +++ b/src/lua/core/init.lua @@ -78,10 +78,14 @@ function main() loadModule("debug") end loadModule("random") + loadModule("uuidmgr") loadModule("color") loadModule("buffer") - modules.address = modules.random.uuid() --TODO: PREALPHA: Make constant + --modules.address = modules.random.uuid() --TODO: PREALPHA: Make constant + modules.address = modules.uuidmgr.lookup("modules", "lmao") --Made constant + native.setkbid(modules.uuidmgr.lookup("keyboard", "lmao")) + --Core loadModule("component") @@ -127,7 +131,7 @@ function main() if native.debug then modules.debug.hook() end - + modules.uuidmgr.store() modules.boot.boot() end diff --git a/src/lua/core/internet.lua b/src/lua/core/internet.lua index 64fe311..3cbc1d8 100644 --- a/src/lua/core/internet.lua +++ b/src/lua/core/internet.lua @@ -157,7 +157,7 @@ function internet.start() } end - modules.component.api.register(nil, "internet", component) + modules.component.api.register(modules.uuidmgr.lookup("internet", "lmao"), "internet", component) end return internet diff --git a/src/lua/core/serial.lua b/src/lua/core/serial.lua index d26552c..7688495 100644 --- a/src/lua/core/serial.lua +++ b/src/lua/core/serial.lua @@ -24,7 +24,7 @@ function com.register() return serial.read(dev, amt) end - modules.component.api.register("Serial:"..ports[i], "serial", component) + modules.component.api.register(modules.uuidmgr.lookup("serial", ports[i]), "serial", component) ::continue:: end end diff --git a/src/lua/core/textgpu.lua b/src/lua/core/textgpu.lua index cac1045..814a2ea 100644 --- a/src/lua/core/textgpu.lua +++ b/src/lua/core/textgpu.lua @@ -255,9 +255,9 @@ function textgpu.start() gpu.setForeground(0xFFFFFF) gpu.setBackground(0x000000) - local gpuaddr = modules.component.api.register(nil, "gpu", gpu) - screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c - modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) + local gpuaddr = modules.component.api.register(modules.uuidmgr.lookup("gpu", "text"), "gpu", gpu) + screenAddr = modules.component.api.register(modules.uuidmgr.lookup("screen", "lmao"), "screen", {getKeyboards = function() return {modules.uuidmgr.lookup("keyboard", "lmao")} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + modules.component.api.register(modules.uuidmgr.lookup("keyboard", "lmao"), "keyboard", {}) deadhooks[#deadhooks + 1] = function() write("\x1b[?25h\x1b[" .. ((h-1)|0) .. ";1H") --Enable cursor on quit diff --git a/src/lua/core/uuidmgr.lua b/src/lua/core/uuidmgr.lua new file mode 100644 index 0000000..76611c8 --- /dev/null +++ b/src/lua/core/uuidmgr.lua @@ -0,0 +1,63 @@ +local uuidmgr = {} + +local uuidstorage = {} + +local storeheader = "c32BH" + +do + local store = io.open("uuidstore.dat", "r") + if store then + pcall(function() + local count = string.unpack("H", store:read(2)) + for i=1, count do + local uuid, clen, ilen = storeheader:unpack(store:read(storeheader:packsize())) + local cname, id = store:read(clen), store:read(ilen) + uuidstorage[cname] = uuidstorage[cname] or {} + uuidstorage[cname][id] = uuid + end + store:close() + end) + end +end + +function uuidmgr.lookup(component, identifier) + uuidstorage[component] = uuidstorage[component] or {} + local uuid = uuidstorage[component][identifier] + if not uuid then + uuid = modules.random.uuid() + if native.debug then + lprint(string.format("DEBUG: Registed component of type %s with identifier %s as uuid %s", component, identifier, uuid)) + end + uuidstorage[component][identifier] = uuid + uuidmgr.store() + else + if native.debug then + lprint(string.format("DEBUG: Looked up component of type %s with identifier %s as uuid %s", component, identifier, uuid)) + end + end + return uuid +end + +function uuidmgr.store() + local store = io.open("uuidstore.dat", "w") + local saved_ids = {} + for component, ids in pairs(uuidstorage) do + for id, uuid in pairs(ids) do + table.insert(saved_ids, {uuid=uuid, component=component, id=id}) + end + end + if native.debug then + lprint(string.format("DEBUG: Saving %d static UUIDs", #saved_ids)) + end + store:write(string.pack("H", #saved_ids)) + for i=1, #saved_ids do + local uuid, component, id = saved_ids[i].uuid, saved_ids[i].component, saved_ids[i].id + store:write(storeheader:pack(uuid, #component, #id),component,id) + if native.debug then + lprint(string.format("DEBUG: Saving %s:%s as %s", component, id, uuid)) + end + end + store:close() +end + +return uuidmgr \ No newline at end of file diff --git a/src/lua/core/winapigpu.lua b/src/lua/core/winapigpu.lua index c709783..3f1cbb9 100644 --- a/src/lua/core/winapigpu.lua +++ b/src/lua/core/winapigpu.lua @@ -158,9 +158,9 @@ function wingpu.start() lprint("Couldn't open window: " .. tostring(reason)) end - modules.component.api.register(nil, "gpu", gpu) - screenAddr = modules.component.api.register(nil, "screen", {getKeyboards = function() return {"TODO:SetThisUuid"} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c - modules.component.api.register("TODO:SetThisUuid", "keyboard", {}) + modules.component.api.register(modules.uuidmgr.lookup("gpu", "wingpu"), "gpu", gpu) + screenAddr = modules.component.api.register(modules.uuidmgr.lookup("screen", "lmao"), "screen", {getKeyboards = function() return {modules.uuidmgr.lookup("keyboard", "lmao")} end}) --verry dummy screen, TODO: make it better, kbd uuid also in epoll.c + modules.component.api.register(modules.uuidmgr.lookup("keyboard", "lmao"), "keyboard", {}) return s end