From abc66484e5838e72d1746fcc8bb3224fe5e6781a Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 25 May 2019 09:06:44 -0400 Subject: [PATCH] fix: 14a raw commands - running lua scripts works again for MIX/NG frames --- client/lualibs/commands.lua | 41 ++++++++++++++----------------------- client/lualibs/read14a.lua | 6 +++--- client/scripting.c | 38 ++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/client/lualibs/commands.lua b/client/lualibs/commands.lua index 59825d2ca..5b94cf2de 100644 --- a/client/lualibs/commands.lua +++ b/client/lualibs/commands.lua @@ -142,33 +142,18 @@ function Command:sendMIX( ignore_response, timeout ) end -- lets digest - local data - local count, cmd, length, magic, status, crc, arg1, arg2, arg3 = bin.unpack('SSIsSLLL', response) + data = nil + cmd = nil + arg1 = nil + arg2 = nil + arg3 = nil + local count, length, magic, status, crc, ng + +-- S S I s S L L L + count, cmd, length, magic, status, crc, arg1, arg2, arg3 = bin.unpack('SSIsSLLL', response) count, data, ng = bin.unpack('H'..length..'C', response, count) ---[[ uncomment if you want to debug - self.resp_cmd = cmd - self.resp_length = length - self.resp_magic = magic - self.resp_status = status - self.resp_crc = crc - self.resp_arg1 = arg1 - self.resp_arg2 = arg2 - self.resp_arg3 = arg3 - self.resp_data = data - self.resp_ng = ng - self:__responsetostring() ---]] - local packed = bin.pack("LLLLH", cmd, arg1, arg2, arg3, data) - --[[ - return { Cmd = cmd, - Arg1 = arg1, - Arg2 = arg2, - Arg3 = arg3, - Data = data, - } - --]] return packed, nil; end function Command:sendNG( ignore_response, timeout ) @@ -185,8 +170,12 @@ function Command:sendNG( ignore_response, timeout ) if response == nil then return nil, 'Error, waiting for response timed out :: '..msg end - local data - local count, cmd, length, magic, status, crc, arg0, arg1, arg2 = bin.unpack('SSIsSLLL', response) + + data = nil + cmd = nil + local count, length, magic, status, crc, arg0, arg1, arg2 + + count, cmd, length, magic, status, crc, arg0, arg1, arg2 = bin.unpack('SSIsSLLL', response) count, data, ng = bin.unpack('H'..length..'C', response, count) --[[ uncomment if you want to debug diff --git a/client/lualibs/read14a.lua b/client/lualibs/read14a.lua index 6fe7f8bd1..fbb92cbd5 100644 --- a/client/lualibs/read14a.lua +++ b/client/lualibs/read14a.lua @@ -63,7 +63,7 @@ local function parse14443a(data) } __attribute__((__packed__)) iso14a_card_select_t; --]] - local count, uid, uidlen, atqa, sak, ats_len, ats = bin.unpack('H10CH2CC',data) + local count, uid, uidlen, atqa, sak, ats_len, ats = bin.unpack('H10CH2CC', data) uid = uid:sub(1, 2 * uidlen) local man_byte = tonumber(uid:sub(1,2), 16) @@ -98,8 +98,8 @@ local function read14443a(dont_disconnect, no_rats) local result, err = command:sendMIX() if result then - local count,cmd,arg0,arg1,arg2 = bin.unpack('LLLL',result) - if arg0 == 0 then + local count, cmd, arg1, arg2, arg3 = bin.unpack('LLLL',result) + if arg1 == 0 then return nil, 'iso14443a card select failed' end data = string.sub(result, count) diff --git a/client/scripting.c b/client/scripting.c index 87de2c088..19914c258 100644 --- a/client/scripting.c +++ b/client/scripting.c @@ -303,11 +303,45 @@ static int l_WaitForResponseTimeout(lua_State *L) { ms_timeout = luaL_checkunsigned(L, 2); PacketResponseNG resp; - if (WaitForResponseTimeout(cmd, &resp, ms_timeout) == 0) + if (WaitForResponseTimeout(cmd, &resp, ms_timeout) == false) return returnToLuaWithError(L, "No response from the device"); + char foo[sizeof(PacketResponseNG)]; + n = 0; + + memcpy(foo + n, &resp.cmd, sizeof(resp.cmd)); + n += sizeof(resp.cmd); + + memcpy(foo + n, &resp.length, sizeof(resp.length)); + n += sizeof(resp.length); + + memcpy(foo + n, &resp.magic, sizeof(resp.magic)); + n += sizeof(resp.magic); + + memcpy(foo + n, &resp.status, sizeof(resp.status)); + n += sizeof(resp.status); + + memcpy(foo + n, &resp.crc, sizeof(resp.crc)); + n += sizeof(resp.crc); + + memcpy(foo + n, &resp.oldarg[0], sizeof(resp.oldarg[0])); + n += sizeof(resp.oldarg[0]); + + memcpy(foo + n, &resp.oldarg[1], sizeof(resp.oldarg[1])); + n += sizeof(resp.oldarg[1]); + + memcpy(foo + n, &resp.oldarg[2], sizeof(resp.oldarg[2])); + n += sizeof(resp.oldarg[2]); + + memcpy(foo + n, resp.data.asBytes, sizeof(resp.data)); + n += sizeof(resp.data); + + memcpy(foo + n, &resp.ng, sizeof(resp.ng)); + n += sizeof(resp.ng); + //Push it as a string - lua_pushlstring(L, (const char *)&resp, sizeof(PacketResponseNG)); + lua_pushlstring(L, (const char *)&foo, sizeof(foo)); + return 1; }