From 6b15e7837ddeea458ad1e405ed3399729a8ab958 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Tue, 22 Jul 2014 18:10:30 +0200 Subject: [PATCH] [cooja] memory: Added address and symbol based memory access classes --- .../contikios/cooja/mote/memory/Memory.java | 256 ++++++++++++ .../cooja/mote/memory/VarMemory.java | 383 ++++++++++++++++++ 2 files changed, 639 insertions(+) create mode 100644 tools/cooja/java/org/contikios/cooja/mote/memory/Memory.java create mode 100644 tools/cooja/java/org/contikios/cooja/mote/memory/VarMemory.java diff --git a/tools/cooja/java/org/contikios/cooja/mote/memory/Memory.java b/tools/cooja/java/org/contikios/cooja/mote/memory/Memory.java new file mode 100644 index 000000000..4f405a07c --- /dev/null +++ b/tools/cooja/java/org/contikios/cooja/mote/memory/Memory.java @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2014, TU Braunschweig. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND 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 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +package org.contikios.cooja.mote.memory; + +import org.contikios.cooja.mote.memory.MemoryLayout.DataType; + +/** + * Represents memory that can be accessed with address and size informations. + * + * @author Enrico Jorns + */ +public abstract class Memory { + + private final MemoryInterface memIntf; + + /** + * Creates new memory for given MemoryLayout. + * + * @param intf + */ + public Memory(MemoryInterface intf) { + memIntf = intf; + } + + // -- Get fixed size types + /** + * Read 8 bit integer from address. + * + * @param addr Address to read from + * @return 8 bit value read from address + */ + public byte getInt8ValueOf(long addr) { + return memIntf.getMemorySegment(addr, DataType.INT8.getSize())[0]; + } + + /** + * Read 16 bit integer from address. + * + * @param addr Address to read from + * @return 16 bit value read from address + */ + public short getInt16ValueOf(long addr) { + return MemoryBuffer.wrap( + memIntf.getLayout(), + memIntf.getMemorySegment(addr, DataType.INT16.getSize())).getInt16(); + } + + /** + * Read 32 bit integer from address. + * + * @param addr Address to read from + * @return 32 bit value read from address + */ + public int getInt32ValueOf(long addr) { + return MemoryBuffer.wrap( + memIntf.getLayout(), + memIntf.getMemorySegment(addr, DataType.INT32.getSize())).getInt32(); + } + + /** + * Read 64 bit integer from address. + * + * @param addr Address to read from + * @return 64 bit value read from address + */ + public long getInt64ValueOf(long addr) { + return MemoryBuffer.wrap( + memIntf.getLayout(), + memIntf.getMemorySegment(addr, DataType.INT64.getSize())).getInt64(); + } + + // -- Get compiler-dependent types + /** + * Read byte from address. + * + * @param addr Address to read from + * @return byte read from address + */ + public byte getByteValueOf(long addr) { + return memIntf.getMemorySegment(addr, DataType.BYTE.getSize())[0]; + } + + /** + * Read short from address. + * + * @param addr Address to read from + * @return short read from address + */ + public short getShortValueOf(long addr) { + return MemoryBuffer.wrap(memIntf.getLayout(), memIntf.getMemorySegment(addr, 2)).getShort(); + } + + /** + * Read integer from address. + *

+ * Note: Size of integer depends on platform type. + * + * @param addr Address to read from + * @return integer read from address + */ + public int getIntValueOf(long addr) { + return MemoryBuffer.wrap(memIntf.getLayout(), memIntf.getMemorySegment(addr, memIntf.getLayout().intSize)).getInt(); + } + + /** + * Read long from address. + * + * @param addr Address to read from + * @return long read from address + */ + public long getLongValueOf(long addr) { + return MemoryBuffer.wrap(memIntf.getLayout(), memIntf.getMemorySegment(addr, 4)).getLong(); + } + + /** + * Read pointer from address. + *

+ * Note: Size of pointer depends on platform type. + * + * @param addr Address to read from + * @return pointer read from address + */ + public long getAddrValueOf(long addr) { + return MemoryBuffer.wrap(memIntf.getLayout(), memIntf.getMemorySegment(addr, memIntf.getLayout().addrSize)).getAddr(); + } + + // -- Set fixed size types + /** + * Write 8 bit integer to address. + * + * @param addr Address to write to + * @param value 8 bit value to write + */ + public void setInt8ValueOf(long addr, byte value) { + memIntf.setMemorySegment(addr, new byte[]{value}); + } + + /** + * Write 16 bit integer to address. + * + * @param addr Address to write to + * @param value 16 bit value to write + */ + public void setInt16ValueOf(long addr, short value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap( + memIntf.getLayout(), + new byte[DataType.INT16.getSize()]).putShort(value).getBytes()); + } + + /** + * Write 32 bit integer to address. + * + * @param addr Address to write to + * @param value 32 bit value to write + */ + public void setInt32ValueOf(long addr, int value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap( + memIntf.getLayout(), + new byte[DataType.INT32.getSize()]).putInt(value).getBytes()); + } + + /** + * Write 64 bit integer to address. + * + * @param addr Address to write to + * @param value 64 bit value to write + */ + public void setInt64ValueOf(long addr, long value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap( + memIntf.getLayout(), + new byte[DataType.INT64.getSize()]).putLong(value).getBytes()); + } + + // -- Set compiler-dependent types + /** + * Write byte to address. + * + * @param addr Address to write to + * @param value byte to write + */ + public void setByteValueOf(long addr, byte value) { + memIntf.setMemorySegment(addr, new byte[]{value}); + } + + /** + * Write short to address. + * + * @param addr Address to write to + * @param value short to write + */ + public void setShortValueOf(long addr, short value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap(memIntf.getLayout(), new byte[2]).putShort(value).getBytes()); + } + + /** + * Write integer to address. + *

+ * Note: Size of integer depends on platform type. + * + * @param addr Address to write to + * @param value integer to write + */ + public void setIntValueOf(long addr, int value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap(memIntf.getLayout(), new byte[memIntf.getLayout().intSize]).putInt(value).getBytes()); + } + + /** + * Write long to address. + * + * @param addr Address to write to + * @param value long to write + */ + public void setLongValueOf(long addr, long value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap(memIntf.getLayout(), new byte[4]).putLong(value).getBytes()); + } + + /** + * Write pointer to address. + *

+ * Note: Size of pointer depends on platform type. + * + * @param addr Address to write to + * @param value pointer to write + */ + public void setAddrValueOf(long addr, long value) { + memIntf.setMemorySegment(addr, MemoryBuffer.wrap(memIntf.getLayout(), new byte[memIntf.getLayout().addrSize]).putAddr(value).getBytes()); + } + +} diff --git a/tools/cooja/java/org/contikios/cooja/mote/memory/VarMemory.java b/tools/cooja/java/org/contikios/cooja/mote/memory/VarMemory.java new file mode 100644 index 000000000..c8866eeef --- /dev/null +++ b/tools/cooja/java/org/contikios/cooja/mote/memory/VarMemory.java @@ -0,0 +1,383 @@ +/* + * Copyright (c) 2014, TU Braunschweig. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND 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 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +package org.contikios.cooja.mote.memory; + +import java.util.Collection; +import java.util.Set; + +import org.contikios.cooja.mote.memory.MemoryInterface.Symbol; +import org.contikios.cooja.mote.memory.MemoryInterface.SegmentMonitor; +import org.contikios.cooja.mote.memory.MemoryInterface.SegmentMonitor.EventType; + +/** + * Represents memory that can be accessed with names of variables. + * + * @author Enrico Jorns + */ +public class VarMemory extends Memory { + + private MemoryInterface memIntf; + + /** + * Creates new VarMemory. + * + * @param intf + */ + public VarMemory(MemoryInterface intf) { + super(intf); + memIntf = intf; + } + + /** + * Allows to change the MemoryInterface associated with this access class. + * + * @param intf Interface to associate with + */ + public void associateMemory(MemoryInterface intf) { + memIntf = intf; + } + + /** + * Generates and returns an array of all variables in this memory + * + * @return All variables located in this memory + */ + public Collection getVariables() { + return memIntf.getSymbolMap().values(); + } + + /** + * Generates an array of all variable names in this memory. + * + * @return All variable names located in this memory + */ + public Set getVariableNames() { + return memIntf.getSymbolMap().keySet(); + } + + /** + * Checks if given variable exists in memory. + * + * @param varName Variable name + * @return True if variable exists, false otherwise + */ + public boolean variableExists(String varName) { + return memIntf.getSymbolMap().containsKey(varName); + } + + /** + * Returns address of variable with given name. + * + * @param varName Variable name + * @return Variable address + */ + public Symbol getVariable(String varName) throws UnknownVariableException { + return memIntf.getSymbolMap().get(varName); + } + + /** + * Returns address of variable with given name. + * + * @param varName Variable name + * @return Address of variable + * @throws UnknownVariableException If variable not found + */ + public long getVariableAddress(String varName) throws UnknownVariableException { + return getVariable(varName).addr; + } + + /** + * Return size of variable with given name. + * + * @param varName Variable name + * @return Size of variable, -1 if unknown size + * @throws UnknownVariableException If variable not found + */ + public int getVariableSize(String varName) throws UnknownVariableException { + return getVariable(varName).size; + } + + /** + * Read 8 bit integer from location associated with this variable name. + * + * @param varName Variable name + * @return 8 bit integer value read from location assigned to variable name + */ + public byte getInt8ValueOf(String varName) + throws UnknownVariableException { + return getInt8ValueOf(getVariable(varName).addr); + } + + /** + * Read 16 bit integer from location associated with this variable name. + * + * @param varName Variable name + * @return 16 bit integer value read from location assigned to variable name + */ + public short getInt16ValueOf(String varName) + throws UnknownVariableException { + return getInt16ValueOf(getVariable(varName).addr); + } + + /** + * Read 32 bit integer from location associated with this variable name. + * + * @param varName Variable name + * @return 32 bit integer value read from location assigned to variable name + */ + public int getInt32ValueOf(String varName) + throws UnknownVariableException { + return getInt32ValueOf(getVariable(varName).addr); + } + + /** + * Read 64 bit integer from location associated with this variable name. + * + * @param varName Variable name + * @return 64 bit integer value read from location assigned to variable name + */ + public long getInt64ValueOf(String varName) + throws UnknownVariableException { + return getInt64ValueOf(getVariable(varName).addr); + } + + /** + * Read byte from location associated with this variable name. + * + * @param varName Variable name + * @return byte value read from location assigned to variable name + */ + public byte getByteValueOf(String varName) + throws UnknownVariableException { + return getByteValueOf(getVariable(varName).addr); + } + + /** + * Read short from location associated with this variable name. + * + * @param varName Variable name + * @return short value read from location assigned to variable name + */ + public short getShortValueOf(String varName) + throws UnknownVariableException { + short val = getShortValueOf(getVariable(varName).addr); + return val; + } + + /** + * Read integer from location associated with this variable name. + * + * @param varName Variable name + * @return integer value read from location assigned to variable name + */ + public int getIntValueOf(String varName) + throws UnknownVariableException { + int val = getIntValueOf(getVariable(varName).addr); + return val; + } + + /** + * Read long from location associated with this variable name. + * + * @param varName Variable name + * @return long value read from location assigned to variable name + */ + public long getLongValueOf(String varName) + throws UnknownVariableException { + long val = getLongValueOf(getVariable(varName).addr); + return val; + } + + /** + * Read pointer from location associated with this variable name. + * + * The number of bytes actually read depends on the pointer size + * defined in memory layout. + * + * @param varName Variable name + * @return pointer value read from location assigned to variable name + */ + public long getAddrValueOf(String varName) + throws UnknownVariableException { + long val = getAddrValueOf(getVariable(varName).addr); + return val; + } + + /** + * Read byte array starting at location associated with this variable name. + * + * @param varName Variable name + * @param length Numbe of bytes to read + * @return byte array read from location assigned to variable name + */ + public byte[] getByteArray(String varName, int length) + throws UnknownVariableException { + return memIntf.getMemorySegment(getVariable(varName).addr, length); + } + + /** + * Write 8 bit integer value to location associated with this variable name. + * + * @param varName Variable name + * @param value 8 bit integer value to write + */ + public void setInt8ValueOf(String varName, byte value) + throws UnknownVariableException { + setInt8ValueOf(getVariable(varName).addr, value); + } + + /** + * Write 16 bit integer value to location associated with this variable name. + * + * @param varName Variable name + * @param value 16 bit integer value to write + */ + public void setInt16ValueOf(String varName, byte value) + throws UnknownVariableException { + setInt16ValueOf(getVariable(varName).addr, value); + } + + /** + * Write 32 bit integer value to location associated with this variable name. + * + * @param varName Variable name + * @param value 32 bit integer value to write + */ + public void setInt32ValueOf(String varName, byte value) + throws UnknownVariableException { + setInt32ValueOf(getVariable(varName).addr, value); + } + + /** + * Write 64 bit integer value to location associated with this variable name. + * + * @param varName Variable name + * @param value 64 bit integer value to write + */ + public void setInt64ValueOf(String varName, byte value) + throws UnknownVariableException { + setInt64ValueOf(getVariable(varName).addr, value); + } + + /** + * Write byte value to location associated with this variable name. + * + * @param varName Variable name + * @param value byte value to write + */ + public void setByteValueOf(String varName, byte value) + throws UnknownVariableException { + setByteValueOf(getVariable(varName).addr, value); + } + + /** + * Write short value to location associated with this variable name. + * + * @param varName Variable name + * @param value short value to write + */ + public void setShortValueOf(String varName, short value) + throws UnknownVariableException { + setShortValueOf(getVariable(varName).addr, value); + } + + /** + * Write int value to location associated with this variable name. + * + * @param varName Variable name + * @param value int value to write + */ + public void setIntValueOf(String varName, int value) + throws UnknownVariableException { + setIntValueOf(getVariable(varName).addr, value); + } + + /** + * Write long value to location associated with this variable name. + * + * @param varName Variable name + * @param value long value to write + */ + public void setLongValueOf(String varName, long value) + throws UnknownVariableException { + setLongValueOf(getVariable(varName).addr, value); + } + + /** + * Write pointer value to location associated with this variable name. + * + * The number of bytes actually written depends on the pointer size + * defined in memory layout. + * + * @param varName Variable name + * @param value Value to write + */ + public void setAddrValueOf(String varName, long value) + throws UnknownVariableException { + setAddrValueOf(getVariable(varName).addr, value); + } + + /** + * + * @param varName + * @param data + */ + public void setByteArray(String varName, byte[] data) + throws UnknownVariableException { + memIntf.setMemorySegment(getVariable(varName).addr, data); + } + + /** + * Adds a MemoryMonitor for the specified address region. + * + * @param flag Select memory operation(s) to listen for (read, write, + * read/write) + * @param varName + * @param mm + * @return + */ + public boolean addVarMonitor(EventType flag, final String varName, final SegmentMonitor mm) { + return memIntf.addSegmentMonitor( + flag, + getVariable(varName).addr, + getVariable(varName).size, + mm); + } + + /** + * Removes MemoryMonitor assigned to the specified region. + * + * @param varName + * @param mm MemoryMonitor to remove + */ + public void removeVarMonitor(String varName, SegmentMonitor mm) { + memIntf.removeSegmentMonitor(getVariable(varName).addr, getVariable(varName).size, mm); + } +}