From c3448c19c46eea5d01e17f634dc80eff0d393947 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Sat, 24 Nov 2018 11:37:52 -0500 Subject: [PATCH] make vcdevtest a generic cdev test --- platforms/posix/cmake/sitl_tests.cmake | 28 +++---- src/lib/cdev/CMakeLists.txt | 4 + .../cdev/test}/CMakeLists.txt | 12 +-- .../cdev/test/cdevtest_example.cpp} | 81 ++++++------------- .../cdev/test/cdevtest_example.h} | 10 +-- .../cdev/test/cdevtest_main.cpp} | 13 +-- .../cdev/test/cdevtest_start.cpp} | 21 ++--- src/platforms/px4_app.h | 3 +- 8 files changed, 74 insertions(+), 98 deletions(-) rename src/{platforms/posix/tests/vcdev_test => lib/cdev/test}/CMakeLists.txt (92%) rename src/{platforms/posix/tests/vcdev_test/vcdevtest_example.cpp => lib/cdev/test/cdevtest_example.cpp} (80%) rename src/{platforms/posix/tests/vcdev_test/vcdevtest_example.h => lib/cdev/test/cdevtest_example.h} (95%) rename src/{platforms/posix/tests/vcdev_test/vcdevtest_main.cpp => lib/cdev/test/cdevtest_main.cpp} (92%) rename src/{platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp => lib/cdev/test/cdevtest_start.cpp} (84%) diff --git a/platforms/posix/cmake/sitl_tests.cmake b/platforms/posix/cmake/sitl_tests.cmake index 2ef5a9722d..80958afbe3 100644 --- a/platforms/posix/cmake/sitl_tests.cmake +++ b/platforms/posix/cmake/sitl_tests.cmake @@ -100,24 +100,24 @@ set_tests_properties(shutdown PROPERTIES PASS_REGULAR_EXPRESSION "Shutting down" sanitizer_fail_test_on_error(shutdown) # Dynamic module loading test -add_test(NAME dyn - COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh - $ - none - none - test_dyn_hello - ${PX4_SOURCE_DIR} - ${PX4_BINARY_DIR} - $ - WORKING_DIRECTORY ${SITL_WORKING_DIR}) -set_tests_properties(dyn PROPERTIES PASS_REGULAR_EXPRESSION "1: PASSED") -sanitizer_fail_test_on_error(dyn) +#add_test(NAME dyn +# COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh +# $ +# none +# none +# test_dyn_hello +# ${PX4_SOURCE_DIR} +# ${PX4_BINARY_DIR} +# $ +# WORKING_DIRECTORY ${SITL_WORKING_DIR}) +#set_tests_properties(dyn PROPERTIES PASS_REGULAR_EXPRESSION "1: PASSED") +#sanitizer_fail_test_on_error(dyn) # run arbitrary commands set(test_cmds hello hrt_test - vcdev_test + cdev_test wqueue_test ) @@ -141,7 +141,7 @@ endforeach() add_custom_target(test_results COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test - DEPENDS px4 platforms__posix__tests__dyn_hello + DEPENDS px4 #platforms__posix__tests__dyn_hello USES_TERMINAL COMMENT "Running tests in sitl" WORKING_DIRECTORY ${PX4_BINARY_DIR}) diff --git a/src/lib/cdev/CMakeLists.txt b/src/lib/cdev/CMakeLists.txt index 1f65c76cc4..dbce88e825 100644 --- a/src/lib/cdev/CMakeLists.txt +++ b/src/lib/cdev/CMakeLists.txt @@ -47,3 +47,7 @@ px4_add_library(cdev CDev.cpp ${SRCS_PLATFORM} ) + +if(PX4_TESTING) + add_subdirectory(test) +endif() \ No newline at end of file diff --git a/src/platforms/posix/tests/vcdev_test/CMakeLists.txt b/src/lib/cdev/test/CMakeLists.txt similarity index 92% rename from src/platforms/posix/tests/vcdev_test/CMakeLists.txt rename to src/lib/cdev/test/CMakeLists.txt index 9953de1c9d..a828637bc1 100644 --- a/src/platforms/posix/tests/vcdev_test/CMakeLists.txt +++ b/src/lib/cdev/test/CMakeLists.txt @@ -30,12 +30,14 @@ # POSSIBILITY OF SUCH DAMAGE. # ############################################################################ + px4_add_module( - MODULE platforms__posix__tests__vcdev_test - MAIN vcdev_test + MODULE lib__cdev__test__cdev_test + MAIN cdev_test SRCS - vcdevtest_main.cpp - vcdevtest_start_posix.cpp - vcdevtest_example.cpp + cdevtest_example.cpp + cdevtest_main.cpp + cdevtest_start.cpp DEPENDS + cdev ) diff --git a/src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp b/src/lib/cdev/test/cdevtest_example.cpp similarity index 80% rename from src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp rename to src/lib/cdev/test/cdevtest_example.cpp index 05755881cf..abf66acfb1 100644 --- a/src/platforms/posix/tests/vcdev_test/vcdevtest_example.cpp +++ b/src/lib/cdev/test/cdevtest_example.cpp @@ -33,23 +33,24 @@ ****************************************************************************/ /** - * @file vcdevtest_example.cpp + * @file cdevtest_example.cpp * Example for Linux * * @author Mark Charlebois */ +#include "cdevtest_example.h" + #include #include -#include "vcdevtest_example.h" #include #include #include #include -px4::AppState VCDevExample::appState; +px4::AppState CDevExample::appState; -#define TESTDEV "/dev/vdevtest" +#define TESTDEV "/dev/cdevtest" static bool g_exit = false; @@ -60,8 +61,8 @@ static int writer_main(int argc, char *argv[]) int fd = px4_open(TESTDEV, PX4_F_WRONLY); if (fd < 0) { - PX4_INFO("Writer: Open failed %d %d", fd, px4_errno); - return -px4_errno; + PX4_INFO("Writer: Open failed %d", fd); + return PX4_ERROR; } int ret = 0; @@ -97,15 +98,15 @@ public: size_t _read_offset; }; -class VCDevNode : public cdev::CDev +class CDevNode : public cdev::CDev { public: - VCDevNode() : + CDevNode() : CDev(TESTDEV), _is_open_for_write(false), _write_offset(0) {} - ~VCDevNode() = default; + ~CDevNode() = default; virtual int open(cdev::file_t *handlep); virtual int close(cdev::file_t *handlep); @@ -117,7 +118,7 @@ private: char _buf[1000]; }; -int VCDevNode::open(cdev::file_t *handlep) +int CDevNode::open(cdev::file_t *handlep) { // Only allow one writer if (_is_open_for_write && (handlep->f_oflags & PX4_F_WRONLY)) { @@ -140,7 +141,7 @@ int VCDevNode::open(cdev::file_t *handlep) return 0; } -int VCDevNode::close(cdev::file_t *handlep) +int CDevNode::close(cdev::file_t *handlep) { delete (PrivData *)handlep->f_priv; handlep->f_priv = nullptr; @@ -154,7 +155,7 @@ int VCDevNode::close(cdev::file_t *handlep) return 0; } -ssize_t VCDevNode::write(cdev::file_t *handlep, const char *buffer, size_t buflen) +ssize_t CDevNode::write(cdev::file_t *handlep, const char *buffer, size_t buflen) { for (size_t i = 0; i < buflen && _write_offset < 1000; i++) { _buf[_write_offset] = buffer[i]; @@ -167,7 +168,7 @@ ssize_t VCDevNode::write(cdev::file_t *handlep, const char *buffer, size_t bufle return buflen; } -ssize_t VCDevNode::read(cdev::file_t *handlep, char *buffer, size_t buflen) +ssize_t CDevNode::read(cdev::file_t *handlep, char *buffer, size_t buflen) { PrivData *p = (PrivData *)handlep->f_priv; ssize_t chars_read = 0; @@ -182,7 +183,7 @@ ssize_t VCDevNode::read(cdev::file_t *handlep, char *buffer, size_t buflen) return chars_read; } -VCDevExample::~VCDevExample() +CDevExample::~CDevExample() { if (_node) { delete _node; @@ -190,28 +191,7 @@ VCDevExample::~VCDevExample() } } -static int test_pub_block(int fd, unsigned long blocked) -{ - int ret = px4_ioctl(fd, DEVIOCSPUBBLOCK, blocked); - - if (ret < 0) { - PX4_INFO("ioctl PX4_DEVIOCSPUBBLOCK failed %d %d", ret, px4_errno); - return -px4_errno; - } - - ret = px4_ioctl(fd, DEVIOCGPUBBLOCK, 0); - - if (ret < 0) { - PX4_INFO("ioctl PX4_DEVIOCGPUBBLOCK failed %d %d", ret, px4_errno); - return -px4_errno; - } - - PX4_INFO("pub_blocked = %d %s", ret, (unsigned long)ret == blocked ? "PASS" : "FAIL"); - - return 0; -} - -int VCDevExample::do_poll(int fd, int timeout, int iterations, int delayms_after_poll) +int CDevExample::do_poll(int fd, int timeout, int iterations, int delayms_after_poll) { int pollret, readret; int loop_count = 0; @@ -229,7 +209,7 @@ int VCDevExample::do_poll(int fd, int timeout, int iterations, int delayms_after pollret = px4_poll(fds, 1, timeout); if (pollret < 0) { - PX4_ERR("Reader: px4_poll failed %d %d FAIL", pollret, px4_errno); + PX4_ERR("Reader: px4_poll failed %d FAIL", pollret); goto fail; } @@ -264,42 +244,29 @@ int VCDevExample::do_poll(int fd, int timeout, int iterations, int delayms_after fail: return 1; } -int VCDevExample::main() +int CDevExample::main() { appState.setRunning(true); - _node = new VCDevNode(); + _node = new CDevNode(); if (_node == nullptr) { - PX4_INFO("Failed to allocate VCDevNode"); + PX4_INFO("Failed to allocate CDevNode"); return -ENOMEM; } if (_node->init() != PX4_OK) { - PX4_INFO("Failed to init VCDevNode"); + PX4_INFO("Failed to init CDevNode"); return 1; } int fd = px4_open(TESTDEV, PX4_F_RDONLY); if (fd < 0) { - PX4_INFO("Open failed %d %d", fd, px4_errno); - return -px4_errno; + PX4_INFO("Open failed %d", fd); + return PX4_ERROR; } - int ret = test_pub_block(fd, 1); - - if (ret < 0) { - return ret; - } - - ret = test_pub_block(fd, 0); - - if (ret < 0) { - return ret; - } - - // Start a task that will write something in 4 seconds (void)px4_task_spawn_cmd("writer", SCHED_DEFAULT, @@ -308,7 +275,7 @@ int VCDevExample::main() writer_main, (char *const *)nullptr); - ret = 0; + int ret = 0; PX4_INFO("TEST: BLOCKING POLL ---------------"); diff --git a/src/platforms/posix/tests/vcdev_test/vcdevtest_example.h b/src/lib/cdev/test/cdevtest_example.h similarity index 95% rename from src/platforms/posix/tests/vcdev_test/vcdevtest_example.h rename to src/lib/cdev/test/cdevtest_example.h index 13c91eb0b9..26d78a56f0 100644 --- a/src/platforms/posix/tests/vcdev_test/vcdevtest_example.h +++ b/src/lib/cdev/test/cdevtest_example.h @@ -41,14 +41,14 @@ #include -class VCDevNode; +class CDevNode; -class VCDevExample +class CDevExample { public: - VCDevExample() : _node(0) {} + CDevExample() : _node(0) {} - ~VCDevExample(); + ~CDevExample(); int main(); @@ -57,5 +57,5 @@ public: private: int do_poll(int fd, int timeout, int iterations, int delayms_after_poll); - VCDevNode *_node; + CDevNode *_node; }; diff --git a/src/platforms/posix/tests/vcdev_test/vcdevtest_main.cpp b/src/lib/cdev/test/cdevtest_main.cpp similarity index 92% rename from src/platforms/posix/tests/vcdev_test/vcdevtest_main.cpp rename to src/lib/cdev/test/cdevtest_main.cpp index 6d8d31a4c1..cf4c05a42d 100644 --- a/src/platforms/posix/tests/vcdev_test/vcdevtest_main.cpp +++ b/src/lib/cdev/test/cdevtest_main.cpp @@ -32,23 +32,24 @@ ****************************************************************************/ /** - * @file vcdevtest_main.cpp + * @file cdevtest_main.cpp * Example for Linux * * @author Mark Charlebois */ +#include "cdevtest_example.h" + #include #include -#include "vcdevtest_example.h" #include int PX4_MAIN(int argc, char **argv) { - px4::init(argc, argv, "vcdevtest"); + px4::init(argc, argv, "cdevtest"); - printf("vcdevtest\n"); - VCDevExample vcdevtest; - vcdevtest.main(); + printf("cdevtest\n"); + CDevExample cdevtest; + cdevtest.main(); printf("goodbye\n"); return 0; diff --git a/src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp b/src/lib/cdev/test/cdevtest_start.cpp similarity index 84% rename from src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp rename to src/lib/cdev/test/cdevtest_start.cpp index b6e7681e15..1ea8a2599e 100644 --- a/src/platforms/posix/tests/vcdev_test/vcdevtest_start_posix.cpp +++ b/src/lib/cdev/test/cdevtest_start.cpp @@ -32,12 +32,13 @@ ****************************************************************************/ /** - * @file vcdevtest_start_posix.cpp + * @file cdevtest_start.cpp * * @author Thomas Gubler * @author Mark Charlebois */ -#include "vcdevtest_example.h" +#include "cdevtest_example.h" + #include #include #include @@ -47,24 +48,24 @@ static int daemon_task; /* Handle of deamon task / thread */ //using namespace px4; -extern "C" __EXPORT int vcdev_test_main(int argc, char *argv[]); -int vcdev_test_main(int argc, char *argv[]) +extern "C" __EXPORT int cdev_test_main(int argc, char *argv[]); +int cdev_test_main(int argc, char *argv[]) { if (argc < 2) { - printf("usage: vcdevtest {start|stop|status}\n"); + printf("usage: cdevtest {start|stop|status}\n"); return 1; } if (!strcmp(argv[1], "start")) { - if (VCDevExample::appState.isRunning()) { + if (CDevExample::appState.isRunning()) { printf("already running\n"); /* this is not an error */ return 0; } - daemon_task = px4_task_spawn_cmd("vcdevtest", + daemon_task = px4_task_spawn_cmd("cdevtest", SCHED_DEFAULT, SCHED_PRIORITY_MAX - 5, 2000, @@ -75,12 +76,12 @@ int vcdev_test_main(int argc, char *argv[]) } if (!strcmp(argv[1], "stop")) { - VCDevExample::appState.requestExit(); + CDevExample::appState.requestExit(); return 0; } if (!strcmp(argv[1], "status")) { - if (VCDevExample::appState.isRunning()) { + if (CDevExample::appState.isRunning()) { printf("is running\n"); } else { @@ -90,6 +91,6 @@ int vcdev_test_main(int argc, char *argv[]) return 0; } - printf("usage: vcdevtest_main {start|stop|status}\n"); + printf("usage: cdevtest_main {start|stop|status}\n"); return 1; } diff --git a/src/platforms/px4_app.h b/src/platforms/px4_app.h index c2310899ac..d391751e0e 100644 --- a/src/platforms/px4_app.h +++ b/src/platforms/px4_app.h @@ -73,9 +73,10 @@ private: } // Task/process based build -#if defined(__PX4_ROS) || defined(__PX4_NUTTX) +#if defined(__PX4_ROS) // Thread based build + #else #ifdef PX4_MAIN