summaryrefslogtreecommitdiff
path: root/files/common/openwrt/sensors.module_lua
blob: f6c3e38c3cf848f97297f5878b8f24839a8ae093 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local base = _G
local io = require("io")
local string = require("string")
local cjson = require "cjson.safe"
local glob = require "posix.glob"
local time = require "posix.time"
local unistd = require "posix.unistd"

local _M = {}
local _internal_ = {}
_internal_.setup = {}

-- ############# i2c ###########

function _internal_.write_to_file(path, data)
    -- base.print(string.format("writing '%s' to '%s'", data, path))
    local f, err = io.open(path, "w")
    if not f then return nil, err end

    ret, err = f:write(data)
    f:close()
    return ret, err
end

function _internal_.read_from_file(path)
    local f, err = io.open(path, "r")
    if not f then return nil, err end

    data, err = f:read("*a")
    f:close()
    return data, err
end

-- ############# i2c ###########

function _M.i2c_bus_path(bus)
    return string.format("/sys/bus/i2c/devices/i2c-%d", bus)
end

function _M.i2c_device_name(bus, address)
    return string.format("%d-%04X", bus, address)
end

function _M.i2c_device_path(bus, address)
    return string.format("/sys/bus/i2c/devices/" .. _M.i2c_device_name(bus, address))
end

function _M.i2c_add_device(bus, address, name)
    return _internal_.write_to_file(_M.i2c_bus_path(bus) .. "/new_device", string.format("%s 0x%02X", name, address))
end

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_mux_channels(parent, address)
    local channel_paths, glob_result = glob.glob(_M.i2c_device_path(parent, address) .. "/channel-*", 0)
    if not channel_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

    local channels = {}
    for _, channel_path in base.pairs(channel_paths) do
        local channel = string.match(channel_path, '/channel%-(%d+)$')
        if not channel then return nil, "unable to parse channel number from path: " .. channel_path end

        local bus_path, err = unistd.readlink(channel_path)
        if not bus_path then return nil, err end
        local bus = string.match(bus_path, '/i2c%-(%d+)$')
        if not bus then return nil, "unable to parse bus number from path: " .. bus_path end

        channels[tonumber(channel)] = tonumber(bus)
    end
    return channels
end

function _M.i2c_add_devices_recursive(bus, devices)
    local num_devices = 0
    local device
    for _, device in base.ipairs(devices) do
        -- base.print(string.format("i2c_add_device(%d, 0x%02X, %s)", bus, device.address, device.driver))
        _M.i2c_add_device(bus, device.address, device.driver)
        num_devices = num_devices + 1

        local setup_function = _internal_.setup[device.driver]
        if setup_function ~= nil then
            time.nanosleep({tv_sec = 0, tv_nsec = 100000000})
            setup_function(bus, device.address)
        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)
            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 tmp then return nil, err end
                num_devices = num_devices + tmp
            end
        end
    end

    return num_devices
end


-- ############# II0 ###########

function _M.iio_device_path_from_i2c_device(bus, address)
    local device_paths, glob_result = glob.glob(_M.i2c_device_path(bus, address) .. "/iio:device*", 0)
    if not device_paths then
        if glob_result == glob.GLOB_NOMATCH then return nil, "couldn't find IIO device handle for i2c device " .. _M.i2c_device_name(bus, address) 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
    if #device_paths ~= 1 then return nil, "found more than one IIO device for i2c device " .. _M.i2c_device_name(bus, address) end
    return device_paths[1]
end

function _M.iio_setup_bmp280(bus, address)
    local iio_device, err = _M.iio_device_path_from_i2c_device(bus, address)
    if not iio_device then return nil, err end

    local ret, err = _internal_.write_to_file(iio_device .. '/in_pressure_oversampling_ratio', '1')
    if not ret then return nil, err end
    return _internal_.write_to_file(iio_device .. '/in_temp_oversampling_ratio', '1')
end
_internal_.setup['bmp280'] = _M.iio_setup_bmp280

function _M.iio_setup_bme280(bus, address)
    local iio_device, err = _M.iio_device_path_from_i2c_device(bus, address)
    if not iio_device then return nil, err end

    local ret, err = _internal_.write_to_file(iio_device .. '/in_humidityrelative_oversampling_ratio', '1')
    if not ret then return nil, err end
    local ret, err = _internal_.write_to_file(iio_device .. '/in_pressure_oversampling_ratio', '1')
    if not ret then return nil, err end
    return _internal_.write_to_file(iio_device .. '/in_temp_oversampling_ratio', '1')
end
_internal_.setup['bme280'] = _M.iio_setup_bme280


-- ############# config ###########

function _M.read_config(path)
    sensors_json, err = _internal_.read_from_file(path)
    if not sensors_json then return nil, err end

    return cjson.decode(sensors_json)
end

function _M.setup(config)
    local num_devices = 0
    if config.i2c then
        local i2c_bus
        for _, i2c_bus in ipairs(config.i2c) do
            local tmp, err = _M.i2c_add_devices_recursive(i2c_bus.number, i2c_bus.devices)
            if not tmp then return nil, err end
            num_devices = num_devices + tmp
        end
    end
end


-- ################################

return _M