Example usage for io.netty.buffer ByteBuf nioBuffer

List of usage examples for io.netty.buffer ByteBuf nioBuffer

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf nioBuffer.

Prototype

public abstract ByteBuffer nioBuffer(int index, int length);

Source Link

Document

Exposes this buffer's sub-region as an NIO ByteBuffer .

Usage

From source file:org.traccar.protocol.ApelProtocolDecoder.java

License:Apache License

private void requestArchive(Channel channel) {
    if (lastIndex == 0) {
        lastIndex = newIndex;// w  w  w . ja  va 2s .  c  om
    } else if (newIndex > lastIndex) {
        ByteBuf request = Unpooled.buffer(14);
        request.writeShortLE(MSG_REQUEST_LOG_RECORDS);
        request.writeShortLE(6);
        request.writeIntLE((int) lastIndex);
        request.writeShortLE(512);
        request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 10)));
        channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
    }
}

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  2s. co m*/

    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 www  .j ava  2s .c  o 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.GalileoProtocolEncoder.java

License:Apache License

private ByteBuf encodeText(String uniqueId, String text) {

    ByteBuf buf = Unpooled.buffer(256);

    buf.writeByte(0x01);//from  www  .j a va  2  s .c om
    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.GranitProtocolDecoder.java

License:Apache License

public static void appendChecksum(ByteBuf buffer, int length) {
    buffer.writeByte('*');
    int checksum = Checksum.xor(buffer.nioBuffer(0, length)) & 0xFF;
    String checksumString = String.format("%02X", checksum);
    buffer.writeBytes(checksumString.getBytes(StandardCharsets.US_ASCII));
    buffer.writeByte('\r');
    buffer.writeByte('\n');
}

From source file:org.traccar.protocol.Gt06ProtocolDecoder.java

License:Apache License

private void sendResponse(Channel channel, boolean extended, int type, int index, ByteBuf content) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        int length = 5 + (content != null ? content.readableBytes() : 0);
        if (extended) {
            response.writeShort(0x7979);
            response.writeShort(length);
        } else {// w  w w .  j  av  a 2s .c  o m
            response.writeShort(0x7878);
            response.writeByte(length);
        }
        response.writeByte(type);
        if (content != null) {
            response.writeBytes(content);
            content.release();
        }
        response.writeShort(index);
        response.writeShort(
                Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(2, response.writerIndex() - 2)));
        response.writeByte('\r');
        response.writeByte('\n'); // ending
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.Gt06ProtocolEncoder.java

License:Apache License

private ByteBuf encodeContent(long deviceId, String content) {

    boolean language = Context.getIdentityManager().lookupAttributeBoolean(deviceId, "gt06.language", false,
            true);//from w w w .  j ava2  s.  c o m

    ByteBuf buf = Unpooled.buffer();

    buf.writeByte(0x78);
    buf.writeByte(0x78);

    buf.writeByte(1 + 1 + 4 + content.length() + 2 + 2 + (language ? 2 : 0)); // message length

    buf.writeByte(0x80); // message type

    buf.writeByte(4 + content.length()); // command length
    buf.writeInt(0);
    buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); // command

    if (language) {
        buf.writeShort(2); // english language
    }

    buf.writeShort(0); // message index

    buf.writeShort(Checksum.crc16(Checksum.CRC16_X25, buf.nioBuffer(2, buf.writerIndex() - 2)));

    buf.writeByte('\r');
    buf.writeByte('\n');

    return buf;
}

From source file:org.traccar.protocol.HuabaoProtocolDecoder.java

License:Apache License

public static ByteBuf formatMessage(int type, ByteBuf id, ByteBuf data) {
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(0x7e);/*from w w  w .j  a  v a 2s.  c  o m*/
    buf.writeShort(type);
    buf.writeShort(data.readableBytes());
    buf.writeBytes(id);
    buf.writeShort(1); // index
    buf.writeBytes(data);
    data.release();
    buf.writeByte(Checksum.xor(buf.nioBuffer(1, buf.readableBytes() - 1)));
    buf.writeByte(0x7e);
    return buf;
}

From source file:org.traccar.protocol.RuptelaProtocolEncoder.java

License:Apache License

private ByteBuf encodeContent(int type, ByteBuf content) {

    ByteBuf buf = Unpooled.buffer();

    buf.writeShort(1 + content.readableBytes());
    buf.writeByte(100 + type);//from  w w  w.j a va2s. c  o m
    buf.writeBytes(content);
    buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.nioBuffer(2, buf.writerIndex() - 2)));

    return buf;
}

From source file:org.traccar.protocol.SatsolProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;//from w  ww .ja  v a2s  .  co  m

    buf.readUnsignedShortLE(); // checksum
    buf.readUnsignedShortLE(); // preamble
    long id = buf.readUnsignedIntLE();
    buf.readUnsignedShortLE(); // length

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(id));
    if (deviceSession == null) {
        return null;
    }

    List<Position> positions = new LinkedList<>();

    while (buf.isReadable()) {

        buf.readUnsignedShortLE(); // checksum
        buf.readUnsignedShortLE(); // checksum
        buf.readUnsignedShortLE(); // type
        int length = buf.readUnsignedShortLE();

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
        position.setLatitude(buf.readUnsignedIntLE() * 0.000001);
        position.setLongitude(buf.readUnsignedIntLE() * 0.000001);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.01));
        position.setAltitude(buf.readShortLE());
        position.setCourse(buf.readUnsignedShortLE());
        position.setValid(buf.readUnsignedByte() > 0);

        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        position.set(Position.KEY_EVENT, buf.readUnsignedByte());

        if (BitUtil.check(buf.readUnsignedByte(), 0)) {
            position.set(Position.KEY_ARCHIVE, true);
        }

        positions.add(position);

        buf.skipBytes(length);

    }

    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeShortLE(0);
        response.writeShortLE(0x4CBF); // preamble
        response.writeIntLE((int) id);
        response.writeShortLE(0);
        response.setShortLE(0, Checksum.crc16(Checksum.CRC16_CCITT_FALSE,
                response.nioBuffer(2, response.readableBytes() - 2)));
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return positions;
}