Skip to content

Commit d71d906

Browse files
committed
packets_queue: fix an issue when the device's 16-bit address is unknown
This problem was causing the 'readDataFrom' method to return the first data packet on the queue, not the first one from the given remote device. Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent 15749f2 commit d71d906

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

library/src/main/java/com/digi/xbee/api/models/XBeePacketsQueue.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, Digi International Inc.
2+
* Copyright 2017-2020, Digi International Inc.
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -534,15 +534,21 @@ private boolean addressesMatch(XBeePacket xbeePacket, RemoteXBeeDevice remoteXBe
534534
APIFrameType packetType = ((XBeeAPIPacket)xbeePacket).getFrameType();
535535
switch (packetType) {
536536
case RECEIVE_PACKET:
537-
if (remoteXBeeDevice.get64BitAddress() != null && ((ReceivePacket)xbeePacket).get64bitSourceAddress().equals(remoteXBeeDevice.get64BitAddress()))
537+
if (remoteXBeeDevice.get64BitAddress() != null
538+
&& ((ReceivePacket)xbeePacket).get64bitSourceAddress().equals(remoteXBeeDevice.get64BitAddress()))
538539
return true;
539-
if (remoteXBeeDevice.get16BitAddress() != null && ((ReceivePacket)xbeePacket).get16bitSourceAddress().equals(remoteXBeeDevice.get16BitAddress()))
540+
if (remoteXBeeDevice.get16BitAddress() != null
541+
&& !remoteXBeeDevice.get16BitAddress().equals(XBee16BitAddress.UNKNOWN_ADDRESS)
542+
&& ((ReceivePacket)xbeePacket).get16bitSourceAddress().equals(remoteXBeeDevice.get16BitAddress()))
540543
return true;
541544
break;
542545
case REMOTE_AT_COMMAND_RESPONSE:
543-
if (remoteXBeeDevice.get64BitAddress() != null && ((RemoteATCommandResponsePacket)xbeePacket).get64bitSourceAddress().equals(remoteXBeeDevice.get64BitAddress()))
546+
if (remoteXBeeDevice.get64BitAddress() != null
547+
&& ((RemoteATCommandResponsePacket)xbeePacket).get64bitSourceAddress().equals(remoteXBeeDevice.get64BitAddress()))
544548
return true;
545-
if (remoteXBeeDevice.get16BitAddress() != null && ((RemoteATCommandResponsePacket)xbeePacket).get16bitSourceAddress().equals(remoteXBeeDevice.get16BitAddress()))
549+
if (remoteXBeeDevice.get16BitAddress() != null
550+
&& !remoteXBeeDevice.get16BitAddress().equals(XBee16BitAddress.UNKNOWN_ADDRESS)
551+
&& ((RemoteATCommandResponsePacket)xbeePacket).get16bitSourceAddress().equals(remoteXBeeDevice.get16BitAddress()))
546552
return true;
547553
break;
548554
case RX_16:

library/src/test/java/com/digi/xbee/api/models/XBeePacketsQueueTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017, Digi International Inc.
2+
* Copyright 2017-2020, Digi International Inc.
33
*
44
* This Source Code Form is subject to the terms of the Mozilla Public
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -1438,6 +1438,49 @@ public void testAddressesMatch16BitAddress() throws Exception {
14381438
assertFalse((Boolean)Whitebox.invokeMethod(xbeePacketsQueue, METHOD_ADDRESSES_MATCH, packet, mockedRemoteDevice));
14391439
}
14401440

1441+
/**
1442+
* Test method for {@link com.digi.xbee.api.models.XBeePacketsQueue#addressesMatch(XBeePacket, RemoteXBeeDevice)}.
1443+
*
1444+
* <p>Verify that the {@code addressesMatch} method of the {@code XBeePacketsQueue} class works
1445+
* successfully when the device's 16-bit address is unknown.</p>
1446+
*/
1447+
@Test
1448+
public void testAddressesDontMatchUnkown16BitAddress() throws Exception {
1449+
ArrayList<XBeePacket> api16Packets = new ArrayList<XBeePacket>();
1450+
1451+
// Create a mocked remote XBee device.
1452+
RemoteXBeeDevice mockedRemoteDevice = Mockito.mock(RemoteXBeeDevice.class);
1453+
Mockito.when(mockedRemoteDevice.get64BitAddress()).thenReturn(xbee64BitAddress1);
1454+
Mockito.when(mockedRemoteDevice.get16BitAddress()).thenReturn(XBee16BitAddress.UNKNOWN_ADDRESS);
1455+
1456+
// Fill the list of API packets.
1457+
api16Packets.add(mockedReceivePacket);
1458+
api16Packets.add(mockedRemoteATCommandPacket);
1459+
1460+
// Create an XBeePacketsQueue.
1461+
XBeePacketsQueue xbeePacketsQueue = PowerMockito.spy(new XBeePacketsQueue());
1462+
1463+
// Verify the addresses don't match.
1464+
Mockito.when(mockedReceivePacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress2);
1465+
Mockito.when(mockedReceivePacket.get16bitSourceAddress()).thenReturn(XBee16BitAddress.UNKNOWN_ADDRESS);
1466+
Mockito.when(mockedRemoteATCommandPacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress2);
1467+
Mockito.when(mockedRemoteATCommandPacket.get16bitSourceAddress()).thenReturn(XBee16BitAddress.UNKNOWN_ADDRESS);
1468+
for (XBeePacket packet:api16Packets)
1469+
assertFalse((Boolean)Whitebox.invokeMethod(xbeePacketsQueue, METHOD_ADDRESSES_MATCH, packet, mockedRemoteDevice));
1470+
1471+
// Verify the addresses don't match.
1472+
Mockito.when(mockedReceivePacket.get16bitSourceAddress()).thenReturn(xbee16BitAddress2);
1473+
Mockito.when(mockedRemoteATCommandPacket.get16bitSourceAddress()).thenReturn(xbee16BitAddress2);
1474+
for (XBeePacket packet:api16Packets)
1475+
assertFalse((Boolean)Whitebox.invokeMethod(xbeePacketsQueue, METHOD_ADDRESSES_MATCH, packet, mockedRemoteDevice));
1476+
1477+
// Verify the addresses match.
1478+
Mockito.when(mockedReceivePacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress1);
1479+
Mockito.when(mockedRemoteATCommandPacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress1);
1480+
for (XBeePacket packet:api16Packets)
1481+
assertTrue((Boolean)Whitebox.invokeMethod(xbeePacketsQueue, METHOD_ADDRESSES_MATCH, packet, mockedRemoteDevice));
1482+
}
1483+
14411484
/**
14421485
* Test method for {@link com.digi.xbee.api.models.XBeePacketsQueue#ipAddressesMatch(XBeePacket, Inet4Address)}.
14431486
*

0 commit comments

Comments
 (0)