ruby-nfc 1.2 → 1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGMwZTM5MGEwYWFiODNjNWQ0YTBiMDBhMWNhZjQwOWE2NDdiYTE2ZQ==
4
+ ZDg5NmM4MjM1Zjg5NGQxMzI4OGZjYTUwYjVhNGYzMDQ4NzQ0NjU0Nw==
5
5
  data.tar.gz: !binary |-
6
- Yzc1MGE4MjZhMGE4OGM1YjdiODNhYzEyY2Q3YWQ0ZjcyNTYzOTRmNw==
6
+ MGNkNTBkMmE5YThiY2M0YWI3NDZiODRkOTQ5YjU2Y2MxYjcxZjA3NQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzIwODVjMTFmYjBkZDY3M2Q4ODE5YjJhYzY3NTU1ODZmNmEzMzBmNzEyMDE5
10
- MGYxY2Q1ZDRiNTJkNjlmMTZkMjUyMzUyMjc4YTk5YmZmY2M2Mzg3YjZjMGZm
11
- ZDJhYzQzZWY4MTQ3MzFmMTIwYWU0ZDc1YzNmNmQxYmI5MTdiZDg=
9
+ OGQwOGQzYmJjOTAxYmVkYjcwYzk4NTBhNzZkMWZjYzE0YTUyMjAxYmM0MzBk
10
+ NjZmMDkwYzkyZWQ2NzUzNjQyZTQyM2M0ZWJlYmIxODUxYjM0Y2ZiNjMwZWYw
11
+ MWQwMmI0NzAyM2FiMDdhODVlMzk4YjE0ZTYzM2M3ZTczMTRjOTI=
12
12
  data.tar.gz: !binary |-
13
- MjNmZmZiNDdjMTFkMGFkMWY0YjM0ZjVkN2MwNTkyZGExZTUwZTQ4YTcyN2Yx
14
- YTI5ZmJhMWVmMjdmOWE0ODIzNzE3YzY4M2ZlYjU2ZTg2MzMyMjMwZGZmNTJm
15
- MDk3NTY2NDUwOGU4NWExNDVjMzcyNWE0YzVhODYyZTJmM2I3NmY=
13
+ MThlODVkNjcyYjAxYzg2YTU1MGYwYTdlOTMyZmY3ZWEzYzhhODJlODM0NzQy
14
+ YjRlZTc0Mjg2ZmYyY2RlZWNkMzk5NWUyNWEyYzE4OTc4YjUxZDFkYjBhOGMz
15
+ ZDQ2MDc2MTA2YjBmZjdiODdlOWFmYmUxMTc1YzZlN2M2NWRiZjc=
@@ -7,58 +7,66 @@ module NFC
7
7
  @ptr = nil
8
8
  end
9
9
 
10
- def set_flag(name, value)
11
- LibNFC.nfc_device_set_property_bool(@ptr, name, value)
12
- end
13
-
14
10
  def connect
15
- @ptr ||= LibNFC.nfc_open(NFC.context, @name)
16
- raise NFC::Error.new('Cant connect to ' << @name) if @ptr.null?
11
+ unless @ptr
12
+ @ptr = LibNFC.nfc_open(NFC.context, @name)
13
+ raise NFC::Error.new("Cant connect to #{@name}") if @ptr.null?
14
+
15
+ LibNFC.nfc_initiator_init(@ptr)
16
+
17
+ set_flag(:NP_ACTIVATE_FIELD, false)
18
+ set_flag(:NP_HANDLE_CRC, true)
19
+ set_flag(:NP_HANDLE_PARITY, true)
20
+ set_flag(:NP_AUTO_ISO14443_4, true)
21
+ set_flag(:NP_ACTIVATE_FIELD, true)
22
+
23
+ @modulation = LibNFC::Modulation.new
24
+ @modulation[:nmt] = :NMT_ISO14443A
25
+ @modulation[:nbr] = :NBR_106
26
+ end
27
+
17
28
  self
18
29
  end
19
30
 
20
31
  # Returns list of tags applied to reader
