예제 #1
0
파일: main.c 프로젝트: ESE519/ISA100.11a
void Task1()
{
uint16_t cnt;
int8_t i,fd,val;
uint16_t buf;

  printf( "My node's address is %d\r\n",NODE_ADDR );

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

  
  cnt=0;
  while(1) {
  	// Open ADC device as read 
  	fd=nrk_open(FIREFLY_SENSOR_BASIC,READ);
  	if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));
	nrk_led_toggle(BLUE_LED);

	// Example of setting a sensor 
	val=nrk_set_status(fd,SENSOR_SELECT,BAT);
	// Read battery first while other sensors warm up
	val=nrk_read(fd,&buf,2);
	printf( "Task bat=%d",buf);
		val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
	val=nrk_read(fd,&buf,2);
	printf( " light=%d",buf);
	val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
	val=nrk_read(fd,&buf,2);
	printf( " temp=%d",buf);
	val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
	val=nrk_read(fd,&buf,2);
	printf( " acc_x=%d",buf);
	val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
	val=nrk_read(fd,&buf,2);
	printf( " acc_y=%d",buf);
	val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
	val=nrk_read(fd,&buf,2);
	printf( " acc_z=%d",buf);
  	val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
	nrk_spin_wait_us(60000);
	val=nrk_read(fd,&buf,2);
	printf( " audio=%d\r\n",buf);
	nrk_close(fd);
	nrk_wait_until_next_period();
	cnt++;
	}
}
예제 #2
0
파일: Serial.c 프로젝트: ESE519/ISA100.11a
void send_ACK()
{
	int8_t ret;
	
	ntg_pkt.type = SERIAL_ACK;
	ntg_pkt.length = 0;
	pack_NodeToGatewaySerial_Packet_header(tx_buf, &ntg_pkt);
	//memcpy(tx_buf + SIZE_NODETOGATEWAYSERIAL_PACKET_HEADER, ntg_pkt.data, MAX_SERIAL_PAYLOAD);
	memcpy(tx_buf + SIZE_NODETOGATEWAYSERIAL_PACKET_HEADER, ntg_pkt.data, ntg_pkt.length);
	//ret = sendToSerial(tx_buf, SIZE_NODETOGATEWAYSERIAL_PACKET);
	ret = sendToSerial(tx_buf, SIZE_NODETOGATEWAYSERIAL_PACKET_HEADER + ntg_pkt.length);
	if(ret == NRK_OK)
		if(DEBUG_SR >= 1)
			nrk_kprintf(PSTR("SR: send_ACK(): Sent ACK to gateway\r\n"));
	
	return;
}
예제 #3
0
파일: main.c 프로젝트: adamselevan/dicio
void Task1()
{
uint16_t cnt;
int8_t val;
uint8_t chan;
uint8_t i;

// Example of using eeprom read functions for loading mac address and channel
val=read_eeprom_mac_address(&mac_address);
if(val==NRK_OK)
	{
	nrk_kprintf( PSTR("MAC = 0x"));
	printf( "%x",(uint8_t)((mac_address>>24)&0xff));
	printf( "%x",(uint8_t)((mac_address>>16)&0xff));
	printf( "%x",(uint8_t)((mac_address>>8)&0xff));
	printf( "%x\r\n",(uint8_t)((mac_address & 0xff)));
	}
예제 #4
0
파일: main.c 프로젝트: adamselevan/dicio
void Task1()
{
nrk_time_t t;
uint16_t cnt;
cnt=0;
nrk_kprintf( PSTR("Nano-RK Version ") );
printf( "%d\r\n",NRK_VERSION );


setup_uart1(UART_BAUDRATE_19K2);
DDRE=0x2;
printf( "My node's address is %u\r\n",NODE_ADDR );
  
printf( "Task1 PID=%u\r\n",nrk_get_pid());
t.secs=5;
t.nano_secs=0;

// setup a software watch dog timer
nrk_sw_wdt_init(0, &t, NULL);
nrk_sw_wdt_start(0);


	putc1('~'); putc1('A'); putc1(' '); putc1('8');
	nrk_wait_until_next_period();
	putc1('~'); putc1('A'); putc1(' '); putc1('8');
  while(1) {
	// Update watchdog timer
	nrk_sw_wdt_update(0);
	nrk_led_toggle(ORANGE_LED);
	nrk_gpio_toggle(NRK_DEBUG_0);
	printf( "Task1 cnt=%u\r\n",cnt );
	nrk_wait_until_next_period();
        // Uncomment this line to cause a stack overflow
	// if(cnt>20) kill_stack(10);

	// At time 50, the OS will halt and print statistics
	// This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
	// if(cnt==50)  {
	//	nrk_stats_display_all();
	//	nrk_halt();
	//	}


	cnt++;
	}
}
예제 #5
0
파일: main.c 프로젝트: vanjoe/eece494
void Task1()
{
  nrk_time_t t;
  uint16_t cnt;
  uint8_t val;
  cnt=0;
  nrk_kprintf( PSTR("Nano-RK Version ") );
  printf("b");
  printf( "%d\r\n",NRK_VERSION );
  
  printf( "My node's address is %u\r\n",NODE_ADDR );
  
  printf( "Task1 PID=%u\r\n",nrk_get_pid());
  t.secs=30;
  t.nano_secs=0;
  
  // setup a software watch dog timer
 nrk_sw_wdt_init(0, &t, NULL);
 nrk_sw_wdt_start(0);
 
 nrk_gpio_direction(BUTTON, NRK_PIN_INPUT);
 
 while(1) {
   // Update watchdog timer
   nrk_sw_wdt_update(0);
   nrk_led_toggle(ORANGE_LED);
   val=nrk_gpio_get(BUTTON);
   
   // Button logic is inverter 0 means pressed, 1 not pressed
   printf( "Task1 cnt=%u button state=%u\r\n",cnt,val );
   nrk_wait_until_next_period();
   // Uncomment this line to cause a stack overflow
   // if(cnt>20) kill_stack(10);
   
   // At time 50, the OS will halt and print statistics
   // This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
   if(cnt==50)  {
     nrk_stats_display_all();
     nrk_halt();
   }
   
   
   cnt++;
 }
}
예제 #6
0
파일: main.c 프로젝트: ESE519/ISA100.11a
int
main ()
{
  uint8_t t;
  
  nrk_setup_ports();
  nrk_setup_uart(UART_BAUDRATE_115K2);
  
  nrk_kprintf( PSTR("Starting up...\r\n") );

  nrk_init();

 
  nrk_time_set(0,0);
  nrk_create_taskset ();
  nrk_start();
  return 0;
}
예제 #7
0
파일: main.c 프로젝트: adamselevan/dicio
void nrk_register_drivers ()
{
  int8_t val;

// Register the Basic FireFly Sensor device driver
// Make sure to add: 
//     #define NRK_MAX_DRIVER_CNT  
//     in nrk_cfg.h
// Make sure to add: 
//     SRC += $(ROOT_DIR)/src/drivers/platform/$(PLATFORM_TYPE)/source/ff_basic_sensor.c
//     in makefile

// Might use this later for reading accelerometer etc
  val = nrk_register_driver (&dev_manager_ff_sensors, FIREFLY_SENSOR_BASIC);
  if (val == NRK_ERROR)
    nrk_kprintf (PSTR ("Failed to load my ADC driver\r\n"));

}
예제 #8
0
파일: main.c 프로젝트: ESE519/ISA100.11a
void Task2()
{
  uint8_t cnt;
  printf( "Task2 PID=%d\r\n",nrk_get_pid());
  cnt=0;
  while(1) {
	nrk_led_toggle(GREEN_LED);
	printf( "Task2 cnt=%d\r\n",cnt );
	nrk_wait_until_next_period();
	cnt++;
  	if(cnt==25) 
		{
		nrk_led_clr(ORANGE_LED);
		nrk_kprintf( PSTR("*** Timer stopped by Task2!\r\n" ));
		nrk_timer_int_stop(NRK_APP_TIMER_0);
		}
	}
}
예제 #9
0
파일: main.c 프로젝트: prashar/nanork
int main() {
	nrk_setup_ports();
	nrk_setup_uart(UART_BAUDRATE_115K2);
	nrk_kprintf(PSTR("\r\n Nano-RK Version \r\n "));
	printf("%d\r\n", NRK_VERSION );
	nrk_init();

	nrk_led_clr(ORANGE_LED);
	nrk_led_clr(BLUE_LED);
	nrk_led_clr(GREEN_LED);
	nrk_led_clr(RED_LED);

	nrk_time_set(0, 0);
	nrk_create_taskset();
	printf("NRK_START ::\r\n");
	nrk_start();

	return 0;
}
예제 #10
0
파일: main.c 프로젝트: Zubeen/ESE519
/*PRE-EXISTING STUFF DO NOT TOUCH*/
void nrk_create_taskset()
{
	TaskOne.task = Task1;
	TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE-1];
	TaskOne.Pbos = (void *) &Stack1[0];
	TaskOne.prio = 2;
	TaskOne.FirstActivation = TRUE;
	TaskOne.Type = BASIC_TASK;
	TaskOne.SchType = PREEMPTIVE;
	TaskOne.period.secs = 1;
	TaskOne.period.nano_secs = 0;
	TaskOne.cpu_reserve.secs = 0;
	TaskOne.cpu_reserve.nano_secs = 0;
	TaskOne.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
	TaskOne.offset.secs = 0;
	TaskOne.offset.nano_secs= 0;
	nrk_activate_task (&TaskOne);
	nrk_kprintf( PSTR("Create Done\r\n") );
}
예제 #11
0
파일: queue.c 프로젝트: mlab-upenn/ecgpatch
// This function checks for is full condition. The user of this function may
// not just use isFull before calling this. Just check the return type, you will know.
uint8_t write_packet(QueueType* queue, uint8_t* packet_ptr)
{
    uint8_t is_full = isFull(queue);
    // Dont do anything if the queue is full
    if(!is_full)
    {
        // copy the value at the pointer provided in the queue.
        memcpy(&(queue->packets[queue->write]), packet_ptr, sizeof(ECGPacket));
        if(FIFO_SIZE-1 == queue->write) {
            queue->write = 0;
        }
        else {
            queue->write ++;
        }        
    }else {
      nrk_kprintf(PSTR("ERROR: TX FIFO FULL\n\r"));
    }
    return !is_full;
}
예제 #12
0
파일: main.c 프로젝트: adamselevan/dicio
void Task1()
{
    nrk_kprintf( PSTR("Nano-RK Version ") );
    printf( "%d\r\n",NRK_VERSION );


    start_time.secs=0;
    start_time.nano_secs=0;
    end_time.secs=0;
    end_time.nano_secs=0;
    start_time_1.secs=0;
    start_time_1.nano_secs=0;
    end_time_1.secs=0;
    end_time_1.nano_secs=0;

    while (1) {

        // Measure time with code inbetween
        nrk_time_get(&start_time);
        nrk_time_get(&end_time);

        printf("####### NO WAIT BETWEEN READINGS #########\r\n");
        printf("start: %lu %lu\r\n",start_time.secs,start_time.nano_secs);
        printf("  end: %lu %lu\r\n",end_time.secs,end_time.nano_secs);
        printf("end.nano - start.nano: %lu\r\n",end_time.nano_secs-start_time.nano_secs);
        //printf("start.nano - end.nano: %lu\r\n",start_time.nano_secs-end_time.nano_secs);

        // Measure time with code inbetween
        nrk_time_get(&start_time_1);
        nrk_spin_wait_us(200);
        nrk_time_get(&end_time_1);


        printf("####### 200us BETWEEN READINGS #########\r\n");
        printf("start: %lu %lu\r\n",start_time_1.secs,start_time_1.nano_secs);
        printf("  end: %lu %lu\r\n",end_time_1.secs,end_time_1.nano_secs);
        printf("end.nano - start.nano: %lu\r\n",end_time_1.nano_secs-start_time_1.nano_secs);

        nrk_wait_until_next_period();
    }

}
예제 #13
0
inline void multihop(NW_Packet *pkt)
{
	if( shouldIMultihop(pkt) == MULTIHOP_YES )// see whether this packet should be multi hopped
	{
		enter_cr(bm_sem, 21);
		if( insert_tx_aq(pkt) == NRK_ERROR )
		{
			record_tx_queue_full(pkt);				// record this fact for statistics collection
		}			
		leave_cr(bm_sem, 21);

		if(DEBUG_NL == 2)
		{
			nrk_kprintf(PSTR("NL: multihop(): Inserted packet. "));
			print_tx_buffer();
			//print_pkt_header(pkt);
		} 
	} 
	return;
}
예제 #14
0
void create_network_layer_tasks()
{
  NL_RX_TASK.task = nl_rx_task;
  NL_RX_TASK.Ptos = (void *) &nl_rx_task_stack[NRK_APP_STACKSIZE - 1];
  NL_RX_TASK.Pbos = (void *) &nl_rx_task_stack[0];
  NL_RX_TASK.prio = 19;
  NL_RX_TASK.FirstActivation = TRUE;
  NL_RX_TASK.Type = BASIC_TASK;
  NL_RX_TASK.SchType = PREEMPTIVE;
  
  NL_RX_TASK.cpu_reserve.secs = 0;
  NL_RX_TASK.cpu_reserve.nano_secs = 700 * NANOS_PER_MS;  
  NL_RX_TASK.period.secs = 1;
  NL_RX_TASK.period.nano_secs = 0;
   
  
  NL_RX_TASK.offset.secs = 0;
  NL_RX_TASK.offset.nano_secs= 0;
  nrk_activate_task (&NL_RX_TASK);

/*****************************************************************************************/  
  NL_TX_TASK.task = nl_tx_task;
  NL_TX_TASK.Ptos = (void *) &nl_tx_task_stack[NRK_APP_STACKSIZE - 1];
  NL_TX_TASK.Pbos = (void *) &nl_tx_task_stack[0];
  NL_TX_TASK.prio = 18;
  NL_TX_TASK.FirstActivation = TRUE;
  NL_TX_TASK.Type = BASIC_TASK;
  NL_TX_TASK.SchType = PREEMPTIVE;
  
  NL_TX_TASK.cpu_reserve.secs = 0;
  NL_TX_TASK.cpu_reserve.nano_secs = 500 * NANOS_PER_MS;	
  NL_TX_TASK.period.secs = 1;
  NL_TX_TASK.period.nano_secs = 0;
  
  NL_TX_TASK.offset.secs = 0;
  NL_TX_TASK.offset.nano_secs= 0;
  nrk_activate_task (&NL_TX_TASK);

  if(DEBUG_NL == 2)
	nrk_kprintf(PSTR("create_network_layer_tasks(): Network layer task creation done\r\n"));
}
예제 #15
0
파일: main.c 프로젝트: ESE519/ISA100.11a
void task_rcv()
{
  uint8_t len;
  int8_t rssi;
  uint8_t *local_rx_buf;
  uint16_t count=1;
      
  // Wait until the rx_task starts up the protocol
  while (!wd_started ()) nrk_wait_until_next_period ();
	//printf("ok \r\n");
      
  // 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
  wd_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE);
    
	while(1) {
	        //printf("ok 1 \r\n");
		if (wd_wait_until_rx_packet() == NRK_OK) {
		  	nrk_led_toggle(BLUE_LED);
		  
      // Get the RX packet 
      local_rx_buf = wd_rx_pkt_get (&len, &rssi);
      //if (local_rx_buf!=NULL)	printf ("(%u) Rx Packet len=%u, prio=%u\r\n", count++, len, local_rx_buf[1]);
      nrk_kprintf (PSTR ("r"));
      
      
  		// do something with packet here ...
  		//	
      //for (i = 0; i < len; i++)
      //  printf ("%c", rx_buf[i]);
      //printf ("]\r\n");
  			wd_rx_pkt_release();
			
  	}
 	
		nrk_wait_until_next_period();
	}
}
예제 #16
0
파일: main.c 프로젝트: adamselevan/dicio
int
main ()
{
  uint8_t t;
  nrk_setup_ports();
  nrk_setup_uart(UART_BAUDRATE_115K2);
  
  nrk_kprintf( PSTR("Starting up...\r\n") );

  nrk_init();

  nrk_led_clr(ORANGE_LED);
  nrk_led_clr(BLUE_LED);
  nrk_led_clr(GREEN_LED);
  nrk_led_clr(RED_LED);
 
  nrk_time_set(0,0);
  nrk_create_taskset ();
  nrk_start();
  
  return 0;
}
예제 #17
0
void nrk_create_taskset()
{
  TASK.task = task;
  TASK.Ptos = (void *) &task_stack[NRK_APP_STACKSIZE - 1];
  TASK.Pbos = (void *) &task_stack[0];
  TASK.prio = 3;
  TASK.FirstActivation = TRUE;
  TASK.Type = BASIC_TASK;
  TASK.SchType = PREEMPTIVE;
  
  TASK.cpu_reserve.secs = 0;
  TASK.cpu_reserve.nano_secs = 800 * NANOS_PER_MS;	
  TASK.period.secs = 1;
  TASK.period.nano_secs = 0 * NANOS_PER_MS;
  
  TASK.offset.secs = 0;
  TASK.offset.nano_secs= 0;
  nrk_activate_task (&TASK);
  
  if(DEBUG_APP == 2)
  	nrk_kprintf(PSTR("Created the two application layer tasks\r\n"));
}
예제 #18
0
파일: Serial.c 프로젝트: ESE519/ISA100.11a
/************************* Function definitions ***************************/
int8_t sendToSerial(uint8_t *buf, uint8_t len)		// CHECKED
{
	while( slip_started () == NRK_ERROR )			// wait till the serial task starts the SLIPstream module
		nrk_wait_until_next_period();
		
	/*
	if(len > SIZE_SLIP_TX_BUF)
	{
		nrk_kprintf(PSTR("SR: sendToSerial(): Buffer length too big\r\n"));
		return NRK_ERROR;
	}
	*/
	
	if( slip_tx (buf, len) == NRK_ERROR )
	{
		nrk_int_disable();
		nrk_led_set(RED_LED);
		while(1)
			nrk_kprintf(PSTR("SR: sendToSerial(): Error sending out message over serial port\r\n"));
	}
	return NRK_OK;
}
예제 #19
0
파일: main.c 프로젝트: Zubeen/ESE519
int main ()
{
	nrk_setup_ports();
	nrk_setup_uart(UART_BAUDRATE_115K2);
	nrk_kprintf( PSTR("Starting up...\r\n") );
	nrk_init();

	nrk_led_clr(0);
	nrk_led_clr(1);
	nrk_led_clr(2);
	nrk_led_clr(3);
  
	nrk_time_set(0,0);

	nrk_register_drivers();
	rtl_task_config();
	nrk_create_taskset ();

	nrk_start();
	
	return 0;
}
예제 #20
0
파일: main.c 프로젝트: jnwu/nanork
void Task1()
{
nrk_time_t t;
uint8_t val;

nrk_kprintf( PSTR("Nano-RK Version ") );
printf( "%d\r\n",NRK_VERSION );

nrk_gpio_direction(NRK_BUTTON, NRK_PIN_INPUT);

  while(1) {
	// Update watchdog timer
	nrk_led_toggle(GREEN_LED);

	val=nrk_gpio_get(NRK_BUTTON);
	// Button logic is inverter 0 means pressed, 1 not pressed
	if(val==0) nrk_led_set(BLUE_LED);
	else nrk_led_clr(BLUE_LED);
	printf( "button state=%u\r\n",val );
	nrk_wait_until_next_period();

	}
}
예제 #21
0
void 
collect_Audio_Data(){
printf( "Task PID=%u\r\n",nrk_get_pid());

int8_t fd, val, index;
uint16_t buf;


// 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"));

	while(1)
	{
		val=nrk_set_status(fd,SENSOR_SELECT,AUDIO);
    	//8 KHZ sensor
		nrk_spin_wait_us(125);
		val=nrk_read(fd,&buf,2);

    	// printf( " audio=%d\r\n", buf);

		nrk_led_clr(BLUE_LED);
		nrk_led_clr(RED_LED);
		nrk_led_toggle(RED_LED);

		audio_data[index] = buf;
		index++;

		if(index == audio_data_size)
		{
			index =0;
			calculate_rms(audio_data, audio_data_size);
			// nrk_wait_until_next_period();
		}
	}
}
예제 #22
0
int main ()
{
    nrk_setup_ports();
    nrk_setup_uart(UART_BAUDRATE_115K2);

    printf( "Starting up...\r\n" );

    nrk_init();

    nrk_time_set(0,0);

    //Initialize tasks
    INITIALIZE_TASK(1);
    INITIALIZE_TASK(2);
    INITIALIZE_TASK(3);

    //instead of passing the ceiling priority, the task with the shortest period that accesses the semaphore is given
    //in this case, task1 which has a period 350*NANOS_PER_MS
    my_semaphore = nrk_sem_create(1,350*NANOS_PER_MS);
    if(my_semaphore==NULL) nrk_kprintf( PSTR("Error creating sem\r\n" ));
    nrk_start();

    return 0;
}
예제 #23
0
파일: Serial.c 프로젝트: ESE519/ISA100.11a
void create_serial_task()			// CHECKED
{
	SERIAL_TASK.task = serial_task;
	SERIAL_TASK.Ptos = (void *) &serial_task_stack[NRK_APP_STACKSIZE - 1];
	SERIAL_TASK.Pbos = (void *) &serial_task_stack[0];
	SERIAL_TASK.prio = 19;
	SERIAL_TASK.FirstActivation = TRUE;
	SERIAL_TASK.Type = BASIC_TASK;
	SERIAL_TASK.SchType = PREEMPTIVE;
	
	SERIAL_TASK.cpu_reserve.secs = 0;
	SERIAL_TASK.cpu_reserve.nano_secs = 200 * NANOS_PER_MS;  
	SERIAL_TASK.period.secs = 0;
	SERIAL_TASK.period.nano_secs = 250 * NANOS_PER_MS;
	   
  
  	SERIAL_TASK.offset.secs = 0;
  	SERIAL_TASK.offset.nano_secs= 0;
  	nrk_activate_task (&SERIAL_TASK);
  	
  	nrk_kprintf(PSTR("SR: create_serial_task(): Serial Task started\r\n"));
  	
  	return;
}
예제 #24
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v;
    uint8_t len, i;
    uint8_t chan;


    cnt = 0;
    nrk_kprintf (PSTR ("Nano-RK Version "));
    printf ("%d\r\n", NRK_VERSION);


    nrk_kprintf( PSTR( "RX Task PID=" ));
    printf ("%u\r\n", nrk_get_pid ());
    t.secs = 5;
    t.nano_secs = 0;

    while (cal_done==0)
        nrk_wait_until_next_period ();


    chan = RADIO_CHANNEL;
    if (SET_MAC == 0x00) {

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
            v = read_eeprom_aes_key(aes_key);
        }
        else {
            while (1) {
                nrk_kprintf (PSTR
                             ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
                nrk_wait_until_next_period ();
            }
        }
    }
    else
        mac_address = SET_MAC;

    printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
    printf ("chan = %d\r\n", chan);
    len=0;
    for(i=0; i<16; i++ ) {
        len+=aes_key[i];
    }
    printf ("AES checksum = %d\r\n", len);



    tdma_init (TDMA_CLIENT, chan, (mac_address));

    tdma_aes_setkey(aes_key);
    tdma_aes_enable();

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    v = tdma_tx_slot_add (mac_address&0xff);

    if (v != NRK_OK)
        nrk_kprintf (PSTR ("Could not add slot!\r\n"));

    // setup a software watch dog timer
    t.secs=30;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);
    while (1) {
        // Update watchdog timer
        nrk_sw_wdt_update(0);
        v = tdma_recv (&rx_tdma_fd, &rx_buf, &len, TDMA_BLOCKING);
        if (v == NRK_OK) {
            // printf ("src: %u\r\nrssi: %d\r\n", rx_tdma_fd.src, rx_tdma_fd.rssi);
            // printf ("slot: %u\r\n", rx_tdma_fd.slot);
            // printf ("cycle len: %u\r\n", rx_tdma_fd.cycle_size);
            v=buf_to_pkt(&rx_buf, &rx_pkt);
            if(v==NRK_OK)
            {
                if(((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || ((rx_pkt.dst_mac&0xff)==0xff))
                {
                    if(rx_pkt.type==PING)
                    {
                        send_ack=1;
                        nrk_led_clr(0);
                        nrk_led_clr(1);
                        if(rx_pkt.payload[0]==PING_1)
                        {
                            nrk_led_set(0);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(0);
                        }
                        if(rx_pkt.payload[0]==PING_2)
                        {
                            nrk_led_set(1);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(1);
                        }
                        if(rx_pkt.payload[0]==PING_PERSIST)
                        {
                            nrk_led_set(0);
                        }


                    }

                    if(rx_pkt.type==APP)
                    {
                        // payload 1: Key
                        if(rx_pkt.payload[1]==2)
                        {
                            send_ack=1;
                            // payload 2: Outlet Number
                            // payload 3: On/Off
                            if(rx_pkt.payload[3]==0) {
                                power_socket_disable(rx_pkt.payload[2]);
                                plug_led_green_clr();
                                //printf( "Disable %d\r\n", rx_pkt.payload[2] );
                            }
                            if(rx_pkt.payload[3]==1) {
                                power_socket_enable(rx_pkt.payload[2]);
                                //printf( "Enable %d\r\n", rx_pkt.payload[2] );
                                plug_led_green_set();
                            }
                        }

                        // payload 1: Key
                        if(rx_pkt.payload[1]==3)
                        {
                            send_ack=1;
                            true_power_thresh=((uint32_t)rx_pkt.payload[3])<<16 | ((uint32_t)rx_pkt.payload[4])<<8 | (uint32_t)rx_pkt.payload[5];
                            set_power_thresh(true_power_thresh);
                        }




                    }

                }

            }
            /*      printf ("len: %u\r\npayload: ", len);
            for (i = 0; i < len; i++)
              printf ("%d ", rx_buf[i]);
            printf ("\r\n");

            if(rx_buf[0]==(mac_address&0xff))
            {
            if(rx_buf[2]==0) {
            	power_socket_disable(rx_buf[1]);
            	printf( "Disable %d\r\n", rx_buf[1] );
            }
            if(rx_buf[2]==1) {
            	power_socket_enable(rx_buf[1]);
            	printf( "Enable %d\r\n", rx_buf[1] );
            }
                 }
                 */

        }

        tdma_rx_pkt_release();
        //  nrk_wait_until_next_period();
    }

}
예제 #25
0
void nl_tx_task()
{
	TransmitBuffer *ptr = NULL; 				// pointer to the buffer to be transmitted 
	nrk_sig_t tx_done_signal;					// to hold the tx_done signal from the link layer 
	int8_t ret;										// to hold the return value of various functions 
	int8_t port_index;							// to store the index of the corresponding port element 
	int8_t sent;									// to count the number of times the HELLO msg was sent 
	int8_t isApplication;						// flag to indicate whether the packet in the transmit 
														// buffer is an APPLICATION / NW_CONTROL packet
	 
	nrk_time_t timeout;
	nrk_time_t start;								// used for sending network control messages 
	nrk_time_t end;	
	nrk_time_t elapsed;			
	
	// wait for the nl_rx_task to start bmac 	
	while(!bmac_started()) nrk_wait_until_next_period();
	
	// retrieve and register for the 'transmit done' signal from bmac 	
	tx_done_signal = bmac_get_tx_done_signal();
  	
  	if( nrk_signal_register(tx_done_signal) == NRK_ERROR )
  	{
  		nrk_int_disable();
  		nrk_led_set(RED_LED);
  		while(1)
  			nrk_kprintf(PSTR("NL: Error while registering for the bmax_tx_done_signal\r\n"));
  	}
	
	// initialise the timer
	nrk_time_get(&start);
	end.secs = start.secs;
	end.nano_secs = start.nano_secs;
	sent = 0;
	
	// set the radio power
	if( bmac_set_rf_power(10) == NRK_ERROR)
  	{
  		nrk_led_set(RED_LED);
  		nrk_int_disable();
  		while(1)
  			nrk_kprintf(PSTR("Error setting the transmit power\r\n"));
  	}
	while(1)
	{
		isApplication = FALSE;	// assume at the beginning that a nw_ctrl pkt will be transmitted 
				
		ret = nrk_time_sub(&elapsed, end, start);
		if(ret == 0)
		{
			nrk_int_disable();
			nrk_led_set(RED_LED);
			while(1)
				nrk_kprintf(PSTR("NL: Error returned by nrk_time_sub\r\n"));
		}
		
		nrk_time_compact_nanos(&elapsed);
		
		if(elapsed.secs >= HELLO_PERIOD)
		{	
			sent++;
			build_Msg_Hello(&mhe);				// build the 'HELLO' message
			if(DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("After building Msg_Hello, packet = "));
				print_pkt(&pkt_tx);
			}
				
			enter_cr(bm_sem, 34);
			ret = insert_tx_aq(&pkt_tx);  			// insert it into the transmit queue
			leave_cr(bm_sem, 34);
			
			if(DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("build_Msg_Hello() inserted packet."));
				print_tx_buffer();
				//print_pkt_header(&pkt_tx);
			}
			
			if(ret == NRK_ERROR && DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("HELLO msg was not inserted into the transmit queue\r\n"));
			}
			
			start.secs = end.secs;	
			start.nano_secs = end.nano_secs; // reinitialise the timer 		
		}

		if(sent >= 3) //NGB_LIST_PERIOD / HELLO_PERIOD)	// NGB_LIST period is always a multiple of HELLO_PERIOD		
		{
			build_Msg_NgbList(&mn);				// build the 'NGB_LIST' message 
									
			enter_cr(bm_sem, 34);
			ret = insert_tx_aq(&pkt_tx);  			// insert it into the transmit queue
			leave_cr(bm_sem, 34);
			
			if(DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("build_Msg_NgbList() inserted packet."));
				print_tx_buffer();
				//print_pkt_header(&pkt_tx);
			}
			
			if(ret == NRK_ERROR && DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("NGB_LIST msg was not inserted into the transmit queue\r\n"));
			}
			
			sent = 0;								// reset the value of 'sent'
		}					
		 
	
		if(rand() % 2 == 0)						// random number generator
			collect_queue_statistics();
		
		enter_cr(bm_sem, 34);		
		ptr = remove_tx_aq();
		leave_cr(bm_sem, 34);
		
		if(ptr == NULL)							// transmit queue is empty 
		{
			if(DEBUG_NL == 2)
				nrk_kprintf(PSTR("NL:Transmit queue is empty\r\n"));
			
			// update the end time 
			nrk_time_get(&end);		
				
			nrk_wait_until_next_period();	// FIX ME 
			continue;
		} 

		if(DEBUG_NL ==  2)
		{
			nrk_kprintf(PSTR("NL: nl_tx_task(): Packet removed. Packet = "));
			//print_pkt_header( &(ptr -> pkt) );
			print_pkt( &(ptr -> pkt) );
			//print_tx_buffer();
		}		
		
		// check to see the type of packet. It should be of type APPLICATION and be sent by this 
		// node  		
		if( (pkt_type(&(ptr -> pkt)) == APPLICATION) && ((ptr -> pkt).src == NODE_ADDR) )		 
		{ 		
			// remove the encapsulated TL segment from the packet 
			unpack_TL_UDP_header(&seg, (ptr -> pkt).data);
			memcpy(seg.data, (ptr -> pkt).data + SIZE_TRANSPORT_UDP_HEADER, MAX_APP_PAYLOAD);
			
			if(DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("NL: nl_tx_task(): Segment Removed = "));
				print_seg(&seg);
			}
			isApplication = TRUE;
		}	
			
		// pack the network packet header into the transmit buffer 
		pack_NW_Packet_header(tx_buf, &(ptr -> pkt));
		// append the network payload into the transmit buffer
		memcpy(tx_buf + SIZE_NW_PACKET_HEADER, (ptr -> pkt).data, MAX_NETWORK_PAYLOAD);
		
		enter_cr(bm_sem, 34);		
		insert_tx_fq(ptr);	// release the transmit buffer into the free queue 
		leave_cr(bm_sem, 34);
		if(DEBUG_NL == 2)
		{
			nrk_kprintf(PSTR("NL: nl_tx_task(): Released transmit buffer back into queue\n"));
			print_tx_buffer();
		}
		
		do
		{
			ret =  bmac_tx_pkt_nonblocking(tx_buf, SIZE_NW_PACKET);	// try to queue the buffer in link layer
			if(ret == NRK_ERROR)
				if(nrk_event_wait(SIG(tx_done_signal)) == 0)
				{
					nrk_int_disable();
					nrk_led_set(RED_LED);
					while(1)
						nrk_kprintf(PSTR("NL: Error returned by nrk_event_wait(tx_done_signal)\r\n"));
				}
		}while(ret == NRK_ERROR);
		
		// packet is queued at link layer
		timeout.secs = 10;				// set a wait period of maximum 10 seconds
		timeout.nano_secs = 0;
		if( nrk_signal_register(nrk_wakeup_signal) == NRK_ERROR )
		{
			nrk_int_disable();
			nrk_led_set(RED_LED);
			while(1)
				nrk_kprintf(PSTR("NL:nl_tx(): Error registering for nrk_wakeup_signal\r\n"));
		}
		if( nrk_set_next_wakeup(timeout) == NRK_ERROR)
		{
			nrk_int_disable();
			nrk_led_set(RED_LED);
			while(1)
				nrk_kprintf(PSTR("NL: nl_tx(): Error returned by nrk_set_next_wakeup()\r\n"));
		}		 
		nrk_led_set(BLUE_LED);
  		ret = nrk_event_wait (SIG(tx_done_signal) | SIG(nrk_wakeup_signal));	// wait for its transmission
  		if(ret == 0)
  		{
  			nrk_int_disable();
  			nrk_led_set(RED_LED);
  			while(1)
  				nrk_kprintf(PSTR("NL: Error returned by nrk_event_wait(tx_done_signal)\r\n"));
  		}
	   if(ret & SIG(tx_done_signal)) 	// bmac has successfully sent the packet over the radio 
		{
			if(isApplication == TRUE)		// it was an application layer packet 
			{
				enter_cr(tl_sem, 34);				
				port_index = port_to_port_index(seg.srcPort);
						
				if(port_index == NRK_ERROR)	// sanity check
				{
					nrk_int_disable();
					nrk_led_set(RED_LED);
					while(1)
						nrk_kprintf(PSTR("NL: nl_tx_task: Bug detected in implementation of port element array\r\n"));
				}
				// signal 'send done' signal 			
				if(nrk_event_signal(ports[port_index].send_done_signal) == NRK_ERROR)
				{
					if(nrk_errno_get() == 1)	// sanity check. This means signal was not created 
					{
						nrk_int_disable();
						nrk_led_set(RED_LED);
						while(1)
							nrk_kprintf(PSTR("NL: nl_tx_task: Bug detected in creating signals in port element array\r\n"));
					}
			   }					
				leave_cr(tl_sem, 34);	
				
			}// end if(isApplication == TRUE)
			
			else	// a network control message was transmitted. Nothing to signal
				;	// do nothing 
		
		} // end if(signal received = tx_done_signal)
		else if(ret & SIG(nrk_wakeup_signal))	
			 {
			 	//nrk_led_set(RED_LED);
			 	//nrk_int_disable();
			 	//while(1)			
			 	//{
			 		nrk_kprintf(PSTR("BMAC did not transmit the packet within specified time\r\n"));
			 	//}
			 }			
			else // unknown signal caught 
			{
				nrk_int_disable();
				nrk_led_set(RED_LED);
				while(1)
					nrk_kprintf(PSTR("NL: nl_tx_task(): Unknown signal caught\r\n"));
			}
		
		nrk_led_clr(BLUE_LED);		
		// update the end time 
		nrk_time_get(&end);
	
   } // end while(1) 
   
	return;
}
예제 #26
0
void nl_rx_task()
{
	uint8_t len; 				   // to hold the size of the received packet, always = sizeof(NW_Packet) 
	int8_t rssi;  			   	// to hold rssi of received packet
	uint8_t *local_rx_buf;	   // pointer to receive buffer of link layer
	int8_t val;						// status variable to hold the return type of function calls
	int8_t flag;
	
	nrk_time_t start, end, elapsed;		// needed by nl_rx_task()
										// to decide when to send own NGB_LIST
	if(DEBUG_NL >= 1)
	{
		nrk_kprintf(PSTR("NL_RX_TASK PID = "));
		printf("%d\r\n",nrk_get_pid());
	}
	
	// initialise the timer
	nrk_time_get(&start);
	end.secs = start.secs;
	end.nano_secs = start.nano_secs;

	// initialise the link layer	
	val = bmac_init(25);
	if(val == NRK_ERROR)
	{
		nrk_int_disable();
		nrk_led_set(RED_LED);
		while(1)
			nrk_kprintf(PSTR("NL: Error returned by bmac_init()\r\n"));
	}	
		
	// give the link layer a rx buffer 	
	val = bmac_rx_pkt_set_buffer(rx_buf, RF_BUFFER_SIZE);
	if(val == NRK_ERROR)
	{
		nrk_int_disable();
		nrk_led_set(RED_LED);
		while(1)
			nrk_kprintf(PSTR("NL: Error returned by bmac_rx_pkt_set_buffer()\r\n"));
	}
		
	// start processing forever 
	while(1)
	{
		// decide whether it is time to send your own Ngb_List message
		if(CONNECTED_TO_GATEWAY == TRUE)
		{
			nrk_time_get(&end);
			val = nrk_time_sub(&elapsed, end, start);
			nrk_time_compact_nanos(&elapsed);
			if(elapsed.secs >= NGB_LIST_PERIOD)
			{
				ntg_pkt.type = SERIAL_NGB_LIST;
				ntg_pkt.length = SIZE_MSG_NGB_LIST;
				enter_cr(nl_sem, 34);
				pack_Msg_NgbList(ntg_pkt.data, &nl);
				leave_cr(nl_sem, 34);
				pack_NodeToGatewaySerial_Packet_header(to_gw_buf, &ntg_pkt);
				memcpy(to_gw_buf + SIZE_NODETOGATEWAYSERIAL_PACKET_HEADER, ntg_pkt.data, MAX_SERIAL_PAYLOAD);
				if(DEBUG_NL == 2)
				{
					nrk_kprintf(PSTR("Sending own NGB_LIST message to gateway\r\n"));
				}
				sendToSerial(to_gw_buf, SIZE_NODETOGATEWAYSERIAL_PACKET);
				
				// reset the timer
				start.secs = end.secs;
				start.nano_secs = end.nano_secs;
			} // end if
		} // end if
				
		if(DEBUG_NL >= 1)
		{
			nrk_kprintf(PSTR("Waiting for next pkt from link layer\r\n"));
		}
		
		flag = 0;
		// wait for the next packet 		
		while(bmac_rx_pkt_ready() == 0)
		{
			val = bmac_wait_until_rx_pkt();
			if(DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("NL: bmac_wait_until_rx_packet() returned "));
				printf("%d\n", val);
			}
		}
		
		// Get the packet 
	   	do
	   	{
	   		local_rx_buf = bmac_rx_pkt_get(&len,&rssi);
	   		if(local_rx_buf == NULL)
	   		{
	   			nrk_kprintf(PSTR("NL: NULL returned by bmac_rx_pkt_get()\r\n"));
	   		}
	   		
	   	} while(local_rx_buf == NULL);
	   	
	   	// sanity check for debugging 
	   	if(len != SIZE_NW_PACKET)	// this should not happen 
	   	{
	   		/* 
	   		nrk_int_disable();
	   		nrk_led_set(RED_LED);
	   		while(1)
	   		{
	   			nrk_kprintf(PSTR("NL: Wrong length of packet received: "));
	   			printf("%d\r\n", len);
	   		}
	   		*/
	   		if(DEBUG_NL >= 1)
	   		{
	   			nrk_kprintf(PSTR("NL: nl_rx_task(): Wrong length of packet received: "));
	   			printf("%d\r\n", len);
	   		}
	   		flag = 1;
	   	}
	   	
	   	nrk_led_set(GREEN_LED);
	   	
	   	if(DEBUG_NL == 2)// || flag == 1)
	   	{
				int8_t i; 
				nrk_kprintf(PSTR("NL: Contents of received packet are\r\n"));
				printf("[");
				for(i = 0; i < len; i++)
					printf("%d ", local_rx_buf[i]);
				printf("]\r\n");
		}  	
		if(flag == 1)
		{
			bmac_rx_pkt_release();	// drop the packet and go receive another
			nrk_led_clr(GREEN_LED);
			continue;
		}
			   	
			// unpack the packet header from the received buffer 
			unpack_NW_Packet_header(&pkt_rx, local_rx_buf);
			// copy the packet payload to the data field of the local packet
			memcpy(pkt_rx.data, local_rx_buf + SIZE_NW_PACKET_HEADER, MAX_NETWORK_PAYLOAD);
			// Release the RX buffer quickly so future packets can arrive
			bmac_rx_pkt_release();	
			
			// begin processing this packet
			if(pkt_type(&pkt_rx) == APPLICATION)	// its an application layer packet 
			{
				// case 1: Destination is NODE_ADDR or BCAST_ADDR
				if(pkt_rx.dest == NODE_ADDR || pkt_rx.dest == BCAST_ADDR)
					process_app_pkt(&pkt_rx, rssi);
					
				// case 2: I am enroute to the destination
				else if(pkt_rx.nextHop == NODE_ADDR)
					 {
					 	if(pkt_rx.src == NODE_ADDR)		// routing table corrupted
						{
							nrk_int_disable();
							nrk_led_set(RED_LED);
							while(1)
							{
								nrk_kprintf(PSTR("Routing table corrupted at "));
								printf("%d\r\n", NODE_ADDR);
							} // end while 
						} // end if 
						else
							route_packet(&pkt_rx);
					 } // end if 		
					 // case 3: Routing tables still not made
					 else if(pkt_rx.nextHop == BCAST_ADDR)	
					 			route_packet(&pkt_rx);
					 	  else
					 	  	;		 // drop all other packets								
			} // end if(type == APPLICATION)
			else 
			{
				if(pkt_type(&pkt_rx) == NW_CONTROL)	// its a network control packet 
				{
				  	// case 1: Destination is NODE_ADDR or BCAST_ADDR
				  	if(pkt_rx.dest == NODE_ADDR || pkt_rx.dest == BCAST_ADDR)
				  		process_nw_ctrl_pkt(&pkt_rx, rssi);
				  	
				  	// case 2: I am enroute to a destination
				  	else if(pkt_rx.nextHop == NODE_ADDR)
				  		 {
				  		 	if(pkt_rx.src == NODE_ADDR)		// routing table corrupted
				  		 	{
				  		 		nrk_int_disable();
								nrk_led_set(RED_LED);
								while(1)
								{
									nrk_kprintf(PSTR("Routing table corrupted at "));
									printf("%d\r\n", NODE_ADDR);
								} // end while 
							} // end if
							else
				  		 		route_packet(&pkt_rx);
				  		 } // end if
				  		// case 3: Routing tables still not made 
				  		 else if(pkt_rx.nextHop == BCAST_ADDR)
				  		 		route_packet(&pkt_rx);
				  		 	  else
				  		 		;	// drop all other packets
				  } // end if(type == NW_CONTROL)
				  else	// unknown packet type 
				  {
				  		nrk_kprintf(PSTR("NL: Unknown pkt type received = "));
				  		printf("%d\r\n", pkt_type(&pkt_rx));
				  }
			}
				
			nrk_led_clr(GREEN_LED);			
	 } // end while(1)
	
	return;
}	// end nl_rx_task
예제 #27
0
void process_nw_ctrl_pkt(NW_Packet *pkt, int8_t rssi)
{
	int8_t ret;	// to hold the return value of various function calls  
	int8_t i;
	
	if(DEBUG_NL >= 1)
		nrk_kprintf(PSTR("NL: Entered process_nw_ctrl_pkt()\n"));
		
	switch( nw_ctrl_type(pkt -> type) )
	{
		case HELLO:		// HELLO msg
			
			// unpack the Msg_Hello from the packet payload 
			unpack_Msg_Hello(&mh, pkt -> data);
			mh.n.rssi = rssi;
			ret = add_neighbor(mh.n);	
			if(ret == NRK_ERROR)
			{
				record_max_ngb_limit_reached(pkt);	// record this fact
			} 
			if(DEBUG_NL >= 1)
			{
				nrk_kprintf(PSTR("Received HELLO msg from: "));
				printf("%d ", mh.n.addr);
				nrk_kprintf(PSTR("with RSSI = "));
				printf("%d\r\n",mh.n.rssi);
			}
			
			break;

		case NGB_LIST:		// NGB_LIST msg
			// if the node gets it's own NgbList message back, drop it
			if(pkt -> src == NODE_ADDR)	
				break;
						
			// unpack the Msg_NgbList from the packet payload						
			unpack_Msg_NgbList(&mnlist, pkt -> data);
			nlist = mnlist.nl;
			
			if(DEBUG_NL >= 1)
			{
				int8_t i;	// loop index 
				printf("NL: Received NGB_LIST msg from %d with count = %d\n", nlist.my_addr, nlist.count);
				for(i = 0; i < MAX_NGBS; i++)		
				{
					if(nlist.ngbs[i].addr != BCAST_ADDR)	// valid entry
						printf("%u, ", nlist.ngbs[i].addr);
				}
				printf("\r\n");
			}
			
			//multihop(pkt);		// multihop this NGB_LIST message.
			route_packet(pkt); 
								
			if(CONNECTED_TO_GATEWAY == TRUE)	// this node is connected to the gateway 				
			{					
				// construct a packet to be sent to the gateway over the serial connection 
				ntg_pkt.type = SERIAL_NGB_LIST;
				ntg_pkt.length = SIZE_MSG_NGB_LIST;
				// pack the message in the data field of the NodeToGatewaySerial_Packet 
				pack_Msg_NgbList(ntg_pkt.data, &mnlist);
				
				// pack the NodeToGatewaySerial_Packet header into the serial transmit buffer 
				pack_NodeToGatewaySerial_Packet_header(to_gw_buf, &ntg_pkt);
				// append the payload into the serial transmit buffer 
				memcpy(to_gw_buf + SIZE_NODETOGATEWAYSERIAL_PACKET_HEADER, ntg_pkt.data, MAX_SERIAL_PAYLOAD); // CHECK 
				
				//send the packet to gateway
				if(DEBUG_NL >= 1)
				{
					nrk_kprintf(PSTR("Sending packet to gateway\r\n"));
				}
				sendToSerial(to_gw_buf, SIZE_NODETOGATEWAYSERIAL_PACKET);
				//printBuffer(to_gw_buf, SIZE_NODETOGATEWAYSERIAL_PACKET);
			} 
			break;
			
		case ROUTE_CONFIG:
			
			unpack_Msg_RoutingTable(&mrt, pkt -> data);
			enter_cr(nl_sem, 34);
			DEFAULT_GATEWAY = mrt.dg;	// get the value of DEFAULT_GATEWAY in any case
			leave_cr(nl_sem, 34);
			
			if(DEBUG_NL == 0)
			{
				nrk_kprintf(PSTR("Received a ROUTE_CONFIG message\r\n"));
				for(i = 0; i < MAX_NODES; i++)
				{
					printf("%d -> %d [%d, %d]\r\n", mrt.node, mrt.rt[i].dest, mrt.rt[i].nextHop, mrt.rt[i].cost);
				}
			}
			
			if(mrt.node == NODE_ADDR)	// this is my routing table
			{
				/*enter_cr(nl_sem, 34);
				initialise_routing_table();	// invalidate all entries
				for(i = 0; i < MAX_NODES; i++)
				{
					rt[i] = mrt.rt[i];
				}
				leave_cr(nl_sem, 34);
				*/
				set_RoutingTable(&mrt);
			}
			else						// some other node's routing table. Route it
			{
				route_packet(pkt);
			}
				
			break;
			
		default: 
			nrk_kprintf(PSTR("NL: process_nw_ctrl_pkt(): Unsupported network control message received = "));
			printf("%u\n", pkt -> type);
			break;			
			
	} // end switch
	
	return;
}
예제 #28
0
void process_app_pkt(NW_Packet *pkt, int8_t rssi)
{
	int8_t ret;
	
	if(DEBUG_NL >= 1)
	{
		nrk_kprintf(PSTR("NL: process_app_pkt(): Entered\r\n"));
		nrk_kprintf(PSTR("NL process_app_pkt(): Received from "));
		print_pkt_header(pkt);
		
		/*
		nrk_int_disable();
		nrk_led_set(RED_LED);
		while(1)
			nrk_kprintf(PSTR("What the hell am I doing here\r\n"));
		*/
	}
		
	if(tl_type(pkt -> type) == UDP)				// the TL protocol is UDP 
	{
		// unpack the UDP header					
		unpack_TL_UDP_header(&udp_seg, pkt -> data); 
		// copy the application payload into the data section of the UDP segment					
		memcpy(udp_seg.data, pkt -> data + SIZE_TRANSPORT_UDP_HEADER, MAX_APP_PAYLOAD);  
		
		// check to see if the destination port in the header is associated with any socket 
		if(is_port_associated(udp_seg.destPort) == TRUE)	// yes there is 
		{
			int8_t port_index;
			
			enter_cr(bm_sem, 28);
			enter_cr(tl_sem, 28);
				
			
			if(DEBUG_NL == 2)
			{
				;
				//nrk_kprintf(PSTR("NL: process_app_pkt(): Received segment = "));
				//print_seg(&udp_seg);
				nrk_kprintf(PSTR("NL: process_app_pkt(): Before inserting into port queue\r\n"));
			}			
			insert_rx_pq(&udp_seg, pkt -> prio, pkt -> src, rssi);
			if(DEBUG_NL == 2)
			{
				nrk_kprintf(PSTR("NL: process_app_pkt(): After inserting into port queue\r\n"));
			}				
			
			port_index = port_to_port_index(udp_seg.destPort); // extract the relevant port element 
			if(port_index == NRK_ERROR)	//sanity check for debugging
			{
				nrk_int_disable();
				nrk_led_set(RED_LED);
				while(1)
					nrk_kprintf(PSTR("NL: process_app_pkt(): Bug detected in implementation of port/rbm element array\r\n"));
			}			
			ret = nrk_event_signal(ports[port_index].data_arrived_signal);	// signal 'data arrived'
						
			if(ret == NRK_ERROR)
			{
				if(nrk_errno_get() == 1)	// this means the signal was not created. This is a bug
				{
					nrk_int_disable();
					nrk_led_set(RED_LED);
					while(1)
						nrk_kprintf(PSTR("NL: process_app_pkt(): Bug detected in implementation of port signals\r\n"));
				}				
			} // end if(ret == NRK_ERROR)
		} // end if(is_port_associated())
	
		else // if there is no socket associated with this port, simply drop the packet
		{
			if(DEBUG_NL == 0)
			{
				nrk_kprintf(PSTR("Unassociated port found: "));
				printf("%u\n", udp_seg.destPort);
			}
				
			record_unassociated_socket_pkt(pkt);	// record this fact 
		}
		
		leave_cr(tl_sem, 28);	
		leave_cr(bm_sem, 28);
		
	} // end if(tl_layer == UDP)
	else // as of now UDP is the only supported transport layer, hence print an error
	{
		nrk_kprintf(PSTR("NL: process_app_pkt(): Unsupported transport layer type detected = "));
		printf("%d\r\n", pkt -> type);
	}
	return;
}
예제 #29
0
/******************************* FUNCTION DEFINITIONS ***************************************/
inline int8_t add_neighbor(Neighbor n)
{
	int8_t i;					// loop index
	int8_t found = FALSE;	// flag to indicate whether neighbor was found in array
	
	/* first pass through array checks to see 
		1. if the neighbor had been recorded before
		2. decrements the lastReport (and possibly removes) of each neighbor
	*/
	
	enter_cr(nl_sem, 19);
	for(i = 0; i < MAX_NGBS; i++)	
	{
		// search to see if this neighbor has been recorded before		
		if(nl.ngbs[i].addr == n.addr)
		{
			found = TRUE;
			// update last reported
			nl.ngbs[i].lastReport = TIMEOUT_COUNTER;
			nl.ngbs[i].isNew = FALSE;
			nl.ngbs[i].rssi = n.rssi;
		}
		else if(nl.ngbs[i].addr != BCAST_ADDR)		// entry at this position is valid
      {
			nl.ngbs[i].lastReport--;					// decrement lastReport
			if(nl.ngbs[i].lastReport == 0)			// should I remove this neighbor?
			{
				nl.ngbs[i].addr = BCAST_ADDR;			// invalidate this entry
				nl.ngbs[i].rssi = 0;						// zero out the remaining two entries 
				nl.ngbs[i].isNew = FALSE;
				nl.count--;									// decrement number of ngbs recorded
			}
		}
	} // end for 

	if(found == TRUE)			// neighbor was already present in array; do nothing further
	{
		leave_cr(nl_sem, 19);	
		return NRK_OK;
	}
	
	// check to see if the addition of a new neighbor is possible 	
	if(nl.count == MAX_NGBS)		// cannot store more than MAX_NGBS neighbors
	{
		_nrk_errno_set(MAX_NEIGHBOR_LIMIT_REACHED);
		
		leave_cr(nl_sem, 19);		
		return NRK_ERROR;
	}
	
	/* second pass through array adds the new neighbor */
	for(i = 0; i < MAX_NGBS; i++)	
	{
		if(nl.ngbs[i].addr == BCAST_ADDR)		// this position in array holds an invalid entry
		{
			n.lastReport = TIMEOUT_COUNTER;		// set lastReport to timeout value
			n.isNew = TRUE;							// a new neighbor has reported 
			nl.ngbs[i] = n;							// place the new neighbor at this array index
			nl.count++;									// increment the number of neighbors recorded
			break;
		}
	}
	
	if(i == MAX_NGBS)									// sanity check for debugging
	{
		nrk_int_disable();
		nrk_led_set(RED_LED);
		while(1)
			nrk_kprintf(PSTR("add_neighbor(): Bug found in implementation of MAX_NGBS\r\n"));
	}
		
	leave_cr(nl_sem, 19);	
	return NRK_OK; 
}
예제 #30
0
void tx_task ()
{
    uint8_t j, i, val, cnt;
    int8_t len;
    int8_t v,fd;
    uint8_t buf[2];
    nrk_sig_mask_t ret;
    nrk_time_t t;



    // Set Port D.0 as input
    // On the FireFly nodes we use this for motion or syntonistor input
    // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway
//  DDRD &= ~(0x1);



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

    // setup a software watch dog timer
    t.secs=10;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // set port G as input for gpio
    DDRG=0;


    while (1) {

        nrk_led_clr(RED_LED);


        tx_pkt.payload[0] = 1;  // ELEMENTS
        tx_pkt.payload[1] = 3;  // Key

        fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ);
        if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));


        val=nrk_set_status(fd,SENSOR_SELECT,MOTION);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[28]=buf[1];
        tx_pkt.payload[29]=buf[0];



        val=nrk_set_status(fd,SENSOR_SELECT,BAT);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[2]=buf[1];
        tx_pkt.payload[3]=buf[0];
        //printf( "Task bat=%d",buf);

