author | wimpie <wimpie> | 2005-01-04 01:42:25 (UTC) |
---|---|---|
committer | wimpie <wimpie> | 2005-01-04 01:42:25 (UTC) |
commit | 2487b0a05f502e7410715460f390cc80e7e76fd9 (patch) (unidiff) | |
tree | 430dce90a1bdff8eb85cbf1004db094ab6653ab9 /noncore/settings/networksettings2/opietooth2/OTInquiry.cpp | |
parent | e2094d408c9102f8866aafbe725a65f25fdef063 (diff) | |
download | opie-2487b0a05f502e7410715460f390cc80e7e76fd9.zip opie-2487b0a05f502e7410715460f390cc80e7e76fd9.tar.gz opie-2487b0a05f502e7410715460f390cc80e7e76fd9.tar.bz2 |
*** empty log message ***
Diffstat (limited to 'noncore/settings/networksettings2/opietooth2/OTInquiry.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/opietooth2/OTInquiry.cpp | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/opietooth2/OTInquiry.cpp b/noncore/settings/networksettings2/opietooth2/OTInquiry.cpp new file mode 100644 index 0000000..8e94bbc --- a/dev/null +++ b/noncore/settings/networksettings2/opietooth2/OTInquiry.cpp | |||
@@ -0,0 +1,219 @@ | |||
1 | //-*-c++-*- | ||
2 | /*************************************************************************** | ||
3 | * Copyright (C) 2003 by Fred Schaettgen * | ||
4 | * kdebluetooth@schaettgen.de * | ||
5 | * * | ||
6 | * This program is free software; you can redistribute it and/or modify * | ||
7 | * it under the terms of the GNU General Public License as published by * | ||
8 | * the Free Software Foundation; either version 2 of the License, or * | ||
9 | * (at your option) any later version. * | ||
10 | ***************************************************************************/ | ||
11 | |||
12 | |||
13 | #include <qcstring.h> | ||
14 | #include <qsocket.h> | ||
15 | #include <qdatetime.h> | ||
16 | #include <qtimer.h> | ||
17 | #include <qthread.h> | ||
18 | #include <qapplication.h> | ||
19 | |||
20 | #include <bluezlib.h> | ||
21 | |||
22 | #include <OTGateway.h> | ||
23 | #include <OTDriver.h> | ||
24 | #include <OTPeer.h> | ||
25 | #include <OTHCISocket.h> | ||
26 | #include <OTInquiry.h> | ||
27 | |||
28 | #include <opie2/odebug.h> | ||
29 | |||
30 | using namespace Opietooth2; | ||
31 | |||
32 | #define max(a,b) (((a)>(b)) ? (a) : (b)) | ||
33 | #define min(a,b) (((a)>(b)) ? (b) : (a)) | ||
34 | |||
35 | OTInquiry::OTInquiry( OTDriver * Drv ) : QObject( Drv ) { | ||
36 | |||
37 | reset(); | ||
38 | |||
39 | InquiryTimeoutTimer = new QTimer(this); | ||
40 | |||
41 | connect( InquiryTimeoutTimer, | ||
42 | SIGNAL(timeout()), | ||
43 | this, | ||
44 | SLOT(slotInquiryTimeout())); | ||
45 | |||
46 | Driver = Drv; | ||
47 | Socket = Drv->openSocket(); | ||
48 | Socket->open(); | ||
49 | |||
50 | connect( Socket, | ||
51 | SIGNAL( event(unsigned char, QByteArray)), | ||
52 | this, | ||
53 | SLOT(slotHCIEvent(unsigned char, QByteArray))); | ||
54 | } | ||
55 | |||
56 | OTInquiry::~OTInquiry() { | ||
57 | stopInquiring(); | ||
58 | } | ||
59 | |||
60 | void OTInquiry::stopInquiring( void ) { | ||
61 | if( Socket ) { | ||
62 | owarn << "Stop inquiry" << oendl; | ||
63 | Driver->closeSocket(); | ||
64 | Socket = 0; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | bool OTInquiry::inquire( double timeout, int numResponses, int lap) { | ||
69 | |||
70 | QByteArray cmdBuf(5); | ||
71 | |||
72 | cmdBuf[0] = lap & 0xFF; | ||
73 | cmdBuf[1] = (lap >> 8) & 0xFF; | ||
74 | cmdBuf[2] = (lap >> 16) & 0xFF; | ||
75 | cmdBuf[3] = max(0x01, min(0x30, int(timeout/1.28))); | ||
76 | cmdBuf[4] = (unsigned char)numResponses; | ||
77 | |||
78 | owarn << "Send HCI inquiry command. wait for " << cmdBuf[3] << oendl; | ||
79 | |||
80 | Socket->sendCommand(0x01, 0x0001, cmdBuf); | ||
81 | |||
82 | int status; | ||
83 | |||
84 | if( Socket->readStatus(0x01, 0x0001, &status)) { | ||
85 | if (status == 0) { | ||
86 | SuccessfullyStarted = true; | ||
87 | InquiryTimeoutTimer->start( int(1000*(timeout+1.0)), true); | ||
88 | return true; | ||
89 | } | ||
90 | else { | ||
91 | QString S =QString().sprintf( "%x", status ); | ||
92 | owarn << "OTInquiry::inquiry() failed: 0x" << S << oendl; | ||
93 | emit finished(); | ||
94 | return false; | ||
95 | } | ||
96 | } else { | ||
97 | owarn << "OTInquiry::inquiry(): Timeout." << oendl; | ||
98 | return false; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | bool OTInquiry::isInquiring() { | ||
103 | return InquiryTimeoutTimer->isActive(); | ||
104 | } | ||
105 | |||
106 | bool OTInquiry::isFinished() { | ||
107 | return SuccessfullyStarted && SuccessfullyEnded; | ||
108 | } | ||
109 | |||
110 | void OTInquiry::reset() { | ||
111 | |||
112 | SuccessfullyStarted = false; | ||
113 | SuccessfullyEnded = false; | ||
114 | //addrCache.clear(); | ||
115 | //infoQueue.clear(); | ||
116 | } | ||
117 | |||
118 | |||
119 | void OTInquiry::onPeerFound( OTPeer * Peer, bool IsNew ) { | ||
120 | emit peerFound( Peer, IsNew ); | ||
121 | } | ||
122 | |||
123 | void OTInquiry::slotInquiryTimeout() { | ||
124 | emit error( tr( "Timeout while waiting for end of inquiry.") ); | ||
125 | } | ||
126 | |||
127 | void OTInquiry::slotHCIEvent(unsigned char eventCode, QByteArray buf) { | ||
128 | |||
129 | owarn << "OTInquiry: hci packet received: eventCode=" | ||
130 | << (unsigned int)eventCode | ||
131 | << " packetLength=" | ||
132 | << (unsigned int)buf.size() | ||
133 | << oendl; | ||
134 | |||
135 | unsigned char *data = (unsigned char*)buf.data(); | ||
136 | switch (eventCode) { | ||
137 | case EVT_INQUIRY_COMPLETE: | ||
138 | { unsigned char status = data[0]; | ||
139 | owarn << "EVT_INQUIRY_COMPLETE status=" << status << oendl; | ||
140 | InquiryTimeoutTimer->stop(); | ||
141 | if (status == 0) { | ||
142 | if( SuccessfullyStarted == true) { | ||
143 | owarn << "OTInquiry ended successfully" << oendl; | ||
144 | SuccessfullyEnded = true; | ||
145 | } | ||
146 | emit finished(); | ||
147 | } | ||
148 | else { | ||
149 | emit error( tr( "OTInquiry completed with error (code %1)" ). | ||
150 | arg(status)); | ||
151 | } | ||
152 | } | ||
153 | break; | ||
154 | case EVT_INQUIRY_RESULT: | ||
155 | { int numResults = data[0]; | ||
156 | OTPeer * P = 0; | ||
157 | bool IsNew; | ||
158 | OTDeviceAddress Addr; | ||
159 | QString N; | ||
160 | |||
161 | inquiry_info *results = (inquiry_info*)(data+1); | ||
162 | |||
163 | for (int n=0; n<numResults; n++) { | ||
164 | Addr.setBDAddr( results[n].bdaddr ); | ||
165 | |||
166 | owarn << "INQUIRY_RESULT: " | ||
167 | << Addr.toString() | ||
168 | << oendl; | ||
169 | |||
170 | P = Driver->gateway()->findPeer( Addr ); | ||
171 | |||
172 | if( P ) { | ||
173 | // peer known | ||
174 | if( P->state() != OTPeer::Peer_Up ) { | ||
175 | P->setState( OTPeer::Peer_Up ); | ||
176 | } | ||
177 | IsNew = 0; | ||
178 | } else { | ||
179 | IsNew = 1; | ||
180 | // push the address to the address queue | ||
181 | // where it can be consumed by nextNeighbour() | ||
182 | P = new OTPeer( Driver->gateway() ); | ||
183 | P->setState( OTPeer::Peer_Up ); // we just detected it | ||
184 | P->setAddress( Addr ); | ||
185 | //if( addrCache.find(info.addr) == addrCache.end()) { | ||
186 | // addrCache.insert(info.addr); | ||
187 | |||
188 | P->setDeviceClass( (results[n].dev_class[0] << 16) | | ||
189 | (results[n].dev_class[1] << 8) | | ||
190 | (results[n].dev_class[2] << 0) ); | ||
191 | // infoQueue.push_back(info); | ||
192 | P->setName( Driver->getPeerName( Addr ) ); | ||
193 | } | ||
194 | |||
195 | // call the handler. Emits a signal if not overwritten | ||
196 | onPeerFound( P, IsNew ); | ||
197 | |||
198 | // } | ||
199 | } | ||
200 | } | ||
201 | break; | ||
202 | case EVT_CMD_STATUS : | ||
203 | { int status = data[0]; | ||
204 | int numHciCmdPkts = data[1]; | ||
205 | int cmdOpcode = *((uint16_t*)(data+2)); | ||
206 | owarn << "EVT_CMD_STATUS status=" | ||
207 | << status | ||
208 | << " numPkts=" | ||
209 | << numHciCmdPkts | ||
210 | << " cmdOpcode=" | ||
211 | << cmdOpcode | ||
212 | << oendl; | ||
213 | if (cmdOpcode == OCF_INQUIRY) { | ||
214 | |||
215 | } | ||
216 | } | ||
217 | break; | ||
218 | } | ||
219 | } | ||