summaryrefslogtreecommitdiff
path: root/core/obex/btobex.cpp
Unidiff
Diffstat (limited to 'core/obex/btobex.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/obex/btobex.cpp85
1 files changed, 71 insertions, 14 deletions
diff --git a/core/obex/btobex.cpp b/core/obex/btobex.cpp
index a2866f6..bb5c06d 100644
--- a/core/obex/btobex.cpp
+++ b/core/obex/btobex.cpp
@@ -1,185 +1,242 @@
1 1
2#include "btobex.h" 2#include "btobex.h"
3#include <manager.h>
4#include <services.h>
3 5
4/* OPIE */ 6/* OPIE */
5#include <opie2/oprocess.h> 7#include <opie2/oprocess.h>
6#include <opie2/odebug.h> 8#include <opie2/odebug.h>
7 9
8/* QT */ 10/* QT */
9#include <qfileinfo.h> 11#include <qfileinfo.h>
10 12#include <qstring.h>
11 13#include <qmap.h>
14#include <qmessagebox.h>
12 15
13using namespace OpieObex; 16using namespace OpieObex;
14 17
15using namespace Opie::Core; 18using namespace Opie::Core;
16/* TRANSLATOR OpieObex::Obex */ 19/* TRANSLATOR OpieObex::Obex */
20using namespace OpieTooth;
17 21
18BtObex::BtObex( QObject *parent, const char* name ) 22BtObex::BtObex( QObject *parent, const char* name )
19 : QObject(parent, name ) 23 : QObject(parent, name )
20{ 24{
21 m_rec = 0; 25 m_rec = 0;
22 m_send=0; 26 m_send=0;
23 m_count = 0; 27 m_count = 0;
24 m_receive = false; 28 m_receive = false;
25 connect( this, SIGNAL(error(int) ), // for recovering to receive 29 connect( this, SIGNAL(error(int) ), // for recovering to receive
26 SLOT(slotError() ) ); 30 SLOT(slotError() ) );
27 connect( this, SIGNAL(sent(bool) ), 31 connect( this, SIGNAL(sent(bool) ),
28 SLOT(slotError() ) ); 32 SLOT(slotError() ) );
33 btManager = NULL;
29}; 34};
35
30BtObex::~BtObex() { 36BtObex::~BtObex() {
37 if (btManager)
38 delete btManager;
31 delete m_rec; 39 delete m_rec;
32 delete m_send; 40 delete m_send;
33} 41}
42
34void BtObex::receive() { 43void BtObex::receive() {
35 m_receive = true; 44 m_receive = true;
36 m_outp = QString::null; 45 m_outp = QString::null;
37 m_rec = new OProcess(); 46 m_rec = new OProcess();
38 47
39 // TODO mbhaynie: No idea if this actually works -- maybe opd is better. 48 // TODO mbhaynie: No idea if this actually works -- maybe opd is better.
40 *m_rec << "obexftpd" << "-b"; 49 *m_rec << "obexftpd" << "-b";
41 // connect to the necessary slots 50 // connect to the necessary slots
42 connect(m_rec, SIGNAL(processExited(Opie::Core::OProcess*) ), 51 connect(m_rec, SIGNAL(processExited(Opie::Core::OProcess*) ),
43 this, SLOT(slotExited(Opie::Core::OProcess*) ) ); 52 this, SLOT(slotExited(Opie::Core::OProcess*) ) );
44 53
45 connect(m_rec, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ), 54 connect(m_rec, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int ) ),
46 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) ); 55 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
47 56
48 if(!m_rec->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) { 57 if(!m_rec->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
49 emit done( false ); 58 emit done( false );
50 delete m_rec; 59 delete m_rec;
51 m_rec = 0; 60 m_rec = 0;
52 } 61 }
53} 62}
54 63
55void BtObex::send( const QString& fileName, const QString& bdaddr) { 64void BtObex::send( const QString& fileName, const QString& bdaddr) {
56 // if currently receiving stop it send receive 65 // if currently receiving stop it send receive
57 m_count = 0; 66 m_count = 0;
58 m_file = fileName; 67 m_file = fileName;
59 m_bdaddr = bdaddr; 68 m_bdaddr = bdaddr;
60 if (m_rec != 0 ) { 69 if (m_rec != 0 ) {
61 if (m_rec->isRunning() ) { 70 if (m_rec->isRunning() ) {
62 emit error(-1 ); 71 emit error(-1 );
63 delete m_rec; 72 delete m_rec;
64 m_rec = 0; 73 m_rec = 0;
65 74
66 }else{ 75 }else{
67 emit error( -1 ); // we did not delete yet but it's not running slotExited is pending 76 emit error( -1 ); // we did not delete yet but it's not running slotExited is pending
68 return; 77 return;
69 } 78 }
70 } 79 }
80 //Now we need to find out if the OBEX push is supported for this device
81 //And get the port number
82 if (!btManager) {
83 btManager = new Manager("hci0");
84 connect(btManager,
85 SIGNAL(foundServices(const QString&, Services::ValueList)),
86 this, SLOT(slotFoundServices(const QString&, Services::ValueList)));
87 }
88 btManager->searchServices(bdaddr);
89}
90
91/**
92 * This function reacts on the service discovery finish
93 */
94void BtObex::slotFoundServices(const QString&, Services::ValueList svcList)
95{
96 QValueList<OpieTooth::Services>::Iterator it;
97 QMap<int, QString> classList; //The classes list
98 QMap<int, QString>::Iterator classIt; //Iterator in the class list
99 int portNum = -1; //The desired port number
100 odebug << "BtObex slotFoundServices" << oendl;
101 if (svcList.isEmpty()) {
102 QMessageBox::critical(NULL, tr("Object send"), tr("No services found"));
103 emit error(-1);
104 return;
105 }
106 for (it = svcList.begin(); it != svcList.end(); it++) {
107 classList = (*it).classIdList();
108 classIt = classList.begin();
109 if (classIt == classList.end())
110 continue;
111////We really need symbolic names for service IDs
112 //Ok, we have found the object push service
113 if (classIt.key() == 4357) {
114 portNum = (*it).protocolDescriptorList().last().port();
115 break;
116 }
117 }
118 if (portNum == -1) {
119 QMessageBox::critical(NULL, tr("Object send"),
120 tr("No OBEX Push service"));
121 emit error(-1);
122 return;
123 }
124 m_port = portNum;
71 sendNow(); 125 sendNow();
72} 126}
127
73void BtObex::sendNow(){ 128void BtObex::sendNow(){
74 if ( m_count >= 25 ) { // could not send 129 if ( m_count >= 25 ) { // could not send
75 emit error(-1 ); 130 emit error(-1 );
76 emit sent(false); 131 emit sent(false);
77 return; 132 return;
78 } 133 }
79 // OProcess inititialisation 134 // OProcess inititialisation
80 m_send = new OProcess(); 135 m_send = new OProcess(0, "ussp-push");
81 m_send->setWorkingDirectory( QFileInfo(m_file).dirPath(true) ); 136 m_send->setWorkingDirectory( QFileInfo(m_file).dirPath(true) );
82 137
83 // obextool push file <bdaddr> [channel] 138 // ussp-push --timeo 30 <btaddr:port> file file
84 // 9 for phones. 139 *m_send << "ussp-push" << "--timeo 30";
85 // Palm T3 accepts pictures on 1 140 *m_send << m_bdaddr + "@" + QString::number(m_port);
86 *m_send << "obextool" << "push";
87 *m_send << QFile::encodeName(QFileInfo(m_file).fileName()); 141 *m_send << QFile::encodeName(QFileInfo(m_file).fileName());
88 *m_send << m_bdaddr << "9"; 142 *m_send << QFile::encodeName(QFileInfo(m_file).fileName());
89 143 m_send->setUseShell(true);
144
90 // connect to slots Exited and and StdOut 145 // connect to slots Exited and and StdOut
91 connect(m_send, SIGNAL(processExited(Opie::Core::OProcess*) ), 146 connect(m_send, SIGNAL(processExited(Opie::Core::OProcess*) ),
92 this, SLOT(slotExited(Opie::Core::OProcess*)) ); 147 this, SLOT(slotExited(Opie::Core::OProcess*)) );
93 connect(m_send, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int )), 148 connect(m_send, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int)),
94 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) ); 149 this, SLOT(slotStdOut(Opie::Core::OProcess*, char*, int) ) );
95
96 // now start it 150 // now start it
97 if (!m_send->start(/*OProcess::NotifyOnExit, OProcess::AllOutput*/ ) ) { 151 if (!m_send->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
98 m_count = 25; 152 m_count = 25;
99 emit error(-1 ); 153 emit error(-1 );
100 delete m_send; 154 delete m_send;
101 m_send=0; 155 m_send=0;
102 } 156 }
103 // end 157 // end
104 m_count++; 158 m_count++;
105 emit currentTry( m_count ); 159 emit currentTry( m_count );
106} 160}
107 161
108void BtObex::slotExited(OProcess* proc ){ 162void BtObex::slotExited(OProcess* proc ){
163 odebug << proc->name() << " exited with result "
164 << proc->exitStatus() << oendl;
109 if (proc == m_rec ) // receive process 165 if (proc == m_rec ) // receive process
110 received(); 166 received();
111 else if ( proc == m_send ) 167 else if ( proc == m_send )
112 sendEnd(); 168 sendEnd();
113 169
114} 170}
115void BtObex::slotStdOut(OProcess* proc, char* buf, int len){ 171void BtObex::slotStdOut(OProcess* proc, char* buf, int len){
116 if ( proc == m_rec ) { // only receive 172 if ( proc == m_rec ) { // only receive
117 QByteArray ar( len ); 173 QByteArray ar( len );
118 memcpy( ar.data(), buf, len ); 174 memcpy( ar.data(), buf, len );
119 m_outp.append( ar ); 175 m_outp.append( ar );
120 } 176 }
121} 177}
122 178
123void BtObex::received() { 179void BtObex::received() {
124 if (m_rec->normalExit() ) { 180 if (m_rec->normalExit() ) {
125 if ( m_rec->exitStatus() == 0 ) { // we got one 181 if ( m_rec->exitStatus() == 0 ) { // we got one
126 QString filename = parseOut(); 182 QString filename = parseOut();
127 emit receivedFile( filename ); 183 emit receivedFile( filename );
128 } 184 }
129 }else{ 185 }else{
130 emit done(false); 186 emit done(false);
131 }; 187 };
132 delete m_rec; 188 delete m_rec;
133 m_rec = 0; 189 m_rec = 0;
134 receive(); 190 receive();
135} 191}
136 192
137void BtObex::sendEnd() { 193void BtObex::sendEnd() {
138 if (m_send->normalExit() ) { 194 if (m_send->normalExit() ) {
139 if ( m_send->exitStatus() == 0 ) { 195 if ( m_send->exitStatus() == 0 ) {
140 delete m_send; 196 delete m_send;
141 m_send=0; 197 m_send=0;
142 emit sent(true); 198 emit sent(true);
143 }else if (m_send->exitStatus() == 255 ) { // it failed maybe the other side wasn't ready 199 }else if (m_send->exitStatus() != 0 ) { // it failed maybe the other side wasn't ready
144 // let's try it again 200 // let's try it again
145 delete m_send; 201 delete m_send;
146 m_send = 0; 202 m_send = 0;
147 sendNow(); 203 sendNow();
148 } 204 }
149 }else { 205 }else {
150 emit error( -1 ); 206 emit error( -1 );
151 delete m_send; 207 delete m_send;
152 m_send = 0; 208 m_send = 0;
153 } 209 }
154} 210}
155 211
156// This probably doesn't do anything useful for bt. 212// This probably doesn't do anything useful for bt.
157QString BtObex::parseOut( ){ 213QString BtObex::parseOut(){
158 QString path; 214 QString path;
159 QStringList list = QStringList::split("\n", m_outp); 215 QStringList list = QStringList::split("\n", m_outp);
160 QStringList::Iterator it; 216 QStringList::Iterator it;
161 for (it = list.begin(); it != list.end(); ++it ) { 217 for (it = list.begin(); it != list.end(); ++it ) {
218 odebug << (*it) << oendl;
162 if ( (*it).startsWith("Wrote" ) ) { 219 if ( (*it).startsWith("Wrote" ) ) {
163 int pos = (*it).findRev('(' ); 220 int pos = (*it).findRev('(' );
164 if ( pos > 0 ) { 221 if ( pos > 0 ) {
165 222
166 path = (*it).remove( pos, (*it).length() - pos ); 223 path = (*it).remove( pos, (*it).length() - pos );
167 path = path.mid(6 ); 224 path = path.mid(6 );
168 path = path.stripWhiteSpace(); 225 path = path.stripWhiteSpace();
169 } 226 }
170 } 227 }
171 } 228 }
172 return path; 229 return path;
173} 230}
174/** 231/**
175 * when sent is done slotError is called we will start receive again 232 * when sent is done slotError is called we will start receive again
176 */ 233 */
177void BtObex::slotError() { 234void BtObex::slotError() {
178 if ( m_receive ) 235 if ( m_receive )
179 receive(); 236 receive();
180}; 237};
181void BtObex::setReceiveEnabled( bool receive ) { 238void BtObex::setReceiveEnabled( bool receive ) {
182 if ( !receive ) { // 239 if ( !receive ) { //
183 m_receive = false; 240 m_receive = false;
184 shutDownReceive(); 241 shutDownReceive();
185 } 242 }