make vcdevtest a generic cdev test

This commit is contained in:
Daniel Agar 2018-11-24 11:37:52 -05:00 committed by David Sidrane
parent 3e0a3559a9
commit c3448c19c4
8 changed files with 74 additions and 98 deletions

View File

@ -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
$<TARGET_FILE:px4>
none
none
test_dyn_hello
${PX4_SOURCE_DIR}
${PX4_BINARY_DIR}
$<TARGET_FILE:platforms__posix__tests__dyn_hello>
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
# $<TARGET_FILE:px4>
# none
# none
# test_dyn_hello
# ${PX4_SOURCE_DIR}
# ${PX4_BINARY_DIR}
# $<TARGET_FILE:platforms__posix__tests__dyn_hello>
# 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})

View File

@ -47,3 +47,7 @@ px4_add_library(cdev
CDev.cpp
${SRCS_PLATFORM}
)
if(PX4_TESTING)
add_subdirectory(test)
endif()

View File

@ -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
)

View File

@ -33,23 +33,24 @@
****************************************************************************/
/**
* @file vcdevtest_example.cpp
* @file cdevtest_example.cpp
* Example for Linux
*
* @author Mark Charlebois <charlebm@gmail.com>
*/
#include "cdevtest_example.h"
#include <px4_tasks.h>
#include <px4_time.h>
#include "vcdevtest_example.h"
#include <drivers/drv_device.h>
#include <lib/cdev/CDev.hpp>
#include <unistd.h>
#include <stdio.h>
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 ---------------");

View File

@ -41,14 +41,14 @@
#include <px4_app.h>
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;
};

View File

@ -32,23 +32,24 @@
****************************************************************************/
/**
* @file vcdevtest_main.cpp
* @file cdevtest_main.cpp
* Example for Linux
*
* @author Mark Charlebois <charlebm@gmail.com>
*/
#include "cdevtest_example.h"
#include <px4_middleware.h>
#include <px4_app.h>
#include "vcdevtest_example.h"
#include <stdio.h>
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;

View File

@ -32,12 +32,13 @@
****************************************************************************/
/**
* @file vcdevtest_start_posix.cpp
* @file cdevtest_start.cpp
*
* @author Thomas Gubler <thomasgubler@gmail.com>
* @author Mark Charlebois <mcharleb@gmail.com>
*/
#include "vcdevtest_example.h"
#include "cdevtest_example.h"
#include <px4_app.h>
#include <px4_tasks.h>
#include <stdio.h>
@ -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;
}

View File

@ -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