[cooja] interfaces/IPAddress: Indention cleanup

This commit is contained in:
Enrico Joerns 2014-08-29 12:11:10 +02:00
parent e5653ac150
commit d5c5198171

View file

@ -26,7 +26,6 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
package org.contikios.cooja.interfaces; package org.contikios.cooja.interfaces;
import java.util.Collection; import java.util.Collection;
@ -53,6 +52,7 @@ import org.contikios.cooja.mote.memory.VarMemory;
*/ */
@ClassDescription("IP Address") @ClassDescription("IP Address")
public class IPAddress extends MoteInterface { public class IPAddress extends MoteInterface {
private static Logger logger = Logger.getLogger(IPAddress.class); private static Logger logger = Logger.getLogger(IPAddress.class);
private final VarMemory moteMem; private final VarMemory moteMem;
@ -95,7 +95,7 @@ public class IPAddress extends MoteInterface {
if (isVersion4()) { if (isVersion4()) {
String ipString = ""; String ipString = "";
byte[] ip = moteMem.getByteArray("uip_hostaddr", 4); byte[] ip = moteMem.getByteArray("uip_hostaddr", 4);
for (int i=0; i < 3; i++) { for (int i = 0; i < 3; i++) {
ipString += (0xFF & ip[i]) + "."; ipString += (0xFF & ip[i]) + ".";
} }
ipString += (0xFF & ip[3]); ipString += (0xFF & ip[3]);
@ -124,65 +124,66 @@ public class IPAddress extends MoteInterface {
} }
public byte[] getIPv6Address() { public byte[] getIPv6Address() {
byte[] ip = null; byte[] ip = null;
/* IpV6: Struct sizes and offsets */ /* IpV6: Struct sizes and offsets */
int ipv6NetworkInterfaceAddressOffset = moteMem.getByteValueOf("uip_ds6_netif_addr_list_offset"); int ipv6NetworkInterfaceAddressOffset = moteMem.getByteValueOf("uip_ds6_netif_addr_list_offset");
int ipv6AddressStructSize = moteMem.getByteValueOf("uip_ds6_addr_size"); int ipv6AddressStructSize = moteMem.getByteValueOf("uip_ds6_addr_size");
if (ipv6NetworkInterfaceAddressOffset == 0 || ipv6AddressStructSize == 0) { if (ipv6NetworkInterfaceAddressOffset == 0 || ipv6AddressStructSize == 0) {
return null; return null;
} }
/* TODO No need to copy the entire array! */ /* TODO No need to copy the entire array! */
byte[] structData = moteMem.getByteArray("uip_ds6_if", byte[] structData = moteMem.getByteArray(
ipv6NetworkInterfaceAddressOffset+IPv6_MAX_ADDRESSES*ipv6AddressStructSize); "uip_ds6_if",
ipv6NetworkInterfaceAddressOffset + IPv6_MAX_ADDRESSES * ipv6AddressStructSize);
ipv6AddressIndex = -1; ipv6AddressIndex = -1;
for (int addressIndex=0; addressIndex < IPv6_MAX_ADDRESSES; addressIndex++) { for (int addressIndex = 0; addressIndex < IPv6_MAX_ADDRESSES; addressIndex++) {
int offset = ipv6NetworkInterfaceAddressOffset+addressIndex*ipv6AddressStructSize; int offset = ipv6NetworkInterfaceAddressOffset + addressIndex * ipv6AddressStructSize;
byte isUsed = structData[offset]; byte isUsed = structData[offset];
if (isUsed == 0) { if (isUsed == 0) {
continue; continue;
} }
byte[] addressData = new byte[16]; byte[] addressData = new byte[16];
System.arraycopy( System.arraycopy(
structData, offset+2/* ipaddr offset */, structData, offset + 2/* ipaddr offset */,
addressData, 0, 16); addressData, 0, 16);
if (addressData[0] == (byte)0xFE && addressData[1] == (byte)0x80) { if (addressData[0] == (byte) 0xFE && addressData[1] == (byte) 0x80) {
ipv6IsGlobal = false; ipv6IsGlobal = false;
} else { } else {
ipv6IsGlobal = true; ipv6IsGlobal = true;
} }
ip = addressData; ip = addressData;
ipv6AddressIndex = addressIndex; ipv6AddressIndex = addressIndex;
if (ipv6IsGlobal) { if (ipv6IsGlobal) {
break; break;
} }
} }
if (ip == null) { if (ip == null) {
ip = new byte[16]; ip = new byte[16];
ipv6AddressIndex = -1; ipv6AddressIndex = -1;
} }
return ip; return ip;
} }
public static String getUncompressedIPv6AddressString(byte[] ip) { public static String getUncompressedIPv6AddressString(byte[] ip) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i=0; i < 14; i+=2) { for (int i = 0; i < 14; i += 2) {
sb.append(String.format("%02x%02x:", 0xFF&ip[i+0], 0xFF&ip[i+1])); sb.append(String.format("%02x%02x:", 0xFF & ip[i + 0], 0xFF & ip[i + 1]));
} }
sb.append(String.format("%02x%02x", 0xFF&ip[14], 0xFF&ip[15])); sb.append(String.format("%02x%02x", 0xFF & ip[14], 0xFF & ip[15]));
return sb.toString(); return sb.toString();
} }
public String getUncompressedIPv6Address() { public String getUncompressedIPv6Address() {
byte[] ip = getIPv6Address(); byte[] ip = getIPv6Address();
if (ip == null) { if (ip == null) {
return ""; return "";
} }
return getUncompressedIPv6AddressString(ip); return getUncompressedIPv6AddressString(ip);
} }
/** /**
@ -196,10 +197,9 @@ public class IPAddress extends MoteInterface {
* @return True if mote has an IPv6 address * @return True if mote has an IPv6 address
*/ */
public boolean isVersion6() { public boolean isVersion6() {
return return moteMem.variableExists("uip_ds6_netif_addr_list_offset")
moteMem.variableExists("uip_ds6_netif_addr_list_offset") && && moteMem.variableExists("uip_ds6_addr_size")
moteMem.variableExists("uip_ds6_addr_size") && && moteMem.variableExists("uip_ds6_if");
moteMem.variableExists("uip_ds6_if");
} }
public void removed() { public void removed() {
@ -225,8 +225,8 @@ public class IPAddress extends MoteInterface {
if (isVersion4()) { if (isVersion4()) {
ipLabel.setText("IPv4 address: " + getIPString()); ipLabel.setText("IPv4 address: " + getIPString());
} else if (isVersion6()) { } else if (isVersion6()) {
ipLabel.setText((ipv6IsGlobal?"Global":"Local") + ipLabel.setText((ipv6IsGlobal ? "Global" : "Local")
" IPv6 address(#" + ipv6AddressIndex + "): " + getIPString()); + " IPv6 address(#" + ipv6AddressIndex + "): " + getIPString());
} else { } else {
ipLabel.setText("Unknown IP"); ipLabel.setText("Unknown IP");
} }