Beispiel #1
0
static void linkTo()
{
  uint8_t  msg[10], delay = 0;
  memset(msg, 0, 10);

  while (SMPL_SUCCESS != SMPL_Link(&sLinkID1))
  {
    /* blink LEDs until we link successfully */
    toggleLED(1);
    toggleLED(2);
    SPIN_ABOUT_A_SECOND;
  }

  /* we're linked. turn off red LED. received messages will toggle the green LED. */
  if (BSP_LED2_IS_ON())
  {
    toggleLED(2);
  }

  /* turn on RX. default is RX off. */
  SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0);

  /* put LED to toggle in the message */
  msg[0] = 2;  /* toggle red */
  while (1)
  {
	__bis_SR_register(LPM4_bits + GIE);       // Enter LPM4 w/interrupt
    SPIN_ABOUT_A_SECOND;
    if (delay > 0x00)
    {
      SPIN_ABOUT_A_SECOND;
    }
    if (delay > 0x01)
    {
      SPIN_ABOUT_A_SECOND;
    }
    if (delay > 0x02)
    {
      SPIN_ABOUT_A_SECOND;
    }

    /* delay longer and longer -- then start over */
    delay = (delay+1) & 0x03;
    /* put the sequence ID in the message */
    msg[9] = ++sTxTid;
    ReadRTCCTimeDate(&Rtcctimedate);
    /* put data into packet */
    msg[1] = Rtcctimedate.Sec;
    msg[2] = Rtcctimedate.Min;
    msg[3] = Rtcctimedate.Hour;
    msg[4] = Rtcctimedate.Day;
    msg[5] = Rtcctimedate.Date;
    msg[6] = Rtcctimedate.Month;
    msg[7] = Rtcctimedate.Year;
    SMPL_Send(sLinkID1, msg, sizeof(msg));
  }
}
//*****************************************************************************
//
// This function attempts to link to another SimpliciTI device by sending a
// link request.
//
//*****************************************************************************
tBoolean
LinkTo(void)
{
    smplStatus_t eRetcode;
    unsigned long ulCount;

    //
    // Turn both "LEDs" on.
    //
    SetLED(1, true);
    SetLED(2, true);

    //
    // Tell SimpliciTI to try to link to an access point.
    //
    for(ulCount = 1; ulCount <= 10; ulCount++)
    {
        //
        // Update the displayed count.  Note that we must process the widget
        // message queue here to ensure that the change makes it onto the
        // display.
        //
        UpdateStatus(false, "Link request %d (%s)", ulCount,
                     (ulCount > 1) ? MapSMPLStatus(eRetcode) : "Waiting");
        WidgetMessageQueueProcess();

        //
        // Try to link to the access point.
        //
        eRetcode = SMPL_Link(&sLinkID);
        if(eRetcode == SMPL_SUCCESS)
        {
            break;
        }

        //
        // Wait a bit before trying again.
        //
        NWK_DELAY(1000);

        //
        // Toggle both the LEDs
        //
        ToggleLED(1);
        ToggleLED(2);
    }

    //
    // Did we manage to link to the access point?
    //
    if(eRetcode == SMPL_SUCCESS)
    {
        //
        // Tell the user how we got on.
        //
        UpdateStatus(false, "Link successful.");
        SetLED(2, false);

        //
        // Turn on RX. Default is off.
        //
        SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0);

        //
        // Set the time at which we have to send the next packet to our peer to
        // one second in the future.
        //
        g_ulNextPacketTick = g_ulSysTickCount + TICKS_PER_SECOND;

        //
        // Tell the main loop that we established communication successfully.
        //
        return(true);
    }
    else
    {
        UpdateStatus(false, "Failed to link.");

        //
        // Tell the main loop that we failed to establish communication.
        //
        return(false);
    }
}
static void linkTo()
{
  uint8_t     msg[2];
  uint8_t     button, misses, done;

  /* Keep trying to link... */
  while (SMPL_SUCCESS != SMPL_Link(&sLinkID1))
  {
    toggleLED(1);
    toggleLED(2);
    SPIN_ABOUT_A_SECOND;
  }

  /* Turn off LEDs. */
  if (BSP_LED2_IS_ON())
  {
    toggleLED(2);
  }
  if (BSP_LED1_IS_ON())
  {
    toggleLED(1);
  }

  /* sleep until button press... */
  SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);

  while (1)
  {
    button = 0;
    /* Send a message when either button pressed */
    if (BSP_BUTTON1())
    {
      SPIN_ABOUT_A_QUARTER_SECOND;  /* debounce... */
      /* Message to toggle LED 1. */
      button = 1;
    }
    else if (BSP_BUTTON2())
    {
      SPIN_ABOUT_A_QUARTER_SECOND;  /* debounce... */
      /* Message to toggle LED 2. */
      button = 2;
    }
    if (button)
    {
      uint8_t      noAck;
      smplStatus_t rc;

      /* get radio ready...awakens in idle state */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0);

      /* Set TID and designate which LED to toggle */
      msg[1] = ++sTid;
      msg[0] = (button == 1) ? 1 : 2;
      done = 0;
      while (!done)
      {
        noAck = 0;

        /* Try sending message MISSES_IN_A_ROW times looking for ack */
        for (misses=0; misses < MISSES_IN_A_ROW; ++misses)
        {
          if (SMPL_SUCCESS == (rc=SMPL_SendOpt(sLinkID1, msg, sizeof(msg), SMPL_TXOPTION_ACKREQ)))
          {
            /* Message acked. We're done. Toggle LED 1 to indicate ack received. */
            toggleLED(1);
            break;
          }
          if (SMPL_NO_ACK == rc)
          {
            /* Count ack failures. Could also fail becuase of CCA and
             * we don't want to scan in this case.
             */
            noAck++;
          }
        }
        if (MISSES_IN_A_ROW == noAck)
        {
          /* Message not acked. Toggle LED 2. */
          toggleLED(2);
#ifdef FREQUENCY_AGILITY
          /* Assume we're on the wrong channel so look for channel by
           * using the Ping to initiate a scan when it gets no reply. With
           * a successful ping try sending the message again. Otherwise,
           * for any error we get we will wait until the next button
           * press to try again.
           */
          if (SMPL_SUCCESS != SMPL_Ping(sLinkID1))
          {
            done = 1;
          }
#else
          done = 1;
#endif  /* FREQUENCY_AGILITY */
        }
        else
        {
          /* Got the ack or we don't care. We're done. */
          done = 1;
        }
      }

      /* radio back to sleep */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);
    }
  }
}
Beispiel #4
0
void main(void)
{

	sensor.cadc = 'A';
	sensor.iadc = 125;

	trigger(0x5 + (0x6 << 3) + (0x1 << 6) + (0x7 << 9));

  	BSP_Init();                               // init bsp first, then simpliciti

  	BCSCTL3 = LFXT1S_2; 					// aclk = vlo

  	// address check and creation
	Flash_Addr = (char *)0x10F0;              // RF Address = 0x10F0
	if( Flash_Addr[0] == 0xFF &&              // Check if device Address is missing
    	Flash_Addr[1] == 0xFF &&
    	Flash_Addr[2] == 0xFF &&
    	Flash_Addr[3] == 0xFF )
  	{
    	createRandomAddress();                  // Create Random device address at
	}                                         // initial startup if missing
	lAddr.addr[0] = Flash_Addr[0];
	lAddr.addr[1] = Flash_Addr[1];
	lAddr.addr[2] = Flash_Addr[2];
	lAddr.addr[3] = Flash_Addr[3];
	// load address
	SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);

  	SMPL_Init(NULL);                          // null callback for TX

  	Init_ADC10();
  	Init_TIMER0A0();

  	do {                                       // wait for button
    if (BSP_BUTTON1())
    {
      break;
    }
  } while (1);

  while (SMPL_SUCCESS != SMPL_Link(&linkIDTemp))    // link to Rx
  {
    BSP_TOGGLE_LED1();                      // toggle red for not linked
  }

  	BSP_TURN_OFF_LED1();                      // red off
  	BSP_Delay(2000);                             // for 2 seconds
  	BSP_TURN_ON_LED1();

  	_EINT();                                  // Enable Global Interupts

  while (1)
  {
	  BSP_TOGGLE_LED2();
    // adc with dtc in use
    ADC10CTL0 &= ~ENC;						// turn off adc10
    while (ADC10CTL1 & BUSY);               // wait if adc10 core is active
    ADC10SA = (unsigned int)ADCdata;    	// data buffer start
    ADC10CTL0 |= ENC + ADC10SC;             // sampling and conversion start

    LPM3;

    // insert sensor calculations here
  	// or
    // send raw adc data from P1.0
  	sensor.iadc = ADCdata[0];

	// turn on radio and tx sensor struct
    SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0);
    SMPL_Send(linkIDTemp, (uint8_t *)&sensor, sizeof( my_sensors ));

  }
}
// *************************************************************************************************
// @fn          simpliciti_link
// @brief       Init hardware and try to link to access point.
// @param       none
// @return      unsigned char		0 = Could not link, timeout or external cancel.
//									1 = Linked successful.
// *************************************************************************************************
unsigned char simpliciti_link(void)
{
    uint8_t timeout;
    addr_t lAddr;
    uint8_t i;
    uint8_t pwr;

    // Configure timer
    BSP_InitBoard();

    // Change network address to value set in calling function
    for (i = 0; i < NET_ADDR_SIZE; i++)
    {
        lAddr.addr[i] = simpliciti_ed_address[i];
    }
    SMPL_Ioctl(IOCTL_OBJ_ADDR, IOCTL_ACT_SET, &lAddr);

    // Set flag
    simpliciti_flag = SIMPLICITI_STATUS_LINKING;

    /* Keep trying to join (a side effect of successful initialization) until
     * successful. Toggle LEDS to indicate that joining has not occurred.
     */
    timeout = 0;
    while (SMPL_SUCCESS != SMPL_Init(0))
    {
        NWK_DELAY(1000);

        // Service watchdog
        WDTCTL = WDTPW + WDTIS__512K + WDTSSEL__ACLK + WDTCNTCL;

        // Stop connecting after defined numbers of seconds (15)
        if (timeout++ > TIMEOUT)
        {
            // Clean up SimpliciTI stack to enable restarting
            sInit_done = 0;
            simpliciti_flag = SIMPLICITI_STATUS_ERROR;
            return (0);
        }

        // Break when flag bit SIMPLICITI_TRIGGER_STOP is set
        if (getFlag(simpliciti_flag, SIMPLICITI_TRIGGER_STOP))
        {
            // Clean up SimpliciTI stack to enable restarting
            sInit_done = 0;
            return (0);
        }
    }

    // Set output power to +3.3dmB
    pwr = IOCTL_LEVEL_2;
    SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SETPWR, &pwr);

    /* Unconditional link to AP which is listening due to successful join. */
    timeout = 0;
    while (SMPL_SUCCESS != SMPL_Link(&sLinkID1))
    {
        NWK_DELAY(1000);

        // Service watchdog
        WDTCTL = WDTPW + WDTIS__512K + WDTSSEL__ACLK + WDTCNTCL;

        // Stop linking after timeout
        if (timeout++ > TIMEOUT)
        {
            // Clean up SimpliciTI stack to enable restarting
            sInit_done = 0;
            simpliciti_flag = SIMPLICITI_STATUS_ERROR;
            return (0);
        }

        // Exit when flag bit SIMPLICITI_TRIGGER_STOP is set
        if (getFlag(simpliciti_flag, SIMPLICITI_TRIGGER_STOP))
        {
            // Clean up SimpliciTI stack to enable restarting
            sInit_done = 0;
            return (0);
        }
    }
    simpliciti_flag = SIMPLICITI_STATUS_LINKED;

    return (1);
}
void main1 (void)
{
  /* holds length of current message */
  uint8_t len; 
  
  /* the link token */
  linkID_t LinkID = 0;
  
  /* the transmit and receive buffers */
  uint8_t rx[MAX_APP_PAYLOAD], tx[MAX_APP_PAYLOAD];

  /* holds led indicator time out counts */
  uint16_t led_tmr;
  
  int8_t rssi_value;
  

  BSP_Init( );

  SMPL_Init( NULL );
  
  uart_intfc_init( );

  /* turn on the radio so we are always able to receive data asynchronously */
  SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, NULL );
  
  /* turn on LED. */
  BSP_TURN_ON_LED1( );

