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 |
// XXX \todo improve IOSSerialize::write interface |
---|
48 |
char* buf; |
---|
49 |
asprintf( &buf, "AVCCommand subunit (subunit_type = %d, subunit_id = %d)", |
---|
50 |
getSubunitType(), getSubunitId() ); |
---|
51 |
se.write( m_subunit, buf ); |
---|
52 |
free(buf); |
---|
53 |
se.write( m_opcode, "AVCCommand opcode" ); |
---|
54 |
return true; |
---|
55 |
} |
---|
56 |
|
---|
57 |
bool |
---|
58 |
AVCCommand::deserialize( IISDeserialize& de ) |
---|
59 |
{ |
---|
60 |
de.read( &m_ctype ); |
---|
61 |
de.read( &m_subunit ); |
---|
62 |
de.read( &m_opcode ); |
---|
63 |
return true; |
---|
64 |
} |
---|
65 |
|
---|
66 |
bool |
---|
67 |
AVCCommand::setCommandType( ECommandType commandType ) |
---|
68 |
{ |
---|
69 |
m_ctype = commandType; |
---|
70 |
m_commandType = commandType; |
---|
71 |
return true; |
---|
72 |
} |
---|
73 |
|
---|
74 |
AVCCommand::ECommandType |
---|
75 |
AVCCommand::getCommandType() |
---|
76 |
{ |
---|
77 |
return m_commandType; |
---|
78 |
} |
---|
79 |
|
---|
80 |
AVCCommand::EResponse |
---|
81 |
AVCCommand::getResponse() |
---|
82 |
{ |
---|
83 |
return m_eResponse; |
---|
84 |
} |
---|
85 |
|
---|
86 |
bool |
---|
87 |
AVCCommand::setSubunitType(ESubunitType subunitType) |
---|
88 |
{ |
---|
89 |
byte_t subT = subunitType; |
---|
90 |
|
---|
91 |
m_subunit = ( subT << 3 ) | ( m_subunit & 0x7 ); |
---|
92 |
return true; |
---|
93 |
} |
---|
94 |
|
---|
95 |
bool |
---|
96 |
AVCCommand::setNodeId( fb_nodeid_t nodeId ) |
---|
97 |
{ |
---|
98 |
m_nodeId = nodeId; |
---|
99 |
return true; |
---|
100 |
} |
---|
101 |
|
---|
102 |
bool |
---|
103 |
AVCCommand::setSubunitId(subunit_id_t subunitId) |
---|
104 |
{ |
---|
105 |
m_subunit = ( subunitId & 0x7 ) | ( m_subunit & 0xf8 ); |
---|
106 |
return true; |
---|
107 |
} |
---|
108 |
|
---|
109 |
AVCCommand::ESubunitType |
---|
110 |
AVCCommand::getSubunitType() |
---|
111 |
{ |
---|
112 |
return static_cast<ESubunitType>( ( m_subunit >> 3 ) ); |
---|
113 |
} |
---|
114 |
|
---|
115 |
subunit_id_t |
---|
116 |
AVCCommand::getSubunitId() |
---|
117 |
{ |
---|
118 |
return m_subunit & 0x7; |
---|
119 |
} |
---|
120 |
|
---|
121 |
bool |
---|
122 |
AVCCommand::setVerbose( int verboseLevel ) |
---|
123 |
{ |
---|
124 |
m_verboseLevel = verboseLevel; |
---|
125 |
return true; |
---|
126 |
} |
---|
127 |
|
---|
128 |
int |
---|
129 |
AVCCommand::getVerboseLevel() |
---|
130 |
{ |
---|
131 |
return m_verboseLevel; |
---|
132 |
} |
---|
133 |
|
---|
134 |
|
---|
135 |
void |
---|
136 |
AVCCommand::showFcpFrame( const unsigned char* buf, |
---|
137 |
unsigned short frameSize ) const |
---|
138 |
{ |
---|
139 |
for ( int i = 0; i < frameSize; ++i ) { |
---|
140 |
if ( ( i % 16 ) == 0 ) { |
---|
141 |
if ( i > 0 ) { |
---|
142 |
printf( "\n" ); |
---|
143 |
} |
---|
144 |
printf( " %3d:\t", i ); |
---|
145 |
} else if ( ( i % 4 ) == 0 ) { |
---|
146 |
printf( " " ); |
---|
147 |
} |
---|
148 |
printf( "%02x ", buf[i] ); |
---|
149 |
} |
---|
150 |
printf( "\n" ); |
---|
151 |
} |
---|
152 |
|
---|
153 |
bool |
---|
154 |
AVCCommand::fire() |
---|
155 |
{ |
---|
156 |
memset( &m_fcpFrame, 0x0, sizeof( m_fcpFrame ) ); |
---|
157 |
|
---|
158 |
BufferSerialize se( m_fcpFrame, sizeof( m_fcpFrame ) ); |
---|
159 |
if ( !serialize( se ) ) { |
---|
160 |
printf( "ExtendedPlugInfoCmd::fire: Could not serialize\n" ); |
---|
161 |
return false; |
---|
162 |
} |
---|
163 |
|
---|
164 |
unsigned short fcpFrameSize = se.getNrOfProducesBytes(); |
---|
165 |
|
---|
166 |
if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE ) { |
---|
167 |
printf( "%s:\n", getCmdName() ); |
---|
168 |
puts( " Request:"); |
---|
169 |
showFcpFrame( m_fcpFrame, fcpFrameSize ); |
---|
170 |
|
---|
171 |
CoutSerializer se; |
---|
172 |
serialize( se ); |
---|
173 |
} |
---|
174 |
|
---|
175 |
unsigned int resp_len; |
---|
176 |
quadlet_t* resp = m_1394Service->transactionBlock( m_nodeId, |
---|
177 |
(quadlet_t*)m_fcpFrame, |
---|
178 |
( fcpFrameSize+3 ) / 4, |
---|
179 |
&resp_len ); |
---|
180 |
bool result = false; |
---|
181 |
if ( resp ) { |
---|
182 |
resp_len *= 4; |
---|
183 |
unsigned char* buf = ( unsigned char* ) resp; |
---|
184 |
|
---|
185 |
m_eResponse = ( EResponse )( *buf ); |
---|
186 |
switch ( m_eResponse ) |
---|
187 |
{ |
---|
188 |
case eR_Implemented: |
---|
189 |
case eR_Rejected: |
---|
190 |
case eR_NotImplemented: |
---|
191 |
{ |
---|
192 |
BufferDeserialize de( buf, resp_len ); |
---|
193 |
result = deserialize( de ); |
---|
194 |
|
---|
195 |
if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE) { |
---|
196 |
puts(" Response:"); |
---|
197 |
showFcpFrame( buf, de.getNrOfConsumedBytes() ); |
---|
198 |
|
---|
199 |
CoutSerializer se; |
---|
200 |
serialize( se ); |
---|
201 |
} |
---|
202 |
} |
---|
203 |
break; |
---|
204 |
default: |
---|
205 |
printf( "unexpected response received (0x%x)\n", m_eResponse ); |
---|
206 |
if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE) { |
---|
207 |
puts(" Response:"); |
---|
208 |
BufferDeserialize de( buf, resp_len ); |
---|
209 |
deserialize( de ); |
---|
210 |
|
---|
211 |
showFcpFrame( buf, resp_len ); |
---|
212 |
} |
---|
213 |
|
---|
214 |
} |
---|
215 |
} else { |
---|
216 |
printf( "no response\n" ); |
---|
217 |
} |
---|
218 |
|
---|
219 |
if ( getVerboseLevel() >= DEBUG_EXTRA_VERBOSE ) { |
---|
220 |
printf( "\n" ); |
---|
221 |
} |
---|
222 |
|
---|
223 |
m_1394Service->transactionBlockClose(); |
---|
224 |
|
---|
225 |
usleep( m_time ); |
---|
226 |
|
---|
227 |
return result; |
---|
228 |
} |
---|
229 |
|
---|
230 |
void |
---|
231 |
AVCCommand::setSleepAfterAVCCommand( int time ) |
---|
232 |
{ |
---|
233 |
m_time = time; |
---|
234 |
} |
---|
235 |
|
---|
236 |
const char* subunitTypeStrings[] = |
---|
237 |
{ |
---|
238 |
"Monitor", |
---|
239 |
"Audio", |
---|
240 |
"Printer", |
---|
241 |
"Disc recorder", |
---|
242 |
"Tape recorder/VCR", |
---|
243 |
"Tuner", |
---|
244 |
"CA", |
---|
245 |
"Video camera", |
---|
246 |
"unknown", |
---|
247 |
"Panel", |
---|
248 |
"Bulletin board", |
---|
249 |
"Camera storage", |
---|
250 |
"Music", |
---|
251 |
}; |
---|
252 |
|
---|
253 |
const char* |
---|
254 |
subunitTypeToString( subunit_type_t subunitType ) |
---|
255 |
{ |
---|
256 |
if ( subunitType == AVCCommand::eST_Unit ) { |
---|
257 |
return "Unit"; |
---|
258 |
} |
---|
259 |
if ( subunitType > ( int ) ( sizeof( subunitTypeStrings ) |
---|
260 |
/ sizeof( subunitTypeStrings[0] ) ) ) { |
---|
261 |
return "unknown"; |
---|
262 |
} else { |
---|
263 |
return subunitTypeStrings[subunitType]; |
---|
264 |
} |
---|
265 |
} |
---|