Skip to content
Merged
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: 0 additions & 1 deletion library/src/main/java/com/digi/xbee/api/IPDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.digi.xbee.api.models.XBee64BitAddress;
import com.digi.xbee.api.models.XBeeMessage;
import com.digi.xbee.api.models.XBeePacketsQueue;
import com.digi.xbee.api.models.XBeeTransmitOptions;
import com.digi.xbee.api.packet.XBeeAPIPacket;
import com.digi.xbee.api.packet.XBeePacket;
import com.digi.xbee.api.packet.ip.RXIPv4Packet;
Expand Down
142 changes: 71 additions & 71 deletions library/src/main/java/com/digi/xbee/api/WiFiDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@
public class WiFiDevice extends IPDevice {

// Constants.
private final static int DEFAULT_WIFI_RECEIVE_TIMETOUT = 15000; // 15 seconds of timeout to connect, disconnect and discover access points.
private final static int DEFAULT_ACCESS_POINT_TIMETOUT = 15000; // 15 seconds of timeout to connect, disconnect and scan access points.

private static final String AS_COMMAND = "AS";
private static final String ERROR_ALREADY_CONNECTED = "Device is already connected to an access point.";

private static final int DISCOVER_TIMEOUT = 30000;

// Variables.
private boolean discoveringAccessPoints = false;
private boolean discoveringAccessPointsError = false;
private boolean scanningAccessPoints = false;
private boolean scanningAccessPointsError = false;

protected int wifiReceiveTimeout = DEFAULT_WIFI_RECEIVE_TIMETOUT;
protected int accessPointTimeout = DEFAULT_ACCESS_POINT_TIMETOUT;

/**
* Class constructor. Instantiates a new {@code WiFiDevice} object in
Expand Down Expand Up @@ -189,14 +189,14 @@ public WiFiAssociationIndicationStatus getWiFiAssociationIndicationStatus() thro
}

/**
* Discovers and reports the access point that matches the supplied SSID.
* Finds and reports the access point that matches the supplied SSID.
*
* <p>This method blocks until the access point is discovered or the
* configured Wi-Fi receive timeout expires.</p>
* configured access point timeout expires.</p>
*
* <p>The Wi-Fi receive timeout is configured using the
* {@code setWiFiReceiveTimeout} method and can be consulted with
* {@code getWiFiReceiveTimeout} method.</p>
* <p>The access point timeout is configured using the
* {@code setAccessPointTimeout} method and can be consulted with
* {@code getAccessPointTimeout} method.</p>
*
* @param ssid The SSID of the access point to discover.
*
Expand All @@ -208,17 +208,17 @@ public WiFiAssociationIndicationStatus getWiFiAssociationIndicationStatus() thro
* @throws TimeoutException if there is a timeout getting the access point.
* @throws XBeeException if there is an error sending the discovery command.
*
* @see #discoverAccessPoints()
* @see #getWiFiReceiveTimeout()
* @see #setWiFiReceiveTimeout(int)
* @see #getAccessPointTimeout()
* @see #scanAccessPoints()
* @see #setAccessPointTimeout(int)
*/
public AccessPoint getAccessPoint(String ssid) throws XBeeException {
if (ssid == null)
throw new NullPointerException("SSID cannot be null.");

logger.debug("{}AS for '{}' access point.", toString(), ssid);

List<AccessPoint> accessPointsList = discoverAccessPoints();
List<AccessPoint> accessPointsList = scanAccessPoints();

for (AccessPoint accessPoint:accessPointsList) {
if (accessPoint.getSSID().equals(ssid))
Expand All @@ -229,26 +229,26 @@ public AccessPoint getAccessPoint(String ssid) throws XBeeException {
}

/**
* Performs a discovery to search for access points in the vicinity.
* Performs a scan to search for access points in the vicinity.
*
* <p>This method blocks until all the access points are discovered or the
* configured Wi-Fi receive timeout expires.</p>
* configured access point timeout expires.</p>
*
* <p>The Wi-Fi receive timeout is configured using the
* {@code setWiFiReceiveTimeout} method and can be consulted with
* {@code getWiFiReceiveTimeout} method.</p>
* <p>The access point timeout is configured using the
* {@code setAccessPointTimeout} method and can be consulted with
* {@code getAccessPointTimeout} method.</p>
*
* @return The list of access points discovered.
*
* @throws InterfaceNotOpenException if this device connection is not open.
* @throws TimeoutException if there is a timeout discovering the access points.
* @throws TimeoutException if there is a timeout scanning the access points.
* @throws XBeeException if there is any other XBee related exception.
*
* @see #getAccessPoint(String)
* @see #getWiFiReceiveTimeout()
* @see #setWiFiReceiveTimeout(int)
* @see #getAccessPointTimeout()
* @see #setAccessPointTimeout(int)
*/
public List<AccessPoint> discoverAccessPoints() throws XBeeException {
public List<AccessPoint> scanAccessPoints() throws XBeeException {
// Check if the connection is open.
if (!isOpen())
throw new InterfaceNotOpenException();
Expand All @@ -270,7 +270,7 @@ public List<AccessPoint> discoverAccessPoints() throws XBeeException {
*/
@Override
public void packetReceived(XBeePacket receivedPacket) {
if (!discoveringAccessPoints)
if (!scanningAccessPoints)
return;

try {
Expand All @@ -282,12 +282,12 @@ public void packetReceived(XBeePacket receivedPacket) {

// Check for error.
if (response.getStatus() == ATCommandStatus.ERROR) {
discoveringAccessPointsError = true;
discoveringAccessPoints = false;
scanningAccessPointsError = true;
scanningAccessPoints = false;
// Check for end of discovery.
} else if (response.getCommandValue() == null
|| response.getCommandValue().length == 0)
discoveringAccessPoints = false;
scanningAccessPoints = false;
else {
AccessPoint accessPoint = parseDiscoveredAccessPoint(response.getCommandValue());
if (accessPoint != null)
Expand All @@ -299,30 +299,30 @@ public void packetReceived(XBeePacket receivedPacket) {
}
};

logger.debug("{}Start discovering access points.", toString());
logger.debug("{}Start scanning access points.", toString());
addPacketListener(packetReceiveListener);

try {
discoveringAccessPoints = true;
scanningAccessPoints = true;
// Send the active scan command.
sendPacketAsync(new ATCommandPacket(getNextFrameID(), AS_COMMAND, ""));

// Wait until the discovery process finishes or timeouts.
long deadLine = System.currentTimeMillis() + DISCOVER_TIMEOUT;
while (discoveringAccessPoints && System.currentTimeMillis() < deadLine)
while (scanningAccessPoints && System.currentTimeMillis() < deadLine)
sleep(100);

// Check if we exited because of a timeout.
if (discoveringAccessPoints)
if (scanningAccessPoints)
throw new TimeoutException();
// Check if there was an error in the active scan command (device is already connected).
if (discoveringAccessPointsError)
if (scanningAccessPointsError)
throw new XBeeException(ERROR_ALREADY_CONNECTED);
} finally {
discoveringAccessPoints = false;
discoveringAccessPointsError = false;
scanningAccessPoints = false;
scanningAccessPointsError = false;
removePacketListener(packetReceiveListener);
logger.debug("{}Stop discovering access points.", toString());
logger.debug("{}Stop scanning access points.", toString());
}
}

Expand All @@ -333,11 +333,11 @@ public void packetReceived(XBeePacket receivedPacket) {
* Connects to the access point with provided SSID.
*
* <p>This method blocks until the connection with the access point is
* established or the configured Wi-Fi receive timeout expires.</p>
* established or the configured access point timeout expires.</p>
*
* <p>The Wi-Fi receive timeout is configured using the
* {@code setWiFiReceiveTimeout} method and can be consulted with
* {@code getWiFiReceiveTimeout} method.</p>
* <p>The access point timeout is configured using the
* {@code setAccessPointTimeout} method and can be consulted with
* {@code getAccessPointTimeout} method.</p>
*
* <p>Once the module is connected to the access point, you can issue
* the {@link #writeChanges()} method to save the connection settings. This
Expand All @@ -360,10 +360,10 @@ public void packetReceived(XBeePacket receivedPacket) {
*
* @see #connect(AccessPoint, String)
* @see #disconnect()
* @see #discoverAccessPoints()
* @see #getAccessPoint(String)
* @see #getWiFiReceiveTimeout()
* @see #setWiFiReceiveTimeout(int)
* @see #getAccessPointTimeout()
* @see #scanAccessPoints()
* @see #setAccessPointTimeout(int)
*/
public boolean connect(String ssid, String password)
throws TimeoutException, XBeeException {
Expand All @@ -381,11 +381,11 @@ public boolean connect(String ssid, String password)
* Connects to the provided access point.
*
* <p>This method blocks until the connection with the access point is
* established or the configured Wi-Fi receive timeout expires.</p>
* established or the configured access point timeout expires.</p>
*
* <p>The Wi-Fi receive timeout is configured using the
* {@code setWiFiReceiveTimeout} method and can be consulted with
* {@code getWiFiReceiveTimeout} method.</p>
* <p>The access point timeout is configured using the
* {@code setAccessPointTimeout} method and can be consulted with
* {@code getAccessPointTimeout} method.</p>
*
* <p>Once the module is connected to the access point, you can issue
* the {@code writeSettings} method to save the connection settings. This
Expand All @@ -407,10 +407,10 @@ public boolean connect(String ssid, String password)
*
* @see #connect(String, String)
* @see #disconnect()
* @see #discoverAccessPoints()
* @see #getAccessPoint(String)
* @see #getWiFiReceiveTimeout()
* @see #setWiFiReceiveTimeout(int)
* @see #getAccessPointTimeout()
* @see #scanAccessPoints()
* @see #setAccessPointTimeout(int)
* @see com.digi.xbee.api.models.AccessPoint
*/
public boolean connect(AccessPoint accessPoint, String password)
Expand All @@ -425,7 +425,7 @@ public boolean connect(AccessPoint accessPoint, String password)
setParameter("PK", password.getBytes());

// Wait for the module to connect to the access point.
long deadLine = System.currentTimeMillis() + wifiReceiveTimeout;
long deadLine = System.currentTimeMillis() + accessPointTimeout;
while (System.currentTimeMillis() < deadLine) {
sleep(100);
// Get the association indication value of the module.
Expand All @@ -443,11 +443,11 @@ public boolean connect(AccessPoint accessPoint, String password)
* Disconnects from the access point the device is connected to.
*
* <p>This method blocks until the device disconnects totally from the
* access point or the configured Wi-Fi receive timeout expires.</p>
* access point or the configured access point timeout expires.</p>
*
* <p>The Wi-Fi receive timeout is configured using the
* {@code setWiFiReceiveTimeout} method and can be consulted with
* {@code getWiFiReceiveTimeout} method.</p>
* <p>The access point timeout is configured using the
* {@code setAccessPointTimeout} method and can be consulted with
* {@code getAccessPointTimeout} method.</p>
*
* @return {@code true} if the module disconnected from the access point
* successfully, {@code false} otherwise.
Expand All @@ -459,13 +459,13 @@ public boolean connect(AccessPoint accessPoint, String password)
*
* @see #connect(AccessPoint, String, int)
* @see #connect(String, String, int)
* @see #getWiFiReceiveTimeout()
* @see #setWiFiReceiveTimeout(int)
* @see #getAccessPointTimeout()
* @see #setAccessPointTimeout(int)
*/
public boolean disconnect() throws TimeoutException, XBeeException {
executeParameter("NR");
// Wait for the module to connect to the access point.
long deadLine = System.currentTimeMillis() + wifiReceiveTimeout;
long deadLine = System.currentTimeMillis() + accessPointTimeout;
while (System.currentTimeMillis() < deadLine) {
sleep(100);
// Get the association indication value of the module.
Expand Down Expand Up @@ -598,32 +598,32 @@ private void sleep(int milliseconds) {
}

/**
* Returns the Wi-Fi configured timeout for connecting, disconnecting and
* discovering access points.
* Returns the configured access point timeout for connecting,
* disconnecting and scanning access points.
*
* @return The current Wi-Fi receive timeout in milliseconds.
* @return The current access point timeout in milliseconds.
*
* @see #setWiFiReceiveTimeout(int)
* @see #setAccessPointTimeout(int)
*/
public int getWiFiReceiveTimeout() {
return wifiReceiveTimeout;
public int getAccessPointTimeout() {
return accessPointTimeout;
}

/**
* Configures the Wi-Fi timeout in milliseconds connecting, disconnecting
* and discovering access points.
* Configures the access point timeout in milliseconds for connecting,
* disconnecting and scanning access points.
*
* @param wifiReceiveTimeout The new Wi-Fi receive timeout in milliseconds.
* @param accessPointTimeout The new access point timeout in milliseconds.
*
* @throws IllegalArgumentException if {@code wifiReceiveTimeout < 0}.
* @throws IllegalArgumentException if {@code accessPointTimeout < 0}.
*
* @see #getWiFiReceiveTimeout()
* @see #getAccessPointTimeout()
*/
public void setWiFiReceiveTimeout(int wifiReceiveTimeout) {
if (wifiReceiveTimeout < 0)
throw new IllegalArgumentException("Wi-Fi receive timeout cannot be less than 0.");
public void setAccessPointTimeout(int accessPointTimeout) {
if (accessPointTimeout < 0)
throw new IllegalArgumentException("Access point timeout cannot be less than 0.");

this.wifiReceiveTimeout = wifiReceiveTimeout;
this.accessPointTimeout = accessPointTimeout;
}

/**
Expand Down
Loading