Example #1
0
void discover_task()
{
	uint8_t len;
	uint8_t connection_l;
	while (!bmac_started ())
    nrk_wait_until_next_period ();		

	if(log_g) printf("log:discover_task PID=%d\r\n",nrk_get_pid());


	while(1) {
			nrk_sem_pend(conn_sem);
			connection_l = connection_g;
			nrk_sem_post(conn_sem);
			

			if(connection_l == 0 && vertsk_active_g == 0 && version_g[MAC_ADDR] > 0) {
				nrk_led_toggle(BLUE_LED);	
				if(log_g) printf("log:Sending discover pkt\r\n");
				nrk_sem_pend(tx_sem);
				sprintf(tx_buf,"0:%d:S:%d",MAC_ADDR,version_g[MAC_ADDR]);
				bmac_tx_pkt(tx_buf, strlen(tx_buf));
				nrk_sem_post(tx_sem);
				memset(tx_buf,0,strlen(tx_buf));
			}
    	nrk_wait_until_next_period ();
	
	}

}
Example #2
0
void tx_task()
{
  uint8_t cnt = 0;
  uint8_t val;
  int8_t v;
  
//  printf( "tx_task: PID=%d\r\n",nrk_get_pid());
  
  bmac_init(25);
  while(!bmac_started()) nrk_wait_until_next_period();

  while(1)
  {
        if (cnt > 25) {
            nrk_led_set(BLUE_LED);
            nrk_terminate_task();
        }

        nrk_led_set(RED_LED); 
        sprintf( tx_buf, "%d", cnt );
        val = bmac_tx_pkt(tx_buf, strlen(tx_buf));
        nrk_led_clr(RED_LED); 
//        nrk_kprintf( PSTR("TX task sent data!\r\n") );
        nrk_wait_until_next_period();

        v = nrk_sem_pend(lock);
        if ( startCnt )
            cnt++;
        v = nrk_sem_post(lock);
  }
}
Example #3
0
File: main.c Project: kamladi/dicio
// tx_cmds() - send all commands out to the network.
void tx_cmds() {
  // local variable instantiation
  packet tx_packet;
  volatile uint8_t local_tx_cmd_queue_size;
  volatile uint8_t tx_length = 0;
  volatile int8_t val = 0;

  // atomically get the queue size
  local_tx_cmd_queue_size = atomic_size(&g_cmd_tx_queue, g_cmd_tx_queue_mux);

  // print out task header
  if((TRUE == g_verbose) && (0 < local_tx_cmd_queue_size)) {
    nrk_kprintf(PSTR("tx_cmds...\r\n"));
  }

  // loop on queue size received above, and no more.
  for(uint8_t i = 0; i < local_tx_cmd_queue_size; i++) {
    nrk_led_set(ORANGE_LED);
    // get a packet out of the queue.
    atomic_pop(&g_cmd_tx_queue, &tx_packet, g_cmd_tx_queue_mux);

    // assemble the packet and senx
    tx_length = assemble_packet((uint8_t *)&g_net_tx_buf, &tx_packet);
    val = bmac_tx_pkt(g_net_tx_buf, tx_length);
    if(NRK_OK != val){
      nrk_kprintf( PSTR( "NO ack or Reserve Violated!\r\n" ));
    }

    nrk_led_clr(ORANGE_LED);
  }
  return;
}
Example #4
0
void tx_task ()
{
		int r;
    uint8_t length, val;
  
    nrk_sig_t tx_done_signal;
    nrk_sig_mask_t ret;
    nrk_time_t r_period;
    while (!bmac_started ())
        nrk_wait_until_next_period ();

    tx_done_signal = bmac_get_tx_done_signal ();
    nrk_signal_register (tx_done_signal);     
    while(1) {
						nrk_wait_until_next_period();
			
						if(functionRecieved) {
							tx_buf[0] = MY_ID;
							tx_buf[1] = 100;
							length = 2;
							val=bmac_tx_pkt(tx_buf, length);
							r = copied_function();
							printf("Value returned from copied function is %d\n",r);
            }

        }
  
}
Example #5
0
void inter_tx_task () {
    uint8_t i;
    nrk_sig_t tx_done_signal;
    while (!bmac_started ())
        nrk_wait_until_next_period ();
    tx_done_signal = bmac_get_tx_done_signal ();
    nrk_signal_register (tx_done_signal);
    bmac_addr_decode_enable();
    bmac_addr_decode_set_my_mac(MyOwnAddress);
    while (1) {
        if(ipQue>0) {
            if(itxPtr>queMax-1) {
                itxPtr=0;
            }
            bmac_addr_decode_enable();
            bmac_addr_decode_set_my_mac(MyOwnAddress);
            nrk_led_set(BLUE_LED);
            bmac_auto_ack_disable();
            for(i=0; i<ipLen[itxPtr]; i++) {
                itx_buf[i]=ipDat[itxPtr][i];
            }
            if(ipDes[itxPtr]==0xFF) {
                bmac_addr_decode_dest_mac(0xFFFF);
            }
            else {
                bmac_addr_decode_dest_mac(ipDes[itxPtr]);
            }
            bmac_tx_pkt((char*)itx_buf,ipLen[itxPtr]);
            ipQue--;
            itxPtr++;
            nrk_led_clr(BLUE_LED);
        }
        nrk_wait_until_next_period ();
    }
}
Example #6
0
void ack_tx_task()
{
    int i=0;
    uint8_t val;
    nrk_sig_t tx_done_signal;

    while (!bmac_started ())
        nrk_wait_until_next_period ();
    //tx_done_signal = bmac_get_tx_done_signal ();
    
    
    nrk_signal_register (tx_done_signal);
    //printf("Ack task\n");
    while(1)
    {


        for(i=1;i<=MAX_MOLES;i++)
        {

            if(i==MY_ID)
            {
                continue;
            }

            //send Ack
            if(sendAckFlag[i])
            {
                sendAckFlag[i]=0;
                switch(receiverNextPacket[i])
                {
                case SEND_ACK:
                    val=bmac_tx_pkt(&(ack_buf[i][0]),5);
                    printf("Sent Ack\n");
                    receiverNextPacket[i] = STOP;
                    break;
                default:
                    break;

                }


            }
        }
        nrk_wait_until_next_period();



    }


}
Example #7
0
void Task3()
{
  uint8_t i, len,cnt;
  int8_t rssi, val;
  uint8_t *local_rx_buf;
  nrk_sig_mask_t ret;
  nrk_time_t check_period;  

  printf( "Task3 PID=%u\r\n",nrk_get_pid());
  // init bmac on channel 25 
  bmac_init (25);
  bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);

  while (!bmac_started ())
    nrk_wait_until_next_period ();

/* while (1) {
    // Wait until an RX packet is received
  nrk_kprintf( PSTR( "Waiting for packet\r\n" ));
    val = bmac_wait_until_rx_pkt ();
    // Get the RX packet 
    nrk_led_set (ORANGE_LED);
    local_rx_buf = bmac_rx_pkt_get (&len, &rssi);
    printf ("Got RX packet len=%d RSSI=%d [", len, rssi);
    for (i = 0; i < len; i++)
      printf ("%c", rx_buf[i]);
    printf ("]\r\n");
    nrk_led_clr (ORANGE_LED);
    // Release the RX buffer so future packets can arrive 
    bmac_rx_pkt_release ();
  }
*/

  while (1) {

    // Build a TX packet
    sprintf (tx_buf, "This is a test %d", cnt);
    cnt++;
    nrk_led_set (RED_LED);

    // For blocking transmits, use the following function call.
    // For this there is no need to register  
     val=bmac_tx_pkt(tx_buf, strlen(tx_buf)+1);

    // Task gets control again after TX complete
    nrk_kprintf (PSTR ("Tx task sent data!\r\n"));
    nrk_led_clr (RED_LED);
    nrk_wait_until_next_period ();
   }

}
Example #8
0
void retrans_task() {

  if(log_g) printf ("log:retrans_task PID=%d\r\n", nrk_get_pid ());
	uint8_t cnt = 0;
	uint8_t connection_l;
	uint8_t data_cnt;
	uint8_t prev_data_seq = 0;  
//	while (!bmac_started ())
    nrk_wait_until_next_period ();

	while(1) {
		nrk_sem_pend(conn_sem);
		connection_l = connection_g;
		nrk_sem_post(conn_sem);	

		if(pending_retransmit_g == 1)  {
				if(log_g) printf("log:Re-transmitting packet\r\n");	
				bmac_tx_pkt(uart_rx_buf, strlen(uart_rx_buf));
				retransmit_count_g++;
				if(retransmit_count_g==5)
				terminate_connection();
			} else {
				// this checks if there is a connection and one is receivng data then 
				// if the sender shuts down then the connection is terminated after a while	
				if(connection_l == 1 && data_index_g < 0) {
					if(recv_data_seq_g == prev_data_seq) 
							data_cnt++;
					if(recv_data_seq_g > prev_data_seq)
					{
							data_cnt = 0; 	
							prev_data_seq = recv_data_seq_g;
					}
					if(data_cnt > 5) {
						if(log_g)nrk_kprintf(PSTR("log:Recv cn term\n"));
						terminate_connection();
						data_cnt = 0; 
					}
				}

				cnt++;
				//if(log_g) printf("log:ver activ con:%d cnt:%d\n",connection_l,cnt);
				if(cnt>0 && connection_l == 0) {
				cnt = 0;
				vertsk_active_g = 1; 
				}
			}

		nrk_wait_until_next_period();

	}
}
Example #9
0
File: main.c Project: kamladi/dicio
// tx_data_task() - send standard messages out to the network (i.e. handshake messages, etc.)
void tx_data() {
  // local variable initialization
  packet tx_packet;
  volatile int8_t val = 0;
  volatile uint8_t sent_heart = FALSE;
  volatile uint8_t to_send;
  volatile uint8_t tx_length = 0;
  volatile uint8_t local_tx_data_queue_size;
  volatile msg_type tx_type;

  // atomically get the queue size
  local_tx_data_queue_size = atomic_size(&g_data_tx_queue, g_data_tx_queue_mux);

  // print out task header
  if((TRUE == g_verbose) && (0 < local_tx_data_queue_size)){
    nrk_kprintf(PSTR("tx_data...\r\n"));
  }

  // loop on queue size received above, and no more.
  for(uint8_t i = 0; i < local_tx_data_queue_size; i++) {
    nrk_led_set(ORANGE_LED);
    // get a packet out of the queue.
    atomic_pop(&g_data_tx_queue, &tx_packet, g_data_tx_queue_mux);

    // get packet parameters
    tx_type = tx_packet.type;

    // only hop one heartbeat per iteration.
    if(((MSG_HEARTBEAT == tx_type) || (MSG_RESET == tx_type)) && (TRUE == sent_heart)) {
      to_send = FALSE;
    } else {
      to_send = TRUE;
    }

    if (TRUE == to_send) {
      // assembe and send packet
      tx_length = assemble_packet((uint8_t *)&g_net_tx_buf, &tx_packet);
      val = bmac_tx_pkt(g_net_tx_buf, tx_length);
      if(NRK_OK != val){
        nrk_kprintf( PSTR( "NO ack or Reserve Violated!\r\n" ));
      }
      // set flag
      if(MSG_HEARTBEAT == tx_type){
        sent_heart = TRUE;
      }
    }
    nrk_led_clr(ORANGE_LED);
  }

  return;
}
Example #10
0
/***************************************************************************
 *
 *
 *
 *Retransmission Task
 *
 *
 *
 ****************************************************************************/
