Skip to content

Commit

Permalink
Merge pull request #94 from ps2/dev
Browse files Browse the repository at this point in the history
Release 0.3.0
  • Loading branch information
ps2 committed May 23, 2016
2 parents d32a543 + f5cdbe4 commit 7e4ca1e
Show file tree
Hide file tree
Showing 201 changed files with 7,144 additions and 9,481 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ DerivedData
*.xcuserstate
*.xcscmblueprint
.DS_Store
*.log

# CocoaPods
#
Expand All @@ -26,3 +27,5 @@ DerivedData
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
#
# Pods/

Carthage/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ xcode_sdk:
xcode_project: RileyLink.xcodeproj
xcode_scheme:
- RileyLink
before_script:
- carthage bootstrap
script:
- xcodebuild -project RileyLink.xcodeproj -scheme RileyLink -sdk iphonesimulator9.3 test
1 change: 1 addition & 0 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "loudnate/Crypto" "master"
1 change: 1 addition & 0 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "loudnate/Crypto" "e0ef5b498f2c373d676135dabf5d1803b8558509"
42 changes: 21 additions & 21 deletions MinimedKit/CRC16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import Foundation
private let crcTable: [UInt16] = [0, 4129, 8258, 12387, 16516, 20645, 24774, 28903, 33032, 37161, 41290, 45419, 49548, 53677, 57806, 61935, 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302, 37689, 33560, 45947, 41818, 54205, 50076, 62463, 58334, 9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637, 42346, 46411, 34088, 38153, 58862, 62927, 50604, 54669, 13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100, 46939, 42874, 38681, 34616, 63455, 59390, 55197, 51132, 18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371, 51660, 55789, 59790, 63919, 35144, 39273, 43274, 47403, 23285, 19156, 31415, 27286, 6769, 2640, 14899, 10770, 56317, 52188, 64447, 60318, 39801, 35672, 47931, 43802, 27814, 31879, 19684, 23749, 11298, 15363, 3168, 7233, 60846, 64911, 52716, 56781, 44330, 48395, 36200, 40265, 32407, 28342, 24277, 20212, 15891, 11826, 7761, 3696, 65439, 61374, 57309, 53244, 48923, 44858, 40793, 36728, 37256, 33193, 45514, 41451, 53516, 49453, 61774, 57711, 4224, 161, 12482, 8419, 20484, 16421, 28742, 24679, 33721, 37784, 41979, 46042, 49981, 54044, 58239, 62302, 689, 4752, 8947, 13010, 16949, 21012, 25207, 29270, 46570, 42443, 38312, 34185, 62830, 58703, 54572, 50445, 13538, 9411, 5280, 1153, 29798, 25671, 21540, 17413, 42971, 47098, 34713, 38840, 59231, 63358, 50973, 55100, 9939, 14066, 1681, 5808, 26199, 30326, 17941, 22068, 55628, 51565, 63758, 59695, 39368, 35305, 47498, 43435, 22596, 18533, 30726, 26663, 6336, 2273, 14466, 10403, 52093, 56156, 60223, 64286, 35833, 39896, 43963, 48026, 19061, 23124, 27191, 31254, 2801, 6864, 10931, 14994, 64814, 60687, 56684, 52557, 48554, 44427, 40424, 36297, 31782, 27655, 23652, 19525, 15522, 11395, 7392, 3265, 61215, 65342, 53085, 57212, 44955, 49082, 36825, 40952, 28183, 32310, 20053, 24180, 11923, 16050, 3793, 7920]

func computeCRC16(data: NSData) -> UInt16 {

var crc: UInt16 = 0xffff
var pdata = UnsafePointer<UInt8>(data.bytes)
var nbytes = data.length
/* loop over the buffer data */
while nbytes > 0 {
let idx = ((crc >> 8) ^ UInt16(pdata.memory)) & 0xff
crc = ((crc << 8) ^ crcTable[Int(idx)]) & 0xffff
pdata = pdata.successor()
nbytes -= 1
}
return crc
var crc: UInt16 = 0xffff
var pdata = UnsafePointer<UInt8>(data.bytes)
var nbytes = data.length
/* loop over the buffer data */
while nbytes > 0 {
let idx = ((crc >> 8) ^ UInt16(pdata.memory)) & 0xff
crc = ((crc << 8) ^ crcTable[Int(idx)]) & 0xffff
pdata = pdata.successor()
nbytes -= 1
}
return crc
}

