Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
freertos_drivers/common/Node.cxx
Go to the documentation of this file.
1
34#include "Devtab.hxx"
35
36#include <algorithm>
37#include <sys/stat.h>
38
39#include "can_ioctl.h"
41
43int Node::open(File *, const char *, int, int)
44{
46 if (references_++ == 0)
47 {
48 enable();
49 }
50 return 0;
51}
52
55{
57 if (--references_ <= 0)
58 {
59 disable();
60 references_ = 0;
61 }
62 return 0;
63}
64
70int Node::fstat(File* file, struct stat *stat)
71{
72 memset(stat, 0, sizeof(*stat));
73 stat->st_mode = mode_;
74 return 0;
75}
76
83int NonBlockNode::ioctl(File *file, unsigned long int key, unsigned long data)
84{
85 /* sanity check to be sure we have a valid key for this device */
87
88 // Will be called at the end if non-null.
89 Notifiable* n = nullptr;
90
91 if (IOC_SIZE(key) == NOTIFIABLE_TYPE) {
92 n = reinterpret_cast<Notifiable*>(data);
93 }
94
95 switch (key)
96 {
97 default:
98 return -EINVAL;
100 portENTER_CRITICAL();
101 if (!has_rx_buffer_data())
102 {
103 std::swap(n, readableNotify_);
104 }
105 portEXIT_CRITICAL();
106 break;
108 portENTER_CRITICAL();
109 if (!has_tx_buffer_space())
110 {
111 std::swap(n, writableNotify_);
112 }
113 portEXIT_CRITICAL();
114 break;
115 }
116 if (n)
117 {
118 n->notify();
119 }
120 return 0;
121}
virtual int fstat(File *file, struct stat *stat) override
Get the status information of a file or device.
virtual void enable()=0
This will be called once when reference-count goes from 0 to positive.
unsigned int references_
number of open references
Definition Devtab.hxx:592
int close(File *) OVERRIDE
Close method.
virtual void disable()=0
This will be called when reference count goes from non-zero to 0.
int open(File *, const char *, int, int) OVERRIDE
Open method.
OSMutex lock_
protects internal structures.
Definition Devtab.hxx:588
mode_t mode_
File open mode, such as O_NONBLOCK.
Definition Devtab.hxx:590
virtual bool has_rx_buffer_data()=0
Called under a critical section.
int ioctl(File *file, unsigned long int key, unsigned long data) OVERRIDE
Request an ioctl transaction.
Notifiable * readableNotify_
This will be notified if the device has data avilable for read.
Definition Devtab.hxx:635
Notifiable * writableNotify_
This will be notified if the device has buffer avilable for write.
Definition Devtab.hxx:637
virtual bool has_tx_buffer_space()=0
Called under a critical section.
An object that can schedule itself on an executor to run.
virtual void notify()=0
Generic callback.
Class to allow convenient locking and unlocking of mutexes in a C context.
Definition OS.hxx:494
#define CAN_IOC_READ_ACTIVE
read active ioctl.
#define NOTIFIABLE_TYPE
ioctl minor type used for the read/write active notifiable integration.
#define CAN_IOC_WRITE_ACTIVE
write active ioctl.
#define CAN_IOC_MAGIC
Magic number for this driver's ioctl calls.
#define IOC_SIZE(_num)
Decode ioctl size.
#define IOC_TYPE(_num)
Decode ioctl type.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
static int stat(struct _reent *reent, const char *path, struct stat *stat)
Get the status information of a file or device.
Definition Device.cxx:165
File information.
Definition Devtab.hxx:52