2 This is a library to read 14443b tags. It can be used something like this
4 local reader = require('read14b')
5 result, err = reader.read14443b()
13 -- Loads the commands-library
14 local cmds
= require('commands')
15 local utils
= require('utils')
17 -- Shouldn't take longer than 2.5 seconds
20 local ISO14B_COMMAND
= {
22 ISO14B_DISCONNECT
= 0x2,
25 ISO14B_REQUEST_TRIGGER
= 0x10,
26 ISO14B_APPEND_CRC
= 0x20,
27 ISO14B_SELECT_STD
= 0x40,
28 ISO14B_SELECT_SR
= 0x80,
29 ISO14B_SET_TIMEOUT
= 0x100,
30 ISO14B_SEND_CHAINING
= 0x200,
31 ISO14B_SELECT_CTS
= 0x400,
32 ISO14B_CLEARTRACE
= 0x800,
35 local function parse14443b(data
)
37 Based on this struct :
45 } PACKED iso14b_card_select_t;
46 --- local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
49 local uid
= data
:sub(1, 20)
50 local uidlen
= data
:sub(21, 22)
51 local atqb
= data
:sub(23, 36)
52 local chipid
= data
:sub(37, 38)
53 local cid
= data
:sub(39, 40)
55 uid
= uid
:sub(1, 2 * uidlen
)
65 -- This function does a connect and retrieves some info
66 -- @return if successful: an table containing card info
67 -- @return if unsuccessful : nil, error
68 local function read14443b(disconnect
)
69 local flags
= ISO14B_COMMAND
.ISO14B_CONNECT
+
70 ISO14B_COMMAND
.ISO14B_SELECT_STD
74 flags
= flags
+ ISO14B_COMMAND
.ISO14B_DISCONNECT
77 local flags_str
= ('%04x'):format(utils
.SwapEndianness(('%04x'):format(flags
), 16))
78 local time_str
= ('%08x'):format(0)
79 local rawlen_str
= ('%04x'):format(0)
80 local senddata
= ('%s%s%s'):format(flags_str
, time_str
, rawlen_str
)
81 local c
= Command
:newNG
{cmd
= cmds
.CMD_HF_ISO14443B_COMMAND
, data
= senddata
}
84 local result
, err
= c
:sendNG(false, TIMEOUT
)
85 if result
and result
.Status
== 0 then
86 if result
.Oldarg0
== 0 then
87 info
, err
= parse14443b(result
.Data
)
89 err
= 'iso14443b card select failed'
92 err
= 'No response from card'
101 -- turns on the HF field.
102 local function connect14443b()
103 local data
= ('%04x%08x%04x'):format(utils
.SwapEndianness(('%04x'):format(ISO14B_COMMAND
.ISO14B_CONNECT
), 16), 0,0)
104 local c
= Command
:newNG
{cmd
= cmds
.CMD_HF_ISO14443B_COMMAND
, data
= data
}
105 return c
:sendNG(true)
108 -- Sends an instruction to do nothing, only disconnect
109 local function disconnect14443b()
110 local data
= ('%04x%08x%04x'):format(utils
.SwapEndianness(('%04x'):format(ISO14B_COMMAND
.ISO14B_DISCONNECT
), 16), 0,0)
111 local c
= Command
:newNG
{cmd
= cmds
.CMD_HF_ISO14443B_COMMAND
, data
= data
}
112 -- We can ignore the response here, no ACK is returned for this command
113 -- Check /armsrc/iso14443b.c, ReaderIso14443b() for details
114 return c
:sendNG(true)
117 -- Waits for a mifare card to be placed within the vicinity of the reader.
118 -- @return if successful: an table containing card info
119 -- @return if unsuccessful : nil, error
120 local function waitFor14443b()
121 print('Waiting for card... press <Enter> to quit')
122 while not core
.kbd_enter_pressed() do
123 res
, err
= read14443b(false)
124 if res
then return res
, err
end
125 -- err means that there was no response from card
128 return nil, 'Aborted by user'
133 waitFor14443b
= waitFor14443b
,
134 parse14443b
= parse14443b
,
135 connect
= connect14443b
,
136 disconnect
= disconnect14443b
,
137 ISO14B_COMMAND
= ISO14B_COMMAND
,