Skip to content

Commit

Permalink
#35 added GAPManufacturerSpecificData
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos21 committed May 31, 2018
1 parent 3f9557f commit 37cc4d4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
59 changes: 59 additions & 0 deletions Sources/GenericAccessProfile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2955,6 +2955,65 @@ public struct GAPMeshBeacon: GAPData {

}

public struct GAPManufacturerSpecificData: GAPData {

public static let minLength = MemoryLayout<UInt16>.size

public static let dataType: GAPDataType = .manufacturerSpecificData

public let companyIdentifier: CompanyIdentifier

public private(set) var additionalData: [UInt8] = []

public init(companyIdentifier: CompanyIdentifier, additionalData: [UInt8] = []) {

self.companyIdentifier = companyIdentifier
self.additionalData = additionalData
}

public init?(data: Data) {

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

let companyIdentifier = CompanyIdentifier(rawValue: UInt16(littleEndian: UInt16(bytes: (data[0], data[1]))))
var additionalData = [UInt8]()

data.enumerated().forEach { (index, element) in
if index >= GAPManufacturerSpecificData.minLength {
additionalData.append(element)
}
}

self.init(companyIdentifier: companyIdentifier, additionalData: additionalData)
}

public var data: Data {

let bytes = UInt16(littleEndian: companyIdentifier.rawValue).bytes
let data = Data([bytes.0, bytes.1])

return additionalData.reduce(data, { $0.0 + [$0.1] })
}

}

extension GAPManufacturerSpecificData: Equatable {

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

return lhs.companyIdentifier == rhs.companyIdentifier && lhs.additionalData == rhs.additionalData
}
}

extension GAPManufacturerSpecificData: CustomStringConvertible {

public var description: String {

return companyIdentifier.description + additionalData.map { String($0) }.description
}
}

// MARK: - Coding

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

func testDataType() {
Expand Down Expand Up @@ -799,4 +800,29 @@ final class GAPTests: XCTestCase {
}

}

func testGAPManufacturerSpecificData() {

XCTAssertNil(GAPManufacturerSpecificData(data: Data([0x4f])))

do {
let data = Data([0x4f, 0x30])
let manufacturerData = GAPManufacturerSpecificData(data: data)!
XCTAssertEqual(MemoryLayout.size(ofValue: manufacturerData.companyIdentifier), 2)
XCTAssertEqual(manufacturerData.data, data)
XCTAssertEqual(manufacturerData.data.count, 2)
}

do {
let data = Data([0x4f, 0x30, 0x4f, 0x30, 0x4f])
let manufacturerData = GAPManufacturerSpecificData(data: data)!
XCTAssertEqual(MemoryLayout.size(ofValue: manufacturerData.companyIdentifier), 2)
XCTAssertEqual(manufacturerData.additionalData.count, 3)
XCTAssertEqual(manufacturerData.data, data)
XCTAssertEqual(manufacturerData.data.count, 5)
}

XCTAssertEqual(GAPManufacturerSpecificData(data: Data([0x4f, 0x45, 0xff])),
GAPManufacturerSpecificData(data: Data([0x4f, 0x45, 0xff])))
}
}

0 comments on commit 37cc4d4

Please sign in to comment.