Card(3)

πŸ’³ Card(3) β€” the Chipcard::PCSC::Card glow-up πŸ’…

ok so bestie, this is the <pink>Chipcard::PCSC::Card</pink> page from the User Contributed Perl Documentation section, and honestly? She's a whole personality. Buckle up πŸŽ€


✨ NAME ✨

Chipcard::PCSC::Card β€” the smart card communication library. that's it, that's the vibe. she talks to cards. πŸ’³


🌸 SYNOPSIS 🌸

ok here's the whole group chat in one screenshot, no cap:

 $hCard = new Chipcard::PCSC::Card ($hContext, "GemPC430 0 0",
        $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE);

 $RecvData = $hCard->Transmit([0xBC,0xB0,0x09,0xC8, 2]);

 $hCard->Disconnect($Chipcard::PCSC::SCARD_LEAVE_CARD);

 $hCard->Status();

 $hCard->BeginTransaction();

 $hCard->EndTransaction();

 $hCard->TransmitWithCheck($apdu, $sw_expected [, $debug]);

 $hCard->Control($control_code, \@data);

 ISO7816Error($sw);

like… look at her go 😭 she's doing SO much.


πŸ¦‹ DESCRIPTION πŸ¦‹

so the <pink>Chipcard::PCSC::Card</pink> module implements the <pink>Chipcard::PCSC::Card</pink> class (yes, the module and the class have the same name, that's just how Perl rolls, we don't ask questions πŸ’€).

objects from this class are literally your little messengers to a given reader. πŸ”Œ you build them out of a reference to a PCSC object β€” that's the thing that actually drives the reader.

for more info about PC/SC (the whole ecosystem, not just this class), go read the pcscd(1) man page. it's giving required reading πŸ“š

a <pink>Chipcard::PCSC::Card</pink> object uses the following properties (her little accessory collection πŸ’…):

  • $pcsccard_object->{hContext} β€” the reference to the underlying PCSC object
  • $pcsccard_object->{hCard} β€” the current PCSC connection handle
  • $pcsccard_object->{dwProtocol} β€” the protocol being used

πŸ‘‘ CONSTRUCTORS πŸ‘‘

these are the methods that make a <pink>Chipcard::PCSC::Card</pink> object exist. main character creation energy ✨

  • $hCard = new Chipcard::PCSC::Card ($hContext);

constructs a brand new <pink>Chipcard::PCSC::Card</pink> object without connecting to any reader. she's here, she's just not linked up yet πŸ™ˆ

$hContext is mandatory and contains the reference to a valid PCSC object.

  • $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name, $share_mode, $preferred_protocol);

constructs a new Chipcard::PCSC::Card object and connects to the specified reader. glow-up complete πŸ”₯

  • $hContext β€” is mandatory and contains the reference to a valid PCSC object.
  • $reader_name β€” is the name of the reader you want to connect to. it's of the form "GemPC410 0 0".
πŸ“Œ pls note: you can get the list of available readers with a call to "$hContext->ListReaders()". (see the section named PCSC METHODS in the Chipcard::PCSC man page for more info on "ListReaders".)
  • $share_mode β€” is the desired mode of connection to the reader. it can be any of the following:
    • $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE β€” the application do not share the reader (she's possessive, it's fine πŸ’…)
    • $Chipcard::PCSC::SCARD_SHARE_SHARED β€” the application will allow others to share the reader.
    • $Chipcard::PCSC::SCARD_SHARE_DIRECT β€” (not used by PC/SC-lite)
  • $preferred_protocol β€” is the protocol which should be used if possible. if the protocol is not available, another protocol will be used and "$hCard->{dwProtocol}" will be set accordingly. both "$hCard->{dwProtocol}" and $preferred_protocol accept the following values:
    • $Chipcard::PCSC::SCARD_PROTOCOL_T0 β€” the T=0 protocol
    • $Chipcard::PCSC::SCARD_PROTOCOL_T1 β€” the T=1 protocol
    • $Chipcard::PCSC::SCARD_PROTOCOL_RAW β€” raw protocol
  • $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name, $share_mode);

this method is equivalent to:

 $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name,
    $share_mode,
    $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
    $Chipcard::PCSC::SCARD_PROTOCOL_T1);
  • $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name);

this method is equivalent to:

 $hCard = new Chipcard::PCSC::Card ($hContext, $reader_name,
    $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE,
    $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
    $Chipcard::PCSC::SCARD_PROTOCOL_T1);

😭 CONSTRUCTION FAILURE 😭

