Home | History | Annotate | Download | only in c++
      1 /*
      2  * Author: Jon Trulson <jtrulson (at) 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 <iostream>
     27 #include "pn532.h"
     28 
     29 using namespace std;
     30 
     31 // the URL we want to add as an NDEF record
     32 // NOTE: this cannot exceed 34 characters.
     33 static char url[] = "iotdk.intel.com";
     34 
     35 
     36 int main(int argc, char **argv)
     37 {
     38 //! [Interesting]
     39   // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
     40   // IRQ, and gpio 2 for the reset pin.
     41 
     42   upm::PN532 *nfc = new upm::PN532(3, 2);
     43 
     44   if (!nfc->init())
     45     cerr << "init() failed" << endl;
     46 
     47   uint32_t vers = nfc->getFirmwareVersion();
     48 
     49   if (vers)
     50     printf("Got firmware version: 0x%08x\n", vers);
     51   else
     52     {
     53       printf("Could not identify PN532\n");
     54       return 1;
     55     }
     56 
     57   // Now scan and identify any cards that come in range (1 for now)
     58 
     59   // Retry forever
     60   nfc->setPassiveActivationRetries(0xff);
     61 
     62   nfc->SAMConfig();
     63 
     64   uint8_t uidSize;
     65   uint8_t uid[7];
     66 
     67   bool foundCard = false;
     68   while (!foundCard)
     69     {
     70       memset(uid, 0, 7);
     71       if (nfc->readPassiveTargetID(nfc->BAUD_MIFARE_ISO14443A,
     72                                    uid, &uidSize, 2000))
     73         {
     74           // found a card
     75           printf("Found a card: UID len %d\n", uidSize);
     76           printf("UID: ");
     77           for (int i = 0; i < uidSize; i++)
     78             printf("%02x ", uid[i]);
     79           printf("\n");
     80           printf("SAK: 0x%02x\n", nfc->getSAK());
     81           printf("ATQA: 0x%04x\n\n", nfc->getATQA());
     82           foundCard = true;
     83         }
     84       else
     85         {
     86           printf("Waiting for a card...\n");
     87         }
     88     }
     89 
     90   if (uidSize != 7)
     91     {
     92       printf("This example will only write an NDEF URI to preformatted\n");
     93       printf("Mifare Ultralight or NTAG2XX tags\n");
     94 
     95       return 1;
     96     }
     97 
     98   // 48 bytes is maximum data area on ultralight cards, so we use that
     99   // as the maximum datasize here.  Obviously if you have a bigger
    100   // card, you can write more data.
    101   if (!nfc->ntag2xx_WriteNDEFURI(nfc->NDEF_URIPREFIX_HTTP, url, 48))
    102     {
    103       // failure
    104       printf("Failed to write NDEF record tag.\n");
    105       return 1;
    106     }
    107 
    108   printf("Success, URL record written to tag.\n");
    109 
    110 
    111 //! [Interesting]
    112 
    113   delete nfc;
    114   return 0;
    115 }
    116