2012-01-18 14:17:01 +01:00
|
|
|
/*-
|
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-10-21 16:09:16 +02:00
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2010 Romain Tartière
|
|
|
|
* Copyright (C) 2010, 2011, 2012 Romuald Conty
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-01-18 14:17:01 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* 1) Redistributions of source code must retain the above copyright notice,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2012-01-18 14:17:01 +01:00
|
|
|
* 2 )Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-01-18 14:17:01 +01:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-10-21 16:11:17 +02:00
|
|
|
* @file nfc-scan-device.c
|
2012-01-18 14:17:01 +01:00
|
|
|
* @brief Lists each available NFC device
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBUSB
|
|
|
|
# ifdef DEBUG
|
|
|
|
# include <usb.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nfc/nfc.h>
|
|
|
|
|
|
|
|
#include "nfc-utils.h"
|
|
|
|
|
|
|
|
#define MAX_DEVICE_COUNT 16
|
|
|
|
#define MAX_TARGET_COUNT 16
|
|
|
|
|
|
|
|
static nfc_device *pnd;
|
|
|
|
|
2012-05-13 14:55:26 +02:00
|
|
|
static void
|
2012-10-21 16:12:04 +02:00
|
|
|
print_usage(const char *argv[])
|
2012-01-18 14:17:01 +01:00
|
|
|
{
|
2012-10-21 16:12:04 +02:00
|
|
|
printf("Usage: %s [OPTIONS]\n", argv[0]);
|
|
|
|
printf("Options:\n");
|
|
|
|
printf("\t-h\tPrint this help message.\n");
|
|
|
|
printf("\t-v\tSet verbose display.\n");
|
|
|
|
printf("\t-i\tAllow intrusive scan.\n");
|
2012-01-18 14:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, const char *argv[])
|
2012-01-18 14:17:01 +01:00
|
|
|
{
|
|
|
|
const char *acLibnfcVersion;
|
|
|
|
size_t i;
|
|
|
|
bool verbose = false;
|
|
|
|
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_context *context;
|
2012-10-21 16:12:04 +02:00
|
|
|
|
|
|
|
// Get commandline options
|
|
|
|
for (int arg = 1; arg < argc; arg++) {
|
|
|
|
if (0 == strcmp(argv[arg], "-h")) {
|
|
|
|
print_usage(argv);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
} else if (0 == strcmp(argv[arg], "-v")) {
|
2012-01-18 14:17:01 +01:00
|
|
|
verbose = true;
|
2012-10-21 16:12:04 +02:00
|
|
|
} else if (0 == strcmp(argv[arg], "-i")) {
|
2013-01-29 13:52:53 +01:00
|
|
|
// This has to be done before the call to nfc_init()
|
2012-10-21 16:12:04 +02:00
|
|
|
setenv("LIBNFC_INTRUSIVE_SCAN", "yes", 1);
|
2012-01-18 14:17:01 +01:00
|
|
|
} else {
|
2012-10-21 16:12:04 +02:00
|
|
|
ERR("%s is not supported option.", argv[arg]);
|
|
|
|
print_usage(argv);
|
|
|
|
return EXIT_FAILURE;
|
2012-01-18 14:17:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 13:52:53 +01:00
|
|
|
nfc_init(&context);
|
|
|
|
|
|
|
|
// Display libnfc version
|
|
|
|
acLibnfcVersion = nfc_version();
|
|
|
|
printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
|
|
|
|
|
2012-01-18 14:17:01 +01:00
|
|
|
#ifdef HAVE_LIBUSB
|
|
|
|
# ifdef DEBUG
|
2012-05-29 17:54:36 +02:00
|
|
|
usb_set_debug(4);
|
2012-01-18 14:17:01 +01:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
2012-12-04 19:29:57 +01:00
|
|
|
size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
|
2012-01-18 14:17:01 +01:00
|
|
|
|
2012-01-26 22:36:08 +01:00
|
|
|
int res = EXIT_FAILURE;
|
2012-01-18 14:17:01 +01:00
|
|
|
if (szDeviceFound == 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("No NFC device found.\n");
|
2012-01-26 22:36:08 +01:00
|
|
|
goto bye;
|
2012-01-18 14:17:01 +01:00
|
|
|
}
|
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%d NFC device(s) found:\n", (int)szDeviceFound);
|
2012-09-17 15:47:30 +02:00
|
|
|
char *strinfo = NULL;
|
2012-01-18 14:17:01 +01:00
|
|
|
for (i = 0; i < szDeviceFound; i++) {
|
2012-12-04 19:29:57 +01:00
|
|
|
pnd = nfc_open(context, connstrings[i]);
|
2012-01-18 14:17:01 +01:00
|
|
|
if (pnd != NULL) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("- %s:\n %s\n", nfc_device_get_name(pnd), nfc_device_get_connstring(pnd));
|
2012-05-17 02:48:47 +02:00
|
|
|
if (verbose) {
|
2012-09-17 15:47:30 +02:00
|
|
|
if (nfc_device_get_information_about(pnd, &strinfo) >= 0) {
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("%s", strinfo);
|
2012-09-17 15:47:30 +02:00
|
|
|
free(strinfo);
|
2012-05-17 02:48:47 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_close(pnd);
|
2012-05-22 11:48:43 +02:00
|
|
|
} else {
|
2012-05-29 17:52:51 +02:00
|
|
|
printf("nfc_open failed for %s\n", connstrings[i]);
|
|
|
|
}
|
2012-01-18 14:17:01 +01:00
|
|
|
}
|
2012-01-26 22:36:08 +01:00
|
|
|
res = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
bye:
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2012-01-26 22:36:08 +01:00
|
|
|
return res;
|
2012-01-18 14:17:01 +01:00
|
|
|
}
|