Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retain is bit 0 of the flags, not bit 3 #24

Merged
merged 2 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SwiftMQTT/SwiftMQTT/Models/MQTTPublishPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MQTTPublishPacket: MQTTPacket {
class func fixedHeaderFlags(for message: MQTTPubMsg) -> UInt8 {
var flags = UInt8(0)
if message.retain {
flags |= 0x08
flags |= 0x01
}
flags |= message.QoS.rawValue << 1
return flags
Expand Down
36 changes: 36 additions & 0 deletions SwiftMQTT/SwiftMQTTTests/SwiftMQTTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,42 @@ class SwiftMQTTTests: XCTestCase, MQTTSessionDelegate {
}
}

func testPublishPacketHeader() {
let retainPubMsg = MQTTPubMsg(topic: "test", payload: "Test".data(using: .utf8)!, retain: true, QoS: .atMostOnce)

let retainPubPacket = MQTTPublishPacket(messageID: 1, message: retainPubMsg)

let retainFlag = retainPubPacket.header.flags & 0x01

XCTAssert(retainFlag == 1, "Header retention bit is not set")

let qos0 = (retainPubPacket.header.flags & 0x06) >> 1

XCTAssert(qos0 == 0, "QoS not 0 for .atMostOnce")

let nonretainPubMsg = MQTTPubMsg(topic:"test", payload:"Test".data(using: .utf8)!, retain:false, QoS: .atLeastOnce)

let nonretainPubPacket = MQTTPublishPacket(messageID: 2, message: nonretainPubMsg)

let nonretainFlag = nonretainPubPacket.header.flags & 0x01

XCTAssert(nonretainFlag == 0, "Header retenion bit should not be set")

let qos1 = (nonretainPubPacket.header.flags & 0x06) >> 1

XCTAssert(qos1 == 1, "QoS not 1 for .atLeastOnce")

let qos2PubMsg = MQTTPubMsg(topic:"test", payload:"Test".data(using: .utf8)!, retain:false, QoS: .exactlyOnce)

let qos2PubPacket = MQTTPublishPacket(messageID: 3, message: qos2PubMsg)


let qos2 = (qos2PubPacket.header.flags & 0x06) >> 1

XCTAssert(qos2 == 2, "QoS not 2 for .exactlyOnce")

}

func testUnSubscribe() {
let expectation = self.expectation(description: "unSubscribe")
mqttSession.unSubscribe(from: ["/hey/cool", "/no/ok"]) { (succeeded, error) -> Void in
Expand Down