示例#1
0
  public void paintAfterMotes(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.BLACK);

    /* Paint last output below motes */
    Mote[] allMotes = simulation.getMotes();
    for (Mote mote : allMotes) {
      String msg = null;
      {
        IPAddress ipAddr = mote.getInterfaces().getIPAddress();
        if (ipAddr != null) {
          msg = ipAddr.getIPString();
        }
      }

      if (msg == null) {
        RimeAddress rimeAddr = mote.getInterfaces().getRimeAddress();
        if (rimeAddr != null) {
          msg = rimeAddr.getAddressString();
        }
      }

      if (msg == null) {
        continue;
      }

      Position pos = mote.getInterfaces().getPosition();
      Point pixel = visualizer.transformPositionToPixel(pos);

      int msgWidth = fm.stringWidth(msg);
      g.drawString(msg, pixel.x - msgWidth / 2, pixel.y + 2 * Visualizer.MOTE_RADIUS + 3);
    }
  }
示例#2
0
 public void setInactive() {
   simulation.deleteObserver(simObserver);
   for (Mote mote : simulation.getMotes()) {
     IPAddress ipAddr = mote.getInterfaces().getIPAddress();
     if (ipAddr != null) {
       ipAddr.deleteObserver(addrObserver);
     }
     RimeAddress rimeAddr = mote.getInterfaces().getRimeAddress();
     if (rimeAddr != null) {
       rimeAddr.deleteObserver(addrObserver);
     }
   }
 }
示例#3
0
        public void update(Observable obs, Object obj) {

          /* Observe Rime and IP addresses */
          for (Mote mote : simulation.getMotes()) {
            IPAddress ipAddr = mote.getInterfaces().getIPAddress();
            if (ipAddr != null) {
              ipAddr.addObserver(addrObserver);
            }
            RimeAddress rimeAddr = mote.getInterfaces().getRimeAddress();
            if (rimeAddr != null) {
              rimeAddr.addObserver(addrObserver);
            }
          }
          visualizer.repaint();
        }
    public String toString(boolean radioHW, boolean radioRXTX) {
      StringBuilder sb = new StringBuilder();
      String moteString = mote.toString().replace(' ', '_');

      sb.append(moteString + " MONITORED " + duration + " us\n");
      if (radioHW) {
        sb.append(
            String.format(
                    moteString + " ON " + (radioOn + " us ") + "%2.2f %%",
                    100.0 * getRadioOnRatio())
                + "\n");
      }
      if (radioRXTX) {
        sb.append(
            String.format(
                    moteString + " TX " + (radioTx + " us ") + "%2.2f %%",
                    100.0 * getRadioTxRatio())
                + "\n");
        sb.append(
            String.format(
                    moteString + " RX " + (radioRx + " us ") + "%2.2f %%",
                    100.0 * getRadioRxRatio())
                + "\n");
        sb.append(
            String.format(
                    moteString + " INT " + (radioInterfered + " us ") + "%2.2f %%",
                    100.0 * getRadioInterferedRatio())
                + "\n");
      }
      return sb.toString();
    }
示例#5
0
  public IPAddress(final Mote mote) {
    moteMem = (AddressMemory) mote.getMemory();

    /* Detect startup IP (only zeroes) */
    TimeEvent updateWhenAddressReady =
        new TimeEvent(0) {
          public void execute(long t) {
            if (!isVersion4() && !isVersion6()) {
              return;
            }

            String ipString = getIPString();
            ipString = ipString.replace(".", "");
            ipString = ipString.replace("0", "");
            if (!ipString.isEmpty()) {
              setChanged();
              notifyObservers();
              return;
            }

            /* Postpone until IP has been set */
            mote.getSimulation()
                .scheduleEvent(
                    this, mote.getSimulation().getSimulationTime() + Simulation.MILLISECOND);
            return;
          }
        };
    updateWhenAddressReady.execute(0);
  }
 public void setMoteID(int newID) {
   moteID = newID;
   moteMem.setIntValueOf("simMoteID", moteID);
   moteMem.setByteValueOf("simMoteIDChanged", (byte) 1);
   moteMem.setIntValueOf("simRandomSeed", (int) (mote.getSimulation().getRandomSeed() + newID));
   setChanged();
   notifyObservers();
 }
