Example usage for io.netty.buffer ByteBuf readUnsignedIntLE

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

Introduction

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

Prototype

public abstract long readUnsignedIntLE();

Source Link

Document

Gets an unsigned 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:org.traccar.protocol.NyitechProtocolDecoder.java

License:Apache License

private void decodeLocation(Position position, ByteBuf buf) {

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

    int flags = buf.readUnsignedByte();
    position.setValid(BitUtil.to(flags, 2) > 0);

    double lat = buf.readUnsignedIntLE() / 3600000.0;
    double lon = buf.readUnsignedIntLE() / 3600000.0;

    position.setLatitude(BitUtil.check(flags, 2) ? lat : -lat);
    position.setLongitude(BitUtil.check(flags, 3) ? lon : -lon);

    position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShortLE()));
    position.setCourse(buf.readUnsignedShortLE() * 0.1);
    position.setAltitude(buf.readShortLE() * 0.1);
}

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);/*from   www.  ja v  a 2  s .com*/
        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
    }//w  ww .j  a  v  a  2  s  .  co 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;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header

    Position position = new Position(getProtocolName());

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            String.valueOf(buf.readUnsignedShort()));
    if (deviceSession == null) {
        return null;
    }/*from  www. j  a v  a2  s .c o m*/
    position.setDeviceId(deviceSession.getDeviceId());

    position.set("mode", buf.readUnsignedByte());
    position.set(Position.KEY_COMMAND, buf.readUnsignedByte());
    position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);

    buf.skipBytes(5); // status
    buf.readUnsignedShortLE(); // idleCount
    buf.readUnsignedShortLE(); // idleTime in seconds

    position.set(Position.KEY_DISTANCE, buf.readUnsignedIntLE());
    position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedIntLE());

    // Parse GPRMC
    int end = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
    String gprmc = buf.toString(buf.readerIndex(), end - buf.readerIndex(), StandardCharsets.US_ASCII);
    Parser parser = new Parser(PATTERN, gprmc);
    if (!parser.matches()) {
        return null;
    }

    DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0),
            parser.nextInt(0));

    position.setValid(parser.next().equals("A"));
    position.setLatitude(parser.nextCoordinate());
    position.setLongitude(parser.nextCoordinate());
    position.setSpeed(parser.nextDouble(0));
    position.setCourse(parser.nextDouble(0));

    dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0));
    position.setTime(dateBuilder.getDate());

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int type = buf.readUnsignedByte();

    if (type == MSG_ID) {

        buf.skipBytes(16); // name

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

        if (getDeviceSession(channel, remoteAddress, imei) != null && channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(MSG_ACK);
            response.writeByte(0x01); // success
            response.writeByte(0x66); // checksum
            channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
        }/*from  w  w  w  . ja  v a  2  s  .c o m*/

    } else if (type == MSG_GPS || type == MSG_GSM) {

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

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

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

        if (type == MSG_GPS) {

            position.setValid(true);
            position.setFixTime(position.getDeviceTime());
            position.setLatitude(buf.readIntLE() * 0.000001);
            position.setLongitude(buf.readIntLE() * 0.000001);
            position.setSpeed(UnitsConverter.knotsFromKph(buf.readByte()));

        } else {

            getLastLocation(position, position.getDeviceTime());

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

            buf.readUnsignedByte(); // reserved

        }

        int value = buf.readUnsignedByte();

        position.set(Position.KEY_SATELLITES, BitUtil.to(value, 4));
        position.set(Position.KEY_RSSI, BitUtil.between(value, 4, 7));
        position.set(Position.KEY_MOTION, BitUtil.check(value, 7));

        value = buf.readUnsignedByte();

        position.set(Position.KEY_CHARGE, BitUtil.check(value, 0));

        for (int i = 1; i <= 4; i++) {
            position.set(Position.PREFIX_IN + i, BitUtil.check(value, i));
        }

        position.set(Position.KEY_BATTERY_LEVEL, BitUtil.from(value, 5) * 100 / 7);
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

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

        return position;

    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header
    buf.readUnsignedShortLE(); // reserved
    buf.readUnsignedShortLE(); // length
    buf.readUnsignedByte(); // edition

    int type = buf.readUnsignedShortLE();

    buf.readUnsignedIntLE(); // command id

    sendResponse(channel, type);//from   w w  w  .  ja v a2s .  c om

    if (type == MSG_LOGIN) {

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

    } else if (type == MSG_LOCATION) {

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

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

        getLastLocation(position, null);

        return position;

    }

    return null;
}

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;

    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;
    }/*  ww w .j  a  v a 2s . c  o m*/

    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;
}

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

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    if (buf.readableBytes() < 4) {
        return null;
    }//w w  w.j  a v a  2s  .  c o  m

    long length = buf.getUnsignedIntLE(buf.readerIndex());

    if (length < 1024) {
        if (buf.readableBytes() >= length + 4) {
            buf.readUnsignedIntLE();
            return buf.readRetainedSlice((int) length);
        }
    } else {
        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0);
        if (endIndex >= 0) {
            ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex());
            buf.readByte();
            if (frame.readableBytes() > 0) {
                return frame;
            }
        }
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int protocol = buf.readUnsignedByte();
    boolean legacy = protocol == 0x80;

    buf.readUnsignedByte(); // version id
    int index = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    int type = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE();
    buf.readUnsignedShort(); // length
    buf.readUnsignedShort(); // mask
    buf.readUnsignedShort(); // checksum
    long id = legacy ? buf.readUnsignedInt() : buf.readUnsignedIntLE();
    buf.readUnsignedInt(); // time

    Position position = new Position(getProtocolName());
    position.set(Position.KEY_INDEX, index);
    position.setValid(true);/*from  w  ww .j  av a 2  s  . c o m*/

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

    if (protocol == 0x01 && (type == MSG_COMPACT || type == MSG_FULL)) {

        // need to send ack?

        buf.readUnsignedShortLE(); // report trigger
        buf.readUnsignedShortLE(); // state flag

        position.setLatitude(buf.readUnsignedIntLE() * 0.0000001);
        position.setLongitude(buf.readUnsignedIntLE() * 0.0000001);

        position.set(Position.KEY_RSSI, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES, buf.readUnsignedShortLE());
        position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShortLE());
        position.set("gpsAntennaStatus", buf.readUnsignedShortLE());

        position.setSpeed(buf.readUnsignedShortLE() * 0.194384);
        position.setCourse(buf.readUnsignedShortLE());

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

        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE());

        position.set(Position.KEY_CHARGE, buf.readUnsignedShortLE());

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

        // parse other data

        return position;

    } else if (legacy) {

        if (channel != null) {
            channel.writeAndFlush(new NetworkMessage(
                    Unpooled.copiedBuffer("gprs,ack," + index, StandardCharsets.US_ASCII), remoteAddress));
        }

        String sentence = buf.toString(StandardCharsets.US_ASCII);

        Pattern pattern = Pattern.compile("(-?\\d+\\.\\d+), (-?\\d+\\.\\d+)");
        Matcher matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        position.setLatitude(Double.parseDouble(matcher.group(1)));
        position.setLongitude(Double.parseDouble(matcher.group(2)));

        pattern = Pattern.compile("([NSWE]{1,2}) with speed (\\d+) km/h");
        matcher = pattern.matcher(sentence);
        if (matcher.find()) {
            for (int i = 0; i < DIRECTIONS.length; i++) {
                if (matcher.group(1).equals(DIRECTIONS[i])) {
                    position.setCourse(i * 45.0);
                    break;
                }
            }
            position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(matcher.group(2))));
        }

        pattern = Pattern.compile("(\\d{1,2}:\\d{2}(:\\d{2})? \\w{3} \\d{1,2})");
        matcher = pattern.matcher(sentence);
        if (!matcher.find()) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(
                matcher.group(2) != null ? "HH:mm:ss MMM d yyyy" : "HH:mm MMM d yyyy", Locale.ENGLISH);
        position.setTime(DateUtil.correctYear(
                dateFormat.parse(matcher.group(1) + " " + Calendar.getInstance().get(Calendar.YEAR))));

        if (sentence.contains("Ignition on detected")) {
            position.set(Position.KEY_IGNITION, true);
        } else if (sentence.contains("Ignition off detected")) {
            position.set(Position.KEY_IGNITION, false);
        }

        return position;

    }

    return null;
}