From b64fdafc4038fcdca783b2e2262acd2ebf87f0dc Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 29 Nov 2018 17:53:24 +0100 Subject: [PATCH] ADD: basic iso15693 comms in lua support. Not fully functional --- client/lualibs/read15.lua | 93 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 client/lualibs/read15.lua diff --git a/client/lualibs/read15.lua b/client/lualibs/read15.lua new file mode 100644 index 000000000..b337d22d7 --- /dev/null +++ b/client/lualibs/read15.lua @@ -0,0 +1,93 @@ +--[[ + This is a library to read 15693 tags. It can be used something like this + + local reader = require('read15') + result, err = reader.read15693() + if not result then + print(err) + return + end + print(result.name) + +--]] +-- Loads the commands-library +local cmds = require('commands') +local TIMEOUT = 2000 -- Shouldn't take longer than 2 seconds + + +--- Sends a USBpacket to the device +-- @param command - the usb packet to send +-- @param ignoreresponse - if set to true, we don't read the device answer packet +-- which is usually recipe for fail. If not sent, the host will wait 2s for a +-- response of type CMD_ACK +-- @return packet,nil if successfull +-- nil, errormessage if unsuccessfull +local function sendToDevice(command, ignoreresponse) + local err = core.SendCommand(command:getBytes()) + if err then + print(err) + return nil, err + end + if ignoreresponse then return nil,nil end + + local response = core.WaitForResponseTimeout(cmds.CMD_ACK, TIMEOUT) + return response,nil +end + +-- This function does a connect and retrieves som einfo +-- @param dont_disconnect - if true, does not disable the field +-- @return if successfull: an table containing card info +-- @return if unsuccessfull : nil, error +local function read15693(slow, dont_readresponse) + local command, result, info, err, data + + command = Command:new{cmd = cmds.CMD_ISO_15693_COMMAND, arg1 = 0, arg2 = 1, arg3 = 1 } + + if slow then + command.arg2 = 0 + end + if dont_readresponse then + command.arg3 = 0 + end + + local result, err = sendToDevice(command, dont_readresponse) + if result then + local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) + if arg0 == 0 then + return nil, "iso15693 no bytes returned" + end + data = string.sub(result, count) + info, err = bin.unpack('H', data) + print("LEN", arg0, data ) + else + err = "No response from card" + end + + if err then + print(err) + return nil, err + end + return info +end + +--- +-- Waits for a mifare card to be placed within the vicinity of the reader. +-- @return if successfull: an table containing card info +-- @return if unsuccessfull : nil, error +local function waitFor15693() + print("Waiting for card... press any key to quit") + while not core.ukbhit() do + res, err = read15693() + if res then return res end + -- err means that there was no response from card + end + return nil, "Aborted by user" +end +local library = { + read = read15693, + waitFor15693 = waitFor15693, +-- parse15693 = parse15693, + sendToDevice = sendToDevice, +} + +return library