From 8863edf78b38b38ab5323086d73e125a68ae8018 Mon Sep 17 00:00:00 2001
From: Brendan Bartels <bbartels@iastate.edu>
Date: Thu, 30 Mar 2017 13:17:02 -0500
Subject: [PATCH] quad: Add UART tests to CI

---
 quad/Makefile                          |  1 +
 quad/scripts/tests/test_unix_uart.py   | 54 --------------------
 quad/scripts/tests/test_unix_uart.rb   | 68 ++++++++++++++++++++++++++
 quad/src/virt_quad/hw_impl_unix_uart.c |  6 ++-
 4 files changed, 73 insertions(+), 56 deletions(-)
 delete mode 100755 quad/scripts/tests/test_unix_uart.py
 create mode 100644 quad/scripts/tests/test_unix_uart.rb

diff --git a/quad/Makefile b/quad/Makefile
index 98427ec80..f27a39d37 100644
--- a/quad/Makefile
+++ b/quad/Makefile
@@ -39,6 +39,7 @@ test: all
 	$(MAKE) -C src/computation_graph test
 	$(MAKE) -C src/quad_app test
 	ruby scripts/tests/test_safety_checks.rb
+	ruby scripts/tests/test_unix_uart.rb
 
 clean:
 	rm -rf $(INCDIR) $(LIBDIR) $(OUTDIR) $(EXEDIR)
diff --git a/quad/scripts/tests/test_unix_uart.py b/quad/scripts/tests/test_unix_uart.py
deleted file mode 100755
index 760f545a1..000000000
--- a/quad/scripts/tests/test_unix_uart.py
+++ /dev/null
@@ -1,54 +0,0 @@
-#!/usr/bin/env python
-
-import sys
-import time
-import os
-
-import threading
-
-path = os.path.dirname(__file__) + '/../../bin/virt-quad-fifos/'
-
-def create_msg():
-    msg = bytes()
-    msg += b'\xBE'
-
-    msg += b'\x01'
-    msg += b'\x00'
-
-    msg += b'\x00'
-    msg += b'\x00'
-
-    msg += b'\x00'
-    msg += b'\x00'
-
-    print msg
-
-    checksum = 0
-    for b in msg:
-        checksum ^= b
-    msg += checksum.to_bytes(1, 'little')
-    return msg
-
-def listen():
-    with open(path + 'uart-tx', 'r') as fifo:
-        while True:
-            c = fifo.read()
-            if c:
-                print c
-
-def do_test():
-    # Start a listener
-    t = threading.Thread(target=listen)
-    t.daemon = True
-    t.start()
-
-    fifo = open(path + 'uart-rx', 'w')
-    fifo.write(b'\xBE\x01\x00\x00\x00\x00\x00\xBF')
-    fifo.write(b'\xBE\x01\x00\x00\x00\x00\x00\xBF')
-    fifo.write(b'\xBE\x01\x00\x00\x00\x00\x00\xBF')
-
-if __name__ == '__main__':
-    for i in range(1):
-        print("Test ", i)
-        do_test()
-        time.sleep(1)
diff --git a/quad/scripts/tests/test_unix_uart.rb b/quad/scripts/tests/test_unix_uart.rb
new file mode 100644
index 000000000..6621744e3
--- /dev/null
+++ b/quad/scripts/tests/test_unix_uart.rb
@@ -0,0 +1,68 @@
+#!/usr/bin/env ruby
+
+
+# UART test
+#
+# This test is pretty simple, just a UART smoke test, using
+# the debug callback on the quad
+
+GEAR_ON = 170800
+GEAR_OFF = 118300
+
+GEAR = "virt-quad-fifos/pwm-input-gear"
+UART_RX = "virt-quad-fifos/uart-rx"
+UART_TX = "virt-quad-fifos/uart-tx"
+
+require 'test/unit/assertions'
+require 'thread'
+include Test::Unit::Assertions
+
+script_dir = File.expand_path(File.dirname(__FILE__))
+bin_dir = script_dir + "/../../bin/"
+Dir.chdir(bin_dir)
+
+# Start virtual quad
+quad = Process.spawn("./virt-quad")
+
+sleep 1
+
+#################
+#  Begin Tests
+#################
+
+begin
+
+  # Flip gear on
+  File.write(GEAR, GEAR_ON)
+  sleep 0.015
+
+  for j in 1..10
+    # Send a debug command
+    File.write(UART_RX, [0xBE, 1, 0, 0, 0, 0, 0, 0xBF].pack("CCCCCCCC"))
+
+    fifo = File.open(UART_TX)
+    msg = []
+    for i in 1..7
+      sleep 0.010
+      msg.push(fifo.read(1))
+    end
+    length = msg[5..7].join().unpack("S")[0]
+    msg = []
+    for i in 1..length
+      sleep 0.010
+      msg.push(fifo.read(1))
+    end
+    fifo.close
+
+    puts msg.join()
+    assert_equal(msg.join().force_encoding("UTF-8"), "Packets received: #{j}")
+  end
+
+  puts "Basic UART test passed."
+
+ensure
+
+  Process.kill(9, quad)
+
+end
+
diff --git a/quad/src/virt_quad/hw_impl_unix_uart.c b/quad/src/virt_quad/hw_impl_unix_uart.c
index 5790bb2f4..e39fbedfc 100644
--- a/quad/src/virt_quad/hw_impl_unix_uart.c
+++ b/quad/src/virt_quad/hw_impl_unix_uart.c
@@ -4,13 +4,11 @@
 #include <sys/types.h>
 #include <fcntl.h>
 
-static char *fifo_name_rx;
 static char *fifo_full_name_rx;
 static char *fifo_full_name_tx;
 static int fifo_rx;
 
 int unix_uart_reset(struct UARTDriver *self) {
-  fifo_name_rx = "uart-rx";
   fifo_full_name_rx = VIRT_QUAD_FIFOS_DIR "/uart-rx";
   fifo_full_name_tx = VIRT_QUAD_FIFOS_DIR "/uart-tx";
 
@@ -30,6 +28,7 @@ int unix_uart_reset(struct UARTDriver *self) {
 int unix_uart_write(struct UARTDriver *self, unsigned char c) {
   int fifo = open(fifo_full_name_tx, O_WRONLY | O_NONBLOCK);
   if (fifo >= 0) {
+    printf("%s: %x\n", "uart-tx", c);
     write(fifo, &c, 1);
   }
   close(fifo);
@@ -38,5 +37,8 @@ int unix_uart_write(struct UARTDriver *self, unsigned char c) {
 
 int unix_uart_read(struct UARTDriver *self, unsigned char *c) {
   int err = read(fifo_rx, c, 1);
+  if (err > 0) {
+    printf("%s: %x\n", "uart-rx", *c);
+  }
   return err <= 0;
 }
-- 
GitLab