Subject:
[ruby-ffi] libnfc example troubles
From:
Anders Konring Olesen
Date:
5/26/13 3:09 PM
To:
ruby-ffi@googlegroups.com

Hi everyone. 

i need some backup to get going with my project trying to make ruby-wrapper for libnfc:

Test program(trying to simulate this c example:):
 
require 'ffi'
load '~/Documents/nfc/nfc_library.rb'

device_ptr = FFI::MemoryPointer.new :pointer
context_ptr = FFI::MemoryPointer.new :pointer
target = NfcLibrary::NfcTarget.new

NfcLibrary::nfc_init(context_ptr)
device_ptr = NfcLibrary::nfc_open(context_ptr.read_pointer, nil)


NfcLibrary::nfc_initiator_init(device_ptr)
    
nmMifare = NfcLibrary::NfcModulation.new
nmMifare[:nmt] = :NMT_ISO14443A
nmMifare[:nbr] = :NBR_106

device_name = NfcLibrary::nfc_device_get_name(device_ptr)

puts device_name

NfcLibrary::nfc_initiator_select_passive_target(device_ptr, nmMifare, nil, 0, target.pointer)

Output of testprogram(connecting to nfc interface but will call the last method with succes) :
ACS / ACR122U PICC Interface
-2

My FFI library:
module NfcLibrary
  extend FFI::Library
  ffi_lib '/usr/local/Cellar/libnfc/1.7.0-rc1/lib/libnfc.4.dylib'

  typedef :pointer, :nfc_device
  typedef :pointer, :nfc_context
  enum :NfcModulationType,
  [:NMT_ISO14443A, 1,
   :NMT_JEWEL,
  :NMT_ISO14443B,
  :NMT_ISO14443BI, 
  :NMT_ISO14443B2SR, 
  :NMT_ISO14443B2CT, 
  :NMT_FELICA,
  :NMT_DEP]

  enum :NfcBaudRate,
  [:NBR_UNDEFINED, 0,
  :NBR_106,
  :NBR_212,
  :NBR_424,
  :NBR_847]

  class NfcIso14443aInfo < FFI::Struct
    layout :abtAtqa, :uint8, 2,
    :btSak, :uint8, 
    :szUidLen, :size_t,
    :abtUid, :uint8, 10,
    :szAtsLen, :size_t,
    :abtAts, :uint8, 254
  end



  class NfcTargetInfo < FFI::Union
    layout :nai, NfcLibrary::NfcIso14443aInfo
    # :nfi, :nfc_felica_info,
    # :nbi, :nfc_iso14443b_info,
    # :nii, :nfc_iso14443bi_info,
    # :nsi, :nfc_iso14443b2sr_info,
    # :nci, :nfc_iso14443b2ct_info,
    # :nji, :nfc_jewel_info,
    #:ndi, :nfc_dep_info

  end

  class NfcModulation < FFI::Struct
    layout :nmt, :NfcModulationType,
    :nbr, :NfcBaudRate
  end

  class NfcTarget < FFI::Struct
    layout :nfc_target_info, NfcLibrary::NfcTargetInfo,
    :nfc_modulation, NfcLibrary::NfcModulation
  end
  
  attach_function :nfc_init, [:pointer], :void
  attach_function :nfc_target_init, [:pointer, :pointer, :pointer, :size_t, :int], :int
  attach_function :nfc_open, [:pointer, :string], :pointer
  attach_function :nfc_close, [:pointer], :void
  attach_function :nfc_list_devices, [:pointer, :string, :int], :int
  attach_function :nfc_initiator_init, [:pointer], :int
  attach_function :nfc_device_get_name, [:pointer], :string
  attach_function :nfc_initiator_select_passive_target, [:pointer, NfcLibrary::NfcModulation, :pointer, :size_t, :pointer], :int
end

Header function that i am trying to implement:
  NFC_EXPORT int nfc_initiator_select_passive_target(nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt); 

The c example program are running flawlessly and i am connecting to the nfc interface, but somehow the last method is attached wrong but how?

I have been stucked here for some days now trying to figure out why my rb test program will not call the method properbly, when the c version works great.

Any help will be much appreciated.

--
 
---
You received this message because you are subscribed to the Google Groups "ruby-ffi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-ffi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.