obex/obexprotocol/obex/test/tobex/serverpacketaccessui.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2005-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 #include "serverpacketaccessui.h"
       
    17 
       
    18 #include <e32cons.h>
       
    19 #include <obex/extensionapis/obexserverpacketaccessextension.h>
       
    20 #include <obexserver.h>
       
    21 #include <obexheaders.h>
       
    22 #include "serverhandlerbase.h"
       
    23 
       
    24 
       
    25 CObexServerPacketAccessUi* CObexServerPacketAccessUi::NewL(CObexServerHandlerBase& aHandler, CObexServer& aServer)
       
    26 	{
       
    27 	CObexServerPacketAccessUi* self = new (ELeave) CObexServerPacketAccessUi(aHandler, aServer);
       
    28 	CleanupStack::PushL(self);
       
    29 	self->ConstructL();
       
    30 	CleanupStack::Pop(self);
       
    31 	return self;
       
    32 	}
       
    33 	
       
    34 CObexServerPacketAccessUi::~CObexServerPacketAccessUi()
       
    35 	{
       
    36 	Cancel();
       
    37 	
       
    38 	delete iPacketAccess;
       
    39 	delete iConsole;
       
    40 	}
       
    41 
       
    42 CObexServerPacketAccessUi::CObexServerPacketAccessUi(CObexServerHandlerBase& aHandler, CObexServer& aServer)
       
    43 	: CActive(EPriorityNormal), iHandler(aHandler), iServer(aServer)
       
    44 	{
       
    45 	CActiveScheduler::Add(this);
       
    46 	}
       
    47 	
       
    48 void CObexServerPacketAccessUi::ConstructL()
       
    49 	{	
       
    50 	TRAPD(err, iConsole = Console::NewL(_L("Request Packets"),TSize(50,40)));
       
    51 	if (err != KErrNone)
       
    52 		{
       
    53 		TRAPD(err, iConsole = Console::NewL(_L("Request Packets"),TSize(50,15)));
       
    54 		if (err != KErrNone)
       
    55 			{
       
    56 			// we had a problem creating the console, probably because it was too big
       
    57 			// so try again and make it full screen. If we leave this time then just
       
    58 			// go with it.
       
    59 			iConsole = Console::NewL(_L("Request Packets"),TSize(KConsFullScreen,KConsFullScreen));
       
    60 			}
       
    61 		}
       
    62 	iPacketAccess = CObexServerPacketAccessExtension::NewL(iServer, *this);
       
    63 	
       
    64 	SetKeypressActive();
       
    65 	}
       
    66 	
       
    67 void CObexServerPacketAccessUi::RunL()
       
    68 	{
       
    69     TRAPD(err, ProcessKeyPressL(TChar(iConsole->KeyCode())));
       
    70 	if(err != KErrNone)
       
    71 		{
       
    72 		iConsole->Printf(_L("Error: %d\r\n"),err);
       
    73 		}
       
    74 	SetKeypressActive();
       
    75 	}
       
    76 
       
    77 void CObexServerPacketAccessUi::DoCancel()
       
    78 	{
       
    79 	iConsole->ReadCancel();
       
    80 	}
       
    81 	
       
    82 void CObexServerPacketAccessUi::SetKeypressActive()
       
    83 	{
       
    84 	iConsole->Read(iStatus); 
       
    85 	SetActive();
       
    86 	}
       
    87 	
       
    88 void CObexServerPacketAccessUi::ProcessKeyPressL(TChar aChar)
       
    89 	{
       
    90 	if (aChar == EKeyEscape)
       
    91 		{
       
    92 		iHandler.PacketAccessUiL(EFalse);
       
    93 		return;
       
    94 		}
       
    95 
       
    96 	switch (aChar)
       
    97 		{
       
    98 		case 'E': case 'e': // Enable
       
    99 			{
       
   100 			delete iPacketAccess;
       
   101 			iPacketAccess = CObexServerPacketAccessExtension::NewL(iServer, *this);
       
   102 			}
       
   103 			break;
       
   104 		case 'D': case 'd': // Disable
       
   105 			{
       
   106 			delete iPacketAccess;
       
   107 			iPacketAccess = NULL;
       
   108 			}
       
   109 			break;
       
   110 		case 'I': case 'i': // Interactive mode toggle
       
   111 			{
       
   112 			iInteractive = !iInteractive;
       
   113 			}
       
   114 			break;
       
   115 		default:
       
   116 			{
       
   117 			iConsole->Printf(_L("Invalid Key - valid keys 'e','d','i','Esc'\r\n"));
       
   118 			}
       
   119 			break;
       
   120 		}
       
   121 	}
       
   122 	
       
   123 TBool CObexServerPacketAccessUi::RequestPacket(TObexRequestCode aRequest, TObexResponse& aResponse)
       
   124 	{
       
   125 	TBool requestPacketResult(ETrue);
       
   126 	
       
   127 	iConsole->Printf(_L("Request code %d\r\n"), aRequest);
       
   128 	
       
   129 	// Use Packet Headers now
       
   130 	CObexHeaderSet* headerSet = NULL;
       
   131 	iServer.PacketHeaders(headerSet);
       
   132 	if (headerSet)
       
   133 		{
       
   134 		// Ensures that if the function leaves then the only option is to 
       
   135 		// report a false operation.  As RequestPacket function signiture is published partner
       
   136 		TRAPD(err,PrintHeadersL(*headerSet));
       
   137 		if (err != KErrNone)
       
   138 			{
       
   139 			requestPacketResult = EFalse;						
       
   140 			}
       
   141 		delete headerSet;
       
   142 		}
       
   143 	
       
   144 	if (requestPacketResult)
       
   145 		{
       
   146 		requestPacketResult = RequestPacketReturn(aResponse);
       
   147 		}
       
   148 		
       
   149 	// Provide function result to caller	
       
   150 	return requestPacketResult;
       
   151 	}
       
   152 	
       
   153 TBool CObexServerPacketAccessUi::RequestPacket(TObexRequestCode aRequest, TObexConnectInfo& aConnectInfo, TObexResponse& aResponse)
       
   154 	{
       
   155 	TBool requestPacketResult(ETrue);
       
   156 	
       
   157 	iConsole->Printf(_L("Request code %d - Connect\r\n"), aRequest);
       
   158 	iConsole->Printf(_L("\tMajor version=%d, Minor version=%d\r\n"), aConnectInfo.VersionMajor(), aConnectInfo.VersionMinor());
       
   159 	iConsole->Printf(_L("\tConnection flags=0x%02x\r\n"), aConnectInfo.iFlags);
       
   160 	if (aConnectInfo.iWho.Length() > 0)
       
   161 		{
       
   162 		iConsole->Printf(_L("\tWho attribute=%S\r\n"), &(aConnectInfo.iWho));
       
   163 		}
       
   164 	if (aConnectInfo.iTargetHeader.Length() > 0)
       
   165 		{
       
   166 		iConsole->Printf(_L("\tTarget header=%S\r\n"), &(aConnectInfo.iTargetHeader));
       
   167 		}
       
   168 		
       
   169 	// Use Packet Headers now
       
   170 	CObexHeaderSet* headerSet = NULL;
       
   171 	iServer.PacketHeaders(headerSet);
       
   172 	if (headerSet)
       
   173 		{
       
   174 		// Ensures that if the function leaves then the only option is to 
       
   175 		// report a false operation.  As RequestPacket function signiture is published partner
       
   176 		TRAPD(err,PrintHeadersL(*headerSet));
       
   177 		if (err != KErrNone)
       
   178 			{
       
   179 			requestPacketResult = EFalse;						
       
   180 			}
       
   181 		delete headerSet;
       
   182 		}
       
   183 			
       
   184 	if (requestPacketResult)
       
   185 		{
       
   186 		requestPacketResult = RequestPacketReturn(aResponse);
       
   187 		}
       
   188 		
       
   189 	// Provide function result to caller
       
   190 	return requestPacketResult;
       
   191 	}
       
   192 	
       
   193 TBool CObexServerPacketAccessUi::RequestPacket(TObexRequestCode aRequest, CObex::TSetPathInfo& aSetPathInfo, TObexResponse& aResponse)
       
   194 	{
       
   195 	TBool requestPacketResult(ETrue);
       
   196 			
       
   197 	iConsole->Printf(_L("Request code %d - SetPath\r\n"), aRequest);
       
   198 	iConsole->Printf(_L("\tParent=%d\r\n"), aSetPathInfo.Parent());
       
   199 	iConsole->Printf(_L("\tSetPath flags=0x%02x, constants=0x%02x\r\n"), aSetPathInfo.iFlags, aSetPathInfo.iConstants);
       
   200 	if (aSetPathInfo.iNamePresent)
       
   201 		{
       
   202 		iConsole->Printf(_L("\tDestination directory=%S\r\n"), &(aSetPathInfo.iName));
       
   203 		}
       
   204 	
       
   205 	// Use Packet Headers now
       
   206 	CObexHeaderSet* headerSet = NULL;
       
   207 	iServer.PacketHeaders(headerSet);
       
   208 	if (headerSet)
       
   209 		{
       
   210 		// Ensures that if the function leaves then the only option is to 
       
   211 		// report a false operation.  As RequestPacket function signiture is published partner
       
   212 		TRAPD(err,PrintHeadersL(*headerSet));
       
   213 		if (err != KErrNone)
       
   214 			{
       
   215 			requestPacketResult = EFalse;						
       
   216 			}
       
   217 		delete headerSet;
       
   218 		}
       
   219 	
       
   220 	if (requestPacketResult)
       
   221 		{
       
   222 		requestPacketResult = RequestPacketReturn(aResponse);
       
   223 		}
       
   224 	
       
   225 	// Provide function result to caller
       
   226 	return requestPacketResult; 
       
   227 	}
       
   228 	
       
   229 TBool CObexServerPacketAccessUi::RequestPacketReturn(TObexResponse& aResponse)
       
   230 	{
       
   231 	if (iInteractive)
       
   232 		{
       
   233 		TKeyCode key(EKeyNull);
       
   234 		TBuf<2> buf;
       
   235 		aResponse = ERespBadRequest; 
       
   236 		
       
   237 		Cancel(); // cancel getting key presses will in "submenu"
       
   238 		
       
   239 		iConsole->Printf(_L("Adandon Packet? (y/n) "));
       
   240 		while (key != 'y' && key != 'Y' && key != 'n' && key != 'N')
       
   241 			{
       
   242 			key = iConsole->Getch();
       
   243 			}
       
   244 		iConsole->Printf(_L("%c"),key);
       
   245 		if (key == 'n' || key == 'N')
       
   246 			{
       
   247 			SetKeypressActive(); // reactivate key handling
       
   248 			iConsole->Printf(_L("\r\n"));
       
   249 			return ETrue;
       
   250 			}
       
   251 		
       
   252 		// If here then we are abandoning the packet
       
   253 		iConsole->Printf(_L("\r\nResponse to send? 0x"));
       
   254 		
       
   255 		while ((key = iConsole->Getch()) != EKeyEnter)
       
   256 			{
       
   257 			if (key == EKeyBackspace && buf.Length()!=0)
       
   258 				{
       
   259 				buf.SetLength(buf.Length()-1);
       
   260 				}
       
   261 			else if( buf.Length() < buf.MaxLength())
       
   262 				{
       
   263 				buf.Append(key);
       
   264 				}
       
   265 			else 
       
   266 				continue;
       
   267 			iConsole->Printf(_L("%c"),key);
       
   268 			}
       
   269 		iConsole->Printf(_L("\r\n"));
       
   270 		
       
   271 		TLex lex(buf);
       
   272 		TUint value;
       
   273 		TInt lex_err = lex.Val(value, EHex); 
       
   274 		
       
   275 		if(lex_err == KErrNone)
       
   276 			{
       
   277 			aResponse = static_cast<TObexResponse>(value);
       
   278 			}
       
   279 		else 
       
   280 			{
       
   281 			iConsole->Printf(_L("Input parsing failed, use ERespBadRequest as default response\r\n"));
       
   282 			}
       
   283 		SetKeypressActive(); // reactivate key handling
       
   284 		return EFalse;
       
   285 		}
       
   286 	else
       
   287 		{
       
   288 		return ETrue;
       
   289 		}
       
   290 	}
       
   291 	
       
   292 CConsoleBase& CObexServerPacketAccessUi::Console() const
       
   293 	{
       
   294 	return *iConsole;
       
   295 	}
       
   296 	
       
   297 void CObexServerPacketAccessUi::PrintHeadersL(CObexHeaderSet& aHeaderSet) const
       
   298 	{
       
   299 	CObexHeader* header = CObexHeader::NewL();
       
   300 	CleanupStack::PushL(header);
       
   301 	
       
   302 	aHeaderSet.First();
       
   303 	TInt err(aHeaderSet.This(header));
       
   304 	if (err == KErrNone)
       
   305 		{
       
   306 		iConsole->Printf(_L("Packet header contents...\r\n"));
       
   307 		}
       
   308 	while (err == KErrNone)
       
   309 		{
       
   310 		// get the next header
       
   311 		err = aHeaderSet.This(header);
       
   312 		if (err != KErrNone)
       
   313 			{
       
   314 			break;
       
   315 			}
       
   316 		
       
   317 		PrintHeader(*header);
       
   318 		
       
   319 		err = aHeaderSet.Next();
       
   320 		}
       
   321 		
       
   322 	CleanupStack::PopAndDestroy(header);
       
   323 	}
       
   324 	
       
   325 void CObexServerPacketAccessUi::PrintHeader(CObexHeader& aHeader) const
       
   326 	{
       
   327 		// appropriately print the header
       
   328 		iConsole->Printf(_L("\tHI: %d\r\n"),aHeader.HI());
       
   329 		
       
   330 		CObexHeader::THeaderType type = aHeader.Type();
       
   331 		switch(type)
       
   332 			{
       
   333 			case CObexHeader::EUnicode:
       
   334 				{
       
   335 				const TDesC16& headerdes = aHeader.AsUnicode();
       
   336 				const TUint8* bytes = reinterpret_cast<const TUint8*>(headerdes.Ptr());
       
   337 				PrintHex(bytes, headerdes.Size());
       
   338 				}		
       
   339 				break;
       
   340 			case CObexHeader::EByteSeq:
       
   341 				{
       
   342 				const TDesC8& headerbyteseq = aHeader.AsByteSeq();
       
   343 				PrintHex(headerbyteseq.Ptr(), headerbyteseq.Size());
       
   344 				}
       
   345 				break;
       
   346 			case CObexHeader::EByte:
       
   347 				{
       
   348 				const TUint8 headerbyte = aHeader.AsByte();
       
   349 				PrintHex(&headerbyte, 1);
       
   350 				}
       
   351 				break;
       
   352 			case CObexHeader::EFourByte:
       
   353 				{
       
   354 				const TUint32 headerbytes = aHeader.AsFourByte();
       
   355 				const TUint8* bytes = reinterpret_cast<const TUint8*>(&headerbytes);
       
   356 				PrintHex(bytes, 4);
       
   357 				}
       
   358 				break;
       
   359 			default:
       
   360 				// panic here maybe??
       
   361 				break;
       
   362 			}
       
   363 	}
       
   364 	
       
   365 void CObexServerPacketAccessUi::PrintHex(const TUint8* aPtr, TInt aLength) const
       
   366 	{
       
   367 	// local constants
       
   368 	const TUint32 widthC = 8;
       
   369 	_LIT(KPrintHexByte, "%02x ");
       
   370 	_LIT(KPrintHexTab, "\t");
       
   371 	_LIT(KPrintHexChar, "%c ");
       
   372 	_LIT(KPrintHexNonchar, ". ");
       
   373 	_LIT(KPrintHexNewline, "\r\n");
       
   374 	_LIT(KPrintHex2Spaces, "  ");
       
   375 	_LIT(KPrintHex3Spaces, "   ");
       
   376 	
       
   377 	// function begins...
       
   378 	TInt length = 0;
       
   379 	TUint32 cursor = 0;
       
   380 	
       
   381 	TUint8 printArray[widthC];
       
   382 	
       
   383 	while (length < aLength)
       
   384 		{
       
   385 		printArray[cursor] = *aPtr;
       
   386 		
       
   387 		// move along.
       
   388 		aPtr++;
       
   389 		length++;
       
   390 		cursor = length % widthC;
       
   391 		
       
   392 		if (cursor == 0)
       
   393 			{
       
   394 			// we have a full array so print it.
       
   395 			iConsole->Printf(KPrintHexTab());
       
   396 			for (TUint i=0;i<widthC;i++)
       
   397 				{
       
   398 				iConsole->Printf(KPrintHexByte(),printArray[i]);
       
   399 				}
       
   400 			iConsole->Printf(KPrintHexTab());
       
   401 			for (TUint i=0;i<widthC;i++)
       
   402 				{
       
   403 				TUint8 c = printArray[i];
       
   404 				if (c >= 32 && c <= 126)
       
   405 					{
       
   406 					iConsole->Printf(KPrintHexChar(), c);
       
   407 					}
       
   408 				else
       
   409 					{
       
   410 					iConsole->Printf(KPrintHexNonchar());
       
   411 					}
       
   412 				}
       
   413 			iConsole->Printf(KPrintHexNewline());
       
   414 			}
       
   415 		}
       
   416 		
       
   417 		// if we haven't completely finished a set -> finish it
       
   418 		if (cursor > 0)
       
   419 			{
       
   420 			iConsole->Printf(KPrintHexTab());
       
   421 			for (TUint i=0;i<widthC;i++)
       
   422 				{
       
   423 				if (i >= cursor)
       
   424 					{
       
   425 					iConsole->Printf(KPrintHex3Spaces());
       
   426 					}
       
   427 				else
       
   428 					{
       
   429 					iConsole->Printf(KPrintHexByte(),printArray[i]);
       
   430 					}
       
   431 				}
       
   432 			iConsole->Printf(KPrintHexTab());
       
   433 			for (TUint i=0;i<widthC;i++)
       
   434 				{
       
   435 				if (i >= cursor)
       
   436 					{
       
   437 					iConsole->Printf(KPrintHex2Spaces());
       
   438 					}
       
   439 				else
       
   440 					{
       
   441 					TUint8 c = printArray[i];
       
   442 					if (c >= 32 && c <= 126)
       
   443 						{
       
   444 						iConsole->Printf(KPrintHexChar(), c);
       
   445 						}
       
   446 					else
       
   447 						{
       
   448 						iConsole->Printf(KPrintHexNonchar());
       
   449 						}
       
   450 					}
       
   451 				}
       
   452 			iConsole->Printf(KPrintHexNewline());
       
   453 			}
       
   454 			
       
   455 		// should be printed now
       
   456 	}