Example usage for io.netty.buffer ByteBuf readByte

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

Introduction

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

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:com.whizzosoftware.wzwave.codec.ZWaveFrameDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("RCVD: {}", ByteUtil.createString(in));
    }/*from w  ww.ja v a 2s  .  c  om*/

    ByteBuf data;

    // if there was data left from the last decode call, create a composite ByteBuf that contains both
    // previous and new data
    if (previousBuf != null) {
        CompositeByteBuf cbuf = Unpooled.compositeBuffer(2);
        cbuf.addComponent(previousBuf.copy());
        cbuf.addComponent(in);
        cbuf.writerIndex(previousBuf.readableBytes() + in.readableBytes());
        data = cbuf;
        // release the data from the previous decode call
        previousBuf.release();
        previousBuf = null;
    } else {
        data = in;
    }

    while (data.isReadable()) {
        // check for single ACK/NAK/CAN
        if (data.readableBytes() == 1 && isSingleByteFrame(data, data.readerIndex())) {
            out.add(createSingleByteFrame(data));
        } else {
            boolean foundFrame = false;
            // search for a valid frame in the data
            for (int searchStartIx = data.readerIndex(); searchStartIx < data.readerIndex()
                    + data.readableBytes(); searchStartIx++) {
                if (data.getByte(searchStartIx) == DataFrame.START_OF_FRAME) {
                    int frameEndIx = scanForFrame(data, searchStartIx);
                    if (frameEndIx > 0) {
                        if (searchStartIx > data.readerIndex() && isSingleByteFrame(data, searchStartIx - 1)) {
                            data.readerIndex(searchStartIx - 1);
                            out.add(createSingleByteFrame(data));
                        } else if (searchStartIx > data.readerIndex()) {
                            data.readerIndex(searchStartIx);
                        }
                        DataFrame df = createDataFrame(data);
                        if (df != null) {
                            out.add(df);
                            data.readByte(); // discard checksum
                            foundFrame = true;
                        } else {
                            logger.debug("Unable to determine frame type");
                        }
                    }
                }
            }
            if (!foundFrame) {
                previousBuf = data.copy();
                break;
            }
        }
    }

    // make sure we read from the input ByteBuf so Netty doesn't throw an exception
    in.readBytes(in.readableBytes());

    logger.trace("Done processing received data: {}", out);
}

From source file:com.whizzosoftware.wzwave.codec.ZWaveFrameDecoder.java

License:Open Source License

private Frame createSingleByteFrame(ByteBuf data) {
    byte b = data.readByte();
    if (b == ACK.ID) {
        return new ACK();
    } else if (b == NAK.ID) {
        return new NAK();
    } else if (b == CAN.ID) {
        return new CAN();
    } else {//from  w w  w .  j  a  va 2s  .  c o  m
        return null;
    }
}

From source file:com.whizzosoftware.wzwave.frame.AddNodeToNetwork.java

License:Open Source License

public AddNodeToNetwork(ByteBuf buffer) {
    super(buffer);

    funcId = buffer.readByte();
    status = buffer.readByte();//from   w w w.  j  a v a 2  s  .c  o  m
    source = buffer.readByte();

    // read node info if present
    byte len = buffer.readByte();
    if (len > 0) {
        byte basicClass = buffer.readByte();
        byte genericClass = buffer.readByte();
        byte specificClass = buffer.readByte();
        byte[] commandClasses = new byte[len - 3];
        for (int i = 0; i < len - 3; i++) {
            commandClasses[i] = buffer.readByte();
        }
        nodeInfo = new NodeInfo(source, basicClass, genericClass, specificClass, commandClasses);
    }
}

From source file:com.whizzosoftware.wzwave.frame.ApplicationCommand.java

License:Open Source License

public ApplicationCommand(ByteBuf buffer) {
    super(buffer);

    this.status = buffer.readByte();
    this.nodeId = buffer.readByte();

    byte cmdLength = buffer.readByte();
    commandClassBytes = buffer.readBytes(cmdLength).array();
}

From source file:com.whizzosoftware.wzwave.frame.ApplicationUpdate.java

License:Open Source License

