PCSC sample in Prolog

Here is the PCSC sample in Prolog language I promised in PC/SC sample in different languages.

Renaud Mariana wrote a PC/SC wrapper for GNU Prolog. The latest version is 1.01 dated 2003 and is available in GNU Prolog contributions PC/SC Lite interface for GNU Prolog.

Installation


You first need to install GNU Prolog:

apt-get install gprolog

Then apply some patches to the wrapper project, see my mail in the users-prolog mailing list. Then the compilation succeeds without any problem.

gplpcsc/contribs/gplpcsc$ make
gplc -c -C "-Wall -g `pkg-config --cflags libpcsclite`"  pcsc/muscle_c.c
gplc -o testapdu pcsc/muscle_c.o  -L "`pkg-config --libs libpcsclite` -lcrypto -lpthread"  testapdu.pl util_pcsc/util.pl

Source code


:- include('pcsc/muscle').

start :-
    scard_establish_context,
    scard_connect,

    % print ATR data
    scard_status(_, Atr),
    print('ATR: '), print_hex_list(Atr), nl, 

    % enable trace
    scard_enable_verbose(true),

    Commande = [0,0xA4,4,0,0x0A,0xA0,0,0,0,0x62,3,1,0x0C,6,1],
    scard_transmit(Commande, _ApduOut1, _Sw1, _Time1),

    print_hex_list(_ApduOut1), 
    print_hex_list(_Sw1), nl,

    scard_transmit([0,0,0,0,0], _ApduOut2, _Sw2, _Time2),

    print_hex_list(_ApduOut2),
    print_hex_list(_Sw2), nl,
    print_ASCII_list(_ApduOut2), nl.

print_ASCII_list(L) :-
    ( list(L) ->
        print('['), '$print_ASCII_list'(L,[]), print(']')
    ;
        print('print_ASCII_list error, argument is not a list')
    ).

'$print_ASCII_list' --> [], !.
'$print_ASCII_list' --> [A], { format('%c',[A]) },
    '$print_ASCII_list'.

:- initialization(start).

Output


ATR: [3B FA 94 00 00 81 31 20 43 80 65 A2 01 01 01 3D 72 D6 43 21 ]
tr: 00a404000aa00000006203010c0601 - 9000, t= 15ms.
[][90 00 ]
tr: 0000000000 - 48656c6c6f20776f726c64219000, t= 23ms.
[48 65 6C 6C 6F 20 77 6F 72 6C 64 21 ][90 00 ]
[Hello world!]
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- 

Conclusion


Nothing more to add. If you are a Prolog user you may be interested by this wrapper.