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

License:Apache License

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

    if (buf.readableBytes() < MINIMUM_LENGTH) {
        return null;
    }//from www. ja  v a 2  s.  co m

    if (isBinary(buf)) {

        int length;
        switch (buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)) {
        case "+ACK":
            length = buf.getUnsignedByte(buf.readerIndex() + 6);
            break;
        case "+INF":
        case "+BNF":
            length = buf.getUnsignedShort(buf.readerIndex() + 7);
            break;
        case "+HBD":
            length = buf.getUnsignedByte(buf.readerIndex() + 5);
            break;
        case "+CRD":
        case "+BRD":
            length = buf.getUnsignedShort(buf.readerIndex() + 6);
            break;
        default:
            length = buf.getUnsignedShort(buf.readerIndex() + 9);
            break;
        }

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

    } else {

        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '$');
        if (endIndex < 0) {
            endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0);
        }
        if (endIndex > 0) {
            ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex());
            buf.readByte(); // delimiter
            return frame;
        }

    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() >= MESSAGE_HEADER) {
        int length = Integer.parseInt(buf.toString(2, 2, StandardCharsets.US_ASCII)) + 5;
        if (buf.readableBytes() >= length) {
            ByteBuf frame = buf.readRetainedSlice(length);
            while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != '$') {
                buf.readByte();/* w w w  .java2  s.co  m*/
            }
            return frame;
        }
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~');

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);

    if (deviceSession != null && indexTilde == -1) {
        String bufString = buf.toString(StandardCharsets.US_ASCII);
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.setTime(new Date());
        getLastLocation(position, new Date());
        position.setValid(false);/* ww w .  j a va2s .  c o  m*/
        position.set(Position.KEY_RESULT, bufString);
        return position;
    }

    if (buf.readableBytes() < HEADER_LENGTH) {
        return null;
    }
    String header = buf.readSlice(HEADER_LENGTH).toString(StandardCharsets.US_ASCII);

    if (header.equals("+RRCB~")) {

        buf.skipBytes(2); // binary length 26
        int deviceId = buf.readUnsignedShortLE();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        long unixTime = buf.readUnsignedIntLE();
        if (channel != null) {
            sendResponseCurrent(channel, deviceId, unixTime);
        }
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

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

        decodeStructure(buf, position);
        return position;

    } else if (header.equals("+DDAT~")) {

        buf.skipBytes(2); // binary length
        int deviceId = buf.readUnsignedShortLE();
        deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId));
        if (deviceSession == null) {
            return null;
        }
        byte format = buf.readByte();
        if (format != 4) {
            return null;
        }
        byte nblocks = buf.readByte();
        int packNum = buf.readUnsignedShortLE();
        if (channel != null) {
            sendResponseArchive(channel, deviceId, packNum);
        }
        List<Position> positions = new ArrayList<>();
        while (nblocks > 0) {
            nblocks--;
            long unixTime = buf.readUnsignedIntLE();
            int timeIncrement = buf.getUnsignedShortLE(buf.readerIndex() + 120);
            for (int i = 0; i < 6; i++) {
                if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) {
                    Position position = new Position(getProtocolName());
                    position.setDeviceId(deviceSession.getDeviceId());
                    position.setTime(new Date((unixTime + i * timeIncrement) * 1000));
                    decodeStructure(buf, position);
                    position.set(Position.KEY_ARCHIVE, true);
                    positions.add(position);
                } else {
                    buf.skipBytes(20); // skip filled 0xFE structure
                }
            }
            buf.skipBytes(2); // increment
        }
        return positions;

    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < 5) {
        return null;
    }/* w  w w  . j  a  v  a2s . c o m*/

    int length = 2 + 2; // head and tail

    if (buf.getByte(buf.readerIndex()) == 0x78) {
        length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2);
    } else {
        length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2);
    }

    if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) {
        return buf.readRetainedSlice(length);
    }

    int endIndex = buf.readerIndex() - 1;
    do {
        endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d);
        if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) {
            return buf.readRetainedSlice(endIndex + 2 - buf.readerIndex());
        }
    } while (endIndex > 0);

    return null;
}

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

