From 9a254712b58fa075a60581ac0f5ec7222738c2a0 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 12 Mar 2014 00:28:39 +0100 Subject: [PATCH] nfc_initiator_select_passive_target(): validate modulation and baud rate --- libnfc/nfc.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 2094765..4294367 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -158,6 +158,8 @@ nfc_drivers_init(void) #endif /* DRIVER_ARYGON_ENABLED */ } +static int +nfc_device_validate_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation *nm); /** @ingroup lib * @brief Register an NFC device driver with libnfc. @@ -526,6 +528,9 @@ nfc_initiator_select_passive_target(nfc_device *pnd, uint8_t *abtInit = NULL; uint8_t abtTmpInit[MAX(12, szInitData)]; size_t szInit = 0; + int res; + if ((res = nfc_device_validate_modulation(pnd, N_INITIATOR, &nm)) != NFC_SUCCESS) + return res; if (szInitData == 0) { // Provide default values, if any prepare_initiator_data(nm, &abtInit, &szInit); @@ -1207,6 +1212,38 @@ nfc_device_get_supported_baud_rate(nfc_device *pnd, const nfc_mode mode, const n HAL(get_supported_baud_rate, pnd, mode, nmt, supported_br); } +/** @ingroup data + * @brief Validate combination of modulation and baud rate on the currently used device. + * @return Returns 0 on success, otherwise returns libnfc's error code (negative value) + * @param pnd \a nfc_device struct pointer that represent currently used device + * @param mode \a nfc_mode. + * @param nm \a nfc_modulation. + * + */ +static int +nfc_device_validate_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation *nm) +{ + int res; + const nfc_modulation_type *nmt; + if ((res = nfc_device_get_supported_modulation(pnd, mode, &nmt)) < 0) { + return res; + } + for (int i = 0; nmt[i]; i++) { + if (nmt[i] == nm->nmt) { + const nfc_baud_rate *nbr; + if ((res = nfc_device_get_supported_baud_rate(pnd, mode, nmt[i], &nbr)) < 0) { + return res; + } + for (int j = 0; nbr[j]; j++) { + if (nbr[j] == nm->nbr) + return NFC_SUCCESS; + } + return NFC_EINVARG; + } + } + return NFC_EINVARG; +} + /* Misc. functions */ /** @ingroup misc