#include "includes.h"


extern tsEDK 			EDK;
extern tsAppCB  		appCB;
extern tsRD3_packet	 	RD3_packet;
extern DATA_PARSING		gData;
extern tsIN 			relay;


//****************************************************************************
// Send_EOM()
// podle typu zarizeni poslat ukoncovaci retezec
//****************************************************************************
void Send_EOM(void)
{
  	SendCharD0(0x0D);		// ukoncit znakem "CR"
	SendCharD0(0x0A);		// ukoncit znakem "LF"
}


//****************************************************************************
// TycoCRC()
// kumulativni soucet jednotlivych nibble - cislic
//****************************************************************************
#ifdef INTERFACE_RD3
#if defined FORMAT_TYCO	
tUChar TycoCRC(void)
{
 char i;
 int crc=0;
 
 for (i=0; i<7; i++)
  {
	crc += (gCard.cardId[i]&0x0f) + ((gCard.cardId[i]>>4)&0x0f);
  }
  
  return (crc);
}
#endif
#endif






// ************************************************************************* 
// RD3_Out_ACS_HEX
// odeslani cisla karty formatem RD3 ACS v HEX tvaru
// ************************************************************************* 
#ifdef INTERFACE_RD3
void RD3_Out_ACS_HEX(void)
{
  #ifdef READER_MIFARE
	SendCharD0(toAscii(gCard.cardId[3]>>4));	// organizace v pameti R8C je BIG ENDIAN
	SendCharD0(toAscii(gCard.cardId[3]));		// cislo karty se z MIFARE posila podle ctecky R10

	SendCharD0(toAscii(gCard.cardId[2]>>4));	
	SendCharD0(toAscii(gCard.cardId[2]));		

	SendCharD0(toAscii(gCard.cardId[1]>>4));
	SendCharD0(toAscii(gCard.cardId[1]));		

	SendCharD0(toAscii(gCard.cardId[0]>>4));
	SendCharD0(toAscii(gCard.cardId[0]));		
  #endif
  
  #ifdef READER_EMMARIN
	SendCharD0(toAscii(gCard.cardId[0]>>4));	// organizace v pameti R8C je BIG ENDIAN
	SendCharD0(toAscii(gCard.cardId[0]));		// cislo karty se z MIFARE posila podle ctecky R10

	SendCharD0(toAscii(gCard.cardId[1]>>4));	
	SendCharD0(toAscii(gCard.cardId[1]));		

	SendCharD0(toAscii(gCard.cardId[2]>>4));
	SendCharD0(toAscii(gCard.cardId[2]));		

	SendCharD0(toAscii(gCard.cardId[3]>>4));
	SendCharD0(toAscii(gCard.cardId[3]));		
  #endif



	SendCharD0('0');		// doplneno nulami do poctu 16 znaku
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	
 	Send_EOM();				// ukoncovaci retezec

}
#endif


// ************************************************************************* 
// RD3_Out_ACS_DEC
// odeslani cisla karty formatem RD3 ACS v dec. tvaru - aktivace JP2
// ************************************************************************* 
#ifdef INTERFACE_RD3
void RD3_Out_ACS_DEC(void)
{
    tUChar dec[5];      // 5 mist pro dekadicky format 0-65535       
	tUChar hex[4];		// 4 znaky pro cislo karty
  	tUInt temp;			
  
  #ifdef READER_MIFARE
	// nejdrive naplnit pole pro konverzi hex=>dec
	hex[0] = gCard.cardId[1]>>4;	
	hex[1] = gCard.cardId[1]&0x0f;	
	hex[2] = gCard.cardId[0]>>4;	
	hex[3] = gCard.cardId[0]&0x0f;	
    temp = gCard.cardId[1]<<8 | gCard.cardId[0];	// posledni 2 bajty z cisla karty
 #endif

  #ifdef READER_EMMARIN
	// nejdrive naplnit pole pro konverzi hex=>dec
	hex[0] = gCard.cardId[2]>>4;	
	hex[1] = gCard.cardId[2]&0x0f;	
	hex[2] = gCard.cardId[3]>>4;	
	hex[3] = gCard.cardId[3]&0x0f;	
    temp = gCard.cardId[2]<<8 | gCard.cardId[3];	// posledni 2 bajty z cisla karty
 #endif


	Int2Dec(temp, &dec[0]);							// prevest na dekadicky format

	SendCharD0(toAscii(dec[0]));					// poslat jako ASCII
	SendCharD0(toAscii(dec[1]));
	SendCharD0(toAscii(dec[2]));
	SendCharD0(toAscii(dec[3]));
	SendCharD0(toAscii(dec[4]));
		
	SendCharD0('0');		// doplneno nulami do poctu 16 znaku
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');
	SendCharD0('0');							

	Send_EOM();				// ukoncovaci retezec
	
}
#endif


// ************************************************************************* 
// RD3_Out_TYCO - 7-mi bajtovy id
// odeslani cisla karty formatem RD3 TYCO - karty s 32-bitovy ID (COMINFO)
// ************************************************************************* 
#ifdef INTERFACE_RD3		
#if defined FORMAT_TYCO	
void RD3_Out_TYCO(void)
{
	tUChar crc;
	
	SendCharD0('!');		// prefix
	SendCharD0('0');		// nedokumentovano, ale z karty to nejde
	SendCharD0('0');	
	
	SendCharD0(toAscii(gCard.cardId[6]>>4));	// organizace v pameti R8C je BIG ENDIAN
	SendCharD0(toAscii(gCard.cardId[6]));		// cislo karty se z MIFARE posila podle ctecky R10

	SendCharD0(toAscii(gCard.cardId[5]>>4));	// organizace v pameti R8C je BIG ENDIAN
	SendCharD0(toAscii(gCard.cardId[5]));		// cislo karty se z MIFARE posila podle ctecky R10

	SendCharD0(toAscii(gCard.cardId[4]>>4));	
	SendCharD0(toAscii(gCard.cardId[4]));		

	SendCharD0(toAscii(gCard.cardId[3]>>4));	
	SendCharD0(toAscii(gCard.cardId[3]));		

	SendCharD0(toAscii(gCard.cardId[2]>>4));	
	SendCharD0(toAscii(gCard.cardId[2]));		

	SendCharD0(toAscii(gCard.cardId[1]>>4));
	SendCharD0(toAscii(gCard.cardId[1]));		

	SendCharD0(toAscii(gCard.cardId[0]>>4));
	SendCharD0(toAscii(gCard.cardId[0]));		
	
	
	// doplneno CRC souctem jednotlivych bajtu - kumulativni soucet jednotlivych cislic, ne bajtu
	crc = TycoCRC();		
	SendCharD0(toAscii(crc>>4));
	SendCharD0(toAscii(crc&0x0f));	
	
	Send_EOM();			// ukoncovaci retezec

}
#endif
#endif



// ************************************************************************* 
// RD3_SendPacket_Card - vola se nactenim karty
// funkce zpracuje odeslani cisla karty formatem RD3 a jine modifiakce
// ************************************************************************* 
#ifdef INTERFACE_EDK
#ifdef INTERFACE_RD3
void RD3_SendPacket_Card(void)
{
  //----------------------------------------------
  // kontrola nacteni karty
  // osetreni pokud by melo nulovou hodnotu
  // aby v pripade dotazu "?" neposlalo same nuly
  //----------------------------------------------
  if (!gCard.cardId[0] && !gCard.cardId[1] && !gCard.cardId[2] && !gCard.cardId[3]) 
   {
     Send_EOM();								// poslat jenom ukoncovaci retezec jako ze nejsou zadne data
	 return;
   }
  
    SYNC_InterruptDisable;						// po dobu odesilani bajtu zakazat SYNC preruseni z DATA0

	#if defined FORMAT_TYCO	
	  RD3_Out_TYCO();
	#else 
	   if ( EDK.input.state & EDK_IN_WGOUT)		// pokud je osazen JP2 tak RD3 emulovat WIEGAND
	    {										// cislo karty se posila v dekadickem tvaru
		  RD3_Out_ACS_DEC();	
		}
	   else 	 
	    {
		  RD3_Out_ACS_HEX();					// posilat cislo v HEX tvaru
		}
	#endif  
									
	wait_ms(1);									// pockat na odeslani posledniho bajtu z TX bufferu 

	EDK.sys_flag &= ~FLAG_SEND_EDK_PACKET;		// smazat flag zadost o odeslani
	EDK.sys_flag |= FLAG_EDK_PACKET_SENDED;		// nastavit flag ze paket byl odeslan - navazuje na toto povoleni dalsiho cteni 
    
	EDK.sync_count = 0;
		 
	#if !defined ACS_NO_SYNCHRO	
	  SYNC_InterruptEnable;						// povolit detekci SYNC signalu
	#endif
 
}

