-
Notifications
You must be signed in to change notification settings - Fork 5
/
receive.py
70 lines (53 loc) · 1.61 KB
/
receive.py
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
import socket
import io
import struct
"""
LiFX test receiver
Author: Petr Klus
"""
def process_incoming_data(data, addr):
sio = io.BytesIO(data)
size, = struct.unpack("<H", sio.read(2))
sec_part, = struct.unpack("<H", sio.read(2))
protocol = sec_part % 4096
if protocol != 1024:
print("Not LiFX packet")
return {
"packet_id": "-1",
"message": "Not LiFX protocol"
}
source, = struct.unpack("<I", sio.read(4))
# skip over frame address except sequence number
sio.read(15)
seqnum, = struct.unpack("<B", sio.read(1))
res, payloadtype, res2 = struct.unpack("<QHH", sio.read(12))
if payloadtype == 45:
# ACK
print("Packet with seqnum", seqnum, "ACKed")
return {
"packet_id": payloadtype,
"seqnum": seqnum,
}
elif payloadtype == 107:
print("Light state received with seqnum", seqnum)
print("MSG:", size, protocol, payloadtype)
return {
"packet_id": payloadtype,
"seqnum": seqnum,
}
# fallback for not currently processed packets
return {
"packet_id": "-1",
"message": "Payloadtype: {} not currently supported".format(
payloadtype)
}
if __name__ == "__main__":
# standalone demo
UDP_IP = "0.0.0.0"
UDP_PORT = 56700
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
process_incoming_data(data, addr)