bluetoothengine/headsetsimulator/remotecontroller/src/bttools/hsrcdevicediscoverer.cpp
author michal.sulewski
Wed, 15 Sep 2010 15:59:44 +0200
branchheadsetsimulator
changeset 60 90dbfc0435e3
permissions -rw-r--r--
source code commit

/*
 * Component Name: Headset Simulator
 * Author: Comarch S.A.
 * Version: 1.0
 * Copyright (c) 2010 Comarch S.A.
 *  
 * This Software is submitted by Comarch S.A. to Symbian Foundation Limited on 
 * the basis of the Member Contribution Agreement entered between Comarch S.A. 
 * and Symbian Foundation Limited on 5th June 2009 (“Agreement”) and may be 
 * used only in accordance with the terms and conditions of the Agreement. 
 * Any other usage, duplication or redistribution of this Software is not 
 * allowed without written permission of Comarch S.A.
 * 
 */

#include "hsrcbttools.h"
#include "debug.h"

CHsRCDeviceDiscoverer* CHsRCDeviceDiscoverer::NewL(
        MHsRCDeviceDiscovererObserver* aDeviceObserver, RSocketServ& aSServ )
    {
    CHsRCDeviceDiscoverer *self = CHsRCDeviceDiscoverer::NewLC(
            aDeviceObserver, aSServ );
    CleanupStack::Pop( self );
    return self;
    }

CHsRCDeviceDiscoverer* CHsRCDeviceDiscoverer::NewLC(
        MHsRCDeviceDiscovererObserver* aDeviceObserver, RSocketServ& aSServ )
    {
    CHsRCDeviceDiscoverer *self = new ( ELeave ) CHsRCDeviceDiscoverer(
            aDeviceObserver, aSServ );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

CHsRCDeviceDiscoverer::~CHsRCDeviceDiscoverer()
    {
    TRACE_FUNC_ENTRY
    Cancel();
    iHRes.Close();
    TRACE_FUNC_EXIT
    }

void CHsRCDeviceDiscoverer::DeviceSearchL()
    {
    TRACE_FUNC_ENTRY

    Cancel();
    ConnectHostResolverL();
    iSearching = ETrue;
    iHRes.GetByAddress( iSockAddr, iEntry, iStatus );
    SetActive();

    TRACE_FUNC_EXIT
    }

TBTDevAddr CHsRCDeviceDiscoverer::GetBTDevAddress()
    {
    return iDevAddr;
    }

CHsRCDeviceDiscoverer::CHsRCDeviceDiscoverer(
        MHsRCDeviceDiscovererObserver* aDeviceObserver, RSocketServ& aSServ ) :
    CActive( EPriorityNormal ), iSServ( aSServ ), iDeviceObserver(
            aDeviceObserver )
    {
    CActiveScheduler::Add( this );
    }

void CHsRCDeviceDiscoverer::ConstructL()
    {
    }

void CHsRCDeviceDiscoverer::ConnectHostResolverL()
    {

    TRACE_FUNC_ENTRY
    TProtocolDesc protDesc;
    _LIT(KHsProtocolName,"BTLinkManager");
    TProtocolName protName( KHsProtocolName );

    User::LeaveIfError( iSServ.FindProtocol( protName, protDesc ) );
    User::LeaveIfError( iHRes.Open( iSServ, protDesc.iAddrFamily,
            protDesc.iProtocol ) );
    iSockAddr.SetIAC( KGIAC );
    iSockAddr.SetAction( KHostResInquiry | KHostResName );

    TRACE_FUNC_EXIT

    }

void CHsRCDeviceDiscoverer::RunL()
    {
    TRACE_FUNC_ENTRY
    TRACE_ERROR((_L("iStatus value = %d"), iStatus.Int()))

    if ( iStatus.Int() == KErrNone )
        {

        //dev found
        TInquirySockAddr& sockAddr = TInquirySockAddr::Cast( iEntry().iAddr );

        TBuf <KDevAddrLength> devAddrBuf;
        TBuf <KMaxBluetoothNameLen> devNameBuf;

        sockAddr.BTAddr().GetReadable( devAddrBuf );
        devNameBuf.Copy( iEntry().iName );

        TBuf8 <KDevAddrLength> addrBuf8;
        TBuf8 <KMaxBluetoothNameLen> nameBuf8;

        addrBuf8.Copy( devAddrBuf );
        nameBuf8.Copy( devNameBuf );

        if ( iDeviceObserver )
            {
            iDeviceObserver->HandleDeviceFindSuccessL( addrBuf8, nameBuf8,
                    sockAddr.MajorServiceClass(),
                    sockAddr.MajorClassOfDevice(),
                    sockAddr.MinorClassOfDevice() );
            }

        // Get next device
        iHRes.Next( iEntry, iStatus );
        SetActive();
        }
    else
        {
        //Inquiry finished or error
        Cancel();
        iHRes.Close();
        iSearching = EFalse;

        if ( iDeviceObserver )
            {
            iDeviceObserver->HandleDeviceFindFailed( iStatus.Int() );
            }
        }
    TRACE_FUNC_EXIT

    }

void CHsRCDeviceDiscoverer::DoCancel()
    {
    TRACE_FUNC_ENTRY

    if ( iSearching )
        {
        iHRes.Cancel();
        iHRes.Close();
        }

    TRACE_FUNC_EXIT

    }