diff --git a/src/modules/uORB/uORBMain.cpp b/src/modules/uORB/uORBMain.cpp index f144ac8e27..147ce5fbb3 100644 --- a/src/modules/uORB/uORBMain.cpp +++ b/src/modules/uORB/uORBMain.cpp @@ -90,7 +90,7 @@ uorb_main(int argc, char *argv[]) */ if (!strcmp(argv[1], "test")) { - uORBTest::UnitTest t; + uORBTest::UnitTest &t = uORBTest::UnitTest::instance(); return t.test(); } @@ -99,7 +99,7 @@ uorb_main(int argc, char *argv[]) */ if (!strcmp(argv[1], "latency_test")) { - uORBTest::UnitTest t; + uORBTest::UnitTest &t = uORBTest::UnitTest::instance(); if (argc > 2 && !strcmp(argv[2], "medium")) { return t.latency_test(ORB_ID(orb_test_medium), true); } else if (argc > 2 && !strcmp(argv[2], "large")) { @@ -118,7 +118,5 @@ uorb_main(int argc, char *argv[]) } usage(); - errx(-EINVAL, "unrecognized command, try 'start', 'test', 'latency_test' or 'status'"); + return -EINVAL; } - - diff --git a/src/modules/uORB/uORBTest_UnitTest.cpp b/src/modules/uORB/uORBTest_UnitTest.cpp index 6fd960352f..c1ea73959f 100644 --- a/src/modules/uORB/uORBTest_UnitTest.cpp +++ b/src/modules/uORB/uORBTest_UnitTest.cpp @@ -36,6 +36,12 @@ #include #include +uORBTest::UnitTest &uORBTest::UnitTest::instance() +{ + static uORBTest::UnitTest t; + return t; +} + int uORBTest::UnitTest::pubsublatency_main(void) { /* poll on test topic and output latency */ @@ -276,12 +282,8 @@ int uORBTest::UnitTest::test_note(const char *fmt, ...) return OK; } -int uORBTest::UnitTest::pubsubtest_threadEntry( char* const* data ) +int uORBTest::UnitTest::pubsubtest_threadEntry(char* const argv[]) { - uORBTest::UnitTest* t = (uORBTest::UnitTest*) data; - if( data != nullptr ) - { - return t->pubsublatency_main(); - } - return uORB::ERROR; + uORBTest::UnitTest &t = uORBTest::UnitTest::instance(); + return t.pubsublatency_main(); } diff --git a/src/modules/uORB/uORBTest_UnitTest.hpp b/src/modules/uORB/uORBTest_UnitTest.hpp index 366765fa18..f19c95451d 100644 --- a/src/modules/uORB/uORBTest_UnitTest.hpp +++ b/src/modules/uORB/uORBTest_UnitTest.hpp @@ -68,15 +68,22 @@ class uORBTest::UnitTest { public: + // Singleton pattern + static uORBTest::UnitTest &instance(); + ~UnitTest() {} int test(); template int latency_test(orb_id_t T, bool print); int info(); private: - static int pubsubtest_threadEntry( char* const* argv ); + UnitTest() : pubsubtest_passed(false), pubsubtest_print(false) {} + + // Disallow copy + UnitTest(const uORBTest::UnitTest &) {}; + static int pubsubtest_threadEntry(char* const argv[]); int pubsublatency_main(void); - bool pubsubtest_passed = false; - bool pubsubtest_print = false; + bool pubsubtest_passed; + bool pubsubtest_print; int pubsubtest_res = OK; int test_fail(const char *fmt, ...); @@ -93,14 +100,16 @@ int uORBTest::UnitTest::latency_test(orb_id_t T, bool print) int pfd0 = orb_advertise(T, &t); - char * const args[2] = { (char* const) this, 0 }; + char * const args[1] = { NULL }; pubsubtest_print = print; - pubsubtest_passed = false; /* test pub / sub latency */ + // Can't pass a pointer in args, must be a null terminated + // array of strings because the strings are copied to + // prevent access if the caller data goes out of scope int pubsub_task = px4_task_spawn_cmd("uorb_latency", SCHED_DEFAULT, SCHED_PRIORITY_MAX - 5,