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.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 175

def self.pack_state(unpacked_state)
  if unpacked_state.kind_of? Enumerable
    state = 0
    unpacked_state.each do |symbol_bit_or_number|
      if FFILib::CardState[symbol_bit_or_number]
        state |= FFILib::CardState[symbol_bit_or_number]
      else
        state |= symbol_bit_or_number
      end
    end
    return state
  end
  FFILib::CardState[unpacked_state]
end

.unpack_state(packed_state) ⇒ Object

Unpacks a numeric card state into a Set of symbols.

The returned Set may also have an Integer in it, if any of the bits set in the packed state are not covered by the mapping in FFILib::CardState.



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 194

def self.unpack_state(packed_state)
  state = Set.new
  FFILib::CardState.to_h.each do |symbol, mask|
    next if mask == 0
    if (packed_state & mask) == mask
      state << symbol
      packed_state ^= mask
    end
  end
  state << packed_state if packed_state != 0
  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.



138
139
140
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 138

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.



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

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

#changed?Boolean

Short cut to identify a query with changed event_state.

Returns:

  • (Boolean)


128
129
130
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 128

def changed?
  self.event_state.include? :changed
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.



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

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.



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

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.



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

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.



123
124
125
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 123

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.



160
161
162
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 160

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.



167
168
169
170
# File 'lib/smartcard/pcsc/reader_state_queries.rb', line 167

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