| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # multiple_readers.pl: test the pcsc Perl wrapper with TWO readers |
| 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 | |
| 23 | use warnings; |
| 24 | use strict; |
| 25 | |
| 26 | #my $current_protocol; |
| 27 | #my @StatusResult; |
| 28 | |
| 29 | my $hContext; |
| 30 | my @ReadersList; |
| 31 | |
| 32 | my $hCard; |
| 33 | my @StatusResult; |
| 34 | my $tmpVal; |
| 35 | my $SendData; |
| 36 | my $RecvData; |
| 37 | |
| 38 | my $hCard2; |
| 39 | my @StatusResult2; |
| 40 | my $tmpVal2; |
| 41 | my $SendData2; |
| 42 | my $RecvData2; |
| 43 | |
| 44 | #my @ConnectionContext; |
| 45 | |
| 46 | #------------------------------------------------------------------------------- |
| 47 | print "Getting context:\n"; |
| 48 | $hContext = new Chipcard::PCSC(); |
| 49 | die ("Can't create the pcsc object: $Chipcard::PCSC::errno\n") unless (defined $hContext); |
| 50 | print '.'x40 . " OK\n"; |
| 51 | |
| 52 | #------------------------------------------------------------------------------- |
| 53 | print "Retrieving readers'list:\n"; |
| 54 | @ReadersList = $hContext->ListReaders (); |
| 55 | die ("Can't get readers' list: $Chipcard::PCSC::errno\n") unless (defined($ReadersList[0])); |
| 56 | $, = "\n "; |
| 57 | $" = "\n "; |
| 58 | print " @ReadersList\n" . '.'x40 . " OK\n"; |
| 59 | |
| 60 | #------------------------------------------------------------------------------- |
| 61 | print "Connecting to the card: $ReadersList[0]\n"; |
| 62 | $hCard = new Chipcard::PCSC::Card ($hContext, $ReadersList[0]); |
| 63 | die ("Can't connect to the reader '$ReadersList[0]': $Chipcard::PCSC::errno\n") unless (defined($hCard)); |
| 64 | print '.'x40 . " OK\n"; |
| 65 | |
| 66 | #------------------------------------------------------------------------------- |
| 67 | die "no second reader present" if (! defined $ReadersList[1]); |
| 68 | print "Connecting to the card2: $ReadersList[1]\n"; |
| 69 | $hCard2 = new Chipcard::PCSC::Card ($hContext, $ReadersList[1]); |
| 70 | die ("Can't connect to the reader '$ReadersList[1]': $Chipcard::PCSC::errno\n") unless (defined($hCard2)); |
| 71 | print '.'x40 . " OK\n"; |
| 72 | |
| 73 | # sleep (3); |
| 74 | |
| 75 | #------------------------------------------------------------------------------- |
| 76 | if ($hCard->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T0 && |
| 77 | $hCard->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T1 && |
| 78 | $hCard->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_RAW) |
| 79 | { |
| 80 | print "Don't understand the active protocol, reconnecting to the card:\n"; |
| 81 | |
| 82 | my $active_protocol = $hCard->Reconnect($Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE, |
| 83 | $Chipcard::PCSC::SCARD_PROTOCOL_T1, |
| 84 | $Chipcard::PCSC::SCARD_RESET_CARD); |
| 85 | |
| 86 | die ("Failed to reconnect to '$ReadersList[0]': $Chipcard::PCSC::errno\n") unless (defined($active_protocol)); |
| 87 | |
| 88 | if ($hCard->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T0 && |
| 89 | $hCard->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T1 && |
| 90 | $hCard->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_RAW) |
| 91 | { |
| 92 | print "here is '$hCard->{dwProtocol}'"; |
| 93 | die ("Still don't understand the active current protocol: the card may be mute.\n"); |
| 94 | } else { |
| 95 | print '.'x40 . " OK\n"; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #------------------------------------------------------------------------------- |
| 100 | print "Getting Status:\n"; |
| 101 | @StatusResult = $hCard->Status (); |
| 102 | die ("Can't get card status: $Chipcard::PCSC::errno\n") unless (defined ($StatusResult[0])); |
| 103 | |
| 104 | printf " ReaderName = %s\n Status = %X\n Protocol = %X\n ATR = ", |
| 105 | $StatusResult[0], $StatusResult[1], $StatusResult[2]; |
| 106 | |
| 107 | foreach $tmpVal (@{$StatusResult[3]}) { |
| 108 | printf ("%02X ", $tmpVal); |
| 109 | } print "\n"; |
| 110 | print '.'x40 . " OK\n"; |
| 111 | |
| 112 | #------------------------------------------------------------------------------- |
| 113 | print ("Exchanging data:\n"); |
| 114 | $SendData = [0x00,0xA4,0x01,0x00, 0x02, 0x10, 0x00]; |
| 115 | $RecvData = $hCard->Transmit($SendData); |
| 116 | die ("Can't transmit data: $Chipcard::PCSC::errno") unless (defined ($RecvData)); |
| 117 | |
| 118 | print " Send = "; |
| 119 | foreach $tmpVal (@{$SendData}) { |
| 120 | printf ("%02X ", $tmpVal); |
| 121 | } print "\n"; |
| 122 | |
| 123 | print " Recv = "; |
| 124 | foreach $tmpVal (@{$RecvData}) { |
| 125 | printf ("%02X ", $tmpVal); |
| 126 | } print "\n"; |
| 127 | print '.'x40 . " OK\n"; |
| 128 | # sleep (3); |
| 129 | |
| 130 | #------------------------------------------------------------------------------- |
| 131 | if (defined $hCard2 && |
| 132 | $hCard2->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T0 && |
| 133 | $hCard2->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T1 && |
| 134 | $hCard2->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_RAW) |
| 135 | { |
| 136 | print "Don't understand the active protocol, reconnecting to the card:\n"; |
| 137 | |
| 138 | my $active_protocol = $hCard2->Reconnect($Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE, |
| 139 | $Chipcard::PCSC::SCARD_PROTOCOL_T1, |
| 140 | $Chipcard::PCSC::SCARD_RESET_CARD); |
| 141 | |
| 142 | die ("Failed to reconnect to '$ReadersList[1]': $Chipcard::PCSC::errno\n") unless (defined($active_protocol)); |
| 143 | |
| 144 | if ($hCard2->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T0 && |
| 145 | $hCard2->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_T1 && |
| 146 | $hCard2->{dwProtocol} != $Chipcard::PCSC::SCARD_PROTOCOL_RAW) |
| 147 | { |
| 148 | print "here is '$hCard2->{dwProtocol}'"; |
| 149 | die ("Still don't understand the active current protocol: the card may be mute.\n"); |
| 150 | } else { |
| 151 | print '.'x40 . " OK\n"; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #------------------------------------------------------------------------------- |
| 156 | print "Getting Status:\n"; |
| 157 | @StatusResult = $hCard2->Status (); |
| 158 | die ("Can't get card status: $Chipcard::PCSC::errno\n") unless (defined ($StatusResult[0])); |
| 159 | |
| 160 | printf " ReaderName = %s\n Status = %X\n Protocol = %X\n ATR = ", |
| 161 | $StatusResult[0], $StatusResult[1], $StatusResult[2]; |
| 162 | foreach $tmpVal (@{$StatusResult[3]}) { |
| 163 | printf ("%02X ", $tmpVal); |
| 164 | } print "\n"; |
| 165 | print '.'x40 . " OK\n"; |
| 166 | |
| 167 | #------------------------------------------------------------------------------- |
| 168 | print ("Exchanging data:\n"); |
| 169 | $SendData = [0x00,0xA4,0x00,0x00, 0x02, 0x10, 0x00]; |
| 170 | $RecvData = $hCard2->Transmit($SendData); |
| 171 | die ("Can't transmit data: $Chipcard::PCSC::errno") unless (defined ($RecvData)); |
| 172 | |
| 173 | print " Send = "; |
| 174 | foreach $tmpVal (@{$SendData}) { |
| 175 | printf ("%02X ", $tmpVal); |
| 176 | } print "\n"; |
| 177 | |
| 178 | print " Recv = "; |
| 179 | foreach $tmpVal (@{$RecvData}) { |
| 180 | printf ("%02X ", $tmpVal); |
| 181 | } print "\n"; |
| 182 | print '.'x40 . " OK\n"; |
| 183 | |
| 184 | #------------------------------------------------------------------------------- |
| 185 | print "Disconnecting the card:\n"; |
| 186 | $hCard->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD); |
| 187 | undef $hCard; |
| 188 | print '.'x40 . " OK\n"; |
| 189 | #------------------------------------------------------------------------------- |
| 190 | print "Disconnecting the card:\n"; |
| 191 | $hCard2->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD); |
| 192 | undef $hCard2; |
| 193 | print '.'x40 . " OK\n"; |
| 194 | #die ("") unless ($hCard->Disconnect ($Chipcard::PCSC::SCARD_UNPOWER_CARD)); |
| 195 | |
| 196 | #------------------------------------------------------------------------------- |
| 197 | print "Closing context:\n"; |
| 198 | $hContext = undef; |
| 199 | print '.'x40 . " OK\n"; |
| 200 | |
| 201 | # End of File # |
| 202 |