Exemplo n.º 1
0
bool unlock_card(void)
{
    if (magic2)
    {
        #ifdef DEBUG_PRINTF
        fprintf(stderr,"Don't use R/W with this card, this is not required!\n");
        #endif
        sprintf(message_erreur,"Don't use R/W with this card, this is not required !");
        return false;
    }
    //! Configure the CRC
    if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0)
    {
        nfc_perror(pnd, "nfc_configure");
        nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR);
        return false;
    }
    //! Use raw send/receive methods
    if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0)
    {
        nfc_perror(pnd, "nfc_configure");
        nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR);
        return false;
    }
    iso14443a_crc_append(abtHalt, 2);
    transmit_bytes(abtHalt, 4);
    //! now send unlock
    if (!transmit_bits(abtUnlock1, 7))
    {
        #ifdef DEBUG_PRINTF
        fprintf(stderr,"unlock failure!\n");
        #endif
        sprintf(message_erreur,"Unlock failure !");
        return false;
    }
    if (!transmit_bytes(abtUnlock2, 1))
    {
        #ifdef DEBUG_PRINTF
        fprintf(stderr,"unlock failure!\n");
        #endif
        sprintf(message_erreur,"Unlock failure !");
        return false;
    }
    //! reset reader
    //! Configure the CRC
    if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0)
    {
        nfc_perror(pnd, "nfc_device_set_property_bool");
        nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR);
        return false;
    }
    //! Switch off raw send/receive methods
    if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0)
    {
        nfc_perror(pnd, "nfc_device_set_property_bool");
        nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR);
        return false;
    }
    return true;
}
Exemplo n.º 2
0
static int
get_rats(void)
{
  int res;
  uint8_t  abtRats[2] = { 0xe0, 0x50};
  // Use raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return -1;
  }
  res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0);
  if (res > 0) {
    // ISO14443-4 card, turn RF field off/on to access ISO14443-3 again
    if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false) < 0) {
      nfc_perror(pnd, "nfc_configure");
      return -1;
    }
    if (nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true) < 0) {
      nfc_perror(pnd, "nfc_configure");
      return -1;
    }
  }
  // Reselect tag
  if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) {
    printf("Error: tag disappeared\n");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }
  return res;
}
Exemplo n.º 3
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");
}
Exemplo n.º 4
0
static bool
raw_mode_start(void)
{
  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return false;
  }
  // Use raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return false;
  }
  return true;
}
Exemplo n.º 5
0
int
fix_mad_trailer_block (nfc_device_t *device, MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey key, MifareClassicKeyType key_type)
{
    MifareClassicBlock block;
    mifare_classic_trailer_block (&block, mad_public_key_a, 0x0, 0x1, 0x1, 0x6, 0x00, default_keyb);
    if (mifare_classic_authenticate (tag, mifare_classic_sector_last_block (sector), key, key_type) < 0) {
		nfc_perror (device, "fix_mad_trailer_block mifare_classic_authenticate");
		return -1;
    }
    if (mifare_classic_write (tag, mifare_classic_sector_last_block (sector), block) < 0) {
		nfc_perror (device, "mifare_classic_write");
		return -1;
    }
    return 0;
}
Exemplo n.º 6
0
Arquivo: nfc.c Projeto: cybrairai/nfc
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("");
}
Exemplo n.º 7
0
int get_rats(void)
{
    int res;
    uint8_t  abtRats[2] = { 0xe0, 0x50};
    //! Use raw send/receive methods
    if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0)
    {
        nfc_perror(pnd, "nfc_configure");
        nfc_strerror_r(pnd, message_erreur, TAILLE_MESSAGE_ERREUR);
        return -1;
    }
    res = nfc_initiator_transceive_bytes(pnd, abtRats, sizeof(abtRats), abtRx, sizeof(abtRx), 0);
    if (res > 0)
    {
        //! ISO14443-4 card, turn RF field off/on to access ISO14443-3 again
        nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false);
        nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true);
    }
    //! Reselect tag
    if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0)
    {
        sprintf(message_erreur,"Error: tag disappeared !");
        nfc_close(pnd);
        nfc_exit(context);
        return EXIT_FAILURE; //! Échec !
    }
    return res;
}
bool
sam_connection (nfc_device_t * pnd, int mode)
{
  byte_t  pncmd_sam_config[] = { 0xD4, 0x14, 0x00, 0x00 };
  size_t  szCmd = 0;

  byte_t  abtRx[MAX_FRAME_LEN];
  size_t  szRx;

  pncmd_sam_config[2] = mode;

  switch (mode) {
  case VIRTUAL_CARD_MODE:
    {
      // Only the VIRTUAL_CARD_MODE requires 4 bytes.
      szCmd = sizeof (pncmd_sam_config);
    }
    break;

  default:
    {
      szCmd = sizeof (pncmd_sam_config) - 1;
    }
    break;
  }

  if (!pn53x_transceive (pnd, pncmd_sam_config, szCmd, abtRx, &szRx)) {
    nfc_perror(pnd, "pn53x_transceive");
    ERR ("%s %d", "Unable to execute SAMConfiguration command with mode byte:", mode);
    return false;
  }

  return true;
}
Exemplo n.º 9
0
static bool
raw_mode_end(void)
{
  // reset reader
  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    return false;
  }
  // Switch off raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    return false;
  }
  return true;
}
Exemplo n.º 10
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);

  // 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;
}
Exemplo n.º 11
0
static bool
unlock_card(void)
{
  if (magic2) {
    printf("Don't use R/W with this card, this is not required!\n");
    return false;
  }

  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return false;
  }
  // Use raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return false;
  }

  iso14443a_crc_append(abtHalt, 2);
  transmit_bytes(abtHalt, 4);
  // now send unlock
  if (!transmit_bits(abtUnlock1, 7)) {
    printf("unlock failure!\n");
    return false;
  }
  if (!transmit_bytes(abtUnlock2, 1)) {
    printf("unlock failure!\n");
    return false;
  }

  // reset reader
  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    return false;
  }
  // Switch off raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    return false;
  }
  return true;
}
Exemplo n.º 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);
}
Exemplo n.º 13
0
bool
nfc_initiator_jewel_cmd(nfc_device *pnd, const jewel_req req, jewel_res *pres)
{
    size_t nLenReq;
    size_t nLenRes;

    switch (req.rid.btCmd) {
    case TC_RID:
        nLenReq = sizeof(jewel_req_rid);
        nLenRes = sizeof(jewel_res_rid);
        break;
    case TC_RALL:
        nLenReq = sizeof(jewel_req_rall);
        nLenRes = sizeof(jewel_res_rall);
        break;
    case TC_READ:
        nLenReq = sizeof(jewel_req_read);
        nLenRes = sizeof(jewel_res_read);
        break;
    case TC_WRITEE:
        nLenReq = sizeof(jewel_req_writee);
        nLenRes = sizeof(jewel_res_writee);
        break;
    case TC_WRITENE:
        nLenReq = sizeof(jewel_req_writene);
        nLenRes = sizeof(jewel_res_writene);
        break;
    case TC_RSEG:
        nLenReq = sizeof(jewel_req_rseg);
        nLenRes = sizeof(jewel_res_rseg);
        break;
    case TC_READ8:
        nLenReq = sizeof(jewel_req_read8);
        nLenRes = sizeof(jewel_res_read8);
        break;
    case TC_WRITEE8:
        nLenReq = sizeof(jewel_req_writee8);
        nLenRes = sizeof(jewel_res_writee8);
        break;
    case TC_WRITENE8:
        nLenReq = sizeof(jewel_req_writene8);
        nLenRes = sizeof(jewel_res_writene8);
        break;
    default:
        return false;
    }

    if (nfc_initiator_transceive_bytes(pnd, (uint8_t *)&req, nLenReq, (uint8_t *)pres, nLenRes, -1) < 0) {
        nfc_perror(pnd, "nfc_initiator_transceive_bytes");
        return false;
    }

    return true;
}
Exemplo n.º 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;
}
Exemplo n.º 15
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;
}
Exemplo n.º 16
0
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));
}
Exemplo n.º 17
0
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;
}
Exemplo n.º 18
0
static bool
unlock_card(void)
{
  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return false;
  }
  // Use raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) {
    nfc_perror(pnd, "nfc_configure");
    return false;
  }

  iso14443a_crc_append(abtHalt, 2);
  transmit_bytes(abtHalt, 4);
  // now send unlock
  if (!transmit_bits(abtUnlock1, 7)) {
    return false;
  }
  if (!transmit_bytes(abtUnlock2, 1)) {
    return false;
  }

  // reset reader
  // Configure the CRC
  if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    return false;
  }
  // Switch off raw send/receive methods
  if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) {
    nfc_perror(pnd, "nfc_device_set_property_bool");
    return false;
  }
  return true;
}
Exemplo n.º 19
0
int main(int argc, char** argv) {
    
    memset(&mtDump, 0x00, sizeof(mtDump));
    Credentials crd;
    char cmd[200];
    
    signal(SIGINT, stop_polling);
    
    
    nfc_init(&RT.context);
    
    if (RT.context == NULL) {
          ERR("Unable to init libnfc (malloc)");
          exit(EXIT_FAILURE);
        }
        TagInit(&RT);
      
    while(run){
        
            if(ScanForTag(&RT)){
                if(N_read_card(RT,&mtDump)){
                    if(NDDEF_DEGenerate(&crd, &mtDump)){
                        if(Credentials_Check(&crd)){
                            printf("\nGood\n");
                            memset(cmd, 0x00, sizeof(cmd));
                            //snprintf(cmd,200,"startx /usr/bin/nice -n 18 /usr/bin/rdesktop -a 24 -x 6 -P -r sound:local -u %s -p %s -f %s",crd.user,crd.password, crd.VM);
                            //printf("%s",cmd);
                            //system(cmd);
                            printf("Waiting for card removing...");
                            sleep(1);
                            while (0 == nfc_initiator_target_is_present(RT.device, NULL)) {}
                            nfc_perror(RT.device, "nfc_initiator_target_is_present");
                            printf("done.\n");
                        }
                    }
                }
            }
        
        //nfc_idle(RT.device);
        //nfc_exit(RT.context);
        sleep(1);
    }
    if (RT.device != NULL)
        nfc_abort_command(RT.device);
    else {
      nfc_exit(RT.context);
      exit(EXIT_SUCCESS);
    }
}
Exemplo n.º 20
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);
}
Exemplo n.º 21
0
bool receive_bytes (void)
{
    if (!nfc_target_receive_bytes(pnd,abtRx,&szRx)) {
        nfc_perror (pnd, "nfc_target_receive_bytes");
        exit(EXIT_FAILURE);
    }

    // Show received answer
    if (!quiet_output) {
        printf ("Received data: ");
        print_hex (abtRx, szRx);
    }
    // Succesful transfer
    return true;
}
Exemplo n.º 22
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);
}
Exemplo n.º 23
0
bool send_bytes (const byte_t * pbtTx, const size_t szTx)
{
    // Show transmitted command
    if (!quiet_output) {
        printf ("Sent data: ");
        print_hex (pbtTx, szTx);
    }

    // Transmit the command bytes
    if (!nfc_target_send_bytes(pnd, pbtTx, szTx)) {
        nfc_perror (pnd, "nfc_target_send_bytes");
        exit(EXIT_FAILURE);
    }
    // Succesful transfer
    return true;
}
Exemplo n.º 24
0
/*
 * call-seq:
 *  connect
 *
 * Connect to the NFC device
 */
static VALUE connect(VALUE klass)
{
  nfc_init(NULL);
  nfc_device * dev = nfc_open(NULL, NULL);
  if(!dev)
    rb_raise(rb_eRuntimeError, "could not find NFC device");

  if(nfc_initiator_init(dev) < 0)
  {
    nfc_perror (dev, "nfc_initiator_init");

    rb_raise(rb_eRuntimeError, "oh snap, could not init");
  }

  return Data_Wrap_Struct(klass, 0, 0, dev);
}
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);   
}
int
main (int argc, char *argv[])
{
  int     arg;

  // Get commandline options
  for (arg = 1; arg < argc; arg++) {
    if (0 == strcmp (argv[arg], "-h")) {
      print_usage (argv);
      exit(EXIT_SUCCESS);
    } else if (0 == strcmp (argv[arg], "-q")) {
      quiet_output = true;
    } else {
      ERR ("%s is not supported option.", argv[arg]);
      print_usage (argv);
      exit(EXIT_FAILURE);
    }
  }

  // Try to open the NFC reader
  pnd = nfc_connect (NULL);

  if (!pnd) {
    printf ("Error connecting NFC reader\n");
    exit(EXIT_FAILURE);
  }

  // Initialise NFC device as "initiator"
  nfc_initiator_init (pnd);

  // Drop the field for a while
  if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, false)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }

  // Configure the CRC
  if (!nfc_configure (pnd, NDO_HANDLE_CRC, false)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }
  // Configure parity settings
  if (!nfc_configure (pnd, NDO_HANDLE_PARITY, true)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }
  // Use raw send/receive methods
  if (!nfc_configure (pnd, NDO_EASY_FRAMING, false)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }
  // Disable 14443-4 autoswitching
  if (!nfc_configure (pnd, NDO_AUTO_ISO14443_4, false)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }
  // Force 14443-A mode
  if (!nfc_configure (pnd, NDO_FORCE_ISO14443_A, true)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }

  // Enable field so more power consuming cards can power themselves up
  if (!nfc_configure (pnd, NDO_ACTIVATE_FIELD, true)) {
    nfc_perror (pnd, "nfc_configure");
    exit (EXIT_FAILURE);
  }

  printf ("Connected to NFC reader: %s\n\n", pnd->acName);

  // Send the 7 bits request command specified in ISO 14443A (0x26)
  if (!transmit_bits (abtReqa, 7)) {
    printf ("Error: No tag available\n");
    nfc_disconnect (pnd);
    return 1;
  }
  memcpy (abtAtqa, abtRx, 2);

  // Anti-collision
  transmit_bytes (abtSelectAll, 2);

  // Check answer
  if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
    printf("WARNING: BCC check failed!\n");
  }

  // Save the UID CL1
  memcpy (abtRawUid, abtRx, 4);

  //Prepare and send CL1 Select-Command
  memcpy (abtSelectTag + 2, abtRx, 5);
  iso14443a_crc_append (abtSelectTag, 7);
  transmit_bytes (abtSelectTag, 9);
  abtSak = abtRx[0];

  // Test if we are dealing with a CL2
  if (abtSak & CASCADE_BIT) {
    szCL = 2;//or more
    // Check answer
    if (abtRawUid[0] != 0x88) {
      printf("WARNING: Cascade bit set but CT != 0x88!\n");
    }
  }

  if(szCL == 2) {
    // We have to do the anti-collision for cascade level 2

    // Prepare CL2 commands
    abtSelectAll[0] = 0x95;

    // Anti-collision
    transmit_bytes (abtSelectAll, 2);

    // Check answer
    if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
      printf("WARNING: BCC check failed!\n");
    }

    // Save UID CL2
    memcpy (abtRawUid + 4, abtRx, 4);

    // Selection
    abtSelectTag[0] = 0x95;
    memcpy (abtSelectTag + 2, abtRx, 5);
    iso14443a_crc_append (abtSelectTag, 7);
    transmit_bytes (abtSelectTag, 9);
    abtSak = abtRx[0];

    // Test if we are dealing with a CL3
    if (abtSak & CASCADE_BIT) {
      szCL = 3;
      // Check answer
      if (abtRawUid[0] != 0x88) {
        printf("WARNING: Cascade bit set but CT != 0x88!\n");
      }
    }

    if ( szCL == 3) {
      // We have to do the anti-collision for cascade level 3

      // Prepare and send CL3 AC-Command
      abtSelectAll[0] = 0x97;
      transmit_bytes (abtSelectAll, 2);

      // Check answer
      if ((abtRx[0] ^ abtRx[1] ^ abtRx[2] ^ abtRx[3] ^ abtRx[4]) != 0) {
        printf("WARNING: BCC check failed!\n");
      }

      // Save UID CL3
      memcpy (abtRawUid + 8, abtRx, 4);

      // Prepare and send final Select-Command
      abtSelectTag[0] = 0x97;
      memcpy (abtSelectTag + 2, abtRx, 5);
      iso14443a_crc_append (abtSelectTag, 7);
      transmit_bytes (abtSelectTag, 9);
      abtSak = abtRx[0];
    }
  }

  // Request ATS, this only applies to tags that support ISO 14443A-4
  if (abtRx[0] & SAK_FLAG_ATS_SUPPORTED) {
    iso14443a_crc_append(abtRats, 2);
    transmit_bytes (abtRats, 4);
  }

  // Done, halt the tag now
  iso14443a_crc_append(abtHalt, 2);
  transmit_bytes (abtHalt, 4);

  printf ("\nFound tag with\n UID: ");
  switch (szCL) {
    case 1:
      printf ("%02x%02x%02x%02x", abtRawUid[0], abtRawUid[1], abtRawUid[2], abtRawUid[3]);
    break;
    case 2:
      printf ("%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
      printf ("%02x%02x%02x%02x", abtRawUid[4], abtRawUid[5], abtRawUid[6], abtRawUid[7]);
    break;
    case 3:
      printf ("%02x%02x%02x", abtRawUid[1], abtRawUid[2], abtRawUid[3]);
      printf ("%02x%02x%02x", abtRawUid[5], abtRawUid[6], abtRawUid[7]);
      printf ("%02x%02x%02x%02x", abtRawUid[8], abtRawUid[9], abtRawUid[10], abtRawUid[11]);
    break;
  }
  printf("\n");
  printf("ATQA: %02x%02x\n SAK: %02x\n", abtAtqa[1], abtAtqa[0], abtSak);

  nfc_disconnect (pnd);
  return 0;
}
Exemplo n.º 27
0
int main(int argc, char **argv) {
    nfc_device *pnd;
    nfc_target nt;

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

    byte_t abtRx[MAX_FRAME_LEN];
    byte_t abtTx[MAX_FRAME_LEN];
    size_t szRx = sizeof(abtRx);
	size_t szTx;

	byte_t START_14443A[] = {0x4A, 0x01, 0x00};
	byte_t SELECT_APP[] = {0x40,0x01,0x00,0xA4,0x04,0x00,0x07,0xA0,0x00,0x00,0x00,0x42,0x10,0x10,0x00};
	byte_t READ_RECORD_VISA[] = {0x40, 0x01, 0x00, 0xB2, 0x02, 0x0C, 0x00, 0x00};
	byte_t READ_RECORD_MC[] = {0x40, 0x01, 0x00, 0xB2, 0x01, 0x14, 0x00, 0x00};
	byte_t READ_PAYLOG_VISA[] = {0x40, 0x01, 0x00, 0xB2, 0x01, 0x8C, 0x00, 0x00};
	byte_t READ_PAYLOG_MC[] = {0x40, 0x01, 0x00, 0xB2, 0x01, 0x5C, 0x00, 0x00};

	unsigned char *res, output[50], c, amount[10],msg[100];
	unsigned int i, j, expiry;

    // Initialize the libnfc and set the nfc_context
    nfc_init(&context);
    if (context == NULL) {
        printf("Unable to init libnfc(malloc)\n");
        return 1;
    }typedef unsigned char byte;

    // Open first available device
    pnd = nfc_open(context, NULL);
	if (pnd == NULL) {
		printf("Unable to connect to NFC device.\n");
		return(1);
	}
	//printf("Connected to NFC reader: %s\n", pnd->acName);
	nfc_initiator_init(pnd);

	while(1) {

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, START_14443A, sizeof(START_14443A), abtRx, szRx, 0)) {
				nfc_perror(pnd, "START_14443A");
				return(1);
		}
		//show(szRx, abtRx);

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, SELECT_APP, sizeof(SELECT_APP), abtRx, szRx, 0)) {
				nfc_perror(pnd, "SELECT_APP");
				return(1);
		}
		//show(szRx, abtRx);

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, READ_RECORD_VISA, sizeof(READ_RECORD_VISA), abtRx, szRx, 0)) {
				nfc_perror(pnd, "READ_RECORD");
				return(1);
		}
		//show(szRx, abtRx);

		/* Look for cardholder name */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x5f&&*(res+1)==0x20) {
					strncpy(output, res+3, (int) *(res+2));
					output[(int) *(res+2)]=0;
					printf("Cardholder name: %s\n",output);
					break;			
				}
				res++;
		}

		/* Look for PAN & Expiry date */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x4d&&*(res+1)==0x57) {
					strncpy(output, res+3, 13);
					output[11]=0;
					printf("PAN:");
					
					for(j=0;j<8;j++) {
						if(j%2==0) printf(" ");
						c=output[j];
						if(MASKED&j>=2&j<=5) {
							printf("**");
						}
						else {
							printf("%02x",c&0xff);
						}
					}
					printf("\n");
					expiry = (output[10]+(output[9]<<8)+(output[8]<<16))>>4;
					printf("Expiration date: %02x/20%02x\n\n",(expiry&0xff),((expiry>>8)&0xff));
					break;			
				}
				res++;
		}

		szRx = sizeof(abtRx);
        if (!nfc_initiator_transceive_bytes(pnd, READ_RECORD_MC, sizeof(READ_RECORD_MC), abtRx, szRx, 0)) {
				nfc_perror(pnd, "READ_RECORD");
				return(1);
		}
		//show(szRx, abtRx);

		/* Look for cardholder name */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x5f&&*(res+1)==0x20) {
					strncpy(output, res+3, (int) *(res+2));
					output[(int) *(res+2)]=0;
					printf("Cardholder name: %s\n",output);
					break;			
				}
				res++;
		}

		/* Look for PAN & Expiry date */
		res = abtRx;
		for(i=0;i<(unsigned int) szRx-1;i++) {
				if(*res==0x9c&&*(res+1)==0x57) {
					strncpy(output, res+3, 13);
					output[11]=0;
					printf("PAN:");
					
					for(j=0;j<8;j++) {
						if(j%2==0) printf(" ");
						c=output[j];
						if(MASKED&j>=2&j<=5) {
							printf("**");
						}
						else {
							printf("%02x",c&0xff);
						}
					}
					printf("\n");
					expiry = (output[10]+(output[9]<<8)+(output[8]<<16))>>4;
					printf("Expiration date: %02x/20%02x\n\n",(expiry&0xff),((expiry>>8)&0xff));
					break;			
				}
				res++;
		}
