diff --git a/src/configs/config.json b/src/configs/config.json index 03a45d81..193426e6 100644 --- a/src/configs/config.json +++ b/src/configs/config.json @@ -105,6 +105,81 @@ "paramX": "valueX", "paramY": "valueY" }, + "switch": { + "network": [ + { + "config": "config interface 'loopback'", + "options": [ + "option device 'lo'", + "option proto 'static'", + "option ipaddr '127.0.0.1'", + "option netmask '255.0.0.0'" + ] + }, + { + "config": "config globals 'globals'", + "options": [ + "option ula_prefix 'fd91:8cf8:a2dd::/48'" + ] + }, + { + "config": "config device 'switch'", + "options": [ + "option name 'switch'", + "option type 'bridge'" + ] + }, + { + "config": "config bridge-vlan 'wan_vlan'", + "options": [ + "option device 'switch'", + "option vlan '1'", + "option ports 'lan1 lan2 lan3 lan4 lan5 lan6 lan7 lan8 lan9 lan10'" + ] + }, + { + "config": "config bridge-vlan 'lan_vlan'", + "options": [ + "option device 'switch'", + "option vlan '200'", + "option ports 'lan1 lan2 lan3 lan4 lan5 lan6 lan7 lan8 lan9 lan10'" + ] + }, + { + "config": "config interface 'lan'", + "options": [ + "option device 'switch.200'", + "option proto 'static'", + "option ipaddr '192.168.200.1'", + "option netmask '255.255.255.0'", + "option ip6assign '60'" + ] + }, + { + "config": "config interface 'wan'", + "options": [ + "option device 'switch.1'", + "option proto 'dhcp'" + ] + } + ], + "poe": [ + { + "config": "config poe poe", + "options": [ + "option budget 77", + "option port1 1", + "option port2 1", + "option port3 0", + "option port4 0", + "option port5 0", + "option port6 0", + "option port7 0", + "option port8 0" + ] + } + ] + }, "hub": { "ws_host": "172.19.0.2", "ws_port": "9003", diff --git a/src/devices/switch.lua b/src/devices/switch.lua new file mode 100644 index 00000000..84ab84d7 --- /dev/null +++ b/src/devices/switch.lua @@ -0,0 +1,10 @@ +-- Switch configuration + +return { + services = { + "config", + "system", + "switch" + -- ... other services specific to this device version + } +} diff --git a/src/services/switch.lua b/src/services/switch.lua new file mode 100644 index 00000000..b8af2bd7 --- /dev/null +++ b/src/services/switch.lua @@ -0,0 +1,118 @@ +local fiber = require "fibers.fiber" +local op = require "fibers.op" +local log = require "log" +local json = require "dkjson" +local exec = require "fibers.exec" + +local switch_service = {} +switch_service.__index = switch_service + +-- TODO test +local function create_config(config_json) + local config_str = "" + + for _, item in ipairs(config_json) do + -- Add the config command + config_str = config_str .. item.config .. "\n" + + -- Add all the options for this config + for _, option in ipairs(item.options) do + config_str = config_str .. "\t" .. option .. "\n" + end + + config_str = config_str .. "\n" + end + + return config_str +end + +local function write_config(config, path) + local file, err = io.open(path, "w") + + if not file then + return err + end + + file:write(config) + file:close() + return nil +end + +-- TODO unsure if this is correct command +local function restart_service(service) + local cmd = exec.command("/etc/init.d/" .. service, 'restart') + local out, err = cmd:combined_output() + + if err ~= nil then + return err .. out + end + + return nil +end + +local function set_config(config, path, service) + local config_str = create_config(config) + local err = write_config(config_str, path) + + if err ~= nil then + return err + end + + err = restart_service(service) + return err +end + +local function config_receiver(bus_connection) + log.trace("Starting switch config receiver") + local function handle_config(msg, err) + if err then + log.error(err) + error("Message not received") + end + + log.trace("Switch config received!") + local config, _, err = json.decode(msg.payload) + if err then + log.error(err) + error("JSON couldn't be decoded") + end + + if config["network"] ~= nil then + fiber.spawn(function() + log.trace("Network config available") + local err = set_config(config["network"], "/etc/config/network", "network") + + if err ~= nil then + log.error(err) + else + log.trace("Network config set") + end + end) + end + + if config["poe"] ~= nil then + fiber.spawn(function() + log.trace("Poe config available") + local err = set_config(config["poe"], "/etc/config/poe", "network") + + if err ~= nil then + log.error(err) + else + log.trace("Poe config set") + end + end) + end + end + + local sub = bus_connection:subscribe("config/switch") + while true do + op.choice(sub:next_msg_op():wrap(handle_config)):perform() + end +end + +function switch_service:start(bus_connection, device_version) + -- Copy modem set up + fiber.spawn(function() config_receiver(bus_connection) end) +end + +return switch_service diff --git a/src/services/system.lua b/src/services/system.lua index 8bf09218..ccfde123 100644 --- a/src/services/system.lua +++ b/src/services/system.lua @@ -1,3 +1,6 @@ +local system_service = {} +system_service.__index = system_service + local function read_file(path) local file = io.open(path, "r") if not file then return nil end @@ -90,7 +93,7 @@ local function get_ram_info() return total, used, free + buffers + cached end -local function main() +function system_service:start(bus_connection) -- local cpu_model = get_cpu_info() local ram_total, ram_used, ram_free = get_ram_info() @@ -108,4 +111,4 @@ local function main() print("Free RAM: " .. ram_free .. " kB") end -main() \ No newline at end of file +return system_service \ No newline at end of file