bluetoothengine/btnotif/src/BTNInquiAO.cpp
changeset 42 b72428996822
parent 32 19bd632b5100
child 43 7d241e669870
equal deleted inserted replaced
32:19bd632b5100 42:b72428996822
     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:  Part of CBTInqUI class: use Active object to search BT devices.
       
    15 *
       
    16 */
       
    17 #include "btninqui.h"
       
    18 #include "btNotifDebug.h"
       
    19 
       
    20 /**  Identification for active object request */
       
    21 const TInt KBTNotifInquiryNotifierReq = 20;
       
    22 
       
    23 // ----------------------------------------------------------
       
    24 // CBTInqUI::PrepareScannerL
       
    25 // ----------------------------------------------------------
       
    26 //
       
    27 void CBTInqUI::CreateScannerL()
       
    28     {
       
    29     FLOG(_L("[BTNOTIF]\t CBTInqUI::CreateScannerL()"));
       
    30     
       
    31     __ASSERT_DEBUG( iScanner == NULL, User::Panic(_L("BTNotifInqUI - iScanner not released!"),KErrAlreadyExists));
       
    32         
       
    33     // Create scanner object which will do the actual search
       
    34     iScanner = CBTNotifActive::NewL( this, KBTNotifInquiryNotifierReq, CActive::EPriorityStandard );
       
    35     User::LeaveIfError( iSocketServer.Connect() );
       
    36     TProtocolDesc pInfo;
       
    37     User::LeaveIfError( iSocketServer.FindProtocol( _L("BTLinkManager"), pInfo));
       
    38     User::LeaveIfError( iHostResolver.Open(iSocketServer, pInfo.iAddrFamily, pInfo.iProtocol));
       
    39     IssueRequestL();
       
    40 
       
    41     FLOG(_L("[BTNOTIF]\t CBTInqUI::CreateScannerL() completed"));
       
    42     }
       
    43 
       
    44 // ----------------------------------------------------------
       
    45 // CBTInqUI::IssueRequestL
       
    46 // Issue Bluetooth device inquiry/search itself.
       
    47 // Reads also inquiry mode from shared data.
       
    48 // ----------------------------------------------------------
       
    49 //
       
    50 void CBTInqUI::IssueRequestL()
       
    51     {
       
    52     FLOG(_L("[BTNOTIF]\t CBTInqUI::IssueRequestL()"));
       
    53     
       
    54     TUint action = 0; 
       
    55     if( !iPageForName )
       
    56         {
       
    57         action = KHostResInquiry + KHostResEir + KHostResIgnoreCache;
       
    58         }
       
    59     else
       
    60         {
       
    61         if( iIndex < iDevsWithoutName.Count() )
       
    62             {
       
    63             action = KHostResName + KHostResIgnoreCache;
       
    64             TInquirySockAddr& sa = TInquirySockAddr::Cast( iDevsWithoutName[iIndex].iAddr );
       
    65             iInquirySockAddr.SetBTAddr( sa.BTAddr() );            
       
    66             }
       
    67         }
       
    68     if (action)
       
    69         {
       
    70         iInquirySockAddr.SetAction( action ); 
       
    71         iInquirySockAddr.SetIAC(KGIAC);
       
    72         iHostResolver.GetByAddress( iInquirySockAddr, iEntry, iScanner->RequestStatus() );
       
    73         iScanner->GoActive();
       
    74         }
       
    75     else
       
    76         {
       
    77         FLOG(_L("[BTNOTIF]\t CBTInqUI::RunL() All name inquiries complete ."));
       
    78         iPageForName = EFalse;
       
    79         iIndex = 0;
       
    80         InquiryComplete( KErrNone );
       
    81         }
       
    82     FLOG(_L("[BTNOTIF]\t CBTInqUI::IssueRequestL() completed"));
       
    83     }
       
    84 
       
    85 // ----------------------------------------------------------
       
    86 // CBTInqUI::RequestCompletedL
       
    87 // BT-device or timeout has been received. Inform caller for
       
    88 // received device and issue next EIR/Name request.
       
    89 // ----------------------------------------------------------
       
    90 //
       
    91 void CBTInqUI::RequestCompletedL( CBTNotifActive* aActive, TInt aId, TInt aStatus )
       
    92     {
       
    93     FTRACE(FPrint(_L("[BTNOTIF]\t CBTInqUI::RequestCompletedL() status: %d >>"), aStatus ));
       
    94     ASSERT( aId == KBTNotifInquiryNotifierReq);
       
    95     (void) aActive;
       
    96     
       
    97     if( aStatus == KErrNone )
       
    98         {
       
    99         if( iPageForName )
       
   100             {
       
   101             HandleFoundNameL();
       
   102             iIndex++;
       
   103             IssueRequestL();
       
   104             }
       
   105         else
       
   106             {
       
   107             HandleInquiryDeviceL();
       
   108             iHostResolver.Next( iEntry, iScanner->RequestStatus() );
       
   109             iScanner->GoActive();
       
   110             }
       
   111         }
       
   112     else if( aStatus == (KHCIErrorBase - EPageTimedOut) && iPageForName )
       
   113         {
       
   114         FLOG(_L("[BTNOTIF]\t CBTInqUI::RequestCompletedL() HCI:EPageTimeOut, page next one."));
       
   115         iIndex++;
       
   116         IssueRequestL();
       
   117         }
       
   118     else
       
   119         {
       
   120         if( !iPageForName && iDevsWithoutName.Count()>0 )
       
   121             {
       
   122             FTRACE(FPrint(_L("[BTNOTIF]\t CBTInqUI::RequestCompletedL() nameless devices %d, paging for name."), 
       
   123                     iDevsWithoutName.Count() ));
       
   124             iIndex = 0;
       
   125             iPageForName = ETrue;
       
   126             IssueRequestL();
       
   127             }
       
   128         else
       
   129             {
       
   130             FLOG(_L("[BTNOTIF]\t CBTInqUI::RequestCompletedL() NameInquiryComplete with error or no device found."));
       
   131             HandleError( aActive, aId, aStatus );
       
   132             }
       
   133         }
       
   134     FLOG(_L("[BTNOTIF]\t CBTInqUI::RequestCompletedL() <<"));
       
   135     }
       
   136 
       
   137 // ----------------------------------------------------------
       
   138 // CBTInqUI::HandleError
       
   139 // Inform UI from error occured.
       
   140 // ----------------------------------------------------------
       
   141 //
       
   142 void CBTInqUI::HandleError( CBTNotifActive* aActive, TInt aId, TInt aError )
       
   143     {
       
   144     FLOG(_L("[BTNOTIF]\t CBTInqUI::HandleError()"));
       
   145     (void) aActive;
       
   146     (void) aId;
       
   147     iPageForName = EFalse;
       
   148     InquiryComplete( aError );
       
   149     FLOG(_L("[BTNOTIF]\t CBTInqUI::HandleError() completed"));
       
   150     }
       
   151 
       
   152 // ----------------------------------------------------------
       
   153 // CBTInqUI::DoCancelRequest
       
   154 // ----------------------------------------------------------
       
   155 //
       
   156 void CBTInqUI::DoCancelRequest( CBTNotifActive* aActive, TInt aId )
       
   157     {
       
   158     (void) aActive;
       
   159     (void) aId;
       
   160     iHostResolver.Cancel();
       
   161     }
       
   162 	
       
   163 // ----------------------------------------------------------
       
   164 // CBTInqUI::HandleInquiryDeviceL
       
   165 // Inform of properties of the found BTdevice,   
       
   166 // which passes the search filter. Its name would be retrived 
       
   167 // later if not contained by the first round of inquiry.   
       
   168 // ----------------------------------------------------------
       
   169 //
       
   170 void CBTInqUI::HandleInquiryDeviceL()
       
   171     {
       
   172     TInquirySockAddr& sa = TInquirySockAddr::Cast( iEntry().iAddr );
       
   173     
       
   174     if ( iDesiredDeviceClass.DeviceClass() == 0 || sa.MajorClassOfDevice() == iDesiredDeviceClass.MajorDeviceClass() )
       
   175         {
       
   176         FLOG(_L("[BTNOTIF]\t CBTInqUI::HandleInquiryDeviceL() The found device passes the search filter."));
       
   177 #ifdef _DEBUG
       
   178     TBuf<12> devAddrString;
       
   179     sa.BTAddr().GetReadable(devAddrString);
       
   180     FTRACE(FPrint(_L("[BTNOTIF]\t BT Address: %S"), &devAddrString));
       
   181 #endif
       
   182         TBTDeviceName devName;
       
   183         TBool nameGotten = CheckEirDeviceName( iEntry, devName );
       
   184         FTRACE(FPrint(_L("[BTNOTIF]\t CBTInqUI::HandleInquiryDeviceL() EIR device name? %d, %S"), nameGotten, &devName ));
       
   185         if( nameGotten )
       
   186             {
       
   187             DeviceAvailableL( iEntry(), devName );
       
   188             }
       
   189         else
       
   190             {
       
   191             iDevsWithoutName.Append( iEntry() );
       
   192             }
       
   193         }
       
   194     }
       
   195 
       
   196 // ----------------------------------------------------------
       
   197 // CBTInqUI::HandleFoundNameL
       
   198 // Inform of retrieved device name after 2nd inquiry.  
       
   199 // ----------------------------------------------------------
       
   200 //
       
   201 void CBTInqUI::HandleFoundNameL()
       
   202     {
       
   203 #ifdef _DEBUG
       
   204     TBuf<12> devAddrString;
       
   205     TInquirySockAddr& sa = TInquirySockAddr::Cast( iEntry().iAddr );
       
   206     sa.BTAddr().GetReadable(devAddrString);
       
   207     FTRACE(FPrint(_L("[BTNOTIF]\t CBTInqUI::HandleFoundNameL() BT Address: %S"), &devAddrString));
       
   208 #endif
       
   209     if( iEntry().iName != KNullDesC )
       
   210         {
       
   211         FTRACE(FPrint(_L("[BTNOTIF]\t CBTInqUI::HandleFoundNameL() Name found: %S"), &(iEntry().iName) ));
       
   212 
       
   213         DeviceAvailableL( iDevsWithoutName[iIndex], iEntry().iName );
       
   214         }
       
   215     
       
   216     FLOG(_L("[BTNOTIF]\t CBTInqUI::HandleFoundNameL() Complete"));
       
   217     }
       
   218 
       
   219 // ----------------------------------------------------------
       
   220 // CBTInqUI::CheckEirDeviceName
       
   221 // Check if the retrieved the device info contains device name.  
       
   222 // ----------------------------------------------------------
       
   223 //
       
   224 TBool CBTInqUI::CheckEirDeviceName( TNameEntry& aEntry, TBTDeviceName& aName )
       
   225     {
       
   226     TBluetoothNameRecordWrapper eir( aEntry() );
       
   227     TInt length = eir.GetDeviceNameLength();
       
   228     
       
   229     TBool isComplete( EFalse );
       
   230     TInt err( KErrNone );
       
   231         
       
   232     if( length > 0 )
       
   233         {            
       
   234         err = eir.GetDeviceName( aName, isComplete);
       
   235         return (!err);
       
   236         }
       
   237     return EFalse;
       
   238     }
       
   239 // End of File