Example usage for io.netty.buffer ByteBuf readLong

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

Introduction

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

Prototype

public abstract long readLong();

Source Link

Document

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

Usage

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

License:Apache License

private Position decodeEvent(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    Position position = new Position(getProtocolName());

    int type = buf.readUnsignedByte();

    buf.readUnsignedInt(); // mask
    buf.readUnsignedShort(); // length
    buf.readUnsignedByte(); // device type
    buf.readUnsignedShort(); // protocol version

    position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort()));

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }/*w w  w. j  av a 2 s  .com*/
    position.setDeviceId(deviceSession.getDeviceId());

    position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
    position.set(Position.KEY_POWER, buf.readUnsignedShort());

    buf.readUnsignedByte(); // motion status

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

    switch (type) {
    case MSG_EVT_BPL:
        buf.readUnsignedShort(); // backup battery voltage
        break;
    case MSG_EVT_VGN:
    case MSG_EVT_VGF:
        buf.readUnsignedShort(); // reserved
        buf.readUnsignedByte(); // report type
        buf.readUnsignedInt(); // ignition duration
        break;
    case MSG_EVT_UPD:
        buf.readUnsignedShort(); // code
        buf.readUnsignedByte(); // retry
        break;
    case MSG_EVT_IDF:
        buf.readUnsignedInt(); // idling duration
        break;
    case MSG_EVT_GSS:
        buf.readUnsignedByte(); // gps signal status
        buf.readUnsignedInt(); // reserved
        break;
    case MSG_EVT_GES:
        buf.readUnsignedShort(); // trigger geo id
        buf.readUnsignedByte(); // trigger geo enable
        buf.readUnsignedByte(); // trigger mode
        buf.readUnsignedInt(); // radius
        buf.readUnsignedInt(); // check interval
        break;
    case MSG_EVT_GPJ:
        buf.readUnsignedByte(); // cw jamming value
        buf.readUnsignedByte(); // gps jamming state
        break;
    case MSG_EVT_RMD:
        buf.readUnsignedByte(); // roaming state
        break;
    case MSG_EVT_JDS:
        buf.readUnsignedByte(); // jamming state
        break;
    case MSG_EVT_CRA:
        buf.readUnsignedByte(); // crash counter
        break;
    case MSG_EVT_UPC:
        buf.readUnsignedByte(); // command id
        buf.readUnsignedShort(); // result
        break;
    default:
        break;
    }

    buf.readUnsignedByte(); // count

    int hdop = buf.readUnsignedByte();
    position.setValid(hdop > 0);
    position.set(Position.KEY_HDOP, hdop);

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedMedium() * 0.1));
    position.setCourse(buf.readUnsignedShort());
    position.setAltitude(buf.readShort());
    position.setLongitude(buf.readInt() * 0.000001);
    position.setLatitude(buf.readInt() * 0.000001);

    position.setTime(decodeTime(buf));

    position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(),
            buf.readUnsignedShort(), buf.readUnsignedShort())));

    buf.readUnsignedByte(); // reserved

    return position;
}

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

License:Apache License

