using new radio packet format

This commit is contained in:
fros4943 2008-03-18 12:52:01 +00:00
parent 1270545cc2
commit e765735bad
3 changed files with 58 additions and 115 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: ContikiRadio.java,v 1.18 2008/03/17 09:50:27 fros4943 Exp $
* $Id: ContikiRadio.java,v 1.19 2008/03/18 12:52:01 fros4943 Exp $
*/
package se.sics.cooja.contikimote.interfaces;
@ -39,12 +39,11 @@ import org.jdom.Element;
import se.sics.cooja.*;
import se.sics.cooja.contikimote.ContikiMoteInterface;
import se.sics.cooja.interfaces.PacketRadio;
import se.sics.cooja.interfaces.Position;
import se.sics.cooja.interfaces.Radio;
/**
* This class represents a radio transciever. In order to simulate different
* This class represents a radio transceiver. In order to simulate different
* transmission rates, the underlying Contiki system can be locked in either
* transmission or reception states (using multi-threading). When a transmission
* is initiated, it will automatically lock the Contiki system. When a packet is
@ -77,8 +76,7 @@ import se.sics.cooja.interfaces.Radio;
*
* @author Fredrik Osterlind
*/
public class ContikiRadio extends Radio implements ContikiMoteInterface,
PacketRadio {
public class ContikiRadio extends Radio implements ContikiMoteInterface {
private Mote myMote;
private SectionMoteMemory myMoteMemory;
@ -102,9 +100,9 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface,
private double energyListeningRadioPerTick = -1;
private byte[] packetToMote = null;
private RadioPacket packetToMote = null;
private byte[] packetFromMote = null;
private RadioPacket packetFromMote = null;
private boolean radioOn = true;
@ -157,16 +155,16 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface,
}
/* Packet radio support */
public byte[] getLastPacketTransmitted() {
public RadioPacket getLastPacketTransmitted() {
return packetFromMote;
}
public byte[] getLastPacketReceived() {
public RadioPacket getLastPacketReceived() {
return packetToMote;
}
public void setReceivedPacket(byte[] data) {
packetToMote = data;
public void setReceivedPacket(RadioPacket packet) {
packetToMote = packet;
}
/* General radio support */
@ -227,8 +225,8 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface,
myMoteMemory.setByteValueOf("simReceiving", (byte) 0);
// Set data
myMoteMemory.setIntValueOf("simInSize", packetToMote.length);
myMoteMemory.setByteArray("simInDataBuffer", packetToMote);
myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length);
myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData());
lastEventTime = myMote.getSimulation().getSimulationTime();
lastEvent = RadioEvent.RECEPTION_FINISHED;
@ -374,8 +372,8 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface,
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
return;
}
packetFromMote = myMoteMemory.getByteArray("simOutDataBuffer", size);
if (packetFromMote == null || packetFromMote.length == 0) {
packetFromMote = new COOJARadioPacket(myMoteMemory.getByteArray("simOutDataBuffer", size));
if (packetFromMote.getPacketData() == null || packetFromMote.getPacketData().length == 0) {
logger.warn("Skipping zero sized Contiki packet (no buffer)");
myMoteMemory.setByteValueOf("simTransmitting", (byte) 0);
return;

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: DisturberRadio.java,v 1.5 2008/03/17 09:50:27 fros4943 Exp $
* $Id: DisturberRadio.java,v 1.6 2008/03/18 12:54:39 fros4943 Exp $
*/
package se.sics.cooja.motes;
@ -43,16 +43,16 @@ import se.sics.cooja.*;
import se.sics.cooja.interfaces.*;
/**
* This radio periodically transmits data on a configurable channel.
* This radio transmits data packet over and over again on a configurable channel.
*
* @author Fredrik Osterlind, Thiemo Voigt
*/
public class DisturberRadio extends Radio implements PacketRadio {
public class DisturberRadio extends Radio {
private Mote myMote;
private static Logger logger = Logger.getLogger(DisturberRadio.class);
private static byte[] packetFromMote = new byte[] { 1, 2, 3, 4, 5 };
private RadioPacket packetFromMote = new COOJARadioPacket(new byte[] { 1, 2, 3, 4, 5 });
private boolean transmitting = false;
@ -80,15 +80,15 @@ public class DisturberRadio extends Radio implements PacketRadio {
}
/* Packet radio support */
public byte[] getLastPacketTransmitted() {
public RadioPacket getLastPacketTransmitted() {
return packetFromMote;
}
public byte[] getLastPacketReceived() {
public RadioPacket getLastPacketReceived() {
return null;
}
public void setReceivedPacket(byte[] data) {
public void setReceivedPacket(RadioPacket packet) {
}

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: AbstractRadioMedium.java,v 1.3 2007/04/23 11:33:54 fros4943 Exp $
* $Id: AbstractRadioMedium.java,v 1.4 2008/03/18 12:57:04 fros4943 Exp $
*/
package se.sics.cooja.radiomediums;
@ -38,19 +38,19 @@ import se.sics.cooja.*;
import se.sics.cooja.interfaces.*;
/**
* Abstract radio medium provides basic functionality for implementing radio
* Abstract radio medium provides basic functionality for implementing radio
* mediums.
*
* It handles radio registrations, radio loggers, active connections and
*
* It handles radio registrations, radio loggers, active connections and
* observes all registered radio interfaces.
*
*
* @author Fredrik Osterlind
*/
public abstract class AbstractRadioMedium extends RadioMedium {
private static Logger logger = Logger.getLogger(AbstractRadioMedium.class);
private Vector<Radio> registeredRadios = new Vector<Radio>();
private Vector<RadioConnection> activeConnections = new Vector<RadioConnection>();
private Vector<RadioConnection> finishedConnections = new Vector<RadioConnection>();
@ -67,16 +67,14 @@ public abstract class AbstractRadioMedium extends RadioMedium {
private RadioConnection[] lastTickConnections = null;
private ConnectionLogger myLogger = null;
/**
* This constructor should always be called from implemented radio mediums.
*
*
* @param simulation Simulation
*/
public AbstractRadioMedium(Simulation simulation) {
}
/**
* @return All registered radios
*/
@ -93,59 +91,17 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/**
* Creates a new connection from given radio.
*
* This method is responsible for determining which radio should receive or be
* interfered by the transmission.
*
*
* Determines which radios should receive or be interfered by the transmission.
*
* @param radio
* Transmitting radio
* @return New registered connection
*/
abstract public RadioConnection createConnections(Radio radio);
/**
* Forward given packet from source to all destinations in given connection.
* This method is called sometime during an active connection.
*
* @param connection
* Radio connection
* @param packetData
* Packet data
*/
public void forwardPacket(RadioConnection connection, byte[] packetData) {
Radio sourceRadio = connection.getSource();
if (packetData == null || packetData.length == 0) {
logger.warn("Trying to forward null packet");
return;
}
if (sourceRadio instanceof ByteRadio) {
// Byte radios only forwards packets to packet radios
for (Radio receivingRadio : connection.getDestinations()) {
if (receivingRadio instanceof ByteRadio) {
// Do nothing (data will be forwarded on byte-per-byte basis)
} else if (receivingRadio instanceof PacketRadio) {
// Forward packet data
((PacketRadio) receivingRadio).setReceivedPacket(packetData);
} else {
logger.warn("Packet was not forwarded correctly");
}
}
} else if (sourceRadio instanceof PacketRadio) {
// Packet radios forwards packets to all packet radios
for (Radio receivingRadio : connection.getDestinations()) {
if (receivingRadio instanceof PacketRadio) {
// Forward packet data
((PacketRadio) receivingRadio).setReceivedPacket(packetData);
} else {
logger.warn("Packet was not forwarded correctly");
}
}
}
}
/**
* This method should update all radio interfaces' signal strengths according to
* Updates all radio interfaces' signal strengths according to
* the current active connections.
*/
abstract public void updateSignalStrengths();
@ -153,7 +109,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
/**
* Remove given radio from any active connections.
* This method can be called if a radio node falls asleep or is removed.
*
*
* @param radio Radio
*/
private void removeFromActiveConnections(Radio radio) {
@ -181,12 +137,13 @@ public abstract class AbstractRadioMedium extends RadioMedium {
}
}
}
if (connToRemove != null)
if (connToRemove != null) {
activeConnections.remove(connToRemove);
}
}
/**
* This observer is responsible for detecting radio interface events, for example
* This observer is responsible for detecting radio interface events, for example
* new transmissions.
*/
private Observer radioEventsObserver = new Observer() {
@ -231,7 +188,7 @@ public abstract class AbstractRadioMedium extends RadioMedium {
} else if (event == Radio.RadioEvent.TRANSMISSION_STARTED) {
/* Create radio connections */
RadioConnection newConnection = createConnections((Radio) radio);
RadioConnection newConnection = createConnections(radio);
if (newConnection != null) {
activeConnections.add(newConnection);
}
@ -273,8 +230,8 @@ public abstract class AbstractRadioMedium extends RadioMedium {
// Wake up tick observer
radioMediumObservable.setRadioMediumChanged();
} else if (event == Radio.RadioEvent.BYTE_TRANSMITTED) {
/* Forward byte */
} else if (event == Radio.RadioEvent.CUSTOM_DATA_TRANSMITTED) {
/* Forward custom data */
// Find corresponding connection of radio
RadioConnection connection = null;
@ -288,12 +245,11 @@ public abstract class AbstractRadioMedium extends RadioMedium {
if (connection == null) {
logger.fatal("Can't find active connection to forward byte in");
} else {
byte b = ((ByteRadio) radio).getLastByteTransmitted();
long delay = ((ByteRadio) radio).getLastByteTransmittedDelay();
Object data = ((CustomDataRadio) radio).getLastCustomDataTransmitted();
for (Radio dstRadio : connection.getDestinations()) {
if (dstRadio instanceof ByteRadio) {
((ByteRadio) dstRadio).receiveByte(b, delay);
if (dstRadio instanceof CustomDataRadio) {
((CustomDataRadio) dstRadio).receiveCustomData(data);
}
}
}
@ -313,8 +269,14 @@ public abstract class AbstractRadioMedium extends RadioMedium {
if (connection == null) {
logger.fatal("Can't find active connection to forward byte in");
} else {
byte[] packetData = ((PacketRadio) radio).getLastPacketTransmitted();
forwardPacket(connection, packetData);
byte[] packetData = radio.getLastPacketTransmitted().getPacketData();
Radio srcRadio = connection.getSource();
for (Radio dstRadio : connection.getDestinations()) {
if (!(srcRadio instanceof CustomDataRadio) || !(dstRadio instanceof CustomDataRadio)) {
dstRadio.setReceivedPacket(new COOJARadioPacket(packetData));
}
}
}
} else if (event == Radio.RadioEvent.UNKNOWN) {
@ -324,15 +286,13 @@ public abstract class AbstractRadioMedium extends RadioMedium {
}
}
};
/**
* This observer is responsible for making last tick connections available to
* external observers, as well as forwarding finished connections to any
* registered connection logger.
*
* external observers.
*
* @see #getLastTickConnections()
* @see #setConnectionLogger(ConnectionLogger)
*/
private Observer tickObserver = new Observer() {
public void update(Observable obs, Object obj) {
@ -344,21 +304,17 @@ public abstract class AbstractRadioMedium extends RadioMedium {
}
// Do nothing if radio medium unchanged
if (!radioMediumObservable.hasChanged())
if (!radioMediumObservable.hasChanged()) {
return;
}
// Log any new finished connections
if (finishedConnections.size() > 0) {
lastTickConnections = new RadioConnection[finishedConnections.size()];
for (int i = 0; i < finishedConnections.size(); i++)
for (int i = 0; i < finishedConnections.size(); i++) {
lastTickConnections[i] = finishedConnections.get(i);
finishedConnections.clear();
// Notify radio medium logger of the finished transmissions
if (myLogger != null) {
for (RadioConnection conn : lastTickConnections)
myLogger.logConnection(conn);
}
finishedConnections.clear();
}
// Notify all other radio medium observers
@ -381,19 +337,12 @@ public abstract class AbstractRadioMedium extends RadioMedium {
isTickObserver = true;
}
// Warn if radio medium does not support radio
if (!(radio instanceof PacketRadio)) {
logger
.warn("Radio medium does not support radio (no transmission supported)");
}
// Register and start observing radio
registeredRadios.add(radio);
radio.addObserver(radioEventsObserver);
// Set initial signal strenth
// Set initial signal strength
updateSignalStrengths();
//TODO radio.setCurrentSignalStrength(SS_NOTHING);
}
}
@ -425,8 +374,4 @@ public abstract class AbstractRadioMedium extends RadioMedium {
return lastTickConnections;
}
public void setConnectionLogger(ConnectionLogger connection) {
myLogger = connection;
}
}