summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/opietooth2/OTInquiry.h
blob: 559af7a427164e90be6e1d6451ad5e93f033ddc2 (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
//-*-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.                                   *
 ***************************************************************************/

#ifndef OTINQUIRY_H
#define OTINQUIRY_H

#include <qstring.h>
#include <qobject.h>

#include <qguardedptr.h>
#include <qtimer.h>

#include <OTPeer.h>

// #include <set>
// #include <deque>

namespace Opietooth2 {

class QSocket;
class QDateTime;

class OTDriver;
class OTHCISocket;

/** Scans for nearby bluetooth devices.
 * This class provides an asynchronous interface to the
 * inquriy HCI command. To scan for other devices, connect
 * to the signals neighbourFound() and finished() and call
 * inquiry(). Inquiry results are signalled as soon as they arrive,
 * so the information can be displayed before the whole inquiry
 * process is finished.
 * Still no networkSetups should be set up before
 * the finished() signal was sent (hmm, is this always true..?)
 */

class OTInquiry : public QObject {

    Q_OBJECT

public:

    /** Constructor.
    @param owner The owning parent object
    */
    OTInquiry( OTDriver* Drv );

    /** Destructor. */
    virtual ~OTInquiry();

    // General/Unlimited Inquiry Access Code
    static const int GIAC = 0x9E8B33;

    // Limited Dedicated Inquiry Access Code
    static const int LIAC = 0x9E8B00;

    /** Starts the inquiry.
    if you start inquiry several times without calling clear(),
    you will receive each result only once.
    @param timeout duration of the inquiry in seconds.
    It will be rounded to the next multiple of 1.28 sec,
    with a maximum of 61.44 sec.
    @param lap
    */
    bool inquire( double timeout = 8.0, 
                  int numResponses = 0,
                  int lap = GIAC);

    void stopInquiring( );

    /** Enters periodic inquiry mode.
    An inquiry will be started at a random time in the intervall
    between minduration and maxduration.
    @param minduration minimum time between two inquiries. Rounded to
        a multiple of 1.28, (3.84 <= d <= 83884.8)
    @param maxduration maximum time between two inquiries. Rounded to
        a multiple of 1.28, (3.84 <= d <= 83884.8)
    @param timeout duration of the inquiry in seconds.
        It will be rounded to the next multiple of 1.28 sec,
        with a maximum of 61.44 sec.
    @param numResponses Number of responses after which the inquiry
        will be stopped. 0 means no limit.
    @param lap
    */
    /*
    void inquirePeriodically( double minduration, 
                              double maxduration,
                              double timeout = 8.0, 
                              int numResponses = 0,
                              int lap = LIAC
                            );
*/
    /** checks if there the inquiry is running currently
    @return true if there is an inquiry running
    which was started by this object.
    @todo possibility to check for "foreign" inquiries. Deal with
    the fact that we can receive foreign inquiry results.
    */
    bool isInquiring();

    /** This function returns true after after an inquiry was
    started, results were received and the inquiry ended successfully.
    This can be the case without calling inquiry() at all, because
    results of inquiries started by other programs are also processed. 
    */
    bool isFinished();
      
    /** Resets the list of received results and sets 
    isInquiryComplete() to false.
    */
    void reset();

    inline OTDriver * driver() const
      { return Driver; }
      
protected:

    /** Called when a new neighbour was found. The default
    implementation emits the neighbourFound signal.
    @param bdaddr the address found.
    */
    virtual void onPeerFound( OTPeer * Peer, bool  );

    enum ErrorCode {
         InquiryFinishedTimeout = 0x0100
    };

signals :

    void peerFound( OTPeer *, bool );

    /** Emitted after an inquiry has finished successfully.
    If the inquiry was canceled, no finished signals is emitted.
    This signal can be emitted without having called inquiry, since
    other processes may have started an inquiry.
    */
    void finished();

    /** Emitted instead of finished if an error occured after
    calling inquiry() or periodicInquiryMode()
    @param code error code.
    @param message Error message
    */
    void error( QString message );

private:

    // std::set<DeviceAddress> addrCache;
    // double currentTimeout;
    // QByteArray* buf;
    // QSocket* hciSocket;
    QGuardedPtr<OTHCISocket> Socket;
    OTDriver * Driver;

    //QDateTime *startTime;
    QTimer *InquiryTimeoutTimer;

    // std::deque<InquiryInfo> infoQueue;
    bool SuccessfullyStarted;
    bool SuccessfullyEnded;
    
private slots:

    void slotInquiryTimeout();
    void slotHCIEvent(unsigned char eventCode, QByteArray buf);

};

}
#endif