mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-23 06:53:34 +08:00
make vcdevtest a generic cdev test
This commit is contained in:
committed by
David Sidrane
parent
3e0a3559a9
commit
c3448c19c4
@@ -100,24 +100,24 @@ set_tests_properties(shutdown PROPERTIES PASS_REGULAR_EXPRESSION "Shutting down"
|
|||||||
sanitizer_fail_test_on_error(shutdown)
|
sanitizer_fail_test_on_error(shutdown)
|
||||||
|
|
||||||
# Dynamic module loading test
|
# Dynamic module loading test
|
||||||
add_test(NAME dyn
|
#add_test(NAME dyn
|
||||||
COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh
|
# COMMAND ${PX4_SOURCE_DIR}/Tools/sitl_run.sh
|
||||||
$<TARGET_FILE:px4>
|
# $<TARGET_FILE:px4>
|
||||||
none
|
# none
|
||||||
none
|
# none
|
||||||
test_dyn_hello
|
# test_dyn_hello
|
||||||
${PX4_SOURCE_DIR}
|
# ${PX4_SOURCE_DIR}
|
||||||
${PX4_BINARY_DIR}
|
# ${PX4_BINARY_DIR}
|
||||||
$<TARGET_FILE:platforms__posix__tests__dyn_hello>
|
# $<TARGET_FILE:platforms__posix__tests__dyn_hello>
|
||||||
WORKING_DIRECTORY ${SITL_WORKING_DIR})
|
# WORKING_DIRECTORY ${SITL_WORKING_DIR})
|
||||||
set_tests_properties(dyn PROPERTIES PASS_REGULAR_EXPRESSION "1: PASSED")
|
#set_tests_properties(dyn PROPERTIES PASS_REGULAR_EXPRESSION "1: PASSED")
|
||||||
sanitizer_fail_test_on_error(dyn)
|
#sanitizer_fail_test_on_error(dyn)
|
||||||
|
|
||||||
# run arbitrary commands
|
# run arbitrary commands
|
||||||
set(test_cmds
|
set(test_cmds
|
||||||
hello
|
hello
|
||||||
hrt_test
|
hrt_test
|
||||||
vcdev_test
|
cdev_test
|
||||||
wqueue_test
|
wqueue_test
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ endforeach()
|
|||||||
|
|
||||||
add_custom_target(test_results
|
add_custom_target(test_results
|
||||||
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure -T Test
|
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
|
USES_TERMINAL
|
||||||
COMMENT "Running tests in sitl"
|
COMMENT "Running tests in sitl"
|
||||||
WORKING_DIRECTORY ${PX4_BINARY_DIR})
|
WORKING_DIRECTORY ${PX4_BINARY_DIR})
|
||||||
|
|||||||
@@ -47,3 +47,7 @@ px4_add_library(cdev
|
|||||||
CDev.cpp
|
CDev.cpp
|
||||||
${SRCS_PLATFORM}
|
${SRCS_PLATFORM}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if(PX4_TESTING)
|
||||||
|
add_subdirectory(test)
|
||||||
|
endif()
|
||||||
+7
-5
@@ -30,12 +30,14 @@
|
|||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
#
|
#
|
||||||
############################################################################
|
############################################################################
|
||||||
|
|
||||||
px4_add_module(
|
px4_add_module(
|
||||||
MODULE platforms__posix__tests__vcdev_test
|
MODULE lib__cdev__test__cdev_test
|
||||||
MAIN vcdev_test
|
MAIN cdev_test
|
||||||
SRCS
|
SRCS
|
||||||
vcdevtest_main.cpp
|
cdevtest_example.cpp
|
||||||
vcdevtest_start_posix.cpp
|
cdevtest_main.cpp
|
||||||
vcdevtest_example.cpp
|
cdevtest_start.cpp
|
||||||
DEPENDS
|
DEPENDS
|
||||||
|
cdev
|
||||||
)
|
)
|
||||||
+24
-57
@@ -33,23 +33,24 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file vcdevtest_example.cpp
|
* @file cdevtest_example.cpp
|
||||||
* Example for Linux
|
* Example for Linux
|
||||||
*
|
*
|
||||||
* @author Mark Charlebois <charlebm@gmail.com>
|
* @author Mark Charlebois <charlebm@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "cdevtest_example.h"
|
||||||
|
|
||||||
#include <px4_tasks.h>
|
#include <px4_tasks.h>
|
||||||
#include <px4_time.h>
|
#include <px4_time.h>
|
||||||
#include "vcdevtest_example.h"
|
|
||||||
#include <drivers/drv_device.h>
|
#include <drivers/drv_device.h>
|
||||||
#include <lib/cdev/CDev.hpp>
|
#include <lib/cdev/CDev.hpp>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.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;
|
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);
|
int fd = px4_open(TESTDEV, PX4_F_WRONLY);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
PX4_INFO("Writer: Open failed %d %d", fd, px4_errno);
|
PX4_INFO("Writer: Open failed %d", fd);
|
||||||
return -px4_errno;
|
return PX4_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
@@ -97,15 +98,15 @@ public:
|
|||||||
size_t _read_offset;
|
size_t _read_offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
class VCDevNode : public cdev::CDev
|
class CDevNode : public cdev::CDev
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VCDevNode() :
|
CDevNode() :
|
||||||
CDev(TESTDEV),
|
CDev(TESTDEV),
|
||||||
_is_open_for_write(false),
|
_is_open_for_write(false),
|
||||||
_write_offset(0) {}
|
_write_offset(0) {}
|
||||||
|
|
||||||
~VCDevNode() = default;
|
~CDevNode() = default;
|
||||||
|
|
||||||
virtual int open(cdev::file_t *handlep);
|
virtual int open(cdev::file_t *handlep);
|
||||||
virtual int close(cdev::file_t *handlep);
|
virtual int close(cdev::file_t *handlep);
|
||||||
@@ -117,7 +118,7 @@ private:
|
|||||||
char _buf[1000];
|
char _buf[1000];
|
||||||
};
|
};
|
||||||
|
|
||||||
int VCDevNode::open(cdev::file_t *handlep)
|
int CDevNode::open(cdev::file_t *handlep)
|
||||||
{
|
{
|
||||||
// Only allow one writer
|
// Only allow one writer
|
||||||
if (_is_open_for_write && (handlep->f_oflags & PX4_F_WRONLY)) {
|
if (_is_open_for_write && (handlep->f_oflags & PX4_F_WRONLY)) {
|
||||||
@@ -140,7 +141,7 @@ int VCDevNode::open(cdev::file_t *handlep)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int VCDevNode::close(cdev::file_t *handlep)
|
int CDevNode::close(cdev::file_t *handlep)
|
||||||
{
|
{
|
||||||
delete (PrivData *)handlep->f_priv;
|
delete (PrivData *)handlep->f_priv;
|
||||||
handlep->f_priv = nullptr;
|
handlep->f_priv = nullptr;
|
||||||
@@ -154,7 +155,7 @@ int VCDevNode::close(cdev::file_t *handlep)
|
|||||||
return 0;
|
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++) {
|
for (size_t i = 0; i < buflen && _write_offset < 1000; i++) {
|
||||||
_buf[_write_offset] = buffer[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;
|
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;
|
PrivData *p = (PrivData *)handlep->f_priv;
|
||||||
ssize_t chars_read = 0;
|
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;
|
return chars_read;
|
||||||
}
|
}
|
||||||
|
|
||||||
VCDevExample::~VCDevExample()
|
CDevExample::~CDevExample()
|
||||||
{
|
{
|
||||||
if (_node) {
|
if (_node) {
|
||||||
delete _node;
|
delete _node;
|
||||||
@@ -190,28 +191,7 @@ VCDevExample::~VCDevExample()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_pub_block(int fd, unsigned long blocked)
|
int CDevExample::do_poll(int fd, int timeout, int iterations, int delayms_after_poll)
|
||||||
{
|
|
||||||
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 pollret, readret;
|
int pollret, readret;
|
||||||
int loop_count = 0;
|
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);
|
pollret = px4_poll(fds, 1, timeout);
|
||||||
|
|
||||||
if (pollret < 0) {
|
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;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,42 +244,29 @@ int VCDevExample::do_poll(int fd, int timeout, int iterations, int delayms_after
|
|||||||
fail:
|
fail:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int VCDevExample::main()
|
int CDevExample::main()
|
||||||
{
|
{
|
||||||
appState.setRunning(true);
|
appState.setRunning(true);
|
||||||
|
|
||||||
_node = new VCDevNode();
|
_node = new CDevNode();
|
||||||
|
|
||||||
if (_node == nullptr) {
|
if (_node == nullptr) {
|
||||||
PX4_INFO("Failed to allocate VCDevNode");
|
PX4_INFO("Failed to allocate CDevNode");
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_node->init() != PX4_OK) {
|
if (_node->init() != PX4_OK) {
|
||||||
PX4_INFO("Failed to init VCDevNode");
|
PX4_INFO("Failed to init CDevNode");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = px4_open(TESTDEV, PX4_F_RDONLY);
|
int fd = px4_open(TESTDEV, PX4_F_RDONLY);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
PX4_INFO("Open failed %d %d", fd, px4_errno);
|
PX4_INFO("Open failed %d", fd);
|
||||||
return -px4_errno;
|
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
|
// Start a task that will write something in 4 seconds
|
||||||
(void)px4_task_spawn_cmd("writer",
|
(void)px4_task_spawn_cmd("writer",
|
||||||
SCHED_DEFAULT,
|
SCHED_DEFAULT,
|
||||||
@@ -308,7 +275,7 @@ int VCDevExample::main()
|
|||||||
writer_main,
|
writer_main,
|
||||||
(char *const *)nullptr);
|
(char *const *)nullptr);
|
||||||
|
|
||||||
ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
PX4_INFO("TEST: BLOCKING POLL ---------------");
|
PX4_INFO("TEST: BLOCKING POLL ---------------");
|
||||||
|
|
||||||
+5
-5
@@ -41,14 +41,14 @@
|
|||||||
|
|
||||||
#include <px4_app.h>
|
#include <px4_app.h>
|
||||||
|
|
||||||
class VCDevNode;
|
class CDevNode;
|
||||||
|
|
||||||
class VCDevExample
|
class CDevExample
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VCDevExample() : _node(0) {}
|
CDevExample() : _node(0) {}
|
||||||
|
|
||||||
~VCDevExample();
|
~CDevExample();
|
||||||
|
|
||||||
int main();
|
int main();
|
||||||
|
|
||||||
@@ -57,5 +57,5 @@ public:
|
|||||||
private:
|
private:
|
||||||
int do_poll(int fd, int timeout, int iterations, int delayms_after_poll);
|
int do_poll(int fd, int timeout, int iterations, int delayms_after_poll);
|
||||||
|
|
||||||
VCDevNode *_node;
|
CDevNode *_node;
|
||||||
};
|
};
|
||||||
+7
-6
@@ -32,23 +32,24 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file vcdevtest_main.cpp
|
* @file cdevtest_main.cpp
|
||||||
* Example for Linux
|
* Example for Linux
|
||||||
*
|
*
|
||||||
* @author Mark Charlebois <charlebm@gmail.com>
|
* @author Mark Charlebois <charlebm@gmail.com>
|
||||||
*/
|
*/
|
||||||
|
#include "cdevtest_example.h"
|
||||||
|
|
||||||
#include <px4_middleware.h>
|
#include <px4_middleware.h>
|
||||||
#include <px4_app.h>
|
#include <px4_app.h>
|
||||||
#include "vcdevtest_example.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int PX4_MAIN(int argc, char **argv)
|
int PX4_MAIN(int argc, char **argv)
|
||||||
{
|
{
|
||||||
px4::init(argc, argv, "vcdevtest");
|
px4::init(argc, argv, "cdevtest");
|
||||||
|
|
||||||
printf("vcdevtest\n");
|
printf("cdevtest\n");
|
||||||
VCDevExample vcdevtest;
|
CDevExample cdevtest;
|
||||||
vcdevtest.main();
|
cdevtest.main();
|
||||||
|
|
||||||
printf("goodbye\n");
|
printf("goodbye\n");
|
||||||
return 0;
|
return 0;
|
||||||
+11
-10
@@ -32,12 +32,13 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file vcdevtest_start_posix.cpp
|
* @file cdevtest_start.cpp
|
||||||
*
|
*
|
||||||
* @author Thomas Gubler <thomasgubler@gmail.com>
|
* @author Thomas Gubler <thomasgubler@gmail.com>
|
||||||
* @author Mark Charlebois <mcharleb@gmail.com>
|
* @author Mark Charlebois <mcharleb@gmail.com>
|
||||||
*/
|
*/
|
||||||
#include "vcdevtest_example.h"
|
#include "cdevtest_example.h"
|
||||||
|
|
||||||
#include <px4_app.h>
|
#include <px4_app.h>
|
||||||
#include <px4_tasks.h>
|
#include <px4_tasks.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -47,24 +48,24 @@ static int daemon_task; /* Handle of deamon task / thread */
|
|||||||
|
|
||||||
//using namespace px4;
|
//using namespace px4;
|
||||||
|
|
||||||
extern "C" __EXPORT int vcdev_test_main(int argc, char *argv[]);
|
extern "C" __EXPORT int cdev_test_main(int argc, char *argv[]);
|
||||||
int vcdev_test_main(int argc, char *argv[])
|
int cdev_test_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
printf("usage: vcdevtest {start|stop|status}\n");
|
printf("usage: cdevtest {start|stop|status}\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp(argv[1], "start")) {
|
if (!strcmp(argv[1], "start")) {
|
||||||
|
|
||||||
if (VCDevExample::appState.isRunning()) {
|
if (CDevExample::appState.isRunning()) {
|
||||||
printf("already running\n");
|
printf("already running\n");
|
||||||
/* this is not an error */
|
/* this is not an error */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
daemon_task = px4_task_spawn_cmd("vcdevtest",
|
daemon_task = px4_task_spawn_cmd("cdevtest",
|
||||||
SCHED_DEFAULT,
|
SCHED_DEFAULT,
|
||||||
SCHED_PRIORITY_MAX - 5,
|
SCHED_PRIORITY_MAX - 5,
|
||||||
2000,
|
2000,
|
||||||
@@ -75,12 +76,12 @@ int vcdev_test_main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp(argv[1], "stop")) {
|
if (!strcmp(argv[1], "stop")) {
|
||||||
VCDevExample::appState.requestExit();
|
CDevExample::appState.requestExit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!strcmp(argv[1], "status")) {
|
if (!strcmp(argv[1], "status")) {
|
||||||
if (VCDevExample::appState.isRunning()) {
|
if (CDevExample::appState.isRunning()) {
|
||||||
printf("is running\n");
|
printf("is running\n");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -90,6 +91,6 @@ int vcdev_test_main(int argc, char *argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("usage: vcdevtest_main {start|stop|status}\n");
|
printf("usage: cdevtest_main {start|stop|status}\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -73,9 +73,10 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Task/process based build
|
// Task/process based build
|
||||||
#if defined(__PX4_ROS) || defined(__PX4_NUTTX)
|
#if defined(__PX4_ROS)
|
||||||
|
|
||||||
// Thread based build
|
// Thread based build
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#ifdef PX4_MAIN
|
#ifdef PX4_MAIN
|
||||||
|
|||||||
Reference in New Issue
Block a user