/**
  * Returns simulation with with given ID.
  *
  * @param id ID
  * @return Mote or null
  * @see Mote#getID()
  */
 public Mote getMoteWithID(int id) {
   for (Mote m : motes) {
     if (m.getID() == id) {
       return m;
     }
   }
   return null;
 }
 public void addRecord() {
   if (mote.get_reading(channel) != null) {
     // System.out.println("update mote"+mote.getMoteId()+" channel: "+ channel+" reading: "+
     // mote.get_reading(channel)[0]);
     add(
         new ReadingRecord(
             (int) (mote.getLastTimeSeen() - birth.getTime()) / 1000 + deltaTime,
             mote.get_reading(channel)));
   }
 }
Beispiel #3
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();
 }
  /**
   * Remove given mote type from simulation.
   *
   * @param type Mote type
   */
  public void removeMoteType(MoteType type) {
    if (!moteTypes.contains(type)) {
      logger.fatal("Mote type is not registered: " + type);
      return;
    }

    /* Remove motes */
    for (Mote m : getMotes()) {
      if (m.getType() == type) {
        removeMote(m);
      }
    }

    moteTypes.remove(type);
    this.setChanged();
    this.notifyObservers(this);
  }
Beispiel #6
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;
    }
  }
Beispiel #7
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();
  }
Beispiel #8
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();
  }
Beispiel #9
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 int getMoteId() {
   return mote.getMoteId();
 }
 /**
  * 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();
 }
Beispiel #12
0
  /**
   * Sets the current simulation config depending on the given configuration.
   *
   * @param configXML Simulation configuration
   * @param visAvailable True if simulation is allowed to show visualizers
   * @param manualRandomSeed Simulation random seed. May be null, in which case the configuration is
   *     used
   * @return True if simulation was configured successfully
   * @throws Exception If configuration could not be loaded
   */
  public boolean setConfigXML(
      Collection<Element> configXML, boolean visAvailable, Long manualRandomSeed) throws Exception {

    // Parse elements
    for (Element element : configXML) {

      // Title
      if (element.getName().equals("title")) {
        title = element.getText();
      }

      // Delay time
      if (element.getName().equals("delaytime")) {
        setDelayTime(Integer.parseInt(element.getText()));
      }

      // Random seed
      if (element.getName().equals("randomseed")) {
        long newSeed;

        if (element.getText().equals("generated")) {
          randomSeedGenerated = true;
          newSeed = new Random().nextLong();
        } else {
          newSeed = Long.parseLong(element.getText());
        }
        if (manualRandomSeed != null) {
          newSeed = manualRandomSeed;
        }

        setRandomSeed(newSeed);
      }

      // Max mote startup delay
      if (element.getName().equals("motedelay")) {
        maxMoteStartupDelay = Integer.parseInt(element.getText()) * MILLISECOND;
      }
      if (element.getName().equals("motedelay_us")) {
        maxMoteStartupDelay = Integer.parseInt(element.getText());
      }

      // Radio medium
      if (element.getName().equals("radiomedium")) {
        String radioMediumClassName = element.getText().trim();
        Class<? extends RadioMedium> radioMediumClass =
            myGUI.tryLoadClass(this, RadioMedium.class, radioMediumClassName);

        if (radioMediumClass != null) {
          // Create radio medium specified in config
          try {
            currentRadioMedium = RadioMedium.generateRadioMedium(radioMediumClass, this);
          } catch (Exception e) {
            currentRadioMedium = null;
            logger.warn("Could not load radio medium class: " + radioMediumClassName);
          }
        }

        // Show configure simulation dialog
        boolean createdOK = false;
        if (visAvailable) {
          createdOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), this);
        } else {
          createdOK = true;
        }

        if (!createdOK) {
          logger.debug("Simulation not created, aborting");
          throw new Exception("Load aborted by user");
        }

        // Check if radio medium specific config should be applied
        if (radioMediumClassName.equals(currentRadioMedium.getClass().getName())) {
          currentRadioMedium.setConfigXML(element.getChildren(), visAvailable);
        } else {
          logger.info("Radio Medium changed - ignoring radio medium specific config");
        }
      }

      /* Event central */
      if (element.getName().equals("events")) {
        eventCentral.setConfigXML(this, element.getChildren(), visAvailable);
      }

      // Mote type
      if (element.getName().equals("motetype")) {
        String moteTypeClassName = element.getText().trim();

        /* Try to recreate simulation using a different mote type */
        if (visAvailable) {
          String[] availableMoteTypes =
              getGUI().getProjectConfig().getStringArrayValue("se.sics.cooja.GUI.MOTETYPES");
          String newClass =
              (String)
                  JOptionPane.showInputDialog(
                      GUI.getTopParentContainer(),
                      "The simulation is about to load '"
                          + moteTypeClassName
                          + "'\n"
                          + "You may try to load the simulation using a different mote type.\n",
                      "Loading mote type",
                      JOptionPane.QUESTION_MESSAGE,
                      null,
                      availableMoteTypes,
                      moteTypeClassName);
          if (newClass != null && !newClass.equals(moteTypeClassName)) {
            logger.warn("Changing mote type class: " + moteTypeClassName + " -> " + newClass);
            moteTypeClassName = newClass;
          }
        }

        Class<? extends MoteType> moteTypeClass =
            myGUI.tryLoadClass(this, MoteType.class, moteTypeClassName);

        if (moteTypeClass == null) {
          logger.fatal("Could not load mote type class: " + moteTypeClassName);
          throw new MoteType.MoteTypeCreationException(
              "Could not load mote type class: " + moteTypeClassName);
        }

        MoteType moteType = moteTypeClass.getConstructor((Class[]) null).newInstance();

        boolean createdOK = moteType.setConfigXML(this, element.getChildren(), visAvailable);
        if (createdOK) {
          addMoteType(moteType);
        } else {
          logger.fatal("Mote type was not created: " + element.getText().trim());
          throw new Exception("All mote types were not recreated");
        }
      }

      /* Mote */
      if (element.getName().equals("mote")) {

        /* Read mote type identifier */
        MoteType moteType = null;
        for (Element subElement : (Collection<Element>) element.getChildren()) {
          if (subElement.getName().equals("motetype_identifier")) {
            moteType = getMoteType(subElement.getText());
            if (moteType == null) {
              throw new Exception("No mote type '" + subElement.getText() + "' for mote");
            }
            break;
          }
        }
        if (moteType == null) {
          throw new Exception("No mote type specified for mote");
        }

        /* Create mote using mote type */
        Mote mote = moteType.generateMote(this);
        if (mote.setConfigXML(this, element.getChildren(), visAvailable)) {
          if (getMoteWithID(mote.getID()) != null) {
            logger.warn("Ignoring duplicate mote ID: " + mote.getID());
          } else {
            addMote(mote);
          }
        } else {
          logger.fatal("Mote was not created: " + element.getText().trim());
          throw new Exception("All motes were not recreated");
        }
      }
    }

    if (currentRadioMedium != null) {
      currentRadioMedium.simulationFinishedLoading();
    }

    setChanged();
    notifyObservers(this);

    /* Execute simulation thread events now, before simulation starts */
    while (hasPollRequests) {
      popSimulationInvokes().run();
    }

    return true;
  }
