-
Notifications
You must be signed in to change notification settings - Fork 28
/
FIFOStream.cpp
137 lines (107 loc) · 2.81 KB
/
FIFOStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "FIFOStream.h"
#include "ErrorNo.h"
#include "DataStream.h"
#include "ManagedBuffer.h"
#include "CodalDmesg.h"
#include "MessageBus.h"
using namespace codal;
FIFOStream::FIFOStream( DataSource &source ) : upStream( source )
{
this->bufferCount = 0;
this->bufferLength = 0;
this->downStream = NULL;
source.connect( *this );
this->allowInput = false;
this->allowOutput = false;
}
FIFOStream::~FIFOStream()
{
//
}
bool FIFOStream::canPull()
{
return (this->bufferLength > 0) && this->allowOutput;
}
ManagedBuffer FIFOStream::pull()
{
if( (this->bufferLength > 0) && this->allowOutput )
{
ManagedBuffer out = buffer[0];
for (int i = 0; i < FIFO_MAXIMUM_BUFFERS-1; i++)
buffer[i] = buffer[i + 1];
buffer[FIFO_MAXIMUM_BUFFERS-1] = ManagedBuffer();
this->bufferLength -= out.length();
this->bufferCount--;
if (this->bufferCount > 0 && downStream != NULL)
downStream->pullRequest();
return out;
}
return ManagedBuffer();
}
int FIFOStream::length()
{
return this->bufferLength;
}
bool FIFOStream::isFull() {
return this->bufferCount < FIFO_MAXIMUM_BUFFERS;
}
void FIFOStream::dumpState()
{
DMESG(
"TapeDeck { bufferCount = %d/%d, bufferLength = %dB }",
this->bufferCount,
FIFO_MAXIMUM_BUFFERS,
this->bufferLength
);
}
int FIFOStream::pullRequest()
{
if( this->bufferCount >= FIFO_MAXIMUM_BUFFERS )
return DEVICE_NO_RESOURCES;
ManagedBuffer inBuffer = this->upStream.pull();
if( this->allowInput && inBuffer.length() > 0 )
{
this->buffer[ this->bufferCount++ ] = inBuffer;
this->bufferLength += inBuffer.length();
}
if (bufferCount > 0 && this->allowOutput && downStream != NULL)
return downStream->pullRequest();
if( this->bufferCount >= FIFO_MAXIMUM_BUFFERS )
return DEVICE_BUSY;
return DEVICE_OK;
}
void FIFOStream::connect( DataSink &sink )
{
this->downStream = &sink;
}
bool FIFOStream::isConnected()
{
return this->downStream != NULL;
}
void FIFOStream::disconnect()
{
this->downStream = NULL;
}
int FIFOStream::getFormat()
{
return this->upStream.getFormat();
}
int FIFOStream::setFormat( int format )
{
return this->upStream.setFormat( format );
}
void FIFOStream::setInputEnable( bool state )
{
this->allowInput = state;
}
void FIFOStream::setOutputEnable( bool state )
{
bool enabling = false;
DMESG("FIFO:setOutputEnable %d", state );
if (this->allowOutput == false && state)
enabling = true;
this->allowOutput = state;
// If we've just been enabled and have data to send, issue a pullrequest to ensure our downstream is aware of this
if (enabling && bufferCount > 0 && downStream != NULL)
downStream->pullRequest();
}