Example usage for io.netty.buffer ByteBuf readLongLE

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

Introduction

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

Prototype

public abstract long readLongLE();

Source Link

Document

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

Usage

From source file:com.ibasco.agql.protocols.valve.source.query.packets.response.SourceInfoResponsePacket.java

License:Open Source License

@Override
public SourceServer toObject() {
    ByteBuf data = getPayloadBuffer();

    SourceServer server = new SourceServer();

    //Start Decoding
    server.setNetworkVersion(data.readByte());
    server.setName(readString(data));//  w w w  .  j  a  va2s.c  o  m
    server.setMapName(readString(data));
    server.setGameDirectory(readString(data));
    server.setGameDescription(readString(data));
    server.setAppId(data.readShortLE());
    server.setNumOfPlayers(data.readByte());
    server.setMaxPlayers(data.readByte());
    server.setNumOfBots(data.readByte());
    server.setDedicated(data.readByte() == 1);
    server.setOperatingSystem((char) data.readByte());
    server.setPasswordProtected(data.readByte() == 1);
    server.setSecure(data.readByte() == 1);
    server.setGameVersion(readString(data));

    if (data.readableBytes() > 0) {
        byte extraDataFlag = data.readByte();

        if ((extraDataFlag & EDF_GAME_PORT) != 0) {
            data.readShortLE(); //discard, we already know which port based on the sender address
        }

        if ((extraDataFlag & EDF_SERVER_ID) != 0) {
            //this.info.put("serverId", Long.reverseBytes((this.contentData.getInt() << 32) | this.contentData.getInt()));
            server.setServerId(data.readLongLE());
        }

        if ((extraDataFlag & EDF_SOURCE_TV) != 0) {
            server.setTvPort(data.readShortLE());
            server.setTvName(readString(data));
        }

        if ((extraDataFlag & EDF_SERVER_TAGS) != 0) {
            server.setServerTags(readString(data));
        }

        if ((extraDataFlag & EDF_GAME_ID) != 0) {
            server.setGameId(data.readLongLE());
            //this.info.put("gameId", Long.reverseBytes((this.contentData.getInt() << 32) | this.contentData.getInt()));
        }
    }

    return server;
}

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

License:Apache License

