📄 contikiradio.java
字号:
/* * 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: ContikiRadio.java,v 1.23 2008/10/28 15:31:51 fros4943 Exp $ */package se.sics.cooja.contikimote.interfaces;import java.util.*;import javax.swing.*;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.*;import org.apache.log4j.Logger;import org.jdom.Element;import se.sics.cooja.*;import se.sics.cooja.contikimote.ContikiMoteInterface;import se.sics.cooja.interfaces.PolledAfterActiveTicks;import se.sics.cooja.interfaces.Position;import se.sics.cooja.interfaces.Radio;import se.sics.cooja.radiomediums.UDGM;/** * Packet radio transceiver mote interface. * * To simulate transmission rates, the underlying Contiki system is * locked in TX or RX states using multi-threading library. * * Contiki variables: * <ul> * <li>char simTransmitting (1=mote radio is transmitting) * <li>char simReceiving (1=mote radio is receiving) * <li>char simInPolled * <p> * <li>int simInSize (size of received data packet) * <li>byte[] simInDataBuffer (data of received data packet) * <p> * <li>int simOutSize (size of transmitted data packet) * <li>byte[] simOutDataBuffer (data of transmitted data packet) * <p> * <li>char simRadioHWOn (radio hardware status (on/off)) * <li>int simSignalStrength (heard radio signal strength) * <li>int simLastSignalStrength * <li>char simPower (number indicating power output) * <li>int simRadioChannel (number indicating current channel) * </ul> * <p> * * Core interface: * <ul> * <li>radio_interface * </ul> * <p> * * This observable notifies at radio state changes during RX and TX. * * @see #getLastEvent() * @see UDGM * * @author Fredrik 謘terlind */public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledAfterActiveTicks { private Mote myMote; private SectionMoteMemory myMoteMemory; private static Logger logger = Logger.getLogger(ContikiRadio.class); /** * Approximate energy consumption of an active radio. ESB measured energy * consumption is 5 mA. TODO Measure energy consumption */ public final double ENERGY_CONSUMPTION_RADIO_mA; /** * Approximate transmission rate (kbps), used to calculate transmission * duration of packets. Each transmission is assumed to use an encoding with * an overhead nrBytesx0.25 (such as GCR), and a fixed overhead of 34 bytes. */ public final double RADIO_TRANSMISSION_RATE_kbps; private final boolean RAISES_EXTERNAL_INTERRUPT; private double energyListeningRadioPerTick = -1; private RadioPacket packetToMote = null; private RadioPacket packetFromMote = null; private boolean radioOn = true; private double myEnergyConsumption = 0.0; private boolean isTransmitting = false; private boolean isInterfered = false; private int transmissionEndTime = -1; private RadioEvent lastEvent = RadioEvent.UNKNOWN; private int lastEventTime = 0; private int oldOutputPowerIndicator = -1; /** * Creates an interface to the radio at mote. * * @param mote Mote * * @see Mote * @see se.sics.cooja.MoteInterfaceHandler */ public ContikiRadio(Mote mote) { // Read class configurations of this mote type ENERGY_CONSUMPTION_RADIO_mA = mote.getType().getConfig().getDoubleValue( ContikiRadio.class, "ACTIVE_CONSUMPTION_mA"); RAISES_EXTERNAL_INTERRUPT = mote.getType().getConfig().getBooleanValue( ContikiRadio.class, "EXTERNAL_INTERRUPT_bool"); RADIO_TRANSMISSION_RATE_kbps = mote.getType().getConfig().getDoubleValue( ContikiRadio.class, "RADIO_TRANSMISSION_RATE_kbps"); this.myMote = mote; this.myMoteMemory = (SectionMoteMemory) mote.getMemory(); // Calculate energy consumption of a listening radio if (energyListeningRadioPerTick < 0) { energyListeningRadioPerTick = ENERGY_CONSUMPTION_RADIO_mA * 0.001; } radioOn = myMoteMemory.getByteValueOf("simRadioHWOn") == 1; } /* Contiki mote interface support */ public static String[] getCoreInterfaceDependencies() { return new String[] { "radio_interface" }; } /* Packet radio support */ public RadioPacket getLastPacketTransmitted() { return packetFromMote; } public RadioPacket getLastPacketReceived() { return packetToMote; } public void setReceivedPacket(RadioPacket packet) { packetToMote = packet; } /* General radio support */ public boolean isOn() { return radioOn; } public boolean isTransmitting() { return isTransmitting; } public boolean isReceiving() { if (isLockedAtReceiving()) { return true; } return myMoteMemory.getIntValueOf("simInSize") != 0; } public boolean isInterfered() { return isInterfered; } public int getChannel() { return myMoteMemory.getIntValueOf("simRadioChannel"); } public void signalReceptionStart() { packetToMote = null; if (isInterfered() || isReceiving() || isTransmitting()) { interfereAnyReception(); return; } lockInReceivingMode(); lastEventTime = myMote.getSimulation().getSimulationTime(); lastEvent = RadioEvent.RECEPTION_STARTED; this.setChanged(); this.notifyObservers(); } public void signalReceptionEnd() { if (isInterfered() || packetToMote == null) { // Reset interfered flag isInterfered = false; // Reset data packetToMote = null; myMoteMemory.setIntValueOf("simInSize", 0); // Unlock (if locked) myMoteMemory.setByteValueOf("simReceiving", (byte) 0); return; } // Unlock (if locked) myMoteMemory.setByteValueOf("simReceiving", (byte) 0); // Set data myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length); myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData()); lastEventTime = myMote.getSimulation().getSimulationTime(); lastEvent = RadioEvent.RECEPTION_FINISHED; this.setChanged(); this.notifyObservers(); } public RadioEvent getLastEvent() { return lastEvent; } public void interfereAnyReception() { if (!isInterfered()) { isInterfered = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -