-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more detailed example for IsoDep::Tag and Mifare::Classic::Tag, Mifar…
…e::Classic::Tag code cleanup
- Loading branch information
Maxim Chechel
committed
Oct 15, 2014
1 parent
f72195d
commit 6ef14df
Showing
2 changed files
with
55 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,50 @@ | ||
require 'ruby-nfc' | ||
require 'logger' | ||
|
||
logger = Logger.new(STDOUT) | ||
$logger = Logger.new(STDOUT) | ||
|
||
puts "Library version: %s" % NFC.version | ||
def p(str) | ||
$logger.debug str | ||
end | ||
|
||
p "Library version: #{NFC.version}" | ||
readers = NFC::Reader.all | ||
puts "Available readers: %s" % readers.to_s | ||
p "Available readers: #{readers}" | ||
|
||
# The order of tag types in poll arguments defines priority of tag types | ||
readers[0].poll(IsoDep::Tag, Mifare::Classic::Tag, Mifare::Ultralight::Tag) do |tag| | ||
begin | ||
puts "Applied #{tag.class.name}: #{tag}" | ||
p "Applied #{tag.class.name}: #{tag}" | ||
|
||
case tag | ||
when Mifare::Classic::Tag | ||
# Perform authentication to block 0x04 with the Key A that equals | ||
# to "\xFF\xFF\xFF\xFF\xFF\xFF" you can also use "FFFFFFFFFFFF" | ||
# representation. In this case it will be automatically packed to 6 bytes | ||
if auth(4, :key_a, "FFFFFFFFFFFF") | ||
puts "authenticated!" | ||
processed! # mark tag as processed so even if it supports different | ||
# protocol poll method will continue with another physical | ||
# tag | ||
# Mifare::Classic::Tag.read method reads contents of last authenticated | ||
# block | ||
p "Contents of block 0x04: #{tag.read.unpack('H*').pop}" | ||
# Making random 16-byte string | ||
rnd = Array.new(16).map{rand(255)}.pack('C*') | ||
tag.write(rnd) | ||
p "New value: #{rnd.unpack('H*').pop}" | ||
processed! | ||
else | ||
p "Authentication failed!" | ||
end | ||
when Mifare::Ultralight::Tag | ||
puts "Page 1: %s" % read(1).unpack('H*').pop | ||
when Mifare::Ultralight::Tag | ||
p "Page 1: #{read(1).unpack('H*').pop}" | ||
processed! | ||
when IsoDep::Tag | ||
select! ["F75246544101"].pack('H*') | ||
# sending APDU command to tag using send_apdu method | ||
apdu = ['A00D010018B455CAF0F331AF703EFA2E2D744EC7E22AA64076CD19F6D0'].pack('H*') | ||
puts send_apdu(apdu) | ||
p send_apdu(apdu) | ||
|
||
# sending APDU command with "<<" operator which is alias to send_apdu | ||
# response = tag << apdu | ||
# puts response.unpack('H*').pop | ||
response = tag << apdu | ||
p "status word: #{response.sw.to_s(16)} data: #{response.data.unpack('H*').pop}" | ||
processed! | ||
end | ||
rescue Exception => e | ||
logger.debug e | ||
p e | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters