NetBurner 3.5.0
PDF Version
 
fd_adapter.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#ifndef __FD_ADAPTER_H
6#define __FD_ADAPTER_H
7
8#include <basictypes.h>
9#include <iosys.h>
10#include <buffers.h>
11
12#define MAX_FDBUFFER_FIFO_BUFFERS (20)
13
14class fd_adapter
15{
16 protected:
17 int my_fd;
18 bool inDtor;
19
20 virtual int read(char *buf, int nbytes) = 0;
21 virtual int write(const char *buf, int nbytes) = 0;
22 virtual int close() = 0;
23 static int sread(int fd, char *buf, int nbytes);
24 static int swrite(int fd, const char *buf, int nbytes);
25 static int sclose(int fd);
26 static fd_adapter *GetFromFD(int fd);
27
28 public:
29 fd_adapter() {my_fd=0; inDtor = false;};
30 // You _cannot_ call ::close inside the pure virtual base class.
31 // The reason is that by the time we get here, the derived class is destroyed,
32 // which means that the vtable we hit is the base class, which in turn
33 // has a pure virtual method for close. When the naked scope close method
34 // is called, this will hit the fd_adapter sclose which
35 ~fd_adapter() { inDtor = true; if (my_fd) ::close(my_fd); };
36 operator int() { if(my_fd==0) GetActiveFD(); return my_fd;};
37 int GetActiveFD();
38
39};
40
41class FDCounter : public fd_adapter
42{
43 int nwr;
44
45 virtual int read(char *buf, int nbytes);
46 virtual int write(const char *buf, int nbytes);
47 virtual int close();
48
49 public:
50 FDCounter() { nwr = 0; };
51 ~FDCounter() { if (my_fd) ::close(my_fd); }
52
53 int SpaceUsed() { return nwr; };
54};
55
56class FDFlash : public fd_adapter
57{
58 uint32_t m_len;
59 uint32_t m_cs;
60 uint8_t buffer[64];
61 uint8_t *pd;
62 uint32_t blen;
63
64 virtual int read(char *buf, int nbytes);
65 virtual int write(const char *buf, int nbytes);
66 virtual int close();
67
68 void Flush();
69
70 public:
71 FDFlash(uint8_t *pw)
72 {
73 pd = pw;
74 m_len = 0;
75 m_cs = 0;
76 blen = 0;
77 buffer[0] = '\0';
78 };
79
80 uint32_t cs() { return m_cs; };
81
82 uint32_t len() { return m_len; };
83};
84
85class FDBuffer : public fd_adapter
86{
87 fifo_buffer_storage bs{MAX_FDBUFFER_FIFO_BUFFERS, 0};
88 int nwr;
89
90 int read(char *buf, int nbytes) override;
91 int write(const char *buf, int nbytes) override;
92 int close() override;
93
94 public:
95 void Reset()
96 {
97 bs.Reset(MAX_FDBUFFER_FIFO_BUFFERS);
98 nwr = 0;
99 };
100
101 bool StreamTo(int fd); // Returns true when done
102
103 FDBuffer() { nwr = 0; };
104
105 int SpaceUsed() { return nwr; };
106};
107
108class FDcBuf : public fd_adapter
109{
110 OS_CRIT crit;
111 uint8_t *cbuf;
112 int rdIdx;
113 int wrIdx;
114 int bufLen;
115 int blockWaitSkip;
116 bool empty;
117
118 int read(char *buf, int nbytes) override;
119 int write(const char *buf, int nbytes) override;
120 int close() override;
121
122 public:
123 inline void Reset()
124 {
125 rdIdx = 0;
126 wrIdx = 0;
127 empty = true;
128 }
129
130 FDcBuf(uint8_t *buf, int _bufLen, int _blockWaitSkip = 1)
131 : cbuf(buf), rdIdx(0), wrIdx(0), bufLen(_bufLen),
132 blockWaitSkip(_blockWaitSkip), empty(true)
133 { }
134 ~FDcBuf()
135 {
136 if (my_fd)
137 ::close(my_fd);
138
139 }
140
141 inline int SpaceUsed() { return wrIdx - rdIdx + ((rdIdx > wrIdx)*bufLen); };
142};
143#endif /* ----- #ifndef __FD_ADAPTER_H ----- */
NetBurner Buffers API.
NetBurner I/O System Library API.
An OS_CRIT object is used to establish critical sections of code that can only be run by one task at ...
Definition nbrtos.h:1084