summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/bluetooth/bluetoothRFCOMMrun.cpp
blob: 1e91ed19f4331fe5dea14c302606e4c9bd677ad0 (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
#include <qapplication.h>
#include <resources.h>
#include <OTDevice.h>
#include <OTGateway.h>
#include "bluetoothRFCOMMrun.h"

using namespace Opietooth2;

BluetoothRFCOMMRun::~BluetoothRFCOMMRun( void ) {
      if( OT ) {
        OTGateway::releaseOTGateway();
      }
}

State_t BluetoothRFCOMMRun::detectState( void ) {

      if( ! OT ) {
        OT = OTGateway::getOTGateway();
      }

      if( deviceNrOfConnection() >= 0 ) {
        return Available;
      }

      owarn << "Bluetooth " 
            << OT->isEnabled()
            << oendl;

      return ( OT->isEnabled() ) ? Off : Unavailable;
}

QString BluetoothRFCOMMRun::setMyState( NodeCollection *, 
                                      Action_t A, 
                                      bool ) {

      if( OT ) {
        OTGateway::getOTGateway();
      }

      if( A == Activate ) {
        // from OFF to Available
        RFCOMMChannel * Ch = getChannel( );
        System & Sys = NSResources->system();
 
        if( Ch ) {
          // connect to this peer
          DeviceNr = OT->getFreeRFCommDevice();
          QStringList S;

          S << "rfcomm" 
            << "bind"
            << QString().setNum( DeviceNr )
            << Ch->BDAddress
            << QString().setNum( Ch->Channel );
 
          if( Sys.runAsRoot( S ) ) {
            return QString( "Error starting %1").arg(S.join(" "));
          }

          // here rfcomm should be running -> we will detect state later
          return QString();
        } else {
          Log(( "No channel selected -> cancel\n" ));
          return QString( "No channel selected. Operation cancelled" );
        }
      }

      if( A == Deactivate ) {
        if( DeviceNr >= 0 ) {
          if( OT->releaseRFCommDevice( DeviceNr ) ) {
           return QString( "Cannot release RFCOMM connection" );
          }
          DeviceNr = -1;
        }
      }
      return QString();
}

#include <qlistbox.h>
#include <qframe.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qdialog.h>

RFCOMMChannel * BluetoothRFCOMMRun::getChannel( void ) {

    if( Data->Devices.count() == 1 ) {
      // only one device -> return channel
      return Data->Devices[0];
    }

    RFCOMMChannel * Ch = 0;
    QDialog * Dlg = new QDialog( qApp->mainWidget(), 0, TRUE );
    QVBoxLayout * V = new QVBoxLayout( Dlg );

    QLabel * L = new QLabel( 
      qApp->translate( "BluetoothRFCOMMRun", 
                       "Select device to connect to"),
                       Dlg );
    QListBox * LB = new QListBox( Dlg );

    for( unsigned int i = 0; i < Data->Devices.count(); i ++ ) {
      LB->insertItem( QString( "%1 (%2 Chnl %3)" ).
                        arg( Data->Devices[i]->Name ).
                        arg( Data->Devices[i]->BDAddress ).
                        arg( Data->Devices[i]->Channel ) );
    }

    V->addWidget( L );
    V->addWidget( LB );

    Dlg->resize( 100, 100 );
    Dlg->move( 20, 
               (qApp->desktop()->height()-100)/2 );

    if( Dlg->exec() == QDialog::Accepted ) {
      unsigned int i = 0;
      for( i = 0; i < Data->Devices.count(); i ++ ) {
        if( LB->isSelected(i) ) {
          owarn << "Selected " << Data->Devices[i]->Name << oendl;
          Ch = Data->Devices[i];
          break;
        }
      }
    }

    delete Dlg;
    return Ch;
}

QString BluetoothRFCOMMRun::deviceFile( void ) {
    if( deviceNrOfConnection() >= 0 ) {
      OTDevice * OTD = OT->getOTDevice();
      // there is a connection
      return OTD->getRFCommDevicePattern().arg(DeviceNr);
    }
    return QString();
}

int BluetoothRFCOMMRun::deviceNrOfConnection( void ) {

    if( ! OT ) {
      OT = OTGateway::getOTGateway();
    }

    DeviceNr = -1;
    for( unsigned int i = 0; i < Data->Devices.count(); i ++ ) {
      owarn << "Check for rfcomm on " 
            << Data->Devices[i]->BDAddress
            << " "
            << Data->Devices[i]->Channel
            << oendl;
      if( ( DeviceNr = OT->connectedToRFCommChannel( 
              OTDeviceAddress( Data->Devices[i]->BDAddress ),
              Data->Devices[i]->Channel ) ) >= 0 ) {
        owarn << "Up " 
              << oendl;
        break;
      }
    }
    return DeviceNr;
}