License:Apache License

private static double readCoordinate(ByteBuf buf, boolean lon) {

    int degrees = BcdUtil.readInteger(buf, 2);
    if (lon) {/*from  w ww  .  ja  v  a 2 s .c  o m*/
        degrees = degrees * 10 + (buf.getUnsignedByte(buf.readerIndex()) >> 4);
    }

    double result = 0;
    if (lon) {
        result = buf.readUnsignedByte() & 0x0f;
    }

    int length = 6;
    if (lon) {
        length = 5;
    }

    result = result * 10 + BcdUtil.readInteger(buf, length) * 0.0001;

    result /= 60;
    result += degrees;

    return result;
}

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

License:Apache License

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

    if (buf.readableBytes() < 10) {
        return null;
    }/*from www .j  a va  2 s .co m*/

    char type = (char) buf.getByte(buf.readerIndex());

    if (type == '$') {
        boolean longFormat = buf.getUnsignedByte(buf.readerIndex() + 1) == 0x75;
        int length = buf.getUnsignedShort(buf.readerIndex() + (longFormat ? 8 : 7)) + 10;
        if (length <= buf.readableBytes()) {
            return buf.readRetainedSlice(length);
        }
    } else if (type == '(') {
        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')');
        if (endIndex != -1) {
            return buf.readRetainedSlice(endIndex + 1);
        }
    } else {
        throw new ParseException(null, 0); // unknown message
    }

    return null;
}

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

License:Apache License

private List<Position> decodeBinary(ByteBuf buf, Channel channel, SocketAddress remoteAddress) {

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

    buf.readByte(); // header

    boolean longFormat = buf.getUnsignedByte(buf.readerIndex()) == 0x75;

    String id = String.valueOf(Long.parseLong(ByteBufUtil.hexDump(buf.readSlice(5))));
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }//w w w.  j  av  a  2s  . c  o m

    int protocolVersion = 0;
    if (longFormat) {
        protocolVersion = buf.readUnsignedByte();
    }

    int version = BitUtil.from(buf.readUnsignedByte(), 4);
    buf.readUnsignedShort(); // length

    while (buf.readableBytes() > 1) {

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

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

        double latitude = convertCoordinate(BcdUtil.readInteger(buf, 8));
        double longitude = convertCoordinate(BcdUtil.readInteger(buf, 9));

        byte flags = buf.readByte();
        position.setValid((flags & 0x1) == 0x1);
        if ((flags & 0x2) == 0) {
            latitude = -latitude;
        }
        position.setLatitude(latitude);
        if ((flags & 0x4) == 0) {
            longitude = -longitude;
        }
        position.setLongitude(longitude);

        position.setSpeed(BcdUtil.readInteger(buf, 2));
        position.setCourse(buf.readUnsignedByte() * 2.0);

        if (longFormat) {

            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());

            buf.readUnsignedInt(); // vehicle id combined

            int status = buf.readUnsignedShort();
            position.set(Position.KEY_ALARM, BitUtil.check(status, 1) ? Position.ALARM_GEOFENCE_ENTER : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 2) ? Position.ALARM_GEOFENCE_EXIT : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 3) ? Position.ALARM_POWER_CUT : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 4) ? Position.ALARM_VIBRATION : null);
            position.set(Position.KEY_BLOCKED, BitUtil.check(status, 7));
            position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 3) ? Position.ALARM_LOW_BATTERY : null);
            position.set(Position.KEY_ALARM, BitUtil.check(status, 8 + 6) ? Position.ALARM_FAULT : null);
            position.set(Position.KEY_STATUS, status);

            int battery = buf.readUnsignedByte();
            if (battery == 0xff) {
                position.set(Position.KEY_CHARGE, true);
            } else {
                position.set(Position.KEY_BATTERY_LEVEL, battery);
            }

            CellTower cellTower = CellTower.fromCidLac(buf.readUnsignedShort(), buf.readUnsignedShort());
            cellTower.setSignalStrength((int) buf.readUnsignedByte());
            position.setNetwork(new Network(cellTower));

            if (protocolVersion == 0x17) {
                buf.readUnsignedByte(); // geofence id
                buf.skipBytes(3); // reserved
            }

        } else if (version == 1) {

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

            buf.readByte(); // other flags and sensors

            position.setAltitude(buf.readUnsignedShort());

            int cid = buf.readUnsignedShort();
            int lac = buf.readUnsignedShort();
            int rssi = buf.readUnsignedByte();

            if (cid != 0 && lac != 0) {
                CellTower cellTower = CellTower.fromCidLac(cid, lac);
                cellTower.setSignalStrength(rssi);
                position.setNetwork(new Network(cellTower));
            } else {
                position.set(Position.KEY_RSSI, rssi);
            }

        } else if (version == 2) {

            int fuel = buf.readUnsignedByte() << 8;

            decodeStatus(position, buf);
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt() * 1000);

            fuel += buf.readUnsignedByte();
            position.set(Position.KEY_FUEL_LEVEL, fuel);

        } else if (version == 3) {

            BitBuffer bitBuffer = new BitBuffer(buf);

            position.set("fuel1", bitBuffer.readUnsigned(12));
            position.set("fuel2", bitBuffer.readUnsigned(12));
            position.set("fuel3", bitBuffer.readUnsigned(12));
            position.set(Position.KEY_ODOMETER, bitBuffer.readUnsigned(20) * 1000);

            int status = bitBuffer.readUnsigned(24);
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 0));
            position.set(Position.KEY_STATUS, status);

        }

        positions.add(position);

    }

    buf.readUnsignedByte(); // index

    return positions;
}

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

