Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ DerivedData
project.xcworkspace
.DS_Store
*.log
project.xcworkspace

# CocoaPods
#
Expand Down
21 changes: 21 additions & 0 deletions Common/NSData.swift → Common/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ extension Data {
return T(littleEndian: bytes.pointee)
}
}

func toBigEndian<T: FixedWidthInteger>(_: T.Type) -> T {
return self.withUnsafeBytes {
return T(bigEndian: $0.pointee)
}
}

mutating func append<T: FixedWidthInteger>(_ newElement: T) {
var element = newElement.littleEndian
append(UnsafeBufferPointer(start: &element, count: 1))
}

mutating func appendBigEndian<T: FixedWidthInteger>(_ newElement: T) {
var element = newElement.bigEndian
append(UnsafeBufferPointer(start: &element, count: 1))
}

init<T: FixedWidthInteger>(_ value: T) {
var value = value.littleEndian
self.init(buffer: UnsafeBufferPointer(start: &value, count: 1))
}
}

// String conversion methods, adapted from https://stackoverflow.com/questions/40276322/hex-binary-string-conversion-in-swift/40278391#40278391
Expand Down
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.0</string>
<string>2.0.1</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/BatteryChemistryType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public enum BatteryChemistryType: Int, CustomStringConvertible {
}
}

public func chargeRemaining(voltage: Double) -> Double {
let computed = (voltage - self.minVoltage)/(self.maxVoltage - self.minVoltage)
public func chargeRemaining(at voltage: Measurement<UnitElectricPotentialDifference>) -> Double {
let computed = (voltage.converted(to: .volts).value - self.minVoltage)/(self.maxVoltage - self.minVoltage)
return max(min(computed, 1), 0)
}
}
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/CalBGForGHGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public struct CalBGForGHGlucoseEvent: GlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/SensorCalFactorGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SensorCalFactorGlucoseEvent: GlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/SensorCalGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SensorCalGlucoseEvent: RelativeTimestampedGlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/SensorErrorGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SensorErrorGlucoseEvent: RelativeTimestampedGlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/SensorPacketGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SensorPacketGlucoseEvent: RelativeTimestampedGlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/SensorStatusGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SensorStatusGlucoseEvent: GlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/GlucoseEvents/SensorSyncGlucoseEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SensorSyncGlucoseEvent: GlucoseEvent {
return nil
}

