diff --git a/examples/nfc-list.c b/examples/nfc-list.c index f8d9d56..e591056 100644 --- a/examples/nfc-list.c +++ b/examples/nfc-list.c @@ -53,11 +53,11 @@ int main(int argc, const char* argv[]) size_t szFound; size_t i; nfc_target_info_t nti; - nfc_device_desc_t *pnddDevices; + nfc_device_desc_t *pnddDevices = nfc_parse_device_desc(argc, argv, &szFound); const char* acLibnfcVersion; - if (argc > 1) { - errx (1, "usage: %s", argv[0]); + if (argc > 1 && szFound == 0) { + errx (1, "usage: %s [--device driver:port:speed]", argv[0]); } // Display libnfc version @@ -85,13 +85,16 @@ int main(int argc, const char* argv[]) pnd = nfc_connect(&ndd); #endif - if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) + if (szFound == 0) { - fprintf (stderr, "malloc() failed\n"); - return EXIT_FAILURE; - } + if (!(pnddDevices = malloc (MAX_DEVICE_COUNT * sizeof (*pnddDevices)))) + { + fprintf (stderr, "malloc() failed\n"); + return EXIT_FAILURE; + } - nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); + nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szFound); + } if (szFound == 0) { diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 3ac66be..3fdcc87 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -85,6 +85,7 @@ NFC_EXPORT const char* nfc_device_name(nfc_device_t* pnd); NFC_EXPORT void iso14443a_crc(byte_t* pbtData, size_t szLen, byte_t* pbtCrc); NFC_EXPORT void append_iso14443a_crc(byte_t* pbtData, size_t szLen); NFC_EXPORT const char* nfc_version(void); +NFC_EXPORT nfc_device_desc_t* nfc_parse_device_desc(int argc, const char *argv[], size_t* szFound); #ifdef __cplusplus } diff --git a/libnfc/nfc.c b/libnfc/nfc.c index aaa10c2..bbe4888 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -1026,10 +1026,51 @@ const char* nfc_device_name(nfc_device_t* pnd) */ const char* nfc_version(void) { - #ifdef SVN_REVISION - return PACKAGE_VERSION" (r"SVN_REVISION")"; - #else - return PACKAGE_VERSION; - #endif // SVN_REVISION + #ifdef SVN_REVISION + return PACKAGE_VERSION" (r"SVN_REVISION")"; + #else + return PACKAGE_VERSION; + #endif // SVN_REVISION } +/** + * @brief Tries to parse arguments to find device descriptions. + * @return Returns the list of found device descriptions. + */ +nfc_device_desc_t* nfc_parse_device_desc(int argc, const char *argv[], size_t* szFound) +{ + nfc_device_desc_t* pndd = 0; + *szFound = 0; + int arg; + + // Get commandline options + for (arg=1;arg arg+1) { + + pndd = malloc(sizeof(nfc_device_desc_t)); + + char buffer[256]; + strncpy(buffer, argv[++arg], 256); + + // Driver. + pndd->pcDriver = (char *)malloc(256); + strcpy(pndd->pcDriver, strtok(buffer, ":")); + + // Port. + pndd->pcPort = (char *)malloc(256); + strcpy(pndd->pcPort, strtok(NULL, ":")); + + // Speed. + sscanf(strtok(NULL, ":"), "%d", &pndd->uiSpeed); + + *szFound = 1; + } + break; + } + } + + return pndd; +}