您的位置:首页 > 其它

windows平台修改libnfc 1.7.0使其支持pn532_uart模块

2013-04-08 19:15 399 查看
前几天写了一篇文章,详述了libnfc 1.7.0在windows下的编译过程,打开了对acr122u模块、pn53x_usb模块、pn532_uart模块等的支持,今天试了一下,发现在win下libnfc默认好像只扫描使用acr122_pcsc driver和pn53x_usb driver的模块,而我连接了pn532_uart模块之后提示没有找到NFC设备。

Debug模式编译运行nfc-list.exe输出如下:

unknown libnfc.general log_level is set to 3

debug libnfc.general allow_autoscan is set to true

debug libnfc.general allow_intrusive_scan is set to false

debug libnfc.general 0 device(s) defined by user

nfc-list.exe uses libnfc 1.7.0

debug libnfc.general 0 device(s) found using acr122_pcsc driver

debug libnfc.general 0 device(s) found using pn53x_usb driver

No NFC device found.

于是,我开始查找支持pn532_uart的方法,通过查看源码发现libnfc使用两种方式获取nfc设备:

1.自动扫描 autoscan

2.用户自定义设备 user_defined_device

libnfc支持读取libnfc.conf配置文件的方式加载用户定义的设备。

最开始,我尝试了在cmake生成的config.h里增加#define CONFFILES,重新编译的时候报错:

undefined reference to '_imp__regcomp'

这是由于conf.c里面调用了regcomp等函数,这些函数属于regex正则解析库,但是我使用的MinGW64里面没有这个库。

于是我复制了早期的MinGW的libregex库以及头文件,编译能通过,但是运行nfc-list.exe运行到读取配置文件时就出错退出了,估计还是正则库有问题。

libregex使用不了也没关系,其实可以修改conf.c去掉正则解析的过程,直接将参数赋值给nfc_context。

主要需要修改config.h和conf.c两个文件。

在config.h里增加 #define CONFFILES

修改conf.c如下:

#include "conf.h"

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif // HAVE_CONFIG_H

#ifdef CONFFILES
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <regex.h>
#include <sys/stat.h>

#include <nfc/nfc.h>
#include "nfc-internal.h"
#include "log.h"

#define LOG_CATEGORY "libnfc.config"
#define LOG_GROUP    NFC_LOG_GROUP_CONFIG

#ifndef LIBNFC_SYSCONFDIR
// If this define does not already exists, we build it using SYSCONFDIR
#ifndef SYSCONFDIR
#error "SYSCONFDIR is not defined but required."
#endif // SYSCONFDIR
#define LIBNFC_SYSCONFDIR      SYSCONFDIR"/nfc"
#endif // LIBNFC_SYSCONFDIR

//#define LIBNFC_CONFFILE        LIBNFC_SYSCONFDIR"/libnfc.conf"
//#define LIBNFC_DEVICECONFDIR   LIBNFC_SYSCONFDIR"/devices.d"

#define LIBNFC_CONFFILE        "libnfc.conf"
#define LIBNFC_DEVICECONFDIR   "devices.d"

static bool
conf_parse_file(const char *filename, void (*conf_keyvalue)(void *data), void *data)
{
printf("Try to Conf Key Value by chenxupro\n");

conf_keyvalue(data);

return true;
}

static void
conf_keyvalue_context(void *data)
{
nfc_context *context = (nfc_context *)data;
//if (strcmp(key, "allow_autoscan") == 0) {
string_as_boolean("true", &(context->allow_autoscan));
//} else if (strcmp(key, "allow_intrusive_scan") == 0) {
string_as_boolean("false", &(context->allow_intrusive_scan));
//} else if (strcmp(key, "log_level") == 0) {
context->log_level = atoi("2");
//} else if (strcmp(key, "device.name") == 0) {
if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].name, "") != 0) {
if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices.");
return;
}
context->user_defined_device_count++;
}
strcpy(context->user_defined_devices[context->user_defined_device_count - 1].name, "chenxupro PN532");
//} else if (strcmp(key, "device.connstring") == 0) {
if ((context->user_defined_device_count == 0) || strcmp(context->user_defined_devices[context->user_defined_device_count - 1].connstring, "") != 0) {
if (context->user_defined_device_count >= MAX_USER_DEFINED_DEVICES) {
log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Configuration exceeded maximum user-defined devices.");
return;
}
context->user_defined_device_count++;
}
strcpy(context->user_defined_devices[context->user_defined_device_count - 1].connstring, "pn532_uart:COM9:115200");
}

void conf_load(nfc_context *context)
{
conf_parse_file(LIBNFC_CONFFILE, conf_keyvalue_context, context);
}

#endif // CONFFILES


重新编译,运行结果如下:

unknown libnfc.general log_level is set to 2

debug libnfc.general allow_autoscan is set to true

debug libnfc.general allow_intrusive_scan is set to false

debug libnfc.general 1 device(s) defined by user

debug libnfc.general #0 name: "chenxupro PN532", connstring: "pn532_uart:COM9:115200"

debug libnfc.general 0 device(s) found using acr122_pcsc driver

debug libnfc.general 0 device(s) found using pn53x_usb driver

debug libnfc.driver.pn532_uart Attempt to open: COM9 at 115200 bauds.

Try to Conf Key Value by chenxupro

nfc-list.exe uses libnfc 1.7.0

NFC device: pn532_uart:COM9 opened

1 ISO14443A passive target(s) found:

ISO/IEC 14443A (106 kbps) target:

ATQA (SENS_RES): 00 04

UID (NFCID1): 7d 88 c9 1d

SAK (SEL_RES): 08
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: