PN532实现卡模拟功能

背景

使用Android手机和pn532进行通信,此时,PN532应完全模拟一张NFC标签,本文使用PN532模拟NFC Forum Type 2标准的标签

如何实现

此时使用了之前说的libnfc的demo-examples\nfc-emulate-forum-tag2.c,此功能就是使用PN532模拟一张NFC Forum Type 2标准的标签

1、编译安装

libnfc源码编译与使用_让我来敲的博客-CSDN博客

2、打开调试开关

# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
#allow_autoscan = true

# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
#allow_intrusive_scan = false

# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
##############################      HERE!!!!!!!!!!!!!!!!!!!!!!!#############
log_level = 3

# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "microBuilder.eu"
device.connstring = "pn532_uart:/dev/ttyUSB0"

3、执行生成物

执行之后,使用安卓手机靠近PN532,控制台会出现如下输出:

debug	libnfc.chip.pn53x	Diagnose+SAMConfiguration
debug	libnfc.bus.uart	TX: 55 55 00 00 00  00 00 ff 03 fd d4 14 01 17 00 

debug	libnfc.chip.pn53x	SetParameters
debug	libnfc.bus.uart	TX: 00 00 ff 03 fd d4 12 14 06 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 02 fe 
debug	libnfc.bus.uart	RX: d5 13 
debug	libnfc.bus.uart	RX: 18 00 

debug	libnfc.chip.pn53x	WriteRegister
debug	libnfc.bus.uart	TX: 00 00 ff 08 f8 d4 08 63 02 80 63 03 80 59 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 02 fe 
debug	libnfc.bus.uart	RX: d5 09 
debug	libnfc.bus.uart	RX: 22 00 

debug	libnfc.chip.pn53x	RFConfiguration
debug	libnfc.bus.uart	TX: 00 00 ff 04 fc d4 32 01 00 f9 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 02 fe 
debug	libnfc.bus.uart	RX: d5 33 
debug	libnfc.bus.uart	RX: f8 00 

debug	libnfc.chip.pn53x	SetParameters
debug	libnfc.bus.uart	TX: 00 00 ff 03 fd d4 12 10 0a 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 02 fe 
debug	libnfc.bus.uart	RX: d5 13 
debug	libnfc.bus.uart	RX: 18 00 

debug	libnfc.chip.pn53x	WriteRegister
debug	libnfc.bus.uart	TX: 00 00 ff 05 fb d4 08 63 05 04 b8 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 02 fe 
debug	libnfc.bus.uart	RX: d5 09 
debug	libnfc.bus.uart	RX: 22 00 

debug	libnfc.chip.pn53x	TgInitAsTarget
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 27 d9 d4 8c 01 04 00 00 b0 0b 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 e0 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 05 fb 
debug	libnfc.bus.uart	RX: d5 8d 
debug	libnfc.bus.uart	RX: 00 30 00 
debug	libnfc.bus.uart	RX: 6e 00 

debug	libnfc.chip.pn53x	TgResponseToInitiator
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 12 ee d4 90 00 00 00 00 00 00 00 00 00 00 00 00 e1 10 06 0f 96 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 03 fd 
debug	libnfc.bus.uart	RX: d5 91 
debug	libnfc.bus.uart	RX: 00 
debug	libnfc.bus.uart	RX: 9a 00 

debug	libnfc.chip.pn53x	TgGetInitiatorCommand
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 02 fe d4 88 a4 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 05 fb 
debug	libnfc.bus.uart	RX: d5 89 
debug	libnfc.bus.uart	RX: 00 30 04 
debug	libnfc.bus.uart	RX: 6e 00 

debug	libnfc.chip.pn53x	TgResponseToInitiator
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 12 ee d4 90 03 21 d1 02 1c 53 70 91 01 09 54 02 65 6e 4c 69 4d 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 03 fd 
debug	libnfc.bus.uart	RX: d5 91 
debug	libnfc.bus.uart	RX: 00 
debug	libnfc.bus.uart	RX: 9a 00 

debug	libnfc.chip.pn53x	TgGetInitiatorCommand
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 02 fe d4 88 a4 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 05 fb 
debug	libnfc.bus.uart	RX: d5 89 
debug	libnfc.bus.uart	RX: 00 30 08 
debug	libnfc.bus.uart	RX: 6a 00 

