@Test public void testReceivePacketReset() { int[] response = getPacket(new int[] { 0x00, 0x02, 0xA7 }); assertNotNull(response); assertEquals(2, response.length); assertEquals(0x00, response[0]); assertEquals(0x02, response[1]); }
private int[] getPacket() throws IOException { int[] inputBuffer = new int[SPI_MAX_LENGTH]; int inputCount = 0; int inputMax = 0; int inputLength = 0; RxState rxState = RxState.RX_TYPE; while (!closeHandler) { int val = port.read(); logger.trace("SPI RX: {}", String.format("%02X", val)); switch (rxState) { case RX_TYPE: inputBuffer[0] = val; if (val < 10) { inputLength = 1; inputCount = 0; inputMax = 1; rxState = RxState.RX_DATA; } else { rxState = RxState.RX_LENGTH; } break; case RX_LENGTH: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, 1); } inputMax = val; inputBuffer[1] = val; if (val >= SPI_MAX_LENGTH) { rxState = RxState.RX_SEARCH; break; } else if (val == 0) { rxState = RxState.RX_FLAG; } else { rxState = RxState.RX_DATA; } inputLength = 2; inputCount = 0; break; case RX_DATA: inputBuffer[inputLength++] = val; if (++inputCount == inputMax) { rxState = RxState.RX_FLAG; } break; case RX_FLAG: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, inputLength); } rxState = RxState.RX_SEARCH; break; case RX_SEARCH: if (val == SPI_FLAG_BYTE) { rxState = RxState.RX_TYPE; } break; default: logger.error("Unknown state {} in SPI receive state machine", rxState); rxState = RxState.RX_SEARCH; break; } } return null; }
SpiFrameHandler implements EzspProtocolHandler { private int[] getPacket() throws IOException { int[] inputBuffer = new int[SPI_MAX_LENGTH]; int inputCount = 0; int inputMax = 0; int inputLength = 0; RxState rxState = RxState.RX_TYPE; while (!closeHandler) { int val = port.read(); logger.trace("SPI RX: {}", String.format("%02X", val)); switch (rxState) { case RX_TYPE: inputBuffer[0] = val; if (val < 10) { inputLength = 1; inputCount = 0; inputMax = 1; rxState = RxState.RX_DATA; } else { rxState = RxState.RX_LENGTH; } break; case RX_LENGTH: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, 1); } inputMax = val; inputBuffer[1] = val; if (val >= SPI_MAX_LENGTH) { rxState = RxState.RX_SEARCH; break; } else if (val == 0) { rxState = RxState.RX_FLAG; } else { rxState = RxState.RX_DATA; } inputLength = 2; inputCount = 0; break; case RX_DATA: inputBuffer[inputLength++] = val; if (++inputCount == inputMax) { rxState = RxState.RX_FLAG; } break; case RX_FLAG: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, inputLength); } rxState = RxState.RX_SEARCH; break; case RX_SEARCH: if (val == SPI_FLAG_BYTE) { rxState = RxState.RX_TYPE; } break; default: logger.error("Unknown state {} in SPI receive state machine", rxState); rxState = RxState.RX_SEARCH; break; } } return null; } }
SpiFrameHandler implements EzspProtocolHandler { private int[] getPacket() throws IOException { int[] inputBuffer = new int[SPI_MAX_LENGTH]; int inputCount = 0; int inputMax = 0; int inputLength = 0; RxState rxState = RxState.RX_TYPE; while (!closeHandler) { int val = port.read(); logger.trace("SPI RX: {}", String.format("%02X", val)); switch (rxState) { case RX_TYPE: inputBuffer[0] = val; if (val < 10) { inputLength = 1; inputCount = 0; inputMax = 1; rxState = RxState.RX_DATA; } else { rxState = RxState.RX_LENGTH; } break; case RX_LENGTH: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, 1); } inputMax = val; inputBuffer[1] = val; if (val >= SPI_MAX_LENGTH) { rxState = RxState.RX_SEARCH; break; } else if (val == 0) { rxState = RxState.RX_FLAG; } else { rxState = RxState.RX_DATA; } inputLength = 2; inputCount = 0; break; case RX_DATA: inputBuffer[inputLength++] = val; if (++inputCount == inputMax) { rxState = RxState.RX_FLAG; } break; case RX_FLAG: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, inputLength); } rxState = RxState.RX_SEARCH; break; case RX_SEARCH: if (val == SPI_FLAG_BYTE) { rxState = RxState.RX_TYPE; } break; default: logger.error("Unknown state {} in SPI receive state machine", rxState); rxState = RxState.RX_SEARCH; break; } } return null; } SpiFrameHandler(final EzspFrameHandler frameHandler); }
SpiFrameHandler implements EzspProtocolHandler { private int[] getPacket() throws IOException { int[] inputBuffer = new int[SPI_MAX_LENGTH]; int inputCount = 0; int inputMax = 0; int inputLength = 0; RxState rxState = RxState.RX_TYPE; while (!closeHandler) { int val = port.read(); logger.trace("SPI RX: {}", String.format("%02X", val)); switch (rxState) { case RX_TYPE: inputBuffer[0] = val; if (val < 10) { inputLength = 1; inputCount = 0; inputMax = 1; rxState = RxState.RX_DATA; } else { rxState = RxState.RX_LENGTH; } break; case RX_LENGTH: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, 1); } inputMax = val; inputBuffer[1] = val; if (val >= SPI_MAX_LENGTH) { rxState = RxState.RX_SEARCH; break; } else if (val == 0) { rxState = RxState.RX_FLAG; } else { rxState = RxState.RX_DATA; } inputLength = 2; inputCount = 0; break; case RX_DATA: inputBuffer[inputLength++] = val; if (++inputCount == inputMax) { rxState = RxState.RX_FLAG; } break; case RX_FLAG: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, inputLength); } rxState = RxState.RX_SEARCH; break; case RX_SEARCH: if (val == SPI_FLAG_BYTE) { rxState = RxState.RX_TYPE; } break; default: logger.error("Unknown state {} in SPI receive state machine", rxState); rxState = RxState.RX_SEARCH; break; } } return null; } SpiFrameHandler(final EzspFrameHandler frameHandler); void setPollRate(int pollRate); @Override void start(final ZigBeePort port); @Override void setClosing(); @Override void close(); @Override boolean isAlive(); @Override void queueFrame(EzspFrameRequest request); @Override void connect(); @Override Future<EzspFrame> sendEzspRequestAsync(final EzspTransaction ezspTransaction); @Override EzspTransaction sendEzspTransaction(EzspTransaction ezspTransaction); int getSpiErrors(); @Override Future<EzspFrameResponse> eventWaitAsync(final Class<?> eventClass); @Override EzspFrameResponse eventWait(final Class<?> eventClass, int timeout); @Override Map<String, Long> getCounters(); }
SpiFrameHandler implements EzspProtocolHandler { private int[] getPacket() throws IOException { int[] inputBuffer = new int[SPI_MAX_LENGTH]; int inputCount = 0; int inputMax = 0; int inputLength = 0; RxState rxState = RxState.RX_TYPE; while (!closeHandler) { int val = port.read(); logger.trace("SPI RX: {}", String.format("%02X", val)); switch (rxState) { case RX_TYPE: inputBuffer[0] = val; if (val < 10) { inputLength = 1; inputCount = 0; inputMax = 1; rxState = RxState.RX_DATA; } else { rxState = RxState.RX_LENGTH; } break; case RX_LENGTH: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, 1); } inputMax = val; inputBuffer[1] = val; if (val >= SPI_MAX_LENGTH) { rxState = RxState.RX_SEARCH; break; } else if (val == 0) { rxState = RxState.RX_FLAG; } else { rxState = RxState.RX_DATA; } inputLength = 2; inputCount = 0; break; case RX_DATA: inputBuffer[inputLength++] = val; if (++inputCount == inputMax) { rxState = RxState.RX_FLAG; } break; case RX_FLAG: if (val == SPI_FLAG_BYTE) { return Arrays.copyOfRange(inputBuffer, 0, inputLength); } rxState = RxState.RX_SEARCH; break; case RX_SEARCH: if (val == SPI_FLAG_BYTE) { rxState = RxState.RX_TYPE; } break; default: logger.error("Unknown state {} in SPI receive state machine", rxState); rxState = RxState.RX_SEARCH; break; } } return null; } SpiFrameHandler(final EzspFrameHandler frameHandler); void setPollRate(int pollRate); @Override void start(final ZigBeePort port); @Override void setClosing(); @Override void close(); @Override boolean isAlive(); @Override void queueFrame(EzspFrameRequest request); @Override void connect(); @Override Future<EzspFrame> sendEzspRequestAsync(final EzspTransaction ezspTransaction); @Override EzspTransaction sendEzspTransaction(EzspTransaction ezspTransaction); int getSpiErrors(); @Override Future<EzspFrameResponse> eventWaitAsync(final Class<?> eventClass); @Override EzspFrameResponse eventWait(final Class<?> eventClass, int timeout); @Override Map<String, Long> getCounters();  }