Skip to content

Commit

Permalink
Merge pull request #405 from ps2/dev
Browse files Browse the repository at this point in the history
Release 2.0.2
  • Loading branch information
ps2 authored Jun 19, 2018
2 parents cbf3bd8 + 0589062 commit 859b5af
Show file tree
Hide file tree
Showing 42 changed files with 555 additions and 85 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>2.0.1</string>
<string>2.0.2</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
8 changes: 7 additions & 1 deletion MinimedKit/BasalSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension BasalSchedule {
if let entry = BasalScheduleEntry(
index: tuple.index,
rawValue: rawValue[beginOfRange..<endOfRange]
) {
) {
if let last = entries.last, last.timeOffset >= entry.timeOffset {
// Stop if the new timeOffset isn't greater than the last one
break
Expand Down Expand Up @@ -77,6 +77,11 @@ extension BasalSchedule {
byteIndex += rawEntry.count
}

// Send the special "empty" code to clear a schedule
if entries.count == 0 {
buffer[2] = 0x3f
}

return buffer
}
}
Expand All @@ -97,6 +102,7 @@ private extension BasalScheduleEntry {
let offsetMinutes = Double(rawValue.last!) * 30
let timeOffset = TimeInterval(minutes: offsetMinutes)

// 0x3f *30*60 denotes a stop value
guard timeOffset < .hours(24) else {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/BatteryChemistryType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public enum BatteryChemistryType: Int, CustomStringConvertible {
public var minVoltage: Double {
switch self {
case .alkaline:
return 1.20
return 1.18
case .lithium:
return 1.32
}
Expand Down
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>2.0.1</string>
<string>2.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
41 changes: 26 additions & 15 deletions MinimedKit/MessageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ public enum MessageType: UInt8 {
case errorResponse = 0x15
case writeGlucoseHistoryTimestamp = 0x28

case readRemoteControlID = 0x2e // Refused by x23 pumps
case setBasalProfileA = 0x30 // CMD_SET_A_PROFILE
case setBasalProfileB = 0x31 // CMD_SET_B_PROFILE

case changeTime = 0x40
case setMaxBolus = 0x41 // CMD_SET_MAX_BOLUS
case bolus = 0x42

case PumpExperiment_OP67 = 0x43
case PumpExperiment_OP68 = 0x44
case PumpExperiment_OP69 = 0x45
case PumpExperiment_OP69 = 0x45 // CMD_SET_VAR_BOLUS_ENABLE

case selectBasalProfile = 0x4a

case changeTempBasal = 0x4c

case PumpExperiment_OP80 = 0x50
case PumpExperiment_OP81 = 0x51
case PumpExperiment_OP82 = 0x52
case PumpExperiment_OP83 = 0x53
case PumpExperiment_OP84 = 0x54
case PumpExperiment_OP85 = 0x55
case setRemoteControlID = 0x51 // CMD_SET_RF_REMOTE_ID
case PumpExperiment_OP82 = 0x52 // CMD_SET_BLOCK_ENABLE
case setLanguage = 0x53
case PumpExperiment_OP84 = 0x54 // CMD_SET_ALERT_TYPE
case PumpExperiment_OP85 = 0x55 // CMD_SET_PATTERNS_ENABLE
case PumpExperiment_OP86 = 0x56
case PumpExperiment_OP87 = 0x57
case PumpExperiment_OP88 = 0x58
case setRemoteControlEnabled = 0x57 // CMD_SET_RF_ENABLE
case PumpExperiment_OP88 = 0x58 // CMD_SET_INSULIN_ACTION_TYPE
case PumpExperiment_OP89 = 0x59
case PumpExperiment_OP90 = 0x5a

Expand All @@ -49,16 +51,23 @@ public enum MessageType: UInt8 {

case powerOn = 0x5d

case PumpExperiment_OP97 = 0x61
case PumpExperiment_OP98 = 0x62
case PumpExperiment_OP99 = 0x63
case PumpExperiment_O100 = 0x64
case PumpExperiment_O101 = 0x65
case PumpExperiment_O103 = 0x67
case setBolusWizardEnabled1 = 0x61
case setBolusWizardEnabled2 = 0x62
case setBolusWizardEnabled3 = 0x63
case setBolusWizardEnabled4 = 0x64
case setBolusWizardEnabled5 = 0x65
case setAlarmClockEnable = 0x67

case setMaxBasalRate = 0x6e // CMD_SET_MAX_BASAL
case setBasalProfileStandard = 0x6f // CMD_SET_STD_PROFILE

case readTime = 0x70
case getBattery = 0x72
case readRemainingInsulin = 0x73
case readFirmwareVersion = 0x74
case readErrorStatus = 0x75
case readRemoteControlIDs = 0x76 // CMD_READ_REMOTE_CTRL_IDS

case getHistoryPage = 0x80
case getPumpModel = 0x8d
case readProfileSTD512 = 0x92
Expand Down Expand Up @@ -130,6 +139,8 @@ public enum MessageType: UInt8 {
return ReadOtherDevicesIDsMessageBody.self
case .readOtherDevicesStatus:
return ReadOtherDevicesStatusMessageBody.self
case .readRemoteControlIDs:
return ReadRemoteControlIDsMessageBody.self
default:
return UnknownMessageBody.self
}
Expand Down
27 changes: 27 additions & 0 deletions MinimedKit/Messages/ChangeMaxBasalRateMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ChangeMaxBasalRateMessageBody.swift
// MinimedKit
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import Foundation


public class ChangeMaxBasalRateMessageBody: CarelinkLongMessageBody {

static let multiplier: Double = 40

public convenience init?(maxBasalUnitsPerHour: Double) {
guard maxBasalUnitsPerHour >= 0 && maxBasalUnitsPerHour <= 35 else {
return nil
}

let ticks = UInt16(maxBasalUnitsPerHour * type(of: self).multiplier)
var data = Data(bytes: [UInt8(clamping: ticks.bitWidth / 8)])
data.appendBigEndian(ticks)

self.init(rxData: data)
}

}
27 changes: 27 additions & 0 deletions MinimedKit/Messages/ChangeMaxBolusMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ChangeMaxBolusMessageBody.swift
// MinimedKit
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import Foundation


public class ChangeMaxBolusMessageBody: CarelinkLongMessageBody {

static let multiplier: Double = 10

public convenience init?(maxBolusUnits: Double) {
guard maxBolusUnits >= 0 && maxBolusUnits <= 25 else {
return nil
}

let ticks = UInt8(maxBolusUnits * type(of: self).multiplier)
var data = Data(bytes: [UInt8(clamping: ticks.bitWidth / 8)])
data.appendBigEndian(ticks)

self.init(rxData: data)
}

}
30 changes: 30 additions & 0 deletions MinimedKit/Messages/ChangeRemoteControlIDMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ChangeRemoteControlIDMessageBody.swift
// MinimedKit
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import Foundation


public class ChangeRemoteControlIDMessageBody: CarelinkLongMessageBody {
public convenience init?(id: Data? = nil, index: Int) {
guard index < 3 else {
return nil
}

var rxData = Data(repeating: 0x2d, count: 8) // 2d signifies a deletion
rxData[0] = 0x07 // length
rxData[1] = UInt8(clamping: index)

if let id = id {
for (index, byte) in id.enumerated() {
rxData[2 + index] = 0b00110000 + byte
}
}

self.init(rxData: rxData)
}

}
2 changes: 1 addition & 1 deletion MinimedKit/Messages/ReadOtherDevicesIDsMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

public class ReadOtherDevicesIDsMessageBody: CarelinkLongMessageBody {

let ids: [Data]
public let ids: [Data]

public required init?(rxData: Data) {
guard rxData.count == type(of: self).length else {
Expand Down
44 changes: 44 additions & 0 deletions MinimedKit/Messages/ReadRemoteControlIDsMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ReadRemoteControlIDsMessageBody.swift
// MinimedKit
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import Foundation

private let idSize = 6

public class ReadRemoteControlIDsMessageBody: CarelinkLongMessageBody {
public let ids: [Data]

public required init?(rxData: Data) {
guard rxData.count == type(of: self).length else {
return nil
}

var ids: [Data] = []

remotes: for index in stride(from: 0, to: 3, by: 1) {
let start = (index * idSize + 1)
let end = start + idSize

var remoteID = Data(capacity: idSize)

for byte in rxData[start..<end] {
let isEnabled = (byte & 0b00010000) == 0b00010000
guard isEnabled else {
continue remotes
}

remoteID.append(byte & 0xf)
}

ids.append(remoteID)
}

self.ids = ids

super.init(rxData: rxData)
}
}
15 changes: 15 additions & 0 deletions MinimedKit/Messages/SetRemoteControlEnabledMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// SetRemoteControlEnabledMessageBody.swift
// MinimedKit
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import Foundation


public class SetRemoteControlEnabledMessageBody: CarelinkLongMessageBody {
public convenience init(enabled: Bool) {
self.init(rxData: Data(bytes: [1, enabled ? 1 : 0]))!
}
}
9 changes: 9 additions & 0 deletions MinimedKitTests/BasalScheduleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ class BasalScheduleTests: XCTestCase {

XCTAssertEqual(3, frames.count)
}

func testEmptySchedule() {
let emptyData = Data(hexadecimalString: "00003f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!

let profile = BasalSchedule(rawValue: emptyData)
XCTAssertNil(profile)

XCTAssertEqual(emptyData, BasalSchedule(entries: []).rawValue)
}
}
2 changes: 1 addition & 1 deletion MinimedKitTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>2.0.1</string>
<string>2.0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
36 changes: 36 additions & 0 deletions MinimedKitTests/Messages/ChangeMaxBasalRateMessageBodyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// ChangeMaxBasalRateMessageBodyTests.swift
// MinimedKitTests
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import XCTest
@testable import MinimedKit

class ChangeMaxBasalRateMessageBodyTests: XCTestCase {

func testMaxBasalRate() {
var body = ChangeMaxBasalRateMessageBody(maxBasalUnitsPerHour: 6.4)!

XCTAssertEqual(Data(hexadecimalString: "0201000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!, body.txData, body.txData.hexadecimalString)

body = ChangeMaxBasalRateMessageBody(maxBasalUnitsPerHour: 4.0)!

XCTAssertEqual(Data(hexadecimalString: "0200A00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!, body.txData, body.txData.hexadecimalString)
}

func testMaxBasalRateRounded() {
let body = ChangeMaxBasalRateMessageBody(maxBasalUnitsPerHour: 9.115)!

XCTAssertEqual(Data(hexadecimalString: "02016c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!, body.txData, body.txData.hexadecimalString)


}

func testMaxBasalRateOutOfRange() {
XCTAssertNil(ChangeMaxBasalRateMessageBody(maxBasalUnitsPerHour: -1))
XCTAssertNil(ChangeMaxBasalRateMessageBody(maxBasalUnitsPerHour: 36))
}

}
30 changes: 30 additions & 0 deletions MinimedKitTests/Messages/ChangeMaxBolusMessageBodyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ChangeMaxBolusMessageBodyTests.swift
// MinimedKitTests
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import XCTest
@testable import MinimedKit

class ChangeMaxBolusMessageBodyTests: XCTestCase {

func testMaxBolus() {
let body = ChangeMaxBolusMessageBody(maxBolusUnits: 6.4)!

XCTAssertEqual(Data(hexadecimalString: "0140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!, body.txData, body.txData.hexadecimalString)
}

func testMaxBolusRounded() {
let body = ChangeMaxBolusMessageBody(maxBolusUnits: 2.25)!

XCTAssertEqual(Data(hexadecimalString: "0116000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")!, body.txData, body.txData.hexadecimalString)
}

func testMaxBolusOutOfRange() {
XCTAssertNil(ChangeMaxBolusMessageBody(maxBolusUnits: -1))
XCTAssertNil(ChangeMaxBolusMessageBody(maxBolusUnits: 26))
}

}
Loading

0 comments on commit 859b5af

Please sign in to comment.