#endif	 
#endif	



// ************************************************************************* 
// RD3_SendPacket_Io 
// funkce kontroluje stav vstupu a relatka, a odesle informacni paket
// ************************************************************************* 
#ifdef INTERFACE_EDK
#ifdef INTERFACE_RD3
#ifdef HW_SPEC_RRD3
void RD3_SendPacket_Io(void)
{
 // nektery vstup se zmenil?
if ( EDK.input.state != EDK.input.lastState)						// nektery vstup se zmenil
  {
    EDK.input.change = EDK.input.state ^ EDK.input.lastState;		// zjistit zmenu stavu

    // zmena vstupu IN1 - odeslat informaci
    if (EDK.input.change & EDK_IN_INPUT1)		// maska vstupu 
     { 
	   AddReq(PRIORITY_TASK_MID | PROCESS_RD3_INPUT_STATE, EDK_IN_INPUT1, 0);	// volat pres frontu bez parametru
     }

    // zmena vstupu IN2 - odeslat informaci
    if (EDK.input.change & EDK_IN_INPUT2)		// maska vstupu 
     { 
	   AddReq(PRIORITY_TASK_MID | PROCESS_RD3_INPUT_STATE, EDK_IN_INPUT2, 0);	// volat pres frontu bez parametru
     }
  }		// konec zmnena vstupu 
  
  EDK.input.lastState = EDK.input.state;						// po zpracovani ulozit soucasny stav
   

  // zmena stavu rele - odeslat informaci
  relay.lastState = relay.state;				// zalohovat posledni stav
  relay.state = (EDK.output & EDK_OUT_RELAY);	// nacist novy stav
  
  if ( relay.lastState != relay.state) 			// kontrola jestli se stav zmenil
   { 
    //AddReq(PRIORITY_TASK_HIGH | PROCESS_RD3_RELE, 0, 0);	
	RD3_SendPacket_Rele();							// odeslat informaci o zmene
   }
 
}
#endif  
#endif	 
#endif	





// ************************************************************************* 
// RD3_SendPacket_Echo
// jen echo po UART
// ************************************************************************* 
#ifdef INTERFACE_EDK
#ifdef INTERFACE_RD3
void RD3_SendPacket_Echo(tUChar znak)
{
   SendCharD0(znak);		// echovat posledni prijaty znak
}
 #endif
 #endif


