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 "ieee1394service.h" |
---|
23 |
#include "debugmodule.h" |
---|
24 |
|
---|
25 |
Ieee1394Service* Ieee1394Service::m_pInstance = 0; |
---|
26 |
|
---|
27 |
Ieee1394Service::Ieee1394Service() |
---|
28 |
: m_iPort( 0 ) |
---|
29 |
, m_bInitialised( false ) |
---|
30 |
, m_bRHThreadRunning( false ) |
---|
31 |
{ |
---|
32 |
pthread_mutex_init( &m_mutex, NULL ); |
---|
33 |
} |
---|
34 |
|
---|
35 |
Ieee1394Service::~Ieee1394Service() |
---|
36 |
{ |
---|
37 |
stopRHThread(); |
---|
38 |
if ( !m_handle ) { |
---|
39 |
raw1394_destroy_handle( m_handle ); |
---|
40 |
m_handle = 0; |
---|
41 |
} |
---|
42 |
} |
---|
43 |
|
---|
44 |
FBReturnCodes |
---|
45 |
Ieee1394Service::initialize() |
---|
46 |
{ |
---|
47 |
if ( !m_bInitialised ) { |
---|
48 |
m_handle = raw1394_new_handle(); |
---|
49 |
if ( !m_handle ) { |
---|
50 |
if ( !errno ) { |
---|
51 |
fprintf( stderr, "libraw1394 not compatible.\n" ); |
---|
52 |
} else { |
---|
53 |
perror ("Could not get 1394 handle"); |
---|
54 |
fprintf (stderr, "Is ieee1394 and raw1394 driver loaded?\n"); |
---|
55 |
} |
---|
56 |
return eFBRC_Creating1394HandleFailed; |
---|
57 |
} |
---|
58 |
|
---|
59 |
// Store this instance in the user data pointer, in order |
---|
60 |
// to be able to retrieve the instance in the pure C bus reset |
---|
61 |
// call back function. |
---|
62 |
raw1394_set_userdata( m_handle, this ); |
---|
63 |
|
---|
64 |
if ( raw1394_set_port( m_handle, m_iPort ) < 0 ) { |
---|
65 |
perror( "Could not set port" ); |
---|
66 |
return eFBRC_Setting1394PortFailed; |
---|
67 |
} |
---|
68 |
|
---|
69 |
raw1394_set_bus_reset_handler( m_handle, this->resetHandler ); |
---|
70 |
|
---|
71 |
startRHThread(); |
---|
72 |
|
---|
73 |
discoveryDevices(); |
---|
74 |
m_bInitialised = true; |
---|
75 |
} |
---|
76 |
return eFBRC_Success; |
---|
77 |
} |
---|
78 |
|
---|
79 |
Ieee1394Service* |
---|
80 |
Ieee1394Service::instance() |
---|
81 |
{ |
---|
82 |
if ( !m_pInstance ) { |
---|
83 |
m_pInstance = new Ieee1394Service; |
---|
84 |
} |
---|
85 |
return m_pInstance; |
---|
86 |
} |
---|
87 |
|
---|
88 |
FBReturnCodes |
---|
89 |
Ieee1394Service::discoveryDevices() |
---|
90 |
{ |
---|
91 |
return eFBRC_Success; |
---|
92 |
} |
---|
93 |
|
---|
94 |
int |
---|
95 |
Ieee1394Service::resetHandler( raw1394handle_t handle, |
---|
96 |
unsigned int iGeneration ) |
---|
97 |
{ |
---|
98 |
debugPrint( DEBUG_LEVEL_INFO, |
---|
99 |
"Bus reset has occurred (generation = %d).\n", iGeneration ); |
---|
100 |
raw1394_update_generation (handle, iGeneration); |
---|
101 |
Ieee1394Service* pInstance |
---|
102 |
= (Ieee1394Service*) raw1394_get_userdata (handle); |
---|
103 |
pInstance->sigGenerationCount( iGeneration ); |
---|
104 |
return 0; |
---|
105 |
} |
---|
106 |
|
---|
107 |
bool |
---|
108 |
Ieee1394Service::startRHThread() |
---|
109 |
{ |
---|
110 |
if ( m_bRHThreadRunning ) { |
---|
111 |
return true; |
---|
112 |
} |
---|
113 |
debugPrint( DEBUG_LEVEL_INFO, |
---|
114 |
"Starting bus reset handler thread.\n" ); |
---|
115 |
pthread_mutex_lock( &m_mutex ); |
---|
116 |
pthread_create( &m_thread, NULL, rHThread, this ); |
---|
117 |
pthread_mutex_unlock( &m_mutex ); |
---|
118 |
m_bRHThreadRunning = true; |
---|
119 |
return true; |
---|
120 |
} |
---|
121 |
|
---|
122 |
void |
---|
123 |
Ieee1394Service::stopRHThread() |
---|
124 |
{ |
---|
125 |
if ( m_bRHThreadRunning ) { |
---|
126 |
debugPrint( DEBUG_LEVEL_INFO, |
---|
127 |
"Stopping bus reset handler thread.\n" ); |
---|
128 |
pthread_mutex_lock (&m_mutex); |
---|
129 |
pthread_cancel (m_thread); |
---|
130 |
pthread_join (m_thread, NULL); |
---|
131 |
pthread_mutex_unlock (&m_mutex); |
---|
132 |
} |
---|
133 |
m_bRHThreadRunning = false; |
---|
134 |
} |
---|
135 |
|
---|
136 |
void* |
---|
137 |
Ieee1394Service::rHThread( void* arg ) |
---|
138 |
{ |
---|
139 |
Ieee1394Service* pIeee1394Service = (Ieee1394Service*) arg; |
---|
140 |
|
---|
141 |
while (true) { |
---|
142 |
raw1394_loop_iterate (pIeee1394Service->m_handle); |
---|
143 |
pthread_testcancel (); |
---|
144 |
} |
---|
145 |
|
---|
146 |
return NULL; |
---|
147 |
} |
---|