mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-06 06:10:35 +08:00
Experimental virtual file support
QuRT does not have a filesystem, so creating a virtual filesystem that could be implemented as an in-memory file or a remote file over fastRPC. Signed-off-by: Mark Charlebois <charlebm@gmail.com>
This commit is contained in:
@@ -47,6 +47,7 @@ else
|
||||
SRCS = \
|
||||
device_posix.cpp \
|
||||
vdev.cpp \
|
||||
vfile.cpp \
|
||||
vdev_posix.cpp \
|
||||
i2c_posix.cpp \
|
||||
sim.cpp \
|
||||
|
||||
@@ -64,7 +64,7 @@ private:
|
||||
px4_dev_t() {}
|
||||
};
|
||||
|
||||
#define PX4_MAX_DEV 100
|
||||
#define PX4_MAX_DEV 30
|
||||
static px4_dev_t *devmap[PX4_MAX_DEV];
|
||||
|
||||
/*
|
||||
@@ -448,8 +448,12 @@ VDev::remove_poll_waiter(px4_pollfd_struct_t *fds)
|
||||
|
||||
VDev *VDev::getDev(const char *path)
|
||||
{
|
||||
printf("VDev::getDev\n");
|
||||
int i=0;
|
||||
for (; i<PX4_MAX_DEV; ++i) {
|
||||
if (devmap[i]) {
|
||||
printf("%s %s\n", devmap[i]->name, path);
|
||||
}
|
||||
if (devmap[i] && (strcmp(devmap[i]->name, path) == 0)) {
|
||||
return (VDev *)(devmap[i]->cdev);
|
||||
}
|
||||
@@ -479,6 +483,18 @@ void VDev::showTopics()
|
||||
}
|
||||
}
|
||||
|
||||
void VDev::showFiles()
|
||||
{
|
||||
int i=0;
|
||||
printf("Files:\n");
|
||||
for (; i<PX4_MAX_DEV; ++i) {
|
||||
if (devmap[i] && strncmp(devmap[i]->name, "/obj/", 5) != 0 &&
|
||||
strncmp(devmap[i]->name, "/dev/", 5) != 0) {
|
||||
printf(" %s\n", devmap[i]->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *VDev::topicList(unsigned int *next)
|
||||
{
|
||||
for (;*next<PX4_MAX_DEV; (*next)++)
|
||||
|
||||
@@ -60,6 +60,7 @@ namespace device __EXPORT
|
||||
struct file_t {
|
||||
int fd;
|
||||
int flags;
|
||||
mode_t mode;
|
||||
void *priv;
|
||||
void *vdev;
|
||||
|
||||
@@ -330,6 +331,7 @@ public:
|
||||
virtual int ioctl(file_t *filep, int cmd, unsigned long arg);
|
||||
|
||||
static VDev *getDev(const char *path);
|
||||
static void showFiles(void);
|
||||
static void showDevices(void);
|
||||
static void showTopics(void);
|
||||
static const char *devList(unsigned int *next);
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include <px4_posix.h>
|
||||
#include <px4_time.h>
|
||||
#include "device.h"
|
||||
#include "vfile.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
@@ -85,16 +86,26 @@ inline bool valid_fd(int fd)
|
||||
return (fd < PX4_MAX_FD && fd >= 0 && filemap[fd] != NULL);
|
||||
}
|
||||
|
||||
int px4_open(const char *path, int flags)
|
||||
int px4_open(const char *path, int flags, ...)
|
||||
{
|
||||
printf("px4_open\n");
|
||||
VDev *dev = VDev::getDev(path);
|
||||
int ret = 0;
|
||||
int i;
|
||||
mode_t mode;
|
||||
|
||||
if (!dev) {
|
||||
ret = -EINVAL;
|
||||
if (!dev && (flags & (PX4_F_WRONLY|PX4_F_CREAT)) != 0)
|
||||
{
|
||||
va_list p;
|
||||
va_start(p, flags);
|
||||
mode = va_arg(p, mode_t);
|
||||
va_end(p);
|
||||
|
||||
// Create the file
|
||||
warnx("Creating virtual file %s\n", path);
|
||||
dev = VFile::createFile(path, mode);
|
||||
}
|
||||
else {
|
||||
if (dev) {
|
||||
for (i=0; i<PX4_MAX_FD; ++i) {
|
||||
if (filemap[i] == 0) {
|
||||
filemap[i] = new device::file_t(flags,dev,i);
|
||||
@@ -108,6 +119,9 @@ int px4_open(const char *path, int flags)
|
||||
ret = -ENOENT;
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret = -EINVAL;
|
||||
}
|
||||
if (ret < 0) {
|
||||
px4_errno = -ret;
|
||||
return -1;
|
||||
@@ -272,6 +286,11 @@ cleanup:
|
||||
return count;
|
||||
}
|
||||
|
||||
int px4_fsync(int fd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void px4_show_devices()
|
||||
{
|
||||
VDev::showDevices();
|
||||
@@ -282,6 +301,11 @@ void px4_show_topics()
|
||||
VDev::showTopics();
|
||||
}
|
||||
|
||||
void px4_show_files()
|
||||
{
|
||||
VDev::showFiles();
|
||||
}
|
||||
|
||||
const char * px4_get_device_names(unsigned int *handle)
|
||||
{
|
||||
return VDev::devList(handle);
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2015 Mark Charlebois. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file vdev_file.cpp
|
||||
* Virtual file
|
||||
*
|
||||
* @author Mark Charlebois <charlebm@gmail.com>
|
||||
*/
|
||||
|
||||
#include "vfile.h"
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace device;
|
||||
|
||||
VFile::VFile(const char *fname, mode_t mode) :
|
||||
VDev("vfile", fname)
|
||||
{
|
||||
}
|
||||
|
||||
VFile * VFile::createFile(const char *fname, mode_t mode)
|
||||
{
|
||||
VFile *me = new VFile(fname, mode);
|
||||
me->register_driver(fname, me);
|
||||
return me;
|
||||
}
|
||||
|
||||
ssize_t VFile::write(device::file_t *handlep, const char *buffer, size_t buflen)
|
||||
{
|
||||
// ignore what was written, but let pollers know something was written
|
||||
poll_notify(POLLIN);
|
||||
|
||||
return buflen;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2015 Mark Charlebois. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file vfile.cpp
|
||||
* Virtual file
|
||||
*
|
||||
* @author Mark Charlebois <charlebm@gmail.com>
|
||||
*/
|
||||
|
||||
#include <px4_tasks.h>
|
||||
#include <drivers/drv_device.h>
|
||||
#include "device.h"
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class VFile : public device::VDev {
|
||||
public:
|
||||
|
||||
static VFile *createFile(const char *fname, mode_t mode);
|
||||
~VFile() {}
|
||||
|
||||
virtual ssize_t write(device::file_t *handlep, const char *buffer, size_t buflen);
|
||||
|
||||
private:
|
||||
VFile(const char *fname, mode_t mode);
|
||||
VFile(const VFile &);
|
||||
};
|
||||
Reference in New Issue
Block a user