2018-11-30 00:53:24 +08:00
|
|
|
|
--[[
|
2019-03-09 17:40:45 +08:00
|
|
|
|
This is a library to read 15693 tags. It can be used something like this
|
2018-11-30 00:53:24 +08:00
|
|
|
|
|
2019-03-09 17:40:45 +08:00
|
|
|
|
local reader = require('read15')
|
|
|
|
|
local info, err = reader.read()
|
|
|
|
|
if not info then
|
|
|
|
|
print(err)
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
print(info.UID)
|
2018-11-30 00:53:24 +08:00
|
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
-- Loads the commands-library
|
|
|
|
|
local cmds = require('commands')
|
2018-11-30 08:41:39 +08:00
|
|
|
|
local utils = require('utils')
|
2019-03-09 17:40:45 +08:00
|
|
|
|
|
2019-05-08 04:46:00 +08:00
|
|
|
|
-- Shouldn't take longer than 2 seconds
|
|
|
|
|
local TIMEOUT = 2000
|
|
|
|
|
|
|
|
|
|
local ISO15_COMMAND = {
|
|
|
|
|
ISO15_REQ_SUBCARRIER_SINGLE = 0,
|
|
|
|
|
ISO15_REQ_DATARATE_HIGH = 2,
|
|
|
|
|
ISO15_REQ_NONINVENTORY = 0,
|
|
|
|
|
}
|
2018-11-30 00:53:24 +08:00
|
|
|
|
|
2018-11-30 08:41:39 +08:00
|
|
|
|
local function errorString15693(number)
|
2019-03-09 17:40:45 +08:00
|
|
|
|
local errors = {}
|
|
|
|
|
errors[0x01] = "The command is not supported"
|
|
|
|
|
errors[0x02] = "The command is not recognised"
|
|
|
|
|
errors[0x03] = "The option is not supported."
|
|
|
|
|
errors[0x0f] = "Unknown error."
|
|
|
|
|
errors[0x10] = "The specified block is not available (doesn’t exist)."
|
|
|
|
|
errors[0x11] = "The specified block is already -locked and thus cannot be locked again"
|
|
|
|
|
errors[0x12] = "The specified block is locked and its content cannot be changed."
|
|
|
|
|
errors[0x13] = "The specified block was not successfully programmed."
|
|
|
|
|
errors[0x14] = "The specified block was not successfully locked."
|
|
|
|
|
|
|
|
|
|
return errors[number] or "Reserved for Future Use or Custom command error."
|
2018-11-30 08:41:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function parse15693(data)
|
2019-03-09 17:40:45 +08:00
|
|
|
|
local bytes = utils.ConvertAsciiToBytes(data)
|
|
|
|
|
local tmp = utils.ConvertAsciiToHex(data)
|
|
|
|
|
|
|
|
|
|
-- define ISO15_CRC_CHECK 0F47
|
|
|
|
|
local crcStr = utils.Crc15(tmp, #tmp)
|
|
|
|
|
|
|
|
|
|
if string.sub(crcStr, #crcStr - 3) ~= '470F' then
|
2019-05-08 04:46:00 +08:00
|
|
|
|
print('CRC', crc )
|
|
|
|
|
return nil, 'CRC failed'
|
2019-03-09 17:40:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if bytes[1] % 2 == 1 then
|
|
|
|
|
-- Above is a poor-mans bit check:
|
|
|
|
|
-- recv[0] & ISO15_RES_ERROR //(0x01)
|
2019-05-08 04:46:00 +08:00
|
|
|
|
local err = 'Tag returned error %i: %s'
|
2019-03-09 17:40:45 +08:00
|
|
|
|
err = string.format(err, bytes[1], errorString15693(bytes[1]))
|
|
|
|
|
return nil, err
|
|
|
|
|
end
|
|
|
|
|
local uid = utils.ConvertBytesToHex( bytes, true )
|
|
|
|
|
uid = uid:sub(5, #uid-4)
|
|
|
|
|
return { uid = uid, }
|
2018-11-30 08:41:39 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-12-01 03:58:47 +08:00
|
|
|
|
-- This function does a connect and retrieves som info
|
2018-11-30 00:53:24 +08:00
|
|
|
|
-- @param dont_disconnect - if true, does not disable the field
|
2019-09-14 23:44:58 +08:00
|
|
|
|
-- @return if successful: an table containing card info
|
|
|
|
|
-- @return if unsuccessful : nil, error
|
2018-11-30 00:53:24 +08:00
|
|
|
|
local function read15693(slow, dont_readresponse)
|
2018-11-30 08:41:39 +08:00
|
|
|
|
|
|
|
|
|
--[[
|
2019-03-09 17:40:45 +08:00
|
|
|
|
We start by trying this command:
|
|
|
|
|
MANDATORY (present in ALL iso15693 tags) command (the example below is sent to a tag different from the above one):
|
|
|
|
|
|
|
|
|
|
pm3> hf 15 info u
|
|
|
|
|
UID=E007C1A257394244
|
|
|
|
|
Tag Info: Texas Instrument; Tag-it HF-I Standard; 8x32bit
|
|
|
|
|
pm3>
|
|
|
|
|
|
|
|
|
|
From which we obtain less information than the above one.
|
|
|
|
|
|
|
|
|
|
"260100" means
|
|
|
|
|
0x26
|
|
|
|
|
-- #define ISO15_REQ_SUBCARRIER_SINGLE 0x00 // Tag should respond using one subcarrier (ASK)
|
|
|
|
|
-- #define ISO15_REQ_DATARATE_HIGH 0x02 // Tag should respond using high data rate
|
|
|
|
|
-- #define ISO15_REQ_NONINVENTORY 0x00
|
|
|
|
|
0x01
|
|
|
|
|
inventory
|
|
|
|
|
0x00
|
|
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
|
|
local command, result, info, err, data
|
|
|
|
|
|
|
|
|
|
data = utils.Crc15("260100")
|
|
|
|
|
|
2019-05-08 04:46:00 +08:00
|
|
|
|
command = Command:newMIX{
|
2019-08-04 01:17:00 +08:00
|
|
|
|
cmd = cmds.CMD_HF_ISO15693_COMMAND,
|
2019-05-08 04:46:00 +08:00
|
|
|
|
arg1 = #data / 2,
|
|
|
|
|
arg2 = 1,
|
|
|
|
|
arg3 = 1,
|
|
|
|
|
data = data
|
|
|
|
|
}
|
2019-03-09 17:40:45 +08:00
|
|
|
|
|
|
|
|
|
if slow then
|
|
|
|
|
command.arg2 = 0
|
|
|
|
|
end
|
|
|
|
|
if dont_readresponse then
|
|
|
|
|
command.arg3 = 0
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-08 04:46:00 +08:00
|
|
|
|
local result, err = command:sendMIX()
|
|
|
|
|
if result then
|
|
|
|
|
local count, cmd, len, arg2, arg3 = bin.unpack('LLLL', result)
|
|
|
|
|
if len == 0 then
|
|
|
|
|
return nil, 'iso15693 card select failed'
|
|
|
|
|
end
|
2019-03-09 17:40:45 +08:00
|
|
|
|
data = string.sub(result, count, count+len-1)
|
|
|
|
|
info, err = parse15693(data)
|
|
|
|
|
else
|
2019-05-08 04:46:00 +08:00
|
|
|
|
err = 'No response from card'
|
|
|
|
|
end
|
2019-05-08 07:35:51 +08:00
|
|
|
|
|
2019-05-08 04:46:00 +08:00
|
|
|
|
if err then
|
|
|
|
|
print(err)
|
|
|
|
|
return nil, err
|
2019-03-09 17:40:45 +08:00
|
|
|
|
end
|
2019-05-08 04:46:00 +08:00
|
|
|
|
return info
|
2018-11-30 00:53:24 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---
|
2018-12-01 03:58:47 +08:00
|
|
|
|
-- Waits for a ISO15693 card to be placed within the vicinity of the reader.
|
2019-09-14 23:44:58 +08:00
|
|
|
|
-- @return if successful: an table containing card info
|
|
|
|
|
-- @return if unsuccessful : nil, error
|
2018-11-30 00:53:24 +08:00
|
|
|
|
local function waitFor15693()
|
2019-07-11 19:01:34 +08:00
|
|
|
|
print('Waiting for card... press Enter to quit')
|
|
|
|
|
while not core.kbd_enter_pressed() do
|
2019-03-09 17:40:45 +08:00
|
|
|
|
res, err = read15693()
|
|
|
|
|
if res then return res end
|
|
|
|
|
-- err means that there was no response from card
|
|
|
|
|
end
|
2019-05-08 04:46:00 +08:00
|
|
|
|
return nil, 'Aborted by user'
|
2018-11-30 00:53:24 +08:00
|
|
|
|
end
|
2019-05-08 04:46:00 +08:00
|
|
|
|
|
|
|
|
|
-- Sends an instruction to do nothing, only disconnect
|
|
|
|
|
local function disconnect15693()
|
2019-08-04 01:17:00 +08:00
|
|
|
|
local c = Command:newMIX{cmd = cmds.CMD_HF_ISO15693_COMMAND}
|
2019-05-08 04:46:00 +08:00
|
|
|
|
-- We can ignore the response here, no ACK is returned for this command
|
|
|
|
|
-- Check /armsrc/iso14443a.c, ReaderIso14443a() for details
|
2019-06-23 20:13:17 +08:00
|
|
|
|
return c:sendMIX(true)
|
2019-05-08 04:46:00 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-30 00:53:24 +08:00
|
|
|
|
local library = {
|
2019-03-09 17:40:45 +08:00
|
|
|
|
read = read15693,
|
|
|
|
|
waitFor15693 = waitFor15693,
|
|
|
|
|
parse15693 = parse15693,
|
2019-05-08 04:46:00 +08:00
|
|
|
|
disconnect = disconnect15693,
|
2018-11-30 00:53:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return library
|