void rx_task() { uint8_t i, len, rssi, *local_rx_buf; bmac_set_cca_thresh(DEFAULT_BMAC_CCA); bmac_rx_pkt_set_buffer ((char*)rx_buf, RF_MAX_PAYLOAD_SIZE); while (1) { if(cache[0]==MyOwnAddress) { nrk_led_set(RED_LED); } else { nrk_led_clr(RED_LED); } bmac_wait_until_rx_pkt(); nrk_led_set(ORANGE_LED); (char*)local_rx_buf = bmac_rx_pkt_get (&len, &rssi); for(i=0; i<len; i++) { putchar(local_rx_buf[i]); } RxPacketProcess(local_rx_buf,len); nrk_led_clr (ORANGE_LED); bmac_rx_pkt_release(); nrk_wait_until_next_period (); } }
void rx_task () { uint8_t i,len; int8_t rssi, v; nrk_status_t ret; uint8_t *local_rx_buf; // printf( "rx_task: PID=%d\r\n",nrk_get_pid()); bmac_rx_pkt_set_buffer(rx_buf,RF_MAX_PAYLOAD_SIZE); while(!bmac_started()) nrk_wait_until_next_period(); while(1) { if( bmac_rx_pkt_ready()==0) bmac_wait_until_rx_pkt(); v = nrk_sem_pend(lock); if( startCnt == 0 ) startCnt = 1; v = nrk_sem_post(lock); nrk_led_toggle(GREEN_LED); local_rx_buf = bmac_rx_pkt_get(&len,&rssi); printf( "rx_task: rssi=%d data=", rssi); for( i=0; i<len; i++ ) { printf( "%c", local_rx_buf[i]); } nrk_kprintf( PSTR("\r\n") ); bmac_rx_pkt_release(); } }
void rx_task () { uint8_t i, len; int8_t rssi, val; uint8_t *local_rx_buf; nrk_time_t check_period; printf ("rx_task PID=%d\r\n", nrk_get_pid ()); // init bmac on channel 25 bmac_init (15); // Enable AES 128 bit encryption // When encryption is active, messages from plaintext // source will still be received. bmac_encryption_set_key(aes_key,16); bmac_encryption_enable(); // bmac_encryption_disable(); // By default the RX check rate is 100ms // below shows how to change that check_period.secs=0; check_period.nano_secs=25*NANOS_PER_MS; val=bmac_set_rx_check_rate(check_period); // The default Clear Channel Assement RSSI threshold is -45 // Setting this value higher means that you will only trigger // receive with a very strong signal. Setting this lower means // bmac will try to receive fainter packets. If the value is set // too high or too low performance will suffer greatly. // bmac_set_cca_thresh(-45); //if(val==NRK_ERROR) nrk_kprintf( PSTR("ERROR setting bmac rate\r\n" )); // This sets the next RX buffer. // This can be called at anytime before releaseing the packet // if you wish to do a zero-copy buffer switch bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); while (1) { // Wait until an RX packet is received val = bmac_wait_until_rx_pkt (); // Get the RX packet nrk_led_set (ORANGE_LED); local_rx_buf = bmac_rx_pkt_get (&len, &rssi); if( bmac_rx_pkt_is_encrypted()==1 ) nrk_kprintf( PSTR( "Packet Encrypted\r\n" )); printf ("Got RX packet len=%d RSSI=%d [", len, rssi); for (i = 0; i < len; i++) printf ("%c", local_rx_buf[i]); printf ("]\r\n"); nrk_led_clr (ORANGE_LED); // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); } }
void rx_task () { uint8_t i, len, rssi; int8_t val; //char *local_rx_buf; nrk_time_t check_period; printf ("rx_task PID=%d\r\n", nrk_get_pid ()); // init bmac on channel 24 bmac_init (5); bmac_set_cca_thresh(DEFAULT_BMAC_CCA); bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); while (1) { // Wait until an RX packet is received val = bmac_wait_until_rx_pkt (); //printf("Hi..\n"); // Get the RX packet nrk_led_set (ORANGE_LED); local_rx_buf = bmac_rx_pkt_get (&len, &rssi); inter_flag=0; if(rssi<5){ dst_addr=0x0003; //inter_tx_buf=local_rx_buf; } else { dst_addr=0x0001; } if(local_rx_buf[0]=='n'){ dst_addr=0x0001; inter_tx_buf=local_rx_buf; inter_flag=1; } printf("inter_flag: %d",inter_flag); printf("\r\n"); printf("rx_buf:"); for(i=0;i<len;i++){ printf("%c",local_rx_buf[i]); } nrk_led_clr (ORANGE_LED); // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); // this is necessary nrk_wait_until_next_period (); } }
void rx_task () { uint8_t i, len; int8_t rssi, val; uint8_t *local_rx_buf; nrk_time_t check_period; // init bmac on channel 25 bmac_init (25); rx_data_ok=0; // This sets the next RX buffer. // This can be called at anytime before releaseing the packet // if you wish to do a zero-copy buffer switch bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); while (1) { // Wait until an RX packet is received val = bmac_wait_until_rx_pkt (); // Get the RX packet nrk_led_set(0); nrk_led_set(1); nrk_led_set(2); nrk_led_set(3); local_rx_buf = bmac_rx_pkt_get (&len, &rssi); printf("RX received: RSSI = %d\r\n", rssi); nrk_spin_wait_us(50000); // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); nrk_led_clr(0); nrk_led_clr(1); nrk_led_clr(2); nrk_led_clr(3); } }
void rx_task () { uint8_t i, len, rssi; int8_t val; char *local_rx_buf; nrk_time_t check_period; printf ("rx_task PID=%d\r\n", nrk_get_pid ()); // init bmac on channel 24 bmac_init (12); bmac_set_cca_thresh(DEFAULT_BMAC_CCA); bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); rx_count = 0; while (1) { // Wait until an RX packet is received if (tx_count >= 300) { printf("Total Packet received : %d \r\n", rx_count); } val = bmac_wait_until_rx_pkt (); // Get the RX packet nrk_led_set (ORANGE_LED); local_rx_buf = bmac_rx_pkt_get (&len, &rssi); //if( bmac_rx_pkt_is_encrypted()==1 ) nrk_kprintf( PSTR( "Packet Encrypted\r\n" )); if ((len > 0) && (rssi > 0)) { printf ("Got RX packet len=%d RSSI=%d \r\n", len, rssi); nrk_led_clr (ORANGE_LED); rx_count++; } // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); // this is necessary nrk_wait_until_next_period (); } }
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
// This task listens for messages and if it receives an XMPP // message addressed to broadcast or this node, it will print it out void rx_task () { uint8_t len; int8_t rssi, val; uint8_t *local_rx_buf; XMPP_PKT_T rxp; nrk_time_t check_period; printf ("rx_task PID=%d\r\n", nrk_get_pid ()); // Initialize FireFly LCD v1.2 board lcd_setup(); lcd_string_display_escape("Waiting\\nfor msg."); while (!bmac_started ()) nrk_wait_until_next_period (); check_period.secs = 0; check_period.nano_secs = 100 * NANOS_PER_MS; val = bmac_set_rx_check_rate (check_period); while (1) { // Wait for new packet if one isn't already available if (bmac_rx_pkt_ready () == 0) { val = bmac_wait_until_rx_pkt (); } // 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] & (MOBILE_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); // just check the last part of the address because we are lazy // In the future you should check the whole address so that you // don't accidentally get someone elses messages 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 #ifdef TXT_DEBUG 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 = p2p_pkt.payload_start; i < p2p_pkt.buf_len; i++) printf ("%d ", p2p_pkt.buf[i]); nrk_kprintf (PSTR ("]\r\n")); #endif // If it is an XMPP message if (p2p_pkt.pkt_type == XMPP_PKT) { nrk_kprintf (PSTR ("XMPP packet for me!\r\n")); // Unpack the message xmpp_pkt_unpack (&rxp, p2p_pkt.payload, 0); // Print it out printf ("XMPP msg:\r\n"); printf (" seq-num=%d\r\n", p2p_pkt.seq_num); printf (" from-jid=%s\r\n", rxp.src_jid); printf (" msg=%s\r\n", rxp.msg); // Load and display message lcd_string_array_load(rxp.msg); } } } // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); } }
void rx_task () { uint8_t pos,len,i,version; int8_t rssi; uint8_t *local_rx_buf; int16_t recipient,sender,dataseq; char type; if(log_g) printf ("log:rx_task PID=%d\r\n", nrk_get_pid ()); // init bmac on channel 15 bmac_init (15); bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); while (1) { // Wait until an RX packet is received bmac_wait_until_rx_pkt (); // Get the RX packet nrk_led_set (ORANGE_LED); local_rx_buf = bmac_rx_pkt_get (&len, &rssi); //if(log_g) printf ("log:Packet recv\n"); //if(log_g) printf ("log:"); //for (i = 0; i < len; i++) { // if(log_g) printf ("%c", rx_buf[i]); //} //if(log_g) printf ("\n"); pos = 0; recipient = get_next_int(local_rx_buf,&pos,len); pos+=1; sender = get_next_int(local_rx_buf,&pos,len); pos+=1; type = local_rx_buf[pos]; pos+=2; if((recipient != 0 && recipient != MAC_ADDR) || sender == MAC_ADDR) { if(log_g) printf("log:Ignore\r\n"); } else if(recipient == 0 && type == 'S') { version =get_next_int(local_rx_buf,&pos,len); on_discover_pkt(sender, version); } else if (recipient == 0 && type == 'B') { onBasketBallPkt(); } else if (type == 'C') { version =get_next_int(local_rx_buf,&pos,len); onConnectionReq(sender,version); } else if(type == 'D' ) { dataseq = get_next_int(local_rx_buf,&pos,len); onData(sender,dataseq,local_rx_buf,len); } else if(type == 'A') { dataseq = get_next_int(local_rx_buf,&pos,len); onAck(sender, dataseq); } else if(type == 'M') { onContactShare(local_rx_buf,len); }else { if(log_g) printf("log:Invalid MSG\r\n"); } // Release the RX buffer so future packets can arrive memset(local_rx_buf,0,len+1); bmac_rx_pkt_release (); nrk_led_clr(ORANGE_LED); nrk_wait_until_next_period(); } }
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); } } }
void rx_task () { uint8_t i, len; uint16_t j; int8_t rssi, val; int8_t data_ready, button_state; uint8_t *local_rx_buf; uint16_t seq_num; uint16_t pkt_cnt; nrk_time_t check_period; printf ("rx_task PID=%d\r\n", nrk_get_pid ()); // init bmac on channel 25 bmac_init (15); // Enable AES 128 bit encryption // When encryption is active, messages from plaintext // source will still be received. //bmac_encryption_set_key(aes_key,16); //bmac_encryption_enable(); // bmac_encryption_disable(); // By default the RX check rate is 100ms // below shows how to change that //check_period.secs=0; //check_period.nano_secs=200*NANOS_PER_MS; //val=bmac_set_rx_check_rate(check_period); // The default Clear Channel Assement RSSI threshold is -45 // Setting this value higher means that you will only trigger // receive with a very strong signal. Setting this lower means // bmac will try to receive fainter packets. If the value is set // too high or too low performance will suffer greatly. bmac_set_cca_thresh(-50); //if(val==NRK_ERROR) nrk_kprintf( PSTR("ERROR setting bmac rate\r\n" )); // This sets the next RX buffer. // This can be called at anytime before releaseing the packet // if you wish to do a zero-copy buffer switch bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); index=0; while (1) { nrk_led_clr(GREEN_LED); nrk_led_set(RED_LED); do { nrk_led_toggle(BLUE_LED); printf( "\r\nTest Data\r\n" ); for(j=0; j<index; j++ ) { printf( "run: %u pkts: %u rssi: %d\r\n",j, data[j],rssi_avg[j] ); } for(j=0; j<100; j++ ) nrk_wait_until_next_period (); }while( nrk_gpio_get(NRK_BUTTON)==1 ); nrk_led_clr(BLUE_LED); nrk_led_clr(RED_LED); for(j=0; j<100; j++ ) nrk_wait_until_next_period (); nrk_led_set(GREEN_LED); pkt_cnt=0; while(1) { // Wait until an RX packet is received do { data_ready=bmac_rx_pkt_ready(); button_state=nrk_gpio_get(NRK_BUTTON); nrk_wait_until_next_period(); } while(data_ready==0 && button_state==1 ); if(button_state==0) break; val = bmac_wait_until_rx_pkt (); // Get the RX packet nrk_led_set (ORANGE_LED); local_rx_buf = bmac_rx_pkt_get (&len, &rssi); //if( bmac_rx_pkt_is_encrypted()==1 ) nrk_kprintf( PSTR( "Packet Encrypted\r\n" )); //printf ("Got RX packet len=%d RSSI=%d [", len, rssi); rssi_acc+=rssi; seq_num=local_rx_buf[1]<<8 | local_rx_buf[0]; printf ("cnt: %u seq num: %u\r\n",pkt_cnt, seq_num); pkt_cnt++; nrk_led_clr (ORANGE_LED); // Release the RX buffer so future packets can arrive bmac_rx_pkt_release (); } rssi_avg[index]=rssi_acc/pkt_cnt; data[index]=pkt_cnt; index++; } }