int colour; /* Text colour to use for output, -1 for none */
};
+/**
+ * sandbox_serial_written() - Get the total number of characters written
+ *
+ * This returns the number of characters written by the sandbox serial
+ * device. It is intended for performing tests of the serial subsystem
+ * where a console buffer cannot be used. The absolute number should not be
+ * relied upon; call this function twice and compare the difference.
+ *
+ * Return: The number of characters written
+ */
+size_t sandbox_serial_written(void);
+
+/**
+ * sandbox_serial_endisable() - Enable or disable serial output
+ * @enabled: Whether serial output should be enabled or not
+ *
+ * This allows tests to enable or disable the sandbox serial output. All
+ * processes relating to writing output (except the actual writing) will be
+ * performed.
+ */
+void sandbox_serial_endisable(bool enabled);
+
/**
* struct sandbox_serial_priv - Private data for this driver
*
DECLARE_GLOBAL_DATA_PTR;
+static size_t _sandbox_serial_written = 1;
+static bool sandbox_serial_enabled = true;
+
+size_t sandbox_serial_written(void)
+{
+ return _sandbox_serial_written;
+}
+
+void sandbox_serial_endisable(bool enabled)
+{
+ sandbox_serial_enabled = enabled;
+}
+
/**
* output_ansi_colour() - Output an ANSI colour code
*
{
struct sandbox_serial_priv *priv = dev_get_priv(dev);
- sandbox_print_color(dev);
- os_write(1, &ch, 1);
if (ch == '\n')
priv->start_of_line = true;
+ if (sandbox_serial_enabled) {
+ sandbox_print_color(dev);
+ os_write(1, &ch, 1);
+ }
+ _sandbox_serial_written += 1;
return 0;
}
size_t len)
{
struct sandbox_serial_priv *priv = dev_get_priv(dev);
+ ssize_t ret;
- sandbox_print_color(dev);
if (s[len - 1] == '\n')
priv->start_of_line = true;
- return os_write(1, s, len);
+ if (sandbox_serial_enabled) {
+ sandbox_print_color(dev);
+ ret = os_write(1, s, len);
+ if (ret < 0)
+ return ret;
+ } else {
+ ret = len;
+ }
+ _sandbox_serial_written += ret;
+ return ret;
}
static int sandbox_serial_pending(struct udevice *dev, bool input)
#include <log.h>
#include <serial.h>
#include <dm.h>
+#include <asm/serial.h>
#include <dm/test.h>
#include <test/test.h>
#include <test/ut.h>
+static const char test_message[] =
+ "This is a test message\n"
+ "consisting of multiple lines\n";
+
static int dm_test_serial(struct unit_test_state *uts)
{
+ int i;
struct serial_device_info info_serial = {0};
struct udevice *dev_serial;
+ size_t start, putc_written;
+
uint value_serial;
ut_assertok(uclass_get_device_by_name(UCLASS_SERIAL, "serial",
SERIAL_8_BITS,
SERIAL_TWO_STOP)));
+ /* Verify that putc and puts print the same number of characters */
+ sandbox_serial_endisable(false);
+ start = sandbox_serial_written();
+ for (i = 0; i < sizeof(test_message) - 1; i++)
+ serial_putc(test_message[i]);
+ putc_written = sandbox_serial_written();
+ serial_puts(test_message);
+ sandbox_serial_endisable(true);
+ ut_asserteq(putc_written - start,
+ sandbox_serial_written() - putc_written);
+
return 0;
}