Diffstat (limited to 'noncore/settings/networksettings2/bluetooth/bluetoothRFCOMMrun.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/bluetooth/bluetoothRFCOMMrun.cpp | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/bluetooth/bluetoothRFCOMMrun.cpp b/noncore/settings/networksettings2/bluetooth/bluetoothRFCOMMrun.cpp new file mode 100644 index 0000000..1e91ed1 --- a/dev/null +++ b/noncore/settings/networksettings2/bluetooth/bluetoothRFCOMMrun.cpp | |||
@@ -0,0 +1,162 @@ | |||
1 | #include <qapplication.h> | ||
2 | #include <resources.h> | ||
3 | #include <OTDevice.h> | ||
4 | #include <OTGateway.h> | ||
5 | #include "bluetoothRFCOMMrun.h" | ||
6 | |||
7 | using namespace Opietooth2; | ||
8 | |||
9 | BluetoothRFCOMMRun::~BluetoothRFCOMMRun( void ) { | ||
10 | if( OT ) { | ||
11 | OTGateway::releaseOTGateway(); | ||
12 | } | ||
13 | } | ||
14 | |||
15 | State_t BluetoothRFCOMMRun::detectState( void ) { | ||
16 | |||
17 | if( ! OT ) { | ||
18 | OT = OTGateway::getOTGateway(); | ||
19 | } | ||
20 | |||
21 | if( deviceNrOfConnection() >= 0 ) { | ||
22 | return Available; | ||
23 | } | ||
24 | |||
25 | owarn << "Bluetooth " | ||
26 | << OT->isEnabled() | ||
27 | << oendl; | ||
28 | |||
29 | return ( OT->isEnabled() ) ? Off : Unavailable; | ||
30 | } | ||
31 | |||
32 | QString BluetoothRFCOMMRun::setMyState( NodeCollection *, | ||
33 | Action_t A, | ||
34 | bool ) { | ||
35 | |||
36 | if( OT ) { | ||
37 | OTGateway::getOTGateway(); | ||
38 | } | ||
39 | |||
40 | if( A == Activate ) { | ||
41 | // from OFF to Available | ||
42 | RFCOMMChannel * Ch = getChannel( ); | ||
43 | System & Sys = NSResources->system(); | ||
44 | |||
45 | if( Ch ) { | ||
46 | // connect to this peer | ||
47 | DeviceNr = OT->getFreeRFCommDevice(); | ||
48 | QStringList S; | ||
49 | |||
50 | S << "rfcomm" | ||
51 | << "bind" | ||
52 | << QString().setNum( DeviceNr ) | ||
53 | << Ch->BDAddress | ||
54 | << QString().setNum( Ch->Channel ); | ||
55 | |||
56 | if( Sys.runAsRoot( S ) ) { | ||
57 | return QString( "Error starting %1").arg(S.join(" ")); | ||
58 | } | ||
59 | |||
60 | // here rfcomm should be running -> we will detect state later | ||
61 | return QString(); | ||
62 | } else { | ||
63 | Log(( "No channel selected -> cancel\n" )); | ||
64 | return QString( "No channel selected. Operation cancelled" ); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | if( A == Deactivate ) { | ||
69 | if( DeviceNr >= 0 ) { | ||
70 | if( OT->releaseRFCommDevice( DeviceNr ) ) { | ||
71 | return QString( "Cannot release RFCOMM connection" ); | ||
72 | } | ||
73 | DeviceNr = -1; | ||
74 | } | ||
75 | } | ||
76 | return QString(); | ||
77 | } | ||
78 | |||
79 | #include <qlistbox.h> | ||
80 | #include <qframe.h> | ||
81 | #include <qlabel.h> | ||
82 | #include <qlayout.h> | ||
83 | #include <qdialog.h> | ||
84 | |||
85 | RFCOMMChannel * BluetoothRFCOMMRun::getChannel( void ) { | ||
86 | |||
87 | if( Data->Devices.count() == 1 ) { | ||
88 | // only one device -> return channel | ||
89 | return Data->Devices[0]; | ||
90 | } | ||
91 | |||
92 | RFCOMMChannel * Ch = 0; | ||
93 | QDialog * Dlg = new QDialog( qApp->mainWidget(), 0, TRUE ); | ||
94 | QVBoxLayout * V = new QVBoxLayout( Dlg ); | ||
95 | |||
96 | QLabel * L = new QLabel( | ||
97 | qApp->translate( "BluetoothRFCOMMRun", | ||
98 | "Select device to connect to"), | ||
99 | Dlg ); | ||
100 | QListBox * LB = new QListBox( Dlg ); | ||
101 | |||
102 | for( unsigned int i = 0; i < Data->Devices.count(); i ++ ) { | ||
103 | LB->insertItem( QString( "%1 (%2 Chnl %3)" ). | ||
104 | arg( Data->Devices[i]->Name ). | ||
105 | arg( Data->Devices[i]->BDAddress ). | ||
106 | arg( Data->Devices[i]->Channel ) ); | ||
107 | } | ||
108 | |||
109 | V->addWidget( L ); | ||
110 | V->addWidget( LB ); | ||
111 | |||
112 | Dlg->resize( 100, 100 ); | ||
113 | Dlg->move( 20, | ||
114 | (qApp->desktop()->height()-100)/2 ); | ||
115 | |||
116 | if( Dlg->exec() == QDialog::Accepted ) { | ||
117 | unsigned int i = 0; | ||
118 | for( i = 0; i < Data->Devices.count(); i ++ ) { | ||
119 | if( LB->isSelected(i) ) { | ||
120 | owarn << "Selected " << Data->Devices[i]->Name << oendl; | ||
121 | Ch = Data->Devices[i]; | ||
122 | break; | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | |||
127 | delete Dlg; | ||
128 | return Ch; | ||
129 | } | ||
130 | |||
131 | QString BluetoothRFCOMMRun::deviceFile( void ) { | ||
132 | if( deviceNrOfConnection() >= 0 ) { | ||
133 | OTDevice * OTD = OT->getOTDevice(); | ||
134 | // there is a connection | ||
135 | return OTD->getRFCommDevicePattern().arg(DeviceNr); | ||
136 | } | ||
137 | return QString(); | ||
138 | } | ||
139 | |||
140 | int BluetoothRFCOMMRun::deviceNrOfConnection( void ) { | ||
141 | |||
142 | if( ! OT ) { | ||
143 | OT = OTGateway::getOTGateway(); | ||
144 | } | ||
145 | |||
146 | DeviceNr = -1; | ||
147 | for( unsigned int i = 0; i < Data->Devices.count(); i ++ ) { | ||
148 | owarn << "Check for rfcomm on " | ||
149 | << Data->Devices[i]->BDAddress | ||
150 | << " " | ||
151 | << Data->Devices[i]->Channel | ||
152 | << oendl; | ||
153 | if( ( DeviceNr = OT->connectedToRFCommChannel( | ||
154 | OTDeviceAddress( Data->Devices[i]->BDAddress ), | ||
155 | Data->Devices[i]->Channel ) ) >= 0 ) { | ||
156 | owarn << "Up " | ||
157 | << oendl; | ||
158 | break; | ||
159 | } | ||
160 | } | ||
161 | return DeviceNr; | ||
162 | } | ||