From cc5c679b31020840ac6714e9ac64b47f46fdc1ae Mon Sep 17 00:00:00 2001
From: Sebastien Bourdeauducq <sb@m-labs.hk>
Date: Sat, 25 Apr 2020 20:45:25 +0800
Subject: [PATCH] moninj: implement monitoring

---
 runtime/src/main.rs   |  2 +-
 runtime/src/moninj.rs | 23 +++++++++++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/runtime/src/main.rs b/runtime/src/main.rs
index 9d2f538..b920e66 100644
--- a/runtime/src/main.rs
+++ b/runtime/src/main.rs
@@ -1,6 +1,6 @@
 #![no_std]
 #![no_main]
-#![recursion_limit="512"]  // for futures_util::select!
+#![recursion_limit="1024"]  // for futures_util::select!
 
 extern crate alloc;
 extern crate log;
diff --git a/runtime/src/moninj.rs b/runtime/src/moninj.rs
index fe255f4..aa6dbbe 100644
--- a/runtime/src/moninj.rs
+++ b/runtime/src/moninj.rs
@@ -54,7 +54,7 @@ enum DeviceMessage {
     InjectionStatus = 1
 }
 
-fn read_probe(channel: i32, probe: u8) -> i32 {
+fn read_probe(channel: i32, probe: i8) -> i32 {
     unsafe {
         csr::rtio_moninj::mon_chan_sel_write(channel as _);
         csr::rtio_moninj::mon_probe_sel_write(probe as _);
@@ -149,7 +149,26 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer) -> Result<()>
                 }
             },
             _ = timeout_f => {
-                warn!("tick");
+                for (&(channel, probe), previous) in probe_watch_list.iter_mut() {
+                    let current = read_probe(channel, probe);
+                    if previous.is_none() || previous.unwrap() != current {
+                        write_i8(&stream, DeviceMessage::MonitorStatus.to_i8().unwrap()).await?;
+                        write_i32(&stream, channel).await?;
+                        write_i8(&stream, probe).await?;
+                        write_i32(&stream, current).await?;
+                        *previous = Some(current);
+                    }
+                }
+                for (&(channel, overrd), previous) in inject_watch_list.iter_mut() {
+                    let current = read_injection_status(channel, overrd);
+                    if previous.is_none() || previous.unwrap() != current {
+                        write_i8(&stream, DeviceMessage::InjectionStatus.to_i8().unwrap()).await?;
+                        write_i32(&stream, channel).await?;
+                        write_i8(&stream, overrd).await?;
+                        write_i8(&stream, current).await?;
+                        *previous = Some(current);
+                    }
+                }
                 next_check = next_check + Milliseconds(200);
             }
         }