diff mbox series

[1/3] suricatta/lua: Add IPC types / definitions to spec

Message ID 20230517153550.262808-1-christian.storm@siemens.com
State Accepted
Delegated to: Stefano Babic
Headers show
Series [1/3] suricatta/lua: Add IPC types / definitions to spec | expand

Commit Message

Storm, Christian May 17, 2023, 3:35 p.m. UTC
Move SWUpdate IPC types and definitions from the
'General Purpose HTTP Server' example to the Lua
interface specification as it's generally useful
for Suricatta Lua Modules.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 examples/suricatta/swupdate_suricatta.lua | 80 ++---------------------
 suricatta/server_lua.c                    | 24 +++++++
 suricatta/suricatta.lua                   | 72 ++++++++++++++++++++
 3 files changed, 101 insertions(+), 75 deletions(-)
diff mbox series

Patch

diff --git a/examples/suricatta/swupdate_suricatta.lua b/examples/suricatta/swupdate_suricatta.lua
index f1d2da60..3f01b0ce 100644
--- a/examples/suricatta/swupdate_suricatta.lua
+++ b/examples/suricatta/swupdate_suricatta.lua
@@ -252,49 +252,6 @@  end
 -- suricatta.server.register(check_cancel_callback, suricatta.server.CALLBACK_CHECK_CANCEL)
 
 
---- Lua equivalent of `sourcetype` as in `include/swupdate_status.h`.
---
---- @type table<string, number>
---- @class sourcetype
---- @field SOURCE_UNKNOWN            number  0
---- @field SOURCE_WEBSERVER          number  1
---- @field SOURCE_SURICATTA          number  2
---- @field SOURCE_DOWNLOADER         number  3
---- @field SOURCE_LOCAL              number  4
---- @field SOURCE_CHUNKS_DOWNLOADER  number  5
-
-
---- Lua equivalent of `RECOVERY_STATUS` as in `include/swupdate_status.h`.
---
---- @type table<string, number>
---- @class RECOVERY_STATUS
---- @field IDLE        number  0
---- @field START       number  1
---- @field RUN         number  2
---- @field SUCCESS     number  3
---- @field FAILURE     number  4
---- @field DOWNLOAD    number  5
---- @field DONE        number  6
---- @field SUBPROCESS  number  7
---- @field PROGRESS    number  8
-
-
---- Lua equivalent of `progress_msg` as in `include/progress_ipc.h`.
---
---- @class progress_msg
---- @field magic        number           SWUpdate IPC magic number
---- @field status       RECOVERY_STATUS  Update status
---- @field dwl_percent  number           Percent of downloaded data
---- @field nsteps       number           Total steps count
---- @field cur_step     number           Current step
---- @field cur_percent  number           Percent in current step
---- @field cur_image    string           Name of the current image to be installed
---- @field hnd_name     string           Name of the running handler
---- @field source       sourcetype       The source that has triggered the update
---- @field info         string           Additional information about the installation
---- @field jsoninfo     table            If `info` is JSON, according Lua Table
-
-
 --- Progress thread callback handling progress reporting to remote.
 --
 -- Deliberately just uploading the JSON content while not respecting
@@ -306,8 +263,8 @@  end
 -- is suspended in the call to suricatta.{install,download}(). While this is
 -- safe, both callback functions should take care not to starve each other.
 --
---- @param  message  progress_msg  The progress message
---- @return suricatta.status       # Suricatta return code
+--- @param  message  suricatta.ipc.progress_msg  The progress message
+--- @return suricatta.status                     # Suricatta return code
 function progress_callback(message)
     if not gs.channel.progress then
         return suricatta.status.OK
@@ -560,40 +517,13 @@  end
 suricatta.server.register(send_target_data, suricatta.server.SEND_TARGET_DATA)
 
 
---- Lua "enum" of IPC commands as in `include/network_ipc.h`
---
--- `CMD_ENABLE` is not passed through and hence not in `ipc_commands` as
--- it's handled directly in `suricatta/suricatta.c`.
---
---- @type  table<string, number>
---- @class ipc_commands
---- @field ACTIVATION  number  0
---- @field CONFIG      number  1
---- @field GET_STATUS  number  3
-
---- Lua-alike of `ipc_message` as in `include/network_ipc.h`
---
--- Note: Some members are deliberately not passed through to the Lua realm
--- such as `ipc_message.data.len` as that's handled by the C-to-Lua bridge
--- transparently.
--- Also, this is not a direct equivalent as, e.g., the `json` field is not
--- present in `struct ipc_message`, but rather it's a "sensible" selection.
---
---- @class ipc_message
---- @field magic     number        SWUpdate IPC magic number
---- @field commands  ipc_commands  IPC commands
---- @field cmd       number        Command number, one of `ipc_commands`'s values
---- @field msg       string        String data sent via IPC
---- @field json      table         If `msg` is JSON, JSON as Lua Table
-
-
 --- Handle IPC messages sent to Suricatta Lua module.
 --
 -- Lua counterpart of `server_op_res_t server_ipc(ipc_message *msg)`.
 --
---- @param  message  ipc_message  The IPC message sent
---- @return string                # IPC reply string
---- @return suricatta.status      # Suricatta return code
+--- @param  message  suricatta.ipc.ipc_message  The IPC message sent
+--- @return string                              # IPC JSON reply string
+--- @return suricatta.status                    # Suricatta return code
 function ipc(message)
     if not (message or {}).json then
         return escape([[{ "request": "IPC requests must be JSON formatted" }]], { ['"'] = '"' }),
