hf 14b got some love

This commit is contained in:
iceman1001 2021-04-23 22:25:58 +02:00
commit 2119c4e7bd
11 changed files with 645 additions and 393 deletions

View file

@ -181,7 +181,6 @@ function Command:sendNG( ignore_response, timeout )
count, data, ng = bin.unpack('H'..length..'C', response, count)
--[[ uncomment if you want to debug
print('NG package received')
print('CMD ::', tostring(cmd))
print('Length ::', tostring(length))

View file

@ -15,7 +15,7 @@ local cmds = require('commands')
local utils = require('utils')
-- Shouldn't take longer than 2.5 seconds
local TIMEOUT = 2000
local TIMEOUT = 1000
local ISO14B_COMMAND = {
ISO14B_CONNECT = 0x1,
@ -34,7 +34,6 @@ local ISO14B_COMMAND = {
local function parse14443b(data)
--[[
Based on this struct :
typedef struct {
@ -44,10 +43,15 @@ local function parse14443b(data)
uint8_t chipid;
uint8_t cid;
} PACKED iso14b_card_select_t;
--- local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
--]]
local count, uid, uidlen, atqb, chipid, cid = bin.unpack('H10CH7CC',data)
local uid = data:sub(1, 20)
local uidlen = data:sub(21, 22)
local atqb = data:sub(23, 36)
local chipid = data:sub(37, 38)
local cid = data:sub(39, 40)
uid = uid:sub(1, 2 * uidlen)
return {
uid = uid,
@ -62,9 +66,6 @@ end
-- @return if successful: an table containing card info
-- @return if unsuccessful : nil, error
local function read14443b(disconnect)
local result, info, err, data
local flags = ISO14B_COMMAND.ISO14B_CONNECT +
ISO14B_COMMAND.ISO14B_SELECT_STD
@ -72,18 +73,18 @@ local function read14443b(disconnect)
print('DISCONNECT')
flags = flags + ISO14B_COMMAND.ISO14B_DISCONNECT
end
local senddata = ('%04x%08x%04x'):format(flags, 0, 0)
local flags_str = ('%04x'):format(utils.SwapEndianness(('%04x'):format(flags), 16))
local time_str = ('%08x'):format(0)
local rawlen_str = ('%04x'):format(0)
local senddata = ('%s%s%s'):format(flags_str, time_str, rawlen_str)
local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = senddata}
info = nil
local info = nil
local result, err = c:sendNG(false, TIMEOUT)
if result then
local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL', result)
if arg0 == 0 then
data = string.sub(result, count)
info, err = parse14443b(data)
if result and result.Status == 0 then
if result.Oldarg0 == 0 then
info, err = parse14443b(result.Data)
else
err = 'iso14443b card select failed'
end
@ -97,34 +98,35 @@ local function read14443b(disconnect)
return info, nil
end
---
-- Waits for a mifare card to be placed within the vicinity of the reader.
-- @return if successful: an table containing card info
-- @return if unsuccessful : nil, error
local function waitFor14443b()
print('Waiting for card... press Enter to quit')
while not core.kbd_enter_pressed() do
res, err = read14443b(false)
if res then return res, err end
-- err means that there was no response from card
end
return nil, 'Aborted by user'
end
---
-- turns on the HF field.
local function connect14443b()
local data = ('%04x%08x%04x'):format(ISO14B_COMMAND.ISO14B_CONNECT, 0, 0)
local data = ('%04x%08x%04x'):format(utils.SwapEndianness(('%04x'):format(ISO14B_COMMAND.ISO14B_CONNECT), 16), 0,0)
local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = data}
return c:sendNG(true)
end
---
-- Sends an instruction to do nothing, only disconnect
local function disconnect14443b()
local data = ('%04x%08x%04x'):format(ISO14B_COMMAND.ISO14B_DISCONNECT, 0, 0)
local data = ('%04x%08x%04x'):format(utils.SwapEndianness(('%04x'):format(ISO14B_COMMAND.ISO14B_DISCONNECT), 16), 0,0)
local c = Command:newNG{cmd = cmds.CMD_HF_ISO14443B_COMMAND, data = data}
-- We can ignore the response here, no ACK is returned for this command
-- Check /armsrc/iso14443b.c, ReaderIso14443b() for details
return c:sendNG(true)
end
---
-- Waits for a mifare card to be placed within the vicinity of the reader.
-- @return if successful: an table containing card info
-- @return if unsuccessful : nil, error
local function waitFor14443b()
print('Waiting for card... press <Enter> to quit')
while not core.kbd_enter_pressed() do
res, err = read14443b(false)
if res then return res, err end
-- err means that there was no response from card
end
disconnect14443b()
return nil, 'Aborted by user'
end
local library = {
read = read14443b,