debug	libnfc.chip.pn53x	TgResponseToInitiator
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 12 ee d4 90 62 6e 66 63 51 01 0b 55 03 6c 69 62 6e 66 63 2e b2 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 03 fd 
debug	libnfc.bus.uart	RX: d5 91 
debug	libnfc.bus.uart	RX: 00 
debug	libnfc.bus.uart	RX: 9a 00 

debug	libnfc.chip.pn53x	TgGetInitiatorCommand
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 02 fe d4 88 a4 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 05 fb 
debug	libnfc.bus.uart	RX: d5 89 
debug	libnfc.bus.uart	RX: 00 30 0c 
debug	libnfc.bus.uart	RX: 66 00 

debug	libnfc.chip.pn53x	TgResponseToInitiator
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 12 ee d4 90 6f 72 67 00 00 00 00 00 00 00 00 00 00 00 00 00 54 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 03 fd 
debug	libnfc.bus.uart	RX: d5 91 
debug	libnfc.bus.uart	RX: 00 
debug	libnfc.bus.uart	RX: 9a 00 

debug	libnfc.chip.pn53x	TgGetInitiatorCommand
debug	libnfc.chip.pn53x	No timeout
debug	libnfc.bus.uart	TX: 00 00 ff 02 fe d4 88 a4 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 05 fb 
debug	libnfc.bus.uart	RX: d5 89 
debug	libnfc.bus.uart	RX: 00 50 00 
debug	libnfc.bus.uart	RX: 52 00 

debug	libnfc.chip.pn53x	InRelease
debug	libnfc.bus.uart	TX: 00 00 ff 03 fd d4 52 00 da 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 03 fd 
debug	libnfc.bus.uart	RX: d5 53 
debug	libnfc.bus.uart	RX: 00 
debug	libnfc.bus.uart	RX: d8 00 

debug	libnfc.chip.pn53x	PowerDown
debug	libnfc.bus.uart	TX: 00 00 ff 03 fd d4 16 f0 26 00 
debug	libnfc.bus.uart	RX: 00 00 ff 00 ff 00 
debug	libnfc.chip.pn53x	PN53x ACKed
debug	libnfc.bus.uart	RX: 00 00 ff 03 fd 
debug	libnfc.bus.uart	RX: d5 17 
debug	libnfc.bus.uart	RX: 00 
debug	libnfc.bus.uart	RX: 14 00 

开发者需自定义卡片的内容,demo中的卡片数据为

static uint8_t __nfcforum_tag2_memory_area[] = {
  0x00, 0x00, 0x00, 0x00,  // Block 0
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xFF, 0xFF,  // Block 2 (Static lock bytes: CC area and data area are read-only locked)
  0xE1, 0x10, 0x06, 0x0F,  // Block 3 (CC - NFC-Forum Tag Type 2 version 1.0, Data area (from block 4 to the end) is 48 bytes, Read-only mode)

  0x03, 33,   0xd1, 0x02,  // Block 4 (NDEF)
  0x1c, 0x54, 0x70, 0x91,
  0x01, 0x09, 0x54, 0x02,
  0x65, 0x6e, 0x4c, 0x69,

  0x62, 0x6e, 0x66, 0x63,
  0x51, 0x01, 0x0b, 0x55,
  0x03, 0x6c, 0x69, 0x62,
  0x6e, 0x66, 0x63, 0x2e,

  0x6f, 0x72, 0x67, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00,
};

格式解析如下:

 第4快的0x06表示Data区总共有6*8=48个字节,实际开发需要修改此字段,从第5块的第3个字节开始就是一个NDef消息,需自己填入内容,可参考NDEF协议格式,33是NDEF的长度,这种模拟情况下,最多传递250个字节的数据,理论上应该是251个,但是最终有问题,如果有了解的童靴可以一起交流哈。

是 JIS-X-6319-4-2019-ENG.pdf 中定义的该卡的数据组织方式,详见https://www.docin.com/p-2074660216.html

备注:若开发者需要高度定制,需参考demo和PN532的交互流程实现前置流程

最后,本人仅对这种模式初步探究,如何传递更大的数据,以及安卓如何写入未探索,欢迎大家一起分享学习

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值