Exemplo n.º 28
0
int
main(int argc, const char *argv[])
{
  (void) argc;
  (void) argv;

  nfc_context *context;
  nfc_init(&context);
  if (context == NULL) {
    ERR("Unable to init libnfc (malloc)");
    exit(EXIT_FAILURE);
  }

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

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

  if (pnd == NULL) {
    ERR("%s", "Unable to open NFC device.");
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

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

  // Print the example's menu
  printf("\nSelect the communication mode:\n");
  printf("[1] Virtual card mode.\n");
  printf("[2] Wired card mode.\n");
  printf("[3] Dual card mode.\n");
  printf(">> ");

  // Take user's choice
  int input = getchar();
  printf("\n");
  if ((input < '1') || (input > '3')) {
    ERR("%s", "Invalid selection.");
    nfc_close(pnd);
    nfc_exit(context);
    exit(EXIT_FAILURE);
  }

  /*
   * '1' -> "Virtual mode" (0x02)
   * '2' -> "Wired card" (0x03)
   * '3' -> "Dual card" (0x04)
   */
  int iMode = input - '0' + 0x01;
  pn532_sam_mode mode = iMode;

  // Connect with the SAM

  switch (mode) {
    case PSM_VIRTUAL_CARD: {
      // FIXME Its a private pn53x function
      if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
        nfc_perror(pnd, "pn53x_SAMConfiguration");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      }
      printf("Now the SAM is readable for 1 minute from an external reader.\n");
      wait_one_minute();
    }
    break;

    case PSM_WIRED_CARD: {
      // Set opened NFC device to initiator mode
      if (nfc_initiator_init_secure_element(pnd) < 0) {
        nfc_perror(pnd, "nfc_initiator_init_secure_element");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      }

      // Let the reader only try once to find a tag
      if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
        nfc_perror(pnd, "nfc_device_set_property_bool");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      }
      // Read the SAM's info
      const nfc_modulation nmSAM = {
        .nmt = NMT_ISO14443A,
        .nbr = NBR_106,
      };
      nfc_target nt;

      int res;
      if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) {
        nfc_perror(pnd, "nfc_initiator_select_passive_target");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      } else if (res == 0) {
        ERR("No SAM found.");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      } else if (res == 1) {
        printf("The following ISO14443A tag (SAM) was found:\n");
        print_nfc_target(&nt, true);
      } else {
        ERR("%s", "More than one ISO14442 tag found as SAM.");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      }
    }
    break;

    case PSM_DUAL_CARD: {
      // FIXME Its a private pn53x function
      if (pn532_SAMConfiguration(pnd, mode, 0) < 0) {
        nfc_perror(pnd, "pn53x_SAMConfiguration");
        nfc_close(pnd);
        nfc_exit(context);
        exit(EXIT_FAILURE);
      }
      uint8_t  abtRx[MAX_FRAME_LEN];

      nfc_target nt = {
        .nm = {
          .nmt = NMT_ISO14443A,
          .nbr = NBR_UNDEFINED,
        },
        .nti = {
          .nai = {
            .abtAtqa = { 0x04, 0x00 },
            .abtUid = { 0x08, 0xad, 0xbe, 0xef },
            .btSak = 0x20,
            .szUidLen = 4,
            .szAtsLen = 0,
          },
        },
      };
Exemplo n.º 29
0
static nfc_device *
pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
{
  struct pn532_uart_descriptor ndd;
  int connstring_decode_level = pn532_connstring_decode(connstring, &ndd);

  if (connstring_decode_level < 2) {
    return NULL;
  }
  if (connstring_decode_level < 3) {
    ndd.speed = PN532_UART_DEFAULT_SPEED;
  }
  serial_port sp;
  nfc_device *pnd = NULL;

  log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Attempt to open: %s at %d bauds.", ndd.port, ndd.speed);
  sp = uart_open(ndd.port);

  if (sp == INVALID_SERIAL_PORT)
    log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Invalid serial port: %s", ndd.port);
  if (sp == CLAIMED_SERIAL_PORT)
    log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Serial port already claimed: %s", ndd.port);
  if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT))
    return NULL;

  // We need to flush input to be sure first reply does not comes from older byte transceive
  uart_flush_input(sp);
  uart_set_speed(sp, ndd.speed);

  // We have a connection
  pnd = nfc_device_new(context, connstring);
  snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port);

  pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
  DRIVER_DATA(pnd)->port = sp;

  // Alloc and init chip's data
  pn53x_data_new(pnd, &pn532_uart_io);
  // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532
  CHIP_DATA(pnd)->type = PN532;
  // This device starts in LowVBat mode
  CHIP_DATA(pnd)->power_mode = LOWVBAT;

  // empirical tuning
  CHIP_DATA(pnd)->timer_correction = 48;
  pnd->driver = &pn532_uart_driver;

