-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathebm-read.lua
55 lines (45 loc) · 1.18 KB
/
ebm-read.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env lua
-- EBM Single Shot Query
-- Copyright (C) 2023, coreMem Limited <[email protected]>
-- SPDX-License-Identifier: AGPL-3.0-only
-- https://gist.github.com/yi/01e3ab762838d567e65d
function string.tohex (str)
return (str:gsub(".", function (c)
return string.format("%02X", string.byte(c))
end)):lower()
end
function string.print (str)
return (str:gsub(".", function (c)
local b = string.byte(c)
return (b >= 0x20 and b < 0x80) and c or "."
end))
end
local dir = arg[0]:match("^(.-/?)[^/]+.lua$")
local status, ebm = pcall(function () return require "ebm" end)
if not status then
ebm = assert(loadfile(dir .. "ebm.lua"))()
end
if #arg < 3 then
io.stderr:write("Usage: " .. arg[0] .. " IFACE MACADDR REG ...\n")
os.exit(1)
end
local ebm_session = ebm:session({iface=arg[1], addr=arg[2]})
if not ebm_session then
error(err)
end
local regs = {unpack(arg, 3)}
for i, v in ipairs(regs) do
local n = tonumber(v)
if n then
regs[i] = n
end
end
local status, result = ebm_session:read(regs)
if not status then
error(result)
end
ebm_session:close()
print("reg", "hex", "int", "str")
for i, v in ipairs(result.data) do
print(arg[2 + i], v.raw:tohex(), v.int, v.raw:print())
end