Module Smartcard::Iso::AutoConfigurator
In: lib/smartcard/iso/auto_configurator.rb

Automatic configuration code.

Methods

Constants

ENVIRONMENT_VARIABLE_NAME = 'SCARD_PORT'   The name of the environment variable that might supply the transport configuration.
DEFAULT_CONFIGURATIONS = [ { :class => JcopRemoteTransport, :opts => { :host => '127.0.0.1', :port => 8050} }, { :class => PcscTransport, :opts => { :reader_index => 0 }}   The default configurations to be tried if no configuration is specified.

Public Class methods

Creates a transport based on available configuration information.

[Source]

    # File lib/smartcard/iso/auto_configurator.rb, line 24
24:   def self.auto_transport
25:     configuration = env_configuration
26:     return try_transport(configuration) if configuration
27:     
28:     DEFAULT_CONFIGURATIONS.each do |config|
29:       transport = try_transport(config)
30:       return transport if transport
31:     end
32:     return nil
33:   end

Retrieves transport configuration information from an environment variable.

The returned configuration has the keys required by AutoConfigurator#try_transport

[Source]

    # File lib/smartcard/iso/auto_configurator.rb, line 42
42:   def self.env_configuration
43:     return nil unless conf = ENV[ENVIRONMENT_VARIABLE_NAME]
44:     
45:     case conf[0]
46:     when ?:
47:       # :8050 -- JCOP emulator at port 8050
48:       transport_class = JcopRemoteTransport
49:       transport_opts = { :host => '127.0.0.1' }
50:       transport_opts[:port] = conf[1..-1].to_i
51:     when ?@
52:       # @127.0.0.1:8050 -- JCOP emulator at host 127.0.0.1 port 8050
53:       transport_class = JcopRemoteTransport
54:       port_index = conf.rindex(?:) || conf.length
55:       transport_opts = { :host => conf[1...port_index] }
56:       transport_opts[:port] = conf[(port_index + 1)..-1].to_i
57:     when ?#
58:       # #2 -- 2nd PC/SC reader in the system
59:       transport_class = PcscTransport
60:       transport_opts = { :reader_index => conf[1..-1].to_i - 1 }
61:     else
62:       # Reader Name -- the PC/SC reader with the given name
63:       transport_class = PcscTransport
64:       transport_opts = { :reader_name => conf }
65:     end
66:     
67:     transport_opts[:port] = 8050 if transport_opts[:port] == 0
68:     if transport_opts[:reader_index] and transport_opts[:reader_index] < 0
69:       transport_opts[:reader_index] = 0
70:     end
71:     { :class => transport_class, :opts => transport_opts }
72:   end

Attempts to create a new ISO7816 transport with the given configuration.

The configuration should have the following keys:

  class:: the Ruby class implementing the transport
  opts:: the options to be passed to the implementation's constructor

[Source]

    # File lib/smartcard/iso/auto_configurator.rb, line 81
81:   def self.try_transport(configuration)
82:     raise 'No transport class specified' unless configuration[:class]
83:     begin
84:       transport = configuration[:class].new(configuration[:opts] || {})
85:       transport.connect
86:       return transport
87:     rescue Exception
88:       return nil
89:     end
90:   end

[Validate]