[Tinyos-contrib-commits]
CVS: tinyos-1.x/contrib/ustutt/ncunit/java/avrora/sim/radio
SimpleAir.java, NONE, 1.1 Channel.java, NONE, 1.1
Andreas Lachenmann
lachenmann at users.sourceforge.net
Tue Feb 20 04:33:09 PST 2007
Update of /cvsroot/tinyos/tinyos-1.x/contrib/ustutt/ncunit/java/avrora/sim/radio
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv4808/contrib/ustutt/ncunit/java/avrora/sim/radio
Added Files:
SimpleAir.java Channel.java
Log Message:
added files to repository
--- NEW FILE: SimpleAir.java ---
/**
* Copyright (c) 2004-2005, Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of the University of California, Los Angeles 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 COPYRIGHT HOLDERS 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 COPYRIGHT
* OWNER 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 avrora.sim.radio;
import avrora.sim.Simulator;
import avrora.sim.clock.IntervalSynchronizer;
import avrora.sim.clock.Synchronizer;
import avrora.sim.mcu.ADC;
import java.util.HashSet;
/**
* Very simple implementation of radio air. It assumes a lossless environment where all radios are able to
* communicate with each other. This simple air is blind to the frequencies used in transmission (i.e. it
* assumes that all frequencies are really the same).
* <p/>
* This class should provide the proper scheduling policy with respect to threads that more complicated radio
* implementations can use the time scheduling policy and only overload the delivery policy.
*
* @author Daniel Lee
* @author Ben L. Titzer
*/
public class SimpleAir implements RadioAir {
protected final HashSet radios;
protected final Channel radioChannel;
protected final IntervalSynchronizer synchronizer;
private static final int INTERVALS = 1;
private static final int sampleTime = 13 * 64;
private static final int TRANSFER_TIME = Radio.TRANSFER_TIME;
private static final int INTERVAL_TIME = TRANSFER_TIME * INTERVALS;
public SimpleAir() {
radios = new HashSet();
radioChannel = new Channel(8 * INTERVALS, INTERVAL_TIME, true, 0);
synchronizer = new IntervalSynchronizer(INTERVAL_TIME, new MeetEvent());
}
/**
* The <code>addRadio()</code> method adds a new radio to this radio model.
* @param r the radio to add to this air implementation
*/
public synchronized void addRadio(Radio r) {
radios.add(r);
r.setAir(this);
// synchronizer.addNode(r.getSimulatorThread().getNode());
}
/**
* The <code>removeRadio()</code> method removes a radio from this radio model.
* @param r the radio to remove from this air implementation
*/
public synchronized void removeRadio(Radio r) {
radios.remove(r);
// synchronizer.removeNode(r.getSimulatorThread().getNode());
}
/**
* The <code>transmit()</code> method is called by a radio when it begins to transmit
* a packet over the air. The radio packet should be delivered to those radios in
* range which are listening, according to the radio model.
* @param r the radio transmitting this packet
* @param f the radio packet transmitted into the air
*/
public synchronized void transmit(Radio r, Radio.Transmission f) {
radioChannel.write(f.data, 8, r.getSimulator().getClock().getCount());
}
protected class MeetEvent implements Simulator.Event {
long meets;
public void fire() {
radioChannel.advance();
}
}
/**
* The <code>sampleRSSI()</code> method is called by a radio when it wants to
* sample the RSSI value of the air around it at the current time. The air may
* need to block (i.e. wait for neighbors) because this thread may be ahead
* of other threads in global time. The underlying air implementation should use
* a <code>Synchronizer</code> for this purpose.
* @param r the radio sampling the RSSI value
* @return an integer value representing the received signal strength indicator
*/
public int sampleRSSI(Radio r) {
long t = r.getSimulator().getClock().getCount();
synchronizer.waitForNeighbors(t);
return radioChannel.occupied(t - sampleTime, t) ? 0x0 : ADC.VBG_LEVEL;
}
/**
* The <code>readChannel()</code> method reads the value of the channel at the current
* time so that the last 8 bits transmitted (where the bits are 0 if there are no
* transmissions) are returned.
* @param r the radio sampling the channel
* @return the last 8 bits transmitted in the channel
*/
public byte readChannel(Radio r) {
Simulator sim = r.getSimulator();
long time = sim.getClock().getCount();
synchronizer.waitForNeighbors(time);
return (byte)radioChannel.read(time, 8);
}
/**
* The <code>getSynchronizer()</code> method gets the synchronizer for this air
* implementation.
* @return a reference to the synchronizer for this radio model.
*/
public Synchronizer getSynchronizer() {
return synchronizer;
}
}
--- NEW FILE: Channel.java ---
/**
* Copyright (c) 2004-2005, Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of the University of California, Los Angeles 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 COPYRIGHT HOLDERS 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 COPYRIGHT
* OWNER 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 avrora.sim.radio;
import avrora.util.Arithmetic;
/**
* The <code>Channel</code> class implements a serial channel that represents a communication
* channel where bits are sent one by one. The channel allows bits to be written into the channel
* at a particular time and represents their serial transmission over time by an array.
*
* <p>
* The channel is used in simulating radio transmissions; all transmissions write into the channel,
* and all samples read from the channel.
*
*
* @author Ben L. Titzer
*/
public class Channel {
protected final int bits;
protected final long period;
protected final long bitPeriod;
protected long globalTime;
protected final boolean invert;
protected final boolean[] channelValues;
protected final boolean[] channelWritten;
public Channel(int bits, long period, boolean invert, long globalTime) {
this.bits = bits;
this.period = period;
this.bitPeriod = period / bits;
this.invert = invert;
this.globalTime = globalTime;
channelValues = new boolean[bits * 3];
channelWritten = new boolean[bits * 3];
}
/**
* The <code>write()</code> method writes a value into the channel, with the given bit length
* at the given global time.
* @param value the value to write into the channel
* @param bits the number of bits to write
* @param time the global time at which the write takes place
*/
public void write(int value, int bits, long time) {
int off = channelOffset(time);
if ( invert ) value = ~value;
for ( int cntr = 0; cntr < 8; cntr++ ) {
int ind = cntr+off;
boolean bit = Arithmetic.getBit(value, (bits-1)-cntr);
channelValues[ind] |= bit;
channelWritten[ind] = true;
}
}
/**
* The <code>advance()</code> method advances the channel to the next period.
*/
public void advance() {
globalTime += period;
// copy the values written into the channel
for ( int cntr = 0; cntr < bits * 2; cntr++ ) {
channelValues[cntr] = channelValues[cntr+8];
channelWritten[cntr] = channelWritten[cntr+8];
}
// erase the values written for next interval
for ( int cntr = bits * 2; cntr < bits * 3; cntr++ ) {
channelValues[cntr] = false;
channelWritten[cntr] = false;
}
}
/**
* The <code>read()</code> method reads the value of the channel at the current time, going
* back by the number of bits.
* @param time the global time at which to read the channel
* @return a value representing the channel contents at this global time
*/
public int read(long time, int bits) {
int value = 0;
int off = channelOffset(time) - bits;
for ( int cntr = 0; cntr < bits; cntr++ ) {
value = Arithmetic.setBit(value << 1, 0, channelValues[off+cntr]);
}
return value;
}
/**
* The <code>occupied()</code> method tests whether this channel has been written to
* in the window of time specified.
* @param start the start of the interval to check
* @param end the end of the interval to check
* @return true if the channel was written to during the specified time interval; false otherwise
*/
public boolean occupied(long start, long end) {
int off = channelOffset(end);
int diff = (int)((end - start + bitPeriod - 1) / bitPeriod);
for ( int cntr = off - diff - 1; cntr < off; cntr++) {
if ( channelWritten[cntr] ) return true;
}
return false;
}
protected int channelOffset(long gtime) {
long diff = gtime - globalTime;
return (int)(diff / period + bits);
}
public long getGlobalTime() {
return globalTime;
}
}
More information about the Tinyos-contrib-commits
mailing list