2 This is a library to read 14443a tags. It can be used something like this
4 local reader = require('read14a')
5 result, err = reader.read14443a()
13 -- Loads the commands-library
14 local taglib
= require('taglib')
15 local cmds
= require('commands')
17 -- Shouldn't take longer than 2 seconds
20 local ISO14A_COMMAND
= {
22 ISO14A_NO_DISCONNECT
= 2,
25 ISO14A_REQUEST_TRIGGER
= 0x10,
26 ISO14A_APPEND_CRC
= 0x20,
27 ISO14A_SET_TIMEOUT
= 0x40,
28 ISO14A_NO_SELECT
= 0x80,
29 ISO14A_TOPAZMODE
= 0x100,
30 ISO14A_NO_RATS
= 0x200,
31 ISO14A_SEND_CHAINING
= 0x400,
32 ISO14A_USE_ECP
= 0x800,
33 ISO14A_USE_MAGSAFE
= 0x1000,
36 local ISO14443a_TYPES
= {}
37 ISO14443a_TYPES
[0x00] = "NXP MIFARE Ultralight | Ultralight C | NTAG"
38 ISO14443a_TYPES
[0x01] = "NXP MIFARE TNP3xxx Activision Game Appliance"
39 ISO14443a_TYPES
[0x04] = "NXP MIFARE (various !DESFire !DESFire EV1)"
40 ISO14443a_TYPES
[0x08] = "NXP MIFARE CLASSIC 1k | Plus 2k"
41 ISO14443a_TYPES
[0x09] = "NXP MIFARE Mini 0.3k"
42 ISO14443a_TYPES
[0x0A] = "FM11RF005SH (Shanghai Metro)"
43 ISO14443a_TYPES
[0x10] = "NXP MIFARE Plus 2k"
44 ISO14443a_TYPES
[0x11] = "NXP MIFARE Plus 4k"
45 ISO14443a_TYPES
[0x18] = "NXP MIFARE Classic 4k | Plus 4k"
46 ISO14443a_TYPES
[0x20] = "NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41"
47 ISO14443a_TYPES
[0x24] = "NXP MIFARE DESFire | DESFire EV1"
48 ISO14443a_TYPES
[0x28] = "JCOP31 or JCOP41 v2.3.1"
49 ISO14443a_TYPES
[0x38] = "Nokia 6212 or 6131 MIFARE CLASSIC 4K"
50 ISO14443a_TYPES
[0x88] = "Infineon MIFARE CLASSIC 1K"
51 ISO14443a_TYPES
[0x98] = "Gemplus MPCOS"
53 local function tostring_14443a(sak
)
54 return ISO14443a_TYPES
[sak
] or ("Unknown (SAK=%x)"):format(sak
)
57 local function parse14443a(data
)
66 } PACKED iso14a_card_select_t;
69 local count
, uid
, uidlen
, atqa
, sak
, ats_len
, ats
= bin
.unpack('H10CH2CCH', data
)
70 uid
= uid
:sub(1, 2 * uidlen
)
71 local man_byte
= tonumber(uid
:sub(1,2), 16)
77 name
= tostring_14443a(sak
),
79 manufacturer
= taglib
.lookupManufacturer(man_byte
),
84 -- This function does a connect and retrieves som einfo
85 -- @param dont_disconnect - if true, does not disable the field
86 -- @return if successful: an table containing card info
87 -- @return if unsuccessful : nil, error
88 local function read14443a(dont_disconnect
, no_rats
)
89 local command
, result
, info
, err
, data
91 command
= Command
:newMIX
{
92 cmd
= cmds
.CMD_HF_ISO14443A_READER
,
93 arg1
= ISO14A_COMMAND
.ISO14A_CONNECT
96 if dont_disconnect
then
97 command
.arg1
= command
.arg1
+ ISO14A_COMMAND
.ISO14A_NO_DISCONNECT
100 command
.arg1
= command
.arg1
+ ISO14A_COMMAND
.ISO14A_NO_RATS
103 local result
, err
= command
:sendMIX()
105 local count
, cmd
, arg1
, arg2
, arg3
= bin
.unpack('LLLL',result
)
107 return nil, 'iso14443a card select failed'
109 data
= string.sub(result
, count
)
110 info
= parse14443a(data
)
112 err
= 'No response from card'
123 -- Waits for a mifare card to be placed within the vicinity of the reader.
124 -- @return if successful: an table containing card info
125 -- @return if unsuccessful : nil, error
126 local function waitFor14443a()
127 print('Waiting for card... press <Enter> to quit')
128 while not core
.kbd_enter_pressed() do
129 res
, err
= read14443a()
130 if res
then return res
end
131 -- err means that there was no response from card
133 return nil, 'Aborted by user'
136 -- Sends an instruction to do nothing, only disconnect
137 local function disconnect14443a()
138 local c
= Command
:newMIX
{cmd
= cmds
.CMD_HF_ISO14443A_READER
}
139 -- We can ignore the response here, no ACK is returned for this command
140 -- Check /armsrc/iso14443a.c, ReaderIso14443a() for details
141 return c
:sendMIX(true)
146 waitFor14443a
= waitFor14443a
,
147 parse14443a
= parse14443a
,
148 disconnect
= disconnect14443a
,
149 ISO14A_COMMAND
= ISO14A_COMMAND
,