mtptransports/mtpptpiptransport/ptpipdatatypes/src/tptpipresponsepayload.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 02 Feb 2010 01:11:40 +0200
changeset 0 d0791faffa3f
permissions -rw-r--r--
Revision: 201003 Kit: 201005

// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
// All rights reserved.
// This component and the accompanying materials are made available
// under the terms of "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
//
// Initial Contributors:
// Nokia Corporation - initial contribution.
//
// Contributors:
//
// Description:
//

/**
 @internalComponent
*/

#include <mtp/mtpprotocolconstants.h>
#include "ptpipdatatypes.h"
#include "tptpipresponsepayload.h"


// Dataset element metadata.
const TPTPIPTypeResponsePayload::TElementInfo TPTPIPTypeResponsePayload::iElementMetaData[ENumElements] = 
    {
        {EMTPTypeUINT16, 0,  KMTPTypeUINT16Size},   // EResponseCode
        {EMTPTypeUINT32, 2,  KMTPTypeUINT32Size},   // ETransactionId
        {EMTPTypeUINT32, 6,  KMTPTypeUINT32Size},   // EParameter1
        {EMTPTypeUINT32, 10,  KMTPTypeUINT32Size},  // EParameter2
        {EMTPTypeUINT32, 14,  KMTPTypeUINT32Size},  // EParameter3
        {EMTPTypeUINT32, 18,  KMTPTypeUINT32Size},  // EParameter4
        {EMTPTypeUINT32, 22,  KMTPTypeUINT32Size}   // EParameter5
    };

/**
 Constructor.
 */
EXPORT_C TPTPIPTypeResponsePayload::TPTPIPTypeResponsePayload() :
	iElementInfo(iElementMetaData, ENumElements),
	iBuffer(KSize),
	//iNumParameters(0)
	iMaxSetParam(0)
	{
	SetBuffer(iBuffer);
	}

/**
 Provides the number of parameters in the block which are set.
 @return the number of set parameters.
 */
EXPORT_C TUint TPTPIPTypeResponsePayload::NumParameters() const
	{
	return (iMaxSetParam > 0 )? (iMaxSetParam - EParameter1 + 1) : 0;
	//return iMaxSetParam - EParameter1 + 1;
	}

/**
 Copies up to five parameter values into the parameter block dataset from the specified dataset. 
 The paramters to be copied should be non null paramter or valid null paramter.

 @param aFrom The parameter values source dataset. This dataset should define three or five contiguous 32-bit parameter values starting at the specified offset.
 @param aParamStartOffset The element ID of the first in the set of contiguous 
 source dataset parameter values.
 @param aParamEndOffset The element ID of the last in the set of contiguous 
 source dataset parameter values.
 @param aIsNullParamValid a boolean value to check if a null parameter is valid. A value of ETrue means a null parameter is valid; EFalse means invalid.
 @param aNumOfNullParam the number of valid null parameters to be copied.
 */
EXPORT_C void TPTPIPTypeResponsePayload::CopyIn ( 	const TMTPTypeFlatBase& aFrom,
													TUint aParamStartOffset, TUint aParamEndOffset,
													TBool aIsNullParamValid, TUint aNumOfNullParam)

	{
	__ASSERT_DEBUG((aParamEndOffset >= aParamStartOffset && (aParamEndOffset - aParamStartOffset + 1) <= ENumElements), User::Invariant());
	TUint32 parameter = KMTPNotSpecified32;
	TUint numberOfNullParam = 0;

	for (TUint t(EParameter1), s(aParamStartOffset); s <= aParamEndOffset; t++, s++)	
		{
		parameter = aFrom.Uint32(s);
		if (parameter != KMTPNotSpecified32)
		    {
		    SetUint32(t, parameter);
		    }
		else if(aIsNullParamValid && (numberOfNullParam < aNumOfNullParam))
		    {
		    SetUint32(t, parameter);
		    numberOfNullParam++;
		    }
		}
	}

/**
 Copies the non-null parameter values from the parameter block dataset into the specified dataset. 
 Note: the number of parameters to be copied out depends on two conditions:
 1. The number of parameters contained in this parameter block dataset.
 2. The number of parameters to be copied out to the parameter values sink dataset, 
 which is (aParamEndOffset - aParamStartOffset + 1) by the caller.

 @param aTo The parameter values sink dataset. This dataset should define three or five contiguous 32-bit parameter values starting at the specified offset.
 @param aParamStartOffset the element ID of the first in the set of contiguous sink dataset parameter values.
 @param aParamEndOffset the element ID of the last in the set of contiguous sink dataset parameter values.
 */
