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

device.cc

Go to the documentation of this file.
00001 
00002 #include "device.h"
00003 
00004 /* OPIE */
00005 #include <opie2/oprocess.h>
00006 #include <opie2/odebug.h>
00007 #include <opie2/odevice.h>
00008 
00009 using namespace Opie::Core;
00010 
00011 /* STD */
00012 #include <signal.h>
00013 
00014 
00015 using namespace OpieTooth;
00016 
00017 using Opie::Core::OProcess;
00018 namespace {
00019     int parsePid( const QCString& par )
00020     {
00021         int id=0;
00022         QString string( par );
00023         QStringList list =  QStringList::split( '\n', string );
00024 
00025         for( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
00026         {
00027             owarn << "parsePID: " << (*it).latin1() << oendl;
00028 
00029             // FIXME mbhaynie:  Surely there is a better way to skip
00030             // verbosity (E.g. the TI device configuration
00031             // script). Apparently the PID is always on a line by 
00032             // itself, or is at least the first word of a line.  Does
00033             // QString have somethine like startsWithRegex("[0-9]+")?
00034             if( (*it).startsWith("#") ) continue;
00035             if( (*it).startsWith("TI") ) continue;
00036             if( (*it).startsWith("Loading") ) continue;
00037             if( (*it).startsWith("BTS") ) continue;
00038             if( !(*it).startsWith("CSR") )
00039             {
00040                 id = (*it).toInt();
00041                 break;
00042             }
00043         }
00044         return id;
00045     }
00046 }
00047 
00048 Device::Device(const QString &device, const QString &mode, const QString &speed )
00049   : QObject(0, "device") {
00050 
00051   owarn << "OpieTooth::Device create" << oendl;
00052   m_hci = 0;
00053   m_process = 0;
00054   m_attached = false;
00055   m_device = device;
00056   m_mode = mode;
00057   m_speed = speed;
00058   attach();
00059 }
00060 Device::~Device(){
00061   detach();
00062 }
00063 
00064 // FIXME mbhaynie -- If BT is active, and opie is restarted, this
00065 // applet thinks bt is down, and will fail to start it again.  Not
00066 // sure why. 
00067 void Device::attach(){
00068   owarn << "attaching " << m_device.latin1() << " " << m_mode.latin1() << " " << m_speed.latin1() << oendl;
00069   if(m_process == 0 ){
00070     m_output.resize(0);
00071     owarn << "new process to create" << oendl;
00072     m_process = new OProcess();
00073     *m_process << "hciattach";
00074     *m_process << "-p";
00075 
00076     // FIXME -- this is a hack for an odd hciattach interface.
00077     if ( ODevice::inst()->modelString() == "HX4700" )
00078     {
00079         *m_process << "-S" << "/etc/bluetooth/TIInit_3.2.26.bts" << "/dev/ttyS1" << "texas";
00080     }
00081     else
00082     {
00083         *m_process << m_device << m_mode << m_speed;
00084     }
00085     connect(m_process, SIGNAL( processExited(Opie::Core::OProcess*) ),
00086             this, SLOT( slotExited(Opie::Core::OProcess* ) ) );
00087     connect(m_process, SIGNAL( receivedStdout(Opie::Core::OProcess*, char*, int) ),
00088             this, SLOT(slotStdOut(Opie::Core::OProcess*,char*,int ) ) );
00089     connect(m_process, SIGNAL(receivedStderr(Opie::Core::OProcess*, char*, int ) ),
00090             this, SLOT(slotStdErr(Opie::Core::OProcess*,char*,int) ) );
00091     if(!m_process->start(OProcess::NotifyOnExit, OProcess::AllOutput ) ){
00092       owarn << "Could not start" << oendl;
00093       delete m_process;
00094       m_process = 0;
00095     }
00096   };
00097 }
00098 void Device::detach(){
00099   delete m_hci;
00100   delete m_process;
00101   // kill the pid we got
00102   if(m_attached ){
00103     //kill the pid
00104     owarn << "killing" << oendl;
00105     kill(pid, 9);
00106   }
00107   owarn << "detached" << oendl;
00108 }
00109 bool Device::isLoaded()const{
00110   return m_attached;
00111 }
00112 QString Device::devName()const {
00113   return QString::fromLatin1("hci0");
00114 };
00115 void Device::slotExited( OProcess* proc)
00116 {
00117   owarn << "prcess exited" << oendl;
00118   if(proc== m_process ){
00119     owarn << "proc == m_process" << oendl;
00120     if( m_process->normalExit() ){ // normal exit
00121       owarn << "normalExit" << oendl;
00122       int ret = m_process->exitStatus();
00123       if( ret == 0 ){ // attached
00124     owarn << "attached" << oendl;
00125     owarn << "Output: " << m_output.data() << oendl;
00126     pid = parsePid( m_output );
00127     owarn << "Pid = " << pid << oendl;
00128     // now hciconfig hci0 up ( determine hciX FIXME)
00129     // and call hciconfig hci0 up
00130     // FIXME hardcoded to hci0 now :(
00131     m_hci = new OProcess( );
00132     *m_hci << "hciconfig";
00133     *m_hci << "hci0 up";
00134     connect(m_hci, SIGNAL( processExited(Opie::Core::OProcess*) ),
00135                 this, SLOT( slotExited(Opie::Core::OProcess* ) ) );
00136     if(!m_hci->start() ){
00137       owarn << "could not start" << oendl;
00138       m_attached = false;
00139       emit device("hci0", false );
00140     }
00141       }else{
00142         owarn << "crass" << oendl;
00143     m_attached = false;
00144     emit device("hci0", false );
00145 
00146       }
00147     }
00148     delete m_process;
00149     m_process = 0;
00150   }else if(proc== m_hci ){
00151     owarn << "M HCI exited" << oendl;
00152     if( m_hci->normalExit() ){
00153       owarn << "normal exit" << oendl;
00154       int ret = m_hci->exitStatus();
00155       if( ret == 0 ){
00156     owarn << "attached really really attached" << oendl;
00157     m_attached = true;
00158     emit device("hci0", true );
00159       }else{
00160     owarn << "failed" << oendl;
00161     emit device("hci0", false );
00162     m_attached = false;
00163       }
00164     }// normal exit
00165     delete m_hci;
00166     m_hci = 0;
00167   }
00168 }
00169 void Device::slotStdOut(OProcess* proc, char* chars, int len)
00170 {
00171   owarn << "std out" << oendl;
00172   if( len <1 ){
00173     owarn << "len < 1 " << oendl;
00174     return;
00175   }
00176   if(proc == m_process ){
00177     QCString string( chars, len+1 ); // \0 == +1
00178     owarn << "output: " << string.data() << oendl;
00179     m_output.append( string.data() );
00180   }
00181 }
00182 void Device::slotStdErr(OProcess* proc, char* chars, int len)
00183 {
00184   owarn << "std err" << oendl;
00185   slotStdOut( proc, chars, len );
00186 }

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