Class: IsoDep::Tag

Inherits:
NFC::Tag show all
Defined in:
lib/ruby-nfc/tags/isodep.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NFC::Tag

#initialize, #present?, #processed!, #processed?, #to_s, #uid, #uid_hex

Constructor Details

This class inherits a constructor from NFC::Tag

Class Method Details

.match?(target) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ruby-nfc/tags/isodep.rb', line 13

def self.match?(target)
	target[:nti][:nai][:btSak] & IsoDep::ISO_14443_4_COMPATIBLE > 0
end

Instance Method Details

#connect(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-nfc/tags/isodep.rb', line 17

def connect(&block)
 	@reader.set_flag(:NP_AUTO_ISO14443_4, true)

			modulation = LibNFC::Modulation.new
			modulation[:nmt] = :NMT_ISO14443A
			modulation[:nbr] = :NBR_106

			nai_ptr = @target[:nti][:nai].pointer

			# abt + sak + szUidLen offset
			uid_ptr = nai_ptr + FFI.type_size(:uint8) * 3 + FFI.type_size(:size_t)

			res = LibNFC.nfc_initiator_select_passive_target(
@reader.ptr,
modulation,
uid_ptr,
uid.length,
@target.pointer
			)

			if res > 0
super(&block)
			else
raise IsoDep::Error, "Can't select tag: #{res}"
			end
end

#disconnectObject



44
45
46
# File 'lib/ruby-nfc/tags/isodep.rb', line 44

def disconnect
			0 == LibNFC.nfc_initiator_deselect_target(@reader.ptr)
end

#select(aid) ⇒ Object

Public: Select application with given AID (Application Identifier)

aid - Application Identifier of an applet located on a card

Returns nothing. Raises APDU::Errno if application with such AID doesn’t exists on a card



53
54
55
# File 'lib/ruby-nfc/tags/isodep.rb', line 53

def select(aid)
			send_apdu("\x00\xA4\x04\x00#{aid.size.chr}#{aid}")
end

#select!(aid) ⇒ Object

Public: same as select but raises an APDU::Errno exception if application not present on the card or SW is not equal to 0x9000

aid - Identifier of the application that should be selected

Returns APDU::Response Raises APDU::Errno



64
65
66
# File 'lib/ruby-nfc/tags/isodep.rb', line 64

def select!(aid)
	select(aid).raise_errno!
end

#send_apdu(apdu) ⇒ Object Also known as: <<

Public: Send APDU command to tag

apdu - APDU command to send. see ISO/IEC 7816-4 or wiki for details. APDU is a binary string that should

Returns APDU::Response object Raises IsoDep::Error if card didn’t respond

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby-nfc/tags/isodep.rb', line 86

def send_apdu(apdu)
	cmd = apdu
	cmd.force_encoding('ASCII-8BIT')
			command_buffer = FFI::MemoryPointer.new(:uint8, cmd.length)
			command_buffer.write_string_length(cmd, cmd.length)

			response_buffer = FFI::MemoryPointer.new(:uint8, 254)

			res_len = LibNFC.nfc_initiator_transceive_bytes(@reader.ptr,
command_buffer, cmd.length, response_buffer, 254, 0)

			raise IsoDep::Error, "APDU sending failed: #{res_len}" if res_len < 0

			APDU::Response.new(response_buffer.get_bytes(0, res_len).to_s)
end

#send_apdu!(apdu) ⇒ Object

Public: Send APDU command to tag and raises APDU::Errno exception if SW not equal to 0x9000

apdu - APDU command to transmit to the tag

Returns APDU::Response object Raises APDU::Errno if SW is not equal to 0x9000



109
110
111
# File 'lib/ruby-nfc/tags/isodep.rb', line 109

def send_apdu!(apdu)
	send_apdu(apdu).raise_errno!
end