Repository navigation
Expand file tree
/
Copy pathStream.cpp
More file actions
208 lines (186 loc) · 6.1 KB
/
Copy pathStream.cpp
File metadata and controls
208 lines (186 loc) · 6.1 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//-----------------------------------------------------------------------------
//
// Stream.h
//
// Cross-platform circular buffer with signalling
//
// Copyright (c) 2010 Mal Lansell <mal@lansell.org>
// All rights reserved.
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include "platform/Stream.h"
#include "platform/Mutex.h"
#include "platform/Log.h"
#include <string.h>
#include <cstdio>
namespace OpenZWave
{
namespace Internal
{
namespace Platform
{
//-----------------------------------------------------------------------------
// <Stream::Stream>
// Constructor
//-----------------------------------------------------------------------------
Stream::Stream(uint32 _bufferSize) :
m_bufferSize(_bufferSize), m_signalSize(1), m_dataSize(0), m_head(0), m_tail(0), m_mutex(new Mutex())
{
m_buffer = new uint8[m_bufferSize];
memset(m_buffer, 0x00, m_bufferSize);
}
//-----------------------------------------------------------------------------
// <Stream::~Stream>
// Destructor
//-----------------------------------------------------------------------------
Stream::~Stream()
{
m_mutex->Release();
delete[] m_buffer;
}
//-----------------------------------------------------------------------------
// <Stream::SetSignalThreshold>
// Set the amount of data that must be in the buffer for to to be signalled
//-----------------------------------------------------------------------------
void Stream::SetSignalThreshold(uint32 _size)
{
m_signalSize = _size;
if (IsSignalled())
{
// We have more data than we are waiting for, so notify the watchers
Notify();
}
}
//-----------------------------------------------------------------------------
// <Stream::Get>
// Remove data from the buffer
//-----------------------------------------------------------------------------
bool Stream::Get(uint8* _buffer, uint32 _size)
{
if (m_dataSize < _size)
{
// There is not enough data in the buffer to fulfill the request
Log::Write(LogLevel_Error, "ERROR: Not enough data in stream buffer");
return false;
}
m_mutex->Lock();
if ((m_tail + _size) > m_bufferSize)
{
// We will have to wrap around
uint32 block1 = m_bufferSize - m_tail;
uint32 block2 = _size - block1;
memcpy(_buffer, &m_buffer[m_tail], block1);
memcpy(&_buffer[block1], m_buffer, block2);
m_tail = block2;
}
else
{
// Requested data is in a contiguous block
memcpy(_buffer, &m_buffer[m_tail], _size);
m_tail += _size;
}
LogData(_buffer, _size, " Read (buffer->application): ");
m_dataSize -= _size;
m_mutex->Unlock();
return true;
}
//-----------------------------------------------------------------------------
// <Stream::Put>
// Add data to the buffer
//-----------------------------------------------------------------------------
bool Stream::Put(uint8* _buffer, uint32 _size)
{
if ((m_bufferSize - m_dataSize) < _size)
{
// There is not enough space left in the buffer for the data
Log::Write(LogLevel_Error, "ERROR: Not enough space in stream buffer");
return false;
}
m_mutex->Lock();
if ((m_head + _size) > m_bufferSize)
{
// We will have to wrap around
uint32 block1 = m_bufferSize - m_head;
uint32 block2 = _size - block1;
memcpy(&m_buffer[m_head], _buffer, block1);
memcpy(m_buffer, &_buffer[block1], block2);
uint8 * logpos = m_buffer + m_head;
m_head = block2;
LogData(logpos, block1, " Read (controller->buffer): ");
LogData(m_buffer, block2, " Read (controller->buffer): ");
}
else
{
// There is enough space before we reach the end of the buffer
memcpy(&m_buffer[m_head], _buffer, _size);
m_head += _size;
LogData(m_buffer + m_head - _size, _size, " Read (controller->buffer): ");
}
m_dataSize += _size;
if (IsSignalled())
{
// We now have more data than we are waiting for, so notify the watchers
Notify();
}
m_mutex->Unlock();
return true;
}
//-----------------------------------------------------------------------------
// <Stream::Purge>
// Empty the data buffer
//-----------------------------------------------------------------------------
void Stream::Purge()
{
m_tail = 0;
m_head = 0;
m_dataSize = 0;
}
//-----------------------------------------------------------------------------
// <Stream::IsSignalled>
// Test whether there is enough data to be signalled
//-----------------------------------------------------------------------------
bool Stream::IsSignalled()
{
return (m_dataSize >= m_signalSize);
}
//-----------------------------------------------------------------------------
// <Stream::LogData>
// Format the stream buffer data for log output
//-----------------------------------------------------------------------------
void Stream::LogData(uint8* _buffer, uint32 _length, const string &_function)
{
if (!_length)
return;
string str = "";
for (uint32 i = 0; i < _length; ++i)
{
if (i)
{
str += ", ";
}
char byteStr[8];
snprintf(byteStr, sizeof(byteStr), "0x%.2x", _buffer[i]);
str += byteStr;
}
Log::Write(LogLevel_StreamDetail, "%s%s", _function.c_str(), str.c_str());
}
} // namespace Platform
} // namespace Internal
} // namespace OpenZWave