Beispiel #13
0
  /**
   * Returns the current simulation config represented by XML elements. This config also includes
   * the current radio medium, all mote types and motes.
   *
   * @return Current simulation config
   */
  public Collection<Element> getConfigXML() {
    ArrayList<Element> config = new ArrayList<Element>();

    Element element;

    // Title
    element = new Element("title");
    element.setText(title);
    config.add(element);

    // Delay time
    element = new Element("delaytime");
    element.setText("" + getDelayTime());
    config.add(element);

    // Random seed
    element = new Element("randomseed");
    if (randomSeedGenerated) {
      element.setText("generated");
    } else {
      element.setText(Long.toString(getRandomSeed()));
    }
    config.add(element);

    // Max mote startup delay
    element = new Element("motedelay_us");
    element.setText(Long.toString(maxMoteStartupDelay));
    config.add(element);

    // Radio Medium
    element = new Element("radiomedium");
    element.setText(currentRadioMedium.getClass().getName());

    Collection<Element> radioMediumXML = currentRadioMedium.getConfigXML();
    if (radioMediumXML != null) {
      element.addContent(radioMediumXML);
    }
    config.add(element);

    /* Event central */
    element = new Element("events");
    element.addContent(eventCentral.getConfigXML());
    config.add(element);

    // Mote types
    for (MoteType moteType : getMoteTypes()) {
      element = new Element("motetype");
      element.setText(moteType.getClass().getName());

      Collection<Element> moteTypeXML = moteType.getConfigXML(this);
      if (moteTypeXML != null) {
        element.addContent(moteTypeXML);
      }
      config.add(element);
    }

    // Motes
    for (Mote mote : motes) {
      element = new Element("mote");

      Collection<Element> moteConfig = mote.getConfigXML();
      if (moteConfig == null) {
        moteConfig = new ArrayList<Element>();
      }

      /* Add mote type identifier */
      Element typeIdentifier = new Element("motetype_identifier");
      typeIdentifier.setText(mote.getType().getIdentifier());
      moteConfig.add(typeIdentifier);

      element.addContent(moteConfig);
      config.add(element);
    }

    return config;
  }
Beispiel #14
0
 /**
  * Calculates distance from associated mote to another mote.
  *
  * @param m Another mote
  * @return Distance
  */
 public double getDistanceTo(Mote m) {
   return getDistanceTo(m.getInterfaces().getPosition());
 }
Beispiel #15
0
 public Position getPosition() {
   return mote.getInterfaces().getPosition();
 }