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

manager.cc

Go to the documentation of this file.
00001 
00002 #include "parser.h"
00003 #include "manager.h"
00004 
00005 #include <opie2/oprocess.h>
00006 #include <opie2/odebug.h>
00007 using namespace Opie::Core;
00008 
00009 
00010 using namespace OpieTooth;
00011 
00012 using Opie::Core::OProcess;
00013 Manager::Manager( const QString& dev )
00014   : QObject()
00015 {
00016     odebug << "Manager: created" << oendl;
00017     m_device = dev;
00018     m_hcitool = 0;
00019     m_sdp = 0;
00020 }
00021 Manager::Manager( Device* /*dev*/ )
00022   : QObject()
00023 {
00024     m_hcitool = 0;
00025     m_sdp = 0;
00026 }
00027 Manager::Manager()
00028   : QObject()
00029 {
00030     m_hcitool = 0;
00031     m_sdp = 0;
00032 }
00033 Manager::~Manager(){
00034     delete m_hcitool;
00035     delete m_sdp;
00036 }
00037 void Manager::setDevice( const QString& dev ){
00038     m_device = dev;
00039 }
00040 void Manager::setDevice( Device* /*dev*/ ){
00041 
00042 }
00043 void Manager::isAvailable( const QString& device ){
00044     OProcess* l2ping = new OProcess();
00045     l2ping->setName( device.latin1() );
00046     *l2ping << "l2ping" << "-c1" << device;
00047     connect(l2ping, SIGNAL(processExited(Opie::Core::OProcess* ) ),
00048             this, SLOT(slotProcessExited(Opie::Core::OProcess*) ) );
00049     if (!l2ping->start() ) {
00050         emit available( device,  false );
00051         delete l2ping;
00052     }
00053 
00054 }
00055 
00056 void Manager::isAvailable( Device* /*dev*/ ){
00057 
00058 
00059 }
00060 void Manager::searchDevices( const QString& device ){
00061     odebug << "Manager: search devices" << oendl;
00062     OProcess* hcitool = new OProcess();
00063     hcitool->setName( device.isEmpty() ? "hci0" : device.latin1() );
00064     *hcitool << "hcitool" << "scan";
00065     connect( hcitool, SIGNAL(processExited(Opie::Core::OProcess*) ) ,
00066              this, SLOT(slotHCIExited(Opie::Core::OProcess* ) ) );
00067     connect( hcitool, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
00068              this, SLOT(slotHCIOut(Opie::Core::OProcess*, char*, int ) ) );
00069     if (!hcitool->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
00070         odebug << "Manager: could not start" << oendl;
00071         RemoteDevice::ValueList list;
00072         emit foundDevices( device, list );
00073         delete hcitool;
00074     }
00075 }
00076 
00077 void Manager::searchDevices(Device* /*d*/ ){
00078 
00079 
00080 }
00081 void Manager::addService(const QString& name ){
00082     OProcess proc;
00083     proc << "sdptool" << "add" << name;
00084     bool bo = true;
00085     if (!proc.start(OProcess::DontCare ) )
00086         bo = false;
00087     emit addedService( name,  bo );
00088 }
00089 void Manager::addServices(const QStringList& list){
00090     QStringList::ConstIterator it;
00091     for (it = list.begin(); it != list.end(); ++it )
00092         addService( (*it) );
00093 }
00094 void Manager::removeService( const QString& name ){
00095     OProcess prc;
00096     prc << "sdptool" << "del" << name;
00097     bool bo = true;
00098     if (!prc.start(OProcess::DontCare ) )
00099         bo = false;
00100     emit removedService( name,  bo );
00101 }
00102 void Manager::removeServices( const QStringList& list){
00103     QStringList::ConstIterator it;
00104     for (it = list.begin(); it != list.end(); ++it )
00105         removeService( (*it) );
00106 }
00107 void Manager::searchServices( const QString& remDevice ){
00108     OProcess *m_sdp =new OProcess();
00109     *m_sdp << "sdptool" << "browse" << remDevice;
00110     m_sdp->setName( remDevice.latin1() );
00111     odebug << "Manager: search Services for " << remDevice.latin1() << oendl;
00112     connect(m_sdp, SIGNAL(processExited(Opie::Core::OProcess*) ),
00113             this, SLOT(slotSDPExited(Opie::Core::OProcess* ) ) );
00114     connect(m_sdp, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*,  int ) ),
00115             this, SLOT(slotSDPOut(Opie::Core::OProcess*, char*, int) ) );
00116     if (!m_sdp->start(OProcess::NotifyOnExit,  OProcess::AllOutput) ) {
00117         odebug << "Manager: could not start sdptool" << oendl;
00118         delete m_sdp;
00119         Services::ValueList list;
00120         emit foundServices( remDevice, list );
00121     }
00122 }
00123 void Manager::searchServices( const RemoteDevice& dev){
00124     searchServices( dev.mac() );
00125 }
00126 QString Manager::toDevice( const QString& /*mac*/ ){
00127     return QString::null;
00128 }
00129 QString Manager::toMac( const QString &/*device*/ ){
00130     return QString::null;
00131 }
00132 void Manager::slotProcessExited(OProcess* proc ) {
00133     bool conn= false;
00134     if (proc->normalExit() && proc->exitStatus() == 0 )
00135         conn = true;
00136 
00137     QString name = QString::fromLatin1(proc->name() );
00138     emit available( name, conn );
00139     delete proc;
00140 }
00141 void Manager::slotSDPOut(OProcess* proc, char* ch, int len)
00142 {
00143     QCString str(ch,  len+1 );
00144     odebug << "Manager: SDP:" << str.data() << oendl;
00145     QMap<QString, QString>::Iterator it;
00146     it = m_out.find(proc->name() );
00147     QString string;
00148     if ( it != m_out.end() ) {
00149         string = it.data();
00150     }
00151     string.append( str );
00152     m_out.replace( proc->name(), string );
00153 
00154 }
00155 void Manager::slotSDPExited( OProcess* proc)
00156 {
00157     odebug << "Manager: proc name " << proc->name() << oendl;
00158     Services::ValueList  list;
00159     if (proc->normalExit()  ) {
00160         QMap<QString, QString>::Iterator it = m_out.find( proc->name() );
00161         if ( it != m_out.end() ) {
00162         odebug << "Manager: found process" << oendl;
00163             list = parseSDPOutput( it.data() );
00164             m_out.remove( it );
00165         }
00166     }
00167     emit foundServices( proc->name(), list );
00168     delete proc;
00169 }
00170 Services::ValueList Manager::parseSDPOutput( const QString& out ) {
00171     Services::ValueList list;
00172     odebug << "Manager: parsing output" << oendl;
00173     Parser parser( out );
00174     list = parser.services();
00175     return list;
00176 }
00177 
00178 void Manager::slotHCIExited(OProcess* proc ) {
00179     odebug << "Manager: process exited" << oendl;
00180     RemoteDevice::ValueList list;
00181     if (proc->normalExit() ) {
00182         odebug << "Manager: normalExit " << proc->name() << oendl;
00183         QMap<QString, QString>::Iterator it = m_devices.find(proc->name() );
00184         if (it != m_devices.end() ) {
00185             odebug << "Manager: != end ;)" << oendl;
00186             list = parseHCIOutput( it.data() );
00187             m_devices.remove( it );
00188         }
00189     }
00190     emit foundDevices( proc->name(), list );
00191     delete proc;
00192 }
00193 void Manager::slotHCIOut(OProcess* proc,  char* ch,  int len) {
00194     QCString str( ch, len+1 );
00195     odebug << "Manager: hci: " << str.data() << oendl;
00196     QMap<QString, QString>::Iterator it;
00197     it = m_devices.find( proc->name() );
00198     odebug << "Manager: proc->name " << proc->name() << oendl;
00199     QString string;
00200     if (it != m_devices.end() ) {
00201         odebug << "Manager: slotHCIOut " << oendl;
00202         string = it.data();
00203     }
00204     string.append( str );
00205 
00206     m_devices.replace( proc->name(),  string );
00207 }
00208 RemoteDevice::ValueList Manager::parseHCIOutput(const QString& output ) {
00209     odebug << "Manager: parseHCI " << output.latin1() << oendl;
00210     RemoteDevice::ValueList list;
00211     QStringList strList = QStringList::split('\n',  output );
00212     QStringList::Iterator it;
00213     QString str;
00214     for ( it = strList.begin(); it != strList.end(); ++it ) {
00215         str = (*it).stripWhiteSpace();
00216         odebug << "Manager: OpieTooth " << str.latin1() << oendl;
00217         int pos = str.findRev(':' );
00218         if ( pos > 0 ) {
00219             QString mac = str.left(17 );
00220             str.remove( 0,  17 );
00221             odebug << "Manager: mac " << mac.latin1() << oendl;
00222             odebug << "Manager: rest: " << str.latin1() << oendl;
00223             RemoteDevice rem( mac , str.stripWhiteSpace() );
00224             list.append( rem );
00225         }
00226     }
00227     return list;
00228 }
00229 
00231 
00237 void Manager::connectTo( const QString& mac) {
00238     OProcess proc;
00239     proc << "hcitool";
00240     proc << "cc";
00241     proc << mac;
00242     proc.start(OProcess::DontCare); // the lib does not care at this point
00243 }
00244 
00245 
00246 void Manager::searchConnections() {
00247     odebug << "Manager: searchConnections()" << oendl;
00248     OProcess* proc = new OProcess();
00249     m_hcitoolCon = QString::null;
00250 
00251     connect(proc, SIGNAL(processExited(Opie::Core::OProcess*) ),
00252             this, SLOT(slotConnectionExited( Opie::Core::OProcess*) ) );
00253     connect(proc, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int) ),
00254             this, SLOT(slotConnectionOutput(Opie::Core::OProcess*, char*, int) ) );
00255     *proc << "hcitool";
00256     *proc << "con";
00257 
00258     if (!proc->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
00259         ConnectionState::ValueList list;
00260         emit connections( list  );
00261         delete proc;
00262     }
00263 }
00264 void Manager::slotConnectionExited( OProcess* proc ) {
00265     ConnectionState::ValueList list;
00266     list = parseConnections( m_hcitoolCon );
00267     emit connections(list );
00268     delete proc;
00269 }
00270 void Manager::slotConnectionOutput(OProcess* /*proc*/, char* cha, int len) {
00271     QCString str(cha, len );
00272     m_hcitoolCon.append( str );
00273     //delete proc;
00274 }
00275 ConnectionState::ValueList Manager::parseConnections( const QString& out ) {
00276     ConnectionState::ValueList list2;
00277     QStringList list = QStringList::split('\n',  out );
00278     QStringList::Iterator it;
00279     // remove the first line ( "Connections:")
00280     it = list.begin();
00281     it = list.remove( it );
00282     for (; it != list.end(); ++it ) {
00283         QString row = (*it).stripWhiteSpace();
00284         QStringList value = QStringList::split(' ', row );
00285         odebug << "Manager: 0: " << value[0].latin1() << oendl;
00286         odebug << "Manager: 1: " << value[1].latin1() << oendl;
00287         odebug << "Manager: 2: " << value[2].latin1() << oendl;
00288         odebug << "Manager: 3: " << value[3].latin1() << oendl;
00289         odebug << "Manager: 4: " << value[4].latin1() << oendl;
00290         odebug << "Manager: 5: " << value[5].latin1() << oendl;
00291         odebug << "Manager: 6: " << value[6].latin1() << oendl;
00292         odebug << "Manager: 7: " << value[7].latin1() << oendl;
00293         odebug << "Manager: 8: " << value[8].latin1() << oendl;
00294         ConnectionState con;
00295         con.setDirection( value[0] == QString::fromLatin1("<") ? Outgoing : Incoming );
00296         con.setConnectionMode( value[1] );
00297         con.setMac( value[2] );
00298         con.setHandle( value[4].toInt() );
00299         con.setState( value[6].toInt() );
00300         con.setLinkMode( value[8] == QString::fromLatin1("MASTER") ? Master : Client );
00301         list2.append( con );
00302     }
00303     return list2;
00304 }
00305 
00306 void Manager::signalStrength( const QString &mac ) {
00307 
00308     OProcess* sig_proc = new OProcess();
00309 
00310     connect(sig_proc, SIGNAL(processExited(Opie::Core::OProcess*) ),
00311             this, SLOT(slotSignalStrengthExited( Opie::Core::OProcess*) ) );
00312     connect(sig_proc, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int) ),
00313             this, SLOT(slotSignalStrengthOutput(Opie::Core::OProcess*, char*, int) ) );
00314     *sig_proc << "hcitool";
00315     *sig_proc << "lq";
00316     *sig_proc << mac;
00317 
00318     sig_proc->setName( mac.latin1() );
00319 
00320     if (!sig_proc->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
00321         emit signalStrength( mac, "-1" );
00322         delete sig_proc;
00323     }
00324 }
00325 
00326 void Manager::slotSignalStrengthOutput(OProcess* proc, char* cha, int len) {
00327     QCString str(cha, len );
00328     QString temp = QString(str).stripWhiteSpace();
00329     QStringList value = QStringList::split(' ', temp );
00330     emit signalStrength( proc->name(), value[2].latin1() );
00331 }
00332 
00333 
00334 void Manager::slotSignalStrengthExited( OProcess* proc ) {
00335      delete proc;
00336 }

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