Module: Smartcard::Iso::JcopRemoteProtocol

Included in:
JcopRemoteServer, JcopRemoteTransport
Defined in:
lib/smartcard/iso/jcop_remote_protocol.rb

Overview

Mixin implementing the JCOP simulator protocol.

The (pretty informal) protocol specification is contained in the JavaDocs for the class com.ibm.jc.terminal.RemoteJCTerminal and should be easy to find by www.google.com/search?q=%22com.ibm.jc.terminal.RemoteJCTerminal%22

Instance Method Summary collapse

Instance Method Details

#recv_message(socket) ⇒ Object

Reads and decodes a JCOP simulator message from a TCP socket.

:call_seq:

client.read_message(socket) -> Hash or nil

If the other side of the TCP socket closes the connection, this method returns nil. Otherwise, a Hash is returned, with the format required by the JcopRemoteProtocol#send_message.



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

def recv_message(socket)
  header = ''
  while header.length < 4
    begin
      partial = socket.recv 4 - header.length
    rescue  # Abrupt hangups result in exceptions that we catch here.        
      return nil
    end
    return false if partial.length == 0
    header += partial
  end
  message_type, node_address, data_length = *header.unpack('CCn')
  raw_data = ''
  while raw_data.length < data_length
    begin
      partial = socket.recv data_length - raw_data.length
    rescue  # Abrupt hangups result in exceptions that we catch here.
      return nil
    end
    return false if partial.length == 0
    raw_data += partial
  end
  
  return false unless raw_data.length == data_length
  data = raw_data.unpack('C*')
  return { :type => message_type, :node => node_address, :data => data }
end

#send_message(socket, message) ⇒ Object

Encodes and sends a JCOP simulator message to a TCP socket.

The message must contain the following keys:

type:: Integer expressing the message type (e.g. 1 = APDU exchange)
node:: Integer expressing the node address (e.g. 0 for most purposes)
data:: message payload, as an array of Integers ranging from 0 to 255


23
24
25
26
27
# File 'lib/smartcard/iso/jcop_remote_protocol.rb', line 23

def send_message(socket, message)
  raw_message = [message[:type], message[:node], message[:data].length].
                pack('CCn') + message[:data].pack('C*')
  socket.send raw_message, 0
end