Exemple #1
0
void
cut_setup(void)
{
  size_t n = nfc_list_devices(NULL, connstrings, 2);
  if (n < 2) {
    cut_omit("At least two NFC devices must be plugged-in to run this test");
  }
  nfc_init(NULL);
  devices[TARGET] = nfc_open(NULL, connstrings[TARGET]);
  devices[INITIATOR] = nfc_open(NULL, connstrings[INITIATOR]);

  signal(SIGINT, abort_test_by_keypress);
}
void
cut_setup(void)
{
  nfc_init(&context);
  size_t n = nfc_list_devices(context, connstrings, 2);
  if (n < 2) {
    cut_omit("At least two NFC devices must be plugged-in to run this test");
  }

  second_device = nfc_open(context, connstrings[0]);
  first_device = nfc_open(context, connstrings[1]);

  signal(SIGINT, abort_test_by_keypress);
}
Exemple #3
0
int
main(int argc, const char *argv[])
{
  uint8_t  abtRx[MAX_FRAME_LEN];
  int  szRx;
  uint8_t  abtTx[] = "Hello Mars!";

  nfc_context *context;
  nfc_init(&context);
#define MAX_DEVICE_COUNT 2
  nfc_connstring connstrings[MAX_DEVICE_COUNT];
  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
  // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
  // the same machine: if there is more than one readers opened
  // nfc-dep-target will open the second reader
  // (we hope they're always detected in the same order)
  if (szDeviceFound == 1) {
    pnd = nfc_open(context, connstrings[0]);
  } else if (szDeviceFound > 1) {
    pnd = nfc_open(context, connstrings[1]);
  } else {
    printf("No device found.\n");
    return EXIT_FAILURE;
  }

  if (argc > 1) {
    printf("Usage: %s\n", argv[0]);
    return EXIT_FAILURE;
  }

  nfc_target nt = {
    .nm = {
      .nmt = NMT_DEP,
      .nbr = NBR_UNDEFINED
    },
    .nti = {
      .ndi = {
        .abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
        .szGB = 4,
        .abtGB = { 0x12, 0x34, 0x56, 0x78 },
        .ndm = NDM_UNDEFINED,
        /* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
        .btDID = 0x00,
        .btBS = 0x00,
        .btBR = 0x00,
        .btTO = 0x00,
        .btPP = 0x01,
      },
    },
Exemple #4
0
static PyObject * my_nfc_open() //PyObject *self, PyObject *args)
{
	// Initialize libnfc and set the nfc_context
	nfc_init(&context);
	if (context == NULL) {
		printf("Unable to init libnfc (malloc)\n");
		exit(EXIT_FAILURE);
	}

	// Open, using the first available NFC device which can be in order of selection:
	//   - default device specified using environment variable or
	//   - first specified device in libnfc.conf (/etc/nfc) or
	//   - first specified device in device-configuration directory (/etc/nfc/devices.d) or
	//   - first auto-detected (if feature is not disabled in libnfc.conf) device
	pnd = nfc_open(context, NULL);

	if (pnd == NULL) {
		printf("ERROR: %s\n", "Unable to open NFC device.");
		exit(EXIT_FAILURE);
	}
	// Set opened NFC device to initiator mode
	if (nfc_initiator_init(pnd) < 0) {
		nfc_perror(pnd, "nfc_initiator_init");
		exit(EXIT_FAILURE);
	}

	//printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
	return Py_BuildValue("");
}
bool verification_lecteur_context(nfc_context**context, nfc_device **pnd, QLabel* label_conf_lecteur, QLabel* Message_utilisateur){

     Message_utilisateur->setText("Detecting reader !");
        bool res=1;
        *pnd = nfc_open(*context, NULL);

        if (*pnd == NULL) {
            Message_utilisateur->setText("Pas de lecteur détécté !");
            label_conf_lecteur->setText("Non détécté");
            label_conf_lecteur->setStyleSheet("background-color: red;");
            res =0;
        }
        // Set opened NFC device to initiator mode
        else {
          if (nfc_initiator_init(*pnd) < 0) {
              Message_utilisateur->setText("Détécté mais non utilisable!");
              label_conf_lecteur->setText("Non utilisable");
              label_conf_lecteur->setStyleSheet("background-color: orange;");
              res =0;
            }
          else {
              Message_utilisateur->setText("lecteur detecte");
              QString message ="";
              message += nfc_device_get_name(*pnd);
              label_conf_lecteur->setText(message);
              label_conf_lecteur->setStyleSheet("background-color: green;");
          }
    }

    return res;

    }
int
main(int argc, const char *argv[])
{
  nfc_device *pnd;
  nfc_target nt;

  // Allocate only a pointer to nfc_context
  nfc_context *context;

  // Initialize libnfc and set the nfc_context
  nfc_init(&context);

  // Display libnfc version
  const char *acLibnfcVersion = nfc_version();
  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);

  // Open, using the first available NFC device which can be in order of selection:
  //   - default device specified using environment variable or
  //   - first specified device in libnfc.conf (/etc/nfc) or
  //   - first specified device in device-configuration directory (/etc/nfc/devices.d) or
  //   - first auto-detected (if feature is not disabled in libnfc.conf) device
  pnd = nfc_open(context, NULL);

  if (pnd == NULL) {
    warnx("ERROR: %s", "Unable to open NFC device.");
    return EXIT_FAILURE;
  }
  // Set opened NFC device to initiator mode
  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    exit(EXIT_FAILURE);
  }

  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));

  // Poll for a ISO14443A (MIFARE) tag
  const nfc_modulation nmMifare = {
    .nmt = NMT_ISO14443A,
    .nbr = NBR_106,
  };
  if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {
    printf("The following (NFC) ISO14443A tag was found:\n");
    printf("    ATQA (SENS_RES): ");
    print_hex(nt.nti.nai.abtAtqa, 2);
    printf("       UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1'));
    print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
    printf("      SAK (SEL_RES): ");
    print_hex(&nt.nti.nai.btSak, 1);
    if (nt.nti.nai.szAtsLen) {
      printf("          ATS (ATR): ");
      print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen);
    }
  }
  // Close NFC device
  nfc_close(pnd);
  // Release the context
  nfc_exit(context);
  return EXIT_SUCCESS;
}
    bool NFCReaderUnit::connectToReader()
    {
       LOG(INFOS) << "Attempting to connect to NFC reader \"" << d_name << "\"";
        if (d_name.empty())
        {
            d_device = nfc_open(getNFCReaderProvider()->getContext(), nullptr);
        }
        else
        {
            d_device = nfc_open(getNFCReaderProvider()->getContext(), d_name.c_str());
        }
		if (d_device == nullptr)
		{
			LOG(ERRORS) << "Failed to instanciate NFC device.";
		}
		return (d_device != NULL);
    }
Exemple #8
0
void TagInit(ReaderTag *rt){
    
  
  nfc_init(&rt->context);
  if (rt->context == NULL) {
    ERR("Unable to init libnfc (malloc)");
    exit(EXIT_FAILURE);
  }

  // Try to open the NFC device
  rt->device = nfc_open(rt->context, NULL);
  if (rt->device == NULL) {
    ERR("Error opening NFC device");
    nfc_exit(rt->context);
    exit(EXIT_FAILURE);
  }

  if (nfc_initiator_init(rt->device) < 0) {
    nfc_perror(rt->device, "nfc_initiator_init");
    nfc_close(rt->device);
    nfc_exit(rt->context);
    exit(EXIT_FAILURE);
  }

  // Scan until the cows come home, or the tag, which ever comes first.
  if (nfc_device_set_property_bool(rt->device, NP_INFINITE_SELECT, true) < 0) {
    nfc_perror(rt->device, "nfc_device_set_property_bool");
    nfc_close(rt->device);
    nfc_exit(rt->context);
    exit(EXIT_FAILURE);
  }

  printf("NFC device: %s opened\n", nfc_device_get_name(rt->device));
  printf("\nWaiting for tag\n");

  // Try to find a MIFARE Ultralight tag
  if (nfc_initiator_select_passive_target(rt->device, nmMifare, NULL, 0, &rt->nt) <= 0) {
    ERR("no tag was found\n");
    nfc_close(rt->device);
    nfc_exit(rt->context);
    exit(EXIT_FAILURE);
  }
  // Test if we are dealing with a MIFARE compatible tag

  if (rt->nt.nti.nai.abtAtqa[1] != 0x44) {
    ERR("tag is not a MIFARE Ultralight card\n");
    nfc_close(rt->device);
    nfc_exit(rt->context);
    exit(EXIT_FAILURE);
  }
  // Get the info from the current tag
  printf("Found MIFARE Ultralight card with UID: ");
  size_t  szPos;
  for (szPos = 0; szPos < rt->nt.nti.nai.szUidLen; szPos++) {
    printf("%02x", rt->nt.nti.nai.abtUid[szPos]);
  }
  printf("\n");
}
Exemple #9
0
void
cut_setup(void)
{
    size_t n;

    nfc_init(NULL);

    llcp_init();

    //cut_pend ("MAC link over DEP does not work");

    n = nfc_list_devices(NULL, device_descriptions, 2);
    if (n < 2) {
        cut_omit("At least two NFC devices must be plugged-in to run this test");
    }

    devices[TARGET] = nfc_open(NULL, device_descriptions[TARGET]);
    devices[INITIATOR] = nfc_open(NULL, device_descriptions[INITIATOR]);
}
Exemple #10
0
silvia_nfc_card_monitor::silvia_nfc_card_monitor()
{
	nfc_init(&context);
	device = nfc_open(context, NULL);
	
	if (device != NULL)
	{
		nfc_initiator_init(device);
	}
}
Exemple #11
0
void nfc_task(void * data)
{
    int len;

    nfc_init(&context);
    if (context == NULL )
    {
        //we should never get here!
        for (;;)
            ;
    }

    pnd = nfc_open(context, NULL);
    for (;;)
    {
        {
            g_bReceivedFromPhone = 0;
            g_bDeliveredToPhone = 0;
            if (pnd != NULL)
            {
                //EnableKeyPoll(1);
                nfc_emulate_target(pnd, &emulator, 0);
                //EnableKeyPoll(0);
                if (g_bReceivedFromPhone || g_bDeliveredToPhone)
                {
                    taginfo.msgLen = (g_bReceivedFromPhone)? (((uint16_t)taginfo.msg[0])<<8) | taginfo.msg[1] : 0;
                    xQueueSend(xUIQueue, &taginfo, portMAX_DELAY);
                }
            }
            else
            {
                pnd = nfc_open(context, NULL);
            }
        }

        if (pnd != NULL)
        {
            //nfc_idle(pnd);
        }
        //EnableKeyPoll(1);
    }
}
Exemple #12
0
int
main(int argc, const char *argv[])
{

  nfc_device *pnd;
  nfc_target nt;
  nfc_context *context;
  nfc_init(&context);
  if (context == NULL) {
    printf("Unable to init libnfc (malloc)\n");
    exit(EXIT_FAILURE);
  }
  const char *acLibnfcVersion = nfc_version();
  (void)argc;

  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
  pnd = nfc_open(context, NULL);

  if (pnd == NULL) {
    printf("ERROR: %s\n", "Unable to open NFC device.");
    exit(EXIT_FAILURE);
  }
  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    exit(EXIT_FAILURE);
  }

  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));

  for( ; ; ){
    const nfc_modulation nmMifare = {
      .nmt = NMT_ISO14443A,
      .nbr = NBR_106,
    };
    if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {
      printf("The following (NFC) ISO14443A tag was found:\n");
      printf("    ATQA (SENS_RES): ");
      print_hex(nt.nti.nai.abtAtqa, 2);
      printf("       UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1'));
      print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
      printf("      SAK (SEL_RES): ");
      print_hex(&nt.nti.nai.btSak, 1);
      if (nt.nti.nai.szAtsLen) {
        printf("          ATS (ATR): ");
        print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen);
      }
      sleep(3);
    }
  }

  nfc_close(pnd);
  nfc_exit(context);
  exit(EXIT_SUCCESS);
}
Exemple #13
0
/*
 * call-seq:
 *  connect
 *
 * Connect to the NFC device
 */
static VALUE connect(VALUE klass)
{
  nfc_device_t * dev = nfc_open(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);
}
Exemple #14
0
nfc_device *rfid_init(void) {
	nfc_init(&nfc_ctx);
	if(!nfc_ctx) {
		log("initializing libnfc failed");
		return NULL;
	}

	door_aid = mifare_desfire_aid_new(0x2305CA);

	nfc_connstring devices[NFC_MAX_DEVICES];
	int device_count = nfc_list_devices(nfc_ctx, devices, NFC_MAX_DEVICES);
	if(device_count <= 0) {
		log("no NFC devices found");
		return NULL;
	}

	log("found %u NFC devices:", device_count);
	for(int i = 0; i < device_count; i++)
		log(" - %s", devices[i]);

	int selected_device = -1;
	const char *conf_connstring = getenv("RFID_CONNSTRING");
	if(conf_connstring) {
		for(int i = 0; i < device_count; i++)
			if(strstr(devices[i], conf_connstring)) {
				selected_device = i;
				break;
			}
	} else {
		log("no connection string supplied, using first device");
		selected_device = 0;
	}

	if(selected_device == -1) {
		log("could not find requested device");
		return NULL;
	}

	log("using device %s", devices[selected_device]);

	nfc_device *dev = nfc_open(nfc_ctx, devices[selected_device]);
	if(!dev) {
		log("opening NFC device failed");
		return NULL;
	}

	if(nfc_initiator_init(dev) < 0) {
		nfc_perror(dev, "configuring NFC device as initiator failed");
		return NULL;
	}

	return dev;
}
void
cut_setup (void)
{
    int res;
    nfc_connstring devices[8];
    size_t device_count;

    nfc_init (&context);
    cut_assert_not_null (context, cut_message ("Unable to init libnfc (malloc)"));

    device_count = nfc_list_devices (context, devices, 8);
    if (device_count <= 0)
        cut_omit ("No device found");

    for (size_t i = 0; i < device_count; i++) {

        device = nfc_open (context, devices[i]);
        if (!device)
            cut_omit ("nfc_open() failed.");

        tags = freefare_get_tags (device);
        cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed"));

        tag = NULL;
        for (int i=0; tags[i]; i++) {
            if (freefare_get_tag_type(tags[i]) == MIFARE_DESFIRE) {
                tag = tags[i];
                res = mifare_desfire_connect (tag);
                cut_assert_equal_int (0, res, cut_message ("mifare_desfire_connect() failed"));

                struct mifare_desfire_version_info version_info;
                res = mifare_desfire_get_version (tag, &version_info);
                cut_assert_equal_int (0, res, cut_message ("mifare_desfire_get_version"));

                if (version_info.hardware.storage_size < 0x18) {
                    cut_omit ("DESFire EV1 tests require at least a 4K card");
                }

                if ((version_info.hardware.version_major >= 1) &&
                    (version_info.software.version_major >= 1)) {
                    return;
                }

                mifare_desfire_disconnect (tag);
            }
        }
        nfc_close (device);
        device = NULL;
        freefare_free_tags (tags);
        tags = NULL;
    }
    cut_omit ("No MIFARE DESFire EV1 tag on NFC device");
}
Exemple #16
0
int
main(int argc, const char *argv[])
{
  system("clear");
  
  nfc_device *pnd;
  nfc_target nt;

  // Allocate only a pointer to nfc_context
  nfc_context *context;

  // Initialize libnfc and set the nfc_context
  nfc_init(&context);

  // Display libnfc version
  const char *acLibnfcVersion = nfc_version();
  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);

  // Open, using the first available NFC device.
  pnd = nfc_open(context, NULL);

  if (pnd == NULL) {
    warnx("ERROR: %s", "Unable to open NFC device.");
    return EXIT_FAILURE;
  }
  // Set opened NFC device to initiator mode
  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    exit(EXIT_FAILURE);
  }

  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
  printf("...\n", nfc_device_get_name(pnd));
  
  while (true){
    // Poll for a ISO14443A (MIFARE) tag
    const nfc_modulation nmMifare = {
      .nmt = NMT_ISO14443A,
      .nbr = NBR_106,
    };
    if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {
      print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
    }
    sleep(1);
  }
    
    // Close NFC device
  nfc_close(pnd);
  // Release the context
  nfc_exit(context);
  return EXIT_SUCCESS;
}
void nfcInitListen() {
    const char *acLibnfcVersion = nfc_version();
    pnd = nfc_open(context, NULL);
    if (pnd == NULL) {
        printf("ERROR: %s", "Unable to open NFC device.");
        exit(EXIT_FAILURE);
    }
    if (nfc_initiator_init(pnd) < 0) {
        nfc_perror(pnd, "nfc_initiator_init");
        exit(EXIT_FAILURE);
    }
    printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
}
int
main (int argc, const char *argv[])
{
  nfc_device *pnd;
  nfc_target nt;
  
  nfc_init (NULL);

  // Display libnfc version
  const char *acLibnfcVersion = nfc_version ();
  printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);

  // Open, using the first available NFC device
  pnd = nfc_open (NULL, NULL);

  if (pnd == NULL) {
    ERR ("%s", "Unable to open NFC device.");
    return EXIT_FAILURE;
  }
  // Set opened NFC device to initiator mode
  if (nfc_initiator_init (pnd) < 0) {
    nfc_perror (pnd, "nfc_initiator_init");
    exit (EXIT_FAILURE);    
  }

  printf ("NFC reader: %s opened\n", nfc_device_get_name (pnd));

  // Poll for a ISO14443A (MIFARE) tag
  const nfc_modulation nmMifare = {
    .nmt = NMT_ISO14443A,
    .nbr = NBR_106,
  };
  if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) > 0) {
    printf ("The following (NFC) ISO14443A tag was found:\n");
    printf ("    ATQA (SENS_RES): ");
    print_hex (nt.nti.nai.abtAtqa, 2);
    printf ("       UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1'));
    print_hex (nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
    printf ("      SAK (SEL_RES): ");
    print_hex (&nt.nti.nai.btSak, 1);
    if (nt.nti.nai.szAtsLen) {
      printf ("          ATS (ATR): ");
      print_hex (nt.nti.nai.abtAts, nt.nti.nai.szAtsLen);
    }
  }
  // Close NFC device
  nfc_close (pnd);
  nfc_exit (NULL);
  return EXIT_SUCCESS;
}
Exemple #19
0
int
main(int argc, const char *argv[])
{
  nfc_device *pnd;
  nfc_target nt;

  // Allocate only a pointer to nfc_context
  nfc_context *context;

  // Initialize libnfc and set the nfc_context
  nfc_init(&context);
  if (context == NULL) {
    exit(EXIT_FAILURE);
  }

  // Display libnfc version
  const char *acLibnfcVersion = nfc_version();
  (void)argc;

  // Open, using the first available NFC device which can be in order of selection:
  //   - default device specified using environment variable or
  //   - first specified device in libnfc.conf (/etc/nfc) or
  //   - first specified device in device-configuration directory (/etc/nfc/devices.d) or
  //   - first auto-detected (if feature is not disabled in libnfc.conf) device
  pnd = nfc_open(context, NULL);

  if (pnd == NULL) {
    exit(EXIT_FAILURE);
  }
  // Set opened NFC device to initiator mode
  if (nfc_initiator_init(pnd) < 0) {
    nfc_perror(pnd, "nfc_initiator_init");
    exit(EXIT_FAILURE);
  }


  // Poll for a ISO14443A (MIFARE) tag
  nfc_modulation nmMifare;
  nmMifare.nmt = NMT_ISO14443A;
  nmMifare.nbr = NBR_106;
  
  if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0)
    print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
  // Close NFC device
  nfc_close(pnd);
  // Release the context
  nfc_exit(context);
  exit(EXIT_SUCCESS);
}
Exemple #20
0
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;
}
Exemple #21
0
int main(int argc, const char *argv[]) {
    pinMode(LED_R, OUTPUT); 	// Set PWM LED as PWM output
    pinMode(LED_G, OUTPUT);     // Set regular LED as output
    pinMode(LED_B, OUTPUT);

    pinMode(DOOR, OUTPUT);		//tür öffner

    // nfc initiiere
    nfc_init(&context);
    if (context == NULL) {
        printf("ERROR: fehler beim dev init\n");
        exit(EXIT_FAILURE);
    }

    while (true) {
        doorCode = 0;
        // öffnet verbindung zum token
        pnd = nfc_open(context, NULL);

        led(1,1,0); //Gelb an

        if (pnd == NULL) {
            printf("ERROR: fehler beim öffnen.\n");
            exit(EXIT_FAILURE);
        }

        if (nfc_initiator_init(pnd) < 0) {
            nfc_perror(pnd, "nfc_initiator_init");
            exit(EXIT_FAILURE);
        }

        const nfc_modulation nmMifare = {
            .nmt = NMT_ISO14443A,
            .nbr = NBR_106,
        };
        if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {
            toTokenKey(nt.nti.nai.abtUid);
            printf("%s\n",tokenKey);
            checkToken();
        }

        // TODO checken ob offen ist oder nicht um dann blau oder black zu zeigen

        nfc_close(pnd);
    }
    nfc_exit(context);
    exit(EXIT_SUCCESS);
}
Exemple #22
0
//Not really useful for now but will be more interesting when we'll handle multiple devices
nfc_device *edgehog_nfc_get_reader_device(nfc_context* context, const nfc_connstring conn_string)
{
    nfc_device *pnd = nfc_open(context, conn_string);
    if(pnd == NULL)
    {
        return NULL;
    }

    if (nfc_initiator_init(pnd) < 0)
    {
        //NOTE: free pnd ?
        return NULL;
    }

    return pnd;
}
int main(int argc, const char *argv[])
{
    nfc_device *pnd;
    nfc_target nt;

    nfc_context *context;

    nfc_init(&context);
    if(context == NULL) {
        printf("unable to init (malloc)\n");
        exit(EXIT_FAILURE);
    }

    pnd = nfc_open(context, NULL);

    if (pnd == NULL) {
        printf("ERROR: %s\n", "Unable to open NFC Device.");
        exit(EXIT_FAILURE);
    }

    if (nfc_initiator_init(pnd) < 0) {
        nfc_perror(pnd, "nfc_initiator_init");
        exit(EXIT_FAILURE);
    }

    printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));

    const nfc_modulation nmMifare = {
        .nmt = NMT_ISO14443A,
        .nbr = NBR_106,
    };

    for(int i = 0; i < 24; i++) {
        if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) {
            printf("The following (NFC) card was found:\n");
            print_card(nt.nti.nai.abtUid, nt.nti.nai.szUidLen);
        }
        while (0 == nfc_initiator_target_is_present(pnd, NULL)){}
    }

    nfc_close(pnd);

    nfc_exit(context);

    exit(EXIT_SUCCESS);   
}
Exemple #24
0
int mf_connect() {

  // Initialize libnfc and set the nfc_context
  nfc_init(&context);

  // Connect to (any) NFC reader
  device = nfc_open(context, NULL);
  if (device == NULL) {
    printf ("Could not connect to any NFC device\n");
    return -1; // Don't jump here, since we don't need to disconnect
  }

  // Initialize the device as a reader
  if (!mf_configure_device()) {
    printf("Error initializing NFC device\n");
    return mf_disconnect(-1);
  }

  // Try to find a tag
  if (!mf_select_target() || target.nti.nai.btSak == 0) {
    printf("Connected to device, but no tag found.\n");
    return mf_disconnect(-1);
  }

  // Test if we are dealing with a Mifare Classic compatible tag
  if ((target.nti.nai.btSak & 0x08) == 0) {
    printf("Incompatible tag type: 0x%02x (i.e. not Mifare Classic).\n",
           target.nti.nai.btSak);
    return mf_disconnect(-1);
  }

  // Guessing tag size
  if ((target.nti.nai.abtAtqa[1] & 0x02)) { // 4K
    size = MF_4K;
  }
  else if ((target.nti.nai.abtAtqa[1] & 0x04)) { // 1K
    size = MF_1K;
  }
  else {
    printf("Unsupported tag size. ATQA 0x%02x 0x%02x (i.e. not [1|4]K.)\n",
           target.nti.nai.abtAtqa[0], target.nti.nai.abtAtqa[1]);
    return mf_disconnect(-1);
  }

  return 0; // Indicate success - we are now connected
}
Exemple #25
0
nfc_device* getRfidDevice()
{
  size_t device_count = 0;
  nfc_connstring devices[8];
  nfc_context *context;

  nfc_init(&context);

  device_count = nfc_list_devices(context,devices,8);

  if (device_count > 0)
    return nfc_open (context,devices[0]);
  else
  {
    fprintf(stderr,"Connecting to nfc device failed\n");
    return NULL;
  }
}
void RFIDHandler::init()
{
	nfc_connstring devices[1];
	nfc_context *context;

    nfc_init (&context);

	if(nfc_list_devices (context, devices, 1) == 0)
	{
		cout << "Error no NFC devices found..." << endl;
	}

	m_nfc_device = nfc_open (context, devices[0]);

	if(m_nfc_device == NULL)
	{
		cout << "Error unable to open NFC device" << endl;
	}
}
Exemple #27
0
void vNFCTask(void *vParameter){

	vDebugString("vNFCTask started");

	vTaskDelay(100);

	nfc_init(&context);

	pnd = nfc_open(context);

	nfc_close(pnd);

	nfc_exit(context);

	for(;;){

			vTaskDelay(1000);
	}
}
void
cut_setup (void)
{
    int res;
    nfc_connstring devices[8];
    size_t device_count;

    nfc_init (&context);
    cut_assert_not_null (context, cut_message ("Unable to init libnfc (malloc)"));

    device_count = nfc_list_devices (context, devices, 8);
    if (device_count <= 0)
        cut_omit ("No device found");

    for (size_t i = 0; i < device_count; i++) {
        device = nfc_open (context, devices[i]);
        if (!device)
            cut_omit ("nfc_open() failed.");

        tags = freefare_get_tags (device);
        cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed"));

        tag = NULL;
        for (int i=0; tags[i]; i++) {
            if ((freefare_get_tag_type(tags[i]) == MIFARE_CLASSIC_1K) ||
                (freefare_get_tag_type(tags[i]) == MIFARE_CLASSIC_4K)) {
                tag = tags[i];
                res = mifare_classic_connect (tag);
                cut_assert_equal_int (0, res, cut_message ("mifare_classic_connect() failed"));
                return;
            }
        }
        nfc_close (device);
        device = NULL;
        freefare_free_tags (tags);
        tags = NULL;
    }
    cut_omit ("No MIFARE Classic tag on NFC device");
}
Exemple #29
0
int
main (void)
{
    nfc_connstring nfc_devices[MAX_NFC_DEVICES];
    size_t nfc_device_count;

    nfc_device_count = nfc_list_devices (NULL, nfc_devices, MAX_NFC_DEVICES);

    bool aborting = false;

    for (size_t n = 0; (!aborting) && (n < nfc_device_count); n++) {
        nfc_device *nfc_device = nfc_open (NULL, nfc_devices[n]);
        MifareTag *tags = freefare_get_tags (nfc_device);
        for (int i = 0; (!aborting) && tags[i]; i++) {
            if (DESFIRE == freefare_get_tag_type (tags[i])) {

                fprintf (stdout, "Found UCard with UID 0x%s\n", freefare_get_tag_uid (tags[i]));

                printf ("Admin access key: ");
                char buffer[BUFSIZ];
                system ("stty -echo");
                fgets (buffer, BUFSIZ, stdin);
                system ("stty echo");
                char *p;
                if ((p = strchr (buffer, '\n')))
                    *p = '\0';

                ucard_derivate_password (buffer, strlen (buffer), 16, admin_key_data);
                memset (buffer, '\0', strlen (buffer));

                if (explain_tag (tags[i]) < 0)
                    aborting = true;
            }
        }
        freefare_free_tags (tags);
    }

    exit((aborting) ? 1 : 0);
}
Exemple #30
0
bool silvia_nfc_card_monitor::wait_for_card(silvia_nfc_card** card)
{
	assert(card != NULL);
	
	if (device == NULL)
	{
		device = nfc_open(context, NULL);
	
		if (device == NULL)
		{
			return false;
		}
		
		nfc_initiator_init(device);
	}
	
	// CAVEAT: only tested with PN533
	const nfc_modulation modulation = { NMT_ISO14443A, NBR_106 };
	
	// Poll for a card
	int rv = 0;
	nfc_target target;
	
	while ((rv = nfc_initiator_select_passive_target(device, modulation, NULL, 0, &target)) == 0)
	{
		usleep(10000);
	}
	
	if (rv < 0)
	{
		return false;
	}
	
	*card = new silvia_nfc_card(device, target, "NFC reader");
	
	return true;
}