Skip to content

Commit

Permalink
some fixes and example update
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Chechel committed Oct 10, 2014
1 parent 6532e8b commit 5d7e615
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
9 changes: 6 additions & 3 deletions examples/listen.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'ruby-nfc'
require 'logger'

logger = Logger.new(STDOUT)

puts "Library version: %s" % NFC.version
readers = NFC::Reader.all
Expand All @@ -24,17 +27,17 @@
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
# puts response.unpack('H*').pop
processed!
end
rescue Exception => e
puts e
logger.debug e
end
end
2 changes: 1 addition & 1 deletion lib/ruby-nfc/apdu/apdu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/ruby-nfc/apdu/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def data
end

def to_s
@response.unpack('H*').pop
@response.unpack('H*').pop.upcase
end

def [](index)
Expand Down
28 changes: 21 additions & 7 deletions lib/ruby-nfc/tags/isodep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d7e615

Please sign in to comment.