htiui/HtiCommPlugins/HtiBtCommPlugin/BtEngine/src/btservicesearcher.cpp
changeset 0 d6fe6244b863
child 3 2703485a934c
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Bluetooth service searcher.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "BTServiceSearcher.h"
       
    21 #include "BTServiceSearcher.pan"
       
    22 
       
    23 #include "HtiBtEngineLogging.h"
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ==============================
       
    26 
       
    27 // ----------------------------------------------------------------------------
       
    28 // CBTServiceSearcher::CBTServiceSearcher()
       
    29 // Constructor.
       
    30 // ----------------------------------------------------------------------------
       
    31 //
       
    32 CBTServiceSearcher::CBTServiceSearcher()
       
    33 :  iIsDeviceSelectorConnected( EFalse )
       
    34     {
       
    35     }
       
    36 
       
    37 // ----------------------------------------------------------------------------
       
    38 // CBTServiceSearcher::~CBTServiceSearcher()
       
    39 // Destructor.
       
    40 // ----------------------------------------------------------------------------
       
    41 //
       
    42 CBTServiceSearcher::~CBTServiceSearcher()
       
    43     {
       
    44     if ( iIsDeviceSelectorConnected )
       
    45         {
       
    46         iDeviceSelector.CancelNotifier( KDeviceSelectionNotifierUid );
       
    47         iDeviceSelector.Close();
       
    48         }
       
    49     delete iSdpSearchPattern;
       
    50     iSdpSearchPattern = NULL;
       
    51 
       
    52     delete iAgent;
       
    53     iAgent = NULL;
       
    54     }
       
    55 
       
    56 // ----------------------------------------------------------------------------
       
    57 // CBTServiceSearcher::SelectDeviceByDiscoveryL()
       
    58 // Select a device.
       
    59 // ----------------------------------------------------------------------------
       
    60 //
       
    61 void CBTServiceSearcher::SelectDeviceByDiscoveryL(
       
    62         TRequestStatus& aObserverRequestStatus )
       
    63     {
       
    64     if ( ! iIsDeviceSelectorConnected )
       
    65         {
       
    66         User::LeaveIfError( iDeviceSelector.Connect() );
       
    67         iIsDeviceSelectorConnected = ETrue;
       
    68         }
       
    69 
       
    70     iSelectionFilter().SetUUID( ServiceClass() );
       
    71 
       
    72     iDeviceSelector.StartNotifierAndGetResponse(
       
    73         aObserverRequestStatus,
       
    74         KDeviceSelectionNotifierUid,
       
    75         iSelectionFilter,
       
    76         iResponse );
       
    77     }
       
    78 
       
    79 // ----------------------------------------------------------------------------
       
    80 // CBTServiceSearcher::SelectDeviceByNameL()
       
    81 // Select a device by BT device name.
       
    82 // ----------------------------------------------------------------------------
       
    83 //
       
    84 void CBTServiceSearcher::SelectDeviceByNameL( const TDesC& aDeviceName,
       
    85         TRequestStatus& aObserverRequestStatus )
       
    86     {
       
    87     LOG_D( "CBTServiceSearcher::SelectDeviceByNameL" );
       
    88 
       
    89     RSocketServ socketServ;
       
    90     User::LeaveIfError( socketServ.Connect() );
       
    91     CleanupClosePushL( socketServ );
       
    92 
       
    93     TProtocolDesc pInfo;
       
    94     _LIT( KL2Cap, "BTLinkManager" );
       
    95     User::LeaveIfError(
       
    96         socketServ.FindProtocol( TProtocolName( KL2Cap ), pInfo ) );
       
    97 
       
    98     LOG_D( "CBTServiceSearcher: Found protocol" );
       
    99 
       
   100     RHostResolver hr;
       
   101     User::LeaveIfError( hr.Open( socketServ,
       
   102                             pInfo.iAddrFamily, pInfo.iProtocol ) );
       
   103     CleanupClosePushL( hr );
       
   104 
       
   105     LOG_D( "CBTServiceSearcher: HostResolver open" );
       
   106 
       
   107     TInquirySockAddr addr;
       
   108     TNameEntry entry;
       
   109     addr.SetIAC( KGIAC );
       
   110     addr.SetAction( KHostResName | KHostResInquiry );
       
   111 
       
   112     User::LeaveIfError( hr.GetByAddress( addr, entry ) );
       
   113     LOG_D( "CBTServiceSearcher: GetByAddress ok" );
       
   114     TBool isFound = EFalse;
       
   115     TInt err = KErrNone;
       
   116     while ( !isFound && !err )
       
   117         {
       
   118         if ( !aDeviceName.CompareF( entry().iName ) )
       
   119             {
       
   120             LOG_D( "CBTServiceSearcher: Name match" );
       
   121             isFound = ETrue;
       
   122             }
       
   123         else
       
   124             {
       
   125             LOG_D( "CBTServiceSearcher: Not match - getting next" );
       
   126             err = hr.Next( entry );
       
   127             }
       
   128         }
       
   129     CleanupStack::PopAndDestroy( 2 ); // hr, socketServ
       
   130 
       
   131     iStatusObserver = &aObserverRequestStatus;
       
   132 
       
   133     if ( isFound )
       
   134         {
       
   135         TInquirySockAddr& sa = TInquirySockAddr::Cast( entry().iAddr );
       
   136         iResponse().SetDeviceAddress( sa.BTAddr() );
       
   137         iResponse().SetDeviceName( entry().iName );
       
   138         TBTDeviceClass deviceClass( sa.MajorServiceClass(),
       
   139                                     sa.MajorClassOfDevice(),
       
   140                                     sa.MinorClassOfDevice() );
       
   141         iResponse().SetDeviceClass( deviceClass );
       
   142         User::RequestComplete( iStatusObserver, KErrNone );
       
   143         }
       
   144 
       
   145     else
       
   146         {
       
   147         LOG_E( "CBTServiceSearcher: Device not found! Can't connect!" );
       
   148         User::RequestComplete( iStatusObserver, err );
       
   149         }
       
   150     }
       
   151 
       
   152 // ----------------------------------------------------------------------------
       
   153 // CBTServiceSearcher::FindServiceL()
       
   154 // Find a service on the specified device.
       
   155 // ----------------------------------------------------------------------------
       
   156 //
       
   157 void CBTServiceSearcher::FindServiceL( const TBTDevAddr& aDeviceAddress,
       
   158     TRequestStatus& aObserverRequestStatus )
       
   159     {
       
   160     iResponse().SetDeviceAddress( aDeviceAddress );
       
   161     if ( !iResponse().IsValidBDAddr() )
       
   162         {
       
   163         User::Leave( KErrArgument );
       
   164         }
       
   165     iHasFoundService = EFalse;
       
   166 
       
   167     // delete any existing agent and search pattern
       
   168     delete iSdpSearchPattern;
       
   169     iSdpSearchPattern = NULL;
       
   170 
       
   171     delete iAgent;
       
   172     iAgent = NULL;
       
   173 
       
   174     iAgent = CSdpAgent::NewL(*this, BTDevAddr());
       
   175 
       
   176     iSdpSearchPattern = CSdpSearchPattern::NewL();
       
   177 
       
   178     iSdpSearchPattern->AddL( ServiceClass() );
       
   179     // return code is the position in the list that the UUID is inserted at
       
   180     // and is intentionally ignored
       
   181 
       
   182     iAgent->SetRecordFilterL( *iSdpSearchPattern );
       
   183 
       
   184     iStatusObserver = &aObserverRequestStatus;
       
   185 
       
   186     iAgent->NextRecordRequestL();
       
   187     }
       
   188 
       
   189 // ----------------------------------------------------------------------------
       
   190 // CBTServiceSearcher::NextRecordRequestComplete()
       
   191 // Process the result of the next record request.
       
   192 // ----------------------------------------------------------------------------
       
   193 //
       
   194 void CBTServiceSearcher::NextRecordRequestComplete(
       
   195     TInt aError,
       
   196     TSdpServRecordHandle aHandle,
       
   197     TInt aTotalRecordsCount)
       
   198     {
       
   199     TRAPD( error,
       
   200            NextRecordRequestCompleteL( aError, aHandle, aTotalRecordsCount );
       
   201     );
       
   202 
       
   203     if ( error != KErrNone )
       
   204         {
       
   205         Panic( EBTServiceSearcherNextRecordRequestComplete );
       
   206         }
       
   207     }
       
   208 
       
   209 // ----------------------------------------------------------------------------
       
   210 // CBTServiceSearcher::NextRecordRequestCompleteL()
       
   211 // Process the result of the next record request.
       
   212 // ----------------------------------------------------------------------------
       
   213 //
       
   214 void CBTServiceSearcher::NextRecordRequestCompleteL(
       
   215     TInt aError,
       
   216     TSdpServRecordHandle aHandle,
       
   217     TInt aTotalRecordsCount )
       
   218     {
       
   219     if ( aError == KErrEof )
       
   220         {
       
   221         Finished();
       
   222         return;
       
   223         }
       
   224 
       
   225     if ( aError != KErrNone )
       
   226         {
       
   227         LOGFMT_E("CBTServiceSearcher: NextRecordRequestCompleteL: %d", aError );
       
   228         Finished( aError );
       
   229         return;
       
   230         }
       
   231 
       
   232     if ( aTotalRecordsCount == 0 )
       
   233         {
       
   234         LOG_I("CBTServiceSearcher: NextRecordRequestCompleteL: No records found");
       
   235         Finished( KErrNotFound );
       
   236         return;
       
   237         }
       
   238 
       
   239     //  Request its attributes
       
   240     iAgent->AttributeRequestL( aHandle, KSdpAttrIdProtocolDescriptorList );
       
   241     }
       
   242 
       
   243 // ----------------------------------------------------------------------------
       
   244 // CBTServiceSearcher::AttributeRequestResult()
       
   245 // Process the next attribute requested.
       
   246 // ----------------------------------------------------------------------------
       
   247 //
       
   248 void CBTServiceSearcher::AttributeRequestResult(
       
   249     TSdpServRecordHandle aHandle,
       
   250     TSdpAttributeID aAttrID,
       
   251     CSdpAttrValue* aAttrValue )
       
   252     {
       
   253     TRAPD( error,
       
   254          AttributeRequestResultL( aHandle, aAttrID, aAttrValue );
       
   255          );
       
   256     if ( error != KErrNone )
       
   257         {
       
   258         Panic( EBTServiceSearcherAttributeRequestResult );
       
   259         }
       
   260 
       
   261     // Delete obsolete local atribute pointer.
       
   262     delete aAttrValue;
       
   263     }
       
   264 
       
   265 // ----------------------------------------------------------------------------
       
   266 // CBTServiceSearcher::AttributeRequestResultL()
       
   267 // Process the next attribute requested.
       
   268 // ----------------------------------------------------------------------------
       
   269 //
       
   270 void CBTServiceSearcher::AttributeRequestResultL(
       
   271     TSdpServRecordHandle /*aHandle*/,
       
   272     TSdpAttributeID aAttrID,
       
   273     CSdpAttrValue* aAttrValue )
       
   274     {
       
   275     __ASSERT_ALWAYS( aAttrID == KSdpAttrIdProtocolDescriptorList,
       
   276                      User::Leave( KErrNotFound ) );
       
   277 
       
   278     TSdpAttributeParser parser( ProtocolList(), *this );
       
   279 
       
   280     // Validate the attribute value, and extract the RFCOMM channel
       
   281     aAttrValue->AcceptVisitorL( parser );
       
   282 
       
   283     if ( parser.HasFinished() )
       
   284         {
       
   285         // Found a suitable record so change state
       
   286         iHasFoundService = ETrue;
       
   287         }
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CBTServiceSearcher::AttributeRequestComplete()
       
   292 // Process the attribute request completion.
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 void CBTServiceSearcher::AttributeRequestComplete( TSdpServRecordHandle aHandle,
       
   296                                                    TInt aError )
       
   297     {
       
   298     TRAPD( error,
       
   299         AttributeRequestCompleteL( aHandle, aError );
       
   300     );
       
   301     if ( error != KErrNone )
       
   302         {
       
   303         Panic( EBTServiceSearcherAttributeRequestComplete );
       
   304         }
       
   305     }
       
   306 
       
   307 // ----------------------------------------------------------------------------
       
   308 // CBTServiceSearcher::AttributeRequestCompleteL()
       
   309 // Process the attribute request completion.
       
   310 // ----------------------------------------------------------------------------
       
   311 //
       
   312 void CBTServiceSearcher::AttributeRequestCompleteL( TSdpServRecordHandle
       
   313                                                     /*aHandle*/,
       
   314                                                     TInt aError )
       
   315     {
       
   316     if ( aError != KErrNone )
       
   317         {
       
   318         LOGFMT_W("CBTServiceSearcher::AttributeRequestCompleteL: %d", aError);
       
   319         }
       
   320     else
       
   321         {
       
   322         // done with attributes of this service record, request next
       
   323         iAgent->NextRecordRequestL();
       
   324         }
       
   325     }
       
   326 
       
   327 // ----------------------------------------------------------------------------
       
   328 // CBTServiceSearcher::Finished()
       
   329 // The search has finished and notify the observer
       
   330 // that the process is complete.
       
   331 // ----------------------------------------------------------------------------
       
   332 //
       
   333 void CBTServiceSearcher::Finished( TInt aError /* default = KErrNone */ )
       
   334     {
       
   335     if ( aError == KErrNone && !HasFoundService() )
       
   336         {
       
   337         aError = KErrNotFound;
       
   338         }
       
   339     User::RequestComplete( iStatusObserver, aError );
       
   340     }
       
   341 
       
   342 // ----------------------------------------------------------------------------
       
   343 // CBTServiceSearcher::HasFinishedSearching()
       
   344 // Is the instance still wanting to search.
       
   345 // ----------------------------------------------------------------------------
       
   346 //
       
   347 TBool CBTServiceSearcher::HasFinishedSearching() const
       
   348     {
       
   349     return EFalse;
       
   350     }
       
   351 
       
   352 // ----------------------------------------------------------------------------
       
   353 // CBTServiceSearcher::BTDevAddr()
       
   354 // Returns the bluetooth device address.
       
   355 // ----------------------------------------------------------------------------
       
   356 //
       
   357 const TBTDevAddr& CBTServiceSearcher::BTDevAddr()
       
   358     {
       
   359     return iResponse().BDAddr();
       
   360     }
       
   361 
       
   362 // ----------------------------------------------------------------------------
       
   363 // CBTServiceSearcher::ResponseParams()
       
   364 // Returns information about the device selected by the user.
       
   365 // ----------------------------------------------------------------------------
       
   366 //
       
   367 const TBTDeviceResponseParams& CBTServiceSearcher::ResponseParams()
       
   368     {
       
   369     return iResponse();
       
   370     }
       
   371 
       
   372 // ----------------------------------------------------------------------------
       
   373 // CBTServiceSearcher::HasFoundService()
       
   374 // True if a service has been found.
       
   375 // ----------------------------------------------------------------------------
       
   376 //
       
   377 TBool CBTServiceSearcher::HasFoundService() const
       
   378     {
       
   379     return iHasFoundService;
       
   380     }
       
   381 
       
   382 // End of File