From 5a5bdf1d66203283b4491e97ee034cbd2fc9b691 Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Fri, 17 Feb 2012 12:09:56 +0000 Subject: [PATCH] add nfc_device_get_supported_modulation() and nfc_device_get_supported_baud_rate() functions. --- include/nfc/nfc-types.h | 11 ++++- include/nfc/nfc.h | 2 + libnfc/chips/pn53x.c | 85 +++++++++++++++++++++++++++++++++++++ libnfc/chips/pn53x.h | 5 +++ libnfc/drivers/acr122.c | 2 + libnfc/drivers/acr122s.c | 2 + libnfc/drivers/arygon.c | 2 + libnfc/drivers/pn532_uart.c | 2 + libnfc/drivers/pn53x_usb.c | 2 + libnfc/nfc-internal.h | 2 + libnfc/nfc.c | 12 ++++++ 11 files changed, 126 insertions(+), 1 deletion(-) diff --git a/include/nfc/nfc-types.h b/include/nfc/nfc-types.h index b0c001c..19e1416 100644 --- a/include/nfc/nfc-types.h +++ b/include/nfc/nfc-types.h @@ -276,7 +276,7 @@ typedef enum { * @brief NFC modulation type enumeration */ typedef enum { - NMT_ISO14443A, + NMT_ISO14443A = 1, NMT_JEWEL, NMT_ISO14443B, NMT_ISO14443BI, // pre-ISO14443B aka ISO/IEC 14443 B' or Type B' @@ -286,6 +286,15 @@ typedef enum { NMT_DEP, } nfc_modulation_type; +/** + * @enum nfc_mode + * @brief NFC mode type enumeration + */ +typedef enum { + N_TARGET, + N_INITIATOR, +} nfc_mode; + /** * @struct nfc_modulation * @brief NFC modulation structure diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 5ca6639..24c2393 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -113,6 +113,8 @@ extern "C" { NFC_EXPORT void iso14443a_crc_append (uint8_t *pbtData, size_t szLen); NFC_EXPORT uint8_t *iso14443a_locate_historical_bytes (uint8_t *pbtAts, size_t szAts, size_t *pszTk); NFC_EXPORT const char *nfc_version (void); + NFC_EXPORT int nfc_device_get_supported_modulation (nfc_device *pnd, const nfc_mode mode, nfc_modulation_type **supported_mt); + NFC_EXPORT int nfc_device_get_supported_baud_rate (nfc_device *pnd, const nfc_modulation_type nmt, nfc_baud_rate **supported_br); /* Error codes */ /** @ingroup error diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index bdd774f..ee85a57 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -49,6 +49,13 @@ const uint8_t pn53x_ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 }; const uint8_t pn53x_nack_frame[] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00 }; static const uint8_t pn53x_error_frame[] = { 0x00, 0x00, 0xff, 0x01, 0xff, 0x7f, 0x81, 0x00 }; +const nfc_baud_rate pn53x_iso14443a_supported_baud_rates[] = { NBR_106, 0 }; +const nfc_baud_rate pn53x_felica_supported_baud_rates[] = { NBR_424, NBR_212, 0 }; +const nfc_baud_rate pn53x_dep_supported_baud_rates[] = { NBR_424, NBR_212, NBR_106, 0 }; +const nfc_baud_rate pn53x_jewel_supported_baud_rates[] = { NBR_106, 0 }; +const nfc_baud_rate pn532_iso14443b_supported_baud_rates[] = { NBR_106, 0 }; +const nfc_baud_rate pn533_iso14443b_supported_baud_rates[] = { NBR_847, NBR_424, NBR_212, NBR_106, 0 }; +const nfc_modulation_type pn53x_supported_modulation_as_target[] = {NMT_ISO14443A, NMT_FELICA, NMT_DEP, 0}; /* prototypes */ int pn53x_reset_settings (struct nfc_device *pnd); @@ -68,6 +75,32 @@ pn53x_init(struct nfc_device *pnd) return res; } + if (!CHIP_DATA(pnd)->supported_modulation_as_initiator) { + CHIP_DATA(pnd)->supported_modulation_as_initiator = malloc(sizeof(nfc_modulation) * 9); + int nbSupportedModulation = 0; + if ((pnd->btSupportByte & SUPPORT_ISO14443A)) { + CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_ISO14443A; + nbSupportedModulation++; + CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_FELICA; + nbSupportedModulation++; + } + if (pnd->btSupportByte & SUPPORT_ISO14443B) { + CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_ISO14443B; + nbSupportedModulation++; + } + if(CHIP_DATA(pnd)->type != PN531) { + CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_JEWEL; + nbSupportedModulation++; + } + CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_DEP; + nbSupportedModulation++; + CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = 0; + } + + if (!CHIP_DATA(pnd)->supported_modulation_as_target) { + CHIP_DATA(pnd)->supported_modulation_as_target = pn53x_supported_modulation_as_target; + } + // CRC handling should be enabled by default as declared in nfc_device_new // which is the case by default for pn53x, so nothing to do here // Parity handling should be enabled by default as declared in nfc_device_new @@ -2758,6 +2791,51 @@ pn53x_nm_to_ptt(const nfc_modulation nm) return PTT_UNDEFINED; } +int +pn53x_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, nfc_modulation_type **supported_mt) +{ + switch (mode) { + case N_TARGET: + *supported_mt = CHIP_DATA(pnd)->supported_modulation_as_target; + break; + case N_INITIATOR: + *supported_mt = CHIP_DATA(pnd)->supported_modulation_as_initiator; + break; + default: + return NFC_EINVARG; + } + return NFC_SUCCESS; +} + +int +pn53x_get_supported_baud_rate (nfc_device *pnd, const nfc_modulation_type nmt, nfc_baud_rate **supported_br) +{ + switch (nmt) { + case NMT_FELICA: + *supported_br = (nfc_baud_rate*)pn53x_felica_supported_baud_rates; + break; + case NMT_ISO14443A: + *supported_br = (nfc_baud_rate*)pn53x_iso14443a_supported_baud_rates; + break; + case NMT_ISO14443B: { + if ((CHIP_DATA(pnd)->type != PN533)) { + *supported_br = (nfc_baud_rate*)pn532_iso14443b_supported_baud_rates; + } else { + *supported_br = (nfc_baud_rate*)pn533_iso14443b_supported_baud_rates; + } + } + break; + case NMT_JEWEL: + *supported_br = (nfc_baud_rate*)pn53x_jewel_supported_baud_rates; + break; + case NMT_DEP: + *supported_br = (nfc_baud_rate*)pn53x_dep_supported_baud_rates; + break; + return NFC_EINVARG; + } + return NFC_SUCCESS; +} + void pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io) { @@ -2794,6 +2872,10 @@ pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io) // Set default communication timeout (52 ms) CHIP_DATA (pnd)->timeout_communication = 52; + + CHIP_DATA (pnd)->supported_modulation_as_initiator = NULL; + + CHIP_DATA (pnd)->supported_modulation_as_target = NULL; } void @@ -2802,5 +2884,8 @@ pn53x_data_free (struct nfc_device *pnd) if (CHIP_DATA (pnd)->current_target) { free (CHIP_DATA (pnd)->current_target); } + if (CHIP_DATA(pnd)->supported_modulation_as_initiator) { + free (CHIP_DATA(pnd)->supported_modulation_as_initiator); + } free (pnd->chip_data); } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index c88e931..a5d1f5f 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -187,6 +187,9 @@ struct pn53x_data { int timeout_atr; /** Communication timeout */ int timeout_communication; +/** Supported modulation type */ + nfc_modulation_type *supported_modulation_as_initiator; + nfc_modulation_type *supported_modulation_as_target; }; #define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data)) @@ -377,6 +380,8 @@ int pn53x_RFConfiguration__MaxRetries (struct nfc_device *pnd, const uint8_t int pn53x_check_ack_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); int pn53x_check_error_frame (struct nfc_device *pnd, const uint8_t *pbtRxFrame, const size_t szRxFrameLen); int pn53x_build_frame (uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, const size_t szData); +int pn53x_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, nfc_modulation_type **supported_mt); +int pn53x_get_supported_baud_rate(nfc_device *pnd, const nfc_modulation_type nmt, nfc_baud_rate **supported_br); void pn53x_data_new (struct nfc_device *pnd, const struct pn53x_io *io); void pn53x_data_free (struct nfc_device *pnd); diff --git a/libnfc/drivers/acr122.c b/libnfc/drivers/acr122.c index 2acfaf8..e39b35c 100644 --- a/libnfc/drivers/acr122.c +++ b/libnfc/drivers/acr122.c @@ -488,6 +488,8 @@ const struct nfc_driver acr122_driver = { .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, + .get_supported_modulation = pn53x_get_supported_modulation, + .get_supported_baud_rate = pn53x_get_supported_baud_rate, .abort_command = NULL, // FIXME: abort is not supported in this driver .idle = NULL, // FIXME: idle is not supported in this driver diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 4f2b2ff..77e70b7 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -743,6 +743,8 @@ const struct nfc_driver acr122s_driver = { .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, + .get_supported_modulation = pn53x_get_supported_modulation, + .get_supported_baud_rate = pn53x_get_supported_baud_rate, .abort_command = acr122s_abort_command, .idle = NULL, diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 56fd340..af9135b 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -585,6 +585,8 @@ const struct nfc_driver arygon_driver = { .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, + .get_supported_modulation = pn53x_get_supported_modulation, + .get_supported_baud_rate = pn53x_get_supported_baud_rate, .abort_command = arygon_abort_command, .idle = NULL, // FIXME arygon driver does not support idle() diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 19d3efc..992a948 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -527,6 +527,8 @@ const struct nfc_driver pn532_uart_driver = { .device_set_property_bool = pn53x_set_property_bool, .device_set_property_int = pn53x_set_property_int, + .get_supported_modulation = pn53x_get_supported_modulation, + .get_supported_baud_rate = pn53x_get_supported_baud_rate, .abort_command = pn532_uart_abort_command, .idle = pn53x_idle, diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 75f90fc..8b94fdc 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -773,6 +773,8 @@ const struct nfc_driver pn53x_usb_driver = { .device_set_property_bool = pn53x_usb_set_property_bool, .device_set_property_int = pn53x_set_property_int, + .get_supported_modulation = pn53x_get_supported_modulation, + .get_supported_baud_rate = pn53x_get_supported_baud_rate, .abort_command = pn53x_usb_abort_command, .idle = pn53x_idle, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index dfe8107..3ff8300 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -151,6 +151,8 @@ struct nfc_driver { int (*device_set_property_bool) (struct nfc_device *pnd, const nfc_property property, const bool bEnable); int (*device_set_property_int) (struct nfc_device *pnd, const nfc_property property, const int value); + int (*get_supported_modulation) (struct nfc_device *pnd, const nfc_mode mode, nfc_modulation_type **supported_mt); + int (*get_supported_baud_rate) (struct nfc_device *pnd, const nfc_modulation_type nmt, nfc_baud_rate **supported_br); int (*abort_command) (struct nfc_device *pnd); int (*idle) (struct nfc_device *pnd); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index eeeb683..dbfaafe 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -1001,3 +1001,15 @@ nfc_version (void) return PACKAGE_VERSION; #endif // SVN_REVISION } + +int +nfc_device_get_supported_modulation (nfc_device *pnd, const nfc_mode mode, nfc_modulation_type **supported_mt) +{ + HAL (get_supported_modulation, pnd, mode, supported_mt); +} + +int +nfc_device_get_supported_baud_rate (nfc_device *pnd, const nfc_modulation_type nmt, nfc_baud_rate **supported_br) +{ + HAL (get_supported_baud_rate, pnd, nmt, supported_br); +} \ No newline at end of file