#ifdef  LINK_TO
  {
    uint8_t cnt = 0;
    tx_send_wait( "Linking to...\r\n", 15 );
    while (SMPL_SUCCESS != SMPL_Link(&LinkID))
      if( cnt++ == 0 )
      {
        /* blink LED until we link successfully */
        BSP_TOGGLE_LED1( );
      }
  }
#else // ifdef LINK_LISTEN
  tx_send_wait( "Listening for Link...\r\n", 23 );
  while (SMPL_SUCCESS != SMPL_LinkListen(&LinkID))
  {
    /* blink LED until we link successfully */
    BSP_TOGGLE_LED1( );
  }
#endif

  tx_send_wait( "Link Established!\r\nReady...\r\n", 29 );
  
  /* turn off the led */
  BSP_TURN_OFF_LED1( );
  
  main_loop:
    /* Check to see if we received any characters from the other radio
     * and if we have send them out the uart and reset indicator timeout.
     * Prioritize on radio received links as that buffer is the most time
     * critical with respect to the UART buffers.
     */
    if( SMPL_Receive( LinkID, tx, &len ) == SMPL_SUCCESS )
    {
      /* blocking call but should be ok if both ends have same uart buad rate */
      //tx_send_wait( tx, len );
      led_tmr = INDICATOR_TIME_OUT;   /* update activity time out */
      
      
      /*
      int8_t dbm;
      SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RSSI, (void *)&dbm);
      sSample= dbm;
      tx_send_wait(&char2send, 1 );//input only (unsigned char)
      tx_send_wait("\n", 1 );*/
      
      
      unsigned char Test2;
      //unsigned char Test;
      rssi_value = 0 ;
      
       ioctlRadioSiginfo_t sigInfo1;
       sigInfo1.lid=LinkID; 
       smplStatus_t SMPL_SUCCESS = SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SIGINFO, (void *)&sigInfo1);
    
      //if( SMPL_SUCCESS == SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RSSI,(void*)&rssi_value) )
      {
        
   
        //transmitData( i, (signed char)sigInfo.sigInfo.rssi, (char*)msg );
        
        //MRFI_Rssi()
        //Test2=RSSI;
        //Test=rssi_value;
        //int8_t i =-127;
        //int i =-127;
        unsigned char rssi_tmp[10];
        int8_t rssi_int = sigInfo1.sigInfo.rssi;
        itoa(rssi_int,rssi_tmp);
        //tx_send_wait("This is Test :", sizeof("This is Test :") );
        //tx_send_wait(&Test2, strlen(&Test2));
        tx_send_wait(&rssi_tmp, strlen(rssi_tmp));
        //tx_send_wait("\r\n", sizeof("\r\n") );
        
        int8_t mk=0x01,bitshift=0;
              
        tx_send_wait("=",1);  
        
        for (bitshift=0;bitshift<8;bitshift++)
        {
          if( (mk<<bitshift) & rssi_int ) 
            tx_send_wait("1",1);
          else
            tx_send_wait("0",1);
          //tx_send_wait("x",1);
          MRFI_DelayMs( 5 );
        }
        tx_send_wait("\r\n", sizeof("\r\n") );
        
        
         
        
        /*        
        tx_send_wait("This is Test2 :", sizeof("This is Test2 :") );
        tx_send_wait( &Test2, sizeof(Test2) );
        tx_send_wait("\n", sizeof("\n") );
        */
      }
      
    }

    FHSS_ACTIVE( if( nwk_pllBackgrounder( false ) != false ) );
    {
      /* check to see if the host has sent any characters and if it has
      * then send them over the radio link and reset indicator timeout.
      */
      len = rx_receive( rx, MAX_APP_PAYLOAD );
      if( len != 0 )
      {
        while( SMPL_Send( LinkID, rx, len ) != SMPL_SUCCESS )
          ;
        /*
        if( SMPL_SUCCESS == SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RSSI,&rssi_value ) ){
          SMPL_Send( LinkID, &rssi_value, sizeof(rssi_value) );
        }*/
        led_tmr = INDICATOR_TIME_OUT;   /* update activity time out */
        
        /* By forcing a minimum delay between transmissions we guarantee
        * a window for receiving packets.  This is necessary as the radio
        * link is half duplex while the UART is full duplex.  Without this
        * delay mechanism, packets can get lost as both ends may attempt to
        * transmit at the same time which the CCA algorithm fails to handle.
        */
        MRFI_DelayMs( 5 );
      }
    }
    
    /* manage led indicator */
    if( led_tmr != 0 )
    {
      led_tmr--;
      BSP_TURN_ON_LED1( );
    }
    else
      BSP_TURN_OFF_LED1( );

    
    goto main_loop; /* do it again and again and again and ... */
    

}
//*****************************************************************************
//
// This function attempts to link to another SimpliciTI device by sending a
// link request.
//
//*****************************************************************************
tBoolean
LinkTo(void)
{
    linkID_t linkID1;
    uint8_t  pucMsg[2], ucWrap;
    unsigned long ulCount;
    smplStatus_t eRetcode;

    //
    // Our message counter hasn't wrapped.
    //
    ucWrap = 0;

    //
    // Turn on LED 2
    //
    SetLED(2, true);

    //
    // Tell the user what we're doing.
    //
    UpdateStatus(false, "Attempting to link...");

    //
    // Try to link for about 10 seconds.
    //
    for(ulCount = 0; ulCount < LINK_TIMEOUT_SECONDS; ulCount++)
    {
        //
        // Try to link.
        //
        eRetcode = SMPL_Link(&linkID1);

        //
        // Did we succeed?
        //
        if(eRetcode == SMPL_SUCCESS)
        {
            //
            // Yes - drop out of the loop.
            //
            break;
        }

        //
        // We didn't link so toggle the LEDs, wait a bit then try again.
        //
        ToggleLED(1);
        ToggleLED(2);
        SPIN_ABOUT_A_SECOND;
    }

    //
    // Did the link succeed?
    //
    if(eRetcode != SMPL_SUCCESS)
    {
        //
        // No - return an error code.
        //
        UpdateStatus(false, "Failed to link!");
        return(false);
    }
    else
    {
        UpdateStatus(false, "Link succeeded.");
    }

    //
    // Turn off the second LED now that we have linked.
    //
    SetLED(2, false);

#ifdef FREQUENCY_AGILITY
    //
    // The radio comes up with Rx off. If Frequency Agility is enabled we need
    // it to be on so we don't miss a channel change broadcast.  We need to
    // hear this since this application has no ack.  The broadcast from the AP
    // is, therefore, the only way to find out about a channel change.
    //
    SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0);
