]>
Commit | Line | Data |
---|---|---|
220d638d | 1 | --[[ |
2 | This is a library to read 14443b tags. It can be used something like this | |
3 | ||
4 | local reader = require('read14b') | |
5 | result, err = reader.select1443b() | |
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 utils = require('utils') | |
4bf0f73d | 16 | local TIMEOUT = 2000 |
6fc68747 | 17 | local ISO14B_COMMAND = { |
18 | ISO14B_CONNECT = 1, | |
19 | ISO14B_DISCONNECT = 2, | |
20 | ISO14B_APDU = 4, | |
21 | ISO14B_RAW = 8, | |
22 | ISO14B_REQUEST_TRIGGER = 0x10, | |
23 | ISO14B_APPEND_CRC = 0x20, | |
24 | ISO14B_SELECT_STD = 0x40, | |
25 | ISO14B_SELECT_SR = 0x80, | |
26 | } | |
220d638d | 27 | |
6fc68747 | 28 | local function parse1443b(data) |
220d638d | 29 | --[[ |
6fc68747 | 30 | |
31 | Based on this struct : | |
32 | ||
33 | typedef struct { | |
34 | byte_t uid[10]; | |
35 | byte_t uidlen; | |
36 | byte_t atqb[7]; | |
37 | byte_t chipid; | |
38 | byte_t cid; | |
39 | } __attribute__((__packed__)) iso14b_card_select_t; | |
220d638d | 40 | |
220d638d | 41 | --]] |
6fc68747 | 42 | |
43 | local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data) | |
44 | uid = uid:sub(1,2*uidlen) | |
45 | return { uid = uid, uidlen = uidlen, atqb = atqb, chipid = chipid, cid = cid } | |
220d638d | 46 | end |
47 | ||
220d638d | 48 | --- Sends a USBpacket to the device |
49 | -- @param command - the usb packet to send | |
6fc68747 | 50 | -- @param ignoreresponse - if set to true, we don't read the device answer packet |
220d638d | 51 | -- which is usually recipe for fail. If not sent, the host will wait 2s for a |
52 | -- response of type CMD_ACK | |
53 | -- @return packet,nil if successfull | |
54 | -- nil, errormessage if unsuccessfull | |
6fc68747 | 55 | local function sendToDevice(cmd, ignoreresponse) |
56 | --core.clearCommandBuffer() | |
57 | local bytes = cmd:getBytes() | |
58 | local count,c,arg0,arg1,arg2 = bin.unpack('LLLL',bytes) | |
220d638d | 59 | local err = core.SendCommand(cmd:getBytes()) |
60 | if err then | |
6fc68747 | 61 | print('ERROR',err) |
220d638d | 62 | return nil, err |
63 | end | |
6fc68747 | 64 | if ignoreresponse then return nil,nil end |
65 | ||
220d638d | 66 | local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT) |
220d638d | 67 | return response,nil |
68 | end | |
69 | --- Picks out and displays the data read from a tag | |
70 | -- Specifically, takes a usb packet, converts to a Command | |
71 | -- (as in commands.lua), takes the data-array and | |
72 | -- reads the number of bytes specified in arg1 (arg0 in c-struct) | |
73 | -- and displays the data | |
74 | -- @param usbpacket the data received from the device | |
75 | local function showData(usbpacket) | |
76 | local response = Command.parse(usbpacket) | |
6fc68747 | 77 | local len = response.arg2 * 2 |
220d638d | 78 | local data = string.sub(response.data, 0, len); |
79 | print("<< ",data) | |
80 | end | |
81 | ||
220d638d | 82 | |
83 | -- This function does a connect and retrieves some info | |
84 | -- @return if successfull: an table containing card info | |
85 | -- @return if unsuccessfull : nil, error | |
6fc68747 | 86 | local function read14443b(disconnect) |
220d638d | 87 | |
6fc68747 | 88 | local command, result, info, err, data |
220d638d | 89 | |
6fc68747 | 90 | local flags = ISO14B_COMMAND.ISO14B_CONNECT + |
91 | ISO14B_COMMAND.ISO14B_SELECT_STD | |
92 | ||
93 | if disconnect then | |
94 | print('DISCONNECT') | |
95 | flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT | |
220d638d | 96 | end |
97 | ||
6fc68747 | 98 | command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} |
99 | local result,err = sendToDevice(command, false) | |
220d638d | 100 | if result then |
6fc68747 | 101 | local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) |
102 | if arg0 == 0 then | |
103 | data = string.sub(result, count) | |
104 | info, err = parse1443b(data) | |
105 | else | |
106 | err = "iso14443b card select failed" | |
220d638d | 107 | end |
220d638d | 108 | else |
109 | err = "No response from card" | |
6fc68747 | 110 | end |
111 | ||
112 | if err then | |
220d638d | 113 | print(err) |
114 | return nil, err | |
115 | end | |
6fc68747 | 116 | return info |
220d638d | 117 | end |
6fc68747 | 118 | --PING / PONG - Custom Anticollison for Navigo. |
119 | -- AA / BB ?!? | |
120 | -- local ping = ('BA00') | |
121 | -- result, err = sendRaw(ping, 1, 1) | |
122 | -- if result then | |
123 | -- resp = Command.parse( result ) | |
124 | -- if arg1 == 0 then | |
125 | -- return nil, "iso14443b card - PING/PONG failed" | |
126 | -- end | |
127 | -- showData(result) | |
128 | -- else | |
129 | -- err = "No response from card" | |
130 | -- print(err) | |
131 | -- return nil, err | |
132 | -- end | |
133 | ||
220d638d | 134 | |
135 | --- | |
136 | -- Waits for a mifare card to be placed within the vicinity of the reader. | |
137 | -- @return if successfull: an table containing card info | |
138 | -- @return if unsuccessfull : nil, error | |
139 | local function waitFor14443b() | |
140 | print("Waiting for card... press any key to quit") | |
141 | while not core.ukbhit() do | |
6fc68747 | 142 | res, err = read14443b(false) |
220d638d | 143 | if res then return res end |
144 | -- err means that there was no response from card | |
145 | end | |
146 | return nil, "Aborted by user" | |
147 | end | |
148 | ||
220d638d | 149 | local library = { |
6fc68747 | 150 | parse1443b = parse1443b, |
151 | read1443b = read14443b, | |
220d638d | 152 | waitFor14443b = waitFor14443b, |
153 | sendToDevice = sendToDevice, | |
220d638d | 154 | showData = showData, |
6fc68747 | 155 | ISO14B_COMMAND = ISO14B_COMMAND, |
220d638d | 156 | } |
157 | ||
158 | return library |