Skip to content

Commit

Permalink
devices: remove casts to XBeeDevice class
Browse files Browse the repository at this point in the history
- New extensions or layers of the XBee Java library (such as the XBee Library
  for Android) can add new devices extending the AbstractXBeeDevice class. This
  means that we cannot expect to always have an XBeeDevice object in internal
  constructors or methods of the library. Thus, all casts to XBeeDevice had to
  disappear.
- In order to remove casts to XBeeDevice, most methods and constructors that
  where using an XBeeDevice as parameter had to change to AbstractXBeeDevice.
  XBeeDevice inherits from AbstractXBeeDevice, so there shouldn't be any
  problem with examples or code already written by clients.
- Moved the class DataReader to the root path of the library so that it can
  access the protected method 'getNetwork()' of XBee devices. DataReader is
  an internal class used by the AbstractXBeeDevice one. So again, there
  shouldn't be any problem with code already written.
- Added IllegalArgumentException to some methods and constructors that are
  now using the AbstractXBeeDevice as parameter when the AbstractXBeeDevice
  is a remote device.
- Updated references in test cases.

Signed-off-by: Diego Escalona <[email protected]>
  • Loading branch information
diescalo committed Jul 8, 2019
1 parent 11c6576 commit c5f2270
Show file tree
Hide file tree
Showing 31 changed files with 219 additions and 216 deletions.
37 changes: 18 additions & 19 deletions library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.digi.xbee.api.connection.IConnectionInterface;
import com.digi.xbee.api.connection.bluetooth.AbstractBluetoothInterface;
import com.digi.xbee.api.connection.ConnectionType;
import com.digi.xbee.api.connection.DataReader;
import com.digi.xbee.api.connection.serial.SerialPortParameters;
import com.digi.xbee.api.exceptions.ATCommandException;
import com.digi.xbee.api.exceptions.BluetoothAuthenticationException;
Expand Down Expand Up @@ -211,8 +210,8 @@ public abstract class AbstractXBeeDevice {
*
* @see #AbstractXBeeDevice(IConnectionInterface)
* @see #AbstractXBeeDevice(String, SerialPortParameters)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(String, int, int, int, int, int)
*/
public AbstractXBeeDevice(String port, int baudRate) {
Expand Down Expand Up @@ -240,8 +239,8 @@ public AbstractXBeeDevice(String port, int baudRate) {
* @see #AbstractXBeeDevice(IConnectionInterface)
* @see #AbstractXBeeDevice(String, int)
* @see #AbstractXBeeDevice(String, SerialPortParameters)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
*/
public AbstractXBeeDevice(String port, int baudRate, int dataBits, int stopBits, int parity, int flowControl) {
this(port, new SerialPortParameters(baudRate, dataBits, stopBits, parity, flowControl));
Expand All @@ -259,8 +258,8 @@ public AbstractXBeeDevice(String port, int baudRate, int dataBits, int stopBits,
*
* @see #AbstractXBeeDevice(IConnectionInterface)
* @see #AbstractXBeeDevice(String, int)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(String, int, int, int, int, int)
* @see com.digi.xbee.api.connection.serial.SerialPortParameters
*/
Expand All @@ -279,8 +278,8 @@ public AbstractXBeeDevice(String port, SerialPortParameters serialPortParameters
*
* @see #AbstractXBeeDevice(String, int)
* @see #AbstractXBeeDevice(String, SerialPortParameters)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(String, int, int, int, int, int)
* @see com.digi.xbee.api.connection.IConnectionInterface
*/
Expand Down Expand Up @@ -311,11 +310,11 @@ public AbstractXBeeDevice(IConnectionInterface connectionInterface) {
* @see #AbstractXBeeDevice(IConnectionInterface)
* @see #AbstractXBeeDevice(String, int)
* @see #AbstractXBeeDevice(String, SerialPortParameters)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
* @see #AbstractXBeeDevice(String, int, int, int, int, int)
* @see com.digi.xbee.api.models.XBee16BitAddress
*/
public AbstractXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
public AbstractXBeeDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
this(localXBeeDevice, addr64, null, null);
}

Expand All @@ -340,12 +339,12 @@ public AbstractXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
* @see #AbstractXBeeDevice(IConnectionInterface)
* @see #AbstractXBeeDevice(String, int)
* @see #AbstractXBeeDevice(String, SerialPortParameters)
* @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(AbstractXBeeDevice, XBee64BitAddress)
* @see #AbstractXBeeDevice(String, int, int, int, int, int)
* @see com.digi.xbee.api.models.XBee16BitAddress
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
public AbstractXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64,
public AbstractXBeeDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64,
XBee16BitAddress addr16, String id) {
if (localXBeeDevice == null)
throw new NullPointerException("Local XBee device cannot be null.");
Expand Down Expand Up @@ -388,7 +387,7 @@ public AbstractXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64,
*
* @since 1.2.1
*/
public AbstractXBeeDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
public AbstractXBeeDevice(AbstractXBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
this(localXBeeDevice, ipv6Addr, null);
}

Expand Down Expand Up @@ -416,7 +415,7 @@ public AbstractXBeeDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
*
* @since 1.2.1
*/
public AbstractXBeeDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr, String id) {
public AbstractXBeeDevice(AbstractXBeeDevice localXBeeDevice, Inet6Address ipv6Addr, String id) {
if (localXBeeDevice == null)
throw new NullPointerException("Local XBee device cannot be null.");
if (ipv6Addr == null)
Expand Down Expand Up @@ -3112,7 +3111,7 @@ protected void open() throws XBeeException {
logger.info(toString() + "Connection interface open.");

// Initialize the data reader.
dataReader = new DataReader(connectionInterface, operatingMode, (XBeeDevice)this);
dataReader = new DataReader(connectionInterface, operatingMode, this);
dataReader.start();

// Wait 10 milliseconds until the dataReader thread is started.
Expand All @@ -3132,7 +3131,7 @@ protected void open() throws XBeeException {
// Perform the Bluetooth authentication.
try {
logger.info(toString() + "Starting Bluetooth authentication...");
BluetoothAuthentication auth = new BluetoothAuthentication((XBeeDevice)this, bluetoothPassword);
BluetoothAuthentication auth = new BluetoothAuthentication(this, bluetoothPassword);
auth.authenticate();
((AbstractBluetoothInterface) connectionInterface).setEncryptionKeys(auth.getKey(), auth.getTxNonce(), auth.getRxNonce());
logger.info(toString() + "Authentication finished successfully.");
Expand Down Expand Up @@ -3313,7 +3312,7 @@ protected XBeeNetwork getNetwork() {
throw new InterfaceNotOpenException();

if (network == null)
network = new XBeeNetwork((XBeeDevice)this);
network = new XBeeNetwork(this);
return network;
}

Expand Down Expand Up @@ -4833,7 +4832,7 @@ private ExplicitXBeeMessage readExplicitDataPacket(RemoteXBeeDevice remoteXBeeDe
if (remoteXBeeDevice != null)
remoteDevice = remoteXBeeDevice;
else
remoteDevice = new RemoteXBeeDevice((XBeeDevice)this, explicitDataPacket.get64BitSourceAddress());
remoteDevice = new RemoteXBeeDevice(this, explicitDataPacket.get64BitSourceAddress());
getNetwork().addRemoteDevice(remoteDevice);
}
int sourceEndpoint = explicitDataPacket.getSourceEndpoint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BluetoothAuthentication {
private static final int LENGTH_NONCE = 12;

// Variables.
private final XBeeDevice device;
private final AbstractXBeeDevice device;
private final String password;

private final Object unlockLock = new Object();
Expand All @@ -82,10 +82,15 @@ class BluetoothAuthentication {
* Class constructor. Instantiates a new {@code BluetoothAuthentication}
* object with the given parameters.
*
* @throws IllegalArgumentException If {@code device.isRemote() == true}.
*
* @param device XBee device.
* @param password Bluetooth password.
*/
BluetoothAuthentication(XBeeDevice device, String password) {
BluetoothAuthentication(AbstractXBeeDevice device, String password) {
if (device.isRemote())
throw new IllegalArgumentException("The given local XBee device is remote.");

this.device = device;
this.password = password;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.digi.xbee.api.connection;
package com.digi.xbee.api;

import java.io.IOException;
import java.net.Inet6Address;
Expand All @@ -25,14 +25,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.digi.xbee.api.RemoteDigiMeshDevice;
import com.digi.xbee.api.RemoteDigiPointDevice;
import com.digi.xbee.api.RemoteRaw802Device;
import com.digi.xbee.api.RemoteThreadDevice;
import com.digi.xbee.api.RemoteXBeeDevice;
import com.digi.xbee.api.RemoteZigBeeDevice;
import com.digi.xbee.api.XBeeDevice;
import com.digi.xbee.api.XBeeNetwork;
import com.digi.xbee.api.connection.IConnectionInterface;
import com.digi.xbee.api.exceptions.XBeeException;
import com.digi.xbee.api.io.IOSample;
import com.digi.xbee.api.listeners.IExplicitDataReceiveListener;
Expand Down Expand Up @@ -117,7 +110,7 @@ public class DataReader extends Thread {

private XBeePacketsQueue xbeePacketsQueue;

private XBeeDevice xbeeDevice;
private AbstractXBeeDevice xbeeDevice;

/**
* Class constructor. Instantiates a new {@code DataReader} object for the
Expand All @@ -131,16 +124,19 @@ public class DataReader extends Thread {
*
* @throws NullPointerException if {@code connectionInterface == null} or
* {@code mode == null}.
* @throws IllegalArgumentException If {@code xbeeDevice.isRemote() == true}.
*
* @see IConnectionInterface
* @see com.digi.xbee.api.XBeeDevice
* @see com.digi.xbee.api.models.OperatingMode
*/
public DataReader(IConnectionInterface connectionInterface, OperatingMode mode, XBeeDevice xbeeDevice) {
public DataReader(IConnectionInterface connectionInterface, OperatingMode mode, AbstractXBeeDevice xbeeDevice) {
if (connectionInterface == null)
throw new NullPointerException("Connection interface cannot be null.");
if (mode == null)
throw new NullPointerException("Operating mode cannot be null.");
if (xbeeDevice.isRemote())
throw new IllegalArgumentException("The given local XBee device is remote.");

this.connectionInterface = connectionInterface;
this.mode = mode;
Expand Down Expand Up @@ -1405,7 +1401,7 @@ private void notifyUserDataRelayReceived(UserDataRelayMessage relayMessage) {
* @since 1.3.0
*/
private void notifyUserDataRelayReceived(final UserDataRelayMessage relayMessage, final boolean notifyGeneric) {
ArrayList listenerList = new ArrayList<>();
ArrayList<?> listenerList = new ArrayList<>();

// Get the list of listeners that should be notified depending on the parameters.
if (notifyGeneric) {
Expand Down
13 changes: 8 additions & 5 deletions library/src/main/java/com/digi/xbee/api/NodeDiscovery.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017, Digi International Inc.
/*
* Copyright 2017-2019, Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -59,7 +59,7 @@ class NodeDiscovery {
// Variables.
private static int globalFrameID = 1;

private XBeeDevice xbeeDevice;
private AbstractXBeeDevice xbeeDevice;

private List<RemoteXBeeDevice> deviceList;

Expand All @@ -76,12 +76,15 @@ class NodeDiscovery {
* @param xbeeDevice XBee Device to perform the discovery operation.
*
* @throws NullPointerException If {@code xbeeDevice == null}.
* @throws IllegalArgumentException If {@code xbeeDevice.isRemote() == true}.
*
* @see XBeeDevice
*/
public NodeDiscovery(XBeeDevice xbeeDevice) {
public NodeDiscovery(AbstractXBeeDevice xbeeDevice) {
if (xbeeDevice == null)
throw new NullPointerException("Local XBee device cannot be null.");
if (xbeeDevice.isRemote())
throw new IllegalArgumentException("The given local XBee device is remote.");

this.xbeeDevice = xbeeDevice;

Expand Down Expand Up @@ -453,7 +456,7 @@ private byte[] getRemoteDeviceData(XBeeAPIPacket packet) {
*
* @return Discovered XBee device.
*/
private RemoteXBeeDevice parseDiscoveryAPIData(byte[] data, XBeeDevice localDevice) {
private RemoteXBeeDevice parseDiscoveryAPIData(byte[] data, AbstractXBeeDevice localDevice) {
if (data == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017, Digi International Inc.
/*
* Copyright 2017-2019, Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -73,7 +73,7 @@ public RemoteDigiMeshDevice(DigiMeshDevice localXBeeDevice, XBee64BitAddress add
*
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
public RemoteDigiMeshDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
public RemoteDigiMeshDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
super(localXBeeDevice, addr64);

// Verify the local device has DigiMesh protocol.
Expand All @@ -100,7 +100,7 @@ public RemoteDigiMeshDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64)
*
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
public RemoteDigiMeshDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64, String id) {
public RemoteDigiMeshDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64, String id) {
super(localXBeeDevice, addr64, null, id);

// Verify the local device has DigiMesh protocol.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017, Digi International Inc.
/*
* Copyright 2017-2019, Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -75,7 +75,7 @@ public RemoteDigiPointDevice(DigiPointDevice localXBeeDevice, XBee64BitAddress a
*
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
public RemoteDigiPointDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
public RemoteDigiPointDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64) {
super(localXBeeDevice, addr64);

// Verify the local device has point-to-multipoint protocol.
Expand Down Expand Up @@ -103,7 +103,7 @@ public RemoteDigiPointDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64
*
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
public RemoteDigiPointDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64, String id) {
public RemoteDigiPointDevice(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64, String id) {
super(localXBeeDevice, addr64, null, id);

// Verify the local device has point-to-multipoint protocol.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017, Digi International Inc.
/*
* Copyright 2017-2019, Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -80,7 +80,7 @@ public RemoteRaw802Device(Raw802Device localXBeeDevice, XBee64BitAddress addr64)
* @see com.digi.xbee.api.models.XBee16BitAddress
* @see com.digi.xbee.api.models.XBee64BitAddress
*/
public RemoteRaw802Device(XBeeDevice localXBeeDevice, XBee64BitAddress addr64,
public RemoteRaw802Device(AbstractXBeeDevice localXBeeDevice, XBee64BitAddress addr64,
XBee16BitAddress addr16, String id) {
super(localXBeeDevice, addr64, addr16, id);

Expand Down Expand Up @@ -127,7 +127,7 @@ public RemoteRaw802Device(Raw802Device localXBeeDevice, XBee16BitAddress addr16)
*
* @see com.digi.xbee.api.models.XBee16BitAddress
*/
public RemoteRaw802Device(XBeeDevice localXBeeDevice, XBee16BitAddress addr16) {
public RemoteRaw802Device(AbstractXBeeDevice localXBeeDevice, XBee16BitAddress addr16) {
super(localXBeeDevice, XBee64BitAddress.UNKNOWN_ADDRESS);

// Verify the local device has 802.15.4 protocol.
Expand Down
16 changes: 8 additions & 8 deletions library/src/main/java/com/digi/xbee/api/RemoteThreadDevice.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017, Digi International Inc.
/*
* Copyright 2017-2019, Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -57,8 +57,8 @@ public class RemoteThreadDevice extends RemoteXBeeDevice {
* @throws NullPointerException if {@code localXBeeDevice == null} or
* if {@code ipv6addr == null}.
*
* @see #RemoteThreadDevice(XBeeDevice, Inet6Address)
* @see #RemoteThreadDevice(XBeeDevice, Inet6Address, String)
* @see #RemoteThreadDevice(AbstractXBeeDevice, Inet6Address)
* @see #RemoteThreadDevice(AbstractXBeeDevice, Inet6Address, String)
* @see com.digi.xbee.api.ThreadDevice
* @see java.net.Inet6Address
*/
Expand All @@ -82,11 +82,11 @@ public RemoteThreadDevice(ThreadDevice localXBeeDevice, Inet6Address ipv6Addr) {
* if {@code ipv6addr == null}.
*
* @see #RemoteThreadDevice(ThreadDevice, Inet6Address)
* @see #RemoteThreadDevice(XBeeDevice, Inet6Address, String)
* @see #RemoteThreadDevice(AbstractXBeeDevice, Inet6Address, String)
* @see com.digi.xbee.api.XBeeDevice
* @see java.net.Inet6Address
*/
public RemoteThreadDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
public RemoteThreadDevice(AbstractXBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
super(localXBeeDevice, ipv6Addr);

// Verify the local device has Thread protocol.
Expand All @@ -112,11 +112,11 @@ public RemoteThreadDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
* if {@code ipv6Address == null}.
*
* @see #RemoteThreadDevice(ThreadDevice, Inet6Address)
* @see #RemoteThreadDevice(XBeeDevice, Inet6Address)
* @see #RemoteThreadDevice(AbstractXBeeDevice, Inet6Address)
* @see com.digi.xbee.api.XBeeDevice
* @see java.net.Inet6Address
*/
public RemoteThreadDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr, String ni) {
public RemoteThreadDevice(AbstractXBeeDevice localXBeeDevice, Inet6Address ipv6Addr, String ni) {
super(localXBeeDevice, ipv6Addr, ni);

// Verify the local device has Thread protocol.
Expand Down
Loading

0 comments on commit c5f2270

Please sign in to comment.