⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eldsmart.c

📁 AT Command for Modem G18 Motorola
💻 C
📖 第 1 页 / 共 2 页
字号:

		if ((hFile=OpenFile(FLASH_INTERNA, nm_file, MODO_ESCRITA, FILE_TYPE_RECORD, file_size,
									rec_size))>=0)
		{
			registros = file_size/rec_size;
			buffer = (UCHAR*)malloc(rec_size);
			sprintf(msg,"\n\rLimpando %ld registros",registros);
			WriteIO(msg,0,0);
			if (buffer!=NULL)
			{
				memset(buffer,0x00,rec_size);
				for (cont=0;cont<registros;cont++)
					WriteFile (hFile, buffer, cont, rec_size);

				free(buffer);
				WriteIO("\n\rArquivo inicializado",0,0);
			}
			else
				WriteIO("\n\rERRO MALLOC!!!",0,0);
		}
	}

	if (hFile<0)
	{
		Display(msg,sprintf(msg,"%s"," FLASH PROBLEMA "),0,0);
		Display(msg,sprintf(msg,"%s",nm_file),1,0);

		while (read_keyboard()!=K_CANC);
	}
	return hFile;
}


void iniSerialPIC (ULONG baud)
{
	/* libera reset do pic */
	HAL_SetIODir (IOPIN_ARTS4, 0);
	HAL_SetIOBit (IOPIN_ARTS4, 1);

	HAL_COMSetMode(PIC_SOCKET, COM_8BIT|COM_IN_DMA);
	HAL_COMSetEvent(PIC_SOCKET, 0);
	HAL_COMSetBR(PIC_SOCKET, baud);
	HAL_COMTxEnable(PIC_SOCKET, 1);
	HAL_COMRxEnable(PIC_SOCKET, 1);

	HAL_COMEnable(PIC_SOCKET);
}


BOOL pic_command(UCHAR slot, UCHAR *cmd, UCHAR *resp, UCHAR respSize)
{
	UCHAR picbuff[100], cont;
//	char msg[200];
   UCHAR dataRead=0xff/*ind*/;
   BOOL  status = FALSE;
   int   cmdsize;

   //Inicializa buffer de comunicacao com o PIC
   memset(picbuff,0x00,sizeof(picbuff));

   //Tamanho sempre na posicao zero;
   cmdsize = cmd[0];

	memcpy(picbuff,cmd,cmdsize);

	HAL_SetIOBit (IOPIN_ARTS4, slot);

	while(HAL_COMRxBytes(PIC_SOCKET,(char *)dataRead,1));

	//Calcula BCC
	for(cont=0;cont<cmdsize;cont++)
	  picbuff[cmdsize] ^= picbuff[cont];

	cont = 0;
	do
	{
		//Inicia transmissao enviando STX
		HAL_COMTxByte(PIC_SOCKET, STX);

		//Verifica se PIC enviou ACK
		dataRead = 0;
		rxChar(PIC_SOCKET, &dataRead, 1);
		if(/*!rxChar(PIC_SOCKET, &dataRead, 1) || */(dataRead != ACK))
		{
			cont++;
			continue;
		}
		//Envia o Comando ao PIC
		HAL_WriteCOM (PIC_SOCKET, (char *)picbuff, cmdsize+1); //cmdsize - incluindo BCC

		//Verifica se PIC enviou ACK
		dataRead = 0;
		rxChar(PIC_SOCKET, &dataRead, 1);
		if(/*!rxChar(PIC_SOCKET, &dataRead, 1) || */(dataRead != ACK))
			cont++;
		else
			if(get_pic_resp(resp,respSize))
				status = TRUE;
			else
				cont++;

	}while((cont < 3) && (!status));

//	memset(msg, 0x00, sizeof(msg));
//	HAL_WriteCOM (COM3_SOCKET, "\n\rLib ", 5);
//	for (ind=0; ind < (cmdsize+1); ind++)
//	   sprintf(msg+strlen(msg),"%02x ",picbuff[ind]);

//	HAL_WriteCOM (COM3_SOCKET, (char *)msg, strlen(msg));
	return status;
}


BOOL get_pic_resp(UCHAR *resp, UCHAR respSize)
{
   UCHAR cont, bcc, dataRead=0xff;
   BOOL status = FALSE;

   //Verifica se PIC enviou STX
   if((rxChar(PIC_SOCKET, &dataRead, 1) && dataRead==STX))
   {
	  HAL_COMTxByte(PIC_SOCKET, ACK);   //Envia ACK

	  //Recebe resposta
	  if (rxChar(PIC_SOCKET,resp,(UINT)respSize))
	  {
	     for(cont=0, bcc=0; cont<respSize-1; cont++)  //Verifica BCC
		    bcc ^= resp[cont];

	     if(bcc == resp[respSize-1])
		 {
	        HAL_COMTxByte(PIC_SOCKET, ACK);    //Envia ACK para o PIC
			status = TRUE;
		 }
		 else
			  HAL_COMTxByte(PIC_SOCKET, NACK);
	  }
   }
   return status;
}


int rxChar(int socket, UCHAR *dataRead, int bytesRead)
{
	int br;
	UCHAR cont;

	for(cont=0;cont<50;cont++)
	{
		if((br=HAL_COMRxBytes(socket,(char*)dataRead,bytesRead))!=0)
			return br;
		HAL_CPUWaitmS(1);
	}
	return 0;
}


BOOL initMultos(struct ICC_ASYNC_CCB *slot_icc, UCHAR slot, UCHAR *iccA)
{
	BOOL status = FALSE;

	if(ICC_AsyncOpen(slot_icc, slot) == 0)
		if(ICC_AsyncAtr(slot_icc,iccA) == 0)
			status = TRUE;

	return status;
}


BOOL send_apdu_command(struct ICC_ASYNC_CCB *slot_icc, int apduSize, UCHAR *apdu, UCHAR *resp, BOOL getresponse)
{
	BOOL status = FALSE;
	int apdugetsize = 5;
	UCHAR getresp[7];
	UCHAR getrespsize;
	/* variavel long que indica a posicao alem de 4 do buffer que contem APDU */
	int posapduresp = 0;

	if(ICC_ExchangeApdu(slot_icc, apdu, resp, apduSize) == 0)
	{
		status = TRUE;
	}
	/* Verifica se o APDU necessita de GET RESPONSE */
	if ((getresponse == TRUE) && (status == TRUE))
	{
		/* Deve ser feito um GET RESPONSE, depois de verificar resposta */
		if(resp[0]==0x02)
		{
			if(resp[1]== 0x61)
			{
				status = FALSE;
				getrespsize = resp[2];
				/* Deve-se verificar retorno de GET RESPONSE ate que
				* nao haja mais bytes a serem lidos
				*/
				memset(getresp, 0x00, sizeof(getresp));
				/* Campo CLA */
				getresp[0]=0x00;
				/* Campo INS */
				getresp[1]=0xC0;
				/* Campo P1 */
				getresp[2]=0x00;
				/* Campo P2 */
				getresp[3]=0x00;
				/* Campo Lc */
				//getresp[4]=0x00;
				/* Campo Data Field */
				//getresp[5]=0x00;

				while (getrespsize != 0)
				{
					/* Campo Le */
					//getresp[6]= getrespsize;
					getresp[4]= getrespsize;

					/* Envia comando send_apdu_command com GET RESPONSE */
					if(ICC_ExchangeApdu(slot_icc, getresp, &(resp[posapduresp]), apdugetsize) != 0)
						break;
					else
					{
						/* Verifica se status retornado foi OK */
						/* Incrementa posapduresp com o n.o de bytes que acabaram de ser lidos */
						posapduresp += getrespsize;
						if(resp[posapduresp + 1] != 0x61)
						{
							if (resp[posapduresp + 1] == 0x90)
								status = TRUE;
							break;
						}

						/* getrespsize recebe quanto bytes ainda faltam ser lidos */
						getrespsize = resp[posapduresp + 2];
					}//Fim do else que indica retorno OK de ICC_ExchangeApdu
				}//Fim do while
			}// Fim if(outbuff[5]== 0x61)
		}// Fim if(outbuff[4]==0x02)
	}//Fim do if(getresponse == TRUE)

	return status;
}

