Class: Smartcard::PCSC::FFILib::ReaderStateQuery

Inherits:
FFI::Struct
  • Object
show all
Defined in:
lib/smartcard/pcsc/ffi_structs.rb,
lib/smartcard/pcsc/reader_state_queries.rb

Overview

:nodoc: extends the reader states with nice accessors

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pack_state(unpacked_state) ⇒ Object

Packs an unpacked card state (symbol or set of symbols) into a number.

This should not be used by client code.



148
149
150
151
152
153
154
155
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 148

def self.pack_state(unpacked_state)
  if unpacked_state.kind_of? Enumerable
    state = 0
    unpacked_state.each { |bit| state |= FFILib::CardState[bit] }
    return state
  end
  FFILib::CardState[unpacked_state]
end

.unpack_state(packed_state) ⇒ Object

Unpacks a numeric card state into a Set of symbols.



158
159
160
161
162
163
164
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 158

def self.unpack_state(packed_state)
  state = Set.new
  FFILib::CardState.to_h.each do |bit, mask|
    state << bit if (packed_state & mask) == mask and mask != 0
  end
  state
end

Instance Method Details

#atrObject

The ATR of the smart-card in the query’s reader.

Smartcard::PCSC::Context#wait_for_status_change updates this value before it returns.

The value is a string containing the ATR bytes.



111
112
113
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 111

def atr    
  self[:atr].to_ptr.get_bytes 0, self[:atr_length]
end

#atr=(new_atr) ⇒ Object

Changes the smart-card ATR stored in the query.

Smartcard::PCSC::Context#wait_for_status_change updates this value before it returns.

The new value should be a string containing the ATR bytes.



121
122
123
124
125
126
127
128
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 121

def atr=(new_atr)
  if new_atr.length > max_length = FFILib::Consts::MAX_ATR_SIZE
    raise ArgumentError, "ATR above maximum length of #{max_length}"
  end
  
  self[:atr_length] = new_atr.length
  self[:atr].to_ptr.put_bytes 0, new_atr, 0, new_atr.length
end

#current_stateObject

The query’s current state.

Smartcard::PCSC::Context#wait_for_status_change blocks while the reader state equals this.

The value is a Set whose elements are FFILib::CardState members.



69
70
71
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 69

def current_state
  FFILib::ReaderStateQuery.unpack_state self[:current_state]
end

#current_state=(new_state) ⇒ Object

Changes the query’s current state.

Smartcard::PCSC::Context#wait_for_status_change blocks while the reader state equals this.

The new value can be a symbol in FFILib::CardState, or an Enumerable containing such symbols.



80
81
82
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 80

def current_state=(new_state)
  self[:current_state] = FFILib::ReaderStateQuery.pack_state new_state    
end

#event_stateObject

The query’s event state.

Smartcard::PCSC::Context#wait_for_status_change updates this value before it returns.

The value is a Set whose elements are FFILib::CardState members.



90
91
92
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 90

def event_state
  FFILib::ReaderStateQuery.unpack_state self[:event_state]
end

#event_state=(new_state) ⇒ Object

Changes the query’s event state.

Smartcard::PCSC::Context#wait_for_status_change updates this value before it returns.

The new value can be a symbol in FFILib::CardState, or an Enumerable containing such symbols.



101
102
103
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 101

def event_state=(new_state)
  self[:event_state] = FFILib::ReaderStateQuery.pack_state new_state
end

#reader_nameObject

The name of the reader referenceed by this query.

Smartcard::PCSC::Context#wait_for_status_change never changes this value.



133
134
135
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 133

def reader_name
  self[:reader_name].read_string
end

#reader_name=(new_name) ⇒ Object

Changes the name of the reader referenceed by this query.

Smartcard::PCSC::Context#wait_for_status_change never changes this value.



140
141
142
143
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 140

def reader_name=(new_name)    
  self[:reader_name].free if self[:reader_name].kind_of? FFI::MemoryPointer
  self[:reader_name] = FFI::MemoryPointer.from_string new_name
end