enet-validator/emu-probes/probe_breathe.py

50 lines
1.7 KiB
Python
Raw Normal View History

import socket,struct,time,math,random
DUT=("192.168.68.200",31772)
HOST_IP=[192,168,68,1]
HOST_MAC=bytes.fromhex("300ed559bb36")
def env(f,ty,sub,total):
f[0:4]=b"ENet"; struct.pack_into("<I",f,4,ty)
f[0x0A]=0x1c; f[0x0B]=0x7c; f[0x12]=1
struct.pack_into("<H",f,0x14,total); struct.pack_into("<H",f,0x18,sub)
def enc(src,r,b12):
rot=(b12^r)&511; out=bytearray(512)
for i in range(512):
cnt=((512-rot-i)&255) if i<512-rot else ((rot-(i-(512-rot)))&255)
out[i]=src[(i+rot)%512]^cnt
return out
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.settimeout(2)
s.bind(("192.168.68.1",0))
# identity
f=bytearray(102); env(f,2,1,102)
f[0x1A:0x1E]=bytes([1,68,168,192]); f[0x1E:0x24]=HOST_MAC
s.sendto(bytes(f),DUT)
try: s.recvfrom(2048)
except socket.timeout: pass
# claim
b12=random.randrange(1,2**31); nonce=random.randrange(1,2**31)
f=bytearray(552); env(f,2,5,552)
f[0x28:0x2C]=b"AECH"
struct.pack_into("<I",f,0x88,nonce); struct.pack_into("<I",f,0x8C,b12)
s.sendto(bytes(f),DUT)
try:
d,_=s.recvfrom(2048); print("AERE ok, B12=%08x"%b12)
except socket.timeout: print("no AERE")
print("STREAMING 90s: ch1 breathing 3s period. WATCH THE STRIP NOW")
t0=time.time(); ctr=0
while time.time()-t0<90:
t=time.time()-t0
v=int((math.sin(t*2*math.pi/3)*0.5+0.5)*255)
p1=bytearray(512); p1[1]=v
r1=random.randrange(1,2**31); r2=random.randrange(1,2**31)
f=bytearray(1084); env(f,4,1,1084)
struct.pack_into("<I",f,0x1A,ctr)
f[0x20]=1; f[0x21]=1
struct.pack_into("<I",f,0x22,r1); struct.pack_into("<I",f,0x26,r2)
f[0x2E]=2; f[0x32]=1; f[0x35]=2; f[0x37]=2; f[0x38]=1
f[0x3C:0x23C]=enc(p1,r1,b12)
f[0x23C:0x43C]=enc(bytearray(512),r2,b12)
s.sendto(bytes(f),DUT)
ctr+=1; time.sleep(0.033)
print("done")