From 9f2f1e1b524151aeb9e1d21e67f7de2b1597fabc Mon Sep 17 00:00:00 2001 From: Sergey Kostin <7er@inbox.ru> Date: Mon, 5 Aug 2019 11:14:52 +0300 Subject: Egts frames split in decoder --- .../org/traccar/protocol/EgtsProtocolDecoder.java | 283 ++++++++++----------- 1 file changed, 136 insertions(+), 147 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java b/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java index 53f3e49f9..75f32f1bb 100644 --- a/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/EgtsProtocolDecoder.java @@ -129,173 +129,162 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { List 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 - just skip them + return null; + } - 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); + int frameDataEnd = buf.readerIndex() + frameDataLength; + long oid = 0L; + while (buf.readerIndex() < frameDataEnd) { - 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 length = buf.readUnsignedShortLE(); + int recordIndex = buf.readUnsignedShortLE(); + int recordFlags = buf.readUnsignedByte(); + + if (BitUtil.check(recordFlags, 0)) { + oid = buf.readUnsignedIntLE(); // object id } - int frameDataEnd = buf.readerIndex() + frameDataLength; - long oid = 0L; - while (buf.readerIndex() < frameDataEnd) { + if (BitUtil.check(recordFlags, 1)) { + buf.readUnsignedIntLE(); // event id + } + if (BitUtil.check(recordFlags, 2)) { + buf.readUnsignedIntLE(); // time + } - int length = buf.readUnsignedShortLE(); - int recordIndex = buf.readUnsignedShortLE(); - int recordFlags = buf.readUnsignedByte(); + int serviceType = buf.readUnsignedByte(); + buf.readUnsignedByte(); // recipient service type - if (BitUtil.check(recordFlags, 0)) { - oid = buf.readUnsignedIntLE(); // object id - } + int recordEnd = buf.readerIndex() + length; - if (BitUtil.check(recordFlags, 1)) { - buf.readUnsignedIntLE(); // event id - } - if (BitUtil.check(recordFlags, 2)) { - buf.readUnsignedIntLE(); // time - } + Position position = new Position(getProtocolName()); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + position.setDeviceId(deviceSession.getDeviceId()); + } - int serviceType = buf.readUnsignedByte(); - buf.readUnsignedByte(); // recipient service type + ByteBuf response = Unpooled.buffer(); + response.writeShortLE(recordIndex); + response.writeByte(0); // success + sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response); - int recordEnd = buf.readerIndex() + length; + while (buf.readerIndex() < recordEnd) { + int type = buf.readUnsignedByte(); + int end = buf.readUnsignedShortLE() + buf.readerIndex(); - Position position = new Position(getProtocolName()); - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession != null) { - position.setDeviceId(deviceSession.getDeviceId()); - } + if (type == MSG_TERM_IDENTITY) { + useOidAsDeviceId = false; - 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.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); } - buf.readerIndex(end); - } + 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); - 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()); - } + int flags = buf.readUnsignedByte(); + position.setValid(BitUtil.check(flags, 0)); + if (BitUtil.check(flags, 5)) { + position.setLatitude(-position.getLatitude()); } - if (position.getDeviceId() != 0L) { - positions.add(position); + 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 (frameDataLength > 0) { - buf.readerIndex(frameDataEnd + 2); + 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); + } } } -- cgit v1.2.3