Module: Tem::Apdus::Buffers

Included in:
Session
Defined in:
lib/tem/apdus/buffers.rb

Instance Method Summary collapse

Instance Method Details

#alloc_buffer(length) ⇒ Object



12
13
14
15
16
# File 'lib/tem/apdus/buffers.rb', line 12

def alloc_buffer(length)
  response = @transport.iso_apdu! :ins => 0x20,
                                     :p12 => to_tem_short(length)
  return read_tem_byte(response, 0)
end

#flush_buffersObject



22
23
24
# File 'lib/tem/apdus/buffers.rb', line 22

def flush_buffers
  @transport.iso_apdu! :ins => 0x26
end

#get_buffer_length(buffer_id) ⇒ Object



26
27
28
29
# File 'lib/tem/apdus/buffers.rb', line 26

def get_buffer_length(buffer_id)
  response = @transport.iso_apdu! :ins => 0x22, :p1 => buffer_id
  return read_tem_short(response, 0)
end

#guess_buffer_chunk_sizeObject



59
60
61
# File 'lib/tem/apdus/buffers.rb', line 59

def guess_buffer_chunk_size
  @buffer_chunk_size ||= guess_buffer_chunk_size!
end

#guess_buffer_chunk_size!Object



63
64
65
66
# File 'lib/tem/apdus/buffers.rb', line 63

def guess_buffer_chunk_size!
  response = @transport.iso_apdu! :ins => 0x25
  return read_tem_short(response, 0)
end

#post_buffer(data) ⇒ Object



90
91
92
93
94
# File 'lib/tem/apdus/buffers.rb', line 90

def post_buffer(data)
  buffer_id = alloc_buffer data.length
  write_buffer buffer_id, data
  return buffer_id
end

#read_buffer(buffer_id) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tem/apdus/buffers.rb', line 31

def read_buffer(buffer_id)
  guess_buffer_chunk_size
  
  buffer = []
  chunk_id = 0
  while true do
    response = @transport.iso_apdu! :ins => 0x23, :p1 => buffer_id,
                                      :p2 => chunk_id
    buffer += response
    break if response.length != @buffer_chunk_size
    chunk_id += 1
  end
  return buffer
end

#release_buffer(buffer_id) ⇒ Object



18
19
20
# File 'lib/tem/apdus/buffers.rb', line 18

def release_buffer(buffer_id)
  @transport.iso_apdu! :ins => 0x21, :p1 => buffer_id
end

#stat_buffersObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tem/apdus/buffers.rb', line 68

def stat_buffers
  response = @transport.iso_apdu! :ins => 0x27
  
  memory_types = [:persistent, :clear_on_reset, :clear_on_deselect]
  stat = {:free => {}, :buffers => []}
  memory_types.each_with_index { |mt, i| stat[:free][mt] = read_tem_short(response, i * 2) }
  offset = 6
  i = 0
  while offset < response.length do
    stat[:buffers][i] =
      {:type => memory_types[read_tem_ubyte(response, offset) & 0x1F],
      :pinned => (read_tem_ubyte(response, offset) & 0x80) != 0,
      :free => (read_tem_ubyte(response, offset) & 0x40) == 0,
      :public => (read_tem_ubyte(response, offset) & 0x20) != 0,
      :length => read_tem_ushort(response, offset + 1), 
      :xlength => read_tem_ushort(response, offset + 3)}
    offset += 5
    i += 1
  end
  return stat
end

#write_buffer(buffer_id, data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tem/apdus/buffers.rb', line 46

def write_buffer(buffer_id, data)
  guess_buffer_chunk_size
  
  chunk_id, offset = 0, 0
  while offset < data.length do
    write_size = [data.length - offset, @buffer_chunk_size].min
    @transport.iso_apdu! :ins => 0x24, :p1 => buffer_id, :p2 => chunk_id,
                            :data => data[offset, write_size]
    chunk_id += 1
    offset += write_size
  end
end