diff --git a/script/cpu-usage.py b/script/cpu-net-monitor.py similarity index 71% rename from script/cpu-usage.py rename to script/cpu-net-monitor.py index c3e1cba..a507889 100755 --- a/script/cpu-usage.py +++ b/script/cpu-net-monitor.py @@ -13,9 +13,14 @@ def cpu_load_serial_driver(device:str, retry_interval:float=5.0): avg_usage_to_serial(port) except serial.SerialException as e: print(f"[!] Could not connect to device: {str(e)}", file=sys.stderr) + else: + port.close() sleep(retry_interval) def avg_usage_to_serial(port:serial.Serial): + net = psutil.net_io_counters() + net_tx = net.bytes_sent + net_rx = net.bytes_recv port.write(struct.pack("BB", 0, 0)) port.flush() while True: @@ -27,6 +32,15 @@ def avg_usage_to_serial(port:serial.Serial): except serial.SerialException as e: print(f"[!] Failed writing payload to device: {str(e)}", file=sys.stderr) break + net = psutil.net_io_counters() + try: + port.write(struct.pack("BBBB", 2, 2, int(net.bytes_sent > net_tx), int(net.bytes_recv > net_rx))) + port.flush() + except serial.SerialException as e: + print(f"[!] Failed writing payload to device: {str(e)}", file=sys.stderr) + break + net_rx = net.bytes_recv + net_tx = net.bytes_sent if __name__ == "__main__": diff --git a/src/main.rs b/src/main.rs index 632b49a..4d74a23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,8 @@ fn main() -> ! { let timer2 = Timer2Pwm::new(dp.TC2, Prescaler::Direct); let pins = arduino_hal::pins!(dp); let mut led_load = pins.d6.into_output(); + let mut led_rx = pins.d5.into_output(); // green + let mut led_tx = pins.d4.into_output(); // red let button = pins.d2.into_pull_up_input(); let mut cpu_leds = FourLedDisplay::new( pins.d3.into_output().into_pwm(&timer2), @@ -36,6 +38,8 @@ fn main() -> ! { let mut serial = arduino_hal::default_serial!(dp, pins, 57600); led_load.set_high(); + led_rx.set_high(); + led_tx.set_high(); // prepare display let mut display: GraphicsMode<_> = Builder::new().with_size(sh1106::prelude::DisplaySize::Display128x64).connect_i2c(i2c).into(); @@ -58,6 +62,8 @@ fn main() -> ! { cpu_leds.set(4, 255); led_load.set_low(); + led_tx.set_low(); + led_rx.set_low(); let mut pkt_builder = PacketBuilder::new(); @@ -83,6 +89,12 @@ fn main() -> ! { cpu_leds.set_many(payload[0], payload[1], payload[2], payload[3]); } }, + PacketId::NetStatePacket => { + if let Some(payload) = pkt.payload && payload.len() == 2 { + if payload[0] == 0 { led_tx.set_low() } else { led_tx.set_high() }; + if payload[1] == 0 { led_rx.set_low() } else { led_rx.set_high() }; + } + }, _ => {}, // TODO log it? } } diff --git a/src/packet.rs b/src/packet.rs index 1f9a5ae..5e85e3d 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -5,6 +5,7 @@ pub enum PacketId { Invalid = 0xFF, Reset = 0x00, SetLedsPacket = 0x01, + NetStatePacket = 0x02, } impl From for PacketId { @@ -12,6 +13,7 @@ impl From for PacketId { match x { 0 => PacketId::Reset, 1 => PacketId::SetLedsPacket, + 2 => PacketId::NetStatePacket, _ => PacketId::Invalid, } } @@ -53,7 +55,7 @@ impl PacketBuilder { PacketBuilderStep::ID => { let id = PacketId::from(byte); match id { - PacketId::SetLedsPacket | PacketId::Reset => { + PacketId::SetLedsPacket | PacketId::Reset | PacketId::NetStatePacket => { self.id = id; self.step = PacketBuilderStep::SIZE; },