#endif

    //
    // Set up the initial message and message counter.
    //
    pucMsg[0] = 2;
    pucMsg[1] = ++g_ucTid;
    ulCount = 0;

    //
    // We've linked successfully so drop into an infinite loop during which
    // we send messages to the receiver every 5 seconds or so.
    //
    while (1)
    {
        //
        // Send a message every 5 seconds. This could be emulating a sleep.
        //
#ifndef FREQUENCY_AGILITY
        //
        // If Frequency Agility is disabled we don't need to listen for the
        // broadcast channel change command.
        //
        SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);
#endif

        //
        // Kill about 5 seconds.
        //
        SPIN_ABOUT_A_SECOND;
        SPIN_ABOUT_A_SECOND;
        SPIN_ABOUT_A_SECOND;
        SPIN_ABOUT_A_SECOND;
        SPIN_ABOUT_A_SECOND;

#ifndef FREQUENCY_AGILITY
        //
        // See comment above...If Frequency Agility is not enabled we never
        // listen so it is OK if we just awaken leaving Rx off.
        //
        SMPL_Ioctl(IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0);
#endif

        //
        // Send a message.
        //
        eRetcode = SMPL_Send(linkID1, pucMsg, sizeof(pucMsg));
        if (SMPL_SUCCESS == eRetcode)
        {
            //
            // Toggle LED 1 to indicate that we sent something.
            //
            ToggleLED(1);

            //
            // Set up the next message.  Every 8th message toggle LED 1 instead
            // of LED 2 on the receiver.
            //
            pucMsg[0] = (++ucWrap & 0x7) ? 2 : 1;
            pucMsg[1] = ++g_ucTid;
        }

        //
        // Tell the user what we did.
        //
        UpdateStatus(false, "Sent msg %d (%s).", ++ulCount,
                     MapSMPLStatus(eRetcode));
    }
}
Beispiel #8
0
static void linkTo()
{
  uint8_t     msg[2];
  uint8_t     button, misses, done;

  /* Keep trying to link... */
  while (SMPL_SUCCESS != SMPL_Link(&sLinkID1))
  {
    toggleLED(1);
    toggleLED(2);
    SPIN_ABOUT_A_SECOND;
  }

  /* Turn off LEDs. */
  if (BSP_LED2_IS_ON())
  {
    toggleLED(2);
  }
  if (BSP_LED1_IS_ON())
  {
    toggleLED(1);
  }

  /* sleep until button press... */
  SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);

  while (1)
  {
    button = 0;
    /* Send a message when either button pressed */
    if (BSP_BUTTON1())
    {
      SPIN_ABOUT_A_QUARTER_SECOND;  /* debounce... */
      /* Message to toggle LED 1. */
      button = 1;
    }
    else if (BSP_BUTTON2())
    {
      SPIN_ABOUT_A_QUARTER_SECOND;  /* debounce... */
      /* Message to toggle LED 2. */
      button = 2;
    }
    if (button)
    {
      /* get radio ready...awakens in idle state */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0);

      /* Set TID and designate which LED to toggle */
      msg[1] = ++sTid;
      msg[0] = (button == 1) ? 1 : 2;
      done = 0;
      while (!done)
      {
        for (misses=0; misses < MISSES_IN_A_ROW; ++misses)
        {
          if (SMPL_SUCCESS == SMPL_Send(sLinkID1, msg, sizeof(msg)))
          {
#if defined( FREQUENCY_AGILITY )
            /* If macro is defined we're supporting Frequency Agility. In this case
             * the peer will acknowledge the message sent. It is the only way we can
             * tell if we are on the right channel. Wait for ack.
             */
            {
              bspIState_t intState;

              SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXON, 0);
              NWK_REPLY_DELAY();
              SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_RXIDLE, 0);

              if (!sPeerFrameSem)
              {
                /* Try again if we havn't received anything. */
                continue;
              }
              else
              {
                uint8_t len;

                BSP_ENTER_CRITICAL_SECTION(intState);
                sPeerFrameSem--;
                BSP_EXIT_CRITICAL_SECTION(intState);

                /* We got something. Go get it. */
                SMPL_Receive(sLinkID1, msg, &len);
                if (len && (*msg & NWK_APP_REPLY_BIT))
                {
                  toggleLED(*msg & ~NWK_APP_REPLY_BIT);
                  break;
                }
              }
            }
#else
            /* Not supporting Frequency agility. Just break out since there
             * will be no ack.
             */
            break;
#endif
          }
        }
        if (misses == MISSES_IN_A_ROW)
        {
          /* This can only happen if we are supporting Frequency Agility and we
           * appear not to have received an acknowledge. Do a scan.
           */
          ioctlScanChan_t scan;
          freqEntry_t     freq[NWK_FREQ_TBL_SIZE];

          scan.freq = freq;
          SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_SCAN, &scan);
          /* If we now know the channel (number == 1) change to it. In any case
           * try it all again. If we changed channels we should get an ack now.
           */
          if (1 == scan.numChan)
          {
            SMPL_Ioctl(IOCTL_OBJ_FREQ, IOCTL_ACT_SET, freq);
          }
        }
        else
        {
          /* Got the ack. We're done. */
          done = 1;
        }
      }

      /* radio back to sleep */
      SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);
    }
  }
}
//*****************************************************************************
//
// Link to the access point and continue processing local button requests
// forever.  This function is called following initialization in main() and
// does not return.
//
//*****************************************************************************
static void
LinkTo(void)
{
    uint8_t     pucMsg[2];
    uint8_t     ucMisses, ucDone;
    uint8_t     ucNoAck;
    smplStatus_t eRetcode;
    unsigned long ulButton;

    UpdateStatus(false, "Linking to Access Point");

    //
    // Keep trying to link.  Flash our "LEDs" while these attempts continue.
    //
    while (SMPL_SUCCESS != SMPL_Link(&g_sLinkID))
    {
        ToggleLED(1);
        ToggleLED(2);
        SPIN_ABOUT_A_SECOND;
    }

    //
    // Turn off both LEDs now that we are linked.
    //
    SetLED(1, false);
    SetLED(2, false);

    //
    // Tell the user all is well.
    //
    UpdateStatus(false, "Link successful");

    //
    // Put the radio to sleep until a button is pressed.
    //
    SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);

    //
    // Start of the main button processing loop.
    //
    while (1)
    {

        //
        // Grab the button press flag and clear it.  We do this since the flag
        // could be changed during the loop whenever function
        // WidgetMessageQueueProcess() is called and we don't want to miss any
        // button presses.
        //
        ulButton = g_ulButtonPressed;

        //
        // Has either button been pressed?
        //
        if (ulButton)
        {
            //
            // Clear the main button press flag.
            //
            g_ulButtonPressed = 0;

            //
            // Wake the radio up since we are about to need it.
            //
            SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_AWAKE, 0);

            /* Set TID and designate which LED to toggle */
            pucMsg[1] = ++g_ucTid;
            pucMsg[0] = (ulButton == 1) ? 1 : 2;
            ucDone = 0;
            while (!ucDone)
            {

                //
                // Remember that we have yet to receive an ack from the AP.
                //
                ucNoAck = 0;

                //
                // Try sending the message MISSES_IN_A_ROW times looking for an
                // ack after each transmission.
                //
                for (ucMisses = 0; ucMisses < MISSES_IN_A_ROW; ucMisses++)
                {
                    //
                    // Send the message and request acknowledgement.
                    //
                    eRetcode=SMPL_SendOpt(g_sLinkID, pucMsg, sizeof(pucMsg),
                                          SMPL_TXOPTION_ACKREQ);

                    //
                    // Did we get the ack?
                    //
                    if (eRetcode == SMPL_SUCCESS)
                    {
                        //
                        // Yes - Message acked.  We're done.  Toggle LED 1 to
                        // indicate ack received. */
                        ToggleLED(1);
                        break;
                    }

                    //
                    // Did we send the message but fail to get an ack back?
                    //
                    if (SMPL_NO_ACK == eRetcode)
                    {
                        //
                        // Count ack failures.  Could also fail because of CCA
                        // and we don't want to scan in this case.
                        //
                        ucNoAck++;
                    }
                }

                //
                // Did we fail to get an ack after every transmission?
                //
                if (MISSES_IN_A_ROW == ucNoAck)
                {
                    //
                    // Tell the user what happened.
                    //
                    UpdateStatus(false, "Channel changed?");

                    //
                    // Message not acked.  Toggle LED 2.
                    //
                    ToggleLED(2);

#ifdef FREQUENCY_AGILITY
                    //
                    // At this point, we assume we're on the wrong channel so
                    // look for the correct channel by using Ping to initiate a
                    // scan when it gets no reply.  With a successful ping try
                    // sending the message again.  Otherwise, for any error we
                    // get we will wait until the next button press to try
                    // again.
                    //
                    if (SMPL_SUCCESS != SMPL_Ping(g_sLinkID))
                    {
                        ucDone = 1;
                    }
#else
                    ucDone = 1;
#endif  /* FREQUENCY_AGILITY */
                }
                else
                {
                    //
                    // We got the ack so drop out of the transmit loop.
                    //
                    ucDone = 1;

                    UpdateStatus(false, "Toggled AP LED %d", ulButton);
                }
            }

            //
            // Now that we are finished with it, put the radio back to sleep.
            //
            SMPL_Ioctl( IOCTL_OBJ_RADIO, IOCTL_ACT_RADIO_SLEEP, 0);
        }

        //
        // Process the widget message queue.
        //
        WidgetMessageQueueProcess();
    }
}