From 1e0e3a43894466246d6042ec71b9c01f63c58b13 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 01:28:00 +0200 Subject: binary decoder test --- src/org/traccar/protocol/WatchFrameDecoder.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/protocol/WatchFrameDecoder.java') diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index b2ac5b4f7..b4a82c0be 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -33,7 +33,8 @@ public class WatchFrameDecoder extends BaseFrameDecoder { while (buf.readerIndex() < endIndex) { byte b = buf.readByte(); if (b == '}') { - switch (buf.readByte()) { + byte c = buf.readByte(); + switch (c) { case 0x01: frame.writeByte('}'); break; @@ -50,7 +51,8 @@ public class WatchFrameDecoder extends BaseFrameDecoder { frame.writeByte('*'); break; default: - throw new IllegalArgumentException(); + throw new IllegalArgumentException(String.format( + "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, c)); } } else { frame.writeByte(b); -- cgit v1.2.3 From 074b1c302dcd6671737d44c69aa83d0fdd179168 Mon Sep 17 00:00:00 2001 From: Gavriel Fleischer Date: Sun, 17 Feb 2019 21:42:29 +0200 Subject: resolving review issues --- src/org/traccar/protocol/WatchFrameDecoder.java | 12 +++++------ src/org/traccar/protocol/WatchProtocolDecoder.java | 24 +++++++++------------- .../traccar/protocol/WatchProtocolDecoderTest.java | 2 +- 3 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src/org/traccar/protocol/WatchFrameDecoder.java') diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index b4a82c0be..f99bd52e2 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -31,10 +31,10 @@ public class WatchFrameDecoder extends BaseFrameDecoder { if (endIndex > 0) { ByteBuf frame = Unpooled.buffer(); while (buf.readerIndex() < endIndex) { - byte b = buf.readByte(); - if (b == '}') { - byte c = buf.readByte(); - switch (c) { + byte b1 = buf.readByte(); + if (b1 == '}') { + byte b2 = buf.readByte(); + switch (b2) { case 0x01: frame.writeByte('}'); break; @@ -52,10 +52,10 @@ public class WatchFrameDecoder extends BaseFrameDecoder { break; default: throw new IllegalArgumentException(String.format( - "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, c)); + "unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, b2)); } } else { - frame.writeByte(b); + frame.writeByte(b1); } } return frame; diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index aaf63206c..70b207e9b 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -67,7 +67,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("(.*)") // cell and wifi .compile(); - private void sendTextResponse(Channel channel, String id, String index, String content) { + private void sendResponse(Channel channel, String id, String index, String content) { if (channel != null) { String response; if (index != null) { @@ -182,10 +182,6 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { return manufacturer; } - protected int nextIndexOf(ByteBuf buf, char delimiter) { - return buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) delimiter); - } - @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -196,7 +192,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { manufacturer = buf.readSlice(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter - int idIndex = nextIndexOf(buf, '*'); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); String id = buf.readSlice(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { @@ -206,10 +202,10 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(1); // '*' delimiter String index = null; - int contentIndex = nextIndexOf(buf, '*'); + int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { - int indexLength = nextIndexOf(buf, '*') - buf.readerIndex(); + int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); hasIndex = true; index = buf.readSlice(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // '*' delimiter @@ -220,7 +216,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.writerIndex(buf.writerIndex() - 1); // ']' ignore ending - contentIndex = nextIndexOf(buf, ','); + contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } @@ -233,11 +229,11 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (type.equals("INIT")) { - sendTextResponse(channel, id, index, "INIT,1"); + sendResponse(channel, id, index, "INIT,1"); } else if (type.equals("LK")) { - sendTextResponse(channel, id, index, "LK"); + sendResponse(channel, id, index, "LK"); if (buf.isReadable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); @@ -262,14 +258,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (position != null) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } - sendTextResponse(channel, id, index, "AL"); + sendResponse(channel, id, index, "AL"); } return position; } else if (type.equals("TKQ")) { - sendTextResponse(channel, id, index, "TKQ"); + sendResponse(channel, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { @@ -311,7 +307,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (buf.readableBytes() == 1) { byte result = buf.readByte(); if (result != '1') { - LOGGER.error(type + "," + result); + LOGGER.warn(type + "," + result); } return null; } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index c4e5bf61a..f1287d721 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -114,7 +114,7 @@ public class WatchProtocolDecoderTest extends ProtocolTest { } @Test - public void testDecodeTK() throws Exception { + public void testDecodeVoiceMessage() throws Exception { WatchProtocolDecoder decoder = new WatchProtocolDecoder(null); verifyNull(decoder.decode(null, null, buffer("[CS*1234567890*0004*TK,1]"))); -- cgit v1.2.3