cbsref/csyrefplugins/csy27010/src/PortFactory.cpp
branchRCL_3
changeset 65 630d2f34d719
equal deleted inserted replaced
61:17af172ffa5f 65:630d2f34d719
       
     1 //
       
     2 // * Copyright 2004 Neusoft America Inc.
       
     3 // * All rights reserved.
       
     4 // * This component and the accompanying materials are made available
       
     5 // * under the terms of the 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 // * Contributors:
       
    10 // * Keith Collins (Neusoft America Inc.)  original software development and additional code and modifications.
       
    11 // * Thomas Gahagen (Neusoft America Inc.)  additional code and modifications.
       
    12 // * Zhen Yuan (Neusoft America Inc.)  additional code and modifications.
       
    13 // *
       
    14 // * Description:  This file contains the implementation for the class CPortFactory.
       
    15 // *               This class is used to create C32 port interface instances.
       
    16 //
       
    17 
       
    18 // PortFactory.cpp
       
    19 
       
    20 /** @file PortFactory.cpp
       
    21  *
       
    22  */
       
    23 
       
    24 /*! \mainpage CSY 27.010 Documentation
       
    25  *
       
    26  * \section intro Introduction
       
    27  *
       
    28  * This CSY has been designed and implemented by TapRoot Systems.
       
    29  * This CSY is part of the TapRoot Systems Communications Platform (CommP)
       
    30  * product family for Symbian OS. Please contact TapRoot Systems for 
       
    31  * information on CommP.
       
    32  * <p>
       
    33  * A CSY is a specialized type of DLL that implements API interfaces
       
    34  * required by the C32 Serial Communications Server. "CSY" is the file
       
    35  * name extension used by CSY modules as in "ecuart.csy", which is the
       
    36  * name of Symbian's generic CSY for non-multiplexed serial ports.  
       
    37  * TapRoot Systems 27.010 CSY (gsm0701.csy) is compatible with the
       
    38  * 3GPP 27.010 v5.0.0 specification.
       
    39  * <p>
       
    40  * The CSY has been designed and tested with the Intel Lubbock application
       
    41  * processor, TI Condat baseband processor and Symbian OS 8.0a.
       
    42  * The CSY is not guaranteed to work with other AP's, BP's or Symbian OS versions.
       
    43  * The CSY has been integrated with Symbian OS supplied TSY and NIF IP.
       
    44  * The CSY is not guaranteed to work with other TSY's and NIF's.
       
    45  *
       
    46  * \section feat Features
       
    47  *
       
    48  * The CSY supports the 27.010 Basic Option. The 3GPP 27.010 specification 
       
    49  * is derived from the GSM 07.10 specification. 3GPP 27.010 defines a mechanism
       
    50  * whereby multiple commands and data streams can be carried over the same
       
    51  * physical link. This specification defines a multiplexing protocol between
       
    52  * a Terminal Equipment (TE) and a Mobile Station (MS) allowing multiple 
       
    53  * simultaneous sessions over a single start-stop, framed, serial link. 
       
    54  * Each session (channel) can carry different types of data including voice,
       
    55  * fax, data, SMS, CBS, phonebook maintenance, battery status, GPRS, USSD, etc.
       
    56  * <p>
       
    57  * The multiplexer has three operating options, basic, advanced without error
       
    58  * recovery and advanced with error recovery. CSY 27.010 supports the
       
    59  * basic option. The basic option is recommended on highly reliable links. 
       
    60  * Advanced options are not supported by the CSY. The characteristics of the 
       
    61  * basic option are:
       
    62  * <li> Length indicator used instead of the HDLC transparency mechanism
       
    63  * <li> Different flag octet from that used by HDLC
       
    64  * <li> Cannot be used on links that use XON/XOFF flow control
       
    65  * <li> May have longer recovery procedure from loss of synchronization
       
    66  * <p>
       
    67  * Each channel between the TE and MS is called a Data Link Connection (DLC).
       
    68  * A special DLC called "control channel" is used to exchange management 
       
    69  * information (e.g. parameter negotiation), power saving control information,
       
    70  * testing, flow control, close down, etc. between the TE and MS.
       
    71  * <p>
       
    72  * The multiplexer is activated with the 3GPP 27.007 AT+CMUX command.
       
    73  * <p>
       
    74  * 
       
    75  * \section install Installation
       
    76  *
       
    77  * Refer to the readme.txt file for installation and build instructions.
       
    78  * <br>
       
    79  * Please contact TapRoot Systems for information on product updates and
       
    80  * additional support.
       
    81  */
       
    82 
       
    83 /** @file PortFactory.cpp
       
    84  * This file contains the implementation for the class CPortFactory.
       
    85  * This class is used to create C32 port interface instances.
       
    86  */
       
    87 
       
    88 #include <cs_port.h>
       
    89 #include <f32file.h>
       
    90 #include <commsdattypesv1_1.h>
       
    91 #include <commsdat_partner.h>
       
    92 using namespace CommsDat;
       
    93 #include "Portfactory.h"
       
    94 #include "CsyGlobals.h"
       
    95 #include "Mux0710Protocol.h"
       
    96 #include "ChannelMgrCtrl.h"
       
    97 #include "ChannelMgrCmdData.h"
       
    98 #include "PortC32InterfaceBase.h"
       
    99 #include "PortC32Interface.h"
       
   100 #include "PortC32InterfaceIp.h"
       
   101 #include "CommFrameWriterAo.h"
       
   102 #include "CommFrameReaderAo.h"
       
   103 #include "CsyDebugLogger.h"
       
   104 
       
   105 CPortFactory* CPortFactory::NewL()
       
   106 /**
       
   107  * This method uses two phase construction and the cleanup stack to create
       
   108  * an instance of class CPortFactory. This method is invoked when the user
       
   109  * invokes the C32 comm server's LoadCommModule() method to load the CSY.
       
   110  * Note that multiple instances of CPortFactory can be created by C32,
       
   111  * so no memory allocation or objects should be created when the 
       
   112  * port factory is first created.
       
   113  *
       
   114  * @return Pointer to the created instance of CPortFactory
       
   115  */
       
   116 	{
       
   117 	_LOG_L4C1("CPortFactory::NewL");
       
   118 
       
   119 	CPortFactory* self = new(ELeave) CPortFactory;
       
   120 	TCleanupItem closeSelf(CPortFactory::CloseObject, self);
       
   121 	CleanupStack::PushL(closeSelf);
       
   122 	self->ConstructL();
       
   123 	CleanupStack::Pop(self);
       
   124 	return (self);
       
   125 	}
       
   126 
       
   127 CPortFactory::CPortFactory()
       
   128 : iPortC32InterfaceList(_FOFF(CPortC32Interface, iLink)),
       
   129   iPortC32InterfaceIter(iPortC32InterfaceList),
       
   130   iDataChannelList(_FOFF(CChannelMgrCmdData, iLink)),
       
   131   iDataChannelIter(iDataChannelList)
       
   132 /**
       
   133  * Constructor.
       
   134  */
       
   135 	{
       
   136 	_LOG_L4C1("CPortFactory::CPortFactory");
       
   137 	}
       
   138 
       
   139 CPortFactory::~CPortFactory()
       
   140 /**
       
   141  * Destructor. Delete all objects and memory created/allocated by this class.
       
   142  */
       
   143 	{
       
   144 	_LOG_L4C1(">>CPortFactory::~CPortFactory");
       
   145 
       
   146 	// check if the CSY's objects have been created 
       
   147 	// C32 creates a new instance of the port factory each time a client
       
   148 	// loads the CSY. The C32 recognizes a redundant CSerial name and promptly
       
   149 	// destroys the redundant CSY.
       
   150 	if (iMuxObjectsCreated)
       
   151 		{
       
   152 		// client channels
       
   153 		CChannelMgrCmdData* anyChannel = NULL;
       
   154 		iDataChannelIter.SetToFirst();
       
   155 		while ((anyChannel = iDataChannelIter++) != NULL)
       
   156 			{
       
   157 			iDataChannelList.Remove(*anyChannel);
       
   158 			delete anyChannel;
       
   159 			}
       
   160 
       
   161 		// control channel
       
   162 		delete iChannelCtrl;
       
   163 
       
   164 		delete iCommWriterAo;
       
   165 		delete iCommReaderAo;
       
   166 
       
   167 		delete iMux0710Protocol;
       
   168 
       
   169 		// may not be necessary ???
       
   170 		// MAF /\ what does this comment mean
       
   171 
       
   172 		// delete any remaining C32 port objects that were not closed by the client
       
   173 		CPortC32InterfaceBase* port = NULL;
       
   174 		iPortC32InterfaceIter.SetToFirst();
       
   175 		while ((port = iPortC32InterfaceIter++) != NULL)
       
   176 			{
       
   177 			delete port;
       
   178 			}
       
   179 
       
   180 		iCommPort.Close();
       
   181 		}
       
   182 
       
   183 	//iMuxObjectsCreated = EFalse;
       
   184 
       
   185 	_LOG_L4C1("<<CPortFactory::~CPortFactory");
       
   186 	_LOG_L3C1(" ");
       
   187 	}
       
   188 
       
   189 void CPortFactory::ConstructL()
       
   190 /**
       
   191  * 2nd-phase construction. Registers the CSY Name with C32.
       
   192  */
       
   193 	{
       
   194 	_LOG_L4C1("CPortFactory::ConstructL");
       
   195 
       
   196 #ifdef _DEBUG
       
   197 	TInt usedCellCount;
       
   198 	TInt freeCellCount;
       
   199 	usedCellCount = User::CountAllocCells(freeCellCount);
       
   200 	_LOG_L4C3("Cell Counts Used = %d   Free = %d", usedCellCount, freeCellCount);
       
   201 #endif
       
   202 
       
   203 	// inform C32 of the CSY name
       
   204 	TName name(KCSY_Gsm0710ServerName);
       
   205 	TInt retVal1 = SetName(&name);
       
   206 	if (retVal1 != KErrNone)
       
   207 		{
       
   208 		_LOG_L1C3("** Failed to set CSY:%S in C32. Err: %d **", &name, retVal1);
       
   209 		User::Leave(retVal1);
       
   210 		}
       
   211 	iVersion = TVersion(KCSY_Gsm0710MinorVersionNumber,
       
   212 						KCSY_Gsm0710MajorVersionNumber,
       
   213 						KCSY_Gsm0710BuildVersionNumber);
       
   214 	}
       
   215 
       
   216 void CPortFactory::CreateCsyObjectsL()
       
   217 /**
       
   218  * This method is called to create the CSY objects for the multiplexing channels,
       
   219  * message queues, i/o interface, etc.  This method should be called when the
       
   220  * 1st client request to create a port is received by the CSY via NewPortL().
       
   221  */
       
   222 	{
       
   223 	_LOG_L4C1(">>CPortFactory::CreateCsyObjectsL");
       
   224 
       
   225 	// set flag
       
   226 	iMuxObjectsCreated = ETrue;
       
   227 	iOpenPortFailed = EFalse;
       
   228 
       
   229 	// load physical device drivers for serial port(s)
       
   230 
       
   231 #ifdef __WINS__
       
   232 	// 1st make sure the file server is running
       
   233 	RFs fileServer;
       
   234 	User::LeaveIfError(fileServer.Connect());
       
   235 	fileServer.Close();
       
   236 
       
   237 	_LIT(KPDDName, "ECDRV");
       
   238 	TInt retVal1 = User::LoadPhysicalDevice(KPDDName);
       
   239 	if ((retVal1 != KErrNone) && (retVal1 != KErrAlreadyExists))
       
   240 		User::Leave(retVal1);
       
   241 
       
   242 #else  // target
       
   243 
       
   244 	TBuf<20> commPddName;
       
   245 	TInt retVal1;
       
   246 
       
   247 	commPddName.Copy(KCsyDefaultPDDNameComm1);
       
   248 	retVal1 = User::LoadPhysicalDevice(commPddName);
       
   249 	if ((retVal1 != KErrNone) && (retVal1 != KErrAlreadyExists))
       
   250 		User::Leave(retVal1);
       
   251 	// only load 2nd PDD if debugging is enabled 
       
   252 
       
   253 #ifdef __DEBUGSERIALPORT__
       
   254 	commPddName.Copy(KCsyDefaultPDDNameComm2);
       
   255 	retVal1 = User::LoadPhysicalDevice(commPddName);
       
   256 	if ((retVal1 != KErrNone) && (retVal1 != KErrAlreadyExists))
       
   257 		User::Leave(retVal1);
       
   258 #endif //__DEBUGSERIALPORT__
       
   259 
       
   260 #endif  //__WINS__
       
   261 
       
   262 	// load logical device driver for serial ports
       
   263 	_LIT(KLDDName, "ECOMM");
       
   264 	retVal1 = User::LoadLogicalDevice(KLDDName);
       
   265 	if ((retVal1 != KErrNone) && (retVal1 != KErrAlreadyExists))
       
   266 		User::Leave(retVal1);
       
   267 
       
   268 	ConfigurePhysicalCommPortL();
       
   269 	
       
   270 	// create 27.010 framer and comm i/o objects
       
   271 	iMux0710Protocol = CMux0710Protocol::NewL(*this);
       
   272 	iCommWriterAo    = CCommFrameWriterAo::NewL(this, iMux0710Protocol);
       
   273 	iMux0710Protocol->SetCommWriter(iCommWriterAo);
       
   274 
       
   275 	iCommReaderAo    = CCommFrameReaderAo::NewL(this, iMux0710Protocol);
       
   276 	iCommReaderAo->Read();  // start it
       
   277 	
       
   278 	// create the Multiplexer Control Channel
       
   279 	iChannelCtrl = CChannelMgrCtrl::NewL(*this, *iMux0710Protocol);
       
   280 
       
   281 	// create All Multiplexer Channels	
       
   282 	for (TUint8 i=1; i < KCsyDefaultMaxDLCChannels; i++)
       
   283 		{
       
   284 		CChannelMgrCmdData* channelMgrCmd =
       
   285 			CChannelMgrCmdData::NewL(i, *this, *iMux0710Protocol);
       
   286 		iDataChannelList.AddLast(*channelMgrCmd);
       
   287 		}
       
   288 
       
   289 	// switch to multiplexer mode
       
   290 	iMux0710Protocol->SwitchToMuxMode();
       
   291 
       
   292 #ifdef _DEBUG
       
   293 	TInt usedCellCount;
       
   294 	TInt freeCellCount;
       
   295 	usedCellCount = User::CountAllocCells(freeCellCount);
       
   296 	_LOG_L4C3("Cell Counts Used = %d   Free = %d", usedCellCount, freeCellCount);
       
   297 #endif
       
   298 
       
   299 	_LOG_L4C1("<<CPortFactory::CreateCsyObjectsL");
       
   300 	}
       
   301 
       
   302 void CPortFactory::CloseObject(TAny* aObject)
       
   303 /**
       
   304  * Utility to close objects created by the port factory. 
       
   305  * Used during construction with the cleanup stack.
       
   306  *
       
   307  * @param aObject - The CObject to close.
       
   308  * @return void
       
   309  */
       
   310 	{
       
   311 	reinterpret_cast<CObject*>(aObject)->Close();
       
   312 	}
       
   313 
       
   314 CPort* CPortFactory::NewPortL(const TUint aUnit)
       
   315 /**
       
   316  * This method is called by C32 when a port is opened via RComm::Open().
       
   317  * An instance of the CSY's class CPortC32Interface is created.
       
   318  * The instance is assigned a channel manager and then is added to
       
   319  * the list of C32 port interface objects.
       
   320  *
       
   321  * @param aUnit - Numeric portion of the port identifier
       
   322  * @return Pointer to the created C32 port interface object
       
   323  */
       
   324 	{
       
   325 	_LOG_L4C2(">>CPortFactory::NewPortL [aUnit=%d]", aUnit);
       
   326 
       
   327 	TC32PortInfo portInfo;
       
   328 
       
   329 	portInfo.iClientType = GetClientType(aUnit);
       
   330 	portInfo.iPortNumber = aUnit;
       
   331 
       
   332 	// check if port number is valid and translates to a client type
       
   333 	if (portInfo.iClientType == EC32ClientUndefined)
       
   334 		{
       
   335 		_LOG_L4C1("<<CPortFactory::NewPortL - ** Unknown client **");
       
   336 		return NULL;
       
   337 		}
       
   338 
       
   339 	// check if this is the 1st port to be opened
       
   340 	if (!iMuxObjectsCreated)
       
   341 		{
       
   342 		_LOG_L4C1("first port to be opened");
       
   343 		CreateCsyObjectsL();
       
   344 		}
       
   345 	else if (iChannelCtrl->GetChannelReady() == EFalse)
       
   346 		{
       
   347 		_LOG_L4C1("CSY objects already exist");
       
   348 		// switch to multiplexer mode
       
   349 		iMux0710Protocol->SwitchToMuxMode();
       
   350 		}
       
   351 
       
   352 	// first make sure control channel is already connected
       
   353 	iChannelCtrl->WaitForChannelReady();
       
   354 	
       
   355 	if (! iOpenPortFailed)
       
   356 		{
       
   357 		// create a new C32 Interface Port based on port number
       
   358 		CPortC32InterfaceBase* port = NULL;
       
   359 	
       
   360 		if (portInfo.iClientType == EC32ClientIpNif)
       
   361 			{
       
   362 			_LOG_L4C1("Raw IP port");
       
   363  			port = CPortC32InterfaceIp::NewL(*this, portInfo);
       
   364  			}
       
   365 		else
       
   366 			{
       
   367 			_LOG_L4C1("Normal port");
       
   368  			port = CPortC32Interface::NewL(*this, portInfo);
       
   369  			}
       
   370 	
       
   371 		CleanupStack::PushL(port);
       
   372 		CChannelMgrCmdData* channel = FindChannelMgr(aUnit);
       
   373 
       
   374 		if (channel == NULL)
       
   375 			{
       
   376 			_LOG_L2C1("** channel is null, delete interface **");
       
   377 			CleanupStack::PopAndDestroy(port);
       
   378 			User::Leave(KErrNotFound);
       
   379 			}
       
   380 		TName name;
       
   381 		name.Format(_L("%d"), aUnit);
       
   382 		port->SetName(&name);
       
   383 
       
   384 		// channel->Open() will add this port to it's port list, and connect channel if needed
       
   385 		// Make sure that during channel init we do not tx/rx through channel
       
   386 		channel->Open(port);
       
   387 	
       
   388 		iNumOfOpenPorts++;
       
   389 		_LOG_L4C2("No. of ports = %d",iNumOfOpenPorts);
       
   390  		
       
   391 		port->SetMuxChannel(channel);
       
   392 
       
   393 		channel->WaitForChannelReady();
       
   394 
       
   395 		//Need to wait for the channel to be ready. How to wait for something without blocking whole thread?
       
   396 		// add it to list of ports
       
   397 		iPortC32InterfaceList.AddLast(*port);	
       
   398 		CleanupStack::Pop(port);
       
   399 
       
   400 #ifdef _DEBUG
       
   401 		TInt usedCellCount;
       
   402 		TInt freeCellCount;
       
   403 		usedCellCount = User::CountAllocCells(freeCellCount);
       
   404 		_LOG_L4C3("Cell Counts Used = %d   Free = %d", usedCellCount, freeCellCount);
       
   405 #endif
       
   406 
       
   407 		_LOG_L4C2("<<CPortFactory::NewPortL [port=%d]", port);
       
   408 		return port;
       
   409 		}
       
   410 	else //iPortOpenFailed == ETrue
       
   411 		{
       
   412 		_LOG_L4C1("<<CPortFactory::NewPortL [port=NULL]");
       
   413 		User::Leave(KErrTimedOut);
       
   414 		return NULL;
       
   415 		}
       
   416 	}
       
   417  
       
   418 void CPortFactory::Info(TSerialInfo& aSerialInfo)
       
   419 /**
       
   420  * Called by C32 to identify the CSY module.
       
   421  * @param aSerialInfo - Reference holder for information to be populated by the function.
       
   422  * @return void
       
   423  */
       
   424 	{
       
   425 	_LOG_L4C1("CPortFactory::Info");
       
   426 
       
   427 	aSerialInfo.iDescription	= KCSY_Description;
       
   428 	aSerialInfo.iName			= KCSY_Gsm0710ServerName;
       
   429 	aSerialInfo.iLowUnit		= KCSY_Gsm0710LowUnit;
       
   430 	aSerialInfo.iHighUnit		= KCSY_Gsm0710HighUnit;
       
   431 	}
       
   432 
       
   433 CPortFactory::TC32ClientType CPortFactory::GetClientType(TInt aC32PortNum)
       
   434 /**
       
   435  * Returns the ClientType for the specified C32 port number.
       
   436  * @param aC32PortNum -  C32 port number
       
   437  * @return Client type - TSY or NIF
       
   438  */
       
   439 	{ 
       
   440 	_LOG_L4C2("CPortFactory::GetClientType [aC32PortNum=%d]", aC32PortNum);
       
   441 
       
   442 	if (aC32PortNum >= KCOMMP_IP_NIF_OFFSET)
       
   443 		{
       
   444 		_LOG_L4C1("Raw Ip");
       
   445 		return CPortFactory::EC32ClientIpNif;
       
   446 		}
       
   447 	else if (aC32PortNum == KCOMMP_CSD_PPP_NUMBER)
       
   448 		{ 
       
   449 		_LOG_L4C1("Csd PPP");
       
   450 		return CPortFactory::EC32ClientNif;
       
   451 		}
       
   452 
       
   453 	_LOG_L4C1("Tsy");
       
   454 	return CPortFactory::EC32ClientTsy;
       
   455 	}
       
   456 
       
   457 CPortC32InterfaceBase* CPortFactory::FindPortC32Interface(const TUint8 aDlcNum)
       
   458 /**
       
   459  * This method returns a pointer to the C32 port interface object 
       
   460  * that is associated with the specified DLC number.
       
   461  *
       
   462  * @param aDlcNum - DLC number
       
   463  * @return Pointer to C32 port interface object or NULL if not found
       
   464  */
       
   465 	{
       
   466 	_LOG_L4C2(">>CPortFactory::FindPortC32Interface [aDlcNum=%d]", aDlcNum);
       
   467 
       
   468 	CPortC32InterfaceBase* port = NULL;
       
   469 
       
   470 	CChannelMgrCmdData* anyChannel = NULL;
       
   471 	iDataChannelIter.SetToFirst();
       
   472 	while ((anyChannel = iDataChannelIter++) != NULL)
       
   473 		{
       
   474 		if (anyChannel->GetDlcNumber() == aDlcNum)
       
   475 			{
       
   476 			_LOG_L4C1("AT channel");
       
   477 			port = anyChannel->GetC32Port();
       
   478 			break;
       
   479 			}
       
   480 		}
       
   481 	
       
   482 	_LOG_L4C2("<<CPortFactory::FindPortC32Interface [port=0x%x]",port);
       
   483 	return port;
       
   484 	}
       
   485 
       
   486 CChannelMgrCmdData* CPortFactory::FindChannelMgr(const TUint aC32PortNum)
       
   487 /**
       
   488  * This method is only called when a port is created.
       
   489  *
       
   490  * @param aC32PortNum - C32 port number
       
   491  * @return Pointer to the channel manager object or NULL
       
   492  */
       
   493 	{
       
   494 	_LOG_L4C2(">>CPortFactory::FindChannelMgr [aC32PortNum=%d]", aC32PortNum);
       
   495 
       
   496 	TUint dlcNum = ++iLastDlcNum;
       
   497 	_LOG_L4C2("iLastDlcNum: %d", iLastDlcNum);
       
   498 
       
   499 	//
       
   500 	// MAF Horrible
       
   501 	// This just counts through the array of channel pointers
       
   502 	// and returns a pointer when the count gets to the correct value.
       
   503 	// 
       
   504 	// We should be able to use an array here and directly access it using
       
   505 	// the dlc number!?!?
       
   506 	//
       
   507 
       
   508 	TUint count  = 1;
       
   509 	CChannelMgrCmdData* anyChannelCmd;
       
   510 
       
   511 	iDataChannelIter.SetToFirst();
       
   512 	while ((anyChannelCmd = iDataChannelIter++) != NULL)
       
   513 		{
       
   514 		if (count == dlcNum)
       
   515 			{
       
   516 			_LOG_L4C2("<<CPortFactory::FindChannelMgr() - found, count %d",count);
       
   517 			return anyChannelCmd;
       
   518 			}
       
   519 		count++;
       
   520 		}
       
   521 
       
   522 	_LOG_L4C1("<<CPortFactory::FindChannelMgr() - *** NOT FOUND ***");
       
   523 	return NULL;
       
   524 	}
       
   525 
       
   526 CChannelMgrCmdData* CPortFactory::FindChannelMgrByDlcNum(const TUint8 aDlcNum)
       
   527 /**
       
   528  * This method returns a pointer to the channel manager object 
       
   529  * that has the specified  DLC number.
       
   530  *
       
   531  * @param aDlcNum -  DLC number
       
   532  * @return Pointer to the channel manager object or NULL
       
   533  */
       
   534 	{
       
   535 	_LOG_L4C2(">>CPortFactory::FindChannelMgrByDlcNum [aDlcNum=%d]", aDlcNum);
       
   536 
       
   537     CChannelMgrCmdData* channel = NULL;
       
   538 	iDataChannelIter.SetToFirst();
       
   539 	while ((channel = iDataChannelIter++) != NULL)
       
   540 		{
       
   541 		if (channel->GetDlcNumber() == aDlcNum)
       
   542 			{
       
   543 			_LOG_L4C1("AT channel");
       
   544 			return channel;
       
   545 			}
       
   546 		}
       
   547 
       
   548 	_LOG_L2C1("<<CPortFactory::FindChannelMgrByDlcNum() - NOT FOUND");
       
   549 	return NULL;
       
   550 	}
       
   551 
       
   552 TInt CPortFactory::ConfigurePhysicalCommPortL()
       
   553 /**
       
   554  * This method is used to configure the specified physical comm port.
       
   555  * The parameters are read from CommDb or supplied by C32.
       
   556  * @param aPhysPortNum - Physical port number
       
   557  * @return KErrorNone or KErrAccessDenied.
       
   558  */
       
   559 	{
       
   560 	_LOG_L4C1(">>CPortFactory::ConfigurePhysicalCommPortL");
       
   561 
       
   562 	// create a package pointer
       
   563 	TPckgBuf<TCommConfigV01> newCfg;
       
   564 	TInt ret, len;
       
   565 
       
   566 	// get the modem table	- Get port settings from CommDB.
       
   567 	CMDBSession* db = CMDBSession::NewL(KCDLatestVersion);
       
   568 
       
   569 	CleanupStack::PushL(db);
       
   570 
       
   571 	TUint32 modemId = 0;
       
   572 
       
   573 	// We are using the same modem as TSY
       
   574 	// Accessing the ModemPhoneServicesSMS field in the Global Settings Table
       
   575 	CMDBField<TUint32>* globalSettingsField = new(ELeave) CMDBField<TUint32>(KCDTIdModemPhoneServicesSMS);
       
   576 	CleanupStack::PushL(globalSettingsField);
       
   577 	globalSettingsField->SetRecordId(1);
       
   578 	globalSettingsField->LoadL(*db);
       
   579 	modemId = *globalSettingsField;	// The field value gives the record id in the Modem Bearer Table.
       
   580 	CleanupStack::PopAndDestroy(globalSettingsField);
       
   581 	
       
   582 	_LOG_L4C2("Accessing modem entry %d in the modem table", modemId);
       
   583 
       
   584 	TUint32 rate;
       
   585 	TUint32 dataBits;
       
   586 	TUint32 stopBits;
       
   587 	TUint32 parity;
       
   588 	TUint32 handshake;
       
   589 	TPckgBuf<TCommConfigV01> config;
       
   590 	
       
   591 	TUint port = 0;
       
   592 	TBuf<KCommsDbSvrMaxFieldLength> controlPort;
       
   593 	
       
   594 	// Accessing the ControlChannelPort Name from the ModemBearer Table in the record number 
       
   595 	//corresponding to modemId
       
   596 	CMDBField<TDesC>* portField = new(ELeave) CMDBField<TDesC>(KCDTIdControlChannelPortName);
       
   597 	CleanupStack::PushL(portField);
       
   598 	portField->SetRecordId(modemId);
       
   599 	portField->SetMaxLengthL(KMaxTextLength);
       
   600 	portField->LoadL(*db);
       
   601 	controlPort = *portField;
       
   602 	CleanupStack::PopAndDestroy(portField);
       
   603 	// Diagnostics - control port
       
   604 	if (controlPort.Length())
       
   605 		{
       
   606 		TBuf8<16> portName;
       
   607 		portName.Copy(controlPort.Left(16));
       
   608 		_LOG_L4C2("Control port(MODEM_CONTROL_CHANNEL_PORT_NAME) '%S'",
       
   609 			&portName);
       
   610 
       
   611 		// Get number of port out of the port descriptor.
       
   612 		_LIT(KDoubleColon, "::");
       
   613 		TInt len = controlPort.Find(KDoubleColon);
       
   614 		if (len < KErrNone)
       
   615 			{
       
   616 			_LOG_L1C1("** finding :: in control port failed **");
       
   617 			User::Leave(KErrGeneral);
       
   618 			}
       
   619 		TUint numPos = len + KDoubleColon.iTypeLength;
       
   620 		TPtrC numPtr(&controlPort[numPos], controlPort.Length() - numPos);
       
   621 		TLex lexer(numPtr);
       
   622 		TInt ret = lexer.Val(port);
       
   623 		if (ret)
       
   624 			{
       
   625 			_LOG_L1C1("** lexing to get port number failed **");
       
   626 			User::Leave(ret);
       
   627 			}
       
   628 		}
       
   629 	else
       
   630 		{
       
   631 		_LOG_L1C1("** Control port not set in commDb **");
       
   632 		User::Leave(KErrGeneral);
       
   633 		}
       
   634 	
       
   635 	// Accessing the Rate from the ModemBearer Table in the record number 
       
   636 	//corresponding to modemId
       
   637 	CMDBField<TUint32>* rateField = new(ELeave) CMDBField<TUint32>(KCDTIdRate);
       
   638 	CleanupStack::PushL(rateField);
       
   639 	rateField->SetRecordId(modemId);
       
   640 	rateField->LoadL(*db);
       
   641 	rate = *rateField;
       
   642 	CleanupStack::PopAndDestroy(rateField);
       
   643 	
       
   644 	// Accessing the DataBits from the ModemBearer Table in the record number 
       
   645 	//corresponding to modemId
       
   646 	CMDBField<TUint32>* dataBitsField = new(ELeave) CMDBField<TUint32>(KCDTIdDataBits);
       
   647 	CleanupStack::PushL(dataBitsField);
       
   648 	dataBitsField->SetRecordId(modemId);
       
   649 	dataBitsField->LoadL(*db);
       
   650 	dataBits = *dataBitsField;
       
   651 	CleanupStack::PopAndDestroy(dataBitsField);
       
   652 
       
   653 	// Accessing the StopBits from the ModemBearer Table in the record number 
       
   654 	//corresponding to modemId
       
   655 	CMDBField<TUint32>* stopBitsField = new(ELeave) CMDBField<TUint32>(KCDTIdStopBits);
       
   656 	CleanupStack::PushL(stopBitsField);
       
   657 	stopBitsField->SetRecordId(modemId);
       
   658 	stopBitsField->LoadL(*db);
       
   659 	stopBits = *stopBitsField;
       
   660 	CleanupStack::PopAndDestroy(stopBitsField);
       
   661 	
       
   662 	// Accessing the Parity from the ModemBearer Table in the record number 
       
   663 	//corresponding to modemId
       
   664 	CMDBField<TUint32>* parityField = new(ELeave) CMDBField<TUint32>(KCDTIdParity);
       
   665 	CleanupStack::PushL(parityField);
       
   666 	parityField->SetRecordId(modemId);
       
   667 	parityField->LoadL(*db);
       
   668 	parity = *parityField;
       
   669 	CleanupStack::PopAndDestroy(parityField);
       
   670 	
       
   671 	// Accessing the HandShake from the ModemBearer Table in the record number 
       
   672 	//corresponding to modemId
       
   673 	CMDBField<TUint32>* handshakeField = new(ELeave) CMDBField<TUint32>(KCDTIdHandshaking);
       
   674 	CleanupStack::PushL(handshakeField);
       
   675 	handshakeField->SetRecordId(modemId);
       
   676 	handshakeField->LoadL(*db);
       
   677 	handshake = *handshakeField;
       
   678 	CleanupStack::PopAndDestroy(handshakeField);
       
   679 
       
   680 	config().iRate		  = (TBps)rate;
       
   681 	config().iDataBits    = (TDataBits)dataBits;
       
   682 	config().iStopBits    = (TStopBits)stopBits;
       
   683 	config().iParity      = (TParity)parity;
       
   684 	config().iHandshake   = (TUint)handshake;
       
   685 	config().iParityError = KConfigParityErrorIgnore;
       
   686 	config().iFifo        = static_cast<TUint>(EFifoEnable);
       
   687 
       
   688 	newCfg.Copy(config);
       
   689 
       
   690 	ret = iCommPort.Open(port);
       
   691 	if (ret)
       
   692 		{
       
   693 		_LOG_L1C3("** Failed CommPort.Open(%d) ret=%d **", port,ret);
       
   694 		User::Leave(ret);
       
   695 		}
       
   696 
       
   697 	// Configure the port
       
   698 	len = iCommPort.ReceiveBufferLength();
       
   699 	
       
   700 	if (len < KErrNone)
       
   701 		{
       
   702 		_LOG_L1C2("** Leave: CommPort.ReceiveBufferLength() len=%d **", len);
       
   703 		User::Leave(KErrGeneral);
       
   704 		}
       
   705 
       
   706 	// Increase the receive buffer to make sure we can operate as slowly as we 
       
   707 	// can without filling up the LDD's receive buffer with incoming signals 
       
   708 	len *= KCSY_ReceiveBufferSizeIncreaseFactor; 
       
   709 	ret = iCommPort.SetReceiveBufferLength(len);
       
   710 
       
   711 	TCommConfig cbuf;
       
   712 	TCommConfigV01 &cfg = cbuf();
       
   713 	iCommPort.Config(cbuf);					// Get the Configuration Parameters
       
   714 
       
   715 	cfg.iRate        = newCfg().iRate;
       
   716 	cfg.iDataBits    = newCfg().iDataBits;
       
   717 	cfg.iStopBits    = newCfg().iStopBits;
       
   718 	cfg.iParity      = newCfg().iParity;
       
   719 	cfg.iHandshake   = newCfg().iHandshake;
       
   720 	cfg.iParityError = newCfg().iParityError;
       
   721 	cfg.iFifo        = newCfg().iFifo;
       
   722 
       
   723 	_LOG_L4C2("  Set Phys Port Config for Port %d", port);
       
   724 	_LOG_L4C2("    Rate = 0x%x", cfg.iRate);
       
   725 	_LOG_L4C2("    DataBits = 0x%x", cfg.iDataBits);
       
   726 	_LOG_L4C2("    Stop Bits = 0x%x", cfg.iStopBits);
       
   727 	_LOG_L4C2("    Parity = 0x%x", cfg.iParity);
       
   728 	_LOG_L4C2("    Handshake = 0x%x", cfg.iHandshake);
       
   729 	_LOG_L4C2("    Parity Error = 0x%x", cfg.iParityError);
       
   730 	_LOG_L4C2("    Fifo = 0x%x", cfg.iFifo);
       
   731 
       
   732 	ret = iCommPort.SetConfig(cbuf);		// Set the Configuration Parameters
       
   733 	if (ret)
       
   734 		{
       
   735 		_LOG_L1C2("** CommPort.SetConfig ret=%d **", ret);
       
   736 		User::Leave(KErrNotSupported);
       
   737 		}
       
   738 
       
   739 	// Clean up the stack
       
   740 	CleanupStack::PopAndDestroy(db);
       
   741 
       
   742 	_LOG_L4C2("<<CPortFactory::ConfigurePhysicalCommPortL [ret=%d]", ret);
       
   743 	return ret;
       
   744 	}
       
   745 
       
   746 void CPortFactory::ConnectControlChannel()
       
   747 /**
       
   748  * This method is called to connect the 27.010 control channel,
       
   749  * which is DLC 0.
       
   750  */
       
   751 	{
       
   752 	_LOG_L4C1("CPortFactory::ConnectControlChannel");
       
   753 
       
   754 	TInt ret = 	iChannelCtrl->Connect();	
       
   755 	if (ret != KErrNone)
       
   756 		{
       
   757 		_LOG_L4C2(("<<CCPortFactory::ConnectControlChannel ret=%d"),ret);
       
   758 		}
       
   759 	}
       
   760 
       
   761 void CPortFactory::DisconnectControlChannel()
       
   762 /**
       
   763  * This method is called to set the 27.010 control channel to the "disconnect" state.
       
   764  */
       
   765 	{
       
   766 	_LOG_L4C1("CPortFactory::DisconnectAllChannels");
       
   767 	iChannelCtrl->Disconnect();
       
   768 	}
       
   769 
       
   770 void CPortFactory::ChannelCtrlDoCancel()
       
   771 /**
       
   772  * This method is called when the port has been opened and the max number of retries has been reached.
       
   773  */
       
   774 	{
       
   775 	_LOG_L4C1(">>CPortFactory::ChannelCtrlDoCancel");
       
   776 	
       
   777 	iOpenPortFailed = ETrue;
       
   778 	iChannelCtrl->NotifyChannelReady();
       
   779 
       
   780 	_LOG_L4C1("<<CPortFactory::ChannelCtrlDoCancel");
       
   781 	}
       
   782 	
       
   783 
       
   784 void CPortFactory::RemoveC32Port(CPortC32InterfaceBase* aPort)
       
   785 /**
       
   786  * This method removes the specified C32 port object from the
       
   787  * port factory's list of current/open C32 port objects.
       
   788  * This will allow another client to open that port.
       
   789  * @param aPort - Pointer to C32 port object
       
   790  * @return void
       
   791  */
       
   792 	{
       
   793 	_LOG_L4C2(">>CPortFactory::RemoveC32Port [aPort=%d]", aPort->GetPortNumber());
       
   794 
       
   795 	CPortC32InterfaceBase* port = NULL;
       
   796 	iPortC32InterfaceIter.SetToFirst();
       
   797 	while ((port = iPortC32InterfaceIter++) != NULL)
       
   798 		{
       
   799 		if (port == aPort)
       
   800 			{
       
   801 			iPortC32InterfaceList.Remove(*port);
       
   802 			_LOG_L4C1("<<CPortFactory::RemoveC32Port - okay");
       
   803 			return;
       
   804 			}
       
   805 		}
       
   806 
       
   807 	// MAF this is a bad condition
       
   808 	_LOG_L4C1("<<CPortFactory::RemoveC32Port ** Not Removed? **");
       
   809 	}
       
   810 
       
   811 void CPortFactory::ConnectIpNifPort(const TUint8 aDlcNum)
       
   812 /**
       
   813  * This method connects the port interface object
       
   814  * in the CSY that are used for a data interface to a NIF.
       
   815  * @param void
       
   816  */
       
   817 	{
       
   818 	_LOG_L4C2("CPortFactory::ConnectIpNifPort [aDlcNum=%d]",aDlcNum);
       
   819 
       
   820 	CPortC32InterfaceBase* port = FindPortC32Interface(aDlcNum);
       
   821 
       
   822 	if (port)
       
   823 		{
       
   824 		CChannelMgrCmdData* iMuxChannel = port->GetMuxChannel();
       
   825 		if (iMuxChannel)
       
   826 			{
       
   827 			
       
   828 			TInt err = iMuxChannel->Connect();
       
   829 			if (err != KErrNone)
       
   830 				{
       
   831 				_LOG_L1C2("iMuxChannel->Connect() error =%d",err);
       
   832 				}
       
   833 			}
       
   834 		else
       
   835 			{
       
   836 			// MAF assert here
       
   837 			_LOG_L1C2("** Could not find channel for port [0x%x] **",port);
       
   838 			}
       
   839 		}
       
   840 	else
       
   841 		{
       
   842 		// MAF assert here
       
   843 		_LOG_L1C1("** Could not find port for dlcnumber **");
       
   844 		}
       
   845 	}
       
   846 
       
   847 TBool CPortFactory::FindDlcToEnable()
       
   848 /**
       
   849  * We have resources again. Find a dlc that has had its flow control
       
   850  * set and re-enable.
       
   851  *
       
   852  * @return ETrue if there are still dlcs that are stopped due to flow control.
       
   853  */
       
   854 	{
       
   855 	_LOG_L4C1(">>CPortFactory::FindDlcToEnable");
       
   856 
       
   857 	TInt numberOfStopped = 0;
       
   858 	TBool dlcsStillToEnable = EFalse;
       
   859 
       
   860     CChannelMgrCmdData* channel = NULL;
       
   861 
       
   862 	iDataChannelIter.SetToFirst();
       
   863 	while ((channel = iDataChannelIter++) != NULL)
       
   864 		{
       
   865 		if (channel->CsyToModemFlowControl() == EFlowControlOn)
       
   866 			{
       
   867 			numberOfStopped++;
       
   868 			if (numberOfStopped == 1)
       
   869 				{
       
   870 				_LOG_L4C1("Found a dlc to re-enable");
       
   871 				if (channel->SetCsyToModemFlowControl(EFlowControlOff))
       
   872 					{
       
   873 					_LOG_L4C1("Re-enable failed");
       
   874 					dlcsStillToEnable = ETrue;
       
   875 					break;
       
   876 					}
       
   877 				}
       
   878 			else
       
   879 				{
       
   880 				_LOG_L4C1("Other dlc still to re-enable");
       
   881 				dlcsStillToEnable = ETrue;
       
   882 				}
       
   883 			}
       
   884 		}
       
   885 
       
   886 	_LOG_L4C2("<<CPortFactory::FindDlcToEnable numberOfStopped=%d",numberOfStopped);
       
   887 	return dlcsStillToEnable;
       
   888 	}
       
   889 
       
   890 void CPortFactory::FindActiveDataDlcToStop()
       
   891 /**
       
   892  * MAF come up with a better algorithm. Currently this scans sequentially for a data
       
   893  * DLC to stop.
       
   894  * Check the data dlcs to see if one of them can be stopped
       
   895  */
       
   896 	{
       
   897 	_LOG_L4C1(">>CPortFactory::FindActiveDataDlcToStop");
       
   898 
       
   899     CChannelMgrCmdData* channel = NULL;
       
   900 	CPortC32InterfaceBase* port = NULL;
       
   901 
       
   902 	iDataChannelIter.SetToFirst();
       
   903 	while ((channel = iDataChannelIter++) != NULL)
       
   904 		{
       
   905 		port = channel->GetC32Port();
       
   906 		if ((port)&&(port->GetClientType() == EC32ClientIpNif))
       
   907 			{
       
   908 			if (channel->CsyToModemFlowControl() == EFlowControlOff)
       
   909 				{
       
   910 				_LOG_L4C1("Found a data dlc to stop");
       
   911 				if (channel->SetCsyToModemFlowControl(EFlowControlOn))
       
   912 					{
       
   913 					_LOG_L4C1("Failed to stop dlc");
       
   914 					}
       
   915 				else
       
   916 					break;
       
   917 				}
       
   918 			}
       
   919 		}
       
   920 
       
   921 	_LOG_L4C1("<<CPortFactory::FindActiveDataDlcToStop");
       
   922 	}
       
   923 
       
   924 void CPortFactory::StopAnyDlc()
       
   925 /**
       
   926  * Time for drastic action - enforce flow control on any DLC (except ctrl).
       
   927  */
       
   928 	{
       
   929 	_LOG_L4C1(">>CPortFactory::StopAnyDlc");
       
   930 
       
   931     CChannelMgrCmdData* channel = NULL;
       
   932 
       
   933 	iDataChannelIter.SetToFirst();
       
   934 	while ((channel = iDataChannelIter++) != NULL)
       
   935 		{
       
   936 		if (channel->CsyToModemFlowControl() == EFlowControlOff)
       
   937 			{
       
   938 			_LOG_L4C1("Found a dlc to stop");
       
   939 			if (channel->SetCsyToModemFlowControl(EFlowControlOn))
       
   940 				{
       
   941 				_LOG_L4C1("Failed to stop dlc");
       
   942 				}
       
   943 			else
       
   944 				break;
       
   945 			}
       
   946 		}
       
   947 
       
   948 	_LOG_L4C1("<<CPortFactory::StopAnyDlc");
       
   949 	}
       
   950 
       
   951 TBool CPortFactory::DecrementNumOfOpenPorts()
       
   952 /**
       
   953  * Used to decrement the number of open ports and disconnect if number = 0
       
   954  *
       
   955  * aReturn returns EFalse if Num of Ports is 0; ETrue otherwise
       
   956  */
       
   957 {
       
   958 	if (iNumOfOpenPorts==0)
       
   959 		return EFalse;
       
   960 	else if (--iNumOfOpenPorts == 0)
       
   961 		{
       
   962 		_LOG_L4C1("******* DISCONNECT ALL *********");
       
   963 		DisconnectControlChannel();
       
   964 		iLastDlcNum = 0;
       
   965 		return EFalse;
       
   966 		}	
       
   967 	return ETrue;
       
   968 }
       
   969 
       
   970 
       
   971 TSecurityPolicy CPortFactory::PortPlatSecCapability(TUint /*aPort*/) const
       
   972 /**
       
   973  * This method returns the capabilities required to open the given port number
       
   974  * This 27.010 CSY requires the same capabilities for all port numbers
       
   975  *
       
   976  * @param aPort - The port number to check against
       
   977  * @return TSecurityPolicy - containing the required capabilities to use this CSY
       
   978  */
       
   979 {
       
   980 	return TSecurityPolicy(ECapabilityNetworkServices, ECapabilityNetworkControl, ECapabilityReadUserData, ECapabilityWriteUserData);
       
   981 }
       
   982