Skip to content

Commit 84ac194

Browse files
committed
nbiot: add new protocol and subclass to work with NB-IoT modules
Included also the corresponding unit tests. Signed-off-by: Ruben Moral <Ruben.Moral@digi.com>
1 parent 8907d05 commit 84ac194

File tree

12 files changed

+1110
-53
lines changed

12 files changed

+1110
-53
lines changed

examples/configuration/ResetModuleSample/ReadMe.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
--------------------
2828
* 802.15.4
2929
* Cellular
30+
* Cellular NB-IoT
3031
* DigiMesh
3132
* Point-to-Multipoint
3233
* Wi-Fi

examples/io/LocalADCSample/ReadMe.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
--------------------
3232
* 802.15.4
3333
* Cellular
34+
* Cellular NB-IoT
3435
* DigiMesh
3536
* Point-to-Multipoint
3637
* Wi-Fi

examples/io/LocalDIOSample/ReadMe.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
--------------------
3636
* 802.15.4
3737
* Cellular
38+
* Cellular NB-IoT
3839
* DigiMesh
3940
* Point-to-Multipoint
4041
* Wi-Fi

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public CellularDevice(IConnectionInterface connectionInterface) {
268268
@Override
269269
public void open() throws XBeeException {
270270
super.open();
271-
if (xbeeProtocol != XBeeProtocol.CELLULAR)
271+
if (xbeeProtocol != XBeeProtocol.CELLULAR && xbeeProtocol != XBeeProtocol.CELLULAR_NBIOT)
272272
throw new XBeeDeviceException("XBee device is not a " + getXBeeProtocol().getDescription() + " device, it is a " + xbeeProtocol.getDescription() + " device.");
273273
}
274274

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

Lines changed: 134 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -600,30 +600,7 @@ public void stopListening() throws TimeoutException, XBeeException {
600600
public void sendIPData(Inet4Address ipAddress, int destPort,
601601
IPProtocol protocol, boolean closeSocket, byte[] data)
602602
throws TimeoutException, XBeeException {
603-
if (ipAddress == null)
604-
throw new NullPointerException("IP address cannot be null");
605-
if (data == null)
606-
throw new NullPointerException("Data cannot be null");
607-
608-
if (destPort < 0 || destPort > 65535)
609-
throw new IllegalArgumentException("Destination port must be between 0 and 65535.");
610-
611-
// Check if device is remote.
612-
if (isRemote())
613-
throw new OperationNotSupportedException("Cannot send IP data from a remote device.");
614-
615-
// The source port value depends on the protocol used in the transmission. For UDP, source port
616-
// value must be the same as 'C0' one. For TCP it must be 0.
617-
int sourcePort = this.sourcePort;
618-
if (protocol != IPProtocol.UDP)
619-
sourcePort = 0;
620-
621-
logger.debug(toString() + "Sending IP data to {}:{} >> {}.", ipAddress, destPort, HexUtils.prettyHexString(data));
622-
623-
XBeePacket xbeePacket = new TXIPv4Packet(getNextFrameID(), ipAddress, destPort,
624-
sourcePort, protocol, closeSocket ? TXIPv4Packet.OPTIONS_CLOSE_SOCKET: TXIPv4Packet.OPTIONS_LEAVE_SOCKET_OPEN, data);
625-
626-
sendAndCheckXBeePacket(xbeePacket, false);
603+
sendIPDataImpl(ipAddress, destPort, protocol, closeSocket, data);
627604
}
628605

629606
/**
@@ -664,7 +641,7 @@ public void sendIPData(Inet4Address ipAddress, int destPort,
664641
*/
665642
public void sendIPData(Inet4Address ipAddress, int destPort, IPProtocol protocol, byte[] data)
666643
throws TimeoutException, XBeeException {
667-
sendIPData(ipAddress, destPort, protocol, false, data);
644+
sendIPDataImpl(ipAddress, destPort, protocol, false, data);
668645
}
669646

670647
/**
@@ -701,29 +678,7 @@ public void sendIPData(Inet4Address ipAddress, int destPort, IPProtocol protocol
701678
*/
702679
public void sendIPDataAsync(Inet4Address ipAddress, int destPort,
703680
IPProtocol protocol, boolean closeSocket, byte[] data) throws XBeeException {
704-
if (ipAddress == null)
705-
throw new NullPointerException("IP address cannot be null");
706-
if (data == null)
707-
throw new NullPointerException("Data cannot be null");
708-
if (destPort < 0 || destPort > 65535)
709-
throw new IllegalArgumentException("Destination port must be between 0 and 65535.");
710-
711-
// Check if device is remote.
712-
if (isRemote())
713-
throw new OperationNotSupportedException("Cannot send IP data from a remote device.");
714-
715-
// The source port value depends on the protocol used in the transmission. For UDP, source port
716-
// value must be the same as 'C0' one. For TCP it must be 0.
717-
int sourcePort = this.sourcePort;
718-
if (protocol != IPProtocol.UDP)
719-
sourcePort = 0;
720-
721-
logger.debug(toString() + "Sending IP data asynchronously to {}:{} >> {}.", ipAddress, destPort, HexUtils.prettyHexString(data));
722-
723-
XBeePacket xbeePacket = new TXIPv4Packet(getNextFrameID(), ipAddress, destPort, sourcePort,
724-
protocol, closeSocket ? TXIPv4Packet.OPTIONS_CLOSE_SOCKET: TXIPv4Packet.OPTIONS_LEAVE_SOCKET_OPEN, data);
725-
726-
sendAndCheckXBeePacket(xbeePacket, true);
681+
sendIPDataAsyncImpl(ipAddress, destPort, protocol, closeSocket, data);
727682
}
728683

729684
/**
@@ -756,7 +711,7 @@ public void sendIPDataAsync(Inet4Address ipAddress, int destPort,
756711
*/
757712
public void sendIPDataAsync(Inet4Address ipAddress, int destPort,
758713
IPProtocol protocol, byte[] data) throws TimeoutException, XBeeException {
759-
sendIPDataAsync(ipAddress, destPort, protocol, false, data);
714+
sendIPDataAsyncImpl(ipAddress, destPort, protocol, false, data);
760715
}
761716

762717
/**
@@ -1026,4 +981,134 @@ private IPMessage readIPDataPacket(Inet4Address remoteIPAddress, int timeout) {
1026981
// Create and return the IP message.
1027982
return new IPMessage(ipAddress, sourcePort, destPort, protocol, data);
1028983
}
984+
985+
/**
986+
* Sends the provided IP data to the given IP address and port using
987+
* the specified IP protocol. For TCP and TCP SSL protocols, you can
988+
* also indicate if the socket should be closed when data is sent.
989+
*
990+
* <p>This method blocks till a success or error response arrives or the
991+
* configured receive timeout expires.</p>
992+
*
993+
* <p>The receive timeout is configured using the {@code setReceiveTimeout}
994+
* method and can be consulted with {@code getReceiveTimeout} method.</p>
995+
*
996+
* @param ipAddress The IP address to send IP data to.
997+
* @param destPort The destination port of the transmission.
998+
* @param protocol The IP protocol used for the transmission.
999+
* @param closeSocket {@code true} to close the socket just after the
1000+
* transmission. {@code false} to keep it open.
1001+
* @param data Byte array containing the IP data to be sent.
1002+
*
1003+
* @throws IllegalArgumentException if {@code destPort < 0} or
1004+
* if {@code destPort > 65535}
1005+
* @throws InterfaceNotOpenException if this device connection is not open.
1006+
* @throws NullPointerException if {@code ipAddress == null} or
1007+
* if {@code protocol == null} or
1008+
* if {@code data == null}.
1009+
* @throws TimeoutException if there is a timeout sending the data.
1010+
* @throws XBeeException if there is any other XBee related exception.
1011+
*
1012+
* @see #getReceiveTimeout()
1013+
* @see #sendBroadcastIPData(int, byte[])
1014+
* @see #sendIPData(Inet4Address, int, IPProtocol, byte[])
1015+
* @see #sendIPData(Inet4Address, int, IPProtocol, boolean, byte[])
1016+
* @see #sendIPDataAsync(Inet4Address, int, IPProtocol, byte[])
1017+
* @see #sendIPDataAsync(Inet4Address, int, IPProtocol, boolean, byte[])
1018+
* @see #setReceiveTimeout(int)
1019+
* @see com.digi.xbee.api.models.IPProtocol
1020+
* @see java.net.Inet4Address
1021+
*/
1022+
private void sendIPDataImpl(Inet4Address ipAddress, int destPort,
1023+
IPProtocol protocol, boolean closeSocket, byte[] data)
1024+
throws TimeoutException, XBeeException {
1025+
if (ipAddress == null)
1026+
throw new NullPointerException("IP address cannot be null");
1027+
if (protocol == null)
1028+
throw new NullPointerException("Protocol cannot be null");
1029+
if (data == null)
1030+
throw new NullPointerException("Data cannot be null");
1031+
1032+
if (destPort < 0 || destPort > 65535)
1033+
throw new IllegalArgumentException("Destination port must be between 0 and 65535.");
1034+
1035+
// Check if device is remote.
1036+
if (isRemote())
1037+
throw new OperationNotSupportedException("Cannot send IP data from a remote device.");
1038+
1039+
// The source port value depends on the protocol used in the transmission. For UDP, source port
1040+
// value must be the same as 'C0' one. For TCP it must be 0.
1041+
int sourcePort = this.sourcePort;
1042+
if (protocol != IPProtocol.UDP)
1043+
sourcePort = 0;
1044+
1045+
logger.debug(toString() + "Sending IP data to {}:{} >> {}.", ipAddress, destPort, HexUtils.prettyHexString(data));
1046+
1047+
XBeePacket xbeePacket = new TXIPv4Packet(getNextFrameID(), ipAddress, destPort,
1048+
sourcePort, protocol, closeSocket ? TXIPv4Packet.OPTIONS_CLOSE_SOCKET: TXIPv4Packet.OPTIONS_LEAVE_SOCKET_OPEN, data);
1049+
1050+
sendAndCheckXBeePacket(xbeePacket, false);
1051+
}
1052+
1053+
/**
1054+
* Sends the provided IP data to the given IP address and port
1055+
* asynchronously using the specified IP protocol. For TCP and TCP SSL
1056+
* protocols, you can also indicate if the socket should be closed when
1057+
* data is sent.
1058+
*
1059+
* <p>Asynchronous transmissions do not wait for answer from the remote
1060+
* device or for transmit status packet.</p>
1061+
*
1062+
* @param ipAddress The IP address to send IP data to.
1063+
* @param destPort The destination port of the transmission.
1064+
* @param protocol The IP protocol used for the transmission.
1065+
* @param closeSocket {@code true} to close the socket just after the
1066+
* transmission. {@code false} to keep it open.
1067+
* @param data Byte array containing the IP data to be sent.
1068+
*
1069+
* @throws IllegalArgumentException if {@code destPort < 0} or
1070+
* if {@code destPort > 65535}
1071+
* @throws InterfaceNotOpenException if this device connection is not open.
1072+
* @throws NullPointerException if {@code ipAddress == null} or
1073+
* if {@code protocol == null} or
1074+
* if {@code data == null}.
1075+
* @throws TimeoutException if there is a timeout sending the data.
1076+
* @throws XBeeException if there is any other XBee related exception.
1077+
*
1078+
* @see #sendBroadcastIPData(int, byte[])
1079+
* @see #sendIPData(Inet4Address, int, IPProtocol, byte[])
1080+
* @see #sendIPData(Inet4Address, int, IPProtocol, boolean, byte[])
1081+
* @see #sendIPDataAsync(Inet4Address, int, IPProtocol, byte[])
1082+
* @see #sendIPDataAsync(Inet4Address, int, IPProtocol, boolean, byte[])
1083+
* @see com.digi.xbee.api.models.IPProtocol
1084+
* @see java.net.Inet4Address
1085+
*/
1086+
private void sendIPDataAsyncImpl(Inet4Address ipAddress, int destPort,
1087+
IPProtocol protocol, boolean closeSocket, byte[] data) throws XBeeException {
1088+
if (ipAddress == null)
1089+
throw new NullPointerException("IP address cannot be null");
1090+
if (protocol == null)
1091+
throw new NullPointerException("Protocol cannot be null");
1092+
if (data == null)
1093+
throw new NullPointerException("Data cannot be null");
1094+
if (destPort < 0 || destPort > 65535)
1095+
throw new IllegalArgumentException("Destination port must be between 0 and 65535.");
1096+
1097+
// Check if device is remote.
1098+
if (isRemote())
1099+
throw new OperationNotSupportedException("Cannot send IP data from a remote device.");
1100+
1101+
// The source port value depends on the protocol used in the transmission. For UDP, source port
1102+
// value must be the same as 'C0' one. For TCP it must be 0.
1103+
int sourcePort = this.sourcePort;
1104+
if (protocol != IPProtocol.UDP)
1105+
sourcePort = 0;
1106+
1107+
logger.debug(toString() + "Sending IP data asynchronously to {}:{} >> {}.", ipAddress, destPort, HexUtils.prettyHexString(data));
1108+
1109+
XBeePacket xbeePacket = new TXIPv4Packet(getNextFrameID(), ipAddress, destPort, sourcePort,
1110+
protocol, closeSocket ? TXIPv4Packet.OPTIONS_CLOSE_SOCKET: TXIPv4Packet.OPTIONS_LEAVE_SOCKET_OPEN, data);
1111+
1112+
sendAndCheckXBeePacket(xbeePacket, true);
1113+
}
10291114
}

0 commit comments

Comments
 (0)