examples/ForumNokia/BluetoothPMPExample/src/DeviceDiscoverer.cpp

00001 /*
00002  * Copyright (c) 2009-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 CDeviceDiscoverer* CDeviceDiscoverer::NewL(RSocketServ& aSocketServ,
00036                                            MDeviceDiscoObserver& aObserver)
00037     {
00038     CDeviceDiscoverer* self = CDeviceDiscoverer::NewLC(aSocketServ, aObserver);
00039     CleanupStack::Pop(self);
00040     return self;
00041     }
00042 
00043 
00044 CDeviceDiscoverer* CDeviceDiscoverer::NewLC(RSocketServ& aSocketServ,
00045                                             MDeviceDiscoObserver& aObserver)
00046     {
00047     CDeviceDiscoverer* self = new (ELeave) CDeviceDiscoverer(aSocketServ,
00048         aObserver);
00049     CleanupStack::PushL(self);
00050     self->ConstructL();
00051     return self;
00052     }
00053 
00054 
00055 void CDeviceDiscoverer::ConstructL()
00056     {
00057     }
00058 
00059 
00060 // ----------------------------------------------------------------------------
00061 // CDeviceDiscoverer::CDeviceDiscoverer(RSocketServ *aSocketServ,
00062 //                                      MDeviceDiscoObserver *aObserver)
00063 //
00064 // constructor
00065 // ----------------------------------------------------------------------------
00066 CDeviceDiscoverer::CDeviceDiscoverer(RSocketServ& aSocketServ,
00067                                      MDeviceDiscoObserver& aObserver):
00068     CActive(CActive::EPriorityStandard),
00069     iSocketServ(aSocketServ),
00070     iObserver(aObserver)
00071     {
00072     CActiveScheduler::Add(this);
00073     }
00074 
00075 
00076 // ----------------------------------------------------------------------------
00077 // CDeviceDiscoverer::~CDeviceDiscoverer()
00078 //
00079 // destructor
00080 // ----------------------------------------------------------------------------
00081 CDeviceDiscoverer::~CDeviceDiscoverer()
00082     { 
00083     // cancel active object
00084     Cancel();
00085     iResolver.Close();
00086     }
00087 
00088 
00089 // ----------------------------------------------------------------------------
00090 // CDeviceDiscoverer::DiscoverDevicesL(TDeviceDataList *aDevDataList)
00091 //
00092 // discover remote devices.  RHostResolver will be used to do the discovery.
00093 // any found devices will be placed in aDevDataList.
00094 // ----------------------------------------------------------------------------
00095 void CDeviceDiscoverer::DiscoverDevicesL(TDeviceDataList* aDevDataList)
00096     {
00097     if (!IsActive())
00098         {
00099         // initialize host resolver
00100         // load protocol for discovery
00101         TProtocolDesc pdesc;
00102         User::LeaveIfError(iSocketServ.FindProtocol(KBTLinkManagerTxt(), pdesc));
00103 
00104         iResolver.Close();
00105         User::LeaveIfError(iResolver.Open(iSocketServ, pdesc.iAddrFamily, pdesc.iProtocol));
00106         
00107         // wipe existing device data list, start fresh
00108         iDevDataList=aDevDataList;
00109         iDevDataList->Reset();
00110 
00111         // start device discovery by invoking remote address lookup
00112     #ifdef ENABLE_LIAC
00113         TUint myIAC( iLIAC?KLIAC:KGIAC );
00114 
00115         iAddr.SetIAC( myIAC );
00116     #else
00117         iAddr.SetIAC( KGIAC );
00118     #endif
00119 
00120         iAddr.SetAction(KHostResInquiry|KHostResName|KHostResIgnoreCache);
00121         iResolver.GetByAddress(iAddr, iEntry, iStatus);
00122         SetActive();
00123         }
00124     else
00125         {
00126         User::Leave(KErrNotReady);
00127         }
00128     }
00129 
00130 
00131 void CDeviceDiscoverer::RunL()
00132     {
00133     // RHostResolver.GetByAddress(..) has completed, process results
00134     if ( iStatus!=KErrNone )
00135         {
00136         // all devices (if any) done, notify 
00137         if (iDevDataList->Count()>0)
00138             {
00139             iObserver.HandleDeviceDiscoveryComplete(KErrNone);
00140             }
00141         else
00142             {
00143             iObserver.HandleDeviceDiscoveryComplete(KErrNotFound);
00144             }
00145         }
00146     else 
00147         {
00148         // new device data entry
00149         TDeviceData *devData = new (ELeave) TDeviceData();
00150         devData->iDeviceName = iEntry().iName;
00151         devData->iDeviceAddr = 
00152             static_cast<TBTSockAddr>(iEntry().iAddr).BTAddr();
00153         devData->iDeviceServicePort = 0;
00154         // add device data entry
00155         iDevDataList->Append(devData);
00156         
00157         iObserver.DeviceDiscovered(*devData);
00158 
00159         // get next discovered device
00160         iResolver.Next(iEntry, iStatus);
00161         // wait for resolver to complete
00162         SetActive();
00163         }
00164     }
00165 
00166 void CDeviceDiscoverer::DoCancel()
00167     {
00168     iResolver.Cancel();
00169     }
00170 
00171 // ----------------------------------------------------------------------------
00172 // CDeviceDiscoverer::HasDevices()
00173 //
00174 // returns true if any devices were discovered
00175 // ----------------------------------------------------------------------------
00176 TBool CDeviceDiscoverer::HasDevices()
00177     {
00178     TBool exists = EFalse;
00179     
00180     if (iDevDataList)
00181         {
00182         if (iDevDataList->Count() > 0)
00183             {
00184             exists = ETrue;
00185             }
00186         }
00187     return exists;
00188     }
00189 
00190 // ----------------------------------------------------------------------------
00191 // CBluetoothPMPExampleEngine::SetLIAC
00192 // ----------------------------------------------------------------------------
00193 #ifdef ENABLE_LIAC
00194 void CDeviceDiscoverer::SetLIAC( TBool aState )
00195     {
00196     iLIAC = aState;
00197     }
00198 #endif
00199 
00200 // ----------------------------------------------------------------------------
00201 // CBluetoothPMPExampleEngine::StopDiscovery
00202 // ----------------------------------------------------------------------------
00203 void CDeviceDiscoverer::StopDiscovery()
00204     {
00205     Cancel();
00206 
00207     if (HasDevices())
00208         {
00209         iObserver.HandleDeviceDiscoveryComplete(KErrNone);
00210         }
00211     else
00212         {
00213         iObserver.HandleDeviceDiscoveryComplete(KErrNotFound);
00214         }
00215     }

Generated by  doxygen 1.6.2