private Object decode(ByteBuf buf, State state, Decoder<?> decoder) throws IOException {
    int keyLen;/* w w  w .  j a va  2s  .c  o 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.AdmProtocolDecoder.java

License:Apache License

private Position decodeData(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int type) {

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }//from  w  ww. ja  va  2  s  .c o m

    if (BitUtil.to(type, 2) == 0) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        position.set(Position.KEY_INDEX, buf.readUnsignedShortLE());

        int status = buf.readUnsignedShortLE();
        position.set(Position.KEY_STATUS, status);
        position.setValid(!BitUtil.check(status, 5));
        position.setLatitude(buf.readFloatLE());
        position.setLongitude(buf.readFloatLE());
        position.setCourse(buf.readUnsignedShortLE() * 0.1);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.1));

        position.set(Position.KEY_ACCELERATION, buf.readUnsignedByte() * 0.1);
        position.setAltitude(buf.readShortLE());
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f);

        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));

        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

        if (BitUtil.check(type, 2)) {
            buf.readUnsignedByte(); // vib
            buf.readUnsignedByte(); // vib_count

            int out = buf.readUnsignedByte();
            for (int i = 0; i <= 3; i++) {
                position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(out, i) ? 1 : 0);
            }

            buf.readUnsignedByte(); // in_alarm
        }

        if (BitUtil.check(type, 3)) {
            for (int i = 1; i <= 6; i++) {
                position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE() * 0.001);
            }
        }

        if (BitUtil.check(type, 4)) {
            for (int i = 1; i <= 2; i++) {
                position.set(Position.PREFIX_COUNT + i, buf.readUnsignedIntLE());
            }
        }

        if (BitUtil.check(type, 5)) {
            for (int i = 1; i <= 3; i++) {
                buf.readUnsignedShortLE(); // fuel level
            }
            for (int i = 1; i <= 3; i++) {
                position.set(Position.PREFIX_TEMP + i, buf.readUnsignedByte());
            }
        }

        if (BitUtil.check(type, 6)) {
            int endIndex = buf.readerIndex() + buf.readUnsignedByte();
            while (buf.readerIndex() < endIndex) {
                int mask = buf.readUnsignedByte();
                long value;
                switch (BitUtil.from(mask, 6)) {
                case 3:
                    value = buf.readLongLE();
                    break;
                case 2:
                    value = buf.readUnsignedIntLE();
                    break;
                case 1:
                    value = buf.readUnsignedShortLE();
                    break;
                default:
                    value = buf.readUnsignedByte();
                    break;
                }
                int index = BitUtil.to(mask, 6);
                switch (index) {
                case 1:
                    position.set(Position.PREFIX_TEMP + 1, value);
                    break;
                case 2:
                    position.set("humidity", value);
                    break;
                case 3:
                    position.set("illumination", value);
                    break;
                case 4:
                    position.set(Position.KEY_BATTERY, value);
                    break;
                default:
                    position.set("can" + index, value);
                    break;
                }
            }
        }

        if (BitUtil.check(type, 7)) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        }

        return position;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    if (buf.getUnsignedByte(buf.readerIndex()) == 0x01) {
        buf.readUnsignedByte(); // codec id
    }//from w w  w . jav  a2 s . c om

    int type = buf.readUnsignedByte();
    buf.readUnsignedMediumLE(); // length
    buf.skipBytes(BLOCK_LENGTH - 1 - 3);

    if (type == MSG_DEVICE_ID) {

        String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII);
        if (getDeviceSession(channel, remoteAddress, imei) != null) {

            byte[] iv = new byte[BLOCK_LENGTH];
            buf.readBytes(iv);
            IvParameterSpec ivSpec = new IvParameterSpec(iv);

            SecretKeySpec keySpec = new SecretKeySpec(
                    DataConverter.parseHex("000102030405060708090a0b0c0d0e0f"), "AES");

            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            byte[] data = new byte[BLOCK_LENGTH];
            buf.readBytes(data);
            cipher.update(data);

        }

    } else if (type == MSG_TRACK_RESPONSE) {

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

        if (buf.capacity() <= BLOCK_LENGTH) {
            return null; // empty message
        }

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

        byte[] data = new byte[buf.capacity() - BLOCK_LENGTH];
        buf.readBytes(data);
        buf = Unpooled.wrappedBuffer(cipher.update(data));
        try {
            while (buf.readableBytes() >= 63) {

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

                buf.readUnsignedShortLE(); // index
                buf.readUnsignedShortLE(); // reserved

                position.setValid(true);

                position.setTime(new Date(buf.readLongLE() * 1000));

                position.setLatitude(buf.readFloatLE());
                position.setLongitude(buf.readFloatLE());
                position.setAltitude(buf.readFloatLE());
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readFloatLE()));
                position.setCourse(buf.readFloatLE());

                buf.readUnsignedIntLE(); // geozone event
                buf.readUnsignedIntLE(); // io events
                buf.readUnsignedIntLE(); // geozone value
                buf.readUnsignedIntLE(); // io values
                buf.readUnsignedShortLE(); // operator

                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());

                position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);

                buf.readUnsignedShortLE(); // cid
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                buf.readUnsignedByte(); // current profile

                position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
                position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte());
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

                positions.add(position);

            }
        } finally {
            buf.release();
        }

        return positions;

    }

    if (type == MSG_DEVICE_ID) {
        sendRequest(channel);
    }

    return null;
}

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 va  2  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.BceProtocolDecoder.java

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    String imei = String.format("%015d", buf.readLongLE());
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
    if (deviceSession == null) {
        return null;
    }//from  www.j a  v a  2s .  c om

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

    while (buf.readableBytes() > 1) {

        int dataEnd = buf.readUnsignedShortLE() + buf.readerIndex();
        int type = buf.readUnsignedByte();

        if (type != MSG_ASYNC_STACK && type != MSG_TIME_TRIGGERED) {
            return null;
        }

        int confirmKey = buf.readUnsignedByte() & 0x7F;

        while (buf.readerIndex() < dataEnd) {

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

            int structEnd = buf.readUnsignedByte() + buf.readerIndex();

            long time = buf.readUnsignedIntLE();
            if ((time & 0x0f) == DATA_TYPE) {

                time = time >> 4 << 1;
                time += 0x47798280; // 01/01/2008
                position.setTime(new Date(time * 1000));

                // Read masks
                int mask;
                List<Integer> masks = new LinkedList<>();
                do {
                    mask = buf.readUnsignedShortLE();
                    masks.add(mask);
                } while (BitUtil.check(mask, 15));

                mask = masks.get(0);

                if (BitUtil.check(mask, 0)) {
                    position.setValid(true);
                    position.setLongitude(buf.readFloatLE());
                    position.setLatitude(buf.readFloatLE());
                    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

                    int status = buf.readUnsignedByte();
                    position.set(Position.KEY_SATELLITES, BitUtil.to(status, 4));
                    position.set(Position.KEY_HDOP, BitUtil.from(status, 4));

                    position.setCourse(buf.readUnsignedByte() * 2);
                    position.setAltitude(buf.readUnsignedShortLE());

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

                if (BitUtil.check(mask, 1)) {
                    position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
                }

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

                if (BitUtil.check(mask, 10)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 11)) {
                    buf.skipBytes(4);
                }
                if (BitUtil.check(mask, 12)) {
                    buf.skipBytes(2);
                }
                if (BitUtil.check(mask, 13)) {
                    buf.skipBytes(2);
                }

                if (BitUtil.check(mask, 14)) {
                    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShortLE(),
                            buf.readUnsignedByte(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(),
                            buf.readUnsignedByte())));
                    buf.readUnsignedByte();
                }

                if (BitUtil.check(mask, 0)) {
                    positions.add(position);
                }
            }

            buf.readerIndex(structEnd);
        }

        // Send response
        if (type == MSG_ASYNC_STACK && channel != null) {
            ByteBuf response = Unpooled.buffer(8 + 2 + 2 + 1);
            response.writeLongLE(Long.parseLong(imei));
            response.writeShortLE(2);
            response.writeByte(MSG_STACK_COFIRM);
            response.writeByte(confirmKey);

            int checksum = 0;
            for (int i = 0; i < response.writerIndex(); i++) {
                checksum += response.getUnsignedByte(i);
            }
            response.writeByte(checksum);

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

    return positions;
}

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

License:Apache License

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

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

    if (type == MSG_IDENT || type == MSG_IDENT_FULL) {

        buf.readUnsignedIntLE(); // id
        int length = buf.readUnsignedShortLE();
        buf.skipBytes(length);/*ww w  .  j  ava2s  .  co m*/
        length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        String imei = buf.readSlice(length).toString(StandardCharsets.US_ASCII);
        getDeviceSession(channel, remoteAddress, imei);

    } else if (type == MSG_POINT || type == MSG_ALARM || type == MSG_LOGMSG) {

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

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

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

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

            if (type == MSG_LOGMSG) {
                position.set(Position.KEY_ARCHIVE, true);
                int subtype = buf.readUnsignedShortLE();
                if (subtype == MSG_ALARM) {
                    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
                }
                if (buf.readUnsignedShortLE() > buf.readableBytes()) {
                    lastIndex += 1;
                    break; // workaround for device bug
                }
                lastIndex = buf.readUnsignedIntLE();
                position.set(Position.KEY_INDEX, lastIndex);
            } else {
                newIndex = buf.readUnsignedIntLE();
            }

            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setSpeed(buf.readUnsignedIntLE() * 0.01);
            position.setCourse(buf.readUnsignedShortLE() * 0.01);
            position.setAltitude(buf.readUnsignedShortLE() * 0.01);

            int satellites = buf.readUnsignedByte();
            position.setValid(satellites >= 3);
            position.set(Position.KEY_SATELLITES, satellites);

            position.set(Position.KEY_RSSI, buf.readUnsignedByte());
            position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());

            long extraFlags = buf.readLongLE();

            if (BitUtil.check(extraFlags, 0)) {
                int count = buf.readUnsignedShortLE();
                for (int i = 1; i <= count; i++) {
                    position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                }
            }

            if (BitUtil.check(extraFlags, 1)) {
                int size = buf.readUnsignedShortLE();
                position.set("can", buf.toString(buf.readerIndex(), size, StandardCharsets.US_ASCII));
                buf.skipBytes(size);
            }

            if (BitUtil.check(extraFlags, 2)) {
                position.set("passenger", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE())));
            }

            if (type == MSG_ALARM) {
                position.set(Position.KEY_ALARM, true);
                byte[] response = { (byte) 0xC9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(response), remoteAddress));
            }

            buf.readUnsignedIntLE(); // crc

            positions.add(position);
        }

        requestArchive(channel);

        return positions;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readIntLE();
    buf.readUnsignedIntLE(); // length

    if (type != MSG_HEARTBEAT) {
        buf.readUnsignedShortLE(); // version
        buf.readUnsignedShortLE(); // index
    }//from   www.j  a  v  a 2 s .  c  o m

    if (type == MSG_SIGNAL_LINK_REGISTRATION) {

        getDeviceSession(channel, remoteAddress, buf.readSlice(12).toString(StandardCharsets.US_ASCII));

    } else if (type == MSG_GPS_DATA) {

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

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

        position.setTime(new Date(buf.readLongLE()));

        int flags = buf.readUnsignedByte();

        if (BitUtil.check(flags, 0)) {

            buf.readUnsignedShortLE(); // declination

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE()));

            position.setLongitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);
            position.setLatitude(buf.readUnsignedByte() + buf.readUnsignedByte() / 60.0);

            position.setLongitude(position.getLongitude() + buf.readUnsignedIntLE() / 3600.0);
            position.setLatitude(position.getLatitude() + buf.readUnsignedIntLE() / 3600.0);

            int status = buf.readUnsignedByte();

            position.setValid(BitUtil.check(status, 0));
            if (BitUtil.check(status, 1)) {
                position.setLongitude(-position.getLongitude());
            }
            if (!BitUtil.check(status, 2)) {
                position.setLatitude(-position.getLatitude());
            }

        } else {

            getLastLocation(position, position.getDeviceTime());

        }

        return position;

    }

    return null;
}