summaryrefslogtreecommitdiff
path: root/ucoo/base
diff options
context:
space:
mode:
authorNicolas Schodet2015-06-03 13:46:44 +0200
committerNicolas Schodet2019-10-07 00:44:44 +0200
commit574480980dcffbfa7cd6dbe71e88253eb4c9feba (patch)
tree5f1c1261341a7c2a56de01d8c6d0815b164be8ba /ucoo/base
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/base')
-rw-r--r--ucoo/base/proto/Config2
-rw-r--r--ucoo/base/proto/Module1
-rw-r--r--ucoo/base/proto/proto.cc257
-rw-r--r--ucoo/base/proto/proto.hh99
-rw-r--r--ucoo/base/proto/test/Makefile9
-rw-r--r--ucoo/base/proto/test/test_proto.cc67
-rw-r--r--ucoo/base/stdio/Module1
-rw-r--r--ucoo/base/stdio/stdio.cc68
-rw-r--r--ucoo/base/stdio/stdio.hh39
-rw-r--r--ucoo/base/test/Config5
-rw-r--r--ucoo/base/test/Module1
-rw-r--r--ucoo/base/test/test.cc118
-rw-r--r--ucoo/base/test/test.hh99
-rw-r--r--ucoo/base/test/test.host.cc42
-rw-r--r--ucoo/base/test/test.stm32.cc48
-rw-r--r--ucoo/base/test/test/Makefile9
-rw-r--r--ucoo/base/test/test/test_test.cc48
17 files changed, 913 insertions, 0 deletions
diff --git a/ucoo/base/proto/Config b/ucoo/base/proto/Config
new file mode 100644
index 0000000..4edaa6b
--- /dev/null
+++ b/ucoo/base/proto/Config
@@ -0,0 +1,2 @@
+[base/proto]
+args_max_size = 16
diff --git a/ucoo/base/proto/Module b/ucoo/base/proto/Module
new file mode 100644
index 0000000..68f866f
--- /dev/null
+++ b/ucoo/base/proto/Module
@@ -0,0 +1 @@
+base_proto_SOURCES = proto.cc
diff --git a/ucoo/base/proto/proto.cc b/ucoo/base/proto/proto.cc
new file mode 100644
index 0000000..838a79b
--- /dev/null
+++ b/ucoo/base/proto/proto.cc
@@ -0,0 +1,257 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "proto.hh"
+
+#include <cctype>
+
+namespace ucoo {
+
+Proto::Proto (Handler &handler, Stream &stream)
+ : handler_ (handler), stream_ (stream), step_ (IDLE)
+{
+}
+
+void
+Proto::accept ()
+{
+ int c;
+ while ((c = stream_.getc ()) != -1)
+ {
+ if (c == '!')
+ step_ = BANG;
+ else
+ {
+ switch (step_)
+ {
+ case IDLE:
+ // Nothing received yet.
+ break;
+ case BANG:
+ // Bang received yet.
+ if (std::isalpha (c))
+ {
+ cmd_ = c;
+ size_ = 0;
+ step_ = COMMAND;
+ }
+ else
+ {
+ handler_.proto_handle (*this, '?', 0, 0);
+ step_ = IDLE;
+ }
+ break;
+ case COMMAND:
+ // Command received yet.
+ if (c == '\r' || c == '\n')
+ {
+ handler_.proto_handle (*this, cmd_, args_, size_);
+ step_ = IDLE;
+ }
+ else if (c == '\'')
+ step_ = ARG_CHAR;
+ else if (c == '"')
+ step_ = ARG_STRING;
+ else
+ {
+ step_ = ARG_DIGIT;
+ accept_digit (c);
+ }
+ break;
+ case ARG_DIGIT:
+ step_ = COMMAND;
+ accept_digit (c);
+ break;
+ case ARG_CHAR:
+ step_ = COMMAND;
+ accept_char (c);
+ break;
+ case ARG_STRING:
+ if (c == '\r' || c == '\n')
+ {
+ handler_.proto_handle (*this, cmd_, args_, size_);
+ step_ = IDLE;
+ }
+ else
+ {
+ accept_char (c);
+ }
+ break;
+ }
+ }
+ }
+}
+
+void
+Proto::send (char cmd)
+{
+ char buf[3];
+ char *p = buf;
+ *p++ = '!';
+ *p++ = cmd;
+ *p++ = '\n';
+ stream_.write (buf, p - buf);
+}
+
+void
+Proto::send (char cmd, const char *fmt, int a0)
+{
+ send (cmd, fmt, a0, 0, 0, 0);
+}
+
+void
+Proto::send (char cmd, const char *fmt, int a0, int a1)
+{
+ send (cmd, fmt, a0, a1, 0, 0);
+}
+
+void
+Proto::send (char cmd, const char *fmt, int a0, int a1, int a2)
+{
+ send (cmd, fmt, a0, a1, a2, 0);
+}
+
+static inline void
+send_byte (char *buf, uint8_t b)
+{
+ static const char hexchars[] = "0123456789abcdef";
+ buf[0] = hexchars[b >> 4];
+ buf[1] = hexchars[b & 0xf];
+}
+
+static char *
+send_arg (char *buf, char fmt, int v)
+{
+ switch (fmt)
+ {
+ case 'L':
+ case 'l':
+ send_byte (buf, (v >> 24) & 0xff);
+ buf += 2;
+ send_byte (buf, (v >> 16) & 0xff);
+ buf += 2;
+ case 'H':
+ case 'h':
+ send_byte (buf, (v >> 8) & 0xff);
+ buf += 2;
+ case 'B':
+ case 'b':
+ send_byte (buf, v & 0xff);
+ buf += 2;
+ break;
+ default:
+ assert_unreachable ();
+ }
+ return buf;
+}
+
+void
+Proto::send (char cmd, const char *fmt, int a0, int a1, int a2, int a3)
+{
+ char buf[3 + 4 * 4 * 2]; // Large enough for the largest frame.
+ char *p = buf;
+ *p++ = '!';
+ *p++ = cmd;
+ if (*fmt)
+ {
+ p = send_arg (p, *fmt++, a0);
+ if (*fmt)
+ {
+ p = send_arg (p, *fmt++, a1);
+ if (*fmt)
+ {
+ p = send_arg (p, *fmt++, a2);
+ if (*fmt)
+ {
+ p = send_arg (p, *fmt++, a3);
+ }
+ }
+ }
+ }
+ *p++ = '\n';
+ stream_.write (buf, p - buf);
+}
+
+void
+Proto::send_buf (char cmd, const uint8_t *args, int size)
+{
+ int i;
+ char buf[3 + 2 * size];
+ char *p = buf;
+ *p++ = '!';
+ *p++ = cmd;
+ for (i = 0; i < size; i++)
+ {
+ send_byte (p, args[i]);
+ p += 2;
+ }
+ *p++ = '\n';
+ stream_.write (buf, p - buf);
+}
+
+void
+Proto::accept_digit (int c)
+{
+ // Test for argument list overflow.
+ if (size_ >= UCOO_CONFIG_BASE_PROTO_ARGS_MAX_SIZE)
+ {
+ handler_.proto_handle (*this, '?', 0, 0);
+ step_ = IDLE;
+ return;
+ }
+ // Convert from hexa.
+ if ('0' <= c && c <= '9')
+ c -= '0';
+ else if ('a' <= c && c <= 'f')
+ c -= 'a' - 10;
+ else if ('A' <= c && c <= 'F')
+ c -= 'A' - 10;
+ else
+ {
+ handler_.proto_handle (*this, '?', 0, 0);
+ step_ = IDLE;
+ return;
+ }
+ // Add digit.
+ args_[size_] <<= 4;
+ args_[size_] |= c;
+ if (step_ == COMMAND)
+ size_++;
+}
+
+void
+Proto::accept_char (int c)
+{
+ // Test for argument list overflow or unwanted char.
+ if (size_ >= UCOO_CONFIG_BASE_PROTO_ARGS_MAX_SIZE || !std::isprint (c))
+ {
+ handler_.proto_handle (*this, '?', 0, 0);
+ step_ = IDLE;
+ return;
+ }
+ // Add char.
+ args_[size_] = c;
+ size_++;
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/proto/proto.hh b/ucoo/base/proto/proto.hh
new file mode 100644
index 0000000..a4ee999
--- /dev/null
+++ b/ucoo/base/proto/proto.hh
@@ -0,0 +1,99 @@
+#ifndef ucoo_base_proto_proto_hh
+#define ucoo_base_proto_proto_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/common.hh"
+#include "ucoo/intf/stream.hh"
+
+#include "config/base/proto.hh"
+
+namespace ucoo {
+
+/// Support for old proto protocol.
+class Proto
+{
+ public:
+ /// Receivers should implement this interface.
+ class Handler
+ {
+ public:
+ /// Handle a received message.
+ virtual void proto_handle (Proto &proto, char cmd,
+ const uint8_t *args, int size) = 0;
+ };
+ public:
+ /// Constructor.
+ Proto (Handler &handler, Stream &stream);
+ /// Read from stream and handle any received message.
+ void accept ();
+ /// Send a message with no argument.
+ void send (char cmd);
+ /// Send a message with one argument.
+ void send (char cmd, const char *fmt, int a0);
+ /// Send a message with two arguments.
+ void send (char cmd, const char *fmt, int a0, int a1);
+ /// Send a message with three arguments.
+ void send (char cmd, const char *fmt, int a0, int a1, int a2);
+ /// Send a message with four arguments.
+ void send (char cmd, const char *fmt, int a0, int a1, int a2, int a3);
+ /// Send a message, with a byte buffer.
+ void send_buf (char cmd, const uint8_t *args, int size);
+ private:
+ /// Accept a digit to be used for args.
+ void accept_digit (int c);
+ /// Accept a quoted char to be used for args.
+ void accept_char (int c);
+ private:
+ /// Handler interface.
+ Handler &handler_;
+ /// Connected stream.
+ Stream &stream_;
+ /// Decoding step.
+ enum Step
+ {
+ /// Nothing received yet.
+ IDLE,
+ /// Exclamation mark received.
+ BANG,
+ /// Expecting argument or end of frame.
+ COMMAND,
+ /// Expecting second hex digit of an argument.
+ ARG_DIGIT,
+ /// Quote received, expect any char.
+ ARG_CHAR,
+ /// Double quote received, read a string.
+ ARG_STRING,
+ };
+ Step step_;
+ /// Received message current size.
+ int size_;
+ /// Message arguments being received.
+ uint8_t args_[UCOO_CONFIG_BASE_PROTO_ARGS_MAX_SIZE];
+ /// Command being received.
+ char cmd_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_base_proto_proto_hh
diff --git a/ucoo/base/proto/test/Makefile b/ucoo/base/proto/test/Makefile
new file mode 100644
index 0000000..f02be13
--- /dev/null
+++ b/ucoo/base/proto/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = host stm32f4
+PROGS = test_proto
+test_proto_SOURCES = test_proto.cc
+
+MODULES = base/proto base/test hal/usb
+
+include $(BASE)/build/top.mk
diff --git a/ucoo/base/proto/test/test_proto.cc b/ucoo/base/proto/test/test_proto.cc
new file mode 100644
index 0000000..9a11233
--- /dev/null
+++ b/ucoo/base/proto/test/test_proto.cc
@@ -0,0 +1,67 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/base/proto/proto.hh"
+
+#include "ucoo/arch/arch.hh"
+#include "ucoo/base/test/test.hh"
+
+class TestProto : public ucoo::Proto::Handler
+{
+ public:
+ void proto_handle (ucoo::Proto &proto, char cmd,
+ const uint8_t *args, int size)
+ {
+#define c(cmd, size) ((cmd) << 8 | (size))
+ switch (c (cmd, size))
+ {
+ // Receive various format.
+ case c ('a', 1):
+ case c ('a', 4):
+ case c ('b', 8):
+ // Send various formats.
+ proto.send ('h', "hh", -1, 1);
+ proto.send ('e', "B", 0xf0);
+ proto.send ('l', "HBB", 0x1234, 0x56, 0x78);
+ proto.send ('o', "L", 0x87654321u);
+ break;
+ default:
+ // Error, unknown command.
+ proto.send ('?');
+ return;
+ }
+ proto.send_buf (cmd, args, size);
+ }
+};
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::Stream &s = ucoo::test_stream ();
+ TestProto test_proto;
+ ucoo::Proto proto (test_proto, s);
+ while (1)
+ proto.accept ();
+}
+
diff --git a/ucoo/base/stdio/Module b/ucoo/base/stdio/Module
new file mode 100644
index 0000000..f9cffeb
--- /dev/null
+++ b/ucoo/base/stdio/Module
@@ -0,0 +1 @@
+base_stdio_SOURCES := stdio.cc
diff --git a/ucoo/base/stdio/stdio.cc b/ucoo/base/stdio/stdio.cc
new file mode 100644
index 0000000..91bbe00
--- /dev/null
+++ b/ucoo/base/stdio/stdio.cc
@@ -0,0 +1,68 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "stdio.hh"
+
+#include <unistd.h>
+
+namespace ucoo {
+
+/// Callback to read a stream.
+ssize_t
+stream_io_read (void *cookie, char *buf, size_t n)
+{
+ Stream *stream = static_cast<Stream *> (cookie);
+ int r = stream->read (buf, n);
+ if (r == -2)
+ return 0;
+ else
+ return r;
+}
+
+/// Callback to write a stream.
+ssize_t
+stream_io_write (void *cookie, const char *buf, size_t n)
+{
+ Stream *stream = static_cast<Stream *> (cookie);
+ int r = stream->write (buf, n);
+ return r;
+}
+
+/// Table of callback for fopencookie.
+static const cookie_io_functions_t stream_io_functions =
+{
+ stream_io_read,
+ stream_io_write,
+ NULL,
+ NULL,
+};
+
+FILE *
+fstreamopen (Stream &stream, const char *mode)
+{
+ // fopencookie is a glibc extension also implemented in newlib, nice!
+ return fopencookie (static_cast<void *> (&stream), mode,
+ stream_io_functions);
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/stdio/stdio.hh b/ucoo/base/stdio/stdio.hh
new file mode 100644
index 0000000..b56736a
--- /dev/null
+++ b/ucoo/base/stdio/stdio.hh
@@ -0,0 +1,39 @@
+#ifndef ucoo_base_stdio_stdio_hh
+#define ucoo_base_stdio_stdio_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/stream.hh"
+
+#include <cstdio>
+
+namespace ucoo {
+
+/// Open a stdio stream connecter to STREAM. The MODE parameter uses the same
+/// syntax as stdio fopen.
+FILE *
+fstreamopen (Stream &stream, const char *mode);
+
+} // namespace ucoo
+
+#endif // ucoo_base_stdio_stdio_hh
diff --git a/ucoo/base/test/Config b/ucoo/base/test/Config
new file mode 100644
index 0000000..133808b
--- /dev/null
+++ b/ucoo/base/test/Config
@@ -0,0 +1,5 @@
+[base/test]
+wait = false
+
+[base/test:stm32]
+wait = true
diff --git a/ucoo/base/test/Module b/ucoo/base/test/Module
new file mode 100644
index 0000000..9a3c77b
--- /dev/null
+++ b/ucoo/base/test/Module
@@ -0,0 +1 @@
+base_test_SOURCES := test.cc test.host.cc test.stm32.cc
diff --git a/ucoo/base/test/test.cc b/ucoo/base/test/test.cc
new file mode 100644
index 0000000..4f2e8f0
--- /dev/null
+++ b/ucoo/base/test/test.cc
@@ -0,0 +1,118 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoo/common.hh"
+#include "ucoo/base/stdio/stdio.hh"
+#include "ucoo/utils/delay.hh"
+
+#include "config/base/test.hh"
+
+#include <cstdarg>
+
+namespace ucoo {
+
+TestSuite::TestSuite (const char *test_suite)
+ : test_suite_ (test_suite), test_group_ ("none"),
+ test_nb_ (0), fail_nb_ (0), in_test_ (false)
+{
+ test_stream_setup ();
+ if (UCOO_CONFIG_BASE_TEST_WAIT)
+ {
+ Stream &s = test_stream ();
+ int n;
+ char b;
+ s.block (false);
+ do
+ {
+ printf ("waiting to start...\n");
+ delay (1);
+ n = s.read (&b, 1);
+ } while (n <= 0);
+ s.block ();
+ }
+}
+
+bool
+TestSuite::report ()
+{
+ assert (!in_test_);
+ printf ("test results: %d tests, %d fails\n", test_nb_, fail_nb_);
+ return fail_nb_ == 0;
+}
+
+void
+TestSuite::group (const char *test_group)
+{
+ test_group_ = test_group;
+}
+
+Test::Test (TestSuite &suite, const char *test)
+ : suite_ (suite), test_ (test), failed_ (false)
+{
+ assert (!suite_.in_test_);
+ suite_.in_test_ = true;
+ suite_.test_nb_++;
+}
+
+Test::~Test (void)
+{
+ if (!failed_)
+ printf ("%s:%s:%s:P: pass\n", suite_.test_suite_, suite_.test_group_,
+ test_);
+ suite_.in_test_ = false;
+}
+
+void
+Test::fail ()
+{
+ fail ("fail");
+}
+
+void
+Test::fail (const char *fail_fmt, ...)
+{
+ va_list ap;
+ assert (!failed_);
+ printf ("%s:%s:%s:F: ", suite_.test_suite_, suite_.test_group_, test_);
+ va_start (ap, fail_fmt);
+ vprintf (fail_fmt, ap);
+ va_end (ap);
+ putchar ('\n');
+ suite_.fail_nb_++;
+ failed_ = true;
+}
+
+void
+Test::info (const char *info_fmt, ...)
+{
+ va_list ap;
+ printf ("%s:%s:%s:I: ", suite_.test_suite_, suite_.test_group_, test_);
+ va_start (ap, info_fmt);
+ vprintf (info_fmt, ap);
+ va_end (ap);
+ putchar ('\n');
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/test/test.hh b/ucoo/base/test/test.hh
new file mode 100644
index 0000000..fd8ac32
--- /dev/null
+++ b/ucoo/base/test/test.hh
@@ -0,0 +1,99 @@
+#ifndef ucoo_base_test_test_hh
+#define ucoo_base_test_test_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/stream.hh"
+
+/// Test helpers to fail and break to end of current block.
+#define test_fail_break_unless(test, cond) \
+ ({ if (!(cond)) { \
+ test.fail (#cond); \
+ break; \
+ } })
+
+namespace ucoo {
+
+/// Get a reference to test stream. This stream is the preferred way to
+/// interact with a tester.
+Stream &
+test_stream (void);
+
+/// Setup stdin and stdout to use the reference test stream. On host, this
+/// function does nothing.
+void
+test_stream_setup (void);
+
+/// Test suite context.
+class TestSuite
+{
+ public:
+ /// Create a new test suite.
+ TestSuite (const char *test_suite);
+ /// Report the overall test results, return true if all tests passed.
+ bool report ();
+ /// Enter a new test group.
+ void group (const char *test_group);
+ private:
+ friend class Test;
+ /// Test suite name.
+ const char *test_suite_;
+ /// Test group name.
+ const char *test_group_;
+ /// Number of attempted tests.
+ int test_nb_;
+ /// Number of test failures.
+ int fail_nb_;
+ /// Running a test.
+ bool in_test_;
+};
+
+/// Test context.
+class Test
+{
+ public:
+ /// Start a new test, may be followed by fail, test stops at object
+ /// destruction.
+ Test (TestSuite &suite, const char *test);
+ /// Stop a test.
+ ~Test (void);
+ /// End the current test with a fail status.
+ void fail ();
+ /// End the current test with a fail status and message.
+ void __attribute__ ((format (printf, 2, 3)))
+ fail (const char *fail_fmt, ...);
+ /// Give info about the current test.
+ void __attribute__ ((format (printf, 2, 3)))
+ info (const char *info_fmt, ...);
+ private:
+ /// Attached test suite.
+ TestSuite &suite_;
+ /// Test name.
+ const char *test_;
+ /// Failed yet.
+ bool failed_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_base_test_test_hh
diff --git a/ucoo/base/test/test.host.cc b/ucoo/base/test/test.host.cc
new file mode 100644
index 0000000..d43d235
--- /dev/null
+++ b/ucoo/base/test/test.host.cc
@@ -0,0 +1,42 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoo/arch/host/host_stream.hh"
+
+namespace ucoo {
+
+Stream &
+test_stream (void)
+{
+ static HostStream s;
+ return s;
+}
+
+void
+test_stream_setup (void)
+{
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/test/test.stm32.cc b/ucoo/base/test/test.stm32.cc
new file mode 100644
index 0000000..3961a70
--- /dev/null
+++ b/ucoo/base/test/test.stm32.cc
@@ -0,0 +1,48 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "test.hh"
+
+#include "ucoo/hal/usb/usb.hh"
+#include "ucoo/arch/syscalls.newlib.hh"
+
+namespace ucoo {
+
+Stream &
+test_stream (void)
+{
+ static UsbStreamControl usc ("APBTeam", "test");
+ static UsbStream us (usc, 0);
+ return us;
+}
+
+void
+test_stream_setup (void)
+{
+ Stream *s = &test_stream ();
+ syscalls_streams[0] = s;
+ syscalls_streams[1] = s;
+ syscalls_streams[2] = s;
+}
+
+} // namespace ucoo
diff --git a/ucoo/base/test/test/Makefile b/ucoo/base/test/test/Makefile
new file mode 100644
index 0000000..610c71a
--- /dev/null
+++ b/ucoo/base/test/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = host stm32f4
+PROGS = test_test
+test_test_SOURCES = test_test.cc
+
+MODULES = utils base/test hal/usb
+
+include $(BASE)/build/top.mk
diff --git a/ucoo/base/test/test/test_test.cc b/ucoo/base/test/test/test_test.cc
new file mode 100644
index 0000000..dcc6b76
--- /dev/null
+++ b/ucoo/base/test/test/test_test.cc
@@ -0,0 +1,48 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/base/test/test.hh"
+#include "ucoo/arch/arch.hh"
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite test_suite ("test_test");
+ // Really, what a dumb test!
+ test_suite.group ("group one");
+ {
+ ucoo::Test test (test_suite, "the first test");
+ test.fail ("Oh no! test %d!", 123);
+ }
+ {
+ ucoo::Test test (test_suite, "try again");
+ test.info ("working harder...");
+ }
+ test_suite.group ("group two");
+ {
+ ucoo::Test test (test_suite, "simple test");
+ }
+ return test_suite.report () ? 0 : 1;
+}
+