21
- def discover(*card_types)
22
- # TODO: по правильному здесь надо делать низкоуровневый
23
- card_types.inject([]) do |tags, card_type|
24
- raise NFC::Error.new('Wrong card type') unless card_type.respond_to? :discover
25
- tags += card_type.discover(connect)
32
+ def discover(*tag_types)
33
+ connect
34
+
35
+ targets = FFI::MemoryPointer.new(:uchar, LibNFC::Target.size * 10)
36
+ res = LibNFC.nfc_initiator_list_passive_targets(@ptr, @modulation,
37
+ targets, 10)
38
+ 0.upto(res - 1).inject([]) do |tags, i|
39
+ target = LibNFC::Target.new(targets + i * LibNFC::Target.size)
40
+
41
+ tag_types.select{|type| type.match?(target)}.inject(tags) do |r, type|
42
+ r << type.new(target, self)
43
+ end
26
44
  end
27
45
  end
28
46
 
29
- def poll(*card_types, &block)
47
+ def poll(*tag_types, &block)
30
48
  connect
31
49
 
32
- LibNFC.nfc_initiator_init(@ptr) # we'll be initiator not a target
33
-
34
- set_flag(:NP_ACTIVATE_FIELD, false)
35
- set_flag(:NP_HANDLE_CRC, true)
36
- set_flag(:NP_HANDLE_PARITY, true)
37
- set_flag(:NP_AUTO_ISO14443_4, true)
38
- set_flag(:NP_ACTIVATE_FIELD, true)
39
-
40
- modulation = LibNFC::Modulation.new
41
- modulation[:nmt] = :NMT_ISO14443A
42
- modulation[:nbr] = :NBR_106
43
-
44
50
  targets = FFI::MemoryPointer.new(:uchar, LibNFC::Target.size * 10)
45
51
 
46
52
  loop do
47
- res = LibNFC.nfc_initiator_list_passive_targets(@ptr, modulation,
53
+ res = LibNFC.nfc_initiator_list_passive_targets(@ptr, @modulation,
48
54
  targets, 10)
49
55
 
50
- # iterate over all applied targets and iterate
56
+ # iterate over all applied targets
51
57
  0.upto(res - 1) do |i|
52
58
  target = LibNFC::Target.new(targets + i * LibNFC::Target.size)
53
59
  # iterate over requested card types for each target
54
60
  # notice that some targets can match several types i.e.
55
61
  # contactless credit cards (PayPass/payWave) with mifare chip
56
62
  # on board
57
- card_types.each do |card_type|
58
- if card_type.match?(target)
59
- tag = card_type.new(target, self)
63
+ tag_types.each do |tag_type|
64
+ if tag_type.match?(target)
65
+ tag = tag_type.new(target, self)
60
66
  tag.connect(&block)
61
- # if this tag was marked as processed - continue with next tag
67
+
68
+ # if this tag was marked as processed within the bloc -
69
+ # continue with the next tag
62
70
  break if target.processed?
63
71
  end
64
72
  end
@@ -81,5 +89,9 @@ module NFC
81
89
  names.map {|name| Reader.new(name)}
82
90
  end
83
91
  end
92
+
93
+ def set_flag(name, value)
94
+ LibNFC.nfc_device_set_property_bool(@ptr, name, value)
95
+ end
84
96
  end
85
97
  end
@@ -65,16 +65,6 @@ module IsoDep
65
65
  select(aid).raise_errno!
66
66
  end
67
67
 
68
- # Public: Select application with given AID (Application Identifier)
69
- #
70
- # aid - Application Identifier of an applet located on a card
71
- #
72
- # Returns nothing.
73
- # Raises APDU::Errno if application with such AID doesn't exists on a card
74
- def select(aid)
75
- send_apdu!("\x00\xA4\x04\x00#{aid.size.chr}#{aid}")
76
- end
77
-
78
68
  # Public: Send APDU command to tag
79
69
  #
80
70
  # apdu - APDU command to send. see ISO/IEC 7816-4 or wiki for details.
@@ -6,10 +6,10 @@ module NFC
6
6
  @processed = false
7
7
  end
8
8
 
9
- def connect(&block)
9
+ def connect
10
10
  if block_given?
11
11
  begin
12
- self.instance_eval(&block)
12
+ yield self
13
13
  ensure
14
14
  disconnect
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nfc
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Chechel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-23 00:00:00.000000000 Z
11
+ date: 2015-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi