ncdengine/debuglogger/obex/src/btservicesearcher.cpp
changeset 0 ba25891c3a9e
child 25 7333d7932ef7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "BTServiceSearcher.h"
       
    20 #include "BTServiceSearcher.pan"
       
    21 
       
    22 
       
    23 CBTServiceSearcher::CBTServiceSearcher()
       
    24 :  iIsDeviceSelectorConnected(EFalse)   
       
    25     {
       
    26     }
       
    27 
       
    28 CBTServiceSearcher::~CBTServiceSearcher()
       
    29 {
       
    30     if (iIsDeviceSelectorConnected)
       
    31 	{
       
    32         iDeviceSelector.CancelNotifier(KDeviceSelectionNotifierUid);
       
    33         iDeviceSelector.Close();
       
    34 	}
       
    35     delete iSdpSearchPattern;
       
    36     iSdpSearchPattern = NULL;
       
    37 	
       
    38     delete iAgent;
       
    39     iAgent = NULL;
       
    40     
       
    41 	// An outstanding FindServiceL request must be completed upon object delete
       
    42 	if (iStatusObserver) {
       
    43 		User::RequestComplete(iStatusObserver, KErrNone); // everything ~OK
       
    44 	}
       
    45 }
       
    46 
       
    47 void CBTServiceSearcher::Cancel() {
       
    48 
       
    49     if (iIsDeviceSelectorConnected)
       
    50 	{
       
    51         iDeviceSelector.CancelNotifier(KDeviceSelectionNotifierUid);
       
    52     
       
    53 	}
       
    54     delete iSdpSearchPattern;
       
    55     iSdpSearchPattern = NULL;
       
    56 	
       
    57     delete iAgent;
       
    58     iAgent = NULL;
       
    59     
       
    60 	// An outstanding FindServiceL request must be completed upon object delete
       
    61 	if (iStatusObserver) {
       
    62 		User::RequestComplete(iStatusObserver, KErrCancel); // everything ~OK
       
    63 	}
       
    64 
       
    65 
       
    66 
       
    67 }
       
    68 
       
    69 void CBTServiceSearcher::SelectDeviceByDiscoveryL(TRequestStatus& aObserverRequestStatus)
       
    70     {
       
    71     if (!iIsDeviceSelectorConnected)
       
    72         {
       
    73         User::LeaveIfError(iDeviceSelector.Connect());
       
    74         iIsDeviceSelectorConnected = ETrue;
       
    75         }
       
    76 
       
    77     //  Request a device selection 
       
    78     TBTDeviceSelectionParamsPckg selectionFilter;
       
    79     selectionFilter().SetUUID(ServiceClass());
       
    80 
       
    81     iDeviceSelector.StartNotifierAndGetResponse(
       
    82         aObserverRequestStatus, 
       
    83         KDeviceSelectionNotifierUid, 
       
    84         selectionFilter, 
       
    85         iResponse);
       
    86     }
       
    87 
       
    88 
       
    89 void CBTServiceSearcher::FindServiceL(TRequestStatus& aObserverRequestStatus)
       
    90     {
       
    91     if (!iResponse().IsValidBDAddr())
       
    92         {
       
    93         User::Leave(KErrNotFound);
       
    94         }
       
    95     iHasFoundService = EFalse;
       
    96 
       
    97     // delete any existing agent and search pattern
       
    98     delete iSdpSearchPattern;
       
    99     iSdpSearchPattern = NULL;
       
   100 
       
   101     delete iAgent;
       
   102     iAgent = NULL;
       
   103 
       
   104     iAgent = CSdpAgent::NewL(*this, BTDevAddr());
       
   105 
       
   106     iSdpSearchPattern = CSdpSearchPattern::NewL();
       
   107    
       
   108     iSdpSearchPattern->AddL(ServiceClass()); 
       
   109     // return code is the position in the list that the UUID is inserted at 
       
   110     // and is intentionally ignored
       
   111   
       
   112     iAgent->SetRecordFilterL(*iSdpSearchPattern);
       
   113 
       
   114     iStatusObserver = &aObserverRequestStatus;
       
   115 
       
   116     iAgent->NextRecordRequestL();
       
   117     }
       
   118 
       
   119 void CBTServiceSearcher::NextRecordRequestComplete(
       
   120     TInt aError, 
       
   121     TSdpServRecordHandle aHandle, 
       
   122     TInt aTotalRecordsCount)
       
   123     {
       
   124     TRAPD(error,
       
   125         NextRecordRequestCompleteL(aError, aHandle, aTotalRecordsCount);
       
   126     );
       
   127 
       
   128     if (error != KErrNone) 
       
   129         {
       
   130         Panic(EBTServiceSearcherNextRecordRequestComplete);
       
   131         }
       
   132     }
       
   133 
       
   134 void CBTServiceSearcher::NextRecordRequestCompleteL(
       
   135     TInt aError, 
       
   136     TSdpServRecordHandle aHandle, 
       
   137     TInt aTotalRecordsCount
       
   138 )
       
   139     {
       
   140     if (aError == KErrEof)
       
   141         {
       
   142         Finished();
       
   143         return;
       
   144         }
       
   145 
       
   146     if (aError != KErrNone)
       
   147         {        
       
   148         Finished(aError);
       
   149         return;
       
   150         }
       
   151 
       
   152     if (aTotalRecordsCount == 0)
       
   153         {        
       
   154         Finished(KErrNotFound);
       
   155         return;
       
   156         }
       
   157 
       
   158     //  Request its attributes
       
   159     iAgent->AttributeRequestL(aHandle, KSdpAttrIdProtocolDescriptorList);
       
   160     }
       
   161 
       
   162 void CBTServiceSearcher::AttributeRequestResult(
       
   163     TSdpServRecordHandle aHandle, 
       
   164     TSdpAttributeID aAttrID, 
       
   165     CSdpAttrValue* aAttrValue
       
   166 )
       
   167     {
       
   168     TRAPD(error,
       
   169         AttributeRequestResultL(aHandle, aAttrID, aAttrValue);
       
   170     );
       
   171     if (error != KErrNone)
       
   172         {
       
   173         Panic(EBTServiceSearcherAttributeRequestResult);
       
   174         }
       
   175 
       
   176     // Ownership has been transferred
       
   177     delete aAttrValue;
       
   178     }
       
   179 
       
   180 void CBTServiceSearcher::AttributeRequestResultL(
       
   181     TSdpServRecordHandle /*aHandle*/, 
       
   182     TSdpAttributeID aAttrID, 
       
   183     CSdpAttrValue* aAttrValue
       
   184 )
       
   185     {
       
   186     __ASSERT_ALWAYS(aAttrID == KSdpAttrIdProtocolDescriptorList, User::Leave( KErrNotFound ));
       
   187 
       
   188     TSdpAttributeParser parser(ProtocolList(), *this);
       
   189 
       
   190     // Validate the attribute value, and extract the RFCOMM channel
       
   191     aAttrValue->AcceptVisitorL(parser);
       
   192 
       
   193     if (parser.HasFinished())
       
   194         {
       
   195         // Found a suitable record so change state
       
   196         iHasFoundService = ETrue;
       
   197         }
       
   198     }
       
   199 
       
   200 void CBTServiceSearcher::AttributeRequestComplete(TSdpServRecordHandle aHandle, TInt aError)
       
   201     {
       
   202     TRAPD(error,
       
   203         AttributeRequestCompleteL(aHandle, aError);
       
   204     );
       
   205     if (error != KErrNone)
       
   206         {
       
   207         Panic(EBTServiceSearcherAttributeRequestComplete);
       
   208         }
       
   209     }
       
   210 
       
   211 void CBTServiceSearcher::AttributeRequestCompleteL(TSdpServRecordHandle /*aHandle*/, TInt aError)
       
   212     {
       
   213     if (aError != KErrNone)
       
   214         {        
       
   215         }
       
   216     else if (!HasFinishedSearching())
       
   217         {
       
   218         // have not found a suitable record so request another
       
   219         iAgent->NextRecordRequestL();
       
   220         }
       
   221     else
       
   222         {        
       
   223         Finished();
       
   224         }
       
   225     }
       
   226 
       
   227 void CBTServiceSearcher::Finished(TInt aError /* default = KErrNone */)
       
   228     {
       
   229     if (aError == KErrNone && !HasFoundService())
       
   230         {
       
   231         aError = KErrNotFound;
       
   232         }
       
   233     User::RequestComplete(iStatusObserver, aError);
       
   234     }
       
   235 
       
   236 TBool CBTServiceSearcher::HasFinishedSearching() const
       
   237     {
       
   238     return EFalse;
       
   239     }
       
   240 
       
   241 const TBTDevAddr& CBTServiceSearcher::BTDevAddr()
       
   242     {
       
   243     return iResponse().BDAddr();
       
   244     }
       
   245 
       
   246 const TBTDeviceResponseParams& CBTServiceSearcher::ResponseParams()
       
   247     {
       
   248     return iResponse();
       
   249     }
       
   250 
       
   251 TBool CBTServiceSearcher::HasFoundService() const
       
   252     {
       
   253     return iHasFoundService;
       
   254     }