Skip to content

Commit f133f1a

Browse files
committed
devices: add several methods to read received packets synchronously
- readPacket() - readPacket(timeout) - readPacketFrom(remoteDevice) - readPacketFrom(remoteDevice, timeout) Once a packet has been read with one of these methods, it is removed from the queue and cannot be read again. Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent d71d906 commit f133f1a

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

library/src/main/java/com/digi/xbee/api/AbstractXBeeDevice.java

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,6 +4459,173 @@ protected void softwareReset() throws TimeoutException, XBeeException {
44594459
logger.info(toString() + "Module reset successfully.");
44604460
}
44614461

4462+
/**
4463+
* Reads new packet received by this XBee device during the configured
4464+
* receive timeout.
4465+
*
4466+
* <p>This method blocks until new packet is received or the configured
4467+
* receive timeout expires.</p>
4468+
*
4469+
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
4470+
* method and can be consulted with {@code getReceiveTimeout} method.</p>
4471+
*
4472+
* <p>For non-blocking operations, register a {@code IPacketReceiveListener}
4473+
* using the method {@link #addPacketListener(IPacketReceiveListener)}.</p>
4474+
*
4475+
* @return The received XBee packet, or {@code null} if none was received
4476+
* during the configured timeout.
4477+
*
4478+
* @throws InterfaceNotOpenException if this device connection is not open.
4479+
*
4480+
* @see #getReceiveTimeout()
4481+
* @see #setReceiveTimeout(int)
4482+
* @see #readPacket(int)
4483+
* @see #readPacketFrom(RemoteXBeeDevice)
4484+
* @see #readPacketFrom(RemoteXBeeDevice, int)
4485+
* @see XBeePacket
4486+
*/
4487+
protected XBeePacket readPacket() {
4488+
return readPacket(null, TIMEOUT_READ_PACKET);
4489+
}
4490+
4491+
/**
4492+
* Reads new packet received by this XBee device during the provided
4493+
* timeout.
4494+
*
4495+
* <p>This method blocks until new packet is received or the provided
4496+
* timeout expires.</p>
4497+
*
4498+
* <p>For non-blocking operations, register a {@code IPacketReceiveListener}
4499+
* using the method {@link #addPacketListener(IPacketReceiveListener)}.</p>
4500+
*
4501+
* @param timeout The time to wait for new packet in milliseconds.
4502+
*
4503+
* @return The received XBee packet, or {@code null} if none was received
4504+
* during the provided timeout.
4505+
*
4506+
* @throws IllegalArgumentException if {@code timeout < 0}.
4507+
* @throws InterfaceNotOpenException if this device connection is not open.
4508+
*
4509+
* @see #readPacket()
4510+
* @see #readPacketFrom(RemoteXBeeDevice)
4511+
* @see #readPacketFrom(RemoteXBeeDevice, int)
4512+
* @see XBeePacket
4513+
*/
4514+
protected XBeePacket readPacket(int timeout) {
4515+
if (timeout < 0)
4516+
throw new IllegalArgumentException("Read timeout must be 0 or greater.");
4517+
4518+
return readPacket(null, timeout);
4519+
}
4520+
4521+
/**
4522+
* Reads new packet received from the given remote XBee device during the
4523+
* configured receive timeout.
4524+
*
4525+
* <p>This method blocks until new packet from the provided remote XBee
4526+
* device is received or the configured receive timeout expires.</p>
4527+
*
4528+
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
4529+
* method and can be consulted with {@code getReceiveTimeout} method.</p>
4530+
*
4531+
* <p>For non-blocking operations, register a {@code IPacketReceiveListener}
4532+
* using the method {@link #addPacketListener(IPacketReceiveListener)}.</p>
4533+
*
4534+
* @param remoteXBeeDevice The remote device to read packet from.
4535+
*
4536+
* @return The received XBee packet, or {@code null} if none was received
4537+
* from the given remote device during the configured timeout.
4538+
*
4539+
* @throws InterfaceNotOpenException if this device connection is not open.
4540+
* @throws NullPointerException if {@code remoteXBeeDevice == null}.
4541+
*
4542+
* @see #getReceiveTimeout()
4543+
* @see #setReceiveTimeout(int)
4544+
* @see #readPacket()
4545+
* @see #readPacket(int)
4546+
* @see #readPacketFrom(RemoteXBeeDevice, int)
4547+
* @see XBeePacket
4548+
*/
4549+
protected XBeePacket readPacketFrom(RemoteXBeeDevice remoteXBeeDevice) {
4550+
if (remoteXBeeDevice == null)
4551+
throw new NullPointerException("Remote XBee device cannot be null.");
4552+
4553+
return readPacketFrom(remoteXBeeDevice, TIMEOUT_READ_PACKET);
4554+
}
4555+
4556+
/**
4557+
* Reads new packet received from the given remote XBee device during the
4558+
* provided timeout.
4559+
*
4560+
* <p>This method blocks until new packet from the provided remote XBee
4561+
* device is received or the given timeout expires.</p>
4562+
*
4563+
* <p>For non-blocking operations, register a {@code IPacketReceiveListener}
4564+
* using the method {@link #addPacketListener(IPacketReceiveListener)}.</p>
4565+
*
4566+
* @param remoteXBeeDevice The remote device to read packet from.
4567+
* @param timeout The time to wait for new packet in milliseconds.
4568+
*
4569+
* @return The received XBee packet, or {@code null} if none was received
4570+
* from the given remote device during the provided timeout.
4571+
*
4572+
* @throws IllegalArgumentException if {@code timeout < 0}.
4573+
* @throws InterfaceNotOpenException if this device connection is not open.
4574+
* @throws NullPointerException if {@code remoteXBeeDevice == null}.
4575+
*
4576+
* @see #readPacket()
4577+
* @see #readPacket(int)
4578+
* @see #readPacketFrom(RemoteXBeeDevice)
4579+
* @see XBeePacket
4580+
*/
4581+
protected XBeePacket readPacketFrom(RemoteXBeeDevice remoteXBeeDevice, int timeout) {
4582+
if (remoteXBeeDevice == null)
4583+
throw new NullPointerException("Remote XBee device cannot be null.");
4584+
if (timeout < 0)
4585+
throw new IllegalArgumentException("Read timeout must be 0 or greater.");
4586+
4587+
return readPacket(remoteXBeeDevice, timeout);
4588+
}
4589+
4590+
/**
4591+
* Reads a new packet received by this XBee device during the provided
4592+
* timeout.
4593+
*
4594+
* <p>This method blocks until new packet is received or the given timeout
4595+
* expires.</p>
4596+
*
4597+
* <p>If the provided remote XBee device is {@code null} the method returns
4598+
* the first packet read from any remote device.
4599+
* <br>
4600+
* If the remote device is not {@code null} the method returns the first
4601+
* packet read from the provided device.</p>
4602+
*
4603+
* @param remoteXBeeDevice The remote device to get a packet from.
4604+
* {@code null} to read a packet sent by any
4605+
* remote XBee device.
4606+
* @param timeout The time to wait for a packet in milliseconds.
4607+
*
4608+
* @return The received XBee packet, or {@code null} if none was received
4609+
* from the given remote device during the provided timeout.
4610+
*
4611+
* @throws InterfaceNotOpenException if this device connection is not open.
4612+
*
4613+
* @see RemoteXBeeDevice
4614+
* @see XBeePacket
4615+
*/
4616+
private XBeePacket readPacket(RemoteXBeeDevice remoteXBeeDevice, int timeout) {
4617+
// Check connection.
4618+
if (!connectionInterface.isOpen())
4619+
throw new InterfaceNotOpenException();
4620+
4621+
XBeePacketsQueue xbeePacketsQueue = dataReader.getXBeePacketsQueue();
4622+
4623+
if (remoteXBeeDevice != null)
4624+
return xbeePacketsQueue.getFirstPacketFrom(remoteXBeeDevice, timeout);
4625+
else
4626+
return xbeePacketsQueue.getFirstPacket(timeout);
4627+
}
4628+
44624629
/**
44634630
* Reads new data received by this XBee device during the configured
44644631
* receive timeout.

library/src/main/java/com/digi/xbee/api/XBeeDevice.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,26 @@ public void reset() throws TimeoutException, XBeeException {
295295
softwareReset();
296296
}
297297

298+
@Override
299+
public XBeePacket readPacket() {
300+
return super.readPacket();
301+
}
302+
303+
@Override
304+
public XBeePacket readPacket(int timeout) {
305+
return super.readPacket(timeout);
306+
}
307+
308+
@Override
309+
public XBeePacket readPacketFrom(RemoteXBeeDevice remoteXBeeDevice) {
310+
return super.readPacketFrom(remoteXBeeDevice);
311+
}
312+
313+
@Override
314+
public XBeePacket readPacketFrom(RemoteXBeeDevice remoteXBeeDevice, int timeout) {
315+
return super.readPacketFrom(remoteXBeeDevice, timeout);
316+
}
317+
298318
@Override
299319
public XBeeMessage readData() {
300320
return super.readData();

0 commit comments

Comments
 (0)