From f610c572929337bc5b2515bd9b745094df5164b9 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 26 Sep 2013 01:39:27 +0200 Subject: [PATCH 01/11] Remove dependencies on usb drivers (they have been added in both print3d and ultifi). --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ab0f481..07d7cbd 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ define Package/wifibox # DEFAULT:=y TITLE:=Doodle3D WifiBox firmware URL:=http://www.doodle3d.com/wifibox - DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd +kmod-usb-acm +kmod-usb-serial-ftdi +ultifi + DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd +print3d endef define Package/wifibox/description From bfd54e16cac05d996f6e2c75eb3a2c80ddab2c0a Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 26 Sep 2013 12:54:55 +0200 Subject: [PATCH 02/11] Add missing openwrt dependency on uhttpd-mod-lua. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 07d7cbd..452644d 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ define Package/wifibox # DEFAULT:=y TITLE:=Doodle3D WifiBox firmware URL:=http://www.doodle3d.com/wifibox - DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd +print3d + DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd uhttpd-mod-lua +print3d endef define Package/wifibox/description From 725f34b30ae51c5cca61633d912eec52b2cc9952 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 26 Sep 2013 13:00:25 +0200 Subject: [PATCH 03/11] Fix uhttpd dependency. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 452644d..593b840 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ define Package/wifibox # DEFAULT:=y TITLE:=Doodle3D WifiBox firmware URL:=http://www.doodle3d.com/wifibox - DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd uhttpd-mod-lua +print3d + DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd-mod-lua +print3d endef define Package/wifibox/description From af5598c60641e6265890d4a14737097f932e0213 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 26 Sep 2013 18:47:25 +0200 Subject: [PATCH 04/11] Again uhttpd dependency fix. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 593b840..fcb5099 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ define Package/wifibox # DEFAULT:=y TITLE:=Doodle3D WifiBox firmware URL:=http://www.doodle3d.com/wifibox - DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd-mod-lua +print3d + DEPENDS:=+lua +luafilesystem +libuci-lua +libiwinfo-lua +uhttpd +uhttpd-mod-lua +print3d endef define Package/wifibox/description From b042be54057d7938b2f76f14fd430883fa00b695 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Fri, 27 Sep 2013 18:38:31 +0200 Subject: [PATCH 05/11] Signin every hour --- Makefile | 1 + src/conf_defaults.lua | 8 ++++++++ src/main.lua | 14 ++++++++++++++ src/network/signin.lua | 42 +++++++++++++++++++++++++++++++++++++++++ src/rest/request.lua | 2 +- src/script/signin.sh | 7 +++++++ src/script/wifibox_init | 5 ++++- src/util/urlcode.lua | 11 +++++++++++ 8 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/network/signin.lua create mode 100755 src/script/signin.sh 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 e70d303..38c926c 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..5672362 --- /dev/null +++ b/src/network/signin.lua @@ -0,0 +1,42 @@ +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 wlanconfig = require("network.wlanconfig") + +local M = {} + +-- TODO: this function has been duplicated from rest/api/api_system.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 + +--- Signin to connect.doodle3d.com server +-- +function M.signin() + local wifiboxid = "henk" + local localip = "10.0.0.99" + local baseurl = "http://192.168.5.220/connect.doodle3d.local/signin.php" + + local ifconfig = captureCommandOutput("ifconfig wlan0"); + --log:info("ifconfig: "..ifconfig) + local localip = ifconfig:match('inet addr:([%d\.]+)') + --log:info("localip: "..utils.dump(localip)) + + local wifiboxid = wlanconfig.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) + --log:info("wifiboxid: "..utils.dump(wifiboxid)) + + local cmd = "wget -q -O - "..baseurl.."?wifiboxid="..wifiboxid.."\\&localip="..localip; + local output = captureCommandOutput(cmd); + log:info("signin: "..output) + + return 0 +end + +return M \ No newline at end of file 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..c6badf6 --- /dev/null +++ b/src/script/signin.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +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 From d5a2c2cad94d22950952c0e5d3fca68ede529e64 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Fri, 27 Sep 2013 18:39:18 +0200 Subject: [PATCH 06/11] client submodule update --- src/www | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/www b/src/www index 1842ec0..ce8e306 160000 --- a/src/www +++ b/src/www @@ -1 +1 @@ -Subproject commit 1842ec01b2d455142b8f654323ec92a4e2395753 +Subproject commit ce8e3069942fc598fa10bb9b4af1b19083510466 From 6a4879c7b2e9fdc9bf5853f595edf18aa37ed740 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Mon, 30 Sep 2013 18:08:02 +0200 Subject: [PATCH 07/11] added localip to /d3dapi/network/status (centralized captureCommandOutput and getLocalIP) --- src/network/signin.lua | 33 ++++++++++++--------------------- src/network/wlanconfig.lua | 7 +++++++ src/rest/api/api_network.lua | 3 +++ src/rest/api/api_system.lua | 17 ++++------------- src/script/signin.sh | 1 + src/util/utils.lua | 8 ++++++++ 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/network/signin.lua b/src/network/signin.lua index 5672362..3434afc 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -3,37 +3,28 @@ local utils = require('util.utils') local uci = require('uci').cursor() local iwinfo = require('iwinfo') local settings = require('util.settings') -local wlanconfig = require("network.wlanconfig") +local wifi = require("network.wlanconfig") local M = {} --- TODO: this function has been duplicated from rest/api/api_system.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 - --- Signin to connect.doodle3d.com server -- function M.signin() - local wifiboxid = "henk" - local localip = "10.0.0.99" - local baseurl = "http://192.168.5.220/connect.doodle3d.local/signin.php" + log:info("signin") + local baseurl = "http://connect.doodle3d.com/signin.php" - local ifconfig = captureCommandOutput("ifconfig wlan0"); - --log:info("ifconfig: "..ifconfig) - local localip = ifconfig:match('inet addr:([%d\.]+)') - --log:info("localip: "..utils.dump(localip)) + local localip = wifi.getLocalIP(); + log:info("localip: "..utils.dump(localip)) + if localip == nil then + log:error("signin failed no local ip found") + return + end - local wifiboxid = wlanconfig.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) - --log:info("wifiboxid: "..utils.dump(wifiboxid)) + local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) + log:info("wifiboxid: "..utils.dump(wifiboxid)) local cmd = "wget -q -O - "..baseurl.."?wifiboxid="..wifiboxid.."\\&localip="..localip; - local output = captureCommandOutput(cmd); + local output = utils.captureCommandOutput(cmd); log:info("signin: "..output) return 0 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_network.lua b/src/rest/api/api_network.lua index 13caaa2..3ad48ae 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -88,6 +88,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) 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/script/signin.sh b/src/script/signin.sh index c6badf6..7552f06 100755 --- a/src/script/signin.sh +++ b/src/script/signin.sh @@ -1,5 +1,6 @@ #!/bin/sh +sleep 5s while true; do /usr/share/lua/wifibox/script/d3dapi signin > /dev/null 2> /dev/null 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 From aab27a97686fc47d65984befc474fb178591e9aa Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Tue, 1 Oct 2013 12:36:51 +0200 Subject: [PATCH 08/11] less logging --- src/network/signin.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/network/signin.lua b/src/network/signin.lua index 3434afc..c698465 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -10,18 +10,15 @@ local M = {} --- Signin to connect.doodle3d.com server -- function M.signin() - log:info("signin") local baseurl = "http://connect.doodle3d.com/signin.php" local localip = wifi.getLocalIP(); - log:info("localip: "..utils.dump(localip)) if localip == nil then log:error("signin failed no local ip found") return end local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) - log:info("wifiboxid: "..utils.dump(wifiboxid)) local cmd = "wget -q -O - "..baseurl.."?wifiboxid="..wifiboxid.."\\&localip="..localip; local output = utils.captureCommandOutput(cmd); From e7db73a09b356cbc08072720f8420941ac823ac1 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 2 Oct 2013 14:41:15 +0200 Subject: [PATCH 09/11] signing returns success boolean --- src/network/signin.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/signin.lua b/src/network/signin.lua index c698465..9c7e71e 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -15,7 +15,7 @@ function M.signin() local localip = wifi.getLocalIP(); if localip == nil then log:error("signin failed no local ip found") - return + return false end local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) @@ -24,7 +24,7 @@ function M.signin() local output = utils.captureCommandOutput(cmd); log:info("signin: "..output) - return 0 + return string.len(output) > 0 end return M \ No newline at end of file From 8fc5273509758894fc3f53f6369eef29d57b556d Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 2 Oct 2013 14:41:46 +0200 Subject: [PATCH 10/11] When settings are saved, firmware signs in (for changed wifiboxid) --- src/rest/api/api_config.lua | 5 +++++ 1 file changed, 5 insertions(+) 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) From 9a2e8910f4b6879c63ce0e86a8354a9d501487f9 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 2 Oct 2013 14:42:27 +0200 Subject: [PATCH 11/11] Added signin api call (mostly for debugging) --- src/rest/api/api_network.lua | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index 3ad48ae..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 @@ -108,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') @@ -172,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