usbmgmt/usbmgrtest/t_ncm/src/tcpcommand.cpp
branchRCL_3
changeset 15 f92a4f87e424
equal deleted inserted replaced
14:d3e8e7d462dd 15:f92a4f87e424
       
     1 /*
       
     2 * Copyright (c) 2002-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 /** @file
       
    19  @internalComponent
       
    20  @test
       
    21  */
       
    22 
       
    23 #include "tcpcommand.h"
       
    24 #include "tcptest.h"
       
    25 #include "ncmtestconsole.h"
       
    26 
       
    27 _LIT(KTcpClientCommandDescription, "Create tcp connection from H4 to PC");
       
    28 _LIT(KTcpServerCommandDescription, "Create tcp connection from PC to H4");
       
    29 _LIT(KUdpClientCommandDescription, "Create udp connection from H4 to PC");
       
    30 _LIT(KUdpServerCommandDescription, "Create udp connection from PC to H4");
       
    31 
       
    32 
       
    33 const TInt KDefaultPortFrom = 5000;
       
    34 
       
    35 CTcpCommand* CTcpCommand::NewL(CUsbNcmConsole& aTestConsole, TUint aKey, TBool aIsTcp, TBool aIsServer)
       
    36 	{
       
    37 	LOG_STATIC_FUNC_ENTRY
       
    38 	
       
    39 	CTcpCommand* self = new(ELeave) CTcpCommand(aTestConsole, aKey, aIsTcp, aIsServer);
       
    40 	CleanupStack::PushL(self);
       
    41 	self->ConstructL();
       
    42 	CleanupStack::Pop(self);
       
    43 	return self;
       
    44 	}
       
    45 CTcpCommand::~CTcpCommand()
       
    46 	{
       
    47 	for(TInt i = 0; i < KMaxTcpTestCount; i++)
       
    48 		{
       
    49 		delete iTestArray[i];
       
    50 		}	
       
    51 	}
       
    52 
       
    53 CTcpCommand::CTcpCommand(CUsbNcmConsole& aTestConsole, TUint aKey, TBool aIsTcp, TBool aIsServer)
       
    54 	: CNcmCommandBase(EPriorityStandard, aTestConsole, aKey), iIsTcp(aIsTcp), iIsServer(aIsServer)
       
    55 	{
       
    56 	}
       
    57 
       
    58 void CTcpCommand::ConstructL()
       
    59     {
       
    60     if (iIsServer)
       
    61         {
       
    62         if (iIsTcp)
       
    63             {
       
    64             SetDescription(KTcpServerCommandDescription());
       
    65             }
       
    66         else
       
    67             {
       
    68             SetDescription(KUdpServerCommandDescription());
       
    69             }
       
    70         }
       
    71     else
       
    72         {
       
    73         if (iIsTcp)
       
    74             {
       
    75             SetDescription(KTcpClientCommandDescription());
       
    76             }
       
    77         else
       
    78             {
       
    79             SetDescription(KUdpClientCommandDescription());
       
    80             }
       
    81         }
       
    82     }
       
    83 
       
    84 void CTcpCommand::DoCommandL()
       
    85 	{
       
    86 	LOG_FUNC
       
    87 	
       
    88 	//Create an CTcpTestConsole object to run the tcp test.
       
    89 	TBool noRun = ETrue;
       
    90 	for(TInt i = 0; i < KMaxTcpTestCount; i++)
       
    91 		{
       
    92 		if(!iTestArray[i])
       
    93 			{
       
    94 			//create the default parameter for tcp test
       
    95 			//  as a server : the parameter is port (Listen at which port)
       
    96 			//  as a client : the parameter is ip and port. IP is got from NCM's IP + 1
       
    97 			//                   (The DHCP server will assign "NCM's IP+1" to PC.)
       
    98 			RBuf addr;
       
    99 			addr.CreateL(30);
       
   100 			
       
   101 			if(!iIsServer)
       
   102 				{
       
   103 				TInetAddr serverIP;
       
   104 				serverIP.Input(_L("192.168.3.100"));
       
   105 				//serverIP.Input(iTestConsole.GetDisplayItem(ENcmConnIpItem));
       
   106 				TUint32 serverAddr = serverIP.Address();
       
   107 				TUint32 hostId = (serverAddr & ~KInetAddrNetMaskC) + 1;
       
   108 				if (hostId >= 255)
       
   109 					 {
       
   110 					 hostId = 1;
       
   111 					 }
       
   112 				TUint32 clientAddr = (serverAddr & KInetAddrNetMaskC) | hostId;
       
   113 				TInetAddr clientIP(clientAddr, 0);
       
   114 				clientIP.Output(addr);			
       
   115 				}
       
   116 			
       
   117 			addr.AppendFormat(_L(" %d"), KDefaultPortFrom+i);
       
   118 			//Create a tcp test console
       
   119 			iTestArray[i] = CTcpTestConsole::NewL(iIsTcp, iIsServer, addr, i, *this);
       
   120 			noRun = EFalse;
       
   121 			addr.Close();
       
   122 			break;
       
   123 			}
       
   124 		}
       
   125 	if(noRun)
       
   126 		{
       
   127 		//The count of tcp test is up to the max value. 
       
   128 		CUsbNcmConsoleEvent* event = CUsbNcmConsoleEvent::NewL();
       
   129 		TInt count = KMaxTcpTestCount;
       
   130 		event->iEvent.AppendFormat(_L("The test connection is up to %d.Please close some."), count);
       
   131 		iTestConsole.NotifyEvent(event);			
       
   132 		}
       
   133 	}
       
   134 
       
   135 void CTcpCommand::CloseTcpTest(TInt aIndex)
       
   136 	{
       
   137 	__ASSERT_ALWAYS(((aIndex < KMaxTcpTestCount) && (aIndex >= 0)), Panic(ENcmArrayBound));
       
   138 
       
   139 	delete iTestArray[aIndex];
       
   140 	iTestArray[aIndex] = NULL;
       
   141 	}
       
   142