File: test.pl

package info (click to toggle)
pcsc-perl 1.4.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 296 kB
  • ctags: 74
  • sloc: perl: 887; ansic: 109; makefile: 50; sh: 44
file content (186 lines) | stat: -rwxr-xr-x 7,692 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/perl

#    test.pl: simple sample program to test the PCSC Perl wrapper
#    Copyright (C) 2001  Lionel Victor, 2003 Ludovic Rousseau
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# $Id: test.pl,v 1.12 2004/05/30 20:10:11 rousseau Exp $

use ExtUtils::testlib;
use Chipcard::PCSC;
use Chipcard::PCSC::Card;

use warnings;
use strict;

my $hContext;
my @ReadersList;

my $hCard;
my @StatusResult;
my $tmpVal;
my $SendData;
my $RecvData;
my $sw;

#-------------------------------------------------------------------------------
print "Getting context:\n";
$hContext = new Chipcard::PCSC();
die ("Can't create the pcsc object: $Chipcard::PCSC::errno\n") unless (defined $hContext);
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print "Retrieving readers'list:\n";
@ReadersList = $hContext->ListReaders ();
die ("Can't get readers' list: $Chipcard::PCSC::errno\n") unless (defined($ReadersList[0]));
$, = "\n  ";
$" = "\n  ";
print "  @ReadersList\n" . '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print "Getting status change:\n";
my (@readers_states, $reader_state, $timeout, $event_state);
# create the list or readers to watch
map { push @readers_states, ({'reader_name'=>"$_"}) } @ReadersList;

@StatusResult = $hContext->GetStatusChange(\@readers_states);

for my $i (0..$#readers_states)
{
	print "reader: " . $readers_states[$i]{'reader_name'} . "\n";
	print "  ATR: " .
	Chipcard::PCSC::array_to_ascii($readers_states[$i]{'ATR'}) .  "\n" if (defined $readers_states[$i]{'ATR'});
	print "  state:\n";
	$event_state = $readers_states[$i]{'event_state'};
	print "    state changed\n" if ($event_state & $Chipcard::PCSC::SCARD_STATE_CHANGED);
	print "    card present\n" if ($event_state & $Chipcard::PCSC::SCARD_STATE_PRESENT);
	print "    card abstent\n" if ($event_state & $Chipcard::PCSC::SCARD_STATE_EMPTY);
	print "    card mute\n" if ($event_state & $Chipcard::PCSC::SCARD_STATE_MUTE);

	$readers_states[$i]{'current_state'} = $event_state;
}

if (! ($readers_states[0]{'event_state'} &
		$Chipcard::PCSC::SCARD_STATE_PRESENT))
{
	$timeout = 10 * 1000;	# 10 seconds
	print "Insert a card in the first reader please (timeout in $timeout ms)\n";
	@StatusResult = $hContext->GetStatusChange(\@readers_states, $timeout);
}
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print "Connecting to the card:\n";
$hCard = new Chipcard::PCSC::Card ($hContext);
die ("Can't create the reader object: $Chipcard::PCSC::errno\n") unless (defined($hCard));

$tmpVal = $hCard->Connect($ReadersList[0], $Chipcard::PCSC::SCARD_SHARE_SHARED);
unless ($tmpVal) {
	# Try to reconnect and reset if connect fails
	print "Connect failed: trying to reset the card:\n";
	$tmpVal = $hCard->Reconnect ($Chipcard::PCSC::SCARD_SHARE_SHARED, $Chipcard::PCSC::SCARD_PROTOCOL_T0, $Chipcard::PCSC::SCARD_RESET_CARD);
	die ("Can't reconnect to the reader '$ReadersList[0]': $Chipcard::PCSC::errno\n") unless ($tmpVal);
}
die ("Can't understand the current protocol: $hCard->{dwProtocol}\n")
	unless ($hCard->{dwProtocol}==$Chipcard::PCSC::SCARD_PROTOCOL_T0 ||
            $hCard->{dwProtocol}==$Chipcard::PCSC::SCARD_PROTOCOL_T1 ||
	        $hCard->{dwProtocol}==$Chipcard::PCSC::SCARD_PROTOCOL_RAW);
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print ("Setting up timeout value:\n");
die ("Can't set timeout: $Chipcard::PCSC::errno\n") unless ($hContext->SetTimeout (50));
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print "Getting status:\n";
@StatusResult = $hCard->Status();
die ("Can't get status: $Chipcard::PCSC::errno\n") unless ($StatusResult[0]);
print "Reader name is $StatusResult[0]\n";
print "State: $StatusResult[1]\n";
print "Current protocol: $StatusResult[2]\n";
print "ATR: " . Chipcard::PCSC::array_to_ascii ($StatusResult[3]) . "\n";
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print ("Initiating transaction:\n");
die ("Can't initiate transaction: $Chipcard::PCSC::errno\n") unless ($hCard->BeginTransaction());
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
#sleep (13);

print ("Exchanging data:\n");
$SendData = Chipcard::PCSC::ascii_to_array ("00 A4 01 00 02 01 00");
$RecvData = $hCard->Transmit($SendData);
die ("Can't transmit data: $Chipcard::PCSC::errno") unless (defined ($RecvData));

print "  Send -> " . Chipcard::PCSC::array_to_ascii ($SendData) . "\n";
print "  Recv <- " . Chipcard::PCSC::array_to_ascii ($RecvData) . "\n";
print '.'x40 . " OK\n";
# sleep (3);

#-------------------------------------------------------------------------------
print "TransmitWithCheck:\n";
$SendData = "00 A4 00 00 02 3F 00";	# select DF 3F 00
# wait for ".. .." since we the SW will depend on the inserted card
($sw, $RecvData) = $hCard->TransmitWithCheck($SendData, ".. ..", 1);
warn "TransmitWithCheck: $Chipcard::PCSC::Card::Error" unless defined $sw;

print "  Send -> $SendData\n";
print "  Recv <- $RecvData (SW: $sw)\n";
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print "ISO7816Error:\n";
print "$sw: " . &Chipcard::PCSC::Card::ISO7816Error($sw) . "\n";
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
# This test is commented since it is reader/driver specific and may do bad
# things for another reader. Reader your reader and driver
# specifications to know what data to use.
#
#print "Control\n";
#$SendData = Chipcard::PCSC::ascii_to_array ("02");
#$RecvData = $hCard->Control(0x42000001, $SendData);
#die ("Can't Control data: $Chipcard::PCSC::errno") unless (defined ($RecvData));
#
#print "  Send -> " . Chipcard::PCSC::array_to_ascii ($SendData) . "\n";
#print "  Recv <- " . Chipcard::PCSC::array_to_ascii ($RecvData) . "\n";
#print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print ("Ending transaction:\n");
die ("Can't terminate transaction: $Chipcard::PCSC::errno\n") unless ($hCard->EndTransaction($Chipcard::PCSC::SCARD_LEAVE_CARD));
print '.'x40 . " OK\n";

#-------------------------------------------------------------------------------
print "Disconnecting the card:\n";
$tmpVal = $hCard->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD);
die ("Can't disconnect the Chipcard::PCSC object: $Chipcard::PCSC::errno\n") unless $tmpVal;
print '.'x40 . " OK\n";
#-------------------------------------------------------------------------------
print "Closing card object:\n";
$hCard = undef;
print '.'x40 . " OK\n";
#-------------------------------------------------------------------------------
print "Closing context:\n";
$hContext = undef;
#print '.'x40 . " OK\n";

# End of File #