public ApplicationUpdate(ByteBuf buffer) {
    super(buffer);
    state = buffer.readByte();
    this.nodeId = buffer.readByte();
    if (state == UPDATE_STATE_NODE_INFO_RECEIVED) {
        nodeInfo = new NodeInfo(this.nodeId, buffer, dataFrameLength - 6);
    } else {//  w w  w  .j  a  va  2 s.c  o m
        buffer.readByte(); // read 0 length
    }
}

From source file:com.whizzosoftware.wzwave.frame.DataFrame.java

License:Open Source License

/**
 * Constructor.//from   w w  w . j a  v a  2  s . co m
 *
 * @param buffer the readable byte buffer
 */
public DataFrame(ByteBuf buffer) {
    if (buffer.readByte() != START_OF_FRAME) {
        throw new RuntimeException("Data frame parsing error: no SOF");
    }
    this.dataFrameLength = buffer.readByte();
    if (buffer.readableBytes() < dataFrameLength) {
        throw new RuntimeException("Data framing length error");
    }
    this.type = (buffer.readByte() == 0 ? DataFrameType.REQUEST : DataFrameType.RESPONSE);
    this.commandId = buffer.readByte();
}

From source file:com.whizzosoftware.wzwave.frame.InitData.java

License:Open Source License

public InitData(ByteBuf buffer) {
    super(buffer);

    this.version = buffer.readByte();
    this.capabilities = buffer.readByte();

    // read the node information form the bit mask
    if (buffer.readByte() == NODE_BITMASK_SIZE) {
        // 29 bytes * 8 bits == 232 == the number of possible nodes in the network
        for (int i = 0; i < NODE_BITMASK_SIZE; i++) {
            byte b = buffer.readByte();
            for (int j = 0; j < 8; j++) {
                byte nodeId = (byte) ((i * 8) + j + 1);
                if ((b & (0x01 << j)) > 0) {
                    nodes.add(nodeId);// w  ww . j a  v a  2  s.c  o  m
                }
            }
        }
    }

    buffer.readByte(); // unsure what this is
    buffer.readByte(); // unsure what this is
}

From source file:com.whizzosoftware.wzwave.frame.MemoryGetId.java

License:Open Source License

public MemoryGetId(ByteBuf buffer) {
    super(buffer);

    homeId = (((int) buffer.readByte()) << 24) | (((int) buffer.readByte()) << 16)
            | (((int) buffer.readByte()) << 8) | ((int) buffer.readByte());
    nodeId = buffer.readByte();/*from ww  w.  ja va2  s.c  o m*/
}

From source file:com.whizzosoftware.wzwave.frame.NodeProtocolInfo.java

License:Open Source License

public NodeProtocolInfo(ByteBuf buffer) {
    super(buffer);

    byte b4 = buffer.readByte();
    byte b5 = buffer.readByte();
    byte b6 = buffer.readByte();
    byte b7 = buffer.readByte();
    byte b8 = buffer.readByte();
    byte b9 = buffer.readByte();

    // capabilities
    listening = ((b4 & 0x80) != 0);/*from ww  w  .  jav  a2 s .  c o  m*/
    beaming = ((b5 & 0x10) != 0);
    routing = ((b4 & 0x40) != 0);

    maxBaudRate = 9600;
    if ((b4 & 0x38) == 0x10) {
        maxBaudRate = 40000;
    }

    version = (b4 & 0x07) + 1;
    security = ((b5 & 0x01) != 0);

    // device classes
    basicDeviceClass = b7;
    genericDeviceClass = b8;
    specificDeviceClass = b9;
}

From source file:com.whizzosoftware.wzwave.frame.RemoveNodeFromNetwork.java

License:Open Source License

public RemoveNodeFromNetwork(ByteBuf buffer) {
    super(buffer);

    funcId = buffer.readByte();
    status = buffer.readByte();//  ww  w .j a  v  a2 s .  c  om
    source = buffer.readByte();

    // read node info if present
    byte len = buffer.readByte();
    if (len > 0) {
        byte basicClass = buffer.readByte();
        byte genericClass = buffer.readByte();
        byte specificClass = buffer.readByte();
        byte[] commandClasses = new byte[len - 3];
        for (int i = 0; i < len - 3; i++) {
            commandClasses[i] = buffer.readByte();
        }
        nodeInfo = new NodeInfo(source, basicClass, genericClass, specificClass, commandClasses);
    }
}