aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java
blob: 53f3e49f9ffc60df73fbed458a883982b047c2e7 (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
/*
 * Copyright 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.Unpooled;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
import org.traccar.helper.Checksum;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class EgtsProtocolDecoder extends BaseProtocolDecoder {

    private static final Logger LOGGER = LoggerFactory.getLogger(EgtsProtocolDecoder.class);

    public EgtsProtocolDecoder(Protocol protocol) {
        super(protocol);
    }

    private boolean useOidAsDeviceId = true;

    public static final int PT_RESPONSE = 0;
    public static final int PT_APPDATA = 1;
    public static final int PT_SIGNED_APPDATA = 2;

    public static final int SERVICE_AUTH = 1;
    public static final int SERVICE_TELEDATA = 2;
    public static final int SERVICE_COMMANDS = 4;
    public static final int SERVICE_FIRMWARE = 9;
    public static final int SERVICE_ECALL = 10;

    public static final int MSG_RECORD_RESPONSE = 0;
    public static final int MSG_TERM_IDENTITY = 1;
    public static final int MSG_MODULE_DATA = 2;
    public static final int MSG_VEHICLE_DATA = 3;
    public static final int MSG_AUTH_PARAMS = 4;
    public static final int MSG_AUTH_INFO = 5;
    public static final int MSG_SERVICE_INFO = 6;
    public static final int MSG_RESULT_CODE = 7;
    public static final int MSG_POS_DATA = 16;
    public static final int MSG_EXT_POS_DATA = 17;
    public static final int MSG_AD_SENSORS_DATA = 18;
    public static final int MSG_COUNTERS_DATA = 19;
    public static final int MSG_STATE_DATA = 20;
    public static final int MSG_LOOPIN_DATA = 22;
    public static final int MSG_ABS_DIG_SENS_DATA = 23;
    public static final int MSG_ABS_AN_SENS_DATA = 24;
    public static final int MSG_ABS_CNTR_DATA = 25;
    public static final int MSG_ABS_LOOPIN_DATA = 26;
    public static final int MSG_LIQUID_LEVEL_SENSOR = 27;
    public static final int MSG_PASSENGERS_COUNTERS = 28;

    private int packetId;

    private void sendResponse(
            Channel channel, int packetType, int index, int serviceType, int type, ByteBuf content) {
        if (channel != null) {

            ByteBuf data = Unpooled.buffer();
            data.writeByte(type);
            data.writeShortLE(content.readableBytes());
            data.writeBytes(content);
            content.release();

            ByteBuf record = Unpooled.buffer();
            if (packetType == PT_RESPONSE) {
                record.writeShortLE(index);
                record.writeByte(0); // success
            }
            record.writeShortLE(data.readableBytes());
            record.writeShortLE(0);
            record.writeByte(0); // flags (possibly 1 << 6)
            record.writeByte(serviceType);
            record.writeByte(serviceType);
            record.writeBytes(data);
            data.release();
            int recordChecksum = Checksum.crc16(Checksum.CRC16_CCITT_FALSE, record.nioBuffer());

            ByteBuf response = Unpooled.buffer();
            response.writeByte(1); // protocol version
            response.writeByte(0); // security key id
            response.writeByte(0); // flags
            response.writeByte(5 + 2 + 2 + 2); // header length
            response.writeByte(0); // encoding
            response.writeShortLE(record.readableBytes());
            response.writeShortLE(packetId++);
            response.writeByte(packetType);
            response.writeByte(Checksum.crc8(Checksum.CRC8_EGTS, response.nioBuffer()));
            response.writeBytes(record);
            record.release();
            response.writeShortLE(recordChecksum);

            channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));

        }
    }

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

        ByteBuf buf = (ByteBuf) msg;

        List<Position> positions = new LinkedList<>();

        // buf possibly contains multiple packets
        while (buf.readableBytes() >= 11) {

            short headerLength = buf.getUnsignedByte(buf.readerIndex() + 3);
            int frameDataLength = buf.getUnsignedShortLE(buf.readerIndex() + 5);
            int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2);
            short packetType = buf.getUnsignedByte(buf.readerIndex() + 5 + 2 + 2);
            buf.skipBytes(headerLength);

            if (packetType == PT_RESPONSE) {
                // some retranslator systems do send response on response packets
                if (frameDataLength > 0) {
                    buf.skipBytes(frameDataLength + 2); // 2-byte CRC is present only if frameData exists
                }
                continue;
            }

            int frameDataEnd = buf.readerIndex() + frameDataLength;
            long oid = 0L;
            while (buf.readerIndex() < frameDataEnd) {

                int length = buf.readUnsignedShortLE();
                int recordIndex = buf.readUnsignedShortLE();
                int recordFlags = buf.readUnsignedByte();

                if (BitUtil.check(recordFlags, 0)) {
                    oid = buf.readUnsignedIntLE(); // object id
                }

                if (BitUtil.check(recordFlags, 1)) {
                    buf.readUnsignedIntLE(); // event id
                }
                if (BitUtil.check(recordFlags, 2)) {
                    buf.readUnsignedIntLE(); // time
                }

                int serviceType = buf.readUnsignedByte();
                buf.readUnsignedByte(); // recipient service type

                int recordEnd = buf.readerIndex() + length;

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

                ByteBuf response = Unpooled.buffer();
                response.writeShortLE(recordIndex);
                response.writeByte(0); // success
                sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);

                while (buf.readerIndex() < recordEnd) {
                    int type = buf.readUnsignedByte();
                    int end = buf.readUnsignedShortLE() + buf.readerIndex();

                    if (type == MSG_TERM_IDENTITY) {
                        useOidAsDeviceId = false;

                        buf.readUnsignedIntLE(); // object id
                        int flags = buf.readUnsignedByte();

                        if (BitUtil.check(flags, 0)) {
                            buf.readUnsignedShortLE(); // home dispatcher identifier
                        }
                        if (BitUtil.check(flags, 1)) {
                            String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim();
                            LOGGER.trace("[{}] Using IMEI1 as deviceId: {}", channel == null ? "" : channel.id(), imei);
                            getDeviceSession(channel, remoteAddress, imei);
                        }
                        if (BitUtil.check(flags, 2)) {
                            String imei = buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim();
                            LOGGER.trace("[{}] Using IMEI2 as deviceId: {}", channel == null ? "" : channel.id(), imei);
                            getDeviceSession(channel, remoteAddress, imei);
                        }
                        if (BitUtil.check(flags, 3)) {
                            buf.skipBytes(3); // language identifier
                        }
                        if (BitUtil.check(flags, 5)) {
                            buf.skipBytes(3); // network identifier
                        }
                        if (BitUtil.check(flags, 6)) {
                            buf.readUnsignedShortLE(); // buffer size
                        }
                        if (BitUtil.check(flags, 7)) {
                            String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim();
                            LOGGER.trace("[{}] Using IMEI7 as deviceId: {}", channel == null ? "" : channel.id(), imei);
                            getDeviceSession(channel, remoteAddress, imei);
                        }

                        response = Unpooled.buffer();
                        response.writeByte(0); // success
                        sendResponse(channel, PT_APPDATA, 0, serviceType, MSG_RESULT_CODE, response);

                    } else if (type == MSG_POS_DATA) {

                        position.setTime(new Date((buf.readUnsignedIntLE() + 1262304000) * 1000)); // since 2010-01-01
                        position.setLatitude(buf.readUnsignedIntLE() * 90.0 / 0xFFFFFFFFL);
                        position.setLongitude(buf.readUnsignedIntLE() * 180.0 / 0xFFFFFFFFL);

                        int flags = buf.readUnsignedByte();
                        position.setValid(BitUtil.check(flags, 0));
                        if (BitUtil.check(flags, 5)) {
                            position.setLatitude(-position.getLatitude());
                        }
                        if (BitUtil.check(flags, 6)) {
                            position.setLongitude(-position.getLongitude());
                        }

                        int speed = buf.readUnsignedShortLE();
                        position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1));
                        position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0));

                        position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE() * 100);
                        position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                        position.set(Position.KEY_EVENT, buf.readUnsignedByte());

                        if (BitUtil.check(flags, 7)) {
                            position.setAltitude(buf.readMediumLE());
                        }

                    } else if (type == MSG_EXT_POS_DATA) {

                        int flags = buf.readUnsignedByte();

                        if (BitUtil.check(flags, 0)) {
                            position.set(Position.KEY_VDOP, buf.readUnsignedShortLE());
                        }
                        if (BitUtil.check(flags, 1)) {
                            position.set(Position.KEY_HDOP, buf.readUnsignedShortLE());
                        }
                        if (BitUtil.check(flags, 2)) {
                            position.set(Position.KEY_PDOP, buf.readUnsignedShortLE());
                        }
                        if (BitUtil.check(flags, 3)) {
                            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                        }

                    } else if (type == MSG_AD_SENSORS_DATA) {

                        buf.readUnsignedByte(); // inputs flags

                        position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());

                        buf.readUnsignedByte(); // adc flags

                    }

                    buf.readerIndex(end);
                }

                if (serviceType == SERVICE_TELEDATA && position.getFixTime() != null) {
                    if (useOidAsDeviceId && oid != 0L) {
                        LOGGER.debug("[{}] Using OID as deviceId: {}", channel == null ? "" : channel.id(), oid);
                        deviceSession = getDeviceSession(channel, remoteAddress, true, String.valueOf(oid));
                        if (deviceSession != null) {
                            position.setDeviceId(deviceSession.getDeviceId());
                        }
                    }
                    if (position.getDeviceId() != 0L) {
                        positions.add(position);
                    }
                }
            }

            if (frameDataLength > 0) {
                buf.readerIndex(frameDataEnd + 2);
            }
        }

        return positions.isEmpty() ? null : positions;
    }

}