throwing unknown variable exceptions

This commit is contained in:
fros4943 2008-02-11 14:04:16 +00:00
parent afebaa9e97
commit c8f1906479

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2006, Swedish Institute of Computer Science. All rights * Copyright (c) 2006, Swedish Institute of Computer Science. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, * 1. Redistributions of source code must retain the above copyright notice,
@ -12,7 +12,7 @@
* Institute nor the names of its contributors may be used to endorse or promote * Institute nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written * products derived from this software without specific prior written
* permission. * permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@ -23,8 +23,8 @@
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: SectionMoteMemory.java,v 1.5 2007/02/02 11:02:15 fros4943 Exp $ * $Id: SectionMoteMemory.java,v 1.6 2008/02/11 14:04:16 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -43,7 +43,7 @@ import se.sics.cooja.MoteMemory;
* When an unhandled memory segment is set a new section is automatically * When an unhandled memory segment is set a new section is automatically
* created for this segment. * created for this segment.
* <p> * <p>
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
public class SectionMoteMemory implements MoteMemory, AddressMemory { public class SectionMoteMemory implements MoteMemory, AddressMemory {
@ -56,7 +56,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Create a new mote memory with information about which variables exist and * Create a new mote memory with information about which variables exist and
* their relative memory addresses. * their relative memory addresses.
* *
* @param variableAddresses * @param variableAddresses
* Variable addresses * Variable addresses
*/ */
@ -73,12 +73,17 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
return names; return names;
} }
public int getVariableAddress(String varName) { public int getVariableAddress(String varName) throws UnknownVariableException {
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return -1; throw new UnknownVariableException(varName);
}
return ((Integer) variableAddresses.get(varName)).intValue(); return ((Integer) variableAddresses.get(varName)).intValue();
} }
public int getIntegerLength() {
return 4;
}
public void clearMemory() { public void clearMemory() {
sections.clear(); sections.clear();
} }
@ -107,14 +112,15 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
public int getTotalSize() { public int getTotalSize() {
int totalSize = 0; int totalSize = 0;
for (MoteMemorySection section : sections) for (MoteMemorySection section : sections) {
totalSize += section.getSize(); totalSize += section.getSize();
}
return totalSize; return totalSize;
} }
/** /**
* Returns the total number of sections in this memory. * Returns the total number of sections in this memory.
* *
* @return Number of sections * @return Number of sections
*/ */
public int getNumberOfSections() { public int getNumberOfSections() {
@ -124,23 +130,23 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Removes a memory segment from this memory. The section containing the * Removes a memory segment from this memory. The section containing the
* segment may be split into two sections. * segment may be split into two sections.
* *
* @param startAddr * @param startAddr
* Start address * Start address
* @param size * @param size
* Length * Length
*/ */
public void removeSegmentFromMemory(int startAddr, int size) { public void removeSegmentFromMemory(int startAddr, int size) {
for (MoteMemorySection section : sections) for (MoteMemorySection section : sections) {
// Find section containing segment to remove // Find section containing segment to remove
if (section.includesAddr(startAddr) if (section.includesAddr(startAddr)
&& section.includesAddr(startAddr + size - 1)) { && section.includesAddr(startAddr + size - 1)) {
MoteMemorySection oldSection = section; MoteMemorySection oldSection = section;
byte[] dataFirstPart = oldSection.getMemorySegment( byte[] dataFirstPart = oldSection.getMemorySegment(
oldSection.startAddr, (int) (startAddr - oldSection.startAddr)); oldSection.startAddr, (startAddr - oldSection.startAddr));
byte[] dataSecondPart = oldSection byte[] dataSecondPart = oldSection
.getMemorySegment(startAddr + size, (int) (oldSection.startAddr .getMemorySegment(startAddr + size, (oldSection.startAddr
+ oldSection.getSize() - (startAddr + size))); + oldSection.getSize() - (startAddr + size)));
MoteMemorySection newSectionFirstPart = new MoteMemorySection( MoteMemorySection newSectionFirstPart = new MoteMemorySection(
@ -150,51 +156,57 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
// Remove old section, add new sections // Remove old section, add new sections
sections.remove(oldSection); sections.remove(oldSection);
if (newSectionFirstPart.getSize() > 0) if (newSectionFirstPart.getSize() > 0) {
sections.add(newSectionFirstPart); sections.add(newSectionFirstPart);
if (newSectionSecondPart.getSize() > 0) }
if (newSectionSecondPart.getSize() > 0) {
sections.add(newSectionSecondPart); sections.add(newSectionSecondPart);
}
} }
}
} }
/** /**
* Get start address of section at given position. * Get start address of section at given position.
* *
* @param sectionNr * @param sectionNr
* Section position * Section position
* @return Start address of section * @return Start address of section
*/ */
public int getStartAddrOfSection(int sectionNr) { public int getStartAddrOfSection(int sectionNr) {
if (sectionNr >= sections.size()) if (sectionNr >= sections.size()) {
return 0; return 0;
}
return sections.elementAt(sectionNr).getStartAddr(); return sections.elementAt(sectionNr).getStartAddr();
} }
/** /**
* Get size of section at given position. * Get size of section at given position.
* *
* @param sectionNr * @param sectionNr
* Section position * Section position
* @return Size of section * @return Size of section
*/ */
public int getSizeOfSection(int sectionNr) { public int getSizeOfSection(int sectionNr) {
if (sectionNr >= sections.size()) if (sectionNr >= sections.size()) {
return 0; return 0;
}
return sections.elementAt(sectionNr).getSize(); return sections.elementAt(sectionNr).getSize();
} }
/** /**
* Get data of section at given position. * Get data of section at given position.
* *
* @param sectionNr * @param sectionNr
* Section position * Section position
* @return Data at section * @return Data at section
*/ */
public byte[] getDataOfSection(int sectionNr) { public byte[] getDataOfSection(int sectionNr) {
if (sectionNr >= sections.size()) if (sectionNr >= sections.size()) {
return null; return null;
}
return sections.elementAt(sectionNr).getData(); return sections.elementAt(sectionNr).getData();
} }
@ -203,20 +215,25 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
return variableAddresses.containsKey(varName); return variableAddresses.containsKey(varName);
} }
public int getIntValueOf(String varName) { public int getIntValueOf(String varName) throws UnknownVariableException {
// Get start address of variable // Get start address of variable
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return -1; throw new UnknownVariableException(varName);
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
byte[] varData = getMemorySegment(varAddr, 4); byte[] varData = getMemorySegment(varAddr, 4);
if (varData == null) {
throw new UnknownVariableException(varName);
}
int retVal = 0; int retVal = 0;
int pos = 0; int pos = 0;
retVal += ((int) (varData[pos++] & 0xFF)) << 24; retVal += ((varData[pos++] & 0xFF)) << 24;
retVal += ((int) (varData[pos++] & 0xFF)) << 16; retVal += ((varData[pos++] & 0xFF)) << 16;
retVal += ((int) (varData[pos++] & 0xFF)) << 8; retVal += ((varData[pos++] & 0xFF)) << 8;
retVal += ((int) (varData[pos++] & 0xFF)) << 0; retVal += ((varData[pos++] & 0xFF)) << 0;
// TODO Check if small/big-endian when coming from JNI? // TODO Check if small/big-endian when coming from JNI?
retVal = Integer.reverseBytes(retVal); retVal = Integer.reverseBytes(retVal);
@ -224,10 +241,11 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
return retVal; return retVal;
} }
public void setIntValueOf(String varName, int newVal) { public void setIntValueOf(String varName, int newVal) throws UnknownVariableException {
// Get start address of variable // Get start address of variable
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return; throw new UnknownVariableException(varName);
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
// TODO Check if small/big-endian when coming from JNI? // TODO Check if small/big-endian when coming from JNI?
@ -245,21 +263,27 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
setMemorySegment(varAddr, varData); setMemorySegment(varAddr, varData);
} }
public byte getByteValueOf(String varName) { public byte getByteValueOf(String varName) throws UnknownVariableException {
// Get start address of variable // Get start address of variable
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return -1; throw new UnknownVariableException(varName);
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
byte[] varData = getMemorySegment(varAddr, 1); byte[] varData = getMemorySegment(varAddr, 1);
if (varData == null) {
throw new UnknownVariableException(varName);
}
return varData[0]; return varData[0];
} }
public void setByteValueOf(String varName, byte newVal) { public void setByteValueOf(String varName, byte newVal) throws UnknownVariableException {
// Get start address of variable // Get start address of variable
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return; throw new UnknownVariableException(varName);
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
byte[] varData = new byte[1]; byte[] varData = new byte[1];
@ -269,20 +293,22 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
setMemorySegment(varAddr, varData); setMemorySegment(varAddr, varData);
} }
public byte[] getByteArray(String varName, int length) { public byte[] getByteArray(String varName, int length) throws UnknownVariableException {
// Get start address of variable // Get start address of variable
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return null; throw new UnknownVariableException(varName);
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
// TODO Check if small/big-endian when coming from JNI? // TODO Check if small/big-endian when coming from JNI?
return getMemorySegment(varAddr, length); return getMemorySegment(varAddr, length);
} }
public void setByteArray(String varName, byte[] data) { public void setByteArray(String varName, byte[] data) throws UnknownVariableException {
// Get start address of variable // Get start address of variable
if (!variableAddresses.containsKey(varName)) if (!variableAddresses.containsKey(varName)) {
return; throw new UnknownVariableException(varName);
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
// TODO Check if small/big-endian when coming from JNI? // TODO Check if small/big-endian when coming from JNI?
@ -291,7 +317,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* A memory section contains a byte array and a start address. * A memory section contains a byte array and a start address.
* *
* @author Fredrik Osterlind * @author Fredrik Osterlind
*/ */
private class MoteMemorySection { private class MoteMemorySection {
@ -301,7 +327,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Create a new memory section. * Create a new memory section.
* *
* @param startAddr * @param startAddr
* Start address of section * Start address of section
* @param data * @param data
@ -314,7 +340,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Returns start address of this memory section. * Returns start address of this memory section.
* *
* @return Start address * @return Start address
*/ */
public int getStartAddr() { public int getStartAddr() {
@ -323,7 +349,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Returns size of this memory section. * Returns size of this memory section.
* *
* @return Size * @return Size
*/ */
public int getSize() { public int getSize() {
@ -332,7 +358,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Returns the entire byte array which defines this section. * Returns the entire byte array which defines this section.
* *
* @return Byte array * @return Byte array
*/ */
public byte[] getData() { public byte[] getData() {
@ -341,22 +367,23 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* True if given address is part of this memory section. * True if given address is part of this memory section.
* *
* @param addr * @param addr
* Address * Address
* @return True if given address is part of this memory section, false * @return True if given address is part of this memory section, false
* otherwise * otherwise
*/ */
public boolean includesAddr(int addr) { public boolean includesAddr(int addr) {
if (data == null) if (data == null) {
return false; return false;
}
return (addr >= startAddr && addr < (startAddr + data.length)); return (addr >= startAddr && addr < (startAddr + data.length));
} }
/** /**
* Returns memory segment. * Returns memory segment.
* *
* @param addr * @param addr
* Start address of memory segment * Start address of memory segment
* @param size * @param size
@ -371,7 +398,7 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
/** /**
* Sets a memory segment. * Sets a memory segment.
* *
* @param addr * @param addr
* Start of memory segment * Start of memory segment
* @param data * @param data
@ -437,9 +464,11 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
return null; return null;
} }
for (int j = 0; j < sections.get(i).getSize(); j++) for (int j = 0; j < sections.get(i).getSize(); j++) {
if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) {
differences.add(new Integer(sections.get(i).startAddr + j)); differences.add(new Integer(sections.get(i).startAddr + j));
}
}
System.err.println(); System.err.println();
} }
return differences; return differences;
@ -448,8 +477,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
protected void printMemory() { protected void printMemory() {
for (int i = 0; i < sections.size(); i++) { for (int i = 0; i < sections.size(); i++) {
System.err.print("Section[" + i + "]: "); System.err.print("Section[" + i + "]: ");
for (int j = 0; j < sections.get(i).getSize(); j++) for (int j = 0; j < sections.get(i).getSize(); j++) {
System.err.print(sections.get(i).getData()[j] + ","); System.err.print(sections.get(i).getData()[j] + ",");
}
System.err.println(); System.err.println();
} }
} }
@ -471,9 +501,11 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
} }
System.err.print("Section[" + i + "]: "); System.err.print("Section[" + i + "]: ");
for (int j = 0; j < sections.get(i).getSize(); j++) for (int j = 0; j < sections.get(i).getSize(); j++) {
if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) {
System.err.print(j + ","); System.err.print(j + ",");
}
}
System.err.println(); System.err.println();
} }
} }
@ -481,8 +513,8 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
protected void printChecksum() { protected void printChecksum() {
byte[] checksum = this.getChecksum(); byte[] checksum = this.getChecksum();
System.err.print("Checksum: "); System.err.print("Checksum: ");
for (int i = 0; i < checksum.length; i++) { for (byte element : checksum) {
System.err.print(checksum[i] + ","); System.err.print(element + ",");
} }
System.err.println(); System.err.println();
} }