Skip to content

Draft: l2 pairing using mac address instead of serialnumber #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Bluetooth/LibreTransmitterMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public enum SensorType: String, CustomStringConvertible {
case 0xDF, 0xA2: self = .libre1
case 0xE5, 0xE6: self = .libreUS14day
case 0x70: self = .libreProH
case 0xC5, 0x9D, 0xC6: self = .libre2
case 0xC5, 0x9D, 0xC6, 0x7F: self = .libre2
case 0x76: self = patchInfo[3] == 0x02 ? .libre2US : patchInfo[3] == 0x04 ? .libre2CA : patchInfo[2] >> 4 == 7 ? .libreSense : .unknown
default:
if patchInfo.count == 24 {
Expand Down
24 changes: 22 additions & 2 deletions Bluetooth/Transmitter/Libre2DirectTransmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,27 @@ class Libre2DirectTransmitter: LibreTransmitterProxyProtocol {
private var metadata: LibreTransmitterMetadata?

class func canSupportPeripheral(_ peripheral: PeripheralProtocol) -> Bool {
peripheral.name?.lowercased().starts(with: "abbott") ?? false
// name can be one of the following formats:
// <description>: example
//ABBOT<SerialNumber>: ABBOTT3MH015PCNC4
// <MACADDRESS>: A4B7C9023F8D

guard let name = peripheral.name else {
return false
}

if name.lowercased().starts(with: "abbott") == true {
print("Libre 2 detected using legacy name format as matcher")
return true
}

print("Libre 2 detection using MAC address as matcher: \(UserDefaults.standard.preSelectedSensor?.macAddress?.lowercased()) vs \(name.lowercased())")
if let sensor = UserDefaults.standard.preSelectedSensor, let macAddress = sensor.macAddress {
return name.lowercased().contains(macAddress.lowercased())
}


return false
}

class func getDeviceDetailsFromAdvertisement(advertisementData: [String: Any]?) -> String? {
Expand Down Expand Up @@ -143,7 +163,7 @@ class Libre2DirectTransmitter: LibreTransmitterProxyProtocol {

metadata = LibreTransmitterMetadata(hardware: nil, firmware: nil, battery: 100,
name: Self.shortTransmitterName,
macAddress: nil,
macAddress: sensor.macAddress,
patchInfo: sensor.patchInfo,
uid: [UInt8](sensor.uuid))

Expand Down
3 changes: 2 additions & 1 deletion Bluetooth/Transmitter/LibreTransmitterProxyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public protocol LibreTransmitterProxyProtocol: AnyObject {

extension LibreTransmitterProxyProtocol {
func canSupportPeripheral(_ peripheral: PeripheralProtocol) -> Bool {
Self.canSupportPeripheral(peripheral)
print("canSupportPeripheral called")
return Self.canSupportPeripheral(peripheral)
}
public var staticType: LibreTransmitterProxyProtocol.Type {
Self.self
Expand Down
49 changes: 3 additions & 46 deletions LibreSensor/SensorContents/Sensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,66 +80,23 @@ public struct CalibrationToSensorMapping: Codable {
public struct Sensor: Codable {
public let uuid: Data
public let patchInfo: Data
// public let calibrationInfo: SensorData.CalibrationInfo

// public let family: SensorFamily
// public let type: SensorType
// public let region: SensorRegion
// public let serial: String?
// public var state: SensorState
public var age: Int?
public var maxAge: Int
// public var lifetime: Int

public var unlockCount: Int

var sensorName : String?
var macAddress : String?

/*
public var unlockCount: Int {
get {
return UserDefaults.standard.integer(forKey: Key.sensorUnlockCount.rawValue)
}
set {
UserDefaults.standard.setValue(newValue, forKey: Key.sensorUnlockCount.rawValue)
}
}*/

/*
public var elapsedLifetime: Int? {
get {
if let remainingLifetime {
return max(0, lifetime - remainingLifetime)
}

return nil
}
}

public var remainingLifetime: Int? {
get {
if let age {
return max(0, lifetime - age)
}

return nil
}
} */

public init(uuid: Data, patchInfo: Data, maxAge: Int, unlockCount: Int = 0, sensorName: String? = nil) {
public init(uuid: Data, patchInfo: Data, maxAge: Int, unlockCount: Int = 0, sensorName: String? = nil, macAddress: String? = nil) {
self.uuid = uuid
self.patchInfo = patchInfo

// self.family = SensorFamily(patchInfo: patchInfo)
// self.type = SensorType(patchInfo: patchInfo)
// self.region = SensorRegion(patchInfo: patchInfo)
// self.serial = sensorSerialNumber(sensorUID: self.uuid, sensorFamily: self.family)
// self.state = SensorState(fram: fram)
// self.lifetime = Int(fram[327]) << 8 + Int(fram[326])
self.unlockCount = 0
self.maxAge = maxAge
// self.calibrationInfo = calibrationInfo
self.sensorName = sensorName
self.macAddress = macAddress
}

public var description: String {
Expand Down
8 changes: 6 additions & 2 deletions LibreSensor/SensorPairing/SensorPairing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public class SensorPairingInfo: ObservableObject, Codable {
@Published public var streamingEnabled: Bool

@Published public var sensorName : String? = nil
@Published public var macAddress : String? = nil

enum CodingKeys: CodingKey {
case uuid, patchInfo, fram, streamingEnabled, sensorName
case uuid, patchInfo, fram, streamingEnabled, sensorName, macAddress
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
Expand All @@ -27,6 +28,7 @@ public class SensorPairingInfo: ObservableObject, Codable {
try container.encode(fram, forKey: .fram)
try container.encode(streamingEnabled, forKey: .streamingEnabled)
try container.encode(sensorName, forKey: .sensorName)
try container.encode(macAddress, forKey: .macAddress)


}
Expand All @@ -39,15 +41,17 @@ public class SensorPairingInfo: ObservableObject, Codable {
fram = try container.decode(Data.self, forKey: .fram)
streamingEnabled = try container.decode(Bool.self, forKey: .streamingEnabled)
sensorName = try container.decode(String?.self, forKey: .sensorName)
macAddress = try container.decode(String?.self, forKey: .macAddress)
}


public init(uuid: Data=Data(), patchInfo: Data=Data(), fram: Data=Data(), streamingEnabled: Bool = false, sensorName: String? = nil ) {
public init(uuid: Data=Data(), patchInfo: Data=Data(), fram: Data=Data(), streamingEnabled: Bool = false, sensorName: String? = nil, macAddress: String? = nil ) {
self.uuid = uuid
self.patchInfo = patchInfo
self.fram = fram
self.streamingEnabled = streamingEnabled
self.sensorName = sensorName
self.macAddress = macAddress
}

public var sensorData: SensorData? {
Expand Down
6 changes: 4 additions & 2 deletions LibreSensor/SensorPairing/SensorPairingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,19 @@ public class SensorPairingService: NSObject, NFCTagReaderSessionDelegate, Sensor

tag.customCommand(requestFlags: .highDataRate, customCommandCode: Int(cmd.code), customRequestParameters: cmd.parameters) { response, _ in
var streamingEnabled = false
var macAddress : String?

if subCmd == .enableStreaming && response.count == 6 {
streamingEnabled = true
macAddress = Data(response.reversed()).hexEncodedString().uppercased()
}



let patchHex = patchInfo.hexEncodedString()
let sensorType = SensorType(patchInfo: patchInfo)

print("got patchhex: \(patchHex) and sensorType: \(sensorType)")
print("got patchhex: \(patchHex) and sensorType: \(sensorType), with mac address: \(macAddress)")

guard sensorUID.count == 8 && patchInfo.count == 6 && fram.count == 344 else {
// self.readingsSubject.send(completion: .failure(LibreError.noSensorData))
Expand All @@ -199,7 +201,7 @@ public class SensorPairingService: NSObject, NFCTagReaderSessionDelegate, Sensor
do {
let decryptedBytes = try Libre2.decryptFRAM(type: sensorType, id: [UInt8](sensorUID), info: patchInfo, data: [UInt8](fram))

self.sendUpdate(SensorPairingInfo(uuid: sensorUID, patchInfo: patchInfo, fram: Data(decryptedBytes), streamingEnabled: streamingEnabled))
self.sendUpdate(SensorPairingInfo(uuid: sensorUID, patchInfo: patchInfo, fram: Data(decryptedBytes), streamingEnabled: streamingEnabled, macAddress: macAddress))
session.invalidate()
return
} catch {
Expand Down
2 changes: 1 addition & 1 deletion LibreTransmitterUI/Views/Setup/Libre2DirectSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct Libre2DirectSetup: View {

let max = info.sensorData?.maxMinutesWearTime ?? 0

let sensor = Sensor(uuid: info.uuid, patchInfo: info.patchInfo, maxAge: max, sensorName: info.sensorName)
let sensor = Sensor(uuid: info.uuid, patchInfo: info.patchInfo, maxAge: max, sensorName: info.sensorName, macAddress: info.macAddress)
UserDefaults.standard.preSelectedSensor = sensor

SelectionState.shared.selectedUID = pairingInfo.uuid
Expand Down