// ************************************************************************* 
// RD3_SendPacket_Id
// identikace ctecky RD3
// ************************************************************************* 
#ifdef INTERFACE_EDK
#ifdef INTERFACE_RD3
void RD3_SendPacket_Id(void)
{
  
 tUChar ver_buff[5];
 tUInt ver;
  
 #ifdef HW_SPEC_RRD3
  SendCharD0('R');
  SendCharD0('R');
  SendCharD0('D');
  SendCharD0('3');
   #ifdef READER_MIFARE		// RRD3M - Mifare
    SendCharD0('M');  
   #else
    SendCharD0('B');    	// RRD3B - Emmarin
   #endif
 #else
  SendCharD0('R');
  SendCharD0('D');
  SendCharD0('3');
   #ifdef READER_MIFARE		// RD3M - Mifare
    SendCharD0('M');  
   #else
    SendCharD0('B');    	// RD3B - Emmarin
   #endif
 #endif

  SendCharD0('-');
  SendCharD0(toAscii(FW_VERSION_HI));
  SendCharD0('.');
  Int2Dec(FW_VERSION_LOW, &ver_buff[0]);
  
  if (ver_buff[2] != 0)					// potlaceni predrazene nuly ve verzi
   {
     SendCharD0(toAscii(ver_buff[2]));	// version 1.cislo
   }
  SendCharD0(toAscii(ver_buff[3]));  	// version 2.cislo	
  SendCharD0(toAscii(ver_buff[4]));  	// version 3.cislo

  #ifdef FORMAT_TYCO
   SendCharD0('(');
   SendCharD0('T');
   SendCharD0('Y');
   SendCharD0('C');
   SendCharD0('O');
   SendCharD0(')');
  #endif
  	
  Send_EOM();							// ukoncovaci retezec
		
}
#endif
#endif



// ************************************************************************* 
// RD3_SendPacket_InputState
// posle stav vstupu RRD3 - na dotaz
// ************************************************************************* 
#ifdef INTERFACE_EDK
#ifdef INTERFACE_RD3
#ifdef HW_SPEC_RRD3
void RD3_SendPacket_InputState(void)
{
 tUInt parameter;
 
  // pozadavek na stav INPUT1	
  // muze nastav dvoji situace, pokud dojde ke zmene vstupu, predava se parametr s cislem vstupu do funkce
  // pokud se jedna o pozadavek dany prikazem, predava se parametr z fronty uloh
  
  parameter = reqBuffer[appCB.task].parameter;	// prevzit parametr
 
  // nutno rozlisit pozadavek na samostatny vstup nebo na oba soucasne
  if((parameter == EDK_IN_INPUT1) || (parameter == (EDK_IN_INPUT1|EDK_IN_INPUT2)))
   {
	 if(EDK.input.state & EDK_IN_INPUT1)		// vstup IN1 aktivovan    
      {
	   SendCharD0('I');
       SendCharD0('1');
       SendCharD0('1');
      }
     else										// vstup IN1 neaktivni
      {
       SendCharD0('I');
       SendCharD0('1');
       SendCharD0('0');
      }

	  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  	  Send_EOM();
   }
   
  // pozadavek na stav INPUT2	
  if((parameter == EDK_IN_INPUT2) || (parameter == (EDK_IN_INPUT1|EDK_IN_INPUT2)))
   {
	 if(EDK.input.state & EDK_IN_INPUT2)		// vstup IN2 aktivovan    
      {
       SendCharD0('I');
       SendCharD0('2');
       SendCharD0('1');
      }
     else										// vstup IN2 neaktivni
      {
       SendCharD0('I');
       SendCharD0('2');
       SendCharD0('0');
      }

	  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  	  Send_EOM();
   }
}
 #endif
 #endif
 #endif



// ************************************************************************* 
// RD3_SendPacket_InputChange
// posle stav vstupu RRD3 - v pripade jejich zmeny
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef HW_SPEC_RRD3
void RD3_SendPacket_InputChange(void)
{
  // podle stavu IN1 vstupu poslat znak
  if(EDK.input.change & EDK_IN_INPUT1)			// zmena vstupu IN1
   {
   	 AddReq(PRIORITY_TASK_MID | PROCESS_RD3_INPUT_STATE, EDK_IN_INPUT1, 0);		// volat pres frontu s parametrem vstupu
   }

  // podle stavu vstupu poslat znak
  if(EDK.input.change & EDK_IN_INPUT2)			// zmena vstupu IN2
   {
   	 AddReq(PRIORITY_TASK_MID | PROCESS_RD3_INPUT_STATE, EDK_IN_INPUT2, 0);		// volat pres frontu s parametrem vstupu
   }
		
}
 #endif
 #endif
 #endif




