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();

Source Link

Document

Exposes this buffer's readable bytes as an NIO ByteBuffer .

Usage

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

License:Apache License

private ByteBuf encodeContent(long deviceId, short type, ByteBuf content) {

    ByteBuf buf = Unpooled.buffer(0);
    String uniqueId = Context.getIdentityManager().getById(deviceId).getUniqueId();

    buf.writeByte('@');
    buf.writeByte('@');

    buf.writeShortLE(2 + 2 + 1 + 20 + 2 + content.readableBytes() + 2 + 2); // length

    buf.writeByte(1); // protocol version

    buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII));
    buf.writeZero(20 - uniqueId.length());

    buf.writeShort(type);//from   w ww .  j  av a2s  .c o  m
    buf.writeBytes(content);

    buf.writeShortLE(Checksum.crc16(Checksum.CRC16_X25, buf.nioBuffer()));

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

    return buf;
}

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

License:Apache License

private ByteBuf encodeContent(int type, ByteBuf content) {

    ByteBuf buf = Unpooled.buffer();

    buf.writeByte('S');
    buf.writeByte('S');
    buf.writeShort(2 + 2 + 2 + content.readableBytes() + 4 + 2 + 2);
    buf.writeShort(type);/*from  w  ww.  j a v  a2  s .com*/
    buf.writeBytes(content);
    buf.writeInt(0x0B);
    buf.writeShort(Checksum.crc16(Checksum.CRC16_KERMIT, buf.nioBuffer()));
    buf.writeByte('\r');
    buf.writeByte('\n');

    return buf;
}

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

License:Apache License

public static ByteBuf encodeContent(boolean connectionless, String uniqueId, int type, int index,
        ByteBuf content) {//  w  w  w.ja v  a2  s  . c o  m

    ByteBuf buf = Unpooled.buffer();

    if (connectionless) {
        buf.writeBytes(DataConverter.parseHex('0' + uniqueId));
    }

    buf.writeByte(0x67);
    buf.writeByte(0x67);
    buf.writeByte(type);
    buf.writeShort(2 + (content != null ? content.readableBytes() : 0)); // length
    buf.writeShort(index);

    if (content != null) {
        buf.writeBytes(content);
    }

    ByteBuf result = Unpooled.buffer();

    if (connectionless) {
        result.writeByte('E');
        result.writeByte('L');
        result.writeShort(2 + buf.readableBytes()); // length
        result.writeShort(checksum(buf.nioBuffer()));
    }

    result.writeBytes(buf);
    buf.release();

    return result;
}

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

License:Apache License

private void sendResponse(Channel channel, int packetType, int index, int serviceType, int type,
        ByteBuf content) {//from   w w w .  ja va 2  s  .  c om
    if (channel != null) {

        ByteBuf data = Unpooled.buffer();
        data.writeByte(type);
        data.writeShortLE(content.readableBytes());
        data.writeBytes(content);
        content.release();

        ByteBuf record = Unpooled.buffer();
        if (packetType == PT_RESPONSE) {
            record.writeShortLE(index);
            record.writeByte(0); // success
        }
        record.writeShortLE(data.readableBytes());
        record.writeShortLE(0);
        record.writeByte(0); // flags (possibly 1 << 6)
        record.writeByte(serviceType);
        record.writeByte(serviceType);
        record.writeBytes(data);
        data.release();
        int recordChecksum = Checksum.crc16(Checksum.CRC16_CCITT_FALSE, record.nioBuffer());

        ByteBuf response = Unpooled.buffer();
        response.writeByte(1); // protocol version
        response.writeByte(0); // security key id
        response.writeByte(0); // flags
        response.writeByte(5 + 2 + 2 + 2); // header length
        response.writeByte(0); // encoding
        response.writeShortLE(record.readableBytes());
        response.writeShortLE(packetId++);
        response.writeByte(packetType);
        response.writeByte(Checksum.crc8(Checksum.CRC8_EGTS, response.nioBuffer()));
        response.writeBytes(record);
        record.release();
        response.writeShortLE(recordChecksum);

        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));

    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;/*from  w w w . j  a v a2  s  . c  o  m*/

    buf.skipBytes(2); // header
    int type = buf.readUnsignedByte();
    buf.readUnsignedShort(); // size

    if (type == MSG_LOGIN || type == MSG_ADMIN_NUMBER || type == MSG_SEND_TEXT || type == MSG_SMS_ALARM_SWITCH
            || type == MSG_POSITION_REUPLOAD) {

        ByteBuf response = Unpooled.buffer();
        response.writeByte(0x29);
        response.writeByte(0x29); // header
        response.writeByte(MSG_CONFIRMATION);
        response.writeShort(5); // size
        response.writeByte(buf.getByte(buf.writerIndex() - 2));
        response.writeByte(type);
        response.writeByte(buf.writerIndex() > 9 ? buf.getByte(9) : 0); // 10th byte
        response.writeByte(Checksum.xor(response.nioBuffer()));
        response.writeByte(0x0D); // ending

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }

    }

    if (type == MSG_ON_DEMAND || type == MSG_POSITION_UPLOAD || type == MSG_POSITION_REUPLOAD
            || type == MSG_ALARM || type == MSG_REPLY || type == MSG_PERIPHERAL) {

        Position position = new Position(getProtocolName());

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readSerialNumber(buf));
        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));
        position.setValid((buf.readUnsignedByte() & 0x80) != 0);

        if (type != MSG_ALARM) {

            position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
            position.set(Position.KEY_STATUS, buf.readUnsignedInt());
            position.set(Position.KEY_HDOP, buf.readUnsignedByte());
            position.set(Position.KEY_VDOP, buf.readUnsignedByte());
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

            buf.skipBytes(5); // other location data

            if (type == MSG_PERIPHERAL) {

                buf.readUnsignedShort(); // data length

                int dataType = buf.readUnsignedByte();

                buf.readUnsignedByte(); // content length

                switch (dataType) {
                case 0x01:
                    position.set(Position.KEY_FUEL_LEVEL,
                            buf.readUnsignedByte() * 100 + buf.readUnsignedByte());
                    break;
                case 0x02:
                    position.set(Position.PREFIX_TEMP + 1,
                            buf.readUnsignedByte() * 100 + buf.readUnsignedByte());
                    break;
                default:
                    break;
                }

            }

        }

        return position;

    }

    return null;
}

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