void re_tx_task()
{

    int i=0;
    uint8_t val;
    nrk_sig_t tx_done_signal;

    while (!bmac_started ())
        nrk_wait_until_next_period ();

    tx_done_signal = bmac_get_tx_done_signal ();
    nrk_signal_register (tx_done_signal);
    //Retransmission
    while(1)
    {
        for(i=1;i<=MAX_MOLES;i++)
        {

            if(i==MY_ID)
            {
                continue;
            }
            switch(sendNextPacket[i])
            {
            case LAST_PACKET_ACK:
            case WAIT_ACK:
                val=bmac_tx_pkt(&tx_buf[TX_LOCATION(i)], transmittedPacketLength[i]);
                printf("Retransmitted\r\n");
                break;
            default:
                break;
            }



        }
        nrk_wait_until_next_period();



    }
}
Example #11
0
void tx_task ()
{
  uint8_t j, i, val, len;
  int8_t v;
  nrk_sig_t tx_done_signal;
  nrk_sig_mask_t ret;
  nrk_time_t r_period;
	
	
  while (!bmac_started ())
    nrk_wait_until_next_period ();

  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);

  ctr_cnt[0]=0; ctr_cnt[1]=0; ctr_cnt[2]=0; ctr_cnt[3]=0;
  tx_count = 0;
  while (1) {
		if (tx_count >= 300) {
		printf("Total packets sent : %d \r\n", tx_count);
		}
    // Build a TX packet
		if (tx_count < 300) {
			sprintf(tx_buf, "Hello World");
			nrk_led_set (BLUE_LED);
		   
			bmac_auto_ack_disable();

			val=bmac_tx_pkt(tx_buf, strlen(tx_buf));
			if(val==NRK_OK) tx_count++;		 
			else printf("NO ack or Reserve Violated! \r\n");		
		 
			printf("Tx task sent data!\r\n");
			nrk_led_clr (BLUE_LED);
			printf("tx_task PID=%d\r\n", nrk_get_pid ());
		}
		nrk_wait_until_next_period ();
  }
}
Example #12
0
void slaveDiscovery_task(){
    uint8_t val;
    nrk_sig_t sd_done_signal; // Signal registering to get transmission done

    //Wait till BMAC is ready
    while (!bmac_started ())
        nrk_wait_until_next_period ();
    //Set the tx signal
    sd_done_signal = bmac_get_tx_done_signal ();
    nrk_signal_register (sd_done_signal);




    while(1){
        tx_buf[0] = MY_ID;                  // indicates packet from master
        tx_buf[1] = SLAVE_DISCOVERY;        // slave discovery message
        tx_buf[2] = sequenceNumber;         // sequenceNumber of the packet
        sequenceNumber++;                   // incrementing the sequence Number
        val=bmac_tx_pkt(tx_buf,3);          // transmitting the packet
        checkIfReceivedResponse();
        nrk_wait_until_next_period ();
    }
}
Example #13
0
void pingMode()
{

  // Broadcast for ping
  val=bmac_addr_decode_set_my_mac(((uint16_t)my_subnet_mac<<8)|my_mac);
  val=bmac_addr_decode_dest_mac((uint16_t)0xffff);  // broadcast by default
  bmac_addr_decode_enable();
  
  uint8_t i;
  
  // Build a TX packet by hand...
  p2p_pkt.pkt_type = PING_PKT;
    // set as p2p packet (no US_MASK or DS_MASK)
  p2p_pkt.ctrl_flags = MOBILE_MASK ; // | DEBUG_FLAG ;
  p2p_pkt.ack_retry= 0x00;
  p2p_pkt.ttl = 1;
  p2p_pkt.src_subnet_mac[0] = 0;
  p2p_pkt.src_subnet_mac[1] = 0;
  p2p_pkt.src_subnet_mac[2] = 0;
  p2p_pkt.src_mac = my_mac;
  p2p_pkt.last_hop_mac = my_mac;
  p2p_pkt.dst_subnet_mac[0] = BROADCAST;
  p2p_pkt.dst_subnet_mac[1] = BROADCAST;
  p2p_pkt.dst_subnet_mac[2] = BROADCAST;
  p2p_pkt.dst_mac = BROADCAST;
  p2p_pkt.buf=tx_buf;
  p2p_pkt.buf_len = P2P_PAYLOAD_START;
  p2p_pkt.seq_num = cnt;
  p2p_pkt.priority = 0;
  cnt++;
//    p2p_pkt.payload[0]=my_mac;
//    p2p_pkt.payload_len=1;
//    ping_p2p_generate(&p2p_pkt);

  nrk_led_set (BLUE_LED);

  check_period.secs = 0;
  check_period.nano_secs = 100 * NANOS_PER_MS;
  val = bmac_set_rx_check_rate (check_period);

    // Pack data structure values in buffer before transmit
  pack_peer_2_peer_packet(&p2p_pkt);
    // For blocking transmits, use the following function call.
  val = bmac_tx_pkt (p2p_pkt.buf, p2p_pkt.buf_len);

  check_period.secs = 0;
  check_period.nano_secs = FAST_CHECK_RATE * NANOS_PER_MS;
  val = bmac_set_rx_check_rate (check_period);
#ifdef TXT_DEBUG
  nrk_kprintf (PSTR ("\r\nPING Packet Sent. Waiting for Replies...\r\n\n"));
#endif
  nrk_led_clr (BLUE_LED);
  nrk_led_clr(GREEN_LED);

    // Wait for packets or timeout
  nrk_time_get (&start);
    
  while (1) {

    timeout.secs = PING_WAIT_SECS;
    timeout.nano_secs = 0;

      // Wait until an RX packet is received
      //val = bmac_wait_until_rx_pkt ();
    nrk_set_next_wakeup (timeout);
    my_sigs = nrk_event_wait (SIG (rx_signal) | SIG (nrk_wakeup_signal));


    if (my_sigs == 0)
      nrk_kprintf (PSTR ("Error calling nrk_event_wait()\r\n"));
    if (my_sigs & SIG (rx_signal)) {	
        // Get the RX packet 
      local_rx_buf = bmac_rx_pkt_get (&len, &rssi);
	// Check the packet type from raw buffer before unpacking
      if ((local_rx_buf[CTRL_FLAGS] & (DS_MASK | US_MASK)) == 0) {
	// Set the buffer
        p2p_pkt.buf=local_rx_buf;
        p2p_pkt.buf_len=len;
        p2p_pkt.rssi=rssi;
        unpack_peer_2_peer_packet(&p2p_pkt);
        if (p2p_pkt.dst_mac == my_mac || p2p_pkt.dst_mac == BROADCAST) {
          nrk_led_set(GREEN_LED);
          // Packet arrived and is good to go
          printf( "Mac: %x%x%x",p2p_pkt.src_subnet_mac[0], p2p_pkt.src_subnet_mac[1], p2p_pkt.src_subnet_mac[2]);
          printf( "%x ",p2p_pkt.src_mac);
          printf( "| RSSI: %d ",p2p_pkt.rssi);
          printf( "| Type: %d \r\n",p2p_pkt.pkt_type);
        }
      }
      // Release the RX buffer so future packets can arrive
      bmac_rx_pkt_release ();
    }
    nrk_time_get (&current);
    if (start.secs + PING_WAIT_SECS < current.secs)
      break;
  }
  nrk_kprintf (PSTR ("\r\nDone Waiting for Response...\r\n"));
  nrk_led_clr(GREEN_LED);
}
Example #14
0
void tx_task ()
{
    int r;
    int i=0,j=0;
    uint8_t val;
    nrk_sig_t tx_done_signal;
    while (!bmac_started ())
        nrk_wait_until_next_period ();

    tx_done_signal = bmac_get_tx_done_signal ();
    nrk_signal_register (tx_done_signal);
    for(i=1;i<=MAX_MOLES;i++)
    {
        tx_buf[SOURCE_ADDRESS_LOCATION + TX_LOCATION(i)] = MY_ID;
    }
    printf("Transmit Task\r\n");
    while(1){


        for(i=1;i<=MAX_MOLES;i++)
        {

            if(i==MY_ID)
            {
                continue;
            }
            //Check the state of the state machine
            switch(sendNextPacket[i])
            {
            //Start the transmission
            case START:
                tx_buf[SEQUENCE_NUM_LOCATION+TX_LOCATION(i)] = ++sequenceNumber;

                lastSentSequenceNo[i]= sequenceNumber;
                //Create a new packet
                r=createNextTaskPacket(&tx_buf[TX_LOCATION(i)],&data[DATA_LOCATION(i)],packetSize[i],i);
                //Decide the packet length
                transmittedPacketLength[i] = r+HEADER_SIZE;
                
								for(j=0;j<transmittedPacketLength[i];j++)
								printf("%d \t",tx_buf[TX_LOCATION(i)+j]);
								
								if(r == 100)
                {
                    sendNextPacket[i] = WAIT_ACK;
                }
                else
                {
                    sendNextPacket[i] = LAST_PACKET_ACK;
                }
                val=bmac_tx_pkt(&tx_buf[TX_LOCATION(i)], transmittedPacketLength[i]);
                break;

            case IN_PROGRESS:
                tx_buf[SEQUENCE_NUM_LOCATION+TX_LOCATION(i)] = ++sequenceNumber;
                lastSentSequenceNo[i]= sequenceNumber;
                //Create a new packet
                r=createNextTaskPacket(&tx_buf[TX_LOCATION(i)],&data[DATA_LOCATION(i)],packetSize[i],i);
                //Decide the packet length
                transmittedPacketLength[i] = r+HEADER_SIZE;

                if(r == 100)
                {
                    sendNextPacket[i] = WAIT_ACK;
                }
                else
                {
                    printf("last packet\n");
                    sendNextPacket[i] = LAST_PACKET_ACK;
                }
                val=bmac_tx_pkt(&tx_buf[TX_LOCATION(i)], transmittedPacketLength[i]);
                break;
            case WAIT_ACK:
            case END:
            default:
                break;



            }
        }

        nrk_wait_until_next_period();
    }

}
Example #15
0
void tx_task ()
{
  uint8_t j, i, val, len, byte_cnt, got_start;
  int8_t v;
  nrk_sig_t tx_done_signal;
  nrk_sig_mask_t ret;
  nrk_time_t r_period;
  nrk_sig_t uart_rx_signal;
  nrk_sig_mask_t sm;



   // Get the signal for UART RX  
  uart_rx_signal=nrk_uart_rx_signal_get();
  // Register your task to wakeup on RX Data 
  if(uart_rx_signal==NRK_ERROR) nrk_kprintf( PSTR("Get Signal ERROR!\r\n") );
  nrk_signal_register(uart_rx_signal);

  printf ("tx_task PID=%d\r\n", nrk_get_pid ());

  // Wait until the tx_task starts up bmac
  // This should be called by all tasks using bmac that
  // do not call bmac_init()...
  while (!bmac_started ())
    nrk_wait_until_next_period ();

  nrk_led_set(RED_LED);
  // Sample of using Reservations on TX packets
  // This example allows 2 packets to be sent every 5 seconds
  // r_period.secs=5;
  // r_period.nano_secs=0;
  // v=bmac_tx_reserve_set( &r_period, 2 );
  // if(v==NRK_ERROR) nrk_kprintf( PSTR("Error setting b-mac tx reservation (is NRK_MAX_RESERVES defined?)\r\n" ));


  // Get and register the tx_done_signal if you want to
  // do non-blocking transmits
  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);
  bmac_set_cca_active(0);
  ctr_cnt[0]=0; ctr_cnt[1]=0; ctr_cnt[2]=0; ctr_cnt[3]=0;
  while (1) {
    // Wait for UART signal
        got_start=0; 
        byte_cnt=0;
	do {
        sm=nrk_event_wait(SIG(uart_rx_signal));
        while(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0)
                {
                // Read Character
                val=getchar();
		tx_buf[byte_cnt]=val;
		if(got_start && val==0xff ) 
		{
			nrk_led_toggle(ORANGE_LED);
			printf( "0xff byte: %d\n", byte_cnt );
			byte_cnt=0;
			got_start=0;
			val=1;  // Something other than 0xff so that the
				// got_start isn't set
		}
                if(val==0xff) 
			{
				nrk_led_toggle(GREEN_LED);
				got_start=1;
			}
		if(got_start) byte_cnt++;
		if(byte_cnt>3) break;
                }
         } while(byte_cnt<4);

   
    // Build a TX packet
    nrk_led_set (BLUE_LED);

    // Auto ACK is an energy efficient link layer ACK on packets
    // If Auto ACK is enabled, then bmac_tx_pkt() will return failure
    // if no ACK was received. In a broadcast domain, the ACK's will
    // typically collide.  To avoid this, one can use address decoding. 
    // The functions are as follows:
    // bmac_auto_ack_enable();
    // bmac_auto_ack_disable();

    // Address decoding is a way of preventing the radio from receiving
    // packets that are not address to a particular node.  This will 
    // supress ACK packets from nodes that should not automatically ACK.
    // The functions are as follows:
    // bmac_addr_decode_set_my_mac(uint16_t MAC_ADDR); 
    // bmac_addr_decode_dest_mac(uint16_t DST_ADDR);  // 0xFFFF is broadcast
    // bmac_addr_decode_enable();
    // bmac_addr_decode_disable();

     ctr_cnt[0]++; 
     if(ctr_cnt[0]==255) ctr_cnt[1]++; 
     if(ctr_cnt[1]==255) ctr_cnt[2]++; 
     if(ctr_cnt[2]==255) ctr_cnt[3]++; 
     // You need to increase the ctr on each packet to make the 
     // stream cipher not repeat.
     bmac_encryption_set_ctr_counter(&ctr_cnt,4);

    // For blocking transmits, use the following function call.
    // For this there is no need to register  
/*	nrk_kprintf( PSTR("Sending: ") );	
	for(i=0; i<byte_cnt; i++ )
	   printf( "%d ",tx_buf[i] );
	nrk_kprintf( PSTR("\r\n") );	
  */  
     val=bmac_tx_pkt(tx_buf, byte_cnt);


    // This function shows how to transmit packets in a
    // non-blocking manner  
    // val = bmac_tx_pkt_nonblocking(tx_buf, strlen (tx_buf));
    // nrk_kprintf (PSTR ("Tx packet enqueued\r\n"));
    // This functions waits on the tx_done_signal
    // ret = nrk_event_wait (SIG(tx_done_signal));

    // Just check to be sure signal is okay
    // if(ret & SIG(tx_done_signal) == 0 ) 
    // nrk_kprintf (PSTR ("TX done signal error\r\n"));
   
    // If you want to see your remaining reservation
    // printf( "reserve=%d ",bmac_tx_reserve_get() );
    
    // Task gets control again after TX complete
    nrk_led_clr (BLUE_LED);
  }

}
Example #16
0
/*
void inter_tx_task ()
{
  
	uint8_t j, i, val, len, cnt;
  nrk_sig_t tx_done_signal;
  nrk_sig_mask_t ret;
  nrk_time_t r_period;
  	
	
  while (!bmac_started ())
    nrk_wait_until_next_period ();

  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);
  cnt = 0;
  
  while (1) {
    if(inter_flag==1){
		printf("Hi..\n");
	  bmac_addr_decode_disable();
    bmac_addr_decode_set_my_mac(MY_MAC_ADDR);
	  sprintf(&tx_buf[0],"%d",MY_MAC_ADDR);
	  for(i=0;i<strlen(inter_tx_buf);i++){
	  tx_buf[i+1]=inter_tx_buf[i];
	}
	  nrk_led_set (BLUE_LED);
    bmac_auto_ack_disable();
    bmac_addr_decode_dest_mac(dst_addr);  // 0xFFFF is broadcast
	  val=bmac_tx_pkt(tx_buf, strlen(tx_buf));
	  inter_flag=0;
    printf("Tx task sent data!\r\n");
    nrk_led_clr (BLUE_LED);
	  printf("tx_task PID=%d\r\n", nrk_get_pid ());
}  
  nrk_wait_until_next_period ();
  }

}
*/
void tx_task ()
{
  uint8_t j, i, val, len, cnt,turn=0;
  nrk_sig_t tx_done_signal;
  nrk_sig_mask_t ret;
  nrk_time_t r_period;
   while (!bmac_started ())
     nrk_wait_until_next_period ();
  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);
  cnt = 0;
  
  while (1) {
    if(inter_flag==0 ){
		printf("Hi..\n");
	  bmac_addr_decode_disable();
    bmac_addr_decode_set_my_mac(MY_MAC_ADDR);
	  sprintf(tx_buf,"node_id:%d count:%d",MY_MAC_ADDR,cnt);
    nrk_led_set (BLUE_LED);
		
   
    bmac_auto_ack_disable();
    bmac_addr_decode_dest_mac(dst_addr);  // 0xFFFF is broadcast
	  val=bmac_tx_pkt(tx_buf, strlen(tx_buf));
    printf("Tx task sent data!\r\n");
    nrk_led_clr (BLUE_LED);
		printf("tx_task PID=%d\r\n", nrk_get_pid ());
    }
	  
			if(inter_flag==1){
				if(turn==0){
					printf("Hi..\n");
	  bmac_addr_decode_disable();
    bmac_addr_decode_set_my_mac(MY_MAC_ADDR);
	  sprintf(tx_buf,"Path: %d-1 node_id:%d count:%d",MY_MAC_ADDR,MY_MAC_ADDR,cnt);
    nrk_led_set (BLUE_LED);
		bmac_auto_ack_disable();
    bmac_addr_decode_dest_mac(dst_addr);  // 0xFFFF is broadcast
	  val=bmac_tx_pkt(tx_buf, strlen(tx_buf));
    printf("Tx task sent data!\r\n");
    nrk_led_clr (BLUE_LED);
		printf("tx_task PID=%d\r\n", nrk_get_pid ());
					turn=1;
				}
				else{
	  bmac_addr_decode_disable();
    bmac_addr_decode_set_my_mac(MY_MAC_ADDR);
	  sprintf(&tx_buf[0],"%d",MY_MAC_ADDR);
	  for(i=0;i<strlen(inter_tx_buf);i++){
	  tx_buf[i+1]=inter_tx_buf[i];
	}
	  nrk_led_set (BLUE_LED);
    bmac_auto_ack_disable();
    bmac_addr_decode_dest_mac(dst_addr);  // 0xFFFF is broadcast
	  val=bmac_tx_pkt(tx_buf, strlen(tx_buf));
	  printf("Tx task sent data!\r\n");
    nrk_led_clr (BLUE_LED);
	  printf("tx_task PID=%d\r\n", nrk_get_pid ());
	  turn=0;
}
}

  cnt++;
nrk_wait_until_next_period ();
   }

}
Example #17
0
void tx_task ()
{
  uint8_t j, i,  error,unique;
  uint8_t samples;
  int8_t len;
  int8_t rssi, val;
  uint8_t *local_rx_buf;

  nrk_sig_t tx_done_signal;
  nrk_sig_t rx_signal;
  nrk_sig_mask_t ret;
  nrk_time_t check_period;
  nrk_time_t timeout, start, current;
  nrk_sig_mask_t my_sigs;

  printf ("tx_task PID=%d\r\n", nrk_get_pid ());


  // Wait until the tx_task starts up bmac
  // This should be called by all tasks using bmac that
  // do not call bmac_init()...
  bmac_init (26);

  bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);

  val=bmac_addr_decode_set_my_mac(((uint16_t)MY_SUBNET_MAC_0<<8)|MY_MAC);
  val=bmac_addr_decode_dest_mac(0xffff);  // broadcast by default
  bmac_addr_decode_enable();

  nrk_kprintf (PSTR ("bmac_started()\r\n"));
  bmac_set_cca_thresh (-45);


  check_period.secs = 0;
  check_period.nano_secs = 100 * NANOS_PER_MS;
  val = bmac_set_rx_check_rate (check_period);

  // Get and register the tx_done_signal if you want to
  // do non-blocking transmits
  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);

  rx_signal = bmac_get_rx_pkt_signal ();
  nrk_signal_register (rx_signal);

  cnt = 0;

    check_period.secs = 0;
    check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);

  while (1) {



    my_nlist_elements=0;

for(samples=0; samples<10; samples++ )
{
    nrk_led_set (GREEN_LED);
    check_period.secs = 0;
    check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);
    build_ping_pkt( &p2p_pkt );
    // Pack data structure values in buffer before transmit
    pack_peer_2_peer_packet(&p2p_pkt);
    // For blocking transmits, use the following function call.
    val = bmac_tx_pkt (p2p_pkt.buf, p2p_pkt.buf_len);

    check_period.secs = 0;
    check_period.nano_secs = p2p_pkt.check_rate * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);
