| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # single_reader.pl: test the pcsc Perl wrapper with one reader |
| 4 | # copyright (c) 2001 Lionel Victor, 2003 Ludovic Rousseau |
| 5 | # |
| 6 | # this program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the gnu general public license as published by |
| 8 | # the free software foundation; either version 2 of the license, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # this program is distributed in the hope that it will be useful, |
| 12 | # but without any warranty; without even the implied warranty of |
| 13 | # merchantability or fitness for a particular purpose. see the |
| 14 | # gnu general public license for more details. |
| 15 | # |
| 16 | # you should have received a copy of the gnu general public license |
| 17 | # along with this program; if not, write to the free software |
| 18 | # foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa |
| 19 | |
| 20 | use ExtUtils::testlib; |
| 21 | use Chipcard::PCSC; |
| 22 | use Chipcard::PCSC::Card; |
| 23 | |
| 24 | use warnings; |
| 25 | use strict; |
| 26 | |
| 27 | #my $current_protocol; |
| 28 | #my @StatusResult; |
| 29 | |
| 30 | my $hContext; |
| 31 | my @ReadersList; |
| 32 | |
| 33 | my $hCard; |
| 34 | my @StatusResult; |
| 35 | my $tmpVal; |
| 36 | my $SendData; |
| 37 | my $RecvData; |
| 38 | |
| 39 | #my @ConnectionContext; |
| 40 | |
| 41 | #------------------------------------------------------------------------------- |
| 42 | print "Getting context:\n"; |
| 43 | $hContext = new Chipcard::PCSC(); |
| 44 | die ("Can't create the pcsc object: $Chipcard::PCSC::errno\n") unless (defined $hContext); |
| 45 | print '.'x40 . " OK\n"; |
| 46 | |
| 47 | #------------------------------------------------------------------------------- |
| 48 | print "Retrieving readers'list:\n"; |
| 49 | @ReadersList = $hContext->ListReaders (); |
| 50 | die ("Can't get readers' list: $Chipcard::PCSC::errno\n") unless (defined($ReadersList[0])); |
| 51 | $, = "\n "; |
| 52 | $" = "\n "; |
| 53 | print " @ReadersList\n" . '.'x40 . " OK\n"; |
| 54 | |
| 55 | #------------------------------------------------------------------------------- |
| 56 | print "Connecting to the card:\n"; |
| 57 | $hCard = new Chipcard::PCSC::Card ($hContext, $ReadersList[0]); |
| 58 | die ("Can't connect to the reader '$ReadersList[0]': $Chipcard::PCSC::errno\n") unless (defined($hCard)); |
| 59 | print '.'x40 . " OK\n"; |
| 60 | |
| 61 | #------------------------------------------------------------------------------- |
| 62 | if ($hCard->{dwProtocol}!=$Chipcard::PCSC::SCARD_PROTOCOL_T0 && |
| 63 | $hCard->{dwProtocol}!=$Chipcard::PCSC::SCARD_PROTOCOL_T1 && |
| 64 | $hCard->{dwProtocol}!=$Chipcard::PCSC::SCARD_PROTOCOL_RAW) |
| 65 | { |
| 66 | print "Don't understand the active protocol, reconnecting to the card:\n"; |
| 67 | |
| 68 | my $active_protocol = $hCard->Reconnect($Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE, |
| 69 | $Chipcard::PCSC::SCARD_PROTOCOL_T1, |
| 70 | $Chipcard::PCSC::SCARD_RESET_CARD); |
| 71 | |
| 72 | die ("Failed to reconnect to '$ReadersList[0]': $Chipcard::PCSC::errno\n") unless (defined($active_protocol)); |
| 73 | |
| 74 | if ($hCard->{dwProtocol}!=$Chipcard::PCSC::SCARD_PROTOCOL_T0 && |
| 75 | $hCard->{dwProtocol}!=$Chipcard::PCSC::SCARD_PROTOCOL_T1 && |
| 76 | $hCard->{dwProtocol}!=$Chipcard::PCSC::SCARD_PROTOCOL_RAW) |
| 77 | { |
| 78 | print "here is '$hCard->{dwProtocol}'"; |
| 79 | die ("Still don't understand the active current protocol: the card may be mute.\n"); |
| 80 | } else { |
| 81 | print '.'x40 . " OK\n"; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #------------------------------------------------------------------------------- |
| 86 | print "Getting Status:\n"; |
| 87 | @StatusResult = $hCard->Status (); |
| 88 | die ("Can't get card status: $Chipcard::PCSC::errno\n") unless (defined ($StatusResult[0])); |
| 89 | |
| 90 | printf " ReaderName = %s\n Status = %X\n Protocol = %X\n ATR = ", |
| 91 | $StatusResult[0], $StatusResult[1], $StatusResult[2]; |
| 92 | foreach $tmpVal (@{$StatusResult[3]}) { |
| 93 | printf ("%02X ", $tmpVal); |
| 94 | } print "\n"; |
| 95 | print '.'x40 . " OK\n"; |
| 96 | |
| 97 | #------------------------------------------------------------------------------- |
| 98 | print ("Exchanging data:\n"); |
| 99 | $SendData = [0x00,0xA4,0x01,0x00, 0x02, 0x10, 0x00]; |
| 100 | $RecvData = $hCard->Transmit($SendData); |
| 101 | die ("Can't transmit data: $Chipcard::PCSC::errno") unless (defined ($RecvData)); |
| 102 | |
| 103 | print " Send = "; |
| 104 | foreach $tmpVal (@{$SendData}) { |
| 105 | printf ("%02X ", $tmpVal); |
| 106 | } print "\n"; |
| 107 | |
| 108 | print " Recv = "; |
| 109 | foreach $tmpVal (@{$RecvData}) { |
| 110 | printf ("%02X ", $tmpVal); |
| 111 | } print "\n"; |
| 112 | print '.'x40 . " OK\n"; |
| 113 | # sleep (3); |
| 114 | |
| 115 | #------------------------------------------------------------------------------- |
| 116 | # This test is commented since it is reader/driver specific and may do bad |
| 117 | # things for another reader. Reader your reader and driver |
| 118 | # specifications to know what data to use. |
| 119 | # |
| 120 | #print "Control:\n"; |
| 121 | #$SendData = [ 0x02 ]; |
| 122 | #$RecvData = $hCard->Control(0x42000001, $SendData); |
| 123 | #die ("Can't Control: $Chipcard::PCSC::errno") unless (defined ($RecvData)); |
| 124 | # |
| 125 | #print " Send = "; |
| 126 | #foreach $tmpVal (@{$SendData}) { |
| 127 | # printf ("%02X ", $tmpVal); |
| 128 | #} print "\n"; |
| 129 | # |
| 130 | #print " Recv = "; |
| 131 | #foreach $tmpVal (@{$RecvData}) { |
| 132 | # printf ("%02X ", $tmpVal); |
| 133 | #} print "\n"; |
| 134 | #print '.'x40 . " OK\n"; |
| 135 | |
| 136 | #------------------------------------------------------------------------------- |
| 137 | print "Disconnecting the card:\n"; |
| 138 | $hCard->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD); |
| 139 | undef $hCard; |
| 140 | print '.'x40 . " OK\n"; |
| 141 | #------------------------------------------------------------------------------- |
| 142 | print "Closing context:\n"; |
| 143 | $hContext = undef; |
| 144 | print '.'x40 . " OK\n"; |
| 145 | |
| 146 | # End of File # |