2022-08-10 03:44:15 +02:00
|
|
|
#!/usr/bin/env python
|
2022-08-10 23:02:04 +02:00
|
|
|
import sys
|
2022-08-10 03:44:15 +02:00
|
|
|
import struct
|
2022-08-10 23:02:04 +02:00
|
|
|
from time import sleep
|
2022-08-10 03:44:15 +02:00
|
|
|
|
2022-08-10 23:02:04 +02:00
|
|
|
import serial
|
2022-08-10 03:44:15 +02:00
|
|
|
import psutil
|
|
|
|
|
2022-08-10 23:02:04 +02:00
|
|
|
def cpu_load_serial_driver(device:str, retry_interval:float=5.0):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
port = serial.Serial(device, baudrate=57600)
|
|
|
|
avg_usage_to_serial(port)
|
|
|
|
except serial.SerialException as e:
|
|
|
|
print(f"[!] Could not connect to device: {str(e)}", file=sys.stderr)
|
|
|
|
sleep(retry_interval)
|
|
|
|
|
|
|
|
def avg_usage_to_serial(port:serial.Serial):
|
|
|
|
port.write(struct.pack("BB", 0, 0))
|
|
|
|
port.flush()
|
2022-08-10 03:44:15 +02:00
|
|
|
while True:
|
|
|
|
# Map float [0:100] to int [0:255], square it to put more values in the lower end, where led is more sensible
|
2022-08-10 23:02:04 +02:00
|
|
|
load = [ int(((x/100) **2) * 255) for x in psutil.cpu_percent(0.05, percpu=True) ] # mypy whines but percpu returns a list
|
|
|
|
try:
|
|
|
|
port.write(struct.pack("BBBBBB", 1, 4, *load))
|
|
|
|
port.flush()
|
|
|
|
except serial.SerialException as e:
|
|
|
|
print(f"[!] Failed writing payload to device: {str(e)}", file=sys.stderr)
|
|
|
|
break
|
|
|
|
|
2022-08-10 03:44:15 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("[!] No device specified")
|
2022-08-10 23:02:04 +02:00
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
cpu_load_serial_driver(sys.argv[1])
|
2022-08-10 03:44:15 +02:00
|
|
|
|