Skip to content

Commit ec65e15

Browse files
committed
protocol5: added making CONNACK packet type
1 parent ad336c3 commit ec65e15

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

mqtt/protocol5.lua

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,25 @@ local function make_packet_connect(args)
476476
return combine(header, variable_header, payload)
477477
end
478478

479+
-- Create CONNACK packet, DOC: 3.2 CONNACK – Connect acknowledgement
480+
local function make_packet_connack(args)
481+
-- check args
482+
assert(type(args.sp) == "boolean", "expecting .sp to be a boolean with Session Present flag")
483+
assert(type(args.rc) == "number", "expecting .rc to be a number with Connect Reason Code")
484+
-- DOC: 3.2.2 CONNACK Variable Header
485+
local props = make_properties(packet_type.CONNACK, args)
486+
local variable_header = combine(
487+
make_uint8(args.sp and 1 or 0), -- DOC: 3.2.2.1.1 Session Present
488+
make_uint8(args.rc), -- DOC: 3.2.2.2 Connect Reason Code
489+
props -- DOC: 3.2.2.3 CONNACK Properties
490+
)
491+
-- DOC: 3.2.3 CONNACK Payload
492+
-- DOC: The CONNACK packet has no Payload.
493+
-- DOC: 3.2.1 CONNACK Fixed Header
494+
local header = make_header(packet_type.CONNACK, 0, variable_header:len())
495+
return combine(header, variable_header)
496+
end
497+
479498
-- Create PUBLISH packet, DOC: 3.3 PUBLISH – Publish message
480499
local function make_packet_publish(args)
481500
-- check args
@@ -717,7 +736,8 @@ function protocol5.make_packet(args)
717736
local ptype = args.type
718737
if ptype == packet_type.CONNECT then -- 1
719738
return make_packet_connect(args)
720-
-- TODO: CONNACK
739+
elseif ptype == packet_type.CONNACK then -- 3
740+
return make_packet_connack(args)
721741
elseif ptype == packet_type.PUBLISH then -- 3
722742
return make_packet_publish(args)
723743
elseif ptype == packet_type.PUBACK then -- 4

tests/spec/protocol5-make.lua

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,89 @@ describe("MQTT v5.0 protocol: making packets: CONNECT[1]", function()
191191
end)
192192
end)
193193

194+
describe("MQTT v5.0 protocol: making packets: CONNACK[2]", function()
195+
local tools = require("mqtt.tools")
196+
local extract_hex = require("mqtt.tools").extract_hex
197+
local protocol = require("mqtt.protocol")
198+
local protocol5 = require("mqtt.protocol5")
199+
200+
it("CONNACK with minimum params", function()
201+
assert.are.equal(
202+
extract_hex[[
203+
20 -- packet type == 2 (CONNACK), flags == 0
204+
03 -- length == 3 bytes
205+
206+
00 -- Connect Acknowledge Flags, sp=false
207+
00 -- Connect Reason Code == 0
208+
00 -- no props
209+
]],
210+
tools.hex(tostring(protocol5.make_packet{
211+
type = protocol.packet_type.CONNACK,
212+
sp = false, rc = 0,
213+
}))
214+
)
215+
end)
216+
217+
it("CONNACK with flags, rc, and full properties", function()
218+
assert.are.equal(
219+
extract_hex[[
220+
20 -- packet type == 2 (CONNACK), flags == 0
221+
75 -- variable length == 117 bytes
222+
223+
01 -- Connect Acknowledge Flags, sp=true
224+
82 -- Connect Reason Code == 0x82
225+
72 -- properties length
226+
227+
11 00000E10 -- property 0x11 == 3600, -- DOC: 3.2.2.3.2 Session Expiry Interval
228+
12 0005 736C617665 -- property 0x12 == "slave", -- DOC: 3.2.2.3.7 Assigned Client Identifier
229+
13 0078 -- property 0x13 == 120, -- DOC: 3.2.2.3.14 Server Keep Alive
230+
15 0005 6775657373 -- property 0x15 == "guess", -- DOC: 3.2.2.3.17 Authentication Method
231+
16 0002 3130 -- property 0x16 == "10", -- DOC: 3.2.2.3.18 Authentication Data
232+
1A 0005 686572652F -- property 0x1A == "here/", -- DOC: 3.2.2.3.15 Response Information
233+
1C 000D 736565202F6465762F6E756C6C -- property 0x1C == "see /dev/null", -- DOC: 3.2.2.3.16 Server Reference
234+
1F 0007 70726F63656564 -- property 0x1F == "proceed", -- DOC: 3.2.2.3.9 Reason String
235+
21 1234 -- property 0x21 == 0x1234, -- DOC: 3.2.2.3.3 Receive Maximum
236+
22 4321 -- property 0x22 == 0x4321, -- DOC: 3.2.2.3.8 Topic Alias Maximum
237+
24 01 -- property 0x24 == 1, -- DOC: 3.2.2.3.4 Maximum QoS
238+
25 01 -- property 0x25 == 1, -- DOC: 3.2.2.3.5 Retain Available
239+
27 00004567 -- property 0x27 == 0x4567, -- DOC: 3.2.2.3.6 Maximum Packet Size
240+
28 01 -- property 0x28 == 1, -- DOC: 3.2.2.3.11 Wildcard Subscription Available
241+
29 00 -- property 0x29 == 0, -- DOC: 3.2.2.3.12 Subscription Identifiers Available
242+
2A 01 -- property 0x2A == 1, -- DOC: 3.2.2.3.13 Shared Subscription Available
243+
26 0005 68656C6C6F 0005 776F726C64 -- property 0x26 (user) == ("hello", "world") -- DOC: 3.2.2.3.10 User Property
244+
26 0005 68656C6C6F 0005 616761696E -- property 0x26 (user) == ("hello", "again") -- DOC: 3.2.2.3.10 User Property
245+
]],
246+
tools.hex(tostring(protocol5.make_packet{
247+
type = protocol.packet_type.CONNACK,
248+
sp = true, rc = 0x82,
249+
properties={
250+
session_expiry_interval = 3600,
251+
receive_maximum = 0x1234,
252+
maximum_qos = 1,
253+
retain_available = 1,
254+
maximum_packet_size = 0x4567,
255+
assigned_client_identifier = "slave",
256+
topic_alias_maximum = 0x4321,
257+
reason_string = "proceed",
258+
wildcard_subscription_available = 1,
259+
subscription_identifiers_available = 0,
260+
shared_subscription_available = 1,
261+
server_keep_alive = 120,
262+
response_information = "here/",
263+
server_reference = "see /dev/null",
264+
authentication_method = "guess",
265+
authentication_data = "10",
266+
},
267+
user_properties={
268+
hello = "again", -- NOTE: that key+value pair is equivalent of {"hello", "again"} below, thus that pair will be skipped
269+
{"hello", "world"},
270+
{"hello", "again"},
271+
},
272+
}))
273+
)
274+
end)
275+
end)
276+
194277
describe("MQTT v5.0 protocol: making packets: PUBLISH[3]", function()
195278
local tools = require("mqtt.tools")
196279
local extract_hex = require("mqtt.tools").extract_hex

0 commit comments

Comments
 (0)