• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*jslint node:true, vars:true, bitwise:true, unparam:true */
2/*jshint unused:true */
3
4/*
5* Author: Jon Trulson <jtrulson@ics.com>
6* Copyright (c) 2015 Intel Corporation.
7*
8* Permission is hereby granted, free of charge, to any person obtaining
9* a copy of this software and associated documentation files (the
10* "Software"), to deal in the Software without restriction, including
11* without limitation the rights to use, copy, modify, merge, publish,
12* distribute, sublicense, and/or sell copies of the Software, and to
13* permit persons to whom the Software is furnished to do so, subject to
14* the following conditions:
15*
16* The above copyright notice and this permission notice shall be
17* included in all copies or substantial portions of the Software.
18*
19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26*/
27
28// Load PN532 module
29var pn532 = require('jsupm_pn532');
30
31// Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the
32// IRQ, and gpio 2 for the reset pin.
33var myNFCObj = new pn532.PN532(3, 2);
34
35function writeUrl()
36{
37    if (uidSize.getitem(0) != 7)
38    {
39        console.log("This example will only write an NDEF URI to preformatted");
40        console.log("Mifare Ultralight or NTAG2XX tags");
41        exit();
42    }
43
44    // 48 bytes is maximum data area on ultralight cards, so we use that
45    // as the maximum datasize here.  Obviously if you have a bigger
46    // card, you can write more data.
47    if (!myNFCObj.ntag2xx_WriteNDEFURI(pn532.PN532.NDEF_URIPREFIX_HTTP,
48                                       url, 48))
49    {
50        // failure
51        console.log("Failed to write NDEF record tag.");
52        exit(1);
53    }
54
55    console.log("Success, URL record written to tag.");
56}
57
58function toHex(d, pad)
59{
60    // pad should be between 1 and 8
61    return  ("00000000"+(Number(d).toString(16))).slice(-pad)
62}
63
64function exit()
65{
66	clearInterval(myInterval);
67	myNFCObj = null;
68	pn532.cleanUp();
69	pn532 = null;
70	console.log("Exiting");
71	process.exit(0);
72}
73
74// When exiting: clear interval, and print message
75process.on('SIGINT', function()
76{
77	exit();
78});
79
80// "main"
81if (!myNFCObj.init())
82	console.log("init() failed");
83
84var vers = myNFCObj.getFirmwareVersion();
85
86if (vers)
87	console.log("Got firmware version: " + toHex(vers, 8));
88else
89{
90	console.log("Could not identify PN532");
91	exit();
92}
93
94// Now scan and identify any cards that come in range (1 for now)
95
96// Retry forever
97myNFCObj.setPassiveActivationRetries(0xff);
98
99myNFCObj.SAMConfig();
100
101var uidSize = new pn532.uint8Array(0);
102var uid = new pn532.uint8Array(7);
103
104// the URL we want to add as an NDEF record
105// NOTE: this cannot exceed 34 characters.
106url = "iotdk.intel.com";
107
108var myInterval = setInterval(function()
109{
110	for (var x = 0; x < 7; x++)
111		uid.setitem(x, 0);
112	if (myNFCObj.readPassiveTargetID(pn532.PN532.BAUD_MIFARE_ISO14443A,
113		                         uid, uidSize, 2000))
114	{
115		// found a card
116		console.log("Found a card: UID len " + uidSize.getitem(0));
117		process.stdout.write("UID: ");
118		for (var i = 0; i < uidSize.getitem(0); i++)
119                {
120                        var byteVal = uid.getitem(i);
121			process.stdout.write(toHex(byteVal, 2) + " ");
122                }
123		process.stdout.write("\n");
124		console.log("SAK: " + toHex(myNFCObj.getSAK(), 2));
125		console.log("ATQA: " + toHex(myNFCObj.getATQA(), 4));
126		console.log(" ");
127
128                // write the URL
129                writeUrl();
130            	clearInterval(myInterval);
131                return;
132	}
133	else
134		console.log("Waiting for a card...");
135}, 1000);
136
137