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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby-nfc/reader.rb', line 73

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

Raises:



14
15
16
17
18
# File 'lib/ruby-nfc/reader.rb', line 14

def connect
  @ptr ||= LibNFC.nfc_open(NFC.context, @name)
  raise NFC::Error.new('Cant connect to ' << @name) if @ptr.null?
  self
end

#discover(*card_types) ⇒ Object

Returns list of tags applied to reader



21
22
23
24
25
26
27
# File 'lib/ruby-nfc/reader.rb', line 21

def discover(*card_types)
  # TODO: по правильному здесь надо делать низкоуровневый
  card_types.inject([]) do |tags, card_type|
    raise NFC::Error.new('Wrong card type') unless card_type.respond_to? :discover
    tags += card_type.discover(connect)
  end
end

#poll(*card_types, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby-nfc/reader.rb', line 29

def poll(*card_types, &block)
	connect

			LibNFC.nfc_initiator_init(@ptr) # we'll be initiator not a target

  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

  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 and iterate
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
	card_types.each do |card_type|
		if card_type.match?(target)
			tag = card_type.new(target, self)
			tag.connect(&block)
			# if this tag was marked as processed - continue with next tag
			break if target.processed?
		end
	end
end # upto
			end # loop
end

#set_flag(name, value) ⇒ Object



10
11
12
# File 'lib/ruby-nfc/reader.rb', line 10

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

#to_sObject



69
70
71
# File 'lib/ruby-nfc/reader.rb', line 69

def to_s
  @name
end