Class: NFC::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-nfc/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device_name) ⇒ Reader

Returns a new instance of Reader.



5
6
7
8
# File 'lib/ruby-nfc/reader.rb', line 5

def initialize(device_name)
  @name = device_name
  @ptr = nil
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



3
4
5
# File 'lib/ruby-nfc/reader.rb', line 3

def ptr
  @ptr
end

Class Method Details

.allObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby-nfc/reader.rb', line 81

def self.all
  ptr = FFI::MemoryPointer.new(:char, 1024 * 10)
  len = LibNFC.nfc_list_devices(NFC.context, ptr, 10)

  if len <= 0
    raise NFC::Error, "No compatible NFC readers found"
  else
    names = ptr.get_bytes(0, 1024 * len).split("\x00").reject {|e| e.empty?}
    names.map {|name| Reader.new(name)}
  end 
end

Instance Method Details

#connectObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby-nfc/reader.rb', line 10

def connect
  unless @ptr
    @ptr = LibNFC.nfc_open(NFC.context, @name)
    raise NFC::Error.new("Cant connect to #{@name}") if @ptr.null?

    LibNFC.nfc_initiator_init(@ptr)

    set_flag(:NP_ACTIVATE_FIELD, false)
    set_flag(:NP_HANDLE_CRC, true)
    set_flag(:NP_HANDLE_PARITY, true)
    set_flag(:NP_AUTO_ISO14443_4, true)
    set_flag(:NP_ACTIVATE_FIELD, true)

    @modulation = LibNFC::Modulation.new
 @modulation[:nmt] = :NMT_ISO14443A 
 @modulation[:nbr] = :NBR_106
  end

  self
end

#discover(*tag_types) ⇒ Object

Returns list of tags applied to reader



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby-nfc/reader.rb', line 32

def discover(*tag_types)
  connect

  targets = FFI::MemoryPointer.new(:uchar, LibNFC::Target.size * 10)
  res = LibNFC.nfc_initiator_list_passive_targets(@ptr, @modulation, 
                                                  targets, 10)
  0.upto(res - 1).inject([]) do |tags, i|
target = LibNFC::Target.new(targets + i * LibNFC::Target.size)

    tag_types.select{|type| type.match?(target)}.inject(tags) do |r, type|
      r << type.new(target, self)
    end
  end
end

#poll(*tag_types, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby-nfc/reader.rb', line 47

def poll(*tag_types, &block)
	connect

  targets = FFI::MemoryPointer.new(:uchar, LibNFC::Target.size * 10)

			loop do
		res = LibNFC.nfc_initiator_list_passive_targets(@ptr, @modulation, 
																										targets, 10)
			
# iterate over all applied targets
0.upto(res - 1) do |i|
	target = LibNFC::Target.new(targets + i * LibNFC::Target.size)
	# iterate over requested card types for each target
	# notice that some targets can match several types i.e.
	# contactless credit cards (PayPass/payWave) with mifare chip
	# on board
	tag_types.each do |tag_type|
		if tag_type.match?(target)
			tag = tag_type.new(target, self)
			tag.connect(&block)

			# if this tag was marked as processed within the bloc - 
			# continue with the next tag
			break if target.processed?
		end
	end
end # upto
			end # loop
end

#set_flag(name, value) ⇒ Object



93
94
95
# File 'lib/ruby-nfc/reader.rb', line 93

def set_flag(name, value)
			LibNFC.nfc_device_set_property_bool(@ptr, name, value)
end

#to_sObject



77
78
79
# File 'lib/ruby-nfc/reader.rb', line 77

def to_s
  @name
end