Skip to content

Commit

Permalink
Add messages for checking MySentry pairing status (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 authored May 28, 2018
1 parent 3a57ddc commit d127771
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 21 deletions.
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
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
37 changes: 37 additions & 0 deletions MinimedKitTests/Messages/ReadOtherDevicesIDsMessageBodyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ReadOtherDevicesIDsMessageBodyTests.swift
// MinimedKitTests
//
// Copyright © 2018 Pete Schwamb. All rights reserved.
//

import XCTest
@testable import MinimedKit

class ReadOtherDevicesIDsMessageBodyTests: XCTestCase {

func test0IDs() {
let message = PumpMessage(rxData: Data(hexadecimalString: "a7594040f01f0015036800406001070636036f0040600107062f1dfc004020c107062f0e77000000000000000000000000000000000000000000000000000000000000000000")!)
let body = message?.messageBody as! ReadOtherDevicesIDsMessageBody

XCTAssertEqual(0, body.ids.count)
}

func test1IDs() {
let message = PumpMessage(rxData: Data(hexadecimalString: "a7594040f01f0101a21057280000000636036f0040600107062f1dfc004020c107062f0e77000000000000000000000000000000000000000000000000000000000000000000")!)
let body = message?.messageBody as! ReadOtherDevicesIDsMessageBody

XCTAssertEqual(1, body.ids.count)
XCTAssertEqual("a2105728", body.ids[0].hexadecimalString)
}

func test2IDs() {
let message = PumpMessage(rxData: Data(hexadecimalString: "a7594040f01f0201a210572800a2016016036f0040600107062f1dfc004020c107062f0e77000000000000000000000000000000000000000000000000000000000000000000")!)
let body = message?.messageBody as! ReadOtherDevicesIDsMessageBody

XCTAssertEqual(2, body.ids.count)
XCTAssertEqual("a2105728", body.ids[0].hexadecimalString)
XCTAssertEqual("a2016016", body.ids[1].hexadecimalString)
}

}
15 changes: 14 additions & 1 deletion NightscoutUploadKit/NightscoutUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,26 @@ public class NightscoutUploader {
case .flat:
return "Flat"
}
}()
}()

let entry = NightscoutEntry(glucose: glucose, timestamp: sensorDate, device: device, glucoseType: .Sensor, previousSGV: previousSGV, previousSGVNotActive: previousSGVNotActive, direction: direction)
entries.append(entry)
}
flushAll()
}

public func uploadSGV(glucoseMGDL: Int, at date: Date, direction: String?, device: String) {
let entry = NightscoutEntry(
glucose: glucoseMGDL,
timestamp: date,
device: device,
glucoseType: .Sensor,
previousSGV: nil,
previousSGVNotActive: nil,
direction: direction
)
entries.append(entry)
}

