improve test harness for LF

This commit is contained in:
iceman1001 committed 2026-07-28 15:53:20 +02:00
1 parent d4b5e8a772
commit 2dbd4836c3
8 files changed
+802 -826

No files matched your search

+142 -76
View File
@@ -1,53 +1,48 @@
local cmds = require('commands')
local getopt = require('getopt')
local bin = require('bin')
local utils = require('utils')
local dumplib = require('html_dumplib')
local ansicolors = require('ansicolors')
local R = require('testresult')
copyright = ''
author = 'Iceman'
version = 'v1.0.5'
version = 'v1.1.1'
desc = [[
This script will load several traces files in current working directory/traces/ folder and do
"data load"
"lf search -1u"
Replays every LF trace in traces/ through `lf search` and reports what each one
was identified as, against a recorded baseline.
The following tracefiles will be loaded:
em*.pm3
modulation*.pm3
The baseline lives in traces/lf_search_expected.txt, one line per trace.
`-u` - record or refresh the baseline
Runs entirely offline, no device and no tag needed.
]]
example = [[
1. script run data_tracetest
1. script run tests/data_tracetest
2. script run tests/data_tracetest -u
3. script run tests/data_tracetest -f em4
]]
usage = [[
script run data_tracetest [-h]
script run tests/data_tracetest [-h] [-u] [-v] [-f <substring>]
]]
arguments = [[
-h : this help
-u : update the baseline from this run
-v : print the full `lf search` output for every trace
-f <substring> : only traces whose name contains this
]]
local DEBUG = true -- the debug flag
---
-- A debug printout-function
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while result[i] do
dbg(result[i])
i = i+1
end
else
print('###', args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
local BASELINE = 'traces/lf_search_expected.txt'
local RULE = R.RULE
local ROW = '%-44s %-24s %-24s '
local function no_capture(out)
if type(out) == 'string' then return false end
print('[?] Maybe a session already running')
return true
end
---
-- Usage help
local function help()
@@ -62,65 +57,108 @@ local function help()
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
--
-- Exit message
local function ExitMsg(msg)
print( string.rep('--',20) )
print( string.rep('--',20) )
print(msg)
print()
local function identified(out)
local found = {}
local seen = {}
for name in out:gmatch('Valid (.-) ID found') do
if seen[name] == nil then
seen[name] = true
table.insert(found, name)
end
end
table.sort(found)
if #found == 0 then return 'none' end
return table.concat(found, ' + ')
end
local function load_baseline(path)
local t, n = {}, 0
local fh = io.open(path, 'r')
if fh == nil then return nil, 0 end
for line in fh:lines() do
local name, want = line:match('^(%S+)%s+(.-)%s*$')
if name ~= nil and name:sub(1, 1) ~= '#' then
t[name] = want
n = n + 1
end
end
fh:close()
return t, n
end
local function main(args)
print( string.rep('--',20) )
print( string.rep('--',20) )
local update, verbose, filter = false, false, nil
local cmdDataLoad = 'data load -f %s';
local cwd = core.cwd();
local tracesEM = "find '"..cwd.."/traces/ ' -iname 'em*.pm3' -type f"
local tracesMOD = "find '"..cwd.."/traces/' -iname 'modulation*.pm3' -type f"
local write2File = false
local outputTemplate = os.date('testtest_%Y-%m-%d_%H%M%S')
-- Arguments for the script
for o, arg in getopt.getopt(args, 'h') do
for o, a in getopt.getopt(args, 'huvf:') do
if o == 'h' then return help() end
if o == 'u' then update = true end
if o == 'v' then verbose = true end
if o == 'f' then filter = a end
end
core.clearCommandBuffer()
local cwd = core.cwd()
local files = {}
-- Find a set of traces staring with EM
local p = assert( io.popen(tracesEM))
local p = assert(io.popen("find '" .. cwd .. "/traces/' -iname 'lf_*.pm3' -type f | sort"))
for file in p:lines() do
table.insert(files, file)
if filter == nil or file:find(filter, 1, true) then
table.insert(files, file)
end
end
p.close();
p:close()
-- Find a set of traces staring with MODULATION
p = assert( io.popen(tracesMOD) )
for file in p:lines() do
table.insert(files, file)
if #files == 0 then
print('[!] no traces found under ' .. cwd .. '/traces/')
return
end
p.close();
local cmdLFSEARCH = 'lf search -1u'
local baseline, nbase = load_baseline(cwd .. '/' .. BASELINE)
if baseline == nil and update == false then
print('[!] no baseline at ' .. BASELINE)
print('[?] recording what this run finds, then run with -u to freeze it')
baseline = {}
end
-- main loop
io.write('Starting to test traces > ')
for _,file in pairs(files) do
print(RULE)
print(('LF trace identification, %d traces%s'):format(#files, (nbase > 0) and (', baseline of '.. nbase) or ''))
print(RULE)
print(' ' .. ROW:format('trace', 'identified as', 'baseline') .. 'verdict')
print(RULE)
local x = 'data load -f '..file
dbg(x)
core.console(x)
local pass, fail, new, results = 0, 0, 0, {}
dbg(cmdLFSEARCH)
core.console(cmdLFSEARCH)
for _, file in ipairs(files) do
local name = file:match('([^/]+)$')
core.console('data load -f ' .. file, false, true)
local out = core.console('lf search -1u', true, not verbose)
if no_capture(out) then return end
local got = identified(out)
table.insert(results, { name = name, got = got })
if update then
print('[=] ' .. ROW:format(name, got, '') .. R.SKIP)
else
local want = baseline[name]
if want == nil then
new = new + 1
print('[?] ' .. ROW:format(name, got, '( no entry )') .. NEW)
elseif want == got then
pass = pass + 1
print('[+] ' .. ROW:format(name, got, '') .. R.OK)
else
fail = fail + 1
print('[!] ' .. ROW:format(name, got, want) .. R.FAIL)
end
end
core.clearCommandBuffer()
@@ -129,9 +167,37 @@ local function main(args)
break
end
end
io.write('\n')
print( string.rep('--',20) )
print(RULE)
if update then
local fh, err = io.open(cwd .. '/' .. BASELINE, 'w')
if fh == nil then
print('[!] could not write '.. BASELINE ..': '..tostring(err))
return
end
fh:write('# `lf search -1u` identification per trace, recorded by `script run tests/data_tracetest -u`.\n')
for _, r in ipairs(results) do
fh:write(('%-46s %s\n'):format(r.name, r.got))
end
fh:close()
print(('[+] wrote %d entries to %s'):format(#results, BASELINE))
else
local count = R.counter()
count.pass, count.fail, count.other = pass, fail, new
count:summary()
if new > 0 then
print(('[?] %d have no baseline entry, record one with -u'):format(new))
end
end
print(RULE)
return fail
end
main(args)
+32 -111
View File
@@ -1,86 +1,36 @@
local cmds = require('commands')
local getopt = require('getopt')
local bin = require('bin')
local utils = require('utils')
local ansicolors = require('ansicolors')
local format=string.format
local floor=math.floor
local t55 = require('t55xx_config')
copyright = ''
author = 'Iceman'
version = 'v1.0.2'
version = 'v1.1.1'
desc = [[
This script will program a T55x7 TAG with the configuration: block 0x00 data 0x000100
The outlined procedure is as following:
Testsuite for the ASK / MANCHESTER demodulation.
--ASK
00 00 80 40
-- max 2
-- manchester
-- bit rate
Writes block 0 with ASK/Manchester selected and sweeps the bit rate, reads the
configuration back with `lf t55xx detect` and reports, per configuration,
whether what came back is what went in.
"lf t55xx write -b 0 -d 00008040"
"lf t55xx detect"
"lf t55xx info"
00 xx 80 40
-- -- max 2 blocks
-- ----- manchester
-- bit rate, 00 04 08 0C 10 14 18 1C = RF/8 16 32 40 50 64 100 128
Loop:
change the configuretion block 0 with:
-xx 00 xxxx = RF/8
-xx 04 xxxx = RF/16
-xx 08 xxxx = RF/32
-xx 0C xxxx = RF/40
-xx 10 xxxx = RF/50
-xx 14 xxxx = RF/64
-xx 18 xxxx = RF/100
-xx 1C xxxx = RF/128
testsuite for the ASK/MANCHESTER demod
Needs a T5577 in the field.
]]
example =[[
1. script run lf_t55xx_defaultask
example = [[
1. script run tests/lf_t55xx_defaultask
2. script run tests/lf_t55xx_defaultask -v
]]
usage = [[
script run lf_t55xx_defaultask [-h]
script run tests/lf_t55xx_defaultask [-h] [-v]
]]
arguments = [[
-h : this help
-v : also run `lf t55xx info` for each configuration
]]
local DEBUG = true -- the debug flag
local TIMEOUT = 1500
--BLOCK 0 = 00008040 ASK / MAN
local config1 = '00'
local config2 = '8040'
local procedurecmds = {
[1] = '%s%02X%s',
[2] = 'lf t55xx detect',
[3] = 'lf t55xx info',
}
---
-- A debug printout-function
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while result[i] do
dbg(result[i])
i = i+1
end
else
print('###', args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
---
-- Usage help
local function help()
@@ -95,58 +45,29 @@ local function help()
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
--
-- Exit message
local function ExitMsg(msg)
print( string.rep('--',20) )
print( string.rep('--',20) )
print(msg)
print()
end
local function test()
local y
local password = '00000000'
local block = '00'
local flags = '00'
for y = 0x0, 0x1d, 0x4 do
for _ = 1, #procedurecmds do
local pcmd = procedurecmds[_]
if #pcmd == 0 then
elseif _ == 1 then
local config = pcmd:format(config1, y, config2)
dbg(('lf t55xx write -b 0 -d %s'):format(config))
local data = ('%s%s%s%s'):format(utils.SwapEndiannessStr(config, 32), password, block, flags)
local wc = Command:newNG{cmd = cmds.CMD_LF_T55XX_WRITEBL, data = data}
local response, err = wc:sendNG(false, TIMEOUT)
if not response then return oops(err) end
else
dbg(pcmd)
core.console( pcmd )
end
end
core.clearCommandBuffer()
end
print( string.rep('--',20) )
end
local function main(args)
print( string.rep('--',20) )
print( string.rep('--',20) )
local opts = {}
-- Arguments for the script
for o, arg in getopt.getopt(args, 'h') do
for o, _ in getopt.getopt(args, 'hv') do
if o == 'h' then return help() end
if o == 'v' then opts.verbose = true end
end
core.clearCommandBuffer()
test()
print( string.rep('--',20) )
local report = t55.report('T55x7 ASK / Manchester, bit rate sweep')
for bitrate = 0x0, 0x1d, 0x4 do
t55.check(report, ('00%02X8040'):format(bitrate), opts)
if core.kbd_enter_pressed() then
print('aborted by user')
break
end
end
return report:summary()
end
main(args)
+31 -106
View File
@@ -1,80 +1,36 @@
local cmds = require('commands')
local getopt = require('getopt')
local bin = require('bin')
local utils = require('utils')
local ansicolors = require('ansicolors')
local t55 = require('t55xx_config')
copyright = ''
author = 'Iceman'
version = 'v1.0.2'
version = 'v1.1.1'
desc = [[
This script will program a T55x7 TAG with the configuration: block 0x00 data 0x00010040
The outlined procedure is as following:
Testsuite for the ASK / BIPHASE demodulation.
--BIPHASE 00010040
--
Writes block 0 with ASK/Biphase selected and sweeps the bit rate, reads the
configuration back with `lf t55xx detect` and reports, per configuration,
whether what came back is what went in.
"lf t55xx write -b 0 -d 00010040"
"lf t55xx detect"
"lf t55xx info"
00 xx 00 40
-- -- max 2 blocks
-- bit rate in the top bits, biphase in the low bit
00 04 08 0C 10 14 18 1C = RF/8 16 32 40 50 64 100 128, plus 1
Loop:
change the configuretion block 0 with:
-xx01xxxx = RF/8
-xx05xxxx = RF/16
-xx09xxxx = RF/32
-xx0Dxxxx = RF/40
-xx11xxxx = RF/50
-xx15xxxx = RF/64
-xx19xxxx = RF/100
-xx1Dxxxx = RF/128
testsuit for the BIPHASE demod
Needs a T5577 in the field.
]]
example = [[
1. script run lf_t55xx_defaultbi
1. script run tests/lf_t55xx_defaultbi
2. script run tests/lf_t55xx_defaultbi -v
]]
usage = [[
script run lf_t55xx_defaultbi [-h]
script run tests/lf_t55xx_defaultbi [-h] [-v]
]]
arguments = [[
-h : this help
-v : also run `lf t55xx info` for each configuration
]]
local DEBUG = true -- the debug flag
local TIMEOUT = 1500
--BLOCK 0 = 00010040 BIPHASE
local config1 = '00'
local config2 = '0040'
local procedurecmds = {
[1] = '%s%02X%s',
[2] = 'lf t55xx detect',
[3] = 'lf t55xx info',
}
---
-- A debug printout-function
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
---
-- Usage help
local function help()
@@ -89,60 +45,29 @@ local function help()
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
--
-- Exit message
local function ExitMsg(msg)
print( string.rep('--',20) )
print( string.rep('--',20) )
print(msg)
print()
end
local function test()
local y
local password = '00000000'
local block = '00'
local flags = '00'
for y = 1, 0x1D, 4 do
for _ = 1, #procedurecmds do
local pcmd = procedurecmds[_]
if #pcmd == 0 then
elseif _ == 1 then
local config = pcmd:format(config1, y, config2)
dbg(('lf t55xx write -b 0 -d %s'):format(config))
local data = ('%s%s%s%s'):format(utils.SwapEndiannessStr(config, 32), password, block, flags)
local wc = Command:newNG{cmd = cmds.CMD_LF_T55XX_WRITEBL, data = data}
local response, err = wc:sendNG(false, TIMEOUT)
if not response then return oops(err) end
else
dbg(pcmd)
core.console( pcmd )
end
end
core.clearCommandBuffer()
end
print( string.rep('--',20) )
end
local function main(args)
print( string.rep('--',20) )
print( string.rep('--',20) )
local opts = {}
-- Arguments for the script
for o, arg in getopt.getopt(args, 'h') do
for o, _ in getopt.getopt(args, 'hv') do
if o == 'h' then return help() end
if o == 'v' then opts.verbose = true end
end
core.clearCommandBuffer()
test()
print( string.rep('--',20) )
local report = t55.report('T55x7 ASK / Biphase, bit rate sweep')
for bitrate = 0x0, 0x1d, 0x4 do
t55.check(report, ('00%02X0040'):format(bitrate + 1), opts)
if core.kbd_enter_pressed() then
print('aborted by user')
break
end
end
return report:summary()
end
main(args)
+44 -107
View File
@@ -1,82 +1,43 @@
local cmds = require('commands')
local getopt = require('getopt')
local bin = require('bin')
local utils = require('utils')
local ansicolors = require('ansicolors')
local t55 = require('t55xx_config')
copyright = ''
author = 'Iceman'
version = 'v1.0.2'
version = 'v1.1.1'
desc = [[
This script will program a T55x7 TAG with the configuration: block 0x00 data 0x000100
The outlined procedure is as following:
Testsuite for the FSK demodulation.
--ASK
00 00 80 40
-- max 2 blocks
-- FSK1
-- bit rate
Writes block 0 with each of the four FSK variants selected and sweeps the bit
rate, reads the configuration back with `lf t55xx detect` and reports, per
configuration, whether what came back is what went in.
"lf t55xx write -b 0 -d 00007040"
"lf t55xx detect"
"lf t55xx info"
00 xx y0 40
-- -- max 2 blocks
-- FSK1 4, FSK2 5, FSK1a 6, FSK2a 7
-- bit rate, 00 04 08 0C 10 14 18 1C = RF/8 16 32 40 50 64 100 128
Loop:
change the configuretion block 0 with:
-xx 00 xxxx = RF/8
-xx 04 xxxx = RF/16
-xx 08 xxxx = RF/32
-xx 0C xxxx = RF/40
-xx 10 xxxx = RF/50
-xx 14 xxxx = RF/64
-xx 18 xxxx = RF/100
-xx 1C xxxx = RF/128
testsuit for the ASK/MANCHESTER demod
Needs a T5577 in the field.
]]
example = [[
1. script run lf_t55xx_defaultfsk
1. script run tests/lf_t55xx_defaultfsk
2. script run tests/lf_t55xx_defaultfsk -v
]]
usage = [[
script run lf_t55xx_defaultfsk [-h]
script run tests/lf_t55xx_defaultfsk [-h] [-v]
]]
arguments = [[
-h : this help
-v : also run `lf t55xx info` for each configuration
]]
local DEBUG = true -- the debug flag
local TIMEOUT = 1500
--BLOCK 0 = 00008040 FSK
local config1 = '00'
local config2 = '040'
local procedurecmds = {
[1] = '%s%02X%X%s',
[2] = 'lf t55xx detect',
[3] = 'lf t55xx info',
local MODULATIONS = {
[4] = 'FSK1',
[5] = 'FSK2',
[6] = 'FSK1a',
[7] = 'FSK2a',
}
---
-- A debug printout-function
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
---
-- Usage help
local function help()
@@ -91,60 +52,36 @@ local function help()
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
--
-- Exit message
local function ExitMsg(msg)
print( string.rep('--',20) )
print( string.rep('--',20) )
print(msg)
print()
end
local function test(modulation)
local y
local password = '00000000'
local block = '00'
local flags = '00'
for y = 0x0, 0x1d, 0x4 do
for _ = 1, #procedurecmds do
local pcmd = procedurecmds[_]
if #pcmd == 0 then
elseif _ == 1 then
local config = pcmd:format(config1, y, modulation, config2)
dbg(('lf t55xx write -b 0 -d %s'):format(config))
local data = ('%s%s%s%s'):format(utils.SwapEndiannessStr(config, 32), password, block, flags)
local wc = Command:newNG{cmd = cmds.CMD_LF_T55XX_WRITEBL, data = data}
local response, err = wc:sendNG(false, TIMEOUT)
if not response then return oops(err) end
else
dbg(pcmd)
core.console( pcmd )
end
end
core.clearCommandBuffer()
end
print( string.rep('--',20) )
end
local function main(args)
print( string.rep('--',20) )
print( string.rep('--',20) )
local opts = {}
-- Arguments for the script
for o, arg in getopt.getopt(args, 'h') do
for o, _ in getopt.getopt(args, 'hv') do
if o == 'h' then return help() end
if o == 'v' then opts.verbose = true end
end
core.clearCommandBuffer()
test(4)
test(5)
test(6)
test(7)
print( string.rep('--',20) )
local report = t55.report('T55x7 FSK, four variants x bit rate sweep')
local aborted = false
for modulation = 4, 7 do
for bitrate = 0x0, 0x1d, 0x4 do
t55.check(report, ('00%02X%X040'):format(bitrate, modulation), opts)
if core.kbd_enter_pressed() then
print('aborted by user')
aborted = true
break
end
end
if aborted then break end
end
return report:summary()
end
main(args)
+44 -123
View File
@@ -1,80 +1,38 @@
local cmds = require('commands')
local getopt = require('getopt')
local bin = require('bin')
local utils = require('utils')
local ansicolors = require('ansicolors')
local t55 = require('t55xx_config')
copyright = ''
author = 'Iceman'
version = 'v1.0.3'
version = 'v1.1.1'
desc = [[
This script will program a T55x7 TAG with the configuration: block 0x00 data 0x00088040
The outlined procedure is as following:
Testsuite for the PSK demodulation.
"lf t55xx write -b 0 -d 00088040"
"lf t55xx detect"
"lf t55xx info"
Writes block 0 with PSK selected and sweeps the bit rate and the PSK
subcarrier, reads the configuration back with `lf t55xx detect` and reports,
per configuration, whether what came back is what went in.
Loop OUTER:
change the configuretion block 0 with:
-xxxx8xxx = PSK RF/2 with Manchester modulation
-xxxx1xxx = PSK RF/2 with PSK1 modulation (phase change when input changes)
-xxxx2xxx = PSK RF/2 with PSk2 modulation (phase change on bitclk if input high)
-xxxx3xxx = PSK RF/2 with PSk3 modulation (phase change on rising edge of input)
Loop INNER
for each outer configuration, also do
XXXXX0XX = PSK RF/2
XXXXX4XX = PSK RF/4
XXXXX8XX = PSK RF/8
00 xx y z 40
-- -- max 2 blocks
-- subcarrier, 0 4 8 = RF/2 RF/4 RF/8
-- PSK1 1, PSK2 2, PSK3 3
-- bit rate, 00 04 08 0C 10 14 18 1C = RF/8 16 32 40 50 64 100 128
In all 12 individual test for the PSK demod
Needs a T5577 in the field.
]]
example = [[
1. script run lf_t55xx_defaultpsk
1. script run tests/lf_t55xx_defaultpsk
2. script run tests/lf_t55xx_defaultpsk -a
]]
usage = [[
script run lf_t55xx_defaultpsk [-h]
script run tests/lf_t55xx_defaultpsk [-h] [-a] [-v]
]]
arguments = [[
-h : this help
-a : sweep PSK2 and PSK3 as well, not just PSK1
-v : also run `lf t55xx info` for each configuration
]]
local DEBUG = true -- the debug flag
local TIMEOUT = 1500
-- --BLOCK 0 = 00 08 80 40 PSK
-- -----------
-- 08------- bitrate
-- 8----- modulation PSK1
-- 0---- PSK ClockRate
-- 40 max 2 blocks
local procedurecmds = {
[1] = '00%02X%X%X40',
[2] = 'lf t55xx detect',
[3] = 'lf t55xx info',
}
---
-- A debug printout-function
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
---
-- Usage help
local function help()
@@ -89,79 +47,42 @@ local function help()
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
--
-- Exit message
local function ExitMsg(msg)
print( string.rep('--',20) )
print( string.rep('--',20) )
print(msg)
print()
end
local function test(modulation)
local bitrate
local clockrate
local password = '00000000'
local block = '00'
local flags = '00'
for bitrate = 0x0, 0x1d, 0x4 do
for clockrate = 0,8,4 do
for _ = 1, #procedurecmds do
local cmd = procedurecmds[_]
if #cmd == 0 then
elseif _ == 1 then
dbg('Writing to T55x7 TAG')
local config = cmd:format(bitrate, modulation, clockrate)
dbg(('lf t55xx write -b 0 -d %s'):format(config))
local data = ('%s%s%s%s'):format(utils.SwapEndiannessStr(config, 32), password, block, flags)
local wc = Command:newNG{cmd = cmds.CMD_LF_T55XX_WRITEBL, data = data}
local response, err = wc:sendNG(false, TIMEOUT)
if not response then return oops(err) end
else
dbg(cmd)
core.console( cmd )
end
end
core.clearCommandBuffer()
end
end
print( string.rep('--',20) )
end
local function main(args)
print( string.rep('--',20) )
print( string.rep('--',20) )
local opts = {}
local last_mod = 1
-- Arguments for the script
for o, arg in getopt.getopt(args, 'h') do
for o, _ in getopt.getopt(args, 'hav') do
if o == 'h' then return help() end
if o == 'a' then last_mod = 3 end
if o == 'v' then opts.verbose = true end
end
core.clearCommandBuffer()
test(1) -- PSK1
--test(2) -- PSK2
--test(3) -- PSK3
local title = (last_mod == 1) and 'T55x7 PSK1, bit rate x subcarrier sweep' or 'T55x7 PSK1/2/3, bit rate x subcarrier sweep'
local report = t55.report(title)
local aborted = false
print( string.rep('--',20) )
for modulation = 1, last_mod do
for bitrate = 0x0, 0x1d, 0x4 do
for subcarrier = 0, 8, 4 do
t55.check(report, ('00%02X%X%X40'):format(bitrate, modulation, subcarrier), opts)
if core.kbd_enter_pressed() then
print('aborted by user')
aborted = true
break
end
end
if aborted then break end
end
if aborted then break end
end
return report:summary()
end
main(args)
-- Where it iterates over
-- xxxx8xxx = PSK RF/2 with Manchester modulation
-- xxxx1xxx = PSK RF/2 with PSK1 modulation (phase change when input changes)
-- xxxx2xxx = PSK RF/2 with PSk2 modulation (phase change on bitclk if input high)
-- xxxx3xxx = PSK RF/2 with PSk3 modulation (phase change on rising edge of input)
-- XXXXX0XX = PSK RF/2
-- XXXXX4XX = PSK RF/4
-- XXXXX8XX = PSK RF/8
+178 -303
View File
@@ -1,82 +1,58 @@
local cmds = require('commands')
local getopt = require('getopt')
local bin = require('bin')
local utils = require('utils')
local ansicolors = require('ansicolors')
local t55 = require('t55xx_config')
local R = require('testresult')
local format = string.format
local floor = math.floor
copyright = ''
author = "Iceman"
version = 'v1.0.4'
desc =[[
This script will program a T55x7 TAG with a configuration and four blocks of data.
It will then try to detect and read back those block data and compare if read data matches the expected data.
author = 'Iceman'
version = 'v1.1.1'
desc = [[
Writes a configuration and four data blocks to a T55x7, then detects the tag
and reads those blocks back, comparing what came off the tag with what went on.
A strict T55x7 test suite:
lf t55xx wipe
lf t55xx detect
lf t55xx write -b 1 -d 00000000
lf t55xx write -b 2 -d ffffffff
lf t55xx write -b 3 -d 80000000
lf t55xx write -b 4 -d 00000001
lf t55xx wipe
lf t55xx write -b 1 -d 00000000
lf t55xx write -b 2 -d ffffffff
lf t55xx write -b 3 -d 80000000
lf t55xx write -b 4 -d 00000001
Loop:
Each row names the configuration word, what it means, what `lf t55xx detect`
made of it and how many of the four data blocks read back intact. Markers are
also written into the session log ( `rem [ERR:...]` and `rem [SUMMARY:...]` )
try write different configuration blocks, and read block1-4 and comparing the read values with the values used to write.
testsuit for T55XX commands demodulation
Note: that the card is wiped before each modulation.
Needs a T5577 in the field.
]]
example = [[
1. script run lf_t55xx_writetest
2. script run lf_t55xx_writetest -t FSK2A
3. script run lf_t55xx_writetest -t PSK1
1. script run tests/lf_t55xx_writetest
2. script run tests/lf_t55xx_writetest -t PSK1
3. script run tests/lf_t55xx_writetest -t all
]]
usage = [[
script run lf_t55xx_writetest [-h] [-t <modulation type>
script run tests/lf_t55xx_writetest [-h] [-t <modulation>]
]]
arguments = [[
-h this help
-t (optional, defaults to ASK) 'PSK1', 'PSK2', 'PSK3',
'FSK1', 'FSK2', 'FSK1A', 'FSK2A',
'ASK', 'BI'
-t modulation to test, defaults to ASK
'PSK1', 'PSK2', 'PSK3', 'FSK1', 'FSK2', 'FSK1A', 'FSK2A', 'ASK', 'BI', or 'ALL'
]]
local DEBUG = true -- the debug flag
local TIMEOUT = 1500
local total_tests = 0
local total_pass = 0
local data_blocks_cmds = {
local DATA_BLOCKS = {
[1] = '00000000',
[2] = 'ffffffff',
[2] = 'aa5500ff',
[3] = '80000000',
[4] = '00000001',
}
---
-- A debug printout-function
local function dbg(args)
if not DEBUG then return end
if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
end
end
---
-- This is only meant to be used when errors occur
local function oops(err)
print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end
local ALL_MODULATIONS = { 'ASK', 'BI', 'PSK1', 'PSK2', 'PSK3', 'FSK1', 'FSK2', 'FSK1A', 'FSK2A' }
---
-- Usage help
local function help()
@@ -91,313 +67,212 @@ local function help()
print(ansicolors.cyan..'Example usage'..ansicolors.reset)
print(example)
end
---
-- Exit message
local function exitMsg(msg)
print( string.rep('--',20) )
print( string.rep('--',20) )
print(msg)
print()
end
---
-- ask/fsk/psk configuration blocks to test
local function GetConfigs( modulation )
-- The configuration words to sweep for each modulation. A PSK subcarrier that
-- does not divide the bit rate into a whole number of cycles is not a
-- configuration the tag can hold, so those are absent rather than expected to
-- fail: RF/50 with an RF/4 subcarrier would be 12.5 cycles a bit.
local function GetConfigs(modulation)
local t = {}
t['PSK1'] = {
-- Rf2
[1] = '00001040', -- 8
[2] = '00041040', -- 16
[3] = '00081040', -- 32
[4] = '000c1040', -- 40
[5] = '00101040', -- 50
[6] = '00141040', -- 64
[7] = '00181040', -- 100
[8] = '001c1040', -- 128
-- Rf4
[9] = '00001440', -- 8
[10] = '00041440', -- 16
[11] = '00081440', -- 32
[12] = '000c1440', -- 40
-- [] = '00101440', -- 50 50/4 == 12.5 invalid
[13] = '00141440', -- 64
[14] = '00181440', -- 100
[15] = '001c1440', -- 128
-- Rf8
[16] = '00001840', -- 8
[17] = '00041840', -- 16
[18] = '00081840', -- 32
[19] = '000c1840', -- 40
-- [] = '00101840', -- 50 50/8 = 6.25 invalid
[20] = '00141840', -- 64
-- [] = '00181840', -- 100 100/8 == 12.5 invalid
[21] = '001c1840', -- 128
}
-- subcarrier RF/2
'00001040', '00041040', '00081040', '000c1040',
'00101040', '00141040', '00181040', '001c1040',
-- subcarrier RF/4, no RF/50
'00001440', '00041440', '00081440', '000c1440',
'00141440', '00181440', '001c1440',
-- subcarrier RF/8, no RF/50 and no RF/100
'00001840', '00041840', '00081840', '000c1840',
'00141840', '001c1840',
}
t['PSK2'] = {
-- Rf2
[1] = '00002040', -- 8
[2] = '00042040', -- 16
[3] = '00082040', -- 32
[4] = '000c2040', -- 40
[5] = '00102040', -- 50
[6] = '00142040', -- 64
[7] = '00182040', -- 100
[8] = '001c2040', -- 128
-- Rf4
[9] = '00002440', -- 8
[10] = '00042440', -- 16
[11] = '00082440', -- 32
[12] = '000c2440', -- 40
-- [] = '00102440', -- 50 50/4 == 12.5 invalid
[13] = '00142440', -- 64
[14] = '00182440', -- 100
[15] = '001c2440', -- 128
-- Rf8
[16] = '00002840', -- 8
[17] = '00042840', -- 16
[18] = '00082840', -- 32
[19] = '000c2840', -- 40
-- [] = '00102840', -- 50 50/8 == 6.25 invalid
[20] = '00142840', -- 64
-- [] = '00182840', -- 100 100/8 == 12.5 invalid
[21] = '001c2840', -- 128
}
'00002040', '00042040', '00082040', '000c2040',
'00102040', '00142040', '00182040', '001c2040',
'00002440', '00042440', '00082440', '000c2440',
'00142440', '00182440', '001c2440',
'00002840', '00042840', '00082840', '000c2840',
'00142840', '001c2840',
}
t['PSK3'] = {
-- Rf2
[1] = '00003040', -- 8
[2] = '00043040', -- 16
[3] = '00083040', -- 32
[4] = '000c3040', -- 40
[5] = '00103040', -- 50
[6] = '00143040', -- 64
[7] = '00183040', -- 100
[8] = '001c3040', -- 128
-- Rf4
[9] = '00003440', -- 8
[10] = '00043440', -- 16
[11] = '00083440', -- 32
[12] = '000c3440', -- 40
-- [] = '00103440', -- 50 50/4 == 12.5 invalid
[13] = '00143440', -- 64
[14] = '00183440', -- 100
[15] = '001c3440', -- 128
-- Rf2
[16] = '00003840', -- 8
[17] = '00043840', -- 16
[18] = '00083840', -- 32
[19] = '000c3840', -- 40
-- [] = '00103840', -- 50 50/8 == 6.25 invalid
[20] = '00143840', -- 64
-- [] = '00183840', -- 100 100/8 == 12.5 invalid
[21] = '001c3840', -- 128
}
'00003040', '00043040', '00083040', '000c3040',
'00103040', '00143040', '00183040', '001c3040',
'00003440', '00043440', '00083440', '000c3440',
'00143440', '00183440', '001c3440',
'00003840', '00043840', '00083840', '000c3840',
'00143840', '001c3840',
}
t['FSK1'] = {
[1] = '00004040',
[2] = '00004040',
[3] = '00044040',
[4] = '00084040',
[5] = '000c4040',
[6] = '00104040',
[7] = '00144040',
[8] = '00184040',
[9] = '001c4040',
}
'00004040', '00044040', '00084040', '000c4040',
'00104040', '00144040', '00184040', '001c4040',
}
t['FSK2'] = {
[1] = '00005040',
[2] = '00045040',
[3] = '00085040',
[4] = '000c5040',
[5] = '00105040',
[6] = '00145040',
[7] = '00185040',
[8] = '001c5040',
}
'00005040', '00045040', '00085040', '000c5040',
'00105040', '00145040', '00185040', '001c5040',
}
t['FSK1A'] = {
[1] = '00006040',
[2] = '00046040',
[3] = '00086040',
[4] = '000c6040',
[5] = '00106040',
[6] = '00146040',
[7] = '00186040',
[8] = '001c6040',
}
'00006040', '00046040', '00086040', '000c6040',
'00106040', '00146040', '00186040', '001c6040',
}
t['FSK2A'] = {
[1] = '00007040',
[2] = '00047040',
[3] = '00087040',
[4] = '000c7040',
[5] = '00107040',
[6] = '00147040',
[7] = '00187040',
[8] = '001c7040',
}
'00007040', '00047040', '00087040', '000c7040',
'00107040', '00147040', '00187040', '001c7040',
}
t['ASK'] = {
[1] = '00008040',
[2] = '00048040',
[3] = '00088040',
[4] = '000c8040',
[5] = '00108040',
[6] = '00148040',
[7] = '00188040',
[8] = '001c8040',
}
'00008040', '00048040', '00088040', '000c8040',
'00108040', '00148040', '00188040', '001c8040',
}
-- biphase is 16, which spans the modulation field rather than sitting in a
-- nibble of it, so it is the low bit of the bit rate byte
t['BI'] = {
[1] = '00010040',
[2] = '00050040',
[3] = '00090040',
[4] = '000d0040',
[5] = '00110040',
[6] = '00150040',
[7] = '00190040',
[8] = '001d0040',
}
'00010040', '00050040', '00090040', '000d0040',
'00110040', '00150040', '00190040', '001d0040',
}
return t[modulation:upper()]
end
---
-- lf t55xx wipe
local function WipeCard()
-- Wipe, then lay down the four data blocks. Returns false with a reason when
-- the tag is not there or cannot be read back at all, which is a fact about
-- the run rather than about any one configuration.
local function prepare()
print('Wiping card')
core.console('lf t55xx wipe')
core.console('lf t55xx wipe', false, true)
print('Detecting card')
local res, msg = core.t55xx_detect()
if not res then
oops("Can't detect modulation. Test failed.")
core.console('rem [ERR:DETECT:WIPED] Failed to detect after wipe')
return false
else
local wipe_data_cmd = 'lf t55xx write -b %s -d %s'
for _ = 1, #data_blocks_cmds do
local val = data_blocks_cmds[_]
local c = string.format(wipe_data_cmd, _, val)
core.console(c)
if not core.t55xx_detect() then
core.console('rem [ERR:DETECT:WIPED] Failed to detect after wipe', false, true)
return false, 'tag not detected after wipe'
end
for block, val in ipairs(DATA_BLOCKS) do
core.console(format('lf t55xx write -b %d -d %s', block, val), false, true)
end
return true
end
---
-- Read blocks 1-4 back. Returns how many matched and a short note naming the
-- ones that did not.
local function read_back(config)
local good, bad = 0, {}
for block, want in ipairs(DATA_BLOCKS) do
local data = core.t55xx_readblock(block, '0', '0', '')
local got = data and ('%08X'):format(data) or ''
if got:lower() == want:lower() then
good = good + 1
else
table.insert(bad, tostring(block))
core.console(format('rem [ERR:READ:%s:%d] block %d: read %s instead of %s',
config, block, block, got, want), false, true)
end
return true
end
end
---
-- lf t55xx read
local function CheckReadBlock(block)
local data, msg
-- blockno, page1, override, pwd
data, msg = core.t55xx_readblock(block, '0', '0', '')
if not data then
return ''
local note = ''
if #bad > 0 then
note = 'block ' .. table.concat(bad, ',') .. ' bad'
end
return ('%08X'):format(data)
return good, note
end
local function test(modulation)
local function test(modulation, report)
local process_block0_cmds = {}
local y
local password = '00000000'
local block = '00' -- configuration block 0
local flags = '00' -- page 0, no pwd, no testmode
local configs = GetConfigs(modulation)
if configs == nil then
print('[!] no configurations known for modulation ' .. modulation)
return false
end
local s = ('Start test of %s'):format(modulation)
print(s)
local ok, err = prepare()
if ok == false then
print('[!] ' .. err)
print('[?] this suite writes to a tag, it needs a T5577 on the antenna')
return false
end
process_block0_cmds = GetConfigs(modulation)
for _, config in ipairs(configs) do
if process_block0_cmds == nil then return oops('Cant find modulation '..modulation) end
for _ = 1, #process_block0_cmds do
local p_config_cmd = process_block0_cmds[_]
local errors = 0
core.clearCommandBuffer()
-- Write Config block
dbg(('lf t55xx write -b 0 -d %s'):format(p_config_cmd))
local written, werr = t55.write(config, '00000000', TIMEOUT)
if written == false then
report:add(config, nil, { note = 'write failed', extra = '-' })
local data = ('%s%s%s%s'):format(utils.SwapEndiannessStr(p_config_cmd, 32), password, block, flags)
local wc = Command:newNG{cmd = cmds.CMD_LF_T55XX_WRITEBL, data = data}
local response, err = wc:sendNG(false, TIMEOUT)
if not response then return oops(err) end
-- Detect
local res, msg = core.t55xx_detect()
if not res then
print("can't detect modulation, skip to next config")
core.console(format('rem [ERR:DETECT:%s] Failed to detect modulation', p_config_cmd))
core.console(format('rem [SUMMARY:%s] FAIL detection', p_config_cmd))
total_tests = total_tests + #data_blocks_cmds
else
-- Loop block1-2
for _ = 1, #data_blocks_cmds do
total_tests = total_tests + 1
local val = data_blocks_cmds[_]
local blockdata, msg = CheckReadBlock(_)
if blockdata:lower() ~= val:lower() then
print( ('Test %s == %s Failed'):format(val, blockdata))
core.console( format('rem [ERR:READ:%s:%d] block %d: read %s instead of %s', p_config_cmd, _, _, blockdata, val))
errors = errors+1
else
print( ('Test %s == %s OK'):format(val, blockdata))
total_pass = total_pass + 1
end
end
if errors >0 then
core.console( format('rem [SUMMARY:%s] FAIL %d test%s', p_config_cmd, errors, errors > 1 and "s" or ""))
local found = t55.detect()
if found == nil then
core.console(format('rem [SUMMARY:%s] FAIL detection', config), false, true)
report:add(config, nil, { extra = '0/' .. #DATA_BLOCKS })
else
core.console( format('rem [SUMMARY:%s] PASS all tests', p_config_cmd))
local good, note = read_back(config)
local all = (good == #DATA_BLOCKS) and (found.block0:upper() == config:upper())
core.console(format('rem [SUMMARY:%s] %s', config,
all and 'PASS all tests'
or format('FAIL %d test%s', #DATA_BLOCKS - good,
(#DATA_BLOCKS - good) == 1 and '' or 's')), false, true)
report:add(config, found.block0, {
ok = all,
extra = ('%d/%d'):format(good, #DATA_BLOCKS),
note = note,
})
end
end
if core.kbd_enter_pressed() then
print('aborted by user')
return false
end
end
return true
end
local function main(args)
print( string.rep('--',20) )
print( string.rep('--',20) )
local modulation = 'ASK'
local modulation_type = 'ASK'
-- Arguments for the script
for o, arg in getopt.getopt(args, 'ht:') do
for o, a in getopt.getopt(args, 'ht:') do
if o == 'h' then return help() end
if o == 't' then modulation_type = arg end
if o == 't' then modulation = a end
end
core.clearCommandBuffer()
local res
-- Adjust this table to set which configurations should be tested
-- local test_modes = { 'PSK1', 'PSK2', 'PSK3', 'FSK1', 'FSK2', 'FSK1A', 'FSK2A', 'ASK', 'BI' }
--local test_modes = { 'PSK1' }
local test_modes = { modulation_type }
local modes = { modulation }
if modulation:upper() == 'ALL' then modes = ALL_MODULATIONS end
for _ = 1, #test_modes do
res = WipeCard()
if res then
print (test_modes[_])
test(test_modes[_])
else
exitMsg('Abort!')
return
end
local report = t55.report(
('T55x7 write and read back, %s'):format(
(#modes > 1) and 'every modulation' or modes[1]:upper()),
'blocks')
for _, m in ipairs(modes) do
if test(m, report) == false then break end
end
exitMsg('Tests finished')
core.console(
format('rem [SUMMARY] Success rate: %d/%d tests passed%s'
, total_pass
, total_tests
, total_pass < total_tests and ', help me improving that number!' or ' \\o/'
)
)
local fails = report:summary()
core.console(format('rem [SUMMARY] Success rate: %d/%d tests passed%s',
report.count.pass, report.count:total(),
(fails == 0) and ' \\o/' or ', help me improving that number!'), false, true)
return fails
end
main(args)