License:Apache License

private ByteBuf encodeCommand(int command, String uniqueId) {

    ByteBuf buf = Unpooled.buffer();

    buf.writeByte(0x29);//from   ww w  .ja  v  a  2 s . co m
    buf.writeByte(0x29);

    buf.writeByte(command);
    buf.writeShort(6); // size

    uniqueId = "00000000".concat(uniqueId);
    uniqueId = uniqueId.substring(uniqueId.length() - 8);
    buf.writeByte(Integer.parseInt(uniqueId.substring(0, 2)));
    buf.writeByte(Integer.parseInt(uniqueId.substring(2, 4)) + 0x80);
    buf.writeByte(Integer.parseInt(uniqueId.substring(4, 6)) + 0x80);
    buf.writeByte(Integer.parseInt(uniqueId.substring(6, 8)));

    buf.writeByte(Checksum.xor(buf.nioBuffer()));
    buf.writeByte(0x0D); // ending

    return buf;
}

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

License:Apache License

private static void sendResponse(Channel channel, SocketAddress remoteAddress, ByteBuf id, int type,
        ByteBuf msg) {// w  ww .j  a va2  s.co m

    if (channel != null) {
        ByteBuf buf = Unpooled.buffer(2 + 2 + id.readableBytes() + 2 + msg.readableBytes() + 2 + 2);

        buf.writeByte('@');
        buf.writeByte('@');
        buf.writeShort(buf.capacity());
        buf.writeBytes(id);
        buf.writeShort(type);
        buf.writeBytes(msg);
        msg.release();
        buf.writeShort(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.nioBuffer()));
        buf.writeByte('\r');
        buf.writeByte('\n');

        channel.writeAndFlush(new NetworkMessage(buf, remoteAddress));
    }
}

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

License:Apache License

private ByteBuf encodeContent(long deviceId, int type, ByteBuf content) {

    ByteBuf buf = Unpooled.buffer();

    buf.writeByte('@');
    buf.writeByte('@');

    buf.writeShort(2 + 2 + 7 + 2 + content.readableBytes() + 2 + 2); // message length

    buf.writeBytes(DataConverter.parseHex((getUniqueId(deviceId) + "FFFFFFFFFFFFFF").substring(0, 14)));

    buf.writeShort(type);//  w  ww . ja  va  2  s  .  c  om

    buf.writeBytes(content);

    buf.writeShort(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.nioBuffer()));

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

    return buf;
}

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

License:Apache License

private static void sendResponse(Channel channel, int device, long id, int crc) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(device);// w w w .j ava 2s.c  om
        response.writeByte(MSG_ACK);
        response.writeIntLE((int) id);
        response.writeShortLE(crc);
        response.writeShortLE(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));

        ByteBuf encoded = Unpooled.buffer();
        encoded.writeByte(0x01); // header
        while (response.isReadable()) {
            int b = response.readByte();
            if (b == 0x01 || b == 0x04 || b == 0x10 || b == 0x11 || b == 0x13) {
                encoded.writeByte(0x10);
                b += 0x20;
            }
            encoded.writeByte(b);
        }
        response.release();
        encoded.writeByte(0x04); // ending
        channel.writeAndFlush(new NetworkMessage(encoded, channel.remoteAddress()));
    }
}

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

License:Apache License

private void sendAcknowledgment(Channel channel, int sequenceNumber) {
    ByteBuf data = Unpooled.buffer(4);
    data.writeShortLE(sequenceNumber);/*from  w ww.  ja v  a  2 s . co m*/
    data.writeShortLE(0); // OK

    ByteBuf header = Unpooled.buffer(20);
    header.writeByte(1);
    header.writeByte(0);
    header.writeShortLE(senderSequenceNumber++);
    header.writeShortLE(MSG_ACKNOWLEDGEMENT);
    header.writeShortLE(header.capacity() + data.capacity());
    header.writeShortLE(0);
    header.writeShortLE(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, data.nioBuffer()));
    header.writeIntLE(0);
    header.writeIntLE((int) (System.currentTimeMillis() / 1000) + LEAP_SECONDS_DELTA);

    if (channel != null) {
        channel.writeAndFlush(
                new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}