summaryrefslogtreecommitdiffstats
path: root/src/connectivity/bluetooth/symbian/bluetoothlinkmanagerdevicediscoverer.cpp
blob: 314fc3fea36c515933daef488307b6d53290588f (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

// INCLUDE FILES
#include "bluetoothlinkmanagerdevicediscoverer.h"
#include "qbluetoothaddress.h"
#include "qbluetoothdeviceinfo.h"
#include "qbluetoothuuid.h"

#include <qstring.h>
#ifdef EIR_SUPPORTED
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
#include "utils_symbian_p.h"
#include <qdebug.h>

QTM_BEGIN_NAMESPACE

/*! \internal
    \class BluetoothLinkManagerDeviceDiscoverer
    \brief The BluetoothLinkManagerDeviceDiscoverer class searches other bluetooth devices.
    \since 1.2

    \ingroup connectivity-bluetooth
    \inmodule QtConnectivity
    \internal

    BluetoothLinkManagerDeviceDiscoverer is an Symbian ActiveObject derived class that discovers
    other Bluetooth devices using "BTLinkManager" protocol.

    When Bluetoothdevices are found signal deviceDiscovered(QBluetoothDeviceInfo &) is emitted. When
    all devices are found (or none) signal deviceDiscoveryComplete() is emitted.

    In case of an error signal linkManagerError(int error) is emitted. In the case of errorSignal
    one should consider deleting this class and creating it again.
*/

_LIT(KBTLinkManagerTxt,"BTLinkManager");

BluetoothLinkManagerDeviceDiscoverer::BluetoothLinkManagerDeviceDiscoverer(RSocketServ &socketServer,QObject *parent)
    : QObject(parent)
    , CActive(CActive::EPriorityStandard)
    , m_socketServer(socketServer)
    , m_pendingCancel(false), m_pendingStart(false), m_discoveryType (0)
{
    TInt result;

    /* find Bluetooth protocol */
    TProtocolDesc protocol;
    result = m_socketServer.FindProtocol(KBTLinkManagerTxt(),protocol);
    if (result == KErrNone) {
        /* Create and initialise an RHostResolver */
        result = m_hostResolver.Open(m_socketServer, protocol.iAddrFamily, protocol.iProtocol);
    }
    // check possible error
    if (result != KErrNone) {
        setError(result);
    }

    //add this active object to scheduler
    CActiveScheduler::Add(this);
}

BluetoothLinkManagerDeviceDiscoverer::~BluetoothLinkManagerDeviceDiscoverer()
{
    // cancel active object
    Cancel();
    m_hostResolver.Close();
}
/*!
    Starts up device discovery. When devices are discovered signal deviceDiscovered is emitted.
    After signal deviceDiscoveryComplete() is emitted new discovery request can be made.
*/
void BluetoothLinkManagerDeviceDiscoverer::startDiscovery(const uint discoveryType)
{
    m_discoveryType = discoveryType;
    
    if(m_pendingCancel == true) {
        m_pendingStart = true;
        m_pendingCancel = false;  
        return;
    }
    if (!IsActive()) {
        m_addr.SetIAC( discoveryType );
#ifdef EIR_SUPPORTED
        m_addr.SetAction(KHostResInquiry | KHostResName | KHostResIgnoreCache | KHostResEir);
#else
        m_addr.SetAction(KHostResInquiry | KHostResName | KHostResIgnoreCache);
#endif
        m_hostResolver.GetByAddress(m_addr, m_entry, iStatus);
        SetActive();
    }
}

void BluetoothLinkManagerDeviceDiscoverer::stopDiscovery()
{
    m_pendingStart = false; 
    if (IsActive() && !m_pendingCancel) {
        m_pendingCancel = true;
        m_hostResolver.Cancel();
    }
}

/*!
  Informs that our request has been prosessed and the data is available to be used.
*/
void BluetoothLinkManagerDeviceDiscoverer::RunL()
{
    qDebug() << __PRETTY_FUNCTION__ << iStatus.Int();
    switch (iStatus.Int()) {
    case KErrNone:  // found device
        if (m_pendingCancel && !m_pendingStart) {
            m_pendingCancel = false;
            emit canceled();
        } else {
            m_pendingCancel = false;
            m_pendingStart = false;
            // get next (possible) discovered device
            m_hostResolver.Next(m_entry, iStatus);
            SetActive();
            emit deviceDiscovered(currentDeviceDataToQBluetoothDeviceInfo());
        }
        break;
    case KErrHostResNoMoreResults:  // done with search
        if (m_pendingCancel && !m_pendingStart){  // search was canceled prior to finishing
            m_pendingCancel = false;
            m_pendingStart = false;
            emit canceled();
        }
        else if (m_pendingStart){  // search was restarted just prior to finishing
            m_pendingStart = false;
            m_pendingCancel = false;
            startDiscovery(m_discoveryType);
        } else {  // search completed normally
            m_pendingStart = false;
            m_pendingCancel = false;
            emit deviceDiscoveryComplete();
        }
        break;
    case KErrCancel:  // user canceled search
        if (m_pendingStart){  // user activated search after cancel
            m_pendingStart = false;
            m_pendingCancel = false;
            startDiscovery(m_discoveryType);
        } else {  // standard user cancel case
            m_pendingCancel = false;
            emit canceled();
        }
        break;
    default:
        m_pendingStart = false;
        m_pendingCancel = false;
        setError(iStatus.Int());
    }
}
/*!
  Cancel current request.
*/
void BluetoothLinkManagerDeviceDiscoverer::DoCancel()
{
    m_hostResolver.Cancel();
}

TInt BluetoothLinkManagerDeviceDiscoverer::RunError(TInt aError)
{
    setError(aError);
    return KErrNone;
}

bool BluetoothLinkManagerDeviceDiscoverer::isReallyActive() const
{
    if(m_pendingStart)
        return true;
    if(m_pendingCancel)
        return false;
    return IsActive();
}

/*!
    Transforms Symbian device name, address and service classes to QBluetootDeviceInfo.
*/
QBluetoothDeviceInfo BluetoothLinkManagerDeviceDiscoverer::currentDeviceDataToQBluetoothDeviceInfo() const
{
    // extract device information from results and map them to QBluetoothDeviceInfo
#ifdef EIR_SUPPORTED
    TBluetoothNameRecordWrapper eir(m_entry());
    TInt bufferlength = 0;
    TInt err = KErrNone;
    QString deviceName;
    bufferlength = eir.GetDeviceNameLength();

    if (bufferlength > 0) {
        TBool nameComplete;
        HBufC *deviceNameBuffer = 0;
        TRAP(err,deviceNameBuffer = HBufC::NewLC(bufferlength));
        if(err)
            deviceName = QString();
        else
            {
            TPtr ptr = deviceNameBuffer->Des();
            err = eir.GetDeviceName(ptr,nameComplete);
            if (err == KErrNone /*&& nameComplete*/)
                {
                if(!nameComplete)
                    qWarning() << "device name incomplete";
                // isn't it better to get an incomplete name than getting nothing?
                deviceName = QString::fromUtf16(ptr.Ptr(), ptr.Length()).toUpper();
                }
            else
                deviceName = QString();
            CleanupStack::PopAndDestroy(deviceNameBuffer);
            }
    }

    QList<QBluetoothUuid> serviceUidList;
    RExtendedInquiryResponseUUIDContainer uuidContainer;
    QBluetoothDeviceInfo::DataCompleteness completenes = QBluetoothDeviceInfo::DataUnavailable;

    if (eir.GetServiceClassUuids(uuidContainer) == KErrNone) {
        TInt uuidCount = uuidContainer.UUIDs().Count();
        if (uuidCount > 0) {
            for (int i = 0; i < uuidCount; ++i) {
                TPtrC8 shortFormUUid(uuidContainer.UUIDs()[i].ShortestForm());
                if (shortFormUUid.Size() == 2) {
                    QBluetoothUuid uuid(ntohs(*reinterpret_cast<const quint16 *>(shortFormUUid.Ptr())));
                    if (uuidContainer.GetCompleteness(RExtendedInquiryResponseUUIDContainer::EUUID16))
                        completenes = QBluetoothDeviceInfo::DataComplete;
                    else
                        completenes = QBluetoothDeviceInfo::DataIncomplete;
                    serviceUidList.append(uuid);
                }else if (shortFormUUid.Size() == 4) {
                    QBluetoothUuid uuid(ntohl(*reinterpret_cast<const quint32 *>(shortFormUUid.Ptr())));
                    if (uuidContainer.GetCompleteness(RExtendedInquiryResponseUUIDContainer::EUUID32))
                        completenes = QBluetoothDeviceInfo::DataComplete;
                    else
                        completenes = QBluetoothDeviceInfo::DataIncomplete;
                    serviceUidList.append(uuid);
                }else if (shortFormUUid.Size() == 16) {
                    QBluetoothUuid uuid(*reinterpret_cast<const quint128 *>(shortFormUUid.Ptr()));
                    if (uuidContainer.GetCompleteness(RExtendedInquiryResponseUUIDContainer::EUUID128))
                        completenes = QBluetoothDeviceInfo::DataComplete;
                    else
                        completenes = QBluetoothDeviceInfo::DataIncomplete;
                    serviceUidList.append(uuid);
                }
            }
        }
    }
    uuidContainer.Close();

    bufferlength = 0;
    QByteArray manufacturerData;
    bufferlength = eir.GetVendorSpecificDataLength();

    if (bufferlength > 0) {
        HBufC8 *msd = 0;
        TRAP(err,HBufC8::NewLC(bufferlength));
        if(err)
            manufacturerData = QByteArray();
        else
            {
            TPtr8 temp = msd->Des();
            if (eir.GetVendorSpecificData(temp))
                manufacturerData = s60Desc8ToQByteArray(temp);
            else
                manufacturerData = QByteArray();
            CleanupStack::PopAndDestroy(msd);
            }
    }

    // Get transmission power level
    TInt8 transmissionPowerLevel = 0;
    eir.GetTxPowerLevel(transmissionPowerLevel);

    // unique address of the device
    const TBTDevAddr symbianDeviceAddress = static_cast<TBTSockAddr> (m_entry().iAddr).BTAddr();
    QBluetoothAddress bluetoothAddress = qTBTDevAddrToQBluetoothAddress(symbianDeviceAddress);

    // format symbian major/minor numbers
    quint32 deviceClass = qTPackSymbianDeviceClass(m_addr);

    QBluetoothDeviceInfo deviceInfo(bluetoothAddress, deviceName, deviceClass);

    deviceInfo.setRssi(transmissionPowerLevel);
    deviceInfo.setServiceUuids(serviceUidList, completenes);
    deviceInfo.setManufacturerSpecificData(manufacturerData);
#else
    // device name
    THostName symbianDeviceName = m_entry().iName;
    QString deviceName = QString::fromUtf16(symbianDeviceName.Ptr(), symbianDeviceName.Length()).toUpper();

    // unique address of the device
    const TBTDevAddr symbianDeviceAddress = static_cast<TBTSockAddr> (m_entry().iAddr).BTAddr();
    QBluetoothAddress bluetoothAddress = qTBTDevAddrToQBluetoothAddress(symbianDeviceAddress);

    // format symbian major/minor numbers
    quint32 deviceClass = qTPackSymbianDeviceClass(m_addr);

    QBluetoothDeviceInfo deviceInfo(bluetoothAddress, deviceName, deviceClass);

    if (m_addr.Rssi())
        deviceInfo.setRssi(m_addr.Rssi());
#endif
    if (!deviceInfo.rssi())
        deviceInfo.setRssi(1);

    deviceInfo.setCached(false);  //TODO cache support missing from devicediscovery API
    //qDebug()<< "Discovered device: name="<< deviceName <<", address=" << bluetoothAddress.toString() <<", class=" << deviceClass;
    return deviceInfo;
}

void BluetoothLinkManagerDeviceDiscoverer::setError(int errorCode)
{
    qDebug() << __PRETTY_FUNCTION__ << "errorCode=" << errorCode;
    QString errorString;
    switch (errorCode) {
        case KLinkManagerErrBase:
            errorString.append("Link manager base error value or Insufficient baseband resources error value");
            emit linkManagerError(QBluetoothDeviceDiscoveryAgent::UnknownError, errorString);
            break;
        case KErrProxyWriteNotAvailable:
            errorString.append("Proxy write not available error value");
            emit linkManagerError(QBluetoothDeviceDiscoveryAgent::UnknownError, errorString);
            break;
        case KErrReflexiveBluetoothLink:
            errorString.append("Reflexive BT link error value");
            emit linkManagerError(QBluetoothDeviceDiscoveryAgent::UnknownError, errorString);
            break;
        case KErrPendingPhysicalLink:
            errorString.append("Physical link connection already pending when trying to connect the physical link");
            emit linkManagerError(QBluetoothDeviceDiscoveryAgent::UnknownError, errorString);
            break;
        case KErrNotReady:
            errorString.append("KErrNotReady");
            emit linkManagerError(QBluetoothDeviceDiscoveryAgent::UnknownError, errorString);
        case KErrCancel:
            errorString.append("KErrCancel");
            qDebug() << "emitting canceled";
            emit canceled();
            break;
        case KErrNone:
            // do nothing
            break;
        default:
            errorString = QString("Symbian errorCode = %1").arg(errorCode);
            emit linkManagerError(QBluetoothDeviceDiscoveryAgent::UnknownError, errorString);
            break;
    }
}
#include "moc_bluetoothlinkmanagerdevicediscoverer.cpp"

QTM_END_NAMESPACE