Example usage for io.netty.buffer ByteBuf getUnsignedByte

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

Introduction

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

Prototype

public abstract short getUnsignedByte(int index);

Source Link

Document

Gets an unsigned byte at the specified absolute index in this buffer.

Usage

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

License:Apache License

private Position parseNtcbPosition(DeviceSession deviceSession, ByteBuf buf) {
    Position position = new Position(getProtocolName());

    position.setDeviceId(deviceSession.getDeviceId());

    int format;/*www. ja va2  s.c om*/
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        format = buf.readUnsignedShortLE();
    } else {
        format = buf.readUnsignedByte();
    }
    position.set("format", format);

    position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
    position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());

    buf.skipBytes(6); // event time

    short armedStatus = buf.readUnsignedByte();
    if (isFormat(format, F10, F20, F30, F40, F50, F51, F52)) {
        position.set(Position.KEY_ARMED, BitUtil.to(armedStatus, 7));
        if (BitUtil.check(armedStatus, 7)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
    } else if (isFormat(format, F60)) {
        position.set(Position.KEY_ARMED, BitUtil.check(armedStatus, 0));
        if (BitUtil.check(armedStatus, 1)) {
            position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
        }
    }

    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());

    if (isFormat(format, F10, F20, F30)) {
        int output = buf.readUnsignedShortLE();
        position.set(Position.KEY_OUTPUT, output);
        for (int i = 0; i < 16; i++) {
            position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(output, i));
        }
    } else if (isFormat(format, F50, F51, F52)) {
        short extField = buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT, BitUtil.to(extField, 2));
        position.set(Position.PREFIX_OUT + 1, BitUtil.check(extField, 0));
        position.set(Position.PREFIX_OUT + 2, BitUtil.check(extField, 1));
        position.set(Position.KEY_SATELLITES, BitUtil.from(extField, 2));
    } else if (isFormat(format, F40, F60)) {
        short output = buf.readUnsignedByte();
        position.set(Position.KEY_OUTPUT, BitUtil.to(output, 4));
        for (int i = 0; i < 4; i++) {
            position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(output, i));
        }
    }

    if (isFormat(format, F10, F20, F30, F40)) {
        int input = buf.readUnsignedShortLE();
        position.set(Position.KEY_INPUT, input);
        if (!isFormat(format, F40)) {
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IN + (i + 1), BitUtil.check(input, i));
            }
        } else {
            position.set(Position.PREFIX_IN + 1, BitUtil.check(input, 0));
            position.set(Position.PREFIX_IN + 2, BitUtil.check(input, 1));
            position.set(Position.PREFIX_IN + 3, BitUtil.check(input, 2));
            position.set(Position.PREFIX_IN + 4, BitUtil.check(input, 3));
            position.set(Position.PREFIX_IN + 5, BitUtil.between(input, 4, 7));
            position.set(Position.PREFIX_IN + 6, BitUtil.between(input, 7, 10));
            position.set(Position.PREFIX_IN + 7, BitUtil.between(input, 10, 12));
            position.set(Position.PREFIX_IN + 8, BitUtil.between(input, 12, 14));
        }
    } else if (isFormat(format, F50, F51, F52, F60)) {
        short input = buf.readUnsignedByte();
        position.set(Position.KEY_INPUT, input);
        for (int i = 0; i < 8; i++) {
            position.set(Position.PREFIX_IN + (i + 1), BitUtil.check(input, i));
        }
    }

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

    if (isFormat(format, F10, F20, F30)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readShortLE());
    }

    if (isFormat(format, F10, F20, F50, F51, F52, F60)) {
        position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE());
    }
    if (isFormat(format, F60)) {
        position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE());
    }

    // Impulse counters
    if (isFormat(format, F20, F50, F51, F52, F60)) {
        buf.readUnsignedIntLE();
        buf.readUnsignedIntLE();
    }

    if (isFormat(format, F60)) {
        // Fuel
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();

        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        position.set(Position.PREFIX_TEMP + 2, buf.readByte());
        position.set(Position.PREFIX_TEMP + 3, buf.readByte());
        position.set(Position.PREFIX_TEMP + 4, buf.readByte());
        position.set(Position.KEY_AXLE_WEIGHT, buf.readIntLE());
        position.set(Position.KEY_RPM, buf.readUnsignedShortLE());
    }

    if (isFormat(format, F20, F50, F51, F52, F60)) {
        int navSensorState = buf.readUnsignedByte();
        position.setValid(BitUtil.check(navSensorState, 1));
        if (isFormat(format, F60)) {
            position.set(Position.KEY_SATELLITES, BitUtil.from(navSensorState, 2));
        }

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

        if (isFormat(format, F60)) {
            position.setLatitude(buf.readIntLE() / 600000.0);
            position.setLongitude(buf.readIntLE() / 600000.0);
            position.setAltitude(buf.readIntLE() * 0.1);
        } else {
            position.setLatitude(buf.readFloatLE() / Math.PI * 180);
            position.setLongitude(buf.readFloatLE() / Math.PI * 180);
        }

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

        position.set(Position.KEY_ODOMETER, buf.readFloatLE() * 1000);
        position.set(Position.KEY_DISTANCE, buf.readFloatLE() * 1000);

        // Segment times
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
    }

    // Other
    if (isFormat(format, F51, F52)) {
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
        buf.readUnsignedShortLE();
        buf.readByte();
        buf.readUnsignedShortLE();
    }

    // Four temperature sensors
    if (isFormat(format, F40, F52)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        position.set(Position.PREFIX_TEMP + 2, buf.readByte());
        position.set(Position.PREFIX_TEMP + 3, buf.readByte());
        position.set(Position.PREFIX_TEMP + 4, buf.readByte());
    }

    return position;
}

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

