summaryrefslogtreecommitdiff
path: root/files/common/openwrt/sensors.module_lua
blob: 0767733d8e2bc2432cd692e619c406abe9bbb523 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
local base = _G
local cjson = require "cjson.safe"
local glob = require "posix.glob"
local io = require("io")
local string = require("string")
local time = require "posix.time"
local unistd = require "posix.unistd"

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

-- ############# utils ###########

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

    local 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

    local 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[base.tonumber(channel)] = base.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
        _M.i2c_add_device(bus, device.address, device.type)
        num_devices = num_devices + 1

        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)
            if not ret then return nil, err end
        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

function _M.i2c_read_sensors_recursive(bus, devices)
    local readings = {}
    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 read_function = _internal_.read[device.type]
        if read_function ~= nil then
            local values, kind = read_function(bus, device.address)
            if values ~= nil then
                readings[name] = values
                readings[name]['_kind_'] = kind
            end
        end

        if device.channels then
            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 mux_channels[channel.number] then
                    local tmp, err = _M.i2c_read_sensors_recursive(mux_channels[channel.number], channel.devices)
                    if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end
                end
            end
        end
    end

    return readings
end



-- ############# 1-wire ###########

function _M.w1_device_path(address)
    return string.format("/sys/bus/w1/devices/" .. address)
end

function _M.w1_device_family_and_serial(address)
    local family, serial = string.match(address, '^(%d+)%-(%d+)$')
    if not family then return nil, "unable to extract device family and serial from 1-wire address " .. address end
    return family, serial
end

function _M.w1_read_sensors(devices)
    local readings = {}
    local device
    for _, device in base.ipairs(devices) do
        local name = device.name ~= nil and device.name or device.address
        local family, _ = _M.w1_device_family_and_serial(device.address)
        if family ~= nil then
            local read_function = _internal_.read["w1-" .. family]
            if read_function ~= nil then
                local values, kind = read_function(device.address)
                if values ~= nil then
                    readings[name] = values
                    readings[name]['_kind_'] = kind
                end
            end
        end
    end

    return readings
end


-- ############# iio ###########

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_read_bmp280(bus, address)
    local iio_device, err = _M.iio_device_path_from_i2c_device(bus, address)
    if not iio_device then return end

    local values = {}
    local tmp, err = _internal_.read_from_file(iio_device .. '/in_pressure_input')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['pressure'] = val/100 end
    end
    tmp, err = _internal_.read_from_file(iio_device .. '/in_temp_input')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['temperature'] = val/1000 end
    end

    return values, 'bmp280'
end
_internal_.read['bmp280'] = _M.iio_read_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

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

    local values = {}
    local tmp, err = _internal_.read_from_file(iio_device .. '/in_humidityrelative_input')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['humidity'] = val/1000 end
    end
    local tmp, err = _internal_.read_from_file(iio_device .. '/in_pressure_input')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['pressure'] = val/100 end
    end
    tmp, err = _internal_.read_from_file(iio_device .. '/in_temp_input')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['temperature'] = val/1000 end
    end

    return values, 'bme280'
end
_internal_.read['bme280'] = _M.iio_read_bme280


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

    local values = {}
    local tmp, err = _internal_.read_from_file(iio_device .. '/in_humidityrelative_raw')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['humidity'] = val/10  end
    end
    time.nanosleep({tv_sec = 0, tv_nsec = 100000000})
    tmp, err = _internal_.read_from_file(iio_device .. '/in_temp_raw')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['temperature'] = val/10 end
    end

    return values, 'am2315'
end
_internal_.read['am2315'] = _M.iio_read_am2315



-- ############# hwmon ###########

function _M.hwmon_device_path_from_w1_device(address)
    local device_paths, glob_result = glob.glob(_M.w1_device_path(address) .. "/hwmon/hwmon*", 0)
    if not device_paths then
        if glob_result == glob.GLOB_NOMATCH then return nil, "couldn't find hwmon device handle for 1-wire device " .. 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 hwmon device for 1-wire device " .. address end
    return device_paths[1]
end

function _M.hwmon_read_w1_therm(address)
    local hwmon_device, err = _M.hwmon_device_path_from_w1_device(address)
    if not hwmon_device then return end

    local values = {}
    local tmp, err = _internal_.read_from_file(hwmon_device .. '/temp1_input')
    if tmp ~= nil then
        local val = base.tonumber(tmp)
        if val ~= nil then values['temperature'] = val/1000 end
    end

    return values
end
_internal_.read['w1-10'] = function(address) return _M.hwmon_read_w1_therm(address), "ds18s20" end
_internal_.read['w1-22'] = function(address) return _M.hwmon_read_w1_therm(address), "ds1822" end
_internal_.read['w1-28'] = function(address) return _M.hwmon_read_w1_therm(address), "ds18b20" end
_internal_.read['w1-3B'] = function(address) return _M.hwmon_read_w1_therm(address), "ds1825" end
_internal_.read['w1-42'] = function(address) return _M.hwmon_read_w1_therm(address), "ds28ea00" end



-- ############# high-level interface ###########

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 base.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
    -- for now the only supported 1-wire master device is i2c based and will be initialized above
    -- also sensors on 1-wire busses are discvored automatically and for now no supported sensor needs any setup
end

function _M.read(config)
    local readings = {}
    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)
            if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end
        end
    end
    if config.w1 then
        local tmp, err = _M.w1_read_sensors(config.w1)
        if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end
    end

    return readings
end


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

return _M