#ifdef TXT_DEBUG
    nrk_kprintf (PSTR ("\r\nSent Request:\r\n"));
#endif
    nrk_led_clr (GREEN_LED);



    // Wait for packets or timeout
    nrk_time_get (&start);
    while (1) {

      timeout.secs = REPLY_WAIT_SECS;
      timeout.nano_secs = 0;

      // Wait until an RX packet is received
      //val = bmac_wait_until_rx_pkt ();
      nrk_set_next_wakeup (timeout);
      my_sigs = nrk_event_wait (SIG (rx_signal) | SIG (nrk_wakeup_signal));


      if (my_sigs == 0)
        nrk_kprintf (PSTR ("Error calling nrk_event_wait()\r\n"));
      if (my_sigs & SIG (rx_signal)) {
	
        // Get the RX packet 
        local_rx_buf = bmac_rx_pkt_get (&len, &rssi);
	// Check the packet type from raw buffer before unpacking
        if ((local_rx_buf[CTRL_FLAGS] & (DS_MASK | US_MASK)) == 0) {

	// Set the buffer
	p2p_pkt.buf=local_rx_buf;
	p2p_pkt.buf_len=len;
	p2p_pkt.rssi=rssi;
	unpack_peer_2_peer_packet(&p2p_pkt);
#ifdef TXT_DEBUG
	  // Check if newly received packet is for this node
          if (((p2p_pkt.dst_subnet_mac[2] == MY_SUBNET_MAC_2 &&
		p2p_pkt.dst_subnet_mac[1] == MY_SUBNET_MAC_1 &&
		p2p_pkt.dst_subnet_mac[0] == MY_SUBNET_MAC_0 &&
		p2p_pkt.dst_mac == MY_MAC ) 
		|| p2p_pkt.dst_mac == BROADCAST) 
		    && p2p_pkt.pkt_type==PING_PKT) {
		// Packet arrived and is good to go
              	printf( "src: %d ",p2p_pkt.src_mac);  
              	printf( "rssi: %d ",p2p_pkt.rssi);      
              	printf( "subnet: %d %d %d ",p2p_pkt.src_subnet_mac[0], p2p_pkt.src_subnet_mac[1], p2p_pkt.src_subnet_mac[2]);  
              	printf( "type: %d ",p2p_pkt.pkt_type);      
              	nrk_kprintf (PSTR ("payload: ["));
              	for (i = 0; i < p2p_pkt.payload_len; i++)
                	printf ("%d ", p2p_pkt.payload[i]); 
              	nrk_kprintf (PSTR ("]\r\n"));
	
		unique=1;	
		// Check if the MAC is unique
		for(i=0; i<my_nlist_elements; i++ )
		{

		if(my_nlist[i*NLIST_SIZE]==p2p_pkt.src_subnet_mac[2] &&
			my_nlist[i*NLIST_SIZE+1]==p2p_pkt.src_subnet_mac[1] &&
			my_nlist[i*NLIST_SIZE+2]==p2p_pkt.src_subnet_mac[0] &&
			my_nlist[i*NLIST_SIZE+3]==p2p_pkt.src_mac)
			{
				unique=0;
				break;
			}
		}

		// If MAC is unique, add it
		if(unique)
		{
			my_nlist[my_nlist_elements*NLIST_SIZE]=p2p_pkt.src_subnet_mac[2];
			my_nlist[my_nlist_elements*NLIST_SIZE+1]=p2p_pkt.src_subnet_mac[1];
			my_nlist[my_nlist_elements*NLIST_SIZE+2]=p2p_pkt.src_subnet_mac[0];
			my_nlist[my_nlist_elements*NLIST_SIZE+3]=p2p_pkt.src_mac;
			my_nlist[my_nlist_elements*NLIST_SIZE+4]=p2p_pkt.rssi;
			my_nlist_elements++;
		}
            }
#endif

        }
        // Release the RX buffer so future packets can arrive 
        bmac_rx_pkt_release ();
      }

      nrk_time_get (&current);
      if (start.secs + REPLY_WAIT_SECS < current.secs)
        break;
    }
    cnt++; 
}
    check_period.secs = 0;
    check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);
    
    nrk_kprintf (PSTR ("Done Waiting for response...\r\n"));
    nrk_kprintf (PSTR ("\r\n\r\nSurvey Says:\r\n"));
   
    nrk_kprintf( PSTR("LOC_DESC: \"location name\"\r\n" )); 
    for(i=0; i<my_nlist_elements; i++ )
    {
	nrk_kprintf( PSTR( "MAC: " ));
	if(my_nlist[i*NLIST_SIZE]<0x10) printf( "0%x", my_nlist[i*NLIST_SIZE] );
	else printf( "%x", my_nlist[i*NLIST_SIZE] );
	if(my_nlist[i*NLIST_SIZE]<0x10) printf( "0%x", my_nlist[i*NLIST_SIZE+1] );
	else printf( "%x", my_nlist[i*NLIST_SIZE+1] );
	if(my_nlist[i*NLIST_SIZE]<0x10) printf( "0%x", my_nlist[i*NLIST_SIZE+2] );
	else printf( "%x", my_nlist[i*NLIST_SIZE+2] );
	if(my_nlist[i*NLIST_SIZE]<0x10) printf( "0%x", my_nlist[i*NLIST_SIZE+3] );
	else printf( "%x", my_nlist[i*NLIST_SIZE+3] );
	printf( " RSSI: %d\r\n", (int8_t)my_nlist[i*NLIST_SIZE+4] );

    }

 
    nrk_wait_until_next_period ();
    
  }

}
Example #18
0
File: main.c Project: nycdarren/mrk
// This task periodically sends and XMPP message through the gateway
// to an arbitrary JID on the network.
void tx_task ()
{
    uint8_t cnt ;
    int8_t val;


    printf ("tx_task PID=%d\r\n", nrk_get_pid ());

    // Configure address for other packet handlers (my not be needed)
    my_mac= MY_MAC;
    my_subnet_mac[0]= MY_SUBNET_MAC_0;
    my_subnet_mac[1]= MY_SUBNET_MAC_1;
    my_subnet_mac[2]= MY_SUBNET_MAC_2;
    mac_address= (uint8_t)MY_SUBNET_MAC_2 << 24 | (uint8_t)MY_SUBNET_MAC_1 <<16 | (uint8_t)MY_SUBNET_MAC_0 << 8 | (uint8_t)MY_MAC;



    // Wait until the tx_task starts up bmac
    // This should be called by all tasks using bmac that
    // do not call bmac_init()...
    bmac_init (15); //channel 15

    bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);

    val =
        bmac_addr_decode_set_my_mac (((uint16_t) MY_SUBNET_MAC_0 << 8) | MY_MAC);
    val = bmac_addr_decode_dest_mac (0xffff);     // broadcast by default
    bmac_addr_decode_enable ();

    nrk_kprintf (PSTR ("bmac_started()\r\n"));
    bmac_set_cca_thresh (-45);




    cnt = 0;

    while (1) {


        // Build an XMPP p2p packet
        p2p_pkt.pkt_type = XMPP_PKT;
        p2p_pkt.ctrl_flags = LINK_ACK | MOBILE_MASK;
        p2p_pkt.ack_retry = 0xf0;
        p2p_pkt.ttl = 5;
        p2p_pkt.src_subnet_mac[0] = MY_SUBNET_MAC_0;
        p2p_pkt.src_subnet_mac[1] = MY_SUBNET_MAC_1;
        p2p_pkt.src_subnet_mac[2] = MY_SUBNET_MAC_2;
        p2p_pkt.src_mac = MY_MAC;
        p2p_pkt.last_hop_mac = MY_MAC;
        // Set destination to be any nearby gateway
        p2p_pkt.dst_subnet_mac[0] = BROADCAST;
        p2p_pkt.dst_subnet_mac[1] = BROADCAST;
        p2p_pkt.dst_subnet_mac[2] = BROADCAST;
        p2p_pkt.dst_mac = 0;
        p2p_pkt.buf = tx_buf;
        p2p_pkt.buf_len = P2P_PAYLOAD_START;
        p2p_pkt.seq_num = cnt;
        p2p_pkt.priority = 0;
        p2p_pkt.check_rate = 100;
        p2p_pkt.payload = &(tx_buf[P2P_PAYLOAD_START]);


        // Create XMPP lite message that includes:
        //   1) destination JID
        //   2) Your node's password
        //   3) timeout value in seconds
        //   4) Message as a string (only ascii text, no XML delimiters)
        //
        // The total size of the packet must be under 110 bytes

        // CHANGE THIS FIRST!
        // Setting binary_flag to 1 will convert message into ASCII HEX format
        // Setting binary_flag to 0 will send ASCII string
        xp.binary_flag=0;
        xp.pub_sub_flag=0;
        xp.explicit_src_jid_flag=0;
        xp.src_jid_size=0;
        sprintf (my_passwd, "firefly");
        xp.passwd = my_passwd;  // This is the mobile node JID's password
        sprintf (dst_jid, "vrajkuma3");
        //sprintf (dst_jid, "*****@*****.**");
        // JIDs without the @ symbol are set to the gateway's server
        xp.dst_jid = dst_jid;  // This is the destination of the message
        sprintf (tst_msg, "omg, I'm trapped in an elevator, lol %d", cnt);
        xp.msg = tst_msg;  // This is the message body
        xp.timeout = 30;   // 30 second timeout for the connection
        // After 30 seconds, the gateway will log the node out


        // If you need to login as a different user
        xp.explicit_src_jid_flag=1;
        sprintf (src_jid, "vrajkuma2");
        xp.src_jid = src_jid;
        xp.src_jid_size=strlen(src_jid)+1;



        xp.dst_jid_size=strlen(dst_jid)+1;
        xp.passwd_size=strlen(my_passwd)+1;
        xp.msg_size=strlen(tst_msg)+1;
        // Pack the XMPP data structure into a byte sequence for sending
        p2p_pkt.payload_len = xmpp_pkt_pack (&xp, p2p_pkt.payload, 0);

        // Make sure it isn't too long!
        if (p2p_pkt.payload_len == 0) {
            nrk_kprintf (PSTR ("XMPP packet too long!\r\n"));
            nrk_wait_until_next_period ();
            continue;
        }
        cnt++;

        // Lets print out what we are sending
        printf ("Msg to: %s\r\n", xp.src_jid);
        printf ("  body: %s\r\n", xp.msg);

        nrk_led_set (BLUE_LED);

        // Make sure bmac checkrate is correct
        check_period.secs = 0;
        check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
        val = bmac_set_rx_check_rate (check_period);

        // Pack data structure values in buffer before transmit
        pack_peer_2_peer_packet (&p2p_pkt);
        // For blocking transmits, use the following function call.
        val = bmac_tx_pkt (p2p_pkt.buf, p2p_pkt.buf_len);


        nrk_led_clr (BLUE_LED);
        nrk_led_clr (GREEN_LED);

        nrk_wait_until_next_period ();
    }

}
Example #19
0
void tx_task ()
{
  uint8_t j, i, cnt, error;
  int8_t len;
  int8_t rssi, val;
  uint8_t *local_rx_buf;

  nrk_sig_t tx_done_signal;
  nrk_sig_t rx_signal;
  nrk_sig_mask_t ret;
  nrk_time_t check_period;
  nrk_time_t timeout, start, current;
  nrk_sig_mask_t my_sigs;

  printf ("tx_task PID=%d\r\n", nrk_get_pid ());

  // Wait until the tx_task starts up bmac
  // This should be called by all tasks using bmac that
  // do not call bmac_init()...
  bmac_init (26);


  // Configure address for other packet handlers (my not be needed)
  my_mac= MY_MAC;
  my_subnet_mac[0]= MY_SUBNET_MAC_0;
  my_subnet_mac[1]= MY_SUBNET_MAC_1;
  my_subnet_mac[2]= MY_SUBNET_MAC_2;
  mac_address= (uint8_t)MY_SUBNET_MAC_2 << 24 | (uint8_t)MY_SUBNET_MAC_1 <<16 | (uint8_t)MY_SUBNET_MAC_0 << 8 | (uint8_t)MY_MAC; 

  while (!bmac_started ())
    nrk_wait_until_next_period ();
  bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);

  val =
    bmac_addr_decode_set_my_mac (((uint16_t) MY_SUBNET_MAC_0 << 8) | MY_MAC);
  val = bmac_addr_decode_dest_mac (0xffff);     // broadcast by default
  bmac_addr_decode_enable ();

  nrk_kprintf (PSTR ("bmac_started()\r\n"));
  bmac_set_cca_thresh (-45);


  check_period.secs = 0;
  check_period.nano_secs = 100 * NANOS_PER_MS;
  val = bmac_set_rx_check_rate (check_period);

  // Get and register the tx_done_signal if you want to
  // do non-blocking transmits
  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);

  rx_signal = bmac_get_rx_pkt_signal ();
  nrk_signal_register (rx_signal);

  cnt = 0;

  while (1) {


    // Build a TX packet by hand...
    p2p_pkt.pkt_type = DATA_STORAGE_PKT;
    p2p_pkt.ctrl_flags = LINK_ACK | MOBILE_MASK;   // | DEBUG_FLAG ;  
    p2p_pkt.ack_retry = 0x0f;
    p2p_pkt.ttl = 1;
    p2p_pkt.src_subnet_mac[0] = MY_SUBNET_MAC_0;
    p2p_pkt.src_subnet_mac[1] = MY_SUBNET_MAC_1;
    p2p_pkt.src_subnet_mac[2] = MY_SUBNET_MAC_2;
    p2p_pkt.src_mac = MY_MAC;
    p2p_pkt.last_hop_mac = MY_MAC;
//    p2p_pkt.dst_subnet_mac[0] = BROADCAST;
//    p2p_pkt.dst_subnet_mac[1] = BROADCAST;
//    p2p_pkt.dst_subnet_mac[2] = BROADCAST;
//    p2p_pkt.dst_mac = BROADCAST;
    p2p_pkt.dst_subnet_mac[0] = BROADCAST;
    p2p_pkt.dst_subnet_mac[1] = BROADCAST;
    p2p_pkt.dst_subnet_mac[2] = BROADCAST;
    p2p_pkt.dst_mac = 0x1;
    p2p_pkt.buf = tx_buf;
    p2p_pkt.buf_len = P2P_PAYLOAD_START;
    p2p_pkt.seq_num = cnt;
    p2p_pkt.priority = 0;
    p2p_pkt.check_rate = 100;
    p2p_pkt.payload = &(tx_buf[P2P_PAYLOAD_START]);

    cnt++;

#ifdef TEST_WRITE
   data_pkt.mode=EE_WRITE;
   data_pkt.addr=0x110;  // Must be greater than 0x100
   data_pkt.data_len=0x10;
   // point the eeprom data structure to our local data buffer
   data_pkt.eeprom_payload=eeprom_data;
   // copy over some data
   for(i=0; i<data_pkt.data_len; i++ )
	data_pkt.eeprom_payload[i]=i;
#endif

#ifdef TEST_READ
   data_pkt.mode=EE_READ;
   data_pkt.addr=0x200;  // Must be greater than 0x100
   data_pkt.data_len=0x3d;
#endif

   // add the eeprom pkt to the p2p pkt
   p2p_pkt.payload_len= eeprom_storage_pkt_pack(&data_pkt, p2p_pkt.payload);

   // Pack data structure values in buffer before transmit
   pack_peer_2_peer_packet (&p2p_pkt);
   

    nrk_led_set (BLUE_LED);

    check_period.secs = 0;
    check_period.nano_secs = 100 * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);

    nrk_kprintf( PSTR("sending: " ));
    for(i=0; i<p2p_pkt.buf_len; i++ )
	printf( "%u ",p2p_pkt.buf[i] );
    printf( "\r\n" );
    // For blocking transmits, use the following function call.
    val = bmac_tx_pkt (p2p_pkt.buf, p2p_pkt.buf_len);

    check_period.secs = 0;
    check_period.nano_secs = FAST_CHECK_RATE * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);
