summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTDevice.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/opietooth2/OTDevice.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/networksettings2/opietooth2/OTDevice.cpp256
1 files changed, 256 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/opietooth2/OTDevice.cpp b/noncore/settings/networksettings2/opietooth2/OTDevice.cpp
new file mode 100644
index 0000000..62f17a0
--- a/dev/null
+++ b/noncore/settings/networksettings2/opietooth2/OTDevice.cpp
@@ -0,0 +1,256 @@
1#include <qdir.h>
2#include <qfileinfo.h>
3
4#include <bluezlib.h>
5
6/* OPIE */
7#include <opie2/odevice.h>
8#include <opie2/oprocess.h>
9#include <opie2/odebug.h>
10
11#include <OTDevice.h>
12
13using namespace Opie::Core;
14using namespace Opietooth2;
15using Opie::Core::OProcess;
16
17OTDevice::OTDevice( OTGateway * _OT ) : QObject(0, "device") {
18
19 // initialize
20 OT = _OT;
21
22 // detect bluetooth type
23 QString a, b; // fake
24 unsigned long c; // fake
25 detectDeviceType( a, b, c );
26
27 if( needsAttach() ) {
28 // requires HCIATTACH
29 // pid of hciattach
30 m_hciattachPid = getPidOfHCIAttach();
31
32 m_hciattach = 0;
33
34 if( m_hciattachPid == 0 ) {
35 // no pid -> not attached
36 m_deviceNr = -1;
37 } else {
38 // system enabled
39 // currently no way to figure out which attach produce which
40 // hci
41 m_deviceNr = 0;
42 }
43 } else {
44 m_deviceNr = 0;
45 }
46}
47
48OTDevice::~OTDevice(){
49 if( needsAttach() && m_hciattach ) {
50 // does not auto stop bluetooth
51 m_hciattach->detach();
52 delete m_hciattach;
53 }
54}
55
56bool OTDevice::attach(){
57
58 if( needsAttach() && m_hciattachPid == 0 ) {
59 QString Dev, Mode;
60 unsigned long Spd;
61
62 detectDeviceType( Dev, Mode, Spd );
63
64 // not yet started
65 m_hciattach = new OProcess();
66 *m_hciattach << "hciattach";
67 *m_hciattach << "-p"; // so that it prints its pid
68 *m_hciattach << Dev
69 << Mode
70 << QString().setNum(Spd);
71
72 connect( m_hciattach,
73 SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
74 this, SLOT
75 (slotStdOut(Opie::Core::OProcess*,char*,int) ) );
76
77 connect( m_hciattach,
78 SIGNAL(receivedStderr(Opie::Core::OProcess*, char*, int ) ),
79 this, SLOT
80 (slotStdErr(Opie::Core::OProcess*,char*,int) ) );
81
82 // we need to start this in 'dontcare' mode because
83 // if qpe exists we want the hci to keep running
84 if( ! m_hciattach->start( OProcess::DontCare,
85 OProcess::AllOutput
86 ) ){
87 emit error( tr( "Could not start hciattach" ) );
88 delete m_hciattach;
89 m_hciattach = 0;
90 return FALSE;
91 }
92 }
93
94 return TRUE;
95}
96
97bool OTDevice::detach(){
98
99 if( needsAttach() && m_hciattachPid ) {
100 if( m_hciattach ) {
101 delete m_hciattach;
102 m_hciattach = 0;
103 }
104
105 if( kill( m_hciattachPid, 9) < 0 ) {
106 owarn << "could not stop " << errno << oendl;
107 emit error( tr( "Could not stop process" ) );
108 return FALSE;
109 }
110 m_hciattachPid = 0;
111 emit isEnabled( m_deviceNr, 0 );
112 m_deviceNr = -1;
113 }
114
115 return TRUE;
116}
117
118bool OTDevice::isAttached()const{
119 return ! needsAttach() || m_hciattachPid != 0;
120}
121
122bool OTDevice::checkAttach(){
123 if( ! needsAttach() ) {
124 m_deviceNr = 0;
125 emit isEnabled( 0, 1 );
126 return TRUE;
127 }
128
129 if( m_hciattachPid ) {
130 QString S;
131 S.setNum( m_hciattachPid );
132 QDir D( "/proc" );
133 if( !D.exists( S ) ) {
134 // down
135 m_hciattachPid = 0;
136 emit isEnabled( m_deviceNr, 0 );
137 m_deviceNr = -1;
138 }
139 } else {
140 // check
141 m_hciattachPid = getPidOfHCIAttach();
142 if ( m_hciattachPid ) {
143 m_deviceNr = 0;
144 emit isEnabled( m_deviceNr, 1 );
145 }
146 }
147 return m_hciattachPid != 0;
148}
149
150void OTDevice::slotStdOut(OProcess* proc, char* , int ) {
151 if( proc == m_hciattach ) {
152 m_hciattach->detach();
153
154 // system enabled
155 // currently no way to figure out which attach produce which
156 // hci
157 if( m_deviceNr == -1 ) {
158 m_deviceNr = 0;
159 emit isEnabled( m_deviceNr, 1 );
160 }
161 }
162}
163
164void OTDevice::slotStdErr(OProcess* proc, char* chars, int len) {
165
166 if(proc == m_hciattach && len >= 1 ){
167 // collect output
168 QCString string( chars, len+1 ); // \0 == +1
169 QString m_output;
170 m_output.append( string.data() );
171 owarn << m_output << oendl;
172 }
173}
174
175pid_t OTDevice::getPidOfHCIAttach( void ) {
176
177 if( needsAttach() ) {
178 // not yet attached -> perhaps now ?
179 // load /proc dir and check if command name contains hciattach
180 QRegExp R("[0-9]+");
181 QDir ProcDir( "/proc" );
182 QFileInfo FI;
183 QStringList EL = ProcDir.entryList( QDir::Dirs );
184
185 // print it out
186 for ( QStringList::Iterator it = EL.begin();
187 it != EL.end();
188 ++it ) {
189 if( R.match( (*it) ) >= 0 ) {
190 // is pid
191
192 // get command being executed
193 FI.setFile( ProcDir.path()+"/"+ (*it) + "/exe" );
194
195 // get the link
196 if( FI.readLink().right( 9 ) == "hciattach" ) {
197 // this is hci attach process
198
199 return (*it).toULong();
200 break;
201 }
202 }
203 }
204 }
205
206 return 0;
207}
208
209void OTDevice::detectDeviceType( QString & Device,
210 QString & Mode,
211 unsigned long & Speed ) {
212
213 // detect device type and determine parms
214 owarn << "Detecting device" << oendl;
215 switch ( ODevice::inst()->model() ) {
216 case Model_iPAQ_H39xx:
217 Device = "/dev/tts/1";
218 Mode = "bcsp";
219 Speed = 921600;
220 NeedsAttach = 1;
221 break;
222
223 case Model_iPAQ_H5xxx:
224 Device = "/dev/tts/1";
225 Mode = "any";
226 Speed = 921600;
227 NeedsAttach = 1;
228 break;
229
230 case Model_GenuineIntel :
231 Device = "";
232 Mode = "";
233 Speed = 0;
234 NeedsAttach = 0;
235 break;
236
237 default:
238 Device = "/dev/ttySB0";
239 Mode = "bcsp";
240 Speed = 230400;
241 NeedsAttach = 1;
242 break;
243 }
244}
245
246QString OTDevice::getRFCommDevicePattern( void ) {
247
248 QDir D( "/dev/bluetooth/rfcomm" );
249 if( D.exists() ) {
250 // devfs
251 return QString( "/dev/bluetooth/rfcomm/%1" );
252 }
253
254 // regular 'dev' directory
255 return QString( "/dev/rfcomm%1" );
256}