Example usage for io.netty.buffer ByteBuf readIntLE

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

Introduction

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

Prototype

public abstract int readIntLE();

Source Link

Document

Gets a 32-bit integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 4 in this buffer.

Usage

From source file:nearenough.protocol.RtWireTest.java

License:Open Source License

@Test
public void encodeTwoTagMessage() {
    byte[] indxValue = new byte[24];
    byte[] maxtValue = new byte[32];
    Arrays.fill(indxValue, (byte) '1');
    Arrays.fill(maxtValue, (byte) '2');

    RtMessage msg = RtMessage.builder().add(RtTag.INDX, indxValue).add(RtTag.MAXT, maxtValue).build();

    // Wire encoding
    //   4 num_tags
    //   8 (MAXT, INDX) tags
    //   4 INDX offset
    //  24 MAXT value
    //  32 INDX value
    ByteBuf onWire = RtWire.toWire(msg);
    assertThat(onWire.readableBytes(), equalTo(4 + 8 + 4 + 24 + 32));

    // num_tags/*  w ww .j  a  v a  2  s . c om*/
    assertThat(onWire.readIntLE(), equalTo(2));

    // Offset past MAXT value to start of INDX value
    assertThat(onWire.readIntLE(), equalTo(maxtValue.length));

    // MAXT tag
    assertThat(onWire.readInt(), equalTo(RtTag.MAXT.wireEncoding()));
    // INDX tag
    assertThat(onWire.readInt(), equalTo(RtTag.INDX.wireEncoding()));

    // MAXT value
    byte[] readTag1Value = new byte[maxtValue.length];
    onWire.readBytes(readTag1Value);
    assertArrayEquals(maxtValue, readTag1Value);

    // INDX value
    byte[] readTag2Value = new byte[indxValue.length];
    onWire.readBytes(readTag2Value);
    assertArrayEquals(indxValue, readTag2Value);

    // Message was completely read
    assertThat(onWire.readableBytes(), equalTo(0));
}

From source file:org.lanternpowered.server.network.rcon.RconFramingHandler.java

License:MIT License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 4) {
        return;//www  .jav  a 2  s .  c  o  m
    }

    in.markReaderIndex();
    int length = in.readIntLE();
    if (in.readableBytes() < length) {
        in.resetReaderIndex();
        return;
    }

    final ByteBuf buf = ctx.alloc().buffer(length);
    in.readBytes(buf, length);
    out.add(buf);
}

From source file:org.lanternpowered.server.network.rcon.RconHandler.java

License:MIT License

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {
    if (buf.readableBytes() < 8) {
        return;//  www.java2s  . c om
    }

    final int requestId = buf.readIntLE();
    final int type = buf.readIntLE();

    final byte[] payloadData = new byte[buf.readableBytes() - 2];
    buf.readBytes(payloadData);
    final String payload = new String(payloadData, StandardCharsets.UTF_8);

    // Two byte padding
    buf.readBytes(2);

    if (type == TYPE_LOGIN) {
        handleLogin(ctx, payload, this.password, requestId);
    } else if (type == TYPE_COMMAND) {
        handleCommand(ctx, payload, requestId);
    } else {
        sendLargeResponse(ctx, requestId, "Unknown request " + Integer.toHexString(type));
    }
}

From source file:org.redisson.codec.MapCacheEventCodec.java

License:Apache License

