Skip to content

Commit 5276412

Browse files
committed
cellular: add support for XBee Cellular API frames
* TX SMS, RX SMS, TX IPv4 and RX IPv4 frames. * NetworkProtocol enumeration. * IP32BitAddress class. * Unit tests for all classes. https://jira.digi.com/browse/XBJAPI-292 Signed-off-by: Ruben Moral <Ruben.Moral@digi.com>
1 parent 260faad commit 5276412

File tree

13 files changed

+4779
-0
lines changed

13 files changed

+4779
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Copyright (c) 2016 Digi International Inc.,
3+
* All rights not expressly granted are reserved.
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
* You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*
9+
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
10+
* =======================================================================
11+
*/
12+
package com.digi.xbee.api.models;
13+
14+
import java.util.Arrays;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
17+
18+
19+
/**
20+
* This class represents a 32-bit IP address used in some protocols where each
21+
* radio module has its own 32 bit IP address.
22+
*
23+
* <p>This address is only applicable for:</p>
24+
* <ul>
25+
* <li>Cellular</li>
26+
* </ul>
27+
*/
28+
public class IP32BitAddress {
29+
30+
// Constants
31+
private static final String ERROR_IP_NULL = "IP address cannot be null.";
32+
private static final String ERROR_IP_TOO_LONG = "IP address cannot be longer than 4 bytes.";
33+
private static final String ERROR_IP_INVALID = "Invalid IP address.";
34+
35+
private static final int HASH_SEED = 23;
36+
37+
public final static String IP_V4_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
38+
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
39+
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
40+
+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
41+
42+
// Variables
43+
private byte[] address;
44+
45+
/**
46+
* Class constructor. Instantiates a new object of type
47+
* {@code IP32BitAddress} with the given parameters.
48+
*
49+
* @param address The 32 bit IP address as byte array.
50+
*
51+
* @throws IllegalArgumentException If {@code address.length > 4}.
52+
* @throws NullPointerException If {@code address == null}.
53+
*/
54+
public IP32BitAddress(byte[] address) {
55+
if (address == null)
56+
throw new NullPointerException(ERROR_IP_NULL);
57+
if (address.length > 4)
58+
throw new IllegalArgumentException(ERROR_IP_TOO_LONG);
59+
60+
generateIPAddress(address);
61+
}
62+
63+
/**
64+
* Class constructor. Instantiates a new object of type
65+
* {@code IP32BitAddress} with the given parameters.
66+
*
67+
* @param address The 32 bit IP address as integer array.
68+
*
69+
* @throws IllegalArgumentException If {@code address.length > 8}.
70+
* @throws NullPointerException If {@code address == null}.
71+
*/
72+
public IP32BitAddress(int[] address) {
73+
if (address == null)
74+
throw new NullPointerException(ERROR_IP_NULL);
75+
if (address.length > 4)
76+
throw new IllegalArgumentException(ERROR_IP_TOO_LONG);
77+
78+
byte[] byteAddress = new byte[address.length];
79+
for (int i = 0; i < address.length; i++)
80+
byteAddress[i] = (byte)address[i];
81+
82+
generateIPAddress(byteAddress);
83+
}
84+
85+
/**
86+
* Class constructor. Instantiates a new object of type
87+
* {@code IP32BitAddress} with the given parameters.
88+
*
89+
* @param address The 32 bit IP address as string.
90+
*
91+
* @throws IllegalArgumentException If the given address doesn't match the
92+
* IP address pattern.
93+
* @throws NullPointerException If {@code address == null}.
94+
*/
95+
public IP32BitAddress(String address) {
96+
if (address == null)
97+
throw new NullPointerException(ERROR_IP_NULL);
98+
99+
Pattern p = Pattern.compile(IP_V4_PATTERN);
100+
Matcher m = p.matcher(address);
101+
if (!m.matches())
102+
throw new IllegalArgumentException(ERROR_IP_INVALID);
103+
104+
byte[] byteAddress = new byte[4];
105+
106+
byteAddress[0] = (byte)Integer.parseInt(m.group(1));
107+
byteAddress[1] = (byte)Integer.parseInt(m.group(2));
108+
byteAddress[2] = (byte)Integer.parseInt(m.group(3));
109+
byteAddress[3] = (byte)Integer.parseInt(m.group(4));
110+
111+
generateIPAddress(byteAddress);
112+
}
113+
114+
/**
115+
* Generates and saves the IP byte address based on the given byte array.
116+
*
117+
* @param byteAddress The byte array used to generate the final IP byte
118+
* address.
119+
*/
120+
private void generateIPAddress(byte[] byteAddress) {
121+
this.address = new byte[4];
122+
123+
int diff = 4 - byteAddress.length;
124+
for (int i = 0; i < diff; i++)
125+
this.address[i] = 0;
126+
for (int i = diff; i < 4; i++)
127+
this.address[i] = byteAddress[i - diff];
128+
}
129+
130+
/**
131+
* Retrieves the 32 bit IP address value as byte array.
132+
*
133+
* @return 32 bit IP address value as byte array.
134+
*/
135+
public byte[] getValue() {
136+
return address;
137+
}
138+
139+
/**
140+
* Retrieves the 32 bit IP address value as string.
141+
*
142+
* @return 32 bit IP address value as string.
143+
*/
144+
public String getValueString() {
145+
String prettyIP = "";
146+
for (int i = 0; i < address.length; i++) {
147+
if (i != 0)
148+
prettyIP += ".";
149+
prettyIP += address[i] & 0xFF;
150+
}
151+
152+
return prettyIP;
153+
}
154+
155+
/*
156+
* (non-Javadoc)
157+
* @see java.lang.Object#equals(java.lang.Object)
158+
*/
159+
@Override
160+
public boolean equals(Object obj) {
161+
if (obj == null)
162+
return false;
163+
if (!(obj instanceof IP32BitAddress))
164+
return false;
165+
IP32BitAddress addr = (IP32BitAddress)obj;
166+
return Arrays.equals(addr.getValue(), getValue());
167+
}
168+
169+
/*
170+
* (non-Javadoc)
171+
* @see java.lang.Object#hashCode()
172+
*/
173+
@Override
174+
public int hashCode() {
175+
int hash = HASH_SEED;
176+
for (byte b:getValue())
177+
hash = hash * (hash + b);
178+
return hash;
179+
}
180+
181+
/*
182+
* (non-Javadoc)
183+
* @see java.lang.Object#toString()
184+
*/
185+
@Override
186+
public String toString() {
187+
return getValueString();
188+
}
189+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright (c) 2016 Digi International Inc.,
3+
* All rights not expressly granted are reserved.
4+
*
5+
* This Source Code Form is subject to the terms of the Mozilla Public
6+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
7+
* You can obtain one at http://mozilla.org/MPL/2.0/.
8+
*
9+
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
10+
* =======================================================================
11+
*/
12+
package com.digi.xbee.api.models;
13+
14+
import java.util.HashMap;
15+
16+
/**
17+
* Enumerates the different network protocols.
18+
*/
19+
public enum NetworkProtocol {
20+
21+
// Enumeration types.
22+
UDP(0, "UDP"),
23+
TCP(1, "TCP"),
24+
TCP_SSL(4, "TCP SSL");
25+
26+
// Variables.
27+
private int id;
28+
29+
private String name;
30+
31+
private static HashMap<Integer, NetworkProtocol> lookupTable = new HashMap<Integer, NetworkProtocol>();
32+
33+
static {
34+
for (NetworkProtocol protocol:values())
35+
lookupTable.put(protocol.getID(), protocol);
36+
}
37+
38+
/**
39+
* Class constructor. Instantiates a new {@code NetworkProtocol} enumeration
40+
* entry with the given parameters.
41+
*
42+
* @param id Network protocol ID.
43+
* @param name Network protocol name.
44+
*/
45+
private NetworkProtocol(int id, String name) {
46+
this.id = id;
47+
this.name = name;
48+
}
49+
50+
/**
51+
* Retrieves the network protocol ID.
52+
*
53+
* @return Network protocol ID.
54+
*/
55+
public int getID() {
56+
return id;
57+
}
58+
59+
/**
60+
* Retrieves the network protocol name.
61+
*
62+
* @return Network protocol name.
63+
*/
64+
public String getName() {
65+
return name;
66+
}
67+
68+
/**
69+
* Retrieves the network protocol for the given ID.
70+
*
71+
* @param id ID to retrieve the network protocol.
72+
*
73+
* @return The network protocol associated with the given ID.
74+
*/
75+
public static NetworkProtocol get(int id) {
76+
return lookupTable.get(id);
77+
}
78+
79+
/*
80+
* (non-Javadoc)
81+
* @see java.lang.Enum#toString()
82+
*/
83+
@Override
84+
public String toString() {
85+
return name;
86+
}
87+
}

library/src/main/java/com/digi/xbee/api/packet/APIFrameType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public enum APIFrameType {
3131
TRANSMIT_REQUEST (0x10, "Transmit Request"),
3232
EXPLICIT_ADDRESSING_COMMAND_FRAME (0x11, "Explicit Addressing Command Frame"),
3333
REMOTE_AT_COMMAND_REQUEST (0x17, "Remote AT Command Request"),
34+
TX_SMS (0x1F, "TX SMS"),
35+
TX_IPV4 (0x20, "TX IPv4"),
3436
RX_64 (0x80, "RX (Receive) Packet 64-bit Address"),
3537
RX_16 (0x81, "RX (Receive) Packet 16-bit Address"),
3638
RX_IO_64 (0x82, "IO Data Sample RX 64-bit Address Indicator"),
@@ -43,6 +45,8 @@ public enum APIFrameType {
4345
EXPLICIT_RX_INDICATOR (0x91, "Explicit RX Indicator"),
4446
IO_DATA_SAMPLE_RX_INDICATOR (0x92, "IO Data Sample RX Indicator"),
4547
REMOTE_AT_COMMAND_RESPONSE (0x97, "Remote Command Response"),
48+
RX_SMS (0x9F, "RX SMS"),
49+
RX_IPV4 (0xB0, "RX IPv4"),
4650
GENERIC (0xFF, "Generic");
4751

4852
// Variables.

0 commit comments

Comments
 (0)