📄 contikiradio.java
字号:
lastEvent = RadioEvent.RECEPTION_INTERFERED; lastEventTime = myMote.getSimulation().getSimulationTime(); this.setChanged(); this.notifyObservers(); } } public double getCurrentOutputPower() { // TODO Implement method logger.warn("Not implemented, always returning 0 dBm"); return 0; } public int getOutputPowerIndicatorMax() { return 100; } public int getCurrentOutputPowerIndicator() { return myMoteMemory.getByteValueOf("simPower"); } public double getCurrentSignalStrength() { return myMoteMemory.getIntValueOf("simSignalStrength"); } public void setCurrentSignalStrength(double signalStrength) { myMoteMemory.setIntValueOf("simSignalStrength", (int) signalStrength); } public Position getPosition() { return myMote.getInterfaces().getPosition(); } /** * @return True if locked at transmitting */ private boolean isLockedAtTransmitting() { return myMoteMemory.getByteValueOf("simTransmitting") == 1; } /** * @return True if locked at receiving */ private boolean isLockedAtReceiving() { return myMoteMemory.getByteValueOf("simReceiving") == 1; } /** * Locks underlying Contiki system in receiving mode. This may, but does not * have to, be used during a simulated data transfer that takes longer than * one tick to complete. The system is unlocked by delivering the received * data to the mote. */ private void lockInReceivingMode() { // If mote is inactive, try to wake it up if (myMote.getState() != Mote.State.ACTIVE) { if (RAISES_EXTERNAL_INTERRUPT) { myMote.setState(Mote.State.ACTIVE); } if (myMote.getState() != Mote.State.ACTIVE) { return; } } // Lock core radio in receiving loop myMoteMemory.setByteValueOf("simReceiving", (byte) 1); } public void doActionsAfterTick() { /* Check if radio hardware status changed */ if (radioOn != (myMoteMemory.getByteValueOf("simRadioHWOn") == 1)) { radioOn = !radioOn; if (!radioOn) { // Reset status myMoteMemory.setByteValueOf("simReceiving", (byte) 0); myMoteMemory.setIntValueOf("simInSize", 0); myMoteMemory.setByteValueOf("simTransmitting", (byte) 0); myMoteMemory.setIntValueOf("simOutSize", 0); isTransmitting = false; lastEvent = RadioEvent.HW_OFF; } else { lastEvent = RadioEvent.HW_ON; } lastEventTime = myMote.getSimulation().getSimulationTime(); this.setChanged(); this.notifyObservers(); } if (!radioOn) { myEnergyConsumption = 0.0; return; } myEnergyConsumption = energyListeningRadioPerTick; // Check if radio output power changed if (myMoteMemory.getByteValueOf("simPower") != oldOutputPowerIndicator) { oldOutputPowerIndicator = myMoteMemory.getByteValueOf("simPower"); lastEvent = RadioEvent.UNKNOWN; this.setChanged(); this.notifyObservers(); } /* Are we transmitting but should stop? */ /* TODO Use time events */ if (isTransmitting && myMote.getSimulation().getSimulationTime() >= transmissionEndTime) { myMoteMemory.setByteValueOf("simTransmitting", (byte) 0); myMoteMemory.setIntValueOf("simOutSize", 0); isTransmitting = false; lastEventTime = myMote.getSimulation().getSimulationTime(); lastEvent = RadioEvent.TRANSMISSION_FINISHED; // TODO Energy consumption of transmitted packet? this.setChanged(); this.notifyObservers(); //logger.debug("----- CONTIKI TRANSMISSION ENDED -----"); } // Check if a new transmission should be started if (!isTransmitting && myMoteMemory.getByteValueOf("simTransmitting") == 1) { int size = myMoteMemory.getIntValueOf("simOutSize"); if (size <= 0) { logger.warn("Skipping zero sized Contiki packet (no size)"); myMoteMemory.setByteValueOf("simTransmitting", (byte) 0); return; } 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; } isTransmitting = true; // Calculate transmission duration (ms) int duration = (int) ((280 + 10 * size) / RADIO_TRANSMISSION_RATE_kbps); transmissionEndTime = myMote.getSimulation().getSimulationTime() + Math.max(1, duration); lastEventTime = myMote.getSimulation().getSimulationTime(); lastEvent = RadioEvent.TRANSMISSION_STARTED; this.setChanged(); this.notifyObservers(); //logger.debug("----- NEW CONTIKI TRANSMISSION DETECTED -----"); // Deliver packet right away lastEvent = RadioEvent.PACKET_TRANSMITTED; this.setChanged(); this.notifyObservers(); //logger.debug("----- CONTIKI PACKET DELIVERED -----"); } } 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()); 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) { statusLabel.setText("listening for traffic"); } else { statusLabel.setText("HW off"); } lastEventLabel.setText(lastEvent + " @ time=" + lastEventTime); channelLabel.setText("" + getChannel()); 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 double energyConsumption() { return myEnergyConsumption; } public Collection<Element> getConfigXML() { return null; } public void setConfigXML(Collection<Element> configXML, boolean visAvailable) { } public Mote getMote() { return myMote; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -