bluetoothengine/headsetsimulator/profiles/hfpprofile/src/dataprocessing/hfpcommandparser.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 "hfpcommandparser.h"
#include "badesca.h"
#include "debug.h"

CHsHFPParser* CHsHFPParser::NewL()
    {
    CHsHFPParser *self = CHsHFPParser::NewLC();
    CleanupStack::Pop( self );

    return self;
    }

CHsHFPParser* CHsHFPParser::NewLC()
    {

    CHsHFPParser *self = new ( ELeave ) CHsHFPParser();
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

CHsHFPParser::~CHsHFPParser()
    {

    }

void CHsHFPParser::ParseL( const TDesC8& aBufCommand, const TBool aFromAG,
        CHsHFPCommand& aCommand )
    {
    TRACE_FUNC_ENTRY

    aCommand.SetSource( aFromAG );

    THsHFPCommandType type = EHFPCmdUnknown;

    type = RetrieveTypeL( aBufCommand );
    User::LeaveIfError( aCommand.SetType( type ) );

    if ( type == EHFPCmdOK || type == EHFPCmdERROR || type == EHFPCmdCHUP
            || type == EHFPCmdRING || type == EHFPCmdATA || type == EHFPCmdBLDN )
        {
        User::LeaveIfError( aCommand.SetMode( ECmdModeNormal ) );
        RATParamArray params( 1 );
        User::LeaveIfError( aCommand.SetParams( params ) );

        }
    else if ( type == EHFPCmdATD )
        {
        aCommand.SetMode( ECmdModeOther );

        RBuf8 buf;
        buf.CreateL( aBufCommand );
        buf.CleanupClosePushL();
        buf.Delete( 0, KHsHFPATDCommand().Size() );
        buf.Delete( buf.Size() - KHsHFPCommandSuffix().Size(), KHsHFPCommandSuffix().Size() );
        RATParamArray params;
        params.AppendL( TATParam( buf ) );
        User::LeaveIfError( aCommand.SetParams( params ) );
        CleanupStack::PopAndDestroy( &buf );
        }
    else
        {
        THsHFPCommandMode mode = ECmdUnknownMode;
        mode = RetrieveModeL( aBufCommand, aFromAG );
        User::LeaveIfError( aCommand.SetMode( mode ) );

        RATParamArray params( 4 );

        RetrieveParamsL( aBufCommand, mode, params );
        User::LeaveIfError( aCommand.SetParams( params ) );

        }

    TRACE_FUNC_EXIT
    }

CHsHFPParser::CHsHFPParser()
    {

    }

void CHsHFPParser::ConstructL()
    {

    }

THsHFPCommandMode CHsHFPParser::RetrieveModeL( const TDesC8 &aCommand,
        const TBool aFromAG )
    {
    TRACE_FUNC_ENTRY

    THsHFPCommandMode res = ECmdUnknownMode;

    if ( aFromAG )
        {
        if ( aCommand.Find( KHsHFPATTestModeDesAG ) != KErrNotFound )
            {
            res = ECmdModeTest;
            }
        else if ( aCommand.Find( KHsHFPATReadModeDesAG ) != KErrNotFound )
            {
            res = ECmdModeRead;
            }
        else if ( aCommand.Find( KHsHFPATWriteModeDesAG ) != KErrNotFound )
            {
            res = ECmdModeWrite;
            }

        }
    else
        {
        if ( aCommand.Find( KHsHFPATTestModeDes ) != KErrNotFound )
            {
            res = ECmdModeTest;
            }
        else if ( aCommand.Find( KHsHFPATReadModeDes ) != KErrNotFound )
            {
            res = ECmdModeRead;
            }
        else if ( aCommand.Find( KHsHFPATWriteModeDes ) != KErrNotFound )
            {
            res = ECmdModeWrite;
            }

        }

    if ( res == ECmdUnknownMode )
        {
        User::Leave( KErrArgument );
        }
    TRACE_FUNC_EXIT
    return res;

    }

THsHFPCommandType CHsHFPParser::RetrieveTypeL( const TDesC8 &aCommand )
    {
    TRACE_FUNC_ENTRY
    THsHFPCommandType res = EHFPCmdUnknown;
    if ( aCommand.Find( KHsHFPBRSFCommand ) != KErrNotFound )
        {
        res = EHFPCmdBRSF;
        }
    else if ( aCommand.Find( KHsHFPCHUPCommand ) != KErrNotFound )
        {
        res = EHFPCmdCHUP;
        }
    else if ( aCommand.Find( KHsHFPCIEVCommand ) != KErrNotFound )
        {
        res = EHFPCmdCIEV;
        }
    else if ( aCommand.Find( KHsHFPOKCommand ) != KErrNotFound )
        {
        res = EHFPCmdOK;
        }
    else if ( aCommand.Find( KHsHFPERRORCommand ) != KErrNotFound )
        {
        res = EHFPCmdERROR;
        }

    else if ( aCommand.Find( KHsHFPCINDCommand ) != KErrNotFound )
        {
        res = EHFPCmdCIND;
        }
    else if ( aCommand.Find( KHsHFPCMERCommand ) != KErrNotFound )
        {
        res = EHFPCmdCMER;
        }
    else if ( aCommand.Find( KHsHFPCOPSCommand ) != KErrNotFound )
        {
        res = EHFPCmdCOPS;
        }
    else if ( aCommand.Find( KHsHFPRINGCommand ) != KErrNotFound )
        {
        res = EHFPCmdRING;
        }
    else if ( aCommand.Find( KHsHFPATACommand ) != KErrNotFound )
        {
        res = EHFPCmdATA;
        }
    else if ( aCommand.Find( KHsHFPVGSCommand ) != KErrNotFound )
        {
        res = EHFPCmdVGS;
        }
    else if ( aCommand.Find( KHsHFPVGMCommand ) != KErrNotFound )
        {
        res = EHFPCmdVGM;
        }
    else if ( aCommand.Find( KHsHFPBLDNCommand ) != KErrNotFound )
        {
        res = EHFPCmdBLDN;
        }
    else if ( aCommand.Find( KHsHFPATDCommand ) != KErrNotFound )
        {
        res = EHFPCmdATD;
        }
    else if ( aCommand.Find( KHsHFPCLIPCommand ) != KErrNotFound )
        {
        res = EHFPCmdCLIP;
        }
    if ( res == EHFPCmdUnknown )
        User::Leave( KErrArgument );

    TRACE_FUNC_EXIT
    return res;
    }

void CHsHFPParser::RetrieveParamsL( const TDesC8 &aCommand,
        const THsHFPCommandMode aCmdMode, RATParamArray& aParams )
    {
    TRACE_FUNC_ENTRY

    RBuf8 buf;
    buf.CreateL( aCommand );
    buf.CleanupClosePushL();

    TInt prefixLength = 0;
    TInt suffixLength = 0;

    TInt modeLength = 0;

    TInt leftOffset = 0;
    TInt rightOffset = 0;

    TInt startPos = 0;
    TBool indicatorsFound = EFalse;

    if ( buf.Find( KHsHFPIndicatorParamsSeparator ) != KErrNotFound )
        {
        indicatorsFound = ETrue;
        }

    if ( indicatorsFound )
        {
        prefixLength = 9;
        suffixLength = 2;
        rightOffset = 2;
        leftOffset = 1;
        }
    else
        {

        rightOffset = 1;
        suffixLength = 2;

        switch ( aCmdMode )

            {
            case ECmdModeTest:
                {
                startPos = buf.Find( KHsHFPATTestModeDes );
                modeLength = 2;
                }
                break;
            case ECmdModeRead:
                {
                startPos = buf.Find( KHsHFPATReadModeDes );
                modeLength = 1;
                }
                break;
            case ECmdModeWrite:
                {
                startPos = buf.Find( KHsHFPATWriteModeDes );
                modeLength = 1;

                if ( startPos == KErrNotFound )
                    {
                    startPos = buf.Find( KHsHFPATWriteModeDesAG );
                    modeLength = 2;
                    }

                }
                break;
            default:

                User::Leave( KErrArgument );
                break;
            }
        }

    if ( startPos != KErrNotFound )
        {
        //strip command's prefix
        buf.Delete( 0, prefixLength + startPos + modeLength );

        //strip command's suffix
        buf.Delete( buf.Length() - suffixLength, suffixLength );
        }
    else
        {
        User::Leave( KErrArgument );
        }

    while ( buf.Size() > 0 )
        {
        TInt pos = 0;

        if ( indicatorsFound )
            {
            pos = buf.Find( KHsHFPIndicatorParamsSeparator );
            }
        else
            {
            pos = buf.Find( KHsHFPParamsSeparator );
            }

        if ( pos == KErrNotFound )
            {
            aParams.AppendL( buf );
            buf.Zero();

            }
        else
            {
            TPtrC8 ptr = buf.Left( pos + leftOffset );
            aParams.AppendL( ptr );
            buf.Delete( 0, pos + rightOffset );
            }
        }

    CleanupStack::PopAndDestroy( &buf );
    TRACE_FUNC_EXIT
    }