main.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // main.swift
  3. // OmniBLEParser
  4. //
  5. // Based on OmniKitPacketParser/main.swift
  6. // Created by Joseph Moran on 02/02/23.
  7. // Copyright © 2023 LoopKit Authors. All rights reserved.
  8. //
  9. import Foundation
  10. // These options can be forced off by using the -q option argument
  11. fileprivate var printDate: Bool = true // whether to print the date (when available) along with the time (when available)
  12. fileprivate var printFullMessage: Bool = true // whether to print full message decode including the address and seq
  13. //from NSHipster - http://nshipster.com/swift-literal-convertible/
  14. struct Regex {
  15. let pattern: String
  16. let options: NSRegularExpression.Options!
  17. private var matcher: NSRegularExpression {
  18. return try! NSRegularExpression(pattern: self.pattern, options: self.options)
  19. }
  20. init(_ pattern: String, options: NSRegularExpression.Options = []) {
  21. self.pattern = pattern
  22. self.options = options
  23. }
  24. func match(string: String, options: NSRegularExpression.MatchingOptions = []) -> Bool {
  25. return self.matcher.numberOfMatches(in: string, options: options, range: NSMakeRange(0, string.count)) != 0
  26. }
  27. }
  28. protocol RegularExpressionMatchable {
  29. func match(regex: Regex) -> Bool
  30. }
  31. extension String: RegularExpressionMatchable {
  32. func match(regex: Regex) -> Bool {
  33. return regex.match(string: self)
  34. }
  35. }
  36. func ~=<T: RegularExpressionMatchable>(pattern: Regex, matchable: T) -> Bool {
  37. return matchable.match(regex: pattern)
  38. }
  39. func printDecoded(timeStr: String, hexString: String)
  40. {
  41. guard let data = Data(hexadecimalString: hexString), data.count >= 10 else {
  42. print("Bad hex string: \(hexString)")
  43. return
  44. }
  45. do {
  46. // The block type is right after the 4-byte address and the B9 and BLEN bytes
  47. guard let blockType = MessageBlockType(rawValue: data[6]) else {
  48. throw MessageBlockError.unknownBlockType(rawVal: data[6])
  49. }
  50. let type: String
  51. let checkCRC: Bool
  52. switch blockType {
  53. case .statusResponse, .podInfoResponse, .versionResponse, .errorResponse:
  54. type = "RESPONSE: "
  55. // Don't currently understand how to check the CRC16 the DASH pods generate
  56. checkCRC = false
  57. default:
  58. type = "COMMAND: "
  59. checkCRC = true
  60. }
  61. let message = try Message(encodedData: data, checkCRC: checkCRC)
  62. if printFullMessage {
  63. // print the complete message with the address and seq
  64. print("\(type)\(timeStr) \(message)")
  65. } else {
  66. // skip printing the address and seq for each message
  67. print("\(type)\(timeStr) \(message.messageBlocks)")
  68. }
  69. } catch let error {
  70. print("Could not parse \(hexString): \(error)")
  71. }
  72. }
  73. // * 2022-04-05 06:56:14 +0000 Omnipod-Dash 17CAE1DD send 17cae1dd00030e010003b1
  74. // * 2022-04-05 06:56:14 +0000 Omnipod-Dash 17CAE1DD receive 17cae1dd040a1d18002ab00000019fff0198
  75. func parseLoopReportLine(_ line: String) {
  76. let components = line.components(separatedBy: .whitespaces)
  77. let hexString = components[components.count - 1]
  78. let date = components[1]
  79. let time = components[2]
  80. let timeStr = printDate ? date + " " + time : time
  81. printDecoded(timeStr: timeStr, hexString: hexString)
  82. }
  83. // 2023-02-02 15:23:13.094289-0800 Loop[60606:22880823] [PodMessageTransport] Send(Hex): 1776c2c63c030e010000a0
  84. // 2023-02-02 15:23:13.497849-0800 Loop[60606:22880823] [PodMessageTransport] Recv(Hex): 1776c2c6000a1d180064d800000443ff0000
  85. func parseLoopXcodeLine(_ line: String) {
  86. let components = line.components(separatedBy: .whitespaces)
  87. let hexString = components[components.count - 1]
  88. let date = components[0]
  89. let time = components[1].padding(toLength: 15, withPad: " ", startingAt: 0) // skip the -0800 portion
  90. let timeStr = printDate ? date + " " + time : time
  91. printDecoded(timeStr: timeStr, hexString: hexString)
  92. }
  93. // N.B. Simulator output typically has a space after the hex string!
  94. // INFO[7699] pkg command; 0x0e; GET_STATUS; HEX, 1776c2c63c030e010000a0
  95. // INFO[7699] pkg response 0x1d; HEX, 1776c2c6000a1d280064e80000057bff0000
  96. // INFO[2023-09-04T18:17:06-07:00] pkg command; 0x07; GET_VERSION; HEX, ffffffff00060704ffffffff82b2
  97. // INFO[2023-09-04T18:17:06-07:00] pkg response 0x1; HEX, ffffffff04170115040a00010300040208146db10006e45100ffffffff0000
  98. func parseSimulatorLogLine(_ line: String) {
  99. let components = line.components(separatedBy: .whitespaces)
  100. var hexStringIndex = components.count - 1
  101. let hexString: String
  102. if components[hexStringIndex].isEmpty {
  103. hexStringIndex -= 1 // back up to handle a trailing space
  104. }
  105. hexString = components[hexStringIndex]
  106. let c0 = components[0]
  107. // start at 5 for printDate or shorter "INFO[7699]" format
  108. let offset = printDate || c0.count <= 16 ? 5 : 16
  109. let startIndex = c0.index(c0.startIndex, offsetBy: offset)
  110. let endIndex = c0.index(c0.startIndex, offsetBy: c0.count - 2)
  111. let timeStr = String(c0[startIndex...endIndex])
  112. printDecoded(timeStr: timeStr, hexString: hexString)
  113. }
  114. // iAPS or Trio log file
  115. // iAPS_log 2024-05-08T00:03:57-0700 [DeviceManager] DeviceDataManager.swift - deviceManager(_:logEventForDeviceIdentifier:type:message:completion:) - 576 - DEV: Device message: 17ab48aa20071f05494e532e0201d5
  116. // iAPS or Trio Xcode log with timestamp
  117. // 2024-05-25 14:16:54.933281-0700 FreeAPS[2973:2299225] [DeviceManager] DeviceDataManager.swift - deviceManager(_:logEventForDeviceIdentifier:type:message:completion:) - 566 DEV: Device message: 170f1e3710080806494e532e000081ab
  118. // iAPS or Trio Xcode log with no timestamp
  119. // DeviceDataManager.swift - deviceManager(_:logEventForDeviceIdentifier:type:message:completion:) - 566 DEV: Device message: 170f1e3710080806494e532e000081ab
  120. func parseFreeAPSLogOrXcodeLine(_ line: String) {
  121. let components = line.components(separatedBy: .whitespaces)
  122. let hexString = components[components.count - 1]
  123. if components.count > 9 {
  124. // have a timestamp
  125. let date = components[0].prefix(10)
  126. let time: String
  127. if components.count == 12 {
  128. // iAPS or Trio log file with date and time joined with a "T", e.g., 2024-05-25T00:26:05-0700
  129. let dateAndTimeComponents = components[0].components(separatedBy: "T")
  130. time = dateAndTimeComponents[1].padding(toLength: 8, withPad: " ", startingAt: 0) // skip the -0700 portion
  131. } else {
  132. // Xcode log file with separate date and time, e.g., 2024-05-25 14:16:53.571361-0700
  133. time = components[1].padding(toLength: 15, withPad: " ", startingAt: 11) // skip the -0700 portion
  134. }
  135. let timeStr = printDate ? date + " " + time : time
  136. printDecoded(timeStr: timeStr, hexString: hexString)
  137. } else {
  138. // no timestamp
  139. printDecoded(timeStr: "", hexString: hexString)
  140. }
  141. }
  142. // 2020-11-04 13:38:34.256 1336 6945 I PodComm pod command: 08202EAB08030E01070319
  143. // 2020-11-04 13:38:34.979 1336 1378 V PodComm response (hex) 08202EAB0C0A1D9800EB80A400042FFF8320
  144. func parseDashPDMLogLine(_ line: String) {
  145. let components = line.components(separatedBy: .whitespaces)
  146. let hexString = components[components.count - 1]
  147. let date = components[0]
  148. let time = components[1]
  149. let timeStr = printDate ? date + " " + time : time
  150. printDecoded(timeStr: timeStr, hexString: hexString)
  151. }
  152. func usage() {
  153. print("Usage: [-q] file...")
  154. print("Set the Xcode Arguments Passed on Launch using Product->Scheme->Edit Scheme...")
  155. print("to specify the full path to Loop Report, Xcode log, pod simulator log, iAPS log, Trio log or DASH PDM log file(s) to parse.\n")
  156. exit(1)
  157. }
  158. if CommandLine.argc <= 1 {
  159. usage()
  160. }
  161. for arg in CommandLine.arguments[1...] {
  162. if arg == "-q" {
  163. printDate = false
  164. printFullMessage = false
  165. continue
  166. } else if arg.starts(with: "-") {
  167. // no other arguments curently supported
  168. usage()
  169. }
  170. print("\nParsing \(arg)")
  171. do {
  172. let data = try String(contentsOfFile: arg, encoding: .utf8)
  173. let lines = data.components(separatedBy: .newlines)
  174. for line in lines {
  175. switch line {
  176. // Loop Report file
  177. // * 2022-04-05 06:56:14 +0000 Omnipod-Dash 17CAE1DD send 17cae1dd00030e010003b1
  178. // * 2022-04-05 06:56:14 +0000 Omnipod-Dash 17CAE1DD receive 17cae1dd040a1d18002ab00000019fff0198
  179. case Regex("(send|receive) [0-9a-fA-F]+$"):
  180. parseLoopReportLine(line)
  181. // Loop Xcode log
  182. // 2023-02-02 15:23:13.094289-0800 Loop[60606:22880823] [PodMessageTransport] Send(Hex): 1776c2c63c030e010000a0
  183. // 2023-02-02 15:23:13.497849-0800 Loop[60606:22880823] [PodMessageTransport] Recv(Hex): 1776c2c6000a1d180064d800000443ff0000
  184. case Regex(" Loop\\[.*\\] \\[PodMessageTransport\\] (Send|Recv)\\(Hex\\): [0-9a-fA-F]+$"):
  185. parseLoopXcodeLine(line)
  186. // Simulator log file (N.B. typically has a trailing space!)
  187. // INFO[7699] pkg command; 0x0e; GET_STATUS; HEX, 1776c2c63c030e010000a0
  188. // INFO[7699] pkg response 0x1d; HEX, 1776c2c6000a1d280064e80000057bff0000
  189. case Regex("; HEX, [0-9a-fA-F]+ $"), Regex("; HEX, [0-9a-fA-F]+$"):
  190. parseSimulatorLogLine(line)
  191. // iAPS or Trio log file
  192. // iAPS_log 2024-05-08T00:03:57-0700 [DeviceManager] DeviceDataManager.swift - deviceManager(_:logEventForDeviceIdentifier:type:message:completion:) - 576 - DEV: Device message: 17ab48aa20071f05494e532e0201d5
  193. // iAPS or Trio Xcode log with timestamp
  194. // 2024-05-25 14:16:54.933281-0700 FreeAPS[2973:2299225] [DeviceManager] DeviceDataManager.swift - deviceManager(_:logEventForDeviceIdentifier:type:message:completion:) - 566 DEV: Device message: 170f1e3710080806494e532e000081ab
  195. // iAPS or Trio Xcode log with no timestamp
  196. // DeviceDataManager.swift - deviceManager(_:logEventForDeviceIdentifier:type:message:completion:) - 566 DEV: Device message: 170f1e3710080806494e532e000081ab
  197. case Regex("Device message: [0-9a-fA-F]+$"):
  198. parseFreeAPSLogOrXcodeLine(line)
  199. // DASH PDM log file
  200. // 2020-11-04 21:35:52.218 1336 1378 I PodComm pod command: 08202EAB30030E010000BC
  201. // 2020-11-04 21:35:52.575 1336 6945 V PodComm response (hex) 08202EAB340A1D18018D2000000BA3FF81D9
  202. case Regex("I PodComm pod command: "), Regex("V PodComm response \\(hex\\) "):
  203. parseDashPDMLogLine(line)
  204. default:
  205. break
  206. }
  207. }
  208. } catch let error {
  209. print("Error: \(error)")
  210. }
  211. print("\n")
  212. }