Skip to content

Commit

Permalink
#35 added GAPMeshMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos21 committed May 30, 2018
1 parent 9fb159a commit f34770b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
52 changes: 52 additions & 0 deletions Sources/GenericAccessProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,58 @@ public enum GAPTransportDiscoveryDataFlag: UInt8, BitMaskOption {
]
}

/// Any advertisement using the Mesh Message AD Type shall be non-connectable and non-scannable undirected advertising events.
/// If a node receives a Mesh Message AD Type in a connectable advertisement or scannable advertising event, the message shall be ignored.
///
/// Note: Non-connectable advertisements are used since there is no need to include the Flags AD Type in the advertising packets, thereby enabling two additional octets to be allocated to the Network PDU (see [7]).
/// To lower the probability of packet collisions on all advertising channels, it is recommended to randomize the gap between consecutive packets within an Advertising Event (see [1]).
public struct GAPMeshMessage: GAPData {

public static let length = MemoryLayout<UInt16>.size

public static let dataType: GAPDataType = .meshMessage

public var message: UInt16

public init(message: UInt16) {

self.message = message
}

public init?(data: Data) {

guard data.count == type(of: self).length
else { return nil }

let message = UInt16(littleEndian: UInt16(bytes: (data[0], data[1])))

self.init(message: message)
}

public var data: Data {

let value = message.littleEndian

return Data([value.bytes.0, value.bytes.1])
}
}

extension GAPMeshMessage: Equatable {

public static func == (lhs: GAPMeshMessage, rhs: GAPMeshMessage) -> Bool {

return lhs.message == rhs.message
}
}

extension GAPMeshMessage: CustomStringConvertible {

public var description: String {

return message.description
}
}

// MARK: - Coding

public struct GAPDataElement {
Expand Down
18 changes: 17 additions & 1 deletion Tests/BluetoothTests/GAPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ final class GAPTests: XCTestCase {
("testGAPLESecureConnectionsConfirmation", testGAPLESecureConnectionsConfirmation),
("testGAPLESecureConnectionsRandom", testGAPLESecureConnectionsRandom),
("testGAPChannelMapUpdateIndication", testGAPChannelMapUpdateIndication),
("testGAPTransportDiscoveryData", testGAPTransportDiscoveryData)
("testGAPTransportDiscoveryData", testGAPTransportDiscoveryData),
("testGAPMeshMessage", testGAPMeshMessage)
]

func testDataType() {
Expand Down Expand Up @@ -753,4 +754,19 @@ final class GAPTests: XCTestCase {
XCTAssertEqual(GAPTransportDiscoveryData(data: data), GAPTransportDiscoveryData(data: data))
}
}

func testGAPMeshMessage() {

XCTAssertNil(GAPMeshMessage(data: Data([0x4f])))
XCTAssertNil(GAPMeshMessage(data: Data([0x4f, 0xf8, 0x30])))

do {
let data = Data([0xf8, 0x30])
let message = GAPMeshMessage(data: data)!

XCTAssertEqual(message.data, data)
XCTAssertEqual(message.data.count, GAPMeshMessage.length)
XCTAssertEqual(MemoryLayout.size(ofValue: message), GAPMeshMessage.length)
}
}
}

0 comments on commit f34770b

Please sign in to comment.