From 685eaa95a6633fc840bae47a7ced290f37dbd4fb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 18 Aug 2019 18:34:02 -0700 Subject: Fix modular type response --- .../protocol/CellocatorProtocolDecoder.java | 67 +++++++++++++++------- .../protocol/CellocatorProtocolEncoder.java | 37 +++++++----- 2 files changed, 71 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/protocol/CellocatorProtocolDecoder.java b/src/main/java/org/traccar/protocol/CellocatorProtocolDecoder.java index 14d14f4b6..ea4975d3e 100644 --- a/src/main/java/org/traccar/protocol/CellocatorProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/CellocatorProtocolDecoder.java @@ -43,29 +43,52 @@ public class CellocatorProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_SERVER_ACKNOWLEDGE = 4; - private byte commandCount; + public static ByteBuf encodeContent(int type, int uniqueId, int packetNumber, ByteBuf content) { + + ByteBuf buf = Unpooled.buffer(); + buf.writeByte('M'); + buf.writeByte('C'); + buf.writeByte('G'); + buf.writeByte('P'); + buf.writeByte(type); + buf.writeIntLE(uniqueId); + buf.writeByte(packetNumber); + buf.writeIntLE(0); // authentication code + buf.writeBytes(content); + + byte checksum = 0; + for (int i = 4; i < buf.writerIndex(); i++) { + checksum += buf.getByte(i); + } + buf.writeByte(checksum); + + return buf; + } private void sendResponse(Channel channel, SocketAddress remoteAddress, long deviceId, byte packetNumber) { if (channel != null) { - ByteBuf reply = Unpooled.buffer(28); - reply.writeByte('M'); - reply.writeByte('C'); - reply.writeByte('G'); - reply.writeByte('P'); - reply.writeByte(MSG_SERVER_ACKNOWLEDGE); - reply.writeIntLE((int) deviceId); - reply.writeByte(commandCount++); - reply.writeIntLE(0); // authentication code - reply.writeByte(0); - reply.writeByte(packetNumber); - reply.writeZero(11); - - byte checksum = 0; - for (int i = 4; i < 27; i++) { - checksum += reply.getByte(i); - } - reply.writeByte(checksum); + ByteBuf content = Unpooled.buffer(); + content.writeByte(0); + content.writeByte(packetNumber); + content.writeZero(11); + ByteBuf reply = encodeContent(MSG_SERVER_ACKNOWLEDGE, (int) deviceId, packetNumber, content); + channel.writeAndFlush(new NetworkMessage(reply, remoteAddress)); + } + } + + private void sendModuleResponse(Channel channel, SocketAddress remoteAddress, long deviceId, byte packetNumber) { + if (channel != null) { + ByteBuf content = Unpooled.buffer(); + content.writeByte(0x80); + content.writeShortLE(10); // modules length + content.writeIntLE(0); // reserved + content.writeByte(9); // ack module type + content.writeShortLE(3); // module length + content.writeByte(0); // ack + content.writeShortLE(0); // reserved + + ByteBuf reply = encodeContent(MSG_SERVER_ACKNOWLEDGE, (int) deviceId, packetNumber, content); channel.writeAndFlush(new NetworkMessage(reply, remoteAddress)); } } @@ -225,7 +248,11 @@ public class CellocatorProtocolDecoder extends BaseProtocolDecoder { } byte packetNumber = buf.readByte(); - sendResponse(channel, remoteAddress, deviceUniqueId, packetNumber); + if (type == MSG_CLIENT_MODULAR_EXT) { + sendModuleResponse(channel, remoteAddress, deviceUniqueId, packetNumber); + } else { + sendResponse(channel, remoteAddress, deviceUniqueId, packetNumber); + } if (type == MSG_CLIENT_STATUS) { return decodeStatus(buf, deviceSession, alternative); diff --git a/src/main/java/org/traccar/protocol/CellocatorProtocolEncoder.java b/src/main/java/org/traccar/protocol/CellocatorProtocolEncoder.java index 52de4593c..76fa67686 100644 --- a/src/main/java/org/traccar/protocol/CellocatorProtocolEncoder.java +++ b/src/main/java/org/traccar/protocol/CellocatorProtocolEncoder.java @@ -27,24 +27,18 @@ public class CellocatorProtocolEncoder extends BaseProtocolEncoder { super(protocol); } - private ByteBuf encodeContent(long deviceId, int command, int data1, int data2) { + public static ByteBuf encodeContent(int type, int uniqueId, int packetNumber, ByteBuf content) { - ByteBuf buf = Unpooled.buffer(0); + ByteBuf buf = Unpooled.buffer(); buf.writeByte('M'); buf.writeByte('C'); buf.writeByte('G'); buf.writeByte('P'); - buf.writeByte(0); - buf.writeIntLE(Integer.parseInt(getUniqueId(deviceId))); - buf.writeByte(0); // command numerator + buf.writeByte(type); + buf.writeIntLE(uniqueId); + buf.writeByte(packetNumber); buf.writeIntLE(0); // authentication code - buf.writeByte(command); - buf.writeByte(command); - buf.writeByte(data1); - buf.writeByte(data1); - buf.writeByte(data2); - buf.writeByte(data2); - buf.writeIntLE(0); // command specific data + buf.writeBytes(content); byte checksum = 0; for (int i = 4; i < buf.writerIndex(); i++) { @@ -55,6 +49,23 @@ public class CellocatorProtocolEncoder extends BaseProtocolEncoder { return buf; } + private ByteBuf encodeCommand(long deviceId, int command, int data1, int data2) { + + ByteBuf content = Unpooled.buffer(); + content.writeByte(command); + content.writeByte(command); + content.writeByte(data1); + content.writeByte(data1); + content.writeByte(data2); + content.writeByte(data2); + content.writeIntLE(0); // command specific data + + ByteBuf buf = encodeContent(0, Integer.parseInt(getUniqueId(deviceId)), 0, content); + content.release(); + + return buf; + } + @Override protected Object encodeCommand(Command command) { @@ -62,7 +73,7 @@ public class CellocatorProtocolEncoder extends BaseProtocolEncoder { case Command.TYPE_OUTPUT_CONTROL: int data = Integer.parseInt(command.getString(Command.KEY_DATA)) << 4 + command.getInteger(Command.KEY_INDEX); - return encodeContent(command.getDeviceId(), 0x03, data, 0); + return encodeCommand(command.getDeviceId(), 0x03, data, 0); default: return null; } -- cgit v1.2.3