├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [magic890] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### https://raw.github.com/github/gitignore/8ab86f6bb71e85b5046f1d921bbbe5ceec9063ba/Node.gitignore 2 | 3 | # Logs 4 | logs 5 | *.log 6 | 7 | # Runtime data 8 | pids 9 | *.pid 10 | *.seed 11 | 12 | # Directory for instrumented libs generated by jscoverage/JSCover 13 | lib-cov 14 | 15 | # Coverage directory used by tools like istanbul 16 | coverage 17 | 18 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 19 | .grunt 20 | 21 | # node-waf configuration 22 | .lock-wscript 23 | 24 | # Compiled binary addons (http://nodejs.org/api/addons.html) 25 | build/Release 26 | 27 | # Dependency directory 28 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git 29 | node_modules 30 | 31 | 32 | ### https://raw.github.com/github/gitignore/8ab86f6bb71e85b5046f1d921bbbe5ceec9063ba/Global/OSX.gitignore 33 | 34 | .DS_Store 35 | .AppleDouble 36 | .LSOverride 37 | 38 | # Icon must end with two \r 39 | Icon 40 | 41 | # Thumbnails 42 | ._* 43 | 44 | # Files that might appear in the root of a volume 45 | .DocumentRevisions-V100 46 | .fseventsd 47 | .Spotlight-V100 48 | .TemporaryItems 49 | .Trashes 50 | .VolumeIcon.icns 51 | 52 | # Directories potentially created on remote AFP share 53 | .AppleDB 54 | .AppleDesktop 55 | Network Trash Folder 56 | Temporary Items 57 | .apdisk 58 | 59 | 60 | ### https://raw.github.com/github/gitignore/8ab86f6bb71e85b5046f1d921bbbe5ceec9063ba/Global/Windows.gitignore 61 | 62 | # Windows image file caches 63 | Thumbs.db 64 | ehthumbs.db 65 | 66 | # Folder config file 67 | Desktop.ini 68 | 69 | # Recycle Bin used on file shares 70 | $RECYCLE.BIN/ 71 | 72 | # Windows Installer files 73 | *.cab 74 | *.msi 75 | *.msm 76 | *.msp 77 | 78 | # Windows shortcuts 79 | *.lnk 80 | 81 | 82 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Goo.com Srl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rfid-reader-http 2 | 3 | PC/SC lite HTTP wrapper for reading from a card reader (RFID & NFC). 4 | 5 | ## Tested card reader 6 | 7 | * ACS ACR122 (ACR122U-A9) 8 | 9 | ## Prerequisites 10 | 11 | For running the installation step, due to its dependency from [node-pcsclite](https://github.com/santigimeno/node-pcsclite), this project requires [node-gyp](https://github.com/TooTallNate/node-gyp), so please read carefully and satisfy their [installation requirements](https://github.com/TooTallNate/node-gyp#installation), otherwise it may fails compiling the node module. 12 | 13 | You may have to install the proper driver for your card reader. 14 | 15 | * [ACS ACR122U drivers](http://www.acs.com.hk/en/driver/3/acr122u-usb-nfc-reader/) 16 | 17 | ### Requirements installation 18 | 19 | #### Mac OS X 20 | 21 | 1. Download the ACS ACR122U Drivers for Mac OS X 10.x via the [download page](http://www.acs.com.hk/en/driver/3/acr122u-usb-nfc-reader/) or via direct link for [PC/SC Driver Installer 1.1.0 (Mac 10.5, Mac 10.6, Mac 10.7, Mac 10.8, Mac 10.9, Mac 10.10) (2014-09-17 306KB)](http://www.acs.com.hk/download-driver-unified/5133/ACS-Unified-INST-MacOSX-110-P.zip). You have to install it even if the installer is not properly signed. 22 | 23 | It will install the useful driver in: `/usr/libexec/SmartCardServices/drivers/ifd-acsccid.bundle` 24 | 25 | #### Raspberry Pi (ARM) (Debian) 26 | 27 | *NOTE: At this time, Raspberry Pi 1 models have issue running NodeJS, due to wrong architecture identification (ARMv7 instead of ARMv6). This guide works properly on Raspberry Pi 2 (refence [1](http://raspberrypi.stackexchange.com/a/30068/32403) [2](https://github.com/v8/v8-git-mirror/blob/master/src/base/cpu.cc#L483-L499)).* 28 | 29 | 1. Install the latest version of NodeJS and npm, as stated in the official [guide](https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#debian-and-ubuntu-based-linux-distributions): 30 | 31 | curl --silent --location https://deb.nodesource.com/setup_0.12 | sudo bash - 32 | sudo apt-get install --yes nodejs 33 | 34 | 2. Install PC/SC and libnfc (references: [nfc-tools](http://nfc-tools.org/index.php?title=Libnfc#Debian_.2F_Ubuntu), [libnfc](https://github.com/nfc-tools/libnfc)): 35 | 36 | sudo apt-get install pcscd libusb-dev libpcsclite1 libpcsclite-dev dh-autoreconf 37 | 38 | cd /opt/ 39 | sudo wget https://github.com/nfc-tools/libnfc/archive/libnfc-1.7.1.zip 40 | sudo unzip libnfc-1.7.1.zip 41 | cd libnfc-libnfc-1.7.1/ 42 | sudo autoreconf -vis 43 | sudo ./configure --with-drivers=all 44 | sudo make 45 | sudo make install 46 | 47 | Additionaly, you may need to grant permissions to your user to drive the device. 48 | Under GNU/Linux systems, if you use `udev`, you could use the provided `udev` rules. 49 | e.g. under Debian: `sudo cp /opt/libnfc-libnfc-1.7.1/contrib/udev/42-pn53x.rules /lib/udev/rules.d/` 50 | 51 | 3. Make sure the NFC reader is properly recognized: 52 | 53 | sudo nfc-list 54 | 55 | 1. To fix: `error while loading shared libraries: libnfc.so.4: cannot open shared object file: No such file or directory` ([reference](http://seckev.blog.com/2013/04/17/installation-mfterm-with-acr122u-on-kali-linux-system/)) 56 | 57 | echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/usr-local-lib.conf && sudo ldconfig 58 | 59 | 2. If you have kernel version > 3.5, probably `pcscd` and also `nfc-list` will report this error: `Unable to claim USB interface (Device or resource busy)` due to the automatic load of `pn533` driver. 60 | 61 | To read the `pcscd` dameon output you can run it using: `pcscd -f -d` 62 | 63 | 1. Check which kernel version is installed: `uname -a` 64 | 2. Blacklist `pn533` and `nfc` drivers (references: [Arch Linux wiki Touchatag RFID Reader](https://wiki.archlinux.org/index.php/Touchatag_RFID_Reader), [nfc-tools forum](http://forums.nfc-tools.org/post/5308/#p5308)): 65 | 66 | sudo nano /etc/modprobe.d/blacklist-libnfc.conf 67 | 68 | Add the following lines: 69 | 70 | blacklist pn533 71 | blacklist nfc 72 | 73 | 3. Disable kernel modules: 74 | 75 | modprobe -r pn533 nfc 76 | 77 | 4. Restart the `pcscd` daemon: `sudo service pcscd restart` 78 | 79 | ## Install 80 | 81 | 1. `git clone https://github.com/goodotcom/rfid-reader-http.git` 82 | 2. `cd rfid-reader-http` 83 | 3. `npm install` 84 | 85 | ## Usage 86 | 87 | * Run as process: `node index.js` 88 | * Run in background (detached output): `nohup node index.js &` 89 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | 4 | var express = require('express'), 5 | cors = require('cors') 6 | pcsc = require('pcsclite'); 7 | 8 | 9 | var app = express(), 10 | pcsc = pcsc(), 11 | lastRead = false; 12 | 13 | 14 | // Enabling CORS for all routes. 15 | app.use(cors()); 16 | 17 | 18 | // API 19 | app.get('/', function (req, res) { 20 | res.send(lastRead); 21 | 22 | // Preventing multiple reads of the same read. 23 | lastRead = false; 24 | }); 25 | 26 | 27 | // Start web server. 28 | var server = app.listen(3000, function () { 29 | var host = server.address().address; 30 | var port = server.address().port; 31 | 32 | console.log('Remote RFID server API available at http://%s:%s', host, port); 33 | }); 34 | 35 | 36 | // PC/SC interface. 37 | pcsc.on('reader', function(reader) { 38 | console.log('Reader detected:', reader); 39 | 40 | reader.on('error', function(err) { 41 | console.log('Error(', reader.name, '):', err.message); 42 | }); 43 | 44 | reader.on('status', function(status) { 45 | console.log('Status(', reader.name, '):', status); 46 | 47 | // Check changes. 48 | var changes = this.state ^ status.state; 49 | if (changes) { 50 | 51 | // Card removed. 52 | if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) { 53 | console.log('Status(', reader.name, '): Card removed'); 54 | 55 | reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) { 56 | if (err) { 57 | console.log('Error(', reader.name, '):', err); 58 | } 59 | else { 60 | console.log('Status(', reader.name, '): Disconnected'); 61 | } 62 | }); 63 | 64 | } 65 | 66 | // Card inserted. 67 | else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) { 68 | console.log('Status(', reader.name, '): Card inserted'); 69 | 70 | reader.connect({ share_mode : this.SCARD_SHARE_SHARED }, function(err, protocol) { 71 | if (err) { 72 | console.log('Error(', reader.name, '):', err); 73 | } 74 | else { 75 | console.log('Protocol(', reader.name, '):', protocol); 76 | 77 | /* 78 | Read card UID: [0xFF, 0xCA, 0x00, 0x00, 0x00] 79 | 80 | UID is specified in the ISO 14443 T=CL transport protocol while APDU's are specified in the ISO 7816 application layer protocol. 81 | "Get Data Command" is defined in PCSC 3 v2. If your driver is PCSC v2 compliant, you can get UID using it: 82 | Class = 0xFF 83 | INS = 0xCA 84 | P1 = 0x00 85 | P2 = 0x00 86 | Le = 0x00 (return full length: ISO14443A single 4 bytes, double 7 bytes, triple 10 bytes, for ISO14443B 4 bytes PUPI, for 15693 8 bytes UID) 87 | 88 | Expected response: Data+SW1SW2 89 | */ 90 | var message = new Buffer([0xFF, 0xCA, 0x00, 0x00, 0x00]); 91 | 92 | reader.transmit(message, 40, protocol, function(err, data) { 93 | if (err) { 94 | console.log('Error(', reader.name, '):', err); 95 | } 96 | else { 97 | /* 98 | buf.readUIntLE(offset, byteLength[, noAssert]) 99 | Set noAssert to true to skip validation of value and offset. Defaults to false. 100 | */ 101 | lastRead = data.readUIntBE(0, 6, true).toString(16); 102 | 103 | console.log('Status(', reader.name, '): Read:', data, ' toString:', lastRead); 104 | } 105 | }); 106 | } 107 | }); 108 | } 109 | } 110 | }); 111 | 112 | reader.on('end', function() { 113 | console.log('Status(', reader.name, '): Removed'); 114 | 115 | // Release resources. 116 | reader.close(); 117 | pcsc.close(); 118 | }); 119 | }); 120 | 121 | pcsc.on('error', function(err) { 122 | console.log('Error( PCSC ): ', err); 123 | }); 124 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rfid-reader-http", 3 | "version": "0.0.1", 4 | "description": "PC/SC lite HTTP wrapper for reading from a remote card reader (RFID and NFC).", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/goodotcom/rfid-reader-http.git" 12 | }, 13 | "keywords": [ 14 | "pcsc", 15 | "pcsc-lite", 16 | "rfid", 17 | "nfc", 18 | "http", 19 | "rest" 20 | ], 21 | "author": "Goo.com Srl ", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/goodotcom/rfid-reader-http/issues" 25 | }, 26 | "homepage": "https://github.com/goodotcom/rfid-reader-http#readme", 27 | "devDependencies": { 28 | "cors": "^2.7.1", 29 | "express": "^4.12.4", 30 | "pcsclite": "^0.4.0" 31 | } 32 | } 33 | --------------------------------------------------------------------------------