summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTInquiry.cpp
blob: 2bc63c76aa2cbcf4d7d0beccc0c3eb7711010b4d (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//-*-c++-*-
/***************************************************************************
 *   Copyright (C) 2003 by Fred Schaettgen                                 *
 *   kdebluetooth@schaettgen.de                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/


#include <qcstring.h>
#include <qsocket.h>
#include <qdatetime.h>
#include <qtimer.h>
#include <qthread.h>
#include <qapplication.h>

#include <bluezlib.h>

#include <OTGateway.h>
#include <OTDriver.h>
#include <OTPeer.h>
#include <OTHCISocket.h>
#include <OTInquiry.h>

#include <opie2/odebug.h>

using namespace Opietooth2;

#define max(a,b)        (((a)>(b)) ? (a) : (b))
#define min(a,b)        (((a)>(b)) ? (b) : (a))

OTInquiry::OTInquiry( OTDriver * Drv ) : QObject( Drv ) {

    reset();

    InquiryTimeoutTimer = new QTimer(this);

    connect( InquiryTimeoutTimer, 
             SIGNAL(timeout()),
             this, 
             SLOT(slotInquiryTimeout()));

    Driver = Drv;
    Socket = Drv->openSocket();
    Socket->open();

    connect( Socket, 
             SIGNAL( event(unsigned char, QByteArray)),
             this, 
             SLOT(slotHCIEvent(unsigned char, QByteArray)));
}

OTInquiry::~OTInquiry() {
    stopInquiring();
}

void OTInquiry::stopInquiring( void ) {
    if( Socket ) {
      odebug << "Stop inquiry" << oendl;
      Driver->closeSocket();
      Socket = 0;
    }
}

bool OTInquiry::inquire( double timeout, int numResponses, int lap) {

    QByteArray cmdBuf(5);

    cmdBuf[0] = lap & 0xFF;
    cmdBuf[1] = (lap >> 8) & 0xFF;
    cmdBuf[2] = (lap >> 16) & 0xFF;
    cmdBuf[3] = max(0x01, min(0x30, int(timeout/1.28)));
    cmdBuf[4] = (unsigned char)numResponses;

    odebug << "Send HCI inquiry command. wait for " << cmdBuf[3] << oendl;

    Socket->sendCommand(0x01, 0x0001, cmdBuf);

    int status;

    if( Socket->readStatus(0x01, 0x0001, &status)) {
      if (status == 0) {
        SuccessfullyStarted = true;
        InquiryTimeoutTimer->start( int(1000*(timeout+1.0)), true);
        return true;
      }
      else {
        QString S =QString().sprintf( "%x", status );
        odebug << "OTInquiry::inquiry() failed: 0x" << S << oendl;
        emit finished();
        return false;
      }
    } else {
      odebug << "OTInquiry::inquiry(): Timeout." << oendl;
      return false;
    }
}

bool OTInquiry::isInquiring() {
    return InquiryTimeoutTimer->isActive();
}

bool OTInquiry::isFinished() {
    return SuccessfullyStarted && SuccessfullyEnded;
}
      
void OTInquiry::reset() {

    SuccessfullyStarted = false;
    SuccessfullyEnded = false;
    //addrCache.clear();
    //infoQueue.clear();
}


void OTInquiry::onPeerFound( OTPeer * Peer, bool IsNew ) {
    emit peerFound( Peer, IsNew );
}

void OTInquiry::slotInquiryTimeout() {
    emit error( tr( "Timeout while waiting for end of inquiry.") );
}

void OTInquiry::slotHCIEvent(unsigned char eventCode, QByteArray buf) {

    odebug << "OTInquiry: hci packet received: eventCode="
          << (unsigned int)eventCode 
          << " packetLength="
          << (unsigned int)buf.size() 
          << oendl;

    unsigned char *data = (unsigned char*)buf.data();
    switch (eventCode) {
      case EVT_INQUIRY_COMPLETE:
        { unsigned char status = data[0];
          odebug << "EVT_INQUIRY_COMPLETE status=" << status << oendl;
          InquiryTimeoutTimer->stop();
          if (status == 0) {
            if( SuccessfullyStarted == true) {
              odebug << "OTInquiry ended successfully" << oendl;
              SuccessfullyEnded = true;
            }
            emit finished();
          }
          else {
            emit error( tr( "OTInquiry completed with error (code %1)" ).
                          arg(status));
          }
        }
        break;
      case EVT_INQUIRY_RESULT:
        { int numResults = data[0];
          OTPeer * P = 0;
          bool IsNew;
          OTDeviceAddress Addr;
          QString N;

          inquiry_info *results = (inquiry_info*)(data+1);

          for (int n=0; n<numResults; n++) {
            Addr.setBDAddr( results[n].bdaddr );

            odebug << "INQUIRY_RESULT: "
                  << Addr.toString() 
                  << oendl;

            P = Driver->gateway()->findPeer( Addr );

            if( P ) {
              // peer known
              if( P->state() != OTPeer::Peer_Up ) {
                P->setState( OTPeer::Peer_Up );
              }
              IsNew = 0;
            } else {
              IsNew = 1;
              // push the address to the address queue
              // where it can be consumed by nextNeighbour()
              P = new OTPeer( Driver->gateway() );
              P->setState( OTPeer::Peer_Up ); // we just detected it
              P->setAddress( Addr );
              //if( addrCache.find(info.addr) == addrCache.end()) {
              // addrCache.insert(info.addr);

              P->setDeviceClass( (results[n].dev_class[0] << 16) |
                                 (results[n].dev_class[1] << 8) |
                                 (results[n].dev_class[2] << 0) );
              // infoQueue.push_back(info);
              P->setName( Driver->getPeerName( Addr ) ); 
            }

            // call the handler. Emits a signal if not overwritten
            onPeerFound( P, IsNew );

            // }
          }
        }
        break;
      case EVT_CMD_STATUS :
        { int status = data[0];
          int numHciCmdPkts = data[1];
          int cmdOpcode = *((uint16_t*)(data+2));
          odebug << "EVT_CMD_STATUS status=" 
                << status
                << " numPkts=" 
                << numHciCmdPkts 
                << " cmdOpcode="
                << cmdOpcode 
                << oendl;
          if (cmdOpcode == OCF_INQUIRY) {

          }
        }
        break;
    }
}