示例#7
0
  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;
    }
  }
    public MoteTracker(Mote mote) {
      this.simulation = mote.getSimulation();
      this.mote = mote;
      this.radio = mote.getInterfaces().getRadio();

      radioWasOn = radio.isRadioOn();
      if (radio.isTransmitting()) {
        lastRadioState = RadioState.TRANSMITTING;
      } else if (radio.isReceiving()) {
        lastRadioState = RadioState.RECEIVING;
      } else if (radio.isInterfered()) {
        lastRadioState = RadioState.INTERFERED;
      } else {
        lastRadioState = RadioState.IDLE;
      }
      lastUpdateTime = simulation.getSimulationTime();

      radio.addObserver(this);
    }
  private MoteTracker createMoteTracker(Mote mote) {
    final Radio moteRadio = mote.getInterfaces().getRadio();
    if (moteRadio == null) {
      return null;
    }

    /* Radio observer */
    MoteTracker tracker = new MoteTracker(mote);
    tracker.update(null, null);
    return tracker;
  }
示例#10
0
  /* 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();
  }
示例#11
0
  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();
  }
示例#12
0
  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 SerialSocketServer(Mote mote, Simulation simulation, final GUI gui) {
    super("Serial Socket (SERVER) (" + mote + ")", gui, false);
    this.mote = mote;

    LISTEN_PORT = 60000 + mote.getID();

    /* GUI components */
    if (GUI.isVisualized()) {
      Box northBox = Box.createHorizontalBox();
      northBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      statusLabel = configureLabel(northBox, "", "");

      Box mainBox = Box.createHorizontalBox();
      mainBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      inLabel = configureLabel(mainBox, "socket -> mote:", "0 bytes");
      outLabel = configureLabel(mainBox, "mote -> socket", "0 bytes");

      getContentPane().add(BorderLayout.NORTH, northBox);
      getContentPane().add(BorderLayout.CENTER, mainBox);
      pack();
    }

    /* Mote serial port */
    serialPort = (SerialPort) mote.getInterfaces().getLog();
    if (serialPort == null) {
      throw new RuntimeException("No mote serial port");
    }

    try {
      logger.info("Listening on port: " + LISTEN_PORT);
      if (GUI.isVisualized()) {
        statusLabel.setText("Listening on port: " + LISTEN_PORT);
      }
      server = new ServerSocket(LISTEN_PORT);
      new Thread() {
        public void run() {
          while (server != null) {
            try {
              client = server.accept();
              in = new DataInputStream(client.getInputStream());
              out = new DataOutputStream(client.getOutputStream());
              out.flush();

              startSocketReadThread(in);
              if (GUI.isVisualized()) {
                statusLabel.setText("Client connected: " + client.getInetAddress());
              }
            } catch (IOException e) {
              logger.fatal("Listening thread shut down: " + e.getMessage());
              server = null;
              cleanupClient();
              break;
            }
          }
        }
      }.start();
    } catch (Exception e) {
      throw (RuntimeException)
          new RuntimeException("Connection error: " + e.getMessage()).initCause(e);
    }

    /* Observe serial port for outgoing data */
    serialPort.addSerialDataObserver(
        serialDataObserver =
            new Observer() {
              public void update(Observable obs, Object obj) {
                try {
                  if (out == null) {
                    /*logger.debug("out is null");*/
                    return;
                  }
                  out.write(serialPort.getLastSerialData());
                  out.flush();
                  outBytes++;
                  if (GUI.isVisualized()) {
                    outLabel.setText(outBytes + " bytes");
                  }
                } catch (IOException e) {
                  cleanupClient();
                }
              }
            });
  }
