Skip to content

Commit dcf636b

Browse files
committed
lua: added udp-sendto and tcp-sendto (cf. uhppoted/uhppote-core#17)
1 parent f1530d5 commit dcf636b

File tree

4 files changed

+150
-33
lines changed

4 files changed

+150
-33
lines changed

Makefile

+1-3
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,7 @@ lua-help: build regen
304304
$(LUABIN) set-time -h
305305

306306
lua-debug: lua
307-
# $(LUABIN) --debug --bind 192.168.1.100:0 --broadcast 192.168.1.255:60000 --events 0.0.0.0:60001 set-time-profile --controller 405419896 --profile 37 --start-date 2023-01-01 --end-date 2023-12-31 --weekdays Mon,Tue,Sun --segments "8:30-11:45, 13:15-17:30" --linked 33
308-
# $(LUABIN) --debug --bind 192.168.1.100:0 --broadcast 192.168.1.255:60000 --events 0.0.0.0:60001 add-task --controller 405419896 --door 3 --task "door-normally-closed" --at "08:30" --start-date 2023-01-01 --end-date 2023-12-31 --weekdays Mon,Tue,Sun --more-cards 5
309-
$(LUABIN) --debug --bind 192.168.1.100:0 --broadcast 192.168.1.255:60000 --events 0.0.0.0:60001 put-card --controller 405419896 --card 10058400 --start-date 2023-01-02 --end-date 2023-12-30 --doors 1:37,2,4 --PIN 6310
307+
$(LUABIN) --debug --bind 192.168.1.100:0 --broadcast 192.168.1.255:60000 --events 0.0.0.0:60001 get-controller
310308

311309
lua-cmd: lua
312310
$(LUABIN) --debug --bind 192.168.1.100:0 --broadcast 192.168.1.255:60000 --events 0.0.0.0:60001 $(COMMAND)

TODO.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
- [x] Zig
88
- [x] PHP
99
- [x] Erlang
10+
- [ ] Lua
1011
- [x] udp-sendto
1112
- [x] tcp-sendto
12-
- [x] controllers dictionary
13-
- [ ] Lua
13+
- [ ] controllers dictionary
1414
- [ ] models: rename 'device id' to controller and set type to 'controller'
1515
- [ ] remove uhppote::resolve
16+
- [ ] documentation
17+
- [ ] CHANGELOG
18+
- [ ] README
1619

1720
- [x] .ignore file (cf. https://github.com/uhppoted/uhppoted-codegen/issues/5)
1821
- [ ] root globs e.g. **/.DS_Store

bindings/lua/src/uhppote.lua

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
local uhppote = {}
22
local encode = require("src/encode")
33
local decode = require("src/decode")
4-
local udp = require("src/udp")
4+
local ut0311 = require("src/ut0311")
55

66
function uhppote.set_bind_address(address)
7-
udp.set_bind_address(address)
7+
ut0311.set_bind_address(address)
88
end
99

1010
function uhppote.set_broadcast_address(address)
11-
udp.set_broadcast_address(address)
11+
ut0311.set_broadcast_address(address)
1212
end
1313

1414
function uhppote.set_listen_address(address)
15-
udp.set_listen_address(address)
15+
ut0311.set_listen_address(address)
1616
end
1717

1818
function uhppote.set_debug(debug)
19-
udp.set_debug(debug)
19+
ut0311.set_debug(debug)
2020
end
2121

2222
function uhppote.get_all_controllers()
2323
local request = encode.get_controller_request(0)
24-
local replies = udp.broadcast(request)
24+
local replies = ut0311.broadcast(request)
2525

2626
if not replies then
2727
error("no response")
@@ -47,7 +47,7 @@ function uhppote.listen(handler, onerror)
4747
end
4848
end
4949

50-
udp.listen(f)
50+
ut0311.listen(f)
5151
end
5252

5353
{{range .model.functions}}
@@ -56,8 +56,9 @@ end
5656

5757
{{define "function"}}
5858
function uhppote.{{snakeCase .name}}({{template "args" .args}})
59+
local c = resolve(controller)
5960
local request = encode.{{snakeCase .request.name}}({{template "params" .args}})
60-
local reply = udp.send(request)
61+
local reply = ut0311.send(c, request)
6162
{{if .response}}
6263
if not reply then
6364
error("no response")
@@ -70,4 +71,22 @@ function uhppote.{{snakeCase .name}}({{template "args" .args}})
7071
end
7172
{{end}}
7273

74+
function resolve(controller)
75+
local c = {
76+
controller = controller,
77+
address = "",
78+
transport = "udp",
79+
80+
fields = function(self)
81+
return {
82+
"controller",
83+
"address",
84+
"transport"
85+
}
86+
end,
87+
}
88+
89+
return c
90+
end
91+
7392
return uhppote

bindings/lua/src/udp.lua renamed to bindings/lua/src/ut0311.lua

+117-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local udp = {}
1+
local ut0311 = {}
22
local socket = require("socket")
33
local READ <const> = 5 -- seconds
44
local READALL <const> = 2.5 -- seconds
@@ -11,7 +11,7 @@ local broadcast_port = 60000
1111
local listen_address = "*"
1212
local listen_port = 60001
1313

14-
function udp.set_bind_address(addr)
14+
function ut0311.set_bind_address(addr)
1515
if not addr or addr == "" then
1616
bind_address = "*"
1717
bind_port = 0
@@ -31,7 +31,7 @@ function udp.set_bind_address(addr)
3131
end
3232
end
3333

34-
function udp.set_broadcast_address(addr)
34+
function ut0311.set_broadcast_address(addr)
3535
if not addr or addr == "" then
3636
broadcast_address = "255.255.255.255"
3737
broadcast_port = 60000
@@ -51,7 +51,7 @@ function udp.set_broadcast_address(addr)
5151
end
5252
end
5353

54-
function udp.set_listen_address(addr)
54+
function ut0311.set_listen_address(addr)
5555
if not addr or addr == "" then
5656
listen_address = "*"
5757
listen_port = 60001
@@ -71,11 +71,11 @@ function udp.set_listen_address(addr)
7171
end
7272
end
7373

74-
function udp.set_debug(enabled)
74+
function ut0311.set_debug(enabled)
7575
debug = enabled
7676
end
7777

78-
function udp.broadcast(request)
78+
function ut0311.broadcast(request)
7979
local sock = socket.udp4()
8080
local ok, result = pcall(function()
8181
return broadcast(sock, request)
@@ -90,22 +90,19 @@ function udp.broadcast(request)
9090
end
9191
end
9292

93-
function udp.send(request)
94-
local sock = socket.udp4()
95-
local ok, result = pcall(function()
96-
return send(sock, request)
97-
end)
98-
99-
sock:close()
100-
101-
if not ok then
102-
error(result)
103-
else
104-
return result
93+
function ut0311.send(controller, request)
94+
if controller['address'] ~= "" and controller['transport'] == "tcp" then
95+
return tcp_sendto(controller['address'], request)
96+
end
97+
98+
if controller['address'] ~= "" then
99+
return udp_sendto(controller['address'], request)
105100
end
101+
102+
return udp_broadcast_to(request)
106103
end
107104

108-
function udp.listen(handler)
105+
function ut0311.listen(handler)
109106
local f = function(packet)
110107
dump(packet)
111108
handler(packet)
@@ -144,6 +141,92 @@ function broadcast(sock, request)
144141
return read_all(sock)
145142
end
146143

144+
function udp_broadcast_to(request)
145+
local sock = socket.udp4()
146+
local ok, result = pcall(function()
147+
return send(sock, request)
148+
end)
149+
150+
sock:close()
151+
152+
if not ok then
153+
error(result)
154+
else
155+
return result
156+
end
157+
end
158+
159+
function udp_sendto(address, request)
160+
dump(request)
161+
162+
addr,port = addrport(address)
163+
164+
local sock = socket.udp4()
165+
local ok, result = pcall(function()
166+
if sock:setsockname(bind_address, 0) ~= 1 then
167+
error("error binding to address")
168+
end
169+
170+
sock:settimeout(READ, "b")
171+
sock:settimeout(READALL, "t")
172+
sock:sendto(request, addr, port)
173+
174+
-- set-ip doesn't return a reply
175+
if string.byte(request, 2) == 0x96 then
176+
return {}, nil
177+
end
178+
179+
return read(sock)
180+
end)
181+
182+
sock:close()
183+
184+
if not ok then
185+
error(result)
186+
else
187+
return result
188+
end
189+
end
190+
191+
function tcp_sendto(address, request)
192+
dump(request)
193+
194+
addr,port = addrport(address)
195+
196+
local sock = socket.tcp4()
197+
local ok, result = pcall(function()
198+
if sock:connect(addr, port) ~= 1 then
199+
error("error connecting to ", addr, port)
200+
end
201+
202+
sock:settimeout(READ, "b")
203+
sock:settimeout(READALL, "t")
204+
sock:send(request)
205+
206+
-- set-ip doesn't return a reply
207+
if string.byte(request, 2) == 0x96 then
208+
return {}, nil
209+
end
210+
211+
local packet = sock:receive('*a')
212+
if packet and #packet == 64 then
213+
dump(packet)
214+
return packet
215+
end
216+
217+
218+
return nil
219+
end)
220+
221+
sock:close()
222+
223+
if not ok then
224+
error(result)
225+
else
226+
return result
227+
end
228+
end
229+
147230
function send(sock, request)
148231
dump(request)
149232

@@ -235,6 +318,20 @@ function listen_to(sock, f)
235318
end
236319
end
237320

321+
function addrport(address)
322+
if address and address ~= "" then
323+
local addr, port = address:match("^(.-):(%d*)$")
324+
325+
if addr and port and addr ~= "" and port ~= "" then
326+
return addr, tonumber(port)
327+
elseif addr and addr ~= "" then
328+
return addr, 60000
329+
end
330+
end
331+
332+
return "255.255.255.255", 60000
333+
end
334+
238335
function dump(packet)
239336
if debug then
240337
local ix = 1
@@ -255,4 +352,4 @@ function dump(packet)
255352
end
256353
end
257354

258-
return udp
355+
return ut0311

0 commit comments

Comments
 (0)