forked from vanminh0910/blocky_firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_mode.lua
148 lines (129 loc) · 4.25 KB
/
setup_mode.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
gpio.mode(4, gpio.OUTPUT)
gpio.write(4, gpio.LOW)
wifi.setmode(wifi.STATIONAP)
local cfg={}
cfg.ssid='BLOCKY_'..node.chipid()
wifi.ap.config(cfg)
print('Opening setup mode portal')
local rebootExpiry = 300000
rebootTimer = tmr.create()
rebootTimer:alarm(rebootExpiry, tmr.ALARM_SINGLE, function (t)
print('No activity found in setup mode. Restarting...')
node.restart()
end)
function parseRequestArgs(args)
if args == nil or args == '' then
return false
end
ssid, password, authKey = string.match(args, 'ssid\=([^&?]*)&password\=([^&?]*)&authKey\=([^&?]*)')
local function unescape (str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
str = string.gsub (str, "\r\n", "\n")
return str
end
ssid = unescape(ssid)
password = unescape(password)
authKey = unescape(authKey)
if ssid == nil or ssid == '' or password == nil or authKey == nil or authKey == '' then
return false
end
passwordLength = string.len(password)
if passwordLength ~= 0 and (passwordLength < 8 or passwordLength > 64) then
print('Password length should be between 8 and 64 characters')
return false
end
print('New WiFi credentials received')
print('-----------------------------')
print('wifi_ssid : ' .. ssid)
print('wifi_password : ' .. password)
print('auth_key : ' .. authKey)
wifi.sta.config({['ssid']=ssid, ['pwd']=password, ['save']=true})
file.open('config', 'w')
file.writeline(authKey)
file.flush()
file.close()
return true
end
srv=net.createServer(net.TCP)
srv:listen(80, function(conn)
local responseBytes = 0
local method=''
local url=''
local vars=''
conn:on('receive',function(conn, payload)
if rebootTimer ~= nil then
tmr.unregister(rebootTimer)
end
reconnectTimer = tmr.create()
reconnectTimer:alarm(rebootExpiry, tmr.ALARM_SINGLE, function (t)
node.restart()
end)
if string.len(payload) > 2000 then
print('payload is too big')
conn:send('HTTP/1.1 404 file not found')
responseBytes = -1
return
end
_, _, method, url, vars = string.find(payload, '([A-Z]+) /([^?]*)%??(.*) HTTP')
if (url == '') then
-- Only support one sending one file
url='index.html'
responseBytes = 0
conn:send('HTTP/1.1 200 OK\r\n\r\n')
return
elseif (url == 'set') then
-- Check if wifi-credentials have been supplied
if parseRequestArgs(vars) then
conn:send('HTTP/1.1 200 OK\r\n\r\nConfig saved. Now rebooting...')
print('<h1>Saved config. Now restart.</h1>')
tmr.create():alarm(2000, tmr.ALARM_SINGLE, function (t) node.restart() end)
end
return
elseif (url == 'aplist') then
local accessPointsList = {}
wifi.sta.getap(function(t)
for ssid,v in pairs(t) do
local ap = {}
local authmode, rssi, bssid, channel = string.match(v, "([^,]+),([^,]+),([^,]+),([^,]+)")
ap.ssid = ssid
ap.rssi = rssi
table.insert(accessPointsList, ap)
end
responseBytes = -1
conn:send('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n')
conn:send(sjson.encode(accessPointsList))
conn:send('\r\n\r\n')
conn:close()
end)
elseif (url == 'status') then
print('return status')
elseif (url == 'favicon.ico') then
conn:send('HTTP/1.1 404 file not found')
responseBytes = -1
return
else
conn:send('HTTP/1.1 404 not found')
responseBytes = -1
return
end
end)
conn:on('sent',function(conn)
if responseBytes >= 0 and method == 'GET' then
if file.open(url, 'r') then
file.seek('set', responseBytes)
local line = file.read(512)
file.close()
if line then
conn:send(line)
responseBytes = responseBytes + 512
if (string.len(line)==512) then
return
end
end
end
end
conn:close()
end)
end)
print('Setup mode is started and can be accessed via url: http://192.168.4.1/')