libnfc  1.7.1
nfc-scan-device.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions are met:
15  * 1) Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2 )Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Note that this license only applies on the examples, NFC library itself is under LGPL
34  *
35  */
36 
42 #ifdef HAVE_CONFIG_H
43 # include "config.h"
44 #endif // HAVE_CONFIG_H
45 
46 #include <err.h>
47 #include <stdio.h>
48 #include <stddef.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include <nfc/nfc.h>
53 
54 #include "nfc-utils.h"
55 
56 #define MAX_DEVICE_COUNT 16
57 #define MAX_TARGET_COUNT 16
58 
59 static nfc_device *pnd;
60 
61 static void
62 print_usage(const char *argv[])
63 {
64  printf("Usage: %s [OPTIONS]\n", argv[0]);
65  printf("Options:\n");
66  printf("\t-h\tPrint this help message.\n");
67  printf("\t-v\tSet verbose display.\n");
68  printf("\t-i\tAllow intrusive scan.\n");
69 }
70 
71 int
72 main(int argc, const char *argv[])
73 {
74  const char *acLibnfcVersion;
75  size_t i;
76  bool verbose = false;
77 
78  nfc_context *context;
79 
80  // Get commandline options
81  for (int arg = 1; arg < argc; arg++) {
82  if (0 == strcmp(argv[arg], "-h")) {
83  print_usage(argv);
84  exit(EXIT_SUCCESS);
85  } else if (0 == strcmp(argv[arg], "-v")) {
86  verbose = true;
87  } else if (0 == strcmp(argv[arg], "-i")) {
88  // This has to be done before the call to nfc_init()
89  setenv("LIBNFC_INTRUSIVE_SCAN", "yes", 1);
90  } else {
91  ERR("%s is not supported option.", argv[arg]);
92  print_usage(argv);
93  exit(EXIT_FAILURE);
94  }
95  }
96 
97  nfc_init(&context);
98  if (context == NULL) {
99  ERR("Unable to init libnfc (malloc)\n");
100  exit(EXIT_FAILURE);
101  }
102 
103  // Display libnfc version
104  acLibnfcVersion = nfc_version();
105  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
106 
107  nfc_connstring connstrings[MAX_DEVICE_COUNT];
108  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
109 
110  if (szDeviceFound == 0) {
111  printf("No NFC device found.\n");
112  nfc_exit(context);
113  exit(EXIT_FAILURE);
114  }
115 
116  printf("%d NFC device(s) found:\n", (int)szDeviceFound);
117  char *strinfo = NULL;
118  for (i = 0; i < szDeviceFound; i++) {
119  pnd = nfc_open(context, connstrings[i]);
120  if (pnd != NULL) {
121  printf("- %s:\n %s\n", nfc_device_get_name(pnd), nfc_device_get_connstring(pnd));
122  if (verbose) {
123  if (nfc_device_get_information_about(pnd, &strinfo) >= 0) {
124  printf("%s", strinfo);
125  nfc_free(strinfo);
126  }
127  }
128  nfc_close(pnd);
129  } else {
130  printf("nfc_open failed for %s\n", connstrings[i]);
131  }
132  }
133  nfc_exit(context);
134  exit(EXIT_SUCCESS);
135 }
nfc_init
void nfc_init(nfc_context **context)
Initialize libnfc. This function must be called before calling any other libnfc function.
Definition: nfc.c:192
nfc_context
NFC library context Struct which contains internal options, references, pointers, etc....
Definition: nfc-internal.h:175
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_version
const char * nfc_version(void)
Returns the library version.
Definition: nfc.c:1218
nfc_exit
void nfc_exit(nfc_context *context)
Deinitialize libnfc. Should be called after closing all open devices and before your application term...
Definition: nfc.c:209
nfc_device_get_information_about
int nfc_device_get_information_about(nfc_device *pnd, char **buf)
Print information about NFC device.
Definition: nfc.c:1247
nfc_connstring
char nfc_connstring[NFC_BUFSIZE_CONNSTRING]
Definition: nfc-types.h:62
nfc_device_get_connstring
const char * nfc_device_get_connstring(nfc_device *pnd)
Returns the device connection string.
Definition: nfc.c:1176
nfc_device_get_name
const char * nfc_device_get_name(nfc_device *pnd)
Returns the device name.
Definition: nfc.c:1164
nfc_open
nfc_device * nfc_open(nfc_context *context, const nfc_connstring connstring)
Open a NFC device.
Definition: nfc.c:238
nfc_list_devices
size_t nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
Scan for discoverable supported devices (ie. only available for some drivers)
Definition: nfc.c:317
ERR
#define ERR(...)
Print a error message.
Definition: nfc-utils.h:85
nfc_close
void nfc_close(nfc_device *pnd)
Close from a NFC device.
Definition: nfc.c:300
nfc-utils.h
Provide some examples shared functions like print, parity calculation, options parsing.
nfc.h
libnfc interface
nfc_free
void nfc_free(void *p)
Free buffer allocated by libnfc.
Definition: nfc.c:1233