root/branches/echoaudio/src/libavc/util/avc_serialize.cpp

Revision 502, 6.7 kB (checked in by ppalmers, 17 years ago)

Restructure the libavc directory in order to improve maintainability when
extending the implemented AV/C standards. The new directory structure is
a reflection of the specifications: the files in each directory (roughly)
correspond to the same specification.

The breakdown is:

general : AV/C Digital Interface Command Set General Specification
audiosubunit : Audio Subunit Specification
musicsubunit : Music Subunit Specification
ccm : Connection and Compatibility Management Specification
descriptors : AV/C Descriptor Mechanism Specification

util : Various utility classes (not from specs)

  • 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 "avc_serialize.h"
25
26 #include <iostream>
27 #include <iomanip>
28
29 #include <netinet/in.h>
30
31 IMPL_DEBUG_MODULE( CoutSerializer, CoutSerializer, DEBUG_LEVEL_NORMAL );
32 IMPL_DEBUG_MODULE( StringSerializer, StringSerializer, DEBUG_LEVEL_NORMAL );
33 IMPL_DEBUG_MODULE( BufferSerialize, BufferSerialize, DEBUG_LEVEL_NORMAL );
34 IMPL_DEBUG_MODULE( BufferDeserialize, BufferDeserialize, DEBUG_LEVEL_NORMAL );
35
36 bool
37 CoutSerializer::write( byte_t d, const char* name )
38 {
39     debugOutput( DEBUG_LEVEL_NORMAL, "  %3d:        0x%02x %40s\n", m_cnt, d, name );
40     m_cnt += sizeof( byte_t );
41
42     return true;
43 }
44
45 bool
46 CoutSerializer::write( uint16_t d, const char* name )
47 {
48     debugOutput( DEBUG_LEVEL_NORMAL, "  %3d:    0x%04x %40s\n", m_cnt, d, name );
49     m_cnt += sizeof( uint16_t );
50
51     return true;
52 }
53
54 bool
55 CoutSerializer::write( quadlet_t d, const char* name )
56 {
57     debugOutput( DEBUG_LEVEL_NORMAL, "  %3d: 0x%08x %40s\n", m_cnt, d, name );
58     m_cnt += sizeof( quadlet_t );
59     return true;
60 }
61
62 bool
63 CoutSerializer::write( const char * v, size_t len, const char* name )
64 {
65     debugOutput( DEBUG_LEVEL_NORMAL, "  %3d: %s %40s\n", m_cnt, v, name );
66     m_cnt += len;
67     return true;
68 }
69 //////////////////////////////////////////////////
70
71 bool
72 StringSerializer::write( byte_t d, const char* name )
73 {
74     char* result;
75     asprintf( &result, "  %3d:\t0x%02x\t%s\n", m_cnt, d, name );
76
77     m_string += result;
78     free( result );
79
80     m_cnt += sizeof( byte_t );
81
82     return true;
83 }
84
85 bool
86 StringSerializer::write( uint16_t d, const char* name )
87 {
88     char* result;
89     asprintf( &result, "  %3d:\t0x%04x\t%s\n", m_cnt, d, name );
90
91     m_string += result;
92     free( result );
93
94     m_cnt += sizeof( uint16_t );
95
96     return true;
97 }
98
99 bool
100 StringSerializer::write( quadlet_t d, const char* name )
101 {
102     char* result;
103     asprintf( &result, "  %3d:\t0x%08x\t%s\n", m_cnt, d, name );
104
105     m_string += result;
106     free( result );
107
108     m_cnt += sizeof( quadlet_t );
109     return true;
110 }
111
112 bool
113 StringSerializer::write( const char * v, size_t len, const char* name )
114 {
115     char* result;
116     asprintf( &result, "  %3d:\t%s\t%s\n", m_cnt, v, name );
117
118     m_string += result;
119     free( result );
120
121     m_cnt += len;
122     return true;
123 }
124 //////////////////////////////////////////////////
125
126 bool
127 BufferSerialize::write( byte_t value, const char* name )
128 {
129     bool result = false;
130     if ( isCurPosValid() ) {
131         *m_curPos = value;
132         m_curPos += sizeof( byte_t );
133         result = true;
134     }
135     return result;
136 }
137
138 bool
139 BufferSerialize::write( uint16_t value, const char* name )
140 {
141     byte_t hi = (value & 0xFF00) >> 8;
142     byte_t lo = value & 0xFF;
143    
144     bool result = false;
145     if ( isCurPosValid() ) {
146         *m_curPos = hi;
147         m_curPos += sizeof( byte_t );
148         if ( isCurPosValid() ) {
149             *m_curPos = lo;
150             m_curPos += sizeof( byte_t );
151             result = true;
152         }
153     }
154     return result;
155 }
156
157 bool
158 BufferSerialize::write( quadlet_t value,  const char* name )
159 {
160     bool result = false;
161     if ( isCurPosValid() ) {
162         * ( quadlet_t* )m_curPos = value;
163         m_curPos += sizeof( quadlet_t );
164         result = true;
165     }
166     return result;
167 }
168
169 bool
170 BufferSerialize::write( const char * v, size_t len, const char* name )
171 {
172     bool result = false;
173     if ( isCurPosValid() ) {
174         m_curPos += len;
175         // avoid write beyond buffer
176         if ( isCurPosValid() ) {
177             m_curPos -= len;
178             memcpy(m_curPos, v, len);
179             m_curPos += len;
180             result = true;
181         }
182     }
183     return result;
184 }
185
186 bool
187 BufferSerialize::isCurPosValid() const
188 {
189     if ( static_cast<size_t>( ( m_curPos - m_buffer ) ) >= m_length ) {
190         return false;
191     }
192     return true;
193 }
194
195 //////////////////////////////////////////////////
196
197 bool
198 BufferDeserialize::read( byte_t* value )
199 {
200     bool result = false;
201     if ( isCurPosValid() ) {
202         *value = *m_curPos;
203         m_curPos += sizeof( byte_t );
204         result = true;
205     }
206     return result;
207 }
208
209 bool
210 BufferDeserialize::read( uint16_t* value )
211 {
212     byte_t hi;
213     byte_t lo;
214     bool result = false;
215     if ( isCurPosValid() ) {
216         hi = *((byte_t *)m_curPos);
217         m_curPos += sizeof( byte_t );
218         if ( isCurPosValid() ) {
219             lo = *((byte_t *)m_curPos);
220             m_curPos += sizeof( byte_t );
221             *value=(hi << 8) | lo;
222             result = true;
223         }
224     }
225     return result;
226 }
227
228 bool
229 BufferDeserialize::read( quadlet_t* value )
230 {
231     bool result = false;
232     if ( isCurPosValid() ) {
233         *value = *( ( quadlet_t* )m_curPos );
234         m_curPos += sizeof( quadlet_t );
235         result = true;
236     }
237     return result;
238 }
239
240 bool
241 BufferDeserialize::read( char** value, size_t length )
242 {
243     bool result = false;
244     if ( isCurPosValid() ) {
245         *value = ( char* )m_curPos;
246
247         m_curPos += length-1;
248         if ( !isCurPosValid() ) {
249             debugError("Read past end of response\n");
250             result=false;
251         } else {
252             m_curPos++;
253             result = true;
254         }
255     }
256     return result;
257 }
258
259 bool
260 BufferDeserialize::peek( byte_t* value )
261 {
262     bool result = false;
263     if ( isCurPosValid() ) {
264         *value = *m_curPos;
265         result = true;
266     }
267     return result;
268 }
269
270 bool
271 BufferDeserialize::peek( uint16_t* value, size_t offset )
272 {
273     byte_t hi;
274     byte_t lo;
275     bool result = false;
276     m_curPos+=offset;
277     if ( isCurPosValid() ) {
278         hi = *((byte_t *)m_curPos);
279         m_curPos += sizeof( byte_t );
280         if ( isCurPosValid() ) {
281             lo = *((byte_t *)m_curPos);
282             *value=(hi << 8) | lo;
283             result = true;
284         }
285         m_curPos -= sizeof( byte_t );
286     }
287     m_curPos-=offset;
288     return result;
289 }
290
291 bool
292 BufferDeserialize::skip( size_t length ) {
293     m_curPos+=length;
294     return true;
295 }
296
297 bool
298 BufferDeserialize::isCurPosValid() const
299 {
300     if ( static_cast<size_t>( ( m_curPos - m_buffer ) ) >= m_length ) {
301         return false;
302     }
303     return true;
304 }
305
Note: See TracBrowser for help on using the browser.