Example usage for io.netty.buffer ByteBuf readUnsignedMediumLE

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

Introduction

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

Prototype

public abstract int readUnsignedMediumLE();

Source Link

Document

Gets an unsigned 24-bit medium integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 3 in this buffer.

Usage

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  ww w  . j  ava2s.c  o  m

    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.CellocatorProtocolDecoder.java

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

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

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

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

    sendReply(channel, remoteAddress, deviceUniqueId, packetNumber);

    if (type == MSG_CLIENT_STATUS) {

        Position position = new Position(getProtocolName());

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

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

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

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

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

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

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

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

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

        position.setValid(true);

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

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

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

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

        return position;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2);
    buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3));

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

    while (buf.readableBytes() > 2) {

        int length = buf.readUnsignedShortLE();
        int recordIndex = buf.readUnsignedShortLE();
        int recordFlags = buf.readUnsignedByte();

        if (BitUtil.check(recordFlags, 0)) {
            buf.readUnsignedIntLE(); // object id
        }/*from   w ww.  ja v a2s .c  om*/

        if (BitUtil.check(recordFlags, 1)) {
            buf.readUnsignedIntLE(); // event id
        }
        if (BitUtil.check(recordFlags, 2)) {
            buf.readUnsignedIntLE(); // time
        }

        int serviceType = buf.readUnsignedByte();
        buf.readUnsignedByte(); // recipient service type

        int recordEnd = buf.readerIndex() + length;

        Position position = new Position(getProtocolName());
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession != null) {
            position.setDeviceId(deviceSession.getDeviceId());
        }

        ByteBuf response = Unpooled.buffer();
        response.writeShortLE(recordIndex);
        response.writeByte(0); // success
        sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response);

        while (buf.readerIndex() < recordEnd) {
            int type = buf.readUnsignedByte();
            int end = buf.readUnsignedShortLE() + buf.readerIndex();

            if (type == MSG_TERM_IDENTITY) {

                buf.readUnsignedIntLE(); // object id
                int flags = buf.readUnsignedByte();

                if (BitUtil.check(flags, 0)) {
                    buf.readUnsignedShortLE(); // home dispatcher identifier
                }
                if (BitUtil.check(flags, 1)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim());
                }
                if (BitUtil.check(flags, 2)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim());
                }
                if (BitUtil.check(flags, 3)) {
                    buf.skipBytes(3); // language identifier
                }
                if (BitUtil.check(flags, 5)) {
                    buf.skipBytes(3); // network identifier
                }
                if (BitUtil.check(flags, 6)) {
                    buf.readUnsignedShortLE(); // buffer size
                }
                if (BitUtil.check(flags, 7)) {
                    getDeviceSession(channel, remoteAddress,
                            buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim());
                }

                response = Unpooled.buffer();
                response.writeByte(0); // success
                sendResponse(channel, PT_APPDATA, 0, serviceType, MSG_RESULT_CODE, response);

            } else if (type == MSG_POS_DATA) {

                position.setTime(new Date((buf.readUnsignedIntLE() + 1262304000) * 1000)); // since 2010-01-01
                position.setLatitude(buf.readUnsignedIntLE() * 90.0 / 0xFFFFFFFFL);
                position.setLongitude(buf.readUnsignedIntLE() * 180.0 / 0xFFFFFFFFL);

                int flags = buf.readUnsignedByte();
                position.setValid(BitUtil.check(flags, 0));
                if (BitUtil.check(flags, 5)) {
                    position.setLatitude(-position.getLatitude());
                }
                if (BitUtil.check(flags, 6)) {
                    position.setLongitude(-position.getLongitude());
                }

                int speed = buf.readUnsignedShortLE();
                position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1));
                position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0));

                position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE() * 100);
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedByte());

                if (BitUtil.check(flags, 7)) {
                    position.setAltitude(buf.readMediumLE());
                }

            } else if (type == MSG_EXT_POS_DATA) {

                int flags = buf.readUnsignedByte();

                if (BitUtil.check(flags, 0)) {
                    position.set(Position.KEY_VDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 1)) {
                    position.set(Position.KEY_HDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 2)) {
                    position.set(Position.KEY_PDOP, buf.readUnsignedShortLE());
                }
                if (BitUtil.check(flags, 3)) {
                    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                }

            } else if (type == MSG_AD_SENSORS_DATA) {

                buf.readUnsignedByte(); // inputs flags

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

                buf.readUnsignedByte(); // adc flags

            }

            buf.readerIndex(end);
        }

        if (serviceType == SERVICE_TELEDATA && deviceSession != null) {
            positions.add(position);
        }
    }

    return positions.isEmpty() ? null : positions;
}