]>
Commit | Line | Data |
---|---|---|
a2d82b46 | 1 | --[[ |
2 | This is a library to read 14443a tags. It can be used something like this | |
3 | ||
4 | local reader = require('read14a') | |
5 | result, err = reader.read1443a() | |
6 | if not result then | |
7 | print(err) | |
8 | return | |
9 | end | |
10 | print(result.name) | |
11 | ||
12 | --]] | |
13 | -- Loads the commands-library | |
14 | local cmds = require('commands') | |
15 | local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds | |
16 | local ISO14A_COMMAND = { | |
17 | ISO14A_CONNECT = 1, | |
18 | ISO14A_NO_DISCONNECT = 2, | |
19 | ISO14A_APDU = 4, | |
20 | ISO14A_RAW = 8, | |
21 | ISO14A_REQUEST_TRIGGER = 0x10, | |
22 | ISO14A_APPEND_CRC = 0x20, | |
23 | ISO14A_SET_TIMEOUT = 0x40 | |
24 | } | |
25 | ||
26 | local ISO14443a_TYPES = {} | |
27 | ISO14443a_TYPES[0x00] = "NXP MIFARE Ultralight | Ultralight C" | |
28 | ISO14443a_TYPES[0x04] = "NXP MIFARE (various !DESFire !DESFire EV1)" | |
29 | ISO14443a_TYPES[0x08] = "NXP MIFARE CLASSIC 1k | Plus 2k" | |
30 | ISO14443a_TYPES[0x09] = "NXP MIFARE Mini 0.3k" | |
31 | ISO14443a_TYPES[0x10] = "NXP MIFARE Plus 2k" | |
32 | ISO14443a_TYPES[0x11] = "NXP MIFARE Plus 4k" | |
33 | ISO14443a_TYPES[0x18] = "NXP MIFARE Classic 4k | Plus 4k" | |
34 | ISO14443a_TYPES[0x20] = "NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k | JCOP 31/41" | |
35 | ISO14443a_TYPES[0x24] = "NXP MIFARE DESFire | DESFire EV1" | |
36 | ISO14443a_TYPES[0x28] = "JCOP31 or JCOP41 v2.3.1" | |
37 | ISO14443a_TYPES[0x38] = "Nokia 6212 or 6131 MIFARE CLASSIC 4K" | |
38 | ISO14443a_TYPES[0x88] = "Infineon MIFARE CLASSIC 1K" | |
39 | ISO14443a_TYPES[0x98] = "Gemplus MPCOS" | |
40 | ||
41 | ||
42 | local function tostring_1443a(sak) | |
43 | return ISO14443a_TYPES[sak] or ("Unknown (SAK=%x)"):format(sak) | |
44 | end | |
45 | ||
46 | local function parse1443a(data) | |
47 | --[[ | |
48 | ||
49 | Based on this struct : | |
50 | ||
51 | typedef struct { | |
52 | byte_t uid[10]; | |
53 | byte_t uidlen; | |
54 | byte_t atqa[2]; | |
55 | byte_t sak; | |
56 | byte_t ats_len; | |
57 | byte_t ats[256]; | |
58 | } __attribute__((__packed__)) iso14a_card_select_t; | |
59 | ||
60 | --]] | |
61 | ||
62 | local count,uid,uidlen, atqa, sak, ats_len, ats= bin.unpack('H10CH2CC',data) | |
63 | uid = uid:sub(1,2*uidlen) | |
64 | --print("uid, atqa, sak: ",uid, atqa, sak) | |
65 | --print("TYPE: ", tostring_1443a(sak)) | |
66 | return { uid = uid, atqa = atqa, sak = sak, name = tostring_1443a(sak)} | |
67 | end | |
68 | ||
b61f426c | 69 | --- Sends a USBpacket to the device |
70 | -- @param command - the usb packet to send | |
71 | -- @param ignoreresponse - if set to true, we don't read the device answer packet | |
72 | -- which is usually recipe for fail. If not sent, the host will wait 2s for a | |
73 | -- response of type CMD_ACK | |
74 | -- @return packet,nil if successfull | |
75 | -- nil, errormessage if unsuccessfull | |
76 | ||
77 | local function sendToDevice(command, ignoreresponse) | |
78 | core.clearCommandBuffer() | |
79 | local err = core.SendCommand(command:getBytes()) | |
80 | if err then | |
81 | print(err) | |
82 | return nil, err | |
83 | end | |
84 | if ignoreresponse then return nil,nil end | |
85 | ||
86 | local response = core.WaitForResponseTimeout(cmds.CMD_ACK,TIMEOUT) | |
87 | return response,nil | |
88 | end | |
89 | ||
65e344df | 90 | -- This function does a connect and retrieves som einfo |
91 | -- @param dont_disconnect - if true, does not disable the field | |
92 | -- @return if successfull: an table containing card info | |
93 | -- @return if unsuccessfull : nil, error | |
94 | local function read14443a(dont_disconnect) | |
95 | local command, result, info, err, data | |
96 | ||
97 | command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, | |
98 | arg1 = ISO14A_COMMAND.ISO14A_CONNECT} | |
99 | if dont_disconnect then | |
100 | command.arg1 = command.arg1 + ISO14A_COMMAND.ISO14A_NO_DISCONNECT | |
101 | end | |
102 | local result,err = sendToDevice(command) | |
103 | if result then | |
104 | local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) | |
105 | if arg0 == 0 then | |
106 | return nil, "iso14443a card select failed" | |
a2d82b46 | 107 | end |
65e344df | 108 | data = string.sub(result,count) |
109 | info, err = parse1443a(data) | |
110 | else | |
111 | err ="No response from card" | |
112 | end | |
a2d82b46 | 113 | |
65e344df | 114 | if err then |
115 | print(err) | |
116 | return nil, err | |
117 | end | |
118 | return info | |
119 | end | |
120 | ||
121 | --- | |
122 | -- Waits for a mifare card to be placed within the vicinity of the reader. | |
123 | -- @return if successfull: an table containing card info | |
124 | -- @return if unsuccessfull : nil, error | |
125 | local function waitFor14443a() | |
126 | print("Waiting for card... press any key to quit") | |
127 | while not core.ukbhit() do | |
128 | res, err = read14443a() | |
129 | if res then return res end | |
130 | -- err means that there was no response from card | |
131 | end | |
132 | return nil, "Aborted by user" | |
133 | end | |
134 | local library = { | |
135 | ||
136 | read1443a = read14443a, | |
137 | waitFor14443a = waitFor14443a, | |
b61f426c | 138 | parse1443a = parse1443a, |
139 | sendToDevice = sendToDevice, | |
140 | ISO14A_COMMAND = ISO14A_COMMAND, | |
a2d82b46 | 141 | } |
142 | ||
143 | return library |