//*************************PING MODE************************************************ void getDestMac() { mac_addr[0] = '\0'; nrk_kprintf(PSTR("\r\n*************************************************************\r\n")); nrk_kprintf(PSTR(" Please Enter Address of Node to Program \r\n")); nrk_kprintf(PSTR(" MAC (Subnet + Dest) 4 Bytes Hex E.g. 00000004 \r\n")); nrk_kprintf(PSTR("*************************************************************\r\n")); printf("Enter MAC:"); charCount = 0; // Enter Mac do { // Wait for UART signal while(1) { if(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0) { while(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0) { charCount++; // Read Character c=getchar(); printf( "%c",c); sprintf(mac_addr,"%s%c",mac_addr,c); } break; } timeout.secs = 0; timeout.nano_secs = 20 * NANOS_PER_MS; nrk_wait(timeout); } }while(charCount < 8); buffer0[0]=mac_addr[0]; buffer0[1]=mac_addr[1]; buffer0[3]='\0'; buffer1[0]=mac_addr[2]; buffer1[1]=mac_addr[3]; buffer1[3]='\0'; buffer2[0]=mac_addr[4]; buffer2[1]=mac_addr[5]; buffer2[3]='\0'; buffer3[0]=mac_addr[6]; buffer3[1]=mac_addr[7]; buffer3[3]='\0'; val=sscanf( buffer0,"%x",&dest_mac[3]); val+=sscanf( buffer1,"%x",&dest_mac[2]); val+=sscanf( buffer2,"%x",&dest_mac[1]); val+=sscanf( buffer3,"%x",&dest_mac[0]); nrk_kprintf(PSTR("\r\n*************************************************************\r\n")); nrk_kprintf(PSTR(" Node MAC Accepted \r\n")); printf(" Initiating Update for Node 0 x %X %X %X %X \r\n", dest_mac[3],dest_mac[2],dest_mac[1], dest_mac[0]); nrk_kprintf(PSTR("*************************************************************\r\n")); }
void rx_task() { char c; nrk_sig_t uart_rx_signal; nrk_sig_mask_t sm; printf( "My node's address is %d\r\n",NODE_ADDR ); printf( "rx_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("Get Signal ERROR!\r\n") ); nrk_signal_register(uart_rx_signal); while(1) { // Wait for UART signal while(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0) { // Read Character c=getchar(); printf( "%c",c); if(c=='x') nrk_led_set(GREEN_LED); else nrk_led_clr(GREEN_LED); } sm=nrk_event_wait(SIG(uart_rx_signal)); if(sm != SIG(uart_rx_signal)) nrk_kprintf( PSTR("RX signal error") ); nrk_kprintf( PSTR("\r\ngot uart data: ") ); } }
void tx_task () { int8_t v,state,outlet_state,dst_mac, outlet; uint8_t len, cnt; nrk_sig_t uart_rx_signal; char c; printf ("Gateway Tx Task PID=%u\r\n", nrk_get_pid ()); while (!tdma_started ()) nrk_wait_until_next_period (); uart_rx_signal=nrk_uart_rx_signal_get(); nrk_signal_register(uart_rx_signal); cnt = 0; state=0; while (1) { if(nrk_uart_data_ready(NRK_DEFAULT_UART)) { c=getchar(); if(state==1) { dst_mac=c; state=2; } else if(state==2) { outlet=c; state=3; } else if(state==3) { outlet_state=c; state=4; } if(c=='S') state=1; if(c=='E') { if(state==4) { printf( "TX: %d %d %d\r\n",dst_mac, outlet, outlet_state ); tx_buf[0]=dst_mac; tx_buf[1]=outlet; tx_buf[2]=outlet_state; len=3; // Only transmit data if you want to do so // Messages from the host are always broadcasts v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING); if (v == NRK_OK) { nrk_kprintf (PSTR ("Host Packet Sent\n")); } } state=0; } } else nrk_event_wait(SIG(uart_rx_signal)); } }
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); } }
signed int USART0_getchar() { if (nrk_uart_data_ready(NRK_DEFAULT_UART)!=0) return (signed int)(unsigned char)getchar(); else return -1; }
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(); } }
void tx_task () { // 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()... bmac_init (26); bmac_encryption_set_key(aes_key,16); bmac_encryption_enable(); bmac_rx_pkt_set_buffer (rx_buf, RF_MAX_PAYLOAD_SIZE); //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) { nrk_kprintf(PSTR("\r\n*************************************************************\r\n")); nrk_kprintf(PSTR(" PHOENIX WIRELESS UPDATE SYSTEM \r\n")); nrk_kprintf(PSTR("*************************************************************\r\n")); nrk_kprintf(PSTR("Press 'p' : To PING Nodes in Vicinity \r\n")); nrk_kprintf(PSTR("Press 'u' : To Begin Node Update \r\n")); nrk_kprintf(PSTR(" \r\n")); nrk_kprintf(PSTR("*************************************************************\r\n")); printf("Enter Choice: "); //sm=nrk_event_wait(SIG(uart_rx_signal)); //if(sm != SIG(uart_rx_signal)) //{ // nrk_kprintf( PSTR("UART signal error\r\n") ); // while(1); //} // Wait for UART signal while(1) { if(nrk_uart_data_ready(NRK_DEFAULT_UART)!=0) { // Read Character c=getchar(); printf( "%c\r\n",c); break; } timeout.secs = 0; timeout.nano_secs = 20 * NANOS_PER_MS; nrk_wait(timeout); } // Choose mode switch(c){ case 'p': programState = PING; break; case 'u': getDestMac(); phoenix_init(); programState = UPDATE; break; default: programState = NONE; nrk_kprintf(PSTR("Invalid Command! Please Try Again\r\n")); } // Reset c c = 0; nrk_wait_until_next_period(); // Execute protocol switch(programState) { case PING: pingMode(); break; case UPDATE: updateMode(); break; case NONE:;// Do nothing break; default: nrk_kprintf(PSTR("Invalid Program State\r\n")); break; } nrk_wait_until_next_period (); } }
int8_t slip_rx (uint8_t * buf, uint8_t max_len) { uint8_t c; uint8_t index, last_c; uint8_t received, checksum, size; int8_t v; my_uart_rx_signal=nrk_uart_rx_signal_get(); // Register your task to wakeup on RX Data if (my_uart_rx_signal == NRK_ERROR) nrk_kprintf (PSTR ("SLIP RX error: Get Signal\r\n")); v=nrk_signal_register (my_uart_rx_signal); if(v==NRK_ERROR) nrk_kprintf( PSTR( "SLIP RX error: nrk_signal_register\r\n" )); received = 0; if( nrk_uart_data_ready (NRK_DEFAULT_UART) == 0) sm = nrk_event_wait (SIG (my_uart_rx_signal)); // Wait until you receive the packet start (START) command while (1) { // Wait for UART signal while (nrk_uart_data_ready (NRK_DEFAULT_UART) != 0) { // Read Character //c = getchar (); c = get_byte(); if (c == START) goto start; } if( nrk_uart_data_ready (NRK_DEFAULT_UART) == 0) sm = nrk_event_wait (SIG (my_uart_rx_signal)); c = get_byte(); //c = getchar (); if (c == START) break; } start: size = get_byte (); checksum = 0; while (1) { if( nrk_uart_data_ready (NRK_DEFAULT_UART) == 0) sm = nrk_event_wait (SIG (my_uart_rx_signal)); while (nrk_uart_data_ready (NRK_DEFAULT_UART) != 0) { last_c = c; //c = getchar (); c = get_byte(); // handle bytestuffing if necessary switch (c) { // if it's an END character then we're done with // the packet case END: // a minor optimization: if there is no // data in the packet, ignore it. This is // meant to avoid bothering IP with all // the empty packets generated by the // duplicate END characters which are in // turn sent to try to detect line noise. if (received) { checksum &= 0x7f; if (last_c == checksum) return received; } nrk_kprintf( PSTR( "Checksum failed: ") ); printf( "%d %d %d\r\n",received, last_c, checksum ); //return NRK_ERROR; return received; break; // if it's the same code as an ESC character, wait // and get another character and then figure out // what to store in the packet based on that. case ESC: last_c = c; if( nrk_uart_data_ready (NRK_DEFAULT_UART)==0 ) sm = nrk_event_wait (SIG (my_uart_rx_signal)); c = get_byte (); // if "c" is not one of these two, then we // have a protocol violation. The best bet // seems to be to leave the byte alone and // just stuff it into the packet switch (c) { case END: c = END; break; case ESC: c = ESC; break; } // here we fall into the default handler and let // it store the character for us default: if (received < max_len && received < size) { buf[received++] = c; checksum += c; } } } } return 0; }