func d(_ idx:Int) -> Int {
return Int(availableData[idx] as UInt8)
func d(_ idx: Int) -> Int {
return Int(availableData[idx])
}

rawData = availableData.subdata(in: 0..<length)
Expand Down
6 changes: 3 additions & 3 deletions MinimedKit/GlucosePage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class GlucosePage {

func matchEvent(_ offset: Int, relativeTimestamp: DateComponents) -> GlucoseEvent {
let remainingData = pageData.subdata(in: offset..<pageData.count)
let opcode = pageData[offset] as UInt8
let opcode = pageData[offset]
if let eventType = GlucoseEventType(rawValue: opcode) {
if let event = eventType.eventType.init(availableData: remainingData, relativeTimestamp: relativeTimestamp) {
return event
Expand All @@ -53,7 +53,7 @@ public class GlucosePage {

while offset < length {
// ignore null bytes
if pageData[offset] as UInt8 == 0 {
if pageData[offset] == 0 {
offset += 1
continue
} else {
Expand Down Expand Up @@ -90,7 +90,7 @@ public class GlucosePage {

while offset < length {
// ignore null bytes
if pageData[offset] as UInt8 == 0 {
if pageData[offset] == 0 {
offset += 1
continue
}
Expand Down
6 changes: 3 additions & 3 deletions MinimedKit/HistoryPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct HistoryPage {
let pageData = pageData.subdata(in: 0..<1022)

func matchEvent(_ offset: Int) -> PumpEvent? {
if let eventType = PumpEventType(rawValue:(pageData[offset] as UInt8)) {
if let eventType = PumpEventType(rawValue: pageData[offset]) {
let remainingData = pageData.subdata(in: offset..<pageData.count)
if let event = eventType.eventType.init(availableData: remainingData, pumpModel: pumpModel) {
return event
Expand All @@ -48,13 +48,13 @@ public struct HistoryPage {

while offset < length {
// Slurp up 0's
if pageData[offset] as UInt8 == 0 {
if pageData[offset] == 0 {
offset += 1
continue
}
guard var event = matchEvent(offset) else {
events = [PumpEvent]()
throw HistoryPageError.unknownEventType(eventType: pageData[offset] as UInt8)
throw HistoryPageError.unknownEventType(eventType: pageData[offset])
}

if unabsorbedInsulinRecord != nil, var bolus = event as? BolusNormalPumpEvent {
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.0</string>
<string>2.0.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
16 changes: 16 additions & 0 deletions MinimedKit/MessageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public enum MessageType: UInt8 {
case deviceLink = 0x0A
case errorResponse = 0x15
case writeGlucoseHistoryTimestamp = 0x28

case readRemoteControlID = 0x2e // Refused by x23 pumps

case changeTime = 0x40
case bolus = 0x42

Expand Down Expand Up @@ -67,6 +70,15 @@ public enum MessageType: UInt8 {
case readSettings = 0xc0
case readCurrentGlucosePage = 0xcd
case readPumpStatus = 0xce

case unknown_e2 = 0xe2 // a7594040e214190226330000000000021f99011801e00103012c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
case unknown_e6 = 0xe6 // a7594040e60200190000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
case settingsChangeCounter = 0xec // Body[3] increments by 1 after changing certain settings 0200af0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

case readOtherDevicesIDs = 0xf0
case readCaptureEventEnabled = 0xf1 // Body[1] encodes the bool state 0101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
case changeCaptureEventEnable = 0xf2
case readOtherDevicesStatus = 0xf3

var bodyType: MessageBody.Type {
switch self {
Expand Down Expand Up @@ -114,6 +126,10 @@ public enum MessageType: UInt8 {
return GetGlucosePageMessageBody.self
case .errorResponse:
return PumpErrorMessageBody.self
case .readOtherDevicesIDs:
return ReadOtherDevicesIDsMessageBody.self
case .readOtherDevicesStatus:
return ReadOtherDevicesStatusMessageBody.self
default:
return UnknownMessageBody.self
}
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/Messages/GetBatteryCarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class GetBatteryCarelinkMessageBody: CarelinkLongMessageBody {
return nil
}

volts = Double(Int(rxData[2] as UInt8) << 8 + Int(rxData[3] as UInt8)) / 100.0
status = BatteryStatus(statusByte: rxData[1] as UInt8)
volts = Double(Int(rxData[2]) << 8 + Int(rxData[3])) / 100.0
status = BatteryStatus(statusByte: rxData[1])

super.init(rxData: rxData)
}
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/Messages/GetGlucosePageMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class GetGlucosePageMessageBody: CarelinkLongMessageBody {
guard rxData.count == type(of: self).length else {
return nil
}
frameNumber = Int(rxData[0] as UInt8) & 0b1111111
lastFrame = (rxData[0] as UInt8) & 0b10000000 > 0
frameNumber = Int(rxData[0]) & 0b1111111
lastFrame = (rxData[0]) & 0b10000000 > 0
frame = rxData.subdata(in: 1..<65)
super.init(rxData: rxData)
}
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/Messages/GetHistoryPageCarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class GetHistoryPageCarelinkMessageBody: CarelinkLongMessageBody {
guard rxData.count == type(of: self).length else {
return nil
}
frameNumber = Int(rxData[0] as UInt8) & 0b1111111
lastFrame = (rxData[0] as UInt8) & 0b10000000 > 0
frameNumber = Int(rxData[0]) & 0b1111111
lastFrame = (rxData[0]) & 0b10000000 > 0
frame = rxData.subdata(in: 1..<65)
super.init(rxData: rxData)
}
Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/Messages/MeterMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class MeterMessage {
if rxData.count == length,
let packetType = PacketType(rawValue: rxData[0]), packetType == .meter
{
let flags = ((rxData[4] as UInt8) & 0b110) >> 1
let flags = ((rxData[4]) & 0b110) >> 1
ackFlag = flags == 0x03
glucose = Int((rxData[4] as UInt8) & 0b1) << 8 + Int(rxData[4] as UInt8)
glucose = Int((rxData[4]) & 0b1) << 8 + Int(rxData[4])
} else {
ackFlag = false
glucose = 0
Expand Down
2 changes: 1 addition & 1 deletion MinimedKit/Messages/MySentryPumpStatusMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public struct MySentryPumpStatusMessageBody: MessageBody, DictionaryRepresentabl
let matchingHour: UInt8 = rxData[20]
nextSensorCalibrationDateComponents = DateComponents()
nextSensorCalibrationDateComponents?.hour = Int(matchingHour)
nextSensorCalibrationDateComponents?.minute = Int(rxData[21] as UInt8)
nextSensorCalibrationDateComponents?.minute = Int(rxData[21])
nextSensorCalibrationDateComponents?.calendar = calendar
}

Expand Down
4 changes: 2 additions & 2 deletions MinimedKit/Messages/ReadCurrentGlucosePageMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class ReadCurrentGlucosePageMessageBody: CarelinkLongMessageBody {

self.pageNum = rxData[1..<5
].withUnsafeBytes { UInt32(bigEndian: $0.pointee) }
self.glucose = Int(rxData[6] as UInt8)
self.isig = Int(rxData[8] as UInt8)
self.glucose = Int(rxData[6])
self.isig = Int(rxData[8])

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

import Foundation


public class ReadOtherDevicesIDsMessageBody: CarelinkLongMessageBody {

let ids: [Data]

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

let count = Int(rxData[1])

var ids: [Data] = []

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

ids.append(rxData.subdata(in: start..<end))
}

self.ids = ids

super.init(rxData: rxData)
}
}


// Body[1] is the count Body[3..<7] is the first ID.
// 1f0101 a2105728 00 00000636 036f0040600107062f1dfc004020c107062f0e77000000000000000000000000000000000000000000000000000000000000000000
// 1f0201 a2105728 00 a2016016 036f0040600107062f1dfc004020c107062f0e77000000000000000000000000000000000000000000000000000000000000000000
26 changes: 26 additions & 0 deletions MinimedKit/Messages/ReadOtherDevicesStatusMessageBody.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// ReadOtherDevicesStatusMessageBody.swift
// MinimedKit
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import Foundation

public class ReadOtherDevicesStatusMessageBody: CarelinkLongMessageBody {
public let isEnabled: Bool

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

isEnabled = rxData[1] == 1

super.init(rxData: rxData)
}
}

// Body[1] encodes the bool state
// 0200010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// 0201010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
4 changes: 2 additions & 2 deletions MinimedKit/Messages/ReadPumpStatusMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class ReadPumpStatusMessageBody: CarelinkLongMessageBody {
return nil
}

bolusing = (rxData[2] as UInt8) > 0
suspended = (rxData[3] as UInt8) > 0
bolusing = rxData[2] > 0
suspended = rxData[3] > 0

super.init(rxData: rxData)
}
Expand Down
6 changes: 3 additions & 3 deletions MinimedKit/Messages/ReadSettingsCarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ public class ReadSettingsCarelinkMessageBody: CarelinkLongMessageBody {
let newer = rxData[0] == 25 // x23

let maxBolusTicks: UInt8
let maxBasalTicks: Int
let maxBasalTicks: UInt16

if newer {
maxBolusTicks = rxData[7]
maxBasalTicks = Int(bigEndianBytes: rxData.subdata(in: 8..<10))
maxBasalTicks = rxData[8..<10].toBigEndian(UInt16.self)
} else {
maxBolusTicks = rxData[6]
maxBasalTicks = Int(bigEndianBytes: rxData.subdata(in: 7..<9))
maxBasalTicks = rxData[7..<9].toBigEndian(UInt16.self)
}
maxBolus = Double(maxBolusTicks) / type(of: self).maxBolusMultiplier
maxBasal = Double(maxBasalTicks) / type(of: self).maxBasalMultiplier
Expand Down
Loading