/*****************************************************************************/
/**********************  FUNCAO  ini_log_seq  ********************************/
/*****************************************************************************/
/*
void ini_log_seq(void)
{
	log_seq = 0;
}
*/
/*****************************************************************************/
/********************** FUNCAO set_next_log_rec ******************************/
/*****************************************************************************/
/*
BOOL set_next_log_rec(int  hf_log, int  hf_config, BOOL *log_cheio, int sizeStruct)
{
	BOOL status=FALSE;
	long cont;
	UINT aux_local;
	UCHAR *buffer;

 	buffer = (UCHAR*)malloc(sizeStruct);

	cont = next_log_rec;
	ReadFile (hf_config, (UCHAR*)&config_lib, 0, sizeof(config_lib));

	while(cont < n_max_log)
	{
	  ReadFile(hf_log, buffer, cont, 2);
	  memcpy(&aux_local, buffer, 2);

	  if((aux_local >= config_lib.last_trans_log) && (aux_local != 0))
	  {
		 if(cont+1 >= n_max_log)
		 {
	  		cont = 0;
		 }
		 else
		 {
			 cont++;
		 }
	  }

	  else
	  {
       	 if((cont) >= (n_max_log - 1))
		 {
			 ReadFile(hf_log, buffer, 0, 2);
		 }
		 else
		 {
		     ReadFile(hf_log, buffer, cont+1, 2);
		 }
		 memcpy(&aux_local, buffer, 2);
		 if((aux_local >= config_lib.last_trans_log) && (aux_local != 0))
		 {
			 *log_cheio = TRUE;
		 }
		 status = TRUE;
		 break;
	  }//fim do else
	  if(cont == next_log_rec)
	  		break;
	}//fim while

	if(status)
	{
	  next_log_rec = cont;
	}
	else
	{
	  *log_cheio = TRUE;
	}
   	return status;
}
*/

/*****************************************************************************/
/********************** FUNCAO   set_next_log   ******************************/
/*****************************************************************************/
/*
void set_next_log(int  hf_log, int sizeStruct)
{
	UINT cont;
	UINT lst_seq = 0;
	UINT aux_local;
	UCHAR *buffer;

	buffer = (UCHAR*)malloc(sizeStruct);

	for(cont=0;cont<n_max_log;cont++)
	{
		ReadFile(hf_log, buffer, cont, 2);
		memcpy(&aux_local, buffer, 2);

		if (lst_seq<=aux_local)

			lst_seq = log_seq = aux_local;
	}

}
*/
/*****************************************************************************/
/*********************  FUNCAO    save_log  **********************************/
/* Substituida */
/*****************************************************************************/
/*
BOOL save_log_in(UCHAR *log_struct, int  hf_log, int hf_config, BOOL *log_cheio, int tamanho)
{
	BOOL status = FALSE;

	if(set_next_log_rec(hf_log, hf_config, log_cheio, tamanho))
	{
	  log_seq += 1;
	  memcpy(log_struct,&log_seq,2);
	  if(WriteFile(hf_log, (UCHAR*)log_struct, next_log_rec, tamanho) >= 0)
		 status = TRUE;
	}

	return status;
}
*/
/*****************************************************************************/
/*********************    FUNCAO ini_log    **********************************/
/*-----------------10/1/2003 9:06AM-----------------
 * Substituida *
 * --------------------------------------------------*/
/*****************************************************************************/
/*
void ini_log(ULONG log_rec_pos, ULONG max_log)
{
	next_log_rec = log_rec_pos;
	log_seq = 0;
	n_max_logs = max_log;
}
*/
// inicio atualizacao

