Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descalon/coverity fixes #50

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ bin
/target
/.settings
/examples/*/*/.gitignore
*.orig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
55 changes: 32 additions & 23 deletions library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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.
Expand Down Expand Up @@ -377,7 +378,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) {
Expand Down Expand Up @@ -502,7 +503,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;
}
Expand Down Expand Up @@ -1114,27 +1115,32 @@ 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)
return;
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 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 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;
}
Expand All @@ -1145,7 +1151,7 @@ public void packetReceived(XBeePacket receivedPacket) {
if (!sentPacket.equals(receivedPacket)) {
responseList.add(receivedPacket);
synchronized (responseList) {
responseList.notify();
responseList.notifyAll();
}
}
}
Expand Down Expand Up @@ -1826,7 +1832,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,
Expand Down Expand Up @@ -1893,13 +1899,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;
Expand All @@ -1909,7 +1918,7 @@ public void packetReceived(XBeePacket receivedPacket) {

// Continue execution by notifying the lock object.
synchronized (ioLock) {
ioLock.notify();
ioLock.notifyAll();
}
}
};
Expand Down
9 changes: 4 additions & 5 deletions library/src/main/java/com/digi/xbee/api/NodeDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -458,8 +459,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.
Expand All @@ -485,9 +484,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,
Expand Down
18 changes: 10 additions & 8 deletions library/src/main/java/com/digi/xbee/api/XBeeDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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.
Expand Down Expand Up @@ -413,9 +414,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);
Expand All @@ -426,7 +427,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;

Expand Down Expand Up @@ -1403,7 +1404,8 @@ private boolean waitForModemResetStatusPacket() {
addModemStatusListener(resetStatusListener);
synchronized (resetLock) {
try {
resetLock.wait(TIMEOUT_RESET);
while (!modemStatusReceived)
resetLock.wait(TIMEOUT_RESET);
} catch (InterruptedException e) { }
}
removeModemStatusListener(resetStatusListener);
Expand Down Expand Up @@ -1435,7 +1437,7 @@ public void modemStatusEventReceived(ModemStatusEvent modemStatusEvent) {
modemStatusReceived = true;
// Continue execution by notifying the lock object.
synchronized (resetLock) {
resetLock.notify();
resetLock.notifyAll();
}
}
}
Expand Down Expand Up @@ -1899,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();
Expand Down
6 changes: 4 additions & 2 deletions library/src/main/java/com/digi/xbee/api/XBeeNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -748,10 +748,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?
Expand Down
Loading