void coms_test() { IF_SET set; uint8_t ifaces=0; comBuf *recvd; com_mode(IFACE_SERIAL, IF_LISTEN); #ifdef PLATFORM_LINUX com_mode(IFACE_TERMINAL, IF_LISTEN); #else com_mode(IFACE_RADIO, IF_LISTEN); #endif while(1){ IF_ZERO(&set); IF_SET(IFACE_SERIAL, &set); #ifdef PLATFORM_LINUX IF_SET(IFACE_TERMINAL, &set); #else IF_SET(IFACE_RADIO, &set); #endif ifaces = com_select(&set,2000); if(IF_ISSET(IFACE_SERIAL, &set)){ printf("serial brought us back\n"); recvd = com_recv(IFACE_SERIAL); com_free_buf(recvd); } #ifdef PLATFORM_LINUX else if(IF_ISSET(IFACE_TERMINAL, &set)){ printf("terminal brought us back\n"); recvd = com_recv(IFACE_TERMINAL); com_free_buf(recvd); } #else else if(IF_ISSET(IFACE_RADIO, &set)){ printf("radio brought us back\n"); recvd = com_recv(IFACE_RADIO); printf("[%C-%C]", recvd->data[0], recvd->data[recvd->size-1]); com_free_buf(recvd); } #endif else { printf("timed out, returned %d ifaces\n",ifaces); } } }
void recv_thread(void) { while(1) { IF_SET set; IF_ZERO(&set); IF_SET(IFACE_RADIO, &set); if(com_select(&set, 1000)) { comBuf* p = com_recv(IFACE_RADIO); if (p && p->data[0] == PKT_DO_SENSE) { if (p->size != 5) continue; if (crc_compute(p->data, 3) != buf_extract_WORD(p->data, 3)) continue; send_packet(PKT_REQUESTED_SENSE); } com_free_buf(p); } } }
void remote_control() { uint8_t i; comBuf *recvd; com_mode(IFACE_SERIAL, IF_LISTEN); //tell the uart we want to listen while(1) { /* IF_ZERO(&set); //reset the set of interfaces IF_SET(IFACE_TERMINAL, &set); //add stdin to set IF_SET(IFACE_SERIAL, &set); //add serial to set IF_SET(IFACE_UDP, &set); //become a server com_select(&set,COM_BLOCK); //wait for a device to respond */ // if(IF_ISSET(IFACE_SERIAL,&set)){ //something on serial line // recvd = com_recv(IFACE_SERIAL); //retrieve data from serial // com_send(IFACE_TERMINAL, recvd); //send data to terminal //TODO: forward traffic over the network // com_free_buf(recvd); // fflush (stdout); //show the string incase no \n // else if(IF_ISSET(IFACE_TERMINAL,&set)){//user typed something recvd = com_recv(IFACE_TERMINAL); //get the data com_send(IFACE_UDP,recvd); com_free_buf(recvd); // if(!command_parse(local_commands,recvd->data)){//local command? // send_to_nymph(recvd->data); //} } }
void rtscts_sink_thread(){ comBuf *recv_pkt; //give us a packet pointer com_mode(IFACE_RADIO, IF_LISTEN); printf("before entering the while loop\n"); while(1) { recv_pkt = com_recv(IFACE_RADIO); //blocking recv a packet printf("Src = %C Seq = %C Type = %C Size = %C TS = %l TCNT = %C RSSI = %l\n", recv_pkt->data[0], recv_pkt->data[1], recv_pkt->data[2], recv_pkt->size, recv_pkt->ts, recv_pkt->tcnt, recv_pkt->signal); if(recv_pkt->data[2] == RTS_TYPE) { mos_thread_sleep(100); send_cts(recv_pkt->data[0], recv_pkt->data[1], &ctrl_pkt); printf("Send out CTS to %C\n", recv_pkt->data[0]); } com_free_buf(recv_pkt); //free the recv'd packet to the pool mos_led_toggle(0); } }
// Send data (Commands) to URG and wait for reply int urg_sendMessage(const char* command, int timeout, int* recv_n) { int send_size = urg_sendTag(command); int recv_size = send_size + 2 + 1 + 2; char buffer[LineLength]; int n = com_recv(buffer, recv_size, timeout); *recv_n = n; if (n < recv_size) { // When received size not matched return -1; } if (strncmp(buffer, command, send_size -1)) { // When command not matched return -1; } // !!! If possible do calculate check-sum to verify data // Convert the received data to Hex and return data. char reply_str[3] = "00"; reply_str[0] = buffer[send_size]; reply_str[1] = buffer[send_size + 1]; return strtol(reply_str, NULL, 16); }
void sink_thread(){ comBuf *recv_pkt; //give us a packet pointer com_mode(IFACE_RADIO, IF_LISTEN); send_pkt.size =1; send_pkt.data[0] = 99; com_send(IFACE_RADIO, &send_pkt); // printf("before entering the while loop\n"); while(1) { recv_pkt = com_recv(IFACE_RADIO); //blocking recv a packet printf("Src = %C Seq = %C Type = %C Size = %C TS = %l TCNT = %C RSSI = %l\n", recv_pkt->data[0], recv_pkt->data[1], recv_pkt->data[2], recv_pkt->size, recv_pkt->ts, recv_pkt->tcnt, recv_pkt->signal); com_free_buf(recv_pkt); //free the recv'd packet to the pool mos_led_toggle(0); } }
void sniffer() { comBuf *recv_pkt; com_mode(IFACE_RADIO, IF_LISTEN); while (1) { recv_pkt = com_recv(IFACE_RADIO); com_free_buf(recv_pkt); mos_led_toggle(2); } }
void radio_poll() { comBuf *recvdbuf; while(1) { recvdbuf = com_recv(IFACE_RADIO); mos_led_toggle(1); com_free_buf(recvdbuf); } }
void start (void) { comBuf * ptr; //turn the radio and serial on com_mode(IFACE_RADIO, IF_LISTEN); com_mode(IFACE_SERIAL, IF_LISTEN); //change the radio power? //com_ioctl_IFACE_RADIO(CC1000_TX_POWER, 0x50); myID = mos_node_id_get(); //preset baseID = myID; //I'm the base myParentID = 0; //no parents myDTB = 0; //I am the base mySeqNo = 0; //start at 0 //set timer functions sleep_timer.func = sleep_timer_callback; //set timer times sleep_timer.msecs = TO_NEXT_SLEEP; sleep_timer.reset_to = 0; state = GO; //just to let us know it's on printf ("waiting for start signal\n"); while (1) { ptr = com_recv(IFACE_SERIAL); if (strncmp(ptr->data, "startstartstartstartstartstartstart", 35) == 0) { printf ("received start\n"); //this is for the python script com_free_buf(ptr); break; } else { printf ("got %s\n", ptr->data); printf ("which not the start value. Still waiting...\n"); com_free_buf(ptr); } } //kill the serial to save power com_mode(IFACE_SERIAL, IF_OFF); //start sleep alarm mos_alarm (&sleep_timer); mos_thread_new (send, 128, PRIORITY_NORMAL); mos_thread_new (recv, 128, PRIORITY_NORMAL); }
void rx_thread(void) { while(1) { comBuf* p = com_recv(IFACE_RADIO); printf("[RX] received %d bytes\n", p->size); parse_packet(p); com_free_buf(p); } }
void start (void) { uint8_t i; com_mode(IFACE_SERIAL, IF_LISTEN); while(1) { pkt = com_recv (IFACE_SERIAL); printf("Got packet, size=%d, data: ", pkt->size); for(i = 0; i < pkt->size; i++) printf("%d ", pkt->data[i]); putchar('\n'); com_free_buf(pkt); } }
void test_recv(void) { comBuf to_send; str_to_packet(&to_send,"hi mom\n"); com_send(IFACE_TERMINAL, &to_send); str_to_packet(&to_send,"type something to test (it should echo)\n"); com_send(IFACE_TERMINAL, &to_send); packet = com_recv(IFACE_TERMINAL); if(packet != NULL) { com_send(IFACE_TERMINAL,packet); com_free_buf(packet); } }
void recv() { mos_led_on(2); com_mode(IFACE_RADIO,IF_LISTEN); while(1) { recv_packet = com_recv(IFACE_RADIO); revent = (net_event_t *)recv_packet->data; if (revent->from==0) { mos_led_toggle(1); printf("\b"); } else mos_led_toggle(2); com_free_buf(recv_packet); } }
void receiver(){ int i; comBuf *recv_pkt; printf("Radio to uart initialized....\n"); //hello com_mode(IFACE_RADIO, IF_LISTEN); while(1){ mos_led_toggle(1); recv_pkt = com_recv(IFACE_RADIO); //blocking recv a packet //print out as a string. //printf("received [%d] bytes:%s\n",recv_pkt->size,recv_pkt->data); //print out as data printf("Packet[%C]:",recv_pkt->size); for(i=0;i<recv_pkt->size;i++) printf("%C ",recv_pkt->data[i]); printf("\n"); com_free_buf(recv_pkt);//free the packet back to the pool } }
void ack() { com_mode(IFACE_RADIO,IF_LISTEN); while(1) { mos_led_toggle(2); recv_pkt = com_recv(IFACE_RADIO); //blocking recv a packet printf("r"); event = (net_event_t *)recv_pkt->data; if(event->to == 0) { //packet meant for base-station.. event->to = event->from; event->from = 0; recv_pkt->size=4; com_send (IFACE_RADIO, recv_pkt); } com_free_buf(recv_pkt); } }
// Read data (Reply) from URG until the termination int urg_readLine(char *buffer) { int i; for (i = 0; i < LineLength -1; ++i) { char recv_ch; int n =0; n= com_recv(&recv_ch, 1, Timeout); if (n <= 0) { if (i == 0) { return -1; // timeout } break; } if ((recv_ch == '\r') || (recv_ch == '\n')) { break; } buffer[i] = recv_ch; } buffer[i] = '\0'; return i; }
void receiver(){ uint16_t i, size; uint16_t j,k; uint8_t new_val, old_val; uint8_t power; uint8_t frequency; uint8_t testnumber; uint8_t go=1; char dup='d'; //100 in ascii decimal char destroy='D'; //68 in ascii decimal char newfreqsig ='F'; //70 in ascii decimal uint8_t duplicate = 0; uint8_t value1; uint8_t value2; uint8_t value3; uint8_t n=0; uint8_t receiving =1; uint8_t endfrequency =31;//31; //declarations for new com layer comBuf *packet; comBuf outpacket; comBuf *outpacket2; //test number testnumber=1; //change frequency frequency=3; //rf_set_freq(frequency);original cc1000_raw_ioctl(CC1000_FREQ, frequency); //change power power=255; //rf_set_power(power);//origional cc1000_raw_ioctl(CC1000_TX_POWER, power); com_mode(IFACE_RADIO, IF_LISTEN); com_mode(IFACE_SERIAL, IF_LISTEN); mos_led_display(1); // printf("XXX"); mos_led_display(2); //size = flooding_recv((char *)&data, NET_PORT); while(receiving==1){ packet = com_recv(IFACE_RADIO); value1 = packet->data[0]; mos_led_display(value1); com_free_buf(packet); outpacket.data[0] = value1; outpacket.size = 2; com_send(IFACE_SERIAL, &outpacket); com_free_buf(&outpacket); } return; }
void recv() { comBuf * recvBuf; wildfire_packet_t * wp; net_packet_t * np; uint16_t sample; comBuf * ptr; //header info printf("src\tseq\tp_dtb\ts_dtb\tlast\ttemp\thumid\tw_dir\tw_spd\tbat\n"); while (1) { if (state == SLEEPNODE) { mos_thread_set_suspend_state(SUSPEND_STATE_SLEEP); //power save mode com_mode(IFACE_RADIO, IF_OFF); //kill radio com_mode(IFACE_SERIAL, IF_OFF); //kill serial mos_thread_sleep(SLEEP_INTERVAL - COM_RECV_TIMEOUT); mos_thread_set_suspend_state(SUSPEND_STATE_IDLE); //idle mode com_mode(IFACE_SERIAL, IF_LISTEN); //turn serial on while (1) { ptr = com_recv(IFACE_SERIAL); if (strncmp(ptr->data, "startstartstartstartstartstartstart", 35) == 0) { printf ("got it\n"); //for the python script com_free_buf(ptr); break; } else com_free_buf(ptr); } com_mode(IFACE_SERIAL, IF_OFF); //kill the serial com_mode(IFACE_RADIO, IF_LISTEN);//turn radio on //set timer when to begin next sleep cycle sleep_timer.msecs = TO_NEXT_SLEEP; mos_alarm (&sleep_timer); state = GO; //print out battery dev_open(DEV_MICA2_BATTERY); dev_read(DEV_MICA2_BATTERY, &sample, sizeof(sample)); dev_close(DEV_MICA2_BATTERY); printf("base battery = %d\n", sample); } //wait just 5 seconds to recv before timeout recvBuf = com_recv_timed(IFACE_RADIO, COM_RECV_TIMEOUT); if (recvBuf == NULL) { //probably should do something here... don't really care, though. } else { //pull off the net packet header to see what type it is. np = (net_packet_t *)&(recvBuf->data[0]); if (np->type == DATA) { wp = (wildfire_packet_t *)&(recvBuf->data[sizeof(net_packet_t)]); printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", np->src, np->seqno, np->pkt_dtb, np->sender_dtb, np->last_hop, wp->temp, wp->humidity, wp->wind_direction, wp->wind_speed, wp->battery); } else if (np->type == CONTROL) { //we don't do anything with control packets //debug info: //printf ("received control packet\n"); //printf("type\tsrc\tdest\tnext\tlast\tseq\tpktdtb\ts_dtb\n"); //printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", np->type, np->src, np->dest, np->next_hop, np->last_hop, np->seqno, np->pkt_dtb, np->sender_dtb); } else { //we don't care about other packets //debug info: //printf ("received a MOS packet not associated with our system\n"); //INVALID PACKET!!! -- We should never get here. } //printf ("freeing buffer\n"); com_free_buf(recvBuf); } }//end while }//end recv
static mos_file* reprogram_iface (uint8_t iface, uint32_t* mismatches) { uint8_t j; uint16_t crc, crc_index; uint32_t addr; uint32_t image_size; uint16_t page_index; uint32_t image_index; uint8_t size = 0; uint8_t packet_size; send_byte_command (iface, ACK); recvd = com_recv (iface); if(recvd->data[0] == LOAD_IMAGE) { image_size = (uint32_t)*(uint16_t *)(&recvd->data[1]); com_free_buf (recvd); } else { com_free_buf (recvd); printf ("Didn't get LOAD_IMAGE\n"); return 0; } // Find the next page after the last program image we wrote /*uint32_t start_page = (cb->start_addr + cb->byte_count + ATMEL_FLASH_PAGE_SIZE-1) / ATMEL_FLASH_PAGE_SIZE; addr = start_page * ATMEL_FLASH_PAGE_SIZE; if (!compare && addr + image_size < ATMEL_FLASH_SIZE) { cb->start_addr = addr; } else if (!compare) { // There is not enough room on flash to store this image, // start again at 0 cb->start_addr = addr = 0; } else { // We're only comparing, use the address already in the control block addr = cb->start_addr; }*/ mos_file* file = NULL; if (mismatches == NULL) { file = mos_file_create("prg", image_size); addr = 0; } else { mismatches = 0; file = mos_file_open("prg"); // file doesn't exist if (!file) return NULL; // TODO check for matching file size addr = file->start; } image_index = 0; //image index is how many bytes written from the image //image size is the total number of bytes in the image send_byte_command(iface, LOAD_ACK); while(image_index < image_size) { mos_led_toggle (0); recvd = com_recv(iface); // don't send address for performance //addr = *(uint16_t *)(&recvd->data[0]); //addr = image_index; crc = *(uint16_t *)(&recvd->data[0]); com_free_buf (recvd); // Now get the data packets //page index is how many bytes from the current page we've received page_index = 0; //page size is the size of the pages in memory. while (page_index < PAGE_SIZE_BYTES) { //loop until we have a full page recvd = com_recv(iface); for (j = 0; j < recvd->size; j++) { //loop through the packet //copy packet into page buffer [at right index] pagebuf[page_index+j] = recvd->data[j]; } packet_size = recvd->size; com_free_buf (recvd); page_index += packet_size; //don't send page ack for performance //PAGE ACK //send_byte_command(PACKET_ACK); // If this is the last packet then we break out if(packet_size < COM_DATA_SIZE) { size = packet_size; break; } } // actually write the page //dev_ioctl(DEV_ATMEL_FLASH, DEV_SEEK, addr); if (mismatches == NULL) { mos_file_write(pagebuf, file, addr, sizeof(pagebuf)); //dev_write(DEV_ATMEL_FLASH, pagebuf, sizeof(pagebuf)); } else { dev_ioctl(DEV_ATMEL_FLASH, DEV_SEEK, addr); // Don't write, just compare the buffer with the page already in flash if (atmel_flash_compare(pagebuf, sizeof(pagebuf))) (*mismatches)++; } addr += sizeof(pagebuf); // find out the proper index to pass to crc_compute if (packet_size < COM_DATA_SIZE) crc_index = (uint16_t)(image_size - image_index); else crc_index = page_index; // perform the crc calculation and send off the result if (crc_compute (pagebuf, crc_index) != crc) send_byte_command (iface, CRC_ERROR); else send_byte_command (iface, CRC_OK); // finally update the image index image_index += page_index; } send_byte_command (iface, IMAGE_ACK); // In case the flash driver is in buffered mode, flush the buffer dev_ioctl(DEV_ATMEL_FLASH, DEV_FLUSH); return file; }