Skip to content

Commit b89d28f

Browse files
committed
adding MacAddress Data Type
1 parent 5e589bd commit b89d28f

File tree

3 files changed

+80
-7
lines changed

3 files changed

+80
-7
lines changed

Sources/DataDecoder/DataDecoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public extension DataDecoder {
214214
///
215215
/// - Parameter fromLittleEndian: If MAC is encoded as Little Endian
216216
/// - Returns: String Representation of the MAC Address
217-
public mutating func decodeMACAddress(fromLittleEndian: Bool = false) -> String {
217+
public mutating func decodeMACAddress(fromLittleEndian: Bool = false) -> MACAddress {
218218
let val0 = decode.scanValue(index: &index, type: UInt8.self) ?? 0
219219
let val1 = decode.scanValue(index: &index, type: UInt8.self) ?? 0
220220
let val2 = decode.scanValue(index: &index, type: UInt8.self) ?? 0
@@ -230,7 +230,7 @@ public extension DataDecoder {
230230
retVal = String(format: "%02X:%02X:%02X:%02X:%02X:%02X", val0, val1, val2, val3, val4, val5)
231231
}
232232

233-
return retVal
233+
return MACAddress(string: retVal)
234234
}
235235

236236
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// MACAddress.swift
3+
// DataDecoder
4+
//
5+
// Created by Kevin Hoogheem on 4/1/17.
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
// THE SOFTWARE.
24+
25+
import Foundation
26+
27+
28+
public struct MACAddress {
29+
30+
fileprivate(set) public var stringValue: String
31+
32+
public var dataValue: Data {
33+
let substring = stringValue.replacingOccurrences(of: ":", with: "")
34+
//let da = Data(bytes: [Character](substring.characters).byteArray)
35+
36+
return substring.data(using: .utf8)!
37+
}
38+
39+
public init(string: String) {
40+
self.stringValue = string.uppercased()
41+
}
42+
43+
public init(data: Data) {
44+
45+
self.stringValue = "00:00:00:00:00:00"
46+
47+
if data.count == 6 {
48+
self.stringValue = String(format: "%02X:%02X:%02X:%02X:%02X:%02X",
49+
data[0],
50+
data[1],
51+
data[2],
52+
data[3],
53+
data[4],
54+
data[5]).uppercased()
55+
}
56+
}
57+
}
58+
59+
extension MACAddress: Equatable {
60+
61+
public static func ==(lhs: MACAddress, rhs: MACAddress) -> Bool {
62+
return (lhs.stringValue == rhs.stringValue)
63+
}
64+
}
65+
66+
extension MACAddress: Hashable {
67+
68+
public var hashValue: Int {
69+
return stringValue.hashValue
70+
}
71+
}
72+
73+

Tests/DataDecoderTests/DataDecoderTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class DataDecoderTests: XCTestCase {
77
// This is an example of a functional test case.
88
// Use XCTAssert and related functions to verify your tests produce the correct results.
99

10-
let sensorData: Data = Data([ 0x02, 0xFE, 0xFF, 0xEF, 0xBE, 0xAD, 0xDE, 0xA5])
10+
let sensorData: Data = Data([0x02, 0xFE, 0xFF, 0xEF, 0xBE, 0xAD, 0xDE, 0xA5])
1111

1212
let DEADBEEF: UInt32 = 3735928559
1313

@@ -41,7 +41,7 @@ class DataDecoderTests: XCTestCase {
4141
}
4242

4343
func testUInt24() {
44-
let ipData: Data = Data([ 0xFF, 0xFF, 0xFF])
44+
let ipData: Data = Data([0xFF, 0xFF, 0xFF])
4545

4646
var decoder = DataDecoder(ipData)
4747

@@ -53,7 +53,7 @@ class DataDecoderTests: XCTestCase {
5353
}
5454

5555
func testUInt48() {
56-
let ipData: Data = Data([ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
56+
let ipData: Data = Data([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
5757

5858
var decoder = DataDecoder(ipData)
5959

@@ -77,14 +77,14 @@ class DataDecoderTests: XCTestCase {
7777
}
7878
}
7979

80-
func testMACADdress() {
80+
func testMACAddress() {
8181
let ipData: Data = Data([ 0x00, 0x50, 0xC2, 0x34, 0xF7, 0x11])
8282

8383
var decoder = DataDecoder(ipData)
8484

8585
let macaddress = decoder.decodeMACAddress()
8686

87-
if macaddress != "00:50:C2:34:F7:11" {
87+
if macaddress.stringValue != "00:50:C2:34:F7:11" {
8888
XCTFail()
8989
}
9090

0 commit comments

Comments
 (0)