summaryrefslogtreecommitdiff
path: root/wakeup.lua
blob: 73fc25ef1a73eb3c27546987a2b7c3f1148d6455 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
local wakeup = require("+wakeup")


--- Builds and returns a Wake-on-LAN magic packet for [[mac]] address.
local
function magic_packet (mac)
	local data = ""
	for octet in mac:gmatch("%x%x") do
		data = data .. string.char(tonumber(octet, 16))
	end
	if #data ~= 6 then
		error(string.format("Invalid MAC\t%q", mac), 2)
	end
	return string.char(0xff):rep(6) .. data:rep(16)
end


--- Creates a new counter that has :up() method that returns true until it gets called N [[times]]. Then it returns only
--- false.
local
function new_counter (times)
	return {
		count = 0,
		times = times,
		up = function (self)
			self.count = self.count + 1
			return self.count <= self.times
		end,
	}
end


--- Perform the most basic preparations right after device booted up.
function wakeup:boot ()
	gpio.write(4, gpio.HIGH)
	gpio.mode(4, gpio.OUTPUT)
	self.data = magic_packet(self.mac)
	self.step = self.connect
end


--- This is the current state action of the wake-up state machine. It is a callable that has [[(self, [timer]) -> nil]]
--- signature. Steps are called sequentially, usually by a tmr.timer.
wakeup.step = wakeup.boot


--- Will tick for N [[ticks]] and then switch into a state [[after]].
local
function start_timeout (ticks, after)
	local self = new_counter(ticks)
	return function (wakeup)
		if not self:up() then
			wakeup.step = after
		end
	end
end


--- Marks a failure that is pointless to attempt to recover from, e.g., if provided configuration is incomplete.
function wakeup:hard_failure (timer)
	print("Aborting...")
	return timer:unregister()
end


--- Marks a failure that device may recover from by restarting itself.
function wakeup:soft_failure ()
	node.restart()
end


function wakeup:connect ()
	self.step = start_timeout(10, self.soft_failure)
	wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function ()
		self.step = self.sync_time
	end)
	wifi.setmode(wifi.STATION, false)
	wifi.sta.connect()
end


function wakeup:sync_time ()
	self.step = start_timeout(10, self.soft_failure)
	sntp.sync(
		self.sntp,
		function ()
			self.step = self.check_time
		end,
		function (code, err)
			print("SNTP sync failed", code, err)
			self.step = self.soft_failure
		end)
end


--- Creates new state machine action that sends out Wake-on-LAN magic packets to configured host N [[times]].
local
function start_sendout (times)
	local self = new_counter(times)
	self.udp = net.createUDPSocket()
	return function (wakeup, timer)
		if self:up() then
			print("Waking up", wakeup.mac, wakeup.addr)
			self.udp:send(wakeup.port, wakeup.addr, wakeup.data)
		else
			print("Done")
			timer:interval(10000)
			self.udp:close()
			gpio.write(4, gpio.HIGH)
			wakeup.step = start_timeout(6, wakeup.check_time)
		end
	end
end


--- Current UTC wall clock time.
local
function now ()
	local time = rtctime.epoch2cal(rtctime.get())
	return {hour = time.hour, min = time.min}
end


--- Time in minutes to next occurrence of [[pattern]] starting from [[time]].
local
function upcoming (pattern, time)
	local hours = 0
	if pattern.hour then
		hours = pattern.hour - time.hour
		if hours < 0 then
			hours = hours + 24
		end
	end
	local minutes = 0
	if pattern.min then
		minutes = pattern.min - time.min
		if minutes < 0 then
			minutes = minutes + 60
			if hours > 0 then
				hours = hours - 1
			end
		end
	end
	return hours * 60 + minutes
end


function wakeup:check_time (timer)
	local left = upcoming(self.when, now())
	if left == 0 then
		timer:interval(500)
		gpio.write(4, gpio.LOW)
		self.step = start_sendout(5)
	elseif left > 5 then
		timer:interval(1000)
		print(string.format("Need to wait total %d minutes", left))
		left = left - 5
		if left > 50 then
			left = 50
		end
		self.left = left
		self.step = start_timeout(3, self.sleep)
		print(string.format("Going to sleep %d minutes", left))
	else
		timer:interval(30000)
		print(string.format("Wake-up in %d minutes", left))
	end
end


function wakeup:sleep (timer)
	wifi.setmode(wifi.NULLMODE, false)
	timer:unregister()
	rtctime.dsleep(self.left * 60e6, 1)
end


return wakeup