/*
** ===================================================================
** Název souboru   : USB_unit.c
** Mikrokontroler  : ATmega128, ATmega16,...
** Popis           : Program pro USB RF modul - prevadi 868Mhz na virtual serial + sifruje a desifruje
**                 :  
** Autor           : Jan Kristel (kristel_ml@volny,cz)
**                 : xx.04.2014
** ===================================================================
*/


//---- includovano v VirtualSerial.h ----

#include "VirtualSerial.c"
#include "VirtualSerial.h"
#include "RFM12Bx.h"
#include "RFM12Bx.c"
#include "jk_xtea.h"
#include "jk_xtea.c"

volatile unsigned long  key_dec[4] = { 1234,7777,1234,5544}; //ctecka -> USB 
volatile unsigned long  key_enc[4] = { 1234,7777,1234,4455}; //USB(openwrt) -> ctecka 

char buffer_rf_rx [17];
char buffer_UART_rx [17];

volatile uint8_t flag_rf_rx = 0;
volatile uint8_t flag_uart_rx = 0;

unsigned char buffer_serial[17];

unsigned char buffer0[7];
unsigned char buffer1[10];


unsigned char buffer_dec [5];
volatile unsigned char crypting_buffer[7];

//---------- preruseni od RFM12B ---
ISR(INT0_vect)
{
  EIMSK   &= ~ (1<<INT0); // vypne INT0
  EIFR = 0x00;

  	
     while(PIND&(1<<NIRQ)); 
	 LED(1);
	 for (int i=0; i<11; i++) 
	{
       buffer_rf_rx[i] = rfRecv();
    }  
	rffifo_reset(); // po prijmu vsech znaku reset FIFO RFM12B 
    flag_rf_rx = 1;
	LED(0);
}


/** Main program entry point. This routine contains the overall program flow, including initial
 *  setup of all components and the main program loop.
 */
int main(void) {
	SetupHardware();
  
	
	/* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
	CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);

	//LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
	GlobalInterruptEnable();
    flag_rf_rx = 0;


	for (;;)
	{
	
	// _delay_ms(100);
	   
//************* Posila data na VIRTUAL Seriak ********	   
   if (flag_rf_rx == 1) // pouze po prijmu dat
     {
	  flag_rf_rx = 0;
    
	  if ( buffer_rf_rx[0] == '!')  // overi start znak jinak vubec nebere v potaz
	  {
		
	    //************** desifruj - zpracuj--------
        for (uint8_t x = 0; x<6; x++) { 
            crypting_buffer[x] =  buffer_rf_rx[x+6]; // vyzobe sifr ID k desesifr.
		    }		
	  
	   for (uint8_t x = 0; x<6; x++) { 
           buffer0[x] = buffer_rf_rx[x];  // vyzobne prikaz + ID ctecky
	       }
	   	     
	   decode(buffer_dec, crypting_buffer, key_dec);
	   
    if (int anti_replay( crypting_buffer, EE_SHIFT_0 )) // funkce antireplay - one time key
     // {   
	  strcat(buffer_serial, buffer0 );
	  strcat(buffer_serial, buffer_dec); // seradi
	 
	  fputs(buffer_serial, &USBSerialStream);
      CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
	  buffer_serial[0]='\0';

	}      
	    
	 }
    }	   
         
//*******************************************************

//**************Prijima data virtual seriak ********************	   
  _delay_ms(100);
	 
    for (int i=0; i<14; i++ )
     {
      uint8_t ReceivedByte = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
      buffer_UART_rx [i] = ReceivedByte; // porida do bafru*/
	 }
	 
	 
   
//******** RFM12B - TX **********
  if (buffer_UART_rx [0] == '!')
   {

	  LED(1);
	 
	     for (uint8_t x = 0; x<4; x++) {
		     buffer0[x] = buffer_UART_rx [x+1];
			 } // vyzobe sifr ID k desesifr.
	 
	 encode(crypting_buffer, buffer0, key_enc, EE_SHIFT_1); // zasifruje data  -- vystup z koderu --  4byte + 2byte 	 
	 // nastrka vykricnik 1 byte, sifrovana data 6byte, cas 8byte cas
	  for (uint8_t x = 0; x<8; x++) { 
	      buffer1[x] = buffer_UART_rx [x+5];
	      } // vyzobe cas
	 
	  strcat(buffer_serial, "!" );   // start znak
	  strcat(buffer_serial, crypting_buffer); // sysfr data
	  strcat(buffer_serial, buffer1 );  // cas
	  
	  _delay_ms(200);   
	  rftransmitter_mode();  
	  rfSend_char (buffer_serial);
      LED(0);
      _delay_ms(100);
	  rfreceiver_mode();
	  buffer_serial[0]='\0';

   }
   EIMSK  |= (1<<INT0); // znovu enable INT0	  

   }
 }
   
 
