-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

First info: pscs-lite-1.0.0B, towitoko-2.0.4

I cannot read a memorycard, i always get the message "Transaction failed" 
after calling SCardTransmit.
Writing to the memorycard goes OK!

If i use the test-program (ctapi) shipped with the towitoko-driver, reading 
and writing is possible.

I spent already a lot of time trying to run pcsc with my towitoko-reader, but 
with
less success. First I struggled with the protocol (SCARD_PROTOCOL_RAW or 
SCARD_PROTOCOL_ANY), now I am hanging with this problem.

I hope the community can help me.
Thanks in advance

Andi

- -------------------------------------
#include <stdio.h>
#include <pcsclite.h>
#include <winscard.h>
#include <stdlib.h>
#include <string.h>

#define REPEAT_TEST 1


void printAPDU( char *buf, int len )
{
  printf( "APDU =" );
  while( len > 0 ) {
    printf( " %02x", (unsigned char)(*buf) );
    buf++;
    len--;
  }
  printf( "\n" );
}


long SelectFile( SCARDHANDLE hCard, unsigned int fileid )
{
  BYTE cmd_buffer[]= { 0x00, 0xa4, 0x00, 0x00, 0x02, 0x3f, 0x00 };
  BYTE rx_buffer[300];
  long rv;
  SCARD_IO_REQUEST pioRecvPci, pioSendPci;
  unsigned long dwRecvLength;

  printf("Selecting MF                     : ");

  cmd_buffer[5]= (unsigned char)(fileid >> 8);
  cmd_buffer[6]= (unsigned char)(fileid & 255);

  pioSendPci.dwProtocol =  pioRecvPci.dwProtocol = SCARD_PROTOCOL_ANY;

  rv = SCardTransmit( hCard,
                      &pioSendPci, cmd_buffer, 7,
                      &pioRecvPci, rx_buffer,     &dwRecvLength );

  printf("%s\n", pcsc_stringify_error(rv));

  if ( rv == SCARD_S_SUCCESS ) {
    printAPDU( rx_buffer, dwRecvLength );
  }

  return rv;
}



long WriteData( SCARDHANDLE hCard, unsigned int address, unsigned int size, 
unsigned char *data )
{
  BYTE cmd_buffer[5+256];
  BYTE rx_buffer[300];
  long rv;
  SCARD_IO_REQUEST pioRecvPci, pioSendPci;
  unsigned long dwRecvLength;

  printf("Writing Data                 : ");

  cmd_buffer[0]  = 0x00;
  cmd_buffer[1]  = 0xd6;
  cmd_buffer[2] = (unsigned char) (address >> 8);
  cmd_buffer[3] = (unsigned char) (address & 0x00FF);
  cmd_buffer[4] = (unsigned char) (size % 256);

  memcpy( &cmd_buffer[5], data, size );

  pioSendPci.dwProtocol = SCARD_PROTOCOL_ANY;
  pioRecvPci.dwProtocol = SCARD_PROTOCOL_ANY;

  rv = SCardTransmit( hCard,
                      &pioSendPci, cmd_buffer, size+5,
                      &pioRecvPci, rx_buffer, &dwRecvLength );

  printf("%s\n", pcsc_stringify_error(rv));

  if ( rv == SCARD_S_SUCCESS ) {
    printAPDU( rx_buffer, dwRecvLength );
  }

  return rv;
}


long ReadData( SCARDHANDLE hCard, unsigned int address, unsigned int size, 
unsigned char *data )
{
  BYTE cmd_buffer[5];
  BYTE rx_buffer[300];
  long rv;
  SCARD_IO_REQUEST pioRecvPci, pioSendPci;
  unsigned long dwRecvLength;

  printf("Reading Data                 : ");

  cmd_buffer[0]  = 0x00;
  cmd_buffer[1]  = 0xb0;
  cmd_buffer[2]  = (unsigned char) (address >> 8);
  cmd_buffer[3]  = (unsigned char) (address & 0x00ff);
  cmd_buffer[4]  = (unsigned char) size;

  pioSendPci.dwProtocol = SCARD_PROTOCOL_ANY;
  pioRecvPci.dwProtocol = SCARD_PROTOCOL_ANY;

  rv = SCardTransmit( hCard,
                      &pioSendPci, cmd_buffer, 5,
                      &pioRecvPci, rx_buffer, &dwRecvLength );

  printf("%s\n", pcsc_stringify_error(rv));

  if ( rv == SCARD_S_SUCCESS ) {
//    memcpy( data, &rx_buffer[2], size );
    printAPDU( rx_buffer, dwRecvLength );
  }

  return rv;
}





