Example usage for io.netty.buffer ByteBuf readUnsignedShortLE

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

Introduction

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

Prototype

public abstract int readUnsignedShortLE();

Source Link

Document

Gets an unsigned 16-bit short integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 2 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.NyitechProtocolDecoder.java

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    buf.readUnsignedShortLE(); // length

    String id = buf.readCharSequence(12, StandardCharsets.US_ASCII).toString();
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }/*from  w  w  w .  ja  va  2  s  . com*/

    int type = buf.readUnsignedShortLE();

    if (type != MSG_LOGIN && type != MSG_COMPREHENSIVE_LIVE && type != MSG_COMPREHENSIVE_HISTORY
            && type != MSG_ALARM && type != MSG_FIXED) {
        return null;
    }

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

    if (type == MSG_COMPREHENSIVE_LIVE || type == MSG_COMPREHENSIVE_HISTORY) {

        buf.skipBytes(6); // time
        boolean includeLocation = buf.readUnsignedByte() > 0;
        boolean includeObd = buf.readUnsignedByte() > 0;
        buf.readUnsignedByte(); // include sensor

        if (includeLocation) {
            decodeLocation(position, buf);
        } else {
            getLastLocation(position, null);
        }

        if (includeObd) {
            int count = buf.readUnsignedByte();
            for (int i = 0; i < count; i++) {
                int pid = buf.readUnsignedShortLE();
                int length = buf.readUnsignedByte();
                switch (length) {
                case 1:
                    position.add(ObdDecoder.decodeData(pid, buf.readByte(), true));
                    break;
                case 2:
                    position.add(ObdDecoder.decodeData(pid, buf.readShortLE(), true));
                    break;
                case 4:
                    position.add(ObdDecoder.decodeData(pid, buf.readIntLE(), true));
                    break;
                default:
                    buf.skipBytes(length);
                    break;
                }
            }
        }

        position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt() * 0.01);
        position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());

    } else if (type == MSG_ALARM) {

        buf.readUnsignedShortLE(); // random number
        buf.readUnsignedByte(); // tag
        position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte()));
        buf.readUnsignedShortLE(); // threshold
        buf.readUnsignedShortLE(); // value
        buf.skipBytes(6); // time

        decodeLocation(position, buf);

    } else if (type == MSG_FIXED) {

        buf.skipBytes(6); // time

        decodeLocation(position, buf);

    } else {

        decodeLocation(position, buf);

    }

    return position;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(2); // header
    int type = buf.readUnsignedByte() & 0x0f;

    if (type == MSG_USERLOG) {

        int header = buf.readUnsignedByte();

        if ((header & 0x40) != 0) {
            sendResponse(channel, buf);//from  w  ww  .j  av a  2  s  .  c om
        }

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
                String.valueOf(buf.readUnsignedInt()));
        if (deviceSession == null) {
            return null;
        }

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

        for (int i = 0; i < (header & 0x0f); i++) {

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

            position.set(Position.KEY_EVENT, buf.readUnsignedByte());
            buf.readUnsignedByte(); // length
            position.set(Position.KEY_FLAGS, buf.readUnsignedShortLE());

            position.setLatitude(convertCoordinate(buf.readIntLE()));
            position.setLongitude(convertCoordinate(buf.readIntLE()));
            position.setAltitude(buf.readShortLE() / 10.0);
            position.setCourse(buf.readUnsignedShortLE());
            position.setSpeed(buf.readUnsignedShortLE() * 0.0539957);

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

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

            positions.add(position);
        }

        return positions;
    }

    return null;
}

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   w ww  . ja v a2 s. c om
        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 w ww .j a  va 2 s  .com*/

    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  w  w w.  j  av  a 2 s  . co 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));
        }// w  ww .  ja  v  a  2s . c om

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

    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;
    }// w  w  w.j  a v  a2  s  . 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.T800xProtocolDecoder.java

License:Apache License

private Position decodePosition(Channel channel, DeviceSession deviceSession, ByteBuf buf, short header,
        int type, int index, ByteBuf imei) {

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

    position.set(Position.KEY_INDEX, index);

    buf.readUnsignedShort(); // acc on interval
    buf.readUnsignedShort(); // acc off interval
    buf.readUnsignedByte(); // angle compensation
    buf.readUnsignedShort(); // distance compensation

    position.set(Position.KEY_RSSI, BitUtil.to(buf.readUnsignedShort(), 7));

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

    buf.readUnsignedByte(); // gsensor manager status
    buf.readUnsignedByte(); // other flags
    buf.readUnsignedByte(); // heartbeat
    buf.readUnsignedByte(); // relay status
    buf.readUnsignedShort(); // drag alarm setting

    int io = buf.readUnsignedShort();
    position.set(Position.KEY_IGNITION, BitUtil.check(io, 14));
    position.set("ac", BitUtil.check(io, 13));
    for (int i = 0; i <= 2; i++) {
        position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(io, 7 + i));
    }//  w  w w  . j  a v a2 s. c  o m

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

    int alarm = buf.readUnsignedByte();
    position.set(Position.KEY_ALARM, decodeAlarm(alarm));

    buf.readUnsignedByte(); // reserved

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

    int battery = BcdUtil.readInteger(buf, 2);
    if (battery == 0) {
        battery = 100;
    }
    position.set(Position.KEY_BATTERY, battery);

    DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2))
            .setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2))
            .setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2))
            .setSecond(BcdUtil.readInteger(buf, 2));

    if (BitUtil.check(status, 6)) {

        position.setValid(!BitUtil.check(status, 7));
        position.setTime(dateBuilder.getDate());
        position.setAltitude(buf.readFloatLE());
        position.setLongitude(buf.readFloatLE());
        position.setLatitude(buf.readFloatLE());
        position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4) * 0.1));
        position.setCourse(buf.readUnsignedShort());

    } else {

        getLastLocation(position, dateBuilder.getDate());

        int mcc = buf.readUnsignedShortLE();
        int mnc = buf.readUnsignedShortLE();

        if (mcc != 0xffff && mnc != 0xffff) {
            Network network = new Network();
            for (int i = 0; i < 3; i++) {
                network.addCellTower(
                        CellTower.from(mcc, mnc, buf.readUnsignedShortLE(), buf.readUnsignedShortLE()));
            }
            position.setNetwork(network);
        }

    }

    if (buf.readableBytes() >= 2) {
        position.set(Position.KEY_POWER, BcdUtil.readInteger(buf, 4) * 0.01);
    }

    sendResponse(channel, header, type, index, imei, alarm);

    return position;
}