2011-01-27 09:46:29 +00:00
|
|
|
#!/usr/bin/env lua
|
|
|
|
|
2011-01-28 10:17:21 +00:00
|
|
|
--[[
|
|
|
|
|
|
|
|
fsync.lua - synchronize /etc/config/flukso settings with the sensor board
|
2011-02-12 19:38:23 +00:00
|
|
|
via the spid ctrl fifos
|
2011-01-28 10:17:21 +00:00
|
|
|
|
|
|
|
Copyright (C) 2011 Bart Van Der Meerssche <bart.vandermeerssche@flukso.net>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
|
2011-01-27 09:46:29 +00:00
|
|
|
local dbg = require 'dbg'
|
|
|
|
local nixio = require 'nixio'
|
|
|
|
nixio.fs = require 'nixio.fs'
|
|
|
|
local uci = require 'luci.model.uci'.cursor()
|
|
|
|
|
2011-01-30 09:18:19 +00:00
|
|
|
local CTRL_PATH = '/var/run/spid/ctrl'
|
2011-01-27 09:46:29 +00:00
|
|
|
local CTRL_PATH_IN = CTRL_PATH .. '/in'
|
|
|
|
local CTRL_PATH_OUT = CTRL_PATH .. '/out'
|
|
|
|
|
|
|
|
local O_RDWR_NONBLOCK = nixio.open_flags('rdwr', 'nonblock')
|
|
|
|
local POLLIN = nixio.poll_flags('in')
|
|
|
|
local POLL_TIMEOUT_MS = 1000
|
|
|
|
local MAX_TRIES = 5
|
|
|
|
|
|
|
|
local ctrl = { fdin = nixio.open(CTRL_PATH_IN, O_RDWR_NONBLOCK),
|
|
|
|
fdout = nixio.open(CTRL_PATH_OUT, O_RDWR_NONBLOCK),
|
|
|
|
events = POLLIN,
|
|
|
|
revents = 0 }
|
|
|
|
|
|
|
|
if ctrl.fdin == nil or ctrl.fdout == nil then
|
2011-01-28 13:48:56 +00:00
|
|
|
print('Error. Unable to open the ctrl fifos.')
|
2011-01-27 09:46:29 +00:00
|
|
|
print('Exiting...')
|
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
|
2011-01-30 09:18:19 +00:00
|
|
|
ctrl.fd = ctrl.fdout -- need this entry for nixio.poll
|
|
|
|
ctrl.line = ctrl.fdout:linesource()
|
|
|
|
|
2011-01-27 09:46:29 +00:00
|
|
|
-- TODO acquire an exclusive lock on the ctrl fifos or exit
|
|
|
|
|
|
|
|
|
|
|
|
local function send(ctrl, cmd)
|
|
|
|
while ctrl.line() do end -- flush the out fifo
|
|
|
|
|
|
|
|
for i = 1, MAX_TRIES do
|
|
|
|
ctrl.fdin:write(cmd .. '\n')
|
|
|
|
|
|
|
|
local poll, errno, errmsg = nixio.poll({ ctrl }, POLL_TIMEOUT_MS)
|
|
|
|
|
|
|
|
if poll < 0 then
|
2011-01-28 13:48:56 +00:00
|
|
|
print('Error. Poll failed with error message: ' .. errmsg)
|
2011-01-27 09:46:29 +00:00
|
|
|
|
|
|
|
elseif poll == 0 then
|
2011-01-28 13:48:56 +00:00
|
|
|
print('Error. Poll timed out after ' .. POLL_TIMEOUT_MS .. 'ms')
|
2011-01-27 09:46:29 +00:00
|
|
|
|
|
|
|
elseif poll > 0 then
|
|
|
|
reply = ctrl.line()
|
|
|
|
|
|
|
|
if cmd:sub(1, 1) == 's' then
|
|
|
|
if reply == cmd then
|
|
|
|
print(reply .. ' .. ok')
|
|
|
|
return reply
|
|
|
|
else
|
|
|
|
print(reply .. ' .. nok .. should be ' .. cmd .. ' instead')
|
|
|
|
end
|
|
|
|
elseif cmd:sub(1, 2) == reply:sub(1, 2) then
|
|
|
|
print(reply .. ' .. ok')
|
|
|
|
return reply
|
|
|
|
else
|
|
|
|
print(reply .. ' .. nok')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print(MAX_TRIES .. ' write attempts failed. Exiting ...')
|
|
|
|
os.exit(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function toc(num)
|
|
|
|
return num - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- parse and load /etc/config/flukso
|
|
|
|
local flukso = uci:get_all('flukso')
|
|
|
|
|
|
|
|
local HW_CHECK_OVERRIDE = (arg[1] == '-f')
|
|
|
|
|
2011-01-28 20:28:43 +00:00
|
|
|
local MAX_SENSORS = tonumber(flukso.main.max_sensors)
|
|
|
|
local MAX_ANALOG_SENSORS = tonumber(flukso.main.max_analog_sensors)
|
|
|
|
local METERCONST_FACTOR = 0.449
|
2011-01-27 09:46:29 +00:00
|
|
|
|
2011-01-28 20:28:43 +00:00
|
|
|
local GET_HW_VERSION = 'gh'
|
|
|
|
local GET_HW_VERSION_R = '^gh%s+(%d+)%s+(%d+)$'
|
|
|
|
local SET_ENABLE = 'se %d %d'
|
|
|
|
local SET_PHY_TO_LOG = 'sp' -- with [1..MAX_SENSORS] arguments
|
|
|
|
local SET_METERCONST = 'sm %d %d'
|
|
|
|
local SET_COUNTER = 'sc %d %d'
|
2011-02-12 11:36:53 +00:00
|
|
|
local COMMIT = 'ct'
|
2011-01-27 09:46:29 +00:00
|
|
|
|
|
|
|
-- check hardware version
|
|
|
|
local hw_major, hw_minor = send(ctrl, GET_HW_VERSION):match(GET_HW_VERSION_R)
|
|
|
|
|
|
|
|
if hw_major ~= flukso.main.hw_major or hw_minor > flukso.main.hw_minor then
|
|
|
|
print(string.format('Hardware check (major: %s, minor: %s) .. nok', hw_major, hw_minor))
|
|
|
|
if hw_major ~= flukso.main.hw_major then
|
2011-01-28 13:48:56 +00:00
|
|
|
print('Error. Major version does not match.')
|
2011-01-27 09:46:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if hw_minor > flukso.main.hw_minor then
|
2011-01-28 13:48:56 +00:00
|
|
|
print('Error. Sensor board minor version is not supported by this package.')
|
2011-01-27 09:46:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if HW_CHECK_OVERRIDE then
|
|
|
|
print('Overridden. Good luck!')
|
|
|
|
else
|
|
|
|
print('Use -f to override this check at your own peril.')
|
|
|
|
os.exit(3)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print(string.format('Hardware check (major: %s, minor: %s) .. ok', hw_major, hw_minor))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- disable all ports
|
|
|
|
for i = 1, MAX_SENSORS do
|
|
|
|
local cmd = string.format(SET_ENABLE, toc(i), 0)
|
|
|
|
send(ctrl, cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- populate phy_to_log
|
|
|
|
local phy_to_log = {}
|
|
|
|
|
|
|
|
for i = 1, MAX_SENSORS do
|
2011-01-28 19:38:19 +00:00
|
|
|
if flukso[tostring(i)] ~= nil then
|
2011-01-28 20:28:43 +00:00
|
|
|
if flukso[tostring(i)]['type'] == 'analog' and i > MAX_ANALOG_SENSORS then
|
|
|
|
print(string.format('Error. Analog sensor %s should be less than or equal to max_analog_sensors (%s)', i, MAX_ANALOG_SENSORS))
|
|
|
|
os.exit(4)
|
|
|
|
end
|
|
|
|
|
2011-01-27 09:46:29 +00:00
|
|
|
local ports = flukso[tostring(i)].port or {}
|
|
|
|
|
|
|
|
for j = 1, #ports do
|
2011-01-28 20:28:43 +00:00
|
|
|
if tonumber(ports[j]) > MAX_SENSORS then
|
2011-01-28 13:43:55 +00:00
|
|
|
print(string.format('Error. Port numbering in sensor %s should be less than or equal to max_sensors (%s)', i, MAX_SENSORS))
|
2011-01-28 20:28:43 +00:00
|
|
|
os.exit(5)
|
|
|
|
|
|
|
|
else
|
|
|
|
phy_to_log[toc(tonumber(ports[j]))] = toc(i)
|
2011-01-28 13:43:55 +00:00
|
|
|
end
|
2011-01-27 09:46:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-28 19:38:19 +00:00
|
|
|
-- ports that are not in use are mapped to sensor id 0xff
|
2011-01-27 09:46:29 +00:00
|
|
|
for i = 0, MAX_SENSORS - 1 do
|
|
|
|
if not phy_to_log[i] then
|
|
|
|
phy_to_log[i] = 0xff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local cmd = SET_PHY_TO_LOG .. ' ' .. table.concat(phy_to_log, ' ', 0)
|
|
|
|
send(ctrl, cmd)
|
|
|
|
|
|
|
|
-- populate meterconst
|
|
|
|
for i = 1, MAX_SENSORS do
|
|
|
|
local cmd
|
|
|
|
|
|
|
|
if flukso[tostring(i)] == nil then
|
|
|
|
cmd = string.format(SET_METERCONST, toc(i), 0)
|
|
|
|
|
|
|
|
elseif flukso[tostring(i)]['type'] == 'analog' then
|
|
|
|
local voltage = tonumber(flukso[tostring(i)].voltage)
|
|
|
|
local current = tonumber(flukso[tostring(i)].current)
|
|
|
|
cmd = string.format(SET_METERCONST, toc(i), math.floor(METERCONST_FACTOR * voltage * current))
|
|
|
|
|
|
|
|
elseif flukso[tostring(i)]['type'] == 'pulse'then
|
|
|
|
local meterconst = tonumber(flukso[tostring(i)].constant)
|
|
|
|
cmd = string.format(SET_METERCONST, toc(i), meterconst)
|
|
|
|
else
|
|
|
|
cmd = string.format(SET_METERCONST, toc(i), 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
if cmd then send(ctrl,cmd) end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- populate counter if reset_counters is set
|
|
|
|
if flukso.main.reset_counters == '1' then
|
|
|
|
for i = 1, MAX_SENSORS do
|
|
|
|
local cmd = string.format(SET_COUNTER, toc(i), 0)
|
|
|
|
send(ctrl, cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
uci:set('flukso', 'main', 'reset_counters', 0)
|
|
|
|
uci:commit('flukso')
|
|
|
|
end
|
|
|
|
|
2011-01-28 19:38:19 +00:00
|
|
|
-- enable configured sensors
|
2011-01-27 09:46:29 +00:00
|
|
|
for i = 1, MAX_SENSORS do
|
2011-01-28 11:51:10 +00:00
|
|
|
if flukso[tostring(i)] ~= nil and flukso[tostring(i)].enable == '1' then
|
2011-01-28 19:38:19 +00:00
|
|
|
cmd = string.format(SET_ENABLE, toc(i), 1)
|
|
|
|
send(ctrl, cmd)
|
2011-01-27 09:46:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-12 11:36:53 +00:00
|
|
|
-- commit changes
|
|
|
|
send(ctrl, COMMIT)
|
|
|
|
|
|
|
|
|
2011-01-27 09:46:29 +00:00
|
|
|
-- clean up
|
|
|
|
ctrl.fdin:close()
|
|
|
|
ctrl.fdout:close()
|
|
|
|
|
|
|
|
print(arg[0] .. ' completed successfully. Bye!')
|