summaryrefslogtreecommitdiff
path: root/files
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2022-01-04 14:37:50 +0100
committerChristian Pointner <equinox@spreadspace.org>2022-01-04 14:37:50 +0100
commit3bce73818e00e58f01d8394194c6850ce2363da6 (patch)
treec9b5162219b87beb02bc2f864fd5dde9c3989db4 /files
parentprometheus/node/sensors: fix duplicate type string in promehteus output (diff)
sensor: i2c bus detection using regex for name
Diffstat (limited to 'files')
-rw-r--r--files/common/openwrt/sensors.module_lua32
1 files changed, 21 insertions, 11 deletions
diff --git a/files/common/openwrt/sensors.module_lua b/files/common/openwrt/sensors.module_lua
index d5d869d5..4f3ecc23 100644
--- a/files/common/openwrt/sensors.module_lua
+++ b/files/common/openwrt/sensors.module_lua
@@ -96,7 +96,11 @@ function _M.i2c_add_or_replace_device(bus, address, name)
return false, nil
end
-function _M.i2c_get_bus_number_from_name(name)
+function _M.i2c_get_bus_number_from_name(name, name_regex)
+ if name == nil and name_regex == nil then
+ return nil, "unable to find i2c bus: please specifiy either 'number', 'name' or 'name_regex'"
+ end
+
local bus_paths, glob_result = _internal_.glob("/sys/bus/i2c/devices/i2c-*", 0)
if not bus_paths then
if glob_result == glob.GLOB_NOMATCH then return {} end
@@ -110,9 +114,15 @@ function _M.i2c_get_bus_number_from_name(name)
if not num then return nil, "unable to parse bus number from path: " .. bus_path end
local contents, err = _internal_.read_line_from_file(bus_path .. "/name")
- if contents ~= nil and contents == name then return num end
+ if contents ~= nil then
+ if contents == name then return num end
+ if name_regex ~= nil and contents:match(name_regex) then return num end
+ end
+ end
+ if name ~= nil then
+ return nil, "unable to find i2c bus with name: " .. name
end
- return nil, "unable to find i2c bus with name: " .. name
+ return nil, "unable to find i2c bus with name matching: " .. name_regex
end
function _M.i2c_get_mux_channels(parent, address)
@@ -140,10 +150,10 @@ function _M.i2c_get_mux_channels(parent, address)
end
-function _M.i2c_setup_devices_recursive(bus_num, bus_name, devices)
+function _M.i2c_setup_devices_recursive(bus_num, bus_name, bus_name_regex, devices)
if bus_num == nil then
local err
- bus_num, err = _M.i2c_get_bus_number_from_name(bus_name)
+ bus_num, err = _M.i2c_get_bus_number_from_name(bus_name, bus_name_regex)
if bus_num == nil then return nil, err end
end
@@ -168,7 +178,7 @@ function _M.i2c_setup_devices_recursive(bus_num, bus_name, devices)
local channel
for _, channel in base.ipairs(device.channels) do
if not mux_channels[channel.number] then return nil, string.format("i2c-mux %s has no channel %d", _M.i2c_device_name(bus_num, device.address), channel.number) end
- local tmp, err = _M.i2c_setup_devices_recursive(mux_channels[channel.number], nil, channel.devices)
+ local tmp, err = _M.i2c_setup_devices_recursive(mux_channels[channel.number], nil, nil, channel.devices)
if tmp == nil then return nil, err end
num_sensors = num_sensors + tmp
end
@@ -178,11 +188,11 @@ function _M.i2c_setup_devices_recursive(bus_num, bus_name, devices)
return num_sensors
end
-function _M.i2c_read_sensors_recursive(bus_num, bus_name, devices)
+function _M.i2c_read_sensors_recursive(bus_num, bus_name, bus_name_regex, devices)
local readings = {}
if bus_num == nil then
local err
- bus_num, err = _M.i2c_get_bus_number_from_name(bus_name)
+ bus_num, err = _M.i2c_get_bus_number_from_name(bus_name, bus_name_regex)
if bus_num == nil then return readings end
end
@@ -204,7 +214,7 @@ function _M.i2c_read_sensors_recursive(bus_num, bus_name, devices)
local channel
for _, channel in base.ipairs(device.channels) do
if mux_channels[channel.number] then
- local tmp, err = _M.i2c_read_sensors_recursive(mux_channels[channel.number], nil, channel.devices)
+ local tmp, err = _M.i2c_read_sensors_recursive(mux_channels[channel.number], nil, nil, channel.devices)
if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end
end
end
@@ -506,7 +516,7 @@ function _M.setup(config)
if config.i2c then
local i2c_bus
for _, i2c_bus in base.ipairs(config.i2c) do
- local tmp, err = _M.i2c_setup_devices_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.devices)
+ local tmp, err = _M.i2c_setup_devices_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.name_regex, i2c_bus.devices)
if tmp == nil then return nil, err end
num_sensors = num_sensors + tmp
end
@@ -530,7 +540,7 @@ function _M.read(config)
if config.i2c then
local i2c_bus
for _, i2c_bus in base.ipairs(config.i2c) do
- local tmp, err = _M.i2c_read_sensors_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.devices)
+ local tmp, err = _M.i2c_read_sensors_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.name_regex, i2c_bus.devices)
if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end
end
end