private Object decode(ByteBuf buf, State state, Decoder<?> decoder) throws IOException {
    int keyLen;/*  ww w  .  j  av  a 2 s.  co m*/
    if (osType == OSType.WINDOWS) {
        keyLen = buf.readIntLE();
    } else if (osType == OSType.HPNONSTOP) {
        keyLen = (int) buf.readLong();
    } else {
        keyLen = (int) buf.readLongLE();
    }
    ByteBuf keyBuf = buf.readSlice(keyLen);
    Object key = decoder.decode(keyBuf, state);
    return key;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;
    int type = buf.readUnsignedShortLE();
    boolean alarm = (type & 0x8000) != 0;
    type = type & 0x7FFF;//from  ww w .j ava 2  s  .  c o  m
    buf.readUnsignedShortLE(); // length

    if (alarm) {
        sendSimpleMessage(channel, MSG_ACK_ALARM);
    }

    if (type == MSG_TRACKER_ID) {
        return null; // unsupported authentication type
    }

    if (type == MSG_TRACKER_ID_EXT) {

        buf.readUnsignedIntLE(); // id
        int length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        getDeviceSession(channel, remoteAddress, buf.readSlice(length).toString(StandardCharsets.US_ASCII));

    } else if (type == MSG_LAST_LOG_INDEX) {

        long index = buf.readUnsignedIntLE();
        if (index > 0) {
            newIndex = index;
            requestArchive(channel);
        }

    } else if (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }

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

        int recordCount = 1;
        if (type == MSG_LOG_RECORDS) {
            recordCount = buf.readUnsignedShortLE();
        }

        for (int j = 0; j < recordCount; j++) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            int subtype = type;
            if (type == MSG_LOG_RECORDS) {
                position.set(Position.KEY_ARCHIVE, true);
                lastIndex = buf.readUnsignedIntLE() + 1;
                position.set(Position.KEY_INDEX, lastIndex);

                subtype = buf.readUnsignedShortLE();
                if (subtype != MSG_CURRENT_GPS_DATA && subtype != MSG_STATE_FULL_INFO_T104) {
                    buf.skipBytes(buf.readUnsignedShortLE());
                    continue;
                }
                buf.readUnsignedShortLE(); // length
            }

            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);

            if (subtype == MSG_STATE_FULL_INFO_T104) {
                int speed = buf.readUnsignedByte();
                position.setValid(speed != 255);
                position.setSpeed(UnitsConverter.knotsFromKph(speed));
                position.set(Position.KEY_HDOP, buf.readByte());
            } else {
                int speed = buf.readShortLE();
                position.setValid(speed != -1);
                position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.01));
            }

            position.setCourse(buf.readShortLE() * 0.01);
            position.setAltitude(buf.readShortLE());

            if (subtype == MSG_STATE_FULL_INFO_T104) {

                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
                position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());

                for (int i = 1; i <= 8; i++) {
                    position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                }

                position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedIntLE());
                position.set(Position.PREFIX_COUNT + 2, buf.readUnsignedIntLE());
                position.set(Position.PREFIX_COUNT + 3, buf.readUnsignedIntLE());
            }

            positions.add(position);
        }

        buf.readUnsignedIntLE(); // crc

        if (type == MSG_LOG_RECORDS) {
            requestArchive(channel);
        } else {
            sendSimpleMessage(channel, MSG_REQUEST_LAST_LOG_INDEX);
        }

        return positions;
    }

    return null;
}

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

License:Apache License