EXPORT_C void TPTPIPTypeResponsePayload::CopyOut( 	TMTPTypeFlatBase& aTo,
													TUint aParamStartOffset, TUint aParamEndOffset)
	{
	__ASSERT_DEBUG((aParamEndOffset >= aParamStartOffset && (aParamEndOffset - aParamStartOffset + 1) <= ENumElements), User::Invariant());
//	TUint loopCount = aParamEndOffset - aParamStartOffset;
	for (TUint s(EParameter1), t(aParamStartOffset); (s <= iMaxSetParam && t <= aParamEndOffset); s++, t++)
		{
		aTo.SetUint32((t), Uint32(s));
		}
	}

/**
 Resets the dataset.
 */
EXPORT_C void TPTPIPTypeResponsePayload::Reset()
	{
	TMTPTypeFlatBase::Reset();
	//iNumParameters = 0;
	iMaxSetParam = 0;
	}

/**
 Setter for the response op code.
 */
EXPORT_C void TPTPIPTypeResponsePayload::SetUint16(TInt aElementId, TUint16 aData)
	{
	__ASSERT_DEBUG((aElementId == EResponseCode), User::Invariant());
	TMTPTypeFlatBase::SetUint16(aElementId, aData);
	}

/**
 Getter for the response op code. 
 */
EXPORT_C TUint16 TPTPIPTypeResponsePayload::Uint16(TInt aElementId) const
	{
	__ASSERT_DEBUG((aElementId == EResponseCode), User::Invariant());
	return TMTPTypeFlatBase::Uint16(aElementId);
	}

/**
 Setter for the parameters and tran id
 */
EXPORT_C void TPTPIPTypeResponsePayload::SetUint32(TInt aElementId, TUint32 aData)
	{
	__ASSERT_DEBUG((aElementId != EResponseCode), User::Invariant());
	if (aElementId >= EParameter1)
		{
		if (aElementId > iMaxSetParam)
			iMaxSetParam = aElementId;		
		
		/**
		// Recalculate iNumParameters.
		TInt num((aElementId - EParameter1) + 1);

		if (num > iNumParameters)
			{
			iNumParameters = num;
			}
			*/
		}

	//  Set the element value.
	TMTPTypeFlatBase::SetUint32(aElementId, aData);
	}

/**
 Getter for the parameters and tran id. 
 */
EXPORT_C TUint32 TPTPIPTypeResponsePayload::Uint32(TInt aElementId) const
	{
//	__ASSERT_DEBUG((aElementId < iMaxSetParam ), User::Invariant());
//	__ASSERT_DEBUG((aElementId != EResponseCode), User::Invariant());
	return TMTPTypeFlatBase::Uint32(aElementId);
	}


EXPORT_C TInt TPTPIPTypeResponsePayload::FirstReadChunk(TPtrC8& aChunk) const
	{
	TInt ret(TMTPTypeFlatBase::FirstReadChunk(aChunk));
	TUint64 size(Size());

	if (size < aChunk.Size())
		{
		// Truncate the buffer at the last set parameter.
		aChunk.Set(aChunk.Left(size));
		}

	return ret;
	}

EXPORT_C TUint64 TPTPIPTypeResponsePayload::Size() const
	{
	return (NumParameters() * KMTPTypeUINT32Size + KHeaderElementsSize);
	}

EXPORT_C TUint TPTPIPTypeResponsePayload::Type() const
	{
	return EPTPIPTypeResponsePayload;
	}

EXPORT_C TBool TPTPIPTypeResponsePayload::CommitRequired() const
	{
	return ETrue;
	}

/**
 todo: optimise, start from 5, break on set. 
 */
EXPORT_C MMTPType* TPTPIPTypeResponsePayload::CommitChunkL(TPtr8& /*aChunk*/)
	{
	//iNumParameters = 0;

	// Recalculate iNumParameters.
	for (TUint i(EParameter1); (i <= EParameter5); i++)
		{
		if (TMTPTypeFlatBase::Uint32(i) != KMTPNotSpecified32)
			{
			//iNumParameters = (i + 1);
				iMaxSetParam = i;
			}
		}
	return NULL;
	}

EXPORT_C const TMTPTypeFlatBase::TElementInfo& TPTPIPTypeResponsePayload::ElementInfo(TInt aElementId) const
	{
	return iElementInfo[aElementId];
	}