1 |
/* |
---|
2 |
* Copyright (C) 2005-2008 by Daniel Wagner |
---|
3 |
* |
---|
4 |
* This file is part of FFADO |
---|
5 |
* FFADO = Free Firewire (pro-)audio drivers for linux |
---|
6 |
* |
---|
7 |
* FFADO is based upon FreeBoB |
---|
8 |
* |
---|
9 |
* This program is free software: you can redistribute it and/or modify |
---|
10 |
* it under the terms of the GNU General Public License as published by |
---|
11 |
* the Free Software Foundation, either version 2 of the License, or |
---|
12 |
* (at your option) version 3 of the License. |
---|
13 |
* |
---|
14 |
* This program is distributed in the hope that it will be useful, |
---|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 |
* GNU General Public License for more details. |
---|
18 |
* |
---|
19 |
* You should have received a copy of the GNU General Public License |
---|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 |
* |
---|
22 |
*/ |
---|
23 |
|
---|
24 |
#include "cmd_serialize.h" |
---|
25 |
|
---|
26 |
#include <iostream> |
---|
27 |
#include <iomanip> |
---|
28 |
#include <cstdlib> |
---|
29 |
#include <cstring> |
---|
30 |
|
---|
31 |
#include "libutil/ByteSwap.h" |
---|
32 |
|
---|
33 |
|
---|
34 |
namespace Util { |
---|
35 |
namespace Cmd { |
---|
36 |
|
---|
37 |
|
---|
38 |
IMPL_DEBUG_MODULE( CoutSerializer, CoutSerializer, DEBUG_LEVEL_NORMAL ); |
---|
39 |
IMPL_DEBUG_MODULE( StringSerializer, StringSerializer, DEBUG_LEVEL_NORMAL ); |
---|
40 |
IMPL_DEBUG_MODULE( BufferSerialize, BufferSerialize, DEBUG_LEVEL_NORMAL ); |
---|
41 |
IMPL_DEBUG_MODULE( BufferDeserialize, BufferDeserialize, DEBUG_LEVEL_NORMAL ); |
---|
42 |
|
---|
43 |
bool |
---|
44 |
CoutSerializer::write( byte_t d, const char* name ) |
---|
45 |
{ |
---|
46 |
debugOutput( DEBUG_LEVEL_NORMAL, " %3d: 0x%02x %-40.40s\n", m_cnt, d, name ); |
---|
47 |
m_cnt += sizeof( byte_t ); |
---|
48 |
|
---|
49 |
return true; |
---|
50 |
} |
---|
51 |
|
---|
52 |
bool |
---|
53 |
CoutSerializer::write( uint16_t d, const char* name ) |
---|
54 |
{ |
---|
55 |
debugOutput( DEBUG_LEVEL_NORMAL, " %3d: 0x%04x %-40.40s\n", m_cnt, d, name ); |
---|
56 |
m_cnt += sizeof( uint16_t ); |
---|
57 |
|
---|
58 |
return true; |
---|
59 |
} |
---|
60 |
|
---|
61 |
bool |
---|
62 |
CoutSerializer::write( quadlet_t d, const char* name ) |
---|
63 |
{ |
---|
64 |
debugOutput( DEBUG_LEVEL_NORMAL, " %3d: 0x%08x %-40.40s\n", m_cnt, d, name ); |
---|
65 |
m_cnt += sizeof( quadlet_t ); |
---|
66 |
return true; |
---|
67 |
} |
---|
68 |
|
---|
69 |
bool |
---|
70 |
CoutSerializer::write( const char * v, size_t len, const char* name ) |
---|
71 |
{ |
---|
72 |
debugOutput( DEBUG_LEVEL_NORMAL, " %3d: %s %-40.40s\n", m_cnt, v, name ); |
---|
73 |
m_cnt += len; |
---|
74 |
return true; |
---|
75 |
} |
---|
76 |
////////////////////////////////////////////////// |
---|
77 |
|
---|
78 |
bool |
---|
79 |
StringSerializer::write( byte_t d, const char* name ) |
---|
80 |
{ |
---|
81 |
char* result; |
---|
82 |
asprintf( &result, " %3d:\t0x%02x\t%s\n", m_cnt, d, name ); |
---|
83 |
|
---|
84 |
m_string += result; |
---|
85 |
free( result ); |
---|
86 |
|
---|
87 |
m_cnt += sizeof( byte_t ); |
---|
88 |
|
---|
89 |
return true; |
---|
90 |
} |
---|
91 |
|
---|
92 |
bool |
---|
93 |
StringSerializer::write( uint16_t d, const char* name ) |
---|
94 |
{ |
---|
95 |
char* result; |
---|
96 |
asprintf( &result, " %3d:\t0x%04x\t%s\n", m_cnt, d, name ); |
---|
97 |
|
---|
98 |
m_string += result; |
---|
99 |
free( result ); |
---|
100 |
|
---|
101 |
m_cnt += sizeof( uint16_t ); |
---|
102 |
|
---|
103 |
return true; |
---|
104 |
} |
---|
105 |
|
---|
106 |
bool |
---|
107 |
StringSerializer::write( quadlet_t d, const char* name ) |
---|
108 |
{ |
---|
109 |
char* result; |
---|
110 |
asprintf( &result, " %3d:\t0x%08x\t%s\n", m_cnt, d, name ); |
---|
111 |
|
---|
112 |
m_string += result; |
---|
113 |
free( result ); |
---|
114 |
|
---|
115 |
m_cnt += sizeof( quadlet_t ); |
---|
116 |
return true; |
---|
117 |
} |
---|
118 |
|
---|
119 |
bool |
---|
120 |
StringSerializer::write( const char * v, size_t len, const char* name ) |
---|
121 |
{ |
---|
122 |
char* result; |
---|
123 |
asprintf( &result, " %3d:\t%s\t%s\n", m_cnt, v, name ); |
---|
124 |
|
---|
125 |
m_string += result; |
---|
126 |
free( result ); |
---|
127 |
|
---|
128 |
m_cnt += len; |
---|
129 |
return true; |
---|
130 |
} |
---|
131 |
////////////////////////////////////////////////// |
---|
132 |
|
---|
133 |
bool |
---|
134 |
BufferSerialize::write( byte_t value, const char* name ) |
---|
135 |
{ |
---|
136 |
bool result = false; |
---|
137 |
if ( isCurPosValid() ) { |
---|
138 |
*m_curPos = value; |
---|
139 |
m_curPos += sizeof( byte_t ); |
---|
140 |
result = true; |
---|
141 |
} |
---|
142 |
return result; |
---|
143 |
} |
---|
144 |
|
---|
145 |
bool |
---|
146 |
BufferSerialize::write( uint16_t value, const char* name ) |
---|
147 |
{ |
---|
148 |
byte_t hi = (value & 0xFF00) >> 8; |
---|
149 |
byte_t lo = value & 0xFF; |
---|
150 |
|
---|
151 |
bool result = false; |
---|
152 |
if ( isCurPosValid() ) { |
---|
153 |
*m_curPos = hi; |
---|
154 |
m_curPos += sizeof( byte_t ); |
---|
155 |
if ( isCurPosValid() ) { |
---|
156 |
*m_curPos = lo; |
---|
157 |
m_curPos += sizeof( byte_t ); |
---|
158 |
result = true; |
---|
159 |
} |
---|
160 |
} |
---|
161 |
return result; |
---|
162 |
} |
---|
163 |
|
---|
164 |
bool |
---|
165 |
BufferSerialize::write( quadlet_t value, const char* name ) |
---|
166 |
{ |
---|
167 |
bool result = false; |
---|
168 |
if ( isCurPosValid() ) { |
---|
169 |
* ( quadlet_t* )m_curPos = value; |
---|
170 |
m_curPos += sizeof( quadlet_t ); |
---|
171 |
result = true; |
---|
172 |
} |
---|
173 |
return result; |
---|
174 |
} |
---|
175 |
|
---|
176 |
bool |
---|
177 |
BufferSerialize::write( const char * v, size_t len, const char* name ) |
---|
178 |
{ |
---|
179 |
bool result = false; |
---|
180 |
if ( isCurPosValid() ) { |
---|
181 |
m_curPos += len; |
---|
182 |
// avoid write beyond buffer |
---|
183 |
if ( isCurPosValid() ) { |
---|
184 |
m_curPos -= len; |
---|
185 |
memcpy(m_curPos, v, len); |
---|
186 |
m_curPos += len; |
---|
187 |
result = true; |
---|
188 |
} |
---|
189 |
} |
---|
190 |
return result; |
---|
191 |
} |
---|
192 |
|
---|
193 |
bool |
---|
194 |
BufferSerialize::isCurPosValid() const |
---|
195 |
{ |
---|
196 |
if ( static_cast<size_t>( ( m_curPos - m_buffer ) ) >= m_length ) { |
---|
197 |
return false; |
---|
198 |
} |
---|
199 |
return true; |
---|
200 |
} |
---|
201 |
|
---|
202 |
////////////////////////////////////////////////// |
---|
203 |
|
---|
204 |
bool |
---|
205 |
BufferDeserialize::read( byte_t* value ) |
---|
206 |
{ |
---|
207 |
bool result = false; |
---|
208 |
if ( isCurPosValid() ) { |
---|
209 |
*value = *m_curPos; |
---|
210 |
m_curPos += sizeof( byte_t ); |
---|
211 |
result = true; |
---|
212 |
} |
---|
213 |
return result; |
---|
214 |
} |
---|
215 |
|
---|
216 |
bool |
---|
217 |
BufferDeserialize::read( uint16_t* value ) |
---|
218 |
{ |
---|
219 |
byte_t hi; |
---|
220 |
byte_t lo; |
---|
221 |
bool result = false; |
---|
222 |
if ( isCurPosValid() ) { |
---|
223 |
hi = *((byte_t *)m_curPos); |
---|
224 |
m_curPos += sizeof( byte_t ); |
---|
225 |
if ( isCurPosValid() ) { |
---|
226 |
lo = *((byte_t *)m_curPos); |
---|
227 |
m_curPos += sizeof( byte_t ); |
---|
228 |
*value=(hi << 8) | lo; |
---|
229 |
result = true; |
---|
230 |
} |
---|
231 |
} |
---|
232 |
return result; |
---|
233 |
} |
---|
234 |
|
---|
235 |
bool |
---|
236 |
BufferDeserialize::read( quadlet_t* value ) |
---|
237 |
{ |
---|
238 |
bool result = false; |
---|
239 |
if ( isCurPosValid() ) { |
---|
240 |
*value = *( ( quadlet_t* )m_curPos ); |
---|
241 |
m_curPos += sizeof( quadlet_t ); |
---|
242 |
result = true; |
---|
243 |
} |
---|
244 |
return result; |
---|
245 |
} |
---|
246 |
|
---|
247 |
bool |
---|
248 |
BufferDeserialize::read( char** value, size_t length ) |
---|
249 |
{ |
---|
250 |
bool result = false; |
---|
251 |
if ( isCurPosValid() ) { |
---|
252 |
*value = ( char* )m_curPos; |
---|
253 |
|
---|
254 |
m_curPos += length-1; |
---|
255 |
if ( !isCurPosValid() ) { |
---|
256 |
debugError("Read past end of response\n"); |
---|
257 |
result=false; |
---|
258 |
} else { |
---|
259 |
m_curPos++; |
---|
260 |
result = true; |
---|
261 |
} |
---|
262 |
} |
---|
263 |
return result; |
---|
264 |
} |
---|
265 |
|
---|
266 |
bool |
---|
267 |
BufferDeserialize::peek( byte_t* value ) |
---|
268 |
{ |
---|
269 |
bool result = false; |
---|
270 |
if ( isCurPosValid() ) { |
---|
271 |
*value = *m_curPos; |
---|
272 |
result = true; |
---|
273 |
} |
---|
274 |
return result; |
---|
275 |
} |
---|
276 |
|
---|
277 |
bool |
---|
278 |
BufferDeserialize::peek( uint16_t* value, size_t offset ) |
---|
279 |
{ |
---|
280 |
byte_t hi; |
---|
281 |
byte_t lo; |
---|
282 |
bool result = false; |
---|
283 |
m_curPos+=offset; |
---|
284 |
if ( isCurPosValid() ) { |
---|
285 |
hi = *((byte_t *)m_curPos); |
---|
286 |
m_curPos += sizeof( byte_t ); |
---|
287 |
if ( isCurPosValid() ) { |
---|
288 |
lo = *((byte_t *)m_curPos); |
---|
289 |
*value=(hi << 8) | lo; |
---|
290 |
result = true; |
---|
291 |
} |
---|
292 |
m_curPos -= sizeof( byte_t ); |
---|
293 |
} |
---|
294 |
m_curPos-=offset; |
---|
295 |
return result; |
---|
296 |
} |
---|
297 |
|
---|
298 |
bool |
---|
299 |
BufferDeserialize::skip( size_t length ) { |
---|
300 |
m_curPos+=length; |
---|
301 |
return true; |
---|
302 |
} |
---|
303 |
|
---|
304 |
bool |
---|
305 |
BufferDeserialize::isCurPosValid() const |
---|
306 |
{ |
---|
307 |
if ( static_cast<size_t>( ( m_curPos - m_buffer ) ) >= m_length ) { |
---|
308 |
return false; |
---|
309 |
} |
---|
310 |
return true; |
---|
311 |
} |
---|
312 |
|
---|
313 |
} |
---|
314 |
} |
---|