Skip to content

Commit

Permalink
Merge pull request #387 from ps2/dev
Browse files Browse the repository at this point in the history
Release 2.0
  • Loading branch information
ps2 committed Apr 14, 2018
2 parents 882ba30 + 489575d commit ce82fb2
Show file tree
Hide file tree
Showing 181 changed files with 7,591 additions and 7,381 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ DerivedData
*.ipa
*.xcuserstate
*.xcscmblueprint
project.xcworkspace
.DS_Store
*.log

Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: objective-c
osx_image: xcode9
xcode_sdk: iphonesimulator11
osx_image: xcode9.3
xcode_project: RileyLink.xcodeproj
xcode_scheme: RileyLink
script:
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions MinimedKit/Extensions/NSData.swift → Common/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import Foundation


extension Data {
func to<T>(_: T.Type) -> T {
return self.withUnsafeBytes { $0.pointee }
func to<T: FixedWidthInteger>(_: T.Type) -> T {
return self.withUnsafeBytes { (bytes: UnsafePointer<T>) in
return T(littleEndian: bytes.pointee)
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions Common/NumberFormatter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// NumberFormatter.swift
// RileyLink
//
// Copyright © 2017 Pete Schwamb. All rights reserved.
//

import Foundation

extension NumberFormatter {
func decibleString(from decibles: Int?) -> String? {
if let decibles = decibles, let formatted = string(from: NSNumber(value: decibles)) {
return String(format: NSLocalizedString("%@ dB", comment: "Unit format string for an RSSI value in decibles"), formatted)
} else {
return nil
}
}
}
44 changes: 44 additions & 0 deletions Common/OSLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// OSLog.swift
// Loop
//
// Copyright © 2017 LoopKit Authors. All rights reserved.
//

import os.log


extension OSLog {
convenience init(category: String) {
self.init(subsystem: "com.ps2.rileylink", category: category)
}

func debug(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .debug, args)
}

func info(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .info, args)
}

func error(_ message: StaticString, _ args: CVarArg...) {
log(message, type: .error, args)
}

private func log(_ message: StaticString, type: OSLogType, _ args: [CVarArg]) {
switch args.count {
case 0:
os_log(message, log: self, type: type)
case 1:
os_log(message, log: self, type: type, args[0])
case 2:
os_log(message, log: self, type: type, args[0], args[1])
case 3:
os_log(message, log: self, type: type, args[0], args[1], args[2])
case 4:
os_log(message, log: self, type: type, args[0], args[1], args[2], args[3])
default:
os_log(message, log: self, type: type, args)
}
}
}
20 changes: 20 additions & 0 deletions Common/TimeInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import Foundation


extension TimeInterval {
static func hours(_ hours: Double) -> TimeInterval {
return self.init(hours: hours)
}

static func minutes(_ minutes: Int) -> TimeInterval {
return self.init(minutes: Double(minutes))
}
Expand All @@ -18,6 +22,14 @@ extension TimeInterval {
return self.init(minutes: minutes)
}

static func seconds(_ seconds: Double) -> TimeInterval {
return self.init(seconds)
}

static func milliseconds(_ milliseconds: Double) -> TimeInterval {
return self.init(milliseconds / 1000)
}

init(minutes: Double) {
self.init(minutes * 60)
}
Expand All @@ -26,6 +38,14 @@ extension TimeInterval {
self.init(minutes: hours * 60)
}

init(seconds: Double) {
self.init(seconds)
}

init(milliseconds: Double) {
self.init(milliseconds / 1000)
}

var milliseconds: Double {
return self * 1000
}
Expand Down
File renamed without changes.
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.2.2</string>
<string>2.0.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
3 changes: 3 additions & 0 deletions Crypto/es.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* (No Comment) */
"CFBundleName" = "$(PRODUCT_NAME)";

1 change: 1 addition & 0 deletions Crypto/ru.lproj/InfoPlist.strings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* No Localized Strings */
103 changes: 80 additions & 23 deletions MinimedKit/BasalSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public struct BasalScheduleEntry {
public let timeOffset: TimeInterval
public let rate: Double // U/hour

internal init(index: Int, timeOffset: TimeInterval, rate: Double) {
public init(index: Int, timeOffset: TimeInterval, rate: Double) {
self.index = index
self.timeOffset = timeOffset
self.rate = rate
Expand All @@ -23,37 +23,94 @@ public struct BasalScheduleEntry {

public struct BasalSchedule {
public let entries: [BasalScheduleEntry]

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


public init(entries: [BasalScheduleEntry]) {
self.entries = entries
}
}


extension BasalSchedule {
static let rawValueLength = 192
public typealias RawValue = Data

public init?(rawValue: RawValue) {
var entries = [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 + 3
guard endOfRange < data.count else {

guard endOfRange < rawValue.count else {
break
}

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

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
}

entries.append(entry)
} else {
// Stop if we can't decode the entry
break
}
}

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

let newBasalScheduleEntry = BasalScheduleEntry(
index: tuple.index,
timeOffset: TimeInterval(minutes: offsetMinutes),
rate: rate
)
acc.append(newBasalScheduleEntry)
guard entries.count > 0 else {
return nil
}
self.entries = acc

self.init(entries: entries)
}

public var rawValue: RawValue {
var buffer = Data(count: BasalSchedule.rawValueLength)
var byteIndex = 0

for rawEntry in entries.map({ $0.rawValue }) {
buffer.replaceSubrange(byteIndex..<(byteIndex + rawEntry.count), with: rawEntry)
byteIndex += rawEntry.count
}

return buffer
}
}


private extension BasalScheduleEntry {
static let rawValueLength = 3
typealias RawValue = Data

init?(index: Int, rawValue: RawValue) {
guard rawValue.count == BasalScheduleEntry.rawValueLength else {
return nil
}

let rawRate = rawValue[rawValue.startIndex..<rawValue.startIndex.advanced(by: 2)]
let rate = Double(rawRate.to(UInt16.self)) / 40.0

let offsetMinutes = Double(rawValue.last!) * 30
let timeOffset = TimeInterval(minutes: offsetMinutes)

guard timeOffset < .hours(24) else {
return nil
}

self.init(index: index, timeOffset: timeOffset, rate: rate)
}

var rawValue: RawValue {
var buffer = Data(count: type(of: self).rawValueLength)

var rate = UInt16(clamping: Int(self.rate * 40))
buffer.replaceSubrange(0..<2, with: UnsafeBufferPointer(start: &rate, count: 1))
buffer[2] = UInt8(clamping: Int(timeOffset.minutes / 30))

return buffer
}
}
23 changes: 9 additions & 14 deletions MinimedKit/CRC8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@

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, 0x3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, 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: Data) -> UInt8 {

var crc: UInt8 = 0
public extension Sequence where Element == UInt8 {

var pdata = (data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count)
var nbytes = data.count
/* loop over the buffer data */
while nbytes > 0 {
crc = crcTable[Int((crc ^ pdata.pointee) & 0xff)]
pdata = pdata.successor()
nbytes -= 1
public func crc8() -> UInt8 {
var crc: UInt8 = 0
for byte in self {
crc = crcTable[Int((crc ^ byte) & 0xff)]
}
return crc
}
return crc
}

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


extension Int {
init<T: Collection>(bigEndianBytes bytes: T) where T.Iterator.Element == UInt8, T.IndexDistance == Int {
init<T: Collection>(bigEndianBytes bytes: T) where T.Element == UInt8 {
assert(bytes.count <= 4)
var result: UInt = 0

Expand Down
1 change: 1 addition & 0 deletions MinimedKit/Extensions/NSDateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation


extension DateFormatter {
// TODO: Replace with Foundation.ISO8601DateFormatter
class func ISO8601DateFormatter() -> Self {
let formatter = self.init()
formatter.calendar = Calendar(identifier: Calendar.Identifier.iso8601)
Expand Down
69 changes: 69 additions & 0 deletions MinimedKit/FourByteSixByteEncoding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// FourByteSixByteEncoding.swift
// RileyLink
//
// Created by Pete Schwamb on 2/27/16.
// Copyright © 2016 Pete Schwamb. All rights reserved.
//

import Foundation

fileprivate let codes = [21, 49, 50, 35, 52, 37, 38, 22, 26, 25, 42, 11, 44, 13, 14, 28]

fileprivate let codesRev = Dictionary<Int, UInt8>(uniqueKeysWithValues: codes.enumerated().map({ ($1, UInt8($0)) }))

public extension Sequence where Element == UInt8 {

public func decode4b6b() -> [UInt8]? {
var buffer = [UInt8]()
var availBits = 0
var bitAccumulator = 0
for byte in self {
if byte == 0 {
break
}

bitAccumulator = (bitAccumulator << 8) + Int(byte)
availBits += 8
if availBits >= 12 {
guard let hiNibble = codesRev[bitAccumulator >> (availBits - 6)],
let loNibble = codesRev[(bitAccumulator >> (availBits - 12)) & 0b111111]
else {
return nil
}
let decoded = UInt8((hiNibble << 4) + loNibble)
buffer.append(decoded)
availBits -= 12
bitAccumulator = bitAccumulator & (0xffff >> (16-availBits))
}
}
return buffer
}

public func encode4b6b() -> [UInt8] {
var buffer = [UInt8]()
var bitAccumulator = 0x0
var bitcount = 0
for byte in self {
bitAccumulator <<= 6
bitAccumulator |= codes[Int(byte >> 4)]
bitcount += 6

bitAccumulator <<= 6
bitAccumulator |= codes[Int(byte & 0x0f)]
bitcount += 6

while bitcount >= 8 {
buffer.append(UInt8(bitAccumulator >> (bitcount-8)) & 0xff)
bitcount -= 8
bitAccumulator &= (0xffff >> (16-bitcount))
}
}
if bitcount > 0 {
bitAccumulator <<= (8-bitcount)
buffer.append(UInt8(bitAccumulator) & 0xff)
}
return buffer
}
}

Loading

0 comments on commit ce82fb2

Please sign in to comment.