Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Devtab.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_DEVTAB_HXX_
35#define _FREERTOS_DRIVERS_COMMON_DEVTAB_HXX_
36
37#include <dirent.h>
38#include <stropts.h>
39#include <sys/types.h>
40#include <sys/select.h>
41#include "os/OS.hxx"
42
43class FileIO;
44class Device;
45class FileSystem;
46class Notifiable;
48
51struct File
52{
55 union
56 {
57 void *priv;
58 void *privPtr;
59 unsigned privUint;
60 int privInt;
61 };
62 off_t offset;
63 int flags;
64 uint8_t inuse : 1;
65 uint8_t inshdn : 1;
66 uint8_t device : 1;
67 uint8_t dir : 1;
68 uint8_t dirty : 1;
69};
70
72class FileIO
73{
74public:
83 static ssize_t read(struct _reent *reent, int fd, void *buf, size_t count);
84
93 static ssize_t write(struct _reent *reent, int fd, const void *buf, size_t count);
94
103 static _off_t lseek(struct _reent *reent, int fd, _off_t offset, int whence);
104
111 static int fstat(struct _reent *reent, int fd, struct stat *stat);
112
119 static int ioctl(int fd, unsigned long int key, unsigned long data);
120
128 static int fcntl(int fd, int cmd, unsigned long data);
129
134 static bool is_device(int fd)
135 {
136 return file_lookup(fd)->device;
137 }
138
139protected:
144 FileIO(const char *name)
145 : name(name)
146 {
147 }
148
150 virtual ~FileIO()
151 {
152 }
153
161 virtual int open(File *file, const char *path, int flags, int mode) = 0;
162
167 virtual int close(File *file) = 0;
168
176 virtual ssize_t read(File *file, void *buf, size_t count) = 0;
177
185 virtual ssize_t write(File *file, const void *buf, size_t count) = 0;
186
194 virtual off_t lseek(File* f, off_t offset, int whence);
195
201 virtual int fstat(File* file, struct stat *stat) = 0;
202
209 virtual int ioctl(File *file, unsigned long int key, unsigned long data);
210
218 virtual int fcntl(File *file, int cmd, unsigned long data);
219
226 virtual bool select(File* file, int mode)
227 {
228 return true;
229 }
230
235 static int fd_alloc(void);
236
240 static void fd_free(int fd);
241
248 static File* file_lookup(int fd);
249
254 static int fd_lookup(File *file);
255
258 static const unsigned int numOpenFiles;
259
261 static File files[];
262
265
266 const char *name;
269 friend class Device;
270
272 friend class FileSystem;
273
274private:
276};
277
279class FileSystem : public FileIO
280{
281public:
284 FileSystem();
285
287 virtual ~FileSystem();
288
298 virtual void mount(const char *mount_point) = 0;
299
303 virtual void format() = 0;
304
312 static int open(struct _reent *reent, const char *path, int flags,
313 int mode);
314
320 static int close(struct _reent *reent, int fd);
321
327 static int unlink(struct _reent *reent, const char *path);
328
335 static int stat(struct _reent *reent, const char *path, struct stat *stat);
336
341 static int fsync(int fd);
342
347 static int closedir(DIR *dirp);
348
353 static DIR *opendir(const char *name);
354
360 static struct dirent *readdir(DIR *dirp);
361
362protected:
367 virtual int unlink(const char *path) = 0;
368
374 virtual int fstat(File* file, struct stat *stat) override;
375
381 virtual int stat(const char *path, struct stat *stat) = 0;
382
387 virtual int fsync(File *file) = 0;
388
393 virtual int closedir(File *file) = 0;
394
400 virtual File *opendir(File *file, const char *name) = 0;
401
407 virtual struct dirent *readdir(File *file) = 0;
408
409private:
414 static FileSystem *fs_lookup(const char *path);
415
418
421
424
426};
427
428
431struct Device : public FileIO
432{
433public:
438 Device(const char *name);
439
441 virtual ~Device();
442
450 static int open(struct _reent *reent, const char *path, int flags,
451 int mode);
452
458 static int close(struct _reent *reent, int fd);
459
466 static int stat(struct _reent *reent, const char *path, struct stat *stat);
467
479 static int select(int nfds, fd_set *readfds, fd_set *writefds,
480 fd_set *exceptfds, long long timeout);
481
485 static void select_clear();
486
487protected:
491 {
495 : event(0)
496 {
497 }
498
500 OSEventType event;
501 };
502
506 virtual mode_t get_mode()
507 {
508 return 0;
509 }
510
514 static void select_insert(SelectInfo *info);
515
519 static void select_wakeup(SelectInfo *info);
520
525 static void select_wakeup_from_isr(SelectInfo *info, int *woken);
526
528 friend class DeviceBufferBase;
529
531 friend class OSSelectWakeup;
532
533private:
535 static Device *first;
536
539
542
544};
545
548class Node : public Device
549{
550protected:
553 Node(const char *name)
554 : Device(name)
555 , mode_(0)
556 , references_(0)
557 {
558 }
559
561 virtual ~Node()
562 {
563 }
564
567 virtual void enable() = 0;
570 virtual void disable() = 0;
571
574 virtual void flush_buffers() = 0;
575
577 int open(File *, const char *, int, int) OVERRIDE;
579 int close(File *) OVERRIDE;
580
586 virtual int fstat(File* file, struct stat *stat) override;
587
590 mode_t mode_;
591
592 unsigned int references_;
594private:
598 mode_t get_mode() override
599 {
600 return mode_;
601 }
602
604};
605
606
610class NonBlockNode : public Node
611{
612protected:
615 NonBlockNode(const char *name)
616 : Node(name)
617 , readableNotify_(NULL)
618 , writableNotify_(NULL) {}
619
622 virtual bool has_rx_buffer_data() = 0;
625 virtual bool has_tx_buffer_space() = 0;
626
632 int ioctl(File *file, unsigned long int key, unsigned long data) OVERRIDE;
633
638};
639
640#endif /* _FREERTOS_DRIVERS_COMMON_DEVTAB_HXX_ */
Helper for DeviceBuffer which allows for methods to not be inlined.
Base class for both Device and FileSystem objects.
Definition Devtab.hxx:73
static void fd_free(int fd)
Free up a file descriptor.
Definition Fileio.cxx:71
FileIO(const char *name)
Constructor.
Definition Devtab.hxx:144
static File * file_lookup(int fd)
Looks up a reference to a File corresponding to a given file descriptor.
Definition Fileio.cxx:82
static int fstat(struct _reent *reent, int fd, struct stat *stat)
Get the status information of a file or device.
Definition Fileio.cxx:186
friend class Device
Allow access from Device class.
Definition Devtab.hxx:269
static int fcntl(int fd, int cmd, unsigned long data)
Manipulate a file descriptor.
Definition Fileio.cxx:233
virtual int close(File *file)=0
Close a file or device.
static const unsigned int numOpenFiles
Definition Devtab.hxx:258
static ssize_t read(struct _reent *reent, int fd, void *buf, size_t count)
Read from a file or device.
Definition Fileio.cxx:115
static bool is_device(int fd)
Test if the file descriptor belongs to a device.
Definition Devtab.hxx:134
static _off_t lseek(struct _reent *reent, int fd, _off_t offset, int whence)
Change the offset index of a file or device.
Definition Fileio.cxx:163
static File files[]
File descriptor pool.
Definition Devtab.hxx:261
virtual ssize_t write(File *file, const void *buf, size_t count)=0
Write to a file or device.
virtual int fstat(File *file, struct stat *stat)=0
Get the status information of a file or device.
static ssize_t write(struct _reent *reent, int fd, const void *buf, size_t count)
Write to a file or device.
Definition Fileio.cxx:139
static int fd_lookup(File *file)
Looks up a file descriptor corresponding to a given File reference.
Definition Fileio.cxx:101
static int ioctl(int fd, unsigned long int key, unsigned long data)
Request and ioctl transaction.
Definition Fileio.cxx:209
virtual ssize_t read(File *file, void *buf, size_t count)=0
Read from a file or device.
const char * name
device name
Definition Devtab.hxx:266
virtual bool select(File *file, int mode)
Device select method.
Definition Devtab.hxx:226
virtual ~FileIO()
Destructor.
Definition Devtab.hxx:150
virtual int open(File *file, const char *path, int flags, int mode)=0
Open a file or device.
static int fd_alloc(void)
Allocate a free file descriptor.
Definition Fileio.cxx:47
static OSMutex mutex
mutual exclusion for fileio
Definition Devtab.hxx:264
Base class for all File systems.
Definition Devtab.hxx:280
virtual int closedir(File *file)=0
Close a directory.
static int open(struct _reent *reent, const char *path, int flags, int mode)
Open a file or device.
virtual int fsync(File *file)=0
Synchronize (flush) a file to disk.
virtual File * opendir(File *file, const char *name)=0
Open a directory.
FileSystem * next
next device in linked list
Definition Devtab.hxx:420
virtual int fstat(File *file, struct stat *stat) override
Get the status information of a file or device.
virtual struct dirent * readdir(File *file)=0
Read the next entry in a directory.
static DIR * opendir(const char *name)
Open a directory.
static struct dirent * readdir(DIR *dirp)
Read the next entry in a directory.
FileSystem * prev
previous device in linked list
Definition Devtab.hxx:423
static int fsync(int fd)
Synchronize (flush) a file to disk.
virtual ~FileSystem()
Destructor.
virtual void format()=0
Format the file system, all data will be lost.
FileSystem()
Constructor.
static int close(struct _reent *reent, int fd)
Close a file or device.
virtual int stat(const char *path, struct stat *stat)=0
Get the status information of a file or device.
static FileSystem * fs_lookup(const char *path)
Locate the file system for a given path.
virtual void mount(const char *mount_point)=0
Mount the file system.
static int closedir(DIR *dirp)
Close a directory.
static FileSystem * first
first device in linked list
Definition Devtab.hxx:417
static int stat(struct _reent *reent, const char *path, struct stat *stat)
Get the status information of a file or device.
static int unlink(struct _reent *reent, const char *path)
Remove a file.
virtual int unlink(const char *path)=0
Remove a file.
Node information.
Definition Devtab.hxx:549
virtual void flush_buffers()=0
Instructs the device driver to drop all TX and RX queues.
Node(const char *name)
Constructor.
Definition Devtab.hxx:553
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.
mode_t get_mode() override
Get the mode of the device.
Definition Devtab.hxx:598
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 ~Node()
Destructor.
Definition Devtab.hxx:561
Node information for a device node in the filesystem that has support for nonblocking mode via Notifi...
Definition Devtab.hxx:611
virtual bool has_rx_buffer_data()=0
Called under a critical section.
NonBlockNode(const char *name)
Constructor.
Definition Devtab.hxx:615
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.
This class provides a mutex API.
Definition OS.hxx:427
Helper class that allows a select to be asynchronously woken up.
uintptr_t DIR
DIR typedef.
Definition dirent.h:45
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
Select wakeup information.
Definition Devtab.hxx:491
SelectInfo()
Default constructor.
Definition Devtab.hxx:494
OSEventType event
bit mask of clients that need woken
Definition Devtab.hxx:500
Device tab structure.
Definition Devtab.hxx:432
static void select_wakeup_from_isr(SelectInfo *info, int *woken)
Wakeup the list of clients needing woken.
Definition Select.cxx:224
static void select_insert(SelectInfo *info)
Add client to list of clients needing woken.
Definition Select.cxx:197
virtual mode_t get_mode()
Get the mode of the device.
Definition Devtab.hxx:506
static Device * first
first device in linked list
Definition Devtab.hxx:535
static void select_wakeup(SelectInfo *info)
Wakeup the list of clients needing woken.
Definition Select.cxx:210
static int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout)
POSIX select().
Definition Select.cxx:96
static void select_clear()
Clears the current thread's select bits.
Definition Select.cxx:78
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
Device * next
next device in linked list
Definition Devtab.hxx:538
virtual ~Device()
Destructor.
Definition Device.cxx:68
static int close(struct _reent *reent, int fd)
Close a file or device.
Definition Device.cxx:134
static int open(struct _reent *reent, const char *path, int flags, int mode)
Open a file or device.
Definition Device.cxx:93
Device * prev
previous device in linked list
Definition Devtab.hxx:541
File information.
Definition Devtab.hxx:52
uint8_t dirty
true if this file is dirty and needs flush
Definition Devtab.hxx:68
uint8_t inshdn
true if this fd is in shutdown.
Definition Devtab.hxx:65
void * privPtr
file reference specific data "pointer"
Definition Devtab.hxx:58
off_t offset
current offset within file
Definition Devtab.hxx:62
int flags
open flags
Definition Devtab.hxx:63
uint8_t dir
true if this is a directory, else false
Definition Devtab.hxx:67
int privInt
file reference specific data "int"
Definition Devtab.hxx:60
void * priv
file reference specific data "pointer"
Definition Devtab.hxx:57
uint8_t device
true if this is a device, false if file system
Definition Devtab.hxx:66
unsigned privUint
file reference specific data "unsigned"
Definition Devtab.hxx:59
FileIO * dev
file operations
Definition Devtab.hxx:53
uint8_t inuse
true if this is an open fd.
Definition Devtab.hxx:64
Directory entry structure.
Definition dirent.h:49