Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

bluepingplugin.cpp

Go to the documentation of this file.
00001 #include "bluepingplugin.h"
00002 
00003 #include <opie2/oapplication.h>
00004 #include <opie2/odebug.h>
00005 #include <opie2/odevice.h>
00006 
00007 #include <qdialog.h>
00008 #include <qlayout.h>
00009 #include <qhbox.h>
00010 #include <qlabel.h>
00011 #include <qpushbutton.h>
00012 #include <qtimer.h>
00013 
00014 using namespace Opie::Core;
00015 using Opie::Security::MultiauthPluginObject;
00016 using Opie::Security::MultiauthConfigWidget;
00017 
00018 
00020 BluepingPlugin::BluepingPlugin() : MultiauthPluginObject(), m_ping(0), m_bluepingW(0), bluetoothWasOff(false) {
00021 }
00022 
00024 BluepingPlugin::~BluepingPlugin() {
00025     odebug << "closing Blueping plugin..." << oendl;
00026     if (m_ping != 0)
00027         delete m_ping;
00028     if (m_bluepingW != 0)
00029         delete m_bluepingW;
00030     killBluetoothIfNecessary();
00031 }
00032 
00034 QString BluepingPlugin::pluginName() const {
00035     return "Blueping plugin";
00036 }
00037 
00039 MultiauthConfigWidget * BluepingPlugin::configWidget(QWidget * parent) {
00040     if (m_bluepingW == 0)
00041         m_bluepingW = new BluepingConfigWidget(parent, "Blueping configuration widget");
00042     return m_bluepingW;
00043 }
00044 
00045 QString BluepingPlugin::pixmapNameWidget() const {
00046     return "security/bluepingplugin";
00047 }
00048 
00049 QString BluepingPlugin::pixmapNameConfig() const {
00050     return "security/bluepingplugin";
00051 }
00052 
00054 void BluepingPlugin::killBluetoothIfNecessary() {
00055     if (bluetoothWasOff) {
00056         OProcess killB;
00057         killB << "killall" << "hciattach";
00058         odebug << "killing Bluetooth... (since it was up only for Blueping)" << oendl;
00059         if ( !killB.start(OProcess::Block) ) {
00060             oerr << "could not kill bluetooth" << oendl;
00061         }
00062     } else {
00063         odebug << "keeping Bluetooth on" << oendl;
00064     }
00065 }
00066 
00068 void BluepingPlugin::success() {
00069     emit emitCode(MultiauthPluginObject::Success);
00070 }
00071 
00073 void BluepingPlugin::failure() {
00074     emit emitCode(MultiauthPluginObject::Failure);
00075 }
00076 
00078 void BluepingPlugin::skip() {
00079     emit emitCode(MultiauthPluginObject::Skip);
00080 }
00081 
00083 void BluepingPlugin::ping() {
00084     m_ping = new OProcess();
00085     odebug << "pinging device: " << macToPing << oendl;
00086     *m_ping << "l2ping" << "-c 1" << macToPing;
00087 
00088     // starting to ping in the background
00093     if ( !m_ping->start() ) {
00094         oerr << "could not start l2ping" << oendl;
00095         this->skip();
00096     }
00097     QObject::connect(m_ping, SIGNAL(processExited(Opie::Core::OProcess*)),
00098                      this, SLOT(pingFinished(Opie::Core::OProcess*)) );
00099 }
00100 
00102 void BluepingPlugin::pingFinished(OProcess * ping) {
00103     if ( ping->normalExit() && (ping->exitStatus() == 0) )
00104     {
00105         odebug << "Successful Bluetooth ping!" << oendl;
00106         success();
00107     }
00108     else
00109     {
00110         odebug << "Failed Bluetooth ping... (normalExit: " << ping->normalExit() << ", exitStatus: " << ping->exitStatus() << ")" << oendl;
00111         failure();
00112     }
00113 }
00114 
00116 
00120 int BluepingPlugin::authenticate() {
00121 
00122     Config cfg("Security");
00123     cfg.setGroup("BluepingPlugin");
00124     macToPing = cfg.readEntry("mac");
00125     if (!macToPing.isEmpty())
00126     {
00127         /* Standard, inescapable authentication dialog
00128         */
00129         QDialog bluepingDialog(0,
00130                                "Blueping dialog",
00131                                TRUE,
00132                                Qt::WStyle_NoBorder | Qt::WStyle_Customize | Qt::WStyle_StaysOnTop);
00133 
00134         QRect desk = oApp->desktop()->geometry();
00135         bluepingDialog.setGeometry( 0, 0, desk.width(), desk.height() );
00136 
00137         // Creation of the particular widgets of our Blueping user interface
00138         QVBoxLayout *layout = new QVBoxLayout(&bluepingDialog);
00139         layout->setSpacing(11);
00140         layout->setMargin(11);
00141         layout->setAlignment( Qt::AlignTop );
00142 
00143         QLabel title("<center><h1>\"Blueping\" <br />plugin</h1></center>", &bluepingDialog);
00144         QLabel subTitle("<center><h2>Trying to reach your configured bluetooth device...</h2></center>", &bluepingDialog);
00145         QLabel subTitle2("<center>You can skip this step and use another authentication way with the following button</center>", &bluepingDialog);
00146         QPushButton pbSkip("Skip", &bluepingDialog);
00147         layout->addWidget(&title);
00148         layout->addWidget(&subTitle);
00149         layout->addWidget(&subTitle2);
00150         layout->addWidget(&pbSkip, 0, Qt::AlignHCenter);
00151 
00152         // connect the skip button to the skip signal emitting function
00153         QObject::connect(&pbSkip, SIGNAL(clicked()), this, SLOT(skip()));
00154         // connect the signal emitting functions to the bluepingDialog done(int) finishing function
00155         QObject::connect(this, SIGNAL(emitCode(int)), &bluepingDialog, SLOT(done(int)));
00156 
00157 
00158         
00159         /* let's start Bluetooth if it's not running
00160          */
00161         OProcess checkB;
00162         checkB << "pidof" << "hciattach";
00163         odebug << "checking if Bluetooth is running..." << oendl;
00164         // now we start bluetooth *only* if the previous command works, exits normally, and
00165         // it returns a non-null exit code (which means hciattach is not running)
00166         if ( checkB.start(OProcess::Block) && checkB.normalExit() && (checkB.exitStatus() != 0) )
00167         {
00168             // remember to switch off Bluetooth once we're finished...
00169             bluetoothWasOff = true;
00170             odebug << "Bluetooth is not running, we must start it now" << oendl;
00171 
00172             OProcess startB;
00173             switch ( ODevice::inst()->model() ) {
00174                 case Model_iPAQ_H39xx:
00175                     startB << "/sbin/hciattach" << "/dev/tts/1" << "bcsp" << "921600";
00176                     break;
00177 
00178                 case Model_iPAQ_H5xxx:
00179                     startB << "/sbin/hciattach" << "/dev/tts/1" << "any" << "921600";
00180                     break;
00181 
00182                 default:
00183                     startB << "/sbin/hciattach" << "/dev/ttySB0" << "bcsp" << "230400";
00184                     break;
00185             } // end switch on device models
00186 
00187             if ( !startB.start(OProcess::Block) ) {
00188                 oerr << "could not start Bluetooth" << oendl;
00189                 return MultiauthPluginObject::Skip;
00190             }
00191             else
00192             {
00193                 if ( (startB.normalExit()) && (startB.exitStatus() == 0) )
00194                 {
00195                     odebug << "hciattach exited normally, Bluetooth is probably on now, let's wait 500 ms and ping" << oendl;
00196                     // 500 ms timer, so l2ping won't try to find a route before bluetooth has \em really started
00197                     QTimer::singleShot( 500, this, SLOT(ping()) );
00198                 }
00199                 else
00200                 {
00201                     owarn << "hciattach exited anormally (normalExit: " << startB.normalExit() << ", exit status: " << startB.exitStatus() << ")" << oendl;
00202                     return MultiauthPluginObject::Skip;
00203                 } // end if startB exited normaly
00204             } // end if startBluetooth started
00205         }
00206         else
00207         {
00208             // we don't need to wait, since bluetooth has been started long enough ago
00209             odebug << "Bluetooth is already running, we can try to ping now" << oendl;
00210             ping();
00211         } // end if Bluetooth was off
00212 
00213 
00214         // start the dialog event loop, while the ping is starting (or will start soon) in the background
00215         return bluepingDialog.exec();
00216 
00217 
00218     }
00219     else
00220     {
00221         owarn << "No Bluetooth device has been set!" << oendl;
00222         owarn << "We will consider it as a successful authentication though." << oendl;
00223         return MultiauthPluginObject::Success;
00224     } // end if mac defined
00225 }

Generated on Sat Nov 5 16:17:45 2005 for OPIE by  doxygen 1.4.2