From e472c49eac1e8848ec94ef09d45288cf6e95db48 Mon Sep 17 00:00:00 2001 From: kkonteh97 <55326260+kkonteh97@users.noreply.github.com> Date: Sun, 14 Apr 2024 11:19:17 -0700 Subject: [PATCH] Adjusted parcer --- .../SwiftOBD2/Communication/bleManager.swift | 2 +- .../SwiftOBD2/Communication/mockManager.swift | 1 - Sources/SwiftOBD2/elm327.swift | 4 +++- Sources/SwiftOBD2/obd2service.swift | 20 +++++++++++++------ Sources/SwiftOBD2/parser.swift | 5 +++-- 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Sources/SwiftOBD2/Communication/bleManager.swift b/Sources/SwiftOBD2/Communication/bleManager.swift index cd63342..26e905e 100644 --- a/Sources/SwiftOBD2/Communication/bleManager.swift +++ b/Sources/SwiftOBD2/Communication/bleManager.swift @@ -155,7 +155,7 @@ class BLEManager: NSObject, CommProtocol { func didDiscoverServices(_ peripheral: CBPeripheral, error _: Error?) { for service in peripheral.services ?? [] { - print("Discovered service: \(service.uuid)") + logger.info("Discovered service: \(service.uuid.uuidString)") switch service { case CBUUID(string: "FFE0"): peripheral.discoverCharacteristics([CBUUID(string: "FFE1")], for: service) diff --git a/Sources/SwiftOBD2/Communication/mockManager.swift b/Sources/SwiftOBD2/Communication/mockManager.swift index 9d9bde5..08355bc 100644 --- a/Sources/SwiftOBD2/Communication/mockManager.swift +++ b/Sources/SwiftOBD2/Communication/mockManager.swift @@ -228,7 +228,6 @@ extension OBDCommand { return "05" + " " + hexTemp case .maf: let maf = Int.random(in: 0...655) * 100 - // (256A + B) / 100 let A = maf / 256 let B = maf % 256 diff --git a/Sources/SwiftOBD2/elm327.swift b/Sources/SwiftOBD2/elm327.swift index 4cc7dcb..924beef 100644 --- a/Sources/SwiftOBD2/elm327.swift +++ b/Sources/SwiftOBD2/elm327.swift @@ -373,7 +373,9 @@ extension ELM327 { for pidGetter in pidGetters { do { +// logger.info("Getting supported PIDs for \(pidGetter.properties.command)") let response = try await sendCommand(pidGetter.properties.command) +// logger.info("Response: \(response)") // find first instance of 41 plus command sent, from there we determine the position of everything else // Ex. // || || @@ -403,7 +405,7 @@ extension ELM327 { let ecuData = messages.first?.data else { throw NSError(domain: "Invalid data format", code: 0, userInfo: nil) } - let binaryData = BitArray(data: ecuData[1...]).binaryArray + let binaryData = BitArray(data: ecuData.dropFirst()).binaryArray return extractSupportedPIDs(binaryData) } diff --git a/Sources/SwiftOBD2/obd2service.swift b/Sources/SwiftOBD2/obd2service.swift index 5d52859..a39429f 100644 --- a/Sources/SwiftOBD2/obd2service.swift +++ b/Sources/SwiftOBD2/obd2service.swift @@ -1,10 +1,10 @@ import Combine import Foundation -public enum connectionType { - case bluetooth - case wifi - case demo +public enum ConnectionType: String, CaseIterable { + case bluetooth = "Bluetooth" + case wifi = "Wi-Fi" + case demo = "Demo" } public protocol OBDServiceDelegate: AnyObject { @@ -20,6 +20,12 @@ public protocol OBDServiceDelegate: AnyObject { /// - Managing the connection state. public class OBDService: ObservableObject, OBDServiceDelegate { @Published public var connectionState: ConnectionState = .disconnected + @Published public var connectionType: ConnectionType { + didSet { + self.switchConnectionType(connectionType) + UserDefaults.standard.set(connectionType.rawValue, forKey: "connectionType") + } + } /// The internal ELM327 object responsible for direct adapter interaction. private var elm327: ELM327 @@ -27,7 +33,7 @@ public class OBDService: ObservableObject, OBDServiceDelegate { /// Initializes the OBDService object. /// /// - Parameter connectionType: The desired connection type (default is Bluetooth). - public init(connectionType: connectionType = .bluetooth) { + public init(connectionType: ConnectionType = .bluetooth) { #if targetEnvironment(simulator) elm327 = ELM327(comm: MOCKComm()) #else @@ -40,6 +46,7 @@ public class OBDService: ObservableObject, OBDServiceDelegate { elm327 = ELM327(comm: MOCKComm()) } #endif + self.connectionType = connectionType elm327.obdDelegate = self } @@ -86,7 +93,7 @@ public class OBDService: ObservableObject, OBDServiceDelegate { /// Switches the active connection type (between Bluetooth and Wi-Fi). /// /// - Parameter connectionType: The new desired connection type. - public func switchConnectionType(_ connectionType: connectionType) { + public func switchConnectionType(_ connectionType: ConnectionType) { switch connectionType { case .bluetooth: elm327 = ELM327(comm: BLEManager()) @@ -95,6 +102,7 @@ public class OBDService: ObservableObject, OBDServiceDelegate { case .demo: elm327 = ELM327(comm: MOCKComm()) } + elm327.obdDelegate = self } // MARK: - Request Handling diff --git a/Sources/SwiftOBD2/parser.swift b/Sources/SwiftOBD2/parser.swift index 3d78072..cbb6d30 100644 --- a/Sources/SwiftOBD2/parser.swift +++ b/Sources/SwiftOBD2/parser.swift @@ -75,15 +75,16 @@ public struct Message { } private func parseSingleFrameMessage(_ frames: [Frame]) -> Data? { + guard let frame = frames.first, frame.type == .singleFrame, let dataLen = frame.dataLen, dataLen > 0, - frame.data.count >= 2 + Int(dataLen) + frame.data.count == dataLen + 1 else { // Pre-validate the length print("Failed to parse single frame message") print("frame: \(frames.first)") return nil } - return frame.data.subdata(in: 2 ..< (2 + Int(dataLen))) // Using Substring + return frame.data.dropFirst(2) } private func parseMultiFrameMessage(_ frames: [Frame]) -> Data? {