From 52cacb7947952b31fed625fea3cdbd6edc2db54f Mon Sep 17 00:00:00 2001 From: fros4943 Date: Thu, 7 Feb 2008 14:54:16 +0000 Subject: [PATCH] esb and msp430-general interfaces --- .../cooja/mspmote/interfaces/CRCCoder.java | 61 +++++ .../cooja/mspmote/interfaces/ESBButton.java | 115 +++++++++ .../sics/cooja/mspmote/interfaces/ESBLED.java | 176 ++++++++++++++ .../sics/cooja/mspmote/interfaces/ESBLog.java | 138 +++++++++++ .../cooja/mspmote/interfaces/GCRCoder.java | 225 ++++++++++++++++++ .../cooja/mspmote/interfaces/MspClock.java | 95 ++++++++ .../cooja/mspmote/interfaces/MspMoteID.java | 157 ++++++++++++ 7 files changed, 967 insertions(+) create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/CRCCoder.java create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBButton.java create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLED.java create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLog.java create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/GCRCoder.java create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspClock.java create mode 100644 tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspMoteID.java diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/CRCCoder.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/CRCCoder.java new file mode 100644 index 000000000..ab1758da4 --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/CRCCoder.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2007, 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: CRCCoder.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import org.apache.log4j.Logger; + + +/** + * Ported from contiki-2.x/core/lib/crc16.[ch]. + * + * @author Fredrik Osterlind + */ +public class CRCCoder { + private static Logger logger = Logger.getLogger(CRCCoder.class); + + /** + * Updates given accumulated CRC16 checksum with given byte. + * + * @param b Byte to be added to CRC + * @param crc Accumulated CRC that is to be updated + * @return + */ + public static short crc16Add(byte b, short acc) { + acc ^= 0xff & b; + acc = (short) ((0xff & (acc >> 8)) | (0xff00 & (acc << 8))); + acc ^= 0xffff & ((acc & 0xff00) << 4); + acc ^= (0xffff & (0xff & (acc >> 8)) >> 4); + acc ^= 0xffff & ((0xffff & (acc & 0xff00)) >>> 5); + return acc; + } + +} \ No newline at end of file diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBButton.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBButton.java new file mode 100644 index 000000000..bf63c575a --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBButton.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2007, 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: ESBButton.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Collection; +import javax.swing.JButton; +import javax.swing.JPanel; +import org.apache.log4j.Logger; +import org.jdom.Element; + +import se.sics.cooja.*; +import se.sics.cooja.interfaces.Button; +import se.sics.cooja.mspmote.ESBMote; + +/** + * @author Fredrik Osterlind + */ +@ClassDescription("Button") +public class ESBButton extends Button { + private static Logger logger = Logger.getLogger(ESBButton.class); + + private ESBMote esbMote; + + public ESBButton(Mote mote) { + esbMote = (ESBMote) mote; + } + + public void clickButton() { + pressButton(); + releaseButton(); + } + + public void releaseButton() { + esbMote.esbNode.setButton(false); + setChanged(); + notifyObservers(); + } + + public void pressButton() { + esbMote.esbNode.setButton(true); + setChanged(); + notifyObservers(); + } + + public boolean isPressed() { + return false; + } + + public void doActionsBeforeTick() { + } + + public void doActionsAfterTick() { + } + + public double energyConsumptionPerTick() { + return 0.0; + } + + public JPanel getInterfaceVisualizer() { + JPanel panel = new JPanel(); + final JButton clickButton = new JButton("Click button"); + + panel.add(clickButton); + + clickButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + clickButton(); + } + }); + + return panel; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + } + + public Collection getConfigXML() { + return null; + } + + public void setConfigXML(Collection configXML, boolean visAvailable) { + } + +} diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLED.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLED.java new file mode 100644 index 000000000..232f23c97 --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLED.java @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2007, 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: ESBLED.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import org.apache.log4j.Logger; +import org.jdom.Element; + +import se.sics.cooja.*; +import se.sics.mspsim.core.*; +import se.sics.mspsim.platform.esb.ESBNode; +import se.sics.cooja.interfaces.LED; +import se.sics.cooja.mspmote.ESBMote; + +/** + * @author Fredrik Osterlind + */ +@ClassDescription("ESB LED") +public class ESBLED extends LED implements PortListener { + private static Logger logger = Logger.getLogger(ESBLED.class); + + private ESBMote mspMote; + private boolean redOn = false; + private boolean greenOn = false; + private boolean yellowOn = false; + + private static final Color DARK_GREEN = new Color(0, 100, 0); + private static final Color DARK_YELLOW = new Color(100, 100, 0); + private static final Color DARK_RED = new Color(100, 0, 0); + private static final Color GREEN = new Color(0, 255, 0); + private static final Color YELLOW = new Color(255, 255, 0); + private static final Color RED = new Color(255, 0, 0); + + public ESBLED(Mote mote) { + mspMote = (ESBMote) mote; + + /* Listen for port writes */ + IOUnit unit = mspMote.getCPU().getIOUnit("Port 2"); + if (unit instanceof IOPort) { + ((IOPort) unit).setPortListener(this); + } + } + + public boolean isAnyOn() { + return redOn || greenOn || yellowOn; + } + + public boolean isGreenOn() { + return greenOn; + } + + public boolean isYellowOn() { + return yellowOn; + } + + public boolean isRedOn() { + return redOn; + } + + public void doActionsBeforeTick() { + } + + public void doActionsAfterTick() { + } + + public double energyConsumptionPerTick() { + return 0.0; + } + + public JPanel getInterfaceVisualizer() { + final JPanel panel = new JPanel() { + public void paintComponent(Graphics g) { + super.paintComponent(g); + + if (isGreenOn()) { + g.setColor(GREEN); + g.fillOval(20, 20, 20, 20); + } else { + g.setColor(DARK_GREEN); + g.fillOval(20, 20, 20, 20); + } + + if (isYellowOn()) { + g.setColor(YELLOW); + g.fillOval(60, 20, 20, 20); + } else { + g.setColor(DARK_YELLOW); + g.fillOval(60, 20, 20, 20); + } + + if (isRedOn()) { + g.setColor(RED); + g.fillOval(100, 20, 20, 20); + } else { + g.setColor(DARK_RED); + g.fillOval(100, 20, 20, 20); + } + } + }; + + Observer observer; + this.addObserver(observer = new Observer() { + public void update(Observable obs, Object obj) { + panel.repaint(); + } + }); + + // Saving observer reference for releaseInterfaceVisualizer + panel.putClientProperty("intf_obs", observer); + + panel.setMinimumSize(new Dimension(140, 60)); + panel.setPreferredSize(new Dimension(140, 60)); + + return panel; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + Observer observer = (Observer) panel.getClientProperty("intf_obs"); + if (observer == null) { + logger.fatal("Error when releasing panel, observer is null"); + return; + } + + this.deleteObserver(observer); + } + + + public Collection getConfigXML() { + return null; + } + + public void setConfigXML(Collection configXML, boolean visAvailable) { + } + + + public void portWrite(IOPort source, int data) { + redOn = (data & ESBNode.RED_LED) != 0; + greenOn = (data & ESBNode.GREEN_LED) != 0; + yellowOn = (data & ESBNode.YELLOW_LED) != 0; + + setChanged(); + notifyObservers(); + } + +} diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLog.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLog.java new file mode 100644 index 000000000..ddb94c16f --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/ESBLog.java @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2007, 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: ESBLog.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import java.awt.*; +import java.util.*; +import javax.swing.*; +import org.apache.log4j.Logger; +import org.jdom.Element; + +import se.sics.cooja.*; +import se.sics.mspsim.core.*; +import se.sics.cooja.interfaces.Log; +import se.sics.cooja.mspmote.ESBMote; + +/** + * @author Fredrik Osterlind + */ +@ClassDescription("Serial port") +public class ESBLog extends Log implements USARTListener { + private static Logger logger = Logger.getLogger(ESBLog.class); + + private Mote myMote; + private String lastLogMessage = ""; + private String newMessage = ""; + + public ESBLog(ESBMote mote) { + myMote = mote; + + /* Listen to port writes */ + IOUnit usart = mote.getCPU().getIOUnit("USART 1"); + if (usart instanceof USART) { + ((USART) usart).setUSARTListener(this); + } + } + + public String getLastLogMessages() { + return lastLogMessage; + } + + public void doActionsBeforeTick() { + } + + public void doActionsAfterTick() { + } + + public JPanel getInterfaceVisualizer() { + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + final JTextArea logTextPane = new JTextArea(); + logTextPane.setOpaque(false); + logTextPane.setEditable(false); + + if (getLastLogMessages() == null) { + logTextPane.setText(""); + } else { + logTextPane.append(getLastLogMessages()); + } + + Observer observer; + this.addObserver(observer = new Observer() { + public void update(Observable obs, Object obj) { + logTextPane.append(getLastLogMessages()); + logTextPane.setCaretPosition(logTextPane.getDocument().getLength()); + } + }); + + // Saving observer reference for releaseInterfaceVisualizer + panel.putClientProperty("intf_obs", observer); + + JScrollPane scrollPane = new JScrollPane(logTextPane); + scrollPane.setPreferredSize(new Dimension(100,100)); + panel.add(BorderLayout.NORTH, new JLabel("Last log messages:")); + panel.add(BorderLayout.CENTER, scrollPane); + return panel; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + Observer observer = (Observer) panel.getClientProperty("intf_obs"); + if (observer == null) { + logger.fatal("Error when releasing panel, observer is null"); + return; + } + + this.deleteObserver(observer); + } + + public double energyConsumptionPerTick() { + return 0.0; + } + + public Collection getConfigXML() { + return null; + } + + public void setConfigXML(Collection configXML, boolean visAvailable) { + } + + public void dataReceived(USART source, int data) { + newMessage += (char) data; + if (data == '\n') { + lastLogMessage = newMessage; + newMessage = ""; + this.setChanged(); + this.notifyObservers(myMote); + } + } + +} diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/GCRCoder.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/GCRCoder.java new file mode 100644 index 000000000..e184db95d --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/GCRCoder.java @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2007, 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: GCRCoder.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import org.apache.log4j.Logger; + +/** + * Ported from contiki-2.x/core/lib/gcr.[ch]. + * + * @author Fredrik Osterlind + */ +public class GCRCoder { + private static Logger logger = Logger.getLogger(GCRCoder.class); + + /* + * GCR conversion table - used for converting ordinary byte to 10-bits (or 4 + * bits to 5) + */ + static final int[] GCR_encode = new int[] { 0x0a, 0x0b, 0x12, 0x13, 0x0e, + 0x0f, 0x16, 0x17, 0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15 }; + + /* 5 bits > 4 bits (0xff => invalid) */ + static final int[] GCR_decode = new int[] { 0xff, 0xff, 0xff, 0xff, // 0 - + // 3invalid... + 0xff, 0xff, 0xff, 0xff, // 4 - 7 invalid... + 0xff, 0x08, 0x00, 0x01, // 8 invalid... 9 = 8, a = 0, b = 1 + 0xff, 0x0c, 0x04, 0x05, // c invalid... d = c, e = 4, f = 5 + + 0xff, 0xff, 0x02, 0x03, // 10-11 invalid... + 0xff, 0x0f, 0x06, 0x07, // 14 invalid... + 0xff, 0x09, 0x0a, 0x0b, // 18 invalid... + 0xff, 0x0d, 0x0e, 0xff, // 1c, 1f invalid... + }; + + private int gcr_bits = 0; + + private int gcr_val = 0; + + public GCRCoder() { + } + + /* Call before starting encoding or decoding */ + public void gcr_init() { + gcr_val = 0; + gcr_bits = 0; + } + + /* Use this to check if encoding / decoding is complete for now */ + public boolean gcr_finished() { + return gcr_bits == 0; + } + + /* Encode one character - and store in bits - get encoded with get_encoded */ + void gcr_encode(int raw_data) { + gcr_val |= ((GCR_encode[raw_data >> 4] << 5) | GCR_encode[raw_data & 0xf]) << gcr_bits; + gcr_bits += 10; + } + + /* Gets the current char of the encoded stream */ + boolean gcr_get_encoded(int[] raw_data, int current_pos) { + if (gcr_bits >= 8) { + raw_data[current_pos] = (gcr_val & 0xff); + gcr_val = gcr_val >> 8; + gcr_bits = gcr_bits - 8; + return true; + } + return false; + } + + /* Decode one char - result can be get from get_decoded */ + void gcr_decode(int gcr_data) { + gcr_val |= gcr_data << gcr_bits; + gcr_bits += 8; + } + + /* check if the current decoded stream is correct */ + boolean gcr_valid() { + if (gcr_bits >= 10) { + int val = gcr_val & 0x3ff; + if ((GCR_decode[val >> 5] << 4) == 0xff + || (GCR_decode[val & 0x1f]) == 0xff) { + return false; + } + } + return true; + } + + /* gets the decoded stream - if any char is available */ + boolean gcr_get_decoded(int[] raw_data, int current_pos) { + if (gcr_bits >= 10) { + int val = gcr_val & 0x3ff; + raw_data[current_pos] = ((GCR_decode[val >> 5] << 4) | (GCR_decode[val & 0x1f])); + gcr_val = gcr_val >> 10; + gcr_bits = gcr_bits - 10; + return true; + } + return false; + } + + /** + * Decodes given data. If decoding fails null is returned. + * + * WARNING! Decoding encoded data may differ from original data due to + * appended zeroes during encoding. + * + * @param data + * Data + * @param dataLength + * Data length to decode + * @return Decoded data or null + */ + public byte[] gcrDecode(byte[] data, int dataLength) { + // Reset GCR (de)coder + gcr_init(); + + // Length of decoded data + int convertedLength = 0; + + // Temporary decoded data storage + int[] convertedInts = new int[dataLength]; // Maximum length + + for (int i = 0; i < dataLength; i++) { + + // Try decode byte + gcr_decode(0xff & data[i]); + if (!gcr_valid()) { + logger.fatal("GCR decoding failed, dropping packet"); + return null; + } + + // If new byte decoded, store it + if (gcr_get_decoded(convertedInts, convertedLength)) { + convertedLength++; + } + } + + // Convert to byte array + byte[] convertedBytes = new byte[convertedLength]; + for (int i = 0; i < convertedLength; i++) { + convertedBytes[i] = (byte) (0xff & convertedInts[i]); + } + + return convertedBytes; + } + + /** + * Encodes given data. If encoding fails null is returned. + * + * WARNING! May append data by extra zeroes if needed by GCR. + * + * @param data + * Data + * @param dataLength + * Data length to decode + * @return Encoded data or null + */ + public byte[] gcrEncode(byte[] data, int dataLength) { + // Reset GCR (en)coder + gcr_init(); + + // Length of decoded data + int convertedLength = 0; + + // Temporary encoded data storage + int[] convertedInts = new int[dataLength * 2]; // Maximum length + + for (int i = 0; i < dataLength; i++) { + + // Try encode byte + gcr_encode(0xff & data[i]); + + // Store encoded bytes + while (gcr_get_encoded(convertedInts, convertedLength)) { + convertedLength++; + } + } + + // Append extra 0 if GCR not finished + if (!gcr_finished()) { + gcr_encode(0); + + while (gcr_get_encoded(convertedInts, convertedLength)) { + convertedLength++; + } + } + + // Convert to byte array + byte[] convertedBytes = new byte[convertedLength]; + for (int i = 0; i < convertedLength; i++) { + convertedBytes[i] = (byte) (0xff & convertedInts[i]); + } + + return convertedBytes; + } + +} \ No newline at end of file diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspClock.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspClock.java new file mode 100644 index 000000000..348394a30 --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspClock.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2007, 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: MspClock.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import java.util.Collection; +import javax.swing.JPanel; +import org.apache.log4j.Logger; +import org.jdom.Element; + +import se.sics.cooja.*; +import se.sics.cooja.interfaces.Clock; +import se.sics.cooja.mspmote.MspMote; +import se.sics.mspsim.core.MSP430; + +/** + * @author Fredrik Osterlind + */ +@ClassDescription("Cycle clock") +public class MspClock extends Clock { + private static Logger logger = Logger.getLogger(MspClock.class); + + private MspMote myMote; + private MSP430 cpu; + + public MspClock(MspMote mote) { + myMote = mote; + cpu = myMote.getCPU(); + } + + public void setTime(int newTime) { + logger.fatal("Can't change emulated CPU time"); + } + + public int getTime() { + return (int) (cpu.cycles / MspMote.NR_CYCLES_PER_MSEC); + } + + public void setDrift(int drift) { + myMote.cycleCounter = -MspMote.NR_CYCLES_PER_MSEC * drift; + } + + public void doActionsBeforeTick() { + } + + public void doActionsAfterTick() { + } + + public JPanel getInterfaceVisualizer() { + return null; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + } + + public double energyConsumptionPerTick() { + return 0.0; + } + + public Collection getConfigXML() { + return null; + } + + public void setConfigXML(Collection configXML, boolean visAvailable) { + } + +} diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspMoteID.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspMoteID.java new file mode 100644 index 000000000..047a4e01b --- /dev/null +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/interfaces/MspMoteID.java @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2007, 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: MspMoteID.java,v 1.1 2008/02/07 14:54:16 fros4943 Exp $ + */ + +package se.sics.cooja.mspmote.interfaces; + +import java.util.Collection; +import java.util.Observable; +import java.util.Observer; +import java.util.Vector; + +import javax.swing.JLabel; +import javax.swing.JPanel; + +import org.apache.log4j.Logger; +import org.jdom.Element; + +import se.sics.cooja.*; +import se.sics.cooja.interfaces.MoteID; +import se.sics.cooja.mspmote.MspMote; +import se.sics.cooja.mspmote.MspMoteMemory; + +/** + * @author Fredrik Osterlind + */ +public class MspMoteID extends MoteID { + private final int PERSISTENT_ID_TIME = 100; + + private static Logger logger = Logger.getLogger(MspMoteID.class); + + private MspMote mote; + private MspMoteMemory moteMem = null; + + private int persistentID = -1; + + /** + * Creates an interface to the mote ID at mote. + * + * @param mote + * Mote ID's mote. + * @see Mote + * @see se.sics.cooja.MoteInterfaceHandler + */ + public MspMoteID(Mote mote) { + this.mote = (MspMote) mote; + this.moteMem = (MspMoteMemory) mote.getMemory(); + } + + public int getMoteID() { + return moteMem.getIntValueOf("node_id"); + } + + public void setMoteID(int newID) { + if (mote.getInterfaces().getClock().getTime() < PERSISTENT_ID_TIME) { + persistentID = newID; + } + moteMem.setIntValueOf("node_id", newID); + setChanged(); + notifyObservers(); + } + + public void doActionsBeforeTick() { + } + + public void doActionsAfterTick() { + if (persistentID > 0) { + if (mote.getInterfaces().getClock().getTime() < PERSISTENT_ID_TIME) { + setMoteID(persistentID); + } else { + persistentID = 0; + } + } + } + + public JPanel getInterfaceVisualizer() { + JPanel panel = new JPanel(); + final JLabel idLabel = new JLabel(); + + idLabel.setText("Mote ID: " + getMoteID()); + + panel.add(idLabel); + + Observer observer; + this.addObserver(observer = new Observer() { + public void update(Observable obs, Object obj) { + idLabel.setText("Mote ID: " + getMoteID()); + } + }); + + // Saving observer reference for releaseInterfaceVisualizer + panel.putClientProperty("intf_obs", observer); + + return panel; + } + + public void releaseInterfaceVisualizer(JPanel panel) { + Observer observer = (Observer) panel.getClientProperty("intf_obs"); + if (observer == null) { + logger.fatal("Error when releasing panel, observer is null"); + return; + } + + this.deleteObserver(observer); + } + + public double energyConsumptionPerTick() { + return 0.0; + } + + public Collection getConfigXML() { + Vector config = new Vector(); + Element element; + + // Infinite boolean + element = new Element("id"); + element.setText(Integer.toString(getMoteID())); + config.add(element); + + return config; + } + + public void setConfigXML(Collection configXML, boolean visAvailable) { + for (Element element : configXML) { + if (element.getName().equals("id")) { + setMoteID(Integer.parseInt(element.getText())); + } + } + } + +}