📄 simureader.c
字号:
/* ============================================================================ simureader.c ========================================================================= *//* */#include "platform.h"#include "socket.h"#include "exp_simumsg.h"/* ============================================================================ hexa2byte() note that c2 can be \0 ========================================================================= */jbyte hexa2byte(char c1,char c2){ jbyte b = 0x00; if ((c1>='0') && (c1<='9')) { b = c1 - '0'; } if ((c1>='a') && (c1<='f')) { b = c1 - 'a' + 10; } if ((c1>='A') && (c1<='F')) { b = c1 - 'A' + 10; } if ((c2>='0') && (c2<='9')) { b |= (c2 - '0')<<4; } if ((c2>='a') && (c2<='f')) { b |= (c2 - 'a' + 10)<<4; } if ((c2>='A') && (c2<='F')) { b = (c2 - 'A' + 10)<<4; } return b;}jbyte __byte[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };void byte2hexa(jbyte b,char* s){ s[0] = __byte[b>>4]; s[1] = __byte[b&0x0F]; s[2] = 0x00;}/* ============================================================================ send_simumsg_to_card() send a message to the card ========================================================================= */jresult send_simumsg_to_card(SOCKET s,jbyte interf,char* msg){ SIMUMSG* pmsg; jdword i; jresult ret; if (strcmp(msg,"POWERON")==0) { /* power-on the card */ pmsg = new_simumsg(SIMUMSG_POWERON,interf); if (pmsg==NULL) return JY_FAILED; return send_simumsg(s,pmsg); } else if (strcmp(msg,"POWEROFF")==0) { /* power-off the card */ pmsg = new_simumsg(SIMUMSG_POWEROFF,interf); if (pmsg==NULL) return JY_FAILED; return send_simumsg(s,pmsg); } else if (interf==IF_TYPE_CONTACT) { /* send a T=0 sequence */ pmsg = new_simumsg(SIMUMSG_T0_CHAR,0x00); if (pmsg==NULL) return JY_FAILED; for (i=0;i<strlen(msg);i+=2) { pmsg->param = hexa2byte(msg[i],msg[i+1]); if (JY_OK != (ret = send_simumsg(s,pmsg))) return ret; } free_simumsg(pmsg); return JY_OK; } /* otherwise */ /* send a T=CL block */ pmsg = new_simumsg(SIMUMSG_TCL_BLOCK,strlen(msg)); if (pmsg==NULL) return JY_FAILED; for (i=0;i<strlen(msg);i+=2) { pmsg->pdata[i] = hexa2byte(msg[i],msg[i+1]); } if (JY_OK != (ret = send_simumsg(s,pmsg))) return ret; free_simumsg(pmsg); return JY_OK;}/* ============================================================================ receive_simumsg_from_card() receive a message from the card ========================================================================= */jresult receive_simumsg_from_card(SOCKET s,char** msg){ SIMUMSG* pmsg; jdword i; jresult ret; /* receive a message from the card */ if (JY_OK != (ret = receive_simumsg(s,&pmsg))) return ret; if (pmsg->kindof==SIMUMSG_T0_CHAR) { *msg = (char*)malloc(3*sizeof(char)); if (*msg==NULL) return JY_FAILED; byte2hexa(pmsg->param,*msg); return JY_OK; } if (pmsg->kindof==SIMUMSG_TCL_BLOCK) { *msg = (char*)malloc((1+pmsg->param)*sizeof(char)); if (*msg==NULL) return JY_FAILED; for (i=0;i<pmsg->param;i++) { byte2hexa(pmsg->pdata[i],&(*msg)[2*i]); } return JY_OK; } /* unsupported kind of block ! */ return JY_FAILED;}/* ============================================================================ ========================================================================= */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -