44 #include <px4_platform_common/posix.h> 53 PX4_DEBUG(
"CDev::CDev");
55 int ret = px4_sem_init(&
_lock, 0, 1);
58 PX4_DEBUG(
"SEM INIT FAIL: ret %d", ret);
64 PX4_DEBUG(
"CDev::~CDev");
74 px4_sem_destroy(&
_lock);
80 PX4_DEBUG(
"CDev::register_class_devname %s", class_devname);
82 if (class_devname ==
nullptr) {
86 int class_instance = 0;
89 while (class_instance < 4) {
91 snprintf(name,
sizeof(name),
"%s%d", class_devname, class_instance);
101 if (class_instance == 4) {
105 return class_instance;
111 PX4_DEBUG(
"CDev::unregister_class_devname");
114 snprintf(name,
sizeof(name),
"%s%u", class_devname, class_instance);
121 PX4_DEBUG(
"CDev::init");
143 PX4_DEBUG(
"CDev::open");
168 PX4_DEBUG(
"CDev::close");
194 PX4_DEBUG(
"CDev::ioctl");
201 *(
void **)(uintptr_t)arg = (
void *)
this;
215 PX4_DEBUG(
"CDev::Poll %s", setup ?
"setup" :
"teardown");
223 fds->priv = (
void *)filep;
224 PX4_DEBUG(
"CDev::poll: fds->priv = %p", filep);
244 px4_pollfd_struct_t **prev_pollset =
_pollset;
248 px4_leave_critical_section(flags);
250 px4_pollfd_struct_t **new_pollset =
new px4_pollfd_struct_t *[new_count];
253 flags = px4_enter_critical_section();
274 px4_leave_critical_section(flags);
278 delete[](prev_pollset);
282 flags = px4_enter_critical_section();
291 px4_leave_critical_section(flags);
294 delete[] new_pollset;
296 flags = px4_enter_critical_section();
306 fds->revents |= fds->events &
poll_state(filep);
309 if (fds->revents != 0) {
310 px4_sem_post(fds->sem);
332 PX4_DEBUG(
"CDev::poll_notify events = %0x", events);
349 PX4_DEBUG(
"CDev::poll_notify_one");
352 fds->revents |= fds->events & events;
354 PX4_DEBUG(
" Events fds=%p %0x %0x %0x", fds, fds->revents, fds->events, events);
356 if (fds->revents != 0) {
357 px4_sem_post(fds->sem);
365 PX4_DEBUG(
"CDev::store_poll_waiter");
383 PX4_DEBUG(
"CDev::remove_poll_waiter");
394 PX4_DEBUG(
"poll: bad fd state");
uint16_t _open_count
number of successful opens
int unregister_driver_and_memory()
First, unregisters the driver.
virtual int close_last(file_t *filep)
Notification of the last close.
const char * _devname
device node name
virtual int open(file_t *filep)
Handle an open of the device.
px4_sem_t _lock
lock to protect access to all class members (also for derived classes)
virtual int register_class_devname(const char *class_devname)
Register a class device name, automatically adding device class instance suffix if need be...
void lock()
Take the driver lock.
virtual pollevent_t poll_state(file_t *filep)
Check the current state of the device for poll events from the perspective of the file...
virtual void poll_notify_one(px4_pollfd_struct_t *fds, pollevent_t events)
Internal implementation of poll_notify.
static const px4_file_operations_t fops
Pointer to the default cdev file operations table; useful for registering clone devices etc...
virtual int close(file_t *filep)
Handle a close of the device.
virtual int open_first(file_t *filep)
Notification of the first open.
virtual void poll_notify(pollevent_t events)
Report new poll events.
Generic device / sensor interface.
uint8_t _max_pollwaiters
size of the _pollset array
px4_pollfd_struct_t ** _pollset
virtual int unregister_class_devname(const char *class_devname, unsigned class_instance)
Register a class device name, automatically adding device class instance suffix if need be...
int remove_poll_waiter(px4_pollfd_struct_t *fds)
Remove a poll waiter.
int store_poll_waiter(px4_pollfd_struct_t *fds)
Store a pollwaiter in a slot where we can find it later.
bool _registered
true if device name was registered
CDev(const char *devname)
Constructor.
virtual int ioctl(file_t *filep, int cmd, unsigned long arg)
Perform an ioctl operation on the device.
void unlock()
Release the driver lock.
virtual int poll(file_t *filep, px4_pollfd_struct_t *fds, bool setup)
Perform a poll setup/teardown operation.