public void setCurrentSignalStrength(final double signalStrength) {
    if (signalStrength == currentSignalStrength) {
      return; /* ignored */
    }
    currentSignalStrength = signalStrength;
    if (rssiLastCounter == 0) {
      getMote()
          .getSimulation()
          .scheduleEvent(
              new MspMoteTimeEvent(mote, 0) {
                public void execute(long t) {
                  super.execute(t);

                  /* Update average */
                  System.arraycopy(rssiLast, 1, rssiLast, 0, 7);
                  rssiLast[7] = currentSignalStrength;
                  double avg = 0;
                  for (double v : rssiLast) {
                    avg += v;
                  }
                  avg /= rssiLast.length;

                  cc1101.setRSSI((int) avg);

                  rssiLastCounter--;
                  if (rssiLastCounter > 0) {
                    mote.getSimulation().scheduleEvent(this, t + DELAY_BETWEEN_BYTES / 2);
                  }
                }
              },
              mote.getSimulation().getSimulationTime());
    }
    rssiLastCounter = 8;
  }
Example #2
0
  /**
   * Creates an interface to the mote ID at mote.
   *
   * @param mote Mote ID's mote.
   * @see Mote
   * @see se.sics.cooja.MoteInterfaceHandler
   */
  public MspMoteID(Mote m) {
    this.mote = (MspMote) m;
    this.moteMem = (MspMoteMemory) mote.getMemory();

    if (moteMem.variableExists("node_id") && moteMem.variableExists("TOS_NODE_ID")) {
      location = ID_LOCATION.VARIABLES_BOTH;
    } else if (moteMem.variableExists("node_id")) {
      location = ID_LOCATION.VARIABLE_NODE_ID;
    } else if (moteMem.variableExists("TOS_NODE_ID")) {
      location = ID_LOCATION.VARIABLE_TOS_NODE_ID;
    } else {
      location = ID_LOCATION.JAVA_ONLY;
    }

    /*logger.debug("ID location: " + location);*/

    final TimeEvent persistentSetIDEvent =
        new MoteTimeEvent(mote, 0) {
          public void execute(long t) {

            if (persistentSetIDCounter-- > 0) {
              setMoteID(moteID);
              /*logger.info("Setting ID: " + moteID + " at " + t);*/

              if (t + mote.getInterfaces().getClock().getDrift() < 0) {
                /* Wait until node is booting */
                mote.getSimulation()
                    .scheduleEvent(this, -mote.getInterfaces().getClock().getDrift());
              } else {
                mote.getSimulation().scheduleEvent(this, t + Simulation.MILLISECOND);
              }
            }
          }
        };

    if (PERSISTENT_SET_ID) {
      mote.getSimulation()
          .invokeSimulationThread(
              new Runnable() {
                public void run() {
                  persistentSetIDEvent.execute(
                      MspMoteID.this.mote.getSimulation().getSimulationTime());
                };
              });
    }
  }
  public void receiveCustomData(Object data) {
    if (!(data instanceof Byte)) {
      logger.fatal("Bad custom data: " + data);
      return;
    }
    lastIncomingByte = (Byte) data;

    final byte inputByte;
    if (isInterfered()) {
      inputByte = (byte) 0xFF;
    } else {
      inputByte = lastIncomingByte;
    }
    mote.getSimulation()
        .scheduleEvent(
            new MspMoteTimeEvent(mote, 0) {
              public void execute(long t) {
                super.execute(t);
                cc1101.receivedByte(inputByte);
                mote.requestImmediateWakeup();
              }
            },
            mote.getSimulation().getSimulationTime());
  }
Example #4
0
  public void setMoteID(int newID) {
    /* tell mote instance */
    if (moteID != newID) {
      mote.idUpdated(newID);
    }
    moteID = newID;

    if (location == ID_LOCATION.VARIABLE_NODE_ID) {
      if (GENERATE_ID_HEADER) {
        /* Write to external flash */
        SkyFlash flash = mote.getInterfaces().getInterfaceOfType(SkyFlash.class);
        if (flash != null) {
          flash.writeIDheader(newID);
        }
      }
      moteMem.setIntValueOf("node_id", newID);
      /* Experimental: set Contiki random seed variable if it exists */
      if (moteMem.variableExists("rseed")) {
        moteMem.setIntValueOf("rseed", (int) (mote.getSimulation().getRandomSeed() + newID));
      }
      setChanged();
      notifyObservers();
      return;
    }

    if (location == ID_LOCATION.VARIABLES_BOTH) {
      if (GENERATE_ID_HEADER) {
        /* Write to external flash */
        SkyFlash flash = mote.getInterfaces().getInterfaceOfType(SkyFlash.class);
        if (flash != null) {
          flash.writeIDheader(newID);
        }
      }
      moteMem.setIntValueOf("node_id", newID);
      /* Experimental: set Contiki random seed variable if it exists */
      if (moteMem.variableExists("rseed")) {
        moteMem.setIntValueOf("rseed", (int) (mote.getSimulation().getRandomSeed() + newID));
      }

      if (moteMem.variableExists("TOS_NODE_ID")) {
        moteMem.setIntValueOf("TOS_NODE_ID", newID);
      }
      if (moteMem.variableExists("ActiveMessageAddressC__addr")) {
        moteMem.setIntValueOf("ActiveMessageAddressC__addr", newID);
      }
      if (moteMem.variableExists("ActiveMessageAddressC$addr")) {
        moteMem.setIntValueOf("ActiveMessageAddressC$addr", newID);
      }

      setChanged();
      notifyObservers();
      return;
    }

    if (location == ID_LOCATION.VARIABLE_TOS_NODE_ID) {
      if (moteMem.variableExists("TOS_NODE_ID")) {
        moteMem.setIntValueOf("TOS_NODE_ID", newID);
      }
      if (moteMem.variableExists("ActiveMessageAddressC__addr")) {
        moteMem.setIntValueOf("ActiveMessageAddressC__addr", newID);
      }
      if (moteMem.variableExists("ActiveMessageAddressC$addr")) {
        moteMem.setIntValueOf("ActiveMessageAddressC$addr", newID);
      }

      setChanged();
      notifyObservers();
      return;
    }

    if (location == ID_LOCATION.JAVA_ONLY) {
      setChanged();
      notifyObservers();
      return;
    }

    logger.fatal("Unknown node ID location");
  }
 public Position getPosition() {
   return mote.getInterfaces().getPosition();
 }