int main(void) { uint8_t teststring[] = "teststring\r\n"; uint8_t packettype = 0xEE; uart_init9600(); rfm12_init(); sei(); while(1) { #ifdef SENDER rfm12_tx (sizeof(teststring), packettype, teststring); #else rf rfm12_rx (sizeof(teststring), packettype, teststring); teststring[ sizeof(teststring)-1 ] = 0; printf( "rx: %s\r", teststring ); #endif rfm12_tick(); } return 0; }
int main(void) { timeCount = 0; hardwareInit(); uart0_init(BAUD_SETTING); /* Initialise timer to divide by 1025, giving 32ms time tick */ timer0Init(0,5); rfm12_init(); wdtInit(); sei(); indx = 0; for(;;) { wdt_reset(); uint8_t sendMessage = false; uint16_t character = uart0_getc(); /* Wait for a serial incoming message to be built. */ if (character != UART_NO_DATA) { inBuf[indx++] = (uint8_t)(character & 0xFF); /* Signal to transmit if a CR was received, or string too long. */ sendMessage = ((indx > MAX_MESSAGE) || (character == 0x0D)); } /* Send a transmission if message is ready to send, or something was received and time waited is too long. */ if (sendMessage || (timeCount++ > TIMEOUT)) { /* Wait for the transmit buffer to be freed and message loaded. */ if ((indx > 0) && (rfm12_tx(indx, 0, inBuf) != RFM12_TX_OCCUPIED)) indx = 0; timeCount = 0; } rfm12_tick(); /* If an RF incoming message has been received, take it from the buffer one character at a time and transmit via the serial port. */ if (rfm12_rx_status() == STATUS_COMPLETE) { uint8_t *bufferContents = rfm12_rx_buffer(); uint8_t messageLength = rfm12_rx_len(); uint8_t i; for (i=0;i<messageLength;i++) { uart0_putc(bufferContents[i]); } /* Clear the "in use" status of the receive buffer to be available for rfm12lib. */ rfm12_rx_clear(); } } }
void SendFrame(char *SFrame) { rfm12_tx(strlen(SFrame), 0, (uint8_t*)SFrame); for (uint8_t j = 0; j < 100; j++) { put_string(". "); rfm12_tick(); _delay_us(500); } }
/*** * * void send(uint8_t) : function to enqueue a packet for transmission * returns: 0x02 (error) 0x03 (occupied) 0x80 (enqueued) * ***/ uint8_t megaRF::send(uint8_t s) { /* while (*s) { // so lange *s != '\0' also ungleich dem "String-Endezeichen(Terminator)" UART_send_byte(*s); s++; }*/ return rfm12_tx(1, 0xEE, &s); }
void send_command(uint8_t cmd, uint8_t param) { uint8_t comm[6]; comm[0] = SYG_ID[0]; comm[1] = SYG_ID[1]; comm[2] = SYG_ID[2]; comm[3] = SYG_ID[3]; comm[4] = cmd; comm[5] = param; rfm12_tx(6, 0, comm); }
/*** * * void send(char*) : function to enqueue a packet for transmission * returns: 0x02 (error) 0x03 (occupied) 0x80 (enqueued) * ***/ uint8_t megaRF::send(char* s) { uint8_t* ptr=(uint8_t*)s; uint8_t i=0; //get the length of char s while (*ptr) { // so lange *s != '\0' also ungleich dem "String-Endezeichen(Terminator)" ptr++; i++; } return rfm12_tx(i, 0xEE, (uint8_t*)s); }
void send_data(){ buf[0]='7'; buf[1]='4'; buf[2]='7'; buf[3]='4'; uart_puts("sending \r\n"); rfm12_tx(10,0,buf); for (int i=0;i<10;i++){ rfm12_tick(); _delay_ms(2); } }
/*** * * void send(String&) : function to enqueue a packet for transmission * returns: 0x02 (error) 0x03 (occupied) 0x80 (enqueued) * ***/ uint8_t megaRF::send(String& s) { uint8_t buf[255]; s.getBytes(buf, s.length()); uint8_t* ptr=(uint8_t*)buf; uint8_t i=0; //get the length of char s while (*ptr) { // so lange *s != '\0' also ungleich dem "String-Endezeichen(Terminator)" ptr++; i++; } return rfm12_tx(i, 0xEE, (uint8_t*)buf); }
void sendData(bufferStruct buffer, uint8_t ack){ #if STATE_UART_DEBUG >=2 uart_putstr("seData\r\n"); #endif state = STATE_SENDING_DATA; memcpy(backupData.buffer, buffer.buffer, buffer.bufferLength); backupData.bufferLength = buffer.bufferLength; backupType = ack; rfm12_tx (buffer.bufferLength+OVERHEAD, encode(ack, SEND_DATA, 0x05, buffer.bufferLength, buffer.buffer)); if(ack & NEED_ACK){ #if STATE_UART_DEBUG >=2 uart_putstr("expAck\r\n"); #endif state = STATE_EXPECKT_ACK; startTimer(); } }
void sendAck(bufferStruct buffer){ rfm12_tx (OVERHEAD, encode(ACK, NO_DATA, getAddress(buffer.bufferLength, buffer.buffer), 0, 0x00)); }
void sendNack(bufferStruct buffer){ state = STATE_SENDING_DATA; rfm12_tx (OVERHEAD, encode(NO_ACK, NACK, getAddress(buffer.bufferLength, buffer.buffer), 0, 0x00)); }
uint8_t rfm12_sendbuf(uint8_t len) { uint8_t aes_byte_count = aes256_encrypt_cbc(bufx, len); rfm12_tx(aes_byte_count, 0, (uint8_t *) bufx); return aes_byte_count; }
void rfm12_sendbuf(void) { uint8_t aes_byte_count = aes256_encrypt_cbc(bufx, 16); rfm12_tx(aes_byte_count, 0, (uint8_t *) bufx); }
int RecievePacket(char* Rpacket) { /* see http://www.hansinator.de/rfm12lib/ for rfm12b libray details receive frame, send acknowledgement if received a frame: de-bytestuff check crc if recipient: acknowledge pass to network */ uint8_t* bufptr; char Rframe[50], ackstr[50]; struct frame ack; struct frame Nrframe[FRAMECOUNT]; int Received_Final_frame = 0; int i = 0; while(!Received_Final_frame){ //int Rframe_len; if (rfm12_rx_status() == STATUS_COMPLETE) { bufptr = rfm12_rx_buffer(); for(uint8_t k = 0; k < (rfm12_rx_len()); k++) { Rframe[k] = bufptr[k]; } Rframe[rfm12_rx_len()] = '\0'; rfm12_rx_clear(); put_string(Rframe); strcpy(ackstr, Rframe); int Rframe_check = decode_frame(Nrframe[i], Rframe); put_string("\nRframe_check: "); put_number(Rframe_check); if(Rframe_check & (1<<1)) { if(Rframe_check & 1<<3) { Received_Final_frame = 1; } /* frame received, frame for me acknowledge */ rfm12_tx(strlen(ackstr), 0, (uint8_t*)ackstr); for (uint8_t j = 0; j < 100; j++) { //put_string(". "); rfm12_tick(); _delay_us(500); } i++; } else if(!Rframe_check) { ; } else { break; } } } // if(i && i < FRAMECOUNT) { // put_string("\nPacketComplete"); // strcpy(Rpacket, Nrframe[0].frame); // for(int l = 1; l<i; l++) { // strcat(Rpacket, Nrframe[l].frame); // } // strcat(Rpacket, "\n"); // } return i; }
int SendPacket(char dest, char* Spacket) { /* Packet length = 122 => max 6 frames split packet into frames, send frame, await acknowledgement */ struct frame data[FRAMECOUNT]; int no_frames; put_number(millis()); put_char('\n'); no_frames = makeframe(&data, dest, Spacket, 0); put_number(millis()); put_char('\n'); uint8_t *bufptr; char temp[50]; int i,k, send_complete; struct frame ack; unsigned long time; for(i = 0; i < no_frames; i++) { send_complete = 0; char test[FRAMELEN] = "0123456789012345678901234"; while(!send_complete) { ///////////////////send////////////////////// rfm12_tx(strlen(data[i].frame), 0, (uint8_t*)data[i].frame); for (uint8_t j = 0; j < 100; j++) { //put_string(". "); rfm12_tick(); _delay_us(500); } time = millis() + 500; while((millis() != time)) { ///////////check for acknowledgemt///////////////// if(rfm12_rx_status() == STATUS_COMPLETE) { //send_complete = 1; bufptr = rfm12_rx_buffer(); for(k = 0; k < (rfm12_rx_len()); k++) { temp[k] = bufptr[k]; } temp[rfm12_rx_len()] = '\0'; rfm12_rx_clear(); // put_string("\nRECEIVED: "); // put_string(temp); // put_string("\n\n"); ////////////////check if acknowledgemnt valid//////////////// if(decode_frame(ack, temp) & (1<<1)) { //if(ack.checksum[0] == data[i].checksum[0]) { put_string("\nSend Complete!\n"); send_complete = 1; break; //} } } } if(!send_complete) { put_string("\nTIMEOUT\n"); } } } /* for frame in frames: send_complete = 0; while not send complete: send frame start timer while timer: if acknowledgement: send_complete = 1; */ return 0; }
// Zyklisches Programm void rfm12_layer2_tick() { if (rfm12_send_state == send_buffered) { // Es ist etwas zum Senden vorhanden uint8_t retval = rfm12_tx (RFM12_MYTYPE, (uint8_t*)&rfm12_send_buffer, rfm12_send_buffer.len+RFM12_SEND_HEADER_SIZE); if (retval==0x80) { // erfolgreich eingetragen rfm12_send_state = send_wait; rfm12_send_time = time_ms; debug_write_Pstr(PSTR("Packet Enqeued\r\n")); } else if ((time_ms-rfm12_send_time)>RFM12_SEND_QUEUE_TIMEOUT) { // Timeout beim Warten auf die Sendemöglichkeit // Fehlerzustand einnehmen rfm12_send_state = send_error; rfm12_send_lasterror = send_hardware; debug_write_Pstr(PSTR("SEND QUEUE TIMEOUT\r\n")); } } rfm12_tick(); if (rfm12_rx_status() == RFM12_STATUS_COMPLETE) { uint8_t rx_no_clear=0; if ((rfm12_rx_type()!=RFM12_MYTYPE) || (rfm12_rx_len()<RFM12_SEND_HEADER_SIZE)) { debug_write_Pstr(PSTR("REC INVALID\r\n")); // Paket aus anderem Protokoll oder zu kleines Paket ignorieren } else { // Flag fuer den Sendeanstoss für Antwortpakete uint8_t send_header_buffer=0; rfm12_tsend_buffer *buf = (rfm12_tsend_buffer*)rfm12_rx_buffer(); if (buf->teltype==RFM12_TELTYPE_DATANACK || (buf->destaddress==RFM12_BROADCAST_ADDRESS && buf->teltype==RFM12_TELTYPE_DATA)) { // Broadcast Empfang melden und nicht bestätigen debug_write_Pstr(PSTR("REC BROADCAST\r\n")); if (rfm12_receive_state == receive_idle) { rfm12_receive_buffer.address = buf->srcaddress; rfm12_receive_buffer.len = rfm12_rx_len()-RFM12_SEND_HEADER_SIZE; rfm12_receive_buffer.data = buf->data; rfm12_receive_state = receive_data; rx_no_clear = 1; } } else if (buf->destaddress==RFM12_MYADDRESS) { // Paket für meine Adresse switch (buf->teltype) { case RFM12_TELTYPE_DATA : // Empfang melden und bestätigen debug_write_Pstr(PSTR("REC DATA\r\n")); if (rfm12_receive_state == receive_idle) { rfm12_receive_buffer.address = buf->srcaddress; rfm12_receive_buffer.len = rfm12_rx_len()-RFM12_SEND_HEADER_SIZE; rfm12_receive_buffer.data = buf->data; rfm12_receive_state = receive_data; rx_no_clear = 1; // Antwortpaket zusammenstellen rfm12_send_header_buffer.teltype = RFM12_TELTYPE_ACK; rfm12_send_header_buffer.destaddress = buf->srcaddress; rfm12_send_header_buffer.telcount = buf->telcount; send_header_buffer=1; } break; case RFM12_TELTYPE_ACK : debug_write_Pstr(PSTR("REC ACK\r\n")); if (rfm12_send_state==send_wait && rfm12_send_buffer.destaddress==buf->srcaddress && rfm12_send_buffer.telcount==buf->telcount) { // Korrekte Bestätigung erhalten, Senden ist damit erfolgreich beendet rfm12_send_state = send_ok; } break; case RFM12_TELTYPE_NACK : debug_write_Pstr(PSTR("REC NACK\r\n")); if (rfm12_send_state==send_wait && rfm12_send_buffer.destaddress==buf->srcaddress && rfm12_send_buffer.telcount==buf->telcount) { // Ablehnung erhalten, Senden ist damit mit Fehler beendet rfm12_send_state = send_error; rfm12_send_lasterror = send_nack; } break; case RFM12_TELTYPE_PING : // Antwortpaket zusammenstellen debug_write_Pstr(PSTR("REC PING\r\n")); rfm12_send_header_buffer.teltype = RFM12_TELTYPE_PINGACK; rfm12_send_header_buffer.destaddress = buf->srcaddress; rfm12_send_header_buffer.telcount = buf->telcount; send_header_buffer=1; break; case RFM12_TELTYPE_PINGACK : debug_write_Pstr(PSTR("REC PINGACK\r\n")); if (rfm12_send_state==send_wait && rfm12_send_buffer.destaddress==buf->srcaddress && rfm12_send_buffer.telcount==buf->telcount && rfm12_send_buffer.teltype==RFM12_TELTYPE_PING) { // Korrekte Bestätigung erhalten, Ping ist damit erfolgreich beendet rfm12_send_state = send_ok; } break; case RFM12_TELTYPE_FRAGMENT : // noch nicht implementiert break; default: // alles andere wird ignoriert break; } if (send_header_buffer) { // Ein Antworttelegramm soll gesendet werden rfm12_send_header_buffer.srcaddress = RFM12_MYADDRESS; // Telegramm senden, der Rückgabewert ist uns hier egal, weil man daraus nichts ableiten kann rfm12_tx (RFM12_MYTYPE, (uint8_t*)&rfm12_send_header_buffer, RFM12_SEND_HEADER_SIZE); debug_write_Pstr(PSTR("SEND HEADER\r\n")); } } // if MYADDRESS } if (!rx_no_clear) rfm12_rx_clear(); // unterlagerten Puffer wieder freigeben } // if received if (rfm12_send_state==send_wait && (time_ms-rfm12_send_time)>RFM12_ACK_TIMEOUT) { // Timeout beim Warten auf Sendebestätigung abgelaufen debug_write_Pstr(PSTR("SEND TIMEOUT\r\n")); if (rfm12_send_buffer.retry_count++ < rfm12_send_retry_limit) { rfm12_tx (RFM12_MYTYPE, (uint8_t*)&rfm12_send_buffer, rfm12_send_buffer.len+RFM12_SEND_HEADER_SIZE); rfm12_send_time=time_ms; } else { rfm12_send_state=send_error; rfm12_send_lasterror = send_timeout; } } }