// ************************************************************************* 
// RD3_SendPacket_Rele
// posle stav rele RRD3
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef HW_SPEC_RRD3
void RD3_SendPacket_Rele(void)
{
  SendCharD0('R');

  // podle stavu rele poslat znak
  if (EDK.output & EDK_OUT_RELAY)
   {
     SendCharD0('O');
   }
  else
   {
     SendCharD0('F');
   }

  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  Send_EOM();

		
}

 #endif
 #endif
 #endif



// ************************************************************************* 
// RD3_SendPacket_Ctrl
// ovladaci prikazy RRD3
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef HW_SPEC_RRD3

void RD3_SendPacket_Ctrl(void)
{
   

}
		
 #endif
 #endif
 #endif




// ************************************************************************* 
// RD3_SendPacket_KEY_LOAD_EE
// nahrati klice do EEPROM OK
// ************************************************************************* 
/*void RD3_SendPacket_ACK(tUInt error)
{
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef READER_MIFARE
 
   SendCharD0('C');  
   
   if ( error == NO_ERROR)
    {
	  SendCharD0('0');
      SendCharD0('0'); 
	}
   else
    {
      SendCharD0(toAscii(error>>4)); 
      SendCharD0(toAscii(error&0x0f)); 
	}

  Send_EOM();
  	 
 #endif
 #endif
 #endif
}
*/



// ************************************************************************* 
// RD3_SendPacket_NACK
// negativni odpoved - nepodporovany prikaz apod...
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef READER_MIFARE

void RD3_SendPacket_ERR(tUChar error)
{
   
  SendCharD0('E');
  
  SendCharD0(toAscii(error>>4)); 
  SendCharD0(toAscii(error&0x0f)); 
  	
  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  Send_EOM();
	
}

 #endif
 #endif
 #endif



// ************************************************************************* 
// RD3_Config_Send_ACK
// negativni odpoved - nepodporovany prikaz apod...
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3

void RD3_Config_Send_ACK(tUChar error)
{
  SendCharD0('C');
  
  SendCharD0(toAscii(error>>4)); 
  SendCharD0(toAscii(error&0x0f)); 
  	
  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  Send_EOM();

}

 #endif
 #endif

// ************************************************************************* 
// RD3_Info_Send_ACK
// odpovi uzivatelsky definovanym znakem na ostatni dotazy
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3

void RD3_Info_Send_ACK(tUChar znak, tUChar param)
{
   
  SendCharD0(znak);
  
  SendCharD0(toAscii(param>>4)); 
  SendCharD0(toAscii(param&0x0f)); 
  	
  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  Send_EOM();

}
 #endif
 #endif



// ************************************************************************* 
// RD3_SendPacket_Medium
// podrobnosti o typu RFID media
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3

void RD3_SendPacket_MediumType(void)
{
  #ifdef READER_EMMARIN
   SendCharD0('I');  
   SendCharD0('D');  
   SendCharD0('=');  
   SendCharD0(toAscii(gCard.cardId[0]>>4)); 		// EMMARIN ma prvni bajt MANUFACTURER ID
   SendCharD0(toAscii(gCard.cardId[0]&0x0f)); 
   Send_EOM();
  #endif

  #ifdef READER_MIFARE
   SendCharD0('A');  
   SendCharD0('T');  
   SendCharD0('Q');  
   SendCharD0('=');     
   SendCharD0(toAscii(gCard.card_type[0]>>4)); 		// MIFARE ATQ kod
   SendCharD0(toAscii(gCard.card_type[0]&0x0f)); 		
   SendCharD0(toAscii(gCard.card_type[1]>>4)); 		// MIFARE ATQ kod
   SendCharD0(toAscii(gCard.card_type[1]&0x0f)); 		
   SendCharD0(' ');  
   SendCharD0('S');  
   SendCharD0('A');  
   SendCharD0('K');  
   SendCharD0('=');     
   SendCharD0(toAscii(gCard.card_type[2]>>4)); 		// MIFARE SAK kod
   SendCharD0(toAscii(gCard.card_type[2]&0x0f)); 		
   Send_EOM();
  #endif
  
}

 #endif
 #endif


