From 69405ce183eaebcbaa31bbe0217c1af35ea89684 Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Mon, 18 Oct 2021 23:58:41 +0000 Subject: [PATCH] Paritally working --- components/modules/ble.c | 14 ++++++---- components/modules/file.c | 54 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/components/modules/ble.c b/components/modules/ble.c index 1d6ecf47..277cebbb 100644 --- a/components/modules/ble.c +++ b/components/modules/ble.c @@ -412,6 +412,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { // -1 is the services list int nc = 0; for (int i = 1; i <= ns; i++) { + MODLOG_DFLT(INFO, "Counting -- service %d (top %d)\n", i, lua_gettop(L)); lua_geti(L, -1, i); // -1 is now the service which should be a table. It must have a uuid if (lua_type(L, -1) != LUA_TTABLE) { @@ -427,6 +428,8 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { lua_pop(L, 2); } + MODLOG_DFLT(INFO, "Discovered %d services with %d characteristics\n", ns, nc); + int size = (ns + 1) * sizeof(struct ble_gatt_svc_def) + (nc + ns) * sizeof(struct ble_gatt_chr_def) + (ns + nc) * sizeof(ble_uuid_any_t); @@ -444,6 +447,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { // Now fill out the data structure // -1 is the services list for (int i = 1; i <= ns; i++) { + MODLOG_DFLT(INFO, "Processing service %d (top %d)\n", i, lua_gettop(L)); struct ble_gatt_svc_def *svc = svcs++; lua_geti(L, -1, i); // -1 is now the service which should be a table. It must have a uuid @@ -457,9 +461,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { free_gatt_svcs(L, result); return luaL_error(L, "Unable to convert UUID: %s", lua_tostring(L, -1)); } - if (i == 1) { - svc->type = BLE_GATT_SVC_TYPE_PRIMARY; - } + svc->type = BLE_GATT_SVC_TYPE_PRIMARY; svc->uuid = (ble_uuid_t *) uuids++; svc->characteristics = chrs; lua_pop(L, 1); @@ -467,6 +469,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { lua_getfield(L, -1, "characteristics"); int nc = lua_rawlen(L, -1); for (int j = 1; j <= nc; j++) { + MODLOG_DFLT(INFO, "Processing characteristic %d (top %d)\n", j, lua_gettop(L)); struct ble_gatt_chr_def *chr = chrs++; lua_geti(L, -1, j); @@ -488,13 +491,13 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { chr->flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE; lua_pop(L, 1); // pop off value } else { - lua_getfield(L, -1, "read"); + lua_getfield(L, -2, "read"); if (!lua_isnoneornil (L, -1)) { luaL_checkfunction (L, -1); chr->flags |= BLE_GATT_CHR_F_READ; } - lua_getfield(L, -1, "write"); + lua_getfield(L, -3, "write"); if (!lua_isnoneornil (L, -1)) { luaL_checkfunction (L, -1); chr->flags |= BLE_GATT_CHR_F_WRITE; @@ -508,6 +511,7 @@ lble_build_gatt_svcs(lua_State *L, struct ble_gatt_svc_def **resultp) { chr->access_cb = lble_access_cb; } lua_pop(L, 2); + chrs++; // terminate the list of characteristics for this service } lua_pop(L, 1); diff --git a/components/modules/file.c b/components/modules/file.c index 2c579cf2..933bfea8 100644 --- a/components/modules/file.c +++ b/components/modules/file.c @@ -534,6 +534,58 @@ static int file_fsinfo( lua_State* L ) return 3; } +// Lua: getfile(filename) +static int file_getfile( lua_State* L ) +{ + // Warning this code C calls other file_* routines to avoid duplication code. These + // use Lua stack addressing of arguments, so this does Lua stack maniplation to + // align these + int ret_cnt = 0; + lua_settop(L ,1); + // Stack [1] = FD + file_open(L); + // Stack [1] = filename; [2] = FD or nil + if (!lua_isnil(L, -1)) { + lua_remove(L, 1); // dump filename, so [1] = FD + file_fd_ud *ud = (file_fd_ud *)luaL_checkudata(L, 1, "file.obj"); + ret_cnt = file_g_read(L, LUAI_MAXINT32, EOF, ud->fd); + // Stack [1] = FD; [2] = contents if ret_cnt = 1; + file_close(L); // leaves Stack unchanged if [1] = FD + lua_remove(L, 1); // Dump FD leaving contents as [1] / ToS + } + return ret_cnt; +} + +// Lua: getfile(filename) +static int file_putfile( lua_State* L ) +{ + // Warning this code C calls other file_* routines to avoid duplication code. These + // use Lua stack addressing of arguments, so this does Lua stack maniplation to + // align these + int ret_cnt = 0; + lua_settop(L, 2); + lua_pushvalue(L, 2); //dup contents onto the ToS [3] + lua_pushliteral(L, "w+"); + lua_replace(L, 2); + // Stack [1] = filename; [2] "w+" [3] contents; + file_open(L); + // Stack [1] = filename; [2] "w+" [3] contents; [4] FD or nil + + if (!lua_isnil(L, -1)) { + lua_remove(L, 2); //dump "w+" attribute literal + lua_replace(L, 1); + // Stack [1] = FD; [2] contents + file_write(L); + // Stack [1] = FD; [2] contents; [3] result status + lua_remove(L, 2); //dump contents + file_close(L); + lua_remove(L, 1); // Dump FD leaving status as ToS + } + return 1; +} + + + typedef struct { vfs_vol *vol; } volume_type; @@ -570,6 +622,8 @@ LROT_BEGIN(file, NULL, 0) LROT_FUNCENTRY( writeline, file_writeline ) LROT_FUNCENTRY( read, file_read ) LROT_FUNCENTRY( readline, file_readline ) + LROT_FUNCENTRY( getcontents, file_getfile ) + LROT_FUNCENTRY( putcontents, file_putfile ) #ifdef CONFIG_NODEMCU_BUILD_SPIFFS LROT_FUNCENTRY( format, file_format ) LROT_FUNCENTRY( fscfg, file_fscfg )