Class: APDU::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-nfc/apdu/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#claObject

Returns the value of attribute cla.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def cla
  @cla
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def data
  @data
end

#insObject

Returns the value of attribute ins.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def ins
  @ins
end

#lcObject

Returns the value of attribute lc.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def lc
  @lc
end

#leObject

Returns the value of attribute le.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def le
  @le
end

#p1Object

Returns the value of attribute p1.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def p1
  @p1
end

#p2Object

Returns the value of attribute p2.



5
6
7
# File 'lib/ruby-nfc/apdu/request.rb', line 5

def p2
  @p2
end

Class Method Details

.from_hex_string(apdu) ⇒ Object

Raises:



28
29
30
31
# File 'lib/ruby-nfc/apdu/request.rb', line 28

def self.from_hex_string(apdu)
  raise APDU::Error, "Wrong format" if apdu !~ /^([a-fA-F0-9]{2}){5,128}$/
    from_string([apdu].pack('H*'))
end

.from_string(apdu) ⇒ Object

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby-nfc/apdu/request.rb', line 7

def self.from_string(apdu)
	raise APDU::Error, "APDU is too short: #{apdu.size}" if apdu.size < 5

	apdu_8bit = apdu.dup
	apdu_8bit.force_encoding('ASCII-8BIT')
	
  req = self.new
  req.cla, req.ins, req.p1, req.p2, req.lc, req.data = apdu.unpack('CCCCCA*')

  if req.data.size == req.lc
    req.le = 0
  elsif req.data.size == req.lc + 1
    req.le = req.data[-1,1].ord
    req.data = req.data[0...-1]
  else
    raise APDU::Error, "Wrong Lc or wrong command data length"
  end

    req
end

Instance Method Details

#buildObject

Public: Build APDU command



34
35
36
# File 'lib/ruby-nfc/apdu/request.rb', line 34

def build
    [self.to_s].pack('H*')
end

#to_sObject



38
39
40
# File 'lib/ruby-nfc/apdu/request.rb', line 38

def to_s
    [cla, ins, p1, p2, lc, data, le].pack('CCCCCA*C').unpack('H*').pop.upcase
end