Class: Smartcard::Iso::PcscTransport

Inherits:
Object
  • Object
show all
Includes:
IsoCardMixin
Defined in:
lib/smartcard/iso/pcsc_transport.rb

Overview

Implements the transport layer for a smartcard connected to a PC/SC reader.

Constant Summary

PCSC =
Smartcard::PCSC

Instance Method Summary (collapse)

Methods included from IsoCardMixin

deserialize_response, #iso_apdu, #iso_apdu!, serialize_apdu

Constructor Details

- (PcscTransport) initialize(options)



18
19
20
21
22
23
# File 'lib/smartcard/iso/pcsc_transport.rb', line 18

def initialize(options)
  @options = options
  @context = nil
  @card = nil    
  @atr = nil
end

Instance Method Details

- (Object) card_atr



31
32
33
# File 'lib/smartcard/iso/pcsc_transport.rb', line 31

def card_atr
  @atr
end

- (Object) connect



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
# File 'lib/smartcard/iso/pcsc_transport.rb', line 35

def connect
  @context = PCSC::Context.new if @context.nil?
  
  if @options[:reader_name]
    @reader_name = @options[:reader_name]
  else
    # Get the first reader.
    readers = @context.readers
    @reader_name = readers[@options[:reader_index] || 0]
  end
  
  # Query the reader's status.
  queries = PCSC::ReaderStateQueries.new 1
  queries[0].reader_name = @reader_name
  queries[0].current_state = :unknown
  @context.wait_for_status_change queries, 100
  queries.ack_changes
  
  # Prompt for card insertion unless that already happened.
  unless queries[0].current_state.include? :present
    puts "Please insert smart-card card in reader #{@reader_name}\n"
    until queries[0].current_state.include? :presentt do
      @context.wait_for_status_change queries
      queries.ack_changes
    end
    puts "Card detected\n"
  end
  
  # Connect to the card.
  @card = @context.card @reader_name, :shared 
  @atr = @card.info[:atr]
end

- (Object) disconnect



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/smartcard/iso/pcsc_transport.rb', line 68

def disconnect
  unless @card.nil?
    @card.disconnect
    @card = nil
    @atr = nil
  end
  unless @context.nil?
    @context.release
    @context = nil
  end
end

- (Object) exchange_apdu(apdu)



25
26
27
28
29
# File 'lib/smartcard/iso/pcsc_transport.rb', line 25

def exchange_apdu(apdu)
  xmit_apdu_string = apdu.pack('C*')
  result_string = @card.transmit xmit_apdu_string
  return result_string.unpack('C*')
end

- (Object) to_s



80
81
82
83
# File 'lib/smartcard/iso/pcsc_transport.rb', line 80

def to_s
  "#<PC/SC Terminal: disconnected>" if @card.nil?
  "#<PC/SC Terminal: #{@reader_name}>"
end