44 #include <px4_platform_common/tasks.h> 45 #include <px4_platform_common/time.h> 53 #define TESTDEV "/dev/cdevtest" 64 PX4_INFO(
"Writer: Open failed %d", fd);
73 PX4_INFO(
"Writer: Sleeping for 2 sec");
77 PX4_INFO(
"Writer: sleep failed %d %d", ret, errno);
81 buf[0] =
'A' + (char)(i % 26);
82 PX4_INFO(
"Writer: writing char '%c'", buf[0]);
88 PX4_INFO(
"Writer: stopped");
106 _is_open_for_write(false),
113 ssize_t
write(
cdev::file_t *handlep,
const char *buffer,
size_t buflen)
override;
124 if (_is_open_for_write && (handlep->
f_oflags & PX4_F_WRONLY)) {
129 int ret = CDev::open(handlep);
137 if (_is_open_for_write && (handlep->
f_oflags & PX4_F_WRONLY)) {
138 _is_open_for_write =
true;
147 handlep->
f_priv =
nullptr;
148 CDev::close(handlep);
151 if ((handlep->
f_oflags & PX4_F_WRONLY) && _is_open_for_write) {
152 _is_open_for_write =
false;
160 for (
size_t i = 0; i < buflen && _write_offset < 1000; i++) {
161 _buf[_write_offset] = buffer[i];
174 ssize_t chars_read = 0;
175 PX4_INFO(
"read %zu write %zu", p->
_read_offset, _write_offset);
177 for (
size_t i = 0; i < buflen && (p->
_read_offset < _write_offset); i++) {
196 int pollret, readret;
199 px4_pollfd_struct_t fds[1];
202 fds[0].events = POLLIN;
205 bool mustblock = (timeout < 0);
208 while ((!appState.exitRequested()) && (loop_count < iterations)) {
209 pollret =
px4_poll(fds, 1, timeout);
212 PX4_ERR(
"Reader: px4_poll failed %d FAIL", pollret);
216 PX4_INFO(
"Reader: px4_poll returned %d", pollret);
219 readret =
px4_read(fd, readbuf, 10);
223 PX4_ERR(
"Reader: read failed %d FAIL", readret);
227 PX4_INFO(
"Reader: read failed %d FAIL", readret);
231 readbuf[readret] =
'\0';
232 PX4_INFO(
"Reader: px4_poll returned %d, read '%s' PASS", pollret, readbuf);
236 if (delayms_after_poll) {
237 px4_usleep(delayms_after_poll * 1000);
249 appState.setRunning(
true);
253 if (_node ==
nullptr) {
254 PX4_INFO(
"Failed to allocate CDevNode");
258 if (_node->init() != PX4_OK) {
259 PX4_INFO(
"Failed to init CDevNode");
266 PX4_INFO(
"Open failed %d", fd);
271 (void)px4_task_spawn_cmd(
"writer",
273 SCHED_PRIORITY_MAX - 6,
276 (
char *
const *)
nullptr);
280 PX4_INFO(
"TEST: BLOCKING POLL ---------------");
282 if (do_poll(fd, -1, 3, 0)) {
287 PX4_INFO(
"TEST: ZERO TIMEOUT POLL -----------");
289 if (do_poll(fd, 0, 3, 0)) {
295 PX4_INFO(
"TEST: ZERO TIMEOUT POLL -----------");
297 if (do_poll(fd, 0, 3, 0)) {
303 PX4_INFO(
"TEST: ZERO TIMEOUT POLL -----------");
305 if (do_poll(fd, 0, 3, 0)) {
310 PX4_INFO(
"TEST: 100ms TIMEOUT POLL -----------");
312 if (do_poll(fd, 0, 30, 100)) {
317 PX4_INFO(
"TEST: 1 SEC TIMOUT POLL ------------");
319 if (do_poll(fd, 1000, 3, 0)) {
324 PX4_INFO(
"TEST: waiting for writer to stop");
328 PX4_INFO(
"TEST: waiting for writer to stop");
int do_poll(int fd, int timeout, int iterations, int delayms_after_poll)
int open(cdev::file_t *handlep) override
Handle an open of the device.
ssize_t write(cdev::file_t *handlep, const char *buffer, size_t buflen) override
Perform a write to the device.
static px4::AppState appState
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
Abstract class for any character device.
static unsigned char _buf[2048]
int close(cdev::file_t *handlep) override
Handle a close of the device.
static void write(bootloader_app_shared_t *pshared)
static int writer_main(int argc, char *argv[])
ssize_t read(cdev::file_t *handlep, char *buffer, size_t buflen) override
Perform a read from the device.