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

CHsHFPDataHandler* CHsHFPDataHandler::NewL()
    {

    CHsHFPDataHandler *self = CHsHFPDataHandler::NewLC();
    CleanupStack::Pop( self );

    return self;
    }

CHsHFPDataHandler* CHsHFPDataHandler::NewLC()
    {

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

CHsHFPDataHandler::~CHsHFPDataHandler()
    {
    TRACE_FUNC_ENTRY
    if ( iFeatureManager )
        {
        delete iFeatureManager;
        }

    if ( iParser )
        {
        delete iParser;
        }
    TRACE_FUNC_EXIT
    }

void CHsHFPDataHandler::ProcessDataL( const TDesC8& aDataIn,
        const TBool aFromAG, TDes8& aDataOut )
    {
    TRACE_FUNC_ENTRY
    if ( aDataIn.Compare( KNullDesC8 ) == 0 )
        {
        User::Leave( KErrArgument );
        }

    aDataOut.Zero();

    CDesC8ArrayFlat* cmdArr = SplitCommandsL( aDataIn );
    CleanupStack::PushL( cmdArr );

    CHsHFPCommand* cmdIn = CHsHFPCommand::NewL();
    CleanupStack::PushL( cmdIn );

    CHsHFPCommand* cmdOut = CHsHFPCommand::NewL();
    CleanupStack::PushL( cmdOut );

    RBuf8 buf, tmpOut;

    tmpOut.CreateL( KTmpBufferLength );
    tmpOut.CleanupClosePushL();

    for ( TInt i = 0; i < cmdArr->Count(); i++ )
        {
        buf.CreateL( cmdArr->MdcaPoint( i ) );
        buf.CleanupClosePushL();
        TRACE_INFO((_L8("DATA RETRIEVED FROM ARRAY: %S") , &buf))

        iParser->ParseL( buf, aFromAG, *cmdIn );

        iFeatureManager->PerformDataProcessingL( cmdIn, *cmdOut );

        cmdOut->ToDes8( tmpOut );
        aDataOut.Append( tmpOut );
        CleanupStack::PopAndDestroy( &buf );
        }    
    CleanupStack::PopAndDestroy( &tmpOut );
    CleanupStack::PopAndDestroy( cmdOut );
    CleanupStack::PopAndDestroy( cmdIn );
    CleanupStack::PopAndDestroy( cmdArr );
    
    TRACE_FUNC_EXIT
    }

void CHsHFPDataHandler::HandleClientDisconnected( TInt aErr )
    {
    TRACE_FUNC_ENTRY
    iFeatureManager->HandleClientDisconnected( aErr );
    TRACE_FUNC_EXIT
    }

void CHsHFPDataHandler::HandleClientConnected( TDes8& aCommandOut )
    {
    TRACE_FUNC_ENTRY
    TRAP_IGNORE( iFeatureManager->HandleClientConnectedL( aCommandOut ) );
    TRACE_FUNC_EXIT
    }

void CHsHFPDataHandler::HandleAcceptCallL( TDes8& aCommandOut )
    {
    TRACE_FUNC_ENTRY

    PerformRequestL( _L8("ATA\r\n"), aCommandOut );
    TRACE_FUNC_EXIT
    }

void CHsHFPDataHandler::HandleReleaseCallL( TDes8& aCommandOut )
    {
    TRACE_FUNC_ENTRY
    PerformRequestL( _L8("AT+CHUP\r\n"), aCommandOut );
    TRACE_FUNC_EXIT
    }

CHsHFPDataHandler::CHsHFPDataHandler()
    {

    }

void CHsHFPDataHandler::ConstructL()
    {
    TRACE_FUNC_ENTRY
    iFeatureManager = CHsHFPFeatureManager::NewL();

    iParser = CHsHFPParser::NewL();
    TRACE_FUNC_EXIT
    }

CDesC8ArrayFlat* CHsHFPDataHandler::SplitCommandsL( const TDesC8 &aCommands )
    {
    TRACE_FUNC_ENTRY
    CDesC8ArrayFlat* array = new ( ELeave ) CDesC8ArrayFlat(
            KCommandArrayGranularity );

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

    const TInt offset = 2;

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

        if ( pos == KErrNotFound )
            {
            array->AppendL( buf );
            buf.Zero();
            }
        else
            {

            TPtrC8 ptr = buf.Left( pos + offset );
            array->AppendL( ptr );
            buf.Delete( 0, pos + offset );
            }
        }
    CleanupStack::PopAndDestroy( &buf );

    TRACE_FUNC_EXIT
    return array;
    }

void CHsHFPDataHandler::PerformRequestL( const TDesC8& aCommand,
        TDes8& aResponse )
    {
    TRACE_FUNC_ENTRY
    CHsHFPCommand* cmdIn = CHsHFPCommand::NewL();
    CleanupStack::PushL( cmdIn );
    CHsHFPCommand* cmdOut = CHsHFPCommand::NewL();
    CleanupStack::PushL( cmdOut );

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

    iParser->ParseL( buf, EFalse, *cmdIn );

    iFeatureManager->PerformDataProcessingL( cmdIn, *cmdOut );
    cmdOut->ToDes8( aResponse );

    CleanupStack::PopAndDestroy( &buf );
    CleanupStack::PopAndDestroy( &cmdOut );
    CleanupStack::PopAndDestroy( &cmdIn );
    TRACE_FUNC_EXIT
    }