protected Node findNode() throws ProtocolException { Node result = null; BidibNode rootNode = getBidib().getRootNode(); int count = rootNode.getNodeCount(); for (int index = 1; index <= count; index++) { Node node = rootNode.getNextNode(); if (node != null && (node.getUniqueId() & 0xffffffffffffffL) == nodeId.longValue()) { int magic = getBidib().getNode(node).getMagic(null); LOGGER.info("Node returned magic: {}", magic); // after we fetch the magic we must try to get the FEATURE_RELEVANT_PID_BITS Feature relevantPidBits = getBidib().getNode(node).getFeature(BidibLibrary.FEATURE_RELEVANT_PID_BITS); if (relevantPidBits != null) { node.setFeature(relevantPidBits); node.setRelevantPidBits(relevantPidBits.getValue()); } result = node; break; } } return result; }
&& (bidibNode.getNodeMagic() != null || message instanceof SysMagicResponse)) { int numExpected = bidibNode.getNextReceiveMsgNum(message); int numReceived = message.getNum(); LOGGER new Object[] { message, numExpected, bidibNode }); if (!ignoreWrongMessageNumber.get() && bidibNode.getNodeMagic() != null) { throw new ProtocolException( "wrong message number: expected " + numExpected + " but got " + numReceived); numReceived); bidibNode.adjustReceiveMsgNum(numReceived);
protected void messageReceived(final BidibNode bidibNode, final BidibMessage message) { // put the message into the receiveQueue because somebody waits for it ... LOGGER.debug("Offer received message to node: {}, message: {}", bidibNode, message); try { bidibNode.addResponseToReceiveQueue(message); } catch (Exception ex) { LOGGER.error("Offer received message to node failed. Message was: " + message, ex); } }
protected BidibNode createBidibNode(final Node node) { // create the new bidib node BidibNode bidibNode = new BidibNode(node.getAddr(), messageReceiver, ignoreWaitTimeout); // initialize the node bidibNode.setUniqueId(node.getUniqueId()); bidibNode.setBidib(bidib); bidibNode.setRequestFactory(requestFactory); bidibNode.setResponseTimeout(bidib.getResponseTimeout()); bidibNode.setFirmwarePacketTimeout(bidib.getFirmwarePacketTimeout()); LOGGER.info("Created new bidibNode, firmwarePacketTimeout: {}", bidib.getFirmwarePacketTimeout()); return bidibNode; }
@Test public void receiveNodeTabResponseTest() throws IOException, ProtocolException { NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); BlockingQueue<BidibMessage> receiveQueue = Mockito.mock(BlockingQueue.class); SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue // 11.08.2013 22:38:40.383: receive NodeTabResponse[[1, 0],num=2,type=137,data=[1, 0, 129, 0, 13, 114, 0, 31, // 0]] : 0d 01 00 02 89 01 00 81 00 0d 72 00 1f 00 byte[] message = new byte[] { 0x0d, 0x01, 0x00, 0x02, (byte) 0x89, 0x01, 0x00, (byte) 0x81, 0x00, (byte) 0x0d, (byte) 0x72, 0x00, 0x1f, 0x00, (byte) 0xFE }; final byte[] nodeAddress = new byte[] { 1 }; Mockito.when(nodeFactory.findNode(nodeAddress)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(2)); // Mockito.when(bidibNode.getReceiveQueue()).thenReturn(receiveQueue); Mockito.doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); receiveQueue.offer((BidibMessage) args[0]); return null; } }).when(bidibNode).addResponseToReceiveQueue(Mockito.any(BidibMessage.class)); receiver.receive(pack(message)); BidibMessage response = new BidibResponseFactory().create(message); // verify that message was signaled Mockito.verify(receiveQueue, Mockito.times(1)).offer(response); }
@Test public void receiveBoostStatResponseTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x04, 0x00, 0x3a, (byte) 0xb0, (byte) 0x06, (byte) 0x74, (byte) 0xFE }; final byte[] nodeAddress = new byte[] { 0 }; Mockito.when(nodeFactory.findNode(nodeAddress)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(58)); receiver.receive(pack(data)); // verify that booster state is on Mockito.verify(messageListener, Mockito.never()).boosterState(address, BoosterState.ON); }
if (bidibNode != null && bidibNode.isSecureAckEnabled()) { bidibNode.acknowledgeFree(((FeedbackFreeResponse) message).getDetectorNumber()); nodeRegistry.triggerPendingAcknowledge(bidibNode); if (bidibNode != null && bidibNode.isSecureAckEnabled()) { bidibNode.acknowledgeMultiple(baseAddress, size, detectorData); nodeRegistry.triggerPendingAcknowledge(bidibNode); FeedbackOccupiedResponse feedbackOccupiedResponse = (FeedbackOccupiedResponse) message; if (bidibNode != null && bidibNode.isSecureAckEnabled()) { bidibNode.acknowledgeOccupied(feedbackOccupiedResponse.getDetectorNumber()); nodeRegistry.triggerPendingAcknowledge(bidibNode); if (bidibNode != null && bidibNode.isSecureAckEnabled()) { bidibNode .acknowledgePosition(feedbackPositionResponse.getDecoderAddress(), feedbackPositionResponse.getLocationType(), feedbackPositionResponse.getLocationAddress()); nodeRegistry.triggerPendingAcknowledge(bidibNode); bidibNode.acknowledgeNodeChanged(node.getVersion()); nodeRegistry.triggerPendingAcknowledge(bidibNode); bidibNode.acknowledgeNodeChanged(nodeLostResponse.getNodeTabVersion()); nodeRegistry.triggerPendingAcknowledge(bidibNode);
/** * Get the magic from the root node * * @return the magic provided by the root node * @throws ProtocolException */ private int sendMagic() throws ProtocolException { BidibNode rootNode = getRootNode(); // Ignore the first exception ... int magic = -1; try { magic = rootNode.getMagic(15000); } catch (Exception e) { LOGGER.warn("Get magic from node failed. Retry get magic from node."); magic = rootNode.getMagic(15000); } LOGGER.info("The node returned magic: {}", magic); return magic; }
/** * Verify if the node supports FW updates. * * @return true if the node has the FW update feature set, false otherwise * @throws ProtocolException */ public boolean isUpdatable(Node node) throws ProtocolException { try { Feature feature = getFeature(BidibLibrary.FEATURE_FW_UPDATE_MODE); if (feature != null) { node.setFeature(feature); return feature.getValue() == 1; } } catch (ProtocolException ex) { LOGGER.warn("Check if node is updatable caused protocol exception.", ex); } return false; }
@Test public void receiveMagicResponseTest() throws IOException, ProtocolException { NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); BlockingQueue<BidibMessage> receiveQueue = Mockito.mock(BlockingQueue.class); SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue // 0x05, 0x00, 0x00, (byte) 0x81, (byte) 0xFD, (byte) 0xDE, (byte) 0xAF, (byte) 0x89, 0xFE byte[] rawmessage = new byte[] { 0x05, 0x00, 0x00, (byte) 0x81, (byte) 0xFD, (byte) 0xDE, (byte) 0xAF, (byte) 0x89, (byte) 0xFE }; byte[] message = new byte[] { 0x05, 0x00, 0x00, (byte) 0x81, (byte) 0xFE, (byte) 0xAF }; final byte[] nodeAddress = new byte[] { 0 }; Mockito.when(nodeFactory.findNode(nodeAddress)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(0)); // Mockito.when(bidibNode.getReceiveQueue()).thenReturn(receiveQueue); Mockito.doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); receiveQueue.offer((BidibMessage) args[0]); return null; } }).when(bidibNode).addResponseToReceiveQueue(Mockito.any(BidibMessage.class)); receiver.receive(pack(rawmessage)); BidibMessage response = new BidibResponseFactory().create(message); // verify that message was signaled Mockito.verify(receiveQueue, Mockito.times(1)).offer(response); }
@Test public void receiveMessageTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // prepare the object to test SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x05, 0x00, 0x01, (byte) 0x86, (byte) 0x02, (byte) 0x00, (byte) 0x46, (byte) 0xFE }; Mockito.when(nodeFactory.findNode(address)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(1)); receiver.receive(pack(data)); // verify that error was called once Mockito.verify(messageListener, Mockito.times(1)).error(address, 2, address); }
/** * Get the magic from the node. * * @param receiveTimeout * the timeout in milliseconds used to wait for a response from the node. If null the default timeout is * used. * @return the magic * @throws ProtocolException */ @Override public int getMagic(Integer receiveTimeout) throws ProtocolException { LOGGER.trace("Get magic from root node!"); int magic = super.getMagic(receiveTimeout); LOGGER.info("Get magic from root node returns: {}", magic); if (BIDIB_MAGIC_UNKNOWN == magic) { LOGGER.warn("The interface did not respond the get magic request!"); StringBuilder sb = new StringBuilder("The interface did not respond the get magic request!"); String errorInfo = getMessageReceiver().getErrorInformation(); if (StringUtils.isNotBlank(errorInfo)) { LOGGER.warn("Found received data that was not identifed as BiDiB messages: {}", errorInfo); sb.append("\r\n"); sb.append(errorInfo); } NoAnswerException ex = new NoAnswerException("Establish communication with interface failed."); ex.setDescription(sb.toString()); LOGGER.warn("Prepared exception to throw:", ex); throw ex; } return magic; }
Feature feature = accessoryNode.getBidibNode().getFeature(BidibLibrary.FEATURE_CTRL_MAC_COUNT); int macroCount = feature != null ? feature.getValue() : 0; feature = accessoryNode.getBidibNode().getFeature(BidibLibrary.FEATURE_CTRL_MAC_SIZE);
Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(46)); return null; }).when(bidibNode).addResponseToReceiveQueue(Mockito.any(BidibMessage.class));
@Test public void receiveMessageTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // prepare the object to test NetMessageReceiver receiver = new NetMessageReceiver(nodeFactory, false); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x05, 0x00, 0x01, (byte) 0x86, (byte) 0x02, (byte) 0x00, 0x06, 0x00, 0x01, (byte) 0xc1, 0x00, 0x01, 0x02 }; Mockito.when(nodeFactory.findNode(address)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(1)); receiver.receive(pack(data)); // verify that error was called once Mockito.verify(messageListener, Mockito.times(1)).error(address, 2, address); Mockito.verify(messageListener, Mockito.times(1)).lcNa(Mockito.eq(address), Mockito.any(), Mockito.eq(2)); } }
Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(113)); return null; }).when(bidibNode).addResponseToReceiveQueue(Mockito.any(BidibMessage.class));
@Test public void receiveMessageLeadingMagicTest() throws IOException { byte[] address = new byte[] { 0 }; NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // prepare the object to test SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { (byte) 0xFE, 0x05, 0x00, 0x01, (byte) 0x86, (byte) 0x02, (byte) 0x00, (byte) 0x46, (byte) 0xFE }; Mockito.when(nodeFactory.findNode(address)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(1)); receiver.receive(pack(data)); // verify that error was called once Mockito.verify(messageListener, Mockito.times(1)).error(address, 2, address); }
.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))) .thenReturn(Integer.valueOf(0), Integer.valueOf(1)); return null; }).when(bidibNode).addResponseToReceiveQueue(Mockito.any(BidibMessage.class));
@Test public void receive2MessagesTest() throws IOException, ProtocolException { BidibMessage featureResponse1 = new BidibResponseFactory().create(new byte[] { 0x06, 0x01, 0x00, 0x0e, (byte) 0x90, 0x00, 0x20 }); NodeRegistry nodeFactory = Mockito.mock(NodeRegistry.class); BidibNode bidibNode = Mockito.mock(BidibNode.class); MessageListener messageListener = Mockito.mock(MessageListener.class); // BlockingQueue<BidibMessage> receiveQueue = Mockito.mock(BlockingQueue.class); SerialMessageReceiver receiver = new SerialMessageReceiver(nodeFactory); receiver.addMessageListener(messageListener); // set the receive queue byte[] data = new byte[] { 0x06, 0x01, 0x00, 0x0e, (byte) 0x90, 0x00, 0x20, /* CRC */ (byte) 0xA7, (byte) 0xFE, /* start 2nd message */0x06, 0x01, 0x00, 0x0f, (byte) 0x90, 0x00, 0x20, /* * CRC */ (byte) 0x28, (byte) 0xFE }; final byte[] nodeAddress = new byte[] { 1 }; Mockito.when(nodeFactory.findNode(nodeAddress)).thenReturn(bidibNode); Mockito.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(0x0f)); Mockito.when(bidibNode.getNextReceiveMsgNum(featureResponse1)).thenReturn(Integer.valueOf(0x0e)); // Mockito.when(bidibNode.getReceiveQueue()).thenReturn(receiveQueue); receiver.receive(pack(data)); // verify that no message listener methods were called. Mockito.verifyZeroInteractions(messageListener); }
.when(bidibNode.getNextReceiveMsgNum(Mockito.any(BidibMessage.class))).thenReturn(Integer.valueOf(0x17), Integer.valueOf(0x18), Integer.valueOf(0x19), Integer.valueOf(0x1A), Integer.valueOf(0x1B), Integer.valueOf(0x1C), Integer.valueOf(0x1D)); return null; }).when(bidibNode).addResponseToReceiveQueue(Mockito.any(BidibMessage.class));