#!/usr/bin/env lua --[[ Copyright (C) 2020 LibreMesh.org This is free software, licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 Copyright 2020 Santiago Piccinini ]]-- local ubus = require "ubus" local json = require 'luci.jsonc' local utils = require 'lime.utils' local limeutilsadmin = require 'lime-utils-admin' local conn = ubus.connect() if not conn then error("Failed to connect to ubus") end local UPGRADE_METADATA_FILE = "/etc/upgrade_metadata" local function set_root_password(msg) local result = limeutilsadmin.set_root_password(msg) return utils.printJson(result) end local function set_hostname(msg) local result = limeutilsadmin.set_hostname(msg) return utils.printJson(result) end local function is_upgrade_confirm_supported() local result = limeutilsadmin.is_upgrade_confirm_supported() return utils.printJson(result) end local function firmware_upgrade(msg) local result = limeutilsadmin.firmware_upgrade(msg) return utils.printJson(result) end local function last_upgrade_metadata() local result = limeutilsadmin.last_upgrade_metadata() return utils.printJson(result) end local function firmware_confirm() local result = limeutilsadmin.firmware_confirm() return utils.printJson(result) end --! Creates a client connection to a wifi hotspot local function hotspot_wwan_enable(msg) local result = limeutilsadmin.hotspot_wwan_enable(msg) return utils.printJson(result) end local function hotspot_wwan_disable(msg) local result = limeutilsadmin.hotspot_wwan_disable(msg) return utils.printJson(result) end local function safe_reboot(msg) local result = limeutilsadmin.safe_reboot(msg) utils.printJson(result) end local methods = { set_root_password = { password = 'value'}, set_hostname = { hostname = 'value'}, is_upgrade_confirm_supported = { no_params = 0 }, firmware_upgrade = { fw_path = 'value', preserve_config = 'value', metadata = 'value', fw_type = 'value'}, last_upgrade_metadata = { no_params = 0 }, firmware_confirm = { no_params = 0 }, hotspot_wwan_enable = { radio = 'value', ssid = 'value', password = 'value', encryption = 'value'}, hotspot_wwan_disable = { radio = 'value' }, safe_reboot = {action = 'value', value = 'value'}, } if arg[1] == 'list' then utils.printJson(methods) end if arg[1] == 'call' then local msg = utils.rpcd_readline() msg = json.parse(msg) if arg[2] == 'set_root_password' then set_root_password(msg) elseif arg[2] == 'set_hostname' then set_hostname(msg) elseif arg[2] == 'is_upgrade_confirm_supported' then is_upgrade_confirm_supported(msg) elseif arg[2] == 'firmware_upgrade' then firmware_upgrade(msg) elseif arg[2] == 'last_upgrade_metadata' then last_upgrade_metadata(msg) elseif arg[2] == 'firmware_confirm' then firmware_confirm(msg) elseif arg[2] == 'hotspot_wwan_enable' then hotspot_wwan_enable(msg) elseif arg[2] == 'hotspot_wwan_disable' then hotspot_wwan_disable(msg) elseif arg[2] == 'safe_reboot' then safe_reboot(msg) else utils.printJson({ error = "Method not found" }) end end