/* wrap data, transmit to the device and get answer */ RESPONSECODE sendData(PUCHAR TxBuffer, DWORD TxLength, PUCHAR RxBuffer, PDWORD RxLength, int wait) { RESPONSECODE rc; if (present == 0) { return IFD_COMMUNICATION_ERROR; } unsigned char apdu[TxLength]; unsigned char buffer[2000]; unsigned int i, j, response_length, n = 0; int sLen = TxLength; for (i = 0; i < TxLength; ++i) { apdu[i] = *(TxBuffer + i); } Log2(PCSC_LOG_DEBUG, "preparing to write %d data bytes", TxLength); printf(">> %s \n", hexdump(TxBuffer, TxLength)); int rv; rv = rfid_protocol_transceive(ph, TxBuffer, TxLength, RxBuffer, &response_length, 0, 0); if (rv < 0) { Log2(PCSC_LOG_ERROR, "Error from transceive, returned %d\n", rv); present = 0; rfid_protocol_close(ph); rfid_layer2_close(l2h); rfid_reader_close(rh); return IFD_COMMUNICATION_ERROR; } Log2(PCSC_LOG_DEBUG, "wrote %d data bytes", TxLength); Log2(PCSC_LOG_DEBUG, "got %d response bytes", response_length); if (response_length > 0) { *RxLength = response_length; //for (j = 0; j < response_length; ++j) //{ // *(RxBuffer + j) = buffer[j]; //} rc = IFD_SUCCESS; } else { Log1(PCSC_LOG_ERROR, "no response, meaning eof, reader not usable anymore\n"); present = 0; rfid_protocol_close(ph); rfid_layer2_close(l2h); rfid_reader_close(rh); rc = IFD_COMMUNICATION_ERROR; } return rc; }
static int send_command(char* sbuf, int slen, char* rbuf, int rlen) { int rv; static int doit; int answer, c = 0; if(doit == 0) { fprintf(stderr, "?? %s (%i bytes)\n", hexdump(sbuf, slen), slen); fprintf(stderr, "Execute? (Yes/No/All/Exit) "); answer = getc(stdin); if(answer != '\n') do { c = getc(stdin); } while(c != '\n' && c != EOF); switch(answer) { case 'y': // Fall-through case 'Y': case '\n': // Do nothing break; case 'n': // Fall-through case 'N': return 0; break; case 'a': // Fall-through case 'A': doit = 1; break; case 'e': // Fall-through case 'E': return -1; break; default: return 0; // Default to 'n' break; } } printf(">> %s (%i bytes)\n", hexdump(sbuf, slen), slen); rv = rfid_protocol_transceive(ph, sbuf, slen, rbuf, &rlen, 0, 0); if (rv < 0) { fprintf(stderr, "Error from transceive: %i\n", rv); return rv; } printf("<< %s (%i bytes)\n", hexdump(rbuf, rlen), rlen); if(rlen < 2 || rbuf[rlen-2] != (char)0x90 || rbuf[rlen-1] != 0x00) { fprintf(stderr, "SW is not 90 00. Ignore (i) or Abort (a)? "); answer = getc(stdin); if(answer != '\n') do { c = getc(stdin); } while(c != '\n' && c != EOF); switch(answer) { case 'i': // Fall-through case 'I': case '\n': // Do nothing break; case 'a': // Fall-through case 'A': return -1; break; default: return -1; // Default to 'a' } } return rlen; }