PX4 Firmware
PX4 Autopilot Software http://px4.io
cdev_platform.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2014 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file cdev_platform.cpp
36  *
37  * NuttX Character device functions
38  */
39 #include "cdev_platform.hpp"
40 #include "../CDev.hpp"
41 
42 #include <px4_platform_common/posix.h>
43 #include "drivers/drv_device.h"
44 #include <sys/ioctl.h>
45 
46 #ifdef CONFIG_DISABLE_POLL
47 # error This driver is not compatible with CONFIG_DISABLE_POLL
48 #endif
49 
50 namespace cdev
51 {
52 
53 /*
54  * The standard NuttX operation dispatch table can't call C++ member functions
55  * directly, so we have to bounce them through this dispatch table.
56  */
57 static int cdev_open(file_t *filp);
58 static int cdev_close(file_t *filp);
59 static ssize_t cdev_read(file_t *filp, char *buffer, size_t buflen);
60 static ssize_t cdev_write(file_t *filp, const char *buffer, size_t buflen);
61 static off_t cdev_seek(file_t *filp, off_t offset, int whence);
62 static int cdev_ioctl(file_t *filp, int cmd, unsigned long arg);
63 static int cdev_poll(file_t *filp, px4_pollfd_struct_t *fds, bool setup);
64 
65 /**
66  * Character device indirection table.
67  *
68  * Every cdev we register gets the same function table; we use the private data
69  * field in the inode to store the instance pointer.
70  *
71  * Note that we use the GNU extension syntax here because we don't get designated
72  * initialisers in gcc 4.6.
73  */
74 const struct file_operations cdev::CDev::fops = {
75 open : cdev_open,
76 close : cdev_close,
77 read : cdev_read,
79 seek : cdev_seek,
80 ioctl : cdev_ioctl,
81 poll : cdev_poll,
82 #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
83 unlink : nullptr
84 #endif
85 };
86 
87 static int
89 {
90  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
91 
92  return cdev->open(filp);
93 }
94 
95 static int
97 {
98  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
99 
100  return cdev->close(filp);
101 }
102 
103 static ssize_t
104 cdev_read(file_t *filp, char *buffer, size_t buflen)
105 {
106  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
107 
108  return cdev->read(filp, buffer, buflen);
109 }
110 
111 static ssize_t
112 cdev_write(file_t *filp, const char *buffer, size_t buflen)
113 {
114  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
115 
116  return cdev->write(filp, buffer, buflen);
117 }
118 
119 static off_t
120 cdev_seek(file_t *filp, off_t offset, int whence)
121 {
122  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
123 
124  return cdev->seek(filp, offset, whence);
125 }
126 
127 static int
128 cdev_ioctl(file_t *filp, int cmd, unsigned long arg)
129 {
130  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
131 
132  return cdev->ioctl(filp, cmd, arg);
133 }
134 
135 static int
136 cdev_poll(file_t *filp, px4_pollfd_struct_t *fds, bool setup)
137 {
138  cdev::CDev *cdev = (cdev::CDev *)(filp->f_inode->i_private);
139 
140  return cdev->poll(filp, fds, setup);
141 }
142 
143 } // namespace cdev
virtual int open(file_t *filep)
Handle an open of the device.
Definition: CDev.cpp:141
static int cdev_close(file_t *filp)
static const px4_file_operations_t fops
Pointer to the default cdev file operations table; useful for registering clone devices etc...
Definition: CDev.hpp:176
virtual ssize_t read(file_t *filep, char *buffer, size_t buflen)
Perform a read from the device.
Definition: CDev.hpp:111
virtual int close(file_t *filep)
Handle a close of the device.
Definition: CDev.cpp:166
static void read(bootloader_app_shared_t *pshared)
Generic device / sensor interface.
virtual off_t seek(file_t *filep, off_t offset, int whence)
Perform a logical seek operation on the device.
Definition: CDev.hpp:135
Abstract class for any character device.
Definition: CDev.hpp:58
virtual ssize_t write(file_t *filep, const char *buffer, size_t buflen)
Perform a write to the device.
Definition: CDev.hpp:123
Definition: CDev.cpp:47
static ssize_t cdev_read(file_t *filp, char *buffer, size_t buflen)
struct file file_t
static int cdev_open(file_t *filp)
virtual int ioctl(file_t *filep, int cmd, unsigned long arg)
Perform an ioctl operation on the device.
Definition: CDev.cpp:192
static int cdev_ioctl(file_t *filp, int cmd, unsigned long arg)
static int cdev_poll(file_t *filp, px4_pollfd_struct_t *fds, bool setup)
static off_t cdev_seek(file_t *filp, off_t offset, int whence)
static ssize_t cdev_write(file_t *filp, const char *buffer, size_t buflen)
virtual int poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup)
Perform a poll setup/teardown operation.
Definition: CDev.cpp:213