examples/QtQuick/bluetoothex/devicediscoverer.cpp

00001 /*
00002  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 // INCLUDE FILES
00031 #include "devicediscoverer.h"
00032 
00033 _LIT(KBTLinkManagerTxt,"BTLinkManager");
00034 
00035 // Call Static function NewL to create an instance of this class.
00036 CDeviceDiscoverer* CDeviceDiscoverer::NewL(RSocketServ& aSocketServ,
00037                                            MDeviceDiscoObserver& aObserver)
00038     {
00039     CDeviceDiscoverer* self = CDeviceDiscoverer::NewLC(aSocketServ, aObserver);
00040     CleanupStack::Pop(self);
00041     return self;
00042     }
00043 
00044 // Call Static function NewLC to create an instance of this class.
00045 CDeviceDiscoverer* CDeviceDiscoverer::NewLC(RSocketServ& aSocketServ,
00046                                             MDeviceDiscoObserver& aObserver)
00047     {
00048     CDeviceDiscoverer* self = new (ELeave) CDeviceDiscoverer(aSocketServ,
00049         aObserver);
00050     CleanupStack::PushL(self);
00051     self->ConstructL();
00052     return self;
00053     }
00054 
00055 // Two phase contructor.
00056 void CDeviceDiscoverer::ConstructL()
00057     {
00058     }
00059 
00060 // constructor
00061 CDeviceDiscoverer::CDeviceDiscoverer(RSocketServ& aSocketServ,
00062                                      MDeviceDiscoObserver& aObserver):
00063     CActive(CActive::EPriorityStandard),
00064     iSocketServ(aSocketServ),
00065     iObserver(aObserver)
00066     {
00067     CActiveScheduler::Add(this);
00068     }
00069 
00070 // Destructor
00071 CDeviceDiscoverer::~CDeviceDiscoverer()
00072     { 
00073     // cancel active object
00074     Cancel();
00075     iResolver.Close();
00076     }
00077 
00083 void CDeviceDiscoverer::DiscoverDevicesL(TDeviceDataList* aDevDataList)
00084     {
00085     if (!IsActive())
00086         {
00087         // initialize host resolver
00088         // load protocol for discovery
00089         TProtocolDesc pdesc;
00090         User::LeaveIfError(iSocketServ.FindProtocol(KBTLinkManagerTxt(), pdesc));
00091 
00092         iResolver.Close();
00093         User::LeaveIfError(iResolver.Open(iSocketServ, pdesc.iAddrFamily, pdesc.iProtocol));
00094         
00095         // wipe existing device data list, start fresh
00096         iDevDataList=aDevDataList;
00097         iDevDataList->Reset();
00098 
00099         iAddr.SetIAC( KGIAC );
00100 
00101         iAddr.SetAction(KHostResInquiry|KHostResName|KHostResIgnoreCache);
00102         iResolver.GetByAddress(iAddr, iEntry, iStatus);
00103         SetActive();
00104         }
00105     else
00106         {
00107         User::Leave(KErrNotReady);
00108         }
00109     }
00110 
00111 // RunL
00112 void CDeviceDiscoverer::RunL()
00113     {
00114     // RHostResolver.GetByAddress(..) has completed, process results
00115     if ( iStatus!=KErrNone )
00116         {
00117         // all devices (if any) done, notify 
00118         if (iDevDataList->Count()>0)
00119             {
00120             iObserver.HandleDeviceDiscoveryComplete(KErrNone);
00121             }
00122         else
00123             {
00124             iObserver.HandleDeviceDiscoveryComplete(KErrNotFound);
00125             }
00126         }
00127     else 
00128         {
00129         // new device data entry
00130         TDeviceData *devData = new (ELeave) TDeviceData();
00131         devData->iDeviceName = iEntry().iName;
00132         devData->iDeviceAddr = 
00133             static_cast<TBTSockAddr>(iEntry().iAddr).BTAddr();
00134         devData->iDeviceServicePort = 0;
00135         // add device data entry
00136         iDevDataList->Append(devData);
00137         
00138         iObserver.DeviceDiscovered(*devData);
00139 
00140         // get next discovered device
00141         iResolver.Next(iEntry, iStatus);
00142         // wait for resolver to complete
00143         SetActive();
00144         }
00145     }
00146 
00147 // Cancel the current request.
00148 void CDeviceDiscoverer::DoCancel()
00149     {
00150     iResolver.Cancel();
00151     }
00152 
00153 
00154 // Returns true if any devices were discovered
00155 TBool CDeviceDiscoverer::HasDevices()
00156     {
00157     TBool exists = EFalse;
00158     
00159     if (iDevDataList)
00160         {
00161         if (iDevDataList->Count() > 0)
00162             {
00163             exists = ETrue;
00164             }
00165         }
00166     return exists;
00167     }
00168 
00169 // Stop discovery
00170 void CDeviceDiscoverer::StopDiscovery()
00171     {
00172     Cancel();
00173 
00174     if (HasDevices())
00175         {
00176         iObserver.HandleDeviceDiscoveryComplete(KErrNone);
00177         }
00178     else
00179         {
00180         iObserver.HandleDeviceDiscoveryComplete(KErrNotFound);
00181         }
00182     }

Generated by  doxygen 1.6.2