refactoring

This commit is contained in:
iceman1001 2019-05-07 22:48:18 +02:00
parent 6fbd45aaed
commit 9bf961cb8a
2 changed files with 60 additions and 49 deletions

View file

@ -4,27 +4,22 @@ local lib14b = require('read14b')
local utils = require('utils') local utils = require('utils')
local iso7816 = require('7816_error') local iso7816 = require('7816_error')
example = "script runs 14b raw commands to query a CAPLYPSO tag" copyright = ''
author = "Iceman, 2016" author = 'Iceman'
desc = version = 'v1.0.1'
[[ desc = [[
This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands This is a script to communicate with a CALYSPO / 14443b tag using the '14b raw' commands
]]
example = [[
script run calypso -b 11223344
]]
usage = [[
script run calypso -h -b
Arguments: Arguments:
-b 123 h this helptext
Examples : b raw bytes to send
script run f -b 11223344
script run f
Examples :
# 1. Connect and don't disconnect
script run f
# 2. Send mf auth, read response
script run f
# 3. disconnect
script run f
]] ]]
--[[ --[[
@ -61,23 +56,34 @@ end
--- ---
-- 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)
calypso_switch_off_field() calypso_switch_off_field()
return nil, err 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
-- --
-- helper function, give current count of items in lua-table. -- helper function, give current count of items in lua-table.
@ -185,26 +191,26 @@ local _calypso_cmds = {
-- Electronic Transaction log file -- Electronic Transaction log file
--["01.Select ICC file"] = '0294 a4 00 0002 3f00', --['01.Select ICC file'] = '0294 a4 00 0002 3f00',
["01.Select ICC file"] = '0294 a4 080004 3f00 0002', ['01.Select ICC file'] = '0294 a4 080004 3f00 0002',
["02.ICC"] = '0394 b2 01 041d', ['02.ICC'] = '0394 b2 01 041d',
["03.Select EnvHol file"] = '0294 a4 080004 2000 2001', ['03.Select EnvHol file'] = '0294 a4 080004 2000 2001',
["04.EnvHol1"] = '0394 b2 01 041d', ['04.EnvHol1'] = '0394 b2 01 041d',
["05.Select EvLog file"] = '0294 a4 080004 2000 2010', ['05.Select EvLog file'] = '0294 a4 080004 2000 2010',
["06.EvLog1"] = '0394 b2 01 041d', ['06.EvLog1'] = '0394 b2 01 041d',
["07.EvLog2"] = '0294 b2 02 041d', ['07.EvLog2'] = '0294 b2 02 041d',
["08.EvLog3"] = '0394 b2 03 041d', ['08.EvLog3'] = '0394 b2 03 041d',
["09.Select ConList file"]= '0294 a4 080004 2000 2050', ['09.Select ConList file']= '0294 a4 080004 2000 2050',
["10.ConList"] = '0394 b2 01 041d', ['10.ConList'] = '0394 b2 01 041d',
["11.Select Contra file"] = '0294 a4 080004 2000 2020', ['11.Select Contra file'] = '0294 a4 080004 2000 2020',
["12.Contra1"] = '0394 b2 01 041d', ['12.Contra1'] = '0394 b2 01 041d',
["13.Contra2"] = '0294 b2 02 041d', ['13.Contra2'] = '0294 b2 02 041d',
["14.Contra3"] = '0394 b2 03 041d', ['14.Contra3'] = '0394 b2 03 041d',
["15.Contra4"] = '0294 b2 04 041d', ['15.Contra4'] = '0294 b2 04 041d',
["16.Select Counter file"]= '0394 a4 080004 2000 2069', ['16.Select Counter file']= '0394 a4 080004 2000 2069',
["17.Counter"] = '0294 b2 01 041d', ['17.Counter'] = '0294 b2 01 041d',
["18.Select SpecEv file"] = '0394 a4 080004 2000 2040', ['18.Select SpecEv file'] = '0394 a4 080004 2000 2040',
["19.SpecEv1"] = '0294 b2 01 041d', ['19.SpecEv1'] = '0294 b2 01 041d',
} }
--- ---
@ -218,7 +224,8 @@ function main(args)
local data, apdu, flags, uid, cid, result, err, card local data, apdu, flags, uid, cid, result, err, card
-- Read the parameters -- Read the parameters
for o, a in getopt.getopt(args, 'h') do for o, a in getopt.getopt(args, 'h') do
if o == "h" then return help() end if o == 'h' then return help() end
if o == 'b' then bytes = a end
end end
calypso_switch_on_field() calypso_switch_on_field()
@ -248,7 +255,7 @@ function main(args)
--result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file --result, err = calypso_send_cmd_raw('0294a40800043f000002',false) --select ICC file
for i, apdu in spairs(_calypso_cmds) do for i, apdu in spairs(_calypso_cmds) do
print('>>', i ) print('>>', i )
apdu = apdu:gsub("%s+","") apdu = apdu:gsub('%s+', '')
result, err = calypso_send_cmd_raw(apdu , false) result, err = calypso_send_cmd_raw(apdu , false)
if result then if result then
calypso_apdu_status(result.data) calypso_apdu_status(result.data)
@ -263,12 +270,12 @@ end
-- a simple selftest function, tries to convert -- a simple selftest function, tries to convert
function selftest() function selftest()
DEBUG = true DEBUG = true
dbg("Performing test") dbg('Performing test')
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

View file

@ -1,12 +1,16 @@
--[[
A sampe script file on how to implement at cmd line inteface.
--]]
print("This is how a cmd-line interface could be implemented\nPrint 'exit' to exit.\n") print("This is how a cmd-line interface could be implemented\nPrint 'exit' to exit.\n")
local answer local answer
repeat repeat
io.write("$>") io.write("$>")
io.flush() io.flush()
answer=io.read() answer = io.read()
if answer ~= 'exit' then if answer ~= 'exit' then
local func = assert(loadstring("return " .. answer)) local func = assert(loadstring("return " .. answer))
io.write("\n"..tostring(func() or "").."\n"); io.write("\n"..tostring(func() or "").."\n");
end--]] end--]]
until answer=="exit" until answer == "exit"
print("Bye\n"); print("Bye\n");