// ************************************************************************* 
// RD3_SendPacket_ReadAck
// posle 16 bajtu z nacteneho bloku karty
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef READER_MIFARE
void RD3_SendPacket_ReadAck(void)
{
  tUChar i;
  tUChar j;
  
  tUInt crcValue;
  tUChar msgLen;
  tUInt status;
  
  tUChar TxBuffer[64];
  
  my_memset (TxBuffer, 0, sizeof(TxBuffer) );
  j = 0;
  
  
  // volame stejnou funkci v pripade nacitani z karty, tak tady zablokujeme odesilani paketu v rezimu DUMP
  if(!(appCB.mode & MIFARE_DUMP_MODE))
   {
     // odpoved na cteni bloku
     TxBuffer[j++] = 'T';		

     // cislo cteneho bloku
     TxBuffer[j++]  = toAscii(RD3_packet.block>>4);
     TxBuffer[j++] = toAscii(RD3_packet.block&0x0f);
	
     // vysledek operace cteni
     TxBuffer[j++]  = toAscii(gCard.mif_status>>4); 
     TxBuffer[j++] = toAscii(gCard.mif_status&0x0f);


	// podle rezimu cteni - rozlisujeme cteni DATA bloku tj. cely obsah 16 bajtu
	// nebo v rezimu cteni VALUE bloku nacteme 4 bajty VALUE
     // pridat nactene data z bloku  
     if (gCard.mif_status == NO_ERROR)
      { 
		if (gCard.mif_rwmode == READ_DATA_BLOCK)						// v rezimu cteni DATA bloku odesleme 16 bajtu
         {
		   for(i=0; i<32; i+=2)
            {
             TxBuffer[j+i] = toAscii(gCard.sector_data[i/2]>>4);		//nutno posilat i data v ASCII
	         TxBuffer[j+i+1] = toAscii(gCard.sector_data[i/2]&0x0f);	//nutno posilat i data v ASCII
	        }
           j+=32;
		 }
		else if (gCard.mif_rwmode == READ_VALUE_BLOCK)					// v rezimu cteni VALUE bloku odesleme 4 bajty
		 {
		   // jelikoz VALUE je na karte ulozeno LSB bajt first, je potreba bajty prohodit
		   ValueBlockChangeOrder( &gCard.sector_data[0]);
		   for(i=0; i<8; i+=2)
            {
             TxBuffer[j+i] = toAscii(gCard.sector_data[i/2]>>4);		//nutno posilat i data v ASCII
	         TxBuffer[j+i+1] = toAscii(gCard.sector_data[i/2]&0x0f);	//nutno posilat i data v ASCII
	        }
           j+=8;
		 }
      }

     // spocitat CRC bloku dat
     crcValue = crc16_ccitt((tUChar*)&TxBuffer, j);		// zadat delku vcente crc (ktere zde neni)

     // ulozit CRC jako ASCII 
     TxBuffer[j++]  = toAscii(crcValue>>12); 
     TxBuffer[j++]  = toAscii(crcValue>>8); 
     TxBuffer[j++]  = toAscii(crcValue>>4); 
     TxBuffer[j++]  = toAscii(crcValue&0x000f);   

		  
     // odeslat blok dat na SCI
     for(i=0; i<j; i++)
      {
	    SendCharD0(TxBuffer[i]);
      }
  
     // ukoncovaci retezec podle typu zarizeni, "End of Message"
     Send_EOM();
   
   }			// konec MIFARE_DUMP_MODE;
   
   
//  EDK.sys_flag |= FLAG_RD3_PACKET_SENDED;		// flag paket byl odeslan, aby se zabranilo opakovanemu volani
  gCard.mif_status = NO_ERROR;					// smazat status pokud mel predtim chybu
  RestoreIdleLedState();						// obnovit puvodni stav LED

}
#endif
#endif
#endif
		