private Position decodeInformation(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

    Position position = new Position(getProtocolName());

    int type = buf.readUnsignedByte();

    buf.readUnsignedInt(); // mask
    buf.readUnsignedShort(); // length

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.format("%015d", buf.readLong()));
    if (deviceSession == null) {
        return null;
    }//  w  ww  .ja  va2  s.co  m
    position.setDeviceId(deviceSession.getDeviceId());

    buf.readUnsignedByte(); // device type
    buf.readUnsignedShort(); // protocol version

    position.set(Position.KEY_VERSION_FW, String.valueOf(buf.readUnsignedShort()));

    if (type == MSG_INF_VER) {
        buf.readUnsignedShort(); // hardware version
        buf.readUnsignedShort(); // mcu version
        buf.readUnsignedShort(); // reserved
    }

    buf.readUnsignedByte(); // motion status
    buf.readUnsignedByte(); // reserved

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

    buf.readUnsignedByte(); // mode
    buf.skipBytes(7); // last fix time
    buf.readUnsignedByte(); // reserved
    buf.readUnsignedByte();
    buf.readUnsignedShort(); // response report mask
    buf.readUnsignedShort(); // ign interval
    buf.readUnsignedShort(); // igf interval
    buf.readUnsignedInt(); // reserved
    buf.readUnsignedByte(); // reserved

    if (type == MSG_INF_BAT) {
        position.set(Position.KEY_CHARGE, buf.readUnsignedByte() != 0);
        position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001);
        position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
    }

    buf.skipBytes(10); // iccid

    if (type == MSG_INF_CSQ) {
        position.set(Position.KEY_RSSI, buf.readUnsignedByte());
        buf.readUnsignedByte();
    }

    buf.readUnsignedByte(); // time zone flags
    buf.readUnsignedShort(); // time zone offset

    if (type == MSG_INF_GIR) {
        buf.readUnsignedByte(); // gir trigger
        buf.readUnsignedByte(); // cell number
        position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(),
                buf.readUnsignedShort(), buf.readUnsignedShort())));
        buf.readUnsignedByte(); // ta
        buf.readUnsignedByte(); // rx level
    }

    getLastLocation(position, decodeTime(buf));

    return position;
}

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

License:Apache License

private Object decodeBasicOther(Channel channel, ByteBuf buf, DeviceSession deviceSession, int type,
        int dataLength) {

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

    if (type == MSG_LBS_MULTIPLE || type == MSG_LBS_EXTEND || type == MSG_LBS_WIFI || type == MSG_LBS_2
            || type == MSG_WIFI_3) {/*from   www.j  a v a  2 s.  c  o  m*/

        boolean longFormat = type == MSG_LBS_2 || type == MSG_WIFI_3;

        DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone())
                .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
                .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());

        getLastLocation(position, dateBuilder.getDate());

        int mcc = buf.readUnsignedShort();
        int mnc = BitUtil.check(mcc, 15) ? buf.readUnsignedShort() : buf.readUnsignedByte();
        Network network = new Network();
        for (int i = 0; i < 7; i++) {
            int lac = longFormat ? buf.readInt() : buf.readUnsignedShort();
            int cid = longFormat ? (int) buf.readLong() : buf.readUnsignedMedium();
            int rssi = -buf.readUnsignedByte();
            if (lac > 0) {
                network.addCellTower(CellTower.from(BitUtil.to(mcc, 15), mnc, lac, cid, rssi));
            }
        }

        buf.readUnsignedByte(); // time leads

        if (type != MSG_LBS_MULTIPLE && type != MSG_LBS_2) {
            int wifiCount = buf.readUnsignedByte();
            for (int i = 0; i < wifiCount; i++) {
                String mac = ByteBufUtil.hexDump(buf.readSlice(6)).replaceAll("(..)", "$1:");
                network.addWifiAccessPoint(
                        WifiAccessPoint.from(mac.substring(0, mac.length() - 1), buf.readUnsignedByte()));
            }
        }

        position.setNetwork(network);

    } else if (type == MSG_STRING) {

        getLastLocation(position, null);

        int commandLength = buf.readUnsignedByte();

        if (commandLength > 0) {
            buf.readUnsignedByte(); // server flag (reserved)
            position.set(Position.KEY_RESULT,
                    buf.readSlice(commandLength - 1).toString(StandardCharsets.US_ASCII));
        }

    } else if (isSupported(type)) {

        if (hasGps(type)) {
            decodeGps(position, buf, false, deviceSession.getTimeZone());
        } else {
            getLastLocation(position, null);
        }

        if (hasLbs(type)) {
            decodeLbs(position, buf, hasStatus(type));
        }

        if (hasStatus(type)) {
            decodeStatus(position, buf);
        }

        if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 2 + 6) {
            int mask = buf.readUnsignedShort();
            position.set(Position.KEY_IGNITION, BitUtil.check(mask, 8 + 7));
            position.set(Position.PREFIX_IN + 2, BitUtil.check(mask, 8 + 6));
            if (BitUtil.check(mask, 8 + 4)) {
                int value = BitUtil.to(mask, 8 + 1);
                if (BitUtil.check(mask, 8 + 1)) {
                    value = -value;
                }
                position.set(Position.PREFIX_TEMP + 1, value);
            } else {
                int value = BitUtil.to(mask, 8 + 2);
                if (BitUtil.check(mask, 8 + 5)) {
                    position.set(Position.PREFIX_ADC + 1, value);
                } else {
                    position.set(Position.PREFIX_ADC + 1, value * 0.1);
                }
            }
        }

        if (type == MSG_GPS_LBS_1 && buf.readableBytes() == 4 + 6) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if (type == MSG_GPS_LBS_2 && buf.readableBytes() == 3 + 6) {
            position.set(Position.KEY_IGNITION, buf.readUnsignedByte() > 0);
            position.set(Position.KEY_EVENT, buf.readUnsignedByte()); // reason
            position.set(Position.KEY_ARCHIVE, buf.readUnsignedByte() > 0);
        }

    } else {

        if (dataLength > 0) {
            buf.skipBytes(dataLength);
        }
        if (type != MSG_COMMAND_0 && type != MSG_COMMAND_1 && type != MSG_COMMAND_2) {
            sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);
        }
        return null;

    }

    if (hasLanguage(type)) {
        buf.readUnsignedShort();
    }

    if (type == MSG_GPS_LBS_STATUS_3 || type == MSG_FENCE_MULTI) {
        position.set(Position.KEY_GEOFENCE, buf.readUnsignedByte());
    }

    sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null);

    return position;
}

From source file:org.traccar.protocol.NvsProtocolDecoder.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()) == 0) {

        buf.readUnsignedShort(); // length

        String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);

        if (getDeviceSession(channel, remoteAddress, imei) != null) {
            sendResponse(channel, remoteAddress, "OK");
        } else {/*from   w  w w  .j a  va  2s  .co  m*/
            sendResponse(channel, remoteAddress, "NO01");
        }

    } else {

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

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

        buf.skipBytes(4); // marker
        buf.readUnsignedShort(); // length
        buf.readLong(); // imei
        buf.readUnsignedByte(); // codec
        int count = buf.readUnsignedByte();

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

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

            position.set("reason", buf.readUnsignedByte());

            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);
            position.setAltitude(buf.readShort());
            position.setCourse(buf.readUnsignedShort());

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

            position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
            position.setValid(buf.readUnsignedByte() != 0);

            buf.readUnsignedByte(); // used systems

            buf.readUnsignedByte(); // cause element id

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedByte());
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort());
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt());
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readLong());
            }

            positions.add(position);
        }

        return positions;

    }

    return null;
}

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

License:Apache License

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

    if (channel != null) {
        channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x11 }), remoteAddress));
    }/*from   w  w  w  . ja  v  a 2s.c  om*/

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedInt(); // length

    int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
    String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII);
    buf.readByte();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }

    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position.setTime(new Date(buf.readUnsignedInt() * 1000));

    buf.readUnsignedInt(); // bit flags

    while (buf.isReadable()) {

        buf.readUnsignedShort(); // block type
        int blockEnd = buf.readInt() + buf.readerIndex();
        buf.readUnsignedByte(); // security attribute
        int dataType = buf.readUnsignedByte();

        int nameLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
        String name = buf.readBytes(nameLength).toString(StandardCharsets.US_ASCII);
        buf.readByte();

        if (name.equals("posinfo")) {
            position.setValid(true);
            position.setLongitude(buf.readDoubleLE());
            position.setLatitude(buf.readDoubleLE());
            position.setAltitude(buf.readDoubleLE());
            position.setSpeed(buf.readShort());
            position.setCourse(buf.readShort());
            position.set(Position.KEY_SATELLITES, buf.readByte());
        } else {
            switch (dataType) {
            case 1:
                int len = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
                position.set(name, buf.readBytes(len).toString(StandardCharsets.US_ASCII));
                buf.readByte();
                break;
            case 3:
                position.set(name, buf.readInt());
                break;
            case 4:
                position.set(name, buf.readDoubleLE());
                break;
            case 5:
                position.set(name, buf.readLong());
                break;
            default:
                break;
            }
        }

        buf.readerIndex(blockEnd);

    }

    if (position.getLatitude() == 0 && position.getLongitude() == 0) {
        getLastLocation(position, position.getDeviceTime());
    }

    return position;
}

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

