libNFC 是不是只支持 READ、WRITE 和 COMP_WRITE

Posted

技术标签:

【中文标题】libNFC 是不是只支持 READ、WRITE 和 COMP_WRITE【英文标题】:Does libNFC only support READ, WRITE and COMP_WRITElibNFC 是否只支持 READ、WRITE 和 COMP_WRITE 【发布时间】:2017-09-14 19:08:39 【问题描述】:

一周前我买了一个 NFC 阅读器 (PN532 I2C) 和 5 个 NFC 标签 (NTAG216),目标是为 Java 创建一个接口,这样我就可以在我的项目中使用 Java。界面按预期工作,但是当我尝试了 NTAG216 支持的所有命令(NTAG216 data sheet 第 32 页)时,只有 READ、WRITE 和 COMP_WRITE 工作。这不是接口造成的,我也在C中尝试过,但结果相同。那么,我的命令中是否有任何错误,或者 libNFC 是否以任何方式阻止了这些命令的执行? 我很抱歉英语可能不好,我希望我的问题仍然可以理解。

感谢您的回答, 雅各布 代码:

public class Main 

  public static void main(String[] args) 
    System.load(new File("libNFC-JavaInterface.so").getAbsolutePath());
    Context context = new Context();
    Device device = new Device(context);
    Target target = new Target();
    device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106), null,
        target);
    int[] response;

    System.out.print("UID: ");
    try 
      System.out.printf("%014X", getUID(device));
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Version: ");
    try 
      response = getVersion(device);
      for (int i = 0; i < response.length; i++) 
        System.out.printf("%02X", response[i]);
      
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Read: ");
    try 
      response = read(device, 0x08);
      for (int i = 0; i < response.length; i++) 
        System.out.printf("%02X", response[i]);
      
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Fast Read: ");
    try 
      response = fastRead(device, 0x08, 0x11);
      for (int i = 0; i < response.length; i++) 
        System.out.printf("%02X", response[i]);
      
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Write: ");
    try 
      write(device, 0x08, new int[] 0x41, 0x42, 0x43, 0x44);
      System.out.print("SUCCESSFUL");
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Compatibility Write: ");
    try 
      compatibilityWrite(device, 0x09, new int[] 0x45, 0x46, 0x47, 0x48);
      System.out.print("SUCCESSFUL");
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Read cnt: ");
    try 
      response = readCnt(device);
      for (int i = 0; i < response.length; i++) 
        System.out.printf("%02X", response[i]);
      
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    System.out.print("Read sig: ");
    try 
      response = readSig(device);
      for (int i = 0; i < response.length; i++) 
        System.out.printf("%02X", response[i]);
      
     catch (NFCException e) 
      device.selectPassiveTarget(new Modulation(ModulationType.NMT_ISO14443A, BaudRate.NBR_106),
          null, target);
      System.out.print("FAILED");
    
    System.out.println();

    target.free();
    device.close();
    context.exit();
  

  public static long getUID(Device device) throws NFCException 
    int[] response = read(device, 0x00);
    return (long) response[0] << 48 | (long) response[1] << 40 | (long) response[2] << 32
        | (long) response[4] << 24 | (long) response[5] << 16 | (long) response[6] << 8
        | (long) response[7] << 0;
  

  public static int[] getVersion(Device device) throws NFCException 
    int[] response = new int[8];
    device.transceiveBytes(new int[] 0x60, response, -1);
    return response;
  

  public static int[] read(Device device, int startPage) throws NFCException 
    int[] response = new int[16];
    device.transceiveBytes(new int[] 0x30, startPage & 0xFF, response, -1);
    return response;
  

  public static int[] fastRead(Device device, int startPage, int endPage) throws NFCException 
    int[] response = new int[((endPage &= 0xFF) - (startPage &= 0xFF) + 1) * 4];
    device.transceiveBytes(new int[] 0x3A, startPage, endPage, response, -1);
    return response;
  

  public static void write(Device device, int startPage, int[] data) throws NFCException 
    int[] command = new int[6];
    command[0] = 0xA2;
    command[1] = startPage & 0xFF;
    for (int i = 0; i < data.length; i++) 
      command[2 + i] = data[i] & 0xFF;
    
    device.transceiveBytes(command, new int[0], -1);
  

  public static void compatibilityWrite(Device device, int startPage, int[] data)
      throws NFCException 
    int[] command = new int[18];
    command[0] = 0xA0;
    command[1] = startPage & 0xFF;
    for (int i = 0; i < data.length; i++) 
      command[2 + i] = data[i] & 0xFF;
    
    device.transceiveBytes(command, new int[0], -1);
  

  public static int[] readCnt(Device device) throws NFCException 
    int[] response = new int[3];
    device.transceiveBytes(new int[] 0x39, 0x02, response, -1);
    return response;
  

  public static int[] readSig(Device device) throws NFCException 
    int[] response = new int[32];
    device.transceiveBytes(new int[] 0x3C, 0x00, response, -1);
    return response;
  

并按照输出:

UID: 04675362D55681
Version: error  libnfc.driver.pn532_i2c Application level error detected  (127)
FAILED
Read: 41424344454647480000000000000000
Fast Read: error    libnfc.driver.pn532_i2c Application level error detected  (127)
FAILED
Write: SUCCESSFUL
Compatibility Write: SUCCESSFUL
Read cnt: error libnfc.driver.pn532_i2c Application level error detected  (127)
FAILED
Read sig: error libnfc.driver.pn532_i2c Application level error detected  (127)
FAILED

【问题讨论】:

【参考方案1】:

我没有尝试使用 libNFC,但我的代码遇到了同样的问题,所以它可能是相同的修复...

对于 NTAG2xx 特定函数(GET_VERSION、FAST_READ、READ_CNT、READ_SIG),您需要使用 PN532 函数“InCommunicateThru”(0x42)而不是“InDataExchange”(0x40)。这个仅适用于标准 NFC 论坛功能(READ、WRITE)。

【讨论】:

当我搜索 pn53x.c 时,我发现: if (pnd->bEasyFraming) abtCmd[0] = InDataExchange; ... 其他 abtCmd[0] = InCommunicateThru; ... 进一步查看后,我发现如何禁用 easyFraming:nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false);所以现在我为(GET_VERSION,FAST_READ,READ_CNT,READ_SIG)禁用它,其余的它将被启用。一切正常,除了 READ_CNT 这现在会导致 RF Transmisson 错误,但目前就我的目的而言没问题。 对于 READ_CNT,确保它在 NFC 标签上被激活,否则你会得到一个错误(NFC_CNT_EN 位在 ACCESS 字节中设置为 1,在配置页面中)

以上是关于libNFC 是不是只支持 READ、WRITE 和 COMP_WRITE的主要内容,如果未能解决你的问题,请参考以下文章

使用 libnfc 格式化/读取/写入 NDEF Mifare 1K 卡

SDK中的readonly&readwrite

如何检查某个用户是不是在根节点上具有 Write, read 权限

Hive Read & Write Flink

从 NFC 阅读器直接读取到桌面 Web 应用程序?

linux中read,write和recv,send的区别