forked from luca/mineqtt
1
0
Fork 0

style: add and apply editorconfig

This commit is contained in:
Luca 2024-09-05 14:52:59 +02:00
parent 0d88c472fa
commit ecebc72d3b
2 changed files with 135 additions and 124 deletions

11
.editorconfig Normal file
View File

@ -0,0 +1,11 @@
root = true
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
[*.lua]
indent_style = space
indent_size = 4

248
mqtt.lua
View File

@ -1,175 +1,175 @@
local mqtt = {} local mqtt = {}
local read_varint = function (conn, first_byte) local read_varint = function (conn, first_byte)
local b local b
if first_byte == nil then if first_byte == nil then
b = conn:read(1) b = conn:read(1)
else else
b = first_byte b = first_byte
end end
local n, s = 0, 0 local n, s = 0, 0
while b ~= nil and b & 0x80 == 0x80 do while b ~= nil and b & 0x80 == 0x80 do
if s > 21 then if s > 21 then
return 0, "number too large" return 0, "number too large"
end end
n = n + ((b & 0x7F) << s) n = n + ((b & 0x7F) << s)
s = s + 7 s = s + 7
b = conn:read(1) b = conn:read(1)
end end
if b == nil then if b == nil then
return n, "eof" return n, "eof"
end end
return n + (b << s), nil return n + (b << s), nil
end end
local MqttClient = {} local MqttClient = {}
function mqtt.open (address, port) function mqtt.open (address, port)
local conn = require("internet").open(address, port) local conn = require("internet").open(address, port)
if conn == nil then if conn == nil then
return nil, "connection failed" return nil, "connection failed"
end end
return MqttClient:new(conn), nil return MqttClient:new(conn), nil
end end
function MqttClient:new (conn) function MqttClient:new (conn)
local c = c or {} local c = c or {}
setmetatable(c, self) setmetatable(c, self)
self.__index = self self.__index = self
conn.read_varint = read_varint conn.read_varint = read_varint
c.conn = conn c.conn = conn
c.is_connecting = false c.is_connecting = false
c.is_connected = false c.is_connected = false
return c return c
end end
function MqttClient:handle () function MqttClient:handle ()
local data = self.conn:read(2) local data = self.conn:read(2)
if data == nil then if data == nil then
return "eof" return "eof"
end end
local ptype, length, _ = string.unpack("B B", s) local ptype, length, _ = string.unpack("B B", s)
local length, err = self.conn:read_varint(length) local length, err = self.conn:read_varint(length)
if err ~= nil then if err ~= nil then
return err return err
end end
if length > 0 then if length > 0 then
data = self.conn:read(length) data = self.conn:read(length)
if data == nil then if data == nil then
return "eof" return "eof"
end end
else else
data = "" data = ""
end end
if ptype & 0xF0 == 0x20 then -- CONNACK if ptype & 0xF0 == 0x20 then -- CONNACK
if ptype ~= 0x20 or length < 2 then if ptype ~= 0x20 or length < 2 then
self:disconnect(0x81) self:disconnect(0x81)
return "malformed packet" return "malformed packet"
end end
local flags, reason, _ = string.unpack("B B", data) local flags, reason, _ = string.unpack("B B", data)
if flags ~= 0 then if flags ~= 0 then
self:disconnect(0x81) self:disconnect(0x81)
return "malformed packet" return "malformed packet"
end end
if reason > 127 then if reason > 127 then
self.is_connecting = false self.is_connecting = false
self.is_connected = false self.is_connected = false
self.conn:close() self.conn:close()
self.conn = nil self.conn = nil
return "connection closed by server" return "connection closed by server"
elseif reason ~= 0 then elseif reason ~= 0 then
self:disconnect(0x81) self:disconnect(0x81)
return "malformed packet" return "malformed packet"
end end
self.is_connecting = false self.is_connecting = false
self.is_connected = true self.is_connected = true
elseif ptype == 0x40 then -- PUBACK elseif ptype == 0x40 then -- PUBACK
-- TODO -- TODO
elseif ptype == 0xD0 then -- PINGRESP elseif ptype == 0xD0 then -- PINGRESP
-- TODO -- TODO
elseif ptype == 0xE0 then -- DISCONNECT elseif ptype == 0xE0 then -- DISCONNECT
-- TODO -- TODO
end end
return nil return nil
end end
function MqttClient:connect (username, password) function MqttClient:connect (username, password)
if self.is_connecting or self.is_connected then if self.is_connecting or self.is_connected then
return nil return nil
end end
local length = 15 local length = 15
local flags = 1 local flags = 1
if username ~= nil then if username ~= nil then
length = length + 2 + #username length = length + 2 + #username
flags = flags | 0x80 flags = flags | 0x80
end end
if password ~= nil then if password ~= nil then
length = length + 2 + #password length = length + 2 + #password
flags = flags | 0x40 flags = flags | 0x40
end end
if length > 127 then if length > 127 then
return "packet size exceeds current implementation capabilities" return "packet size exceeds current implementation capabilities"
end end
local data = string.pack("> B B s2 B B I2 B s2", 0x10, length, "MQTT", 5, flags, 0, 0, "") local data = string.pack("> B B s2 B B I2 B s2", 0x10, length, "MQTT", 5, flags, 0, 0, "")
if username ~= nil then if username ~= nil then
data = data .. string.pack("> s2", username) data = data .. string.pack("> s2", username)
end end
if password ~= nil then if password ~= nil then
data = data .. string.pack("> s2", password) data = data .. string.pack("> s2", password)
end end
local _, err = self.conn:write(data) local _, err = self.conn:write(data)
if err ~= nil then if err ~= nil then
return err return err
end end
self.is_connecting = true self.is_connecting = true
return nil return nil
end end
function MqttClient:disconnect (reason) function MqttClient:disconnect (reason)
if not (self.is_connecting or self.is_connected) then if not (self.is_connecting or self.is_connected) then
return nil return nil
end end
if reason == nil then if reason == nil then
reason = 0 reason = 0
end end
local data = string.pack("> B B B", 0xE0, 1, reason) local data = string.pack("> B B B", 0xE0, 1, reason)
local _, err = self.conn:write(data) local _, err = self.conn:write(data)
if err ~= nil then if err ~= nil then
return err return err
end end
self.is_connecting = false self.is_connecting = false
self.is_connected = false self.is_connected = false
self.conn:close() self.conn:close()
self.conn = nil self.conn = nil
return nil return nil
end end
return mqtt return mqtt