List of usage examples for io.netty.buffer ByteBuf getUnsignedShort
public abstract int getUnsignedShort(int index);
From source file:org.traccar.protocol.AtrackFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() >= 2) { if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) { if (buf.readableBytes() >= KEEPALIVE_LENGTH) { return buf.readRetainedSlice(KEEPALIVE_LENGTH); }/*from ww w . j a v a 2 s.c o m*/ } else if (buf.getUnsignedShort(buf.readerIndex()) == 0x4050 && buf.getByte(buf.readerIndex() + 2) != ',') { if (buf.readableBytes() > 6) { int length = buf.getUnsignedShort(buf.readerIndex() + 4) + 4 + 2; if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } } else { int lengthStart = buf.indexOf(buf.readerIndex() + 3, buf.writerIndex(), (byte) ',') + 1; if (lengthStart > 0) { int lengthEnd = buf.indexOf(lengthStart, buf.writerIndex(), (byte) ','); if (lengthEnd > 0) { int length = lengthEnd + Integer.parseInt( buf.toString(lengthStart, lengthEnd - lengthStart, StandardCharsets.US_ASCII)); if (buf.readableBytes() > length && buf.getByte(buf.readerIndex() + length) == '\n') { length += 1; } if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } } else { int endIndex = BufferUtil.indexOf("\r\n", buf); if (endIndex > 0) { return buf.readRetainedSlice(endIndex - buf.readerIndex() + 2); } } } } return null; }
From source file:org.traccar.protocol.AtrackProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) { if (channel != null) { channel.writeAndFlush(new NetworkMessage(buf.retain(), remoteAddress)); // keep-alive message }/* w ww. ja va 2s . co m*/ return null; } else if (buf.getByte(buf.readerIndex()) == '$') { return decodeInfo(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim()); } else if (buf.getByte(buf.readerIndex() + 2) == ',') { return decodeText(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII).trim()); } else { return decodeBinary(channel, remoteAddress, buf); } }
From source file:org.traccar.protocol.EgtsProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2); buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3)); List<Position> positions = new LinkedList<>(); while (buf.readableBytes() > 2) { int length = buf.readUnsignedShortLE(); int recordIndex = buf.readUnsignedShortLE(); int recordFlags = buf.readUnsignedByte(); if (BitUtil.check(recordFlags, 0)) { buf.readUnsignedIntLE(); // object id }//w w w. ja v a2 s .com 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) { buf.readUnsignedIntLE(); // object id int flags = buf.readUnsignedByte(); if (BitUtil.check(flags, 0)) { buf.readUnsignedShortLE(); // home dispatcher identifier } if (BitUtil.check(flags, 1)) { getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim()); } if (BitUtil.check(flags, 2)) { getDeviceSession(channel, remoteAddress, buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim()); } 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)) { getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim()); } 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 && deviceSession != null) { positions.add(position); } } return positions.isEmpty() ? null : positions; }
From source file:org.traccar.protocol.Gl200FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < MINIMUM_LENGTH) { return null; }/*w w w . ja v a 2 s .c o m*/ if (isBinary(buf)) { int length; switch (buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)) { case "+ACK": length = buf.getUnsignedByte(buf.readerIndex() + 6); break; case "+INF": case "+BNF": length = buf.getUnsignedShort(buf.readerIndex() + 7); break; case "+HBD": length = buf.getUnsignedByte(buf.readerIndex() + 5); break; case "+CRD": case "+BRD": length = buf.getUnsignedShort(buf.readerIndex() + 6); break; default: length = buf.getUnsignedShort(buf.readerIndex() + 9); break; } if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } else { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '$'); if (endIndex < 0) { endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0); } if (endIndex > 0) { ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex()); buf.readByte(); // delimiter return frame; } } return null; }
From source file:org.traccar.protocol.Gps056ProtocolDecoder.java
License:Apache License
private static double decodeCoordinate(ByteBuf buf) { double degrees = buf.getUnsignedShort(buf.readerIndex()) / 100; double minutes = buf.readUnsignedShort() % 100 + buf.readUnsignedShort() * 0.0001; degrees += minutes / 60;/*ww w. j a v a 2s . co m*/ byte hemisphere = buf.readByte(); if (hemisphere == 'S' || hemisphere == 'W') { degrees = -degrees; } return degrees; }
From source file:org.traccar.protocol.Gt06FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 5) { return null; }//from w w w.ja va 2 s. c o m int length = 2 + 2; // head and tail if (buf.getByte(buf.readerIndex()) == 0x78) { length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2); } else { length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2); } if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) { return buf.readRetainedSlice(length); } int endIndex = buf.readerIndex() - 1; do { endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d); if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) { return buf.readRetainedSlice(endIndex + 2 - buf.readerIndex()); } } while (endIndex > 0); return null; }
From source file:org.traccar.protocol.IntellitracFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { // Check minimum length if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) { return null; }//w w w . ja v a 2s . c o m // Check for sync packet if (buf.getUnsignedShort(buf.readerIndex()) == 0xFAF8) { ByteBuf syncMessage = buf.readRetainedSlice(8); if (ctx != null && ctx.channel() != null) { ctx.channel().writeAndFlush(new NetworkMessage(syncMessage, ctx.channel().remoteAddress())); } } return super.decode(ctx, buf); }
From source file:org.traccar.protocol.Jt600FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }/*from w ww . j a va2 s. c o m*/ char type = (char) buf.getByte(buf.readerIndex()); if (type == '$') { boolean longFormat = buf.getUnsignedByte(buf.readerIndex() + 1) == 0x75; int length = buf.getUnsignedShort(buf.readerIndex() + (longFormat ? 8 : 7)) + 10; if (length <= buf.readableBytes()) { return buf.readRetainedSlice(length); } } else if (type == '(') { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')'); if (endIndex != -1) { return buf.readRetainedSlice(endIndex + 1); } } else { throw new ParseException(null, 0); // unknown message } return null; }
From source file:org.traccar.protocol.MeiligaoFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { // Strip not '$' (0x24) bytes from the beginning while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != 0x24) { buf.readByte();/*from w w w . j a v a 2 s .c o m*/ } // Check length and return buffer if (buf.readableBytes() >= MESSAGE_HEADER) { int length = buf.getUnsignedShort(buf.readerIndex() + 2); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } return null; }
From source file:org.traccar.protocol.NavisFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.getByte(buf.readerIndex()) == 0x7F) { return buf.readRetainedSlice(1); // keep alive }//from w ww.j av a 2 s . c o m if (ctx != null && flexDataSize == 0) { NavisProtocolDecoder protocolDecoder = BasePipelineFactory.getHandler(ctx.pipeline(), NavisProtocolDecoder.class); if (protocolDecoder != null) { flexDataSize = protocolDecoder.getFlexDataSize(); } } if (flexDataSize > 0) { if (buf.readableBytes() > FLEX_HEADER_LENGTH) { int length = 0; String type = buf.toString(buf.readerIndex(), 2, StandardCharsets.US_ASCII); switch (type) { // FLEX 1.0 case "~A": length = flexDataSize * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH) + 1 + 1; break; case "~T": length = flexDataSize + 4 + 1; break; case "~C": length = flexDataSize + 1; break; // FLEX 2.0 (Extra packages) case "~E": length++; for (int i = 0; i < buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH); i++) { if (buf.readableBytes() > FLEX_HEADER_LENGTH + length + 1) { length += buf.getUnsignedShort(length + FLEX_HEADER_LENGTH) + 2; } else { return null; } } length++; break; case "~X": length = buf.getUnsignedShortLE(buf.readerIndex() + FLEX_HEADER_LENGTH) + 4 + 1; break; default: return null; } if (buf.readableBytes() >= FLEX_HEADER_LENGTH + length) { return buf.readRetainedSlice(buf.readableBytes()); } } } else { if (buf.readableBytes() < NTCB_HEADER_LENGTH) { return null; } int length = NTCB_HEADER_LENGTH + buf.getUnsignedShortLE(buf.readerIndex() + NTCB_LENGTH_OFFSET); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } return null; }