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

/* 
 *
 * Copyright (c) <2010> Comarch S.A. and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of the License "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Original Contributors:
 * Comarch S.A. - original contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */
#include "hfpconnectionmanagement.h"
#include "hfpcommand.h"
#include "hfpsettings.h"
#include "debug.h"
_LIT8(KHsHFPParamValue0,"0");
_LIT8(KHsHFPParamValue1,"1");
_LIT8(KHsHFPParamValue3,"3");
_LIT8(KHsHFPParamValue16,"16");

CHsHFPConnectionManagement* CHsHFPConnectionManagement::NewL(
        MHsHFPFeatureProviderObserver* aObserver )
    {
    CHsHFPConnectionManagement* self = CHsHFPConnectionManagement::NewLC(
            aObserver );
    CleanupStack::Pop( self );
    return self;
    }

CHsHFPConnectionManagement* CHsHFPConnectionManagement::NewLC(
        MHsHFPFeatureProviderObserver* aObserver )
    {
    CHsHFPConnectionManagement* self =
            new ( ELeave ) CHsHFPConnectionManagement( aObserver );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

CHsHFPConnectionManagement::~CHsHFPConnectionManagement()
    {
    TRACE_FUNC_ENTRY
    if ( iSettings )
        {
        iSettings->Release();
        }
    TRACE_FUNC_EXIT
    }

TInt CHsHFPConnectionManagement::EstablishServiceLevelConnection(
        const CHsHFPCommand &aInputCmd, CHsHFPCommand &aOutputCmd )
    {
    TRACE_FUNC_ENTRY
    TInt res = KErrNone;
    switch ( iEstablishmentState )
        {
        case EHFPNotConnected:
            {
            if ( aInputCmd.Type() == EHFPCmdUnknown )//initialization
                {
                aOutputCmd.SetType( EHFPCmdBRSF );
                aOutputCmd.SetMode( ECmdModeWrite );
                RATParamArray params;
                params.Append( TATParam( KHsHFPParamValue16 ) );
                aOutputCmd.SetParams( params );
                iEstablishmentState = EHFPWaitingForBrsfAndOk;
                }
            else
                {
                res = KErrArgument;
                }
            break;
            }
        case EHFPWaitingForBrsfAndOk:
            {
            if ( aInputCmd.Type() == EHFPCmdBRSF )
                {
                iWaitingForOK = ETrue;
                }
            else if ( iWaitingForOK && aInputCmd.Type() == EHFPCmdOK )
                {
                iWaitingForOK = EFalse;
                aOutputCmd.SetType( EHFPCmdCIND );
                aOutputCmd.SetMode( ECmdModeTest );
                iEstablishmentState = EHFPWaitingForCindTestAndOk;
                }
            else
                {
                res = KErrArgument;
                }
            break;
            }
        case EHFPWaitingForCindTestAndOk:
            {
            if ( aInputCmd.Type() == EHFPCmdCIND )
                {
                TRAP( res, InitializeIndicatorsL( &aInputCmd ) );
                iWaitingForOK = ETrue;
                }
            else if ( iWaitingForOK && aInputCmd.Type() == EHFPCmdOK )
                {
                iWaitingForOK = EFalse;
                aOutputCmd.SetType( EHFPCmdCIND );
                aOutputCmd.SetMode( ECmdModeRead );
                iEstablishmentState = EHFPWaitingForCindReadAndOk;
                }
            else
                {
                res = KErrArgument;
                }
            break;
            }
        case EHFPWaitingForCindReadAndOk:
            {
            if ( aInputCmd.Type() == EHFPCmdCIND )
                {
                TRAP( res, SetupIndicatorsL( &aInputCmd ) );
                iWaitingForOK = ETrue;
                }
            else if ( iWaitingForOK && aInputCmd.Type() == EHFPCmdOK )
                {
                iWaitingForOK = EFalse;
                aOutputCmd.SetType( EHFPCmdCMER );
                aOutputCmd.SetMode( ECmdModeWrite );
                RATParamArray params;
                params.Append( TATParam( KHsHFPParamValue3 ) );
                params.Append( TATParam( KHsHFPParamValue0 ) );
                params.Append( TATParam( KHsHFPParamValue0 ) );
                params.Append( TATParam( KHsHFPParamValue1 ) );
                aOutputCmd.SetParams( params );
                iEstablishmentState = EHFPWaitingForOk;
                }
            else
                {
                res = KErrArgument;
                }
            break;
            }
        case EHFPWaitingForOk:
            {
            if ( aInputCmd.Type() == EHFPCmdOK )
                {
                iEstablishmentState = EHFPConnected;

                // Initialize volume level synchronization
                aOutputCmd.SetType( EHFPCmdVGS );
                aOutputCmd.SetMode( ECmdModeWrite );
                RATParamArray params;
                TBuf8 <KHsHFPMaxVolumeLevelParamSize> vgsLevel;
                vgsLevel.AppendNum( iSettings->iVgsLevel );
                params.Append( TATParam( vgsLevel ) );
                aOutputCmd.SetParams( params );

                iObserver->HandleProcedureCompleted( KErrNone );
                }
            else
                {
                res = KErrArgument;
                }
            break;
            }
        default:
            res = KErrArgument;
            break;
        }
    TRACE_FUNC_EXIT
    return res;
    }

void CHsHFPConnectionManagement::HandleServiceLevelConnectionRelease( TInt aErr )
    {
    TRACE_FUNC_ENTRY
    iEstablishmentState = EHFPNotConnected;
    iObserver->HandleProcedureCompleted( KErrNone );
    TRACE_FUNC_EXIT
    }

CHsHFPConnectionManagement::CHsHFPConnectionManagement(
        MHsHFPFeatureProviderObserver* aObserver ) :
    iObserver( aObserver )
    {
    }

void CHsHFPConnectionManagement::ConstructL()
    {
    TRACE_FUNC_ENTRY

    iSettings = CHsHFPSettings::InstanceL();
    TRACE_FUNC_EXIT
    }

void CHsHFPConnectionManagement::InitializeIndicatorsL(
        const CHsHFPCommand* aCommand )
    {
    TRACE_FUNC_ENTRY

    TInt params = aCommand->Params().Count();

    TChar end( '\"' );

    const TInt prefixLength = 2;
    const TInt suffixLength = 2;
    for ( TInt i = 0; i < params; i++ )
        {
        TInt pos = aCommand->Params()[i].Des().LocateReverse( end );

        if ( pos == KErrNotFound )
            {
            User::Leave( KErrArgument );
            }

        RBuf8 buf;
        buf.CreateL( aCommand->Params()[i].Des() );
        buf.CleanupClosePushL();

        buf.Delete( 0, prefixLength );

        buf.Delete( pos - suffixLength, buf.Length() - 1 );

        iSettings->iSettingsArr.AppendL( THsHFPIndicatorSetting( buf, 0 ) );
        CleanupStack::PopAndDestroy( &buf );

        }
    TRACE_FUNC_EXIT
    }

void CHsHFPConnectionManagement::SetupIndicatorsL(
        const CHsHFPCommand* aCommand )
    {
    TRACE_FUNC_ENTRY

    for ( TInt i = 0; i < aCommand->Params().Count(); i++ )
        {
        if ( iSettings->iSettingsArr.Count() > ( i + 1 ) )
            {
            iSettings->iSettingsArr[i + 1].SetValueL(
                    aCommand->Params()[i].Des() );
            TRACE_INFO( (_L8("INDICATOR: %S - value: %d "),
                            &iSettings->iSettingsArr[i + 1].Des(),
                            iSettings->iSettingsArr[i + 1].Int() ) );
            }
        }

    TRACE_FUNC_EXIT
    }