Skip to content

Commit 1d6a297

Browse files
committed
Added support for encoding QoS in MQTT-SN packets
1 parent d8f91fa commit 1d6a297

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lib/mqtt/sn/packet.rb

+8
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ def encode_body
104104
def encode_flags
105105
flags = 0x00
106106
flags += 0x80 if duplicate
107+
case qos
108+
when -1
109+
flags += 0x60
110+
when 1
111+
flags += 0x20
112+
when 2
113+
flags += 0x40
114+
end
107115
flags += 0x10 if retain
108116
flags += 0x08 if request_will
109117
flags += 0x04 if clean_session

spec/mqtt_sn_packet_spec.rb

+72-1
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,45 @@
397397
end
398398

399399
describe "when serialising a packet with a short topic id type" do
400-
it "should output the correct bytes for a publish packet" do
400+
it "should output the correct bytes for a publish packet of QoS -1" do
401401
packet = MQTT::SN::Packet::Publish.new(
402+
:qos => -1,
403+
:topic_id => 'tt',
404+
:topic_id_type => :short,
405+
:data => "Hello World"
406+
)
407+
expect(packet.to_s).to eq("\x12\x0C\x62tt\x00\x00Hello World")
408+
end
409+
410+
it "should output the correct bytes for a publish packet of QoS 0" do
411+
packet = MQTT::SN::Packet::Publish.new(
412+
:qos => 0,
402413
:topic_id => 'tt',
403414
:topic_id_type => :short,
404415
:data => "Hello World"
405416
)
406417
expect(packet.to_s).to eq("\x12\x0C\x02tt\x00\x00Hello World")
407418
end
419+
420+
it "should output the correct bytes for a publish packet of QoS 1" do
421+
packet = MQTT::SN::Packet::Publish.new(
422+
:qos => 1,
423+
:topic_id => 'tt',
424+
:topic_id_type => :short,
425+
:data => "Hello World"
426+
)
427+
expect(packet.to_s).to eq("\x12\x0C\x22tt\x00\x00Hello World")
428+
end
429+
430+
it "should output the correct bytes for a publish packet of QoS 2" do
431+
packet = MQTT::SN::Packet::Publish.new(
432+
:qos => 2,
433+
:topic_id => 'tt',
434+
:topic_id_type => :short,
435+
:data => "Hello World"
436+
)
437+
expect(packet.to_s).to eq("\x12\x0C\x42tt\x00\x00Hello World")
438+
end
408439
end
409440

410441
describe "when parsing a Publish packet with a normal topic id" do
@@ -486,6 +517,46 @@
486517
expect(@packet.data).to eq("Hello World")
487518
end
488519
end
520+
521+
describe "when parsing a Publish packet with a short topic id and QoS -1" do
522+
before(:each) do
523+
@packet = MQTT::SN::Packet.parse(
524+
"\x12\x0C\x62tt\x00\x00Hello World"
525+
)
526+
end
527+
528+
it "should correctly create the right type of packet object" do
529+
expect(@packet.class).to eq(MQTT::SN::Packet::Publish)
530+
end
531+
532+
it "should set the QOS of the packet correctly" do
533+
expect(@packet.qos).to be === -1
534+
end
535+
536+
it "should set the QOS of the packet correctly" do
537+
expect(@packet.duplicate).to be === false
538+
end
539+
540+
it "should set the retain flag of the packet correctly" do
541+
expect(@packet.retain).to be === false
542+
end
543+
544+
it "should set the topic id type of the packet correctly" do
545+
expect(@packet.topic_id_type).to be === :short
546+
end
547+
548+
it "should set the topic id of the packet correctly" do
549+
expect(@packet.topic_id).to be === 'tt'
550+
end
551+
552+
it "should set the message id of the packet correctly" do
553+
expect(@packet.id).to be === 0x0000
554+
end
555+
556+
it "should set the topic name of the packet correctly" do
557+
expect(@packet.data).to eq("Hello World")
558+
end
559+
end
489560
end
490561

491562

0 commit comments

Comments
 (0)