Exemple #1
0
int
pn53x_usb_init (nfc_device *pnd)
{
  int res = 0;
  // Sometimes PN53x USB doesn't reply ACK one the first frame, so we need to send a dummy one...
  //pn53x_check_communication (pnd); // Sony RC-S360 doesn't support this command for now so let's use a get_firmware_version instead:
  const uint8_t abtCmd[] = { GetFirmwareVersion };
  pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), NULL, 0, 0);
  // ...and we don't care about error
  pnd->last_error = 0;
  if (SONY_RCS360 == DRIVER_DATA (pnd)->model) {
    log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "SONY RC-S360 initialization.");
    const uint8_t abtCmd2[] = { 0x18, 0x01 };
    pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, 0, 0);
    pn53x_usb_ack (pnd);
  }

  if ((res = pn53x_init (pnd)) < 0)
    return res;

  if (ASK_LOGO == DRIVER_DATA (pnd)->model) {
    log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "ASK LoGO initialization.");
    /* Internal registers */
    /* Disable 100mA current limit, Power on Secure IC (SVDD) */
    pn53x_write_register (pnd, PN53X_REG_Control_switch_rng, 0xFF, SYMBOL_CURLIMOFF | SYMBOL_SIC_SWITCH_EN | SYMBOL_RANDOM_DATAREADY);
    /* Select the signal to be output on SIGOUT: Modulation signal (envelope) from the internal coder */
    pn53x_write_register (pnd, PN53X_REG_CIU_TxSel, 0xFF, 0x14);

    /* SFR Registers */
    /* Setup push-pulls for pins from P30 to P35 */
    pn53x_write_register (pnd, PN53X_SFR_P3CFGB, 0xFF, 0x37);

/*
On ASK LoGO hardware:
  LEDs port bits definition: 
   * LED 1: bit 2 (P32)
   * LED 2: bit 1 (P31)
   * LED 3: bit 0 or 3 (depending of hardware revision) (P30 or P33)
   * LED 4: bit 5 (P35)
  Notes: 
   * Set logical 0 to switch LED on; logical 1 to switch LED off. 
   * Bit 4 should be maintained at 1 to keep RF field on.

  Progressive field activation:
   The ASK LoGO hardware can progressively power-up the antenna.
   To use this feature we have to switch on the field by switching on
   the field on PN533 (RFConfiguration) then set P34 to '1', and cut-off the 
   field by switching off the field on PN533 then set P34 to '0'.
*/

    /* Set P30, P31, P33, P35 to logic 1 and P32, P34 to 0 logic */
    /* ie. Switch LED1 on and turn off progressive field */
    pn53x_write_register (pnd, PN53X_SFR_P3, 0xFF, _BV (P30) | _BV (P31) | _BV (P33) | _BV (P35));
  }

  return NFC_SUCCESS;
}
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;
}
int
main (int argc, const char *argv[])
{
  size_t  szFound;
  size_t  i;
  nfc_device_t *pnd;
  nfc_device_desc_t *pnddDevices;
  const char *acLibnfcVersion;
  bool    result;

  byte_t  abtRx[PN53x_EXTENDED_FRAME_MAX_LEN];
  size_t  szRx;
  const byte_t pncmd_diagnose_communication_line_test[] = { 0xD4, 0x00, 0x00, 0x06, 'l', 'i', 'b', 'n', 'f', 'c' };
  const byte_t pncmd_diagnose_rom_test[] = { 0xD4, 0x00, 0x01 };
  const byte_t pncmd_diagnose_ram_test[] = { 0xD4, 0x00, 0x02 };

  if (argc > 1) {
    errx (1, "usage: %s", argv[0]);
  }
  // Display libnfc version
  acLibnfcVersion = nfc_version ();
  printf ("%s use libnfc %s\n", argv[0], acLibnfcVersion);

  if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) {
    fprintf (stderr, "malloc() failed\n");
    return EXIT_FAILURE;
  }

  nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound);

  if (szFound == 0) {
    printf ("No NFC device found.\n");
  }

  for (i = 0; i < szFound; i++) {
    pnd = nfc_connect (&(pnddDevices[i]));

    if (pnd == NULL) {
      ERR ("%s", "Unable to connect to NFC device.");
      return EXIT_FAILURE;
    }

    printf ("NFC device [%s] connected.\n", pnd->acName);

    result = pn53x_transceive (pnd, pncmd_diagnose_communication_line_test, sizeof (pncmd_diagnose_communication_line_test), abtRx, &szRx);
    if (result) {
      result = (memcmp (pncmd_diagnose_communication_line_test + 2, abtRx, sizeof (pncmd_diagnose_communication_line_test) - 2) == 0);
    }
    printf (" Communication line test: %s\n", result ? "OK" : "Failed");

    result = pn53x_transceive (pnd, pncmd_diagnose_rom_test, sizeof (pncmd_diagnose_rom_test), abtRx, &szRx);
    if (result) {
      result = ((szRx == 1) && (abtRx[0] == 0x00));
    }
    printf (" ROM test: %s\n", result ? "OK" : "Failed");

    result = pn53x_transceive (pnd, pncmd_diagnose_ram_test, sizeof (pncmd_diagnose_ram_test), abtRx, &szRx);
    if (result) {
      result = ((szRx == 1) && (abtRx[0] == 0x00));
    }
    printf (" RAM test: %s\n", result ? "OK" : "Failed");
  }
}