Skip to content

Commit

Permalink
Merge pull request #354 from ps2/dev
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
ps2 authored Jul 7, 2017
2 parents 9950051 + c6bcace commit 85afbb3
Show file tree
Hide file tree
Showing 68 changed files with 344 additions and 691 deletions.
2 changes: 1 addition & 1 deletion Crypto/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.1.1</string>
<string>1.2.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
13 changes: 6 additions & 7 deletions MinimedKit/BasalSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,25 @@ public struct BasalSchedule {
public let entries: [BasalScheduleEntry]

public init(data: Data) {
let beginPattern: [UInt8] = [0, 0, 0]
let endPattern: [UInt8] = [0, 0, 0x3F]
let beginPattern = Data(bytes: [0, 0, 0])
let endPattern = Data(bytes: [0, 0, 0x3F])
var acc = [BasalScheduleEntry]()

for tuple in sequence(first: (index: 0, offset: 0), next: { (index: $0.index + 1, $0.offset + 3) }) {
let beginOfRange = tuple.offset
let endOfRange = beginOfRange+2
let endOfRange = beginOfRange + 3

if endOfRange >= data.count-1 {
guard endOfRange < data.count else {
break
}

let section = Array(data[beginOfRange...endOfRange])

let section = data.subdata(in: beginOfRange..<endOfRange)
// sanity check
if (section == beginPattern || section == endPattern) {
break
}

let rate = Double(section[0..<2].withUnsafeBytes { $0.load(as: UInt16.self) }) / 40.0
let rate = Double(section.subdata(in: 0..<2).to(UInt16.self)) / 40.0
let offsetMinutes = Double(section[2]) * 30

let newBasalScheduleEntry = BasalScheduleEntry(
Expand Down
61 changes: 28 additions & 33 deletions MinimedKit/Extensions/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,42 @@ extension Data {
}
}

// String conversion methods, adapted from https://stackoverflow.com/questions/40276322/hex-binary-string-conversion-in-swift/40278391#40278391
extension Data {
init?(hexadecimalString: String) {
guard let chars = hexadecimalString.cString(using: String.Encoding.utf8) else {
return nil
self.init(capacity: hexadecimalString.utf16.count / 2)

// Convert 0 ... 9, a ... f, A ...F to their decimal value,
// return nil for all other input characters
func decodeNibble(u: UInt16) -> UInt8? {
switch u {
case 0x30 ... 0x39: // '0'-'9'
return UInt8(u - 0x30)
case 0x41 ... 0x46: // 'A'-'F'
return UInt8(u - 0x41 + 10) // 10 since 'A' is 10, not 0
case 0x61 ... 0x66: // 'a'-'f'
return UInt8(u - 0x61 + 10) // 10 since 'a' is 10, not 0
default:
return nil
}
}

self.init(capacity: chars.count / 2)

for i in 0..<chars.count / 2 {
var num: UInt8 = 0
var multi: UInt8 = 16

for j in 0..<2 {
let c = chars[i * 2 + j]
var offset: UInt8

switch c {
case 48...57: // '0'-'9'
offset = 48
case 65...70: // 'A'-'F'
offset = 65 - 10 // 10 since 'A' is 10, not 0
case 97...102: // 'a'-'f'
offset = 97 - 10 // 10 since 'a' is 10, not 0
default:
return nil
}

num += (UInt8(c) - offset) * multi
multi = 1
var even = true
var byte: UInt8 = 0
for c in hexadecimalString.utf16 {
guard let val = decodeNibble(u: c) else { return nil }
if even {
byte = val << 4
} else {
byte += val
self.append(byte)
}
append(num)
even = !even
}
guard even else { return nil }
}

var hexadecimalString: String {
let string = NSMutableString(capacity: count * 2)

for byte in self {
string.appendFormat("%02x", byte)
}

return string as String
return map { String(format: "%02hhx", $0) }.joined()
}
}
18 changes: 9 additions & 9 deletions MinimedKit/Extensions/NSDateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension DateComponents {
month = Int(mySentryBytes[4] & 0b00001111)
day = Int(mySentryBytes[5] & 0b00011111)

calendar = Calendar(identifier: Calendar.Identifier.gregorian)
calendar = Calendar(identifier: .gregorian)
}

init(pumpEventData: Data, offset: Int, length: Int = 5) {
Expand All @@ -35,29 +35,29 @@ extension DateComponents {
minute = Int(pumpEventBytes[1] & 0b00111111)
hour = Int(pumpEventBytes[2] & 0b00011111)
day = Int(pumpEventBytes[3] & 0b00011111)
month = Int((pumpEventBytes[0] & 0b11000000) >> 4 +
(pumpEventBytes[1] & 0b11000000) >> 6)
month = Int((pumpEventBytes[0] & 0b11000000) >> 4) +
Int((pumpEventBytes[1] & 0b11000000) >> 6)
year = Int(pumpEventBytes[4] & 0b01111111) + 2000
} else {
day = Int(pumpEventBytes[0] & 0b00011111)
month = Int((pumpEventBytes[0] & 0b11100000) >> 4 +
(pumpEventBytes[1] & 0b10000000) >> 7)
month = Int((pumpEventBytes[0] & 0b11100000) >> 4) +
Int((pumpEventBytes[1] & 0b10000000) >> 7)
year = Int(pumpEventBytes[1] & 0b01111111) + 2000
}

calendar = Calendar(identifier: Calendar.Identifier.gregorian)
calendar = Calendar(identifier: .gregorian)
}

init(glucoseEventBytes: Data) {
self.init()

year = Int(glucoseEventBytes[3] & 0b01111111) + 2000
month = Int((glucoseEventBytes[0] & 0b11000000) >> 4 +
(glucoseEventBytes[1] & 0b11000000) >> 6)
month = Int((glucoseEventBytes[0] & 0b11000000) >> 4) +
Int((glucoseEventBytes[1] & 0b11000000) >> 6)
day = Int(glucoseEventBytes[2] & 0b00011111)
hour = Int(glucoseEventBytes[0] & 0b00011111)
minute = Int(glucoseEventBytes[1] & 0b00111111)

calendar = Calendar(identifier: Calendar.Identifier.gregorian)
calendar = Calendar(identifier: .gregorian)
}
}
2 changes: 1 addition & 1 deletion MinimedKit/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.1.1</string>
<string>1.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/Messages/CarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CarelinkShortMessageBody: MessageBody {
let data: Data

public convenience init() {
self.init(rxData: Data(hexadecimalString: "00")!)!
self.init(rxData: Data(repeating: 0, count: 1))!
}

public required init?(rxData: Data) {
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/Messages/MySentryAckMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public struct MySentryAckMessageBody: MessageBody {

sequence = rxData[0]
mySentryID = rxData.subdata(in: 1..<4)
responseMessageTypes = rxData[5...8].flatMap({ MessageType(rawValue: $0) })
responseMessageTypes = rxData[5..<9].flatMap({ MessageType(rawValue: $0) })
}

public var txData: Data {
var buffer = type(of: self).emptyBuffer

buffer[0] = sequence
buffer.replaceSubrange(1...3, with: mySentryID[0...2])
buffer.replaceSubrange(1..<4, with: mySentryID[0..<3])

buffer.replaceSubrange(5..<5 + responseMessageTypes.count, with: responseMessageTypes.map({ $0.rawValue }))

Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/PumpEvents/UnabsorbedInsulinPumpEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct UnabsorbedInsulinPumpEvent: PumpEvent {

let numRecords = (d(1) - 2) / 3

for idx in 0...(numRecords-1) {
for idx in 0..<numRecords {
let record = Record(
amount: Double(d(2 + idx * 3)) / 40,
age: d(3 + idx * 3) + ((d(4 + idx * 3) & 0b110000) << 4))
Expand Down
9 changes: 3 additions & 6 deletions MinimedKit/PumpMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,11 @@ public struct PumpMessage : CustomStringConvertible {
var buffer = [UInt8]()

buffer.append(packetType.rawValue)
buffer += address[0...2]
buffer += address[0..<3]
buffer.append(messageType.rawValue)
buffer.append(contentsOf: messageBody.txData)

var data = Data(bytes: buffer)

data.append(messageBody.txData)

return data
return Data(bytes: buffer)
}

public var description: String {
Expand Down
14 changes: 5 additions & 9 deletions MinimedKitTests/BasalScheduleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import XCTest
@testable import MinimedKit

class BasalScheduleTests: XCTestCase {

override func setUp() {
super.setUp()
}


func testBasicConversion() {
let sampleDataString = "06000052000178050202000304000402000504000602000704000802000904000a02000b04000c02000d02000e02000f040010020011040012020013040014020015040016020017040018020019000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

Expand All @@ -28,20 +24,20 @@ class BasalScheduleTests: XCTestCase {
// Test each element
XCTAssertEqual(basalSchedule[0].index, 0)
XCTAssertEqual(basalSchedule[0].timeOffset, TimeInterval(minutes: 0))
XCTAssertEqualWithAccuracy(basalSchedule[0].rate, 0.15, accuracy: 0.0001)
XCTAssertEqualWithAccuracy(basalSchedule[0].rate, 0.15, accuracy: .ulpOfOne)

XCTAssertEqual(basalSchedule[1].index, 1)
XCTAssertEqual(basalSchedule[1].timeOffset, TimeInterval(minutes: 30))
XCTAssertEqualWithAccuracy(basalSchedule[1].rate, 2.05, accuracy: 0.0001)
XCTAssertEqualWithAccuracy(basalSchedule[1].rate, 2.05, accuracy: .ulpOfOne)

// Tests parsing rates that take two bytes to encode
XCTAssertEqual(basalSchedule[2].index, 2)
XCTAssertEqual(basalSchedule[2].timeOffset, TimeInterval(minutes: 60))
XCTAssertEqualWithAccuracy(basalSchedule[2].rate, 35.00, accuracy: 0.0001)
XCTAssertEqualWithAccuracy(basalSchedule[2].rate, 35.00, accuracy: .ulpOfOne)

// Tests parsing entry on the second page
XCTAssertEqual(basalSchedule[25].index, 25)
XCTAssertEqual(basalSchedule[25].timeOffset, TimeInterval(minutes: 750))
XCTAssertEqualWithAccuracy(basalSchedule[25].rate, 0.05, accuracy: 0.0001)
XCTAssertEqualWithAccuracy(basalSchedule[25].rate, 0.05, accuracy: .ulpOfOne)
}
}
10 changes: 0 additions & 10 deletions MinimedKitTests/CRC16Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ import XCTest

class CRC16Tests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testComputeCRC16() {
let input = Data(hexadecimalString: "5be409a20a1510325000784b502800a400002400a8965c0b404fc038cbd008d5d0010080008000240009a24a15107b0500800c1510180a000ade19a32c15105bde2ba30c1510325000b44b5024006c0000200070965c0b4c78c03482c040c8c001007000700020002ba34c15100a0c22932d75903f2122938d7510c527ad5b0006900f15101a5000b44b500000380000000038965c0e70a1c04c19d03423d04069d00100380038000c0006904f15107b060080101510200e005b0034ab1015100d5000784b500000280000000028965c113858c070f8c04c70d0347ad040c0d00100280028001c0034ab5015100ab005863175903f360586117510c527ad5bb01486111510005100784b50940000000038005c965c14281fc0386fc0700fd04c87d03491d040d7d001005c005c00380014865115105b002291121510285000784b500000840000000084965c145c48c02866c038b6c07056d04cced034d8d0010084008400480022915215107b0700801315102610002100038414151003000000360785341510064a097e009e54b5100c4a03a11415107b0704a11415102610007b0704a11415102610007b0710a1141510261000030003000306a11415100ae937a23475103f1d37a2347510c527ad5be91ea3141510165000784b502c00480000140060965c0e848cc05cd2c028f0c03840d001006000600014001ea35415107b0800801515102a13000a5621ba3515905b5623ba151510005100b455505800000000340024965c116053c084dfc05c25d02843d03893d0010024002400340023ba5515105b00188c161510005000b455500000000000000000965c142411c06061c084edc05c33d02851d038a1d00100180018004c00188c56151000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!
XCTAssertTrue(0x803a == computeCRC16(input))
Expand Down
12 changes: 1 addition & 11 deletions MinimedKitTests/CRC8Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ import XCTest


class CRC8Tests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}


func testComputeCRC8() {
let input = Data(hexadecimalString: "a259705504a24117043a0e080b003d3d00015b030105d817790a0f00000300008b1702000e080b0000")!
XCTAssertEqual(0x71, computeCRC8(input))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ import XCTest
@testable import MinimedKit

class BatteryChangeGlucoseEventTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testDecoding() {
let rawData = Data(hexadecimalString: "0a0bae0a0e")!
let subject = BatteryChangeGlucoseEvent(availableData: rawData, relativeTimestamp: DateComponents())!

let expectedTimestamp = DateComponents(calendar: Calendar.current,
let expectedTimestamp = DateComponents(calendar: Calendar(identifier: .gregorian),
year: 2014, month: 2, day: 10, hour: 11, minute: 46)
XCTAssertEqual(subject.timestamp, expectedTimestamp)
}
Expand Down
10 changes: 1 addition & 9 deletions MinimedKitTests/GlucoseEvents/CalBGForGHGlucoseEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@ import XCTest

class CalBGForGHGlucoseEventTests: XCTestCase {

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

func testDecoding() {
let rawData = Data(hexadecimalString: "0e4f5b138fa0")!
let subject = CalBGForGHGlucoseEvent(availableData: rawData, relativeTimestamp: DateComponents())!

let expectedTimestamp = DateComponents(calendar: Calendar.current,
let expectedTimestamp = DateComponents(calendar: Calendar(identifier: .gregorian),
year: 2015, month: 5, day: 19, hour: 15, minute: 27)
XCTAssertEqual(subject.timestamp, expectedTimestamp)
XCTAssertEqual(subject.amount, 160)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,11 @@ import XCTest

class DateTimeChangeGlucoseEventTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testDecoding() {
let rawData = Data(hexadecimalString: "0c0ad23e0e")!
let subject = DateTimeChangeGlucoseEvent(availableData: rawData, relativeTimestamp: DateComponents())!

let expectedTimestamp = DateComponents(calendar: Calendar.current,
let expectedTimestamp = DateComponents(calendar: Calendar(identifier: .gregorian),
year: 2014, month: 3, day: 30, hour: 10, minute: 18)
XCTAssertEqual(subject.timestamp, expectedTimestamp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import XCTest

class GlucoseSensorDataGlucoseEventTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testDecoding() {
let rawData = Data(hexadecimalString: "35")!
let subject = GlucoseSensorDataGlucoseEvent(availableData: rawData, relativeTimestamp: DateComponents())!
Expand Down
Loading

0 comments on commit 85afbb3

Please sign in to comment.