root/trunk/libfreebob/src/libfreebobavc/avc_generic.cpp

Revision 238, 6.0 kB (checked in by wagi, 18 years ago)

2006-05-31 Daniel Wagner <wagi@monom.org>

  • Free all allocated asprintf buffers after xmlNewChild.
    Compiler warning removed
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* avc_generic.cpp
2  * Copyright (C) 2005 by Daniel Wagner
3  *
4  * This file is part of FreeBob.
5  *
6  * FreeBob is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBob is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBob; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "avc_generic.h"
22 #include "serialize.h"
23 #include "ieee1394service.h"
24
25 #include <netinet/in.h>
26
27 #define DEBUG_EXTRA_VERBOSE 2
28
29 int AVCCommand::m_time = 0;
30
31 AVCCommand::AVCCommand( Ieee1394Service* ieee1394service,
32                         opcode_t opcode )
33     : m_1394Service( ieee1394service )
34     , m_nodeId( 0 )
35     , m_ctype( eCT_Unknown )
36     , m_subunit( 0xff )
37     , m_opcode( opcode )
38     , m_eResponse( eR_Unknown )
39     , m_verboseLevel( 0 )
40 {
41 }
42
43 bool
44 AVCCommand::serialize( IOSSerialize& se )
45 {
46     se.write( m_ctype, "AVCCommand ctype" );
47
48     // XXX \todo improve IOSSerialize::write interface
49     char* buf;
50     asprintf( &buf, "AVCCommand subunit (subunit_type = %d, subunit_id = %d)",
51               getSubunitType(), getSubunitId() );
52     se.write( m_subunit, buf );
53     free( buf );
54
55     se.write( m_opcode, "AVCCommand opcode" );
56     return true;
57 }
58
59 bool
60 AVCCommand::deserialize( IISDeserialize& de )
61 {
62     de.read( &m_ctype );
63     de.read( &m_subunit );
64     de.read( &m_opcode );
65     return true;
66 }
67
68 bool
69 AVCCommand::setCommandType( ECommandType commandType )
70 {
71     m_ctype = commandType;
72     m_commandType = commandType;
73     return true;
74 }
75
76 AVCCommand::ECommandType
77 AVCCommand::getCommandType()
78 {
79     return m_commandType;
80 }
81
82 AVCCommand::EResponse
83 AVCCommand::getResponse()
84 {
85     return m_eResponse;
86 }
87
88 bool
89 AVCCommand::setSubunitType(ESubunitType subunitType)
90 {
91     byte_t subT = subunitType;
92
93     m_subunit = ( subT << 3 ) | ( m_subunit & 0x7 );
94     return true;
95 }
96
97 bool
98 AVCCommand::setNodeId( fb_nodeid_t nodeId )
99 {
100     m_nodeId = nodeId;
101     return true;
102 }
103
104 bool
105 AVCCommand::setSubunitId(subunit_id_t subunitId)
106 {
107     m_subunit = ( subunitId & 0x7 ) | ( m_subunit & 0xf8 );
108     return true;
109 }
110
111 AVCCommand::ESubunitType
112 AVCCommand::getSubunitType()
113 {
114     return static_cast<ESubunitType>( ( m_subunit >> 3 ) );
115 }
116
117 subunit_id_t
118 AVCCommand::getSubunitId()
119 {
120     return m_subunit & 0x7;
121 }
122
123 bool
124 AVCCommand::setVerbose( int verboseLevel )
125 {
126     m_verboseLevel = verboseLevel;
127     return true;
128 }
129
130 int
131 AVCCommand::getVerboseLevel()
132 {
133     return m_verboseLevel;
134 }
135
136
137 void
138 AVCCommand::showFcpFrame( const unsigned char* buf,
139                           unsigned short frameSize ) const
140 {
141     for ( int i = 0; i < frameSize; ++i ) {
142         if ( ( i % 16 ) == 0 ) {
143             if ( i > 0 ) {
144                 printf( "\n" );
145             }
146             printf( "  %3d:\t", i );
147         } else if ( ( i % 4 ) == 0 ) {
148             printf( " " );
149         }
150         printf( "%02x ", buf[i] );
151     }
152     printf( "\n" );
153 }
154
155 bool
156 AVCCommand::fire()
157 {
158     memset( &m_fcpFrame,  0x0,  sizeof( m_fcpFrame ) );
159
160     BufferSerialize se( m_fcpFrame, sizeof( m_fcpFrame ) );
161     if ( !serialize( se ) ) {
162         printf(  "ExtendedPlugInfoCmd::fire: Could not serialize\n" );
163         return false;
164     }
165
166     unsigned short fcpFrameSize = se.getNrOfProducesBytes();
167
168     if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE ) {
169         printf( "%s:\n", getCmdName() );
170         puts( "  Request:");
171         showFcpFrame( m_fcpFrame, fcpFrameSize );
172
173         CoutSerializer se;
174         serialize( se );
175     }
176
177     unsigned int resp_len;
178     quadlet_t* resp = m_1394Service->transactionBlock( m_nodeId,
179                                                        (quadlet_t*)m_fcpFrame,
180                                                        ( fcpFrameSize+3 ) / 4,
181                                                        &resp_len );
182     bool result = false;
183     if ( resp ) {
184         resp_len *= 4;
185         unsigned char* buf = ( unsigned char* ) resp;
186
187         m_eResponse = ( EResponse )( *buf );
188         switch ( m_eResponse )
189         {
190         case eR_Implemented:
191         case eR_Rejected:
192         case eR_NotImplemented:
193         {
194             BufferDeserialize de( buf, resp_len );
195             result = deserialize( de );
196
197             if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE) {
198                 puts("  Response:");
199                 showFcpFrame( buf, de.getNrOfConsumedBytes() );
200
201                 CoutSerializer se;
202                 serialize( se );
203             }
204         }
205         break;
206         default:
207             printf( "unexpected response received (0x%x)\n", m_eResponse );
208             if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE) {
209                 puts("  Response:");
210                 BufferDeserialize de( buf, resp_len );
211                 deserialize( de );
212
213                 showFcpFrame( buf, resp_len );
214             }
215
216         }
217     } else {
218         printf( "no response\n" );
219     }
220
221     if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE ) {
222         printf( "\n" );
223     }
224
225     m_1394Service->transactionBlockClose();
226
227     usleep( m_time );
228
229     return result;
230 }
231
232 void
233 AVCCommand::setSleepAfterAVCCommand( int time )
234 {
235     m_time = time;
236 }
237
238 const char* subunitTypeStrings[] =
239 {
240     "Monitor",
241     "Audio",
242     "Printer",
243     "Disc recorder",
244     "Tape recorder/VCR",
245     "Tuner",
246     "CA",
247     "Video camera",
248     "unknown",
249     "Panel",
250     "Bulletin board",
251     "Camera storage",
252     "Music",
253 };
254
255 const char*
256 subunitTypeToString( subunit_type_t subunitType )
257 {
258     if ( subunitType == AVCCommand::eST_Unit ) {
259         return "Unit";
260     }
261     if ( subunitType > ( int ) ( sizeof( subunitTypeStrings )
262              / sizeof( subunitTypeStrings[0] ) ) ) {
263         return "unknown";
264     } else {
265         return subunitTypeStrings[subunitType];
266     }
267 }
Note: See TracBrowser for help on using the browser.