// TDMA_SN_sleep -- called when radio cannot receive any beacon for a while static void TDMA_SN_sleep(void) { printf("TDMA RDC: SN goes into sleep mode\n"); //ctimer_stop(&SN_listen_timer); ctimer_set(&SN_sleep_timer,MAX_SLEEP_PERIOD,TDMA_SN_listen,(void*)NULL); NETSTACK_RADIO.off(); incorrect_rx_counter = 0; led_timer(LEDS_RED,CLOCK_SECOND); }
int main() { int i = 0; int dev_fd; dev_fd = open(GPIO_DEVICE_FILENAME, O_RDWR | O_NONBLOCK); if (dev_fd == -1) { printf("Cann't open gpio device file\n"); exit(1); } while (1) { i = (i + 1) % 4; led_timer(dev_fd, i, 1); beep_timer(dev_fd, 1); } close(dev_fd); return 0; }
// receives packet -- called in radio.c,radio.h static void input(void) { if(NETSTACK_FRAMER.parse() < 0) { printf("Incorrect decode frame\n"); return; } #ifdef SF_MOTE_TYPE_SENSOR /*-------------SN CODE----------------------*/ ctimer_stop(&SN_sleep_timer); ctimer_stop(&SN_listen_timer); //check if the packet is from BS if (!rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),&rimeaddr_null)) { rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); printf("Packet is not from base station, rejected! %d\n",sent_sn_addr->u8[0]); incorrect_rx_counter++; if (incorrect_rx_counter > TOTAL_TS*10) { TDMA_SN_sleep(); return; } else { //TDMA_SN_listen(); return; } } uint8_t *rx_pkt = (uint8_t *)packetbuf_dataptr(); uint16_t rx_pkt_len = packetbuf_datalen(); //turn off radio -- save power if(NETSTACK_RADIO.off() != 1) { printf("TDMA RDC: SN fails to turn off radio"); } led_timer(LEDS_GREEN,CLOCK_SECOND/(FRAMES_PER_SEC_INT/2)); SN_RX_start_time = packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP); /*--------from BS------------*/ if (packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == PACKETBUF_ATTR_PACKET_TYPE_CMD) { //skip this period for TX rtimer_clock_t next_bkn_time = SN_RX_start_time + SEGMENT_PERIOD - GRD_PERIOD; rtimer_set(&SNTimer,next_bkn_time,0,NETSTACK_RADIO.on,NULL); #ifdef SF_FEATURE_SHELL_OPT char command_string[128]; strncpy(command_string,rx_pkt,rx_pkt_len); command_string[rx_pkt_len] = (uint8_t)'\0'; printf("RX Command: %s %d\n",command_string,strlen(command_string)); //process_post(&remote_shell_process,remote_command_event_message,command_string); remote_shell_input(); return; #endif /* SF_FEATURE_SHELL_OPT */ } else if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == PACKETBUF_ATTR_PACKET_TYPE_DATA) { //schedule for TX if (sf_tdma_slot_num != -1) { //PRINTF("Schedule for TX at Slot %d\n",my_slot); rtimer_clock_t SN_TX_time = SN_RX_start_time + (BS_period+TS_period * (sf_tdma_slot_num-1))-GRD_PERIOD+33;//20 might need to be changed later, do better calibration, increase to 33 if timing problems rtimer_set(&SNTimer,SN_TX_time,0,TDMA_SN_send,NULL); } } PRINTF("Channel: %d;", cc2420_get_channel()); PRINTF("RSSI: %d\n", cc2420_last_rssi-45); app_conn_input(); //For debugging timing #endif /* SF_MOTE_TYPE_SENSOR */ #ifdef SF_MOTE_TYPE_AP /*-----------------BS CODE---------------*/ rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t sent_sn_id = sent_sn_addr->u8[0]; rtimer_clock_t relFrameTime =(rtimer_clock_t)((packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP)-radio_TX_time)%segment_period); uint16_t current_TS = (uint16_t)((relFrameTime-BS_period)/TS_period )+1; //set flag in pkt for TS occupancy /* if(node_list[current_TS-1] == FREE_SLOT_CONST) //collision -- ask the node to find a new available slot { //node_list[current_TS] = rx_pkt[NODE_INDEX]; node_list[current_TS-1] = sent_sn_id; } */ PRINTF("[Sensor: %u] [Slot: %u] [Seq: %u]\n", sent_sn_id,current_TS,packetbuf_attr(PACKETBUF_ATTR_PACKET_ID)); PRINTF("Channel: %d;", cc2420_get_channel()); PRINTF("RSSI: %d\n", cc2420_last_rssi-45); if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == PACKETBUF_ATTR_PACKET_TYPE_DATA) { // callback to application layer app_conn_input(); } #endif /*SF_MOTE_TYPE_AP */ }
// TDMA_SN_send() -- called at a assigned time slot static void TDMA_SN_send(void) { led_timer(LEDS_BLUE,CLOCK_SECOND/(FRAMES_PER_SEC_INT/2)); //set timer for open RADIO -- for opening earlier 2 ms //uint16_t time = RTIMER_TIME(&SNTimer)+RTIMER_MS*(segment_period-BS_period-my_slot*TS_period); uint16_t callBkTime = RTIMER_NOW(); radioontime = SN_RX_start_time+segment_period-GRD_PERIOD;//RTIMER_TIME(&SNTimer) + (total_slot_num-my_slot)*TS_period-GRD_PERIOD;//(segment_period-BS_period-(my_slot)*TS_period - GRD_PERIOD); rtimer_set(&SNTimer,radioontime,0,NETSTACK_RADIO.on,NULL); //update packet sequence number seq_num = seq_num + 1; seq_num = ((seq_num == 10) ? 11 : seq_num); /* //wait if the tdma_rdc_buffer is accessing by other functions while(tdma_rdc_buf_in_using_flg); // lock tdma_rdc_buffer and preventing access from other functions. tdma_rdc_buf_in_using_flg = 1; if (tdma_rdc_buf_full_flg == 0) { packetbuf_copyfrom((void *)&tdma_rdc_buffer[0],sizeof(uint8_t)*tdma_rdc_buf_ptr); packetbuf_set_datalen(tdma_rdc_buf_ptr); } else { uint8_t temp_len = MAX_PKT_PAYLOAD_SIZE - tdma_rdc_buf_send_ptr; memcpy(packetbuf_dataptr(),tdma_rdc_buffer+tdma_rdc_buf_send_ptr,sizeof(uint8_t)*temp_len); memcpy(packetbuf_dataptr()+temp_len,tdma_rdc_buffer,sizeof(uint8_t)*tdma_rdc_buf_send_ptr); packetbuf_set_datalen(MAX_PKT_PAYLOAD_SIZE); } */ tdma_rdc_buf_copyto_packetbuf(); // send packet -- pushed to radio layer if(NETSTACK_RADIO.on()) { packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO,seq_num); uint8_t hdr_len = NETSTACK_FRAMER.create(); if(NETSTACK_RADIO.send(packetbuf_hdrptr(),packetbuf_totlen()) != RADIO_TX_OK) { printf("TDMA RDC: SN fails to send packet\n"); } else { printf("TDMA RDC: SN sends %d, %d bytes\n",seq_num,packetbuf_datalen()); } //tdma_rdc_buf_full_flg = 0; //tdma_rdc_buf_ptr = 0; //tdma_rdc_buf_send_ptr = 0; //memset(tdma_rdc_buffer,0,MAX_PKT_PAYLOAD_SIZE); tdma_rdc_buf_clear(0); } else { printf("TDMA RDC: SN fails to open radio\n"); } // turn off radio NETSTACK_RADIO.off(); // release tdma_rdc_buffer //tdma_rdc_buf_in_using_flg = 0; }