示例#14
0
 public Position getPosition() {
   return mote.getInterfaces().getPosition();
 }
  public MoteSerialSocketConnection(Mote mote, String server, int serverPort) throws IOException {
    isConnected = true;

    /* Simulated -> socket */
    motePort = (SerialPort) mote.getInterfaces().getLog();
    motePort.addSerialDataObserver(
        moteObserver =
            new Observer() {
              public void update(Observable obs, Object obj) {
                try {
                  if (socketOut == null) {
                    return;
                  }
                  socketOut.write(motePort.getLastSerialData());
                  socketOut.flush();
                  toSocket++;

                  if (listeners != null) {
                    for (MoteSerialSocketConnectionListener dl : listeners) {
                      dl.dataTransferred(
                          MoteSerialSocketConnection.this,
                          toMote,
                          toSocket,
                          motePort.getLastSerialData());
                    }
                  }
                } catch (IOException e) {
                  e.printStackTrace();
                  logger.fatal("Write to socket error: " + e.getMessage(), e);
                  cleanup();
                }
              }
            });

    /* Socket -> simulated */
    socket = new Socket(server, serverPort);
    socketIn = new DataInputStream(socket.getInputStream());
    socketOut = new DataOutputStream(socket.getOutputStream());
    socketOut.flush();
    Thread socketThread =
        new Thread(
            new Runnable() {
              public void run() {
                int numRead = 0;
                byte[] data = new byte[1024];
                while (true) {
                  numRead = -1;
                  try {
                    numRead = socketIn.read(data);
                  } catch (IOException e) {
                    e.printStackTrace();
                    return;
                  }

                  if (numRead >= 0) {
                    for (int i = 0; i < numRead; i++) {
                      toMote++;
                      motePort.writeByte(data[i]);
                      if (listeners != null) {
                        for (MoteSerialSocketConnectionListener dl : listeners) {
                          dl.dataTransferred(
                              MoteSerialSocketConnection.this, toMote, toSocket, data[i]);
                        }
                      }
                    }

                  } else {
                    logger.fatal("Incoming data thread shut down");
                    cleanup();
                    break;
                  }
                }
              }
            });
    socketThread.start();
  }
 /**
  * Creates an interface to the mote ID at mote.
  *
  * @param mote Mote
  * @see Mote
  * @see se.sics.cooja.MoteInterfaceHandler
  */
 public ContikiMoteID(Mote mote) {
   this.mote = mote;
   this.moteMem = (SectionMoteMemory) mote.getMemory();
 }
  public GISOO_Simulink_Plugin(Mote mote, Simulation simulation, final GUI gui) {

    super("GISOO-Simulink Plugin (" + mote + ")", gui, false);
    this.mote = mote;
    skyMote = (SkyMote) mote;
    sim = skyMote.getSimulation();
    LISTEN_PORT = 18000 + mote.getID();

    if (GUI.isVisualized()) {
      this.getContentPane().setSize(100, 100);
      Box northBox = Box.createHorizontalBox();
      northBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      JPanel smallPanel = new JPanel(new BorderLayout());
      JLabel label = new JLabel("Listening on port: " + LISTEN_PORT);
      label.setPreferredSize(new Dimension(LABEL_WIDTH, 20));
      smallPanel.add(BorderLayout.CENTER, label);
      northBox.add(smallPanel);

      Box mainBox = Box.createHorizontalBox();
      mainBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      JPanel smallPane = new JPanel(new BorderLayout());
      JLabel simulinkPort = new JLabel("Sending data to port: " + SIMULINK_PORT);
      simulinkPort.setPreferredSize(new Dimension(LABEL_WIDTH, LABEL_HEIGHT));
      smallPane.add(BorderLayout.CENTER, simulinkPort);
      mainBox.add(smallPane);

      Box southernBox = Box.createHorizontalBox();
      southernBox.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
      JPanel smallPane2 = new JPanel(new BorderLayout());
      serialDataShouldRCV = new JCheckBox("Serial reply should be received after(ms): ");
      CPUDellay = new JTextField("1");
      CPUDellay.setPreferredSize(new Dimension(LABEL_WIDTH, 25));
      smallPane2.add(BorderLayout.WEST, serialDataShouldRCV);
      smallPane2.add(BorderLayout.CENTER, CPUDellay);
      southernBox.add(smallPane2);

      getContentPane().add(BorderLayout.NORTH, northBox);
      getContentPane().add(BorderLayout.CENTER, mainBox);
      getContentPane().add(BorderLayout.SOUTH, southernBox);
      pack();
    }

    /* Mote serial port */
    serialPort = (SerialPort) mote.getInterfaces().getLog();
    if (serialPort == null) {
      throw new RuntimeException("No mote serial port");
    }
    for (byte b : serialPayload) {
      b = 0;
    }

    logger.info("Listening on port: " + LISTEN_PORT);

    /* Observe serial port for outgoing data */
    serialPort.addSerialDataObserver(
        serialDataObserver =
            new Observer() {
              public void update(Observable obs, Object obj) {
                serialSendFlag = false;
                /* Check the received message to find the moment that message follow the header format
                "0x 00 ff ff 00 00" which is equal to "0 -1 -1 0 0" in decimal. The next byte after
                this message is the message size*/

                lastByte = serialPort.getLastSerialData();
                logger.info("lastByte: " + lastByte);
                if (lastByte == 126 && nRCVedByte < 7) {
                  nRCVedByte = 1;
                  //  messageIndex=0;
                } else if (nRCVedByte > 0 && nRCVedByte < 7) {
                  nRCVedByte++;
                } else if (nRCVedByte == 7) {
                  sizeOfPayload = lastByte;
                  //                    logger.info("sizeOfPayload is: " + sizeOfPayload);
                  nRCVedByte++;
                } else if (nRCVedByte > 7 && nRCVedByte < 10) {
                  nRCVedByte++;
                } else if (nRCVedByte >= 10 && nRCVedByte < 10 + sizeOfPayload) {
                  //                    logger.info("nRCVedByte: " + nRCVedByte);
                  //                    logger.info("messageIndex: " + messageIndex);

                  serialPayload[messageIndex] = lastByte;
                  //                    logger.info("messageIndex: " + messageIndex + "   lastByte:
                  // " + lastByte);
                  if (nRCVedByte == (10 + sizeOfPayload - 1)) {
                    messageIndex = 0;
                    serialSendFlag = true;
                  } else {
                    messageIndex++;
                  }
                  nRCVedByte++;
                } else {
                  nRCVedByte = 0;
                }

                if (serialSendFlag) {

                  simulationTime = (int) sim.getSimulationTimeMillis();
                  serialPinNumber =
                      (skyMote.getID() * 100)
                          + 18; // 18 = pinNumber for Sending data to the serial port in Simulink
                  int u = 0; // value;
                  byte[] serialDataMSG =
                      serialMessageCreator(simulationTime, serialPinNumber, serialPayload);
                  fileWriter(serialPinNumber, serialPayload);
                  int q = serialMsgSender(serialDataMSG, dSocket);
                  serialSendFlag = false;
                  if (serialDataShouldRCV
                      .isSelected()) // If the second scenario has been selected (It should check
                                     // the second scenario CheckBox )
                  {
                    //                        logger.info("Simulation Time is : " +
                    // sim.getSimulationTime());
                    TimeEvent delayedEvent =
                        new TimeEvent(0) {
                          public void execute(long t) {
                            //                                logger.info("  Simulation Time is : "
                            // + sim.getSimulationTime());

                            simulationTime = (int) sim.getSimulationTimeMillis();
                            //                              logger.info("When recoded Simulation
                            // Time is : " + sim.getSimulationTime());
                            serialPinNumber = (skyMote.getID() * 100) + 19; // SerialDataRequest;
                            //                            logger.info("SerialPinNumber is : " +
                            // serialPinNumber);

                            byte[] serialRequestMesg =
                                serialRequestMsgCreator(simulationTime, serialPinNumber);
                            serialRequestSender(serialRequestMesg, dSocket);
                            fileWriter(serialPinNumber, serialRequestMesg);
                          }
                        };

                    sim.scheduleEvent(
                        delayedEvent,
                        sim.getSimulationTime()
                            + (Long.parseLong(CPUDellay.getText()) * (sim.MILLISECOND)));
                  }
                }
              }
            });

    try {
      dSocket = new DatagramSocket(LISTEN_PORT); // (18000 + skyMote.getID());

    } catch (SocketException ex) {
      System.out.println("Errore in dSocket creation");
    }
    IOUnit adc = skyMote.getCPU().getIOUnit("ADC12");
    if (adc instanceof ADC12) {
      ((ADC12) adc).setADCInput(0, new ADCConnector(0));
      ((ADC12) adc).setADCInput(1, new ADCConnector(1));
      ((ADC12) adc).setADCInput(2, new ADCConnector(2));
      ((ADC12) adc).setADCInput(3, new ADCConnector(3));
      ((ADC12) adc).setADCInput(4, new ADCConnector(4));
      ((ADC12) adc).setADCInput(5, new ADCConnector(5));
      ((ADC12) adc).setADCInput(6, new ADCConnector(6));
      ((ADC12) adc).setADCInput(7, new ADCConnector(7));
      ((ADC12) adc).setADCInput(8, new ADCConnector(8));
      ((ADC12) adc).setADCInput(9, new ADCConnector(9));
      ((ADC12) adc).setADCInput(10, new ADCConnector(10));
      ((ADC12) adc).setADCInput(11, new ADCConnector(11));
      ((ADC12) adc).setADCInput(12, new ADCConnector(12));
      ((ADC12) adc).setADCInput(13, new ADCConnector(13));
      ((ADC12) adc).setADCInput(14, new ADCConnector(14));
      ((ADC12) adc).setADCInput(15, new ADCConnector(15));
    }
    ((DAC12) skyMote.getCPU().getIOUnit("DAC12")).setDACOutput(0, new DACConnector(16));
    ((DAC12) skyMote.getCPU().getIOUnit("DAC12")).setDACOutput(1, new DACConnector(17));
  }