private void decodeCanData(ByteBuf buf, Position position) {

    buf.readUnsignedMedium(); // packet identifier
    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    int count = buf.readUnsignedByte();
    buf.readUnsignedByte(); // batch count
    buf.readUnsignedShort(); // selector bit
    buf.readUnsignedInt(); // timestamp

    buf.skipBytes(8);/*from w ww .j  a v a 2  s.c om*/

    ArrayList<ByteBuf> values = new ArrayList<>(count);

    for (int i = 0; i < count; i++) {
        values.add(buf.readSlice(8));
    }

    for (int i = 0; i < count; i++) {
        ByteBuf value = values.get(i);
        switch (buf.readInt()) {
        case 0x20D:
            position.set(Position.KEY_RPM, value.readShortLE());
            position.set("dieselTemperature", value.readShortLE() * 0.1);
            position.set("batteryVoltage", value.readShortLE() * 0.01);
            position.set("supplyAirTempDep1", value.readShortLE() * 0.1);
            break;
        case 0x30D:
            position.set("activeAlarm", ByteBufUtil.hexDump(value));
            break;
        case 0x40C:
            position.set("airTempDep1", value.readShortLE() * 0.1);
            position.set("airTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x40D:
            position.set("coldUnitState", ByteBufUtil.hexDump(value));
            break;
        case 0x50C:
            position.set("defrostTempDep1", value.readShortLE() * 0.1);
            position.set("defrostTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x50D:
            position.set("condenserPressure", value.readShortLE() * 0.1);
            position.set("suctionPressure", value.readShortLE() * 0.1);
            break;
        case 0x58C:
            value.readByte();
            value.readShort(); // index
            switch (value.readByte()) {
            case 0x01:
                position.set("setpointZone1", value.readIntLE() * 0.1);
                break;
            case 0x02:
                position.set("setpointZone2", value.readIntLE() * 0.1);
                break;
            case 0x05:
                position.set("unitType", value.readIntLE());
                break;
            case 0x13:
                position.set("dieselHours", value.readIntLE() / 60 / 60);
                break;
            case 0x14:
                position.set("electricHours", value.readIntLE() / 60 / 60);
                break;
            case 0x17:
                position.set("serviceIndicator", value.readIntLE());
                break;
            case 0x18:
                position.set("softwareVersion", value.readIntLE() * 0.01);
                break;
            default:
                break;
            }
            break;
        default:
            LOGGER.warn("Aplicom CAN decoding error", new UnsupportedOperationException());
            break;
        }
    }
}

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

License:Apache License

private Position decodeTelemetry(Channel channel, SocketAddress remoteAddress, DeviceSession deviceSession,
        ByteBuf buf) {

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

    position.setTime(new Date(1009843200000L + buf.readUnsignedIntLE() * 1000)); // seconds since 2002
    position.setLatitude(buf.readIntLE() * 0.0000001);
    position.setLongitude(buf.readIntLE() * 0.0000001);

    position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
    position.set(Position.KEY_FUEL_USED, buf.readUnsignedIntLE());

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE()));
    buf.readUnsignedShortLE(); // max speed

    position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
    buf.readUnsignedIntLE(); // di 3 count
    buf.readUnsignedIntLE(); // di 4 count

    for (int i = 0; i < 5; i++) {
        position.set(Position.PREFIX_ADC + (i + 1), buf.readUnsignedShortLE());
    }//from   w w  w .j  a  v a2 s.  c o  m

    position.setCourse(buf.readUnsignedShortLE());

    position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
    position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
    position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readLongLE());

    int index = buf.readUnsignedShortLE();

    buf.readUnsignedShortLE(); // checksum

    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeInt(0xF1F1F1F1); // sync
        response.writeByte(MSG_TELEMETRY_CONFIRM);
        response.writeShortLE(2); // length
        response.writeShortLE(index);
        response.writeShort(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header
    int length = (buf.readUnsignedShortLE() & 0x7fff) + 3;

    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position(getProtocolName());

    while (buf.readerIndex() < length) {

        // Check if new message started
        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }/*from w  w  w  .j  a v  a2 s  . c  om*/
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName());
        }
        tags.add(tag);

        switch (tag) {

        case TAG_IMEI:
            getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII));
            break;

        case TAG_DATE:
            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            break;

        case TAG_COORDINATES:
            hasLocation = true;
            position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
            position.setLatitude(buf.readIntLE() / 1000000.0);
            position.setLongitude(buf.readIntLE() / 1000000.0);
            break;

        case TAG_SPEED_COURSE:
            position.setSpeed(buf.readUnsignedShortLE() * 0.0539957);
            position.setCourse(buf.readUnsignedShortLE() * 0.1);
            break;

        case TAG_ALTITUDE:
            position.setAltitude(buf.readShortLE());
            break;

        case TAG_STATUS:
            int status = buf.readUnsignedShortLE();
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
            if (BitUtil.check(status, 15)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
            break;

        case TAG_DIGITAL_INPUTS:
            int input = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
            }
            break;

        case TAG_DIGITAL_OUTPUTS:
            int output = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
            }
            break;

        case TAG_INPUT_VOLTAGE1:
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE2:
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE3:
            position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE4:
            position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_XT1:
        case TAG_XT2:
        case TAG_XT3:
            buf.skipBytes(16);
            break;

        default:
            break;

        }
    }

    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }

    sendReply(channel, buf.readUnsignedShortLE());

    for (Position p : positions) {
        p.setDeviceId(deviceSession.getDeviceId());
    }

    if (positions.isEmpty()) {
        return null;
    }

    return positions;
}

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

License:Apache License

private void decodeObd(Position position, ByteBuf buf, boolean groups) {

    int count = buf.readUnsignedByte();

    int[] pids = new int[count];
    for (int i = 0; i < count; i++) {
        pids[i] = buf.readUnsignedShortLE() & 0xff;
    }/* w  w  w . ja  va2s.c o m*/

    if (groups) {
        buf.readUnsignedByte(); // group count
        buf.readUnsignedByte(); // group size
    }

    for (int i = 0; i < count; i++) {
        int value;
        switch (PID_LENGTH_MAP.get(pids[i])) {
        case 1:
            value = buf.readUnsignedByte();
            break;
        case 2:
            value = buf.readUnsignedShortLE();
            break;
        case 4:
            value = buf.readIntLE();
            break;
        default:
            value = 0;
            break;
        }
        position.add(ObdDecoder.decodeData(pids[i], value, false));
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    boolean alternative = buf.getByte(buf.readerIndex() + 3) != 'P';

    buf.skipBytes(4); // system code
    int type = buf.readUnsignedByte();
    long deviceUniqueId = buf.readUnsignedIntLE();

    if (type != MSG_CLIENT_SERIAL) {
        buf.readUnsignedShortLE(); // communication control
    }/*  w w w.ja  v a  2  s .  c  o  m*/
    byte packetNumber = buf.readByte();

    sendReply(channel, remoteAddress, deviceUniqueId, packetNumber);

    if (type == MSG_CLIENT_STATUS) {

        Position position = new Position(getProtocolName());

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceUniqueId));
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte());
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        buf.readUnsignedByte(); // protocol version

        position.set(Position.KEY_STATUS, buf.readUnsignedByte() & 0x0f);

        buf.readUnsignedByte(); // operator / configuration flags
        buf.readUnsignedByte(); // reason data
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));

        position.set("mode", buf.readUnsignedByte());
        position.set(Position.KEY_INPUT, buf.readUnsignedIntLE());

        if (alternative) {
            buf.readUnsignedByte(); // input
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE());
        } else {
            buf.readUnsignedByte(); // operator
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedIntLE());
        }

        position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE());

        buf.skipBytes(6); // multi-purpose data
        buf.readUnsignedShortLE(); // fix time
        buf.readUnsignedByte(); // location status
        buf.readUnsignedByte(); // mode 1
        buf.readUnsignedByte(); // mode 2

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

        position.setValid(true);

        if (alternative) {
            position.setLongitude(buf.readIntLE() / 10000000.0);
            position.setLatitude(buf.readIntLE() / 10000000.0);
        } else {
            position.setLongitude(buf.readIntLE() / Math.PI * 180 / 100000000);
            position.setLatitude(buf.readIntLE() / Math.PI * 180 / 100000000);
        }

        position.setAltitude(buf.readIntLE() * 0.01);

        if (alternative) {
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE()));
            position.setCourse(buf.readUnsignedShortLE() / 1000.0);
        } else {
            position.setSpeed(UnitsConverter.knotsFromMps(buf.readUnsignedIntLE() * 0.01));
            position.setCourse(buf.readUnsignedShortLE() / Math.PI * 180.0 / 1000.0);
        }

        DateBuilder dateBuilder = new DateBuilder()
                .setTimeReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedShortLE());
        position.setTime(dateBuilder.getDate());

        return position;
    }

    return null;
}