From 250dc821dd304ca72058012b5a387194489cbe72 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 29 Jul 2020 20:58:02 -0700 Subject: Implement PST commands --- .../java/org/traccar/protocol/PstFrameEncoder.java | 41 ++++++++++++++ .../java/org/traccar/protocol/PstProtocol.java | 9 +++- .../org/traccar/protocol/PstProtocolDecoder.java | 21 +++----- .../org/traccar/protocol/PstProtocolEncoder.java | 62 ++++++++++++++++++++++ 4 files changed, 118 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/traccar/protocol/PstFrameEncoder.java create mode 100644 src/main/java/org/traccar/protocol/PstProtocolEncoder.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/protocol/PstFrameEncoder.java b/src/main/java/org/traccar/protocol/PstFrameEncoder.java new file mode 100644 index 000000000..33ae43fca --- /dev/null +++ b/src/main/java/org/traccar/protocol/PstFrameEncoder.java @@ -0,0 +1,41 @@ +/* + * Copyright 2020 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.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +public class PstFrameEncoder extends MessageToByteEncoder { + + @Override + protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) { + + out.writeByte('('); + while (msg.isReadable()) { + int b = msg.readUnsignedByte(); + if (b == 0x27 || b == 0x28 || b == 0x29) { + out.writeByte(0x27); + out.writeByte(b ^ 0x40); + } else { + out.writeByte(b); + } + } + out.writeByte(')'); + + msg.release(); + } +} diff --git a/src/main/java/org/traccar/protocol/PstProtocol.java b/src/main/java/org/traccar/protocol/PstProtocol.java index 0ed9affd8..d8c7008cb 100644 --- a/src/main/java/org/traccar/protocol/PstProtocol.java +++ b/src/main/java/org/traccar/protocol/PstProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2019 - 2020 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. @@ -18,20 +18,27 @@ package org.traccar.protocol; import org.traccar.BaseProtocol; import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; +import org.traccar.model.Command; public class PstProtocol extends BaseProtocol { public PstProtocol() { + setSupportedDataCommands( + Command.TYPE_ENGINE_STOP, + Command.TYPE_ENGINE_RESUME); addServer(new TrackerServer(true, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new PstProtocolEncoder(PstProtocol.this)); pipeline.addLast(new PstProtocolDecoder(PstProtocol.this)); } }); addServer(new TrackerServer(false, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { + pipeline.addLast(new PstFrameEncoder()); pipeline.addLast(new PstFrameDecoder()); + pipeline.addLast(new PstProtocolEncoder(PstProtocol.this)); pipeline.addLast(new PstProtocolDecoder(PstProtocol.this)); } }); diff --git a/src/main/java/org/traccar/protocol/PstProtocolDecoder.java b/src/main/java/org/traccar/protocol/PstProtocolDecoder.java index 62cc203d2..e3fe1af62 100644 --- a/src/main/java/org/traccar/protocol/PstProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/PstProtocolDecoder.java @@ -38,6 +38,7 @@ public class PstProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ACK = 0x00; public static final int MSG_STATUS = 0x05; + public static final int MSG_COMMAND = 0x06; private Date readDate(ByteBuf buf) { long value = buf.readUnsignedInt(); @@ -61,21 +62,13 @@ public class PstProtocolDecoder extends BaseProtocolDecoder { Channel channel, SocketAddress remoteAddress, long id, int version, long index, int type) { if (channel != null) { - ByteBuf content = Unpooled.buffer(); - content.writeInt((int) id); - content.writeByte(version); - content.writeInt((int) index); - content.writeByte(MSG_ACK); - content.writeByte(type); - - int checksum = Checksum.crc16(Checksum.CRC16_XMODEM, content.nioBuffer()); - ByteBuf response = Unpooled.buffer(); - response.writeByte('('); - response.writeBytes(content); - content.release(); - response.writeShort(checksum); - response.writeByte(')'); + response.writeInt((int) id); + response.writeByte(version); + response.writeInt((int) index); + response.writeByte(MSG_ACK); + response.writeByte(type); + response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer())); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); diff --git a/src/main/java/org/traccar/protocol/PstProtocolEncoder.java b/src/main/java/org/traccar/protocol/PstProtocolEncoder.java new file mode 100644 index 000000000..f3d193324 --- /dev/null +++ b/src/main/java/org/traccar/protocol/PstProtocolEncoder.java @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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 org.traccar.BaseProtocolEncoder; +import org.traccar.Protocol; +import org.traccar.helper.Checksum; +import org.traccar.model.Command; + +public class PstProtocolEncoder extends BaseProtocolEncoder { + + public PstProtocolEncoder(Protocol protocol) { + super(protocol); + } + + private ByteBuf encodeContent(long deviceId, int type, int data1, int data2) { + + ByteBuf buf = Unpooled.buffer(); + + buf.writeInt((int) Long.parseLong(getUniqueId(deviceId))); + buf.writeByte(0x06); // version + + buf.writeInt(1); // index + buf.writeByte(PstProtocolDecoder.MSG_COMMAND); + buf.writeShort(type); + buf.writeShort(data1); + buf.writeShort(data2); + + buf.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, buf.nioBuffer())); + + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + + switch (command.getType()) { + case Command.TYPE_ENGINE_STOP: + return encodeContent(command.getDeviceId(), 0x0002, 0xffff, 0xffff); + case Command.TYPE_ENGINE_RESUME: + return encodeContent(command.getDeviceId(), 0x0001, 0xffff, 0xffff); + default: + return null; + } + } + +} -- cgit v1.2.3