public func handleMeterMessage(_ msg: MeterMessage) {

Expand Down
32 changes: 23 additions & 9 deletions RileyLink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
43D5E7A21FAF7CF2004ACDB7 /* CaseCountable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1C659181E16BA9D0025CC58 /* CaseCountable.swift */; };
43D5E7A31FAF7D05004ACDB7 /* CBPeripheralState.swift in Sources */ = {isa = PBXBuildFile; fileRef = C170C98D1CECD6F300F3D8E5 /* CBPeripheralState.swift */; };
43D5E7A41FAF7D4D004ACDB7 /* TimeZone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4345D1CD1DA16AF300BAAD22 /* TimeZone.swift */; };
43DAD00220A6A470000F8529 /* ReadOtherDevicesStatusMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43DAD00120A6A470000F8529 /* ReadOtherDevicesStatusMessageBody.swift */; };
43DAD00420A6A677000F8529 /* ReadOtherDevicesIDsMessageBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43DAD00320A6A677000F8529 /* ReadOtherDevicesIDsMessageBody.swift */; };
43DAD00620A6B10A000F8529 /* ReadOtherDevicesIDsMessageBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43DAD00520A6B10A000F8529 /* ReadOtherDevicesIDsMessageBodyTests.swift */; };
43EBE4521EAD23C40073A0B5 /* TimeInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43EBE4501EAD238C0073A0B5 /* TimeInterval.swift */; };
43EBE4531EAD23CE0073A0B5 /* TimeInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43EBE4501EAD238C0073A0B5 /* TimeInterval.swift */; };
43EBE4541EAD23EC0073A0B5 /* TimeInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43EBE4501EAD238C0073A0B5 /* TimeInterval.swift */; };
Expand Down Expand Up @@ -583,6 +586,9 @@
43D5E78E1FAF7BFB004ACDB7 /* RileyLinkKitUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RileyLinkKitUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
43D5E7901FAF7BFB004ACDB7 /* RileyLinkKitUI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RileyLinkKitUI.h; sourceTree = "<group>"; };
43D5E7911FAF7BFB004ACDB7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
43DAD00120A6A470000F8529 /* ReadOtherDevicesStatusMessageBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadOtherDevicesStatusMessageBody.swift; sourceTree = "<group>"; };
43DAD00320A6A677000F8529 /* ReadOtherDevicesIDsMessageBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadOtherDevicesIDsMessageBody.swift; sourceTree = "<group>"; };
43DAD00520A6B10A000F8529 /* ReadOtherDevicesIDsMessageBodyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadOtherDevicesIDsMessageBodyTests.swift; sourceTree = "<group>"; };
43EBE4501EAD238C0073A0B5 /* TimeInterval.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TimeInterval.swift; sourceTree = "<group>"; };
43EC9DCA1B786C6200DB0D18 /* LaunchScreen.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LaunchScreen.xib; sourceTree = "<group>"; };
43F348051D596270009933DC /* HKUnit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HKUnit.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1216,6 +1222,7 @@
C14303171C97CC6B00A40450 /* GetPumpModelCarelinkMessageBodyTests.swift */,
C1EAD6D21C826C43006DBA60 /* MySentryPumpStatusMessageBodyTests.swift */,
541688DA1DB820BF005B1891 /* ReadCurrentGlucosePageMessageBodyTests.swift */,
43DAD00520A6B10A000F8529 /* ReadOtherDevicesIDsMessageBodyTests.swift */,
43A068EB1CF6BA6900F9EFE4 /* ReadRemainingInsulinMessageBodyTests.swift */,
C1EAD6D41C826C43006DBA60 /* ReadSettingsCarelinkMessageBodyTests.swift */,
43CA93301CB97191000026B5 /* ReadTempBasalCarelinkMessageBodyTests.swift */,
Expand Down Expand Up @@ -1567,6 +1574,7 @@
C1EAD6BD1C826B92006DBA60 /* CarelinkMessageBody.swift */,
43CA93281CB8CF22000026B5 /* ChangeTempBasalCarelinkMessageBody.swift */,
43CA932A1CB8CF76000026B5 /* ChangeTimeCarelinkMessageBody.swift */,
C1F0004B1EBE68A600F65163 /* DataFrameMessageBody.swift */,
C10AB08E1C855F34000F102E /* DeviceLinkMessageBody.swift */,
C10AB08C1C855613000F102E /* FindDeviceMessageBody.swift */,
C1711A5B1C953F3000CB25BD /* GetBatteryCarelinkMessageBody.swift */,
Expand All @@ -1579,17 +1587,18 @@
C1EAD6C11C826B92006DBA60 /* MySentryPumpStatusMessageBody.swift */,
C1EAD6C21C826B92006DBA60 /* PowerOnCarelinkMessageBody.swift */,
C14303151C97C98000A40450 /* PumpAckMessageBody.swift */,
C1A721611EC3E0500080FAD7 /* PumpErrorMessageBody.swift */,
541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */,
2FDE1A061E57B12D00B56A27 /* ReadCurrentPageNumberMessageBody.swift */,
43DAD00320A6A677000F8529 /* ReadOtherDevicesIDsMessageBody.swift */,
43DAD00120A6A470000F8529 /* ReadOtherDevicesStatusMessageBody.swift */,
C178845C1D4EF3D800405663 /* ReadPumpStatusMessageBody.swift */,
433568751CF67FA800FD9D54 /* ReadRemainingInsulinMessageBody.swift */,
541688DC1DB82213005B1891 /* ReadCurrentGlucosePageMessageBody.swift */,
C1EAD6C31C826B92006DBA60 /* ReadSettingsCarelinkMessageBody.swift */,
43CA932C1CB8CFA1000026B5 /* ReadTempBasalCarelinkMessageBody.swift */,
43CA932D1CB8CFA1000026B5 /* ReadTimeCarelinkMessageBody.swift */,
C1EAD6C41C826B92006DBA60 /* UnknownMessageBody.swift */,
2FDE1A061E57B12D00B56A27 /* ReadCurrentPageNumberMessageBody.swift */,
C1F0004B1EBE68A600F65163 /* DataFrameMessageBody.swift */,
C1A721611EC3E0500080FAD7 /* PumpErrorMessageBody.swift */,
43BF58AF1FF594CB00499C46 /* SelectBasalProfileMessageBody.swift */,
C1EAD6C41C826B92006DBA60 /* UnknownMessageBody.swift */,
);
path = Messages;
sourceTree = "<group>";
Expand Down Expand Up @@ -1919,10 +1928,12 @@
LastSwiftMigration = 0900;
};
C12EA236198B436800309FA4 = {
DevelopmentTeam = 57NRR26737;
LastSwiftMigration = 0900;
ProvisioningStyle = Automatic;
};
C12EA251198B436800309FA4 = {
DevelopmentTeam = 57NRR26737;
ProvisioningStyle = Automatic;
TestTargetID = C12EA236198B436800309FA4;
};
Expand Down Expand Up @@ -2177,6 +2188,7 @@
files = (
C1842C1C1C8FA45100DB42AC /* ChangeBGReminderOffsetPumpEvent.swift in Sources */,
C1842BD11C8FA3D200DB42AC /* AlarmClockReminderPumpEvent.swift in Sources */,
43DAD00220A6A470000F8529 /* ReadOtherDevicesStatusMessageBody.swift in Sources */,
C1842C1B1C8FA45100DB42AC /* ChangeBolusReminderEnablePumpEvent.swift in Sources */,
C1842BC51C8F897E00DB42AC /* BasalProfileStartPumpEvent.swift in Sources */,
54BC448C1DB47BEA00340EED /* SensorCalGlucoseEvent.swift in Sources */,
Expand Down Expand Up @@ -2227,6 +2239,7 @@
54BC449C1DB483F700340EED /* RelativeTimestampedGlucoseEvent.swift in Sources */,
C1330F431DBDA46400569064 /* ChangeSensorAlarmSilenceConfigPumpEvent.swift in Sources */,
C1F6EB871F89C3B100CFE393 /* CRC8.swift in Sources */,
43DAD00420A6A677000F8529 /* ReadOtherDevicesIDsMessageBody.swift in Sources */,
546A85D81DF7C83A00733213 /* SensorDataHighGlucoseEvent.swift in Sources */,
C1842C091C8FA45100DB42AC /* ChangeWatchdogEnablePumpEvent.swift in Sources */,
C1842BC91C8F968B00DB42AC /* BGReceivedPumpEvent.swift in Sources */,
Expand Down Expand Up @@ -2335,6 +2348,7 @@
43A068EC1CF6BA6900F9EFE4 /* ReadRemainingInsulinMessageBodyTests.swift in Sources */,
2F962EC81E7074E60070EFBD /* BolusNormalPumpEventTests.swift in Sources */,
C1EAD6D61C826C43006DBA60 /* MySentryPumpStatusMessageBodyTests.swift in Sources */,
43DAD00620A6B10A000F8529 /* ReadOtherDevicesIDsMessageBodyTests.swift in Sources */,
C1EAD6D81C826C43006DBA60 /* ReadSettingsCarelinkMessageBodyTests.swift in Sources */,
43B0ADC01D0FC03200AAD278 /* NSDateComponentsTests.swift in Sources */,
546A85D21DF7BD5F00733213 /* SensorErrorGlucoseEventTests.swift in Sources */,
Expand Down Expand Up @@ -3199,7 +3213,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 57NRR26737;
INFOPLIST_FILE = "RileyLink/RileyLink-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.rileylink.rlapp;
Expand All @@ -3220,7 +3234,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 57NRR26737;
INFOPLIST_FILE = "RileyLink/RileyLink-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.rileylink.rlapp;
Expand All @@ -3239,7 +3253,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 57NRR26737;
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
INFOPLIST_FILE = "RileyLinkTests/RileyLinkTests-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
Expand All @@ -3259,7 +3273,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 57NRR26737;
INFOPLIST_FILE = "RileyLinkTests/RileyLinkTests-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.rileylink.${PRODUCT_NAME:rfc1034identifier}";
Expand Down
4 changes: 2 additions & 2 deletions RileyLinkBLEKit/PeripheralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PeripheralManager: NSObject {
/// Any error surfaced during the active operation
private var commandError: Error?

unowned let central: CBCentralManager
private(set) weak var central: CBCentralManager?

let configuration: Configuration

Expand Down Expand Up @@ -168,7 +168,7 @@ extension PeripheralManager {
func runCommand(timeout: TimeInterval, command: () -> Void) throws {
// Prelude
dispatchPrecondition(condition: .onQueue(queue))
guard central.state == .poweredOn && peripheral.state == .connected else {
guard central?.state == .poweredOn && peripheral.state == .connected else {
throw PeripheralManagerError.notReady
}

Expand Down
4 changes: 2 additions & 2 deletions RileyLinkBLEKit/RileyLinkDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extension RileyLinkDevice {
}

public func readRSSI() {
guard case .connected = manager.peripheral.state, case .poweredOn = manager.central.state else {
guard case .connected = manager.peripheral.state, case .poweredOn? = manager.central?.state else {
return
}
manager.peripheral.readRSSI()
Expand Down Expand Up @@ -182,7 +182,7 @@ extension RileyLinkDevice {
return
}

guard case .connected = self.manager.peripheral.state, case .poweredOn = self.manager.central.state else {
guard case .connected = self.manager.peripheral.state, case .poweredOn? = self.manager.central?.state else {
return
}

Expand Down
Loading

0 comments on commit d127771

Please sign in to comment.