-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathULogMessage.swift
516 lines (430 loc) · 14.9 KB
/
ULogMessage.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//
// ULogMessage.swift
// ulogReader
//
// Created by Gustaf Kugelberg on 2017-08-11.
// Copyright © 2017 Andreas Okholm. All rights reserved.
//
import Foundation
/*
All the data
HEADER:
16 bytes - magic + timestamp
DEFINITIONS:
{
3 bytes - header (type = format, size)
n bytes - data
}
{
3 bytes - header (type = info, size)
n bytes - data
}
DATA:
{
3 bytes - header (type = addLoggedMessage, size)
n bytes - data
}
{
3 bytes - header (type = data, size)
2 bytes - id
n bytes - data
}
*/
enum MessageType: Character {
case format = "F"
case data = "D"
case info = "I"
case infoMultiple = "M"
case parameter = "P"
case addLoggedMessage = "A"
case removeLoggedMessage = "R"
case sync = "S"
case dropout = "O"
case logging = "L"
case flagBits = "B"
}
struct MessageHeader: CustomStringConvertible {
let size: UInt16
let type: MessageType
var description: String {
return "MessageHeader(size \(size), type: \(type))"
}
init?(ptr: UnsafeRawPointer) {
size = ptr.assumingMemoryBound(to: UInt16.self).pointee // size = ptr.load(as: UInt16.self) works the first time, but not the second !
guard let mt = MessageType(rawValue: ptr.load(fromByteOffset: 2, as: UInt8.self).character) else {
print("Header error: \(Character(UnicodeScalar(ptr.load(fromByteOffset: 2, as: UInt8.self))))")
return nil
}
type = mt
}
}
struct MessageInfo: CustomStringConvertible {
let header: MessageHeader
let keyLength: UInt8
let key: String
let value: UlogValue
var description: String {
return "MessageInfo(keyLength: \(keyLength), key \(key), typeName: \(value.typeName)):\nValue:\n\(value)"
}
init?(data: Data, header: MessageHeader) {
self.header = header
keyLength = data.value()
let typeAndName = data.subdata(in: 1..<Int(1 + keyLength)).asString().components(separatedBy: " ")
let dataValue = data.subdata(in: 1 + Int(keyLength)..<Int(header.size))
value = UlogValue(type: UlogType(typeName: typeAndName[0])!, data: dataValue)
key = typeAndName[1]
}
}
struct MessageParameter {
let header: MessageHeader
let keyLength: UInt8
let key: String
let value: UlogValue
init(data: Data, header: MessageHeader) {
self.header = header
keyLength = data.value()
let typeAndName = data.subdata(in: 1..<(1+Int(keyLength))).asString()
let typeNName = typeAndName.components(separatedBy: " ")
let dataValue = data.subdata(in: 1 + Int(keyLength)..<Int(header.size))
value = UlogValue(type: UlogType(typeName: typeNName.first!)!, data: dataValue)
key = typeNName[1]
}
}
struct MessageAddLoggedMessage {
let header: MessageHeader
let multi_id: UInt8
let id: UInt16
let messageName: String
init(data: Data, header: MessageHeader) {
self.header = header
multi_id = data[0]
id = data.advanced(by: 1).value()
messageName = data.subdata(in: 3..<Int(header.size) ).asString()
}
}
struct MessageData {
let header: MessageHeader
let id: UInt16
let data: Data
init(data: Data, header: MessageHeader) {
self.header = header
id = data.value()
self.data = data.advanced(by: 2)
}
}
struct MessageLog {
let header: MessageHeader
let level: UInt8
let timestamp: UInt64
let message: String
init(data: Data, header: MessageHeader) {
self.header = header
level = data[0]
timestamp = data.advanced(by: 1).value()
message = data.subdata(in: 7..<Int(header.size)).asString()
}
}
struct MessageDropout {
let header: MessageHeader
let duration: UInt16
init(data: Data, header: MessageHeader) {
self.header = header
duration = data.value()
}
}
extension UInt8 {
var character: Character {
return Character(UnicodeScalar(self))
}
}
// MARK: - To be removed
struct MessageFormat {
let header: MessageHeader
let format: String
init?(data: Data, header: MessageHeader) {
self.header = header
format = data.subdata(in: 0..<Int(header.size)).asString()
}
var messageName: String {
return format.substring(to: format.range(of: ":")!.lowerBound)
}
var formatsProcessed: [(String, UlogType)] {
return format
.substring(from: format.range(of: ":")!.upperBound)
.components(separatedBy: ";")
.filter { $0.characters.count > 0 }
.map { split(s: $0) }
.filter { $0.0 != "_padding0" }
}
func split(s: String) -> (String, UlogType) {
let x = s.components(separatedBy: " ")
let typeString = x.first!
let variableName = x[1]
let ulogtype = UlogType(typeName: typeString)!
return (variableName, ulogtype)
}
}
enum UlogType {
case uint8
case uint16
case uint32
case uint64
case int8
case int16
case int32
case int64
case float
case double
case bool
case string
case array([UlogType])
init?(typeName: String) {
let (name, count) = UlogType.nameAndNumber(typeName)
if let count = count {
self = name == "char" ? .string : .array(Array(repeating: UlogType(typeName: name)!, count: Int(count)))
}
else {
switch typeName {
case "uint8_t": self = .uint8
case "uint16_t": self = .uint16
case "uint32_t": self = .uint32
case "uint64_t": self = .uint64
case "int8_t": self = .int8
case "int16_t": self = .int16
case "int32_t": self = .int32
case "int64_t": self = .int64
case "float": self = .float
case "double": self = .double
case "bool": self = .bool
default: self = .bool
}
}
}
var byteCount: Int {
switch self {
case .int8: return 1
case .uint8: return 1
case .int16: return 2
case .uint16: return 2
case .int32: return 4
case .uint32: return 4
case .int64: return 8
case .uint64: return 8
case .float: return 4
case .double: return 8
case .bool: return 1
case .string: return 0 // Should not be ussed
case .array(let array): return array.reduce(0) { $0 + $1.byteCount } // Should not be ussed
}
}
private static func nameAndNumber(_ formatString: String) -> (name: String, number: UInt?) {
let parts = formatString.components(separatedBy: ["[", "]"])
guard parts.count == 3, let count = UInt(parts[1]) else {
return (parts[0], nil)
}
return (parts[0], count)
}
}
enum UlogValue: CustomStringConvertible {
case uint8(UInt8)
case uint16(UInt16)
case uint32(UInt32)
case uint64(UInt64)
case int8(Int8)
case int16(Int16)
case int32(Int32)
case int64(Int64)
case float(Float)
case double(Double)
case bool(Bool)
case string(String)
case array([UlogValue])
init(type: UlogType, data: Data) {
// print("Type name :\(type)")
switch type {
case .int8: self = .int8(data.value())
case .uint8: self = .uint8(data.value())
case .int16: self = .int16(data.value())
case .uint16: self = .uint16(data.value())
case .int32: self = .int32(data.value())
case .uint32: self = .uint32(data.value())
case .int64: self = .int64(data.value())
case .uint64: self = .uint64(data.value())
case .float: self = .float(data.value())
case .double: self = .double(data.value())
case .bool: self = .bool(data.value())
case .string: self = .string(data.asString())
case .array(let array):
self = .array(array.enumerated().map { (offset, type) in
return UlogValue(type: type, data: data.advanced(by: offset*type.byteCount))
}
)
}
}
var typeName: String {
switch self {
case .uint8: return "uint8_t"
case .uint16: return "uint16_t"
case .uint32: return "uint32_t"
case .uint64: return "uint64_t"
case .int8: return "int8_t"
case .int16: return "int16_t"
case .int32: return "int32_t"
case .int64: return "int64_t"
case .float: return "float_t"
case .double: return "double_t"
case .bool: return "bool_t"
case .string: return "char_t"
case .array(let val): return val[0].description
}
}
func getValue<T>() -> T {
switch self {
case .int8(let val): return val as! T
case .uint8(let val): return val as! T
case .int16(let val): return val as! T
case .uint16(let val): return val as! T
case .int32(let val): return val as! T
case .uint32(let val): return val as! T
case .int64(let val): return val as! T
case .uint64(let val): return val as! T
case .float(let val): return val as! T
case .double(let val): return val as! T
case .bool(let val): return val as! T
case .string(let val): return val as! T
case .array(let val): return val as! T
}
}
var description: String {
switch self {
case .int8(let val): return String(val)
case .uint8(let val): return String(val)
case .int16(let val): return String(val)
case .uint16(let val): return String(val)
case .int32(let val): return String(val)
case .uint32(let val): return String(val)
case .int64(let val): return String(val)
case .uint64(let val): return String(val)
case .float(let val): return String(val)
case .double(let val): return String(val)
case .bool(let val): return String(val)
case .string(let val): return val
case .array(let val): return String(describing: val)
}
}
}
// HELPER structures
struct Format {
let name: String
let lookup: Dictionary<String, Int>
let types: [UlogType]
}
class ULogOld {
// let data: Data
var infos = [MessageInfo]()
var messageFormats = [MessageFormat]()
var formats = [String : Format]()
var formatsByLoggedId = [Format]()
var parameters = [MessageParameter]()
var addLoggedMessages = [MessageAddLoggedMessage]()
var data = [String : [[UlogValue]]]()
init?(data: Data) {
guard checkMagicHeader(data: data) else {
return nil
}
guard checkVersionHeader(data: data) else {
return nil
}
print(getLoggingStartMicros(data: data))
readFileDefinitions(data: data.subdata(in: 16..<data.endIndex))
}
private func checkMagicHeader(data: Data) -> Bool {
let ulog = "ULog".unicodeScalars.map(UInt8.init(ascii:))
return Array(data[0..<7]) == ulog + ["01", "12", "35"].map { UInt8($0, radix: 16)! }
}
private func checkVersionHeader(data: Data) -> Bool {
return data[7] == 0 || data[7] == 1
}
func getLoggingStartMicros(data: Data) -> UInt64 {
// logging start in micro
return data.subdata(in: 8..<16).value()
}
func readFileDefinitions(data: Data) {
var iteration = 0
let iterationMax = 50000
let startTime = Date()
let numberOfBytes = data.count
data.withUnsafeBytes { (u8Ptr: UnsafePointer<UInt8>) in
var ptr = UnsafeMutableRawPointer(mutating: u8Ptr)
let initialPointer = ptr
while iteration < iterationMax {
iteration += 1
let newTime = Date()
if iteration % (iterationMax/100) == 0 {
print( "complete\(Int(100*iteration/iterationMax)) time: \(newTime.timeIntervalSince(startTime))" )
}
guard let messageHeader = MessageHeader(ptr: ptr) else {
return // complete when the header is nil
}
print(messageHeader)
ptr += 3
if ptr - initialPointer + Int(messageHeader.size) > numberOfBytes { return }
let data = Data(bytes: ptr, count: Int(messageHeader.size))
switch messageHeader.type {
case .info:
guard let message = MessageInfo(data: data, header: messageHeader) else { return }
// infos.append(message)
print(message)
break
case .format:
guard let message = MessageFormat(data: data, header: messageHeader) else { return }
messageFormats.append(message)
let name = message.messageName
var types = [UlogType]()
var lookup = [String : Int]()
message.formatsProcessed.enumerated().forEach { (offset, element) in
lookup[element.0] = offset
types.append(element.1)
}
let f = Format(name: name, lookup: lookup, types: types)
formats[name] = f
break
case .parameter:
let message = MessageParameter(data: data, header: messageHeader)
parameters.append(message)
case .addLoggedMessage:
let message = MessageAddLoggedMessage(data: data, header: messageHeader)
addLoggedMessages.append(message)
formatsByLoggedId.insert(formats[message.messageName]!, at: Int(message.id))
break
case .data:
let message = MessageData(data: data, header: messageHeader)
var index = 0
let format = formatsByLoggedId[Int(message.id)]
var content = [UlogValue]()
for type in format.types {
content.append(UlogValue(type: type, data: message.data.advanced(by: index)))
index += type.byteCount
}
if self.data[format.name] == nil {
self.data[format.name] = [[UlogValue]]()
}
self.data[format.name]!.append(content)
break
case .logging:
let message = MessageLog(data: data, header: messageHeader)
print("logging \(message.message)")
break
case .dropout:
let message = MessageDropout(data: data, header: messageHeader)
print("dropout \(message.duration) ms")
break
default:
print("default, messageHeader.type \(messageHeader.type)")
return
}
ptr += Int(messageHeader.size)
}
}
}
}