int IsCardReady(Communicator& comm,int& cardA,int& cardB,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x02; int dataLen=0; BYTE tempBuffer[16]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,64,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if((SRBuffer[4]&(0x04))!=0) cardA=1; else cardA=0; if((SRBuffer[4]&(0x08))!=0) cardB=1; else cardB=0; if(SRBuffer[2]==0) return 0; else return SCERR_OPERROR; }
int GetScriptData(Communicator& comm,int offset,unsigned char bytes,SmartCom::string& strData,int macNo) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x0d; int dataLen=5; BYTE tempBuffer[8]; BYTE SRBuffer[300]; tempBuffer[0]=(BYTE)(offset>>24); tempBuffer[1]=(BYTE)((offset>>16)&0x000000ff); tempBuffer[2]=(BYTE)((offset>>8)&0x000000ff); tempBuffer[3]=(BYTE)(offset&0x000000ff); tempBuffer[4]=bytes; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,300,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; char t[4]; strData=""; for(int i=0;i<SRBuffer[3];i++) { sprintf(t,"%02X",SRBuffer[4+i]); strData+=t; if(i!=SRBuffer[3]-1) strData+=" "; } if(SRBuffer[2]==0) return 0; else return SCERR_OPERROR; }
int GetDevIDAndMacNo(Communicator& comm,SmartCom::string& devID,int& macNo) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; int cmd=0xe1; int dataLen=0; BYTE tempBuffer[1]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(255,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,64,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; char t[8]; //将设备ID十六进制转换成字符串 devID=""; for(int i=4;i<11;i++) { sprintf(t,"%02X",*(SRBuffer+i)); devID+=t; if(i!=10) { devID+=" "; } } macNo=*(SRBuffer+11); if(SRBuffer[2]==0) return 0; else return SCERR_OPERROR; }
int ExcuteMulAPDUAsyn(Communicator& comm,int cmdNum,int card/* =1 */,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x0c; int dataLen=3; BYTE tempBuffer[4]; BYTE SRBuffer[32]; tempBuffer[0]=card; tempBuffer[1]=(cmdNum>>8); tempBuffer[2]=(cmdNum&0x00ff); int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,32,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if(0==SRBuffer[2]) return 0; else return SCERR_OPERROR; }
int ResetCard(Communicator& comm,SmartCom::string& retCode,int card /* =1 */,int macNo/* =255 */) { retCode=""; if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x03; if(2==card) cmd=0x05; int dataLen=0; BYTE tempBuffer[4]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int ret,recLen; CommProcess(comm,SRBuffer,64,packetLen,ret,recLen); if(recLen<0) return SCERR_TIMEOUT; for(int i=0;i<SRBuffer[3];i++) { sprintf((char*)tempBuffer,"%02X",SRBuffer[i+4]); tempBuffer[2]='\0'; retCode+=(char*)tempBuffer; } if(0x3b==SRBuffer[4]||0x3f==SRBuffer[4]) return 0; else return SCERR_OPERROR; }
int GetChipID(Communicator& comm,char* chipID,int len,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x01; int dataLen=0; BYTE tempBuffer[16]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,64,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; sprintf((char*)tempBuffer,"%02X",*(SRBuffer+4)); tempBuffer[2]='\0'; assert(len>2); strcpy(chipID,(char*)tempBuffer); if(SRBuffer[2]==0) return 0; else return SCERR_OPERROR; }
int DownloadBin(Communicator& comm,int flag,const char* bin,unsigned int len,int macNo) { if(NOT_CONNECT==comm.m_nCommtype) return -2; if(macNo>255||macNo<1) return -3; int cmd=0x0e; if(2==flag) cmd=0x0f; int dataLen=0; BYTE tempBuffer[150]; BYTE SRBuffer[150]; //每次下载m字节 const int m=128; unsigned long readNum=0; UINT filePos=0; unsigned int i=0; bool loop=true; while(loop) { if(i+m<len) { memcpy(tempBuffer+4,bin+i,m); i+=m; readNum=m; } else { int left=len-i; if(0==left) break; memcpy(tempBuffer+4,bin+i,left); loop=false; readNum=left; } dataLen=4+readNum; //填充下载命令的数据项 tempBuffer[0]=filePos>>24;//文件偏移地址的最高字节 tempBuffer[1]=(filePos>>16)&0x000000ff;//文件偏移地址的次高字节 tempBuffer[2]=(filePos>>8)&0x000000ff;//文件偏移地址的低字节 tempBuffer[3]=filePos&0x000000ff;//文件偏移地址的最低字节 int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,150,packetLen,retCode,recLen); if(recLen<0) { return -1; } if(0!=SRBuffer[2]) { return -4; } filePos+=readNum; } return 0; }
int ModifyCardBraudRate(Communicator& comm,int braudRate,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x08; int dataLen=1; BYTE tempBuffer[4]; BYTE SRBuffer[32]; int index=0; switch(braudRate) { case 115200: index=1; break; case 57600: index=2; break; case 38400: index=3; break; case 28800: index=4; break; case 23040: index=5; break; case 19200: index=6; break; case 14400: index=8; break; case 12800: index=9; break; case 9600: index=12; break; default: return SCERR_INVALID_DATA; } tempBuffer[0]=index; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,32,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if(0==SRBuffer[2]&&index==SRBuffer[4]) return 0; else return SCERR_OPERROR; }
int GetCardBraudRate(Communicator& comm,int& braudRate,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x09; int dataLen=0; BYTE tempBuffer[4]; BYTE SRBuffer[32]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,32,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; switch(*(BYTE*)(SRBuffer+4)) { case 1: braudRate=115200; break; case 2: braudRate=57600; break; case 3: braudRate=38400; break; case 4: braudRate=28800; break; case 5: braudRate=23040; break; case 6: braudRate=19200; break; case 8: braudRate=14400; break; case 9: braudRate=12800; break; case 12: braudRate=9600; break; default: return SCERR_INVALID_DATA; } return 0; }
int CardApdu(Communicator& comm,const char* apdu,SmartCom::string& retCode,int card /* =1 */,int macNo/* =255 */) { retCode=""; if(NOT_CONNECT==comm.m_nCommtype) { return SCERR_INVALID_COMM; } if(macNo>255||macNo<1) { return SCERR_MACNO_OUTRANGE; } int cmd=0x04; if(2==card) cmd=0x06; int len=strlen(apdu); int dataLen=len/2; BYTE tempBuffer[1024]; BYTE SRBuffer[1024]; assert(len<1000); memcpy((char*)SRBuffer,apdu,len); //去掉字符串中的空格 //int len=RemBlankSpace((char*)SRBuffer,strlen(apdu)); //将字符串表示的十六进制值转换为数值 SVC_DSP_2_HEX(SRBuffer,tempBuffer,dataLen); int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int ret,recLen; CommProcess(comm,SRBuffer,1024,packetLen,ret,recLen); if(recLen<0) return SCERR_TIMEOUT; //填充retCode for(int i=0;i<SRBuffer[3];i++) { sprintf((char*)tempBuffer,"%02X",SRBuffer[4+i]); tempBuffer[2]='\0'; retCode+=(char*)tempBuffer; } return 0; }
int ShutdownCard(Communicator& comm,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x07; int dataLen=0; BYTE tempBuffer[4]; BYTE SRBuffer[32]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,32,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if(0==SRBuffer[2]) return 0; else return SCERR_OPERROR; }
int CheckBatchResult(Communicator& comm,SmartCom::string& retCode,int macNo/* =255 */) { retCode=""; if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x10; int dataLen=0; BYTE tempBuffer[4]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int ret,recLen; CommProcess(comm,SRBuffer,64,packetLen,ret,recLen); if(recLen<0) return SCERR_TIMEOUT; if(SRBuffer[3]>=3) { for(int i=0;i<SRBuffer[3]-2;i++) { sprintf((char*)tempBuffer,"%02X",SRBuffer[4+i+2]); tempBuffer[2]='\0'; retCode+=(char*)tempBuffer; } return (SRBuffer[4]<<8)+SRBuffer[5]; } else { for(int i=0;i<SRBuffer[3];i++) { sprintf((char*)tempBuffer,"%02X",SRBuffer[4+i]); tempBuffer[2]='\0'; retCode+=(char*)tempBuffer; } return 0; } }
static portTASK_FUNCTION( vCommTask, pvParameters ) { uint8_t buf[nRF_PLOAD_WIDTH]; (void)pvParameters; nRF_Init(); nrf_rx_mode(); vTaskDelay(1); for(;;) { if (nrf_start_rx(buf, nRF_PLOAD_WIDTH) == RX_OK) { vParTestToggleLED(6); CommProcess(buf); buf[0] |= 0x80; nrf_start_tx(buf, nRF_PLOAD_WIDTH); vParTestToggleLED(6); } memset(buf, 0, nRF_PLOAD_WIDTH); } }
int SetMacNoByDevID(Communicator& comm,const char* devID,int macNo) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; //判断要设置的机号与原来机号是否一致 SmartCom::string str; int tempMacNo=0; if(0!=GetDevIDAndMacNo(comm,str,tempMacNo)) return SCERR_TIMEOUT; if(tempMacNo==macNo) return 0; int cmd=0xe3; int dataLen=8; BYTE tempBuffer[64]; BYTE SRBuffer[64]; assert(strlen(devID)<50); strcpy((char*)SRBuffer,devID); //去掉字符串中的空格 RemBlankSpace((char*)SRBuffer,strlen((char*)SRBuffer)); //将字符串表示的十六进制值转换为数值 SVC_DSP_2_HEX(SRBuffer,tempBuffer,7); tempBuffer[7]=macNo; int packetLen=PrepareBytes(255,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,64,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if(SRBuffer[2]==0) return SC_SUCCESS; else return SCERR_OPERROR; }
int ModifyCardPower(Communicator& comm,int power,int card/* =1 */,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x0a; if(2==card) cmd=0x0b; int dataLen=1; BYTE tempBuffer[4]; BYTE SRBuffer[32]; tempBuffer[0]=power-1; int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,32,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if(SRBuffer[4]==power-1) return 0; else return SCERR_OPERROR; }
int DownloadFile(Communicator& comm,int flag,const char* fileName,int macNo/* =255 */) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(macNo>255||macNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0x0e; if(2==flag) cmd=0x0f; int dataLen=0; BYTE tempBuffer[150]; BYTE SRBuffer[150]; //每次下载m字节 const int m=128; //打开文件 //FILE* aFile=fopen(fileName,"rb"); HANDLE hFile=CreateFileA(fileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,0); //if(NULL==aFile) //return -5; if(INVALID_HANDLE_VALUE==hFile) return SCERR_OPENFILE_FAIL; unsigned long readNum=0; UINT filePos=0; BOOL bReturn=FALSE; OVERLAPPED olp; olp.Offset=0; olp.OffsetHigh=0; olp.hEvent=0; //while((readNum=fread(tempBuffer+4,1,m,aFile))!=0) while(true) { bReturn=ReadFile(hFile,tempBuffer+4,m,&readNum,&olp); if(readNum==0) break; if(bReturn==FALSE) return SCERR_OPERROR; dataLen=4+readNum; //填充下载命令的数据项 tempBuffer[0]=filePos>>24;//文件偏移地址的最高字节 tempBuffer[1]=(filePos>>16)&0x000000ff;//文件偏移地址的次高字节 tempBuffer[2]=(filePos>>8)&0x000000ff;//文件偏移地址的低字节 tempBuffer[3]=filePos&0x000000ff;//文件偏移地址的最低字节 int packetLen=PrepareBytes(macNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,150,packetLen,retCode,recLen); if(recLen<0) { //fclose(aFile); CloseHandle(hFile); return SCERR_TIMEOUT; } if(0!=SRBuffer[2]) { //fclose(aFile); CloseHandle(hFile); return SCERR_OPERROR; } filePos+=readNum; olp.Offset+=readNum; } //fclose(aFile); CloseHandle(hFile); return 0; }
int GetAppVerAndDevType(Communicator& comm,SmartCom::string& appVersion,SmartCom::string& devType, int MacNo) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(MacNo>255||MacNo<1) return SCERR_MACNO_OUTRANGE; int cmd=0xe4; int dataLen=0; BYTE tempBuffer[16]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(MacNo,cmd,tempBuffer,&dataLen,SRBuffer); appVersion=""; devType=""; //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,64,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; if(SRBuffer[2]!=0) return SCERR_OPERROR; char timestr[32]; int year,mon,day,hour; year=*(SRBuffer+6)>>4; year=year*10+(*(SRBuffer+6)&0xf); mon=*(SRBuffer+7)>>4; mon=mon*10+(*(SRBuffer+7)&0xf); day=*(SRBuffer+8)>>4; day=day*10+(*(SRBuffer+8)&0xf); hour=*(SRBuffer+9)>>4; hour=hour*10+(*(SRBuffer+9)&0xf); sprintf(timestr,"%d年%d月%d日%d时",year,mon,day,hour); appVersion=timestr; char t[8]; sprintf(t,"%02X",*(SRBuffer+4)); devType+=t; sprintf(t,"%02X",*(SRBuffer+5)); devType+=t; switch(*(BYTE*)(SRBuffer+5)) { case 1: devType+="(磁卡读写器)"; break; case 2: devType+="(接触式同步卡,存储卡和加密卡)"; break; case 3: devType+="(接触式CPU卡读写器)"; break; case 4: devType+="(非接触式存储卡读写器)"; break; case 5: devType+="(非接触式CPU卡读写器)"; break; case 6: devType+="(双界面卡读写器)"; break; } return 0; }
int GetAppVerAndDevType(Communicator& comm,char* appVersion,int Verlen,char* devType,int typeLen, int MacNo) { if(NOT_CONNECT==comm.m_nCommtype) return SCERR_INVALID_COMM; if(MacNo>255||MacNo<1) return SCERR_MACNO_OUTRANGE; assert(Verlen>30); assert(typeLen>40); int cmd=0xe4; int dataLen=0; BYTE tempBuffer[16]; BYTE SRBuffer[64]; int packetLen=PrepareBytes(MacNo,cmd,tempBuffer,&dataLen,SRBuffer); //发送命令 int retCode,recLen; CommProcess(comm,SRBuffer,64,packetLen,retCode,recLen); if(recLen<0) return SCERR_TIMEOUT; char year[8],month[8],day[8],hour[8]; int j=0; appVersion[0]='\0'; sprintf(year,"%u",*(SRBuffer+4)); sprintf(month,"%u",*(SRBuffer+5)); sprintf(day,"%u",*(SRBuffer+6)); sprintf(hour,"%u",*(SRBuffer+7)); strcat(appVersion,year); strcat(appVersion,"年"); strcat(appVersion,month); strcat(appVersion,"月"); strcat(appVersion,day); strcat(appVersion,"日"); strcat(appVersion,hour); strcat(appVersion,"时"); char t[8]; j=0; devType[0]='\0'; sprintf(t,"%02X",*(SRBuffer+8)); strcat(devType,t); sprintf(t,"%02X",*(SRBuffer+9)); strcat(devType,t); switch(*(BYTE*)(SRBuffer+9)) { case 1: strcat(devType,"(磁卡读写器)"); break; case 2: strcat(devType,"(接触式同步卡,存储卡和加密卡)"); break; case 3: strcat(devType,"(接触式CPU卡读写器)"); break; case 4: strcat(devType,"(非接触式存储卡读写器)"); break; case 5: strcat(devType,"(非接触式CPU卡读写器)"); break; case 6: strcat(devType,"(双界面卡读写器)"); break; } if(SRBuffer[2]==0) return SC_SUCCESS; else return SCERR_OPERROR; }