forked from BertoldVdb/go-ais
-
Notifications
You must be signed in to change notification settings - Fork 2
/
codec.go
625 lines (515 loc) · 15 KB
/
codec.go
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// Package ais is an Automatic Identification System (ITU-R M.1371-5) packet decoder and encoder
package ais
import (
"reflect"
"strconv"
)
// Codec encodes and decodes AIS messages (ITU-R M.1371-5)
type Codec struct {
StrictByteAlignment bool
minValidMap map[string]uint
// DecoderCheckFixedValues will validate that spare values are zero. This is only for debugging
// and should not be used as newer protocl versions may use the spares for something.
DecoderCheckFixedValues bool
// FloatWithoutConversion will disable float conversion, making re-encoding the decoded output yield
// an identical bitstream.
FloatWithoutConversion bool
// DropSpace will drop spaces at the end of strings. Many encoders pad strings
// with spaces instead of @.
DropSpace bool
// FastParse is a non-reflection based parsing method
FastParse bool
}
func assert(condition bool, err string) {
if !condition {
panic("BUG: " + err)
}
}
// CodecNewFast creates and initializes the AIS parser. The first two parameters allow accepting
// messages that a few types of existing encoders seem to transmit with invalid length.
// This is very rare, passing false to both should be fine. The last parameter enables a fast decoder.
func CodecNewFast(acceptShortAck bool, acceptShortShipStaticData bool, parseFast bool) *Codec {
t := &Codec{}
t.minValidMap = make(map[string]uint)
if acceptShortAck {
t.minValidMap["BinaryAcknowledgeData"] = 1
}
if acceptShortShipStaticData {
t.minValidMap["ShipStaticData"] = 420
}
t.FastParse = parseFast
return t
}
// CodecNew creates and initializes the AIS parser. The two parameters allow accepting
// messages that a few types of existing encoders seem to transmit with invalid length.
// This is very rare, passing false to both should be fine.
func CodecNew(acceptShortAck bool, acceptShortShipStaticData bool) *Codec {
return CodecNewFast(acceptShortAck, acceptShortShipStaticData, false)
}
// ChannelToFrequency converts an AIS channel number into its frequency in Hz
func (t *Codec) ChannelToFrequency(channel uint16) uint {
/* https://www.itu.int/dms_pubrec/itu-r/rec/m/R-REC-M.1084-5-201203-I!!PDF-E.pdf */
c := uint(channel % 1000)
duplexType := channel / 1000
shipFrequency := uint(0)
if 1 <= c && c <= 28 {
shipFrequency = 156050000 + (c-1)*50000
} else if 60 <= c && c <= 88 {
shipFrequency = 156025000 + (c-60)*50000
} else if 260 <= c && c <= 287 {
shipFrequency = 156037500 + (c-260)*50000
} else if 201 <= c && c <= 228 {
shipFrequency = 156062500 + (c-201)*50000
} else {
return 0
}
/* Is 287 a special case or a typo in the standard?
It is 1MHz higher than expected */
/* In this range only simplex operation is defined */
if 156375000 <= shipFrequency && shipFrequency <= 156887500 {
return shipFrequency
}
switch duplexType {
case 1:
return shipFrequency
case 2:
return shipFrequency + 4600000
default:
return 0
}
}
func makeSigned(input uint64, length uint) int64 {
result := int64(input)
maxValue := int64(1) << length
if result >= maxValue/2 {
result = result - maxValue
}
return result
}
func extractNumber(payload []byte, isSigned bool, offset *uint, width uint) int64 {
result := uint64(0)
for i := *offset; i < *offset+width; i++ {
result <<= 1
if i < uint(len(payload)) {
result |= uint64(payload[i])
}
}
*offset += width
if isSigned {
return makeSigned(result, width)
}
return int64(result)
}
func extractString(payload []byte, offset *uint, width uint, dropSpace bool) string {
numChars := width / 6
if numChars == 0 {
return ""
}
result := make([]byte, numChars)
for i := uint(0); i < numChars; i++ {
number := extractNumber(payload, false, offset, 6)
if number < 32 {
number = number + 64
}
result[i] = byte(number)
}
/* The string is closed by @ */
stripSpace := len(result)
for i := len(result) - 1; i >= 0; i-- {
if result[i] != '@' {
if !dropSpace || result[i] != ' ' {
break
}
}
stripSpace--
}
result = result[:stripSpace]
return string(result)
}
func aisFindFieldLength(sf reflect.StructField, payload []byte) (valid bool, skip bool, fixedLength bool, length uint) {
depends, ok := sf.Tag.Lookup("aisDependsBit")
if ok {
target := byte(1)
var dependsI int
if depends[0] == '~' {
target = 0
dependsI, _ = strconv.Atoi(depends[1:])
} else {
dependsI, _ = strconv.Atoi(depends)
}
if dependsI >= len(payload) {
return false, false, false, 0
}
if payload[dependsI] != target {
return true, true, false, 0
}
}
vi, _ := strconv.Atoi(sf.Tag.Get("aisWidth"))
if vi < 0 {
return true, false, false, 0
}
return true, false, true, uint(vi)
}
func isBasicValue(val reflect.Value) (bool, int64) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true, val.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true, int64(val.Uint())
case reflect.Bool:
if val.Bool() {
return true, 1
}
return true, 0
default:
return false, 0
}
}
func isSigned(val reflect.Value) bool {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
default:
return false
}
}
func (t *Codec) aisFillMessage(val reflect.Value, payload []byte, offset *uint) int {
/* Return value of -2 propagates all the way up and fails the decode */
optional := false
/* Look up the struct */
st := val.Type()
strType := st.Name()
validField := val.FieldByName("Valid")
if validField.IsValid() {
f, _ := st.FieldByName("Valid")
_, optional = f.Tag.Lookup("aisOptional")
validField.SetBool(false)
}
/* Calculate minimum length */
minLength := uint(0)
for i := 0; i < val.NumField(); i++ {
valid, skip, fixedLength, length := aisFindFieldLength(st.Field(i), payload)
if !valid {
return -1
}
if fixedLength && !skip {
minLength += length
}
}
minBitsForValid, ok := t.minValidMap[strType]
if !ok {
minBitsForValid = minLength
}
/* Is the message long enough? */
if len(payload)-int(*offset) < int(minBitsForValid) {
if optional {
return 0
}
return -1
}
for i := 0; i < val.NumField(); i++ {
if st.Field(i).Name == "Valid" {
continue
}
field := val.Field(i)
_, skip, fixedLength, v := aisFindFieldLength(st.Field(i), payload)
if skip {
continue
}
/* Some fields take up the entire remainder of the message */
if !fixedLength {
varLen := len(payload) - int(minLength)
assert(varLen >= 0, "Variable length payload length is negative.")
v = uint(varLen)
}
var basicValue int64
if b, _ := isBasicValue(field); b {
checkValue := false
correctValue := int64(0)
if t.DecoderCheckFixedValues {
encodeAsStr, encodeAsFound := st.Field(i).Tag.Lookup("aisCheckValue")
if !encodeAsFound {
encodeAsStr, encodeAsFound = st.Field(i).Tag.Lookup("aisEncodeAs")
}
if encodeAsFound {
correctValue, _ = strconv.ParseInt(encodeAsStr, 10, 64)
checkValue = true
}
}
basicValue = extractNumber(payload, isSigned(field), offset, v)
if checkValue && (basicValue != correctValue) {
return -2
}
}
switch field.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(basicValue)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
field.SetUint(uint64(basicValue))
case reflect.Bool:
field.SetBool(basicValue == 1)
case reflect.String:
field.SetString(extractString(payload, offset, v, t.DropSpace))
case reflect.Slice:
field.SetBytes(payload[*offset : *offset+v])
*offset += v
case reflect.Array:
for k := 0; k < field.Len(); k++ {
subField := field.Index(k)
ok := t.aisFillMessage(subField, payload, offset)
assert(ok != -2, "Could not encode array subfield. This should not happen with the current message definition")
if ok == -1 {
if k == 0 {
return -2
}
break
}
}
case reflect.Struct:
ok := t.aisFillMessage(field, payload, offset)
if ok < 0 {
return ok
}
case reflect.Float64, reflect.Float32:
signed := field.Type().Name() != "Field10"
value := extractNumber(payload, signed, offset, v)
field.SetFloat(float64(value))
if !t.FloatWithoutConversion {
switch field.Type().Name() {
case "FieldLatLonFine":
field.SetFloat(float64(value) / 10000.0 / 60.0)
case "FieldLatLonCoarse":
field.SetFloat(float64(value) / 10.0 / 60.0)
case "Field10":
field.SetFloat(float64(value) / 10.0)
}
}
}
}
if validField.IsValid() {
validField.SetBool(true)
}
return 0
}
// DecodePacket will convert a []byte containing 0 and 1 to an object containing the decoded packet.
// It will return nil if decoding failed.
func (t *Codec) DecodePacket(payload []byte) Packet {
if len(payload)%8 != 0 {
/* AIS messages should be a multiple of 8-bits:
* [Order AIS message bits into 8-bit bytes for assembly of transmission packet, see § 3.3.7.]
* Most AIS messages are already a multiple of 8-bits and not all transmitters seem to implement
* this padding requirement, so you may get some messages that are not a multiple of 8 bits long.
* Also, some receivers seem to return invalid fillBits values (off by 1 or 2), therefore it is
* recommended to not treat bad padding as an error condition */
if t.StrictByteAlignment {
return nil
}
}
if len(payload) < 6 {
return nil
}
offset := uint(0)
msgID := extractNumber(payload, false, &offset, 6)
offset = 0
if t.FastParse {
if fn, canFastParse := mapper[msgID]; canFastParse {
switch out := fn(t, payload, &offset).(type) {
case Packet:
return decodeHelper(out)
}
return nil
}
}
if msgID >= 1 && msgID <= 27 {
msgType := msgMap[msgID]
msgPtr := reflect.New(msgType.rType)
if t.aisFillMessage(msgPtr.Elem(), payload, &offset) == 0 {
switch out := msgPtr.Elem().Interface().(type) {
case Packet:
return decodeHelper(out)
}
}
}
return nil
}
func encodeNumber(packet []byte, isSigned bool, width uint, number int64) ([]byte, bool) {
if !isSigned {
maxVal := (int64(1) << width) - 1
if number > maxVal {
return packet, false
}
} else {
minVal := -(int64(1) << width) / 2
maxVal := (int64(1)<<width)/2 - 1
if number < minVal || number > maxVal {
return packet, false
}
}
numUnsigned := uint64(number)
for i := int(width - 1); i >= 0; i-- {
packet = append(packet, byte((numUnsigned>>uint(i))&1))
}
return packet, true
}
func encodeString(packet []byte, width uint, fixedWidth bool, str string) ([]byte, bool) {
var i uint
for i = 0; i < uint(len(str)) && (i < width/6 || !fixedWidth); i++ {
char := byte(str[i])
if 64 <= char && char <= 95 {
char -= 64
} else if 32 <= char && char <= 63 {
/* No translation needed */
} else {
/* This character is not valid */
return packet, false
}
packet, _ = encodeNumber(packet, false, 6, int64(char))
}
/* Pad fixed width strings */
if fixedWidth {
for ; i < width/6; i++ {
packet, _ = encodeNumber(packet, false, 6, 0)
}
}
return packet, true
}
func aisEncodedLength(val reflect.Value, i int) (skip bool, fixedLength bool, length uint) {
st := val.Type()
sf := st.Field(i)
depends, dependsFound := sf.Tag.Lookup("aisDependsField")
if dependsFound {
value := true
if depends[0] == '~' {
value = false
depends = depends[1:]
}
df := val.FieldByName(depends)
if df.Bool() != value {
return true, true, 0
}
}
vi, _ := strconv.Atoi(sf.Tag.Get("aisWidth"))
if vi < 0 {
return false, false, 0
}
return false, true, uint(vi)
}
func (t *Codec) aisEncodeMessage(val reflect.Value, packet []byte) ([]byte, bool) {
vf := val.FieldByName("Valid")
if vf.IsValid() && !vf.Bool() {
/* Is it optional? */
tf, _ := val.Type().FieldByName("Valid")
_, opt := tf.Tag.Lookup("aisOptional")
return packet, opt
}
st := val.Type()
var ok bool
for i := 0; i < val.NumField(); i++ {
if st.Field(i).Name == "Valid" {
continue
}
field := val.Field(i)
skip, fixedLength, v := aisEncodedLength(val, i)
if skip {
continue
}
if b, k := isBasicValue(field); b {
encodeAsStr, encodeAsFound := st.Field(i).Tag.Lookup("aisEncodeAs")
if encodeAsFound {
k, _ = strconv.ParseInt(encodeAsStr, 10, 64)
}
packet, ok = encodeNumber(packet, isSigned(field), v, k)
if !ok {
return packet, false
}
}
switch field.Kind() {
case reflect.String:
packet, ok = encodeString(packet, v, fixedLength, field.String())
if !ok {
return packet, false
}
case reflect.Slice:
tmp := field.Bytes()
assert(!fixedLength, "Fixed length slices are not supported since they do not occur in the current spec")
packet = append(packet, tmp...)
case reflect.Array:
for k := 0; k < field.Len(); k++ {
subField := field.Index(k)
packet, ok = t.aisEncodeMessage(subField, packet)
if !ok {
if k == 0 {
return packet, false
}
break
}
}
case reflect.Struct:
packet, ok = t.aisEncodeMessage(field, packet)
if !ok {
return packet, false
}
case reflect.Float64, reflect.Float32:
value := int64(field.Float())
signed := field.Type().Name() != "Field10"
if !t.FloatWithoutConversion {
switch field.Type().Name() {
case "FieldLatLonFine":
value = int64(field.Float() * 10000.0 * 60.0)
case "FieldLatLonCoarse":
value = int64(field.Float() * 10.0 * 60.0)
case "Field10":
value = int64(field.Float() * 10.0)
}
}
packet, ok = encodeNumber(packet, signed, v, value)
if !ok {
return packet, false
}
}
}
return packet, true
}
// EncodePacket encodes a valid AIS object to a binary []byte.
// nil is returned if encoding failed.
func (t *Codec) EncodePacket(message Packet) []byte {
/* Check if the type is correct */
mID := message.GetHeader().MessageID
if mID < 1 || mID > 27 {
return nil
}
expectedType := msgMap[mID].rType
if reflect.TypeOf(message) != expectedType {
return nil
}
val := reflect.ValueOf(encodeHelper(message))
vt, _ := val.Type().FieldByName("Valid")
encodeString, ok := vt.Tag.Lookup("aisEncodeMaxLen")
assert(ok, "aisEncodeMaxLen not found")
encodeLen, _ := strconv.Atoi(encodeString)
/* AIS packets need to be a multiple of 8 bits */
if encodeLen%8 != 0 {
encodeLen += 7 - (encodeLen % 8)
}
packet := make([]byte, 0, encodeLen)
packet, ok = t.aisEncodeMessage(val, packet)
if !ok {
return nil
}
if encodeLen > 0 && len(packet) > encodeLen {
return nil
}
/* Pad packet to 8-bit boundary:
* From 3.3.7: Unused bits in the last byte should be set to zero in order to preserve byte boundary.
* This means that you cannot send variable length binary messages where the payload is not a multiple of 8-bits.
* Otherwise, the padding will damage the attached communication state.
*
* Some encoders do seem to produce these messages (total length is not a multiple of 8), we can receive that
* but not encode it. These are likely proprietary systems anyway, all publically documented messages I have seen
* are a multiple of 8-bits long.
*/
for len(packet)%8 != 0 {
packet = append(packet, 0)
}
return packet
}