📄 sectionmotememory.java
字号:
/* * Copyright (c) 2006, Swedish Institute of Computer Science. 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. * * $Id: SectionMoteMemory.java,v 1.6 2008/02/11 14:04:16 fros4943 Exp $ */package se.sics.cooja;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.*;import org.apache.log4j.Logger;import se.sics.cooja.MoteMemory;/** * Represents a mote memory consisting of non-overlapping memory sections with * variables' memory addresses. * <p> * When an unhandled memory segment is set a new section is automatically * created for this segment. * <p> * * @author Fredrik Osterlind */public class SectionMoteMemory implements MoteMemory, AddressMemory { private static Logger logger = Logger.getLogger(SectionMoteMemory.class); private Vector<MoteMemorySection> sections = new Vector<MoteMemorySection>(); private final Properties variableAddresses; /** * Create a new mote memory with information about which variables exist and * their relative memory addresses. * * @param variableAddresses * Variable addresses */ public SectionMoteMemory(Properties variableAddresses) { this.variableAddresses = variableAddresses; } public String[] getVariableNames() { String[] names = new String[variableAddresses.size()]; Enumeration nameEnum = variableAddresses.keys(); for (int i = 0; i < variableAddresses.size(); i++) { names[i] = (String) nameEnum.nextElement(); } return names; } public int getVariableAddress(String varName) throws UnknownVariableException { if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } return ((Integer) variableAddresses.get(varName)).intValue(); } public int getIntegerLength() { return 4; } public void clearMemory() { sections.clear(); } public byte[] getMemorySegment(int address, int size) { for (MoteMemorySection section : sections) { if (section.includesAddr(address) && section.includesAddr(address + size - 1)) { return section.getMemorySegment(address, size); } } return null; } public void setMemorySegment(int address, byte[] data) { // TODO Creating overlapping memory sections possible for (MoteMemorySection section : sections) { if (section.includesAddr(address) && section.includesAddr(address + data.length - 1)) { section.setMemorySegment(address, data); return; } } sections.add(new MoteMemorySection(address, data)); } public int getTotalSize() { int totalSize = 0; for (MoteMemorySection section : sections) { totalSize += section.getSize(); } return totalSize; } /** * Returns the total number of sections in this memory. * * @return Number of sections */ public int getNumberOfSections() { return sections.size(); } /** * Removes a memory segment from this memory. The section containing the * segment may be split into two sections. * * @param startAddr * Start address * @param size * Length */ public void removeSegmentFromMemory(int startAddr, int size) { for (MoteMemorySection section : sections) { // Find section containing segment to remove if (section.includesAddr(startAddr) && section.includesAddr(startAddr + size - 1)) { MoteMemorySection oldSection = section; byte[] dataFirstPart = oldSection.getMemorySegment( oldSection.startAddr, (startAddr - oldSection.startAddr)); byte[] dataSecondPart = oldSection .getMemorySegment(startAddr + size, (oldSection.startAddr + oldSection.getSize() - (startAddr + size))); MoteMemorySection newSectionFirstPart = new MoteMemorySection( oldSection.startAddr, dataFirstPart); MoteMemorySection newSectionSecondPart = new MoteMemorySection( startAddr + size, dataSecondPart); // Remove old section, add new sections sections.remove(oldSection); if (newSectionFirstPart.getSize() > 0) { sections.add(newSectionFirstPart); } if (newSectionSecondPart.getSize() > 0) { sections.add(newSectionSecondPart); } } } } /** * Get start address of section at given position. * * @param sectionNr * Section position * @return Start address of section */ public int getStartAddrOfSection(int sectionNr) { if (sectionNr >= sections.size()) { return 0; } return sections.elementAt(sectionNr).getStartAddr(); } /** * Get size of section at given position. * * @param sectionNr * Section position * @return Size of section */ public int getSizeOfSection(int sectionNr) { if (sectionNr >= sections.size()) { return 0; } return sections.elementAt(sectionNr).getSize(); } /** * Get data of section at given position. * * @param sectionNr * Section position * @return Data at section */ public byte[] getDataOfSection(int sectionNr) { if (sectionNr >= sections.size()) { return null; } return sections.elementAt(sectionNr).getData(); } public boolean variableExists(String varName) { return variableAddresses.containsKey(varName); } public int getIntValueOf(String varName) throws UnknownVariableException { // Get start address of variable if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); byte[] varData = getMemorySegment(varAddr, 4); if (varData == null) { throw new UnknownVariableException(varName); } int retVal = 0; int pos = 0; retVal += ((varData[pos++] & 0xFF)) << 24; retVal += ((varData[pos++] & 0xFF)) << 16; retVal += ((varData[pos++] & 0xFF)) << 8; retVal += ((varData[pos++] & 0xFF)) << 0; // TODO Check if small/big-endian when coming from JNI? retVal = Integer.reverseBytes(retVal); return retVal; } public void setIntValueOf(String varName, int newVal) throws UnknownVariableException { // Get start address of variable if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); // TODO Check if small/big-endian when coming from JNI? int newValToSet = Integer.reverseBytes(newVal); // Create byte array int pos = 0; byte[] varData = new byte[4]; varData[pos++] = (byte) ((newValToSet & 0xFF000000) >> 24); varData[pos++] = (byte) ((newValToSet & 0xFF0000) >> 16); varData[pos++] = (byte) ((newValToSet & 0xFF00) >> 8); varData[pos++] = (byte) ((newValToSet & 0xFF) >> 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -