8000 Decode multiple UART messages per notification by jjohnstz · Pull Request #19 · combustion-inc/combustion-ios-ble · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Decode multiple UART messages per notification #19

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

Merged
merged 1 commit into from
Jun 3, 2022
Merged
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
39 changes: 22 additions & 17 deletions Sources/CombustionBLE/BleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,7 @@ extension BleManager: CBPeripheralDelegate {
guard let data = characteristic.value else { return }

if characteristic.uuid == Constants.UART_TX_CHAR {
if let logResponse = Response.fromData(data) as? LogResponse {
if(logResponse.success) {
delegate?.updateDeviceWithLogResponse(identifier: peripheral.identifier, logResponse: logResponse)
}
}
else if let setIDResponse = Response.fromData(data) as? SetIDResponse {
delegate?.handleSetIDResponse(identifier: peripheral.identifier, success: setIDResponse.success)
}
else if let setColorResponse = Response.fromData(data) as? SetColorResponse {
delegate?.handleSetColorResponse(identifier: peripheral.identifier, success: setColorResponse.success)
}
else if let sessionResponse = Response.fromData(data) as? SessionInfoResponse {
if(sessionResponse.success) {
delegate?.updateDeviceWithSessionInformation(identifier: peripheral.identifier,
sessionInformation: sessionResponse.info)
}
}
handleUartData(data: data, identifier: peripheral.identifier)
}
else if characteristic.uuid == Constants.DEVICE_STATUS_CHAR {
if let status = DeviceStatus(fromData: data) {
Expand Down Expand Up @@ -284,4 +268,25 @@ extension BleManager: CBPeripheralDelegate {
}
}

private func handleUartData(data: Data, identifier: UUID) {
let responses = Response.fromData(data)

for response in responses {
if let logResponse = response as? LogResponse {
delegate?.updateDeviceWithLogResponse(identifier: identifier, logResponse: logResponse)
}
else if let setIDResponse = response as? SetIDResponse {
delegate?.handleSetIDResponse(identifier: identifier, success: setIDResponse.success)
}
else if let setColorResponse = response as? SetColorResponse {
delegate?.handleSetColorResponse(identifier: identifier, success: setColorResponse.success)
}
else if let sessionResponse = response as? SessionInfoResponse {
if(sessionResponse.success) {
delegate?.updateDeviceWithSessionInformation(identifier: identifier, sessionInformation: sessionResponse.info)
}
}
}
}

}
2 changes: 2 additions & 0 deletions Sources/CombustionBLE/DeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ extension DeviceManager : BleManagerDelegate {
}

func updateDeviceWithLogResponse(identifier: UUID, logResponse: LogResponse) {
guard logResponse.success else { return }

if let probe = devices[identifier.uuidString] as? Probe {
probe.processLogResponse(logResponse: logResponse)
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/CombustionBLE/UART/LogResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import Foundation

class LogResponse: Response {

static let PAYLOAD_LENGTH = 17

let sequenceNumber: UInt32
let temperatures: ProbeTemperatures

Expand All @@ -45,6 +47,6 @@ class LogResponse: Response {
// print("******** Received response!")
// print("Sequence = \(sequenceNumber) : Temperature = \(temperatures)")

super.init(success: success)
super.init(success: success, payLoadLength: LogResponse.PAYLOAD_LENGTH)
}
}
40 changes: 33 additions & 7 deletions Sources/CombustionBLE/UART/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,36 @@ class Response {
static let HEADER_LENGTH = 7

let success: Bool
let payLoadLength: Int

init(success: Bool) {
init(success: Bool, payLoadLength: Int) {
self.success = success
self.payLoadLength = payLoadLength
}
}

extension Response {
static func fromData(_ data : Data) -> Response? {
// print()
// print("*** Parsing UART response")
static func fromData(_ data : Data) -> [Response] {
var responses = [Response]()

var numberBytesRead = 0

while(numberBytesRead < data.count) {
let bytesToDecode = data.subdata(in: numberBytesRead..<data.count)
if let response = responseFromData(bytesToDecode) {
responses.append(response)
numberBytesRead += (response.payLoadLength + Response.HEADER_LENGTH)
}
else {
// Found invalid response, break out of while loop
break
}
}

return responses
}

private static func responseFromData(_ data : Data) -> Response? {
// Sync bytes
let syncBytes = data.subdata(in: 0..<2)
let syncString = syncBytes.reduce("") {$0 + String(format: "%02x", $1)}
Expand Down Expand Up @@ -72,17 +92,23 @@ extension Response {
let payloadLength = lengthByte.withUnsafeBytes {
$0.load(as: UInt8.self)
}
_ = payloadLength // Suppress 'unused variable' warning

let responseLength = Int(payloadLength) + HEADER_LENGTH

// Invalid number of bytes
if(data.count < responseLength) {
return nil
}

// print("Success: \(success), payloadLength: \(payloadLength)")

switch messageType {
case .Log:
return LogResponse(data: data, success: success)
case .SetID:
return SetIDResponse(success: success)
return SetIDResponse(success: success, payLoadLength: Int(payloadLength))
case .SetColor:
return SetColorResponse(success: success)
return SetColorResponse(success: success, payLoadLength: Int(payloadLength))
case .SessionInfo:
return SessionInfoResponse(data: data, success: success)
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/CombustionBLE/UART/SessionInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class SessionInfoRequest: Request {
}

class SessionInfoResponse: Response {
static let PAYLOAD_LENGTH = 6

let info: SessionInformation

init(data: Data, success: Bool) {
Expand All @@ -56,6 +58,6 @@ class SessionInfoResponse: Response {

info = SessionInformation(sessionID: sessionID, samplePeriod: samplePeriod)

super.init(success: success)
super.init(success: success, payLoadLength: SessionInfoResponse.PAYLOAD_LENGTH)
}
}
0