refactored NG

This commit is contained in:
iceman1001 2019-05-07 23:12:58 +02:00
commit e799717880

View file

@ -2,11 +2,27 @@ local cmds = require('commands')
local getopt = require('getopt') local getopt = require('getopt')
local lib14a = require('read14a') local lib14a = require('read14a')
example = "script run 14araw -x 6000F57b" copyright = ''
author = "Martin Holst Swende" author = "Martin Holst Swende"
desc = version = 'v1.0.1'
[[ desc = [[
This is a script to allow raw 1444a commands to be sent and received. This is a script to allow raw 1444a commands to be sent and received.
]]
example = [[
# 1. Connect and don't disconnect
script run 14araw -p
# 2. Send mf auth, read response (nonce)
script run 14araw -o -x 6000F57b -p
# 3. disconnect
script run 14araw -o
# All three steps in one go:
script run 14araw -x 6000F57b
]]
usage = [[
script run 14araw -x 6000F57b
Arguments: Arguments:
-o do not connect - use this only if you previously used -p to stay connected -o do not connect - use this only if you previously used -p to stay connected
@ -17,18 +33,6 @@ Arguments:
-d Debug flag -d Debug flag
-t Topaz mode -t Topaz mode
-3 ISO14443-4 (use RATS) -3 ISO14443-4 (use RATS)
Examples :
# 1. Connect and don't disconnect
script run 14araw -p
# 2. Send mf auth, read response (nonce)
script run 14araw -o -x 6000F57b -p
# 3. disconnect
script run 14araw -o
# All three steps in one go:
script run 14araw -x 6000F57b
]] ]]
--[[ --[[
@ -41,7 +45,6 @@ device-side.
]] ]]
-- Some globals -- Some globals
local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds
local DEBUG = false -- the debug flag local DEBUG = false -- the debug flag
------------------------------- -------------------------------
@ -51,21 +54,34 @@ local DEBUG = false -- the debug flag
--- ---
-- A debug printout-function -- A debug printout-function
local function dbg(args) local function dbg(args)
if DEBUG then if not DEBUG then return end
print("###", args) if type(args) == 'table' then
local i = 1
while args[i] do
dbg(args[i])
i = i+1
end
else
print('###', args)
end end
end end
--- ---
-- This is only meant to be used when errors occur -- This is only meant to be used when errors occur
local function oops(err) local function oops(err)
print("ERROR: ",err) print('ERROR:', err)
core.clearCommandBuffer()
return nil, err
end end
--- ---
-- Usage help -- Usage help
local function help() local function help()
print(copyright)
print(author)
print(version)
print(desc) print(desc)
print("Example usage") print('Example usage')
print(example) print(example)
print(usage)
end end
--- ---
-- The main entry point -- The main entry point
@ -83,31 +99,35 @@ function main(args)
-- Read the parameters -- Read the parameters
for o, a in getopt.getopt(args, 'orcpx:dt3') do for o, a in getopt.getopt(args, 'orcpx:dt3') do
if o == "o" then doconnect = false end if o == 'o' then doconnect = false end
if o == "r" then ignore_response = true end if o == 'r' then ignore_response = true end
if o == "c" then append_crc = true end if o == 'c' then append_crc = true end
if o == "p" then stayconnected = true end if o == 'p' then stayconnected = true end
if o == "x" then payload = a end if o == 'x' then payload = a end
if o == "d" then DEBUG = true end if o == 'd' then DEBUG = true end
if o == "t" then topaz_mode = true end if o == 't' then topaz_mode = true end
if o == "3" then no_rats = true end if o == '3' then no_rats = true end
end end
-- First of all, connect -- First of all, connect
if doconnect then if doconnect then
dbg("doconnect") dbg("doconnect")
-- We reuse the connect functionality from a
-- common library
info, err = lib14a.read(true, no_rats)
if err then return oops(err) end info, err = lib14a.read(true, no_rats)
print(("Connected to card, uid = %s"):format(info.uid)) if err then
lib14a.disconnect()
return oops(err)
end
print(('Connected to card, uid = %s'):format(info.uid))
end end
-- The actual raw payload, if any -- The actual raw payload, if any
if payload then if payload then
res,err = sendRaw(payload,{ignore_response = ignore_response, topaz_mode = topaz_mode, append_crc = append_crc}) res, err = sendRaw(payload,{ignore_response = ignore_response, topaz_mode = topaz_mode, append_crc = append_crc})
if err then return oops(err) end if err then
lib14a.disconnect()
return oops(err)
end
if not ignoreresponse then if not ignoreresponse then
-- Display the returned data -- Display the returned data
@ -116,7 +136,7 @@ function main(args)
end end
-- And, perhaps disconnect? -- And, perhaps disconnect?
if not stayconnected then if not stayconnected then
disconnect() lib14a.disconnect()
end end
end end
@ -132,11 +152,10 @@ function showdata(usbpacket)
--print("data length:",len) --print("data length:",len)
local data = string.sub(tostring(cmd_response.data), 0, len); local data = string.sub(tostring(cmd_response.data), 0, len);
print("<< ",data) print("<< ",data)
--print("----------------")
end end
function sendRaw(rawdata, options) function sendRaw(rawdata, options)
print(">> ", rawdata) print('>> ', rawdata)
local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW
@ -147,39 +166,32 @@ function sendRaw(rawdata, options)
flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC flags = flags + lib14a.ISO14A_COMMAND.ISO14A_APPEND_CRC
end end
local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, local command = Command:newMIX{cmd = cmds.CMD_READER_ISO_14443a,
arg1 = flags, -- Send raw arg1 = flags, -- Send raw
-- arg2 contains the length, which is half the length -- arg2 contains the length, which is half the length
-- of the ASCII-string rawdata -- of the ASCII-string rawdata
arg2 = string.len(rawdata)/2, arg2 = string.len(rawdata)/2,
data = rawdata} data = rawdata}
return lib14a.sendToDevice(command, options.ignore_response) return command:sendMIX(options.ignore_response)
end end
-- Sends an instruction to do nothing, only disconnect
function disconnect()
local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a, arg1 = 0,}
-- We can ignore the response here, no ACK is returned for this command
-- Check /armsrc/iso14443a.c, ReaderIso14443a() for details
return lib14a.sendToDevice(command,true)
end
------------------------- -------------------------
-- Testing -- Testing
------------------------- -------------------------
function selftest() function selftest()
DEBUG = true DEBUG = true
dbg("Performing test") dbg('Performing test')
main() main()
main("-p") main('-p')
main(" -o -x 6000F57b -p") main(' -o -x 6000F57b -p')
main("-o") main('-o')
main("-x 6000F57b") main('-x 6000F57b')
dbg("Tests done") dbg('Tests done')
end end
-- Flip the switch here to perform a sanity check. -- Flip the switch here to perform a sanity check.
-- It read a nonce in two different ways, as specified in the usage-section -- It read a nonce in two different ways, as specified in the usage-section
if "--test"==args then if '--test'==args then
selftest() selftest()
else else
-- Call the main -- Call the main