From edfbfa49c810ebb6a3f4ed705eef4f3dba291f35 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 30 Dec 2021 22:34:57 +0100 Subject: sensors: init is now idempotent --- files/common/openwrt/sensors.module_lua | 52 +++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'files/common') diff --git a/files/common/openwrt/sensors.module_lua b/files/common/openwrt/sensors.module_lua index 8a209dce..6c279188 100644 --- a/files/common/openwrt/sensors.module_lua +++ b/files/common/openwrt/sensors.module_lua @@ -5,6 +5,7 @@ local io = require("io") local string = require("string") local time = require "posix.time" local unistd = require "posix.unistd" +local sys_stat = require "posix.sys.stat" local _M = {} local _internal_ = {} @@ -58,6 +59,29 @@ 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_add_or_replace_device(bus, address, name) + local s, _ = sys_stat.stat(_M.i2c_device_path(bus, address)) + if s == nil then + local ret, err = _M.i2c_add_device(bus, address, name) + if not ret then return nil, err end + return true, nil + end + + local contents, _ = _internal_.read_from_file(_M.i2c_device_path(bus, address) .. "/name") + if contents == nil or contents:gsub("%s+", "") ~= name then + local ret, err = _M.i2c_delete_device(bus, address) + if not ret then return nil, err end + + time.nanosleep({tv_sec = 0, tv_nsec = 100000000}) + + ret, err = _M.i2c_add_device(bus, address, name) + if not ret then return nil, err end + return true, nil + end + + return false, nil +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 @@ -102,7 +126,7 @@ function _M.i2c_get_mux_channels(parent, address) end -function _M.i2c_add_devices_recursive(bus_num, bus_name, devices) +function _M.i2c_setup_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) @@ -112,12 +136,12 @@ function _M.i2c_add_devices_recursive(bus_num, bus_name, devices) local num_sensors = 0 local device for _, device in base.ipairs(devices) do - local ret, err = _M.i2c_add_device(bus_num, device.address, device.type) - if not ret then return nil, err end + local changed, err = _M.i2c_add_or_replace_device(bus_num, device.address, device.type) + if changed == nil 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}) + if changed then time.nanosleep({tv_sec = 0, tv_nsec = 100000000}) end local ret, err = setup_function(bus_num, device.address) if ret == nil then return nil, err end num_sensors = num_sensors + ret @@ -130,8 +154,8 @@ function _M.i2c_add_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_add_devices_recursive(mux_channels[channel.number], nil, channel.devices) - if ret == nil then return nil, err end + local tmp, err = _M.i2c_setup_devices_recursive(mux_channels[channel.number], nil, channel.devices) + if tmp == nil then return nil, err end num_sensors = num_sensors + tmp end end @@ -246,6 +270,16 @@ function _M.gpio_unexport_pin(number) return _internal_.write_to_file("/sys/class/gpio/unexport", string.format("%d", number)) end +function _M.gpio_setup_pin(number) + local s, _ = sys_stat.stat(_M.gpio_pin_path(number)) + if s == nil then + local ret, err = _M.gpio_export_pin(number) + if not ret then return nil, err end + return true, nil + end + return false, nil +end + function _M.gpio_read_pin(number) local value, err = _internal_.read_from_file(_M.gpio_pin_path(number) .. "/value") if not value then return nil, err end @@ -257,8 +291,8 @@ function _M.gpio_setup_pins(pins) local num_pins = 0 local pin for _, pin in base.ipairs(pins) do - local ret, err = _M.gpio_export_pin(pin.number) - if ret == nil then return nil, err end + local changed, err = _M.gpio_setup_pin(pin.number) + if changed == nil then return nil, err end num_pins = num_pins + 1 end @@ -458,7 +492,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.name, i2c_bus.devices) + local tmp, err = _M.i2c_setup_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 -- cgit v1.2.3