Skip to content

Commit cd2135f

Browse files
diescalohgonzaleDigi
authored andcommitted
thread: add IP protocol restrictions to the send IPv6 data methods
- TCP and TCP SSL protocols are not supported by Thread devices. Signed-off-by: Diego Escalona <diego.escalona@digi.com>
1 parent b670922 commit cd2135f

File tree

3 files changed

+126
-11
lines changed

3 files changed

+126
-11
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
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;
3332
import com.digi.xbee.api.models.IPMessage;
3433
import com.digi.xbee.api.models.IPProtocol;
3534
import com.digi.xbee.api.models.XBee16BitAddress;
@@ -264,7 +263,6 @@ protected IPv6Device(IConnectionInterface connectionInterface) {
264263
super(connectionInterface);
265264
}
266265

267-
268266
/**
269267
* @deprecated This protocol does not support the network functionality.
270268
*/
@@ -313,7 +311,7 @@ public XBee16BitAddress get16BitAddress() {
313311

314312
/**
315313
* @deprecated Operation not supported in this protocol. Use
316-
* {@link #getDestinationIPAddress()} instead.
314+
* {@link #getIPv6DestinationAddress()} instead.
317315
* This method will raise an
318316
* {@link UnsupportedOperationException}.
319317
*/
@@ -326,7 +324,7 @@ public XBee64BitAddress getDestinationAddress() throws TimeoutException,
326324

327325
/**
328326
* @deprecated Operation not supported in this protocol. Use
329-
* {@link #setDestinationIPAddress(Inet6Address)} instead.
327+
* {@link #setIPv6DestinationAddress(Inet6Address)} instead.
330328
* This method will raise an
331329
* {@link UnsupportedOperationException}.
332330
*/
@@ -484,8 +482,7 @@ public void stopListening() throws TimeoutException, XBeeException {
484482

485483
/**
486484
* Sends the provided IPv6 data to the given IPv6 address and port using
487-
* the specified IPv6 protocol. For TCP and TCP SSL protocols, you can
488-
* also indicate if the socket should be closed when data is sent.
485+
* the specified IPv6 protocol.
489486
*
490487
* <p>This method blocks till a success or error response arrives or the
491488
* configured receive timeout expires.</p>
@@ -523,9 +520,7 @@ public void sendIPData(Inet6Address ipv6Address, int destPort,
523520

524521
/**
525522
* Sends the provided IPv6 data to the given IPv6 address and port
526-
* asynchronously using the specified IPv6 protocol. For TCP and TCP SSL
527-
* protocols, you can also indicate if the socket should be closed when
528-
* data is sent.
523+
* asynchronously using the specified IPv6 protocol.
529524
*
530525
* <p>Asynchronous transmissions do not wait for answer from the remote
531526
* device or for transmit status packet.</p>
@@ -555,8 +550,7 @@ public void sendIPDataAsync(Inet6Address ipv6Address, int destPort,
555550

556551
/**
557552
* 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.
553+
* the specified IPv6 protocol.
560554
*
561555
* <p>Transmissions can be performed synchronously or asynchronously.
562556
* Synchronous operation blocks till a success or error response arrives
@@ -580,6 +574,8 @@ public void sendIPDataAsync(Inet6Address ipv6Address, int destPort,
580574
* @param destPort The destination port of the transmission.
581575
* @param protocol The IPv6 protocol used for the transmission.
582576
* @param data Byte array containing the IPv6 data to be sent.
577+
* @param async Boolean that should be set to {@code true} if the
578+
* transmission should be asynchronous, and {@code false} otherwise.
583579
*
584580
* @throws IllegalArgumentException if {@code destPort < 0} or
585581
* if {@code destPort > 65535}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.digi.xbee.api.exceptions.XBeeException;
2828
import com.digi.xbee.api.models.AssociationIndicationStatus;
2929
import com.digi.xbee.api.models.HTTPMethodEnum;
30+
import com.digi.xbee.api.models.IPProtocol;
3031
import com.digi.xbee.api.models.RemoteATCommandOptions;
3132
import com.digi.xbee.api.models.ThreadAssociationIndicationStatus;
3233
import com.digi.xbee.api.models.XBeeProtocol;
@@ -54,6 +55,9 @@ public class ThreadDevice extends IPv6Device {
5455
// Constants
5556
private static final String OPERATION_EXCEPTION = "Operation not supported in Thread protocol.";
5657

58+
private static final String ERROR_PROTOCOL_ILLEGAL = String.format("Protocol must be %s or %s.",
59+
IPProtocol.UDP.getName(), IPProtocol.COAP.getName());
60+
5761
/**
5862
* Class constructor. Instantiates a new {@code ThreadDevice} object in
5963
* the given port name and baud rate.
@@ -633,4 +637,29 @@ private byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
633637
// Check for a transmit status and CoAP RX Response.
634638
return sendAndCheckCoAPPacket(coAPPacket, async);
635639
}
640+
641+
/*
642+
* (non-Javadoc)
643+
* @see com.digi.xbee.api.IPv6Device#sendIPData(java.net.Inet6Address, int, com.digi.xbee.api.models.IPProtocol, byte[])
644+
*/
645+
@Override
646+
public void sendIPData(Inet6Address ipv6Address, int destPort,
647+
IPProtocol protocol, byte[] data) throws TimeoutException,
648+
XBeeException {
649+
if (protocol != IPProtocol.UDP && protocol != IPProtocol.COAP)
650+
throw new IllegalArgumentException(ERROR_PROTOCOL_ILLEGAL);
651+
super.sendIPData(ipv6Address, destPort, protocol, data);
652+
}
653+
654+
/*
655+
* (non-Javadoc)
656+
* @see com.digi.xbee.api.IPv6Device#sendIPDataAsync(java.net.Inet6Address, int, com.digi.xbee.api.models.IPProtocol, byte[])
657+
*/
658+
@Override
659+
public void sendIPDataAsync(Inet6Address ipv6Address, int destPort,
660+
IPProtocol protocol, byte[] data) throws XBeeException {
661+
if (protocol != IPProtocol.UDP && protocol != IPProtocol.COAP)
662+
throw new IllegalArgumentException(ERROR_PROTOCOL_ILLEGAL);
663+
super.sendIPDataAsync(ipv6Address, destPort, protocol, data);
664+
}
636665
}

library/src/test/java/com/digi/xbee/api/ThreadDeviceTest.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.*;
2121

2222
import java.io.IOException;
23+
import java.net.Inet6Address;
2324

2425
import org.junit.Before;
2526
import org.junit.Rule;
@@ -37,6 +38,7 @@
3738
import com.digi.xbee.api.exceptions.InvalidOperatingModeException;
3839
import com.digi.xbee.api.exceptions.TimeoutException;
3940
import com.digi.xbee.api.exceptions.XBeeException;
41+
import com.digi.xbee.api.models.IPProtocol;
4042
import com.digi.xbee.api.models.ThreadAssociationIndicationStatus;
4143
import com.digi.xbee.api.models.XBee64BitAddress;
4244

@@ -408,4 +410,92 @@ public void testIsConnectedSuccessDisconnected() throws XBeeException, IOExcepti
408410
// Check the connection.
409411
assertFalse(threadDevice.isConnected());
410412
}
413+
414+
/**
415+
* Test method for {@link com.digi.xbee.api.ThreadDevice#sendIPData(Inet6Address, int, IPProtocol, byte[])
416+
*
417+
* <p>Verify that TCP protocol is not supported when sending IPv6 data synchronously.</p>
418+
*
419+
* @throws Exception
420+
*/
421+
@Test
422+
public void testSendIPDataProtocolIllegalTCP() throws Exception {
423+
// Set up the resources for the test.
424+
Inet6Address address = (Inet6Address) Inet6Address.getByName("FDB3:0001:0002:0000:0004:0005:0006:0007");
425+
byte[] data = "Hello XBee".getBytes();
426+
int destPort = 1234;
427+
428+
exception.expect(IllegalArgumentException.class);
429+
exception.expectMessage(is(equalTo(String.format("Protocol must be %s or %s.",
430+
IPProtocol.UDP.getName(), IPProtocol.COAP.getName()))));
431+
432+
// Call the method under test that should throw an IllegalArgumentException.
433+
threadDevice.sendIPData(address, destPort, IPProtocol.TCP, data);
434+
}
435+
436+
/**
437+
* Test method for {@link com.digi.xbee.api.ThreadDevice#sendIPData(Inet6Address, int, IPProtocol, byte[])
438+
*
439+
* <p>Verify that TCP SSL protocol is not supported when sending IPv6 data synchronously.</p>
440+
*
441+
* @throws Exception
442+
*/
443+
@Test
444+
public void testSendIPDataProtocolIllegalTCPSSL() throws Exception {
445+
// Set up the resources for the test.
446+
Inet6Address address = (Inet6Address) Inet6Address.getByName("FDB3:0001:0002:0000:0004:0005:0006:0007");
447+
byte[] data = "Hello XBee".getBytes();
448+
int destPort = 1234;
449+
450+
exception.expect(IllegalArgumentException.class);
451+
exception.expectMessage(is(equalTo(String.format("Protocol must be %s or %s.",
452+
IPProtocol.UDP.getName(), IPProtocol.COAP.getName()))));
453+
454+
// Call the method under test that should throw an IllegalArgumentException.
455+
threadDevice.sendIPData(address, destPort, IPProtocol.TCP_SSL, data);
456+
}
457+
458+
/**
459+
* Test method for {@link com.digi.xbee.api.ThreadDevice#sendIPDataAsync(Inet6Address, int, IPProtocol, byte[])
460+
*
461+
* <p>Verify that TCP protocol is not supported when sending IPv6 data asynchronously.</p>
462+
*
463+
* @throws Exception
464+
*/
465+
@Test
466+
public void testSendIPDataAsyncProtocolIllegalTCP() throws Exception {
467+
// Set up the resources for the test.
468+
Inet6Address address = (Inet6Address) Inet6Address.getByName("FDB3:0001:0002:0000:0004:0005:0006:0007");
469+
byte[] data = "Hello XBee".getBytes();
470+
int destPort = 1234;
471+
472+
exception.expect(IllegalArgumentException.class);
473+
exception.expectMessage(is(equalTo(String.format("Protocol must be %s or %s.",
474+
IPProtocol.UDP.getName(), IPProtocol.COAP.getName()))));
475+
476+
// Call the method under test that should throw an IllegalArgumentException.
477+
threadDevice.sendIPDataAsync(address, destPort, IPProtocol.TCP, data);
478+
}
479+
480+
/**
481+
* Test method for {@link com.digi.xbee.api.ThreadDevice#sendIPDataAsync(Inet6Address, int, IPProtocol, byte[])
482+
*
483+
* <p>Verify that TCP SSL protocol is not supported when sending IPv6 data asynchronously.</p>
484+
*
485+
* @throws Exception
486+
*/
487+
@Test
488+
public void testSendIPDataAsyncProtocolIllegalTCPSSL() throws Exception {
489+
// Set up the resources for the test.
490+
Inet6Address address = (Inet6Address) Inet6Address.getByName("FDB3:0001:0002:0000:0004:0005:0006:0007");
491+
byte[] data = "Hello XBee".getBytes();
492+
int destPort = 1234;
493+
494+
exception.expect(IllegalArgumentException.class);
495+
exception.expectMessage(is(equalTo(String.format("Protocol must be %s or %s.",
496+
IPProtocol.UDP.getName(), IPProtocol.COAP.getName()))));
497+
498+
// Call the method under test that should throw an IllegalArgumentException.
499+
threadDevice.sendIPDataAsync(address, destPort, IPProtocol.TCP_SSL, data);
500+
}
411501
}

0 commit comments

Comments
 (0)