Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding crc and parity crap

  • Loading branch information...
commit ce1d53c67ff42da2478f710fc495d32825063b10 1 parent 5bbde11
@tenderlove authored
Showing with 48 additions and 7 deletions.
  1. +19 −3 lib/nfc.rb
  2. +6 −0 lib/nfc/lib_nfc.rb
  3. +23 −4 test/test_nfc.rb
View
22 lib/nfc.rb
@@ -1,3 +1,4 @@
+require 'singleton'
require 'nfc/lib_nfc'
class NFC
@@ -5,11 +6,26 @@ class NFC
attr_reader :device
+ include Singleton
+
def initialize
- @device = nil
+ @device = NFC::LibNFC::Device.new(NFC::LibNFC.nfc_connect)
+ LibNFC.nfc_reader_init(@device.pointer)
end
- def connect
- @device = NFC::LibNFC::Device.new(NFC::LibNFC.nfc_connect)
+ def deactivate_field
+ LibNFC.nfc_configure @device.pointer, LibNFC::DCO_ACTIVATE_FIELD, 0
+ end
+
+ def activate_field
+ LibNFC.nfc_configure @device.pointer, LibNFC::DCO_ACTIVATE_FIELD, 1
+ end
+
+ def crc= value
+ LibNFC.nfc_configure @device.pointer, LibNFC::DCO_HANDLE_CRC, value ? 1 : 0
+ end
+
+ def parity= v
+ LibNFC.nfc_configure @device.pointer, LibNFC::DCO_HANDLE_PARITY, v ? 1 : 0
end
end
View
6 lib/nfc/lib_nfc.rb
@@ -7,6 +7,12 @@ class LibNFC # :nodoc
attach_function :nfc_connect, [], :pointer
attach_function :nfc_disconnect, [:pointer], :void
+ attach_function :nfc_reader_init, [:pointer], :int
+ attach_function :nfc_configure, [:pointer, :int, :int], :int
+
+ DCO_HANDLE_CRC = 0x00
+ DCO_HANDLE_PARITY = 0x01
+ DCO_ACTIVATE_FIELD = 0x10
class Device < FFI::Struct
layout(:name, [:char, 256])
View
27 test/test_nfc.rb
@@ -2,10 +2,29 @@
require "nfc"
class TestNFC < Test::Unit::TestCase
+ def setup
+ @nfc = NFC.instance
+ end
+
def test_connect
- nfc = NFC.new
- assert_nil nfc.device
- nfc.connect
- assert_not_nil nfc.device
+ assert_not_nil @nfc.device
+ end
+
+ def test_deactivate_field
+ @nfc.deactivate_field
+ end
+
+ def test_activate_field
+ @nfc.activate_field
+ end
+
+ def test_crc=
+ @nfc.crc = true
+ @nfc.crc = false
+ end
+
+ def test_parity=
+ @nfc.parity = true
+ @nfc.parity = false
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.