License:Apache License

private long readValue(ByteBuf buf, int length, boolean signed) {
    switch (length) {
    case 1:/*from   w  w w.  ja  v  a 2s .co m*/
        return signed ? buf.readByte() : buf.readUnsignedByte();
    case 2:
        return signed ? buf.readShort() : buf.readUnsignedShort();
    case 4:
        return signed ? buf.readInt() : buf.readUnsignedInt();
    default:
        return buf.readLong();
    }
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedShort(); // data length

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

    int type = buf.readUnsignedByte();

    if (type == MSG_RECORDS || type == MSG_EXTENDED_RECORDS) {

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

        buf.readUnsignedByte(); // records left
        int count = buf.readUnsignedByte();

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

            position.setTime(new Date(buf.readUnsignedInt() * 1000));
            buf.readUnsignedByte(); // timestamp extension

            if (type == MSG_EXTENDED_RECORDS) {
                buf.readUnsignedByte(); // record extension
            }

            buf.readUnsignedByte(); // priority (reserved)

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);
            position.setAltitude(buf.readUnsignedShort() / 10.0);
            position.setCourse(buf.readUnsignedShort() / 100.0);

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

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

            position.set(Position.KEY_HDOP, buf.readUnsignedByte() / 10.0);

            if (type == MSG_EXTENDED_RECORDS) {
                position.set(Position.KEY_EVENT, buf.readUnsignedShort());
            } else {
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            }

            // Read 1 byte data
            int cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 1);
            }

            // Read 2 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 2);
            }

            // Read 4 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 4);
            }

            // Read 8 byte data
            cnt = buf.readUnsignedByte();
            for (int j = 0; j < cnt; j++) {
                int id = type == MSG_EXTENDED_RECORDS ? buf.readUnsignedShort() : buf.readUnsignedByte();
                decodeParameter(position, id, buf, 8);
            }

            Long driverIdPart1 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 126);
            Long driverIdPart2 = (Long) position.getAttributes().remove(Position.PREFIX_IO + 127);
            if (driverIdPart1 != null && driverIdPart2 != null) {
                ByteBuf driverId = Unpooled.copyLong(driverIdPart1, driverIdPart2);
                position.set(Position.KEY_DRIVER_UNIQUE_ID, driverId.toString(StandardCharsets.US_ASCII));
                driverId.release();
            }

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("0002640113bc")), remoteAddress));
        }

        return positions;

    } else if (type == MSG_DTCS) {

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

        int count = buf.readUnsignedByte();

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

            buf.readUnsignedByte(); // reserved

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

            position.setValid(true);
            position.setLongitude(buf.readInt() / 10000000.0);
            position.setLatitude(buf.readInt() / 10000000.0);

            if (buf.readUnsignedByte() == 2) {
                position.set(Position.KEY_ARCHIVE, true);
            }

            position.set(Position.KEY_DTCS, buf.readSlice(5).toString(StandardCharsets.US_ASCII));

            positions.add(position);
        }

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.wrappedBuffer(DataConverter.parseHex("00026d01c4a4")), remoteAddress));
        }

        return positions;

    } else {

        return decodeCommandResponse(deviceSession, type, buf);

    }
}

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

License:Apache License

