Firmware
CDev.hpp
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2018 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 
40 #ifndef _CDEV_HPP
41 #define _CDEV_HPP
42 
43 #include <px4_config.h>
44 #include <px4_posix.h>
45 
46 #ifdef __PX4_NUTTX
47 #include "nuttx/cdev_platform.hpp"
48 #else
49 #include "posix/cdev_platform.hpp"
50 #endif
51 
52 namespace cdev
53 {
54 
59 {
60 public:
67  CDev(const char *devname);
68 
69  virtual ~CDev();
70 
71  virtual int init();
72 
82  virtual int open(file_t *filep);
83 
93  virtual int close(file_t *filep);
94 
105  virtual ssize_t read(file_t *filep, char *buffer, size_t buflen) { return -ENOSYS; }
106 
117  virtual ssize_t write(file_t *filep, const char *buffer, size_t buflen) { return -ENOSYS; }
118 
129  virtual off_t seek(file_t *filep, off_t offset, int whence) { return -ENOSYS; }
130 
143  virtual int ioctl(file_t *filep, int cmd, unsigned long arg);
144 
156  virtual int poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup);
157 
163  const char *get_devname() const { return _devname; }
164 
165 protected:
171 
184  virtual pollevent_t poll_state(file_t *filep) { return 0; }
185 
194  virtual void poll_notify(pollevent_t events);
195 
202  virtual void poll_notify_one(px4_pollfd_struct_t *fds, pollevent_t events);
203 
215  virtual int open_first(file_t *filep) { return PX4_OK; }
216 
228  virtual int close_last(file_t *filep) { return PX4_OK; }
229 
237  virtual int register_class_devname(const char *class_devname);
238 
247  virtual int unregister_class_devname(const char *class_devname, unsigned class_instance);
248 
258  void lock() { do {} while (px4_sem_wait(&_lock) != 0); }
259 
263  void unlock() { px4_sem_post(&_lock); }
264 
265  px4_sem_t _lock;
267 private:
268  const char *_devname{nullptr};
270  px4_pollfd_struct_t **_pollset{nullptr};
271 
272  bool _registered{false};
274  uint8_t _max_pollwaiters{0};
275  uint16_t _open_count{0};
284  inline int store_poll_waiter(px4_pollfd_struct_t *fds);
285 
291  inline int remove_poll_waiter(px4_pollfd_struct_t *fds);
292 
293  /* do not allow copying this class */
294  CDev(const CDev &);
295  CDev operator=(const CDev &);
296 };
297 
298 } // namespace cdev
299 
300 #endif /* _CDEV_HPP */
void unlock()
Release the driver lock.
Definition: CDev.hpp:263
virtual ssize_t write(file_t *filep, const char *buffer, size_t buflen)
Perform a write to the device.
Definition: CDev.hpp:117
virtual ssize_t read(file_t *filep, char *buffer, size_t buflen)
Perform a read from the device.
Definition: CDev.hpp:105
void lock()
Take the driver lock.
Definition: CDev.hpp:258
Configuration flags used in code.
Includes POSIX-like functions for virtual character devices.
static const px4_file_operations_t fops
Pointer to the default cdev file operations table; useful for registering clone devices etc...
Definition: CDev.hpp:170
px4_sem_t _lock
lock to protect access to all class members (also for derived classes)
Definition: CDev.hpp:265
virtual int open_first(file_t *filep)
Notification of the first open.
Definition: CDev.hpp:215
const char * get_devname() const
Get the device name.
Definition: CDev.hpp:163
virtual pollevent_t poll_state(file_t *filep)
Check the current state of the device for poll events from the perspective of the file...
Definition: CDev.hpp:184
Definition: I2C.hpp:51
Definition: CDev.cpp:47
Definition: cdev_platform.hpp:20
Definition: cdev_platform.hpp:13
Definition: rc_loss_alarm.cpp:50
Abstract class for any character device.
Definition: CDev.hpp:58
Definition: video_device.h:50
virtual off_t seek(file_t *filep, off_t offset, int whence)
Perform a logical seek operation on the device.
Definition: CDev.hpp:129
virtual int close_last(file_t *filep)
Notification of the last close.
Definition: CDev.hpp:228