serialserver/c32serialserver/SCOMM/CS_MAN.CPP
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1997-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 
       
    17 /** @file
       
    18  *
       
    19  * Implements CPortManager and various small functions
       
    20  */
       
    21 
       
    22 #include "CS_STD.H"
       
    23 #include <f32file.h>
       
    24 #include "C32LOG.H"
       
    25 
       
    26 LOCAL_C void CloseCObject(TAny* aCObject)
       
    27 /**
       
    28  * Utility func for cleanup stack - clean up a CObject
       
    29  *
       
    30  * @param aObj pointer to object to be closed
       
    31  */
       
    32 	{
       
    33 	((CObject*)aCObject)->Close();
       
    34 	}
       
    35 
       
    36 
       
    37 //
       
    38 // implementation of CPortManager
       
    39 //
       
    40 
       
    41 CPort* CPortManager::GetPortL(const TDesC& aName, TUint aPort, CSerial* aSerial,
       
    42 							  TUint aMode, TUint aRole, CCommSession* aSession)
       
    43 /**
       
    44  * Return a port matching a given name for a given mode.
       
    45  * If the port already exists, increments the count on this CObject.
       
    46  *
       
    47  * @param aName    name of the port 
       
    48  * @param aPort    port number
       
    49  * @param aSerial  pointer to the CSerial object
       
    50  * @param aMode    mode; Exclusive, Shared or Preemptable
       
    51  * @param aRole    role; DTE or DCE
       
    52  * @param aSession handle to the clients session
       
    53  * @return CPort*  pointer to the CPort object
       
    54  *
       
    55  * @leave Leave This function may leave
       
    56  */
       
    57 	{
       
    58 	CPort* p = NULL;
       
    59 	TInt handle = 0;
       
    60 	TFullName dummy;
       
    61 	TInt res = iPorts->FindByFullName(handle, aName, dummy);
       
    62 	switch (res)
       
    63 		{
       
    64 	 case KErrNotFound:
       
    65 			{
       
    66 			(void)aSerial->Open();	// CObject->Open() always returns KErrNone so its safe to ignore return value
       
    67 			TCleanupItem serialClose(CloseCObject, aSerial);
       
    68 			CleanupStack::PushL(serialClose);
       
    69 			// And create a port from it.
       
    70 			p = aSerial->NewPortL(aPort);
       
    71 			TCleanupItem portClose(CloseCObject, p);
       
    72 			CleanupStack::PushL(portClose);
       
    73 			p->iExtra = new(ELeave) CPort::CExtra();
       
    74 			p->DoOpenL(aSession,(TInternalCommAccess)aMode,(TCommRole)aRole,ETrue);
       
    75 			p->iPortManager = this;
       
    76 			iPorts->AddL(p);
       
    77 			p->SetOwner(aSerial);
       
    78 			C32LOG2(KC32Player, _L8("CPortManager::GetPortL() New Port - AccessCount: %d"), p->AccessCount());
       
    79 			CleanupStack::Pop(2); // portClose, serialClose
       
    80 			break;
       
    81 			}
       
    82 	case KErrNone:
       
    83 		// find the port, attempt to open it as shared or take possession, and increment
       
    84 		// object usage count.
       
    85 		p = (CPort*)iPorts->At(handle);
       
    86 		p->DoOpenL(aSession, (TInternalCommAccess)aMode,(TCommRole)aRole,EFalse);
       
    87 		(void)p->Open();	// CObject->Open() always returns KErrNone so its safe to ignore return value
       
    88 		C32LOG2(KC32Player, _L8("CPortManager::GetPortL() Existing Port - AccessCount: %d"), p->AccessCount());
       
    89 		break;
       
    90 	default:
       
    91 		User::Leave(res);
       
    92 		}
       
    93 	return p;
       
    94 	}
       
    95 
       
    96 
       
    97 CPortManager* CPortManager::NewL()
       
    98 /**
       
    99  * Locate and initialise all loadable port modules
       
   100  *
       
   101  * @return an newly created CPortManager object
       
   102  * @leave Leave This function may leave
       
   103  */
       
   104 	{
       
   105 	CPortManager* p = new (ELeave) CPortManager();
       
   106 	CleanupStack::PushL(p);
       
   107 	p->ConstructL();
       
   108 	CleanupStack::Pop();
       
   109 	return p;
       
   110 	}
       
   111 
       
   112 CPortManager::~CPortManager()
       
   113 	{
       
   114 	iContainer->Remove(iProviders);		
       
   115 	iContainer->Remove(iPorts);
       
   116 	delete iContainer;
       
   117 	}
       
   118 
       
   119 void CPortManager::ConstructL()
       
   120 /**
       
   121  * construct and init the children
       
   122  */
       
   123 	{
       
   124     iContainer = CObjectConIx::NewL();
       
   125 	iPorts = iContainer->CreateL();
       
   126 	iProviders = iContainer->CreateL();
       
   127 	}
       
   128 
       
   129 
       
   130 LOCAL_C void CloseLibrary(TAny* aLib)
       
   131 /**
       
   132  * Close a library from the cleanup stack
       
   133  *
       
   134  * @param aLib pointer to the library to close
       
   135  */
       
   136 	{
       
   137 	((RLibrary*)aLib)->Close();
       
   138 	}
       
   139 
       
   140 
       
   141 
       
   142 CSerial* CPortManager::GetSerialL(const TDesC& aName)
       
   143 /**
       
   144  * Find a previously loaded CSY by name and return its factory
       
   145  *
       
   146  * @param aName name of the port
       
   147  * @return CSerial* pointer to the serial object
       
   148  * @leave Leave This function may leave
       
   149  */
       
   150 	{
       
   151 	TInt handle = 0;
       
   152 	TName matching;
       
   153 	(void)User::LeaveIfError(iProviders->FindByName(handle, aName, matching));
       
   154 	return (CSerial*)iProviders->At(handle);
       
   155 	}
       
   156 
       
   157 
       
   158 CSerial* CPortManager::LoadCommModuleL(const TDesC& aFileName)
       
   159 /**
       
   160  * Load a comms module on user request or increments the ref count for the module.
       
   161  * This function loads the CSY
       
   162  * specified in aFileName, and if found calls the first ordinal on it.
       
   163  * The CSY instantiates itself and returns the pointer to the CSerial
       
   164  * object implemented in the CSY. If everything goes OK then this 
       
   165  * pointer will be returned to the callee which then takes ownedship of it.
       
   166  *
       
   167  * @param aFileName name of the new CSY module to load (i.e. ECUART.CSY)
       
   168  * @return CSerial* pointer to the new CSerial object
       
   169  * @leave Leave This function may leave
       
   170  */	
       
   171 	{
       
   172 	C32LOG2(KC32Player,_L("CPortManager::LoadCommModuleL(), Comms Module Name : %S"), &aFileName);
       
   173 	RLibrary lib;
       
   174 	TInt r=lib.Load(aFileName);
       
   175 	if (r!=KErrNone)
       
   176 		User::Leave(r);
       
   177 
       
   178 	TCleanupItem libClose(CloseLibrary,&lib);
       
   179 	CleanupStack::PushL(libClose);
       
   180 
       
   181 	// Check the Uid2 [unicode]
       
   182 	if(lib.Type()[1]!=TUid::Uid(KUidUnicodeCommServerModuleV02))
       
   183 		User::Leave(KErrBadLibraryEntryPoint);
       
   184 
       
   185 	TSerialNewL libEntry=(TSerialNewL)lib.Lookup(1);
       
   186 	if (libEntry==NULL)
       
   187 		User::Leave(KErrBadLibraryEntryPoint);
       
   188 
       
   189 	CSerial* s = NULL;
       
   190 	s = (*libEntry)();	// libEntry may leave.  
       
   191     User::LeaveIfNull(s); // libEntry may also return null
       
   192 
       
   193 	CleanupStack::PushL(TCleanupItem(CloseCObject,s));
       
   194 	s->ConstructL(lib); // Once lib is transferred it's handle is nulified because
       
   195 	                    // deleting s will cause the reference to be closed
       
   196 
       
   197 	TRAPD(ret, iProviders->AddL(s));
       
   198 
       
   199 	switch (ret)
       
   200 		{
       
   201 	case KErrAlreadyExists:
       
   202 		{
       
   203 		// The CSY is already loaded since we already have a handle to
       
   204 		// its CSerial factory, so just increase the refcount on the factory.
       
   205 		TInt fHand = 0;
       
   206 		TName dummy;
       
   207 		iProviders->FindByName(fHand, s->Name(), dummy); // Can't be an error
       
   208 		CSerial* s2 = (CSerial*)iProviders->At(fHand);
       
   209 		s->Close(); // Should delete s and the library reference will be closed
       
   210 		s2->Open();
       
   211 		s = s2;
       
   212 		}
       
   213 		break;
       
   214 	case KErrNone:
       
   215 		// success!
       
   216 		break;
       
   217 	default:
       
   218 		User::Leave(ret);
       
   219 		break;
       
   220 		}
       
   221 
       
   222 	CleanupStack::Pop(2); // s and library reference are now in safe hands
       
   223 
       
   224 	return s;
       
   225 	}
       
   226 
       
   227 
       
   228 TInt CPortManager::PortInfo(const TPortName& aName, TSerialInfo& aSerial)
       
   229 /**
       
   230  * get info on a specified port.
       
   231  *
       
   232  * @param aName name of the port
       
   233  * @param aSerial serial info will be written here
       
   234  * @return TInt error code
       
   235  */
       
   236 	{
       
   237 	C32LOG2(KC32Player,_L("CPortManager::PortInfo() by Port Name : %S"), &aName);
       
   238 	TInt handle = 0;
       
   239 	TName matching;
       
   240 	TInt res = iProviders->FindByName(handle,aName,matching);
       
   241 	if(res == KErrNotFound) // INC052021
       
   242 		{
       
   243 		TFileName fname;
       
   244 		TInt index = iProviders->Count();
       
   245 		while(index>0)
       
   246 			{
       
   247 			index--;
       
   248 			CSerial* actual = (CSerial*)iProviders->operator[](index);
       
   249 			actual->ModuleName(fname);
       
   250 			if(fname == aName)
       
   251 				{
       
   252 				actual->Info(aSerial);
       
   253 				return KErrNone;
       
   254 				}
       
   255 			}
       
   256 		}
       
   257 	if (res != KErrNone)
       
   258 		return res;
       
   259 	CSerial* s = (CSerial*)(iProviders->At(handle));
       
   260 	s->Info(aSerial);
       
   261 	return KErrNone;
       
   262 	}
       
   263 
       
   264 
       
   265 // EOF - CS_MAN.CPP