usbmgmt/usbmgrtest/ObexClassController/test/src/simpleObexApp.cpp
changeset 0 c9bc50fca66e
child 15 f92a4f87e424
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2005-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 "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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <c32comm.h>
       
    21 
       
    22 #include <usbman.h>
       
    23 
       
    24 #include "simpleObexApp.h"
       
    25 #include "simpleObexClient.h"
       
    26 #include "simpleObexServer.h"
       
    27 
       
    28 
       
    29 // Define Physical Device Driver and Logical Device Driver
       
    30 // for device.
       
    31 
       
    32 
       
    33 
       
    34 /**
       
    35  * Constructor.
       
    36  */
       
    37 CActiveConsole::CActiveConsole(CConsoleBase* aConsole) 
       
    38 	: CActive(CActive::EPriorityStandard),
       
    39 	  iConsole(aConsole),
       
    40 	iMode(E_Inactive)
       
    41 
       
    42 	{
       
    43 	}
       
    44 
       
    45 /**
       
    46  * NewLC function, calls 2nd phase constructor.
       
    47  */
       
    48 CActiveConsole* CActiveConsole::NewLC(CConsoleBase* aConsole)
       
    49     {
       
    50     CActiveConsole* self = new (ELeave) CActiveConsole(aConsole);
       
    51 	CleanupStack::PushL(self);
       
    52 	self->ConstructL();
       
    53     return self;
       
    54     }
       
    55 
       
    56 
       
    57 /**
       
    58  * 2nd phase construction.
       
    59  */
       
    60 void CActiveConsole::ConstructL ()
       
    61     { 
       
    62       
       
    63     CActiveScheduler::Add(this);// Add to active scheduler
       
    64 
       
    65     }
       
    66 
       
    67 
       
    68 
       
    69 /**
       
    70  * Destructor.
       
    71  */
       
    72 CActiveConsole::~CActiveConsole()
       
    73 	{
       
    74 	// Make sure we're cancelled
       
    75 	Cancel();
       
    76 
       
    77 	// safe to delete NULL
       
    78 	
       
    79     if(iObexClientHandler)
       
    80 		delete iObexClientHandler;
       
    81     if(iObexServerHandler)
       
    82 		delete iObexServerHandler;
       
    83  
       
    84 	}
       
    85 
       
    86 /**
       
    87  * RequestCharacter function, this is responsible for diplasying a menu to the user.
       
    88  */
       
    89 void CActiveConsole::RequestCharacter()
       
    90     {
       
    91     
       
    92 		if (iMode == E_Inactive) 
       
    93 		{	
       
    94 			// Initial menu
       
    95 			iConsole->Printf(_L("----------------------------------------\n"));
       
    96 			iConsole->Printf(_L("|~~~~~~~~~~~~~~~~~OBEX~~~~~~~~~~~~~~~~~|\n"));
       
    97 			iConsole->Printf(_L("---------------------------------------\n"));
       
    98 			iConsole->Printf(_L("|-----------Bluetooth options----------|\n"));
       
    99 			iConsole->Printf(_L("---------------------------------------\n"));
       
   100 			iConsole->Printf(_L("| 1 - Start Obex Server over Bluetooth |\n"));
       
   101 			iConsole->Printf(_L("| 2 - Start Obex Client over Bluetooth |\n"));
       
   102 			iConsole->Printf(_L("---------------------------------------\n"));
       
   103 			iConsole->Printf(_L("|-------------IrDA options-------------|\n"));
       
   104 			iConsole->Printf(_L("---------------------------------------\n"));
       
   105 			iConsole->Printf(_L("|   3 - Start Obex Server over IrDA    |\n"));
       
   106 			iConsole->Printf(_L("|   4 - Start Obex Client over IrDA    |\n"));
       
   107 			iConsole->Printf(_L("---------------------------------------\n"));
       
   108 			iConsole->Printf(_L("|--------------USB options-------------| \n"));
       
   109 			iConsole->Printf(_L("---------------------------------------\n"));
       
   110 			iConsole->Printf(_L("|    5 - Start Obex Server over USB    |\n"));
       
   111 			iConsole->Printf(_L("|    6 - Start Obex Client over USB    |\n"));
       
   112 			iConsole->Printf(_L("---------------------------------------\n"));
       
   113 		
       
   114 		}
       
   115 		
       
   116 		if (iMode == E_Server)
       
   117 			{
       
   118 			// Menu displayed when Start server is called
       
   119 			iConsole->Printf(_L("\n"));
       
   120 			iConsole->Printf(_L("----------------------------------------\n"));
       
   121 			iConsole->Printf(_L("|~~~~~~~~~OBEX_SERVER_OPTIONS~~~~~~~~~~|\n"));
       
   122 			iConsole->Printf(_L("----------------------------------------\n"));
       
   123 			iConsole->Printf(_L("|   S - Stop and kill the Obex Server  |\n"));
       
   124 			iConsole->Printf(_L("----------------------------------------\n"));
       
   125 			iConsole->Printf(_L("|      1 - Enable authentication       |\n"));
       
   126 			iConsole->Printf(_L("----------------------------------------\n"));
       
   127 			iConsole->Printf(_L("|      2 - Disable authentication      |\n"));
       
   128 			iConsole->Printf(_L("----------------------------------------\n"));
       
   129 			}
       
   130 		
       
   131 		
       
   132 		
       
   133 		if (iMode == E_Client)
       
   134 			{
       
   135 			// Menu displayed when start client is started
       
   136 			iConsole->Printf(_L("\n"));
       
   137 			iConsole->Printf(_L("----------------------------------------\n"));
       
   138 			iConsole->Printf(_L("|~~~~~~~~~OBEX_CONNECT_OPTIONS~~~~~~~~~|\n"));
       
   139 			iConsole->Printf(_L("----------------------------------------\n"));
       
   140 			iConsole->Printf(_L("|        1  -  OBEX connect menu       |\n"));
       
   141 			iConsole->Printf(_L("----------------------------------------\n"));
       
   142 			iConsole->Printf(_L("|         2  -  Disconnect Obex        |\n"));
       
   143 			iConsole->Printf(_L("----------------------------------------\n"));
       
   144 			iConsole->Printf(_L("|    3  -  Obex Put obj 1 : %S |\n"), &(iObexClientHandler->iFilename1));
       
   145 			iConsole->Printf(_L("----------------------------------------\n"));
       
   146 			iConsole->Printf(_L("|    4  -  Obex Put obj 2 : %S |\n"), &(iObexClientHandler->iFilename2));
       
   147 			iConsole->Printf(_L("----------------------------------------\n"));
       
   148 			iConsole->Printf(_L("|    5  -  Obex Put obj 3 : %S |\n"), &(iObexClientHandler->iFilename3));
       
   149 			iConsole->Printf(_L("----------------------------------------\n"));
       
   150 			iConsole->Printf(_L("|       6  -  Obex Get by name         |\n"));
       
   151 			iConsole->Printf(_L("----------------------------------------\n"));
       
   152 			}
       
   153 		
       
   154 		
       
   155 		
       
   156 		if (iMode == E_Client_Connect_Menu )
       
   157 			{
       
   158 			// Obex connect menu
       
   159 			iConsole->Printf(_L("\n"));
       
   160 			iConsole->Printf(_L("----------------------------------------\n"));
       
   161 			iConsole->Printf(_L("|~~~~~~~~~~~OBEX_CONNECT_MENU~~~~~~~~~~|\n"));
       
   162 			iConsole->Printf(_L("----------------------------------------\n"));
       
   163 			iConsole->Printf(_L("|       0  -  IrObex Disconnect        |\n"));
       
   164 			iConsole->Printf(_L("----------------------------------------\n"));
       
   165 			iConsole->Printf(_L("|         1  -  IrObex Connect         |\n"));
       
   166 			iConsole->Printf(_L("----------------------------------------\n"));
       
   167 			iConsole->Printf(_L("|   2  -  Connect with Authentication  |\n"));
       
   168 			iConsole->Printf(_L("----------------------------------------\n"));
       
   169 			iConsole->Printf(_L("|     s  -  Back to Client Menu        |\n"));
       
   170 			iConsole->Printf(_L("----------------------------------------\n"));
       
   171 			}
       
   172 		
       
   173 		
       
   174 		iConsole->Printf(_L("press Escape to quit\r\n\r\n"));
       
   175     	iConsole->Read(iStatus); 
       
   176     	SetActive();
       
   177     	
       
   178     }
       
   179 
       
   180 
       
   181 /**
       
   182  * The active object DoCancel function
       
   183  */
       
   184 void  CActiveConsole::DoCancel()
       
   185     {
       
   186     iConsole->ReadCancel();
       
   187     }
       
   188     
       
   189  
       
   190 
       
   191 /**
       
   192  * The active object RunL function. It is responsible for routing the users
       
   193  * selection by calling the ProcessKeyPressL function.
       
   194  */
       
   195 void  CActiveConsole::RunL()
       
   196     {
       
   197     TUint oldMode = iMode;
       
   198 	TRAPD(err,ProcessKeyPressL(TChar(iConsole->KeyCode())));
       
   199 	if(err != KErrNone)
       
   200 		{
       
   201 		iConsole->Printf(_L("Failed. Error: %d\r\n"),err);
       
   202 		// Put the test mode back again
       
   203 		iMode = oldMode;
       
   204 		RequestCharacter();
       
   205 		}	
       
   206     }
       
   207 
       
   208 
       
   209 /**
       
   210  * This function is called after the user has made a selection.
       
   211  */
       
   212 void CActiveConsole::ProcessKeyPressL(TChar aChar)
       
   213     {
       
   214     
       
   215     if (aChar == EKeyEscape)
       
   216 		{
       
   217 		CActiveScheduler::Stop();
       
   218 		return;
       
   219 		}
       
   220 		
       
   221 	else if (iMode == E_Inactive)
       
   222 		{
       
   223 		switch(aChar)
       
   224 			{
       
   225 			case '1':
       
   226 				//Obex Server over Bluetooth
       
   227 				iMode = E_Server;
       
   228 				iTransport = EBluetooth;
       
   229 				iObexServerHandler = CObexServerHandler::NewL(this, EBluetooth);
       
   230 				break;
       
   231 			
       
   232 			case '2':
       
   233 				//Obex Client over Bluetooth
       
   234 				iMode = E_SdpQuery;
       
   235 				iTransport = EBluetooth;
       
   236 				iObexClientHandler = CObexClientHandler::NewL(this, EBluetooth);
       
   237 				break;
       
   238 			
       
   239 			case '3':
       
   240 			
       
   241 				//Obex server over IrDA
       
   242 				iMode = E_Server;
       
   243 				iTransport = EIrda;
       
   244 				iObexServerHandler = CObexServerHandler::NewL(this, EIrda);
       
   245 				break;
       
   246 			
       
   247 			case '4':
       
   248 				//Obex client over IrDA
       
   249 				iMode = E_Client;
       
   250 				iTransport = EIrda;
       
   251 				iObexClientHandler = CObexClientHandler::NewL(this, EIrda);
       
   252 				break;
       
   253 			
       
   254 			case '5':
       
   255 				{
       
   256 				// Obex server over USB.
       
   257 				iMode = E_Server;
       
   258 				
       
   259 				#ifdef __WINS__
       
   260 					// If starting usb on PC emulator side
       
   261 					iTransport = EWin32Usb;
       
   262 					iObexServerHandler = CObexServerHandler::NewL(this, EWin32Usb);
       
   263 				#else
       
   264 					// This is where the Class controller code provided is implemented	
       
   265 					RUsb usb;
       
   266 					TRequestStatus status;
       
   267 					TInt obexPersonality = 1;
       
   268 					User::LeaveIfError(usb.Connect());
       
   269 					// Start the OBEX personality that will
       
   270 					// start up an OBEX server.
       
   271 					usb.TryStop(status);
       
   272 					User::WaitForRequest(status);
       
   273 					usb.TryStart(obexPersonality, status);
       
   274 					User::WaitForRequest(status);
       
   275 	
       
   276 				#endif
       
   277 				}
       
   278 				break;
       
   279 			case '6':
       
   280 				{
       
   281 				iMode = E_Client;
       
   282 				
       
   283 				#ifdef __WINS__
       
   284 					// If starting client on PC emulator
       
   285 					iTransport = EWin32Usb;
       
   286 					iObexClientHandler = CObexClientHandler::NewL(this, EWin32Usb);
       
   287 				#else
       
   288 					iTransport = EUsb;
       
   289 					DoUsbInitialisationL();
       
   290 					iObexClientHandler = CObexClientHandler::NewL(this, EUsb);
       
   291 					User::LeaveIfError(iUsbDriver.DeviceConnectToHost());// Plugin driver
       
   292 					iUsbDriver.Close();
       
   293 				#endif
       
   294 				}
       
   295 			default:
       
   296 				break; 
       
   297 			
       
   298 			}
       
   299 		
       
   300 		}
       
   301 	
       
   302 	else if (iMode == E_Client_Connect_Menu )
       
   303 		{
       
   304 		switch (aChar)
       
   305 			{
       
   306 			case '0':
       
   307 				// Disconnect Obex connection
       
   308 				iObexClientHandler->Disconnect();
       
   309 				break;
       
   310 		
       
   311 			case '1':
       
   312 				// Start Obex connection
       
   313 				iObexClientHandler->Connect();
       
   314 				break;
       
   315 		
       
   316 			case '2':
       
   317 			// Start Obex connection with authentication
       
   318 			iObexClientHandler->ConnectWithAuthenticationL();		
       
   319 			
       
   320 			default:
       
   321 				iMode = E_Client;
       
   322 				break;
       
   323 			}
       
   324 		}
       
   325 		
       
   326 	else if (iMode == E_Server)
       
   327 		{
       
   328 		switch(aChar)
       
   329 			{
       
   330 			case 's':
       
   331 				// Stop the Obex server
       
   332 				iObexServerHandler->Stop();
       
   333 				break;
       
   334 			
       
   335 			case '1':
       
   336 				// Force authenication
       
   337 				iObexServerHandler->EnableAuthenticationL();
       
   338 				break;
       
   339 			
       
   340 			case '2':
       
   341 				// Stop the need to authenicate
       
   342 				iObexServerHandler->DisableAuthentication();
       
   343 			
       
   344 			default:
       
   345 				break;
       
   346 		
       
   347 			}
       
   348 		
       
   349 		
       
   350 		}
       
   351 		
       
   352 	else if (iMode == E_Client)
       
   353 		{
       
   354 		switch(aChar)
       
   355 			{
       
   356 		
       
   357 			case '1':
       
   358 				iMode = E_Client_Connect_Menu;				
       
   359 				break;
       
   360 			
       
   361 			case '2':
       
   362 				// Disconnect client and server link
       
   363 				iObexClientHandler->Disconnect();
       
   364 				break;
       
   365 			
       
   366 			case '3':
       
   367 				// Put iFilename1 object
       
   368 				iObexClientHandler->Put(iObexClientHandler->iFilename1);
       
   369 				break;
       
   370 			
       
   371 			case '4':
       
   372 				// Put iFilename2 object
       
   373 				iObexClientHandler->Put(iObexClientHandler->iFilename2);
       
   374 				break;
       
   375 			
       
   376 			case '5':
       
   377 				// Put iFilename3 object
       
   378 				iObexClientHandler->Put(iObexClientHandler->iFilename3);
       
   379 				break;
       
   380 			
       
   381 			case '6':
       
   382 				// Call get by name on active object
       
   383 				iObexClientHandler->GetByNameL();
       
   384 			default:
       
   385 				break;
       
   386 			
       
   387 			}
       
   388 		}
       
   389 	
       
   390 	else 
       
   391 		{
       
   392 		CActiveScheduler::Stop();
       
   393 		return;
       
   394 		}
       
   395 	// Dislpay next menu
       
   396 	RequestCharacter ();
       
   397 	return;
       
   398     
       
   399     }
       
   400 
       
   401 
       
   402 /**
       
   403  * Function used in creating a new active console.
       
   404  */
       
   405 CConsoleBase* CActiveConsole::Console()
       
   406 	{
       
   407 	return iConsole;
       
   408 	}
       
   409 
       
   410 
       
   411 void CActiveConsole::DoUsbInitialisationL()
       
   412 	{
       
   413 	// Before creating an Obex client or server some initialisation
       
   414 	// is needed. 
       
   415 		
       
   416 	// Load the USB device driver
       
   417 	User::LeaveIfError(User::LoadLogicalDevice(KEusbc));
       
   418 		
       
   419 	// Now connect to USBmanager		
       
   420 	RUsb usb;
       
   421 	User::LeaveIfError(usb.Connect());
       
   422 	iConsole->Printf(_L("Connected to USB\n"));
       
   423 	// Before starting the WHCM USB personality foe OBEX
       
   424 	// we need to stop the current personality.
       
   425 	TRequestStatus status;
       
   426 	usb.TryStop(status);
       
   427 	User::WaitForRequest(status);
       
   428 	User::LeaveIfError(status.Int());
       
   429 	// Now we want to start the WHCM personality
       
   430 	const TUint KWhcmPersonality=2;
       
   431 	usb.TryStart(KWhcmPersonality, status);
       
   432 	User::WaitForRequest(status);
       
   433 			
       
   434 	iConsole->Printf(_L("Starting USB returned %d\n"), status.Int());
       
   435 	User::LeaveIfError(status.Int());
       
   436 	// We are now finished with Usb manager so close. 
       
   437 	usb.Close();
       
   438 		
       
   439 	// We now need to open a channel to a client driver
       
   440 	User::LeaveIfError(iUsbDriver.Open(0));
       
   441 	// Unplug driver to allow for a new CObexClient to be created.
       
   442 	User::LeaveIfError(iUsbDriver.DeviceDisconnectFromHost());
       
   443 
       
   444 	}
       
   445 
       
   446 /**
       
   447  * This is the function called by the entry point code.
       
   448  * It exists to perform initialisation
       
   449  *
       
   450  */
       
   451 void RunAppL(void)
       
   452 	{
       
   453 
       
   454 	CActiveScheduler *myScheduler = new (ELeave) CActiveScheduler();
       
   455 	CleanupStack::PushL(myScheduler);
       
   456 	CActiveScheduler::Install(myScheduler); 
       
   457 
       
   458 	CConsoleBase* aConsole = 	
       
   459 	Console::NewL(_L("Obex Program"),TSize(KConsFullScreen, KConsFullScreen));
       
   460 	
       
   461 	// Load Device drivers
       
   462 	
       
   463 	TInt err;
       
   464 	// Start C32 now
       
   465 	aConsole->Printf(_L("Loading C32...\n"));
       
   466 	err=StartC32();
       
   467 	if (err!=KErrNone && err!=KErrAlreadyExists)
       
   468 		aConsole->Printf(_L("	Failed %d\n"), err);
       
   469 	else
       
   470 		aConsole->Printf(_L("	Sucess\n"));
       
   471 	// If running on PC emulator
       
   472 	
       
   473 	// Load drivers for using Serial communication
       
   474 	#ifdef __WINS__
       
   475 		TInt load =	User::LoadLogicalDevice(KWinsLddName);
       
   476 		aConsole->Printf(_L("Load LDD : %d\n"), load);
       
   477 		load =	User::LoadPhysicalDevice(KWinsPddName);
       
   478 		aConsole->Printf(_L("Load PDD : %d\n"), load);
       
   479 	#endif //__WINS__
       
   480 
       
   481 
       
   482 
       
   483 	CleanupStack::PushL(aConsole);
       
   484 	CActiveConsole* my_console = CActiveConsole::NewLC(aConsole);// New active console
       
   485 	my_console->RequestCharacter();
       
   486 	CActiveScheduler::Start();
       
   487 	CleanupStack::PopAndDestroy(3); 
       
   488 	
       
   489 	}
       
   490 
       
   491 
       
   492 
       
   493 
       
   494 /**
       
   495  * The E32main function is the main entry point for the
       
   496  * code.
       
   497  */
       
   498 
       
   499 TInt E32Main()
       
   500 	{
       
   501 	__UHEAP_MARK;
       
   502 	CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
       
   503 	TRAPD(error,RunAppL()); // more initialization, then do example
       
   504 	__ASSERT_ALWAYS(!error,User::Panic(_L("Simple OBEX Application"),error));
       
   505 	delete cleanup; // destroy clean-up stack
       
   506 	__UHEAP_MARKEND;
       
   507 
       
   508 	return 0; // and return
       
   509 	}