+---
+-- Usage help
+function help()
+ print(desc)
+ print("Example usage")
+ print(example)
+end
+
+---
+-- The main entry point
+function main(args)
+
+ if args == nil or #args == 0 then
+ return help()
+ end
+
+ local ignore_response = false
+ local appendcrc = false
+ local stayconnected = false
+ local payload = nil
+ local doconnect = true
+
+ -- Read the parameters
+ for o, a in getopt.getopt(args, 'corcpx:') do
+ if o == "o" then doconnect = false end
+ if o == "r" then ignore_response = true end
+ if o == "c" then appendcrc = true end
+ if o == "p" then stayconnected = true end
+ if o == "x" then payload = a end
+ if o == "d" then DEBUG = true end
+ end
+
+ -- First of all, connect
+ if doconnect then
+ dbg("doconnect")
+ -- We reuse the connect functionality from a
+ -- common library
+ info, err = lib14a.read1443a(true)
+
+ if err then return oops(err) end
+ print(("Connected to card, uid = %s"):format(info.uid))
+ end
+
+ -- The actual raw payload, if any
+ if payload then
+ res,err = sendRaw(payload,{ignore_response = ignore_response})
+ if err then return oops(err) end
+
+ if not ignoreresponse then
+ -- Display the returned data
+ showdata(res)
+ end
+ end
+ -- And, perhaps disconnect?
+ if not stayconnected then
+ disconnect()
+ end
+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
+function showdata(usbpacket)
+ local cmd_response = Command.parse(usbpacket)
+ local len = tonumber(cmd_response.arg1) *2
+ --print("data length:",len)
+ local data = string.sub(tostring(cmd_response.data), 0, len);
+ print("<< ",data)
+ --print("----------------")
+end
+
+
+
+function sendRaw(rawdata, options)
+ print(">> ", rawdata)
+
+ local flags = lib14a.ISO14A_COMMAND.ISO14A_NO_DISCONNECT + lib14a.ISO14A_COMMAND.ISO14A_RAW
+
+ local command = Command:new{cmd = cmds.CMD_READER_ISO_14443a,
+ arg1 = flags, -- Send raw
+ -- arg2 contains the length, which is half the length
+ -- of the ASCII-string rawdata