📄 reader.c
字号:
/* ============================================================================ Project Name : jayaCard TCK Module Name : proto/tck/cos/common/reader.c Version : $Id: reader.c,v 1.31 2004/04/23 21:33:44 dgil Exp $ Description: common sources of a testing reader The Original Code is jayaCard TCK code. The Initial Developer of the Original Code is Gilles Dumortier. Portions created by the Initial Developer are Copyright (C) 2002-2004 the Initial Developer. All Rights Reserved. Contributor(s): Permission is granted to any individual to use, copy, or redistribute this software so long as all of the original files are included unmodified, that it is not sold for profit, and that this copyright notice is retained. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. History Rev Description 021503 dgil wrote it from scratch 072203 dgil remove specific message for Type A protocol ============================================================================*/#include "precomp.h"/* ============================================================================ send_simumsg_to_card() send a message to the card ========================================================================= */jresult send_simumsg_to_card(SOCKET s,jbyte interf,char* msg,jword param){ SIMUMSG* pmsg; jword i; jword len; jresult res; res = JY_OK; if (strcmp(msg,"POWERON")==0) { /* power-on the card */ pmsg = new_simumsg(SIMUMSG_POWERON,interf); if (pmsg==NULL) return JY_ALLOCATION_FAILURE;send: res = send_simumsg(s,pmsg); free_simumsg(pmsg); return res; } else if (strcmp(msg,"POWEROFF")==0) { /* power-off the card */ pmsg = new_simumsg(SIMUMSG_POWEROFF,interf); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"RESET")==0) { /* RESET the card */ pmsg = new_simumsg(SIMUMSG_RESET,interf); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"EXIT_SIMULATOR")==0) { /* stop and exit the simulator */ pmsg = new_simumsg(SIMUMSG_CLOSE,SIMUMSG_CLOSE_AND_EXIT); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"CLOSE_CONNECTION")==0) { /* stop and exit the simulator */ pmsg = new_simumsg(SIMUMSG_CLOSE,SIMUMSG_CLOSE_AND_WAIT); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"UNLOCK_BOOTSTRAP")==0) { /* unlock the BOOTSTRAP lock */ pmsg = new_simumsg(SIMUMSG_UNLOCK,0x00); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"UNLOCK_INIT")==0) { /* unlock the INIT lock */ pmsg = new_simumsg(SIMUMSG_UNLOCK,0x01); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"UNLOCK_PERSO")==0) { /* unlock the PERSO lock */ pmsg = new_simumsg(SIMUMSG_UNLOCK,0x02); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"UNLOCK_BLOCKED")==0) { /* unlock the BLOCKED lock */ pmsg = new_simumsg(SIMUMSG_UNLOCK,0x03); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (strcmp(msg,"SAVE_EEPROM")==0) { /* save the eeprom content to file now */ pmsg = new_simumsg(SIMUMSG_SAVE_EEPROM,0x00); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; pmsg->h.param = param; goto send; } else if (strcmp(msg,"SAVE_RAM")==0) { /* save the ram content to file now */ pmsg = new_simumsg(SIMUMSG_SAVE_RAM,0x00); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; pmsg->h.param = param; goto send; } else if (strcmp(msg,"LOAD_EEPROM")==0) { /* load the eeprom content from file now */ pmsg = new_simumsg(SIMUMSG_LOAD_EEPROM,0x00); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; pmsg->h.param = param; goto send; } else if (strcmp(msg,"CLEAR_EEPROM")==0) { /* clear the eeprom content now */ pmsg = new_simumsg(SIMUMSG_CLEAR_EEPROM,0x00); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; goto send; } else if (interf==TCK_INTERFACE_CONTACT) { /* send a T=0 sequence */ pmsg = new_simumsg(SIMUMSG_T0_CHAR,0x00); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; for (i=0;i<strlen(msg);i+=2) { pmsg->h.param = hexa2byte(msg[i],msg[i+1]); res = send_simumsg(s,pmsg); if (res!=JY_OK) break; } free_simumsg(pmsg); return res; } /* otherwise */ /* send a T=CL block */ len = strlen(msg); if ((len&1)==1) { fprintf(stderr,"*** invalid TCL block - strlen must be even ! \n"); return JY_FAILED; } len = len>>1; HAL_TCL_INIT_CRC(); pmsg = new_simumsg(SIMUMSG_TCL_FRAME,len+2); if (pmsg==NULL) return JY_ALLOCATION_FAILURE; for (i=0;i<len;i++) { pmsg->buf[i] = hexa2byte(msg[2*i],msg[(2*i)+1]); HAL_TCL_CRC(pmsg->buf[i]); /* LOG4("TEMP","%d 0x%.2X <- %c%c",pmsg->buf[i],pmsg->buf[i],msg[2*i],msg[(2*i)+1]); */ } pmsg->buf[i++] = HAL_TCL_LOCRC(); pmsg->buf[i++] = HAL_TCL_HICRC(); /* param contains the baudrate indication */ pmsg->h.param += param; goto send;}/* ============================================================================ receive_simumsg_from_card() receive a message from the card note: the parameter len is to collect len characters from the T=0 protocol and this value will be ignored for the other message types. ========================================================================= */jresult receive_simumsg_from_card(SOCKET s,char** msg,jword param){ SIMUMSG* pmsg = NULL; jdword i; char* amsg; jresult res; jword len; /* receive a message from the card */ res = receive_simumsg(s,&pmsg); if (res!=JY_OK) goto err; if (pmsg->h.kindof==SIMUMSG_T0_CHAR) { len = param; if (len==0) len=1; *msg = (char*)malloc((1+(2*len))*sizeof(char)); if (*msg==NULL) { res = JY_ALLOCATION_FAILURE; goto err; } amsg = *msg; for (i=0; i<len; i++) { if (i>0) { res = receive_simumsg(s,&pmsg); if (res!=JY_OK) goto err; if (pmsg->h.kindof!=SIMUMSG_T0_CHAR) { res = JY_UNEXPECTED; goto err; } } byte2hexa( (jbyte) (SIMUMSG_TCL_FRAME_LEN(pmsg->h.param)),amsg); amsg+=2; /*printf("%d %d %s\n",len,i,*msg); */ free_simumsg(pmsg); } return JY_OK; } if (pmsg->h.kindof==SIMUMSG_TCL_FRAME) { HAL_TCL_INIT_CRC(); /* checking received baudarte against expected baudrate */ if ( SIMUMSG_TCL_FRAME_BAUDRATE(pmsg->h.param) != SIMUMSG_TCL_FRAME_BAUDRATE(param)) { fprintf(stderr, "*** msg received with baudrate %d while expecting baudrate %d", SIMUMSG_TCL_FRAME_BAUDRATE(pmsg->h.param), SIMUMSG_TCL_FRAME_BAUDRATE(param) ); res = JY_PROTOCOL_ERROR; goto err; } len = SIMUMSG_TCL_FRAME_LEN( pmsg->h.param ); *msg = (char*)malloc((1+2*len)*sizeof(char)); if (*msg==NULL) { res = JY_ALLOCATION_FAILURE; goto err; } for (i=0;i<len;i++) { HAL_TCL_CRC(pmsg->buf[i]); byte2hexa(pmsg->buf[i],&(*msg)[2*i]); } if ((HAL_TCL_LOCRC()!=0) || (HAL_TCL_HICRC()!=0)) { fprintf(stderr,"*** CRC error receiving TCL block ! \n"); res = JY_CRC_ERROR; goto err; } free_simumsg(pmsg); return JY_OK; } /* unsupported kind of block ! */ res = JY_UNEXPECTED;err: /* some error : be sure to free the message */ if (pmsg!=NULL) free_simumsg(pmsg); return res;}/* ============================================================================ That's all folks ! ========================================================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -