static unsigned short SetMastKey1(unsigned char *data) { unsigned char buf[33]; int file = -1; u8 i = 0; memset((char*)buf, 0x00, sizeof(buf)); //strncpy((char*)buf, (char*)data, 32); printf("主密钥:\r\n"); for(i = 0;i < strlen(data);i++) printf("%s\r\n",data); StrToHex(buf,data); file = sdk_open_file(FILENAME_MASTKEY, FILE_OPEN_CREATE | FILE_OPEN_WRITE); if(sdk_write_file((char*)buf, 16, file)) { DEBUG printf("file write error\r\n"); } sdk_close_file(file); return ESUCCESS; }
/**************************************************************************** **函数名: void download_param(unsigned char* tag, unsigned char* value) **描述: 下载参数,从pc端或者手机端下载。 **输入参数: tag:标签; value:数据 **输出参数:无 **返回值: **备注: ** ** **版权:郑州友池电子技术有限公司深圳分公司 ** **作者 & 日期: 申伟宏(2015-04-10) **--------------------------------------------------------------------------- **修改记录: ****************************************************************************/ void download_param(unsigned char* tag, unsigned char* value) { int i = 0; unsigned char buf[100]; disp_fill_title(); sdk_fill_lcd(2, 0, (char*)tag, SHOW_MID); sdk_fill_lcd(3, 0, (char*)"正在下载...", SHOW_MID); for(i = 0; i < sizeof(gDownParam) / sizeof(gDownParam[0]); i++){ if(strlen((char*)tag) == 0){ i = -1; save_sysparam(); sdk_fill_lcd(3, 0, (char*)"数据已保存", SHOW_MID); waitkey_exit(KEY_MASK_ENTER | KEY_MASK_ESC, 500); break; } if(strncmp((char*)tag, (char*)gDownParam[i].m_label, 50) == 0){ disp_fill_title(); sdk_fill_lcd(2, 0, (char*)tag, SHOW_MID); if(gDownParam[i].m_dealmode == DEAL_MODE_PARAM){ sdk_fill_lcd(3, 0, (char*)"下载完成", SHOW_MID); if(gDownParam[i].m_datatype == DATA_TYPE_STR){ strncpy((char*)gDownParam[i].m_addr, (char*)value, gDownParam[i].m_maxlen); } else if(gDownParam[i].m_datatype == DATA_TYPE_HEX){ StrToHex(gDownParam[i].m_addr, value); } else if(gDownParam[i].m_datatype == DATA_TYPE_INT){ switch(gDownParam[i].m_maxlen){ case 1: *(unsigned char*)buf = atoi((char*)value); memcpy((char*)gDownParam[i].m_addr, (char*)buf, 1); break; case 2: *(unsigned short*)buf = (u16)atoi((char*)value); memcpy((char*)gDownParam[i].m_addr, (char*)buf, 2); break; case 4: *(unsigned long*)buf = (u32)atoi((char*)value); memcpy((char*)gDownParam[i].m_addr, (char*)buf, 4); break; } } } else if(gDownParam[i].m_dealmode == DEAL_MODE_FUN){ ((DealParamFun)gDownParam[i].m_addr)(value); sdk_fill_lcd(3, 0, (char*)"下载完成", SHOW_MID); } else{ sdk_fill_lcd(3, 0, (char*)"下载失败", SHOW_MID); } i = -1; break; } } if(i >= 0){ disp_fill_title(); sdk_fill_lcd(2, 0, (char*)tag, SHOW_MID); sdk_fill_lcd(3, 0, (char*)"下载失败", SHOW_MID); pro_reply_cmd(0x15, 0x02, (u8*)POSPRO_ERECVDATA); } else{ pro_reply_cmd(0x06, 0x02, (u8*)POSPRO_SUCCESS); } }
void main (int argc, char** argv) { FILE* fptr; char filename[10]; char label[32], opcode[32], operand[32]; int loc = 0; int line = 0; int loop; int is_empty_line; int is_comment; int loader_flag = 0; int search_symtab, x; int xe = 0; if (argc ==2) strcpy(filename, argv[1]); else if (argc == 3 && strcmp(argv[1], "xe") == 0) { strcpy(filename, argv[2]); xe=1; } else { printf("\n./sxass [xe] filename"); exit(1); } fptr = fopen(filename,"r"); if (fptr == NULL) { printf("ERROE: Unable to open the %s file.\n",filename); exit(1); } while (fgets(Buffer,256,fptr) != NULL) { is_empty_line = strlen(Buffer); Index = 0; j = 0; strcpy(label,ReadLabel()); if (Label[0] == '.') is_comment = 1; else is_comment = 0; if (is_empty_line>1 && is_comment!=1) { Index = 0; j = 0; IMRArray[ArrayIndex] = (IntermediateRec*)malloc(sizeof(IntermediateRec));/* [A] */ IMRArray[ArrayIndex]->LineIndex = ArrayIndex; strcpy(label,ReadLabel()); strcpy(IMRArray[ArrayIndex]->LabelField,label); SkipSpace(); if (line == 0) { strcpy(opcode,ReadOprator()); strcpy(IMRArray[ArrayIndex]->OperatorField,opcode);/* [A] */ if (!strcmp(opcode,"START")) { SkipSpace(); strcpy(operand,ReadOperand()); strcpy(IMRArray[ArrayIndex]->OperandField, operand);/* [A] */ LOCCTR[LocctrCounter] = StrToHex(operand); start_address = LOCCTR[LocctrCounter]; } else { LOCCTR[LocctrCounter] = 0; start_address = LOCCTR[LocctrCounter]; } } else { strcpy(opcode, ReadOprator()); strcpy(IMRArray[ArrayIndex]->OperatorField,opcode); SkipSpace(); strcpy(operand,ReadOperand()); strcpy(IMRArray[ArrayIndex]->OperandField,operand); if (strcmp(opcode,"END")) { if (label[0] != '\0') { if (SearchSymtab(label)) { fclose(fptr); printf("ERROE: Duplicate Symbol\n"); FoundOnSymtab_flag = 0; exit(1); } RecordSymtab(label); } if (SearchOptab (opcode)) LOCCTR[LocctrCounter] = loc + (int) (OPTAB[Counter].Format-'0'); else if (!strcmp(opcode,"WORD")) LOCCTR[LocctrCounter] = loc + 3; else if (!strcmp(opcode,"RESW")) LOCCTR[LocctrCounter] = loc + 3 * StrToDec(operand); else if (!strcmp(opcode,"RESB")) LOCCTR[LocctrCounter] = loc + StrToDec(operand); else if (!strcmp(opcode,"BYTE")) LOCCTR[LocctrCounter] = loc + ComputeLen(operand); else{ fclose(fptr); printf("ERROE: Invalid Operation Code, %s\n", opcode); exit(1); } } } loc = LOCCTR[LocctrCounter]; IMRArray[ArrayIndex]->Loc = LOCCTR[LocctrCounter-1]; LocctrCounter++; ArrayIndex++; } FoundOnOptab_flag = 0; line += 1; } program_length = LOCCTR[LocctrCounter-2]- LOCCTR[0]; printf("Pass 2 Processing...\n"); unsigned long inst_fmt;// unsigned long inst_fmt_opcode; unsigned long inst_fmt_index; unsigned long inst_fmt_address; for (loop = 1; loop<ArrayIndex; loop++){ inst_fmt_opcode = 0; inst_fmt_index = 0; inst_fmt_address = 0; strcpy(opcode,IMRArray[loop]->OperatorField); if (SearchOptab(opcode)){ inst_fmt_opcode = OPTAB[Counter].ManchineCode; inst_fmt_opcode <<=16; IMRArray[loop]->ObjectCode = inst_fmt_opcode; strcpy(operand,IMRArray[loop]->OperandField); if (operand[strlen(operand)-2] == ',' && operand[strlen(operand)-1] == 'X'){ inst_fmt_index = 0x008000; operand[strlen(operand)-2] = '\0'; } else inst_fmt_index = 0x000000; for(search_symtab = 0; search_symtab<SymtabCounter; search_symtab++){ if(!strcmp(operand, SYMTAB[search_symtab].Label)) inst_fmt_address = (long)SYMTAB[search_symtab].Address; } inst_fmt = inst_fmt_opcode + inst_fmt_index + inst_fmt_address; IMRArray[loop]->ObjectCode = inst_fmt; } else if (!strcmp(opcode, "WORD")){ strcpy(operand,IMRArray[loop]->OperandField); IMRArray[loop]->ObjectCode = StrToDec(operand); } else if (!strcmp(opcode,"BYTE")){ strcpy(operand,IMRArray[loop]->OperandField); IMRArray[loop]->ObjectCode = 0; if(operand[0]=='C' || operand[0]=='c' && operand[1]=='\''){ for (x = 2; x<=(int) (strlen(operand)-2); x++){ IMRArray[loop]->ObjectCode=IMRArray[loop]->ObjectCode + (int)operand[x]; IMRArray[loop]->ObjectCode<<=8; } } if(operand[0]=='X' || operand[0]=='x' && operand[1]=='\''){ char *operand_ptr; operand_ptr = &operand[2]; *(operand_ptr+2)='\0'; for (x=2; x<=(int) (strlen(operand)-2); x++){ IMRArray[loop]->ObjectCode=IMRArray[loop]->ObjectCode + StrToHex(operand_ptr); IMRArray[loop]->ObjectCode<<=8; } } IMRArray[loop]->ObjectCode>>=8; } else
uint32 GetInputHex() { char buf[30]; GetDlgItemText(hWndDlg,IDC_BREAK_AT_COUNT, buf, 29); return StrToHex(buf); }
//写数据 bool CTemperHumiCtrl::WriteData(const char *pbuf, const int len, const uint8 timeout) { static uint8 send_buf[1024] = {0}; // 存储待发送的十六进制命令 static char temp_buf[1024] = {0}; // 临时存放去除空格的命令字符串 int left_len = 0; // 待发送的命令长度 int nfds = 0; // 可写句柄数量 fd_set write_fds; // 句柄集合 struct timeval tv; // select 等待时间 if (len > sizeof(send_buf)) { MainApp.m_log.WriteLog("%s:%d 发送命令的长度必须小于 1024", __FILE__, __LINE__); return -1; } memset(temp_buf, 0x00, sizeof(temp_buf)); int temp_len = TrimSpace(temp_buf, pbuf, len); if (temp_len % 2 != 0) { MainApp.m_log.WriteLog("%s:%d 命令的长度必须是2的整数倍", __FILE__, __LINE__); return false;; } //fprintf(stdout, "len=%d, cmd=%s\n", temp_len, temp_buf); memset(send_buf, 0x00, sizeof(send_buf)); StrToHex(send_buf, temp_buf, temp_len); uint8 CRC[2]; memset(CRC, 0, sizeof(CRC)); CRC16_Modbus(send_buf, (uint16)(temp_len/2), CRC); //fprintf(stdout, "CRC[0]=%02X, CRC[1]=%02X\n", CRC[0], CRC[1]); // 待发送缓冲区加上两个字节的CRC码 send_buf[temp_len/2] = CRC[0]; send_buf[temp_len/2 + 1] = CRC[1]; left_len = temp_len/2 + 2; uint8 *p = send_buf; while (left_len > 0) { tv.tv_sec = 0; tv.tv_usec = 500000; FD_ZERO(&write_fds); FD_SET(m_fd, &write_fds); nfds = select(m_fd + 1, NULL, &write_fds, NULL, &tv); if (nfds < 0) { break; } else if(0 == nfds) { //printf("no fd use write!\n"); break; } int nwrite = write(m_fd, p, left_len); if (nwrite > 0) { left_len -= nwrite; p += nwrite; } else { break; } } if (left_len != 0) { return false; } return true; }
bool CJMYPassPay::WriteData(string &srcCmd) { if (-1 == m_fd) { PrintLog("写数据失败,串口已关闭。"); return false; } // 判断读线程是否存在 if (ESRCH == pthread_kill(m_thid, 0)) { PrintLog("线程已退出,重启读数据线程"); pthread_create(&m_thid, NULL, ReadThread, (void *)(&m_fd)); } static char srcBuff[CMD_BUF_SIZE] = {0}; // 存放原始字符串 static uint8 sendBuf[CMD_BUF_SIZE] = {0}; // 存储待发送的十六进制命令 static char tempBuf[CMD_BUF_SIZE] = {0}; // 临时存放去除空格的命令字符串 int srcLen = srcCmd.length(); // 待发送的命令长度 if (0 >= srcLen || srcLen > CMD_BUF_SIZE - 4) // 预留两个字节的长度和1个自己的BCC校验码 { PrintLog("cmd length must low then %d", CMD_BUF_SIZE - 4); return false; } memcpy(srcBuff, srcCmd.c_str(), srcLen); memset(tempBuf, 0x00, sizeof(tempBuf)); int tempLen = TrimSpace(tempBuf, srcBuff, srcLen); if (tempLen % 2 != 0) { PrintLog("cmd len must mod 2 == 0"); return false; } // 计算命令串的总长度,不包括一个字节的BCC长度,命令长度 + 两个字节的长度。 memset(sendBuf, 0x00, sizeof(sendBuf)); StrToHex(&sendBuf[3], tempBuf, tempLen); int cmdLen = tempLen / 2; // 加上两个字节的长度,加一个字节的设备地址 cmdLen += 3; sendBuf[0] = cmdLen & 0XFF00; sendBuf[1] = cmdLen & 0X00FF; sendBuf[2] = 0X00; // 计算BCC校验码 uint8 bcc = 0; CaculateBCC(sendBuf, cmdLen, bcc); // 命令尾部加上bcc校验码 sendBuf[cmdLen] = bcc; cmdLen += 1; char temp[CMD_BUF_SIZE]; memset(temp, 0x00, sizeof(temp)); for (int i = 0; i < cmdLen; i++) { sprintf(temp + i * 3, "%02X ", sendBuf[i]); } srcCmd = temp; int left_len = cmdLen; // 待发送的命令长度 int nfds = 0; // 可写句柄数量 fd_set write_fds; // 句柄集合 struct timeval tv; // select 等待时间 tv.tv_sec = 0; tv.tv_usec = 1; FD_ZERO(&write_fds); FD_SET(m_fd, &write_fds); uint8 *p = sendBuf; while (left_len > 0) { nfds = select(m_fd + 1, NULL, &write_fds, NULL, &tv); if (nfds < 0) { break; } else if(0 == nfds) { //PrintLog("no fd use write!"); break; } int nwrite = write(m_fd, p, left_len); if (nwrite > 0) { left_len -= nwrite; p += nwrite; } else { break; } } if (left_len != 0) { return false; } return true; }
/**************************************************************************** **函数名: int disp_set_terparam(u8* param) **描述: 设置终端参数 **输入参数: param :传入参数 **输出参数: **返回值: >= 0:正常状态; < 0:不正常状态。 **备注: ** **版权:郑州友池电子技术有限公司深圳分公司 ** **作者 & 日期: 申伟宏(2015-01-31) **----------------------------------------------------------------------------- **修改记录: ****************************************************************************/ int disp_set_terparam(u8* param) { int re = ESUCCESS; u8 inputdata[50]; memset(inputdata, 0x00, sizeof(inputdata)); memcpy((char*)inputdata, (char*)gSysParam.m_TerminalNo, sizeof(gSysParam.m_TerminalNo)); re = disp_input_data((u8*)"终端参数", (u8*)"请输入终端号:", INPUT_TYPE_UNKNOW, (u8*)inputdata, 8, 8); if(re < 0){ goto EXIT_disp_set_terparam; } memcpy((char*)gSysParam.m_TerminalNo, (char*)inputdata, sizeof(gSysParam.m_TerminalNo)); memset((char*)inputdata, 0x00, sizeof(inputdata)); memcpy((char*)inputdata, (char*)gSysParam.m_MerchantNo, sizeof(gSysParam.m_MerchantNo)); re = disp_input_data((u8*)"终端参数", (u8*)"请输入商户号:", INPUT_TYPE_UNKNOW, (u8*)inputdata, 15, 15); if(re < 0){ goto EXIT_disp_set_terparam; } memcpy((char*)gSysParam.m_MerchantNo, (char*)inputdata, sizeof(gSysParam.m_MerchantNo)); memset(inputdata, 0x00, sizeof(inputdata)); HexToStr(gSysParam.m_VoucherNo,inputdata, sizeof(gSysParam.m_VoucherNo) * 2); re = disp_input_data((u8*)"终端参数", (u8*)"请设置流水号:", INPUT_TYPE_UNKNOW, inputdata, sizeof(gSysParam.m_VoucherNo) * 2, sizeof(gSysParam.m_VoucherNo) * 2); if(re < 0){ goto EXIT_disp_set_terparam; } inputdata[sizeof(gSysParam.m_VoucherNo) * 2] = 0x00; StrToHex(gSysParam.m_VoucherNo,inputdata); memset(inputdata, 0x00, sizeof(inputdata)); HexToStr(gSysParam.m_BatchNo,inputdata, sizeof(gSysParam.m_BatchNo) * 2); re = disp_input_data((u8*)"终端参数", (u8*)"请设置批次号:", INPUT_TYPE_UNKNOW, (u8*)inputdata, sizeof(gSysParam.m_BatchNo) * 2, sizeof(gSysParam.m_BatchNo) * 2); if(re < 0){ goto EXIT_disp_set_terparam; } inputdata[sizeof(gSysParam.m_BatchNo) * 2] = 0x00; StrToHex(gSysParam.m_BatchNo,inputdata); memset(inputdata, 0x00, sizeof(inputdata)); HexToStr(gSysParam.m_MaxTradeAmount,inputdata, sizeof(gSysParam.m_MaxTradeAmount) * 2); re = disp_input_data((u8*)"终端参数", (u8*)"最大交易金额:", INPUT_TYPE_AMT, (u8*)inputdata, 0, sizeof(gSysParam.m_MaxTradeAmount) * 2); if(re < 0){ goto EXIT_disp_set_terparam; } inputdata[sizeof(gSysParam.m_MaxTradeAmount) * 2] = 0x00; StrToHex(gSysParam.m_MaxTradeAmount,inputdata); memset(inputdata, 0x00, sizeof(inputdata)); sprintf((char*)inputdata, "%d", (int)gSysParam.m_MaxTradeNum); re = disp_input_data((u8*)"终端参数", (u8*)"最大交易笔数:", INPUT_TYPE_UNKNOW, (u8*)inputdata, 1, 10); if(re < 0){ goto EXIT_disp_set_terparam; } gSysParam.m_MaxTradeNum = atoi((char*)inputdata); memset(inputdata, 0x00, sizeof(inputdata)); sprintf((char*)inputdata, "%d", gSysParam.m_RepeatSend); re = disp_input_data((u8*)"终端参数", (u8*)"请设置重发次数:", INPUT_TYPE_UNKNOW, (u8*)inputdata, 1, 2); if(re < 0){ goto EXIT_disp_set_terparam; } gSysParam.m_RepeatSend= atoi((char*)inputdata); memset(inputdata, 0x00, sizeof(inputdata)); strcpy((char*)inputdata, (char*)gSysParam.m_MerChanName); re = disp_input_data((u8*)"终端参数", (u8*)"请设置商户名称:", INPUT_TYPE_UNKNOW, (u8*)inputdata, 0, 40); if(re < 0){ goto EXIT_disp_set_terparam; } strcpy((char*)gSysParam.m_MerChanName, (char*)inputdata); memset(inputdata, 0x00, sizeof(inputdata)); sprintf((char*)inputdata, "%d", (int)gSysParam.m_LowpowerTimeout); re = disp_input_data((u8*)"终端参数", (u8*)"低功耗时间(秒):", INPUT_TYPE_UNKNOW, (u8*)inputdata, 1, 10); if(re < 0){ goto EXIT_disp_set_terparam; } gSysParam.m_LowpowerTimeout = atoi((char*)inputdata); memset((char*)inputdata, 0x00, sizeof(inputdata)); sprintf((char*)inputdata, "%d", gSysParam.m_ShutDownTimeout); re = disp_input_data((u8*)"终端参数", (u8*)"低功耗自动关机(秒):", INPUT_TYPE_UNKNOW, (u8*)inputdata, 1, 10); if(re < 0){ goto EXIT_disp_set_terparam; } gSysParam.m_ShutDownTimeout = atoi((char*)inputdata); EXIT_disp_set_terparam: save_sysparam(); sharebuf_remove_globalfun(); return re; }
int ParseParam(int argc,char* argv[]) { int i; for(i=1; i<argc; i++) { if(strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0) { Usage(0,NULL); } else if(strcmp(argv[i],"-r")==0 || strcmp(argv[i],"--read")==0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_ReadOffset = StrToHex(argv[i+1]); st_ReadInit = 1; i += 1; } else if(strcmp(argv[i],"-w")==0 || strcmp(argv[i],"--write")==0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_WriteOffset = StrToHex(argv[i+1]); st_WriteInit = 1; i += 1; } else if(strcmp(argv[i],"-m")==0 || strcmp(argv[i],"--memsize")==0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_MemSize = StrToHex(argv[i+1]); if(st_MemSize == 0) { Usage(3,"memsize can not be 0"); } i += 1; } else if(strcmp(argv[i],"-s")==0 || strcmp(argv[i],"--size")==0) { unsigned int oldsize=st_BufSize; unsigned char* pOldBuffer=st_pBuffer; if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_BufSize= StrToHex(argv[i+1]); if(st_BufSize == 0) { Usage(3,"size must > 0"); } st_pBuffer = (unsigned char*)malloc(st_BufSize); if(st_pBuffer == NULL) { ERROR_INFO("could not malloc size %d(0x%08x)\n",st_BufSize,st_BufSize); exit(3); } if(pOldBuffer) { memcpy(st_pBuffer,pOldBuffer,st_BufSize > oldsize ? oldsize: st_BufSize); free(pOldBuffer); pOldBuffer = NULL; oldsize = 0; } i += 1; } else if(strcmp(argv[i],"-c")==0 || strcmp(argv[i],"--content")==0) { unsigned int c; if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } if(st_pBuffer == NULL) { if(st_BufSize < 4) { st_BufSize = 4; } st_pBuffer =(unsigned char*) malloc(st_BufSize); if(st_pBuffer == NULL) { Usage(3,"can not allocate size %d",st_BufSize); } } else if(st_BufSize < 4) { st_BufSize = 4; if(st_pBuffer) { free(st_pBuffer); } st_pBuffer=NULL; st_pBuffer = (unsigned char*)malloc(st_BufSize); if(st_pBuffer == NULL) { ERROR_INFO("could not allocate size %d\n",st_BufSize); exit(3); } } c = StrToHex(argv[i+1]); memcpy(st_pBuffer,&c,st_BufSize > 4 ? 4 : st_BufSize); i += 1; } else if(strcmp(argv[i],"-f") == 0 || strcmp(argv[i],"--from") == 0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_pFromFile = argv[i+1]; i += 1; } else if(strcmp(argv[i],"-t") == 0 || strcmp(argv[i],"--to") == 0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_pToFile = argv[i+1]; i += 1; } else if(strcmp(argv[i],"-T") == 0 || strcmp(argv[i],"--timeout") == 0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_Timeout = StrToHex(argv[i+1]); i += 1; } else if (strcmp(argv[i],"-C") == 0 || strcmp(argv[i],"--create") == 0) { st_CreateMem = 1; } else if(strcmp(argv[i],"-n") == 0 || strcmp(argv[i],"--name") == 0) { if(argc <= (i+1)) { Usage(3,"argv[%d] %s need an arg",i,argv[i]); } st_pShareName = argv[i+1]; i += 1; } else { Usage(3,"unknown params %s",argv[i]); } } if(st_pShareName == NULL) { Usage(3,"must specify share name"); } if(st_MemSize == 0) { Usage(3,"must specify memsize"); } if(st_BufSize == 0) { st_BufSize = 4; assert(st_pBuffer == NULL); { st_pBuffer = (unsigned char*)malloc(st_BufSize); if(st_pBuffer == NULL) { ERROR_INFO("could not get the %d size buffer\n",st_BufSize); exit(3); } } } if(st_ReadInit == 0 && st_WriteInit == 0) { Usage(3,"must specify read/write use (-r/-w)"); } return 0; }
void CardOPeration(int choice1, unsigned char slot) { long retval; FILE * txtfd; char fbuf[1024]; char *pBuf; int i; int TempLen; int choice2; UsrParam.p_oBuf = RecBuf; UsrParam.p_iBuf = CmdBuf; switch(choice1) { case '1': { CardPowerOn(slot); break; } case '2': { ioctl(fd, IFD_CMD(PowerOff, slot), &UsrParam); break; } case '3': { do { printf("Exchange Apdu with slot%d:\n",slot); printf("==============================================\n\n"); printf("1: ACOS3 T0 Test\n"); printf("2: ACOS5 Test\n"); printf("3: ACOS6 Test\n"); // printf("4: ACOS7 Test\n"); // printf("5: ACOS9 Test\n"); printf("6: JCOP30 T1 Test\n"); printf("\n"); printf("0: Exit the test program\n\n"); printf("==============================================\n\n"); choice2 = getc(stdin); if(choice2 == 10) { choice2 = getc(stdin); } if(choice2 == '1') { txtfd = fopen("ACOS3T0Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS3T0Test.txt"); } } else if(choice2 == '2') { txtfd = fopen("ACOS5Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS5Test.txt"); } } else if(choice2 == '3') { txtfd = fopen("ACOS6Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS6Test.txt"); } } /* else if(choice2 == '4') { txtfd = fopen("ACOS7Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS7Test.txt"); } } else if(choice2 == '5') { txtfd = fopen("ACOS9Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: ACOS9Test.txt"); } } */ else if(choice2 == '6') { txtfd = fopen("JCOP30.txt","r"); if(txtfd == NULL) { printf("fail to open the file: JCOP30.txt"); } } if(txtfd != NULL) { while( fgets(fbuf, 1024, txtfd) != NULL) { if(*fbuf == ';') { printf("%s",fbuf); } else if(*fbuf == '\n'); else if(strstr(fbuf, ".RESET") != NULL) { CardPowerOn(slot); } else if(strstr(fbuf, "Command:") != NULL) { pBuf = fbuf + 9; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } UsrParam.p_iBuf = CmdBuf; UsrParam.iDataLen = i; UsrParam.oDataLen = 271; PrtMsg("\n\nCMD Len: %d\n", UsrParam.iDataLen); PrtMsg("slot%d CMD:", slot); for(i = 0; i < UsrParam.iDataLen; i++ ) { PrtMsg("0x%X ", UsrParam.p_iBuf[i]); } PrtMsg("\n\n"); if((retval = ioctl(fd, IFD_CMD(XfrAPDU, slot), &UsrParam)) < 0) { PrtMsg("\nOperation fail with errorcode = %lX\n", retval); break; } else { PrtMsg("Response Length: %d\n", UsrParam.oDataLen); PrtMsg("Response Data:"); for(i = 0; i < UsrParam.oDataLen; i++ ) { PrtMsg("0x%X ", RecBuf[i]); } PrtMsg("\n\n"); fgets(fbuf, 1024, txtfd); if(strstr(fbuf, "Response:") != NULL) { pBuf = fbuf + 10; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { if(*pBuf == 'x' || *pBuf == 'X'); else { if(RecBuf[i] != StrToHex(pBuf, 2)) { goto exchangefail; } } pBuf += 3; i++; } TempLen = i; if(TempLen == UsrParam.oDataLen) { PrtMsg("Success to Exchange with the slot%d!\n", slot); PrtMsg("\n\n"); } else { exchangefail: PrtMsg("Fail to Exchange with the slot%d!\n", slot); PrtMsg("The expected data:\n"); PrtMsg("%s",fbuf); PrtMsg("\n\n"); break; } } } } } } }while(choice2 != '0'); #if 0 txtfd = fopen("ACOS3T0Test.txt","r"); if(txtfd == NULL) { printf("fail to open the file: JCOP30.txt"); } else { while( fgets(fbuf, 1024, txtfd) != NULL) { while(*fbuf == 0x0D && *(fbuf + 1) == 0x0A) fgets(fbuf, 1024, txtfd); pBuf = fbuf; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } UsrParam.p_iBuf = CmdBuf; UsrParam.iDataLen = i; UsrParam.oDataLen = 271; PrtMsg("\n\nCMD Len: %d\n", UsrParam.iDataLen); PrtMsg("slot%d CMD:", slot); for(i = 0; i < UsrParam.iDataLen; i++ ) { PrtMsg("0x%X ", UsrParam.p_iBuf[i]); } PrtMsg("\n\n"); if((retval = ioctl(fd, IFD_CMD(XfrAPDU, slot), &UsrParam)) < 0) { PrtMsg("\nOperation fail with errorcode = %lX\n", retval); break; } else { PrtMsg("Response Length: %d\n", UsrParam.oDataLen); PrtMsg("Response Data:"); for(i = 0; i < UsrParam.oDataLen; i++ ) { PrtMsg("0x%X ", RecBuf[i]); } PrtMsg("\n\n"); PrtMsg("Success to Exchange with the slot%d!\n", slot); PrtMsg("\n\n"); } } } //#else txtfd = fopen("JCOP30.txt","r"); if(txtfd == NULL) { printf("fail to open the file: JCOP30.txt"); } else { while( fgets(fbuf, 1024, txtfd) != NULL) { while(strstr(fbuf, "Command:") == NULL) fgets(fbuf, 1024, txtfd); pBuf = fbuf + 9; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } UsrParam.p_iBuf = CmdBuf; UsrParam.iDataLen = i; UsrParam.oDataLen = 271; PrtMsg("\n\nCMD Len: %d\n", UsrParam.iDataLen); PrtMsg("slot%d CMD:", slot); for(i = 0; i < UsrParam.iDataLen; i++ ) { PrtMsg("0x%X ", UsrParam.p_iBuf[i]); } PrtMsg("\n\n"); if((retval = ioctl(fd, IFD_CMD(XfrAPDU, slot), &UsrParam)) < 0) { PrtMsg("\nOperation fail with errorcode = %lX\n", retval); break; } else { PrtMsg("Response Length: %d\n", UsrParam.oDataLen); PrtMsg("Response Data:"); for(i = 0; i < UsrParam.oDataLen; i++ ) { PrtMsg("0x%X ", RecBuf[i]); } PrtMsg("\n\n"); fgets(fbuf, 1024, txtfd); while(strstr(fbuf, "Response:") == NULL) fgets(fbuf, 1024, txtfd); pBuf = fbuf + 10; i = 0; while(*pBuf != '\n' && *pBuf != ' ') { CmdBuf[i] = StrToHex(pBuf, 2); pBuf += 3; i++; } TempLen = i; if( (TempLen == UsrParam.oDataLen) && ArrayCompare(UsrParam.p_iBuf, UsrParam.p_oBuf, TempLen)) { PrtMsg("Success to Exchange with the slot%d!\n", slot); PrtMsg("\n\n"); } else { PrtMsg("Fail to Exchange with the slot%d!\n", slot); PrtMsg("The expected data:\n"); for(i = 0; i < TempLen; i++ ) { PrtMsg("0x%X ", CmdBuf[i]); } PrtMsg("\n\n"); break; } } } } #endif break; } default: printf("Invalid selection: %d\n",choice1); } }