int main( int argc, char **argv ) {
  SCARDHANDLE hCard;
  SCARDCONTEXT hContext;
  SCARD_READERSTATE_A rgReaderStates[1];
  SCARD_IO_REQUEST pioRecvPci, pioSendPci;
  unsigned long dwReaderLen, dwState, dwProt, dwAtrLen;
  unsigned long dwSendLength, dwRecvLength, dwPref, dwReaders, dwActiveProt;
  char *pcReaders, *mszReaders;
  unsigned char pbAtr[MAX_ATR_SIZE];
  const char * mszGroups;
  long rv;
  int i, p, iReader;
  int iList[16];
  BYTE pbRecvBuffer[300];
  BYTE pbSendBuffer[300];
  int address;
  int size;
  BYTE data[] = {1,2,3,4,5,6,7,8,9,12,11,12,13,14,15,16};
  int t = 0;


  while(1) {

  /*-----------*/
  printf("Testing SCardEstablishContext    : ");
  rv = SCardEstablishContext( SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext );

  printf("%s\n", pcsc_stringify_error(rv));

  if ( rv != SCARD_S_SUCCESS ) {
    return -1;
  }


/*-----------*/
  printf("Testing SCardConnect             : ");
  rv = SCardConnect(hContext,  "ChipDrive1 0 0",
                    SCARD_SHARE_SHARED,
                    SCARD_PROTOCOL_ANY,
                    &hCard, &dwActiveProt );


  if ( rv != SCARD_S_SUCCESS ) {
    SCardReleaseContext( hContext );
    return -1;
  }

  printf( "ActiveProt=%li\n",dwActiveProt  );

/*------------*/

  rv = SelectFile( hCard, 0x3f00 );
  if ( rv != SCARD_S_SUCCESS ) {
    SCardReleaseContext( hContext );
    return -1;
  }

  rv = WriteData( hCard, 0, 16, data );
  if ( rv != SCARD_S_SUCCESS ) {
    SCardReleaseContext( hContext );
    return -1;
  }

  rv = SelectFile( hCard, 0x3f00 );
  if ( rv != SCARD_S_SUCCESS ) {
    SCardReleaseContext( hContext );
    return -1;
  }

  rv = ReadData( hCard, 0, 16, data );
  if ( rv != SCARD_S_SUCCESS ) {
    SCardReleaseContext( hContext );
    return -1;
  }


/*--------------*/

  printf("Testing SCardDisconnect          : ");
  rv = SCardDisconnect( hCard, SCARD_UNPOWER_CARD );

  printf("%s\n", pcsc_stringify_error(rv));

  if ( rv != SCARD_S_SUCCESS ) {
    SCardReleaseContext( hContext );
    return -1;
  }


  printf("Waiting for card removing         \n");
  rgReaderStates[0].dwCurrentState = SCARD_STATE_PRESENT;
  rv = SCardGetStatusChange( hContext, INFINITE, rgReaderStates, 1 );
  printf("                                 : %s\n", pcsc_stringify_error(rv));



  printf("Testing SCardReleaseContext      : ");
  rv = SCardReleaseContext( hContext );

  printf("%s\n", pcsc_stringify_error(rv));

  if ( rv != SCARD_S_SUCCESS ) {
    return -1;
  }

  }

  printf("\n");
  printf("PC/SC Test Completed Successfully !\n");

  return 0;
}




-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.5 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7u3TyLcUxFzy6sSoRAmHSAJ45eeKhNCk3ZUh8K2VTUSwyDxOZXACfcLTH
l0Cw0UILO3lRhs18qk3cPTM=
=ySUo
-----END PGP SIGNATURE-----
***************************************************************
Unix Smart Card Developers - M.U.S.C.L.E.
(Movement for the Use of Smart Cards in a Linux Environment)
http://www.linuxnet.com/
To unsubscribe send an email to [EMAIL PROTECTED] with
unsubscribe sclinux
***************************************************************

Reply via email to