telephonyserverplugins/multimodetsy/test/Te_LoopBack/Te_LoopBackSCOMM.CPP
changeset 0 3553901f7fa8
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     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 // This file contains the methods necessary for interacting with the Communications Server
       
    15 // and ports.  The MComm mixin class's methods (open, close, read, and write) are implemented 
       
    16 // here.  The RunL functions for read and write are implemented here as well as the Cancel
       
    17 // functions.
       
    18 // 
       
    19 //
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include "Te_LoopBackATSTD.H"
       
    26 #include "Te_LoopBackSCOMM.H"
       
    27 
       
    28 MComm::MComm()
       
    29 /**
       
    30  * This is a single phase constructor that simply initializes private member data.  An instance
       
    31  * of the MComm class is created.
       
    32  *
       
    33  * @return	None
       
    34  */
       
    35 	{
       
    36 	iCommReader = NULL;
       
    37 	iCommWriter = NULL;
       
    38 	}
       
    39 
       
    40 MComm::~MComm()
       
    41 /**
       
    42  * The MComm class destructor which deletes the iCommReader and iCommWriter of this mixin
       
    43  * class.  Additionally, port and server cleanup (close) is handled here.
       
    44  *
       
    45  * @return	None
       
    46  */
       
    47 	{
       
    48     delete iCommReader;
       
    49 	delete iCommWriter;
       
    50 	iCommPort.Close();
       
    51 	iCommServer.Close();
       
    52 	}
       
    53 
       
    54 TInt MComm::CommOpen(const TDesC& aDll, const TDesC& aName, TCommAccess aMode)
       
    55 /**
       
    56  * This method implements a connection to the comm server, the loading of the specified
       
    57  * DLL (typically a CSY) and the open of a specified port.  This method is called from the 
       
    58  * ConstructL of the derived class.
       
    59  *
       
    60  * First, the method connects to the communications server.  Then, the specified DLL is 
       
    61  * loaded.  If successful, then the actual port is opened.  First the port is opened in
       
    62  * an exclusive mode.  If that fails (regardless of the mode requested by the calling 
       
    63  * application), an error is returned.  If this open is successful, and the requested mode
       
    64  * is shared, the exclusive open is released and the port is re-opened in shared mode.
       
    65  *
       
    66  * @param	aDll, the Dynamic Library name to be loaded via LoadCommModule.  This is typically
       
    67  *			a CSY.
       
    68  * @param	aName, the port name to be opened.
       
    69  * @param	aMode, the access mode for this port.  See note below.
       
    70  *
       
    71  * @return	KErrNone or errors associated with connecting to the Comm Server, errors loading
       
    72  *			the DLL, or errors opening the port.
       
    73  *
       
    74  * @note	If the specified DLL has been previously loaded, this method will return an
       
    75  *          error.
       
    76  * @note	Even if the caller specifies a shared access mode, it first must be able to open
       
    77  *			the port exclusively for this method to succeed.  Thus a second open on a shared
       
    78  *			port will fail.
       
    79  * @note	This is an overloaded function (in this case requiring a DLL name).
       
    80  */
       
    81  // Todo: add reasoning, when call loaned, the port must be reopened by others, but gsm tsy
       
    82  //       does not want this, it wants control.
       
    83 	{
       
    84 	TInt err;
       
    85 	if (err = iCommServer.Connect(), err!=KErrNone)
       
    86 		return err;
       
    87 	
       
    88 	if (aDll.Length()>0)
       
    89 		{
       
    90 		if (err = iCommServer.LoadCommModule(aDll), err!=KErrNone)
       
    91 			{
       
    92 			iCommServer.Close();
       
    93 			return err;
       
    94 			}
       
    95 		}
       
    96 	if (err = iCommPort.Open(iCommServer, aName, ECommExclusive, ECommRoleDCE), err!=KErrNone)
       
    97 		// if any other TSY (or other app) is using comm port, we want to return gracefully
       
    98 		// with error
       
    99 		{
       
   100 		iCommServer.Close();
       
   101 		return err;
       
   102 		}
       
   103 	if (aMode==ECommShared)
       
   104 		{
       
   105 		iCommPort.Close();
       
   106 		if (err = iCommPort.Open(iCommServer, aName, aMode, ECommRoleDCE), err!=KErrNone)
       
   107 			{
       
   108 			iCommServer.Close();
       
   109 			return err;
       
   110 			}
       
   111 		}
       
   112 	return KErrNone;
       
   113 	}
       
   114 
       
   115 
       
   116 TInt MComm::CommOpen(const TDesC& aName, TCommAccess aMode)
       
   117 /**
       
   118  * This method implements a connection to the comm server and the open of the specified port.
       
   119  * This method assumes that the needed DLL has already been loaded into the system.
       
   120  * This method is called from the ConstructL of the derived class.
       
   121  *
       
   122  * First, the method connects to the communications server.  If successful, then the actual 
       
   123  * port is opened.  First the port is opened in an exclusive mode.  If that fails (regardless
       
   124  * of the mode requested by the calling application), an error is returned.  If this open 
       
   125  * is successful, and the requested mode is shared, the exclusive open is released and the 
       
   126  * port is re-opened in shared mode.
       
   127  *
       
   128  * @param	aName, the port name to be opened.
       
   129  * @param	aMode, the access mode for this port.  See note below.
       
   130  *
       
   131  * @return	KErrNone or errors associated with connecting to the Comm Server, errors loading
       
   132  *			the DLL, or errors opening the port.
       
   133  *
       
   134  * @note	Even if the caller specifies a shared access mode, it first must be able to open
       
   135  *			the port exclusively for this method to succeed.  Thus a second open on a shared
       
   136  *			port will fail.
       
   137  * @note	This is an overloaded function (in this case not requiring a DLL name).
       
   138  */	{
       
   139 	TInt err;
       
   140 	if (err = iCommServer.Connect(), err!=KErrNone)
       
   141 		return err;
       
   142 	if (err = iCommPort.Open(iCommServer, aName, ECommExclusive, ECommRoleDCE), err!=KErrNone)
       
   143 		// if any other TSY (or other app) is using comm port, we want to return gracefully
       
   144 		// with error
       
   145 		{
       
   146 		iCommServer.Close();
       
   147 		return err;
       
   148 		}
       
   149 	if (aMode==ECommShared)
       
   150 		{
       
   151 		iCommPort.Close();
       
   152 		if (err = iCommPort.Open(iCommServer, aName, aMode, ECommRoleDCE), err!=KErrNone)
       
   153 			{
       
   154 			iCommServer.Close();
       
   155 			return err;
       
   156 			}
       
   157 		}
       
   158 	return KErrNone;
       
   159 	}
       
   160 
       
   161 void MComm::CommClose()
       
   162 /**
       
   163  * The method handles cleanup of the MComm objects.  It cancels an outstanding reads or writes
       
   164  * and closes the port and comm server sessions.
       
   165  *
       
   166  * @return	None
       
   167  */
       
   168 	{
       
   169 	iCommReader->Cancel();
       
   170 	iCommWriter->Cancel();
       
   171 	iCommPort.Close();
       
   172 	iCommServer.Close();
       
   173 	}
       
   174 
       
   175 void MComm::CommConstructL(TInt aReadPriority, TInt aWritePriority)
       
   176 /**
       
   177  * This method is a constructor for the CCommReader and CCommWriter classes.  This method is
       
   178  * typically called from the ConstructL of the derived class.  It instansiates a new CCommReader
       
   179  * and CCommWriter.
       
   180  *
       
   181  * @param	aReadPriority is the priority to be associated to the CCommReader active object.
       
   182  * @param	aWritePriority is the priority to be associated to the CCommWriter active object.
       
   183  * @leave	This function can leave if memory is not available to create the active objects.
       
   184  */
       
   185 	{
       
   186 	iCommReader = new (ELeave) CCommReader(this, aReadPriority);
       
   187 	iCommWriter = new (ELeave) CCommWriter(this, aWritePriority);
       
   188 	};
       
   189 
       
   190 void MComm::CommWrite(const TDesC8& aDes)
       
   191 /**
       
   192  * This method implements the write to the Comm Server.  It uses the TRequestStatus member of
       
   193  * the iCommWriter class and activates the object, indicating a request had been made.
       
   194  *
       
   195  * @param	aDes, a descriptor reference containing the data to be written to the port.
       
   196  * @return	None
       
   197  */
       
   198 	{
       
   199 	__ASSERT_ALWAYS(iCommWriter!=NULL, HayesPanic(EATCommand_NotConstructed));
       
   200 	iCommPort.Write(iCommWriter->StatusRef(), aDes);
       
   201 	iCommWriter->Activate();
       
   202 	}
       
   203 
       
   204 void MComm::CommWriteReady()
       
   205 /**
       
   206  * This method is used during initialization for synchronization purposes.  It issues a write
       
   207  * of a NULL descriptor to the communications server port.  It then waits for the request to be
       
   208  * completed.  This request will be completed when the associated serial port is ready for write
       
   209  * data.
       
   210  *
       
   211  * @return	None
       
   212  */
       
   213 	{
       
   214 	__ASSERT_ALWAYS(iCommWriter!=NULL, HayesPanic(EATCommand_NotConstructed));
       
   215 	TRequestStatus status;
       
   216 	iCommPort.Write(status, TPtrC8(NULL, 0));
       
   217 	User::WaitForRequest(status);
       
   218 	}
       
   219 
       
   220 void MComm::CommRead(TDes8& aDes)
       
   221 /**
       
   222  * This method implements the read from the Comm Server.  The number of bytes read is specified
       
   223  * by the length member of the descriptor.  It uses the TRequestStatus member of
       
   224  * the iCommReader class and activates the object, indicating a request had been made.
       
   225  *
       
   226  * @param	aDes, a descriptor reference containing the buffer into which data will be placed.
       
   227  *
       
   228  * @return	None
       
   229  */
       
   230 	{
       
   231 	__ASSERT_ALWAYS(iCommReader!=NULL, HayesPanic(EATCommand_NotConstructed));
       
   232 	iCommPort.Read(iCommReader->StatusRef(), aDes, aDes.Length());
       
   233 	iCommReader->Activate();
       
   234 	}
       
   235 
       
   236 void MComm::CommReadOneOrMore(TDes8& aDes)
       
   237 /**
       
   238  * This method pends a read for one or more characters to the comm server port.  The comm
       
   239  * server will return as much data as is ready up to the maximum length 
       
   240  * specified in the descriptor, aDes.  As soon as the available data is exhausted (after the
       
   241  * first byte) this read will be completed.  This differs from a Read which is posted with a
       
   242  * specific length in that the normal Read would pend until the number of characters specified
       
   243  * has been read (or the read timed out).  The object is activated, indicating that a request
       
   244  * has been made.
       
   245  *
       
   246  * @param  aDes  When the read completes, the contents of aDes will have been modified.
       
   247  */
       
   248 	{
       
   249 	__ASSERT_ALWAYS(iCommReader!=NULL, HayesPanic(EATCommand_NotConstructed));
       
   250 	iCommPort.ReadOneOrMore(iCommReader->StatusRef(), aDes);
       
   251 	iCommReader->Activate();
       
   252 	}
       
   253 
       
   254 void MComm::CommReadReady()
       
   255 /**
       
   256  * This method is used during initialization for synchronization purposes.  It issues a read
       
   257  * of with a zero length to the communications server port.  It then waits for the request to be
       
   258  * completed.  A side effect of issuing the read to the serial port is that the serial port will
       
   259  * be powered up.
       
   260  */	{
       
   261 	__ASSERT_ALWAYS(iCommReader!=NULL, HayesPanic(EATCommand_NotConstructed));
       
   262 	TRequestStatus status;
       
   263 	iCommPort.Read(status,iBuf,0);
       
   264 	User::WaitForRequest(status);
       
   265 	}
       
   266 
       
   267 void MComm::CommCancel()
       
   268 /**
       
   269  * The mixin class cancel routine which cancels any outstanding requests for the iCommWriter and
       
   270  * iCommReader.  The routine first validates the iCommWriter and iCommReader member pointers.
       
   271  *
       
   272  * @return	None.
       
   273  */
       
   274 	{
       
   275 	if (iCommWriter)
       
   276 		iCommWriter->Cancel();
       
   277 	if (iCommReader)
       
   278 		iCommReader->Cancel();
       
   279 	}
       
   280 
       
   281 void MComm::CommWriteCancel()
       
   282 /**
       
   283  * The mixin class cancel routine which only cancels outstanding write requests.  The 
       
   284  * routine first validates the iCommWriter pointer.
       
   285  *
       
   286  * @return	None.
       
   287  */
       
   288 	{
       
   289 	if (iCommWriter)
       
   290 		iCommWriter->Cancel();
       
   291 	}
       
   292 
       
   293 void MComm::CommReadCancel()
       
   294 /**
       
   295  * The mixin class cancel routine which only cancels outstanding read requests.  The 
       
   296  * routine first validates the iCommReader pointer.
       
   297  *
       
   298  * @return	None.
       
   299  */
       
   300 	{
       
   301 	if (iCommReader)
       
   302 		iCommReader->Cancel();
       
   303 	}
       
   304 
       
   305 //
       
   306 // CCommWriter
       
   307 //
       
   308 CCommWriter::CCommWriter(MComm* aComm, TInt aPriority)
       
   309 	: CActive(aPriority), iComm(aComm)
       
   310 /**
       
   311  * This constructor adds the CCommWriter active object to the active scheduler of the thread in
       
   312  * which it is called.  The constructor initializes the iComm member data and calls the CActive
       
   313  * method of this CBase derived class to set the priority.  A new instance of the CCommWriter is
       
   314  * created by this constructor.
       
   315  *
       
   316  * @param	aComm A pointer to the MComm class object to be stored in member data iComm.
       
   317  * @param	aPriority The priority at which this active object should be handled by the
       
   318  *			active scheduler.
       
   319  * @return	None
       
   320  */
       
   321 	{
       
   322 	__DECLARE_NAME(_S("CCommWriter"));
       
   323 	CActiveScheduler::Add(this);
       
   324 	}
       
   325 
       
   326 CCommWriter::~CCommWriter()
       
   327 /**
       
   328  * This destructor simply requests cancellation any outstanding requests for the iCommWriter object.
       
   329  *
       
   330  * @return	None.
       
   331  */
       
   332 	{
       
   333 	Cancel();
       
   334 	}
       
   335 
       
   336 void CCommWriter::RunL()
       
   337 /**
       
   338  * This is the RunL function invoked by the active scheduler for the CCommWriter active object.
       
   339  * It in turn calls the write complete method accessible via the mixin class stored in the
       
   340  * iComm member, passing the status.  The mixin class write complete method is a pure virtual 
       
   341  * method which must be implemented by the derived class.
       
   342  *
       
   343  * @return	None
       
   344  */
       
   345 	{
       
   346 	iComm->CommWriteComplete(iStatus.Int());
       
   347 	}
       
   348 
       
   349 void CCommWriter::DoCancel()
       
   350 /**
       
   351  * This method implments the cancel function necessary for a CActive derived object.  It simply
       
   352  * invokes the port specific method to cancel outstanding writes.
       
   353  *
       
   354  * @return	None
       
   355  */
       
   356 	{
       
   357 	iComm->iCommPort.WriteCancel();
       
   358 	}
       
   359 
       
   360 //
       
   361 // CCommReader
       
   362 //
       
   363 CCommReader::CCommReader(MComm* aComm, TInt aPriority)
       
   364 	: CActive(aPriority), iComm(aComm)
       
   365 /**
       
   366  * This constructor adds the CCommReader active object to the active scheduler of the thread in
       
   367  * which it is called.  The constructor initializes the iComm member data and calls the CActive
       
   368  * method of this CBase derived class to set the priority.  A new instance of the CCommReader is
       
   369  * created by this constructor.
       
   370  *
       
   371  * @param	aComm A pointer to the MComm class object to be stored in member data iComm.
       
   372  * @param	aPriority The priority at which this active object should be handled by the
       
   373  *			active scheduler.
       
   374  * @return	None
       
   375  */
       
   376 	{
       
   377 	__DECLARE_NAME(_S("CCommReader"));
       
   378 	CActiveScheduler::Add(this);
       
   379 	}
       
   380 
       
   381 CCommReader::~CCommReader()
       
   382 /**
       
   383  * This destructor simply requests cancellation any outstanding requests for the iCommReader object.
       
   384  *
       
   385  * @return	None.
       
   386  */
       
   387 	{
       
   388 	Cancel();
       
   389 	}
       
   390 
       
   391 void CCommReader::RunL()
       
   392 /**
       
   393  * This is the RunL function invoked by the active scheduler for the CCommReader active object.
       
   394  * It in turn calls the read complete method accessible via the mixin class stored in the
       
   395  * iComm member, passing the status.  The mixin class read complete method is a pure virtual 
       
   396  * method which must be implemented by the derived class.
       
   397  *
       
   398  * @return	None
       
   399  */
       
   400 	{
       
   401 	iComm->CommReadComplete(iStatus.Int());
       
   402 	}
       
   403 
       
   404 void CCommReader::DoCancel()
       
   405 /**
       
   406  * This method implments the cancel function necessary for a CActive derived object.  It simply
       
   407  * invokes the port specific method to cancel outstanding writes.
       
   408  *
       
   409  * @return	None
       
   410  */
       
   411 	{
       
   412 	iComm->iCommPort.ReadCancel();
       
   413 	}