#ifndef WIN32
  // pipe-based abort mecanism
  if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) {
    return NULL;
  }
#else
  DRIVER_DATA(pnd)->abort_flag = false;
#endif

  // Check communication using "Diagnose" command, with "Communication test" (0x00)
  if (pn53x_check_communication(pnd) < 0) {
    nfc_perror(pnd, "pn53x_check_communication");
    pn532_uart_close(pnd);
    return NULL;
  }

  pn53x_init(pnd);
  return pnd;
}
Exemplo n.º 30
0
// main entry point
int main(int argc, const char *argv[])
{
  init_leds();

  nfc_device *device_1;
  nfc_target target_1;
  nfc_context *context_1;

  nfc_device *device_2;
  nfc_target target_2;
  nfc_context *context_2;

  nfc_device *device_3;
  nfc_target target_3;
  nfc_context *context_3;

  // Initialize libnfc and set the nfc_context
  nfc_init(&context_1);
  nfc_init(&context_2);
  nfc_init(&context_3);

  if (context_1 == NULL || context_2 == NULL) || context_3 == NULL)) {
    printf("Unable to init libnfc.\n");
    exit(EXIT_FAILURE);
  }

  device_1 = nfc_open(context_1, NULL);
  device_2 = nfc_open(context_1, NULL);
  device_3 = nfc_open(context_2, NULL);

  if (device_1 == NULL || device_2 == NULL || device_3 == 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);
  }

  const char *deviceId = nfc_device_get_connstring(pnd);
  printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
  printf("NFC reader connstring: %s \n", deviceId);

  // Poll for a ISO14443A (MIFARE) tag
  const nfc_modulation nmMifare = {
    .nmt = NMT_ISO14443A,
    .nbr = NBR_106,
  };

  // get the index of the leds to flickr
  int index;
  index = atoi(argv[4]);

  const char* url;
  url = argv[3];
  char body[50];

  while(1){


    // always make sure leds are just on properly on init of loop
    // by giving an out of range index all leds will init
    flash_leds(12);

    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);
      }

      // convert the tag into human readable format to work with
      char usertagid[20]="";
      size_t szPos;
      for(szPos=0; szPos<nt.nti.nai.szUidLen;szPos++){
       sprintf(usertagid + strlen(usertagid), "%02x", nt.nti.nai.abtUid[szPos]);
     } 

      // logging
     printf("tag found w/ id \"%s\"\n", usertagid); 
     printf("writing to file \"%s\"", argv[2]);

     flash_leds(index);
     usleep(60*1000);
     flash_leds(12);
     usleep(60*1000);
     flash_leds(index);
     usleep(60*1000);
     flash_leds(12);


      // get time
     //struct timeval tv;
     //gettimeofday(&tv,NULL);
      //tv.tv_sec // seconds
      //tv.tv_usec // microseconds
     int ctime;
    ctime = ((unsigned)time(NULL))*1000; // convert to ms

      // write that tag to the file arg
      // TODO write all params so we can use this as a backup in case of no network
    FILE *file; 
      file = fopen(argv[2],"a+"); /* apend file (add text to 
      a file or create a file if it does not exist.*/ 
      fprintf(file,"%d;%s\n",ctime, usertagid); /*writes*/ 
      fclose(file); /*done!*/ 

        CURL *curl;
        CURLcode res; 
  /* get a curl handle */ 
        curl = curl_easy_init();
        if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */ 
       curl_easy_setopt(curl, CURLOPT_URL, url);
       http://stackoverflow.com/questions/11444583/command-line-curl-timeout-parameter
       curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2);
    /* Now specify the POST data */ 

    // prepare the body
    // timestamp and other fields to be added here instead of this sample data
       sprintf( body, "timestamp=%d&tagId=%s&%s", ctime, usertagid, argv[5]);
       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);

    // logging posting to 
       printf("posting to url [%s] with body [%s]", url, body);

    /* Perform the request, res will get the return code */ 
       res = curl_easy_perform(curl);
    /* Check for errors */ 
       if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
          curl_easy_strerror(res));

    /* always cleanup */ 
      curl_easy_cleanup(curl);
    }
    
    } // end of found target if

    // add some wait time here, sleep expects seconds
    // check usleep, should work with microseconds instead
    usleep(100);

  }// end of loop

  // Close NFC device
  nfc_close(pnd);
  // Release the context
  nfc_exit(context);
  exit(EXIT_SUCCESS);
}