summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTDevice.cpp
blob: c6f7d5ed2778b680de2d60f977d1540e982283be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <qdir.h>
#include <qfileinfo.h>

#include <bluezlib.h>

/* OPIE */
#include <opie2/odevice.h>
#include <opie2/oprocess.h>
#include <opie2/odebug.h>

#include <OTDevice.h>

using namespace Opie::Core;
using namespace Opietooth2;
using Opie::Core::OProcess;

OTDevice::OTDevice( OTGateway * _OT ) : QObject(0, "device") {
      
      // initialize 
      OT = _OT;

      // detect bluetooth type
      QString a, b; // fake
      unsigned long c; // fake
      detectDeviceType( a, b, c );

      if( needsAttach() ) {
        // requires HCIATTACH
        // pid of hciattach
        m_hciattachPid = getPidOfHCIAttach();

        m_hciattach = 0;

        if( m_hciattachPid == 0 ) {
          // no pid -> not attached
          m_deviceNr = -1;
        } else {
          // system enabled
          // currently no way to figure out which attach produce which
          // hci
          m_deviceNr = 0;
        }
      } else {
        m_deviceNr = 0;
      }
}

OTDevice::~OTDevice(){
      if( needsAttach() && m_hciattach ) {
        // does not auto stop bluetooth
        m_hciattach->detach();
        delete m_hciattach;
      }
}

bool OTDevice::attach(){

      if( needsAttach() && m_hciattachPid == 0 ) {
        QString Dev, Mode;
        unsigned long Spd;

        detectDeviceType( Dev, Mode, Spd );

        // not yet started
        m_hciattach = new OProcess();
        *m_hciattach << "hciattach";
        *m_hciattach << "-p"; // so that it prints its pid
        *m_hciattach << Dev 
                     << Mode 
                     << QString().setNum(Spd);

        connect( m_hciattach, 
                 SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
                 this, SLOT
                 (slotStdOut(Opie::Core::OProcess*,char*,int) ) );

        connect( m_hciattach, 
                 SIGNAL(receivedStderr(Opie::Core::OProcess*, char*, int ) ),
                 this, SLOT
                 (slotStdErr(Opie::Core::OProcess*,char*,int) ) );

        // we need to start this in 'dontcare' mode because
        // if qpe exists we want the hci to keep running
        if( ! m_hciattach->start( OProcess::DontCare, 
                                  OProcess::AllOutput
                                ) ){
          emit error( tr( "Could not start hciattach" ) );
          delete m_hciattach;
          m_hciattach = 0;
          return FALSE;
        }
      }

      return TRUE;
}

bool OTDevice::detach(){

      if( needsAttach() && m_hciattachPid ) {
        if( m_hciattach ) {
          delete m_hciattach;
          m_hciattach = 0;
        }

        if( kill( m_hciattachPid, 9) < 0 ) {
          odebug << "could not stop " << errno << oendl;
          emit error( tr( "Could not stop process" ) );
          return FALSE;
        }
        m_hciattachPid = 0;
        emit isEnabled( m_deviceNr, 0 );
        m_deviceNr = -1;
      }

      return TRUE;
}

bool OTDevice::isAttached()const{
      return ! needsAttach() || m_hciattachPid != 0;
}

bool OTDevice::checkAttach(){
      if( ! needsAttach() ) {
        m_deviceNr = 0;
        emit isEnabled( 0, 1 );
        return TRUE;
      }

      if( m_hciattachPid ) {
        QString S;
        S.setNum( m_hciattachPid );
        QDir D( "/proc" );
        if( !D.exists( S ) ) {
          // down
          m_hciattachPid = 0;
          emit isEnabled( m_deviceNr, 0 );
          m_deviceNr = -1;
        }
      } else {
        // check
        m_hciattachPid = getPidOfHCIAttach();
        if ( m_hciattachPid ) {
          m_deviceNr = 0;
          emit isEnabled( m_deviceNr, 1 );
        }
      }
      return m_hciattachPid != 0;
}

void OTDevice::slotStdOut(OProcess* proc, char* , int ) {
      if( proc == m_hciattach ) {
        m_hciattach->detach();

        // system enabled
        // currently no way to figure out which attach produce which
        // hci
        if( m_deviceNr == -1 ) {
          m_deviceNr = 0;
          emit isEnabled( m_deviceNr, 1 );
        }
      } 
}

void OTDevice::slotStdErr(OProcess* proc, char* chars, int len) {

      if(proc == m_hciattach && len >= 1 ){
        // collect output
        QCString string( chars, len+1 ); // \0 == +1
        QString m_output;
        m_output.append( string.data() );
        odebug << m_output << oendl;
      }
}

pid_t OTDevice::getPidOfHCIAttach( void ) {

      if( needsAttach() ) {
        // not yet attached -> perhaps now ?
        // load /proc dir and check if command name contains hciattach
        QRegExp R("[0-9]+");
        QDir ProcDir( "/proc" );
        QFileInfo FI;
        QStringList EL = ProcDir.entryList( QDir::Dirs );

        // print it out
        for ( QStringList::Iterator it = EL.begin(); 
              it != EL.end(); 
              ++it ) {
          if( R.match( (*it) ) >= 0 ) {
            // is pid

            // get command being executed
            FI.setFile( ProcDir.path()+"/"+ (*it) + "/exe" );

            // get the link
            if( FI.readLink().right( 9 ) == "hciattach" ) {
              // this is hci attach process

              return (*it).toULong();
              break;
            }
          }
        }
      }

      return 0;
}

void OTDevice::detectDeviceType( QString & Device,
                                 QString & Mode,
                                 unsigned long & Speed ) {

     // detect device type and determine parms
     odebug << "Detecting device" << oendl;
     switch ( ODevice::inst()->model() ) {
      case Model_iPAQ_H39xx:
          Device = "/dev/tts/1";
          Mode = "bcsp";
          Speed = 921600;
          NeedsAttach = 1;
          break;

      case Model_iPAQ_H5xxx:
          Device = "/dev/tts/1";
          Mode = "any";
          Speed = 921600;
          NeedsAttach = 1;
          break;

      case Model_GenuineIntel :
          Device = "";
          Mode = "";
          Speed = 0;
          NeedsAttach = 0;
          break;

      default:
          Device = "/dev/ttySB0";
          Mode = "bcsp";
          Speed = 230400;
          NeedsAttach = 1;
          break;
      }
}

QString OTDevice::getRFCommDevicePattern( void ) {

      QDir D( "/dev/bluetooth/rfcomm" );
      if( D.exists() ) {
        // devfs
        return QString( "/dev/bluetooth/rfcomm/%1" );
      }

      // regular 'dev' directory
      return QString( "/dev/rfcomm%1" );
}