void ini_log(int hf_log, int sizeStruct, int last_log_rec_send, int max_log)
{
   int cont;
   int registro;
   UCHAR *buffer;
   int last_seq_log_send, next_seq;

   buffer = (UCHAR*)malloc(sizeStruct);

   next_log_rec = -1;
   n_max_log = max_log;
   ReadFile(hf_log, buffer, last_log_rec_send, 2);
   memcpy(&last_seq_log_send, buffer, 2);
   if (last_seq_log_send == 0)
   {
	   next_log_rec = 0;
	   next_log_seq = 0;
	   return;
   }

   for(cont=1; cont < n_max_log; cont++)
   {
	  registro = (last_log_rec_send + cont)%n_max_log; // obtera o sequencial do proximo registro
	  ReadFile(hf_log, buffer, registro, 2);
	  memcpy(&next_seq, buffer, 2);

	  if((next_seq < last_seq_log_send) || (next_seq == 0))// compara o sequencial do proximo registro com o ultimo sequencial enviado
	  {
		 next_log_rec = registro; // salvo o proximo registro livre
	     registro = (last_log_rec_send + (cont-1))%n_max_log; // obtera o sequencial do registro anterior
	     ReadFile(hf_log, buffer, registro, 2);
	     memcpy(&next_log_seq, buffer, 2);
	     break;
	  }
   }
}

BOOL check_log(int  hf_log, int hf_config, BOOL *log_cheio, int sizeStruct)
{
	BOOL status=FALSE;
	ULONG cont;
	int seq_local;
	UCHAR *buffer;

	buffer = (UCHAR*)malloc(sizeStruct);

	if(next_log_rec < 0)
	{
	   *log_cheio = TRUE;
	   return status;
	}

	cont = next_log_rec;
	cont = (cont+1)%n_max_log;

   ReadFile(hf_log, buffer, cont, 2);
	memcpy(&seq_local, buffer, 2);

	ReadFile (hf_config, (UCHAR*)&config_lib, 0, sizeof(config_lib));

	if((seq_local < config_lib.last_trans_log) || (seq_local == 0))
	   *log_cheio = FALSE;
	else
	   *log_cheio = TRUE;

	status = TRUE;
	return status;
}


BOOL save_log_in(UCHAR *log_struct, int  hf_log, int hf_config, BOOL *log_cheio, int tamanho)
{
	BOOL status = FALSE;

	if(check_log(hf_log, hf_config, log_cheio, tamanho))
	{
		next_log_seq++;
		memcpy(log_struct,&next_log_seq,2);

		if(WriteFile(hf_log, (UCHAR*)log_struct, next_log_rec, tamanho) >= 0)
		{
			next_log_rec = (next_log_rec+1)%n_max_log;
			status = TRUE;
		}
		else
			next_log_seq--;
	}

	return status;
}


BOOL erase_log(int hf_log, ULONG max_log, int sizeStruct)
{
	BOOL status = FALSE;
	UCHAR *aux_log;
	int cont;

	aux_log = (UCHAR*)malloc(sizeStruct);

	memset(aux_log, 0x00, sizeStruct);

	for(cont=0; cont < max_log; cont++)
		if(WriteFile(hf_log, aux_log, cont, sizeStruct) < 0)
		{
			status = FALSE;
			break;
		}
		else
			status = TRUE;

	if(status)
	{
		next_log_rec = 0;
		next_log_seq = 0;
	}

	return status;
}

// fim atualizacao



void rev_byte(UCHAR *var, UCHAR bytes)
{
	UCHAR cont;
	UCHAR temp[4];

	memset(temp,0x00,sizeof(temp));

	for (cont=0;cont<bytes;cont++)
		temp[cont] = var[(bytes-cont)-1];

	memcpy(var,temp,bytes);
}


BOOL get_picKey(UCHAR setor, UCHAR cardType, UCHAR keyType, UCHAR *serialCard, UCHAR *picKey)
{
	BOOL status = FALSE;
	UCHAR cod_setor[10] = {0, 0, 0, 1, 1, 2, 2, 2, 2, 3};
	UCHAR picCmd[14], picResp[11], ind = 0;

	memset(picCmd,  0x00, sizeof(picCmd));
	memset(picResp, 0x00, sizeof(picResp));

	picCmd[ind++] = 13;
	picCmd[ind++] = 'G';
	picCmd[ind++] = cod_setor[setor];
	picCmd[ind++] = cardType;
	picCmd[ind++] = keyType;
	memcpy(&(picCmd[ind]), serialCard, 8);

	if(pic_command(1, picCmd, picResp,11) == TRUE)
		if (picResp[1]=='G')
		{
			memcpy(picKey, &(picResp[2]), 8);
			status = TRUE;
		}
	return status;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -