diff --git a/Makefile b/Makefile index fcb5099..1aefe13 100644 --- a/Makefile +++ b/Makefile @@ -99,6 +99,7 @@ define Package/wifibox/install $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/wifibox_init $(1)/etc/init.d/wifibox # copy directly to init dir (required for post-inst enabling) $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/$(TGT_LUA_DIR_SUFFIX)/script + $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/signin.sh $(1)/$(TGT_LUA_DIR_SUFFIX)/script $(CP) $(WIFIBOX_BASE_DIR)/script/wifibox.uci.config $(1)/etc/config/wifibox # copy base configuration to uci config dir diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index 697f122..ac24151 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -50,6 +50,14 @@ M.network_ap_netmask = { regex = '%d+\.%d+\.%d+\.%d+' } +M.network_cl_wifiboxid = { + default = 'Doodle3D-%%MAC_ADDR_TAIL%%', + type = 'string', + description = 'Client mode WiFi box id', + min = 1, + max = 32 +} + M.printer_type = { default = 'ultimaker', type = 'string', diff --git a/src/main.lua b/src/main.lua index 709cc1b..144c8fb 100644 --- a/src/main.lua +++ b/src/main.lua @@ -8,6 +8,7 @@ local wifi = require('network.wlanconfig') local netconf = require('network.netconfig') local RequestClass = require('rest.request') local ResponseClass = require('rest.response') +local Signin = require('network.signin') local postData = nil @@ -172,6 +173,19 @@ local function main(environment) else log:error("autowifi setup failed (" .. msg .. ")") end + elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then + log:info("running in signin mode") + + local ds = wifi.getDeviceState() + if ds.mode == "sta" then + local rv,msg = Signin.signin() + end + + --[[if rv then + log:info("autowifi setup done (" .. msg .. ")") + else + log:error("autowifi setup failed (" .. msg .. ")") + end]]-- elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then -- log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") -- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) diff --git a/src/network/signin.lua b/src/network/signin.lua new file mode 100644 index 0000000..9c7e71e --- /dev/null +++ b/src/network/signin.lua @@ -0,0 +1,30 @@ +local log = require('util.logger') +local utils = require('util.utils') +local uci = require('uci').cursor() +local iwinfo = require('iwinfo') +local settings = require('util.settings') +local wifi = require("network.wlanconfig") + +local M = {} + +--- Signin to connect.doodle3d.com server +-- +function M.signin() + local baseurl = "http://connect.doodle3d.com/signin.php" + + local localip = wifi.getLocalIP(); + if localip == nil then + log:error("signin failed no local ip found") + return false + end + + local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) + + local cmd = "wget -q -O - "..baseurl.."?wifiboxid="..wifiboxid.."\\&localip="..localip; + local output = utils.captureCommandOutput(cmd); + log:info("signin: "..output) + + return string.len(output) > 0 +end + +return M \ No newline at end of file diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index 8638b31..eab892a 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -113,6 +113,13 @@ function M.getMacAddress() return out:upper() end +--returns the wireless local ip address +function M.getLocalIP() + local ifconfig, rv = utils.captureCommandOutput("ifconfig wlan0"); + local localip = ifconfig:match('inet addr:([%d\.]+)') + return localip; +end + function M.getDeviceName() return deviceName end diff --git a/src/rest/api/api_config.lua b/src/rest/api/api_config.lua index e127c99..304a689 100644 --- a/src/rest/api/api_config.lua +++ b/src/rest/api/api_config.lua @@ -1,6 +1,7 @@ local log = require('util.logger') local settings = require('util.settings') local printer = require('util.printer') +local signin = require('network.signin') local M = { isApi = true @@ -26,6 +27,10 @@ function M._global_POST(request, response) else response:addData(k, "could not set key ('" .. m .. "')") end end + + log:info("API:Network:try signing in") + signin.signin(); + log:info("API:Network:signed in") end function M.all_GET(request, response) diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index 13caaa2..e2b5936 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -1,8 +1,10 @@ +local log = require('util.logger') local settings = require('util.settings') local utils = require('util.utils') local netconf = require('network.netconfig') local wifi = require('network.wlanconfig') local ResponseClass = require('rest.response') +local signin = require('network.signin') local M = { isApi = true @@ -88,6 +90,9 @@ function M.status(request, response) response:addData("signal", ds.signal) response:addData("noise", ds.noise) if withRaw then response:addData("_raw", utils.dump(ds)) end + + local localip = wifi.getLocalIP() + response:addData("localip", localip) end --requires ssid(string), accepts phrase(string), recreate(bool) @@ -105,10 +110,12 @@ function M.associate_POST(request, response) return end - local associate = function() - local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate) + local associate = function() + local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate) end - response:addPostResponseFunction(associate) + response:addPostResponseFunction(associate) + + --[[local helloA = function() local log = require('util.logger') @@ -169,4 +176,15 @@ function M.remove_POST(request, response) end end +function M.signin(request, response) + log:info("API:Network:signin") + if signin.signin() then + log:info("API:Network:signed in") + response:setSuccess("API:Network:signed in") + else + log:info("API:Network:Signing in failed") + response:setError("Signing in failed") + end +end + return M diff --git a/src/rest/api/api_system.lua b/src/rest/api/api_system.lua index 27304d3..6a9d8de 100644 --- a/src/rest/api/api_system.lua +++ b/src/rest/api/api_system.lua @@ -1,18 +1,9 @@ +local utils = require('util.utils') + local M = { isApi = true } - --- TODO: this function has been duplicated from test/test_wlanconfig.lua -local function captureCommandOutput(cmd) - local f = assert(io.popen(cmd, 'r')) - local output = assert(f:read('*all')) - --TODO: test if this works to obtain the return code (http://stackoverflow.com/questions/7607384/getting-return-status-and-program-output) - --local rv = assert(f:close()) - --return output,rv[3] - return output -end - function M._global(request, response) response:setSuccess() end @@ -24,13 +15,13 @@ function M.fwversions(request, response) response:setSuccess() - output = captureCommandOutput(opkg .. ' list-installed wifibox') + output = utils.captureCommandOutput(opkg .. ' list-installed wifibox') local version = output:match('^wifibox %- (.*)\n$') response:addData('current', version) rv = os.execute(opkg .. ' update >/dev/null') if rv == 0 then - output = captureCommandOutput(opkg .. ' list wifibox') + output = utils.captureCommandOutput(opkg .. ' list wifibox') local versions = {} for v in output:gmatch('wifibox %- (%d+\.%d+\.%d+%-%d+) %- ') do versions[#versions+1] = v diff --git a/src/rest/request.lua b/src/rest/request.lua index 3e86f4c..1d610b2 100644 --- a/src/rest/request.lua +++ b/src/rest/request.lua @@ -175,7 +175,7 @@ function M.new(environment, postData, debugEnabled) self.pathArgs = arrayFromPath(environment['PATH_INFO']) -- override path arguments with command line parameter and allow to emulate GET/POST if debugging is enabled *and* if the autowifi special command wasn't mentioned - if debugEnabled and self.requestMethod == 'CMDLINE' and self:get('autowifi') == nil then + if debugEnabled and self.requestMethod == 'CMDLINE' and self:get('autowifi') == nil and self:get('signin') == nil then self.pathArgs = arrayFromPath(self.cmdLineArgs['p']) if self.cmdLineArgs['r'] == 'GET' or self.cmdLineArgs['r'] == nil then diff --git a/src/script/signin.sh b/src/script/signin.sh new file mode 100755 index 0000000..7552f06 --- /dev/null +++ b/src/script/signin.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +sleep 5s +while true; do + /usr/share/lua/wifibox/script/d3dapi signin > /dev/null 2> /dev/null + + sleep 1h +done diff --git a/src/script/wifibox_init b/src/script/wifibox_init index 46d43e9..d1b7b78 100755 --- a/src/script/wifibox_init +++ b/src/script/wifibox_init @@ -8,6 +8,9 @@ LOGGER="logger -s -t autowifi -p 6" boot() { $LOGGER "Invoking Doodle3D WiFi box network auto-initialization..." /usr/share/lua/wifibox/script/d3dapi autowifi + + $LOGGER "Start signing in..." + /usr/share/lua/wifibox/script/signin.sh > /dev/null 2> /dev/null & } #start() { @@ -16,4 +19,4 @@ boot() { #stop() { # $LOGGER "dummy stop" -#} +#} \ No newline at end of file diff --git a/src/util/urlcode.lua b/src/util/urlcode.lua index 6aa6921..c0bc529 100644 --- a/src/util/urlcode.lua +++ b/src/util/urlcode.lua @@ -97,20 +97,31 @@ end ---------------------------------------------------------------------------- function _M.parsequeryNoRegex (query, args) if type(query) == "string" then + local log = require('util.logger') + local util = require('util.utils') + log:info("parsequeryNoRegex") + --log:info(" query: " .. util.dump(query)) + --log:info(" args: " .. util.dump(args)) + local insertfield, unescape = _M.insertfield, _M.unescape local k = 1 while true do local v = query:find('=', k+1, true) -- look for '=', assuming a key of at least 1 character and do not perform pattern matching + --log:info(" v: " .. util.dump(v)) if not v then break end -- no k/v pairs left local key = query:sub(k, v-1) + log:info(" key: " .. util.dump(key)) v = v + 1 + --log:info(" >v: " .. util.dump(v)) local ampersand = query:find('&', v, true) + --log:info(" ampersand: " .. util.dump(ampersand)) if not ampersand then ampersand = 0 end -- 0 will become -1 in the substring call below...meaning end of string local value = query:sub(v, ampersand - 1) + --log:info(" value: " .. util.dump(value)) insertfield (args, unescape(key), unescape(value)) if ampersand == 0 then break end -- we couldn't find any ampersands anymore so this was the last k/v diff --git a/src/util/utils.lua b/src/util/utils.lua index 32dd1ef..674794a 100644 --- a/src/util/utils.lua +++ b/src/util/utils.lua @@ -103,4 +103,12 @@ function M.readFile(filePath) return res end +-- TODO: this function has been duplicated from rest/api/api_system.lua +function M.captureCommandOutput(cmd) + local f = assert(io.popen(cmd, 'r')) + local output = assert(f:read('*all')) + f:close() + return output; +end + return M