Skip to content

Commit

Permalink
Merge pull request #84 from ps2/dev
Browse files Browse the repository at this point in the history
Version 0.2.0
  • Loading branch information
ps2 committed May 14, 2016
2 parents 6ec7ba0 + 8e5fb6d commit d32a543
Show file tree
Hide file tree
Showing 355 changed files with 10,015 additions and 5,986 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ DerivedData
*.hmap
*.ipa
*.xcuserstate
*.xcscmblueprint
.DS_Store

# CocoaPods
Expand Down
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: objective-c
osx_image: xcode7.3
xcode_sdk:
- iphonesimulator9.3
xcode_project: RileyLink.xcodeproj
xcode_scheme:
- RileyLink
script:
- xcodebuild -project RileyLink.xcodeproj -scheme RileyLink -sdk iphonesimulator9.3 test
23 changes: 23 additions & 0 deletions MinimedKit/AlertType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// AlertType.swift
// Naterade
//
// Created by Nathan Racklyeft on 9/13/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//

public enum AlertType: UInt8 {
case NoDelivery = 0x04
case MaxHourlyBolus = 0x33
case LowReservoir = 0x52
case HighGlucose = 0x65
case LowGlucose = 0x66
case MeterBGNow = 0x68
case MeterBGSoon = 0x69
case CalibrationError = 0x6a
case SensorEnd = 0x6b
case WeakSignal = 0x70
case LostSensor = 0x71
case HighPredicted = 0x72
case LowPredicted = 0x73
}
38 changes: 38 additions & 0 deletions MinimedKit/CRC16.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// CRC16.swift
// RileyLink
//
// Created by Pete Schwamb on 2/27/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

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
}

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
}

}
28 changes: 28 additions & 0 deletions MinimedKit/CRC8.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// CRC8.swift
// RileyLink
//
// Created by Pete Schwamb on 2/27/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

private let crcTable: [UInt8] = [0x0, 0x9B, 0xAD, 0x36, 0xC1, 0x5A, 0x6C, 0xF7, 0x19, 0x82, 0xB4, 0x2F, 0xD8, 0x43, 0x75, 0xEE, 0x32, 0xA9, 0x9F, 0x4, 0xF3, 0x68, 0x5E, 0xC5, 0x2B, 0xB0, 0x86, 0x1D, 0xEA, 0x71, 0x47, 0xDC, 0x64, 0xFF, 0xC9, 0x52, 0xA5, 0x3E, 0x8, 0x93, 0x7D, 0xE6, 0xD0, 0x4B, 0xBC, 0x27, 0x11, 0x8A, 0x56, 0xCD, 0xFB, 0x60, 0x97, 0xC, 0x3A, 0xA1, 0x4F, 0xD4, 0xE2, 0x79, 0x8E, 0x15, 0x23, 0xB8, 0xC8, 0x53, 0x65, 0xFE, 0x9, 0x92, 0xA4, 0x3F, 0xD1, 0x4A, 0x7C, 0xE7, 0x10, 0x8B, 0xBD, 0x26, 0xFA, 0x61, 0x57, 0xCC, 0x3B, 0xA0, 0x96, 0xD, 0xE3, 0x78, 0x4E, 0xD5, 0x22, 0xB9, 0x8F, 0x14, 0xAC, 0x37, 0x1, 0x9A, 0x6D, 0xF6, 0xC0, 0x5B, 0xB5, 0x2E, 0x18, 0x83, 0x74, 0xEF, 0xD9, 0x42, 0x9E, 0x5, 0x33, 0xA8, 0x5F, 0xC4, 0xF2, 0x69, 0x87, 0x1C, 0x2A, 0xB1, 0x46, 0xDD, 0xEB, 0x70, 0xB, 0x90, 0xA6, 0x3D, 0xCA, 0x51, 0x67, 0xFC, 0x12, 0x89, 0xBF, 0x24, 0xD3, 0x48, 0x7E, 0xE5, 0x39, 0xA2, 0x94, 0xF, 0xF8, 0x63, 0x55, 0xCE, 0x20, 0xBB, 0x8D, 0x16, 0xE1, 0x7A, 0x4C, 0xD7, 0x6F, 0xF4, 0xC2, 0x59, 0xAE, 0x35, 0x3, 0x98, 0x76, 0xED, 0xDB, 0x40, 0xB7, 0x2C, 0x1A, 0x81, 0x5D, 0xC6, 0xF0, 0x6B, 0x9C, 0x7, 0x31, 0xAA, 0x44, 0xDF, 0xE9, 0x72, 0x85, 0x1E, 0x28, 0xB3, 0xC3, 0x58, 0x6E, 0xF5, 0x2, 0x99, 0xAF, 0x34, 0xDA, 0x41, 0x77, 0xEC, 0x1B, 0x80, 0xB6, 0x2D, 0xF1, 0x6A, 0x5C, 0xC7, 0x30, 0xAB, 0x9D, 0x6, 0xE8, 0x73, 0x45, 0xDE, 0x29, 0xB2, 0x84, 0x1F, 0xA7, 0x3C, 0xA, 0x91, 0x66, 0xFD, 0xCB, 0x50, 0xBE, 0x25, 0x13, 0x88, 0x7F, 0xE4, 0xD2, 0x49, 0x95, 0xE, 0x38, 0xA3, 0x54, 0xCF, 0xF9, 0x62, 0x8C, 0x17, 0x21, 0xBA, 0x4D, 0xD6, 0xE0, 0x7B]


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
}