func checkCRC16(data: NSData) -> Bool {
if data.length > 2 {
let lowByte: UInt8 = data[data.length - 1]
let hiByte: UInt8 = data[data.length - 2]
let packetCRC: UInt16 = (UInt16(hiByte) << 8) + UInt16(lowByte)
return packetCRC == computeCRC16(data.subdataWithRange(NSMakeRange(0, data.length-2)))
} else {
return false
}

if data.length > 2 {
let lowByte: UInt8 = data[data.length - 1]
let hiByte: UInt8 = data[data.length - 2]
let packetCRC: UInt16 = (UInt16(hiByte) << 8) + UInt16(lowByte)
return packetCRC == computeCRC16(data.subdataWithRange(NSMakeRange(0, data.length-2)))
} else {
return false
}
}
24 changes: 12 additions & 12 deletions MinimedKit/CRC8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ private let crcTable: [UInt8] = [0x0, 0x9B, 0xAD, 0x36, 0xC1, 0x5A, 0x6C, 0xF7,


func computeCRC8(data: NSData) -> UInt8 {

var crc: UInt8 = 0

var pdata = UnsafePointer<UInt8>(data.bytes)
var nbytes = data.length
/* loop over the buffer data */
while nbytes > 0 {
crc = crcTable[Int((crc ^ pdata.memory) & 0xff)]
pdata = pdata.successor()
nbytes -= 1
}
return crc
var crc: UInt8 = 0
var pdata = UnsafePointer<UInt8>(data.bytes)
var nbytes = data.length
/* loop over the buffer data */
while nbytes > 0 {
crc = crcTable[Int((crc ^ pdata.memory) & 0xff)]
pdata = pdata.successor()
nbytes -= 1
}
return crc
}

100 changes: 50 additions & 50 deletions MinimedKit/HistoryPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,60 @@
//

public class HistoryPage {

public enum Error: ErrorType {
case InvalidCRC
case UnknownEventType(eventType: UInt8)
}

public let events: [PumpEvent]

public init(pageData: NSData, pumpModel: PumpModel) throws {

guard checkCRC16(pageData) else {
events = [PumpEvent]()
throw Error.InvalidCRC
public enum Error: ErrorType {
case InvalidCRC
case UnknownEventType(eventType: UInt8)
}

let pageData = pageData.subdataWithRange(NSMakeRange(0, 1022))
public let events: [PumpEvent]

func matchEvent(offset: Int) -> PumpEvent? {
if let eventType = PumpEventType(rawValue:(pageData[offset] as UInt8)) {
let remainingData = pageData.subdataWithRange(NSMakeRange(offset, pageData.length - offset))
if let event = eventType.eventType.init(availableData: remainingData, pumpModel: pumpModel) {
return event
public init(pageData: NSData, pumpModel: PumpModel) throws {
guard checkCRC16(pageData) else {
events = [PumpEvent]()
throw Error.InvalidCRC
}
}
return nil
}

var offset = 0
let length = pageData.length
var unabsorbedInsulinRecord: UnabsorbedInsulinPumpEvent?
var tempEvents = [PumpEvent]()

while offset < length {
// Slurp up 0's
if pageData[offset] as UInt8 == 0 {
offset += 1
continue
}
guard let event = matchEvent(offset) else {
events = [PumpEvent]()
throw Error.UnknownEventType(eventType: pageData[offset] as UInt8)
}
if event.dynamicType == BolusNormalPumpEvent.self && unabsorbedInsulinRecord != nil {
let bolus: BolusNormalPumpEvent = event as! BolusNormalPumpEvent
bolus.unabsorbedInsulinRecord = unabsorbedInsulinRecord
unabsorbedInsulinRecord = nil
}
if event.dynamicType == UnabsorbedInsulinPumpEvent.self {
unabsorbedInsulinRecord = event as? UnabsorbedInsulinPumpEvent
} else {
tempEvents.append(event)
}
offset += event.length

let pageData = pageData.subdataWithRange(NSMakeRange(0, 1022))

func matchEvent(offset: Int) -> PumpEvent? {
if let eventType = PumpEventType(rawValue:(pageData[offset] as UInt8)) {
let remainingData = pageData.subdataWithRange(NSMakeRange(offset, pageData.length - offset))
if let event = eventType.eventType.init(availableData: remainingData, pumpModel: pumpModel) {
return event
}
}
return nil
}

var offset = 0
let length = pageData.length
var unabsorbedInsulinRecord: UnabsorbedInsulinPumpEvent?
var tempEvents = [PumpEvent]()

while offset < length {
// Slurp up 0's
if pageData[offset] as UInt8 == 0 {
offset += 1
continue
}
guard let event = matchEvent(offset) else {
events = [PumpEvent]()
throw Error.UnknownEventType(eventType: pageData[offset] as UInt8)
}
if event.dynamicType == BolusNormalPumpEvent.self && unabsorbedInsulinRecord != nil {
let bolus: BolusNormalPumpEvent = event as! BolusNormalPumpEvent
bolus.unabsorbedInsulinRecord = unabsorbedInsulinRecord
unabsorbedInsulinRecord = nil
}
if event.dynamicType == UnabsorbedInsulinPumpEvent.self {
unabsorbedInsulinRecord = event as? UnabsorbedInsulinPumpEvent
} else {
tempEvents.append(event)
}
offset += event.length
}
events = tempEvents
}
events = tempEvents
}
}
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>0.2.0</string>
<string>0.3.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
14 changes: 7 additions & 7 deletions MinimedKit/MessageType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum MessageType: UInt8 {
case GetPumpModel = 0x8d
case ReadTempBasal = 0x98
case ReadSettings = 0xc0

var bodyType: MessageBody.Type {
switch self {
case .Alert:
Expand All @@ -44,17 +44,17 @@ public enum MessageType: UInt8 {
case .ReadTime:
return ReadTimeCarelinkMessageBody.self
case .FindDevice:
return FindDeviceMessageBody.self
return FindDeviceMessageBody.self
case .DeviceLink:
return DeviceLinkMessageBody.self
return DeviceLinkMessageBody.self
case .ButtonPress:
return ButtonPressCarelinkMessageBody.self
return ButtonPressCarelinkMessageBody.self
case .GetPumpModel:
return GetPumpModelCarelinkMessageBody.self
return GetPumpModelCarelinkMessageBody.self
case .GetHistoryPage:
return GetHistoryPageCarelinkMessageBody.self
return GetHistoryPageCarelinkMessageBody.self
case .GetBattery:
return GetBatteryCarelinkMessageBody.self
return GetBatteryCarelinkMessageBody.self
default:
return UnknownMessageBody.self
}
Expand Down
58 changes: 29 additions & 29 deletions MinimedKit/Messages/BolusCarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ import Foundation


public class BolusCarelinkMessageBody: CarelinkLongMessageBody {

public convenience init(units: Double, strokesPerUnit: Int = 10) {

let length: Int
let scrollRate: Int

if strokesPerUnit >= 40 {
length = 2

// 40-stroke pumps scroll faster for higher unit values
switch units {
case let u where u > 10:
scrollRate = 4
case let u where u > 1:
scrollRate = 2
default:
scrollRate = 1
}
} else {
length = 1
scrollRate = 1

public convenience init(units: Double, strokesPerUnit: Int = 10) {

let length: Int
let scrollRate: Int

if strokesPerUnit >= 40 {
length = 2

// 40-stroke pumps scroll faster for higher unit values
switch units {
case let u where u > 10:
scrollRate = 4
case let u where u > 1:
scrollRate = 2
default:
scrollRate = 1
}
} else {
length = 1
scrollRate = 1
}

let strokes = Int(units * Double(strokesPerUnit / scrollRate)) * scrollRate

let data = NSData(hexadecimalString: String(format: "%02x%0\(2 * length)x", length, strokes))!

self.init(rxData: data)!
}

let strokes = Int(units * Double(strokesPerUnit / scrollRate)) * scrollRate

let data = NSData(hexadecimalString: String(format: "%02x%0\(2 * length)x", length, strokes))!

self.init(rxData: data)!
}


}
30 changes: 15 additions & 15 deletions MinimedKit/Messages/ButtonPressCarelinkMessageBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import Foundation

public class ButtonPressCarelinkMessageBody: CarelinkLongMessageBody {

public enum ButtonType: UInt8 {
case Act = 0x02
case Esc = 0x01
case Down = 0x04
case Up = 0x03
case Easy = 0x00
}

public convenience init(buttonType: ButtonType) {
let numArgs = 1
let data = NSData(hexadecimalString: String(format: "%02x%02x", numArgs, buttonType.rawValue))!

self.init(rxData: data)!
}

public enum ButtonType: UInt8 {
case Act = 0x02
case Esc = 0x01
case Down = 0x04
case Up = 0x03
case Easy = 0x00
}

public convenience init(buttonType: ButtonType) {
let numArgs = 1
let data = NSData(hexadecimalString: String(format: "%02x%02x", numArgs, buttonType.rawValue))!

self.init(rxData: data)!
}

}
Loading

0 comments on commit 7e4ca1e

Please sign in to comment.