root/trunk/libffado/src/libutil/cmd_serialize.cpp

Revision 630, 6.7 kB (checked in by wagi, 13 years ago)

Some more fixing of the cashing code. The loading is still disabled because it seems not to work right. But at least it doesn't crash anymore. Some further debugging is
needed.

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