aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/AplicomProtocolDecoder.java
blob: 14ab8378821d7440ad1e71622a915a38333c2c33 (plain)
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
 * Copyright 2013 - 2018 Anton Tananaev (anton@traccar.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.Context;
import org.traccar.DeviceSession;
import org.traccar.helper.Checksum;
import org.traccar.helper.Log;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;

public class AplicomProtocolDecoder extends BaseProtocolDecoder {

    public AplicomProtocolDecoder(AplicomProtocol protocol) {
        super(protocol);
    }

    private static final long IMEI_BASE_TC65_V20 = 0x1437207000000L;
    private static final long IMEI_BASE_TC65_V28 = 358244010000000L;
    private static final long IMEI_BASE_TC65I_V11 = 0x14143B4000000L;

    private static boolean validateImei(long imei) {
        return Checksum.luhn(imei / 10) == imei % 10;
    }

    private static long imeiFromUnitId(long unitId) {

        if (unitId == 0) {

            return 0;

        } else {

            // Try TC65i
            long imei = IMEI_BASE_TC65I_V11 + unitId;
            if (validateImei(imei)) {
                return imei;
            }

            // Try TC65 v2.8
            imei = IMEI_BASE_TC65_V28 + ((unitId + 0xA8180) & 0xFFFFFF);
            if (validateImei(imei)) {
                return imei;
            }

            // Try TC65 v2.0
            imei = IMEI_BASE_TC65_V20 + unitId;
            if (validateImei(imei)) {
                return imei;
            }

        }

        return unitId;
    }

    private static final int DEFAULT_SELECTOR_D = 0x0002fC;
    private static final int DEFAULT_SELECTOR_E = 0x007ffc;
    private static final int DEFAULT_SELECTOR_F = 0x0007fd;

    private static final int EVENT_DATA = 119;

    private void decodeEventData(Position position, ByteBuf buf, int event) {
        switch (event) {
            case 2:
            case 40:
                buf.readUnsignedByte();
                break;
            case 9:
                buf.readUnsignedMedium();
                break;
            case 31:
            case 32:
                buf.readUnsignedShort();
                break;
            case 38:
                buf.skipBytes(4 * 9);
                break;
            case 113:
                buf.readUnsignedInt();
                buf.readUnsignedByte();
                break;
            case 119:
                StringBuilder sb = new StringBuilder();
                while (buf.isReadable()) {
                    char c = (char) buf.readByte();
                    if (c >= 0x20) {
                        sb.append(c);
                    } else {
                        break;
                    }
                }
                position.set("eventData", sb.toString());
                break;
            case 121:
            case 142:
                buf.readLong();
                break;
            case 130:
                buf.readUnsignedInt(); // incorrect
                break;
            case 188:
                decodeEB(position, buf);
                break;
            default:
                break;
        }
    }

    private void decodeCanData(ByteBuf buf, Position position) {

        buf.readUnsignedMedium(); // packet identifier
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        int count = buf.readUnsignedByte();
        buf.readUnsignedByte(); // batch count
        buf.readUnsignedShort(); // selector bit
        buf.readUnsignedInt(); // timestamp

        buf.skipBytes(8);

        ArrayList<ByteBuf> values = new ArrayList<>(count);

        for (int i = 0; i < count; i++) {
            values.add(buf.readSlice(8));
        }

        for (int i = 0; i < count; i++) {
            ByteBuf value = values.get(i);
            switch (buf.readInt()) {
                case 0x20D:
                    position.set(Position.KEY_RPM, value.readShortLE());
                    position.set("dieselTemperature", value.readShortLE() * 0.1);
                    position.set("batteryVoltage", value.readShortLE() * 0.01);
                    position.set("supplyAirTempDep1", value.readShortLE() * 0.1);
                    break;
                case 0x30D:
                    position.set("activeAlarm", ByteBufUtil.hexDump(value));
                    break;
                case 0x40C:
                    position.set("airTempDep1", value.readShortLE() * 0.1);
                    position.set("airTempDep2", value.readShortLE() * 0.1);
                    break;
                case 0x40D:
                    position.set("coldUnitState", ByteBufUtil.hexDump(value));
                    break;
                case 0x50C:
                    position.set("defrostTempDep1", value.readShortLE() * 0.1);
                    position.set("defrostTempDep2", value.readShortLE() * 0.1);
                    break;
                case 0x50D:
                    position.set("condenserPressure", value.readShortLE() * 0.1);
                    position.set("suctionPressure", value.readShortLE() * 0.1);
                    break;
                case 0x58C:
                    value.readByte();
                    value.readShort(); // index
                    switch (value.readByte()) {
                        case 0x01:
                            position.set("setpointZone1", value.readIntLE() * 0.1);
                            break;
                        case 0x02:
                            position.set("setpointZone2", value.readIntLE() * 0.1);
                            break;
                        case 0x05:
                            position.set("unitType", value.readIntLE());
                            break;
                        case 0x13:
                            position.set("dieselHours", value.readIntLE() / 60 / 60);
                            break;
                        case 0x14:
                            position.set("electricHours", value.readIntLE() / 60 / 60);
                            break;
                        case 0x17:
                            position.set("serviceIndicator", value.readIntLE());
                            break;
                        case 0x18:
                            position.set("softwareVersion", value.readIntLE() * 0.01);
                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    Log.warning(new UnsupportedOperationException());
                    break;
            }
        }
    }

    private void decodeD(Position position, ByteBuf buf, int selector, int event) {

        if ((selector & 0x0008) != 0) {
            position.setValid((buf.readUnsignedByte() & 0x40) != 0);
        } else {
            getLastLocation(position, null);
        }

        if ((selector & 0x0004) != 0) {
            buf.skipBytes(4); // snapshot time
        }

        if ((selector & 0x0008) != 0) {
            position.setTime(new Date(buf.readUnsignedInt() * 1000));
            position.setLatitude(buf.readInt() / 1000000.0);
            position.setLongitude(buf.readInt() / 1000000.0);
            position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedByte());
        }

        if ((selector & 0x0010) != 0) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            position.set("maximumSpeed", buf.readUnsignedByte());
            position.setCourse(buf.readUnsignedByte() * 2.0);
        }

        if ((selector & 0x0040) != 0) {
            position.set(Position.KEY_INPUT, buf.readUnsignedByte());
        }

        if ((selector & 0x0020) != 0) {
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
            position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort());
            position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort());
        }

        if ((selector & 0x8000) != 0) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
            position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
        }

        // Pulse rate 1
        if ((selector & 0x10000) != 0) {
            buf.readUnsignedShort();
            buf.readUnsignedInt();
        }

        // Pulse rate 2
        if ((selector & 0x20000) != 0) {
            buf.readUnsignedShort();
            buf.readUnsignedInt();
        }

        if ((selector & 0x0080) != 0) {
            position.set("trip1", buf.readUnsignedInt());
        }

        if ((selector & 0x0100) != 0) {
            position.set("trip2", buf.readUnsignedInt());
        }

        if ((selector & 0x0040) != 0) {
            position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());
        }

        if ((selector & 0x0200) != 0) {
            position.set(Position.KEY_DRIVER_UNIQUE_ID,
                    String.valueOf(((long) buf.readUnsignedShort()) << 32) + buf.readUnsignedInt());
        }

        if ((selector & 0x0400) != 0) {
            buf.readUnsignedByte(); // Keypad
        }

        if ((selector & 0x0800) != 0) {
            position.setAltitude(buf.readShort());
        }

        if ((selector & 0x2000) != 0) {
            buf.readUnsignedShort(); // snapshot counter
        }

        if ((selector & 0x4000) != 0) {
            buf.skipBytes(8); // state flags
        }

        if ((selector & 0x80000) != 0) {
            buf.skipBytes(11); // cell info
        }

        if ((selector & 0x1000) != 0) {
            decodeEventData(position, buf, event);
        }

        if (Context.getConfig().getBoolean(getProtocolName() + ".can")
                && buf.isReadable() && (selector & 0x1000) != 0 && event == EVENT_DATA) {
            decodeCanData(buf, position);
        }
    }

    private void decodeE(Position position, ByteBuf buf, int selector) {

        if ((selector & 0x0008) != 0) {
            position.set("tachographEvent", buf.readUnsignedShort());
        }

        if ((selector & 0x0004) != 0) {
            getLastLocation(position, new Date(buf.readUnsignedInt() * 1000));
        } else {
            getLastLocation(position, null);
        }

        if ((selector & 0x0010) != 0) {
            String time = buf.readUnsignedByte() + "s " + buf.readUnsignedByte() + "m " + buf.readUnsignedByte() + "h "
                    + buf.readUnsignedByte() + "M " + buf.readUnsignedByte() + "D " + buf.readUnsignedByte() + "Y "
                    + buf.readByte() + "m " + buf.readByte() + "h";
            position.set("tachographTime", time);
        }

        position.set("workState", buf.readUnsignedByte());
        position.set("driver1State", buf.readUnsignedByte());
        position.set("driver2State", buf.readUnsignedByte());

        if ((selector & 0x0020) != 0) {
            position.set("tachographStatus", buf.readUnsignedByte());
        }

        if ((selector & 0x0040) != 0) {
            position.set(Position.KEY_OBD_SPEED, buf.readUnsignedShort() / 256.0);
        }

        if ((selector & 0x0080) != 0) {
            position.set(Position.KEY_OBD_ODOMETER, buf.readUnsignedInt() * 5);
        }

        if ((selector & 0x0100) != 0) {
            position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt() * 5);
        }

        if ((selector & 0x8000) != 0) {
            position.set("kFactor", buf.readUnsignedShort() * 0.001 + " pulses/m");
        }

        if ((selector & 0x0200) != 0) {
            position.set(Position.KEY_RPM, buf.readUnsignedShort() * 0.125);
        }

        if ((selector & 0x0400) != 0) {
            position.set("extraInfo", buf.readUnsignedShort());
        }

        if ((selector & 0x0800) != 0) {
            position.set(Position.KEY_VIN, buf.readSlice(18).toString(StandardCharsets.US_ASCII).trim());
        }

        if ((selector & 0x2000) != 0) {
            buf.readUnsignedByte(); // card 1 type
            buf.readUnsignedByte(); // card 1 country code
            String card = buf.readSlice(20).toString(StandardCharsets.US_ASCII).trim();
            if (!card.isEmpty()) {
                position.set("card1", card);
            }
        }

        if ((selector & 0x4000) != 0) {
            buf.readUnsignedByte(); // card 2 type
            buf.readUnsignedByte(); // card 2 country code
            String card = buf.readSlice(20).toString(StandardCharsets.US_ASCII).trim();
            if (!card.isEmpty()) {
                position.set("card2", card);
            }
        }

        if ((selector & 0x10000) != 0) {
            int count = buf.readUnsignedByte();
            for (int i = 1; i <= count; i++) {
                position.set("driver" + i, buf.readSlice(22).toString(StandardCharsets.US_ASCII).trim());
                position.set("driverTime" + i, buf.readUnsignedInt());
            }
        }
    }

    private void decodeH(Position position, ByteBuf buf, int selector) {

        if ((selector & 0x0004) != 0) {
            getLastLocation(position, new Date(buf.readUnsignedInt() * 1000));
        } else {
            getLastLocation(position, null);
        }

        if ((selector & 0x0040) != 0) {
            buf.readUnsignedInt(); // reset time
        }

        if ((selector & 0x2000) != 0) {
            buf.readUnsignedShort(); // snapshot counter
        }

        int index = 1;
        while (buf.readableBytes() > 0) {

            position.set("h" + index + "Index", buf.readUnsignedByte());

            buf.readUnsignedShort(); // length

            int n = buf.readUnsignedByte();
            int m = buf.readUnsignedByte();

            position.set("h" + index + "XLength", n);
            position.set("h" + index + "YLength", m);

            if ((selector & 0x0008) != 0) {
                position.set("h" + index + "XType", buf.readUnsignedByte());
                position.set("h" + index + "YType", buf.readUnsignedByte());
                position.set("h" + index + "Parameters", buf.readUnsignedByte());
            }

            boolean percentageFormat = (selector & 0x0020) != 0;

            StringBuilder data = new StringBuilder();
            for (int i = 0; i < n * m; i++) {
                if (percentageFormat) {
                    data.append(buf.readUnsignedByte() * 0.5).append("%").append(" ");
                } else {
                    data.append(buf.readUnsignedShort()).append(" ");
                }
            }

            position.set("h" + index + "Data", data.toString().trim());

            position.set("h" + index + "Total", buf.readUnsignedInt());

            if ((selector & 0x0010) != 0) {
                int k = buf.readUnsignedByte();

                data = new StringBuilder();
                for (int i = 1; i < n; i++) {
                    if (k == 1) {
                        data.append(buf.readByte()).append(" ");
                    } else if (k == 2) {
                        data.append(buf.readShort()).append(" ");
                    }
                }
                position.set("h" + index + "XLimits", data.toString().trim());

                data = new StringBuilder();
                for (int i = 1; i < m; i++) {
                    if (k == 1) {
                        data.append(buf.readByte()).append(" ");
                    } else if (k == 2) {
                        data.append(buf.readShort()).append(" ");
                    }
                }
                position.set("h" + index + "YLimits", data.toString().trim());
            }

            index += 1;
        }
    }

    private void decodeEB(Position position, ByteBuf buf) {

        if (buf.readByte() != (byte) 'E' || buf.readByte() != (byte) 'B') {
            return;
        }

        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        position.set(Position.KEY_EVENT, buf.readUnsignedShort());
        position.set("dataValidity", buf.readUnsignedByte());
        position.set("towed", buf.readUnsignedByte());
        buf.readUnsignedShort(); // length

        while (buf.readableBytes() > 0) {
            buf.readUnsignedByte(); // towed position
            int type = buf.readUnsignedByte();
            int length = buf.readUnsignedByte();
            int end = buf.readerIndex() + length;

            switch (type) {
                case 0x01:
                    position.set("brakeFlags", ByteBufUtil.hexDump(buf.readSlice(length)));
                    break;
                case 0x02:
                    position.set("wheelSpeed", buf.readUnsignedShort() / 256.0);
                    position.set("wheelSpeedDifference", buf.readUnsignedShort() / 256.0 - 125.0);
                    position.set("lateralAcceleration", buf.readUnsignedByte() / 10.0 - 12.5);
                    position.set("vehicleSpeed", buf.readUnsignedShort() / 256.0);
                    break;
                case 0x03:
                    position.set(Position.KEY_AXLE_WEIGHT, buf.readUnsignedShort() * 2);
                    break;
                case 0x04:
                    position.set("tyrePressure", buf.readUnsignedByte() * 10);
                    position.set("pneumaticPressure", buf.readUnsignedByte() * 5);
                    break;
                case 0x05:
                    position.set("brakeLining", buf.readUnsignedByte() * 0.4);
                    position.set("brakeTemperature", buf.readUnsignedByte() * 10);
                    break;
                case 0x06:
                    position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 5L);
                    position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt() * 5L);
                    position.set(Position.KEY_ODOMETER_SERVICE, (buf.readUnsignedInt() - 2105540607) * 5L);
                    break;
                case 0x0A:
                    position.set("absStatusCounter", buf.readUnsignedShort());
                    position.set("atvbStatusCounter", buf.readUnsignedShort());
                    position.set("vdcActiveCounter", buf.readUnsignedShort());
                    break;
                case 0x0B:
                    position.set("brakeMinMaxData", ByteBufUtil.hexDump(buf.readSlice(length)));
                    break;
                case 0x0C:
                    position.set("missingPgn", ByteBufUtil.hexDump(buf.readSlice(length)));
                    break;
                case 0x0D:
                    buf.readUnsignedByte();
                    position.set("towedDetectionStatus", buf.readUnsignedInt());
                    buf.skipBytes(17); // vin
                    break;
                case 0x0E:
                default:
                    break;
            }

            buf.readerIndex(end);
        }
    }

    private void decodeF(Position position, ByteBuf buf, int selector) {

        getLastLocation(position, null);

        if ((selector & 0x0004) != 0) {
            buf.skipBytes(4); // snapshot time
        }

        buf.readUnsignedByte(); // data validity

        if ((selector & 0x0008) != 0) {
            position.set(Position.KEY_RPM, buf.readUnsignedShort());
            position.set("rpmMax", buf.readUnsignedShort());
            position.set("rpmMin", buf.readUnsignedShort());
        }

        if ((selector & 0x0010) != 0) {
            position.set("engineTemp", buf.readShort());
            position.set("engineTempMax", buf.readShort());
            position.set("engineTempMin", buf.readShort());
        }

        if ((selector & 0x0020) != 0) {
            position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedInt()));
            position.set("serviceDistance", buf.readInt());
            position.set("driverActivity", buf.readUnsignedByte());
            position.set(Position.KEY_THROTTLE, buf.readUnsignedByte());
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
        }

        if ((selector & 0x0040) != 0) {
            position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt());
        }

        if ((selector & 0x0080) != 0) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if ((selector & 0x0100) != 0) {
            position.set(Position.KEY_OBD_SPEED, buf.readUnsignedByte());
            position.set("speedMax", buf.readUnsignedByte());
            position.set("speedMin", buf.readUnsignedByte());
            position.set("hardBraking", buf.readUnsignedByte());
        }

        if ((selector & 0x0200) != 0) {
            position.set("tachographSpeed", buf.readUnsignedByte());
            position.set("driver1State", buf.readUnsignedByte());
            position.set("driver2State", buf.readUnsignedByte());
            position.set("tachographStatus", buf.readUnsignedByte());
            position.set("overspeedCount", buf.readUnsignedByte());
        }

        if ((selector & 0x0800) != 0) {
            position.set(Position.KEY_HOURS, buf.readUnsignedInt() * 0.05);
            position.set(Position.KEY_RPM, buf.readUnsignedShort() * 0.125);
            position.set(Position.KEY_OBD_SPEED, buf.readUnsignedShort() / 256.0);
            position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt() * 0.5);
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 0.4);
        }

        if ((selector & 0x1000) != 0) {
            position.set("ambientTemperature", buf.readUnsignedShort() * 0.03125 - 273);
            buf.readUnsignedShort(); // fuel rate
            position.set("fuelEconomy", buf.readUnsignedShort() / 512.0);
            position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt() * 0.001);
            buf.readUnsignedByte(); // pto drive engagement
        }

        if ((selector & 0x2000) != 0) {
            buf.skipBytes(buf.readUnsignedByte()); // driver identification
        }

        if ((selector & 0x4000) != 0) {
            position.set("torque", buf.readUnsignedByte());
            position.set("brakePressure1", buf.readUnsignedByte() * 8);
            position.set("brakePressure2", buf.readUnsignedByte() * 8);
            position.set("grossWeight", buf.readUnsignedShort() * 10);
            position.set("exhaustFluid", buf.readUnsignedByte() * 0.4);
            buf.readUnsignedByte(); // retarder torque mode
            position.set("retarderTorque", buf.readUnsignedByte());
            position.set("retarderSelection", buf.readUnsignedByte() * 0.4);
            buf.skipBytes(8); // tell tale status block 1
            buf.skipBytes(8); // tell tale status block 2
            buf.skipBytes(8); // tell tale status block 3
            buf.skipBytes(8); // tell tale status block 4
        }

        if ((selector & 0x8000) != 0) {
            position.set("parkingBrakeStatus", buf.readUnsignedByte());
            position.set("doorStatus", buf.readUnsignedByte());
            buf.skipBytes(8); // status per door
            position.set("alternatorStatus", buf.readUnsignedByte());
            position.set("selectedGear", buf.readUnsignedByte());
            position.set("currentGear", buf.readUnsignedByte());
            buf.skipBytes(4 * 2); // air suspension pressure
        }

        if ((selector & 0x0400) != 0) {
            int count = buf.readUnsignedByte();
            for (int i = 0; i < count; i++) {
                position.set("axle" + i, buf.readUnsignedShort());
            }
        }

    }

    @Override
    protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

        ByteBuf buf = (ByteBuf) msg;

        char protocol = (char) buf.readByte();
        int version = buf.readUnsignedByte();

        String imei;
        if ((version & 0x80) != 0) {
            imei = String.valueOf((buf.readUnsignedInt() << (3 * 8)) | buf.readUnsignedMedium());
        } else {
            imei = String.valueOf(imeiFromUnitId(buf.readUnsignedMedium()));
        }

        buf.readUnsignedShort(); // length

        int selector = DEFAULT_SELECTOR_D;
        if (protocol == 'E') {
            selector = DEFAULT_SELECTOR_E;
        } else if (protocol == 'F') {
            selector = DEFAULT_SELECTOR_F;
        }
        if ((version & 0x40) != 0) {
            selector = buf.readUnsignedMedium();
        }

        Position position = new Position(getProtocolName());
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        int event = buf.readUnsignedByte();
        position.set(Position.KEY_EVENT, event);
        position.set("eventInfo", buf.readUnsignedByte());

        if (protocol == 'D') {
            decodeD(position, buf, selector, event);
        } else if (protocol == 'E') {
            decodeE(position, buf, selector);
        } else if (protocol == 'H') {
            decodeH(position, buf, selector);
        } else if (protocol == 'F') {
            decodeF(position, buf, selector);
        } else {
            return null;
        }

        return position;
    }

}