License:Apache License

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

    // Strip not '$' (0x24) bytes from the beginning
    while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != 0x24) {
        buf.readByte();//  ww  w. j a  v a 2 s  .  c  o m
    }

    // Check length and return buffer
    if (buf.readableBytes() >= MESSAGE_HEADER) {
        int length = buf.getUnsignedShort(buf.readerIndex() + 2);
        if (buf.readableBytes() >= length) {
            return buf.readRetainedSlice(length);
        }
    }

    return null;
}

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

License:Apache License

private List<Position> parseFormatA(DeviceSession deviceSession, ByteBuf buf) {
    List<Position> positions = new LinkedList<>();

    FloatReader latitudeReader = new FloatReader();
    FloatReader longitudeReader = new FloatReader();
    TimeReader timeReader = new TimeReader();

    try {//w w  w .j a  va 2s  . c om
        while (buf.isReadable()) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            short flags = buf.readUnsignedByte();

            short event = buf.readUnsignedByte();
            if (BitUtil.check(event, 7)) {
                if (BitUtil.check(event, 6)) {
                    buf.skipBytes(8);
                } else {
                    while (BitUtil.check(event, 7)) {
                        event = buf.readUnsignedByte();
                    }
                }
            }

            position.setLatitude(latitudeReader.readFloat(buf) / Math.PI * 180);
            position.setLongitude(longitudeReader.readFloat(buf) / Math.PI * 180);
            position.setTime(timeReader.readTime(buf));

            if (BitUtil.check(flags, 0)) {
                buf.readUnsignedByte(); // status
            }

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

            if (BitUtil.check(flags, 2)) {
                position.setSpeed(buf.readUnsignedShort() & 0x03ff);
                position.setCourse(buf.readUnsignedByte());
            }

            if (BitUtil.check(flags, 3)) {
                position.set(Position.KEY_ODOMETER, buf.readUnsignedShort() * 1000);
            }

            if (BitUtil.check(flags, 4)) {
                position.set(Position.KEY_FUEL_CONSUMPTION + "Accumulator1", buf.readUnsignedInt());
                position.set(Position.KEY_FUEL_CONSUMPTION + "Accumulator2", buf.readUnsignedInt());
                position.set("hours1", buf.readUnsignedShort());
                position.set("hours2", buf.readUnsignedShort());
            }

            if (BitUtil.check(flags, 5)) {
                position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort() & 0x03ff);
            }

            if (BitUtil.check(flags, 6)) {
                position.set(Position.PREFIX_TEMP + 1, buf.readByte());
                buf.getUnsignedByte(buf.readerIndex()); // control (>> 4)
                position.set(Position.KEY_INPUT, buf.readUnsignedShort() & 0x0fff);
                buf.readUnsignedShort(); // old sensor state (& 0x0fff)
            }

            if (BitUtil.check(flags, 7)) {
                position.set(Position.KEY_BATTERY, buf.getUnsignedByte(buf.readerIndex()) >> 2);
                position.set(Position.KEY_POWER, buf.readUnsignedShort() & 0x03ff);
                position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

                position.set(Position.KEY_RSSI, (buf.getUnsignedByte(buf.readerIndex()) >> 4) & 0x07);

                int satellites = buf.readUnsignedByte() & 0x0f;
                position.setValid(satellites >= 3);
                position.set(Position.KEY_SATELLITES, satellites);
            }
            positions.add(position);
        }
    } catch (IndexOutOfBoundsException error) {
        LOGGER.warn("MTA6 parsing error", error);
    }

    return positions;
}

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

