Introduction

PCSC/Ada provides an Ada binding to PC/SC-middleware. The library allows programs written in Ada to communicate with smart cards using the SCard API.

PC/SC is a specification for SmartCard integration in computing environment. PC/SC is implemented in Microsoft Windows 200x/XP and available under Microsoft Windows NT/9x. A free implementation of PC/SC, PC/SC Lite, is available under Linux and bundled with Mac OS X.

Overview and Examples

PCSC/Ada provides a thin and a thick binding for the PC/SC API. The thin binding is not explained here though, since Ada programmers should not use this binding directly. It is much more convenient to use the thick binding provided by the PCSC.SCard package.

The following code is used to demonstrate how to establish a PC/SC context, connect to the first reader of a sytem and then send some arbitrary command to the smart card:

with PCSC.SCard;

use PCSC;

procedure Sample is
   Context : SCard.Context;
   --  PC/SC context
   Card    : SCard.Card;
   --  Card handle
   Readers : SCard.Reader_ID_Set;
   --  Set of readers
begin

   --  Establish context

   SCard.Establish_Context (Context => Context,
                            Scope   => SCard.Scope_System);

   --  List readers

   Readers := SCard.List_Readers (Context => Context);

   --  Connect to first reader

   SCard.Connect (Context => Context,
                  Card    => Card,
                  Reader  => Readers.First_Item,
                  Mode    => SCard.Share_Shared);

   --  Send APDU to card

   declare
      Recv_Buffer : SCard.Byte_Set (1 .. 10);
      Send_Buffer : constant SCard.Byte_Set
        := (16#00#, 16#A4#, 16#00#, 16#00#, 16#02#, 16#3F#, 16#00#);
      Recv_Length : Natural := 0;
      Recv_PCI    : SCard.IO_Request;
   begin
      SCard.Transmit (Card        => Card,
                      Send_Buffer => Send_Buffer,
                      Recv_Pci    => Recv_PCI,
                      Recv_Buffer => Recv_Buffer,
                      Recv_Len    => Recv_Length);
   end;

   --  Disconnect

   SCard.Disconnect (Card   => Card,
                     Action => SCard.Reset_Card);

   --  Release context

   SCard.Release_Context (Context => Context);
end Sample;

For more detailed examples on how to use PC/SC Ada in your own applications, examine the sample applications which are included in the distribution tarball examples directory (for information about the included examples, check the Examples section in the README).

Browse the source

  • You can browse the PCSC/Ada source code with gitweb here.

Licence

Copyright (C) 2008-2016 Reto Buerki

PCSC/Ada is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2.1 of the License, or (at your option) any later
version.