Beispiel #1
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();
    }

}
Beispiel #2
0
void rx_task()
{
nrk_time_t t;
uint16_t cnt;
int8_t v;
uint8_t len,i,chan;


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

  
printf( "Gateway Task PID=%u\r\n",nrk_get_pid());
t.secs=10;
t.nano_secs=0;

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

  chan = 16;
  if (SET_MAC == 0xffff) {

    v = read_eeprom_mac_address (&mac_address);
    if (v == NRK_OK) {
      v = read_eeprom_channel (&chan);
    }
    else {
      while (1) {
        nrk_kprintf (PSTR
                     ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
  	nrk_led_toggle(RED_LED);
        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);


tdma_init(TDMA_HOST, chan, mac_address);

// Change these parameters anytime you want...
tdma_set_slot_len_ms(10);
tdma_set_slots_per_cycle(12);

slip_init (stdin, stdout, 0, 0);

while(!tdma_started()) nrk_wait_until_next_period();
nrk_led_set(GREEN_LED);
  while(1) {
	v=tdma_recv(&rx_tdma_fd, &slip_tx_buf, &len, TDMA_BLOCKING );	
	nrk_led_set(ORANGE_LED);
	if(v==NRK_OK)
	{
		//for(i=0; i<len; i++ ) printf( "%c", rx_buf[i]);
		// Got a packet from the network so send it over SLIP
		slip_tx ( slip_tx_buf, len );
	}
	else tdma_rx_pkt_release();
	nrk_led_clr(ORANGE_LED);
	nrk_sw_wdt_update(0); 
  	}
}
Beispiel #3
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v,fd;
    uint8_t len, i;
    uint8_t chan;


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


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

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

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
        }
        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);



    tdma_init (TDMA_CLIENT, chan, mac_address);


    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // Set TDMA slot to lower byte of MAC address
    v = tdma_tx_slot_add (mac_address & 0xff);

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

    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);
            tdma_rx_pkt_release();
            /*if((rx_pkt.dst_mac&0xff) == (mac_address&0xff))
            {
            printf ("len: %u\r\npayload: ", len);
            for (i = 0; i < len; i++)
              printf ("%d ", rx_buf[i]);
            printf ("\r\n");




            }*/


        }

        //  nrk_wait_until_next_period();
    }

}
Beispiel #4
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);


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

    chan = CHAN;
    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 ();

    // Mask off lower byte of MAC address for TDMA slot
    // FIXME: This should eventually be lower 2 bytes for larger TDMA cycles
    v = tdma_tx_slot_add (mac_address&0xFF);

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

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

            /*
                  printf ("raw len: %u\r\nraw buf: ", len);
                  for (i = 0; i < len; i++)
                    printf ("%d ", rx_buf[i]);
                  printf ("\r\n");
            */
            if(rx_pkt.type==PING && (((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || ((rx_pkt.dst_mac&0xff)==0xff)))
            {
                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==NW_CONFIG && (((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || (rx_pkt.dst_mac==0xff)))
            {

                /*      printf ("payload len: %u\r\npayload: ", rx_pkt.payload_len);
                      for (i = 0; i < rx_pkt.payload_len; i++)
                        printf ("%d ", rx_pkt.payload[i]);
                      printf ("\r\n");
                */
                // Sleep control packet
                if(rx_pkt.payload[1]==NW_CONFIG_SLEEP)
                {
                    // sleep mode off
                    if(rx_pkt.payload[2]==0 )
                    {
                        // Enter Deep Sleep
                        deep_sleep_button();

                    }
                    if(rx_pkt.payload[2]==2 )
                    {
                        // Enter snooze mode
                        tdma_disable();
                        nrk_wait_until_next_period();
                        tdma_snooze();
                        tdma_enable();

                    }


                }

            }

        }

        //  nrk_wait_until_next_period();
    }

}