1/*jslint node:true, vars:true, bitwise:true, unparam:true */
2/*jshint unused:true */
3
4/*
5* Author: Zion Orent <zorent@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
35if (!myNFCObj.init())
36	console.log("init() failed");
37
38var vers = myNFCObj.getFirmwareVersion();
39
40if (vers)
41	console.log("Got firmware version: " + toHex(vers, 8));
42else
43{
44	console.log("Could not identify PN532");
45	exit();
46}
47
48// Now scan and identify any cards that come in range (1 for now)
49
50// Retry forever
51myNFCObj.setPassiveActivationRetries(0xff);
52
53myNFCObj.SAMConfig();
54
55var uidSize = new pn532.uint8Array(0);
56var uid = new pn532.uint8Array(7);
57
58var myInterval = setInterval(function()
59{
60	for (var x = 0; x < 7; x++)
61		uid.setitem(x, 0);
62	if (myNFCObj.readPassiveTargetID(pn532.PN532.BAUD_MIFARE_ISO14443A,
63		                         uid, uidSize, 2000))
64	{
65		// found a card
66		console.log("Found a card: UID len " + uidSize.getitem(0));
67		process.stdout.write("UID: ");
68		for (var i = 0; i < uidSize.getitem(0); i++)
69                {
70                        var byteVal = uid.getitem(i);
71			process.stdout.write(toHex(byteVal, 2) + " ");
72                }
73		process.stdout.write("\n");
74		console.log("SAK: " + toHex(myNFCObj.getSAK(), 2));
75		console.log("ATQA: " + toHex(myNFCObj.getATQA(), 4));
76		console.log(" ");
77	}
78	else
79		console.log("Waiting for a card...");
80}, 1000);
81
82function toHex(d, pad)
83{
84    // pad should be between 1 and 8
85    return  ("00000000"+(Number(d).toString(16))).slice(-pad)
86}
87
88function exit()
89{
90	clearInterval(myInterval);
91	myNFCObj = null;
92	pn532.cleanUp();
93	pn532 = null;
94	console.log("Exiting");
95	process.exit(0);
96}
97
98// When exiting: clear interval, and print message
99process.on('SIGINT', function()
100{
101	exit();
102});
103