RPC Server in Lua with Custom Binary Serialization and Method Discovery

RPC server in Lua with custom binary serialization and method discovery for robust serial communication with Arduino UNO microcontrollers.

Brief Introduction

RPC Server in Lua with Custom Binary Serialization and Method Discovery (lua)
DESCRIPTION:
Implementation of an RPC server in Lua that uses a custom binary serialization for sending and receiving data, with support for dynamic discovery of exposed methods. The server is designed to communicate via serial with an Arduino device, efficiently and robustly handling remote calls and responses.

ADDITIONAL NOTES:
This example provides a robust and modular implementation of an RPC server in Lua, suitable for interfacing with microcontrollers via serial. The custom binary serialization is optimized for simplicity and speed, avoiding overhead of textual formats like JSON. Method discovery allows the client to dynamically query which functions are available, facilitating integration and debugging.

EXPLANATIONS:
- The 'serialize' function converts Lua parameters into a binary buffer according to a simple schema: type + data. It supports integer (32 bit), strings (length + data), and booleans.
- The 'deserialize' function interprets the received binary buffer reconstructing the original parameters, allowing to handle incoming RPC calls.
- The server maintains a 'methods' table with exposed functions. The special call 'list_methods' returns the list of available functions.
- Serial communication is managed via the 'luars232' library, with standard 115200 8N1 configuration.
- The protocol includes a 5-byte header: 4 bytes for payload length and 1 byte for message type (request, response, error).
- The main loop reads messages from serial, deserializes them, executes the requested function, and sends the serialized response.
- Basic integrity checks and error handling are implemented to avoid crashes and ensure stability.

Example Code: lua

-- RPC server in Lua with custom binary serialization and method discovery
-- Serial communication with Arduino

local rs232 = require('luars232')
local bit32 = bit32 or require('bit32') -- Lua 5.3+ has bit32 integrated

-- Serial configuration
local PORT_NAME = '/dev/ttyUSB0' -- Change according to system (e.g. COM3 on Windows)
local BAUD_RATE = rs232.RS232_BAUD_115200

-- Message types
local MSG_TYPE_REQUEST  = 0x01
local MSG_TYPE_RESPONSE = 0x02
local MSG_TYPE_ERROR    = 0x03

-- Exposed RPC functions
local methods = {}

-- Example function: sum two integers
methods.sum = function(a, b)
  assert(type(a) == 'number' and type(b) == 'number', 'Parameters must be numbers')
  return a + b
end

-- Example function: concatenate two strings
methods.concat = function(s1, s2)
  assert(type(s1) == 'string' and type(s2) == 'string', 'Parameters must be strings')
  return s1 .. s2
end

-- Example function: return boolean state
methods.is_even = function(n)
  assert(type(n) == 'number', 'Parameter must be a number')
  return (n % 2) == 0
end

-- Method discovery
methods.list_methods = function()
  local list = {}
  for k, v in pairs(methods) do
    if type(v) == 'function' then
      table.insert(list, k)
    end
  end
  return list
end

-- Custom binary serialization
-- Type schema: 
-- 0x01 = integer (4 bytes, signed, big endian)
-- 0x02 = string (4 byte length + utf8 bytes)
-- 0x03 = boolean (1 byte: 0x00 false, 0x01 true)

local function int_to_bytes(n)
  local b1 = bit32.rshift(bit32.band(n, 0xFF000000), 24)
  local b2 = bit32.rshift(bit32.band(n, 0x00FF0000), 16)
  local b3 = bit32.rshift(bit32.band(n, 0x0000FF00), 8)
  local b4 = bit32.band(n, 0x000000FF)
  return string.char(b1, b2, b3, b4)
end

local function bytes_to_int(s)
  local b1, b2, b3, b4 = s:byte(1,4)
  return bit32.lshift(b1,24) + bit32.lshift(b2,16) + bit32.lshift(b3,8) + b4
end

