Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Chechel committed Oct 15, 2014
1 parent 6ef14df commit 8db319c
Showing 1 changed file with 66 additions and 50 deletions.
116 changes: 66 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ About ruby-nfc
========

This gem brings some NFC functionality for Ruby programming language.
It allows to:
It allows you to:
* Read and modify contents of Mifare tags
* Send APDU commands to Android HCE or Blackberry VTE devices
* Communicate with dual-interface smart cards like Master Card PayPass or Visa payWave cards
Expand All @@ -23,7 +23,7 @@ Prerequisites
# make && make install
```

Or if you're using Ubuntu Utopic Unicorn or higher version еnable "Universe" repository and then run:
Or if you're using Ubuntu Utopic Unicorn or a higher version еnable "Universe" repository and then run:
```
sudo apt-get install libfreefare-bin
```
Expand All @@ -41,10 +41,10 @@ Prerequisites
# ./configure && make && make install
```

* Look at lsusb output and make sure that your reader is present in 42-pn53x.rules
* Install appropriate driver for your NFC reader (if required)
* If your reader is plugged in unplug it and plug it again. If you run example below and get "Device or resource busy" error then you need to reboot your system before you can continue.
* If you getting "Unable to claim USB interface (Operation not permitted)" error then changing MODE from 0664 to 0666 in 42-pn53x.rules may help
* Look at the lsusb output and make sure that your reader is present in 42-pn53x.rules file
* Install the appropriate driver for your NFC reader (if required)
* If your reader is plugged in unplug it and plug it in again. If you run the example below and get "Device or resource busy" error then you need to reboot your system before you can continue.
* If you getting "Unable to claim USB interface (Operation not permitted)" error then changing MODE from 0664 to 0666 in 42-pn53x.rules file may help

Installation
------------
Expand All @@ -57,69 +57,85 @@ Usage

```ruby
require 'ruby-nfc'
require 'logger'

puts "Library version: %s" % NFC.version
$logger = Logger.new(STDOUT)

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}"

case tag
when Mifare::Classic::Tag
tag.select do
# 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
end
end
when Mifare::Ultralight::Tag
tag.select do
puts "Page 1: %s" % read(1).unpack('H*').pop
processed!
end
when IsoDep::Tag
aid = ["F75246544101"].pack('H*')
tag.select(aid) do
p "Applied #{tag.class.name}: #{tag}"

case tag
when Mifare::Classic::Tag
if auth(4, :key_a, "FFFFFFFFFFFF")
# 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
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).unpack('H*').pop
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
end
rescue Exception => e
puts e
p e
end
end

```

