void Task1() { uint8_t * local_rx_buf; uint8_t my_addr8; uint8_t my_level; int8_t rssi; uint8_t length; uint16_t slot; uint8_t cnt = 0; tdma_init(10); while(!tdma_started()) nrk_wait_until_next_period(); //tdma_schedule_print(); //my_addr8 = tdma_mac_get(); my_addr8 = 1; //my_level = tdma_tree_level_get(); my_level = 0; nrk_kprintf(PSTR("Starting task!\r\n")); while(1) { if (tdma_rx_pkt_check() != 0) { // I have a packet local_rx_buf = tdma_rx_pkt_get(&length, &rssi, &slot); printf("Got pkt len %d rssi %d slot %d\r\n", length, rssi, slot); for (uint8_t i = TDMA_DATA_START; i < length; i++) { printf("%c", local_rx_buf[i]); } nrk_kprintf(PSTR("\r\n")); tdma_rx_pkt_release(); } /* if (tdma_tx_pkt_check() == 0) { sprintf(&tx_buf[TDMA_DATA_START], "From %d lvl %d cnt %d\r\n", my_addr8, my_level, cnt); length = strlen(&tx_buf[TDMA_DATA_START] + TDMA_DATA_START); tdma_tx_pkt(tx_buf, length); cnt++; } tdma_wait_until_rx_or_tx(); */ } }
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(); } }
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(); } }
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); } }