00001 #include <qapplication.h>
00002 #include <resources.h>
00003
00004 #include <OTPeer.h>
00005 #include <OTDevice.h>
00006 #include <OTGateway.h>
00007 #include <Opietooth.h>
00008
00009 #include <qlistbox.h>
00010 #include <qframe.h>
00011 #include <qlabel.h>
00012 #include <qlayout.h>
00013 #include <qdialog.h>
00014
00015 #include "bluetoothRFCOMMrun.h"
00016
00017 using Opietooth2::OTGateway;
00018 using Opietooth2::OTDevice;
00019 using Opietooth2::OTDeviceAddress;
00020 using Opietooth2::OTScan;
00021 using Opietooth2::OTPeer;
00022
00023 BluetoothRFCOMMRun::~BluetoothRFCOMMRun( void ) {
00024 if( OT ) {
00025 OTGateway::releaseOTGateway();
00026 }
00027 }
00028
00029 State_t BluetoothRFCOMMRun::detectState( void ) {
00030
00031 if( ! OT ) {
00032 OT = OTGateway::getOTGateway();
00033 }
00034
00035 if( deviceNrOfNetworkSetup() >= 0 ) {
00036 return Available;
00037 }
00038
00039 odebug << "Bluetooth "
00040 << OT->isEnabled()
00041 << oendl;
00042
00043 return ( OT->isEnabled() ) ? Off : Unavailable;
00044 }
00045
00046 QString BluetoothRFCOMMRun::setMyState( NetworkSetup *,
00047 Action_t A,
00048 bool ) {
00049
00050 if( OT ) {
00051 OTGateway::getOTGateway();
00052 }
00053
00054 if( A == Activate ) {
00055
00056 RFCOMMChannel * Ch = getChannel( );
00057 System & Sys = NSResources->system();
00058
00059 if( Ch ) {
00060
00061 DeviceNr = OT->getFreeRFCommDevice();
00062 QStringList S;
00063
00064 S << "rfcomm"
00065 << "bind"
00066 << QString().setNum( DeviceNr )
00067 << Ch->BDAddress
00068 << QString().setNum( Ch->Channel );
00069
00070
00071 delete Ch;
00072
00073 if( Sys.runAsRoot( S ) ) {
00074 return QString( "Error starting %1").arg(S.join(" "));
00075 }
00076
00077
00078 return QString();
00079 } else {
00080 Log(( "No channel selected -> cancel\n" ));
00081 return QString( "No channel selected. Operation cancelled" );
00082 }
00083 }
00084
00085 if( A == Deactivate ) {
00086 if( DeviceNr >= 0 ) {
00087 if( OT->releaseRFCommDevice( DeviceNr ) ) {
00088 return QString( "Cannot release RFCOMM NetworkSetup" );
00089 }
00090 DeviceNr = -1;
00091 }
00092 }
00093 return QString();
00094 }
00095
00096 RFCOMMChannel * BluetoothRFCOMMRun::getChannel( void ) {
00097
00098 if( Data->Devices.count() == 1 ) {
00099
00100 return Data->Devices[0];
00101 }
00102
00103 RFCOMMChannel * Ch = 0;
00104
00105 if( Data->Devices.count() == 0 ) {
00106 OTPeer * Peer;
00107 int Channel;
00108
00109 if( OTScan::getDevice( Peer, Channel, OT ) == QDialog::Accepted ) {
00110 Ch = new RFCOMMChannel;
00111 Ch->BDAddress = Peer->address().toString();
00112 Ch->Name = Peer->name();
00113 Ch->Channel = Channel;
00114 return Ch;
00115 }
00116
00117 return 0;
00118 }
00119
00120 QDialog * Dlg = new QDialog( qApp->mainWidget(), 0, TRUE );
00121 QVBoxLayout * V = new QVBoxLayout( Dlg );
00122
00123 QLabel * L = new QLabel(
00124 qApp->translate( "BluetoothRFCOMMRun",
00125 "Select device to connect to"),
00126 Dlg );
00127 QListBox * LB = new QListBox( Dlg );
00128
00129 for( unsigned int i = 0; i < Data->Devices.count(); i ++ ) {
00130 LB->insertItem( QString( "%1 (%2 Chnl %3)" ).
00131 arg( Data->Devices[i]->Name ).
00132 arg( Data->Devices[i]->BDAddress ).
00133 arg( Data->Devices[i]->Channel ) );
00134 }
00135
00136 V->addWidget( L );
00137 V->addWidget( LB );
00138
00139 Dlg->resize( 100, 100 );
00140 Dlg->move( 20,
00141 (qApp->desktop()->height()-100)/2 );
00142
00143 if( Dlg->exec() == QDialog::Accepted ) {
00144 unsigned int i = 0;
00145 for( i = 0; i < Data->Devices.count(); i ++ ) {
00146 if( LB->isSelected(i) ) {
00147 odebug << "Selected " << Data->Devices[i]->Name << oendl;
00148 Ch = new RFCOMMChannel;
00149 Ch->BDAddress = Data->Devices[i]->BDAddress;
00150 Ch->Name = Data->Devices[i]->Name;
00151 Ch->Channel = Data->Devices[i]->Channel;
00152 break;
00153 }
00154 }
00155 }
00156
00157 delete Dlg;
00158 return Ch;
00159 }
00160
00161 QString BluetoothRFCOMMRun::deviceFile( void ) {
00162 if( deviceNrOfNetworkSetup() >= 0 ) {
00163 OTDevice * OTD = OT->getOTDevice();
00164
00165 return OTD->getRFCommDevicePattern().arg(DeviceNr);
00166 }
00167 return QString();
00168 }
00169
00170 int BluetoothRFCOMMRun::deviceNrOfNetworkSetup( void ) {
00171
00172 if( ! OT ) {
00173 OT = OTGateway::getOTGateway();
00174 }
00175
00176 DeviceNr = -1;
00177 for( unsigned int i = 0; i < Data->Devices.count(); i ++ ) {
00178 odebug << "Check for rfcomm on "
00179 << Data->Devices[i]->BDAddress
00180 << " "
00181 << Data->Devices[i]->Channel
00182 << oendl;
00183 if( ( DeviceNr = OT->connectedToRFCommChannel(
00184 OTDeviceAddress( Data->Devices[i]->BDAddress ),
00185 Data->Devices[i]->Channel ) ) >= 0 ) {
00186 odebug << "Up "
00187 << oendl;
00188 break;
00189 }
00190 }
00191 return DeviceNr;
00192 }