contentmgmt/referencedrmagent/RefTestAgent/streamingrefagent/source/utils/srautils.cpp
changeset 8 35751d3474b7
child 19 cd501b96611d
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Implements some utility functions for reference agent.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "srautils.h"
       
    21 #include <s32file.h>
       
    22 #include <sdpfmtattributefield.h>
       
    23 #include <sdpcodecstringpool.h>
       
    24 #include <sdpattributefield.h>
       
    25 
       
    26 HBufC8* ExtractKmsIdLC(const TDesC8& aValue)
       
    27 /**
       
    28  	Searches the given format attribute value for KMS ID.
       
    29  	@param The format attribute value.
       
    30  	@return The value of KMS ID, if it exists. Otherwise, NULL.
       
    31  */
       
    32 	{
       
    33 	HBufC8* kmsid(0);
       
    34 	_LIT8(KKmsIdParameter, "IPDCKMSId=");
       
    35 	
       
    36 	TLex8 parser(aValue);
       
    37 	while(!parser.Eos())
       
    38 		{
       
    39 		TPtrC8 token(parser.NextToken());
       
    40 		if(token.Find(KKmsIdParameter) == KErrNotFound) continue;
       
    41 		TLex8 subParser(token);
       
    42 		while(!subParser.Eos())
       
    43 			{
       
    44 			if(subParser.Get() != '=') continue;
       
    45 			TPtrC8 value(subParser.NextToken());
       
    46 			if(value.Length() == 0) break;
       
    47 			//Copy the kmsid value to a buffer
       
    48 			kmsid = HBufC8::NewLC(value.Length()+1);
       
    49 			TPtr8 ptrBuf(kmsid->Des());
       
    50 			ptrBuf.Copy(value);
       
    51 			//Copy the separator if it does not exist
       
    52 			_LIT8(KSeparator,";");
       
    53 			if(ptrBuf.Find(KSeparator) == KErrNotFound)
       
    54 				{
       
    55 				ptrBuf.Append(KSeparator);
       
    56 				}
       
    57 			break;	
       
    58 			}
       
    59 		break;
       
    60 		}
       
    61 	return kmsid;
       
    62 	}
       
    63 
       
    64 
       
    65 EXPORT_C TBool CheckKeyStreamSupportedL(const CSdpMediaField& aSdpKeyStream, const TDesC8& aSupportedKmsIds)
       
    66 /**
       
    67  	Function to determine whether the test stream agent supports the key management scheme 
       
    68  	specified in the SDP key stream definition
       
    69  	@param aSdpKeyStream Contains the metadata for the SDP key management scheme of the stream
       
    70  	@param aSupportedKmsIds The list of supported KMS Ids. The delimeter of the list is semi-colon.
       
    71  	@return ETrue if the test stream agent recognizes the SDP format and the KMS Id is supported.
       
    72 	@return EFalse if the test stream agent fails to recognize the SDP format or is unable to decode the key stream.
       
    73  */
       
    74 	{
       
    75 	//FormatAttributeFields method is not constant. So create a new instance.
       
    76 	CSdpMediaField* sdp = aSdpKeyStream.CloneL();
       
    77 	CleanupStack::PushL(sdp);
       
    78 	
       
    79 	TInt attributesCount = sdp->FormatAttributeFields().Count();
       
    80 	if (attributesCount <= 0)
       
    81 		{
       
    82 		CleanupStack::PopAndDestroy(sdp);
       
    83 		return EFalse;
       
    84 		}
       
    85 
       
    86 	TBool supported = EFalse;
       
    87 	HBufC8* kmsid(0);
       
    88 	
       
    89 	for(TInt i=0; i<attributesCount && !supported; ++i)
       
    90 		{
       
    91 		if(sdp->FormatAttributeFields()[i]->Format().Compare(_L8("ipdc-ksm"))) continue;
       
    92 		
       
    93 		kmsid = ExtractKmsIdLC(sdp->FormatAttributeFields()[i]->Value());
       
    94 		if(!kmsid) continue;
       
    95 		
       
    96 		if(aSupportedKmsIds.Find(*kmsid) != KErrNotFound) 
       
    97 			{
       
    98 			supported = ETrue;
       
    99 			}
       
   100 		CleanupStack::PopAndDestroy(kmsid);
       
   101 		}
       
   102 	CleanupStack::PopAndDestroy(sdp);
       
   103 	
       
   104 	return supported;
       
   105 	}
       
   106 
       
   107 
       
   108 
       
   109 void FindAndLoadRightsObjectL(RFs& aFs, const TDesC8& aCid, const TDesC& aPrivateFolder, CSraRightsObject*& aRo)
       
   110 /**
       
   111  	The reference agent keeps rights object files in its own private folder.
       
   112  	This function scans the private directory and reads RO files in turn.
       
   113  	If the RO with the given Content ID is found, it's assigned as the session's rights object.
       
   114  	
       
   115  	A real-life agent should pre-calculate the map of all supported Rights Object on construction 
       
   116  	and import to avoid costly searches on each opened stream.
       
   117  	
       
   118  	@param aFs The file server handle.
       
   119  	@param aCid The searched Content Id.
       
   120  	@param aPrivateFolder The private folder of the agent.
       
   121  	@param aRo Rights Object which is found in the private folder of the agent.
       
   122  */
       
   123 	{
       
   124 	ASSERT(!aRo);
       
   125 	TFileName privFolder(aPrivateFolder);
       
   126 	privFolder[0] = aFs.GetSystemDriveChar();
       
   127 	privFolder.Append(_L("*.dr"));
       
   128 	
       
   129 	CDir *entryList;
       
   130 	User::LeaveIfError(aFs.GetDir(privFolder,KEntryAttNormal,ESortByName|EDescending|EDirsLast,entryList));
       
   131 	CleanupStack::PushL(entryList);
       
   132 	privFolder.SetLength(privFolder.Length()-4);
       
   133 	
       
   134 	TInt count = entryList->Count();
       
   135 	TInt i = 0;
       
   136 	TFileName filePath;
       
   137 	RFile fRo;
       
   138 	RFileReadStream stream;
       
   139 	CSraRightsObject* ro = NULL;
       
   140 	for(i=0; i<count; ++i)
       
   141 		{
       
   142 		if((*entryList)[i].IsDir()) continue;
       
   143 
       
   144 		filePath.Copy(privFolder);
       
   145 		filePath.Append((*entryList)[i].iName);
       
   146 		User::LeaveIfError(fRo.Open(aFs, filePath, EFileRead | EFileShareReadersOnly));
       
   147 		CleanupClosePushL(fRo);
       
   148 		stream.Attach(fRo);
       
   149 		CleanupClosePushL(stream);
       
   150 		ro = CSraRightsObject::NewL(stream);
       
   151 		CleanupStack::PushL(ro);
       
   152 		if(aCid.Compare(ro->ContentId()->Des()) == 0)
       
   153 			{
       
   154 			CleanupStack::Pop(ro);
       
   155 			aRo = ro;
       
   156 			CleanupStack::PopAndDestroy(2, &fRo);
       
   157 			break;
       
   158 			}
       
   159 		else
       
   160 			{
       
   161 			CleanupStack::PopAndDestroy(3, &fRo);		
       
   162 			}//if
       
   163 		}//for
       
   164 	CleanupStack::PopAndDestroy(entryList);
       
   165 	}
       
   166 
       
   167 EXPORT_C void DoSetRightsObjectL(RFs& aFs, CSdpMediaField& aSdp, CSraRightsObject*& aRo, const TDesC& aPrivateFolder)
       
   168 /**
       
   169  	Finds and loads the Rights Object specified in the SDP given.
       
   170  */
       
   171 	{
       
   172 	// Find the rights object if it is defined in the SDP
       
   173 	CSdpAttributeField* roAttrField = NULL;
       
   174 	TInt count = aSdp.AttributeFields().Count();
       
   175 	if(count <= 0)
       
   176 		{
       
   177 		// No rights object is defined in the SDP. 
       
   178 		// The agent cannot service without any rights object.
       
   179 		User::Leave(KErrNotSupported);
       
   180 		}
       
   181 
       
   182 	// The content Id of the rights object is defined with the following string.
       
   183 	_LIT8(KSdpAttributeRightsObject,"baseCID");
       
   184 	RStringPool pool = SdpCodecStringPool::StringPoolL();
       
   185 	// Create a string for the content id by using the current string pool.
       
   186 	RStringF roAttribute = pool.OpenFStringL(KSdpAttributeRightsObject);
       
   187 	CleanupClosePushL(roAttribute);
       
   188 		
       
   189 	TInt i = 0;
       
   190 	for(i=0; i<count; ++i)
       
   191 		{
       
   192 		if((aSdp.AttributeFields()[i]->Attribute()) == roAttribute)
       
   193 			{
       
   194 			// The attribute of the content id has been found.
       
   195 			roAttrField = aSdp.AttributeFields()[i];
       
   196 			break;
       
   197 			}//if
       
   198 		}//for
       
   199 	CleanupStack::PopAndDestroy(&roAttribute);
       
   200 	
       
   201 	if(roAttrField) 
       
   202 		{//the rights object is defined in the SDP
       
   203 		FindAndLoadRightsObjectL(aFs, roAttrField->Value(), aPrivateFolder, aRo);
       
   204 		}
       
   205 	}
       
   206 
       
   207 EXPORT_C void DoSetSdpMediaFieldL(RFs& aFs, CSdpMediaField*& aSdp, CSraRightsObject*& aRo, const TDesC8& aSdpData, const TDesC& aPrivateFolder)
       
   208 /**
       
   209  	Generates a Service Description Protocol (SDP) object from the given SDP data.
       
   210  	
       
   211  	@param aFs The file server handle.
       
   212  	@param aSdp The session description protocol object generated by this function.
       
   213  	@param aRo Rights Object which is found in the private folder of the agent.
       
   214  	@param aSdpData The SDP data sent by the client.
       
   215  	@param aPrivateFolder The private folder of the agent.
       
   216   */
       
   217 	{
       
   218 	ASSERT(!aSdp);
       
   219 	// Decode the received message into an SDP object
       
   220 	aSdp = CSdpMediaField::DecodeL(aSdpData,ETrue);
       
   221 	// Set the specified RO
       
   222 	DoSetRightsObjectL(aFs, *aSdp, aRo, aPrivateFolder);
       
   223 	}