throwing unknown variable exceptions

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

View file

@ -24,7 +24,7 @@
* (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;
@ -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,8 +112,9 @@ 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;
} }
@ -131,16 +137,16 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* 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,12 +156,15 @@ 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.
@ -165,8 +174,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* @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();
} }
@ -179,8 +189,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* @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();
} }
@ -193,8 +204,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* @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?
@ -348,8 +374,9 @@ public class SectionMoteMemory implements MoteMemory, AddressMemory {
* 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));
} }
@ -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();
} }