Няма описание

pn532-writeurl.js 3.8KB

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