diff --git a/examples/listen.rb b/examples/listen.rb index 82ce954..f1d84df 100644 --- a/examples/listen.rb +++ b/examples/listen.rb @@ -1,4 +1,7 @@ require 'ruby-nfc' +require 'logger' + +logger = Logger.new(STDOUT) puts "Library version: %s" % NFC.version readers = NFC::Reader.all @@ -24,10 +27,10 @@ puts "Page 1: %s" % read(1).unpack('H*').pop processed! when IsoDep::Tag - select ["F75246544101"].pack('H*') + select! ["F75246544101"].pack('H*') # sending APDU command to tag using send_apdu method apdu = ['A00D010018B455CAF0F331AF703EFA2E2D744EC7E22AA64076CD19F6D0'].pack('H*') - puts send_apdu(apdu).unpack('H*').pop + puts send_apdu(apdu) # sending APDU command with "<<" operator which is alias to send_apdu # response = tag << apdu @@ -35,6 +38,6 @@ processed! end rescue Exception => e - puts e + logger.debug e end end diff --git a/lib/ruby-nfc/apdu/apdu.rb b/lib/ruby-nfc/apdu/apdu.rb index 145f51b..f8e1612 100644 --- a/lib/ruby-nfc/apdu/apdu.rb +++ b/lib/ruby-nfc/apdu/apdu.rb @@ -86,7 +86,7 @@ def initialize(sw) @code = sw message = STATUS_STRINGS[sw] || "Unknown error" - super("#{sw.to_s(16)} #{message}") + super("#{sw.to_s(16).upcase} #{message}") end end end diff --git a/lib/ruby-nfc/apdu/response.rb b/lib/ruby-nfc/apdu/response.rb index 5f9aeca..fa3482d 100644 --- a/lib/ruby-nfc/apdu/response.rb +++ b/lib/ruby-nfc/apdu/response.rb @@ -38,7 +38,7 @@ def data end def to_s - @response.unpack('H*').pop + @response.unpack('H*').pop.upcase end def [](index) diff --git a/lib/ruby-nfc/tags/isodep.rb b/lib/ruby-nfc/tags/isodep.rb index cbb3d0c..34defcf 100644 --- a/lib/ruby-nfc/tags/isodep.rb +++ b/lib/ruby-nfc/tags/isodep.rb @@ -35,12 +35,6 @@ def connect(&block) ) if res > 0 - # trying to select applet if applet identifier was given - if aid - sw = send_apdu("\x00\xA4\x04\x00#{aid.size.chr}#{aid}") - raise IsoDep::Error, "Application not found: #{aid.unpack('H*').pop}" unless "\x90\x00" == sw - end - super(&block) else raise IsoDep::Error, "Can't select tag: #{res}" @@ -51,6 +45,26 @@ def disconnect 0 == LibNFC.nfc_initiator_deselect_target(@reader.ptr) end + # Public: select application with give AID + # + # aid - Identifier of the application that should be selected + # + # Returns APDU::Response + def select(aid) + send_apdu("\x00\xA4\x04\x00#{aid.size.chr}#{aid}") + end + + # 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 + def select!(aid) + select(aid).raise_errno! + end + # Public: Select application with given AID (Application Identifier) # # aid - Application Identifier of an applet located on a card @@ -82,7 +96,7 @@ def send_apdu(apdu) raise IsoDep::Error, "APDU sending failed: #{res_len}" if res_len < 0 - APDU::Response.from_string(response_buffer.get_bytes(0, res_len).to_s) + APDU::Response.new(response_buffer.get_bytes(0, res_len).to_s) end # Public: Send APDU command to tag and raises APDU::Errno exception