public CC1101Radio(Mote m) {
    this.mote = (MspMote) m;
    Radio802154 r = this.mote.getCPU().getChip(Radio802154.class);
    if (r == null || !(r instanceof CC1101)) {
      throw new IllegalStateException("Mote is not equipped with an CC1101 radio");
    }
    this.cc1101 = (CC1101) r;

    cc1101.addRFListener(
        new RFListener() {
          int len = 0;
          int expLen = 0;
          byte[] buffer = new byte[256 + 15];
          private boolean gotSynchbyte = false;

          public void receivedByte(byte data) {
            if (!isTransmitting()) {
              /* Start transmission */
              lastEvent = RadioEvent.TRANSMISSION_STARTED;
              isTransmitting = true;
              len = 0;
              gotSynchbyte = false;
              /*logger.debug("----- CC1101 TRANSMISSION STARTED -----");*/
              setChanged();
              notifyObservers();
            }
            if (len >= buffer.length) {
              /* Bad size packet, too large */
              logger.debug("Error: bad size: " + len + ", dropping outgoing byte: " + data);
              return;
            }

            /* send this byte to all nodes */
            lastOutgoingByte = data;
            lastEvent = RadioEvent.CUSTOM_DATA_TRANSMITTED;
            setChanged();
            notifyObservers();

            /* Await synch byte */
            if (!gotSynchbyte) {
              if (lastOutgoingByte == CC1101.SYNCH_BYTE_LAST) {
                gotSynchbyte = true;
              }
              return;
            }

            final int HEADERLEN = 1; /* 1x Length byte */
            final int FOOTERLEN = 2; /* TODO Fix CRC in Mspsim's CC1101.java */
            if (len == 0) {
              expLen = (0xff & data) + HEADERLEN + FOOTERLEN;
            }
            buffer[len++] = data;

            if (len == expLen) {
              /*logger.debug("----- CC1101 CUSTOM DATA TRANSMITTED -----");*/

              final byte[] buf = new byte[expLen];
              System.arraycopy(buffer, 0, buf, 0, expLen);
              lastOutgoingPacket =
                  new RadioPacket() {
                    public byte[] getPacketData() {
                      return buf;
                    }
                  };

              lastEvent = RadioEvent.PACKET_TRANSMITTED;
              /*logger.debug("----- CC1101 PACKET TRANSMITTED -----");*/
              setChanged();
              notifyObservers();

              /*logger.debug("----- CC1101 TRANSMISSION FINISHED -----");*/
              isTransmitting = false;
              lastEvent = RadioEvent.TRANSMISSION_FINISHED;
              setChanged();
              notifyObservers();
              len = 0;
            }
          }
        });

    cc1101.setReceiverListener(
        new ReceiverListener() {
          public void newState(boolean on) {
            if (cc1101.isReadyToReceive()) {
              lastEvent = RadioEvent.HW_ON;
              setChanged();
              notifyObservers();
            } else {
              radioOff();
            }
          }
        });

    cc1101.addChannelListener(
        new ChannelListener() {
          public void channelChanged(int channel) {
            /* XXX Currently assumes zero channel switch time */
            lastEvent = RadioEvent.UNKNOWN;
            setChanged();
            notifyObservers();
          }
        });
  }
 public int getFrequency() {
   return cc1101.getActiveFrequency();
 }
 public boolean isRadioOn() {
   return cc1101.isReadyToReceive();
 }
 public int getChannel() {
   return cc1101.getActiveChannel();
 }