This example should provide similar output when you apply your tags to NFC-reader:
This example should provide similar output when you apply your tags to the NFC-reader:
```
Library version: 1.7.1
Available readers: [acr122_usb:001:009]
Applied Mifare::Classic::Tag: bde74d1d Mifare Classic 4k SAK: 0x18
authenticated!
Applied Mifare::Classic::Tag: 25161b49 Infineon Mifare Classic 1k SAK: 0x88
authenticated!
Applied Mifare::Ultralight::Tag: 04a42572373080 Mifare UltraLight SAK: 0x0
Page 1: 72373080
Applied IsoDep::Tag: a0d98978
aeee833d6a26476221290c3e4978290cce67422257aa37fedeca655fe7c67a5636669529e676a7c53fa51b9af3ae62e631b6cbebd4a65228a2fbf9cfe8b860e5efc69000
Applied Mifare::Ultralight::Tag: 04ffc68aaa2b80 Mifare UltraLight SAK: 0x0
Page 1: 8aaa2b80
Applied IsoDep::Tag: 087a1ae3
Application not found: f75246544101
D, [2014-10-15T23:21:48.965893 #9846] DEBUG -- : Library version: 1.7.1
D, [2014-10-15T23:21:49.221798 #9846] DEBUG -- : Available readers: [acr122_usb:001:009]
D, [2014-10-15T23:21:53.393871 #9846] DEBUG -- : Applied IsoDep::Tag: a0d98978
D, [2014-10-15T23:21:53.507046 #9846] DEBUG -- : AEEE833D6A26476221290C3E4978290CCE67422257AA37FEDECA655FE7C67A5636669529E676A7C53FA51B9AF3AE62E631B6CBEBD4A65228A2FBF9CFE8B860E5EFC69000
D, [2014-10-15T23:21:53.597682 #9846] DEBUG -- : status word: 9000 data: aeee833d6a26476221290c3e4978290cce67422257aa37fedeca655fe7c67a5636669529e676a7c53fa51b9af3ae62e631b6cbebd4a65228a2fbf9cfe8b860e5efc6
D, [2014-10-15T23:22:03.037690 #9846] DEBUG -- : Applied Mifare::Classic::Tag: 25161b49 Infineon Mifare Classic 1k SAK: 0x88
D, [2014-10-15T23:22:03.058840 #9846] DEBUG -- : Contents of block 0x04: 8ad53ef7f0f4055bf218b418a3fc884b
D, [2014-10-15T23:22:03.075083 #9846] DEBUG -- : New value: 57597505950dc90e569b8bee19852e6e
D, [2014-10-15T23:22:06.436528 #9846] DEBUG -- : Applied Mifare::Classic::Tag: 25161b49 Infineon Mifare Classic 1k SAK: 0x88
D, [2014-10-15T23:22:06.457830 #9846] DEBUG -- : Contents of block 0x04: 57597505950dc90e569b8bee19852e6e
D, [2014-10-15T23:22:06.473809 #9846] DEBUG -- : New value: 05f42ee777e08631cffbb5c8edadbb6d
D, [2014-10-15T23:22:10.001636 #9846] DEBUG -- : Applied Mifare::Classic::Tag: 25161b49 Infineon Mifare Classic 1k SAK: 0x88
D, [2014-10-15T23:22:10.022172 #9846] DEBUG -- : Contents of block 0x04: 05f42ee777e08631cffbb5c8edadbb6d
D, [2014-10-15T23:22:10.039478 #9846] DEBUG -- : New value: f31a513ff3c6cad190fdd570e67259b1
D, [2014-10-15T23:22:13.365357 #9846] DEBUG -- : Applied Mifare::Ultralight::Tag: 04a42572373080 Mifare UltraLight SAK: 0x0
D, [2014-10-15T23:22:13.376352 #9846] DEBUG -- : Page 1: 72373080
D, [2014-10-15T23:22:22.158846 #9846] DEBUG -- : Applied Mifare::Classic::Tag: bde74d1d Mifare Classic 4k SAK: 0x18
D, [2014-10-15T23:22:22.180474 #9846] DEBUG -- : Contents of block 0x04: 00000000000000000000000000000000
D, [2014-10-15T23:22:22.195963 #9846] DEBUG -- : New value: 8a5b666d6b53bb7c3c52e3d2a076da8b
D, [2014-10-15T23:22:27.187482 #9846] DEBUG -- : Applied Mifare::Classic::Tag: bde74d1d Mifare Classic 4k SAK: 0x18
D, [2014-10-15T23:22:27.208464 #9846] DEBUG -- : Contents of block 0x04: 8a5b666d6b53bb7c3c52e3d2a076da8b
D, [2014-10-15T23:22:27.223680 #9846] DEBUG -- : New value: 35adbc1378afd3d48af367809fd4491d
D, [2014-10-15T23:22:31.756429 #9846] DEBUG -- : Applied Mifare::Classic::Tag: bde74d1d Mifare Classic 4k SAK: 0x18
D, [2014-10-15T23:22:31.777233 #9846] DEBUG -- : Contents of block 0x04: 35adbc1378afd3d48af367809fd4491d
D, [2014-10-15T23:22:31.792688 #9846] DEBUG -- : New value: 46b613dae824cd84363340a45805f9a4
```

Debugging
Expand Down

0 comments on commit 8db319c

Please sign in to comment.