Class: NFC::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/nfc/device.rb,
ext/nfc/nfc_device.c

Defined Under Namespace

Classes: Modulation

Constant Summary collapse

DCO_HANDLE_CRC =
0x00
DCO_HANDLE_PARITY =
0x01
DCO_ACTIVATE_FIELD =
0x10
DCO_INFINITE_LIST_PASSIVE =
0x20
IM_ISO14443A_106 =
Modulation.new Modulation::NMT_ISO14443A,
Modulation::NBR_106

Instance Method Summary collapse

Instance Method Details

#deselectObject

Deselect the current tag



128
129
130
131
132
133
134
135
136
# File 'ext/nfc/nfc_device.c', line 128

static VALUE dev_deselect(VALUE self)
{
    nfc_device * dev;
    Data_Get_Struct(self, nfc_device, dev);

    nfc_initiator_deselect_target(dev);

    return self;
}

#initiator_initObject



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/nfc/nfc_device.c', line 176

static VALUE initiator_init(VALUE self)
{
    nfc_device * dev;
    int err;

    Data_Get_Struct(self, nfc_device, dev);

    err = nfc_initiator_init(dev);
    if (0 == err)
  return Qtrue;

    return INT2NUM(err);
}

#nameObject

Get the name of the tag reader



114
115
116
117
118
119
120
# File 'ext/nfc/nfc_device.c', line 114

static VALUE name(VALUE self)
{
    nfc_device * dev;
    Data_Get_Struct(self, nfc_device, dev);

    return rb_str_new2(nfc_device_get_name(dev));
}

#poll(poll_nr = 1, ms = 1) ⇒ Object



16
17
18
# File 'lib/nfc/device.rb', line 16

def poll poll_nr = 1, ms = 1
  poll_target NFC::Device::IM_ISO14443A_106, poll_nr,ms
end

#poll_target(tag, ms) ⇒ Object

Poll the tag type from the device



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/nfc/nfc_device.c', line 73

static VALUE poll_target(VALUE self, VALUE tag, VALUE poll_nr, VALUE ms)
{
    nfc_device * dev;
    nfc_modulation * mod;
    nfc_target * ti;
    int code;
    int ms_c, poll_nr_c;
    Data_Get_Struct(self, nfc_device, dev);
    Data_Get_Struct(tag, nfc_modulation, mod);

    ms_c = FIX2INT(ms);
    poll_nr_c = FIX2INT(poll_nr);

    ti = (nfc_target *)xmalloc(sizeof(nfc_target));

    code = nfc_initiator_poll_target(dev, mod, 1, poll_nr_c, ms_c, ti);

    if (code > 0) {
  switch(mod->nmt) {
      case NMT_ISO14443A:
    return Data_Wrap_Struct(cNfcISO14443A, 0, xfree, ti);
    break;
      case NMT_FELICA:
    return Data_Wrap_Struct(cNfcFelica, 0, xfree, ti);
    break;
      default:
    rb_raise(rb_eRuntimeError, "untested type: %d", mod->nmt);
  }
    }else {
  xfree(ti);
    }

    return INT2NUM(code);
}

#selectObject

Find a tag, blocks until there is a tag available



12
13
14
# File 'lib/nfc/device.rb', line 12

def select
  select_passive_target NFC::Device::IM_ISO14443A_106
end

#select_passive_target(tag) ⇒ Object

Select the tag type from the device



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
# File 'ext/nfc/nfc_device.c', line 32

static VALUE select_passive_target(VALUE self, VALUE tag)
{
    nfc_device * dev;
    nfc_modulation * mod;
    nfc_target * ti;
    struct nogvl_ctx ctx;

    Data_Get_Struct(self, nfc_device, dev);
    Data_Get_Struct(tag, nfc_modulation, mod);

    ti = (nfc_target *)xmalloc(sizeof(nfc_target));

    ctx.dev = dev;
    ctx.mod = mod;
    ctx.ti = ti;

    if (rb_thread_call_without_gvl(nogvl_select_passive_target, &ctx, RUBY_UBF_IO, 0) ) {
  switch(mod->nmt) {
      case NMT_ISO14443A:
    return Data_Wrap_Struct(cNfcISO14443A, 0, xfree, ti);
    break;
      case NMT_FELICA:
    /* return Data_Wrap_Struct(cNfcFelica, 0, free, ti); */
    return Qnil;
    break;
      default:
    rb_raise(rb_eRuntimeError, "untested type: %d", mod->nmt);
  }
    } else {
  xfree(ti);
    }

    return Qfalse;
}