Class: NFC::Context

Inherits:
Object
  • Object
show all
Defined in:
ext/nfc/context.c

Instance Method Summary collapse

Instance Method Details

#devices(len) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'ext/nfc/context.c', line 34

static VALUE devices(VALUE self, VALUE len)
{
  nfc_context *ctx;
  nfc_connstring * strs;
  size_t found, i;
  VALUE devs;

  Data_Get_Struct(self, nfc_context, ctx);

  strs = malloc(sizeof(nfc_connstring) * len);

  found = nfc_list_devices(ctx, strs, 10);
  devs = rb_ary_new2(found);
  for (i = 0; i < found; i++) {
    rb_ary_push(devs, rb_str_new2(strs[i]));
  }
  free(strs);
  return devs;
}

#open(name) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'ext/nfc/context.c', line 9

static VALUE open_dev(VALUE self, VALUE name)
{
  nfc_context * ctx;
  nfc_device * dev;
  VALUE device;

  Data_Get_Struct(self, nfc_context, ctx);

  if (NIL_P(name)) {
    dev = nfc_open(ctx, NULL);
  } else {
    dev = nfc_open(ctx, StringValuePtr(name));
  }

  if (NULL == dev)
    rb_raise(rb_eRuntimeError, "Unable to open the device");

  if(nfc_initiator_init(dev) < 0)
    rb_raise(rb_eRuntimeError, "Could not initialize device");

  device = Data_Wrap_Struct(cNfcDevice, 0, nfc_close, dev);
  rb_iv_set(device, "@context", self);
  return device;
}