• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <unistd.h>
26 #include <signal.h>
27 #include <iostream>
28 #include "pn532.h"
29 
30 using namespace std;
31 
32 bool shouldRun = true;
33 
sig_handler(int signo)34 void sig_handler(int signo)
35 {
36   if (signo == SIGINT)
37     shouldRun = false;
38 }
39 
40 
main(int argc,char ** argv)41 int main(int argc, char **argv)
42 {
43   signal(SIGINT, sig_handler);
44 
45 //! [Interesting]
46   // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
47   // IRQ, and gpio 2 for the reset pin.
48 
49   upm::PN532 *nfc = new upm::PN532(3, 2);
50 
51   if (!nfc->init())
52     cerr << "init() failed" << endl;
53 
54   uint32_t vers = nfc->getFirmwareVersion();
55 
56   if (vers)
57     printf("Got firmware version: 0x%08x\n", vers);
58   else
59     {
60       printf("Could not identify PN532\n");
61       return 1;
62     }
63 
64   // Now scan and identify any cards that come in range (1 for now)
65 
66   // Retry forever
67   nfc->setPassiveActivationRetries(0xff);
68 
69   nfc->SAMConfig();
70 
71   uint8_t uidSize;
72   uint8_t uid[7];
73 
74   while (shouldRun)
75     {
76       memset(uid, 0, 7);
77       if (nfc->readPassiveTargetID(nfc->BAUD_MIFARE_ISO14443A,
78                                    uid, &uidSize, 2000))
79         {
80           // found a card
81           printf("Found a card: UID len %d\n", uidSize);
82           printf("UID: ");
83           for (int i = 0; i < uidSize; i++)
84             printf("%02x ", uid[i]);
85           printf("\n");
86           printf("SAK: 0x%02x\n", nfc->getSAK());
87           printf("ATQA: 0x%04x\n\n", nfc->getATQA());
88           sleep(1);
89         }
90       else
91         {
92           printf("Waiting for a card...\n");
93         }
94     }
95 
96 
97 //! [Interesting]
98 
99   delete nfc;
100   return 0;
101 }
102