telephonyserverplugins/simtsy/src/CSimSerComm.cpp
changeset 0 3553901f7fa8
child 19 630d2f34d719
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2001-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 // Implementation of the Comms reader and writer mixin class.
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 #include "CSimSerComm.h"
       
    23 #include "CSimPhone.h"
       
    24 #include "Simlog.h"
       
    25 
       
    26 MComm::MComm()
       
    27 /**
       
    28  * Trivial constructor.
       
    29  */
       
    30 	{
       
    31 	iCommReader = NULL;
       
    32 	iCommWriter = NULL;
       
    33 	}
       
    34 
       
    35 MComm::~MComm()
       
    36 /**
       
    37  * Destructor.  Destroy the heap-based objects and close the serial ports.
       
    38  */
       
    39 	{
       
    40     delete iCommReader;
       
    41 	delete iCommWriter;
       
    42 	iCommPort.Close();
       
    43 	iCommServer.Close();
       
    44 	}
       
    45 
       
    46 void MComm::CommConstructL(TInt aReadPriority, TInt aWritePriority)
       
    47 /**
       
    48  * Second phase constructor for this class.  This function creates the reader and writer
       
    49  * objects.
       
    50  * @param aReadPriority		The active scheduler priority associated with the reader class.
       
    51  * @param aWritePriority	The active scheduler priority associated with the writer class.
       
    52  */
       
    53 	{
       
    54 	iCommReader = new (ELeave) CCommReader(this, aReadPriority);
       
    55 	iCommWriter = new (ELeave) CCommWriter(this, aWritePriority);
       
    56 	};
       
    57 
       
    58 TInt MComm::CommOpen(const TDesC& aDll, const TDesC& aName, TCommAccess aMode)
       
    59 /**
       
    60  * Connect to the C32 Server, load the CSY module and open the port ready for
       
    61  * reading and writing.  If a "shared" open is requested, the function will
       
    62  * briefly attempt to exclusively open the comm port to ensure that no-one
       
    63  * is currently on the port.  This client will allow others to share the port
       
    64  * but it wants to remain in control.
       
    65  *
       
    66  * @param aDll		The CSY module name.
       
    67  * @param aName		The serial port name.
       
    68  * @param aMode		The required access mode: shared, exclusive, etc...
       
    69  * @return TInt		Standard error value.
       
    70  */
       
    71 	{
       
    72 	TInt err;
       
    73 	if (err = iCommServer.Connect(), err!=KErrNone)
       
    74 		return err;
       
    75 	if (aDll.Length()>0)
       
    76 		{
       
    77 		if (err = iCommServer.LoadCommModule(aDll), err!=KErrNone)
       
    78 			{
       
    79 			iCommServer.Close();
       
    80 			return err;
       
    81 			}
       
    82 		}
       
    83 	if (err = iCommPort.Open(iCommServer, aName, ECommExclusive), err!=KErrNone)
       
    84 		// if any other TSY (or other app) is using comm port, we want to return gracefully
       
    85 		// with error
       
    86 		{
       
    87 		iCommServer.Close();
       
    88 		return err;
       
    89 		}
       
    90 	if (aMode==ECommShared)
       
    91 		{
       
    92 		iCommPort.Close();
       
    93 		if (err = iCommPort.Open(iCommServer, aName, aMode), err!=KErrNone)
       
    94 			{
       
    95 			iCommServer.Close();
       
    96 			return err;
       
    97 			}
       
    98 		}
       
    99 	return KErrNone;
       
   100 	}
       
   101 
       
   102 TInt MComm::CommOpen(const TDesC& aName, TCommAccess aMode)
       
   103 /**
       
   104  * Connect to the C32 Server and open the port ready for
       
   105  * reading and writing.  If a "shared" open is requested, the function will
       
   106  * briefly attempt to exclusively open the comm port to ensure that no-one
       
   107  * is currently on the port.  This client will allow others to share the port
       
   108  * but it wants to remain in control.
       
   109  *
       
   110  * @param aName		The serial port name.
       
   111  * @param aMode		The required access mode: shared, exclusive, etc...
       
   112  * @return TInt		Standard error value.
       
   113  */
       
   114 	{
       
   115 	LOGDATA1("Attempting to Open Serial Port");
       
   116 	TInt err;
       
   117 	if (err = iCommServer.Connect(), err!=KErrNone)
       
   118 		return err;
       
   119 	if (err = iCommPort.Open(iCommServer, aName, ECommExclusive), err!=KErrNone)
       
   120 		// if any other TSY (or other app) is using comm port, we want to return gracefully
       
   121 		// with error
       
   122 		{
       
   123 		iCommServer.Close();
       
   124 		return err;
       
   125 		}
       
   126 	if (aMode==ECommShared)
       
   127 		{
       
   128 		iCommPort.Close();
       
   129 		if (err = iCommPort.Open(iCommServer, aName, aMode), err!=KErrNone)
       
   130 			{
       
   131 			iCommServer.Close();
       
   132 			return err;
       
   133 			}
       
   134 		}
       
   135 	LOGDATA1("Sucessfully Opened Serial Port");
       
   136 	return KErrNone;
       
   137 	}
       
   138 
       
   139 void MComm::CommClose()
       
   140 /**
       
   141  * Cancel any outstanding requests and close all port handles.
       
   142  */
       
   143 	{
       
   144  	if (iCommReader != NULL)
       
   145  		iCommReader->Cancel();
       
   146  	
       
   147  	if (iCommWriter != NULL)
       
   148  		iCommWriter->Cancel();
       
   149 	iCommPort.Close();
       
   150 	iCommServer.Close();
       
   151 	}
       
   152 
       
   153 void MComm::CommWrite(const TDesC8& aDes)
       
   154 /**
       
   155  * Asynchronously start to write the contents of a descriptor to the opened serial port.
       
   156  * When the write completes, the callback function CommWriteComplete will be called.
       
   157  * @param aDes		Descriptor whose contents will be written to the port.
       
   158  */
       
   159 	{
       
   160 	__ASSERT_ALWAYS(iCommWriter!=NULL, SimPanic(ECommWriterNotConstructed));
       
   161 	iCommPort.Write(iCommWriter->StatusRef(), aDes);
       
   162 	iCommWriter->Activate();
       
   163 	}
       
   164 
       
   165 void MComm::CommWriteReady()
       
   166 /**
       
   167  * This function sychronously primes the serial port for writing.  The function will not
       
   168  * complete until the serial port is powered up and the handshaking lines are
       
   169  * set to allow write operations.
       
   170  */
       
   171 	{
       
   172 	__ASSERT_ALWAYS(iCommWriter!=NULL, SimPanic(ECommWriterNotConstructed));
       
   173 	TRequestStatus aStatus;
       
   174 	iCommPort.Write(aStatus, TPtrC8(NULL, 0));
       
   175 	User::WaitForRequest(aStatus);
       
   176 	}
       
   177 
       
   178 void MComm::CommRead(TDes8& aDes)
       
   179 /**
       
   180  * Asynchronously to read data from the opened serial port.
       
   181  * When the passed buffer is full, the read will complete, calling the CommReadComplete callback function.
       
   182  * @param aDes		Read buffer descriptor.
       
   183  */
       
   184 	{
       
   185 	__ASSERT_ALWAYS(iCommReader!=NULL, SimPanic(ECommReaderNotConstructed));
       
   186 	iCommPort.Read(iCommReader->StatusRef(), aDes, aDes.Length());
       
   187 	iCommReader->Activate();
       
   188 	}
       
   189 
       
   190 void MComm::CommReadOneOrMore(TDes8& aDes)
       
   191 /**
       
   192  * Asynchronously to read data from the opened serial port.
       
   193  * As soon as a single or multiple octets have been read, the read will complete,
       
   194  * calling the CommReadComplete callback function.
       
   195  * @param aDes		Read buffer descriptor.
       
   196  */
       
   197 	{
       
   198 	__ASSERT_ALWAYS(iCommReader!=NULL, SimPanic(ECommReaderNotConstructed));
       
   199 	iCommPort.ReadOneOrMore(iCommReader->StatusRef(), aDes);
       
   200 	iCommReader->Activate();
       
   201 	}
       
   202 
       
   203 void MComm::CommReadReady()
       
   204 /**
       
   205  * This function sychronously primes the serial port for reading.  The function will not
       
   206  * complete until the serial port is powered up.
       
   207  */
       
   208 	{
       
   209 	__ASSERT_ALWAYS(iCommReader!=NULL, SimPanic(ECommReaderNotConstructed));
       
   210 	TRequestStatus aStatus;
       
   211 	iCommPort.Read(aStatus,iBuf,0);
       
   212 	User::WaitForRequest(aStatus);
       
   213 	}
       
   214 
       
   215 void MComm::CommCancel()
       
   216 /**
       
   217  * Cancel any outstanding read or write requests.
       
   218  */
       
   219 	{
       
   220 	if (iCommWriter)
       
   221 		iCommWriter->Cancel();
       
   222 	if (iCommReader)
       
   223 		iCommReader->Cancel();
       
   224 	}
       
   225 
       
   226 void MComm::CommWriteCancel()
       
   227 /**
       
   228  * Cancel an outstanding write request.
       
   229  */
       
   230 	{
       
   231 	if (iCommWriter)
       
   232 		iCommWriter->Cancel();
       
   233 	}
       
   234 
       
   235 void MComm::CommReadCancel()
       
   236 /**
       
   237  * Cancel an outstanding read request.
       
   238  */
       
   239 	{
       
   240 	if (iCommReader)
       
   241 		iCommReader->Cancel();
       
   242 	}
       
   243 
       
   244 //
       
   245 // CCommWriter
       
   246 //
       
   247 CCommWriter::CCommWriter(MComm* aComm, TInt aPriority)
       
   248 	: CActive(aPriority), iComm(aComm)
       
   249 /**
       
   250  * Constructor for asynchronous writer class.
       
   251  */
       
   252 	{
       
   253 	__DECLARE_NAME(_S("CCommWriter"));
       
   254 	CActiveScheduler::Add(this);
       
   255 	}
       
   256 
       
   257 CCommWriter::~CCommWriter()
       
   258 /**
       
   259  * Destructor for writer class.
       
   260  */
       
   261 	{
       
   262 	Cancel();
       
   263 	}
       
   264 
       
   265 void CCommWriter::RunL()
       
   266 /**
       
   267  * Active Scheduler callback function, indicating a write completion.
       
   268  */
       
   269 	{
       
   270 	iComm->CommWriteComplete(iStatus.Int());
       
   271 	}
       
   272 
       
   273 void CCommWriter::DoCancel()
       
   274 /**
       
   275  * Cancel an outstanding write operation.
       
   276  */
       
   277 	{
       
   278 	iComm->iCommPort.WriteCancel();
       
   279 	}
       
   280 
       
   281 //
       
   282 // CCommReader
       
   283 //
       
   284 CCommReader::CCommReader(MComm* aComm, TInt aPriority)
       
   285 	: CActive(aPriority), iComm(aComm)
       
   286 /**
       
   287  * Constructor for asynchronous reader class.
       
   288  */
       
   289 	{
       
   290 	__DECLARE_NAME(_S("CCommReader"));
       
   291 	CActiveScheduler::Add(this);
       
   292 	}
       
   293 
       
   294 CCommReader::~CCommReader()
       
   295 /**
       
   296  * Destructor for reader class.
       
   297  */
       
   298 	{
       
   299 	Cancel();
       
   300 	}
       
   301 
       
   302 void CCommReader::RunL()
       
   303 /**
       
   304  * Active Scheduler callback function, indicating a read completion.
       
   305  */
       
   306 	{
       
   307 	iComm->CommReadComplete(iStatus.Int());
       
   308 	}
       
   309 
       
   310 void CCommReader::DoCancel()
       
   311 /**
       
   312  * Cancel an outstanding write operation.
       
   313  */
       
   314 	{
       
   315 	iComm->iCommPort.ReadCancel();
       
   316 	}