From 4547330a0dec378347bb9f0407dc494f1fecc762 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Thu, 30 Apr 2015 17:18:57 +0200 Subject: [PATCH 01/14] Coverity fixes wave 1 --- .../xbee/api/receivedatapolling/MainApp.java | 8 +- .../receivedata/MyDataReceiveListener.java | 5 +- .../xbee/api/sendbroadcastdata/MainApp.java | 5 +- .../digi/xbee/api/senddataasync/MainApp.java | 5 +- .../com/digi/xbee/api/senddata/MainApp.java | 5 +- .../xbee/api/setgetparameters/MainApp.java | 5 +- .../com/digi/xbee/api/AbstractXBeeDevice.java | 7 +- .../java/com/digi/xbee/api/NodeDiscovery.java | 6 +- .../java/com/digi/xbee/api/XBeeDevice.java | 14 ++-- .../digi/xbee/api/connection/DataReader.java | 75 ++++++++++-------- .../api/connection/serial/SerialPortRxTx.java | 24 +++--- .../com/digi/xbee/api/models/ATCommand.java | 8 +- .../xbee/api/models/ATCommandResponse.java | 4 +- .../digi/xbee/api/models/ATCommandStatus.java | 4 +- .../xbee/api/models/ModemStatusEvent.java | 4 +- .../xbee/api/models/XBeeDiscoveryStatus.java | 4 +- .../com/digi/xbee/api/models/XBeeMessage.java | 3 +- .../xbee/api/models/XBeeTransmitStatus.java | 4 +- .../digi/xbee/api/packet/XBeeAPIPacket.java | 6 +- .../com/digi/xbee/api/packet/XBeePacket.java | 10 ++- .../api/packet/common/ATCommandPacket.java | 15 ++-- .../packet/common/ATCommandQueuePacket.java | 15 ++-- .../common/ATCommandResponsePacket.java | 17 ++-- .../common/IODataSampleRxIndicatorPacket.java | 4 + .../api/packet/common/ModemStatusPacket.java | 4 +- .../packet/common/RemoteATCommandPacket.java | 16 ++-- .../common/RemoteATCommandResponsePacket.java | 18 ++--- .../packet/common/TransmitStatusPacket.java | 8 +- .../xbee/api/packet/raw/RX16IOPacket.java | 4 + .../xbee/api/packet/raw/RX64IOPacket.java | 4 + .../xbee/api/packet/raw/TXStatusPacket.java | 4 +- .../com/digi/xbee/api/utils/ByteUtils.java | 36 +-------- .../com/digi/xbee/api/utils/HexUtils.java | 14 ++-- .../com/digi/xbee/api/utils/StringUtils.java | 79 +++++++++++++++++++ .../digi/xbee/api/XBeeDeviceReadDataTest.java | 17 ++-- ...BeeNetworkDiscoverDevicesListenerTest.java | 2 +- .../xbee/api/models/ModemStatusEventTest.java | 8 +- .../xbee/api/models/XBee64BitAddressTest.java | 7 +- .../xbee/api/models/XBeeProtocolTest.java | 13 ++- .../common/ATCommandResponsePacketTest.java | 22 +++--- .../packet/common/ModemStatusPacketTest.java | 8 +- .../RemoteATCommandResponsePacketTest.java | 22 +++--- .../common/TransmitStatusPacketTest.java | 22 +++--- .../api/packet/raw/TXStatusPacketTest.java | 10 +-- 44 files changed, 332 insertions(+), 243 deletions(-) create mode 100644 library/src/main/java/com/digi/xbee/api/utils/StringUtils.java diff --git a/examples/communication/ReceiveDataPollingSample/src/com/digi/xbee/api/receivedatapolling/MainApp.java b/examples/communication/ReceiveDataPollingSample/src/com/digi/xbee/api/receivedatapolling/MainApp.java index 603d7a3e..a31199ca 100644 --- a/examples/communication/ReceiveDataPollingSample/src/com/digi/xbee/api/receivedatapolling/MainApp.java +++ b/examples/communication/ReceiveDataPollingSample/src/com/digi/xbee/api/receivedatapolling/MainApp.java @@ -15,6 +15,7 @@ import com.digi.xbee.api.exceptions.XBeeException; import com.digi.xbee.api.models.XBeeMessage; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * XBee Java Library Receive Data polling sample application. @@ -54,10 +55,11 @@ public static void main(String[] args) { while (true) { XBeeMessage xbeeMessage = myDevice.readData(); - if (xbeeMessage != null) + if (xbeeMessage != null) { System.out.format("From %s >> %s | %s%n", xbeeMessage.getDevice().get64BitAddress(), - HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData())), - new String(xbeeMessage.getData())); + HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData())), + StringUtils.byteArrayToString(xbeeMessage.getData())); + } } } } diff --git a/examples/communication/ReceiveDataSample/src/com/digi/xbee/api/receivedata/MyDataReceiveListener.java b/examples/communication/ReceiveDataSample/src/com/digi/xbee/api/receivedata/MyDataReceiveListener.java index 85556de6..588d45f5 100644 --- a/examples/communication/ReceiveDataSample/src/com/digi/xbee/api/receivedata/MyDataReceiveListener.java +++ b/examples/communication/ReceiveDataSample/src/com/digi/xbee/api/receivedata/MyDataReceiveListener.java @@ -14,6 +14,7 @@ import com.digi.xbee.api.listeners.IDataReceiveListener; import com.digi.xbee.api.models.XBeeMessage; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * Class to manage the XBee received data that was sent by other modules in the @@ -34,7 +35,7 @@ public class MyDataReceiveListener implements IDataReceiveListener { @Override public void dataReceived(XBeeMessage xbeeMessage) { System.out.format("From %s >> %s | %s%n", xbeeMessage.getDevice().get64BitAddress(), - HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData())), - new String(xbeeMessage.getData())); + HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData())), + StringUtils.byteArrayToString(xbeeMessage.getData())); } } diff --git a/examples/communication/SendBroadcastDataSample/src/com/digi/xbee/api/sendbroadcastdata/MainApp.java b/examples/communication/SendBroadcastDataSample/src/com/digi/xbee/api/sendbroadcastdata/MainApp.java index 2cc8bac5..f6b0be9a 100644 --- a/examples/communication/SendBroadcastDataSample/src/com/digi/xbee/api/sendbroadcastdata/MainApp.java +++ b/examples/communication/SendBroadcastDataSample/src/com/digi/xbee/api/sendbroadcastdata/MainApp.java @@ -13,6 +13,7 @@ import com.digi.xbee.api.XBeeDevice; import com.digi.xbee.api.exceptions.XBeeException; +import com.digi.xbee.api.utils.StringUtils; /** * XBee Java Library Send Broadcast Data sample application. @@ -44,12 +45,12 @@ public static void main(String[] args) { System.out.println(" +------------------------------------------------+\n"); XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE); - byte[] dataToSend = DATA_TO_SEND.getBytes(); + byte[] dataToSend = StringUtils.stringToByteArray(DATA_TO_SEND); try { myDevice.open(); - System.out.format("Sending broadcast data: '%s'...%n", new String(dataToSend)); + System.out.format("Sending broadcast data: '%s'...%n", StringUtils.byteArrayToString(dataToSend)); myDevice.sendBroadcastData(dataToSend); diff --git a/examples/communication/SendDataAsyncSample/src/com/digi/xbee/api/senddataasync/MainApp.java b/examples/communication/SendDataAsyncSample/src/com/digi/xbee/api/senddataasync/MainApp.java index 9428f438..6abe5a29 100644 --- a/examples/communication/SendDataAsyncSample/src/com/digi/xbee/api/senddataasync/MainApp.java +++ b/examples/communication/SendDataAsyncSample/src/com/digi/xbee/api/senddataasync/MainApp.java @@ -16,6 +16,7 @@ import com.digi.xbee.api.XBeeNetwork; import com.digi.xbee.api.exceptions.XBeeException; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * XBee Java Library Send Data Asynchronously sample application. @@ -49,7 +50,7 @@ public static void main(String[] args) { System.out.println(" +---------------------------------------------------+\n"); XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE); - byte[] dataToSend = DATA_TO_SEND.getBytes(); + byte[] dataToSend = StringUtils.stringToByteArray(DATA_TO_SEND); try { myDevice.open(); @@ -64,7 +65,7 @@ public static void main(String[] args) { System.out.format("Sending data to %s >> %s | %s... ", remoteDevice.get64BitAddress(), HexUtils.prettyHexString(HexUtils.byteArrayToHexString(dataToSend)), - new String(dataToSend)); + StringUtils.byteArrayToString(dataToSend)); myDevice.sendDataAsync(remoteDevice, dataToSend); diff --git a/examples/communication/SendDataSample/src/com/digi/xbee/api/senddata/MainApp.java b/examples/communication/SendDataSample/src/com/digi/xbee/api/senddata/MainApp.java index b7d83f4f..dd8c67cf 100644 --- a/examples/communication/SendDataSample/src/com/digi/xbee/api/senddata/MainApp.java +++ b/examples/communication/SendDataSample/src/com/digi/xbee/api/senddata/MainApp.java @@ -16,6 +16,7 @@ import com.digi.xbee.api.XBeeNetwork; import com.digi.xbee.api.exceptions.XBeeException; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * XBee Java Library Send Data sample application. @@ -49,7 +50,7 @@ public static void main(String[] args) { System.out.println(" +--------------------------------------+\n"); XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE); - byte[] dataToSend = DATA_TO_SEND.getBytes(); + byte[] dataToSend = StringUtils.stringToByteArray(DATA_TO_SEND); try { myDevice.open(); @@ -64,7 +65,7 @@ public static void main(String[] args) { System.out.format("Sending data to %s >> %s | %s... ", remoteDevice.get64BitAddress(), HexUtils.prettyHexString(HexUtils.byteArrayToHexString(dataToSend)), - new String(dataToSend)); + StringUtils.byteArrayToString(dataToSend)); myDevice.sendData(remoteDevice, dataToSend); diff --git a/examples/configuration/SetAndGetParametersSample/src/com/digi/xbee/api/setgetparameters/MainApp.java b/examples/configuration/SetAndGetParametersSample/src/com/digi/xbee/api/setgetparameters/MainApp.java index e8d36de5..34797c8e 100644 --- a/examples/configuration/SetAndGetParametersSample/src/com/digi/xbee/api/setgetparameters/MainApp.java +++ b/examples/configuration/SetAndGetParametersSample/src/com/digi/xbee/api/setgetparameters/MainApp.java @@ -14,6 +14,7 @@ import com.digi.xbee.api.XBeeDevice; import com.digi.xbee.api.exceptions.XBeeException; import com.digi.xbee.api.utils.ByteUtils; +import com.digi.xbee.api.utils.StringUtils; /** * XBee Java Library Set and Get parameters sample application. @@ -62,7 +63,7 @@ public static void main(String[] args) { myDevice.open(); // Set parameters. - myDevice.setParameter(PARAM_NODE_ID, PARAM_VALUE_NODE_ID.getBytes()); + myDevice.setParameter(PARAM_NODE_ID, StringUtils.stringToByteArray(PARAM_VALUE_NODE_ID)); myDevice.setParameter(PARAM_PAN_ID, PARAM_VALUE_PAN_ID); myDevice.setParameter(PARAM_DEST_ADDRESS_H, ByteUtils.intToByteArray(PARAM_VALUE_DEST_ADDRESS_H)); myDevice.setParameter(PARAM_DEST_ADDRESS_L, ByteUtils.intToByteArray(PARAM_VALUE_DEST_ADDRESS_L)); @@ -74,7 +75,7 @@ public static void main(String[] args) { byte[] paramValueDL = myDevice.getParameter(PARAM_DEST_ADDRESS_L); // Compare the read parameter values with the values that were set. - if (!new String(paramValueNI).equals(PARAM_VALUE_NODE_ID)) { + if (!StringUtils.byteArrayToString(paramValueNI).equals(PARAM_VALUE_NODE_ID)) { System.out.println("NI parameter was not set correctly."); myDevice.close(); System.exit(1); diff --git a/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java b/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java index 9b63c576..4ceabaa0 100644 --- a/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java +++ b/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java @@ -65,6 +65,7 @@ import com.digi.xbee.api.packet.raw.TXStatusPacket; import com.digi.xbee.api.utils.ByteUtils; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * This class provides common functionality for all XBee devices. @@ -375,7 +376,7 @@ public void readDeviceInfo() throws TimeoutException, XBeeException { } // Get the Node ID. response = getParameter("NI"); - nodeID = new String(response); + nodeID = StringUtils.byteArrayToString(response); // Get the hardware version. if (hardwareVersion == null) { @@ -487,7 +488,7 @@ public void setNodeID(String nodeID) throws TimeoutException, XBeeException { if (nodeID.length() > 20) throw new IllegalArgumentException("Node ID length must be less than 21."); - setParameter("NI", nodeID.getBytes()); + setParameter("NI", StringUtils.stringToByteArray(nodeID)); this.nodeID = nodeID; } @@ -1769,7 +1770,7 @@ public IOSample readIOSample() throws TimeoutException, XBeeException { throw new InterfaceNotOpenException(); // Try to build an IO Sample from the sample payload. - byte[] samplePayload = null; + byte[] samplePayload; IOSample ioSample; // The response to the IS command in local 802.15.4 devices is empty, diff --git a/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java b/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java index 7d13e605..21760dc7 100644 --- a/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java +++ b/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java @@ -458,8 +458,6 @@ private RemoteXBeeDevice parseDiscoveryAPIData(byte[] data, XBeeDevice localDevi // TODO role of the device: coordinator, router, end device or unknown. //XBeeDeviceType role = XBeeDeviceType.UNKNOWN; int signalStrength = 0; - byte[] profileID = null; - byte[] manufacturerID = null; ByteArrayInputStream inputStream = new ByteArrayInputStream(data); // Read 16 bit address. @@ -485,9 +483,9 @@ private RemoteXBeeDevice parseDiscoveryAPIData(byte[] data, XBeeDevice localDevi // Consume status byte, it is not used yet. ByteUtils.readBytes(1, inputStream); // Read profile ID. - profileID = ByteUtils.readBytes(2, inputStream); + byte[] profileID = ByteUtils.readBytes(2, inputStream); // Read manufacturer ID. - manufacturerID = ByteUtils.readBytes(2, inputStream); + byte[] manufacturerID = ByteUtils.readBytes(2, inputStream); logger.debug("{}Discovered {} device: 16-bit[{}], 64-bit[{}], id[{}], parent[{}], profile[{}], manufacturer[{}].", xbeeDevice.toString(), localDevice.getXBeeProtocol().getDescription(), addr16, diff --git a/library/src/main/java/com/digi/xbee/api/XBeeDevice.java b/library/src/main/java/com/digi/xbee/api/XBeeDevice.java index b39df153..ad963f74 100644 --- a/library/src/main/java/com/digi/xbee/api/XBeeDevice.java +++ b/library/src/main/java/com/digi/xbee/api/XBeeDevice.java @@ -43,6 +43,7 @@ import com.digi.xbee.api.packet.raw.RX64Packet; import com.digi.xbee.api.packet.raw.TX64Packet; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * This class represents a local XBee device. @@ -402,9 +403,9 @@ private boolean enterATCommandMode() throws InvalidOperatingModeException, Timeo byte[] readData = new byte[256]; try { // Send the command mode sequence. - connectionInterface.writeData(COMMAND_MODE_CHAR.getBytes()); - connectionInterface.writeData(COMMAND_MODE_CHAR.getBytes()); - connectionInterface.writeData(COMMAND_MODE_CHAR.getBytes()); + connectionInterface.writeData(StringUtils.stringToByteArray(COMMAND_MODE_CHAR)); + connectionInterface.writeData(StringUtils.stringToByteArray(COMMAND_MODE_CHAR)); + connectionInterface.writeData(StringUtils.stringToByteArray(COMMAND_MODE_CHAR)); // Wait some time to let the module generate a response. Thread.sleep(TIMEOUT_ENTER_COMMAND_MODE); @@ -415,7 +416,7 @@ private boolean enterATCommandMode() throws InvalidOperatingModeException, Timeo throw new TimeoutException(); // Check if the read data is 'OK\r'. - String readString = new String(readData, 0, readBytes); + String readString = StringUtils.byteArrayToString(readData, 0, readBytes); if (!readString.contains(COMMAND_MODE_OK)) return false; @@ -933,7 +934,8 @@ private boolean waitForModemResetStatusPacket() { addModemStatusListener(resetStatusListener); synchronized (resetLock) { try { - resetLock.wait(TIMEOUT_RESET); + while (!modemStatusReceived) + resetLock.wait(TIMEOUT_RESET); } catch (InterruptedException e) { } } removeModemStatusListener(resetStatusListener); @@ -965,7 +967,7 @@ public void modemStatusEventReceived(ModemStatusEvent modemStatusEvent) { modemStatusReceived = true; // Continue execution by notifying the lock object. synchronized (resetLock) { - resetLock.notify(); + resetLock.notifyAll(); } } } diff --git a/library/src/main/java/com/digi/xbee/api/connection/DataReader.java b/library/src/main/java/com/digi/xbee/api/connection/DataReader.java index 6fa7683b..869329f8 100644 --- a/library/src/main/java/com/digi/xbee/api/connection/DataReader.java +++ b/library/src/main/java/com/digi/xbee/api/connection/DataReader.java @@ -321,13 +321,14 @@ public void run() { // Clear the list of read packets. xbeePacketsQueue.clearQueue(); try { - synchronized (connectionInterface) { - connectionInterface.wait(); - } while (running) { + synchronized (connectionInterface) { + while (connectionInterface.getInputStream() != null &&connectionInterface.getInputStream().available() == 0) + connectionInterface.wait(); + } if (!running) break; - if (connectionInterface.getInputStream() != null) { + if (connectionInterface.getInputStream() != null && connectionInterface.getInputStream().available() > 0) { switch (mode) { case AT: break; @@ -347,15 +348,9 @@ public void run() { default: break; } - } else if (connectionInterface.getInputStream() == null) - break; + } if (connectionInterface.getInputStream() == null) break; - else if (connectionInterface.getInputStream().available() > 0) - continue; - synchronized (connectionInterface) { - connectionInterface.wait(); - } } } catch (IOException e) { logger.error("Error reading from input stream.", e); @@ -484,38 +479,50 @@ public RemoteXBeeDevice getRemoteXBeeDeviceFromPacket(XBeeAPIPacket packet) thro switch(apiType) { case RECEIVE_PACKET: - ReceivePacket receivePacket = (ReceivePacket)apiPacket; - addr64 = receivePacket.get64bitSourceAddress(); - addr16 = receivePacket.get16bitSourceAddress(); - remoteDevice = network.getDevice(addr64); + if (apiPacket instanceof ReceivePacket) { + ReceivePacket receivePacket = (ReceivePacket)apiPacket; + addr64 = receivePacket.get64bitSourceAddress(); + addr16 = receivePacket.get16bitSourceAddress(); + remoteDevice = network.getDevice(addr64); + } break; case RX_64: - RX64Packet rx64Packet = (RX64Packet)apiPacket; - addr64 = rx64Packet.get64bitSourceAddress(); - remoteDevice = network.getDevice(addr64); + if (apiPacket instanceof RX64Packet) { + RX64Packet rx64Packet = (RX64Packet)apiPacket; + addr64 = rx64Packet.get64bitSourceAddress(); + remoteDevice = network.getDevice(addr64); + } break; case RX_16: - RX16Packet rx16Packet = (RX16Packet)apiPacket; - addr64 = XBee64BitAddress.UNKNOWN_ADDRESS; - addr16 = rx16Packet.get16bitSourceAddress(); - remoteDevice = network.getDevice(addr16); + if (apiPacket instanceof RX16Packet) { + RX16Packet rx16Packet = (RX16Packet)apiPacket; + addr64 = XBee64BitAddress.UNKNOWN_ADDRESS; + addr16 = rx16Packet.get16bitSourceAddress(); + remoteDevice = network.getDevice(addr16); + } break; case IO_DATA_SAMPLE_RX_INDICATOR: - IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket; - addr64 = ioSamplePacket.get64bitSourceAddress(); - addr16 = ioSamplePacket.get16bitSourceAddress(); - remoteDevice = network.getDevice(addr64); + if (apiPacket instanceof IODataSampleRxIndicatorPacket) { + IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket; + addr64 = ioSamplePacket.get64bitSourceAddress(); + addr16 = ioSamplePacket.get16bitSourceAddress(); + remoteDevice = network.getDevice(addr64); + } break; case RX_IO_64: - RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket; - addr64 = rx64IOPacket.get64bitSourceAddress(); - remoteDevice = network.getDevice(addr64); + if (apiPacket instanceof RX64IOPacket) { + RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket; + addr64 = rx64IOPacket.get64bitSourceAddress(); + remoteDevice = network.getDevice(addr64); + } break; case RX_IO_16: - RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket; - addr64 = XBee64BitAddress.UNKNOWN_ADDRESS; - addr16 = rx16IOPacket.get16bitSourceAddress(); - remoteDevice = network.getDevice(addr16); + if (apiPacket instanceof RX16IOPacket) { + RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket; + addr64 = XBee64BitAddress.UNKNOWN_ADDRESS; + addr16 = rx16IOPacket.get16bitSourceAddress(); + remoteDevice = network.getDevice(addr16); + } break; default: // Rest of the types are considered not to contain information @@ -770,7 +777,7 @@ public boolean isRunning() { public void stopReader() { running = false; synchronized (connectionInterface) { - connectionInterface.notify(); + connectionInterface.notifyAll(); } logger.debug(connectionInterface.toString() + "Data reader stopped."); } diff --git a/library/src/main/java/com/digi/xbee/api/connection/serial/SerialPortRxTx.java b/library/src/main/java/com/digi/xbee/api/connection/serial/SerialPortRxTx.java index 2ed7efe2..02316b7b 100644 --- a/library/src/main/java/com/digi/xbee/api/connection/serial/SerialPortRxTx.java +++ b/library/src/main/java/com/digi/xbee/api/connection/serial/SerialPortRxTx.java @@ -214,14 +214,12 @@ public void close() { } synchronized (lock) { if (serialPort != null) { - try { - serialPort.notifyOnDataAvailable(false); - serialPort.removeEventListener(); - portIdentifier.removePortOwnershipListener(this); - serialPort.close(); - serialPort = null; - connectionOpen = false; - } catch (Exception e) { } + serialPort.notifyOnDataAvailable(false); + serialPort.removeEventListener(); + portIdentifier.removePortOwnershipListener(this); + serialPort.close(); + serialPort = null; + connectionOpen = false; } } } @@ -242,7 +240,7 @@ public void serialEvent(SerialPortEvent event) { // Serial device has been disconnected. close(); synchronized (this) { - this.notify(); + this.notifyAll(); } break; } @@ -250,13 +248,15 @@ public void serialEvent(SerialPortEvent event) { try { if (getInputStream().available() > 0) { synchronized (this) { - this.notify(); + this.notifyAll(); } } } catch (Exception e) { logger.error(e.getMessage(), e); } break; + default: + break; } } @@ -389,6 +389,8 @@ public void ownershipChange(int nType) { case CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED: onSerialOwnershipRequested(null); break; + default: + break; } } @@ -405,7 +407,7 @@ private void onSerialOwnershipRequested(Object data) { StackTraceElement[] elems = e.getStackTrace(); String requester = elems[elems.length - 4].getClassName(); synchronized (this) { - this.notify(); + this.notifyAll(); } close(); String myPackage = this.getClass().getPackage().getName(); diff --git a/library/src/main/java/com/digi/xbee/api/models/ATCommand.java b/library/src/main/java/com/digi/xbee/api/models/ATCommand.java index 1e6316d1..b2809d15 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ATCommand.java +++ b/library/src/main/java/com/digi/xbee/api/models/ATCommand.java @@ -11,6 +11,8 @@ */ package com.digi.xbee.api.models; +import com.digi.xbee.api.utils.StringUtils; + /** * This class represents an AT command used to read or set different properties * of the XBee device. @@ -57,7 +59,7 @@ public ATCommand(String command) { * @throws NullPointerException if {@code command == null}. */ public ATCommand(String command, String parameter) { - this(command, parameter == null ? null : parameter.getBytes()); + this(command, parameter == null ? null : StringUtils.stringToByteArray(parameter)); } /** @@ -111,7 +113,7 @@ public byte[] getParameter() { public String getParameterString() { if (parameter == null) return null; - return new String(parameter); + return StringUtils.byteArrayToString(parameter); } /** @@ -120,7 +122,7 @@ public String getParameterString() { * @param parameter The AT command parameter as string. */ public void setParameter(String parameter) { - this.parameter = parameter.getBytes(); + this.parameter = StringUtils.stringToByteArray(parameter); } /** diff --git a/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java b/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java index c4a865a3..8d6417de 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java +++ b/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java @@ -11,6 +11,8 @@ */ package com.digi.xbee.api.models; +import com.digi.xbee.api.utils.StringUtils; + /** * This class represents the response of an AT Command sent by the connected * XBee device or by a remote device after executing an AT Command. @@ -133,7 +135,7 @@ public byte[] getResponse() { public String getResponseString() { if (response == null) return null; - return new String(response); + return StringUtils.byteArrayToString(response); } /** diff --git a/library/src/main/java/com/digi/xbee/api/models/ATCommandStatus.java b/library/src/main/java/com/digi/xbee/api/models/ATCommandStatus.java index 0ae9db24..fc3bd7d7 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ATCommandStatus.java +++ b/library/src/main/java/com/digi/xbee/api/models/ATCommandStatus.java @@ -39,7 +39,7 @@ public enum ATCommandStatus { static { for (ATCommandStatus at:values()) - lookupTable.put(at.getId(), at); + lookupTable.put(at.getID(), at); } /** @@ -59,7 +59,7 @@ public enum ATCommandStatus { * * @return The AT Command Status ID. */ - public int getId() { + public int getID() { return id; } diff --git a/library/src/main/java/com/digi/xbee/api/models/ModemStatusEvent.java b/library/src/main/java/com/digi/xbee/api/models/ModemStatusEvent.java index fd137c6d..b5ba0cd4 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ModemStatusEvent.java +++ b/library/src/main/java/com/digi/xbee/api/models/ModemStatusEvent.java @@ -54,7 +54,7 @@ public enum ModemStatusEvent { static { for (ModemStatusEvent at:values()) - lookupTable.put(at.getId(), at); + lookupTable.put(at.getID(), at); } /** @@ -74,7 +74,7 @@ public enum ModemStatusEvent { * * @return The modem status ID. */ - public int getId() { + public int getID() { return id; } diff --git a/library/src/main/java/com/digi/xbee/api/models/XBeeDiscoveryStatus.java b/library/src/main/java/com/digi/xbee/api/models/XBeeDiscoveryStatus.java index bb1f21d4..3eb5877f 100644 --- a/library/src/main/java/com/digi/xbee/api/models/XBeeDiscoveryStatus.java +++ b/library/src/main/java/com/digi/xbee/api/models/XBeeDiscoveryStatus.java @@ -39,7 +39,7 @@ public enum XBeeDiscoveryStatus { static { for (XBeeDiscoveryStatus at:values()) - lookupTable.put(at.getId(), at); + lookupTable.put(at.getID(), at); } /** @@ -59,7 +59,7 @@ private XBeeDiscoveryStatus(int id, String description) { * * @return The discovery status ID. */ - public int getId() { + public int getID() { return id; } diff --git a/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java b/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java index bca0388c..60a161c9 100644 --- a/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java +++ b/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java @@ -12,6 +12,7 @@ package com.digi.xbee.api.models; import com.digi.xbee.api.RemoteXBeeDevice; +import com.digi.xbee.api.utils.StringUtils; /** * This class represents an XBee message containing the remote XBee device the @@ -95,7 +96,7 @@ public byte[] getData() { * @return The data of the message in string format. */ public String getDataString() { - return new String(data); + return StringUtils.byteArrayToString(data); } /** diff --git a/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java b/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java index ffe74abe..091bf05c 100644 --- a/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java +++ b/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java @@ -52,7 +52,7 @@ public enum XBeeTransmitStatus { static { for (XBeeTransmitStatus ts:values()) - lookupTable.put(ts.getId(), ts); + lookupTable.put(ts.getID(), ts); } /** @@ -72,7 +72,7 @@ private XBeeTransmitStatus(int id, String description) { * * @return XBee transmit status ID. */ - public int getId() { + public int getID() { return id; } diff --git a/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java b/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java index 0dfb5693..1a5ed68e 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java @@ -119,9 +119,7 @@ public byte[] getPacketData() { data.write(frameTypeValue); byte[] apiData = getAPIData(); - if (apiData == null) - apiData = new byte[0]; - if (apiData != null && apiData.length > 0) { + if (apiData.length > 0) { try { data.write(apiData); } catch (IOException e) { @@ -149,7 +147,7 @@ public byte[] getAPIData() { if (needsAPIFrameID()) data.write(frameID); - if (apiData != null && apiData.length > 0) { + if (apiData.length > 0) { try { data.write(apiData); } catch (IOException e) { diff --git a/library/src/main/java/com/digi/xbee/api/packet/XBeePacket.java b/library/src/main/java/com/digi/xbee/api/packet/XBeePacket.java index 81d54d0e..7e6614f3 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/XBeePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/XBeePacket.java @@ -14,6 +14,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.LinkedHashMap; +import java.util.Map; import com.digi.xbee.api.exceptions.InvalidPacketException; import com.digi.xbee.api.models.SpecialByte; @@ -176,11 +177,12 @@ public String toString() { * @return Pretty String representing the packet. */ public String toPrettyString() { - String value = "Packet: " + toString() + "\n"; + StringBuilder value = new StringBuilder(); + value.append("Packet: " + toString() + "\n"); LinkedHashMap parameters = getParameters(); - for (String parameter:parameters.keySet()) - value = value + parameter + ": " + parameters.get(parameter) + "\n"; - return value; + for (Map.Entry parameter:parameters.entrySet()) + value.append(parameter.getKey() + ": " + parameter.getValue() + "\n"); + return value.toString(); } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java index 77e6c60c..21cee631 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java @@ -23,6 +23,7 @@ import com.digi.xbee.api.packet.XBeeAPIPacket; import com.digi.xbee.api.packet.APIFrameType; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * This class represents an AT Command XBee packet. Packet is built @@ -83,7 +84,7 @@ public static ATCommandPacket createPacket(byte[] payload) { index = index + 1; // 2 bytes of AT command, starting at 2nd byte. - String command = new String(new byte[]{payload[index], payload[index + 1]}); + String command = StringUtils.byteArrayToString(new byte[]{payload[index], payload[index + 1]}); index = index + 2; // Get data. @@ -108,7 +109,7 @@ public static ATCommandPacket createPacket(byte[] payload) { * @throws NullPointerException if {@code command == null}. */ public ATCommandPacket(int frameID, String command, String parameter) { - this(frameID, command, parameter == null ? null : parameter.getBytes()); + this(frameID, command, parameter == null ? null : StringUtils.stringToByteArray(parameter)); } /** @@ -145,7 +146,7 @@ public ATCommandPacket(int frameID, String command, byte[] parameter) { protected byte[] getAPIPacketSpecificData() { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { - os.write(command.getBytes()); + os.write(StringUtils.stringToByteArray(command)); if (parameter != null) os.write(parameter); } catch (IOException e) { @@ -181,7 +182,7 @@ public void setParameter(String parameter) { if (parameter == null) this.parameter = null; else - this.parameter = parameter.getBytes(); + this.parameter = StringUtils.stringToByteArray(parameter); } /** @@ -211,7 +212,7 @@ public byte[] getParameter() { public String getParameterAsString() { if (parameter == null) return null; - return new String(parameter); + return StringUtils.byteArrayToString(parameter); } /* @@ -230,10 +231,10 @@ public boolean isBroadcast() { @Override public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); - parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(command.getBytes())) + " (" + command + ")"); + parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(StringUtils.stringToByteArray(command))) + " (" + command + ")"); if (parameter != null) { if (ATStringCommands.get(command) != null) - parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter)) + " (" + new String(parameter) + ")"); + parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter)) + " (" + StringUtils.byteArrayToString(parameter) + ")"); else parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java index abc736a6..48646a22 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java @@ -23,6 +23,7 @@ import com.digi.xbee.api.packet.XBeeAPIPacket; import com.digi.xbee.api.packet.APIFrameType; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * This class represents an AT Command Queue XBee packet. Packet is built @@ -89,7 +90,7 @@ public static ATCommandQueuePacket createPacket(byte[] payload) { index = index + 1; // 2 bytes of AT command, starting at 2nd byte. - String command = new String(new byte[]{payload[index], payload[index + 1]}); + String command = StringUtils.byteArrayToString(new byte[]{payload[index], payload[index + 1]}); index = index + 2; // Get data. @@ -114,7 +115,7 @@ public static ATCommandQueuePacket createPacket(byte[] payload) { * @throws NullPointerException if {@code command == null}. */ public ATCommandQueuePacket(int frameID, String command, String parameter) { - this(frameID, command, parameter == null ? null : parameter.getBytes()); + this(frameID, command, parameter == null ? null : StringUtils.stringToByteArray(parameter)); } /** @@ -151,7 +152,7 @@ public ATCommandQueuePacket(int frameID, String command, byte[] parameter) { protected byte[] getAPIPacketSpecificData() { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { - os.write(command.getBytes()); + os.write(StringUtils.stringToByteArray(command)); if (parameter != null) os.write(parameter); } catch (IOException e) { @@ -187,7 +188,7 @@ public void setParameter(String parameter) { if (parameter == null) this.parameter = null; else - this.parameter = parameter.getBytes(); + this.parameter = StringUtils.stringToByteArray(parameter); } /** @@ -217,7 +218,7 @@ public byte[] getParameter() { public String getParameterAsString() { if (parameter == null) return null; - return new String(parameter); + return StringUtils.byteArrayToString(parameter); } /* @@ -236,10 +237,10 @@ public boolean isBroadcast() { @Override public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); - parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(command.getBytes())) + " (" + command + ")"); + parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(StringUtils.stringToByteArray(command))) + " (" + command + ")"); if (parameter != null) { if (ATStringCommands.get(command) != null) - parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter)) + " (" + new String(parameter) + ")"); + parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter)) + " (" + StringUtils.byteArrayToString(parameter) + ")"); else parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java index 581f5a3e..456ea39b 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java @@ -23,6 +23,7 @@ import com.digi.xbee.api.packet.XBeeAPIPacket; import com.digi.xbee.api.packet.APIFrameType; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; import com.digi.xbee.api.models.ATStringCommands; /** @@ -91,7 +92,7 @@ public static ATCommandResponsePacket createPacket(byte[] payload) { index = index + 1; // 2 bytes of AT command, starting at 2nd byte. - String command = new String(new byte[]{payload[index], payload[index + 1]}); + String command = StringUtils.byteArrayToString(new byte[]{payload[index], payload[index + 1]}); index = index + 2; // Status byte. @@ -148,8 +149,8 @@ public ATCommandResponsePacket(int frameID, ATCommandStatus status, String comma protected byte[] getAPIPacketSpecificData() { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { - os.write(command.getBytes()); - os.write(status.getId()); + os.write(StringUtils.stringToByteArray(command)); + os.write(status.getID()); if (commandValue != null) os.write(commandValue); } catch (IOException e) { @@ -196,7 +197,7 @@ public void setCommandValue(String commandValue) { if (commandValue == null) this.commandValue = null; else - this.commandValue = commandValue.getBytes(); + this.commandValue = StringUtils.stringToByteArray(commandValue); } /** @@ -226,7 +227,7 @@ public byte[] getCommandValue() { public String getCommandValueAsString() { if (commandValue == null) return null; - return new String(commandValue); + return StringUtils.byteArrayToString(commandValue); } /* @@ -245,11 +246,11 @@ public boolean isBroadcast() { @Override public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); - parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(command.getBytes())) + " (" + command + ")"); - parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(status.getId(), 1)) + " (" + status.getDescription() + ")"); + parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(StringUtils.stringToByteArray(command))) + " (" + command + ")"); + parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(status.getID(), 1)) + " (" + status.getDescription() + ")"); if (commandValue != null) { if (ATStringCommands.get(command) != null) - parameters.put("Response", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(commandValue)) + " (" + new String(commandValue) + ")"); + parameters.put("Response", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(commandValue)) + " (" + StringUtils.byteArrayToString(commandValue) + ")"); else parameters.put("Response", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(commandValue))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java index 968675f9..ac629642 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java @@ -267,10 +267,14 @@ public LinkedHashMap getAPIPacketParameters() { parameters.put("Digital channel mask", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getDigitalMask(), 2))); parameters.put("Analog channel mask", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getAnalogMask(), 1))); for (int i = 0; i < 16; i++) { + if (IOLine.getDIO(i) == null) + continue; if (ioSample.hasDigitalValue(IOLine.getDIO(i))) parameters.put(IOLine.getDIO(i).getName() + " digital value", ioSample.getDigitalValue(IOLine.getDIO(i)).getName()); } for (int i = 0; i < 6; i++) { + if (IOLine.getDIO(i) == null) + continue; if (ioSample.hasAnalogValue(IOLine.getDIO(i))) parameters.put(IOLine.getDIO(i).getName() + " analog value", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getAnalogValue(IOLine.getDIO(i)), 2))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ModemStatusPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ModemStatusPacket.java index 99510622..b4e7faf7 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ModemStatusPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ModemStatusPacket.java @@ -93,7 +93,7 @@ public ModemStatusPacket(ModemStatusEvent modemStatusEvent) { @Override public byte[] getAPIPacketSpecificData() { byte[] data = new byte[1]; - data[0] = (byte)(modemStatusEvent.getId() & 0xFF); + data[0] = (byte)(modemStatusEvent.getID() & 0xFF); return data; } @@ -131,7 +131,7 @@ public boolean isBroadcast() { @Override public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); - parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(modemStatusEvent.getId(), 1)) + " (" + modemStatusEvent.getDescription() + ")"); + parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(modemStatusEvent.getID(), 1)) + " (" + modemStatusEvent.getDescription() + ")"); return parameters; } } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java index cd335fac..cda5a03f 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java @@ -24,8 +24,8 @@ import com.digi.xbee.api.models.XBee64BitAddress; import com.digi.xbee.api.packet.APIFrameType; import com.digi.xbee.api.packet.XBeeAPIPacket; -import com.digi.xbee.api.utils.ByteUtils; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * This class represents a Remote AT Command Request packet. Packet is built @@ -111,7 +111,7 @@ public static RemoteATCommandPacket createPacket(byte[] payload) { index = index + 1; // 2 bytes of AT command. - String command = new String(new byte[]{payload[index], payload[index + 1]}); + String command = StringUtils.byteArrayToString(new byte[]{payload[index], payload[index + 1]}); index = index + 2; // Get data. @@ -167,7 +167,7 @@ public RemoteATCommandPacket(int frameID, XBee64BitAddress destAddress64, XBee16 this.transmitOptions = transmitOptions; this.command = command; if (parameter != null) - this.parameter = parameter.getBytes(); + this.parameter = StringUtils.stringToByteArray(parameter); this.logger = LoggerFactory.getLogger(RemoteATCommandPacket.class); } @@ -229,7 +229,7 @@ protected byte[] getAPIPacketSpecificData() { data.write(destAddress64.getValue()); data.write(destAddress16.getValue()); data.write(transmitOptions); - data.write(ByteUtils.stringToByteArray(command)); + data.write(StringUtils.stringToByteArray(command)); if (parameter != null) data.write(parameter); } catch (IOException e) { @@ -308,7 +308,7 @@ public void setParameter(String parameter) { if (parameter == null) this.parameter = null; else - this.parameter = parameter.getBytes(); + this.parameter = StringUtils.stringToByteArray(parameter); } /** @@ -338,7 +338,7 @@ public byte[] getParameter() { public String getParameterAsString() { if (parameter == null) return null; - return new String(parameter); + return StringUtils.byteArrayToString(parameter); } /* @@ -351,10 +351,10 @@ public LinkedHashMap getAPIPacketParameters() { parameters.put("64-bit dest. address", HexUtils.prettyHexString(destAddress64.toString())); parameters.put("16-bit dest. address", HexUtils.prettyHexString(destAddress16.toString())); parameters.put("Command options", HexUtils.prettyHexString(HexUtils.integerToHexString(transmitOptions, 1))); - parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(command.getBytes())) + " (" + command + ")"); + parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(StringUtils.stringToByteArray(command))) + " (" + command + ")"); if (parameter != null) { if (ATStringCommands.get(command) != null) - parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter)) + " (" + new String(parameter) + ")"); + parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter)) + " (" + StringUtils.byteArrayToString(parameter) + ")"); else parameters.put("Parameter", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(parameter))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java index fe9e4e67..0075cb87 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java @@ -25,8 +25,8 @@ import com.digi.xbee.api.models.XBee64BitAddress; import com.digi.xbee.api.packet.APIFrameType; import com.digi.xbee.api.packet.XBeeAPIPacket; -import com.digi.xbee.api.utils.ByteUtils; import com.digi.xbee.api.utils.HexUtils; +import com.digi.xbee.api.utils.StringUtils; /** * This class represents a Remote AT Command Response packet. Packet is built @@ -108,7 +108,7 @@ public static RemoteATCommandResponsePacket createPacket(byte[] payload) { index = index + 2; // 2 bytes of AT command. - String command = new String(new byte[]{payload[index], payload[index + 1]}); + String command = StringUtils.byteArrayToString(new byte[]{payload[index], payload[index + 1]}); index = index + 2; // Status byte. @@ -182,8 +182,8 @@ protected byte[] getAPIPacketSpecificData() { try { data.write(sourceAddress64.getValue()); data.write(sourceAddress16.getValue()); - data.write(ByteUtils.stringToByteArray(command)); - data.write(status.getId()); + data.write(StringUtils.stringToByteArray(command)); + data.write(status.getID()); if (commandValue != null) data.write(commandValue); } catch (IOException e) { @@ -252,7 +252,7 @@ public void setCommandValue(String commandValue) { if (commandValue == null) this.commandValue = null; else - this.commandValue = commandValue.getBytes(); + this.commandValue = StringUtils.stringToByteArray(commandValue); } /** @@ -282,7 +282,7 @@ public byte[] getCommandValue() { public String getCommandValueAsString() { if (commandValue == null) return null; - return new String(commandValue); + return StringUtils.byteArrayToString(commandValue); } /* @@ -303,11 +303,11 @@ public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); parameters.put("64-bit source address", HexUtils.prettyHexString(sourceAddress64.toString())); parameters.put("16-bit source address", HexUtils.prettyHexString(sourceAddress16.toString())); - parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(command.getBytes())) + " (" + command + ")"); - parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(status.getId(), 1)) + " (" + status.getDescription() + ")"); + parameters.put("AT Command", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(StringUtils.stringToByteArray(command))) + " (" + command + ")"); + parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(status.getID(), 1)) + " (" + status.getDescription() + ")"); if (commandValue != null) { if (ATStringCommands.get(command) != null) - parameters.put("Response", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(commandValue)) + " (" + new String(commandValue) + ")"); + parameters.put("Response", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(commandValue)) + " (" + StringUtils.byteArrayToString(commandValue) + ")"); else parameters.put("Response", HexUtils.prettyHexString(HexUtils.byteArrayToHexString(commandValue))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java index dbca508d..a9a8a4fd 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java @@ -163,8 +163,8 @@ protected byte[] getAPIPacketSpecificData() { try { data.write(destAddress16.getValue()); data.write(tranmistRetryCount); - data.write(transmitStatus.getId()); - data.write(discoveryStatus.getId()); + data.write(transmitStatus.getID()); + data.write(discoveryStatus.getID()); } catch (IOException e) { logger.error(e.getMessage(), e); } @@ -240,8 +240,8 @@ public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); parameters.put("16-bit dest. address", HexUtils.prettyHexString(destAddress16.toString())); parameters.put("Tx. retry count", HexUtils.prettyHexString(HexUtils.integerToHexString(tranmistRetryCount, 1)) + " (" + tranmistRetryCount + ")"); - parameters.put("Delivery status", HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getId(), 1)) + " (" + transmitStatus.getDescription() + ")"); - parameters.put("Discovery status", HexUtils.prettyHexString(HexUtils.integerToHexString(discoveryStatus.getId(), 1)) + " (" + discoveryStatus.getDescription() + ")"); + parameters.put("Delivery status", HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getID(), 1)) + " (" + transmitStatus.getDescription() + ")"); + parameters.put("Discovery status", HexUtils.prettyHexString(HexUtils.integerToHexString(discoveryStatus.getID(), 1)) + " (" + discoveryStatus.getDescription() + ")"); return parameters; } } diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java index 96f577cc..abdb287c 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java @@ -258,10 +258,14 @@ public LinkedHashMap getAPIPacketParameters() { parameters.put("Digital channel mask", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getDigitalMask(), 2))); parameters.put("Analog channel mask", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getAnalogMask(), 2))); for (int i = 0; i < 16; i++) { + if (IOLine.getDIO(i) == null) + continue; if (ioSample.hasDigitalValue(IOLine.getDIO(i))) parameters.put(IOLine.getDIO(i).getName() + " digital value", ioSample.getDigitalValue(IOLine.getDIO(i)).getName()); } for (int i = 0; i < 6; i++) { + if (IOLine.getDIO(i) == null) + continue; if (ioSample.hasAnalogValue(IOLine.getDIO(i))) parameters.put(IOLine.getDIO(i).getName() + " analog value", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getAnalogValue(IOLine.getDIO(i)), 2))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java index 2ef8f9b9..a7018b1c 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java @@ -258,10 +258,14 @@ public LinkedHashMap getAPIPacketParameters() { parameters.put("Digital channel mask", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getDigitalMask(), 2))); parameters.put("Analog channel mask", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getAnalogMask(), 2))); for (int i = 0; i < 16; i++) { + if (IOLine.getDIO(i) == null) + continue; if (ioSample.hasDigitalValue(IOLine.getDIO(i))) parameters.put(IOLine.getDIO(i).getName() + " digital value", ioSample.getDigitalValue(IOLine.getDIO(i)).getName()); } for (int i = 0; i < 6; i++) { + if (IOLine.getDIO(i) == null) + continue; if (ioSample.hasAnalogValue(IOLine.getDIO(i))) parameters.put(IOLine.getDIO(i).getName() + " analog value", HexUtils.prettyHexString(HexUtils.integerToHexString(ioSample.getAnalogValue(IOLine.getDIO(i)), 2))); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java index 3c4d10d4..c464569e 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java @@ -109,7 +109,7 @@ public TXStatusPacket(int frameID, XBeeTransmitStatus transmitStatus) { */ @Override protected byte[] getAPIPacketSpecificData() { - return new byte[] {(byte)transmitStatus.getId()}; + return new byte[] {(byte)transmitStatus.getID()}; } /* @@ -148,7 +148,7 @@ public boolean isBroadcast() { @Override public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); - parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getId(), 1)) + " (" + transmitStatus.getDescription() + ")"); + parameters.put("Status", HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getID(), 1)) + " (" + transmitStatus.getDescription() + ")"); return parameters; } } diff --git a/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java b/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java index 24a15389..9668d06c 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java @@ -212,40 +212,6 @@ public static short byteArrayToShort(byte[] byteArray) { | byteArray[1] & 0x00FF); } - /** - * Converts the given string into a byte array. - * - * @param value String to convert to byte array. - * - * @return Byte array of the given string. - * - * @throws NullPointerException if {@code value == null}. - * - * @see #byteArrayToString(byte[]) - */ - public static byte[] stringToByteArray(String value) { - if (value == null) - throw new NullPointerException("Value cannot be null."); - - return value.getBytes(); - } - - /** - * Converts the given byte array into a string. - * - * @param value Byte array to convert to string. - * - * @return Converted String. - * - * @throws NullPointerException if {@code value == null}. - */ - public static String byteArrayToString(byte[] value) { - if (value == null) - throw new NullPointerException("Byte array cannot be null."); - - return new String(value); - } - /** * Converts the given byte into an integer. * @@ -269,7 +235,7 @@ public static int byteToInt(byte b) { * in the {@code containerInteger}, {@code false} otherwise. */ public static boolean isBitEnabled(int containerInteger, int bitPosition) { - return (((containerInteger & 0xFFFFFFFF) >> bitPosition) & 0x01) == 0x01; + return ((containerInteger >> bitPosition) & 0x01) == 0x01; } /** diff --git a/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java b/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java index ed39fedc..254d83d6 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java @@ -103,7 +103,7 @@ public static boolean containsLetters(String parameter) { if (parameter == null) throw new NullPointerException("Parameter cannot be null."); - byte[] byteArray = parameter.getBytes(); + byte[] byteArray = StringUtils.stringToByteArray(parameter); for (int i = 0; i < byteArray.length; i++){ if (!((byteArray[i] >= '0') && (byteArray[i] <= '9'))) return true; @@ -121,15 +121,15 @@ public static boolean containsLetters(String parameter) { */ public static String integerToHexString(int value, int minBytes) { byte[] intAsByteArray = ByteUtils.intToByteArray(value); - String intAsHexString = ""; + StringBuilder intAsHexString = new StringBuilder(); boolean numberFound = false; for (int i = 0; i < intAsByteArray.length; i++) { if (intAsByteArray[i] == 0x00 && !numberFound && intAsByteArray.length - i > minBytes) continue; - intAsHexString += HexUtils.byteArrayToHexString(new byte[] {(byte)(intAsByteArray[i] & 0xFF)}); + intAsHexString.append(HexUtils.byteArrayToHexString(new byte[] {(byte)(intAsByteArray[i] & 0xFF)})); numberFound = true; } - return intAsHexString; + return intAsHexString.toString(); } /** @@ -148,13 +148,13 @@ public static String prettyHexString(String hexString) { if (hexString == null) throw new NullPointerException("Hexadecimal string cannot be null."); - String prettyHexString = ""; + StringBuilder prettyHexString = new StringBuilder(); if (hexString.length() % 2 != 0) hexString = "0" + hexString; int iterations = hexString.length() / 2; for (int i = 0; i < iterations; i++) - prettyHexString += hexString.substring(2 * i, 2 * i + 2) + " "; - return prettyHexString.trim(); + prettyHexString.append(hexString.substring(2 * i, 2 * i + 2) + " "); + return prettyHexString.toString().trim(); } /** diff --git a/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java b/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java new file mode 100644 index 00000000..81735797 --- /dev/null +++ b/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java @@ -0,0 +1,79 @@ +package com.digi.xbee.api.utils; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; + +public class StringUtils { + + // Constants. + private final static String UTF8_CHARSET = "UTF-8"; + + /** + * Transforms the given string to the corresponding byte array. + * + * @param source The {@code String} to transform to a byte array. + * + * @return The given string as a byte array. + * + * @throws NullPointerException if {@code source == null}. + * + * @see #byteArrayToString(byte[]) + * @see #byteArrayToString(byte[], int, int) + */ + public static byte[] stringToByteArray(String source) { + if (source == null) + throw new NullPointerException("Value cannot be null."); + + byte[] byteArray; + try { + byteArray = source.getBytes(UTF8_CHARSET); + } catch (UnsupportedEncodingException e) { + byteArray = source.getBytes(Charset.defaultCharset()); + } + return byteArray; + } + + /** + * Transforms the given byte array to its corresponding string. + * + * @param byteArray The byte array to transform to string. + * + * @return The given byte array as string. + * + * @throws NullPointerException if {@code byteArray == null}. + * + * @see #stringToByteArray(String) + * @see #byteArrayToString(byte[], int, int) + */ + public static String byteArrayToString(byte[] byteArray) { + return byteArrayToString(byteArray, 0, byteArray.length); + } + + /** + * Transforms the given byte array to its corresponding string using the + * given parameters. + * + * @param byteArray The byte array to transform to string. + * @param offset Offset in the array to start reading bytes. + * @param numBytes Number of bytes to read from the array. + * + * @return The given byte array as string. + * + * @throws NullPointerException if {@code byteArray == null}. + * + * @see #byteArrayToString(byte[]) + * @see #stringToByteArray(String) + */ + public static String byteArrayToString(byte[] byteArray, int offset, int numBytes) { + if (byteArray == null) + throw new NullPointerException("Value cannot be null."); + + String value; + try { + value = new String(byteArray, offset, numBytes, UTF8_CHARSET); + } catch (UnsupportedEncodingException e) { + value = new String(byteArray, offset, numBytes, Charset.defaultCharset()); + } + return value; + } +} diff --git a/library/src/test/java/com/digi/xbee/api/XBeeDeviceReadDataTest.java b/library/src/test/java/com/digi/xbee/api/XBeeDeviceReadDataTest.java index 6ff58d72..bdce9d8c 100644 --- a/library/src/test/java/com/digi/xbee/api/XBeeDeviceReadDataTest.java +++ b/library/src/test/java/com/digi/xbee/api/XBeeDeviceReadDataTest.java @@ -5,6 +5,8 @@ import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertThat; +import java.io.ByteArrayInputStream; + import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -45,6 +47,8 @@ public class XBeeDeviceReadDataTest { private SerialPortRxTx mockConnectionInterface; + private ByteArrayInputStream dummyInputStream; + //private DataReader mockDataReader; private XBeePacketsQueue mockXBeePacketsQueue; @@ -59,23 +63,26 @@ public void setUp() throws Exception { addr64 = new XBee64BitAddress("0013A20040A820DB"); addr16 = new XBee16BitAddress("9634"); receivedData = "Received data"; + dummyInputStream = new ByteArrayInputStream(new byte[8]); mockConnectionInterface = Mockito.mock(SerialPortRxTx.class); Mockito.doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Exception { - Mockito.when(mockConnectionInterface.isOpen()).thenReturn(true); + Mockito.doReturn(true).when(mockConnectionInterface).isOpen(); return null; } }).when(mockConnectionInterface).open(); Mockito.doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Exception { - Mockito.when(mockConnectionInterface.isOpen()).thenReturn(false); + Mockito.doReturn(false).when(mockConnectionInterface).isOpen(); return null; } }).when(mockConnectionInterface).close(); + Mockito.doReturn(dummyInputStream).when(mockConnectionInterface).getInputStream(); + mockXBeePacketsQueue = Mockito.mock(XBeePacketsQueue.class); Mockito.doAnswer(new Answer() { @@ -114,7 +121,7 @@ public void tearDown() throws Exception { @Test public final void testReadDataInterfaceNotOpenException() { // Setup the resources for the test. - Mockito.when(xbeeDevice.isOpen()).thenReturn(false); + Mockito.doReturn(false).when(xbeeDevice).isOpen(); exception.expect(InterfaceNotOpenException.class); exception.expectMessage(is(equalTo("The connection interface is not open."))); @@ -131,7 +138,7 @@ public final void testReadDataInterfaceNotOpenException() { @Test public final void testReadData() throws Exception { // Setup the resources for the test. - + assertThat("Network should be empty", xbeeDevice.getNetwork().getNumberOfDevices(), is(equalTo(0))); // Call the method under test. @@ -321,7 +328,7 @@ public final void testReadDataFromTimeout() throws Exception { @Test public final void testReadDataPacketInterfaceNotOpenException() throws Exception { // Setup the resources for the test. - Mockito.when(xbeeDevice.isOpen()).thenReturn(false); + Mockito.doReturn(false).when(xbeeDevice).isOpen(); exception.expect(InterfaceNotOpenException.class); exception.expectMessage(is(equalTo("The connection interface is not open."))); diff --git a/library/src/test/java/com/digi/xbee/api/XBeeNetworkDiscoverDevicesListenerTest.java b/library/src/test/java/com/digi/xbee/api/XBeeNetworkDiscoverDevicesListenerTest.java index 160067e9..d5f55df5 100644 --- a/library/src/test/java/com/digi/xbee/api/XBeeNetworkDiscoverDevicesListenerTest.java +++ b/library/src/test/java/com/digi/xbee/api/XBeeNetworkDiscoverDevicesListenerTest.java @@ -35,7 +35,7 @@ public class XBeeNetworkDiscoverDevicesListenerTest { @Rule - public ExpectedException exception = ExpectedException.none(); + private ExpectedException exception = ExpectedException.none(); // Variables. private XBeeNetwork network; diff --git a/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java b/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java index b6f9cb17..fd2c50be 100644 --- a/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java @@ -31,14 +31,14 @@ public void setup() { } /** - * Test method for {@link com.digi.xbee.api.models.ModemStatusEvent#getId()}. + * Test method for {@link com.digi.xbee.api.models.ModemStatusEvent#getID()}. * *

Verify that the ID of each ModemStatusEvent entry is valid.

*/ @Test public void testModemStatusEventValues() { for (ModemStatusEvent modemStatusEvent:modemStatusEventValues) - assertTrue(modemStatusEvent.getId() >= 0); + assertTrue(modemStatusEvent.getID() >= 0); } /** @@ -76,7 +76,7 @@ public void testModemStatusEventDescriptions() { @Test public void testModemStatusEventStaticAccess() { for (ModemStatusEvent modemStatusEvent:modemStatusEventValues) - assertEquals(modemStatusEvent, ModemStatusEvent.get(modemStatusEvent.getId())); + assertEquals(modemStatusEvent, ModemStatusEvent.get(modemStatusEvent.getID())); } /** @@ -88,6 +88,6 @@ public void testModemStatusEventStaticAccess() { @Test public void testModemStatusEventToString() { for (ModemStatusEvent modemStatusEvent:modemStatusEventValues) - assertEquals(HexUtils.byteToHexString((byte)modemStatusEvent.getId()) + ": " + modemStatusEvent.getDescription(), modemStatusEvent.toString()); + assertEquals(HexUtils.byteToHexString((byte)modemStatusEvent.getID()) + ": " + modemStatusEvent.getDescription(), modemStatusEvent.toString()); } } diff --git a/library/src/test/java/com/digi/xbee/api/models/XBee64BitAddressTest.java b/library/src/test/java/com/digi/xbee/api/models/XBee64BitAddressTest.java index a1fe4668..9240919b 100644 --- a/library/src/test/java/com/digi/xbee/api/models/XBee64BitAddressTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/XBee64BitAddressTest.java @@ -88,13 +88,8 @@ public void testCreateWithInvalidParameters() { */ public void testCreateWithValidByteNumbers() { // Test with valid byte number values. - XBee64BitAddress address = null; - try { - address = new XBee64BitAddress(VALID_BYTE_ARRAY[0] & 0xFF, VALID_BYTE_ARRAY[1] & 0xFF, VALID_BYTE_ARRAY[2] & 0xFF, VALID_BYTE_ARRAY[3] & 0xFF, + XBee64BitAddress address = new XBee64BitAddress(VALID_BYTE_ARRAY[0] & 0xFF, VALID_BYTE_ARRAY[1] & 0xFF, VALID_BYTE_ARRAY[2] & 0xFF, VALID_BYTE_ARRAY[3] & 0xFF, (int)VALID_BYTE_ARRAY[4] & 0xFF, VALID_BYTE_ARRAY[5] & 0xFF, VALID_BYTE_ARRAY[6] & 0xFF, VALID_BYTE_ARRAY[7] & 0xFF); - } catch (Exception e) { - fail("This exception should have not been thrown."); - } assertArrayEquals(VALID_BYTE_ARRAY, address.getValue()); assertEquals(VALID_STRING_NO_PREFIX, address.toString()); diff --git a/library/src/test/java/com/digi/xbee/api/models/XBeeProtocolTest.java b/library/src/test/java/com/digi/xbee/api/models/XBeeProtocolTest.java index 22f593fa..8ef187f4 100644 --- a/library/src/test/java/com/digi/xbee/api/models/XBeeProtocolTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/XBeeProtocolTest.java @@ -122,8 +122,9 @@ public void testDetermineProtocol() throws FileNotFoundException { // Generate the list of firmware entries from the firmware_entries_xctu.txt file. File firmwareEntriesFile = new File(getClass().getResource(FILE_FIRMWARE_ENTRIES).getFile()); - BufferedReader reader = new BufferedReader(new FileReader(firmwareEntriesFile.getAbsolutePath())); + BufferedReader reader = null; try { + reader = new BufferedReader(new FileReader(firmwareEntriesFile.getAbsolutePath())); String line = reader.readLine(); while (line != null) { // Skip empty lines. @@ -151,9 +152,13 @@ public void testDetermineProtocol() throws FileNotFoundException { line = reader.readLine(); } } catch (IOException e) { - try { - reader.close(); - } catch (IOException e1) { } + e.printStackTrace(); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { } + } } // Verify that the determineProtocol method is able to determine the protocol of all the firmware entries of the list. diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/ATCommandResponsePacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/ATCommandResponsePacketTest.java index 64304d2d..3c2fb3b9 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/ATCommandResponsePacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/ATCommandResponsePacketTest.java @@ -113,7 +113,7 @@ public final void testCreatePacketPayloadShorterThanNeeded() { int frameType = APIFrameType.AT_COMMAND_RESPONSE.getValue(); //int frameID = 0xE7; byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] payload = new byte[2 + atCommand.length]; payload[0] = (byte)frameType; @@ -139,7 +139,7 @@ public final void testCreatePacketPayloadNotIncludingFrameType() { // Setup the resources for the test. int frameID = 0xE7; byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] data = new byte[]{0x68, 0x6F, 0x6C, 0x61}; byte[] payload = new byte[2 + atCommand.length + data.length]; @@ -167,7 +167,7 @@ public final void testCreatePacketValidPayloadWithoutData() { int frameType = APIFrameType.AT_COMMAND_RESPONSE.getValue(); int frameID = 0xE7; byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] payload = new byte[3 + atCommand.length]; payload[0] = (byte)frameType; @@ -182,7 +182,7 @@ public final void testCreatePacketValidPayloadWithoutData() { assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); assertThat("Returned AT Command is not the expected one", packet.getCommand(), is(equalTo(new String(atCommand)))); - assertThat("Returned status is not the expected one", packet.getStatus().getId(), is(equalTo(status))); + assertThat("Returned status is not the expected one", packet.getStatus().getID(), is(equalTo(status))); assertThat("Returned Command value is not the expected one", packet.getCommandValue(), is(nullValue())); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); @@ -200,7 +200,7 @@ public final void testCreatePacketValidPayloadWithData() { int frameType = APIFrameType.AT_COMMAND_RESPONSE.getValue(); int frameID = 0xE7; byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] value = new byte[]{0x68, 0x6F, 0x6C, 0x61}; byte[] payload = new byte[3 + atCommand.length + value.length]; @@ -217,7 +217,7 @@ public final void testCreatePacketValidPayloadWithData() { assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); assertThat("Returned AT Command is not the expected one", packet.getCommand(), is(equalTo(new String(atCommand)))); - assertThat("Returned status is not the expected one", packet.getStatus().getId(), is(equalTo(status))); + assertThat("Returned status is not the expected one", packet.getStatus().getID(), is(equalTo(status))); assertThat("Returned Command value is not the expected one", packet.getCommandValue(), is(equalTo(value))); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); @@ -379,7 +379,7 @@ public final void testGetAPIDataATCommandParameterNull() { byte[] expectedData = new byte[expectedLength]; expectedData[0] = (byte)frameID; System.arraycopy(command.getBytes(), 0, expectedData, 1, command.length()); - expectedData[1 + command.length()] = (byte)status.getId(); + expectedData[1 + command.length()] = (byte)status.getID(); // Call the method under test. byte[] data = packet.getAPIData(); @@ -406,7 +406,7 @@ public final void testGetAPIDataATCommandParameterNotNull() { byte[] expectedData = new byte[expectedLength]; expectedData[0] = (byte)frameID; System.arraycopy(command.getBytes(), 0, expectedData, 1, command.length()); - expectedData[1 + command.length()] = (byte)status.getId(); + expectedData[1 + command.length()] = (byte)status.getID(); System.arraycopy(parameter, 0, expectedData, 2 + command.length(), parameter.length); // Call the method under test. @@ -431,7 +431,7 @@ public final void testGetAPIPacketParametersATCommandParameterNull() { ATCommandResponsePacket packet = new ATCommandResponsePacket(frameID, status, command, parameter); String expectedATCommand = HexUtils.prettyHexString(command.getBytes()) + " (" + command + ")"; - String expectedStatus = Integer.toHexString(status.getId()).toUpperCase() + " (" + status.getDescription() + ")"; + String expectedStatus = Integer.toHexString(status.getID()).toUpperCase() + " (" + status.getDescription() + ")"; // Call the method under test. LinkedHashMap packetParams = packet.getAPIPacketParameters(); @@ -458,7 +458,7 @@ public final void testGetAPIPacketParametersATCommandParameterNotNull() { ATCommandResponsePacket packet = new ATCommandResponsePacket(frameID, status, command, parameter); String expectedATCommand = HexUtils.prettyHexString(command.getBytes()) + " (" + command + ")"; - String expectedStatus = Integer.toHexString(status.getId()).toUpperCase() + " (" + status.getDescription() + ")"; + String expectedStatus = Integer.toHexString(status.getID()).toUpperCase() + " (" + status.getDescription() + ")"; String expectedATParameter = HexUtils.prettyHexString(parameter) + " (" + new String(parameter) + ")"; // Call the method under test. @@ -486,7 +486,7 @@ public final void testGetAPIPacketParametersATCommandParameterNotNullNonStringCm ATCommandResponsePacket packet = new ATCommandResponsePacket(frameID, status, command, parameter); String expectedATCommand = HexUtils.prettyHexString(command.getBytes()) + " (" + command + ")"; - String expectedStatus = Integer.toHexString(status.getId()).toUpperCase() + " (" + status.getDescription() + ")"; + String expectedStatus = Integer.toHexString(status.getID()).toUpperCase() + " (" + status.getDescription() + ")"; String expectedATParameter = HexUtils.prettyHexString(parameter); // Call the method under test. diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/ModemStatusPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/ModemStatusPacketTest.java index ea03a9df..1dda6b00 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/ModemStatusPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/ModemStatusPacketTest.java @@ -136,7 +136,7 @@ public final void testCreatePacketPayloadInvalidFrameType() { byte[] payload = new byte[2]; payload[0] = (byte)frameType; - payload[1] = (byte)modemStatusEvent.getId(); + payload[1] = (byte)modemStatusEvent.getID(); exception.expect(IllegalArgumentException.class); exception.expectMessage(is(equalTo("Payload is not a Modem Status packet."))); @@ -158,7 +158,7 @@ public final void testCreatePacketValidPayload() { byte[] payload = new byte[2]; payload[0] = (byte)frameType; - payload[1] = (byte)modemStatusEvent.getId(); + payload[1] = (byte)modemStatusEvent.getID(); // Call the method under test. ModemStatusPacket packet = ModemStatusPacket.createPacket(payload); @@ -220,7 +220,7 @@ public final void testGetAPIData() { int expectedLength = 1 /* Modem status */; byte[] expectedData = new byte[expectedLength]; - expectedData[0] = (byte)modemStatusEvent.getId(); + expectedData[0] = (byte)modemStatusEvent.getID(); // Call the method under test. byte[] data = packet.getAPIData(); @@ -240,7 +240,7 @@ public final void testGetAPIPacketParameters() { ModemStatusEvent modemStatusEvent = ModemStatusEvent.STATUS_JOINED_NETWORK; ModemStatusPacket packet = new ModemStatusPacket(modemStatusEvent); - String expectedModemStatus = HexUtils.integerToHexString(modemStatusEvent.getId(), 1) + " (" + modemStatusEvent.getDescription() + ")"; + String expectedModemStatus = HexUtils.integerToHexString(modemStatusEvent.getID(), 1) + " (" + modemStatusEvent.getDescription() + ")"; // Call the method under test. LinkedHashMap packetParams = packet.getAPIPacketParameters(); diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacketTest.java index 4497183b..f3076915 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacketTest.java @@ -117,7 +117,7 @@ public final void testCreatePacketPayloadShorterThanNeeded() { XBee64BitAddress source64Addr = new XBee64BitAddress("0013A2004032D9AB"); XBee16BitAddress source16Addr = new XBee16BitAddress("B45C"); byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] payload = new byte[12 + atCommand.length]; payload[0] = (byte)frameType; @@ -147,7 +147,7 @@ public final void testCreatePacketPayloadNotIncludingFrameType() { XBee64BitAddress source64Addr = new XBee64BitAddress("0013A2004032D9AB"); XBee16BitAddress source16Addr = new XBee16BitAddress("B45C"); byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] data = new byte[]{0x68, 0x6F, 0x6C, 0x61}; byte[] payload = new byte[12 + atCommand.length + data.length]; @@ -179,7 +179,7 @@ public final void testCreatePacketValidPayloadWithoutData() { XBee64BitAddress source64Addr = new XBee64BitAddress("0013A2004032D9AB"); XBee16BitAddress source16Addr = new XBee16BitAddress("B45C"); byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] payload = new byte[13 + atCommand.length]; payload[0] = (byte)frameType; @@ -198,7 +198,7 @@ public final void testCreatePacketValidPayloadWithoutData() { assertThat("Returned source 64-bit address is not the expected one", packet.get64bitSourceAddress(), is(equalTo(source64Addr))); assertThat("Returned source 16-bit address is not the expected one", packet.get16bitSourceAddress(), is(equalTo(source16Addr))); assertThat("Returned AT Command is not the expected one", packet.getCommand(), is(equalTo(new String(atCommand)))); - assertThat("Returned status is not the expected one", packet.getStatus().getId(), is(equalTo(status))); + assertThat("Returned status is not the expected one", packet.getStatus().getID(), is(equalTo(status))); assertThat("Returned value is not the expected one", packet.getCommandValue(), is(nullValue())); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); @@ -218,7 +218,7 @@ public final void testCreatePacketValidPayloadWithData() { XBee64BitAddress source64Addr = new XBee64BitAddress("0013A2004032D9AB"); XBee16BitAddress source16Addr = new XBee16BitAddress("B45C"); byte[] atCommand = "NI".getBytes(); - int status = ATCommandStatus.OK.getId(); + int status = ATCommandStatus.OK.getID(); byte[] value = new byte[]{0x68, 0x6F, 0x6C, 0x61}; byte[] payload = new byte[13 + atCommand.length + value.length]; @@ -239,7 +239,7 @@ public final void testCreatePacketValidPayloadWithData() { assertThat("Returned source 64-bit address is not the expected one", packet.get64bitSourceAddress(), is(equalTo(source64Addr))); assertThat("Returned source 16-bit address is not the expected one", packet.get16bitSourceAddress(), is(equalTo(source16Addr))); assertThat("Returned AT Command is not the expected one", packet.getCommand(), is(equalTo(new String(atCommand)))); - assertThat("Returned status is not the expected one", packet.getStatus().getId(), is(equalTo(status))); + assertThat("Returned status is not the expected one", packet.getStatus().getID(), is(equalTo(status))); assertThat("Returned Command value is not the expected one", packet.getCommandValue(), is(equalTo(value))); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); @@ -471,7 +471,7 @@ public final void testGetAPIDataATCommandParameterNull() { System.arraycopy(source64Addr.getValue(), 0, expectedData, 1, source64Addr.getValue().length); System.arraycopy(source16Addr.getValue(), 0, expectedData, 9, source16Addr.getValue().length); System.arraycopy(command.getBytes(), 0, expectedData, 11, command.length()); - expectedData[13] = (byte)status.getId(); + expectedData[13] = (byte)status.getID(); // Call the method under test. byte[] data = packet.getAPIData(); @@ -502,7 +502,7 @@ public final void testGetAPIDataATCommandParameterNotNull() { System.arraycopy(source64Addr.getValue(), 0, expectedData, 1, source64Addr.getValue().length); System.arraycopy(source16Addr.getValue(), 0, expectedData, 9, source16Addr.getValue().length); System.arraycopy(command.getBytes(), 0, expectedData, 11, command.length()); - expectedData[13] = (byte)status.getId(); + expectedData[13] = (byte)status.getID(); System.arraycopy(parameter, 0, expectedData, 12 + command.length(), parameter.length); // Call the method under test. @@ -530,7 +530,7 @@ public final void testGetAPIPacketParametersATCommandParameterNull() { String expectedDest64Addr = HexUtils.prettyHexString(source64Addr.getValue()); String expectedDest16Addr = HexUtils.prettyHexString(source16Addr.getValue()); - String expectedStatus = HexUtils.prettyHexString(HexUtils.integerToHexString(status.getId(), 1)) + " (" + status.getDescription() + ")"; + String expectedStatus = HexUtils.prettyHexString(HexUtils.integerToHexString(status.getID(), 1)) + " (" + status.getDescription() + ")"; String expectedATCommand = HexUtils.prettyHexString(command.getBytes()) + " (" + command + ")"; // Call the method under test. @@ -563,7 +563,7 @@ public final void testGetAPIPacketParametersATCommandParameterNonNull() { String expectedDest64Addr = HexUtils.prettyHexString(source64Addr.getValue()); String expectedDest16Addr = HexUtils.prettyHexString(source16Addr.getValue()); - String expectedStatus = HexUtils.prettyHexString(HexUtils.integerToHexString(status.getId(), 1)) + " (" + status.getDescription() + ")"; + String expectedStatus = HexUtils.prettyHexString(HexUtils.integerToHexString(status.getID(), 1)) + " (" + status.getDescription() + ")"; String expectedATCommand = HexUtils.prettyHexString(command.getBytes()) + " (" + command + ")"; String expectedATParameter = HexUtils.prettyHexString(parameter) + " (" + new String(parameter) + ")"; @@ -597,7 +597,7 @@ public final void testGetAPIPacketParametersATCommandParameterByteArrayNonString String expectedDest64Addr = HexUtils.prettyHexString(source64Addr.getValue()); String expectedDest16Addr = HexUtils.prettyHexString(source16Addr.getValue()); - String expectedStatus = HexUtils.prettyHexString(HexUtils.integerToHexString(status.getId(), 1)) + " (" + status.getDescription() + ")"; + String expectedStatus = HexUtils.prettyHexString(HexUtils.integerToHexString(status.getID(), 1)) + " (" + status.getDescription() + ")"; String expectedATCommand = HexUtils.prettyHexString(command.getBytes()) + " (" + command + ")"; String expectedATParameter = HexUtils.prettyHexString(parameter); diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java index bc6fce67..d2a97823 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java @@ -115,7 +115,7 @@ public final void testCreatePacketPayloadShorterThanNeeded() { int frameID = 0xE7; XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; - int deliveryStatus = XBeeTransmitStatus.SUCCESS.getId(); + int deliveryStatus = XBeeTransmitStatus.SUCCESS.getID(); byte[] payload = new byte[6]; payload[0] = (byte)frameType; @@ -143,8 +143,8 @@ public final void testCreatePacketPayloadNotIncludingFrameType() { int frameID = 0xE7; XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; - int deliveryStatus = XBeeTransmitStatus.SUCCESS.getId(); - int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getId(); + int deliveryStatus = XBeeTransmitStatus.SUCCESS.getID(); + int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getID(); byte[] payload = new byte[7]; payload[0] = (byte)frameID; @@ -173,8 +173,8 @@ public final void testCreatePacketValidPayload() { int frameID = 0xE7; XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; - int deliveryStatus = XBeeTransmitStatus.SUCCESS.getId(); - int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getId(); + int deliveryStatus = XBeeTransmitStatus.SUCCESS.getID(); + int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getID(); byte[] payload = new byte[7]; payload[0] = (byte)frameType; @@ -192,8 +192,8 @@ public final void testCreatePacketValidPayload() { assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); assertThat("Returned destination 16-bit address is not the expected one", packet.get16bitDestinationAddress(), is(equalTo(address))); assertThat("Returned retry count is not the expected one", packet.getTransmitRetryCount(), is(equalTo(retryCount))); - assertThat("Returned delivery status is not the expected one", packet.getTransmitStatus().getId(), is(equalTo(deliveryStatus))); - assertThat("Returned discovery status is not the expected one", packet.getDiscoveryStatus().getId(), is(equalTo(discoveryStatus))); + assertThat("Returned delivery status is not the expected one", packet.getTransmitStatus().getID(), is(equalTo(deliveryStatus))); + assertThat("Returned discovery status is not the expected one", packet.getDiscoveryStatus().getID(), is(equalTo(discoveryStatus))); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); } @@ -401,8 +401,8 @@ public final void testGetAPIData() { expectedData[0] = (byte)frameID; System.arraycopy(dest16Addr.getValue(), 0, expectedData, 1, dest16Addr.getValue().length); expectedData[3] = (byte)retryCount; - expectedData[4] = (byte)transmitStatus.getId(); - expectedData[5] = (byte)discoveryStatus.getId(); + expectedData[4] = (byte)transmitStatus.getID(); + expectedData[5] = (byte)discoveryStatus.getID(); // Call the method under test. byte[] apiData = packet.getAPIData(); @@ -428,8 +428,8 @@ public final void testGetAPIPacketParameters() { String expectedDest16Addr = HexUtils.prettyHexString(dest16Addr.getValue()); String expectedRetryCount = HexUtils.prettyHexString(Integer.toHexString(retryCount)) + " (" + retryCount + ")"; - String expectedTransmitStatus = HexUtils.prettyHexString(Integer.toHexString(transmitStatus.getId())) + " (" + transmitStatus.getDescription() + ")"; - String expectedDiscoveryStatus = HexUtils.prettyHexString(Integer.toHexString(discoveryStatus.getId())) + " (" + discoveryStatus.getDescription() + ")"; + String expectedTransmitStatus = HexUtils.prettyHexString(Integer.toHexString(transmitStatus.getID())) + " (" + transmitStatus.getDescription() + ")"; + String expectedDiscoveryStatus = HexUtils.prettyHexString(Integer.toHexString(discoveryStatus.getID())) + " (" + discoveryStatus.getDescription() + ")"; // Call the method under test. LinkedHashMap packetParams = packet.getAPIPacketParameters(); diff --git a/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java index dd6b949e..10fd8386 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java @@ -133,7 +133,7 @@ public final void testCreatePacketPayloadShorterThanNeeded() { public final void testCreatePacketPayloadNotIncludingFrameType() { // Setup the resources for the test. int frameID = 0xE7; - int status = XBeeTransmitStatus.SUCCESS.getId(); + int status = XBeeTransmitStatus.SUCCESS.getID(); byte[] payload = new byte[3]; payload[0] = (byte)frameID; @@ -157,7 +157,7 @@ public final void testCreatePacketValidPayload() { // Setup the resources for the test. int frameType = APIFrameType.TX_STATUS.getValue(); int frameID = 0xE7; - int status = XBeeTransmitStatus.SUCCESS.getId(); + int status = XBeeTransmitStatus.SUCCESS.getID(); byte[] payload = new byte[3]; payload[0] = (byte)frameType; @@ -170,7 +170,7 @@ public final void testCreatePacketValidPayload() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); - assertThat("Returned status is not the expected one", packet.getTransmitStatus().getId(), is(equalTo(status))); + assertThat("Returned status is not the expected one", packet.getTransmitStatus().getID(), is(equalTo(status))); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); } @@ -270,7 +270,7 @@ public final void testGetAPIData() { int expectedLength = 1 /* Frame ID */ + 1 /* transmit status */; byte[] expectedData = new byte[expectedLength]; expectedData[0] = (byte)frameID; - expectedData[1] = (byte)transmitStatus.getId(); + expectedData[1] = (byte)transmitStatus.getID(); // Call the method under test. byte[] apiData = packet.getAPIData(); @@ -291,7 +291,7 @@ public final void testGetAPIPacketParameters() { XBeeTransmitStatus transmitStatus = XBeeTransmitStatus.SUCCESS; TXStatusPacket packet = new TXStatusPacket(frameID, transmitStatus); - String expectedTransmitStatus = HexUtils.prettyHexString(Integer.toHexString(transmitStatus.getId())) + " (" + transmitStatus.getDescription() + ")"; + String expectedTransmitStatus = HexUtils.prettyHexString(Integer.toHexString(transmitStatus.getID())) + " (" + transmitStatus.getDescription() + ")"; // Call the method under test. LinkedHashMap packetParams = packet.getAPIPacketParameters(); From f1a83cea8325161cd780ab8dbde9e0991683dcdd Mon Sep 17 00:00:00 2001 From: David Escalona Date: Tue, 5 May 2015 13:55:36 +0200 Subject: [PATCH 02/14] Coverity fixes wave 2 --- .../com/digi/xbee/api/AbstractXBeeDevice.java | 25 ++++++++++++------- .../java/com/digi/xbee/api/XBeeNetwork.java | 6 +++-- .../xbee/api/models/ATStringCommands.java | 4 ++- .../com/digi/xbee/api/utils/StringUtils.java | 17 +++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java b/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java index 4ceabaa0..66e2ca0a 100644 --- a/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java +++ b/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java @@ -1053,16 +1053,20 @@ public void packetReceived(XBeePacket receivedPacket) { if (sentAPIPacket.getFrameType() == APIFrameType.AT_COMMAND) { if (receivedAPIPacket.getFrameType() != APIFrameType.AT_COMMAND_RESPONSE) return; - if (!((ATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((ATCommandResponsePacket)receivedPacket).getCommand())) - return; + if (sentAPIPacket instanceof ATCommandPacket && receivedPacket instanceof ATCommandResponsePacket) { + if (!((ATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((ATCommandResponsePacket)receivedPacket).getCommand())) + return; + } } // If the packet sent is a remote AT command, verify that the received one is a remote AT command response and // the command matches in both packets. if (sentAPIPacket.getFrameType() == APIFrameType.REMOTE_AT_COMMAND_REQUEST) { if (receivedAPIPacket.getFrameType() != APIFrameType.REMOTE_AT_COMMAND_RESPONSE) return; - if (!((RemoteATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((RemoteATCommandResponsePacket)receivedPacket).getCommand())) - return; + if (sentAPIPacket instanceof RemoteATCommandPacket && receivedPacket instanceof RemoteATCommandResponsePacket) { + if (!((RemoteATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((RemoteATCommandResponsePacket)receivedPacket).getCommand())) + return; + } } } @@ -1071,7 +1075,7 @@ public void packetReceived(XBeePacket receivedPacket) { if (!isSamePacket(sentPacket, receivedPacket)) { responseList.add(receivedPacket); synchronized (responseList) { - responseList.notify(); + responseList.notifyAll(); } } } @@ -1837,13 +1841,16 @@ public void packetReceived(XBeePacket receivedPacket) { // Save the packet value (IO sample payload) switch (((XBeeAPIPacket)receivedPacket).getFrameType()) { case IO_DATA_SAMPLE_RX_INDICATOR: - ioPacketPayload = ((IODataSampleRxIndicatorPacket)receivedPacket).getRFData(); + if (receivedPacket instanceof IODataSampleRxIndicatorPacket) + ioPacketPayload = ((IODataSampleRxIndicatorPacket)receivedPacket).getRFData(); break; case RX_IO_16: - ioPacketPayload = ((RX16IOPacket)receivedPacket).getRFData(); + if (receivedPacket instanceof RX16IOPacket) + ioPacketPayload = ((RX16IOPacket)receivedPacket).getRFData(); break; case RX_IO_64: - ioPacketPayload = ((RX64IOPacket)receivedPacket).getRFData(); + if (receivedPacket instanceof RX64IOPacket) + ioPacketPayload = ((RX64IOPacket)receivedPacket).getRFData(); break; default: return; @@ -1853,7 +1860,7 @@ public void packetReceived(XBeePacket receivedPacket) { // Continue execution by notifying the lock object. synchronized (ioLock) { - ioLock.notify(); + ioLock.notifyAll(); } } }; diff --git a/library/src/main/java/com/digi/xbee/api/XBeeNetwork.java b/library/src/main/java/com/digi/xbee/api/XBeeNetwork.java index 3152827b..9c97ede8 100644 --- a/library/src/main/java/com/digi/xbee/api/XBeeNetwork.java +++ b/library/src/main/java/com/digi/xbee/api/XBeeNetwork.java @@ -744,10 +744,12 @@ private XBee16BitAddress get16BitAddress(RemoteXBeeDevice device) { switch (device.getXBeeProtocol()) { case RAW_802_15_4: - address = ((RemoteRaw802Device)device).get16BitAddress(); + if (device instanceof RemoteRaw802Device) + address = ((RemoteRaw802Device)device).get16BitAddress(); break; case ZIGBEE: - address = ((RemoteZigBeeDevice)device).get16BitAddress(); + if (device instanceof RemoteZigBeeDevice) + address = ((RemoteZigBeeDevice)device).get16BitAddress(); break; default: // TODO should we allow this operation for general remote devices? diff --git a/library/src/main/java/com/digi/xbee/api/models/ATStringCommands.java b/library/src/main/java/com/digi/xbee/api/models/ATStringCommands.java index b327a3f0..b32c4dd9 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ATStringCommands.java +++ b/library/src/main/java/com/digi/xbee/api/models/ATStringCommands.java @@ -13,6 +13,8 @@ import java.util.HashMap; +import com.digi.xbee.api.utils.StringUtils; + /** * Enumerates several AT commands used to parse AT command packets. The list * of AT Command alias listed here represents those AT commands whose values @@ -66,6 +68,6 @@ public String getCommand() { * alias. */ public static ATStringCommands get(String command) { - return lookupTable.get(command.toUpperCase()); + return lookupTable.get(StringUtils.stringToUpperCase(command)); } } diff --git a/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java b/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java index 81735797..48d76cff 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java @@ -2,12 +2,29 @@ import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; +import java.util.Locale; public class StringUtils { // Constants. private final static String UTF8_CHARSET = "UTF-8"; + /** + * Transforms the given string to upper case format. + * + * @param source String to transform to upper case. + * + * @return The given string in upper case. + * + * @throws NullPointerException if {@code source == null}. + */ + public static String stringToUpperCase(String source) { + if (source == null) + throw new NullPointerException("String cannot be null."); + + return source.toUpperCase(new Locale(UTF8_CHARSET)); + } + /** * Transforms the given string to the corresponding byte array. * From 62a651b233664a84cecbb026cfe178e0fe1e4479 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Tue, 5 May 2015 17:00:31 +0200 Subject: [PATCH 03/14] Coverity fixes wave 3 --- .../java/com/digi/xbee/api/io/IOSample.java | 2 +- .../com/digi/xbee/api/models/ATCommand.java | 14 +++++++--- .../xbee/api/models/ATCommandResponse.java | 9 +++++-- .../com/digi/xbee/api/models/XBeeMessage.java | 4 +-- .../xbee/api/packet/GenericXBeePacket.java | 14 +++++++--- .../xbee/api/packet/UnknownXBeePacket.java | 14 +++++++--- .../api/packet/common/ATCommandPacket.java | 14 +++++++--- .../packet/common/ATCommandQueuePacket.java | 14 +++++++--- .../common/ATCommandResponsePacket.java | 14 +++++++--- .../common/IODataSampleRxIndicatorPacket.java | 22 ++++++++++----- .../xbee/api/packet/common/ReceivePacket.java | 14 +++++++--- .../packet/common/RemoteATCommandPacket.java | 14 +++++++--- .../common/RemoteATCommandResponsePacket.java | 14 +++++++--- .../api/packet/common/TransmitPacket.java | 14 +++++++--- .../xbee/api/packet/raw/RX16IOPacket.java | 23 +++++++++++----- .../digi/xbee/api/packet/raw/RX16Packet.java | 14 +++++++--- .../xbee/api/packet/raw/RX64IOPacket.java | 27 +++++++++++++------ .../digi/xbee/api/packet/raw/RX64Packet.java | 14 +++++++--- .../digi/xbee/api/packet/raw/TX16Packet.java | 14 +++++++--- .../digi/xbee/api/packet/raw/TX64Packet.java | 14 +++++++--- 20 files changed, 216 insertions(+), 67 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/io/IOSample.java b/library/src/main/java/com/digi/xbee/api/io/IOSample.java index b1233277..ec019396 100644 --- a/library/src/main/java/com/digi/xbee/api/io/IOSample.java +++ b/library/src/main/java/com/digi/xbee/api/io/IOSample.java @@ -138,7 +138,7 @@ public IOSample(byte[] ioSamplePayload) { if (ioSamplePayload.length < 5) throw new IllegalArgumentException("IO sample payload must be longer than 4."); - this.ioSamplePayload = ioSamplePayload; + this.ioSamplePayload = ioSamplePayload.clone(); if (ioSamplePayload.length % 2 != 0) parseRawIOSample(); else diff --git a/library/src/main/java/com/digi/xbee/api/models/ATCommand.java b/library/src/main/java/com/digi/xbee/api/models/ATCommand.java index b2809d15..4a759b40 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ATCommand.java +++ b/library/src/main/java/com/digi/xbee/api/models/ATCommand.java @@ -82,7 +82,10 @@ public ATCommand(String command, byte[] parameter) { throw new IllegalArgumentException("Command lenght must be 2."); this.command = command; - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; } /** @@ -101,7 +104,9 @@ public String getCommand() { * have a parameter. */ public byte[] getParameter() { - return parameter; + if (parameter != null) + return parameter.clone(); + return null; } /** @@ -131,6 +136,9 @@ public void setParameter(String parameter) { * @param parameter The AT command parameter as byte array. */ public void setParameter(byte[] parameter) { - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; } } diff --git a/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java b/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java index 8d6417de..91530eb5 100644 --- a/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java +++ b/library/src/main/java/com/digi/xbee/api/models/ATCommandResponse.java @@ -101,7 +101,10 @@ public ATCommandResponse(ATCommand command, byte[] response, ATCommandStatus sta throw new NullPointerException("Status cannot be null."); this.command = command; - this.response = response; + if (response != null) + this.response = response.clone(); + else + this.response = null; this.status = status; } @@ -123,7 +126,9 @@ public ATCommand getCommand() { * {@code null} if there is not response data. */ public byte[] getResponse() { - return response; + if (response != null) + return response.clone(); + return null; } /** diff --git a/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java b/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java index 60a161c9..afb205d0 100644 --- a/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java +++ b/library/src/main/java/com/digi/xbee/api/models/XBeeMessage.java @@ -66,7 +66,7 @@ public XBeeMessage(RemoteXBeeDevice remoteXBeeDevice, byte[] data, boolean isBro throw new NullPointerException("Data cannot be null."); this.remoteXBeeDevice = remoteXBeeDevice; - this.data = data; + this.data = data.clone(); this.isBroadcast = isBroadcast; } @@ -87,7 +87,7 @@ public RemoteXBeeDevice getDevice() { * @return A byte array containing the data of the message. */ public byte[] getData() { - return data; + return data.clone(); } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/GenericXBeePacket.java b/library/src/main/java/com/digi/xbee/api/packet/GenericXBeePacket.java index e9aed705..225b90d7 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/GenericXBeePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/GenericXBeePacket.java @@ -79,7 +79,10 @@ public static GenericXBeePacket createPacket(byte[] payload) { */ public GenericXBeePacket(byte[] rfData) { super(APIFrameType.GENERIC); - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(GenericXBeePacket.class); } @@ -114,7 +117,10 @@ public boolean needsAPIFrameID() { * @param rfData The new XBee RF Data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -123,7 +129,9 @@ public void setRFData(byte[] rfData) { * @return The RF Data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/UnknownXBeePacket.java b/library/src/main/java/com/digi/xbee/api/packet/UnknownXBeePacket.java index 1f8acf9c..a44db9cb 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/UnknownXBeePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/UnknownXBeePacket.java @@ -80,7 +80,10 @@ public static UnknownXBeePacket createPacket(byte[] payload) { */ public UnknownXBeePacket(int apiIDValue, byte[] rfData) { super(apiIDValue); - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(UnknownXBeePacket.class); } @@ -115,7 +118,10 @@ public boolean needsAPIFrameID() { * @param rfData The new XBee RF Data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -124,7 +130,9 @@ public void setRFData(byte[] rfData) { * @return The RF Data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java index 21cee631..d8c3aee6 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandPacket.java @@ -134,7 +134,10 @@ public ATCommandPacket(int frameID, String command, byte[] parameter) { this.frameID = frameID; this.command = command; - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; this.logger = LoggerFactory.getLogger(ATCommandPacket.class); } @@ -191,7 +194,10 @@ public void setParameter(String parameter) { * @param parameter The AT command parameter. */ public void setParameter(byte[] parameter) { - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; } /** @@ -200,7 +206,9 @@ public void setParameter(byte[] parameter) { * @return The AT command parameter. */ public byte[] getParameter() { - return parameter; + if (parameter != null) + return parameter.clone(); + return null; } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java index 48646a22..0bb1b891 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandQueuePacket.java @@ -140,7 +140,10 @@ public ATCommandQueuePacket(int frameID, String command, byte[] parameter) { this.frameID = frameID; this.command = command; - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; this.logger = LoggerFactory.getLogger(ATCommandQueuePacket.class); } @@ -197,7 +200,10 @@ public void setParameter(String parameter) { * @param parameter The AT command parameter. */ public void setParameter(byte[] parameter) { - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; } /** @@ -206,7 +212,9 @@ public void setParameter(byte[] parameter) { * @return The AT command parameter. */ public byte[] getParameter() { - return parameter; + if (parameter != null) + return parameter.clone(); + return null; } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java index 456ea39b..1f732c7d 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ATCommandResponsePacket.java @@ -137,7 +137,10 @@ public ATCommandResponsePacket(int frameID, ATCommandStatus status, String comma this.frameID = frameID; this.status = status; this.command = command; - this.commandValue = commandValue; + if (commandValue != null) + this.commandValue = commandValue.clone(); + else + this.commandValue = null; this.logger = LoggerFactory.getLogger(ATCommandResponsePacket.class); } @@ -206,7 +209,10 @@ public void setCommandValue(String commandValue) { * @param commandValue The AT command response value. */ public void setCommandValue(byte[] commandValue) { - this.commandValue = commandValue; + if (commandValue != null) + this.commandValue = commandValue.clone(); + else + this.commandValue = null; } /** @@ -215,7 +221,9 @@ public void setCommandValue(byte[] commandValue) { * @return The AT command response value. */ public byte[] getCommandValue() { - return commandValue; + if (commandValue != null) + return commandValue.clone(); + return null; } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java index ac629642..8ac36ff5 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java @@ -53,7 +53,7 @@ public class IODataSampleRxIndicatorPacket extends XBeeAPIPacket { private final XBee64BitAddress sourceAddress64; private final XBee16BitAddress sourceAddress16; - private final IOSample ioSample; + private IOSample ioSample; private final int receiveOptions; @@ -144,11 +144,13 @@ public IODataSampleRxIndicatorPacket(XBee64BitAddress sourceAddress64, XBee16Bit this.sourceAddress64 = sourceAddress64; this.sourceAddress16 = sourceAddress16; this.receiveOptions = receiveOptions; - this.rfData = rfData; - if (rfData != null) + if (rfData != null) { + this.rfData = rfData.clone(); ioSample = new IOSample(rfData); - else + } else { + this.rfData = null; ioSample = null; + } this.logger = LoggerFactory.getLogger(RX64Packet.class); } @@ -240,7 +242,13 @@ public IOSample getIOSample() { * @param rfData Received RF data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) { + this.rfData = rfData.clone(); + this.ioSample = new IOSample(this.rfData); + } else { + this.rfData = null; + this.ioSample = null; + } } /** @@ -249,7 +257,9 @@ public void setRFData(byte[] rfData) { * @return Received RF data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ReceivePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ReceivePacket.java index f5c00f6a..43d4f7db 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ReceivePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ReceivePacket.java @@ -139,7 +139,10 @@ public ReceivePacket(XBee64BitAddress sourceAddress64, XBee16BitAddress sourceAd this.sourceAddress64 = sourceAddress64; this.sourceAddress16 = sourceAddress16; this.receiveOptions = receiveOptions; - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(ReceivePacket.class); } @@ -219,7 +222,10 @@ public int getReceiveOptions() { * @param rfData Received RF data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -228,7 +234,9 @@ public void setRFData(byte[] rfData) { * @return Received RF data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java index cda5a03f..589a3a90 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandPacket.java @@ -214,7 +214,10 @@ public RemoteATCommandPacket(int frameID, XBee64BitAddress destAddress64, XBee16 this.destAddress16 = destAddress16; this.transmitOptions = transmitOptions; this.command = command; - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; this.logger = LoggerFactory.getLogger(RemoteATCommandPacket.class); } @@ -317,7 +320,10 @@ public void setParameter(String parameter) { * @param parameter The AT command parameter. */ public void setParameter(byte[] parameter) { - this.parameter = parameter; + if (parameter != null) + this.parameter = parameter.clone(); + else + this.parameter = null; } /** @@ -326,7 +332,9 @@ public void setParameter(byte[] parameter) { * @return The AT command parameter. */ public byte[] getParameter() { - return parameter; + if (parameter != null) + return parameter.clone(); + return null; } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java index 0075cb87..315715a7 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/RemoteATCommandResponsePacket.java @@ -168,7 +168,10 @@ public RemoteATCommandResponsePacket(int frameID, XBee64BitAddress sourceAddress this.sourceAddress16 = sourceAddress16; this.command = command; this.status = status; - this.commandValue = commandValue; + if (commandValue != null) + this.commandValue = commandValue.clone(); + else + this.commandValue = null; this.logger = LoggerFactory.getLogger(RemoteATCommandResponsePacket.class); } @@ -261,7 +264,10 @@ public void setCommandValue(String commandValue) { * @param commandValue The AT command response value. */ public void setCommandValue(byte[] commandValue) { - this.commandValue = commandValue; + if (commandValue != null) + this.commandValue = commandValue.clone(); + else + this.commandValue = null; } /** @@ -270,7 +276,9 @@ public void setCommandValue(byte[] commandValue) { * @return The AT command response value. */ public byte[] getCommandValue() { - return commandValue; + if (commandValue != null) + return commandValue.clone(); + return null; } /** diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/TransmitPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/TransmitPacket.java index 0aea2524..71ba1b99 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/TransmitPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/TransmitPacket.java @@ -192,7 +192,10 @@ public TransmitPacket(int frameID, XBee64BitAddress destAddress64, XBee16BitAddr this.destAddress16 = destAddress16; this.broadcastRadius = broadcastRadius; this.transmitOptions = transmitOptions; - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(TransmitPacket.class); } @@ -283,7 +286,10 @@ public int getTransmitOptions() { * @param rfData RF Data to send. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -292,7 +298,9 @@ public void setRFData(byte[] rfData) { * @return RF Data to send. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java index abdb287c..82bcca54 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java @@ -43,7 +43,7 @@ public class RX16IOPacket extends XBeeAPIPacket { // Variables. private final XBee16BitAddress sourceAddress16; - private final IOSample ioSample; + private IOSample ioSample; private final int rssi; private final int receiveOptions; @@ -136,11 +136,13 @@ public RX16IOPacket(XBee16BitAddress sourceAddress16, int rssi, int receiveOptio this.sourceAddress16 = sourceAddress16; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData; - if (rfData != null) + if (rfData != null) { + this.rfData = rfData.clone(); ioSample = new IOSample(rfData); - else + } else { + this.rfData = null; ioSample = null; + } this.logger = LoggerFactory.getLogger(RX16IOPacket.class); } @@ -231,7 +233,14 @@ public IOSample getIOSample() { * @param rfData Received RF data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) { + this.rfData = rfData.clone(); + this.ioSample = new IOSample(this.rfData); + } else { + this.rfData = null; + this.ioSample = null; + } + } /** @@ -240,7 +249,9 @@ public void setRFData(byte[] rfData) { * @return Received RF data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16Packet.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16Packet.java index a5452ad4..6c990601 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16Packet.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16Packet.java @@ -135,7 +135,10 @@ public RX16Packet(XBee16BitAddress sourceAddress16, int rssi, int receiveOptions this.sourceAddress16 = sourceAddress16; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(RX16Packet.class); } @@ -214,7 +217,10 @@ public int getReceiveOptions() { * @param rfData Received RF data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -223,7 +229,9 @@ public void setRFData(byte[] rfData) { * @return Received RF data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java index a7018b1c..522e7f80 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java @@ -43,7 +43,7 @@ public class RX64IOPacket extends XBeeAPIPacket { // Variables. private final XBee64BitAddress sourceAddress64; - private final IOSample ioSample; + private IOSample ioSample; private final int rssi; private final int receiveOptions; @@ -136,11 +136,13 @@ public RX64IOPacket(XBee64BitAddress sourceAddress64, int rssi, int receiveOptio this.sourceAddress64 = sourceAddress64; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData; - if (rfData != null) + if (rfData != null) { + this.rfData = rfData.clone(); ioSample = new IOSample(rfData); - else + } else { + this.rfData = null; ioSample = null; + } this.logger = LoggerFactory.getLogger(RX64Packet.class); } @@ -230,8 +232,15 @@ public IOSample getIOSample() { * * @param rfData Received RF data. */ - public void setRFData(byte[] rfData){ - this.rfData = rfData; + public void setRFData(byte[] rfData) { + if (rfData != null) { + this.rfData = rfData.clone(); + this.ioSample = new IOSample(this.rfData); + } else { + this.rfData = null; + this.ioSample = null; + } + } /** @@ -239,8 +248,10 @@ public void setRFData(byte[] rfData){ * * @return Received RF data. */ - public byte[] getRFData(){ - return rfData; + public byte[] getRFData() { + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64Packet.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64Packet.java index 551b00a0..828eb2ce 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64Packet.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64Packet.java @@ -135,7 +135,10 @@ public RX64Packet(XBee64BitAddress sourceAddress64, int rssi, int receiveOptions this.sourceAddress64 = sourceAddress64; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(RX64Packet.class); } @@ -214,7 +217,10 @@ public int getReceiveOptions() { * @param rfData Received RF data. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -223,7 +229,9 @@ public void setRFData(byte[] rfData) { * @return Received RF data. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/TX16Packet.java b/library/src/main/java/com/digi/xbee/api/packet/raw/TX16Packet.java index f69d6cf1..ab2e0c66 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/TX16Packet.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/TX16Packet.java @@ -129,7 +129,10 @@ public TX16Packet(int frameID, XBee16BitAddress destAddress16, int transmitOptio this.frameID = frameID; this.destAddress16 = destAddress16; this.transmitOptions = transmitOptions; - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(TX16Packet.class); } @@ -197,7 +200,10 @@ public int getTransmitOptions() { * @param rfData RF Data to send. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -206,7 +212,9 @@ public void setRFData(byte[] rfData) { * @return RF data to send. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/TX64Packet.java b/library/src/main/java/com/digi/xbee/api/packet/raw/TX64Packet.java index f678a175..4322e658 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/TX64Packet.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/TX64Packet.java @@ -129,7 +129,10 @@ public TX64Packet(int frameID, XBee64BitAddress destAddress64, int transmitOptio this.frameID = frameID; this.destAddress64 = destAddress64; this.transmitOptions = transmitOptions; - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; this.logger = LoggerFactory.getLogger(TX64Packet.class); } @@ -197,7 +200,10 @@ public int getTransmitOptions() { * @param rfData RF Data to send. */ public void setRFData(byte[] rfData) { - this.rfData = rfData; + if (rfData != null) + this.rfData = rfData.clone(); + else + this.rfData = null; } /** @@ -206,7 +212,9 @@ public void setRFData(byte[] rfData) { * @return RF data to send. */ public byte[] getRFData() { - return rfData; + if (rfData != null) + return rfData.clone(); + return null; } /* From adf801098f45a8a1c27d2f63baa57bd653aeb037 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 11:58:33 +0200 Subject: [PATCH 04/14] Added *.orig files to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 678af657..2863048f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ bin /target /.settings /examples/*/*/.gitignore +*.orig From cee013a8918b924725fc0f4d1fb6c87feba7b273 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 11:59:40 +0200 Subject: [PATCH 05/14] Fixes after master merge to pass all tests. --- .../digi/xbee/api/models/XBeeTransmitStatus.java | 2 +- .../com/digi/xbee/api/packet/XBeeAPIPacket.java | 4 +++- .../common/IODataSampleRxIndicatorPacket.java | 5 +---- .../api/packet/common/TransmitStatusPacket.java | 4 ++-- .../com/digi/xbee/api/packet/raw/RX16IOPacket.java | 6 ++---- .../com/digi/xbee/api/packet/raw/RX64IOPacket.java | 6 ++---- .../digi/xbee/api/packet/raw/TXStatusPacket.java | 2 +- .../java/com/digi/xbee/api/utils/HexUtils.java | 4 ++-- .../java/com/digi/xbee/api/utils/StringUtils.java | 4 +++- .../digi/xbee/api/models/ModemStatusEventTest.java | 2 +- .../xbee/api/models/XBeeDiscoveryStatusTest.java | 10 +++++----- .../xbee/api/models/XBeeTransmitStatusTest.java | 10 +++++----- .../packet/common/TransmitStatusPacketTest.java | 14 +++++++------- .../xbee/api/packet/raw/TXStatusPacketTest.java | 2 +- .../com/digi/xbee/api/utils/ByteUtilsTest.java | 14 +++++++------- 15 files changed, 43 insertions(+), 46 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java b/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java index c81880a4..b6d8db24 100644 --- a/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java +++ b/library/src/main/java/com/digi/xbee/api/models/XBeeTransmitStatus.java @@ -118,7 +118,7 @@ public static XBeeTransmitStatus get(int id) { */ @Override public String toString() { - if (id != SUCCESS.getId()) + if (id != SUCCESS.getID()) return String.format("Error: %s (0x%02X)", description, id); else return String.format("%s (0x%02X)", description, id); diff --git a/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java b/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java index 3534d299..0b58dc1d 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java @@ -122,7 +122,9 @@ public byte[] getPacketData() { data.write(frameTypeValue); byte[] apiData = getAPIData(); - if (apiData.length > 0) { + if (apiData == null) + apiData = new byte[0]; + if (apiData != null && apiData.length > 0) { try { data.write(apiData); } catch (IOException e) { diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java index 661fdb1f..3a198564 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java @@ -142,14 +142,11 @@ public IODataSampleRxIndicatorPacket(XBee64BitAddress sourceAddress64, XBee16Bit this.sourceAddress64 = sourceAddress64; this.sourceAddress16 = sourceAddress16; this.receiveOptions = receiveOptions; - if (rfData != null) { this.rfData = rfData; if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); - } else { - this.rfData = null; + else ioSample = null; - } this.logger = LoggerFactory.getLogger(RX64Packet.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java index 4bea0e3d..03b6b3b9 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/TransmitStatusPacket.java @@ -243,10 +243,10 @@ public LinkedHashMap getAPIPacketParameters() { HexUtils.prettyHexString(HexUtils.integerToHexString(tranmistRetryCount, 1)) + " (" + tranmistRetryCount + ")"); parameters.put("Delivery status", - HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getId(), 1)) + HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getID(), 1)) + " (" + transmitStatus.getDescription() + ")"); parameters.put("Discovery status", - HexUtils.prettyHexString(HexUtils.integerToHexString(discoveryStatus.getId(), 1)) + HexUtils.prettyHexString(HexUtils.integerToHexString(discoveryStatus.getID(), 1)) + " (" + discoveryStatus.getDescription() + ")"); return parameters; } diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java index 24a1c7bc..6ffad28e 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java @@ -134,13 +134,11 @@ public RX16IOPacket(XBee16BitAddress sourceAddress16, int rssi, int receiveOptio this.sourceAddress16 = sourceAddress16; this.rssi = rssi; this.receiveOptions = receiveOptions; - if (rfData != null) { + this.rfData = rfData; if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); - } else { - this.rfData = null; + else ioSample = null; - } this.logger = LoggerFactory.getLogger(RX16IOPacket.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java index 1477eadf..5f2378e6 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java @@ -134,13 +134,11 @@ public RX64IOPacket(XBee64BitAddress sourceAddress64, int rssi, int receiveOptio this.sourceAddress64 = sourceAddress64; this.rssi = rssi; this.receiveOptions = receiveOptions; - if (rfData != null) { + this.rfData = rfData; if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); - } else { - this.rfData = null; + else ioSample = null; - } this.logger = LoggerFactory.getLogger(RX64Packet.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java index f39620b4..d57cfa10 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/TXStatusPacket.java @@ -149,7 +149,7 @@ public boolean isBroadcast() { public LinkedHashMap getAPIPacketParameters() { LinkedHashMap parameters = new LinkedHashMap(); parameters.put("Status", - HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getId(), 1)) + HexUtils.prettyHexString(HexUtils.integerToHexString(transmitStatus.getID(), 1)) + " (" + transmitStatus.getDescription() + ")"); return parameters; } diff --git a/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java b/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java index 47d03387..4d540550 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java @@ -147,17 +147,17 @@ public static String prettyHexString(String hexString) { throw new NullPointerException("Hexadecimal string cannot be null."); String copy = hexString.toUpperCase(); - StringBuilder prettyHexString = new StringBuilder(); for (final char c : copy.toCharArray()) { if (!HEXES.contains(""+c)) throw new IllegalArgumentException("Given string cannot contain non-hexadecimal characters."); } + StringBuilder prettyHexString = new StringBuilder(); if (copy.length() % 2 != 0) copy = "0" + copy; int iterations = copy.length() / 2; for (int i = 0; i < iterations; i++) - prettyHexString.append(hexString.substring(2 * i, 2 * i + 2) + " "); + prettyHexString.append(copy.substring(2 * i, 2 * i + 2) + " "); return prettyHexString.toString().trim(); } diff --git a/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java b/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java index 48d76cff..63cf7968 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/StringUtils.java @@ -63,6 +63,8 @@ public static byte[] stringToByteArray(String source) { * @see #byteArrayToString(byte[], int, int) */ public static String byteArrayToString(byte[] byteArray) { + if (byteArray == null) + throw new NullPointerException("Byte array cannot be null."); return byteArrayToString(byteArray, 0, byteArray.length); } @@ -83,7 +85,7 @@ public static String byteArrayToString(byte[] byteArray) { */ public static String byteArrayToString(byte[] byteArray, int offset, int numBytes) { if (byteArray == null) - throw new NullPointerException("Value cannot be null."); + throw new NullPointerException("Byte array cannot be null."); String value; try { diff --git a/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java b/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java index a8945734..1e28557d 100644 --- a/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/ModemStatusEventTest.java @@ -86,6 +86,6 @@ public void testModemStatusEventStaticAccess() { @Test public void testModemStatusEventToString() { for (ModemStatusEvent modemStatusEvent:modemStatusEventValues) - assertEquals(String.format("0x%02X: %s", modemStatusEvent.getId(), modemStatusEvent.getDescription()), modemStatusEvent.toString()); + assertEquals(String.format("0x%02X: %s", modemStatusEvent.getID(), modemStatusEvent.getDescription()), modemStatusEvent.toString()); } } diff --git a/library/src/test/java/com/digi/xbee/api/models/XBeeDiscoveryStatusTest.java b/library/src/test/java/com/digi/xbee/api/models/XBeeDiscoveryStatusTest.java index 31f276d5..c21d8278 100644 --- a/library/src/test/java/com/digi/xbee/api/models/XBeeDiscoveryStatusTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/XBeeDiscoveryStatusTest.java @@ -63,13 +63,13 @@ public void tearDown() throws Exception { public void testGetWithASupportedValue() { // Setup the resources for the test. XBeeDiscoveryStatus expectedStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_EXTENDED_TIMEOUT_DISCOVERY; - int id = expectedStatus.getId(); + int id = expectedStatus.getID(); // Call the method under test. XBeeDiscoveryStatus status = XBeeDiscoveryStatus.get(id); // Verify the result. - assertThat("The identifier of both status does not have the same value", status.getId(), is(equalTo(id))); + assertThat("The identifier of both status does not have the same value", status.getID(), is(equalTo(id))); assertThat("Transmit status is not the expected", status, is(equalTo(expectedStatus))); } @@ -86,7 +86,7 @@ public void testGetWithANonSupportedValue() { XBeeDiscoveryStatus status = XBeeDiscoveryStatus.get(id); // Verify the result. - assertThat("The identifier of both status does not have the same value", status.getId(), is(equalTo(expectedStatus.getId()))); + assertThat("The identifier of both status does not have the same value", status.getID(), is(equalTo(expectedStatus.getID()))); assertThat("The description of both status does not have the same value", status.getDescription(), is(equalTo(expectedStatus.getDescription()))); assertThat("Transmit status is not the expected", status, is(equalTo(expectedStatus))); } @@ -99,7 +99,7 @@ public void testGetWithANonSupportedValue() { @Test public void testXBeeDiscoveryStatusValues() { for (XBeeDiscoveryStatus status: discoveryStatusValues) - assertTrue(status.getId() >= 0); + assertTrue(status.getID() >= 0); } /** @@ -137,6 +137,6 @@ public void testXBeeDiscoveryStatusDescriptions() { @Test public void testXBeeDiscoveryStatusStaticAccess() { for (XBeeDiscoveryStatus status: discoveryStatusValues) - assertEquals(status, XBeeDiscoveryStatus.get(status.getId())); + assertEquals(status, XBeeDiscoveryStatus.get(status.getID())); } } diff --git a/library/src/test/java/com/digi/xbee/api/models/XBeeTransmitStatusTest.java b/library/src/test/java/com/digi/xbee/api/models/XBeeTransmitStatusTest.java index 3267bd80..62ab63fb 100644 --- a/library/src/test/java/com/digi/xbee/api/models/XBeeTransmitStatusTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/XBeeTransmitStatusTest.java @@ -63,13 +63,13 @@ public void tearDown() throws Exception { public void testGetWithASupportedValue() { // Setup the resources for the test. XBeeTransmitStatus expectedStatus = XBeeTransmitStatus.ADDRESS_NOT_FOUND; - int id = expectedStatus.getId(); + int id = expectedStatus.getID(); // Call the method under test. XBeeTransmitStatus status = XBeeTransmitStatus.get(id); // Verify the result. - assertThat("The identifier of both status does not have the same value", status.getId(), is(equalTo(id))); + assertThat("The identifier of both status does not have the same value", status.getID(), is(equalTo(id))); assertThat("Transmit status is not the expected", status, is(equalTo(expectedStatus))); } @@ -86,7 +86,7 @@ public void testGetWithANonSupportedValue() { XBeeTransmitStatus status = XBeeTransmitStatus.get(id); // Verify the result. - assertThat("The identifier of both status does not have the same value", status.getId(), is(equalTo(expectedStatus.getId()))); + assertThat("The identifier of both status does not have the same value", status.getID(), is(equalTo(expectedStatus.getID()))); assertThat("The description of both status does not have the same value", status.getDescription(), is(equalTo(expectedStatus.getDescription()))); assertThat("Transmit status is not the expected", status, is(equalTo(expectedStatus))); } @@ -99,7 +99,7 @@ public void testGetWithANonSupportedValue() { @Test public void testXBeeTransmitStatusValues() { for (XBeeTransmitStatus status: transmitStatusValues) - assertTrue(status.getId() >= 0 || status.getId() == -1); + assertTrue(status.getID() >= 0 || status.getID() == -1); } /** @@ -137,6 +137,6 @@ public void testXBeeTransmitStatusDescriptions() { @Test public void testXBeeTransmitStatusStaticAccess() { for (XBeeTransmitStatus status: transmitStatusValues) - assertEquals(status, XBeeTransmitStatus.get(status.getId())); + assertEquals(status, XBeeTransmitStatus.get(status.getID())); } } diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java index fc42d6d0..65e88d4c 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/TransmitStatusPacketTest.java @@ -211,7 +211,7 @@ public final void testCreatePacketValidPayloadUnknownDeliveryStatus() { XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; int deliveryStatus = 255; - int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getId(); + int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getID(); byte[] payload = new byte[7]; payload[0] = (byte)frameType; @@ -229,7 +229,7 @@ public final void testCreatePacketValidPayloadUnknownDeliveryStatus() { assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); assertThat("Returned destination 16-bit address is not the expected one", packet.get16bitDestinationAddress(), is(equalTo(address))); assertThat("Returned retry count is not the expected one", packet.getTransmitRetryCount(), is(equalTo(retryCount))); - assertThat("Returned delivery status id is not the expected one", packet.getTransmitStatus().getId(), is(equalTo(XBeeTransmitStatus.UNKNOWN.getId()))); + assertThat("Returned delivery status id is not the expected one", packet.getTransmitStatus().getID(), is(equalTo(XBeeTransmitStatus.UNKNOWN.getID()))); assertThat("Returned delivery status description is not the expected one", packet.getTransmitStatus().getDescription(), is(equalTo(XBeeTransmitStatus.UNKNOWN.getDescription()))); assertThat("Returned discovery status is not the expected one", packet.getDiscoveryStatus(), is(equalTo(XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD))); @@ -248,7 +248,7 @@ public final void testCreatePacketValidPayloadUnknownDiscoveryStatus() { int frameID = 0xE7; XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; - int deliveryStatus = XBeeTransmitStatus.BROADCAST_FAILED.getId();; + int deliveryStatus = XBeeTransmitStatus.BROADCAST_FAILED.getID();; int discoveryStatus = 255; byte[] payload = new byte[7]; @@ -557,7 +557,7 @@ public final void testGetAPIDataUnknownTransmitStatus() { XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; int deliveryStatus = 255; - int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getId(); + int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_NO_DISCOVERY_OVERHEAD.getID(); byte[] payload = new byte[7]; payload[0] = (byte)frameType; @@ -591,7 +591,7 @@ public final void testGetAPIDataUnknownDiscoveryStatus() { int frameID = 0xE7; XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; - int deliveryStatus = XBeeTransmitStatus.NO_ACK.getId(); + int deliveryStatus = XBeeTransmitStatus.NO_ACK.getID(); int discoveryStatus = 255; byte[] payload = new byte[7]; @@ -658,7 +658,7 @@ public final void testGetAPIPacketParametersWithUnknownTransmitStatus() { XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; int deliveryStatus = 255; - int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_ADDRESS_DISCOVERY.getId(); + int discoveryStatus = XBeeDiscoveryStatus.DISCOVERY_STATUS_ADDRESS_DISCOVERY.getID(); byte[] payload = new byte[7]; payload[0] = (byte)frameType; @@ -698,7 +698,7 @@ public final void testGetAPIPacketParametersWithUnknownDiscoveryStatus() { int frameID = 0xE7; XBee16BitAddress address = new XBee16BitAddress("B45C"); int retryCount = 3; - int deliveryStatus = XBeeTransmitStatus.NO_ACK.getId(); + int deliveryStatus = XBeeTransmitStatus.NO_ACK.getID(); int discoveryStatus = 255; byte[] payload = new byte[7]; diff --git a/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java index 4b9616ea..b0814e08 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/raw/TXStatusPacketTest.java @@ -198,7 +198,7 @@ public final void testCreatePacketValidPayloadUnknownStatus() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); - assertThat("Returned status id is not the expected one", packet.getTransmitStatus().getId(), is(equalTo(XBeeTransmitStatus.UNKNOWN.getId()))); + assertThat("Returned status id is not the expected one", packet.getTransmitStatus().getID(), is(equalTo(XBeeTransmitStatus.UNKNOWN.getID()))); assertThat("Returned status description is not the expected one", packet.getTransmitStatus().getDescription(), is(equalTo(XBeeTransmitStatus.UNKNOWN.getDescription()))); assertThat("Returned payload array is not the expected one", packet.getPacketData(), is(equalTo(payload))); diff --git a/library/src/test/java/com/digi/xbee/api/utils/ByteUtilsTest.java b/library/src/test/java/com/digi/xbee/api/utils/ByteUtilsTest.java index c300f9cf..f4ca8c55 100644 --- a/library/src/test/java/com/digi/xbee/api/utils/ByteUtilsTest.java +++ b/library/src/test/java/com/digi/xbee/api/utils/ByteUtilsTest.java @@ -583,7 +583,7 @@ public final void testStringToByteArrayNullString() { exception.expectMessage(is(equalTo("Value cannot be null."))); // Call the method under test. - ByteUtils.stringToByteArray(s); + StringUtils.stringToByteArray(s); } /** @@ -595,7 +595,7 @@ public final void testStringToByteArrayEmptyString() { String s = ""; // Call the method under test. - byte[] result = ByteUtils.stringToByteArray(s); + byte[] result = StringUtils.stringToByteArray(s); // Verify the result. assertThat("Returned byte array must be empty", result.length, is(equalTo(0))); @@ -611,7 +611,7 @@ public final void testStringToByteArray() { byte[] expectedResult = new byte[]{'H', 'e', 'l', 'l', 'o'}; // Call the method under test. - byte[] result = ByteUtils.stringToByteArray(s); + byte[] result = StringUtils.stringToByteArray(s); // Verify the result. assertThat("Returned byte array must be equal to 'expectedResult'", result, is(equalTo(expectedResult))); @@ -629,7 +629,7 @@ public final void testByteArrayToStringNullArray() { exception.expectMessage(is(equalTo("Byte array cannot be null."))); // Call the method under test. - ByteUtils.byteArrayToString(byteArray); + StringUtils.byteArrayToString(byteArray); } /** @@ -642,7 +642,7 @@ public final void testByteArrayToStringEmptyArray() { String expectedResult = ""; // Call the method under test. - String result = ByteUtils.byteArrayToString(byteArray); + String result = StringUtils.byteArrayToString(byteArray); // Verify the result. assertThat("Returned string must be equal to 'expectedResult'", result, is(equalTo(expectedResult))); @@ -658,7 +658,7 @@ public final void testByteArrayToString() { String expectedResult = "Hello"; // Call the method under test. - String result = ByteUtils.byteArrayToString(byteArray); + String result = StringUtils.byteArrayToString(byteArray); // Verify the result. assertThat("Returned string must be equal to 'expectedResult'", result, is(equalTo(expectedResult))); @@ -674,7 +674,7 @@ public final void testStringToByteArrayAndByteArrayToString() { String s = "Hello"; // Call the method under test. - String result = ByteUtils.byteArrayToString(ByteUtils.stringToByteArray(s)); + String result = StringUtils.byteArrayToString(StringUtils.stringToByteArray(s)); // Verify the result. assertThat("Returned string must be equal to initial string", result, is(equalTo(s))); From 5673ebcb69a777586301f3107271d041764dff24 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 12:20:02 +0200 Subject: [PATCH 06/14] Removed mutable objects errors. --- .../digi/xbee/api/packet/common/ExplicitAddressingPacket.java | 2 +- .../digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java | 2 +- .../xbee/api/packet/common/IODataSampleRxIndicatorPacket.java | 2 +- .../main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java | 2 +- .../main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java index 33a71d6b..2efbe9b4 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java @@ -257,7 +257,7 @@ public ExplicitAddressingPacket(int frameID, XBee64BitAddress destAddress64, XBe this.profileID = profileID; this.broadcastRadius = broadcastRadius; this.transmitOptions = transmitOptions; - this.rfData = rfData; + this.rfData = rfData.clone(); this.logger = LoggerFactory.getLogger(ExplicitAddressingPacket.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java index c614e962..a40d5701 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java @@ -202,7 +202,7 @@ public ExplicitRxIndicatorPacket(XBee64BitAddress sourceAddress64, XBee16BitAddr this.clusterID = clusterID; this.profileID = profileID; this.receiveOptions = receiveOptions; - this.rfData = rfData; + this.rfData = rfData.clone(); this.logger = LoggerFactory.getLogger(ExplicitRxIndicatorPacket.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java index 3a198564..428b2888 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java @@ -142,7 +142,7 @@ public IODataSampleRxIndicatorPacket(XBee64BitAddress sourceAddress64, XBee16Bit this.sourceAddress64 = sourceAddress64; this.sourceAddress16 = sourceAddress16; this.receiveOptions = receiveOptions; - this.rfData = rfData; + this.rfData = rfData.clone(); if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); else diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java index 6ffad28e..e775cf49 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java @@ -134,7 +134,7 @@ public RX16IOPacket(XBee16BitAddress sourceAddress16, int rssi, int receiveOptio this.sourceAddress16 = sourceAddress16; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData; + this.rfData = rfData.clone(); if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); else diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java index 5f2378e6..e32fde55 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java @@ -134,7 +134,7 @@ public RX64IOPacket(XBee64BitAddress sourceAddress64, int rssi, int receiveOptio this.sourceAddress64 = sourceAddress64; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData; + this.rfData = rfData.clone(); if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); else From b51f2b82bb3a50774f3f08dc0bf3fba2b3c97092 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 13:47:30 +0200 Subject: [PATCH 07/14] Fixed null pointer errors on tests after non mutable data changes. --- .../digi/xbee/api/packet/common/ExplicitAddressingPacket.java | 3 ++- .../digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java | 3 ++- .../xbee/api/packet/common/IODataSampleRxIndicatorPacket.java | 3 ++- .../main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java | 3 ++- .../main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java index 2efbe9b4..678bc659 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java @@ -257,7 +257,8 @@ public ExplicitAddressingPacket(int frameID, XBee64BitAddress destAddress64, XBe this.profileID = profileID; this.broadcastRadius = broadcastRadius; this.transmitOptions = transmitOptions; - this.rfData = rfData.clone(); + if (rfData != null) + this.rfData = rfData.clone(); this.logger = LoggerFactory.getLogger(ExplicitAddressingPacket.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java index a40d5701..daf4dacc 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java @@ -202,7 +202,8 @@ public ExplicitRxIndicatorPacket(XBee64BitAddress sourceAddress64, XBee16BitAddr this.clusterID = clusterID; this.profileID = profileID; this.receiveOptions = receiveOptions; - this.rfData = rfData.clone(); + if (rfData != null) + this.rfData = rfData.clone(); this.logger = LoggerFactory.getLogger(ExplicitRxIndicatorPacket.class); } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java index 428b2888..93219ea1 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/IODataSampleRxIndicatorPacket.java @@ -142,7 +142,8 @@ public IODataSampleRxIndicatorPacket(XBee64BitAddress sourceAddress64, XBee16Bit this.sourceAddress64 = sourceAddress64; this.sourceAddress16 = sourceAddress16; this.receiveOptions = receiveOptions; - this.rfData = rfData.clone(); + if (rfData != null) + this.rfData = rfData.clone(); if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); else diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java index e775cf49..cf3a9609 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX16IOPacket.java @@ -134,7 +134,8 @@ public RX16IOPacket(XBee16BitAddress sourceAddress16, int rssi, int receiveOptio this.sourceAddress16 = sourceAddress16; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData.clone(); + if (rfData != null) + this.rfData = rfData.clone(); if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); else diff --git a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java index e32fde55..ad83dce5 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/raw/RX64IOPacket.java @@ -134,7 +134,8 @@ public RX64IOPacket(XBee64BitAddress sourceAddress64, int rssi, int receiveOptio this.sourceAddress64 = sourceAddress64; this.rssi = rssi; this.receiveOptions = receiveOptions; - this.rfData = rfData.clone(); + if (rfData != null) + this.rfData = rfData.clone(); if (rfData != null && rfData.length >= 5) ioSample = new IOSample(rfData); else From 3741f3de0757bf442ad35213d5dc056f8960b8d6 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 14:10:59 +0200 Subject: [PATCH 08/14] Added a check before casting to Data Reader. --- .../src/main/java/com/digi/xbee/api/connection/DataReader.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/src/main/java/com/digi/xbee/api/connection/DataReader.java b/library/src/main/java/com/digi/xbee/api/connection/DataReader.java index 6a01401a..bce63ca7 100644 --- a/library/src/main/java/com/digi/xbee/api/connection/DataReader.java +++ b/library/src/main/java/com/digi/xbee/api/connection/DataReader.java @@ -589,6 +589,8 @@ public RemoteXBeeDevice getRemoteXBeeDeviceFromPacket(XBeeAPIPacket packet) thro } break; case EXPLICIT_RX_INDICATOR: + if (!(apiPacket instanceof ExplicitRxIndicatorPacket)) + return remoteDevice; ExplicitRxIndicatorPacket explicitDataPacket = (ExplicitRxIndicatorPacket)apiPacket; addr64 = explicitDataPacket.get64BitSourceAddress(); addr16 = explicitDataPacket.get16BitSourceAddress(); From 062f32e716395fe13d216dde18790bd21789b67f Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 16:36:21 +0200 Subject: [PATCH 09/14] Unified get16bit* and get64bit* address methods to be consistent replacing all upper case B in the code. --- .../main/java/com/digi/xbee/api/XBeeDevice.java | 4 ++-- .../com/digi/xbee/api/connection/DataReader.java | 8 ++++---- .../digi/xbee/api/models/XBeePacketsQueue.java | 2 +- .../packet/common/ExplicitAddressingPacket.java | 8 ++++---- .../packet/common/ExplicitRxIndicatorPacket.java | 4 ++-- .../listeners/IDataReceiveListenerXBeeTest.java | 4 ++-- .../IExplicitDataReceiveListenerTest.java | 2 +- .../xbee/api/models/XBeePacketsQueueTest.java | 6 +++--- .../common/ExplicitAddressingPacketTest.java | 16 ++++++++-------- .../common/ExplicitRxIndicatorPacketTest.java | 16 ++++++++-------- 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/XBeeDevice.java b/library/src/main/java/com/digi/xbee/api/XBeeDevice.java index d8388640..1379dded 100644 --- a/library/src/main/java/com/digi/xbee/api/XBeeDevice.java +++ b/library/src/main/java/com/digi/xbee/api/XBeeDevice.java @@ -1901,12 +1901,12 @@ private ExplicitXBeeMessage readExplicitDataPacket(RemoteXBeeDevice remoteXBeeDe // Obtain the necessary data from the packet. ExplicitRxIndicatorPacket explicitDataPacket = (ExplicitRxIndicatorPacket)xbeePacket; - RemoteXBeeDevice remoteDevice = getNetwork().getDevice(explicitDataPacket.get64BitSourceAddress()); + RemoteXBeeDevice remoteDevice = getNetwork().getDevice(explicitDataPacket.get64bitSourceAddress()); if (remoteDevice == null) { if (remoteXBeeDevice != null) remoteDevice = remoteXBeeDevice; else - remoteDevice = new RemoteXBeeDevice(this, explicitDataPacket.get64BitSourceAddress()); + remoteDevice = new RemoteXBeeDevice(this, explicitDataPacket.get64bitSourceAddress()); getNetwork().addRemoteDevice(remoteDevice); } int sourceEndpoint = explicitDataPacket.getSourceEndpoint(); diff --git a/library/src/main/java/com/digi/xbee/api/connection/DataReader.java b/library/src/main/java/com/digi/xbee/api/connection/DataReader.java index bce63ca7..f207d4a1 100644 --- a/library/src/main/java/com/digi/xbee/api/connection/DataReader.java +++ b/library/src/main/java/com/digi/xbee/api/connection/DataReader.java @@ -485,8 +485,8 @@ private void packetReceived(XBeePacket packet) { clusterID == ExplicitRxIndicatorPacket.DATA_CLUSTER && profileID == ExplicitRxIndicatorPacket.DIGI_PROFILE) { notifyDataReceived(new XBeeMessage(remoteDevice, data, apiPacket.isBroadcast())); - xbeePacketsQueue.addPacket(new ReceivePacket(explicitDataPacket.get64BitSourceAddress(), - explicitDataPacket.get16BitSourceAddress(), + xbeePacketsQueue.addPacket(new ReceivePacket(explicitDataPacket.get64bitSourceAddress(), + explicitDataPacket.get16bitSourceAddress(), explicitDataPacket.getReceiveOptions(), explicitDataPacket.getRFData())); } @@ -592,8 +592,8 @@ public RemoteXBeeDevice getRemoteXBeeDeviceFromPacket(XBeeAPIPacket packet) thro if (!(apiPacket instanceof ExplicitRxIndicatorPacket)) return remoteDevice; ExplicitRxIndicatorPacket explicitDataPacket = (ExplicitRxIndicatorPacket)apiPacket; - addr64 = explicitDataPacket.get64BitSourceAddress(); - addr16 = explicitDataPacket.get16BitSourceAddress(); + addr64 = explicitDataPacket.get64bitSourceAddress(); + addr16 = explicitDataPacket.get16bitSourceAddress(); remoteDevice = network.getDevice(addr64); break; default: diff --git a/library/src/main/java/com/digi/xbee/api/models/XBeePacketsQueue.java b/library/src/main/java/com/digi/xbee/api/models/XBeePacketsQueue.java index 0fca0c81..5bfae6ae 100644 --- a/library/src/main/java/com/digi/xbee/api/models/XBeePacketsQueue.java +++ b/library/src/main/java/com/digi/xbee/api/models/XBeePacketsQueue.java @@ -384,7 +384,7 @@ private boolean addressesMatch(XBeePacket xbeePacket, RemoteXBeeDevice remoteXBe return true; break; case EXPLICIT_RX_INDICATOR: - if (((ExplicitRxIndicatorPacket)xbeePacket).get64BitSourceAddress().equals(remoteXBeeDevice.get64BitAddress())) + if (((ExplicitRxIndicatorPacket)xbeePacket).get64bitSourceAddress().equals(remoteXBeeDevice.get64BitAddress())) return true; break; default: diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java index 678bc659..7de0742f 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacket.java @@ -303,8 +303,8 @@ public boolean needsAPIFrameID() { */ @Override public boolean isBroadcast() { - if (get64BitDestinationAddress().equals(XBee64BitAddress.BROADCAST_ADDRESS) - || get16BitDestinationAddress().equals(XBee16BitAddress.BROADCAST_ADDRESS)) + if (get64bitDestinationAddress().equals(XBee64BitAddress.BROADCAST_ADDRESS) + || get16bitDestinationAddress().equals(XBee16BitAddress.BROADCAST_ADDRESS)) return true; return false; } @@ -316,7 +316,7 @@ public boolean isBroadcast() { * * @see com.digi.xbee.api.models.XBee64BitAddress */ - public XBee64BitAddress get64BitDestinationAddress() { + public XBee64BitAddress get64bitDestinationAddress() { return destAddress64; } @@ -327,7 +327,7 @@ public XBee64BitAddress get64BitDestinationAddress() { * * @see com.digi.xbee.api.models.XBee16BitAddress */ - public XBee16BitAddress get16BitDestinationAddress() { + public XBee16BitAddress get16bitDestinationAddress() { return destAddress16; } diff --git a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java index daf4dacc..79b0e75a 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacket.java @@ -259,7 +259,7 @@ public boolean isBroadcast() { * * @see com.digi.xbee.api.models.XBee64BitAddress */ - public XBee64BitAddress get64BitSourceAddress() { + public XBee64BitAddress get64bitSourceAddress() { return sourceAddress64; } @@ -270,7 +270,7 @@ public XBee64BitAddress get64BitSourceAddress() { * * @see com.digi.xbee.api.models.XBee16BitAddress */ - public XBee16BitAddress get16BitSourceAddress() { + public XBee16BitAddress get16bitSourceAddress() { return sourceAddress16; } diff --git a/library/src/test/java/com/digi/xbee/api/listeners/IDataReceiveListenerXBeeTest.java b/library/src/test/java/com/digi/xbee/api/listeners/IDataReceiveListenerXBeeTest.java index b3b3c754..d31ed7e1 100644 --- a/library/src/test/java/com/digi/xbee/api/listeners/IDataReceiveListenerXBeeTest.java +++ b/library/src/test/java/com/digi/xbee/api/listeners/IDataReceiveListenerXBeeTest.java @@ -81,8 +81,8 @@ public static void setupOnce() throws Exception { explicitPacket = Mockito.mock(ExplicitRxIndicatorPacket.class); Mockito.when(explicitPacket.getFrameType()).thenReturn(APIFrameType.EXPLICIT_RX_INDICATOR); Mockito.when(explicitPacket.getRFData()).thenReturn(RECEIVED_DATA_BYTES); - Mockito.when(explicitPacket.get64BitSourceAddress()).thenReturn(XBEE_64BIT_ADDRESS); - Mockito.when(explicitPacket.get16BitSourceAddress()).thenReturn(XBee16BitAddress.UNKNOWN_ADDRESS); + Mockito.when(explicitPacket.get64bitSourceAddress()).thenReturn(XBEE_64BIT_ADDRESS); + Mockito.when(explicitPacket.get16bitSourceAddress()).thenReturn(XBee16BitAddress.UNKNOWN_ADDRESS); Mockito.when(explicitPacket.isBroadcast()).thenReturn(false); // Mock an invalid packet. diff --git a/library/src/test/java/com/digi/xbee/api/listeners/IExplicitDataReceiveListenerTest.java b/library/src/test/java/com/digi/xbee/api/listeners/IExplicitDataReceiveListenerTest.java index 18b4b23e..e775bbff 100644 --- a/library/src/test/java/com/digi/xbee/api/listeners/IExplicitDataReceiveListenerTest.java +++ b/library/src/test/java/com/digi/xbee/api/listeners/IExplicitDataReceiveListenerTest.java @@ -75,7 +75,7 @@ public static void setupOnce() throws Exception { // Mock Explicit Data Packet. explicitDataPacket = Mockito.mock(ExplicitRxIndicatorPacket.class); Mockito.when(explicitDataPacket.getFrameType()).thenReturn(APIFrameType.EXPLICIT_RX_INDICATOR); - Mockito.when(explicitDataPacket.get64BitSourceAddress()).thenReturn(XBEE_64BIT_ADDRESS); + Mockito.when(explicitDataPacket.get64bitSourceAddress()).thenReturn(XBEE_64BIT_ADDRESS); Mockito.when(explicitDataPacket.getSourceEndpoint()).thenReturn(RECEIVED_SOURCE_ENDPOINT); Mockito.when(explicitDataPacket.getDestinationEndpoint()).thenReturn(RECEIVED_DESTINATION_ENDPOINT); Mockito.when(explicitDataPacket.getClusterID()).thenReturn(RECEIVED_CLUSTER_ID); diff --git a/library/src/test/java/com/digi/xbee/api/models/XBeePacketsQueueTest.java b/library/src/test/java/com/digi/xbee/api/models/XBeePacketsQueueTest.java index 5d32c0a1..f5fe11b4 100644 --- a/library/src/test/java/com/digi/xbee/api/models/XBeePacketsQueueTest.java +++ b/library/src/test/java/com/digi/xbee/api/models/XBeePacketsQueueTest.java @@ -259,7 +259,7 @@ public void testGetFirstPacketFrom() { // Add 2 additional packets from different senders. Mockito.when(mockedRxIO64Packet.get64bitSourceAddress()).thenReturn(xbee64BitAddress2); xbeePacketsQueue.addPacket(mockedRxIO64Packet); - Mockito.when(mockedExplicitRxIndicatorPacket.get64BitSourceAddress()).thenReturn(xbee64BitAddress3); + Mockito.when(mockedExplicitRxIndicatorPacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress3); xbeePacketsQueue.addPacket(mockedExplicitRxIndicatorPacket); // Request the first packet from the queue sent by the mocked remote device and @@ -335,7 +335,7 @@ public void testGetFirstDataPacketFrom() { // Add additional (non-data) packets from the same sender. Mockito.when(mockedRxIO64Packet.get64bitSourceAddress()).thenReturn(xbee64BitAddress1); xbeePacketsQueue.addPacket(mockedRxIO64Packet); - Mockito.when(mockedExplicitRxIndicatorPacket.get64BitSourceAddress()).thenReturn(xbee64BitAddress1); + Mockito.when(mockedExplicitRxIndicatorPacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress1); xbeePacketsQueue.addPacket(mockedExplicitRxIndicatorPacket); // Request the first data packet from the queue sent by the mocked remote device and @@ -407,7 +407,7 @@ public void testGetFirstExplicitDataPacketFrom() { xbeePacketsQueue.addPacket(Mockito.mock(XBeePacket.class)); // Add an explicit data packet from the mocked 64-bit address. - Mockito.when(mockedExplicitRxIndicatorPacket.get64BitSourceAddress()).thenReturn(xbee64BitAddress1); + Mockito.when(mockedExplicitRxIndicatorPacket.get64bitSourceAddress()).thenReturn(xbee64BitAddress1); xbeePacketsQueue.addPacket(mockedExplicitRxIndicatorPacket); // Add a data packet from the mocked 64-bit address. diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacketTest.java index 54bb3478..6d77a946 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitAddressingPacketTest.java @@ -226,8 +226,8 @@ public final void testCreatePacketValidPayloadWithoutData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); - assertThat("Returned destination 64-bit address is not the expected one", packet.get64BitDestinationAddress(), is(equalTo(dest64Addr))); - assertThat("Returned destination 16-bit address is not the expected one", packet.get16BitDestinationAddress(), is(equalTo(dest16Addr))); + assertThat("Returned destination 64-bit address is not the expected one", packet.get64bitDestinationAddress(), is(equalTo(dest64Addr))); + assertThat("Returned destination 16-bit address is not the expected one", packet.get16bitDestinationAddress(), is(equalTo(dest16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); @@ -281,8 +281,8 @@ public final void testCreatePacketValidPayloadWithData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); - assertThat("Returned destination 64-bit address is not the expected one", packet.get64BitDestinationAddress(), is(equalTo(dest64Addr))); - assertThat("Returned destination 16-bit address is not the expected one", packet.get16BitDestinationAddress(), is(equalTo(dest16Addr))); + assertThat("Returned destination 64-bit address is not the expected one", packet.get64bitDestinationAddress(), is(equalTo(dest64Addr))); + assertThat("Returned destination 16-bit address is not the expected one", packet.get16bitDestinationAddress(), is(equalTo(dest16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); @@ -757,8 +757,8 @@ public final void testCreateExplicitAddressingPacketValidWithData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(expectedLength))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); - assertThat("Returned destination 64-bit address is not the expected one", packet.get64BitDestinationAddress(), is(equalTo(dest64Addr))); - assertThat("Returned destination 16-bit address is not the expected one", packet.get16BitDestinationAddress(), is(equalTo(dest16Addr))); + assertThat("Returned destination 64-bit address is not the expected one", packet.get64bitDestinationAddress(), is(equalTo(dest64Addr))); + assertThat("Returned destination 16-bit address is not the expected one", packet.get16bitDestinationAddress(), is(equalTo(dest16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); @@ -797,8 +797,8 @@ public final void testCreateExplicitAddressingPacketValidWithoutData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(expectedLength))); assertThat("Frame ID is not the expected one", packet.getFrameID(), is(equalTo(frameID))); - assertThat("Returned destination 64-bit address is not the expected one", packet.get64BitDestinationAddress(), is(equalTo(dest64Addr))); - assertThat("Returned destination 16-bit address is not the expected one", packet.get16BitDestinationAddress(), is(equalTo(dest16Addr))); + assertThat("Returned destination 64-bit address is not the expected one", packet.get64bitDestinationAddress(), is(equalTo(dest64Addr))); + assertThat("Returned destination 16-bit address is not the expected one", packet.get16bitDestinationAddress(), is(equalTo(dest16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); diff --git a/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacketTest.java b/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacketTest.java index 3e1131f6..2bfaa8b4 100644 --- a/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacketTest.java +++ b/library/src/test/java/com/digi/xbee/api/packet/common/ExplicitRxIndicatorPacketTest.java @@ -212,8 +212,8 @@ public final void testCreatePacketValidPayloadWithoutData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); - assertThat("Returned source 64-bit address is not the expected one", packet.get64BitSourceAddress(), is(equalTo(source64Addr))); - assertThat("Returned source 16-bit address is not the expected one", packet.get16BitSourceAddress(), is(equalTo(source16Addr))); + assertThat("Returned source 64-bit address is not the expected one", packet.get64bitSourceAddress(), is(equalTo(source64Addr))); + assertThat("Returned source 16-bit address is not the expected one", packet.get16bitSourceAddress(), is(equalTo(source16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); @@ -261,8 +261,8 @@ public final void testCreatePacketValidPayloadWithData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(payload.length))); - assertThat("Returned source 64-bit address is not the expected one", packet.get64BitSourceAddress(), is(equalTo(source64Addr))); - assertThat("Returned source 16-bit address is not the expected one", packet.get16BitSourceAddress(), is(equalTo(source16Addr))); + assertThat("Returned source 64-bit address is not the expected one", packet.get64bitSourceAddress(), is(equalTo(source64Addr))); + assertThat("Returned source 16-bit address is not the expected one", packet.get16bitSourceAddress(), is(equalTo(source16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); @@ -599,8 +599,8 @@ public final void testCreateExplicitRxIndicatorPacketValidWithData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(expectedLength))); - assertThat("Returned source 64-bit address is not the expected one", packet.get64BitSourceAddress(), is(equalTo(source64Addr))); - assertThat("Returned source 16-bit address is not the expected one", packet.get16BitSourceAddress(), is(equalTo(source16Addr))); + assertThat("Returned source 64-bit address is not the expected one", packet.get64bitSourceAddress(), is(equalTo(source64Addr))); + assertThat("Returned source 16-bit address is not the expected one", packet.get16bitSourceAddress(), is(equalTo(source16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); @@ -635,8 +635,8 @@ public final void testCreateExplicitRxIndicatorPacketValidWithoutData() { // Verify the result. assertThat("Returned length is not the expected one", packet.getPacketLength(), is(equalTo(expectedLength))); - assertThat("Returned source 64-bit address is not the expected one", packet.get64BitSourceAddress(), is(equalTo(source64Addr))); - assertThat("Returned source 16-bit address is not the expected one", packet.get16BitSourceAddress(), is(equalTo(source16Addr))); + assertThat("Returned source 64-bit address is not the expected one", packet.get64bitSourceAddress(), is(equalTo(source64Addr))); + assertThat("Returned source 16-bit address is not the expected one", packet.get16bitSourceAddress(), is(equalTo(source16Addr))); assertThat("Returned source endpoint is not the expected one", packet.getSourceEndpoint(), is(equalTo(sourceEndpoint))); assertThat("Returned destination endpoint is not the expected one", packet.getDestinationEndpoint(), is(equalTo(destEndpoint))); assertThat("Returned cluster ID is not the expected one", packet.getClusterID(), is(equalTo(clusterID))); From 87eabff34d0e58ff9a528797a2cb30776b868fee Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 16:37:54 +0200 Subject: [PATCH 10/14] Added locale to the toUpperCase method in the HexUtils to remove coverity error. --- library/src/main/java/com/digi/xbee/api/utils/HexUtils.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java b/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java index 4d540550..52e42132 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/HexUtils.java @@ -11,6 +11,8 @@ */ package com.digi.xbee.api.utils; +import java.util.Locale; + /** * Utility class containing methods to work with hexadecimal values and several * data type conversions. @@ -146,7 +148,7 @@ public static String prettyHexString(String hexString) { if (hexString == null) throw new NullPointerException("Hexadecimal string cannot be null."); - String copy = hexString.toUpperCase(); + String copy = hexString.toUpperCase(Locale.getDefault()); for (final char c : copy.toCharArray()) { if (!HEXES.contains(""+c)) throw new IllegalArgumentException("Given string cannot contain non-hexadecimal characters."); From 008ece1a1ad14ebba36781642ca0d7859ca28a26 Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 16:38:52 +0200 Subject: [PATCH 11/14] Removed unnecessary AND in the bit check of ByteUtils to fix coverity error. --- library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java b/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java index c9b3f00b..e6e1d81d 100644 --- a/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java +++ b/library/src/main/java/com/digi/xbee/api/utils/ByteUtils.java @@ -259,7 +259,7 @@ public static boolean isBitEnabled(int containerInteger, int bitPosition) { if (bitPosition < 0 || bitPosition > 31) throw new IllegalArgumentException("Bit position must be between 0 and 31."); - return (((containerInteger & 0xFFFFFFFF) >> bitPosition) & 0x01) == 0x01; + return ((containerInteger >> bitPosition) & 0x01) == 0x01; } /** From 97a3617fe1c3d93722aba5c57cc856b5b7c3429d Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 18:12:54 +0200 Subject: [PATCH 12/14] Removed redundant null check from XBeeAPIPacket. --- .../src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java b/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java index 0b58dc1d..da3e09fd 100644 --- a/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java +++ b/library/src/main/java/com/digi/xbee/api/packet/XBeeAPIPacket.java @@ -124,7 +124,7 @@ public byte[] getPacketData() { byte[] apiData = getAPIData(); if (apiData == null) apiData = new byte[0]; - if (apiData != null && apiData.length > 0) { + if (apiData.length > 0) { try { data.write(apiData); } catch (IOException e) { From 43b52815d80f4ec4eadb3e05e0f63372888d82fa Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 18:16:59 +0200 Subject: [PATCH 13/14] Added instance check before casting packet to XBeeAPIPacket in the Node Discovery process. --- library/src/main/java/com/digi/xbee/api/NodeDiscovery.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java b/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java index 4ade79cc..f081100d 100644 --- a/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java +++ b/library/src/main/java/com/digi/xbee/api/NodeDiscovery.java @@ -287,7 +287,8 @@ public void packetReceived(XBeePacket receivedPacket) { if (!discovering) return; RemoteXBeeDevice rdevice = null; - + if (!(receivedPacket instanceof XBeeAPIPacket)) + return; byte[] commandValue = getRemoteDeviceData((XBeeAPIPacket)receivedPacket); rdevice = parseDiscoveryAPIData(commandValue, xbeeDevice); From 87fa7549b96cb8aab4b665ac52329b6678be6f8d Mon Sep 17 00:00:00 2001 From: David Escalona Date: Mon, 14 Sep 2015 18:28:07 +0200 Subject: [PATCH 14/14] Added instance check before casting packet to XBeeAPIPacket in the AbstractXBeeDevice. --- .../com/digi/xbee/api/AbstractXBeeDevice.java | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java b/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java index c0b8dd51..5aa15ab0 100644 --- a/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java +++ b/library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java @@ -1115,33 +1115,34 @@ private IPacketReceiveListener createPacketReceivedListener(final XBeePacket sen @Override public void packetReceived(XBeePacket receivedPacket) { // Check if it is the packet we are waiting for. + if (!(receivedPacket instanceof XBeeAPIPacket)) + return; + if (!(sentPacket instanceof XBeeAPIPacket)) + return; if (((XBeeAPIPacket)receivedPacket).checkFrameID((((XBeeAPIPacket)sentPacket).getFrameID()))) { // Security check to avoid class cast exceptions. It has been observed that parallel processes // using the same connection but with different frame index may collide and cause this exception at some point. - if (sentPacket instanceof XBeeAPIPacket - && receivedPacket instanceof XBeeAPIPacket) { - XBeeAPIPacket sentAPIPacket = (XBeeAPIPacket)sentPacket; - XBeeAPIPacket receivedAPIPacket = (XBeeAPIPacket)receivedPacket; - - // If the packet sent is an AT command, verify that the received one is an AT command response and - // the command matches in both packets. - if (sentAPIPacket.getFrameType() == APIFrameType.AT_COMMAND) { - if (receivedAPIPacket.getFrameType() != APIFrameType.AT_COMMAND_RESPONSE) + XBeeAPIPacket sentAPIPacket = (XBeeAPIPacket)sentPacket; + XBeeAPIPacket receivedAPIPacket = (XBeeAPIPacket)receivedPacket; + + // If the packet sent is an AT command, verify that the received one is an AT command response and + // the command matches in both packets. + if (sentAPIPacket.getFrameType() == APIFrameType.AT_COMMAND) { + if (receivedAPIPacket.getFrameType() != APIFrameType.AT_COMMAND_RESPONSE) + return; + if (sentAPIPacket instanceof ATCommandPacket && receivedPacket instanceof ATCommandResponsePacket) { + if (!((ATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((ATCommandResponsePacket)receivedPacket).getCommand())) return; - if (sentAPIPacket instanceof ATCommandPacket && receivedPacket instanceof ATCommandResponsePacket) { - if (!((ATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((ATCommandResponsePacket)receivedPacket).getCommand())) - return; - } } - // If the packet sent is a remote AT command, verify that the received one is a remote AT command response and - // the command matches in both packets. - if (sentAPIPacket.getFrameType() == APIFrameType.REMOTE_AT_COMMAND_REQUEST) { - if (receivedAPIPacket.getFrameType() != APIFrameType.REMOTE_AT_COMMAND_RESPONSE) + } + // If the packet sent is a remote AT command, verify that the received one is a remote AT command response and + // the command matches in both packets. + if (sentAPIPacket.getFrameType() == APIFrameType.REMOTE_AT_COMMAND_REQUEST) { + if (receivedAPIPacket.getFrameType() != APIFrameType.REMOTE_AT_COMMAND_RESPONSE) + return; + if (sentAPIPacket instanceof RemoteATCommandPacket && receivedPacket instanceof RemoteATCommandResponsePacket) { + if (!((RemoteATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((RemoteATCommandResponsePacket)receivedPacket).getCommand())) return; - if (sentAPIPacket instanceof RemoteATCommandPacket && receivedPacket instanceof RemoteATCommandResponsePacket) { - if (!((RemoteATCommandPacket)sentAPIPacket).getCommand().equalsIgnoreCase(((RemoteATCommandResponsePacket)receivedPacket).getCommand())) - return; - } } }