//	tx_pkt.payload[2]=0;
//	tx_pkt.payload[3]=0;

        val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[4]=buf[1];
        tx_pkt.payload[5]=buf[0];
        //printf( " light=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[6]=buf[1];
        tx_pkt.payload[7]=buf[0];
        //printf( " temp=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[8]=buf[1];
        tx_pkt.payload[9]=buf[0];
        //printf( " acc_x=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[10]=buf[1];
        tx_pkt.payload[11]=buf[0];
        //printf( " acc_y=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[12]=buf[1];
        tx_pkt.payload[13]=buf[0];
        //printf( " acc_z=%d",buf);


        val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[16]=buf[3];
        tx_pkt.payload[17]=buf[2];
        tx_pkt.payload[18]=buf[1];
        tx_pkt.payload[19]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,TEMP2);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[20]=buf[3];
        tx_pkt.payload[21]=buf[2];
        tx_pkt.payload[22]=buf[1];
        tx_pkt.payload[23]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,PRESS);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[24]=buf[3];
        tx_pkt.payload[25]=buf[2];
        tx_pkt.payload[26]=buf[1];
        tx_pkt.payload[27]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[14]=buf[1];
        tx_pkt.payload[15]=buf[0];


        // GPIO data
//	gpio_pin = !!(PINE & 0x4);
        gpio_pin = PING & 0x1;
        tx_pkt.payload[30]=gpio_pin;
        //printf( " audio=%d\r\n",buf);

        nrk_close(fd);

        tx_pkt.payload_len=31;
        tx_pkt.src_mac=mac_address;
        tx_pkt.dst_mac=0;
        tx_pkt.type=APP;

        len=pkt_to_buf(&tx_pkt,&tx_buf );

        if(len>0)
        {
            v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
            if (v == NRK_OK) {
            }
        } else {
            nrk_kprintf(PSTR("Pkt tx error\r\n"));
            nrk_wait_until_next_period();
        }


        nrk_sw_wdt_update(0);
    }



}