not every glow-up goes to plan, and that's okay. <pink>Chipcard::PCSC::Card</pink> constructors return an undef value when the object can not be created. πŸ’€

$Chipcard::PCSC::errno can be used to get more information about the error β€” she will tell you what went wrong, you just gotta ask. see the section ERROR HANDLING in the Chipcard::PCSC man page for more info.


🫢 Chipcard::PCSC::Card METHODS 🫢

here's the full roster of every method that can be used with a <pink>Chipcard::PCSC::Card</pink> object. she's versatile, she contains multitudes ✨


⭐ $hCard->Connect($reader_name, $share_mode, $preferred_protocol);

Connect() can be used to connect to the reader and its smart card β€” but only if the connection hasn't been established yet. the default constructor can establish the connection if you give it enough parameters. (yes, she can do it herself, she just needs the info first πŸ™)

the return value upon successful completion is the protocol used to communicate with the smart card. it can be any of the following:

  • $Chipcard::PCSC::SCARD_PROTOCOL_T0 β€” the T=0 protocol
  • $Chipcard::PCSC::SCARD_PROTOCOL_T1 β€” the T=1 protocol
  • $Chipcard::PCSC::SCARD_PROTOCOL_RAW β€” raw protocol
  • $reader_name β€” is mandatory. it contains the name of the reader you want to connect to. it's of the form "GemPC410 0 0".
πŸ“Œ pls note: you can get the list of available readers with a call to "$hContext->ListReaders()". (see the section named PCSC METHODS in the Chipcard::PCSC man page for more info on "ListReaders".)
  • $share_mode β€” is the desired mode of connection to the reader. it can be any of the following:
    • $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE β€” the application do not share the reader
    • $Chipcard::PCSC::SCARD_SHARE_SHARED β€” the application will allow others to share the reader.
    • $Chipcard::PCSC::SCARD_SHARE_DIRECT β€” (not used by PCSClite)
  • $preferred_protocol β€” is the protocol which should be used if possible. if the protocol is not available, another protocol will be used and "$hCard->{dwProtocol}" will be set accordingly. $preferred_protocol accept the following values:
    • $Chipcard::PCSC::SCARD_PROTOCOL_T0 β€” the T=0 protocol
    • $Chipcard::PCSC::SCARD_PROTOCOL_T1 β€” the T=1 protocol
    • $Chipcard::PCSC::SCARD_PROTOCOL_RAW β€” raw protocol

⭐ $hCard->Connect($reader_name, $share_mode);

this method is equivalent to:

 $hCard->Connect($reader_name, $share_mode,
    $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
    $Chipcard::PCSC::SCARD_PROTOCOL_T1);

⭐ $hCard->Connect($reader_name);

this method is equivalent to:

 $hCard->Connect($reader_name, $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE,
    $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
    $Chipcard::PCSC::SCARD_PROTOCOL_T1);

πŸ” $hCard->Reconnect($share_mode, $preferred_protocol, $initialization);