private void decodeLocation(Position position, ByteBuf buf, int codec) {

    int globalMask = 0x0f;

    if (codec == CODEC_GH3000) {

        long time = buf.readUnsignedInt() & 0x3fffffff;
        time += 1167609600; // 2007-01-01 00:00:00

        globalMask = buf.readUnsignedByte();
        if (BitUtil.check(globalMask, 0)) {

            position.setTime(new Date(time * 1000));

            int locationMask = buf.readUnsignedByte();

            if (BitUtil.check(locationMask, 0)) {
                position.setLatitude(buf.readFloat());
                position.setLongitude(buf.readFloat());
            }//from www .ja va 2s . c o m

            if (BitUtil.check(locationMask, 1)) {
                position.setAltitude(buf.readUnsignedShort());
            }

            if (BitUtil.check(locationMask, 2)) {
                position.setCourse(buf.readUnsignedByte() * 360.0 / 256);
            }

            if (BitUtil.check(locationMask, 3)) {
                position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
            }

            if (BitUtil.check(locationMask, 4)) {
                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
            }

            if (BitUtil.check(locationMask, 5)) {
                CellTower cellTower = CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort());

                if (BitUtil.check(locationMask, 6)) {
                    cellTower.setSignalStrength((int) buf.readUnsignedByte());
                }

                if (BitUtil.check(locationMask, 7)) {
                    cellTower.setOperator(buf.readUnsignedInt());
                }

                position.setNetwork(new Network(cellTower));

            } else {
                if (BitUtil.check(locationMask, 6)) {
                    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                }
                if (BitUtil.check(locationMask, 7)) {
                    position.set(Position.KEY_OPERATOR, buf.readUnsignedInt());
                }
            }

        } else {

            getLastLocation(position, new Date(time * 1000));

        }

    } else {

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

        position.set("priority", buf.readUnsignedByte());

        position.setLongitude(buf.readInt() / 10000000.0);
        position.setLatitude(buf.readInt() / 10000000.0);
        position.setAltitude(buf.readShort());
        position.setCourse(buf.readUnsignedShort());

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

        position.setValid(satellites != 0);

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

        position.set(Position.KEY_EVENT, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16));
        if (codec == CODEC_16) {
            buf.readUnsignedByte(); // generation type
        }

        readExtByte(buf, codec, CODEC_8_EXT); // total IO data records

    }

    // Read 1 byte data
    if (BitUtil.check(globalMask, 1)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 1, codec);
        }
    }

    // Read 2 byte data
    if (BitUtil.check(globalMask, 2)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 2, codec);
        }
    }

    // Read 4 byte data
    if (BitUtil.check(globalMask, 3)) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 4, codec);
        }
    }

    // Read 8 byte data
    if (codec == CODEC_8 || codec == CODEC_8_EXT || codec == CODEC_16) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            decodeOtherParameter(position, readExtByte(buf, codec, CODEC_8_EXT, CODEC_16), buf, 8);
        }
    }

    // Read 16 byte data
    if (extended) {
        int cnt = readExtByte(buf, codec, CODEC_8_EXT);
        for (int j = 0; j < cnt; j++) {
            int id = readExtByte(buf, codec, CODEC_8_EXT, CODEC_16);
            position.set(Position.PREFIX_IO + id, ByteBufUtil.hexDump(buf.readSlice(16)));
        }
    }

    // Read X byte data
    if (codec == CODEC_8_EXT) {
        int cnt = buf.readUnsignedShort();
        for (int j = 0; j < cnt; j++) {
            int id = buf.readUnsignedShort();
            int length = buf.readUnsignedShort();
            if (id == 256) {
                position.set(Position.KEY_VIN, buf.readSlice(length).toString(StandardCharsets.US_ASCII));
            } else {
                position.set(Position.PREFIX_IO + id, ByteBufUtil.hexDump(buf.readSlice(length)));
            }
        }
    }

    decodeNetwork(position);

}

From source file:org.virtue.network.protocol.login.LoginDecoder.java

License:Open Source License

