bluetoothengine/headsetsimulator/profiles/hfpprofile/src/features/hfpremoteaudiovolumecontrol.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 "hfpremoteaudiovolumecontrol.h"
#include "hfpcommand.h"
#include "hfpsettings.h"
#include "debug.h"

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

CHsHFPRemoteAudioVolumeControl* CHsHFPRemoteAudioVolumeControl::NewLC(
        MHsHFPFeatureProviderObserver* aObserver )
    {
    CHsHFPRemoteAudioVolumeControl* self =
            new ( ELeave ) CHsHFPRemoteAudioVolumeControl( aObserver );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }
CHsHFPRemoteAudioVolumeControl::~CHsHFPRemoteAudioVolumeControl()
    {
    TRACE_FUNC_ENTRY
    if ( iSettings )
        {
        iSettings->Release();
        }
    TRACE_FUNC_EXIT
    }

TInt CHsHFPRemoteAudioVolumeControl::VolumeLevelSynchronization(
        const CHsHFPCommand &aInputCmd, CHsHFPCommand &aOutputCmd )
    {
    TRACE_FUNC_ENTRY

    TInt res = KErrNone;
    if ( aInputCmd.FromAG() )
        {

        if ( aInputCmd.Type() == EHFPCmdVGS )
            {
            iSettings->iVgsLevel = aInputCmd.Params()[0].IntL();
            }
        else if ( aInputCmd.Type() == EHFPCmdVGM )
            {
            iSettings->iVgmLevel = aInputCmd.Params()[0].IntL();
            }
        else if ( iWaitingForOK && aInputCmd.Type() == EHFPCmdOK )
            {
            iWaitingForOK = EFalse;
            iWaitingForVgmOK = ETrue;
            aOutputCmd.SetType( EHFPCmdVGM );
            aOutputCmd.SetMode( ECmdModeWrite );
            RATParamArray params;
            TBuf8 <KHsHFPMaxVolumeLevelParamSize> vgmLevel;
            vgmLevel.AppendNum( iSettings->iVgmLevel );
            res = params.Append( TATParam( vgmLevel ) );
            aOutputCmd.SetParams( params );
            }
        else if ( iWaitingForVgmOK && aInputCmd.Type() == EHFPCmdOK )
            {
            iWaitingForVgmOK = EFalse;
            iVolumeLevelSynchronized = ETrue;
            }
        else
            {
            res = KErrArgument;
            }
        }
    else
        {
        res = KErrArgument;
        }

    TRACE_FUNC_EXIT
    return res;
    }

void CHsHFPRemoteAudioVolumeControl::Reset()
    {
    TRACE_FUNC_ENTRY
    iVolumeLevelSynchronized = EFalse;
    iWaitingForOK = ETrue;
    iWaitingForVgmOK = EFalse;
    TRACE_FUNC_EXIT
    }

TInt CHsHFPRemoteAudioVolumeControl::ProcessCommand(
        const CHsHFPCommand &aInputCmd, CHsHFPCommand &aOutputCmd )
    {
    TRACE_FUNC_ENTRY

    TInt res = KErrNone;
    if ( !iVolumeLevelSynchronized )
        {
        res = VolumeLevelSynchronization( aInputCmd, aOutputCmd );
        }
    else if ( aInputCmd.FromAG() )
        {
        if ( aInputCmd.Type() == EHFPCmdVGS && aInputCmd.Params().Count() == 1 )
            {
            TRAP( res, iSettings->iVgsLevel = aInputCmd.Params()[0].IntL() );
            }
        else if ( aInputCmd.Type() == EHFPCmdVGM && aInputCmd.Params().Count() == 1 )
            {
            TRAP( res, iSettings->iVgmLevel = aInputCmd.Params()[0].IntL() );
            }
        else if ( iWaitingForOK && aInputCmd.Type() == EHFPCmdOK )
            {
            iWaitingForOK = EFalse;
            iObserver->HandleProcedureCompleted( KErrNone );
            }
        else
            {
            res = KErrArgument;
            }
        }
    else
        {
        if ( aInputCmd.Type() == EHFPCmdVGS && aInputCmd.Params().Count() == 1 )
            {
            iWaitingForOK = ETrue;
            TRAP( res, iSettings->iVgsLevel = aInputCmd.Params()[0].IntL();
                    CHsHFPCommand ::CopyL( aInputCmd, aOutputCmd ) );
            }
        else if ( aInputCmd.Type() == EHFPCmdVGM && aInputCmd.Params().Count() == 1 )
            {
            iWaitingForOK = ETrue;
            TRAP( res, iSettings->iVgmLevel = aInputCmd.Params()[0].IntL();
                    CHsHFPCommand::CopyL( aInputCmd, aOutputCmd ) );
            }
        else
            {
            res = KErrArgument;
            }
        }

    TRACE_FUNC_EXIT
    return res;
    }

CHsHFPRemoteAudioVolumeControl::CHsHFPRemoteAudioVolumeControl(
        MHsHFPFeatureProviderObserver* aObserver ) :
    iObserver( aObserver ), iWaitingForOK( ETrue )
    {
    }

void CHsHFPRemoteAudioVolumeControl::ConstructL()
    {
    iSettings = CHsHFPSettings::InstanceL();
    }