libnfc  1.7.0-rc7
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  * Additional contributors of this file:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  * 1) Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  * 2 )Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Note that this license only applies on the examples, NFC library itself is under LGPL
33  *
34  */
35 
41 #ifdef HAVE_CONFIG_H
42 # include "config.h"
43 #endif // HAVE_CONFIG_H
44 
45 #include <err.h>
46 #include <stdio.h>
47 #include <stddef.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 #include <nfc/nfc.h>
52 
53 #include "nfc-utils.h"
54 
55 #define MAX_DEVICE_COUNT 16
56 #define MAX_TARGET_COUNT 16
57 
58 static nfc_device *pnd;
59 
60 static void
61 print_usage(const char *argv[])
62 {
63  printf("Usage: %s [OPTIONS]\n", argv[0]);
64  printf("Options:\n");
65  printf("\t-h\tPrint this help message.\n");
66  printf("\t-v\tSet verbose display.\n");
67  printf("\t-i\tAllow intrusive scan.\n");
68 }
69 
70 int
71 main(int argc, const char *argv[])
72 {
73  const char *acLibnfcVersion;
74  size_t i;
75  bool verbose = false;
76 
77  nfc_context *context;
78 
79  // Get commandline options
80  for (int arg = 1; arg < argc; arg++) {
81  if (0 == strcmp(argv[arg], "-h")) {
82  print_usage(argv);
83  exit(EXIT_SUCCESS);
84  } else if (0 == strcmp(argv[arg], "-v")) {
85  verbose = true;
86  } else if (0 == strcmp(argv[arg], "-i")) {
87  // This has to be done before the call to nfc_init()
88  setenv("LIBNFC_INTRUSIVE_SCAN", "yes", 1);
89  } else {
90  ERR("%s is not supported option.", argv[arg]);
91  print_usage(argv);
92  exit(EXIT_FAILURE);
93  }
94  }
95 
96  nfc_init(&context);
97  if (context == NULL) {
98  ERR("Unable to init libnfc (malloc)\n");
99  exit(EXIT_FAILURE);
100  }
101 
102  // Display libnfc version
103  acLibnfcVersion = nfc_version();
104  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
105 
106  nfc_connstring connstrings[MAX_DEVICE_COUNT];
107  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
108 
109  if (szDeviceFound == 0) {
110  printf("No NFC device found.\n");
111  nfc_exit(context);
112  exit(EXIT_FAILURE);
113  }
114 
115  printf("%d NFC device(s) found:\n", (int)szDeviceFound);
116  char *strinfo = NULL;
117  for (i = 0; i < szDeviceFound; i++) {
118  pnd = nfc_open(context, connstrings[i]);
119  if (pnd != NULL) {
120  printf("- %s:\n %s\n", nfc_device_get_name(pnd), nfc_device_get_connstring(pnd));
121  if (verbose) {
122  if (nfc_device_get_information_about(pnd, &strinfo) >= 0) {
123  printf("%s", strinfo);
124  nfc_free(strinfo);
125  }
126  }
127  nfc_close(pnd);
128  } else {
129  printf("nfc_open failed for %s\n", connstrings[i]);
130  }
131  }
132  nfc_exit(context);
133  exit(EXIT_SUCCESS);
134 }