Example usage for io.netty.buffer ByteBuf writeShortLE

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

Introduction

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

Prototype

public abstract ByteBuf writeShortLE(int value);

Source Link

Document

Sets the specified 16-bit short integer in the Little Endian Byte Order at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

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);// www  .  j  a va 2  s .c  o m
        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);
    data.writeShortLE(0); // OK

    ByteBuf header = Unpooled.buffer(20);
    header.writeByte(1);//from  ww  w.  ja  v a2 s . com
    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()));
    }
}

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

License:Apache License

private void sendNtcbReply(Channel channel, ByteBuf data) {
    if (channel != null) {
        ByteBuf header = Unpooled.buffer(16);
        header.writeCharSequence(prefix, StandardCharsets.US_ASCII);
        header.writeIntLE((int) deviceUniqueId);
        header.writeIntLE((int) serverId);
        header.writeShortLE(data.readableBytes());
        header.writeByte(Checksum.xor(data.nioBuffer()));
        header.writeByte(Checksum.xor(header.nioBuffer()));

        channel.writeAndFlush(/*www .  j a va 2 s. c om*/
                new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.NoranProtocolDecoder.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 a 2s.c o m*/

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

    if (type == MSG_SHAKE_HAND && channel != null) {

        ByteBuf response = Unpooled.buffer(13);
        response.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
        response.writeByte(0);
        response.writeShortLE(response.capacity());
        response.writeShortLE(MSG_SHAKE_HAND_RESPONSE);
        response.writeByte(1); // status
        response.writeCharSequence("\r\n", StandardCharsets.US_ASCII);

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

    } else if (type == MSG_UPLOAD_POSITION || type == MSG_UPLOAD_POSITION_NEW || type == MSG_CONTROL_RESPONSE
            || type == MSG_ALARM) {

        boolean newFormat = false;
        if (type == MSG_UPLOAD_POSITION && buf.readableBytes() == 48
                || type == MSG_ALARM && buf.readableBytes() == 48
                || type == MSG_CONTROL_RESPONSE && buf.readableBytes() == 57) {
            newFormat = true;
        }

        Position position = new Position(getProtocolName());

        if (type == MSG_CONTROL_RESPONSE) {
            buf.readUnsignedIntLE(); // GIS ip
            buf.readUnsignedIntLE(); // GIS port
        }

        position.setValid(BitUtil.check(buf.readUnsignedByte(), 0));

        short alarm = buf.readUnsignedByte();
        switch (alarm) {
        case 1:
            position.set(Position.KEY_ALARM, Position.ALARM_SOS);
            break;
        case 2:
            position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED);
            break;
        case 3:
            position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT);
            break;
        case 9:
            position.set(Position.KEY_ALARM, Position.ALARM_POWER_OFF);
            break;
        default:
            break;
        }

        if (newFormat) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readFloatLE());
        } else {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            position.setCourse(buf.readUnsignedShortLE());
        }
        position.setLongitude(buf.readFloatLE());
        position.setLatitude(buf.readFloatLE());

        if (!newFormat) {
            long timeValue = buf.readUnsignedIntLE();
            DateBuilder dateBuilder = new DateBuilder().setYear((int) BitUtil.from(timeValue, 26))
                    .setMonth((int) BitUtil.between(timeValue, 22, 26))
                    .setDay((int) BitUtil.between(timeValue, 17, 22))
                    .setHour((int) BitUtil.between(timeValue, 12, 17))
                    .setMinute((int) BitUtil.between(timeValue, 6, 12))
                    .setSecond((int) BitUtil.to(timeValue, 6));
            position.setTime(dateBuilder.getDate());
        }

        ByteBuf rawId;
        if (newFormat) {
            rawId = buf.readSlice(12);
        } else {
            rawId = buf.readSlice(11);
        }
        String id = rawId.toString(StandardCharsets.US_ASCII).replaceAll("[^\\p{Print}]", "");
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        if (newFormat) {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            position.setTime(dateFormat.parse(buf.readSlice(17).toString(StandardCharsets.US_ASCII)));
            buf.readByte();
        }

        if (!newFormat) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
            position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
        } else if (type == MSG_UPLOAD_POSITION_NEW) {
            position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
            position.set(Position.KEY_ODOMETER, buf.readFloatLE());
        }

        return position;
    }

    return null;
}

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

License:Apache License

private ByteBuf encodeContent(String content) {

    ByteBuf buf = Unpooled.buffer(12 + 56);

    buf.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII);
    buf.writeByte(0);/*from  w  w  w  .j  av a 2 s.c  o m*/
    buf.writeShortLE(buf.capacity());
    buf.writeShortLE(NoranProtocolDecoder.MSG_CONTROL);
    buf.writeInt(0); // gis ip
    buf.writeShortLE(0); // gis port
    buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII));
    buf.writerIndex(buf.writerIndex() + 50 - content.length());
    buf.writeCharSequence("\r\n", StandardCharsets.US_ASCII);

    return buf;
}

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

License:Apache License

private void requestArchive(Channel channel) {
    if (lastIndex == 0) {
        lastIndex = newIndex;/*from   ww  w  .j  av a2 s.co  m*/
    } else if (newIndex > lastIndex) {
        ByteBuf request = Unpooled.buffer(12);
        request.writeShortLE(MSG_LOG_SYNC);
        request.writeShortLE(4);
        request.writeIntLE((int) lastIndex);
        request.writeIntLE(0);
        channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
    }
}

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

License:Apache License

private void sendResponse(Channel channel, int type) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(0xaa); // header
        response.writeShortLE(0x85da); // reserved
        response.writeShortLE(15); // length
        response.writeByte(1); // edition
        response.writeShortLE(MSG_RESPONSE);
        response.writeShortLE(type);/*  w w w  .  ja v  a2  s. co  m*/
        response.writeIntLE(0); // command id
        response.writeByte(0); // status
        response.writeByte(0); // result length
        response.writeIntLE(0x20000); // result data ?
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }
}

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  www.j av a  2 s  .  c o 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;
}