List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, SocketAddress remoteAddress, int version, ByteBuf id, short type, ByteBuf content) {/*from w w w . j a v a 2 s .c om*/ if (channel != null) { int length = 2 + 2 + 1 + id.readableBytes() + 2 + 2 + 2; if (content != null) { length += content.readableBytes(); } ByteBuf response = Unpooled.buffer(length); response.writeByte('@'); response.writeByte('@'); response.writeShortLE(length); response.writeByte(version); response.writeBytes(id); response.writeShort(type); if (content != null) { response.writeBytes(content); content.release(); } response.writeShortLE( Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } }
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, SocketAddress remoteAddress, ByteBuf id, short type) { if (channel != null) { int length = 2 + 2 + id.readableBytes() + 2 + 4 + 8 + 2 + 2; ByteBuf response = Unpooled.buffer(length); response.writeByte('@'); response.writeByte('@'); response.writeShortLE(length);//from w w w. ja v a2s .co m response.writeBytes(id); response.writeShort(type); response.writeIntLE(0); for (int i = 0; i < 8; i++) { response.writeByte(0xff); } response.writeShortLE( Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } }
From source file:org.traccar.protocol.CellocatorProtocolEncoder.java
License:Apache License
private ByteBuf encodeContent(long deviceId, int command, int data1, int data2) { ByteBuf buf = Unpooled.buffer(0); buf.writeByte('M'); buf.writeByte('C'); buf.writeByte('G'); buf.writeByte('P'); buf.writeByte(0);//from w w w . j a v a 2 s. c o m buf.writeIntLE(Integer.parseInt(getUniqueId(deviceId))); buf.writeByte(0); // command numerator 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 byte checksum = 0; for (int i = 4; i < buf.writerIndex(); i++) { checksum += buf.getByte(i); } buf.writeByte(checksum); return buf; }
From source file:org.traccar.protocol.EnforaProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; // Find IMEI number int index = -1; for (int i = buf.readerIndex(); i < buf.writerIndex() - IMEI_LENGTH; i++) { index = i;/*from w ww .j a va2 s .c o m*/ for (int j = i; j < i + IMEI_LENGTH; j++) { if (!Character.isDigit((char) buf.getByte(j))) { index = -1; break; } } if (index > 0) { break; } } if (index == -1) { return null; } String imei = buf.toString(index, IMEI_LENGTH, StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; } // Find NMEA sentence int start = BufferUtil.indexOf("GPRMC", buf); if (start == -1) { return null; } String sentence = buf.toString(start, buf.readableBytes() - start, StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setValid(parser.next().equals("A")); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); position.setSpeed(parser.nextDouble(0)); position.setCourse(parser.nextDouble(0)); dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); return position; }
From source file:org.traccar.protocol.EskyFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 'E')); int endIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 'E'); if (endIndex > 0) { return buf.readRetainedSlice(endIndex - buf.readerIndex()); } else {/* w ww . j a v a 2s .c o m*/ return buf.readRetainedSlice(buf.readableBytes()); // assume full frame } }
From source file:org.traccar.protocol.GalileoProtocolEncoder.java
License:Apache License
private ByteBuf encodeText(String uniqueId, String text) { ByteBuf buf = Unpooled.buffer(256); buf.writeByte(0x01);//from ww w . j a v a 2 s . c o m buf.writeShortLE(uniqueId.length() + text.length() + 11); buf.writeByte(0x03); // imei tag buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII)); buf.writeByte(0x04); // device id tag buf.writeShortLE(0); // not needed if imei provided buf.writeByte(0xE0); // index tag buf.writeIntLE(0); // index buf.writeByte(0xE1); // command text tag buf.writeByte(text.length()); buf.writeBytes(text.getBytes(StandardCharsets.US_ASCII)); buf.writeShortLE(Checksum.crc16(Checksum.CRC16_MODBUS, buf.nioBuffer(0, buf.writerIndex()))); return buf; }
From source file:org.traccar.protocol.GatorProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // length String id = decodeId(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());/*from ww w.jav a 2 s . c o m*/ sendResponse(channel, remoteAddress, buf.getByte(buf.writerIndex() - 2)); if (type == MSG_POSITION_DATA || type == MSG_ROLLCALL_RESPONSE || type == MSG_ALARM_DATA || type == MSG_BLIND_AREA) { Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, "1" + id, id); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2)) .setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)) .setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)) .setSecond(BcdUtil.readInteger(buf, 2)); position.setTime(dateBuilder.getDate()); position.setLatitude(BcdUtil.readCoordinate(buf)); position.setLongitude(BcdUtil.readCoordinate(buf)); position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4))); position.setCourse(BcdUtil.readInteger(buf, 4)); int flags = buf.readUnsignedByte(); position.setValid((flags & 0x80) != 0); position.set(Position.KEY_SATELLITES, flags & 0x0f); position.set(Position.KEY_STATUS, buf.readUnsignedByte()); position.set("key", buf.readUnsignedByte()); position.set("oil", buf.readUnsignedShort() / 10.0); position.set(Position.KEY_POWER, buf.readUnsignedByte() + buf.readUnsignedByte() * 0.01); position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); return position; } return null; }
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; }/*from www.j a va2 s . c om*/ 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.GranitFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { int indexEnd = BufferUtil.indexOf("\r\n", buf); if (indexEnd != -1) { int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~'); if (indexTilde != -1 && indexTilde < indexEnd) { int length = buf.getUnsignedShortLE(indexTilde + 1); indexEnd = BufferUtil.indexOf("\r\n", buf, indexTilde + 2 + length, buf.writerIndex()); if (indexEnd == -1) { return null; }// w w w. j a v a 2 s .c o m } ByteBuf frame = buf.readRetainedSlice(indexEnd - buf.readerIndex()); buf.skipBytes(2); return frame; } return null; }
From source file:org.traccar.protocol.GranitProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~'); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null && indexTilde == -1) { String bufString = buf.toString(StandardCharsets.US_ASCII); Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date()); getLastLocation(position, new Date()); position.setValid(false);// ww w . j a va 2s. c om position.set(Position.KEY_RESULT, bufString); return position; } if (buf.readableBytes() < HEADER_LENGTH) { return null; } String header = buf.readSlice(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); if (header.equals("+RRCB~")) { buf.skipBytes(2); // binary length 26 int deviceId = buf.readUnsignedShortLE(); deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); if (deviceSession == null) { return null; } long unixTime = buf.readUnsignedIntLE(); if (channel != null) { sendResponseCurrent(channel, deviceId, unixTime); } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(unixTime * 1000)); decodeStructure(buf, position); return position; } else if (header.equals("+DDAT~")) { buf.skipBytes(2); // binary length int deviceId = buf.readUnsignedShortLE(); deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); if (deviceSession == null) { return null; } byte format = buf.readByte(); if (format != 4) { return null; } byte nblocks = buf.readByte(); int packNum = buf.readUnsignedShortLE(); if (channel != null) { sendResponseArchive(channel, deviceId, packNum); } List<Position> positions = new ArrayList<>(); while (nblocks > 0) { nblocks--; long unixTime = buf.readUnsignedIntLE(); int timeIncrement = buf.getUnsignedShortLE(buf.readerIndex() + 120); for (int i = 0; i < 6; i++) { if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date((unixTime + i * timeIncrement) * 1000)); decodeStructure(buf, position); position.set(Position.KEY_ARCHIVE, true); positions.add(position); } else { buf.skipBytes(20); // skip filled 0xFE structure } } buf.skipBytes(2); // increment } return positions; } return null; }