root/trunk/freebob/src/ieee1394service.cpp

Revision 15, 10.6 kB (checked in by wagi, 19 years ago)

Removed sigc++ code, using instead boost library.
Print information on screen when doing bus scan.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* ieee1394service.cpp
2  * Copyright (C) 2004 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 <errno.h>
22 #include <libavc1394/avc1394.h>
23 #include <libavc1394/avc1394_vcr.h>
24 #include "ieee1394service.h"
25 #include "debugmodule.h"
26
27 Ieee1394Service* Ieee1394Service::m_pInstance = 0;
28
29 Ieee1394Service::Ieee1394Service()
30     : m_iPort( 0 )
31     , m_bInitialised( false )
32     , m_bRHThreadRunning( false )
33 {
34     pthread_mutex_init( &m_mutex, NULL );
35 }
36
37 Ieee1394Service::~Ieee1394Service()
38 {
39     stopRHThread();
40
41     if ( m_rhHandle ) {
42         raw1394_destroy_handle( m_rhHandle );
43         m_rhHandle = 0;
44     }
45
46     if ( m_handle ) {
47         raw1394_destroy_handle( m_handle );
48         m_handle = 0;
49     }
50
51     m_pInstance = 0;
52 }
53
54 FBReturnCodes
55 Ieee1394Service::initialize()
56 {
57     if ( !m_bInitialised ) {
58         m_rhHandle = raw1394_new_handle();
59         m_handle = raw1394_new_handle();
60         if ( !m_rhHandle || !m_handle ) {
61             if ( !errno ) {
62                 fprintf( stderr,  "libraw1394 not compatible.\n" );
63             } else {
64                 perror ("Could not get 1394 handle");
65                 fprintf (stderr, "Is ieee1394 and raw1394 driver loaded?\n");
66             }
67             return eFBRC_Creating1394HandleFailed;
68         }
69
70         // Store this instance in the user data pointer, in order
71         // to be able to retrieve the instance in the pure C bus reset
72         // call back function.
73         raw1394_set_userdata( m_rhHandle,  this );
74
75         if ( raw1394_set_port( m_rhHandle,  m_iPort ) < 0 ) {
76             perror( "Could not set port" );
77             return eFBRC_Setting1394PortFailed;
78         }
79
80         if ( raw1394_set_port( m_handle,  m_iPort ) < 0 ) {
81             perror( "Could not set port" );
82             return eFBRC_Setting1394PortFailed;
83         }
84
85         raw1394_set_bus_reset_handler( m_rhHandle,  this->resetHandler );
86
87         startRHThread();
88
89         discoveryDevices();
90         m_bInitialised = true;
91     }
92     return eFBRC_Success;
93 }
94
95 void
96 Ieee1394Service::shutdown()
97 {
98     delete this;
99 }
100
101 Ieee1394Service*
102 Ieee1394Service::instance()
103 {
104     if ( !m_pInstance ) {
105         m_pInstance = new Ieee1394Service;
106     }
107     return m_pInstance;
108 }
109
110 FBReturnCodes
111 Ieee1394Service::discoveryDevices()
112 {
113     //scan bus
114     int iNodeCount = raw1394_get_nodecount( m_handle );
115     for ( int iNodeId = 0; iNodeId < iNodeCount; ++iNodeId ) {
116         rom1394_directory romDir;
117         rom1394_get_directory( m_handle, iNodeId, &romDir );
118         printRomDirectory( iNodeId, &romDir );
119
120         switch (rom1394_get_node_type( &romDir )) {
121         case ROM1394_NODE_TYPE_UNKNOWN:
122             debugPrint (DEBUG_LEVEL_INFO,
123                         "Node %d has node type UNKNOWN\n", iNodeId);
124             break;
125         case ROM1394_NODE_TYPE_DC:
126             debugPrint (DEBUG_LEVEL_INFO,
127                         "Node %d has node type DC\n", iNodeId);
128             break;
129         case ROM1394_NODE_TYPE_AVC:
130             debugPrint (DEBUG_LEVEL_INFO,
131                         "Node %d has node type AVC\n", iNodeId);
132             printAvcUnitInfo( iNodeId );
133
134             if ( avc1394_check_subunit_type( m_handle, iNodeId,
135                                              AVC1394_SUBUNIT_TYPE_AUDIO ) ) {
136                 // XXX
137                 // create avcDevice which discovers itself :)
138             }
139             break;
140         case ROM1394_NODE_TYPE_SBP2:
141             debugPrint( DEBUG_LEVEL_INFO,
142                         "Node %d has node type SBP2\n", iNodeId);
143             break;
144         case ROM1394_NODE_TYPE_CPU:
145             debugPrint( DEBUG_LEVEL_INFO,
146                         "Node %d has node type CPU\n", iNodeId);
147             break;
148         default:
149             debugPrint( DEBUG_LEVEL_INFO,
150                         "No matching node type found for node %d\n", iNodeId);
151         }
152     }
153     return eFBRC_Success;
154 }
155
156 void
157 Ieee1394Service::printAvcUnitInfo( int iNodeId )
158 {
159     printf( "AVC: video monitor?......%s\n",
160             avc1394_check_subunit_type( m_handle, iNodeId,
161                                         AVC1394_SUBUNIT_TYPE_VIDEO_MONITOR ) ?
162             "yes":"no" );
163     printf( "AVC: audio?..............%s\n",
164             avc1394_check_subunit_type( m_handle, iNodeId,
165                                         AVC1394_SUBUNIT_TYPE_AUDIO ) ?
166             "yes":"no" );
167     printf( "AVC; printer?............%s\n",
168             avc1394_check_subunit_type( m_handle, iNodeId,
169                                         AVC1394_SUBUNIT_TYPE_PRINTER ) ?
170             "yes":"no" );
171     printf( "AVC: disk recorder?......%s\n",
172             avc1394_check_subunit_type( m_handle, iNodeId,
173                                         AVC1394_SUBUNIT_TYPE_DISC_RECORDER ) ?
174             "yes":"no" );
175     printf( "AVC: video recorder?.....%s\n",
176             avc1394_check_subunit_type( m_handle, iNodeId,
177                                         AVC1394_SUBUNIT_TYPE_TAPE_RECORDER ) ?
178             "yes":"no" );
179     printf( "AVC: vcr?................%s\n",
180             avc1394_check_subunit_type( m_handle, iNodeId,
181                                         AVC1394_SUBUNIT_TYPE_VCR ) ?
182             "yes":"no" );
183     printf( "AVC: tuner?..............%s\n",
184             avc1394_check_subunit_type( m_handle, iNodeId,
185                                         AVC1394_SUBUNIT_TYPE_TUNER ) ?
186             "yes":"no" );
187     printf( "AVC: CA?.................%s\n",
188             avc1394_check_subunit_type( m_handle, iNodeId,
189                                         AVC1394_SUBUNIT_TYPE_CA ) ?
190             "yes":"no" );
191     printf( "AVC: video camera?.......%s\n",
192             avc1394_check_subunit_type( m_handle, iNodeId,
193                                         AVC1394_SUBUNIT_TYPE_VIDEO_CAMERA ) ?
194             "yes":"no" );
195     printf( "AVC: panel?..............%s\n",
196             avc1394_check_subunit_type(m_handle, iNodeId,
197                                         AVC1394_SUBUNIT_TYPE_PANEL ) ?
198             "yes":"no" );
199     printf( "AVC: camera storage?.....%s\n",
200             avc1394_check_subunit_type( m_handle, iNodeId,
201                                         AVC1394_SUBUNIT_TYPE_CAMERA_STORAGE ) ?
202             "yes":"no" );
203     printf( "AVC: bulletin board?.....%s\n",
204             avc1394_check_subunit_type( m_handle, iNodeId,
205                                         AVC1394_SUBUNIT_TYPE_BULLETIN_BOARD ) ?
206             "yes":"no" );
207     printf( "AVC: vendor specificr?...%s\n",
208             avc1394_check_subunit_type( m_handle, iNodeId,
209                                         AVC1394_SUBUNIT_TYPE_VENDOR_UNIQUE ) ?
210             "yes":"no" );
211     printf( "AVC: extended?...........%s\n",
212             avc1394_check_subunit_type( m_handle, iNodeId,
213                                         AVC1394_SUBUNIT_TYPE_EXTENDED ) ?
214             "yes":"no" );
215     printf( "AVC: unit?...............%s\n",
216             avc1394_check_subunit_type( m_handle, iNodeId,
217                                         AVC1394_SUBUNIT_TYPE_UNIT ) ?
218             "yes":"no" );
219 }
220
221 void
222 Ieee1394Service::printRomDirectory( int iNodeId,  rom1394_directory* pRomDir )
223 {
224     int iBusInfoBlockLength
225         = rom1394_get_bus_info_block_length( m_handle,  iNodeId );
226     int iBusId = rom1394_get_bus_id( m_handle,  iNodeId );
227     octlet_t oGuid = rom1394_get_guid( m_handle, iNodeId );
228     rom1394_bus_options busOptions;
229     rom1394_get_bus_options( m_handle, iNodeId, &busOptions );
230
231     printf ( "\nNode %d: \n", iNodeId);
232     printf ( "-------------------------------------------------\n");
233     printf ("bus info block length = %d\n", iBusInfoBlockLength);
234     printf ("bus id = 0x%08x\n", iBusId);
235     printf ("bus options:\n");
236     printf ("    isochronous resource manager capable: %d\n", busOptions.irmc);
237     printf ("    cycle master capable                : %d\n", busOptions.cmc);
238     printf ("    isochronous capable                 : %d\n", busOptions.isc);
239     printf ("    bus manager capable                 : %d\n", busOptions.bmc);
240     printf ("    cycle master clock accuracy         : %d ppm\n", busOptions.cyc_clk_acc);
241     printf ("    maximum asynchronous record size    : %d bytes\n", busOptions.max_rec);
242     printf ("GUID: 0x%08x%08x\n", (quadlet_t) (oGuid>>32),
243             (quadlet_t) (oGuid & 0xffffffff));
244     printf ("directory:\n");
245     printf ("    node capabilities    : 0x%08x\n", pRomDir->node_capabilities);
246     printf ("    vendor id            : 0x%08x\n", pRomDir->vendor_id);
247     printf ("    unit spec id         : 0x%08x\n", pRomDir->unit_spec_id);
248     printf ("    unit software version: 0x%08x\n", pRomDir->unit_sw_version);
249     printf ("    model id             : 0x%08x\n", pRomDir->model_id);
250     printf ("    textual leaves       : %s\n",     pRomDir->label);
251 }
252
253 int
254 Ieee1394Service::resetHandler( raw1394handle_t handle,
255                                unsigned int iGeneration )
256 {
257     debugPrint( DEBUG_LEVEL_INFO,
258                 "Bus reset has occurred (generation = %d).\n", iGeneration );
259     raw1394_update_generation (handle, iGeneration);
260     Ieee1394Service* pInstance
261         = (Ieee1394Service*) raw1394_get_userdata (handle);
262     pInstance->sigGenerationCount( iGeneration );
263     return 0;
264 }
265
266 bool
267 Ieee1394Service::startRHThread()
268 {
269     if ( m_bRHThreadRunning ) {
270         return true;
271     }
272     debugPrint( DEBUG_LEVEL_INFO,
273                 "Starting bus reset handler thread.\n" );
274     pthread_mutex_lock( &m_mutex );
275     pthread_create( &m_thread, NULL, rHThread, this );
276     pthread_mutex_unlock( &m_mutex );
277     m_bRHThreadRunning = true;
278     return true;
279 }
280
281 void
282 Ieee1394Service::stopRHThread()
283 {
284     if ( m_bRHThreadRunning ) {
285         debugPrint( DEBUG_LEVEL_INFO,
286                     "Stopping bus reset handler thread.\n" );
287         pthread_mutex_lock (&m_mutex);
288         pthread_cancel (m_thread);
289         pthread_join (m_thread, NULL);
290         pthread_mutex_unlock (&m_mutex);
291     }
292     m_bRHThreadRunning = false;
293 }
294
295 void*
296 Ieee1394Service::rHThread( void* arg )
297 {
298     Ieee1394Service* pIeee1394Service = (Ieee1394Service*) arg;
299
300     while (true) {
301         raw1394_loop_iterate (pIeee1394Service->m_rhHandle);
302         pthread_testcancel ();
303     }
304
305     return NULL;
306 }
Note: See TracBrowser for help on using the browser.