Skip to content

Commit 687ff79

Browse files
diescalohgonzaleDigi
authored andcommitted
thread: add code review modifications to the thread implementation
Signed-off-by: Diego Escalona <diego.escalona@digi.com>
1 parent 1600a56 commit 687ff79

36 files changed

+462
-308
lines changed

examples/communication/coap/ReceiveCoAPDataSample/src/com/digi/xbee/api/receivecoapdata/MainApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import com.digi.xbee.api.exceptions.XBeeException;
2020

2121
/**
22-
* XBee Java Library Receive IPv6 Data sample application.
22+
* XBee Java Library Receive CoAP Data sample application.
2323
*
24-
* <p>This example registers a listener to manage the received IPv6 data.</p>
24+
* <p>This example registers a listener to manage the received CoAP data.</p>
2525
*
2626
* <p>For a complete description on the example, refer to the 'ReadMe.txt' file
2727
* included in the root directory.</p>

examples/communication/coap/SendCoAPDataSample/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<build>
2020
<sourceDirectory>src</sourceDirectory>
21-
<directory>../../../../target/examples/communication/coap/SendICoAPDataSample</directory>
21+
<directory>../../../../target/examples/communication/coap/SendCoAPDataSample</directory>
2222
<plugins>
2323
<plugin>
2424
<groupId>org.codehaus.mojo</groupId>

examples/communication/ip/SendIPv6DataSample/src/com/digi/xbee/api/sendipv6data/MainApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
/**
2727
* XBee Java Library Send IPv6 Data sample application.
2828
*
29-
* <p>This example sends IP data to another Thread device specified by its IPv6
30-
* address and port number.</p>
29+
* <p>This example sends IPv6 data to another Thread device specified by its
30+
* IPv6 address and port number.</p>
3131
*
3232
* <p>For a complete description on the example, refer to the 'ReadMe.txt' file
3333
* included in the root directory.</p>

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ public AbstractXBeeDevice(XBeeDevice localXBeeDevice, XBee64BitAddress addr64,
414414
* @see #AbstractXBeeDevice(Context, int)
415415
* @see #AbstractXBeeDevice(Context, int, AndroidUSBPermissionListener)
416416
* @see java.net.Inet6Address
417+
*
418+
* @since 1.2.1
417419
*/
418420
public AbstractXBeeDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
419421
this(localXBeeDevice, ipv6Addr, null);
@@ -442,6 +444,8 @@ public AbstractXBeeDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr) {
442444
* @see #AbstractXBeeDevice(Context, int)
443445
* @see #AbstractXBeeDevice(Context, int, AndroidUSBPermissionListener)
444446
* @see java.net.Inet6Address
447+
*
448+
* @since 1.2.1
445449
*/
446450
public AbstractXBeeDevice(XBeeDevice localXBeeDevice, Inet6Address ipv6Addr, String id) {
447451
if (localXBeeDevice == null)
@@ -599,6 +603,8 @@ public XBee64BitAddress get64BitAddress() {
599603
* @return The IPv6 address of this IPv6 device.
600604
*
601605
* @see java.net.Inet6Address
606+
*
607+
* @since 1.2.1
602608
*/
603609
public Inet6Address getIPv6Address() {
604610
return ipv6Address;
@@ -1524,7 +1530,7 @@ else if (receivedPacket instanceof TXStatusPacket)
15241530
* transmission status and CoAP RX Response are success for synchronous
15251531
* transmissions.
15261532
*
1527-
* <p>If the status or the CoAP response are not successful, an
1533+
* <p>If the status or the CoAP response is not successful, an
15281534
* {@code TransmitException} is thrown.</p>
15291535
*
15301536
* @param packet The {@code CoAPTxRequestPacket} to be sent.
@@ -1547,7 +1553,9 @@ else if (receivedPacket instanceof TXStatusPacket)
15471553
* not successful or if there is any other XBee
15481554
* related error.
15491555
*
1550-
* @see com.digi.xbee.api.packet.XBeePacket
1556+
* @see com.digi.xbee.api.packet.thread.CoAPTxRequestPacket
1557+
*
1558+
* @since 1.2.1
15511559
*/
15521560
protected byte[] sendAndCheckCoAPPacket(CoAPTxRequestPacket packet,
15531561
boolean asyncTransmission) throws TransmitException, XBeeException {
@@ -1557,7 +1565,7 @@ protected byte[] sendAndCheckCoAPPacket(CoAPTxRequestPacket packet,
15571565
IPacketReceiveListener listener = createCoAPResponseListener(coapResponsePackets);
15581566
addPacketListener(listener);
15591567

1560-
// Send the XBee packet.
1568+
// Send the CoAP Tx Request packet.
15611569
XBeePacket receivedPacket = null;
15621570
try {
15631571
if (asyncTransmission)
@@ -1594,7 +1602,7 @@ protected byte[] sendAndCheckCoAPPacket(CoAPTxRequestPacket packet,
15941602
}
15951603
if (coapRxPacket == null)
15961604
throw new XBeeException("CoAP response was null.");
1597-
return coapRxPacket.getRfData();
1605+
return coapRxPacket.getData();
15981606
}
15991607

16001608
/**
@@ -1610,6 +1618,8 @@ protected byte[] sendAndCheckCoAPPacket(CoAPTxRequestPacket packet,
16101618
*
16111619
* @see com.digi.xbee.api.listeners.IPacketReceiveListener
16121620
* @see com.digi.xbee.api.packet.thread.CoAPRxResponsePacket
1621+
*
1622+
* @since 1.2.1
16131623
*/
16141624
private IPacketReceiveListener createCoAPResponseListener(final ArrayList<CoAPRxResponsePacket> coapResponsePackets) {
16151625
IPacketReceiveListener listener = new IPacketReceiveListener() {
@@ -1636,6 +1646,8 @@ public void packetReceived(XBeePacket receivedPacket) {
16361646
* @throws XBeeException if CoAP response packet is not received or it is
16371647
* not successful or if there is any other XBee
16381648
* related error.
1649+
*
1650+
* @since 1.2.1
16391651
*/
16401652
private CoAPRxResponsePacket waitForCoAPRxResponsePacket(ArrayList<CoAPRxResponsePacket> coapResponsePackets) throws XBeeException {
16411653
synchronized (coapResponsePackets) {
@@ -2017,13 +2029,15 @@ public XBee64BitAddress getDestinationAddress() throws TimeoutException, XBeeExc
20172029
*
20182030
* @param ipv6Address Destination IPv6 address.
20192031
*
2020-
* @throws NullPointerException if {@code address == null}.
2021-
* @throws TimeoutException if there is a timeout setting the destination
2022-
* address.
2032+
* @throws NullPointerException if {@code ipv6Address == null}.
2033+
* @throws TimeoutException if there is a timeout setting the IPv6
2034+
* destination address.
20232035
* @throws XBeeException if there is any other XBee related exception.
20242036
*
2025-
* @see #getDestinationIPAddress()
2037+
* @see #getIPv6DestinationAddress()
20262038
* @see java.net.Inet6Address
2039+
*
2040+
* @since 1.2.1
20272041
*/
20282042
public void setIPv6DestinationAddress(Inet6Address ipv6Address) throws TimeoutException, XBeeException {
20292043
if (ipv6Address == null)
@@ -2037,12 +2051,14 @@ public void setIPv6DestinationAddress(Inet6Address ipv6Address) throws TimeoutEx
20372051
*
20382052
* @return The configured destination IPv6 address.
20392053
*
2040-
* @throws TimeoutException if there is a timeout reading the destination
2041-
* address.
2054+
* @throws TimeoutException if there is a timeout reading the IPv6
2055+
* destination address.
20422056
* @throws XBeeException if there is any other XBee related exception.
20432057
*
2044-
* @see #setDestinationIPAddress(Inet6Address)
2058+
* @see #setIPv6DestinationAddress(Inet6Address)
20452059
* @see java.net.Inet6Address
2060+
*
2061+
* @since 1.2.1
20462062
*/
20472063
public Inet6Address getIPv6DestinationAddress() throws TimeoutException, XBeeException {
20482064
try {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343
/**
4444
* This class represents a local Cellular device.
4545
*
46-
* @see XBeeDevice
47-
* @see DigiMeshDevice
4846
* @see DigiPointDevice
47+
* @see DigiMeshDevice
4948
* @see Raw802Device
49+
* @see ThreadDevice
5050
* @see WiFiDevice
51+
* @see XBeeDevice
5152
* @see ZigBeeDevice
52-
* @see ThreadDevice
5353
*
5454
* @since 1.2.0
5555
*/

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535
/**
3636
* This class represents a local DigiMesh device.
3737
*
38-
* @see XBeeDevice
39-
* @see DigiMeshDevice
38+
* @see CellularDevice
4039
* @see DigiPointDevice
4140
* @see Raw802Device
41+
* @see ThreadDevice
4242
* @see WiFiDevice
43+
* @see XBeeDevice
4344
* @see ZigBeeDevice
44-
* @see ThreadDevice
4545
*/
4646
public class DigiMeshDevice extends XBeeDevice {
4747

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
/**
3737
* This class represents a local DigiPoint device.
3838
*
39-
* @see XBeeDevice
40-
* @see DigiPointDevice
39+
* @see CellularDevice
4140
* @see DigiMeshDevice
4241
* @see Raw802Device
42+
* @see ThreadDevice
4343
* @see WiFiDevice
44+
* @see XBeeDevice
4445
* @see ZigBeeDevice
45-
* @see ThreadDevice
4646
*/
4747
public class DigiPointDevice extends XBeeDevice {
4848

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

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.digi.xbee.api.exceptions.XBeeException;
3030
import com.digi.xbee.api.listeners.IDataReceiveListener;
3131
import com.digi.xbee.api.listeners.IIPDataReceiveListener;
32+
import com.digi.xbee.api.models.HTTPMethodEnum;
3233
import com.digi.xbee.api.models.IPMessage;
3334
import com.digi.xbee.api.models.IPProtocol;
3435
import com.digi.xbee.api.models.XBee16BitAddress;
@@ -92,9 +93,9 @@ protected IPv6Device(String port, int baudRate) {
9293
* @param port Serial port name where IPv6 device is attached to.
9394
* @param baudRate Serial port baud rate to communicate with the device.
9495
* @param dataBits Serial port data bits.
95-
* @param stopBits Serial port data bits.
96-
* @param parity Serial port data bits.
97-
* @param flowControl Serial port data bits.
96+
* @param stopBits Serial port stop bits.
97+
* @param parity Serial port parity.
98+
* @param flowControl Serial port flow control.
9899
*
99100
* @throws IllegalArgumentException if {@code baudRate < 0} or
100101
* if {@code dataBits < 0} or
@@ -510,40 +511,14 @@ public void stopListening() throws TimeoutException, XBeeException {
510511
* @throws XBeeException if there is any other XBee related exception.
511512
*
512513
* @see #getReceiveTimeout()
513-
* @see #sendIPData(Inet6Address, int, IPProtocol, byte[])
514514
* @see #sendIPDataAsync(Inet6Address, int, IPProtocol, byte[])
515-
* @see #sendIPDataAsync(Inet6Address, int, IPProtocol, boolean, byte[])
516515
* @see #setReceiveTimeout(int)
517516
* @see com.digi.xbee.api.models.IPProtocol
518517
* @see java.net.Inet6Address
519518
*/
520519
public void sendIPData(Inet6Address ipv6Address, int destPort,
521520
IPProtocol protocol, byte[] data) throws TimeoutException, XBeeException {
522-
if (ipv6Address == null)
523-
throw new NullPointerException("IPv6 address cannot be null");
524-
if (data == null)
525-
throw new NullPointerException("Data cannot be null");
526-
527-
if (destPort < 0 || destPort > 65535)
528-
throw new IllegalArgumentException("Destination port must be between 0 and 65535.");
529-
530-
// Check if device is remote.
531-
if (isRemote())
532-
throw new OperationNotSupportedException("Cannot send IPv6 data from a remote device.");
533-
534-
// The source port value depends on the protocol used in the transmission. For UDP, source port
535-
// value must be the same as 'C0' one. For TCP it must be 0.
536-
int sourcePort = this.sourcePort;
537-
if (protocol != IPProtocol.UDP)
538-
sourcePort = 0;
539-
540-
logger.debug(toString() + "Sending IPv6 data to {}:{} >> {}.", ipv6Address,
541-
destPort, HexUtils.prettyHexString(data));
542-
543-
XBeePacket xbeePacket = new TXIPv6Packet(getNextFrameID(), ipv6Address, destPort,
544-
sourcePort, protocol, data);
545-
546-
sendAndCheckXBeePacket(xbeePacket, false);
521+
sendIPData(ipv6Address, destPort, protocol, data, false);
547522
}
548523

549524
/**
@@ -570,13 +545,60 @@ public void sendIPData(Inet6Address ipv6Address, int destPort,
570545
* @throws XBeeException if there is any other XBee related exception.
571546
*
572547
* @see #sendIPData(Inet6Address, int, IPProtocol, byte[])
573-
* @see #sendIPData(Inet6Address, int, IPProtocol, boolean, byte[])
574-
* @see #sendIPDataAsync(Inet6Address, int, IPProtocol, byte[])
575548
* @see com.digi.xbee.api.models.IPProtocol
576549
* @see java.net.Inet6Address
577550
*/
578551
public void sendIPDataAsync(Inet6Address ipv6Address, int destPort,
579552
IPProtocol protocol, byte[] data) throws XBeeException {
553+
sendIPData(ipv6Address, destPort, protocol, data, true);
554+
}
555+
556+
/**
557+
* Sends the provided IPv6 data to the given IPv6 address and port using
558+
* the specified IPv6 protocol. For TCP and TCP SSL protocols, you can
559+
* also indicate if the socket should be closed when data is sent.
560+
*
561+
* <p>Transmissions can be performed synchronously or asynchronously.
562+
* Synchronous operation blocks till a success or error response arrives
563+
* or the configured receive timeout expires. Asynchronous transmissions
564+
* do not wait for answer from the remote device or for transmit status
565+
* packet.</p>
566+
*
567+
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
568+
* method and can be consulted with {@code getReceiveTimeout} method.</p>
569+
*
570+
* <p>For synchronous operations use this method:</p>
571+
* <ul>
572+
* <li>{@link #sendIPData(Inet6Address, int, IPProtocol, byte[])}.</li>
573+
* </ul>
574+
* <p>For asynchronous operations use this one:</p>
575+
* <ul>
576+
* <li>{@link #sendIPDataAsync(Inet6Address, int, IPProtocol, byte[])}.</li>
577+
* </ul>
578+
*
579+
* @param ipv6Address The IPv6 address to send IPv6 data to.
580+
* @param destPort The destination port of the transmission.
581+
* @param protocol The IPv6 protocol used for the transmission.
582+
* @param data Byte array containing the IPv6 data to be sent.
583+
*
584+
* @throws IllegalArgumentException if {@code destPort < 0} or
585+
* if {@code destPort > 65535}
586+
* @throws InterfaceNotOpenException if this device connection is not open.
587+
* @throws NullPointerException if {@code ipv6Address == null} or
588+
* if {@code protocol == null} or
589+
* if {@code data == null}.
590+
* @throws TimeoutException if there is a timeout sending the data.
591+
* @throws XBeeException if there is any other XBee related exception.
592+
*
593+
* @see #getReceiveTimeout()
594+
* @see #sendIPData(Inet6Address, int, IPProtocol, byte[])
595+
* @see #sendIPDataAsync(Inet6Address, int, IPProtocol, byte[])
596+
* @see #setReceiveTimeout(int)
597+
* @see com.digi.xbee.api.models.IPProtocol
598+
* @see java.net.Inet6Address
599+
*/
600+
private void sendIPData(Inet6Address ipv6Address, int destPort,
601+
IPProtocol protocol, byte[] data, boolean async) throws XBeeException {
580602
if (ipv6Address == null)
581603
throw new NullPointerException("IPv6 address cannot be null");
582604
if (data == null)
@@ -594,13 +616,17 @@ public void sendIPDataAsync(Inet6Address ipv6Address, int destPort,
594616
if (protocol != IPProtocol.UDP)
595617
sourcePort = 0;
596618

597-
logger.debug(toString() + "Sending IPv6 data asynchronously to {}:{} >> {}.", ipv6Address,
598-
destPort, HexUtils.prettyHexString(data));
619+
if (async)
620+
logger.debug(toString() + "Sending IPv6 data asynchronously to {}:{} >> {}.", ipv6Address,
621+
destPort, HexUtils.prettyHexString(data));
622+
else
623+
logger.debug(toString() + "Sending IPv6 data to {}:{} >> {}.", ipv6Address,
624+
destPort, HexUtils.prettyHexString(data));
599625

600626
XBeePacket xbeePacket = new TXIPv6Packet(getNextFrameID(), ipv6Address, destPort,
601627
sourcePort, protocol, data);
602628

603-
sendAndCheckXBeePacket(xbeePacket, true);
629+
sendAndCheckXBeePacket(xbeePacket, async);
604630
}
605631

606632
/**
@@ -782,8 +808,8 @@ public IPMessage readIPDataFrom(Inet6Address ipv6Address, int timeout) {
782808
* data package read from the provided IPv6 address.
783809
* </p>
784810
*
785-
* @param remoteIPAddress The IPv6 address to get a IPv6 data packet from.
786-
* {@code null} to read a IPv6 data packet from
811+
* @param remoteIPAddress The IPv6 address to get an IPv6 data packet from.
812+
* {@code null} to read an IPv6 data packet from
787813
* any IPv6 address.
788814
* @param timeout The time to wait for a IPv6 data packet in milliseconds.
789815
*
@@ -795,7 +821,7 @@ public IPMessage readIPDataFrom(Inet6Address ipv6Address, int timeout) {
795821
*
796822
* @throws InterfaceNotOpenException if this device connection is not open.
797823
*
798-
* @see com.digi.xbee.api.models.XBeeMessage
824+
* @see com.digi.xbee.api.models.IPMessage
799825
* @see java.net.Inet6Address
800826
*/
801827
private IPMessage readIPDataPacket(Inet6Address remoteIPAddress, int timeout) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
/**
4040
* This class represents a local 802.15.4 device.
4141
*
42-
* @see XBeeDevice
4342
* @see CellularDevice
44-
* @see 802.15.4Device
4543
* @see DigiPointDevice
44+
* @see DigiMeshDevice
45+
* @see ThreadDevice
4646
* @see WiFiDevice
47+
* @see XBeeDevice
4748
* @see ZigBeeDevice
48-
* @see ThreadDevice
4949
*/
5050
public class Raw802Device extends XBeeDevice {
5151

0 commit comments

Comments
 (0)