diff --git a/suricatta/server_lua.c b/suricatta/server_lua.c
index bdf34084..60133a80 100644
--- a/suricatta/server_lua.c
+++ b/suricatta/server_lua.c
@@ -1569,7 +1569,31 @@  static int suricatta_lua_module(lua_State *L)
 	lua_newtable(L);
 	luaL_setfuncs(L, lua_funcs_bootloader_env, 0);
 	lua_settable(L, -3);
+	lua_settable(L, -3);
 
+	lua_pushstring(L, "ipc");
+	lua_newtable(L);
+	lua_pushstring(L, "sourcetype");
+	lua_newtable(L);
+	push_to_table(L, "SOURCE_UNKNOWN", SOURCE_UNKNOWN);
+	push_to_table(L, "SOURCE_WEBSERVER", SOURCE_WEBSERVER);
+	push_to_table(L, "SOURCE_SURICATTA", SOURCE_SURICATTA);
+	push_to_table(L, "SOURCE_DOWNLOADER", SOURCE_DOWNLOADER);
+	push_to_table(L, "SOURCE_LOCAL", SOURCE_LOCAL);
+	push_to_table(L, "SOURCE_CHUNKS_DOWNLOADER", SOURCE_CHUNKS_DOWNLOADER);
+	lua_settable(L, -3);
+	lua_pushstring(L, "RECOVERY_STATUS");
+	lua_newtable(L);
+	push_to_table(L, "IDLE", IDLE);
+	push_to_table(L, "START", START);
+	push_to_table(L, "RUN", RUN);
+	push_to_table(L, "SUCCESS", SUCCESS);
+	push_to_table(L, "FAILURE", FAILURE);
+	push_to_table(L, "DOWNLOAD", DOWNLOAD);
+	push_to_table(L, "DONE", SUBPROCESS);
+	push_to_table(L, "SUBPROCESS", SUBPROCESS);
+	push_to_table(L, "PROGRESS", PROGRESS);
+	lua_settable(L, -3);
 	lua_settable(L, -3);
 
 	lua_pushstring(L, "status");
diff --git a/suricatta/suricatta.lua b/suricatta/suricatta.lua
index bfcb3be5..c61d483f 100644
--- a/suricatta/suricatta.lua
+++ b/suricatta/suricatta.lua
@@ -386,4 +386,76 @@  suricatta.get_tmpdir = function() end
 suricatta.getversion = function() end
 
 
+--- SWUpdate IPC types and definitions.
+--
+--- @class suricatta.ipc
+suricatta.ipc = {}
+
+--- @enum suricatta.ipc.sourcetype
+--- Lua equivalent of `sourcetype` as in `include/swupdate_status.h`.
+suricatta.ipc.sourcetype = {
+    SOURCE_UNKNOWN           = 0,
+    SOURCE_WEBSERVER         = 1,
+    SOURCE_SURICATTA         = 2,
+    SOURCE_DOWNLOADER        = 3,
+    SOURCE_LOCAL             = 4,
+    SOURCE_CHUNKS_DOWNLOADER = 5
+}
+
+--- @enum suricatta.ipc.RECOVERY_STATUS
+--- Lua equivalent of `RECOVERY_STATUS` as in `include/swupdate_status.h`.
+suricatta.ipc.RECOVERY_STATUS = {
+    IDLE       = 0,
+    START      = 1,
+    RUN        = 2,
+    SUCCESS    = 3,
+    FAILURE    = 4,
+    DOWNLOAD   = 5,
+    DONE       = 6,
+    SUBPROCESS = 7,
+    PROGRESS   = 8
+}
+
+--- Lua-alike of `progress_msg` as in `include/progress_ipc.h`.
+--
+--- @class suricatta.ipc.progress_msg
+--- @field magic        number                         SWUpdate IPC magic number
+--- @field status       suricatta.ipc.RECOVERY_STATUS  Update status
+--- @field dwl_percent  number                         Percent of downloaded data
+--- @field nsteps       number                         Total steps count
+--- @field cur_step     number                         Current step
+--- @field cur_percent  number                         Percent in current step
+--- @field cur_image    string                         Name of the current image to be installed (max: 256 chars)
+--- @field hnd_name     string                         Name of the running handler (max: 64 chars)
+--- @field source       suricatta.ipc.sourcetype       The source that has triggered the update
+--- @field info         string                         Additional information about the installation (max: 2048 chars)
+--- @field jsoninfo     table                          If `info` is JSON, according Lua Table
+
+--- Lua enum of IPC commands as in `include/network_ipc.h`.
+--
+-- `CMD_ENABLE` is not passed through and hence not in `ipc_commands`
+-- as it's handled directly in `suricatta/suricatta.c`.
+--
+--- @type  {[string]: number}
+--- @class suricatta.ipc.ipc_commands
+--- @field ACTIVATION  number  0
+--- @field CONFIG      number  1
+--- @field GET_STATUS  number  3
+
+--- Lua-alike of `ipc_message` as in `include/network_ipc.h`.
+--
+-- Note: Some members are deliberately not passed through to the Lua
+-- realm such as `ipc_message.data.len` since that's transparently
+-- handled by the C-to-Lua bridge.
+-- Note: This is not a direct equivalent but rather a "sensible" selection
+-- as, e.g., the `json` field is not present in `struct ipc_message`.
+--
+--- @class suricatta.ipc.ipc_message
+--- @field magic     number                      SWUpdate IPC magic number
+--- @field commands  suricatta.ipc.ipc_commands  IPC commands
+--- @field cmd       number                      Command number, one of `ipc_commands`'s values
+--- @field msg       string                      String data sent via IPC
+--- @field json      table                       If `msg` is JSON, JSON as Lua Table
+
+
 return suricatta