local function serialize_value(v)
  if type(v) == 'number' then
    -- 32-bit integer
    return string.char(0x01) .. int_to_bytes(v)
  elseif type(v) == 'string' then
    local len = #v
    return string.char(0x02) .. int_to_bytes(len) .. v
  elseif type(v) == 'boolean' then
    return string.char(0x03) .. (v and '\1' or '\0')
  elseif type(v) == 'table' then
    -- Serialize only array of strings for list_methods
    local result = string.char(0x04) .. int_to_bytes(#v) -- type 0x04 = array of strings
    for _, str in ipairs(v) do
      local slen = #str
      result = result .. int_to_bytes(slen) .. str
    end
    return result
  else
    error('Unsupported type for serialization: ' .. type(v))
  end
end

local function deserialize_value(data, pos)
  pos = pos or 1
  local t = data:byte(pos)
  pos = pos + 1
  if t == 0x01 then
    -- integer
    local int_bytes = data:sub(pos, pos+3)
    local val = bytes_to_int(int_bytes)
    pos = pos + 4
    -- interpret as signed 32 bit
    if val >= 0x80000000 then val = val - 0x100000000 end
    return val, pos
  elseif t == 0x02 then
    -- string
    local len = bytes_to_int(data:sub(pos, pos+3))
    pos = pos + 4
    local str = data:sub(pos, pos+len-1)
    pos = pos + len
    return str, pos
  elseif t == 0x03 then
    -- boolean
    local b = data:byte(pos)
    pos = pos + 1
    return b ~= 0, pos
  elseif t == 0x04 then
    -- array of strings
    local count = bytes_to_int(data:sub(pos, pos+3))
    pos = pos + 4
    local arr = {}
    for i=1,count do
      local slen = bytes_to_int(data:sub(pos, pos+3))
      pos = pos + 4
      local str = data:sub(pos, pos+slen-1)
      pos = pos + slen
      table.insert(arr, str)
    end
    return arr, pos
  else
    error('Unsupported type in deserialization: ' .. string.format('0x%02X', t))
  end
end

-- Serialize list of parameters
local function serialize_params(params)
  local result = ''
  for _, v in ipairs(params) do
    result = result .. serialize_value(v)
  end
  return result
end

-- Deserialize list of parameters
local function deserialize_params(data)
  local params = {}
  local pos = 1
  while pos <= #data do
    local val
    val, pos = deserialize_value(data, pos)
    table.insert(params, val)
  end
  return params
end

-- Build message with header
local function build_message(msg_type, payload)
  local len = #payload
  local header = int_to_bytes(len) .. string.char(msg_type)
  return header .. payload
end

-- Parse message from buffer
local function parse_message(buffer)
  if #buffer < 5 then return nil, buffer end -- incomplete header
  local len = bytes_to_int(buffer:sub(1,4))
  local msg_type = buffer:byte(5)
  if #buffer < 5 + len then return nil, buffer end -- incomplete payload
  local payload = buffer:sub(6, 5 + len)
  local rest = buffer:sub(6 + len)
  return {type=msg_type, payload=payload}, rest
end

-- Open serial port
local function open_serial()
  local e, port = rs232.open(PORT_NAME)
  if e ~= rs232.RS232_ERR_NOERROR then
    error('Unable to open serial port: ' .. tostring(e))
  end
  assert(port:set_baud_rate(BAUD_RATE) == rs232.RS232_ERR_NOERROR)
  assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
  assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
  assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
  assert(port:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR)
  return port
end

-- Execute RPC call
local function execute_rpc(method_name, params)
  local f = methods[method_name]
  if not f then
    error('RPC method not found: ' .. method_name)
  end
  return f(table.unpack(params))
end

-- Parse request payload: method name + parameters
local function parse_request(payload)
  -- Payload is: method name string + serialized parameters
  local pos = 1
  local method_name, newpos = deserialize_value(payload, pos)
  pos = newpos
  local params = {}
  while pos <= #payload do
    local val
    val, pos = deserialize_value(payload, pos)
    table.insert(params, val)
  end
  return method_name, params
end

-- Build response payload: serialized value
local function build_response(result)
  return serialize_value(result)
end

-- Build error payload: string message
local function build_error(msg)
  return serialize_value(msg)
end

-- Main loop
local function main()
  local port = open_serial()
  print('RPC server started on port ' .. PORT_NAME)

  local buffer = ''

  while true do
    -- Read serial data with 100ms timeout
    local e, data, size = port:read(1024, 100)
    if e == rs232.RS232_ERR_NOERROR and size > 0 then
      buffer = buffer .. data
      -- Process complete messages
      while true do
        local msg, rest = parse_message(buffer)
        if not msg then break end
        buffer = rest

        if msg.type == MSG_TYPE_REQUEST then
          -- Handle RPC request
          local status, method_name, params = pcall(parse_request, msg.payload)
          if not status then
            -- Request parsing error
            local err_msg = build_message(MSG_TYPE_ERROR, build_error('Request parsing failed: ' .. method_name))
            port:write(err_msg)
          else
            -- Execute method
            local ok, result = pcall(execute_rpc, method_name, params)
            if ok then
              local resp = build_message(MSG_TYPE_RESPONSE, build_response(result))
              port:write(resp)
            else
              local err = build_message(MSG_TYPE_ERROR, build_error('Method execution error: ' .. result))
              port:write(err)
            end
          end
        else
          -- Unhandled message types
          print('Unknown message type received: ' .. tostring(msg.type))
        end
      end
    elseif e ~= rs232.RS232_ERR_TIMEOUT then
      print('Serial read error: ' .. tostring(e))
    end
  end
end

-- Start server
main()
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Arduino UNO connected via USB serial to the PC (or Raspberry Pi) running the Lua server.

Arduino Pinout: powered via USB, no specific pins required for standard serial communication (TX=pin1, RX=pin0).

Lua 5.3+ software with 'luars232' library for serial communication (https://github.com/paulhammond/luars232).

Lua 'bit32' module or equivalent for bit manipulation (integrated in Lua 5.3+).

Serial configuration: 115200 baud, 8 data bits, no parity, 1 stop bit, no flow control.

Custom RPC protocol: binary messages with header (length, message type), payload serialized with simple schema (basic types: integer, string, boolean).

Method discovery: the server exposes a table of accessible functions and responds to requests for the list of available methods.

Error and timeout handling for serial communication and RPC calls.

Console logging for debugging and monitoring.

Standalone Lua script, ready to run on Linux/Windows systems with Lua and luars232 installed.