24 changes: 24 additions & 0 deletions MinimedKit/Extensions/Int.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Int.swift
// Naterade
//
// Created by Nathan Racklyeft on 12/26/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//

import Foundation


extension Int {
init(bigEndianBytes bytes: [UInt8]) {
assert(bytes.count <= 4)
var result: UInt = 0

for idx in 0..<(bytes.count) {
let shiftAmount = UInt((bytes.count) - idx - 1) * 8
result += UInt(bytes[idx]) << shiftAmount
}

self.init(result)
}
}
113 changes: 113 additions & 0 deletions MinimedKit/Extensions/NSData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// NSData.swift
// Naterade
//
// Created by Nathan Racklyeft on 9/2/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//

import Foundation


extension NSData {
@nonobjc subscript(index: Int) -> Int8 {
let bytes: [Int8] = self[index...index]

return bytes[0]
}

@nonobjc subscript(index: Int) -> UInt8 {
let bytes: [UInt8] = self[index...index]

return bytes[0]
}

@nonobjc subscript(range: Range<Int>) -> UInt16 {
return self[range][0]
}

@nonobjc subscript(range: Range<Int>) -> UInt32 {
return self[range][0]
}

subscript(range: Range<Int>) -> [Int8] {
var dataArray = [Int8](count: range.count, repeatedValue: 0)
self.getBytes(&dataArray, range: NSRange(range))

return dataArray
}

subscript(range: Range<Int>) -> [UInt8] {
var dataArray = [UInt8](count: range.count, repeatedValue: 0)
self.getBytes(&dataArray, range: NSRange(range))

return dataArray
}

subscript(range: Range<Int>) -> [UInt16] {
var dataArray = [UInt16](count: range.count / 2, repeatedValue: 0)
self.getBytes(&dataArray, range: NSRange(range))

return dataArray
}

subscript(range: Range<Int>) -> [UInt32] {
var dataArray = [UInt32](count: range.count / 4, repeatedValue: 0)
self.getBytes(&dataArray, range: NSRange(range))

return dataArray
}

subscript(range: Range<Int>) -> NSData {
return subdataWithRange(NSRange(range))
}

convenience init?(hexadecimalString: String) {
if let
chars = hexadecimalString.cStringUsingEncoding(NSUTF8StringEncoding),
mutableData = NSMutableData(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:
self.init()
return nil
}

num += (UInt8(c) - offset) * multi
multi = 1
}
mutableData.appendBytes(&num, length: 1)
}

self.init(data: mutableData)
} else {
return nil
}
}

var hexadecimalString: String {
let bytesCollection = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(bytes), count: length)

let string = NSMutableString(capacity: length * 2)

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

return string as String
}
}
25 changes: 25 additions & 0 deletions MinimedKit/Extensions/NSDateComponents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// NSDateComponents.swift
// Naterade
//
// Created by Nathan Racklyeft on 9/13/15.
// Copyright © 2015 Nathan Racklyeft. All rights reserved.
//

import Foundation


extension NSDateComponents {
convenience init(mySentryBytes: [UInt8]) {
self.init()

hour = Int(mySentryBytes[0])
minute = Int(mySentryBytes[1])
second = Int(mySentryBytes[2])
year = Int(mySentryBytes[3]) + 2000
month = Int(mySentryBytes[4])
day = Int(mySentryBytes[5])

calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
}
}
66 changes: 66 additions & 0 deletions MinimedKit/HistoryPage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// HistoryPage.swift
// RileyLink
//
// Created by Pete Schwamb on 3/3/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

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
}

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
}
}
26 changes: 26 additions & 0 deletions MinimedKit/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
Loading

0 comments on commit d32a543

Please sign in to comment.