using MIX

This commit is contained in:
iceman1001 2019-05-07 22:33:26 +02:00
commit b73146533a
2 changed files with 33 additions and 64 deletions

View file

@ -120,13 +120,13 @@ end
-- @return if successfull: an table containing card info -- @return if successfull: an table containing card info
-- @return if unsuccessfull : nil, error -- @return if unsuccessfull : nil, error
local function waitFor14443a() local function waitFor14443a()
print("Waiting for card... press any key to quit") print('Waiting for card... press any key to quit')
while not core.ukbhit() do while not core.ukbhit() do
res, err = read14443a() res, err = read14443a()
if res then return res end if res then return res end
-- err means that there was no response from card -- err means that there was no response from card
end end
return nil, "Aborted by user" return nil, 'Aborted by user'
end end
-- Sends an instruction to do nothing, only disconnect -- Sends an instruction to do nothing, only disconnect

View file

@ -2,7 +2,7 @@
This is a library to read 14443b tags. It can be used something like this This is a library to read 14443b tags. It can be used something like this
local reader = require('read14b') local reader = require('read14b')
result, err = reader.select1443b() result, err = reader.read14443b()
if not result then if not result then
print(err) print(err)
return return
@ -13,7 +13,10 @@
-- Loads the commands-library -- Loads the commands-library
local cmds = require('commands') local cmds = require('commands')
local utils = require('utils') local utils = require('utils')
-- Shouldn't take longer than 2.5 seconds
local TIMEOUT = 2500 local TIMEOUT = 2500
local ISO14B_COMMAND = { local ISO14B_COMMAND = {
ISO14B_CONNECT = 1, ISO14B_CONNECT = 1,
ISO14B_DISCONNECT = 2, ISO14B_DISCONNECT = 2,
@ -41,45 +44,16 @@ local function parse1443b(data)
--]] --]]
local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data) local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
uid = uid:sub(1, 2*uidlen) uid = uid:sub(1, 2 * uidlen)
return { uid = uid, uidlen = uidlen, atqb = atqb, chipid = chipid, cid = cid } return {
uid = uid,
uidlen = uidlen,
atqb = atqb,
chipid = chipid,
cid = cid
}
end end
--- Sends a USBpacket to the device
-- @param command - the usb packet to send
-- @param ignoreresponse - if set to true, we don't read the device answer packet
-- which is usually recipe for fail. If not sent, the host will wait 2s for a
-- response of type CMD_ACK
-- @return packet,nil if successfull
-- nil, errormessage if unsuccessfull
local function sendToDevice(cmd, ignoreresponse)
--core.clearCommandBuffer()
local bytes = cmd:getBytes()
local count,c,arg0,arg1,arg2 = bin.unpack('LLLL',bytes)
local err = core.SendCommand(cmd:getBytes())
if err then
print('ERROR',err)
return nil, err
end
if ignoreresponse then return nil,nil end
local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT)
return response,nil
end
--- Picks out and displays the data read from a tag
-- Specifically, takes a usb packet, converts to a Command
-- (as in commands.lua), takes the data-array and
-- reads the number of bytes specified in arg1 (arg0 in c-struct)
-- and displays the data
-- @param usbpacket the data received from the device
local function showData(usbpacket)
local response = Command.parse(usbpacket)
local len = response.arg2 * 2
local data = string.sub(response.data, 0, len);
print("<< ",data)
end
-- This function does a connect and retrieves some info -- This function does a connect and retrieves some info
-- @return if successfull: an table containing card info -- @return if successfull: an table containing card info
-- @return if unsuccessfull : nil, error -- @return if unsuccessfull : nil, error
@ -95,18 +69,22 @@ local function read14443b(disconnect)
flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT
end end
command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags} command = Command:newMIX{
local result, err = sendToDevice(command, false) cmd = cmds.CMD_ISO_14443B_COMMAND,
arg1 = flags
}
local result, err = command:sendMIX()
if result then if result then
local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result)
if arg0 == 0 then if arg0 == 0 then
data = string.sub(result, count) data = string.sub(result, count)
info, err = parse1443b(data) info, err = parse1443b(data)
else else
err = "iso14443b card select failed" err = 'iso14443b card select failed'
end end
else else
err = "No response from card" err = 'No response from card'
end end
if err then if err then
@ -115,43 +93,34 @@ local function read14443b(disconnect)
end end
return info return info
end end
--PING / PONG - Custom Anticollison for Navigo.
-- AA / BB ?!?
-- local ping = ('BA00')
-- result, err = sendRaw(ping, 1, 1)
-- if result then
-- resp = Command.parse( result )
-- if arg1 == 0 then
-- return nil, "iso14443b card - PING/PONG failed"
-- end
-- showData(result)
-- else
-- err = "No response from card"
-- print(err)
-- return nil, err
-- end
--- ---
-- Waits for a mifare card to be placed within the vicinity of the reader. -- Waits for a mifare card to be placed within the vicinity of the reader.
-- @return if successfull: an table containing card info -- @return if successfull: an table containing card info
-- @return if unsuccessfull : nil, error -- @return if unsuccessfull : nil, error
local function waitFor14443b() local function waitFor14443b()
print("Waiting for card... press any key to quit") print('Waiting for card... press any key to quit')
while not core.ukbhit() do while not core.ukbhit() do
res, err = read14443b(false) res, err = read14443b(false)
if res then return res end if res then return res end
-- err means that there was no response from card -- err means that there was no response from card
end end
return nil, "Aborted by user" return nil, 'Aborted by user'
end
-- Sends an instruction to do nothing, only disconnect
local function disconnect14443b()
local c = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443b}
-- We can ignore the response here, no ACK is returned for this command
-- Check /armsrc/iso14443b.c, ReaderIso14443b() for details
return c.sendMIX(true)
end end
local library = { local library = {
read = read14443b, read = read14443b,
waitFor14443b = waitFor14443b, waitFor14443b = waitFor14443b,
parse1443b = parse1443b, parse1443b = parse1443b,
sendToDevice = sendToDevice, disconnect = disconnect14443b,
showData = showData,
ISO14B_COMMAND = ISO14B_COMMAND, ISO14B_COMMAND = ISO14B_COMMAND,
} }