Skip to content

Commit

Permalink
protocol5: added making CONNACK packet type
Browse files Browse the repository at this point in the history
  • Loading branch information
xHasKx committed Nov 26, 2023
1 parent ad336c3 commit ec65e15
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mqtt/protocol5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,25 @@ local function make_packet_connect(args)
return combine(header, variable_header, payload)
end

-- Create CONNACK packet, DOC: 3.2 CONNACK – Connect acknowledgement
local function make_packet_connack(args)
-- check args
assert(type(args.sp) == "boolean", "expecting .sp to be a boolean with Session Present flag")
assert(type(args.rc) == "number", "expecting .rc to be a number with Connect Reason Code")
-- DOC: 3.2.2 CONNACK Variable Header
local props = make_properties(packet_type.CONNACK, args)
local variable_header = combine(
make_uint8(args.sp and 1 or 0), -- DOC: 3.2.2.1.1 Session Present
make_uint8(args.rc), -- DOC: 3.2.2.2 Connect Reason Code
props -- DOC: 3.2.2.3 CONNACK Properties
)
-- DOC: 3.2.3 CONNACK Payload
-- DOC: The CONNACK packet has no Payload.
-- DOC: 3.2.1 CONNACK Fixed Header
local header = make_header(packet_type.CONNACK, 0, variable_header:len())
return combine(header, variable_header)
end

-- Create PUBLISH packet, DOC: 3.3 PUBLISH – Publish message
local function make_packet_publish(args)
-- check args
Expand Down Expand Up @@ -717,7 +736,8 @@ function protocol5.make_packet(args)
local ptype = args.type
if ptype == packet_type.CONNECT then -- 1
return make_packet_connect(args)
-- TODO: CONNACK
elseif ptype == packet_type.CONNACK then -- 3
return make_packet_connack(args)
elseif ptype == packet_type.PUBLISH then -- 3
return make_packet_publish(args)
elseif ptype == packet_type.PUBACK then -- 4
Expand Down
83 changes: 83 additions & 0 deletions tests/spec/protocol5-make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,89 @@ describe("MQTT v5.0 protocol: making packets: CONNECT[1]", function()
end)
end)

describe("MQTT v5.0 protocol: making packets: CONNACK[2]", function()
local tools = require("mqtt.tools")
local extract_hex = require("mqtt.tools").extract_hex
local protocol = require("mqtt.protocol")
local protocol5 = require("mqtt.protocol5")

it("CONNACK with minimum params", function()
assert.are.equal(
extract_hex[[
20 -- packet type == 2 (CONNACK), flags == 0
03 -- length == 3 bytes
00 -- Connect Acknowledge Flags, sp=false
00 -- Connect Reason Code == 0
00 -- no props
]],
tools.hex(tostring(protocol5.make_packet{
type = protocol.packet_type.CONNACK,
sp = false, rc = 0,
}))
)
end)

it("CONNACK with flags, rc, and full properties", function()
assert.are.equal(
extract_hex[[
20 -- packet type == 2 (CONNACK), flags == 0
75 -- variable length == 117 bytes
01 -- Connect Acknowledge Flags, sp=true
82 -- Connect Reason Code == 0x82
72 -- properties length
11 00000E10 -- property 0x11 == 3600, -- DOC: 3.2.2.3.2 Session Expiry Interval
12 0005 736C617665 -- property 0x12 == "slave", -- DOC: 3.2.2.3.7 Assigned Client Identifier
13 0078 -- property 0x13 == 120, -- DOC: 3.2.2.3.14 Server Keep Alive
15 0005 6775657373 -- property 0x15 == "guess", -- DOC: 3.2.2.3.17 Authentication Method
16 0002 3130 -- property 0x16 == "10", -- DOC: 3.2.2.3.18 Authentication Data
1A 0005 686572652F -- property 0x1A == "here/", -- DOC: 3.2.2.3.15 Response Information
1C 000D 736565202F6465762F6E756C6C -- property 0x1C == "see /dev/null", -- DOC: 3.2.2.3.16 Server Reference
1F 0007 70726F63656564 -- property 0x1F == "proceed", -- DOC: 3.2.2.3.9 Reason String
21 1234 -- property 0x21 == 0x1234, -- DOC: 3.2.2.3.3 Receive Maximum
22 4321 -- property 0x22 == 0x4321, -- DOC: 3.2.2.3.8 Topic Alias Maximum
24 01 -- property 0x24 == 1, -- DOC: 3.2.2.3.4 Maximum QoS
25 01 -- property 0x25 == 1, -- DOC: 3.2.2.3.5 Retain Available
27 00004567 -- property 0x27 == 0x4567, -- DOC: 3.2.2.3.6 Maximum Packet Size
28 01 -- property 0x28 == 1, -- DOC: 3.2.2.3.11 Wildcard Subscription Available
29 00 -- property 0x29 == 0, -- DOC: 3.2.2.3.12 Subscription Identifiers Available
2A 01 -- property 0x2A == 1, -- DOC: 3.2.2.3.13 Shared Subscription Available
26 0005 68656C6C6F 0005 776F726C64 -- property 0x26 (user) == ("hello", "world") -- DOC: 3.2.2.3.10 User Property
26 0005 68656C6C6F 0005 616761696E -- property 0x26 (user) == ("hello", "again") -- DOC: 3.2.2.3.10 User Property
]],
tools.hex(tostring(protocol5.make_packet{
type = protocol.packet_type.CONNACK,
sp = true, rc = 0x82,
properties={
session_expiry_interval = 3600,
receive_maximum = 0x1234,
maximum_qos = 1,
retain_available = 1,
maximum_packet_size = 0x4567,
assigned_client_identifier = "slave",
topic_alias_maximum = 0x4321,
reason_string = "proceed",
wildcard_subscription_available = 1,
subscription_identifiers_available = 0,
shared_subscription_available = 1,
server_keep_alive = 120,
response_information = "here/",
server_reference = "see /dev/null",
authentication_method = "guess",
authentication_data = "10",
},
user_properties={
hello = "again", -- NOTE: that key+value pair is equivalent of {"hello", "again"} below, thus that pair will be skipped
{"hello", "world"},
{"hello", "again"},
},
}))
)
end)
end)

describe("MQTT v5.0 protocol: making packets: PUBLISH[3]", function()
local tools = require("mqtt.tools")
local extract_hex = require("mqtt.tools").extract_hex
Expand Down

0 comments on commit ec65e15

Please sign in to comment.