License:Apache License

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

    if (buf.readableBytes() < 4 + 2) {
        return null;
    }/*  www .ja  va2s  .com*/

    int length;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        length = 2 + buf.getUnsignedShort(buf.readerIndex());
    } else {
        length = 4 + 2 + buf.getUnsignedShort(buf.readerIndex() + 4) + 2;
    }

    if (buf.readableBytes() >= length) {
        return buf.readRetainedSlice(length);
    }

    return null;
}

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  ww  . j  a v a 2  s. c o 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.OigoProtocolDecoder.java

License:Apache License

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

    buf.readUnsignedByte(); // tag
    int flags = buf.getUnsignedByte(buf.readerIndex());

    DeviceSession deviceSession;//from w ww. j ava2 s .c o m
    if (BitUtil.check(flags, 6)) {
        buf.readUnsignedByte(); // flags
        deviceSession = getDeviceSession(channel, remoteAddress);
    } else {
        String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1);
        deviceSession = getDeviceSession(channel, remoteAddress, imei);
    }

    if (deviceSession == null) {
        return null;
    }

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

    buf.skipBytes(8); // imsi

    int date = buf.readUnsignedShort();

    DateBuilder dateBuilder = new DateBuilder()
            .setDate(2010 + BitUtil.from(date, 12), BitUtil.between(date, 8, 12), BitUtil.to(date, 8))
            .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), 0);

    position.setValid(true);
    position.setLatitude(convertCoordinate(buf.readInt()));
    position.setLongitude(convertCoordinate(buf.readInt()));

    position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
    position.setCourse(buf.readUnsignedShort());
    position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));

    position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.1);
    position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());

    dateBuilder.setSecond(buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());

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

    int index = buf.readUnsignedByte();

    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
    position.set(Position.KEY_ODOMETER, (long) (buf.readUnsignedInt() * 1609.34));

    if (channel != null && BitUtil.check(flags, 7)) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(MSG_ACKNOWLEDGEMENT);
        response.writeByte(index);
        response.writeByte(0x00);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }

    return position;
}

From source file:org.traccar.protocol.OigoProtocolDecoder.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()) == 0x7e) {
        return decodeArMessage(channel, remoteAddress, buf);
    } else {//from ww w  .  ja  v a2  s.  co m
        return decodeMgMessage(channel, remoteAddress, buf);
    }
}

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

License:Apache License

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

    int length = 6;

    if (buf.readableBytes() >= length) {

        int type = buf.getUnsignedByte(buf.readerIndex() + 2) & 0x0f;

        if (type == OrionProtocolDecoder.MSG_USERLOG && buf.readableBytes() >= length + 5) {

            int index = buf.readerIndex() + 3;
            int count = buf.getUnsignedByte(index) & 0x0f;
            index += 5;/*  w w w  . j  av  a  2  s  .  c  o m*/
            length += 5;

            for (int i = 0; i < count; i++) {
                if (buf.readableBytes() < length) {
                    return null;
                }
                int logLength = buf.getUnsignedByte(index + 1);
                index += logLength;
                length += logLength;
            }

            if (buf.readableBytes() >= length) {
                return buf.readRetainedSlice(length);
            }

        } else if (type == OrionProtocolDecoder.MSG_SYSLOG && buf.readableBytes() >= length + 12) {

            length += buf.getUnsignedShortLE(buf.readerIndex() + 8);
            if (buf.readableBytes() >= length) {
                return buf.readRetainedSlice(length);
            }

        }
    }

    return null;
}

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

License:Apache License

private static void sendResponse(Channel channel, ByteBuf buf) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer(4);
        response.writeByte('*');
        response.writeShort(buf.getUnsignedShort(buf.writerIndex() - 2));
        response.writeByte(buf.getUnsignedByte(buf.writerIndex() - 3));
        channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
    }/*from  w  ww . j a v  a2 s  .c om*/
}

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

License:Apache License

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

    if (buf.readableBytes() < 10) {
        return null;
    }//from   ww  w  .  j a v a2  s.c  om

    if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf
            && buf.toString(buf.readerIndex() + BINARY_HEADER, 4, StandardCharsets.US_ASCII).equals("$PHD")) {

        int length = buf.getUnsignedShortLE(buf.readerIndex() + 3);
        if (buf.readableBytes() >= length) {
            buf.skipBytes(BINARY_HEADER);
            ByteBuf result = buf.readRetainedSlice(length - BINARY_HEADER - 2);
            buf.skipBytes(2); // line break
            return result;
        }

    } else {

        if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) {
            buf.skipBytes(BINARY_HEADER);
        }

        int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r');
        if (index < 0) {
            index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n');
        }

        if (index > 0) {
            ByteBuf result = buf.readRetainedSlice(index - buf.readerIndex());
            while (buf.isReadable()
                    && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) {
                buf.skipBytes(1);
            }
            return result;
        }

    }

    return null;
}

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

License:Apache License

private int messageLength(ByteBuf buf) {
    switch (buf.getUnsignedByte(buf.readerIndex())) {
    case RoboTrackProtocolDecoder.MSG_ID:
        return 69;
    case RoboTrackProtocolDecoder.MSG_ACK:
        return 3;
    case RoboTrackProtocolDecoder.MSG_GPS:
    case RoboTrackProtocolDecoder.MSG_GSM:
    case RoboTrackProtocolDecoder.MSG_IMAGE_START:
        return 24;
    case RoboTrackProtocolDecoder.MSG_IMAGE_DATA:
        return 8 + buf.getUnsignedShortLE(buf.readerIndex() + 1);
    case RoboTrackProtocolDecoder.MSG_IMAGE_END:
        return 6;
    default://ww  w .j a  v  a  2 s .  c o  m
        return Integer.MAX_VALUE;
    }
}

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

License:Apache License

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

    if (buf.readableBytes() < 17) {
        return null;
    }//w w  w .  ja va2  s .  co m

    int length = 17 + buf.getUnsignedByte(16) + (BitUtil.from(buf.getUnsignedByte(15), 6) << 6);
    if (buf.readableBytes() >= length) {
        return buf.readBytes(length);
    }

    return null;
}