// ************************************************************************* 
// RD3_SendPacket_WriteAck
// posle 16 bajtu z nacteneho bloku karty
// ************************************************************************* 
#if defined INTERFACE_EDK || defined INTERFACE_RD3 || defined  READER_MIFARE
void RD3_SendPacket_WriteAck(void)
{
  tUChar i,j;
  tUInt crcValue;
  tUChar msgLen;
  tUInt status;
  
  tUChar TxBuffer[45];
  
  my_memset (TxBuffer, 0, sizeof(TxBuffer) );
  j = 0;
  
  
  // odpoved na zapis do bloku
  TxBuffer[j++] = 'P';		

  // cislo zapisovaneho bloku
  TxBuffer[j++]  = toAscii(RD3_packet.block>>4);
  TxBuffer[j++] = toAscii(RD3_packet.block);
	
  // vysledek operace zapis
  TxBuffer[j++] = toAscii(gCard.mif_status>>4); 
  TxBuffer[j++] = toAscii(gCard.mif_status&0x0f);


  // spocitat CRC bloku dat
  crcValue = crc16_ccitt(TxBuffer, j);

  // ulozit CRC jako ASCII 
  TxBuffer[j++]  = toAscii(crcValue>>12); 
  TxBuffer[j++]  = toAscii(crcValue>>8); 
  TxBuffer[j++]  = toAscii(crcValue>>4); 
  TxBuffer[j++]  = toAscii(crcValue&0x000f);   
  
  // odeslat blok dat na SCI
  for(i=0; i<j; i++)
   {
	 SendCharD0(TxBuffer[i]);
   }
  
  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  Send_EOM();

//  EDK.sys_flag |= FLAG_RD3_PACKET_SENDED;		// flag paket byl odeslan, aby se zabranilo opakovanemu volani
  gCard.mif_status = NO_ERROR;					// smazat status pokud mel predtim chybu
  RestoreIdleLedState();						// obnovit puvodni stav LED


  my_memset ( (tUChar*)&gRx[0], 0, sizeof (gRx));
}
#endif


// ************************************************************************* 
// RD3_SendPacket_EE_BLOCK
// posle 16 bajtu z nacteneho bloku EEPROM MFRC531
// ************************************************************************* 
 #ifdef INTERFACE_EDK
 #ifdef INTERFACE_RD3
 #ifdef READER_MIFARE

void RD3_SendPacket_EE_BLOCK( tUChar block, tUChar* data)
{
  tUChar i,j;
  tUInt crcValue;
  tUChar msgLen;
  tUInt status;
  tUChar TxBuffer[45];
  
  my_memset (TxBuffer, 0, sizeof(TxBuffer) );
  j = 0;
  
  // odpoved na zapis do bloku
  TxBuffer[j++] = 'P';		

  // cislo zapisovaneho bloku
  TxBuffer[j++]  = toAscii(block>>4);
  TxBuffer[j++] = toAscii(block);

  // data
  for (i=0; i<16; i++)
   {
    TxBuffer[j++] = toAscii(data[i]>>4); 
    TxBuffer[j++] = toAscii(data[i]);
   }
   
   
  // spocitat CRC bloku dat
  crcValue = crc16_ccitt(TxBuffer, j);

  // ulozit CRC jako ASCII 
  TxBuffer[j]  = toAscii(crcValue>>12); 
  TxBuffer[j+1]  = toAscii(crcValue>>8); 
  TxBuffer[j+2]  = toAscii(crcValue>>4); 
  TxBuffer[j+3]  = toAscii(crcValue&0x000f);   
  j+=4;
  
  // odeslat blok dat na SCI
  for(i=0; i<j; i++)
   {
	 SendCharD0(TxBuffer[i]);
   }
  
  // ukoncovaci retezec podle typu zarizeni, "End of Message"
  Send_EOM();
  
}
#endif
#endif
#endif  