#ifdef TXT_DEBUG
    nrk_kprintf (PSTR ("\r\nSent Request:\r\n"));
#endif
    nrk_led_clr (BLUE_LED);
    nrk_led_clr (GREEN_LED);

    // Wait for packets or timeout
    nrk_time_get (&start);
    while (1) {

      timeout.secs = REPLY_WAIT_SECS;
      timeout.nano_secs = 0;

      // Wait until an RX packet is received
      //val = bmac_wait_until_rx_pkt ();
      if(bmac_rx_pkt_ready()==0)
      {
        nrk_set_next_wakeup (timeout);
        my_sigs = nrk_event_wait (SIG (rx_signal) | SIG (nrk_wakeup_signal));
      }


      if (my_sigs == 0)
        nrk_kprintf (PSTR ("Error calling nrk_event_wait()\r\n"));
      if (my_sigs & SIG (rx_signal)) {

        // Get the RX packet 
        local_rx_buf = bmac_rx_pkt_get (&len, &rssi);
        // Check the packet type from raw buffer before unpacking
        if ((local_rx_buf[CTRL_FLAGS] & (DS_MASK | US_MASK)) == 0) {

          // Set the buffer
          p2p_pkt.buf = local_rx_buf;
          p2p_pkt.buf_len = len;
          p2p_pkt.rssi = rssi;
          unpack_peer_2_peer_packet (&p2p_pkt);
#ifdef TXT_DEBUG
            if ((p2p_pkt.dst_subnet_mac[2] == MY_SUBNET_MAC_2 &&
		p2p_pkt.dst_subnet_mac[1] == MY_SUBNET_MAC_1 &&
		p2p_pkt.dst_subnet_mac[0] == MY_SUBNET_MAC_0 &&
		p2p_pkt.dst_mac == MY_MAC ) 
		|| p2p_pkt.dst_mac == BROADCAST) 
	    {
	    nrk_led_set (GREEN_LED);
            // Packet arrived and is good to go
            printf ("full mac: %d %d %d %d ", p2p_pkt.src_subnet_mac[0],
                    p2p_pkt.src_subnet_mac[1], p2p_pkt.src_subnet_mac[2],
		    p2p_pkt.src_mac);
            printf ("rssi: %d ", p2p_pkt.rssi);
            printf ("type: %d ", p2p_pkt.pkt_type);
            nrk_kprintf (PSTR ("payload: ["));
            for (i = 0; i < p2p_pkt.payload_len; i++)
              printf ("%d ", p2p_pkt.payload[i]);
            nrk_kprintf (PSTR ("]\r\n"));
        
	if(p2p_pkt.pkt_type== DATA_STORAGE_PKT )
		{
		eeprom_storage_pkt_unpack(&data_pkt, p2p_pkt.payload );
		nrk_kprintf( PSTR("\r\n   Storage Packet " ));
		nrk_kprintf( PSTR("\r\n   Mode: " ));
		switch(data_pkt.mode)
		{
		case EE_REPLY: nrk_kprintf( PSTR( "Reply")); break;
		case EE_ERROR: nrk_kprintf( PSTR( "Error" )); break;
		case EE_READ: nrk_kprintf( PSTR( "Read" )); break;
		case EE_WRITE: nrk_kprintf( PSTR( "Write" )); break;
		default: nrk_kprintf( PSTR( "unknown" )); 

		}
		nrk_kprintf( PSTR("\r\n   From: " ));
		printf( "%u",data_pkt.mac );
		nrk_kprintf( PSTR("\r\n   Addr: " ));
		printf( "%u",data_pkt.addr);
		nrk_kprintf( PSTR("\r\n   Len: " ));
		printf( "%u",data_pkt.data_len);
		nrk_kprintf( PSTR("\r\n   Data: " ));
		for(i=0; i<data_pkt.data_len; i++ ) printf( "%u ",data_pkt.eeprom_payload[i]);
		nrk_kprintf( PSTR("\r\n" ));
		} 
	
	}
#endif

        }
      }

      nrk_time_get (&current);
      if (start.secs + REPLY_WAIT_SECS < current.secs)
        break;
        // Release the RX buffer so future packets can arrive 
        bmac_rx_pkt_release ();
    }
    nrk_kprintf (PSTR ("Done Waiting for response...\r\n"));
    nrk_wait_until_next_period ();
  }

}
Example #20
0
void tx_task () {
    uint8_t i;
    nrk_sig_t tx_done_signal;
    while (!bmac_started ())
        nrk_wait_until_next_period ();
    tx_done_signal = bmac_get_tx_done_signal ();
    nrk_signal_register (tx_done_signal);
    bmac_addr_decode_enable();
    bmac_addr_decode_set_my_mac(MyOwnAddress);
    while (1) {
        if(updateCnt==0) {
            updateCnt = updateCntMax;
            for(i=0; i<MaxuIDTrack; i++) {
                uniqueIDsRREQ[i]=0;
            }
            for(i=0; i<MaxuIDTrack; i++) {
                uniqueIDsRSAL[i]=0;
            }
            for(i=0; i<MaxuIDTrack; i++) {
                if(ackTrackR[i]==ackTrackS[i]) {
                    ackTrackR[i]=0;
                    ackTrackS[i]=0;
                    ackTrack[i]=0;
                }
            }
            for(i=0; i<MaxuIDTrack; i++) {
                if(ackTrack[i]!=0) {
                    if(cache[1]==ackTrack[i]) {
                        cache[0]=0;
                    }
                    ackTrackS[i]=0;
                    ackTrackR[i]=0;
                    RsalInitiate(ackTrack[i]);
                    ackTrack[i]=0;
                }
            }
            for(i=0; i<MaxuIDTrack; i++) {
                ackTrack[i]=ackTrackS[i];
            }
        }
        else {
            updateCnt--;
        }
        if(cache[0]==0) {
            if(rDiscCnt==0) {
                RouteDiscovery();
                rDiscCnt=rDiscCntMax;
            }
            else {
                rDiscCnt--;
            }
        }
        else {
            DataInitiate();
            rDiscCnt=0;
        }
        if(pQue>0) {
            if(txPtr>queMax-1) {
                txPtr=0;
            }
            bmac_addr_decode_enable();
            bmac_addr_decode_set_my_mac(MyOwnAddress);
            nrk_led_set(GREEN_LED);
            bmac_auto_ack_disable();
            for(i=0; i<pLen[txPtr]; i++) {
                tx_buf[i]=pDat[txPtr][i];
            }
            if(pDes[txPtr]==0xFF) {
                bmac_addr_decode_dest_mac(0xFFFF);
            }
            else {
                bmac_addr_decode_dest_mac(pDes[txPtr]);
            }
            bmac_tx_pkt((char*)tx_buf,pLen[txPtr]);
            for(i=0; i<pLen[txPtr]; i++) {
                putchar(tx_buf[i]);
            }
            putchar(pDes[txPtr]);
            pQue--;
            txPtr++;
            nrk_led_clr(GREEN_LED);
        }
        nrk_wait_until_next_period ();
    }
}
Example #21
0
void whacky_task ()
{
  uint8_t i, len, fd;
  int8_t rssi, val;
  uint8_t *local_buf;
  uint16_t light, node_id, got_poll;
  uint8_t pos;
 
  printf ("whacky_task PID=%d\r\n", nrk_get_pid ());
  
  // Open ADC device as read 
  fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ);
  if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));
  
    printf(PSTR("Sensor opened\r\n"));
  // init bmac on channel 15 
  bmac_init (15);
    printf(PSTR("bmac init\r\n"));
	
  srand(1);

  // This sets the next RX buffer.
  // This can be called at anytime before releasing the packet
  // if you wish to do a zero-copy buffer switch
  bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);
    printf(PSTR("RX buffer set\r\n"));
  while (1) 
  {
    node_id = 0;
    got_poll = 0;

    printf(PSTR("Waiting for a Packet\r\n"));
    // Get the RX packet 
    nrk_led_set (ORANGE_LED);
    // Wait until an RX packet is received
    if(!bmac_rx_pkt_ready())
    {
   	 val = bmac_wait_until_rx_pkt ();
    }
    local_buf = bmac_rx_pkt_get (&len, &rssi);
    printf ("Got RX packet len=%d RSSI=%d [%s]\r\n", len, rssi, local_buf);
    // Check for a poll packet
    if(len>5 && local_buf[0] == 'P' && local_buf[1] == 'O' && local_buf[2] == 'L' &&
		local_buf[3] == 'L' && local_buf[4] == ':')
    {
	// Assume that there is a space after POLL
	pos = 6;
	while(pos < len && local_buf[pos] != '\0' && local_buf[pos] >='0' && local_buf[pos]<='9') 
	{
		node_id *= 10;
		node_id += (local_buf[pos]-'0');
		pos++;
	}
	if(pos > 6)
	{
		got_poll = 1;
	}
    }
    nrk_led_clr (ORANGE_LED);
    // Release the RX buffer so future packets can arrive 
    bmac_rx_pkt_release ();
    
    if(got_poll == 1 && node_id == MAC_ADDR)
    {    
	    val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
	    val=nrk_read(fd,&light,2);
    
	    sprintf (tx_buf, "Node %d Status %u", MAC_ADDR, light);
	    nrk_led_set (BLUE_LED);
	    val=bmac_tx_pkt(tx_buf, strlen(tx_buf)+1);
	    if(val != NRK_OK)	
	    {
		nrk_kprintf(PSTR("Could not Transmit!\r\n"));
	    }

	    // Task gets control again after TX complete
	    nrk_kprintf (PSTR ("Tx task sent data!\r\n"));
	    printf("%s\r\n", tx_buf);
	    nrk_led_clr (BLUE_LED);
   }
  }
}
Example #22
0
// Run update Mode Protocol
void updateMode()
{
  // Set destination
  val=bmac_addr_decode_set_my_mac(((uint16_t)my_subnet_mac<<8)|my_mac);
  val=bmac_addr_decode_dest_mac(((uint16_t)dest_mac[1]<<8)|dest_mac[0]);
  bmac_addr_decode_enable();
  
  while(programState == UPDATE)
  {
    // Build tx packet by txState
    buildTxPkt();
    
    nrk_led_toggle (BLUE_LED);

    check_period.secs = 0;
    check_period.nano_secs = 100 * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);

    // Send with hardware acknowledgement.
    while(1)
    {
      val = bmac_tx_pkt (tx_buf, len);
      // Break if message ack recieved or is a broadcast
      if(val == NRK_OK){
        nrk_kprintf(PSTR("Packet Sent\r\n"));
        break;
      }
      else{
        nrk_kprintf(PSTR("Packet ReSent\r\n"));
      }
      // Resend if ack not recieved
      nrk_wait_until_next_period();
    }

  #ifdef TXT_DEBUG
      nrk_kprintf (PSTR ("\r\nSent Request:\r\n"));
  #endif
    nrk_led_clr (BLUE_LED);
    nrk_led_clr(GREEN_LED);

      // Wait if reply expected / Change TX state if not
      //***********************************************************
    if(needReply == FALSE)
    {
        // Change TX state, update page number
      changeState();
    }
    else
    {
        // Wait for packets or timeout
  #ifdef TXT_DEBUG
        nrk_kprintf (PSTR ("\r\nExpecting Reply\r\n"));
  #endif
      timeout.secs = REPLY_WAIT_SECS;
      timeout.nano_secs = REPLY_WAIT_NANOSECS;

      nrk_set_next_wakeup (timeout);
      my_sigs = nrk_event_wait (SIG (rx_signal) | SIG (nrk_wakeup_signal));

      if (my_sigs == 0)
        nrk_kprintf (PSTR ("Error calling nrk_event_wait()\r\n"));
      if (my_sigs & SIG (rx_signal))
      {
            // Get the RX packet
        local_rx_buf = bmac_rx_pkt_get (&len, &rssi);
        nrk_led_clr (ORANGE_LED);
  #ifdef TXT_DEBUG
        nrk_kprintf (PSTR ("Recieved Reply\r\n"));
  #endif
        // Handle recieved packet
        updateReplyRecieved();
      }
      else
      {
  #ifdef TXT_DEBUG
        nrk_kprintf (PSTR ("Timed Out Waiting for response...\r\n"));
  #endif
      }
    }
    nrk_wait_until_next_period();
  }
}
Example #23
0
void uart_task()
{
	char c;
	nrk_sig_t uart_rx_signal;
	//nrk_sig_mask_t sm;
	uint8_t buf_pos = 0;
	uint8_t vbuf_pos = 0;
	uint8_t pos = 0;
	uint8_t other_active = 1;  
	uint8_t version_buf[VERSION_BUF_SIZE];
	int16_t my_version = 0;
	uint8_t connection_l;  
	uint8_t activate_uart_l;  
	uint8_t ack_received_l;

  if(log_g) printf("log:uart_task PID=%d\r\n",nrk_get_pid());

  // Get the signal for UART RX  
  uart_rx_signal=nrk_uart_rx_signal_get();
  // Register your task to wakeup on RX Data 
  if(uart_rx_signal==NRK_ERROR) nrk_kprintf( PSTR("log:Get Signal ERROR!\r\n") );
  nrk_signal_register(uart_rx_signal);


  while(1) {

	nrk_sem_pend(conn_sem);
	connection_l = connection_g;  
	nrk_sem_post(conn_sem);
	nrk_sem_pend(ack_sem);
	ack_received_l = ack_received_g;
	nrk_sem_post(ack_sem);


	if(activate_uart_g == 1 && other_active == 1)
	{
		memset(uart_rx_buf,0,buf_pos); 
		buf_pos = 0;
		if(log_g) printf("log:Switching off logs\r\n");
		req_for_next_data();
		log_g = 0;
		other_active = 0;
	}
	else if(activate_uart_g == 0 && vertsk_active_g == 0)
	{
		nrk_wait_until_next_period();	
		continue;
	} else if(activate_uart_g == 0 && connection_l == 0 && vertsk_active_g == 1) { // assuming this completes in one period
			
		if(log_g)nrk_kprintf(PSTR("verreq\n"));
		log_g = 0;
		while(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0)
		{
			// Read Character
			c=getchar();
			if(vbuf_pos >= VERSION_BUF_SIZE)
 			{
				vbuf_pos = 0; 
				nrk_kprintf(PSTR("Ver buf ovflw\n"));
			}
			version_buf[vbuf_pos++] = c; 	
			nrk_led_toggle(GREEN_LED);
			if(c =='&')
			{
				log_g = 1;
				version_buf[vbuf_pos++] = '\0';
				pos = 0; 
				my_version = get_next_int(version_buf,&pos,vbuf_pos);
				if(log_g) printf("log:Ver %d\n",my_version);
				version_g[MAC_ADDR] = my_version;
				if(vbuf_pos > 6) 
				{	
					if(log_g)nrk_kprintf(PSTR("Cnct Frnd\n"));
					pos++;
					bmac_tx_pkt(version_buf+pos,vbuf_pos-pos);
 				}
				vertsk_active_g = 0;
				vbuf_pos = 0; 
			} else if (c == '$') {
				vbuf_pos = 0; 
			}
		}		
		log_g = 1;
		nrk_wait_until_next_period();
		continue; 
	}
	
	nrk_sem_pend(conn_sem);
	connection_l = connection_g;  
	nrk_sem_post(conn_sem);
	nrk_sem_pend(ack_sem);
	ack_received_l = ack_received_g;
	nrk_sem_post(ack_sem);



	if(ack_received_l == 1 && connection_l == 1)  
	{
	
		while(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0)
		{
			// Read Character
			c=getchar();
			uart_rx_buf[buf_pos++] = c; 	
			nrk_led_toggle(GREEN_LED);
			if(c =='$')
			{
				log_g = 1;
				uart_rx_buf[buf_pos++] = '\0';
				if(log_g) nrk_kprintf(PSTR("log:From UART\n"));
				if(log_g) printf("log:%s\n",uart_rx_buf);
				activate_uart_g = 0;
				other_active = 1;
				// tx a packet				
				bmac_tx_pkt(uart_rx_buf,buf_pos);
				nrk_sem_pend(ack_sem);
				ack_received_g = 0; 
				nrk_sem_post(ack_sem);
				ack_received_l = 0;
				pending_retransmit_g = 1;
			} else if (c == '&') {
				buf_pos = 0; 		
			}
		}
		log_g = 1;
	}
	//sm=nrk_event_wait(SIG(uart_rx_signal));
	//if(sm != SIG(uart_rx_signal))
	//	nrk_kprintf( PSTR("RX signal error\n") );
	nrk_wait_until_next_period();	
  }

}
Example #24
0
void tx_task ()
{
  uint8_t j, i, val, len, cnt;
  int8_t v;
  nrk_sig_t tx_done_signal;
  nrk_sig_mask_t ret;
  nrk_time_t r_period;
	
	// printf("tx_task PID=%d\r\n", nrk_get_pid ());

  // Wait until the tx_task starts up bmac
  // This should be called by all tasks using bmac that
  // do not call bmac_init()...
  while (!bmac_started ())
    nrk_wait_until_next_period ();


  // Sample of using Reservations on TX packets
  // This example allows 2 packets to be sent every 5 seconds
  // r_period.secs=5;
  // r_period.nano_secs=0;
  // v=bmac_tx_reserve_set( &r_period, 2 );
  // if(v==NRK_ERROR) nrk_kprintf( PSTR("Error setting b-mac tx reservation (is NRK_MAX_RESERVES defined?)\r\n" ));


  // Get and register the tx_done_signal if you want to
  // do non-blocking transmits
  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);

  ctr_cnt[0]=0; ctr_cnt[1]=0; ctr_cnt[2]=0; ctr_cnt[3]=0;
  cnt = 0;
	
  while (1) {
    // Build a TX packet
    sprintf (tx_buf, "This is a test %d", cnt);
    nrk_led_set (BLUE_LED);
		
    // Auto ACK is an energy efficient link layer ACK on packets
    // If Auto ACK is enabled, then bmac_tx_pkt() will return failure
    // if no ACK was received. In a broadcast domain, the ACK's will
    // typically collide.  To avoid this, one can use address decoding. 
    // The functions are as follows:
    // bmac_auto_ack_enable();
			 bmac_auto_ack_disable();

    // Address decoding is a way of preventing the radio from receiving
    // packets that are not address to a particular node.  This will 
    // supress ACK packets from nodes that should not automatically ACK.
    // The functions are as follows:
    // bmac_addr_decode_set_my_mac(uint16_t MAC_ADDR); 
    // bmac_addr_decode_dest_mac(uint16_t DST_ADDR);  // 0xFFFF is broadcast
    // bmac_addr_decode_enable();
    // bmac_addr_decode_disable();
/*
     ctr_cnt[0]=cnt; 
     if(ctr_cnt[0]==255) ctr_cnt[1]++; 
     if(ctr_cnt[1]==255) ctr_cnt[2]++; 
     if(ctr_cnt[2]==255) ctr_cnt[3]++; 
     // You need to increase the ctr on each packet to make the 
     // stream cipher not repeat.
     bmac_encryption_set_ctr_counter(&ctr_cnt,4);

*/  // For blocking transmits, use the following function call.
    // For this there is no need to register  
     val=bmac_tx_pkt(tx_buf, strlen(tx_buf));
		 if(val==NRK_OK) cnt++;
 		 else printf("NO ack or Reserve Violated! \r\n");

    // This function shows how to transmit packets in a
    // non-blocking manner  
    // val = bmac_tx_pkt_nonblocking(tx_buf, strlen (tx_buf));
    // printf ("Tx packet enqueued\r\n");
    // This functions waits on the tx_done_signal
    //ret = nrk_event_wait (SIG(tx_done_signal));

    // Just check to be sure signal is okay
    //if(ret & SIG(tx_done_signal) == 0 ) 
    //printf ("TX done signal error\r\n");
   
    // If you want to see your remaining reservation
    // printf( "reserve=%d ",bmac_tx_reserve_get() );
    
    // Task gets control again after TX complete
    printf("Tx task sent data!\r\n");
    nrk_led_clr (BLUE_LED);
		printf("tx_task PID=%d\r\n", nrk_get_pid ());
    nrk_wait_until_next_period ();
  }

}
Example #25
0
void tx_task ()
{
  uint8_t i, unique;
  uint8_t samples ;
  uint8_t len;
  int8_t rssi, val;
  uint8_t *local_rx_buf;

  nrk_sig_t tx_done_signal;
  nrk_sig_t rx_signal;
  nrk_time_t check_period;
  nrk_time_t timeout, start, current;
  nrk_sig_mask_t my_sigs;

  printf ("tx_task PID=%d\r\n", nrk_get_pid ());


  // Wait until the tx_task starts up bmac
  // This should be called by all tasks using bmac that
  // do not call bmac_init()...
  bmac_init (26);

  bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);

  val =
    bmac_addr_decode_set_my_mac (((uint16_t) MY_SUBNET_MAC_0 << 8) | MY_MAC );
  val = bmac_addr_decode_dest_mac (0xffff);     // broadcast by default
  bmac_addr_decode_enable ();

  nrk_kprintf (PSTR ("bmac_started()\r\n"));
  bmac_set_cca_thresh (-45);


  check_period.secs = 0;
  check_period.nano_secs = 100 * NANOS_PER_MS;
  val = bmac_set_rx_check_rate (check_period);

  // Get and register the tx_done_signal if you want to
  // do non-blocking transmits
  tx_done_signal = bmac_get_tx_done_signal ();
  nrk_signal_register (tx_done_signal);

  rx_signal = bmac_get_rx_pkt_signal ();
  nrk_signal_register (rx_signal);

  cnt = 0;

  check_period.secs = 0;
  check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
  val = bmac_set_rx_check_rate (check_period);


  // Main loop that does:
  //   1) Sends out ping message
  //   2) Collects replies, build neighbor list and then times out
  //   3) Repeat 1 and 2 for 3 times
  //   4) Build Extended Neighborlist packet
  //   5) Send Neighbor list packet
  //   6) Wait until next period and repeat 1-6
  while (1) {

    nrk_led_clr (ORANGE_LED);

    // Set our local neighbor list to be empty
    my_nlist_elements = 0;

    for (samples = 0; samples < 3; samples++) {
      nrk_led_set (GREEN_LED);
      check_period.secs = 0;
      check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
      val = bmac_set_rx_check_rate (check_period);

      // Construct a ping packet to send (this is being built into tx_buf)
      build_ping_pkt (&p2p_pkt);

      // Pack data structure values in buffer before transmit
      pack_peer_2_peer_packet (&p2p_pkt);

      // Send the Ping packet 
      val = bmac_tx_pkt (p2p_pkt.buf, p2p_pkt.buf_len);

      // Set update rate based on p2p reply rate.
      // This is usually faster to limit congestion
      check_period.secs = 0;
      check_period.nano_secs = p2p_pkt.check_rate * NANOS_PER_MS;
      val = bmac_set_rx_check_rate (check_period);


#ifdef TXT_DEBUG
      nrk_kprintf (PSTR ("Pinging...\r\n"));
#endif
      nrk_led_clr (GREEN_LED);



      // Grab start time for timeout 
      nrk_time_get (&start);

      while (1) {

	// Set the amount of time to wait for timeout
        timeout.secs = REPLY_WAIT_SECS;
        timeout.nano_secs = 0;

	// Check if packet is already ready, or wait until one arrives
	// Also set timeout to break from function if no packets come
	my_sigs=0;
        if (bmac_rx_pkt_ready () == 0) {
          nrk_set_next_wakeup (timeout);
          my_sigs =
            nrk_event_wait (SIG (rx_signal) | SIG (nrk_wakeup_signal));
        }



        if (my_sigs == 0)
          nrk_kprintf (PSTR ("Error calling nrk_event_wait()\r\n"));
        if (my_sigs & SIG (rx_signal)) {

          // Get the RX packet 
          local_rx_buf = bmac_rx_pkt_get (&len, &rssi);
          // Check the packet type from raw buffer before unpacking
          if ((local_rx_buf[CTRL_FLAGS] & (DS_MASK | US_MASK)) == 0) {

            // Setup a p2p packet data structure with the newly received buffer 
            p2p_pkt.buf = local_rx_buf;
            p2p_pkt.buf_len = len;
            p2p_pkt.rssi = rssi;

            // Unpack the data from the array into the p2p_pkt data struct
            unpack_peer_2_peer_packet (&p2p_pkt);


	    // Check if newly received packet is for this node
          if (((p2p_pkt.dst_subnet_mac[2] == MY_SUBNET_MAC_2 &&
		p2p_pkt.dst_subnet_mac[1] == MY_SUBNET_MAC_1 &&
		p2p_pkt.dst_subnet_mac[0] == MY_SUBNET_MAC_0 &&
		p2p_pkt.dst_mac == MY_MAC ) 
		|| p2p_pkt.dst_mac == BROADCAST) 
                && (p2p_pkt.pkt_type == PING_PKT)) {
              // Packet arrived and is  ping pkt!
	      // Lets print some values out on the terminal
            printf ("full mac: %d %d %d %d ", p2p_pkt.src_subnet_mac[0],
                    p2p_pkt.src_subnet_mac[1], p2p_pkt.src_subnet_mac[2],
		    p2p_pkt.src_mac);
              printf ("rssi: %d ", p2p_pkt.rssi);
              printf ("type: %d ", p2p_pkt.pkt_type);
              nrk_kprintf (PSTR ("payload: ["));
              for (i = 0; i < p2p_pkt.payload_len; i++)
                printf ("%d ", p2p_pkt.payload[i]);
              nrk_kprintf (PSTR ("]\r\n"));

              unique = 1;
              // Check if the MAC of this ping is unique or if it already
              // exists in our neighbor list
              for (i = 0; i < my_nlist_elements; i++) {

                if (my_nlist[i * NLIST_SIZE] == p2p_pkt.src_subnet_mac[2] &&
                    my_nlist[i * NLIST_SIZE + 1] == p2p_pkt.src_subnet_mac[1] &&
                    my_nlist[i * NLIST_SIZE + 2] == p2p_pkt.src_subnet_mac[0] &&
                    my_nlist[i * NLIST_SIZE + 3] == p2p_pkt.src_mac) {
                  unique = 0;
                  break;
                }
              }

              // If MAC is unique, add it to our neighbor list
              if (unique) {
                my_nlist[my_nlist_elements * NLIST_SIZE] =
                  p2p_pkt.src_subnet_mac[2];
                my_nlist[my_nlist_elements * NLIST_SIZE + 1] =
                  p2p_pkt.src_subnet_mac[1];
                my_nlist[my_nlist_elements * NLIST_SIZE + 2] =
                  p2p_pkt.src_subnet_mac[0];
                my_nlist[my_nlist_elements * NLIST_SIZE + 3] =
                  p2p_pkt.src_mac;
                my_nlist[my_nlist_elements * NLIST_SIZE + 4] = p2p_pkt.rssi;
                my_nlist_elements++;
              }
            }

          }


        }

	// Check if we are done waiting for pings
        nrk_time_get (&current);
        if (start.secs + REPLY_WAIT_SECS < current.secs)
          break; // exit loops waiting for pings
	
          // Release the RX buffer so future packets can arrive 
          bmac_rx_pkt_release ();

      // Go back to top loop to wait for more pings
      }
      cnt++;
    // Repeat ping 3 times
    }

    // Now we are ready to build extended neighborlist packet and send it to gateway
    check_period.secs = 0;
    check_period.nano_secs = DEFAULT_CHECK_RATE * NANOS_PER_MS;
    val = bmac_set_rx_check_rate (check_period);

    nrk_kprintf (PSTR ("Done Waiting for response...\r\n"));

    // If we have any neighbors, build the list
    if (my_nlist_elements > 0) {
      // Look in this function for format of extended neighborlist packet
      // This function also configures the parameters and destination address
      // of the p2p packet.  The values are probably okay as defaults.
      build_extended_neighbor_list_pkt (&p2p_pkt, my_nlist,
                                        my_nlist_elements);
      // This function takes at p2p struct and packs it into an array for sending
      pack_peer_2_peer_packet (&p2p_pkt);

      nrk_led_set (BLUE_LED);
      // Send the list to the gateway.
      val = bmac_tx_pkt (p2p_pkt.buf, p2p_pkt.buf_len);
      printf ("size of pkt: %d\r\n", p2p_pkt.buf_len);
      nrk_kprintf (PSTR ("sent neighbor list packet\r\n"));
      nrk_led_clr (BLUE_LED);
    }
    else {
      nrk_led_set (RED_LED);
      nrk_spin_wait_us (1000);
      nrk_led_clr (RED_LED);
    }

    // Wait a long time until we send out the pings again
    // This is in a loop so that period can be small for 
    // other uses.
    for (i = 0; i < 10; i++)
      nrk_wait_until_next_period ();

    // Might as well release packets that arrived during long
    // break since they are not replies to your ping.
    bmac_rx_pkt_release ();

  }

}