Example usage for io.netty.buffer ByteBuf getUnsignedShort

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

Introduction

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

Prototype

public abstract int getUnsignedShort(int index);

Source Link

Document

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

Usage

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;
    }/*from   w  w w.  j a  va2 s  . 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.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   www.j  av  a 2  s.com
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress,
            buf.readSlice(7).toString(StandardCharsets.US_ASCII));
    if (deviceSession == null) {
        return null;
    }//from w w  w .  j a  va2  s  .c om

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

    position.set("eventType", buf.readUnsignedByte());
    position.set("packetVersion", buf.readUnsignedByte());
    position.set(Position.KEY_STATUS, buf.readUnsignedByte());
    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
    position.set(Position.KEY_GPS, buf.readUnsignedByte());

    position.setTime(new DateBuilder()
            .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
            .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate());

    position.setValid(true);

    double lat = buf.getUnsignedShort(buf.readerIndex()) / 100;
    lat += (buf.readUnsignedShort() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
    position.setLatitude(buf.readUnsignedByte() == 'S' ? -lat : lat);

    double lon = buf.getUnsignedMedium(buf.readerIndex()) / 100;
    lon += (buf.readUnsignedMedium() % 100 * 10000 + buf.readUnsignedShort()) / 600000.0;
    position.setLongitude(buf.readUnsignedByte() == 'W' ? -lon : lon);

    position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));

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

    position.set("analogAlerts", buf.readUnsignedByte());
    position.set("customAlertTypes", buf.readUnsignedShort());

    for (int i = 1; i <= 5; i++) {
        position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort());
    }

    position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium());
    position.set(Position.KEY_RPM, buf.readUnsignedShort());

    if (channel != null) {
        channel.writeAndFlush(
                new NetworkMessage(Unpooled.copiedBuffer("ACK", StandardCharsets.US_ASCII), remoteAddress));
    }

    return position;
}

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

License:Apache License

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

    // Check minimum length
    if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }//  ww  w .j  a  v a2s .c  o m

    // Read packet
    int length = buf.getUnsignedShort(buf.readerIndex());
    if (length > 0) {
        if (buf.readableBytes() >= (length + 2)) {
            return buf.readRetainedSlice(length + 2);
        }
    } else {
        int dataLength = buf.getInt(buf.readerIndex() + 4);
        if (buf.readableBytes() >= (dataLength + 12)) {
            return buf.readRetainedSlice(dataLength + 12);
        }
    }

    return null;
}

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

License:Apache License

private Object decodeTcp(Channel channel, SocketAddress remoteAddress, ByteBuf buf) throws Exception {

    if (buf.getUnsignedShort(0) > 0) {
        parseIdentification(channel, remoteAddress, buf);
    } else {//from   w  w w . ja  va  2  s. c o  m
        buf.skipBytes(4);
        return parseData(channel, remoteAddress, buf, 0);
    }

    return null;
}

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

License:Apache License

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

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

    int length;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0x80) {
        length = buf.getUnsignedShortLE(buf.readerIndex() + 6);
    } else {
        length = buf.getUnsignedShort(buf.readerIndex() + 6);
    }

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

    return null;
}

From source file:pl.tcs.btppllib.OcitResponseTelegramHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg; // (1)
    log.debug("****** Message received ******");
    log.debug(ByteBufUtil.prettyHexDump(buf));
    log.debug("******************************");

    final int bufLen = buf.readableBytes();

    if (bufLen < 18) {
        throw new CriticalException(String.format("Incomplete telegram received [size]=%d", bufLen));
    }/*from  w  w  w.  j  a  v a 2 s  . c  o m*/

    try {
        // member = 6, otype = 8, method = 10
        int memberPos = 6, otypePos = 8, methodPos = 10;

        final int member = buf.getUnsignedShort(memberPos);
        final int otype = buf.getUnsignedShort(otypePos);
        final int method = buf.getUnsignedShort(methodPos);

        Class handlerClazz = HandlersMapping.getResponseHandler(member, otype, method);
        if (handlerClazz != null) {
            log.debug(String.format("Found response handler for member=%d otype=%d method=%d with name=%s",
                    member, otype, method, handlerClazz.getSimpleName()));

            OcitResponseTelegram resp = (OcitResponseTelegram) handlerClazz.newInstance();
            resp.fromByteBuf(buf); // initialize fields

            List<ResponseTelegramAction> allActions = resp.getActions();
            for (ResponseTelegramAction action : allActions) {
                executor.submit(action);
            }
        }
    } catch (InstantiationException | IllegalAccessException e) {
        throw new CriticalException(e);
    } finally {
        buf.release();
    }
}