Example usage for io.netty.buffer ByteBuf getUnsignedShortLE

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

Introduction

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

Prototype

public abstract int getUnsignedShortLE(int index);

Source Link

Document

Gets an unsigned 16-bit short integer at the specified absolute index in this buffer in Little Endian Byte Order.

Usage

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

License:Apache License

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

    if (header && buf.readableBytes() >= HANDSHAKE_LENGTH) {
        buf.skipBytes(HANDSHAKE_LENGTH);
        header = false;//from  w  ww  .  j  a v  a  2 s  .  com
    }

    int end = 8; // IMEI

    while (buf.readableBytes() >= end + 2 + 1 + 1 + 1) {
        end += buf.getUnsignedShortLE(buf.readerIndex() + end) + 2;

        if (buf.readableBytes() > end && checksum(buf, end) == buf.getByte(buf.readerIndex() + end)) {
            return buf.readRetainedSlice(end + 1);
        }
    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }//ww  w .  j av  a2 s . c o  m

    int length = 0;
    int type = buf.getUnsignedByte(4);
    switch (type) {
    case CellocatorProtocolDecoder.MSG_CLIENT_STATUS:
        length = 70;
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_PROGRAMMING:
        length = 31;
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_SERIAL_LOG:
        length = 70;
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_SERIAL:
        if (buf.readableBytes() >= 19) {
            length = 19 + buf.getUnsignedShortLE(16);
        }
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_MODULAR:
        length = 15 + buf.getUnsignedByte(13);
        break;
    default:
        break;
    }

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

    return null;
}

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

License:Apache License

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

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

    int headerLength = buf.getUnsignedByte(buf.readerIndex() + 3);
    int frameLength = buf.getUnsignedShortLE(buf.readerIndex() + 5);

    int length = headerLength + frameLength + (frameLength > 0 ? 2 : 0);

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

    return null;
}

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

License:Apache License

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

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

    int length = buf.getUnsignedShortLE(buf.readerIndex() + 1) & 0x7fff;
    if (buf.readableBytes() >= (length + MESSAGE_MINIMUM_LENGTH)) {
        return buf.readRetainedSlice(length + MESSAGE_MINIMUM_LENGTH);
    }

    return null;
}

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

License:Apache License

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

    int indexEnd = BufferUtil.indexOf("\r\n", buf);
    if (indexEnd != -1) {
        int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~');
        if (indexTilde != -1 && indexTilde < indexEnd) {
            int length = buf.getUnsignedShortLE(indexTilde + 1);
            indexEnd = BufferUtil.indexOf("\r\n", buf, indexTilde + 2 + length, buf.writerIndex());
            if (indexEnd == -1) {
                return null;
            }//from  w  ww. j ava2s .  c  o  m
        }
        ByteBuf frame = buf.readRetainedSlice(indexEnd - buf.readerIndex());
        buf.skipBytes(2);
        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);//from  ww  w .jav a 2s  .  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.NavigilFrameDecoder.java

License:Apache License

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

    // Check minimum length
    if (buf.readableBytes() < MESSAGE_HEADER) {
        return null;
    }/*from w w  w. j  av  a  2s.c om*/

    // Check for preamble
    boolean hasPreamble = false;
    if (buf.getUnsignedIntLE(buf.readerIndex()) == PREAMBLE) {
        hasPreamble = true;
    }

    // Check length and return buffer
    int length = buf.getUnsignedShortLE(buf.readerIndex() + 6);
    if (buf.readableBytes() >= length) {
        if (hasPreamble) {
            buf.readUnsignedIntLE();
            length -= 4;
        }
        return buf.readRetainedSlice(length);
    }

    return null;
}

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

License:Apache License

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

    if (buf.getByte(buf.readerIndex()) == 0x7F) {
        return buf.readRetainedSlice(1); // keep alive
    }/*ww  w  .  ja v  a 2 s . co m*/

    if (ctx != null && flexDataSize == 0) {
        NavisProtocolDecoder protocolDecoder = BasePipelineFactory.getHandler(ctx.pipeline(),
                NavisProtocolDecoder.class);
        if (protocolDecoder != null) {
            flexDataSize = protocolDecoder.getFlexDataSize();
        }
    }

    if (flexDataSize > 0) {

        if (buf.readableBytes() > FLEX_HEADER_LENGTH) {
            int length = 0;
            String type = buf.toString(buf.readerIndex(), 2, StandardCharsets.US_ASCII);
            switch (type) {
            // FLEX 1.0
            case "~A":
                length = flexDataSize * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH) + 1 + 1;
                break;
            case "~T":
                length = flexDataSize + 4 + 1;
                break;
            case "~C":
                length = flexDataSize + 1;
                break;
            // FLEX 2.0 (Extra packages)
            case "~E":
                length++;
                for (int i = 0; i < buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH); i++) {
                    if (buf.readableBytes() > FLEX_HEADER_LENGTH + length + 1) {
                        length += buf.getUnsignedShort(length + FLEX_HEADER_LENGTH) + 2;
                    } else {
                        return null;
                    }
                }
                length++;
                break;
            case "~X":
                length = buf.getUnsignedShortLE(buf.readerIndex() + FLEX_HEADER_LENGTH) + 4 + 1;
                break;
            default:
                return null;
            }

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

    } else {

        if (buf.readableBytes() < NTCB_HEADER_LENGTH) {
            return null;
        }

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

    }

    return null;
}

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;//from  w ww  .  j av a2  s.  co  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.Pt502FrameDecoder.java

License:Apache License

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

    if (buf.readableBytes() < 10) {
        return null;
    }/*from w ww  . ja v a2s.  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;
}