Reconnect() can be used to re-negotiate an already opened connection. this implies that the <pink>Chipcard::PCSC::Card</pink> object is connected and has "$hCard->{hCard}" set accordingly. (iykyk β€” she's already in the building 🏒)

reconnecting to a smart card is used to change the share mode and the current protocol. it's giving ✨rebrand✨

the return value upon successful completion is the protocol choose to communicate with the smart card. it can be any of the following:

  • $Chipcard::PCSC::SCARD_PROTOCOL_T0 β€” the T=0 protocol
  • $Chipcard::PCSC::SCARD_PROTOCOL_T1 β€” the T=1 protocol
  • $Chipcard::PCSC::SCARD_PROTOCOL_RAW β€” raw protocol
  • $share_mode β€” is the desired mode of connection to the reader. it can be any of the following:
    • $Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE β€” the application do not share the reader
    • $Chipcard::PCSC::SCARD_SHARE_SHARED β€” the application will allow others to share the reader.
    • $Chipcard::PCSC::SCARD_SHARE_DIRECT β€” (not used by PCSClite)
  • $preferred_protocol β€” is the protocol which should be used if possible. if the protocol is not available, another protocol will be used and "$hCard->{dwProtocol}" will be set accordingly. $preferred_protocol accept the following values:
    • $Chipcard::PCSC::SCARD_PROTOCOL_T0 β€” the T=0 protocol
    • $Chipcard::PCSC::SCARD_PROTOCOL_T1 β€” the T=1 protocol
    • $Chipcard::PCSC::SCARD_PROTOCOL_RAW β€” raw protocol
  • $initialization β€” is the action to take when reconnecting to the smart card. it can be any of the following values:
    • $Chipcard::PCSC::SCARD_LEAVE_CARD β€” do nothing on close
    • $Chipcard::PCSC::SCARD_RESET_CARD β€” reset on close
    • $Chipcard::PCSC::SCARD_UNPOWER_CARD β€” power down on close
    • $Chipcard::PCSC::SCARD_EJECT_CARD β€” eject on close

πŸ” $hCard->Reconnect($share_mode, $preferred_protocol);

this method is equivalent to:

 $hCard->Reconnect($share_mode, $preferred_protocol,
    $Chipcard::PCSC::SCARD_LEAVE_CARD);

πŸ” $hCard->Reconnect($share_mode);

this method is equivalent to:

 $hCard->Reconnect($share_mode,
    $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
    $Chipcard::PCSC::SCARD_PROTOCOL_T1,
    $Chipcard::PCSC::SCARD_LEAVE_CARD);

πŸ” $hCard->Reconnect();

this method is equivalent to:

 $hCard->Reconnect($Chipcard::PCSC::SCARD_SHARE_EXCLUSIVE,
    $Chipcard::PCSC::SCARD_PROTOCOL_T0 |
    $Chipcard::PCSC::SCARD_PROTOCOL_T1,
    $Chipcard::PCSC::SCARD_LEAVE_CARD);

πŸ‘‹ $hCard->Disconnect($initialization);

Disconnect() closes the connection to the smart card reader. she's logging off, closing the tab, saying goodbye 😭 it returns true upon successful completion or undef otherwise.

"$hCard->{hContext}" will be set to undef if the connection is successfully closed.

  • $initialization β€” is the action to take when reconnecting to the smart card. it can be any of the following values:
    • $Chipcard::PCSC::SCARD_LEAVE_CARD β€” do nothing on close
    • $Chipcard::PCSC::SCARD_RESET_CARD β€” reset on close
    • $Chipcard::PCSC::SCARD_UNPOWER_CARD β€” power down on close
    • $Chipcard::PCSC::SCARD_EJECT_CARD β€” eject on close

πŸ‘‹ $hCard->Disconnect();

this method is equivalent to:

 $hCard->Disconnect($Chipcard::PCSC::SCARD_EJECT_CARD);

πŸ“Š $hCard->Status();

Status() returns the current status of the connection to a smart card. it's used to retrieve the ATR (Answer To Reset) value as well as the protocol being used to communicate. it's literally her vibe check 🌈

the return value is the undef value if an error occurs. in such a case, $! should be set with a string describing the error. upon successful completion "Status" returns an array as follows:

 ($reader_name, $reader_state, $protocol, "\@atr")
  • $reader_name β€” is a string containing the name of the reader
  • $reader_state β€” is a scalar containing the current state of the reader.

it can be any combination of the following values:

  • $Chipcard::PCSC::SCARD_UNKNOWN β€” unknown state

(ok pause β€” part 2 is coming, don't go anywhere bestie 🫢)

  • $Chipcard::PCSC::SCARD_ABSENT β€” card is absent (ghosted, literally not there πŸ’¨)
    • $Chipcard::PCSC::SCARD_PRESENT β€” card is present (she's here!! πŸŽ€)
    • $Chipcard::PCSC::SCARD_SWALLOWED β€” card not powered (sitting there, no vibes, no juice πŸ”‹)
    • $Chipcard::PCSC::SCARD_POWERED β€” card is powered (charged up, ready to chat ⚑)
    • $Chipcard::PCSC::SCARD_NEGOTIABLE β€” ready for PTS (down to negotiate, iykyk)
    • $Chipcard::PCSC::SCARD_SPECIFIC β€” PTS has been set (negotiation done, it's official πŸ‘‘)
  • $protocol β€” the protocol being used rn. Can be any of these besties:
    • $Chipcard::PCSC::SCARD_PROTOCOL_T0,
    • $Chipcard::PCSC::SCARD_PROTOCOL_T1,
    • $Chipcard::PCSC::SCARD_PROTOCOL_RAW
  • \@atr β€” a reference to an array holding the ATR. Every cell of that array = one byte of the ATR. But this parameter is optional, because the ATR might just not be available in some situations. Like, if the card isn't inserted, no ATR can come back and this parameter will be undef. No card, no ATR, no cap πŸ’€
$hCard->Transmit(\@data);

Transmit() is your whole thing for exchanging data with the card ✨

It returns a reference to an anonymous array holding the answer to the data you emitted. And if something goes wrong? The reference is just the undef value 🫠

  • \@data β€” a reference to the data being sent to the card.

Here's a little sample of how you use transmit:

 $SendData = [0x00, 0xA4, 0x01, 0x00, 0x02, 0x01, 0x00];
 $RecvData = $hCard->Transmit($SendData);

 print "  Recv = ";
 foreach $tmpVal (@{$RecvData}) {
     printf ("%02X ", $tmpVal);
 } print "\n";

$hCard->BeginTransaction();

BeginTransaction() is how you temporarily get exclusive control over the smart card. Like claiming the aux cord, but for a card πŸ”₯

It returns TRUE on a successful completion and FALSE otherwise. $Chipcard::PCSC::errno should be set accordingly if there's an error. Peep the section ERROR HANDLING in the Chipcard::PCSC man page for the full tea.

$hCard->EndTransaction($disposition);

EndTransaction() ends a transaction that was initiated with BeginTransaction(). Closing the group chat, basically 🫢

It returns "TRUE" upon successful completion and FALSE otherwise. $Chipcard::PCSC::errno should be set accordingly in case of an error. Again, section ERROR HANDLING in the Chipcard::PCSC man page has your back.

  • $disposition β€” the action to take when ending the transaction. Can be any of these:
    • $Chipcard::PCSC::SCARD_LEAVE_CARD β€” do nothing on close (leave her be 🧘)
    • $Chipcard::PCSC::SCARD_RESET_CARD β€” reset on close (fresh start era 🌸)
    • $Chipcard::PCSC::SCARD_UNPOWER_CARD β€” power down on close (nap time 😴)
    • $Chipcard::PCSC::SCARD_EJECT_CARD β€” eject on close (bye bestie πŸ‘‹)
$hCard->EndTransaction();

This method is literally equivalent to:

 $hCard->EndTransaction($Chipcard::PCSC::SCARD_LEAVE_CARD);

$hCard->TransmitWithCheck($apdu, $sw_expected [, $debug]);

This one is a wrapper around $hCard->Transmit(), no cap. The $apdu parameter is ASCII text like "00 A4 01 00 02 01 00", and $sw_expected is a Perl regular expression like "90 [01]0" ✨

If the status word that comes back matches the $sw_expected expression, the method returns a list ($sw, $recv). $sw is the status word (like "90 00") of the command, and $recv is the result of the command. Slay.

If the status word does not match the $sw_expected expression, the method returns undef and the variable $Chipcard::PCSC::Card::Error gets set 😭

The $debug argument is optional. If it's present, the method will print the command sent and the response from the card on stdout. Very much giving "show your work" energy.

Example:

 ($sw, $RecvData) = $hCard->TransmitWithCheck($SendData, "6E 00", 1);
 warn "TransmitWithCheck: $Chipcard::PCSC::Card::Error" unless defined $sw;

$hCard->Control($control_code, \@data);

This method uses PC/SC SCardControl() to send data that's specific to the reader driver. Check your driver documentation to know what data to use β€” I can't make that up for you, bestie πŸ™ˆ

Example:

 $data = Chipcard::PCSC::ascii_to_array ("01 23 45");
 $RecvData = $hCard->Control(0x42000001, $SendData);
 die ("Can't Control data: $Chipcard::PCSC::errno") unless (defined ($RecvData));

ISO7816Error($sw);

This method returns the ASCII text that corresponds to the status word $sw, according to ISO 7816-4 specifications. It's the translator of status words fr fr 🧚

Example:

 $sw = "90 00";
 print "$sw: " . &Chipcard::PCSC::Card::ISO7816Error($sw) . "\n";

πŸ‘€ SEE ALSO πŸ‘€

The pcscd man page has useful info about PC/SC-lite. And the Chipcard::PCSC man page holds all the necessary info to create the PCSC object, which is gonna be the basis of Chipcard::PCSC::Card. That's the origin story πŸ¦‹


(C) Lionel VICTOR, 2001, GNU GPL

(C) Ludovic ROUSSEAU, 2003-2008, GNU GPL


πŸ’– AUTHORS / ACKNOWLEDGEMENT πŸ’–

 Lionel VICTOR <lionel.victor@unforgettable.com>
               <lionel.victor@free.fr>

 Ludovic ROUSSEAU <ludovic.rousseau@free.fr>

And that's the whole page, bestie β€” you ate that πŸ“ This came from perl v5.40.2, dated 2026-07-22, and it's Card(3) in the flesh. Go forth and talk to your smart cards πŸ’…βœ¨

πŸ“¦ where this page comes from