added common emulation classes for emulated motes
This commit is contained in:
parent
60a3fc0cae
commit
474698b36e
4 changed files with 653 additions and 0 deletions
59
tools/cooja/java/se/sics/cooja/emulatedmote/CRCCoder.java
Normal file
59
tools/cooja/java/se/sics/cooja/emulatedmote/CRCCoder.java
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
* 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 2009/11/10 12:54:39 joxe Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
package se.sics.cooja.emulatedmote;
|
||||||
|
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 acc Accumulated CRC that is to be updated
|
||||||
|
* @return New accumulated CRC
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
347
tools/cooja/java/se/sics/cooja/emulatedmote/Radio802154.java
Normal file
347
tools/cooja/java/se/sics/cooja/emulatedmote/Radio802154.java
Normal file
|
@ -0,0 +1,347 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2009, 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: Radio802154.java,v 1.1 2009/11/10 12:54:39 joxe Exp $
|
||||||
|
*/
|
||||||
|
package se.sics.cooja.emulatedmote;
|
||||||
|
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.GridLayout;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
import org.jdom.Element;
|
||||||
|
|
||||||
|
import se.sics.cooja.*;
|
||||||
|
import se.sics.cooja.interfaces.CustomDataRadio;
|
||||||
|
import se.sics.cooja.interfaces.Position;
|
||||||
|
import se.sics.cooja.interfaces.Radio;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 802.15.4 radio class for COOJA.
|
||||||
|
*
|
||||||
|
* @author Joakim Eriksson
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class Radio802154 extends Radio implements CustomDataRadio {
|
||||||
|
|
||||||
|
private final static boolean DEBUG = false;
|
||||||
|
|
||||||
|
private static Logger logger = Logger.getLogger(Radio802154.class);
|
||||||
|
|
||||||
|
protected long lastEventTime = 0;
|
||||||
|
|
||||||
|
protected RadioEvent lastEvent = RadioEvent.UNKNOWN;
|
||||||
|
|
||||||
|
protected boolean isInterfered = false;
|
||||||
|
|
||||||
|
private boolean isTransmitting = false;
|
||||||
|
|
||||||
|
protected boolean isReceiving = false;
|
||||||
|
// private boolean hasFailedReception = false;
|
||||||
|
|
||||||
|
private boolean radioOn = true;
|
||||||
|
|
||||||
|
private RadioByte lastOutgoingByte = null;
|
||||||
|
|
||||||
|
private RadioByte lastIncomingByte = null;
|
||||||
|
|
||||||
|
private RadioPacket lastOutgoingPacket = null;
|
||||||
|
|
||||||
|
private RadioPacket lastIncomingPacket = null;
|
||||||
|
|
||||||
|
// private int mode;
|
||||||
|
protected Mote mote;
|
||||||
|
|
||||||
|
public Radio802154(Mote mote) {
|
||||||
|
this.mote = mote;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = 0;
|
||||||
|
int expLen = 0;
|
||||||
|
byte[] buffer = new byte[127 + 15];
|
||||||
|
protected void handleTransmit(byte val) {
|
||||||
|
if (len == 0) {
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
lastEvent = RadioEvent.TRANSMISSION_STARTED;
|
||||||
|
if (DEBUG) logger.debug("----- 802.15.4 TRANSMISSION STARTED -----");
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
}
|
||||||
|
/* send this byte to all nodes */
|
||||||
|
lastOutgoingByte = new RadioByte(val);
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
lastEvent = RadioEvent.CUSTOM_DATA_TRANSMITTED;
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
|
||||||
|
buffer[len++] = val;
|
||||||
|
|
||||||
|
//System.out.println("## 802.15.4: " + (val&0xff) + " transmitted...");
|
||||||
|
|
||||||
|
if (len == 6) {
|
||||||
|
//System.out.println("## CC2420 Packet of length: " + val + " expected...");
|
||||||
|
expLen = val + 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len == expLen) {
|
||||||
|
if (DEBUG) logger.debug("----- 802.15.4 CUSTOM DATA TRANSMITTED -----");
|
||||||
|
|
||||||
|
lastOutgoingPacket = Radio802154PacketConverter.fromCC2420ToCooja(buffer);
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
lastEvent = RadioEvent.PACKET_TRANSMITTED;
|
||||||
|
if (DEBUG) logger.debug("----- 802.15.4 PACKET TRANSMITTED -----");
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
|
||||||
|
// System.out.println("## CC2420 Transmission finished...");
|
||||||
|
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
/*logger.debug("----- SKY TRANSMISSION FINISHED -----");*/
|
||||||
|
lastEvent = RadioEvent.TRANSMISSION_FINISHED;
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Packet radio support */
|
||||||
|
public RadioPacket getLastPacketTransmitted() {
|
||||||
|
return lastOutgoingPacket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RadioPacket getLastPacketReceived() {
|
||||||
|
return lastIncomingPacket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReceivedPacket(RadioPacket packet) {
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Custom data radio support */
|
||||||
|
public Object getLastCustomDataTransmitted() {
|
||||||
|
return lastOutgoingByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getLastCustomDataReceived() {
|
||||||
|
return lastIncomingByte;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receiveCustomData(Object data) {
|
||||||
|
if (data instanceof RadioByte) {
|
||||||
|
lastIncomingByte = (RadioByte) data;
|
||||||
|
handleReceive(lastIncomingByte.getPacketData()[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* General radio support */
|
||||||
|
public boolean isTransmitting() {
|
||||||
|
return isTransmitting;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isReceiving() {
|
||||||
|
return isReceiving;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isInterfered() {
|
||||||
|
return isInterfered;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void handleReceive(byte b);
|
||||||
|
|
||||||
|
protected abstract void handleEndOfReception();
|
||||||
|
|
||||||
|
public abstract int getChannel();
|
||||||
|
|
||||||
|
public abstract int getFrequency();
|
||||||
|
|
||||||
|
public abstract boolean isReceiverOn();
|
||||||
|
|
||||||
|
public abstract double getCurrentOutputPower();
|
||||||
|
|
||||||
|
public abstract int getCurrentOutputPowerIndicator();
|
||||||
|
|
||||||
|
public abstract int getOutputPowerIndicatorMax();
|
||||||
|
|
||||||
|
public abstract double getCurrentSignalStrength();
|
||||||
|
|
||||||
|
public abstract void setCurrentSignalStrength(double signalStrength);
|
||||||
|
|
||||||
|
public abstract double energyConsumption();
|
||||||
|
|
||||||
|
/* need to add a few more methods later??? */
|
||||||
|
public void signalReceptionStart() {
|
||||||
|
isReceiving = true;
|
||||||
|
|
||||||
|
// cc2420.setCCA(true);
|
||||||
|
// hasFailedReception = mode == CC2420.MODE_TXRX_OFF;
|
||||||
|
/* TODO cc2420.setSFD(true); */
|
||||||
|
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
lastEvent = RadioEvent.RECEPTION_STARTED;
|
||||||
|
if (DEBUG) logger.debug("----- 802.15.4 RECEPTION STARTED -----");
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void signalReceptionEnd() {
|
||||||
|
/* Deliver packet data */
|
||||||
|
isReceiving = false;
|
||||||
|
// hasFailedReception = false;
|
||||||
|
isInterfered = false;
|
||||||
|
// cc2420.setCCA(false);
|
||||||
|
|
||||||
|
/* tell the receiver that the packet is ended */
|
||||||
|
handleEndOfReception();
|
||||||
|
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
lastEvent = RadioEvent.RECEPTION_FINISHED;
|
||||||
|
if (DEBUG) logger.debug("----- 802.15.4 RECEPTION FINISHED -----");
|
||||||
|
// Exception e = new IllegalStateException("Why finished?");
|
||||||
|
// e.printStackTrace();
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RadioEvent getLastEvent() {
|
||||||
|
return lastEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void interfereAnyReception() {
|
||||||
|
isInterfered = true;
|
||||||
|
isReceiving = false;
|
||||||
|
// hasFailedReception = false;
|
||||||
|
lastIncomingPacket = null;
|
||||||
|
|
||||||
|
//cc2420.setCCA(true);
|
||||||
|
|
||||||
|
/* is this ok ?? */
|
||||||
|
handleEndOfReception();
|
||||||
|
//recv.nextByte(false, (byte)0);
|
||||||
|
|
||||||
|
lastEventTime = mote.getSimulation().getSimulationTime();
|
||||||
|
lastEvent = RadioEvent.RECEPTION_INTERFERED;
|
||||||
|
/*logger.debug("----- SKY RECEPTION INTERFERED -----");*/
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public JPanel getInterfaceVisualizer() {
|
||||||
|
// Location
|
||||||
|
JPanel wrapperPanel = new JPanel(new BorderLayout());
|
||||||
|
JPanel panel = new JPanel(new GridLayout(5, 2));
|
||||||
|
|
||||||
|
final JLabel statusLabel = new JLabel("");
|
||||||
|
final JLabel lastEventLabel = new JLabel("");
|
||||||
|
final JLabel channelLabel = new JLabel("");
|
||||||
|
final JLabel powerLabel = new JLabel("");
|
||||||
|
final JLabel ssLabel = new JLabel("");
|
||||||
|
final JButton updateButton = new JButton("Update");
|
||||||
|
|
||||||
|
panel.add(new JLabel("STATE:"));
|
||||||
|
panel.add(statusLabel);
|
||||||
|
|
||||||
|
panel.add(new JLabel("LAST EVENT:"));
|
||||||
|
panel.add(lastEventLabel);
|
||||||
|
|
||||||
|
panel.add(new JLabel("CHANNEL:"));
|
||||||
|
panel.add(channelLabel);
|
||||||
|
|
||||||
|
panel.add(new JLabel("OUTPUT POWER:"));
|
||||||
|
panel.add(powerLabel);
|
||||||
|
|
||||||
|
panel.add(new JLabel("SIGNAL STRENGTH:"));
|
||||||
|
JPanel smallPanel = new JPanel(new GridLayout(1, 2));
|
||||||
|
smallPanel.add(ssLabel);
|
||||||
|
smallPanel.add(updateButton);
|
||||||
|
panel.add(smallPanel);
|
||||||
|
|
||||||
|
updateButton.addActionListener(new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
channelLabel.setText(getChannel() + " (freq=" + getFrequency() + " MHz)");
|
||||||
|
powerLabel.setText(getCurrentOutputPower() + " dBm (indicator=" + getCurrentOutputPowerIndicator() + "/" + getOutputPowerIndicatorMax() + ")");
|
||||||
|
ssLabel.setText(getCurrentSignalStrength() + " dBm");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Observer observer;
|
||||||
|
this.addObserver(observer = new Observer() {
|
||||||
|
public void update(Observable obs, Object obj) {
|
||||||
|
if (isTransmitting()) {
|
||||||
|
statusLabel.setText("transmitting");
|
||||||
|
} else if (isReceiving()) {
|
||||||
|
statusLabel.setText("receiving");
|
||||||
|
} else if (radioOn /* mode != CC2420.MODE_TXRX_OFF */) {
|
||||||
|
statusLabel.setText("listening for traffic");
|
||||||
|
} else {
|
||||||
|
statusLabel.setText("HW off");
|
||||||
|
}
|
||||||
|
|
||||||
|
lastEventLabel.setText(lastEvent + " @ time=" + lastEventTime);
|
||||||
|
|
||||||
|
channelLabel.setText(getChannel() + " (freq=" + getFrequency() + " MHz)");
|
||||||
|
powerLabel.setText(getCurrentOutputPower() + " dBm (indicator=" + getCurrentOutputPowerIndicator() + "/" + getOutputPowerIndicatorMax() + ")");
|
||||||
|
ssLabel.setText(getCurrentSignalStrength() + " dBm");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.update(null, null);
|
||||||
|
|
||||||
|
wrapperPanel.add(BorderLayout.NORTH, panel);
|
||||||
|
|
||||||
|
// Saving observer reference for releaseInterfaceVisualizer
|
||||||
|
wrapperPanel.putClientProperty("intf_obs", observer);
|
||||||
|
return wrapperPanel;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Mote getMote() {
|
||||||
|
return mote;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Position getPosition() {
|
||||||
|
return mote.getInterfaces().getPosition();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Element> getConfigXML() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfigXML(Collection<Element> configXML, boolean visAvailable) {
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,198 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2008, 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: Radio802154PacketConverter.java,v 1.1 2009/11/10 12:54:39 joxe Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
package se.sics.cooja.emulatedmote;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import se.sics.cooja.ConvertedRadioPacket;
|
||||||
|
import se.sics.cooja.RadioPacket;
|
||||||
|
import se.sics.cooja.util.StringUtils;
|
||||||
|
/**
|
||||||
|
* Converts radio packets between X-MAC/802.15.4 nodes and COOJA.
|
||||||
|
* Handles radio driver specifics such as length header and CRC footer.
|
||||||
|
*
|
||||||
|
* TODO: Needs to fix 802.15.4 parsing of packets to really convert them...
|
||||||
|
* @author Fredrik Osterlind, Joakim Eriksson
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Radio802154PacketConverter {
|
||||||
|
private static Logger logger = Logger.getLogger(Radio802154PacketConverter.class);
|
||||||
|
|
||||||
|
public static final boolean WITH_PREAMBLE = true;
|
||||||
|
public static final boolean WITH_SYNCH = true;
|
||||||
|
public static final boolean WITH_XMAC = true;
|
||||||
|
public static final boolean WITH_CHECKSUM = false;
|
||||||
|
public static final boolean WITH_TIMESTAMP = true;
|
||||||
|
public static final boolean WITH_FOOTER = true;
|
||||||
|
|
||||||
|
public static byte[] fromCoojaToCC2420(RadioPacket packet) {
|
||||||
|
byte cc2420Data[] = new byte[6+127];
|
||||||
|
int pos = 0;
|
||||||
|
byte packetData[] = packet.getPacketData();
|
||||||
|
byte len; /* total packet minus preamble(4), synch(1) and length(1) */
|
||||||
|
short accumulatedCRC = 0;
|
||||||
|
|
||||||
|
/* 4 bytes preamble */
|
||||||
|
if (WITH_PREAMBLE) {
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 1 byte synch */
|
||||||
|
if (WITH_SYNCH) {
|
||||||
|
cc2420Data[pos++] = 0x7A;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 1 byte length */
|
||||||
|
len = (byte) packetData.length;
|
||||||
|
if (WITH_XMAC) {
|
||||||
|
len += 6;
|
||||||
|
}
|
||||||
|
if (WITH_CHECKSUM) {
|
||||||
|
len += 2;
|
||||||
|
}
|
||||||
|
if (WITH_TIMESTAMP) {
|
||||||
|
len += 3;
|
||||||
|
}
|
||||||
|
if (WITH_FOOTER) {
|
||||||
|
len += 2;
|
||||||
|
}
|
||||||
|
cc2420Data[pos++] = len;
|
||||||
|
|
||||||
|
/* (TODO) 4 byte X-MAC */
|
||||||
|
if (WITH_XMAC) {
|
||||||
|
cc2420Data[pos++] = 1; /* TYPE_DATA */
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add((byte)0, accumulatedCRC);
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add((byte)0, accumulatedCRC);
|
||||||
|
cc2420Data[pos++] = 0; /* XXX sender: 0.0 */
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add((byte)0, accumulatedCRC);
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add((byte)0, accumulatedCRC);
|
||||||
|
cc2420Data[pos++] = 0; /* XXX receiver: 0.0 */
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add((byte)0, accumulatedCRC);
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add((byte)0, accumulatedCRC);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Payload */
|
||||||
|
for (byte b : packetData) {
|
||||||
|
accumulatedCRC = CRCCoder.crc16Add(b, accumulatedCRC);
|
||||||
|
}
|
||||||
|
System.arraycopy(packetData, 0, cc2420Data, pos, packetData.length);
|
||||||
|
pos += packetData.length;
|
||||||
|
|
||||||
|
/* 2 bytes checksum */
|
||||||
|
if (WITH_CHECKSUM) {
|
||||||
|
cc2420Data[pos++] = (byte) (accumulatedCRC & 0xff);
|
||||||
|
cc2420Data[pos++] = (byte) ((accumulatedCRC >> 8) & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (TODO) 3 bytes timestamp */
|
||||||
|
if (WITH_TIMESTAMP) {
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
cc2420Data[pos++] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (TODO) 2 bytes footer */
|
||||||
|
if (WITH_FOOTER) {
|
||||||
|
cc2420Data[pos++] = 0; /* RSSI */
|
||||||
|
cc2420Data[pos++] = (byte) 0x80; /* CRC and CORRELATION */
|
||||||
|
}
|
||||||
|
|
||||||
|
byte cc2420DataStripped[] = new byte[pos];
|
||||||
|
System.arraycopy(cc2420Data, 0, cc2420DataStripped, 0, pos);
|
||||||
|
|
||||||
|
/*logger.info("Data length: " + cc2420DataStripped.length);*/
|
||||||
|
return cc2420DataStripped;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConvertedRadioPacket fromCC2420ToCooja(byte[] data) {
|
||||||
|
int pos = 0;
|
||||||
|
int len; /* Payload */
|
||||||
|
int originalLen;
|
||||||
|
|
||||||
|
/* Use some CC2420/MAC specific field such as X-MAC response */
|
||||||
|
|
||||||
|
/* (IGNORED) 4 bytes preamble */
|
||||||
|
if (WITH_PREAMBLE) {
|
||||||
|
pos += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (IGNORED) 1 byte synch */
|
||||||
|
if (WITH_SYNCH) {
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 1 byte length */
|
||||||
|
len = data[pos];
|
||||||
|
originalLen = len;
|
||||||
|
pos += 1;
|
||||||
|
|
||||||
|
/* (IGNORED) 4 byte X-MAC */
|
||||||
|
if (WITH_XMAC) {
|
||||||
|
pos += 6;
|
||||||
|
len -= 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (IGNORED) 2 bytes checksum */
|
||||||
|
if (WITH_CHECKSUM) {
|
||||||
|
len -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (IGNORED) 3 bytes timestamp */
|
||||||
|
if (WITH_TIMESTAMP) {
|
||||||
|
len -= 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (IGNORED) 2 bytes footer */
|
||||||
|
if (WITH_FOOTER) {
|
||||||
|
len -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*logger.info("Payload pos: " + pos);
|
||||||
|
logger.info("Payload length: " + len);*/
|
||||||
|
|
||||||
|
byte originalData[] = new byte[originalLen];
|
||||||
|
System.arraycopy(data, 6, originalData, 0, originalLen);
|
||||||
|
if (len < 0) {
|
||||||
|
logger.warn("No cross-level conversion available: negative packet length");
|
||||||
|
return new ConvertedRadioPacket(new byte[0], originalData);
|
||||||
|
}
|
||||||
|
byte convertedData[] = new byte[len];
|
||||||
|
System.arraycopy(data, pos, convertedData, 0, len);
|
||||||
|
return new ConvertedRadioPacket(convertedData, originalData);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
tools/cooja/java/se/sics/cooja/emulatedmote/RadioByte.java
Normal file
49
tools/cooja/java/se/sics/cooja/emulatedmote/RadioByte.java
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2009, 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: RadioByte.java,v 1.1 2009/11/10 12:54:39 joxe Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
package se.sics.cooja.emulatedmote;
|
||||||
|
import se.sics.cooja.RadioPacket;
|
||||||
|
|
||||||
|
public class RadioByte implements RadioPacket {
|
||||||
|
private byte[] data = new byte[1];
|
||||||
|
|
||||||
|
public RadioByte(byte data) {
|
||||||
|
this.data[0] = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RadioByte(int intData) {
|
||||||
|
this.data[0] = (byte) intData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getPacketData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue