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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connectObject

Connect to the NFC device



11
12
13
14
15
16
17
18
19
20
21
# File 'ext/nfc/nfc_device.c', line 11

static VALUE connect(VALUE klass)
{
  nfc_device_t * dev = nfc_connect(NULL);
  if(!dev)
    rb_raise(rb_eRuntimeError, "could not find NFC device");

  if(!nfc_initiator_init(dev))
    rb_raise(rb_eRuntimeError, "oh snap, could not init");

  return Data_Wrap_Struct(klass, 0, 0, dev);
}

Instance Method Details

#configure(option, value) ⇒ Object

Configure the Device with option and value



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'ext/nfc/nfc_device.c', line 44

static VALUE configure(VALUE self, VALUE option, VALUE flag)
{
  nfc_device_t * dev;
  Data_Get_Struct(self, nfc_device_t, dev);

  nfc_configure(
    dev,
    (const nfc_device_option_t)NUM2INT(option),
    (const bool)NUM2INT(flag)
  );

  return self;
}

#deselectObject

Deselect the current tag



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

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

  nfc_initiator_deselect_target(dev);

  return self;
}

#disconnectObject

Disconnect from the NFC device



29
30
31
32
33
34
35
36
# File 'ext/nfc/nfc_device.c', line 29

static VALUE disconnect(VALUE self)
{
  nfc_device_t * dev;
  Data_Get_Struct(self, nfc_device_t, dev);
  nfc_disconnect(dev);

  return self;
}

#nameObject

Get the name of the tag reader



97
98
99
100
101
102
103
# File 'ext/nfc/nfc_device.c', line 97

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

  return rb_str_new2(dev->acName);
}

#select(tag) ⇒ Object

Select the tag type from the device



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/nfc/nfc_device.c', line 64

static VALUE dev_select(VALUE self, VALUE tag)
{
  nfc_device_t * dev;
  nfc_modulation_t * mod;
  nfc_target_t * ti;

  Data_Get_Struct(self, nfc_device_t, dev);
  Data_Get_Struct(tag, nfc_modulation_t, mod);

  ti = (nfc_target_t *)calloc(1, sizeof(nfc_target_t));

  if (nfc_initiator_select_passive_target(dev, *mod, NULL, 0, ti) ) {
    switch(mod->nmt) {
      case NMT_ISO14443A:
        return Data_Wrap_Struct(cNfcISO14443A, 0, free, ti);
        break;
      case NMT_FELICA:
        return Data_Wrap_Struct(cNfcFelica, 0, free, ti);
        break;
      default:
        rb_raise(rb_eRuntimeError, "untested type: %d", mod->nmt);
    }
  }

  return Qfalse;
}