License:Apache License

private Position parseFormatA1(DeviceSession deviceSession, ByteBuf buf) {
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());

    short flags = buf.readUnsignedByte();

    // Skip events
    short event = buf.readUnsignedByte();
    if (BitUtil.check(event, 7)) {
        if (BitUtil.check(event, 6)) {
            buf.skipBytes(8);//from  www .  jav a2  s  . c  o m
        } else {
            while (BitUtil.check(event, 7)) {
                event = buf.readUnsignedByte();
            }
        }
    }

    position.setLatitude(new FloatReader().readFloat(buf) / Math.PI * 180);
    position.setLongitude(new FloatReader().readFloat(buf) / Math.PI * 180);
    position.setTime(new TimeReader().readTime(buf));

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

    if (BitUtil.check(flags, 0)) {
        position.setAltitude(buf.readUnsignedShort());
        position.setSpeed(buf.readUnsignedByte());
        position.setCourse(buf.readByte());
        position.set(Position.KEY_ODOMETER, new FloatReader().readFloat(buf));
    }

    if (BitUtil.check(flags, 1)) {
        position.set(Position.KEY_FUEL_CONSUMPTION, new FloatReader().readFloat(buf));
        position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(new FloatReader().readFloat(buf)));
        position.set("tank", buf.readUnsignedByte() * 0.4);
    }

    if (BitUtil.check(flags, 2)) {
        position.set("engine", buf.readUnsignedShort() * 0.125);
        position.set("pedals", buf.readUnsignedByte());
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() - 40);
        position.set(Position.KEY_ODOMETER_SERVICE, buf.readUnsignedShort());
    }

    if (BitUtil.check(flags, 3)) {
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShort());
        position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShort());
    }

    if (BitUtil.check(flags, 4)) {
        position.set(Position.PREFIX_TEMP + 1, buf.readByte());
        buf.getUnsignedByte(buf.readerIndex()); // control (>> 4)
        position.set(Position.KEY_INPUT, buf.readUnsignedShort() & 0x0fff);
        buf.readUnsignedShort(); // old sensor state (& 0x0fff)
    }

    if (BitUtil.check(flags, 5)) {
        position.set(Position.KEY_BATTERY, buf.getUnsignedByte(buf.readerIndex()) >> 2);
        position.set(Position.KEY_POWER, buf.readUnsignedShort() & 0x03ff);
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());

        position.set(Position.KEY_RSSI, buf.getUnsignedByte(buf.readerIndex()) >> 5);

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

    // other data

    return position;
}