Class: ISO7816::Card::TCPCard
Overview
This implementation of card relies on socket communication, it expects an atr to be send on TCP connect
Constant Summary collapse
- SLEEP_TIME =
0.1
Instance Attribute Summary collapse
-
#addr ⇒ Object
Returns the value of attribute addr.
-
#port ⇒ Object
Returns the value of attribute port.
Attributes inherited from Card
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #in_range(byte, from, to) ⇒ Object
-
#initialize(addr = "127.0.0.1", port = 1024) ⇒ TCPCard
constructor
A new instance of TCPCard.
- #receive(le = nil) ⇒ Object
- #send(bytes, flags = 0) ⇒ Object
- #t0? ⇒ Boolean
Methods inherited from Card
Constructor Details
#initialize(addr = "127.0.0.1", port = 1024) ⇒ TCPCard
Returns a new instance of TCPCard.
175 176 177 178 |
# File 'lib/7816/card.rb', line 175 def initialize addr="127.0.0.1", port=1024 @addr = addr @port = port end |
Instance Attribute Details
#addr ⇒ Object
Returns the value of attribute addr.
174 175 176 |
# File 'lib/7816/card.rb', line 174 def addr @addr end |
#port ⇒ Object
Returns the value of attribute port.
174 175 176 |
# File 'lib/7816/card.rb', line 174 def port @port end |
Instance Method Details
#connect ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/7816/card.rb', line 180 def connect require 'socket' @socket = TCPSocket.new(@addr, @port) @connected = true sleep SLEEP_TIME @atr = @socket.recv 1024,0 if block_given? yield self disconnect end @atr end |
#connected? ⇒ Boolean
201 202 203 204 205 |
# File 'lib/7816/card.rb', line 201 def connected? return false unless @connected # we're still connected, check if peer is still available true end |
#disconnect ⇒ Object
194 195 196 197 198 199 |
# File 'lib/7816/card.rb', line 194 def disconnect @socket.close if @socket && @connected @connected = false @expect_sw=nil true end |
#in_range(byte, from, to) ⇒ Object
226 227 228 |
# File 'lib/7816/card.rb', line 226 def in_range byte, from, to byte >= from && byte <= to end |
#receive(le = nil) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/7816/card.rb', line 230 def receive le=nil raise "not connected" unless @connected if @expect_sw le=2 @expect_sw=nil end if le && Socket.const_defined?("MSG_WAITALL") # if we know the num bytes to receive, set MSG_WAIT_ALL flags = Socket::MSG_WAITALL elsif le data = "" data << @socket.recv(le, flags) while data.length < le return data else # set le to 1024 (default) and wait a little bit le = 1024 sleep SLEEP_TIME flags = 0 end #puts "Waiting to receive: #{le}" @socket.recv(le, flags) end |
#send(bytes, flags = 0) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/7816/card.rb', line 207 def send bytes, flags=0 raise "not connected" unless @connected bytes1 = bytes[0,5] bytes2 = bytes[5,bytes.length] @socket.send bytes1, flags # TCP2 echos back the ins byte, flags # according to ISO 7816-3 10.3.3 proc_byte = @socket.recv 1, Socket::MSG_PEEK # recv 90 60, INS, ... if proc_byte == "\x60" # null byte # elsif in_range(proc_byte, "\x61", "\x6f") || in_range(proc_byte, "\x90", "\x9f") @expect_sw=true else # INS being echoed back @socket.recv 1, 0 @socket.send bytes2,flags end end |
#t0? ⇒ Boolean
256 257 258 |
# File 'lib/7816/card.rb', line 256 def t0? true end |