bluetoothengine/headsetsimulator/profiles/hspprofile/src/dataprocessing/hspcommandparser.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 <badesca.h>

#include "hspcommandparser.h"
#include "debug.h"

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

    return self;
    }

CHsHSPParser* CHsHSPParser::NewLC()
    {

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

CHsHSPParser::~CHsHSPParser()
    {

    }

void CHsHSPParser::ParseL( const TDesC8& aBufCommand, const TBool aFromAG,
        CHsHSPCommand& aCommand )
    {
    TRACE_FUNC_ENTRY

    aCommand.SetSource( aFromAG );

    THsHSPCommandType type = EHSPCmdUnknown;

    type = RetrieveTypeL( aBufCommand );

    User::LeaveIfError( aCommand.SetType( type ) );

    if ( type == EHSPCmdOK || type == EHSPCmdERROR || type == EHSPCmdRING )
        {
        User::LeaveIfError( aCommand.SetMode( ECmdModeNormal ) );
        RATParamArray params( 1 );
        User::LeaveIfError( aCommand.SetParams( params ) );

        }
    else
        {
        THsHSPCommandMode 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

    }

CHsHSPParser::CHsHSPParser()
    {

    }

void CHsHSPParser::ConstructL()
    {

    }

THsHSPCommandMode CHsHSPParser::RetrieveModeL( const TDesC8 &aCommand,
        const TBool aFromAG )
    {
    TRACE_FUNC_ENTRY

    THsHSPCommandMode res = ECmdUnknownMode;

    if ( aFromAG )
        {
        if ( aCommand.Find( KHsHSPATTestModeDesAG ) != KErrNotFound )
            {
            res = ECmdModeTest;
            }
        else if ( aCommand.Find( KHsHSPATReadModeDesAG ) != KErrNotFound )
            {
            res = ECmdModeRead;
            }
        else if ( aCommand.Find( KHsHSPATWriteModeDesAG ) != KErrNotFound )
            {
            res = ECmdModeWrite;
            }

        }
    else
        {
        if ( aCommand.Find( KHsHSPATTestModeDes ) != KErrNotFound )
            {
            res = ECmdModeTest;
            }
        else if ( aCommand.Find( KHsHSPATReadModeDes ) != KErrNotFound )
            {
            res = ECmdModeRead;
            }
        else if ( aCommand.Find( KHsHSPATWriteModeDes ) != KErrNotFound )
            {
            res = ECmdModeWrite;
            }

        }

    if ( res == ECmdUnknownMode )
        {

        User::Leave( KErrArgument );
        }
    TRACE_FUNC_EXIT
    return res;

    }

THsHSPCommandType CHsHSPParser::RetrieveTypeL( const TDesC8 &aCommand )
    {
    TRACE_FUNC_ENTRY
    THsHSPCommandType res = EHSPCmdUnknown;
    if ( aCommand.Find( KHsHSPOKCommand ) != KErrNotFound )
        {
        res = EHSPCmdOK;
        }
    else if ( aCommand.Find( KHsHSPERRORCommand ) != KErrNotFound )
        {
        res = EHSPCmdERROR;
        }
    else if ( aCommand.Find( KHsHSPRINGCommand ) != KErrNotFound )
        {
        res = EHSPCmdRING;
        }
    else if ( aCommand.Find( KHsHSPVGSCommand ) != KErrNotFound )
        {
        res = EHSPCmdVGS;
        }
    else if ( aCommand.Find( KHsHSPVGMCommand ) != KErrNotFound )
        {
        res = EHSPCmdVGM;
        }
    else if ( aCommand.Find( KHsHSPCKPDCommand ) != KErrNotFound )
        {
        res = EHSPCmdCKPD;
        }

    if ( res == EHSPCmdUnknown )
        User::Leave( KErrArgument );

    TRACE_FUNC_EXIT
    return res;
    }

void CHsHSPParser::RetrieveParamsL( const TDesC8 &aCommand,
        const THsHSPCommandMode 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( KHsHSPIndicatorParamsSeparator ) != 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( KHsHSPATTestModeDes );
                modeLength = 2;
                }
                break;
            case ECmdModeRead:
                {
                startPos = buf.Find( KHsHSPATReadModeDes );
                modeLength = 1;
                }
                break;
            case ECmdModeWrite:
                {
                startPos = buf.Find( KHsHSPATWriteModeDes );
                modeLength = 1;

                if ( startPos == KErrNotFound )
                    {
                    startPos = buf.Find( KHsHSPATWriteModeDesAG );
                    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( KHsHSPIndicatorParamsSeparator );
            }
        else
            {
            pos = buf.Find( KHsHSPParamsSeparator );
            }

        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
    }