nfc 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/CHANGELOG.rdoc +6 -0
- data/Manifest.txt +9 -0
- data/README.rdoc +57 -0
- data/Rakefile +16 -0
- data/bin/nfc +9 -0
- data/lib/nfc.rb +60 -0
- data/lib/nfc/lib_nfc.rb +66 -0
- data/test/test_nfc.rb +39 -0
- metadata +89 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/CHANGELOG.rdoc
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
= NFC
|
2
|
+
|
3
|
+
* http://seattlerb.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
NFC is a ruby wrapper for the Near Field Communication library. The Near
|
8
|
+
Field Communication library works with many USB RFID readers, so this gem
|
9
|
+
lets you read RFID tags.
|
10
|
+
|
11
|
+
== FEATURES/PROBLEMS:
|
12
|
+
|
13
|
+
* Only supports ISO1443A tags (MIFARE) tags right now.
|
14
|
+
|
15
|
+
== SYNOPSIS:
|
16
|
+
|
17
|
+
require 'nfc'
|
18
|
+
|
19
|
+
# Read your tag and print the info
|
20
|
+
p NFC.instance.find
|
21
|
+
|
22
|
+
== REQUIREMENTS:
|
23
|
+
|
24
|
+
* A USB RFID reader. I'm using the touchatag[http://touchatag.com].
|
25
|
+
* ffi
|
26
|
+
* libnfc
|
27
|
+
|
28
|
+
== INSTALL:
|
29
|
+
|
30
|
+
* First install libnfc[http://libnfc.org/]
|
31
|
+
* Make sure libnfc.dylib or libnfc.so is in your library path
|
32
|
+
* gem install nfc
|
33
|
+
|
34
|
+
== LICENSE:
|
35
|
+
|
36
|
+
(The MIT License)
|
37
|
+
|
38
|
+
Copyright (c) 2009 Aaron Patterson
|
39
|
+
|
40
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
41
|
+
a copy of this software and associated documentation files (the
|
42
|
+
'Software'), to deal in the Software without restriction, including
|
43
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
44
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
45
|
+
permit persons to whom the Software is furnished to do so, subject to
|
46
|
+
the following conditions:
|
47
|
+
|
48
|
+
The above copyright notice and this permission notice shall be
|
49
|
+
included in all copies or substantial portions of the Software.
|
50
|
+
|
51
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
52
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
53
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
54
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
55
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
56
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
57
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
$: << "lib"
|
6
|
+
require 'nfc'
|
7
|
+
|
8
|
+
Hoe.new('nfc', NFC::VERSION) do |p|
|
9
|
+
p.developer('Aaron Patterson', 'aaronp@rubyforge.org')
|
10
|
+
p.readme_file = 'README.rdoc'
|
11
|
+
p.history_file = 'CHANGELOG.rdoc'
|
12
|
+
p.extra_rdoc_files = FileList['*.rdoc']
|
13
|
+
p.extra_deps = ['ffi']
|
14
|
+
end
|
15
|
+
|
16
|
+
# vim: syntax=Ruby
|
data/bin/nfc
ADDED
data/lib/nfc.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'nfc/lib_nfc'
|
3
|
+
|
4
|
+
class NFC
|
5
|
+
VERSION = '1.0.0'
|
6
|
+
|
7
|
+
attr_reader :device
|
8
|
+
|
9
|
+
include Singleton
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@device = NFC::LibNFC::Device.new(NFC::LibNFC.nfc_connect)
|
13
|
+
LibNFC.nfc_reader_init(@device.pointer)
|
14
|
+
end
|
15
|
+
|
16
|
+
def deactivate_field
|
17
|
+
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_ACTIVATE_FIELD, 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def activate_field
|
21
|
+
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_ACTIVATE_FIELD, 1
|
22
|
+
end
|
23
|
+
|
24
|
+
def crc= value
|
25
|
+
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_HANDLE_CRC, value ? 1 : 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def parity= v
|
29
|
+
LibNFC.nfc_configure @device.pointer, LibNFC::DCO_HANDLE_PARITY, v ? 1 : 0
|
30
|
+
end
|
31
|
+
|
32
|
+
def infinite_list_passive= v
|
33
|
+
LibNFC.nfc_configure(
|
34
|
+
@device.pointer,
|
35
|
+
LibNFC::DCO_INFINITE_LIST_PASSIVE,
|
36
|
+
v ? 1 : 0
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def poll_mifare
|
41
|
+
thing = LibNFC::ISO1443A.new
|
42
|
+
LibNFC.nfc_reader_list_passive(
|
43
|
+
@device.pointer,
|
44
|
+
LibNFC::IM_ISO14443A_106,
|
45
|
+
nil,
|
46
|
+
0,
|
47
|
+
thing
|
48
|
+
)
|
49
|
+
thing
|
50
|
+
end
|
51
|
+
|
52
|
+
def find
|
53
|
+
deactivate_field
|
54
|
+
self.infinite_list_passive = false
|
55
|
+
self.crc = true
|
56
|
+
self.parity = true
|
57
|
+
activate_field
|
58
|
+
poll_mifare
|
59
|
+
end
|
60
|
+
end
|
data/lib/nfc/lib_nfc.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
class NFC
|
4
|
+
class LibNFC
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib 'nfc'
|
7
|
+
|
8
|
+
attach_function :nfc_connect, [], :pointer
|
9
|
+
attach_function :nfc_disconnect, [:pointer], :void
|
10
|
+
attach_function :nfc_reader_init, [:pointer], :int
|
11
|
+
attach_function :nfc_configure, [:pointer, :int, :int], :int
|
12
|
+
attach_function :nfc_reader_list_passive, [:pointer, :int, :pointer, :int, :pointer], :int
|
13
|
+
|
14
|
+
DCO_HANDLE_CRC = 0x00
|
15
|
+
DCO_HANDLE_PARITY = 0x01
|
16
|
+
DCO_ACTIVATE_FIELD = 0x10
|
17
|
+
DCO_INFINITE_LIST_PASSIVE = 0x20
|
18
|
+
|
19
|
+
IM_ISO14443A_106 = 0x00
|
20
|
+
|
21
|
+
class Device < FFI::Struct
|
22
|
+
layout(:name, [:char, 256])
|
23
|
+
|
24
|
+
def name
|
25
|
+
pointer.read_string
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.release ptr
|
29
|
+
LibNFC.nfc_disconnect ptr
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ISO1443A < FFI::Struct
|
34
|
+
layout(
|
35
|
+
:abtAtqa, [:uchar, 2],
|
36
|
+
:btSak, :uchar,
|
37
|
+
:uiUidLen, :int,
|
38
|
+
:abtUid, [:uchar, 10],
|
39
|
+
:uiAtsLen, :int,
|
40
|
+
:abtAts, [:uchar, 36]
|
41
|
+
)
|
42
|
+
|
43
|
+
def uid
|
44
|
+
self[:abtUid].to_a.slice(0, self[:uiUidLen])
|
45
|
+
end
|
46
|
+
|
47
|
+
def inspect
|
48
|
+
uid = sprintf((['%02x'] * self[:uiUidLen]).join(' '), *self.uid)
|
49
|
+
|
50
|
+
string_ary =
|
51
|
+
[ "(NFC) ISO14443A Tag",
|
52
|
+
" ATQA (SENS_RES): #{sprintf("%02x %02x", *(self[:abtAtqa]))}",
|
53
|
+
" UID (NFCID1): #{uid}",
|
54
|
+
" SAK (SEL_RES): #{sprintf("%02x", self[:btSak])}"
|
55
|
+
]
|
56
|
+
if self[:uiAtsLen] > 0
|
57
|
+
ats =
|
58
|
+
sprintf((['%02x'] * self[:uiAtsLen]).join(' '), *(self[:abtAts]))
|
59
|
+
string_ary <<
|
60
|
+
" ATS (ATR): #{ats}"
|
61
|
+
end
|
62
|
+
string_ary.join "\n"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/test/test_nfc.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "nfc"
|
3
|
+
|
4
|
+
class TestNFC < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@nfc = NFC.instance
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_connect
|
10
|
+
assert_not_nil @nfc.device
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_deactivate_field
|
14
|
+
@nfc.deactivate_field
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_activate_field
|
18
|
+
@nfc.activate_field
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_crc=
|
22
|
+
@nfc.crc = true
|
23
|
+
@nfc.crc = false
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_parity=
|
27
|
+
@nfc.parity = true
|
28
|
+
@nfc.parity = false
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_infinite_list_passive=
|
32
|
+
@nfc.infinite_list_passive = true
|
33
|
+
@nfc.infinite_list_passive = false
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_poll_mifare
|
37
|
+
thing = @nfc.poll_mifare
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nfc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Patterson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-01 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ffi
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.12.1
|
34
|
+
version:
|
35
|
+
description: |-
|
36
|
+
NFC is a ruby wrapper for the Near Field Communication library. The Near
|
37
|
+
Field Communication library works with many USB RFID readers, so this gem
|
38
|
+
lets you read RFID tags.
|
39
|
+
email:
|
40
|
+
- aaronp@rubyforge.org
|
41
|
+
executables:
|
42
|
+
- nfc
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files:
|
46
|
+
- Manifest.txt
|
47
|
+
- CHANGELOG.rdoc
|
48
|
+
- README.rdoc
|
49
|
+
files:
|
50
|
+
- .autotest
|
51
|
+
- CHANGELOG.rdoc
|
52
|
+
- Manifest.txt
|
53
|
+
- README.rdoc
|
54
|
+
- Rakefile
|
55
|
+
- bin/nfc
|
56
|
+
- lib/nfc.rb
|
57
|
+
- lib/nfc/lib_nfc.rb
|
58
|
+
- test/test_nfc.rb
|
59
|
+
has_rdoc: true
|
60
|
+
homepage: http://seattlerb.rubyforge.org
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --main
|
66
|
+
- README.rdoc
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: "0"
|
80
|
+
version:
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: nfc
|
84
|
+
rubygems_version: 1.3.3
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: NFC is a ruby wrapper for the Near Field Communication library
|
88
|
+
test_files:
|
89
|
+
- test/test_nfc.rb
|