nfc_list_devices() function returns now the number of devices found.

This commit is contained in:
Audrey Diacre 2012-01-10 10:35:36 +00:00
parent 00818e048c
commit 1ec504e163
12 changed files with 23 additions and 34 deletions

View file

@ -63,11 +63,10 @@ main (int argc, const char *argv[])
{ {
uint8_t abtRx[MAX_FRAME_LEN]; uint8_t abtRx[MAX_FRAME_LEN];
int szRx; int szRx;
size_t szDeviceFound;
uint8_t abtTx[] = "Hello Mars!"; uint8_t abtTx[] = "Hello Mars!";
#define MAX_DEVICE_COUNT 2 #define MAX_DEVICE_COUNT 2
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
// Little hack to allow using nfc-dep-initiator & nfc-dep-target from // Little hack to allow using nfc-dep-initiator & nfc-dep-target from
// the same machine: if there is more than one readers connected // the same machine: if there is more than one readers connected
// nfc-dep-target will connect to the second reader // nfc-dep-target will connect to the second reader

View file

@ -82,7 +82,6 @@ main (int argc, char *argv[])
{ {
int arg; int arg;
bool quiet_output = false; bool quiet_output = false;
size_t szFound;
const char *acLibnfcVersion = nfc_version (); const char *acLibnfcVersion = nfc_version ();
// Get commandline options // Get commandline options
@ -110,7 +109,7 @@ main (int argc, char *argv[])
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
// List available devices // List available devices
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (szFound < 2) { if (szFound < 2) {
ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound); ERR ("%zd device found but two connected devices are needed to relay NFC.", szFound);

View file

@ -52,7 +52,6 @@
int int
main (int argc, const char *argv[]) main (int argc, const char *argv[])
{ {
size_t szFound;
size_t i; size_t i;
nfc_device *pnd; nfc_device *pnd;
const char *acLibnfcVersion; const char *acLibnfcVersion;
@ -73,7 +72,7 @@ main (int argc, const char *argv[])
printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion); printf ("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (szFound == 0) { if (szFound == 0) {
printf ("No NFC device found.\n"); printf ("No NFC device found.\n");

View file

@ -67,7 +67,7 @@ extern "C" {
NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring); NFC_EXPORT nfc_device *nfc_connect (const nfc_connstring connstring);
NFC_EXPORT void nfc_disconnect (nfc_device *pnd); NFC_EXPORT void nfc_disconnect (nfc_device *pnd);
NFC_EXPORT int nfc_abort_command (nfc_device *pnd); NFC_EXPORT int nfc_abort_command (nfc_device *pnd);
NFC_EXPORT void nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound); NFC_EXPORT size_t nfc_list_devices (nfc_connstring connstrings[], size_t connstrings_len);
NFC_EXPORT int nfc_idle (nfc_device *pnd); NFC_EXPORT int nfc_idle (nfc_device *pnd);
/* NFC initiator: act as "reader" */ /* NFC initiator: act as "reader" */

View file

@ -81,9 +81,8 @@ nfc_get_default_device (nfc_connstring *connstring)
char *env_default_connstring = getenv ("LIBNFC_DEFAULT_DEVICE"); char *env_default_connstring = getenv ("LIBNFC_DEFAULT_DEVICE");
if (NULL == env_default_connstring) { if (NULL == env_default_connstring) {
// LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device // LIBNFC_DEFAULT_DEVICE is not set, we fallback on probing for the first available device
size_t szDeviceFound;
nfc_connstring listed_cs[1]; nfc_connstring listed_cs[1];
nfc_list_devices (listed_cs, 1, &szDeviceFound); size_t szDeviceFound = nfc_list_devices (listed_cs, 1);
if (szDeviceFound) { if (szDeviceFound) {
strncpy (*connstring, listed_cs[0], sizeof(nfc_connstring)); strncpy (*connstring, listed_cs[0], sizeof(nfc_connstring));
} else { } else {
@ -177,30 +176,32 @@ nfc_disconnect (nfc_device *pnd)
/** /**
* @brief Probe for discoverable supported devices (ie. only available for some drivers) * @brief Probe for discoverable supported devices (ie. only available for some drivers)
* @return Returns the number of devices found.
* @param[out] pnddDevices array of \a nfc_device_desc_t previously allocated by the caller. * @param[out] pnddDevices array of \a nfc_device_desc_t previously allocated by the caller.
* @param szDevices size of the \a pnddDevices array. * @param szDevices size of the \a pnddDevices array.
* @param[out] pszDeviceFound number of devices found. *
*/ */
void size_t
nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices, size_t *pszDeviceFound) nfc_list_devices (nfc_connstring connstrings[] , size_t szDevices)
{ {
size_t szN; size_t szN;
*pszDeviceFound = 0; size_t szDeviceFound = 0;
const struct nfc_driver_t *ndr; const struct nfc_driver_t *ndr;
const struct nfc_driver_t **pndr = nfc_drivers; const struct nfc_driver_t **pndr = nfc_drivers;
log_init (); log_init ();
while ((ndr = *pndr)) { while ((ndr = *pndr)) {
szN = 0; szN = 0;
if (ndr->probe (connstrings + (*pszDeviceFound), szDevices - (*pszDeviceFound), &szN)) { if (ndr->probe (connstrings + (szDeviceFound), szDevices - (szDeviceFound), &szN)) {
*pszDeviceFound += szN; szDeviceFound += szN;
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) szN, ndr->name); log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%ld device(s) found using %s driver", (unsigned long) szN, ndr->name);
if (*pszDeviceFound == szDevices) if (szDeviceFound == szDevices)
break; break;
} }
pndr++; pndr++;
} }
log_fini (); log_fini ();
return szDeviceFound;
} }
/** /**

View file

@ -15,17 +15,16 @@ test_access_storm (void)
{ {
int n = NTESTS; int n = NTESTS;
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t device_count, ref_device_count;
int res = 0; int res = 0;
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &ref_device_count); size_t ref_device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (!ref_device_count) if (!ref_device_count)
cut_omit ("No NFC device found"); cut_omit ("No NFC device found");
while (n) { while (n) {
size_t i; size_t i;
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
cut_assert_equal_int (ref_device_count, device_count, cut_message ("device count")); cut_assert_equal_int (ref_device_count, device_count, cut_message ("device count"));
for (i = 0; i < device_count; i++) { for (i = 0; i < device_count; i++) {

View file

@ -27,9 +27,7 @@ abort_test_by_keypress (int sig)
void void
cut_setup (void) cut_setup (void)
{ {
size_t n; size_t n = nfc_list_devices (connstrings, 2);
nfc_list_devices (connstrings, 2, &n);
if (n < 2) { if (n < 2) {
cut_omit ("At least two NFC devices must be plugged-in to run this test"); cut_omit ("At least two NFC devices must be plugged-in to run this test");
} }

View file

@ -26,9 +26,7 @@ abort_test_by_keypress (int sig)
void void
cut_setup (void) cut_setup (void)
{ {
size_t n; size_t n = nfc_list_devices (connstrings, 2);
nfc_list_devices (connstrings, 2, &n);
if (n < 2) { if (n < 2) {
cut_omit ("At least two NFC devices must be plugged-in to run this test"); cut_omit ("At least two NFC devices must be plugged-in to run this test");
} }

View file

@ -10,10 +10,9 @@ void
test_register_endianness (void) test_register_endianness (void)
{ {
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t device_count;
int res = 0; int res = 0;
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (!device_count) if (!device_count)
cut_omit ("No NFC device found"); cut_omit ("No NFC device found");

View file

@ -11,10 +11,9 @@ void
test_register_endianness (void) test_register_endianness (void)
{ {
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
size_t device_count;
int res = 0; int res = 0;
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &device_count); size_t device_count = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (!device_count) if (!device_count)
cut_omit ("No NFC device found"); cut_omit ("No NFC device found");

View file

@ -114,9 +114,8 @@ main (int argc, const char *argv[])
strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW"); strcpy(ndd.acDevice, "SCM Micro / SCL3711-NFC&RW");
pnd = nfc_connect (&ndd); pnd = nfc_connect (&ndd);
#endif #endif
size_t szDeviceFound;
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound); size_t szDeviceFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (szDeviceFound == 0) { if (szDeviceFound == 0) {
printf ("No NFC device found.\n"); printf ("No NFC device found.\n");

View file

@ -149,7 +149,6 @@ int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
int arg; int arg;
size_t szFound;
const char *acLibnfcVersion = nfc_version (); const char *acLibnfcVersion = nfc_version ();
nfc_target ntRealTarget; nfc_target ntRealTarget;
@ -193,7 +192,7 @@ main (int argc, char *argv[])
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
// List available devices // List available devices
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szFound); size_t szFound = nfc_list_devices (connstrings, MAX_DEVICE_COUNT);
if (initiator_only_mode || target_only_mode) { if (initiator_only_mode || target_only_mode) {
if (szFound < 1) { if (szFound < 1) {