bluetoothappprofiles/avrcp/avc/avcframe.cpp
changeset 70 f5508c13dfe0
parent 67 16e4b9007960
child 71 083fd884d7dd
equal deleted inserted replaced
67:16e4b9007960 70:f5508c13dfe0
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @publishedPartner
       
    19  @released
       
    20 */
       
    21 
       
    22 #include <bluetooth/logger.h>
       
    23 #include <avcframe.h>
       
    24 
       
    25 #ifdef __FLOG_ACTIVE
       
    26 _LIT8(KLogComponent, LOG_COMPONENT_AVRCP_FRAME);
       
    27 #endif
       
    28 
       
    29 #ifdef _DEBUG
       
    30 PANICCATEGORY("avctpframe");
       
    31 #endif
       
    32 
       
    33 /** Constructor.
       
    34 
       
    35 @param aFrameType ECommand if this is a command, EResponse
       
    36 				  if it's a response.
       
    37 @internalComponent
       
    38 @released	
       
    39 */
       
    40 CAVCFrame::CAVCFrame(AVC::TFrameType aFrameType)
       
    41 	: iFrameType(aFrameType)
       
    42 	{
       
    43 	LOG_FUNC
       
    44 	}
       
    45 
       
    46 /** Destructor.
       
    47 
       
    48 @publishedPartner
       
    49 @released
       
    50 */
       
    51 EXPORT_C CAVCFrame::~CAVCFrame()
       
    52 	{
       
    53 	LOG_FUNC
       
    54 	iBuffer.Close();
       
    55 	}
       
    56 
       
    57 /** Factory function.
       
    58 
       
    59 This overload should be called when an AV/C frame is
       
    60 to be constructed, that is it is probably an outgoing frame.
       
    61 
       
    62 @param aFrameType ECommand if this is a command, EResponse
       
    63 				  if it's a response.
       
    64 @param aType The AV/C CType for this frame.
       
    65 @param aSubunitType The AV/C subunit type for this frame.
       
    66 @param aSubunitId The AV/C subunit id for this frame.
       
    67 @return A fully constructed CAVCFrame.
       
    68 @leave System wide error code.
       
    69 @publishedPartner
       
    70 @released
       
    71 */
       
    72 EXPORT_C CAVCFrame* CAVCFrame::NewL(AVC::TFrameType aFrameType, 
       
    73 	AVC::TCType aType, 
       
    74 	AVC::TSubunitType aSubunitType, 
       
    75 	AVC::TSubunitID aSubunitID)
       
    76 	{
       
    77 	CAVCFrame* frame = new(ELeave)CAVCFrame(aFrameType);
       
    78 	CleanupStack::PushL(frame);
       
    79 	frame->ConstructL(aType, aSubunitType, aSubunitID);
       
    80 	CleanupStack::Pop(frame);
       
    81 	return frame;
       
    82 	}
       
    83 
       
    84 /** Factory function.
       
    85 
       
    86 This overload should be used when a data buffer should
       
    87 be parsed as an AV/C frame, that is it is probably an 
       
    88 incoming frame.
       
    89 
       
    90 @param aBuffer A buffer to be parsed as an AV/C frame.
       
    91 @param aFrameType ECommand if this is a command, EResponse
       
    92 				  if it's a response.
       
    93 @return A fully constructed CAVCFrame.
       
    94 @leave System wide error code.				  
       
    95 @publishedPartner
       
    96 @released
       
    97 */
       
    98 EXPORT_C CAVCFrame* CAVCFrame::NewL(const TDesC8& aBuffer, AVC::TFrameType aFrameType)
       
    99 	{
       
   100 	CAVCFrame* frame = new(ELeave)CAVCFrame(aFrameType);
       
   101 	CleanupStack::PushL(frame);
       
   102 	frame->ConstructL(aBuffer);
       
   103 	CleanupStack::Pop(frame);
       
   104 	return frame;
       
   105 	}
       
   106 	
       
   107 /** Second phase construction.
       
   108 
       
   109 This overload is used when an AV/C frame is
       
   110 to be constructed, that is it is probably an outgoing frame.
       
   111 
       
   112 @param aType The AV/C CType for this frame.
       
   113 @param aSubunitType The AV/C subunit type for this frame.
       
   114 @param aSubunitId The AV/C subunit id for this frame.
       
   115 @return A fully constructed CAVCFrame.
       
   116 @leave System wide error code.
       
   117 @internalComponent
       
   118 @released
       
   119 */
       
   120 void CAVCFrame::ConstructL(AVC::TCType aType, AVC::TSubunitType aSubunitType, AVC::TSubunitID aSubunitID)
       
   121 	{
       
   122 	iBuffer.CreateL(KAVCFrameMaxLength);
       
   123 	iBuffer.Zero();
       
   124 	iBuffer.Append(TChar(aType));
       
   125 
       
   126 	TInt subType	= aSubunitType;
       
   127 	TInt subID		= aSubunitID;
       
   128 
       
   129 	if (subType > AVC::ETypeExtended2)
       
   130 		{
       
   131 		iSubunitTypeExtensionBytes = 1;
       
   132 		iBuffer.Append(TChar(AVC::ETypeExtended1 << 3));
       
   133 		subType -= 0x100;
       
   134 
       
   135 		while (subType > 0x100)
       
   136 			{
       
   137 			iBuffer.Append(TChar(AVC::ETypeExtended2));
       
   138 			subType -= 0x100;
       
   139 			}
       
   140 
       
   141 		iBuffer.Append(TChar(subType));
       
   142 		}
       
   143 	else
       
   144 		{
       
   145 		iBuffer.Append(TChar(subType << 3));
       
   146 		}
       
   147 
       
   148 	if (subID > AVC::EIDExtended2)
       
   149 		{
       
   150 		iSubunitIDExtensionBytes = 1;
       
   151 		iBuffer[1] |= AVC::EIDExtended1;
       
   152 		subID -= 0x100;
       
   153 
       
   154 		while (subID > 0x100)
       
   155 			{
       
   156 			iBuffer.Append(TChar(AVC::EIDExtended2));
       
   157 			subID -= 0x100;
       
   158 			}
       
   159 
       
   160 		iBuffer.Append(TChar(subID));
       
   161 		}
       
   162 	else
       
   163 		{
       
   164 		iBuffer[1] |= subID;
       
   165 		}
       
   166 	}
       
   167 
       
   168 /** Second phase construction.
       
   169 
       
   170 This overload is used when a data buffer should
       
   171 be parsed as an AV/C frame, that is it is probably an 
       
   172 incoming frame.
       
   173 
       
   174 For details of parsing refer to the AV/C digital
       
   175 interface command set specification.
       
   176 
       
   177 @param aBuffer A buffer to be parsed as an AV/C frame.
       
   178 @return A fully constructed CAVCFrame.
       
   179 @leave System wide error code.	
       
   180 @publishedPartner
       
   181 @released
       
   182 */	
       
   183 void CAVCFrame::ConstructL(const TDesC8& aBuffer)
       
   184 	{
       
   185 	iBuffer.CreateL(aBuffer);
       
   186 	FindExtensionL(iBuffer, iSubunitTypeExtensionBytes, iSubunitIDExtensionBytes);
       
   187 	}
       
   188 
       
   189 /** Gets the AV/C frame type.
       
   190 
       
   191 @return ECommand if this is a command, EResponse if this
       
   192 		is a response.
       
   193 @publishedPartner
       
   194 @released
       
   195 */
       
   196 EXPORT_C AVC::TFrameType CAVCFrame::FrameType() const
       
   197 	{
       
   198 	return iFrameType;
       
   199 	}
       
   200 
       
   201 /** Gets the AV/C frame type.
       
   202 
       
   203 @param aFrame The frame to get the frame type for.
       
   204 @return ECommand if this is a command, EResponse if this
       
   205 		is a response.
       
   206 @publishedPartner
       
   207 @released
       
   208 */	
       
   209 EXPORT_C AVC::TFrameType CAVCFrame::FrameType(const TDesC8& aFrame)
       
   210 	{
       
   211 	AVC::TFrameType frameType = AVC::ECommand;
       
   212 	
       
   213 	if( aFrame[0] > KAVCCommandMaxRangeLength )  
       
   214 		{
       
   215 			frameType = AVC::EResponse;
       
   216 		}
       
   217 	return frameType;	
       
   218 	}
       
   219 
       
   220 /** Set the AV/C frame type for this frame.
       
   221 
       
   222 @param The frame type to set.
       
   223 @publishedPartner
       
   224 @released
       
   225 */	
       
   226 EXPORT_C void CAVCFrame::SetFrameType(AVC::TFrameType aFrameType)
       
   227 	{
       
   228 	iFrameType = aFrameType;
       
   229 	}
       
   230 
       
   231 /** Get the AV/C CType for this frame.
       
   232 
       
   233 @return The AV/C CType for this frame.
       
   234 @publishedPartner
       
   235 @released
       
   236 */
       
   237 EXPORT_C AVC::TCType CAVCFrame::Type() const
       
   238 	{
       
   239 	return static_cast<AVC::TCType>(iBuffer[0]);
       
   240 	}
       
   241 
       
   242 /** Set the AV/C CType for this frame.
       
   243 
       
   244 @param aType The AV/C CType to set.
       
   245 @publishedPartner
       
   246 @released
       
   247 */	
       
   248 EXPORT_C void CAVCFrame::SetType(AVC::TCType aType)
       
   249 	{
       
   250 	iBuffer[0] = aType;
       
   251 	}
       
   252 
       
   253 /** Get the AV/C subunit type for this frame.
       
   254 
       
   255 @return The AV/C subunit type for this frame.
       
   256 @publishedPartner
       
   257 @released
       
   258 */
       
   259 EXPORT_C AVC::TSubunitType CAVCFrame::SubunitType() const
       
   260 	{
       
   261 	if (iSubunitTypeExtensionBytes == 0)
       
   262 		{
       
   263 		return static_cast<AVC::TSubunitType>((iBuffer[1] & KAVCSubunitTypeMask) >> 3);
       
   264 		}
       
   265 
       
   266 	return static_cast<AVC::TSubunitType>(iBuffer[1 + iSubunitTypeExtensionBytes] + (iSubunitTypeExtensionBytes * 0x100));
       
   267 	}
       
   268 
       
   269 /** Get the AV/C subunit id for this frame.
       
   270 
       
   271 @return The AV/C subunit id for this frame.
       
   272 @publishedPartner
       
   273 @released
       
   274 */
       
   275 EXPORT_C AVC::TSubunitID CAVCFrame::SubunitID() const
       
   276 	{
       
   277 	if (iSubunitIDExtensionBytes == 0)
       
   278 		{
       
   279 		return static_cast<AVC::TSubunitID>(iBuffer[1] & KAVCSubunitIDMask);
       
   280 		}
       
   281 
       
   282 	return static_cast<AVC::TSubunitID>(iBuffer[1 + iSubunitTypeExtensionBytes + iSubunitIDExtensionBytes] + (iSubunitIDExtensionBytes * 0x100));
       
   283 	}
       
   284 
       
   285 /** Find extension bytes for the frame.
       
   286 
       
   287 @param aBuffer buffer to be used.
       
   288 @return True if its a valid frame.
       
   289 @leave System wide error code.
       
   290 @internalComponent
       
   291 @released
       
   292 */
       
   293 /* static */ void CAVCFrame::FindExtensionL(const TDesC8& aBuffer, TInt& aSubunitTypeExtensionBytes, TInt& aSubunitIDExtensionBytes)
       
   294 	{
       
   295 	TInt minLength = KAVCFrameHeaderLength;
       
   296 	if(aBuffer.Length() < minLength)
       
   297 		{
       
   298 		User::Leave(KErrCorrupt);
       
   299 		}
       
   300 	
       
   301 	if (static_cast<AVC::TSubunitType>((aBuffer[1] & KAVCSubunitTypeMask) >> 3) == AVC::ETypeExtended1)
       
   302 		{
       
   303 		aSubunitTypeExtensionBytes++;
       
   304 		minLength++;
       
   305 
       
   306 		while (aBuffer[1 + aSubunitTypeExtensionBytes] == AVC::ETypeExtended2)
       
   307 			{
       
   308 			if(aBuffer.Length() < minLength)
       
   309 				{
       
   310 				User::Leave(KErrCorrupt);
       
   311 				}
       
   312 			
       
   313 			aSubunitTypeExtensionBytes++;
       
   314 			minLength++;
       
   315 			}
       
   316 		}
       
   317 
       
   318 	if (static_cast<AVC::TSubunitID>(aBuffer[1] & KAVCSubunitIDMask) == AVC::EIDExtended1)
       
   319 		{
       
   320 		aSubunitIDExtensionBytes++;
       
   321 		minLength++;
       
   322 
       
   323 		while (aBuffer[1 + aSubunitIDExtensionBytes] == AVC::EIDExtended1)
       
   324 			{
       
   325 			if(aBuffer.Length() < minLength)
       
   326 				{
       
   327 				User::Leave(KErrCorrupt);
       
   328 				}
       
   329 			
       
   330 			aSubunitIDExtensionBytes++;
       
   331 			minLength++;
       
   332 			}
       
   333 		}
       
   334 		
       
   335 	//Ensure frame is a valid length i.e. the Opcode() method can be safely called.
       
   336 	if(aBuffer.Length() < minLength)
       
   337 		{
       
   338 		User::Leave(KErrCorrupt);
       
   339 		}	
       
   340 	}
       
   341 
       
   342 /** Get the AV/C opcode for this frame.
       
   343 
       
   344 @param aBuffer buffer to search.
       
   345 @return The AV/C opcode for this frame.
       
   346 @leave System wide error code.
       
   347 @internalComponent
       
   348 @released
       
   349 */
       
   350 /* static */ EXPORT_C AVC::TOpcode CAVCFrame::OpcodeL(const TDesC8& aBuffer)
       
   351 	{
       
   352 	TInt subunitTypeExtensionBytes=0;
       
   353 	TInt subunitIDExtensionBytes=0;
       
   354 	
       
   355 	FindExtensionL(aBuffer, subunitTypeExtensionBytes, subunitIDExtensionBytes);
       
   356 	return static_cast<AVC::TOpcode> (aBuffer[KAVCFrameHeaderLength + subunitTypeExtensionBytes + subunitIDExtensionBytes - 1]);
       
   357 	}
       
   358 
       
   359 /** Get the AV/C opcode for this frame.
       
   360 
       
   361 @return The AV/C opcode for this frame.
       
   362 @publishedPartner
       
   363 @released
       
   364 */
       
   365 EXPORT_C TUint8 CAVCFrame::Opcode() const
       
   366 	{
       
   367 	return iBuffer[KAVCFrameHeaderLength + iSubunitTypeExtensionBytes + iSubunitIDExtensionBytes - 1];
       
   368 	}
       
   369 
       
   370 /** Get the AV/C OperationId for this frame.
       
   371 
       
   372 This is only valid for passthrough commands.
       
   373 
       
   374 @param aOpId On return, the AV/C opcode for this frame.
       
   375 @return KErrNotSupported if this is not a passthrough command,
       
   376 		KErrCorrupt if this passthrough command does not contain a OpId,
       
   377 		KErrNone otherwise.
       
   378 @publishedPartner
       
   379 @released
       
   380 */	
       
   381 EXPORT_C TInt CAVCFrame::OperationId(TUint8& aOpId) const
       
   382 	{
       
   383 	TInt err = KErrNotSupported;
       
   384 	
       
   385 	if(Opcode() == AVC::EPassThrough)
       
   386 		{
       
   387 		if(DataLength())
       
   388 			{
       
   389 			aOpId = (iBuffer[iSubunitTypeExtensionBytes + iSubunitIDExtensionBytes + KAVCFrameHeaderLength]) & 0x7f;
       
   390 			err = KErrNone;			
       
   391 			}
       
   392 		else
       
   393 			{
       
   394 			err = KErrCorrupt;
       
   395 			}
       
   396 		}
       
   397 		
       
   398 	return err;
       
   399 	}
       
   400 
       
   401 /** Get the AV/C button action for this frame.
       
   402 
       
   403 This is only valid for passthrough commands.
       
   404 
       
   405 @param aOpId On return, the AV/C button action for this frame.
       
   406 @return KErrNotSupported if this is not a passthrough command,
       
   407 		KErrCorrupt if this passthrough command does not contain a button action,
       
   408 		KErrNone otherwise.
       
   409 @publishedPartner
       
   410 @released
       
   411 */	
       
   412 EXPORT_C TInt CAVCFrame::ButtonAct(AVCPanel::TButtonAction& aButtonAction) const
       
   413 	{
       
   414 	TInt err = KErrNotSupported;
       
   415 	
       
   416 	if(Opcode() == AVC::EPassThrough)
       
   417 		{
       
   418 		if(DataLength())
       
   419 			{
       
   420 			aButtonAction = (((iBuffer[iSubunitTypeExtensionBytes + iSubunitIDExtensionBytes + KAVCFrameHeaderLength]) & 0x80) == AVCPanel::EButtonRelease) ? AVCPanel::EButtonRelease : AVCPanel::EButtonPress;
       
   421 			err = KErrNone;	
       
   422 			}
       
   423 		else
       
   424 			{
       
   425 			err = KErrCorrupt;
       
   426 			}
       
   427 		}
       
   428 	return err;
       
   429 	}
       
   430 
       
   431 /** Retrieve data from the AV/C frame.
       
   432 
       
   433 @param aIndex The offset of the data element within the data segment of the frame
       
   434 @return The data element at aIndex.
       
   435 @panic If aIndex is outside the frame. DataLength() should be used to check the length of the data segment before using the [] operator.
       
   436 @publishedPartner
       
   437 @released
       
   438 */
       
   439 EXPORT_C const TUint8& CAVCFrame::operator[](TInt aIndex) const
       
   440 	{
       
   441 	return iBuffer[aIndex + iSubunitTypeExtensionBytes + iSubunitIDExtensionBytes + KAVCFrameHeaderLength];
       
   442 	}
       
   443 
       
   444 /** Retrieve the entire AV/C frame.
       
   445 
       
   446 @return The AV/C frame.
       
   447 @publishedPartner
       
   448 @released
       
   449 */
       
   450 EXPORT_C const TDesC8& CAVCFrame::Data() const
       
   451 	{
       
   452 	return iBuffer;
       
   453 	}
       
   454 
       
   455 /** Append data to the AV/C frame.
       
   456 
       
   457 @param aDes The data to be appended.
       
   458 @publishedPartner
       
   459 @released
       
   460 */
       
   461 EXPORT_C void CAVCFrame::Append(const TDesC8& aDes)
       
   462 	{
       
   463 	iBuffer.Append(aDes);
       
   464 	}
       
   465 
       
   466 /** Append data to the AV/C frame.
       
   467 
       
   468 @param aChar The data to be appended.
       
   469 @publishedPartner
       
   470 @released
       
   471 */
       
   472 EXPORT_C void CAVCFrame::Append(TChar aChar)
       
   473 	{
       
   474 	iBuffer.Append(aChar);
       
   475 	}
       
   476 
       
   477 /** Return the length of the data in the AV/C frame
       
   478 
       
   479 @return The length of the data in the AV/C frame
       
   480 @publishedPartner
       
   481 @released
       
   482 */
       
   483 EXPORT_C TInt CAVCFrame::DataLength() const
       
   484 	{
       
   485 	return (iBuffer.Length() - iSubunitTypeExtensionBytes - iSubunitIDExtensionBytes - KAVCFrameHeaderLength);
       
   486 	}
       
   487 
       
   488 EXPORT_C CAVCFrame* CAVCVendorDependentResponse::NewL(TUint aVendorID)
       
   489 	{
       
   490 	using namespace AVC;
       
   491 	CAVCFrame* frame = CAVCFrame::NewL(EResponse,
       
   492 										ENotImplemented, //client can override
       
   493 										EPanel,
       
   494 										EID0);
       
   495 	// stupid frames don't know about themselves so we construct in derived classes
       
   496 	// first opcode  - base class REALLY ought to have opcode setter
       
   497 	frame->Append(0); //opcode for VD frame
       
   498 	// second vendor
       
   499 	frame->Append(aVendorID>>16);	
       
   500 	frame->Append(aVendorID>>8);	
       
   501 	frame->Append(aVendorID);	
       
   502 	return frame;
       
   503 	}
       
   504 
       
   505 EXPORT_C TPtrC8 CAVCVendorDependentCommand::GetPayloadAndVID(const CAVCFrame& aFrame, TUint& aVID)
       
   506 	{
       
   507 	ASSERT_DEBUG(aFrame.Opcode()==AVC::EVendorDependent); //opcode
       
   508 	aVID = (aFrame.operator[](0)<<16) | 
       
   509 			(aFrame.operator[](1)<<8) |
       
   510 			(aFrame.operator[](2));
       
   511 
       
   512 	return (aFrame.Data().Right(aFrame.DataLength()-KAVCVendorIdLength));
       
   513 	}
       
   514 
       
   515 EXPORT_C TPtrC8 CAVCVendorUniquePassthroughCommand::GetPayloadAndVID(const CAVCFrame& aFrame, TUint& aVID)
       
   516 	{
       
   517 	ASSERT_DEBUG(aFrame.Opcode()==AVC::EPassThrough); //opcode
       
   518 	aVID = (aFrame.operator[](2)<<16) | 
       
   519 			(aFrame.operator[](3)<<8) |
       
   520 			(aFrame.operator[](4));
       
   521 
       
   522 	return (aFrame.Data().Right(aFrame.DataLength()-KAVCVendorIdLength-2));
       
   523 	}