summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/lib/device.cc
blob: 0c552ea9da544f9e65da9bd95ba4f18fcec042c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#include "kprocess.h"

#include "device.h"

using namespace OpieTooth;

Device::Device(const QString &device, const QString &mode )
  : QObject(0, "device" ) {
  m_hci = 0;
  m_process = 0;
  m_attached = false;
  m_device = device;
  m_mode = mode;
  attach();
}
Device::~Device(){
  detach();
}
void Device::attach(){
  if(m_process != 0 ){
    m_process = new KProcess();
    *m_process << "hciattach";
    *m_process << "-p";
    *m_process << m_device << m_mode;
    connect(m_process, SIGNAL( processExited(KProcess*) ),
	    this, SLOT( slotExited(KProcess* ) ) );
    connect(m_process, SIGNAL( receivedStdout(KProcess*, char*, int) ),
	    this, SLOT(slotStdOut(KProcess*,char*,int ) ) );
    connect(m_process, SIGNAL(receivedStderr(KProcess*, char*, int ) ),
	    this, SLOT(slotStdErr(KProcess*,char*,int) ) );
    if(!m_process->start(KProcess::NotifyOnExit, KProcess::AllOutput ) ){
      delete m_process;
      m_process = 0;
    }
  };
}
void Device::detach(){
  delete m_hci;
  delete m_process;
  // kill the pid we got
  if(m_attached )
    //kill the pid
    ;
}
bool Device::isLoaded()const{
  return m_attached;
}
QString Device::devName()const {
  return QString::fromLatin1("hci0");
};
void Device::slotExited( KProcess* proc)
{
  if(proc== m_process ){
    if( m_process->normalExit() ){ // normal exit
      int ret = m_process->exitStatus();
      if( ret == 0 ){ // attached
	// now hciconfig hci0 up ( determine hciX FIXME)
	// and call hciconfig hci0 up
	// FIXME hardcoded to hci0 now :(
	m_hci = new KProcess( );
	*m_hci << "hciconfig";
	*m_hci << "hci0 up";
	connect(m_hci, SIGNAL( processExited(KProcess*) ),
		this, SLOT( slotExited(KProcess* ) ) );
      }else{
	m_attached = false;
	emit device("hci0", false );

      }
    }
    delete m_process;
    m_process = 0;
  }else if(proc== m_hci ){
    if( m_hci->normalExit() ){
      int ret = m_hci->normalExit();
      if( ret == 0 ){
	emit device("hci0", true );
      }else{
	emit device("hci0", false );
	m_attached = false;
      }
    }// normal exit
    delete m_hci;
    m_hci = 0;
  }
}
void Device::slotStdOut(KProcess* proc, char* chars, int len)
{
  if( len <1 )
    return; 
  if(proc == m_process ){
    QCString string( chars );
    if(string.left(3) != "CSR" ){ // it's the pid
      pid = string.toInt();
    };
  }
}
void Device::slotStdErr(KProcess*, char*, int )
{

}