private void decodeLobbyPayload(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
        throws ProtocolException {
    if (buf.readableBytes() < 2)
        throw new IllegalStateException("Not enough readable bytes from buffer.");

    int secureBufferSize = buf.readShort() & 0xFFFF;
    if (buf.readableBytes() < secureBufferSize)
        throw new IllegalStateException("Not enough readable bytes from buffer.");

    byte[] secureBytes = new byte[secureBufferSize];
    buf.readBytes(secureBytes);/*from  w  ww.j  a v  a  2s .  c  om*/
    ByteBuf secureBuffer = Unpooled.wrappedBuffer(new BigInteger(secureBytes)
            .modPow(Constants.getLoginKey(), Constants.getLoginModulus()).toByteArray());

    int blockOpcode = secureBuffer.readByte() & 0xFF;
    if (blockOpcode != 10) {
        throw new ProtocolException("Invalid block opcode: " + blockOpcode);
    }

    int[] xteaKey = new int[4];
    for (int i = 0; i < xteaKey.length; i++) {
        xteaKey[i] = secureBuffer.readInt();
    }

    secureBuffer.readByte();
    secureBuffer.readInt();
    //if (loginHash != 0)
    //throw new ProtocolException("Invalid login hash: " + loginHash);

    String password = BufferUtility.readString(secureBuffer);

    long[] loginSeeds = new long[2];
    for (int i = 0; i < loginSeeds.length; i++) {
        loginSeeds[i] = secureBuffer.readLong();
    }

    byte[] xteaBlock = new byte[buf.readableBytes()];
    buf.readBytes(xteaBlock);

    XTEACipher xtea = new XTEACipher(xteaKey);
    xtea.decrypt(xteaBlock, 0, xteaBlock.length);

    ByteBuf xteaBuffer = Unpooled.wrappedBuffer(xteaBlock);

    boolean decodeAsString = xteaBuffer.readByte() == 1;
    String username = decodeAsString ? BufferUtility.readString(xteaBuffer)
            : Base37Utility.decodeBase37(xteaBuffer.readLong());

    xteaBuffer.readUnsignedByte();//Game ID
    xteaBuffer.readUnsignedByte();//Language ID
    xteaBuffer.readUnsignedByte();//Frame type (Fixed, Resizable, or Full Screen)

    xteaBuffer.readUnsignedShort();//Canvas Width
    xteaBuffer.readUnsignedShort();//Canvas Height
    xteaBuffer.readUnsignedByte();

    int[] randomData = new int[24];
    for (int index = 0; index < randomData.length; index++) {
        randomData[index] = xteaBuffer.readUnsignedByte();
    }

    String token = BufferUtility.readString(xteaBuffer);//Site Settings
    if (!token.equals(Constants.getLoginToken()))
        throw new IllegalStateException("Invalid server token. " + token);

    int length = xteaBuffer.readUnsignedByte();

    byte[] machineData = new byte[length];
    for (int data = 0; data < machineData.length; data++) {
        machineData[data] = (byte) xteaBuffer.readUnsignedByte();
    }

    xteaBuffer.readInt();
    BufferUtility.readString(xteaBuffer);
    xteaBuffer.readInt();
    xteaBuffer.readInt();
    BufferUtility.readString(xteaBuffer);

    xteaBuffer.readUnsignedByte();

    int[] cacheCRC = new int[36];
    for (int index = 0; index < cacheCRC.length; index++) {
        cacheCRC[index] = xteaBuffer.readInt();
    }

    int[] serverKeys = new int[xteaKey.length];
    for (int i = 0; i < serverKeys.length; i++) {
        serverKeys[i] = xteaKey[i] + 50;
    }

    System.out.println("Username = " + username);
    //System.out.println("Password = " + password);
    out.add(new LoginRequestMessage(ctx.channel(), UsernameUtility.formatForProtocol(username), password,
            username.contains("@"), new ISAACCipher(serverKeys), new ISAACCipher(xteaKey), type));
}

From source file:org.virtue.network.protocol.login.LoginDecoder.java

License:Open Source License

private void decodeGamePayload(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
        throws ProtocolException {
    if (buf.readableBytes() < 2) {
        throw new IllegalStateException("Not enough readable bytes from buffer.");
    }/*  w w w .j  a  v  a2s  .  c o m*/

    int secureBufferSize = buf.readShort() & 0xFFFF;
    if (buf.readableBytes() < secureBufferSize) {
        throw new IllegalStateException("Not enough readable bytes from buffer.");
    }

    byte[] secureBytes = new byte[secureBufferSize];
    buf.readBytes(secureBytes);
    ByteBuf secureBuffer = Unpooled.wrappedBuffer(new BigInteger(secureBytes)
            .modPow(Constants.getLoginKey(), Constants.getLoginModulus()).toByteArray());

    int blockOpcode = secureBuffer.readByte() & 0xFF;
    if (blockOpcode != 10) {
        throw new ProtocolException("Invalid block opcode: " + blockOpcode);
    }

    int[] xteaKey = new int[4];
    for (int i = 0; i < xteaKey.length; i++)
        xteaKey[i] = secureBuffer.readInt();

    secureBuffer.readLong();
    //if (loginHash != 0)
    //throw new ProtocolException("Invalid login hash: " + loginHash);

    secureBuffer.readByte();
    secureBuffer.readerIndex(secureBuffer.readerIndex() - 4);

    String password = BufferUtility.readString(secureBuffer);

    long[] loginSeeds = new long[2];
    for (int i = 0; i < loginSeeds.length; i++)
        loginSeeds[i] = secureBuffer.readLong();

    byte[] xteaBlock = new byte[buf.readableBytes()];
    buf.readBytes(xteaBlock);

    XTEACipher xtea = new XTEACipher(xteaKey);
    xtea.decrypt(xteaBlock, 0, xteaBlock.length);

    ByteBuf xteaBuffer = Unpooled.wrappedBuffer(xteaBlock);
    boolean decodeAsString = xteaBuffer.readByte() == 1;
    String username = decodeAsString ? BufferUtility.readString(xteaBuffer)
            : Base37Utility.decodeBase37(xteaBuffer.readLong());

    xteaBuffer.readUnsignedByte();
    xteaBuffer.readUnsignedShort();//Canvas Width
    xteaBuffer.readUnsignedShort();//Canvas Height

    xteaBuffer.readUnsignedByte();

    int[] randomData = new int[24];
    for (int index = 0; index < randomData.length; index++) {
        randomData[index] = xteaBuffer.readUnsignedByte();
    }

    String token = BufferUtility.readString(xteaBuffer);//Site Settings
    if (!token.equals(Constants.getLoginToken())) {
        throw new IllegalStateException("Invalid server token.");
    }

    xteaBuffer.readInt();

    int length = xteaBuffer.readUnsignedByte();

    byte[] machineData = new byte[length];
    for (int data = 0; data < machineData.length; data++) {
        machineData[data] = (byte) xteaBuffer.readUnsignedByte();
    }

    xteaBuffer.readInt();
    xteaBuffer.readInt();//User flow #2
    xteaBuffer.readInt();//User flow #1

    BufferUtility.readString(xteaBuffer);

    boolean extra = xteaBuffer.readUnsignedByte() == 1;
    if (extra) {
        BufferUtility.readString(xteaBuffer);//Create Additional Info
    }

    xteaBuffer.readUnsignedByte();//Javascript enabled
    xteaBuffer.readUnsignedByte();//Has Chrome browser
    xteaBuffer.readUnsignedByte();
    xteaBuffer.readUnsignedByte();

    xteaBuffer.readInt();
    @SuppressWarnings("unused")
    String videoCard = BufferUtility.readString(xteaBuffer);
    xteaBuffer.readUnsignedByte();

    xteaBuffer.readUnsignedShort();//Lobby node ID
    xteaBuffer.readUnsignedShort();//Media stream node ID

    int[] cacheCRC = new int[36];
    for (int index = 0; index < cacheCRC.length; index++) {
        cacheCRC[index] = xteaBuffer.readInt();
    }

    int[] serverKeys = new int[xteaKey.length];
    for (int i = 0; i < serverKeys.length; i++) {
        serverKeys[i] = xteaKey[i] + 50;
    }

    out.add(new LoginRequestMessage(ctx.channel(), UsernameUtility.formatForProtocol(username), password,
            username.contains("@"), new ISAACCipher(serverKeys), new ISAACCipher(xteaKey), type));
}