nfc-list.c 8.69 KB
Newer Older
1
/*-
2
 * Free/Libre Near Field Communication (NFC) library
3
 *
4 5 6 7 8 9
 * Libnfc historical contributors:
 * Copyright (C) 2009      Roel Verdult
 * Copyright (C) 2009-2013 Romuald Conty
 * Copyright (C) 2010-2012 Romain Tartière
 * Copyright (C) 2010-2013 Philippe Teuwen
 * 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 14 15
 * 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,
16
 *  this list of conditions and the following disclaimer.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *  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.
32
 *
33
 * Note that this license only applies on the examples, NFC library itself is under LGPL
34
 *
35 36 37
 */

/**
38
 * @file nfc-list.c
Philippe Teuwen's avatar
Philippe Teuwen committed
39
 * @brief Lists the first target present of each founded device
40
 */
41

42
#ifdef HAVE_CONFIG_H
43
#  include "config.h"
44 45
#endif // HAVE_CONFIG_H

46
#include <err.h>
47 48 49 50 51
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

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

Ludovic Rousseau's avatar
Ludovic Rousseau committed
61
static void
62
print_usage(const char *progname)
63
{
64
  printf("usage: %s [-v] [-t X]\n", progname);
65
  printf("  -v\t verbose display\n");
66 67 68 69 70 71 72 73 74 75 76
  printf("  -t X\t poll only for types according to bitfield X:\n");
  printf("\t   1: ISO14443A\n");
  printf("\t   2: Felica (212 kbps)\n");
  printf("\t   4: Felica (424 kbps)\n");
  printf("\t   8: ISO14443B\n");
  printf("\t  16: ISO14443B'\n");
  printf("\t  32: ISO14443B-2 ST SRx\n");
  printf("\t  64: ISO14443B-2 ASK CTx\n");
  printf("\t 128: Jewel\n");
  printf("\tSo 255 (default) polls for all types.\n");
  printf("\tNote that if 16, 32 or 64 then 8 is selected too.\n");
77
}
78

79
int
80
main(int argc, const char *argv[])
81
{
82
  (void) argc;
83 84
  const char *acLibnfcVersion;
  size_t  i;
85
  bool verbose = false;
86
  int res = 0;
87 88
  int mask = 0xff;
  int arg;
89

90 91
  nfc_context *context;
  nfc_init(&context);
92 93 94 95
  if (context == NULL) {
    ERR("Unable to init libnfc (malloc)");
    exit(EXIT_FAILURE);
  }
96

97
  // Display libnfc version
98 99
  acLibnfcVersion = nfc_version();
  printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion);
100 101 102 103 104 105 106

  // Get commandline options
  for (arg = 1; arg < argc; arg++) {
    if (0 == strcmp(argv[arg], "-h")) {
      print_usage(argv[0]);
      exit(EXIT_SUCCESS);
    } else if (0 == strcmp(argv[arg], "-v")) {
107
      verbose = true;
108 109 110 111 112 113 114 115 116 117 118
    } else if ((0 == strcmp(argv[arg], "-t")) && (arg + 1 < argc)) {
      arg++;
      mask = atoi(argv[arg]);
      if ((mask < 1) || (mask > 255)) {
        ERR("%i is invalid value for type bitfield.", mask);
        print_usage(argv[0]);
        exit(EXIT_FAILURE);
      }
      // Force TypeB for all derivatives of B
      if (mask & 0x70)
        mask |= 0x08;
119
    } else {
120
      ERR("%s is not supported option.", argv[arg]);
121 122
      print_usage(argv[0]);
      exit(EXIT_FAILURE);
123 124
    }
  }
Romuald Conty's avatar
Romuald Conty committed
125

Romuald Conty's avatar
Romuald Conty committed
126 127
  /* Lazy way to open an NFC device */
#if 0
128
  pnd = nfc_open(context, NULL);
Romuald Conty's avatar
Romuald Conty committed
129
#endif
130

131 132
  /* Use connection string if specific device is wanted,
   * i.e. PN532 UART device on /dev/ttyUSB1 */
Romuald Conty's avatar
Romuald Conty committed
133
#if 0
134
  pnd = nfc_open(context, "pn532_uart:/dev/ttyUSB1");
135
#endif
136

137
  nfc_connstring connstrings[MAX_DEVICE_COUNT];
138
  size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
139

140
  if (szDeviceFound == 0) {
141
    printf("No NFC device found.\n");
142 143
  }

144
  for (i = 0; i < szDeviceFound; i++) {
145
    nfc_target ant[MAX_TARGET_COUNT];
146
    pnd = nfc_open(context, connstrings[i]);
147

148
    if (pnd == NULL) {
149
      ERR("Unable to open NFC device: %s", connstrings[i]);
150
      continue;
151
    }
152 153
    if (nfc_initiator_init(pnd) < 0) {
      nfc_perror(pnd, "nfc_initiator_init");
154
      nfc_exit(context);
155
      exit(EXIT_FAILURE);
156
    }
157

158
    printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
159

160
    nfc_modulation nm;
161

162 163 164 165 166 167 168 169 170 171 172 173 174
    if (mask & 0x1) {
      nm.nmt = NMT_ISO14443A;
      nm.nbr = NBR_106;
      // List ISO14443A targets
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
175
      }
176
    }
177

178 179 180 181 182 183 184 185 186 187 188 189 190
    if (mask & 0x02) {
      nm.nmt = NMT_FELICA;
      nm.nbr = NBR_212;
      // List Felica tags
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
191
      }
192
    }
193

194 195 196 197 198 199 200 201 202 203 204 205
    if (mask & 0x04) {
      nm.nmt = NMT_FELICA;
      nm.nbr = NBR_424;
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
206
      }
207
    }
208

209 210 211 212 213 214 215 216 217 218 219 220 221
    if (mask & 0x08) {
      nm.nmt = NMT_ISO14443B;
      nm.nbr = NBR_106;
      // List ISO14443B targets
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
222
      }
223
    }
224

225 226 227 228 229 230 231 232 233 234 235 236 237
    if (mask & 0x10) {
      nm.nmt = NMT_ISO14443BI;
      nm.nbr = NBR_106;
      // List ISO14443B' targets
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
238 239 240
      }
    }

241 242 243 244 245 246 247 248 249 250 251 252 253
    if (mask & 0x20) {
      nm.nmt = NMT_ISO14443B2SR;
      nm.nbr = NBR_106;
      // List ISO14443B-2 ST SRx family targets
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
254 255 256
      }
    }

257 258 259 260 261 262 263 264 265 266 267 268 269
    if (mask & 0x40) {
      nm.nmt = NMT_ISO14443B2CT;
      nm.nbr = NBR_106;
      // List ISO14443B-2 ASK CTx family targets
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
270 271 272
      }
    }

273 274 275 276 277 278 279 280 281 282 283 284 285
    if (mask & 0x80) {
      nm.nmt = NMT_JEWEL;
      nm.nbr = NBR_106;
      // List Jewel targets
      if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
        int n;
        if (verbose || (res > 0)) {
          printf("%d Jewel passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":");
        }
        for (n = 0; n < res; n++) {
          print_nfc_target(&ant[n], verbose);
          printf("\n");
        }
286 287
      }
    }
288
    nfc_close(pnd);
289
  }
290

291
  nfc_exit(context);
292
  exit(EXIT_SUCCESS);
293
}