From 6141ae149267bbcdd9a0889c216c1beda056f7c1 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 30 Dec 2021 17:52:39 +0100 Subject: sensors: find i2c bus numbers by name --- files/common/openwrt/sensors.module_lua | 57 +++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 13 deletions(-) (limited to 'files/common/openwrt/sensors.module_lua') diff --git a/files/common/openwrt/sensors.module_lua b/files/common/openwrt/sensors.module_lua index dbae81ea..8a209dce 100644 --- a/files/common/openwrt/sensors.module_lua +++ b/files/common/openwrt/sensors.module_lua @@ -58,6 +58,25 @@ function _M.i2c_delete_device(bus, address) return _internal_.write_to_file(_M.i2c_bus_path(bus) .. "/delete_device", string.format("0x%02x", address)) end +function _M.i2c_get_bus_number_from_name(name) + local bus_paths, glob_result = glob.glob("/sys/bus/i2c/devices/i2c-*", 0) + if not bus_paths then + if glob_result == glob.GLOB_NOMATCH then return {} end + if glob_result == glob.GLOB_ABORTED then return nil, "glob(): aborted" end + if glob_result == glob.GLOB_NOSPACE then return nil, "glob(): no space" end + return nil, "glob(): unknown error" + end + + for _, bus_path in base.pairs(bus_paths) do + local num = string.match(bus_path, '/i2c%-(%d+)$') + if not num then return nil, "unable to parse bus number from path: " .. bus_path end + + local contents, err = _internal_.read_from_file(bus_path .. "/name") + if contents ~= nil and contents:gsub("%s+", "") == name then return num end + end + return nil, "unable to find i2c bus with name: " .. name +end + function _M.i2c_get_mux_channels(parent, address) local channel_paths, glob_result = glob.glob(_M.i2c_device_path(parent, address) .. "/channel-*", 0) if not channel_paths then @@ -83,29 +102,35 @@ function _M.i2c_get_mux_channels(parent, address) end -function _M.i2c_add_devices_recursive(bus, devices) +function _M.i2c_add_devices_recursive(bus_num, bus_name, devices) + if bus_num == nil then + local err + bus_num, err = _M.i2c_get_bus_number_from_name(bus_name) + if bus_num == nil then return nil, err end + end + local num_sensors = 0 local device for _, device in base.ipairs(devices) do - local ret, err = _M.i2c_add_device(bus, device.address, device.type) + local ret, err = _M.i2c_add_device(bus_num, device.address, device.type) if not ret then return nil, err end local setup_function = _internal_.setup[device.type] if setup_function ~= nil then time.nanosleep({tv_sec = 0, tv_nsec = 100000000}) - local ret, err = setup_function(bus, device.address) + local ret, err = setup_function(bus_num, device.address) if ret == nil then return nil, err end num_sensors = num_sensors + ret end if device.channels then time.nanosleep({tv_sec = 0, tv_nsec = 100000000}) - local mux_channels, err = _M.i2c_get_mux_channels(bus, device.address) + local mux_channels, err = _M.i2c_get_mux_channels(bus_num, device.address) if not mux_channels then return nil, error end 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, device.address), channel.number) end - local tmp, err = _M.i2c_add_devices_recursive(mux_channels[channel.number], channel.devices) + 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_add_devices_recursive(mux_channels[channel.number], nil, channel.devices) if ret == nil then return nil, err end num_sensors = num_sensors + tmp end @@ -115,14 +140,20 @@ function _M.i2c_add_devices_recursive(bus, devices) return num_sensors end -function _M.i2c_read_sensors_recursive(bus, devices) +function _M.i2c_read_sensors_recursive(bus_num, bus_name, devices) local readings = {} + if bus_num == nil then + local err + bus_num, err = _M.i2c_get_bus_number_from_name(bus_name) + if bus_num == nil then return readings end + end + local device for _, device in base.ipairs(devices) do - local name = device.name ~= nil and device.name or device.type .. "_" .. _M.i2c_device_name(bus, device.address) + local name = device.name ~= nil and device.name or device.type .. "_" .. _M.i2c_device_name(bus_num, device.address) local read_function = _internal_.read[device.type] if read_function ~= nil then - local values, kind = read_function(bus, device.address) + local values, kind = read_function(bus_num, device.address) if values ~= nil then readings[name] = values readings[name]['_kind_'] = kind @@ -130,12 +161,12 @@ function _M.i2c_read_sensors_recursive(bus, devices) end if device.channels then - local mux_channels, err = _M.i2c_get_mux_channels(bus, device.address) + local mux_channels, err = _M.i2c_get_mux_channels(bus_num, device.address) if not mux_channels then return nil, error end 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], channel.devices) + local tmp, err = _M.i2c_read_sensors_recursive(mux_channels[channel.number], nil, channel.devices) if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end end end @@ -427,7 +458,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_add_devices_recursive(i2c_bus.number, i2c_bus.devices) + local tmp, err = _M.i2c_add_devices_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.devices) if tmp == nil then return nil, err end num_sensors = num_sensors + tmp end @@ -451,7 +482,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.devices) + local tmp, err = _M.i2c_read_sensors_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.devices) if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end end end -- cgit v1.2.3