using MIX

This commit is contained in:
iceman1001 2019-05-07 22:33:26 +02:00
parent c81bce6bf2
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 unsuccessfull : nil, error
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
res, err = read14443a()
if res then return res end
-- err means that there was no response from card
end
return nil, "Aborted by user"
return nil, 'Aborted by user'
end
-- 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
local reader = require('read14b')
result, err = reader.select1443b()
result, err = reader.read14443b()
if not result then
print(err)
return
@ -13,7 +13,10 @@
-- Loads the commands-library
local cmds = require('commands')
local utils = require('utils')
-- Shouldn't take longer than 2.5 seconds
local TIMEOUT = 2500
local ISO14B_COMMAND = {
ISO14B_CONNECT = 1,
ISO14B_DISCONNECT = 2,
@ -41,45 +44,16 @@ local function parse1443b(data)
--]]
local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
uid = uid:sub(1, 2*uidlen)
return { uid = uid, uidlen = uidlen, atqb = atqb, chipid = chipid, cid = cid }
uid = uid:sub(1, 2 * uidlen)
return {
uid = uid,
uidlen = uidlen,
atqb = atqb,
chipid = chipid,
cid = cid
}
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
-- @return if successfull: an table containing card info
-- @return if unsuccessfull : nil, error
@ -95,18 +69,22 @@ local function read14443b(disconnect)
flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT
end
command = Command:new{cmd = cmds.CMD_ISO_14443B_COMMAND, arg1 = flags}
local result, err = sendToDevice(command, false)
command = Command:newMIX{
cmd = cmds.CMD_ISO_14443B_COMMAND,
arg1 = flags
}
local result, err = command:sendMIX()
if result then
local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result)
if arg0 == 0 then
data = string.sub(result, count)
info, err = parse1443b(data)
else
err = "iso14443b card select failed"
err = 'iso14443b card select failed'
end
else
err = "No response from card"
err = 'No response from card'
end
if err then
@ -115,43 +93,34 @@ local function read14443b(disconnect)
end
return info
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.
-- @return if successfull: an table containing card info
-- @return if unsuccessfull : nil, error
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
res, err = read14443b(false)
if res then return res end
-- err means that there was no response from card
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
local library = {
read = read14443b,
waitFor14443b = waitFor14443b,
parse1443b = parse1443b,
sendToDevice = sendToDevice,
showData = showData,
disconnect = disconnect14443b,
ISO14B_COMMAND = ISO14B_COMMAND,
}