telephonyserverplugins/multimodetsy/hayes/SCOMM.CPP
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
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 // Comms reader and writer mixin
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "ATSTD.H"
       
    19 #include "SCOMM.H"
       
    20 
       
    21 MComm::MComm()
       
    22 	{
       
    23 	iCommReader = NULL;
       
    24 	iCommWriter = NULL;
       
    25 	}
       
    26 
       
    27 MComm::~MComm()
       
    28 	{
       
    29     delete iCommReader;
       
    30 	delete iCommWriter;
       
    31 	iCommPort.Close();
       
    32 	iCommServer.Close();
       
    33 	}
       
    34 
       
    35 TInt MComm::CommOpen(const TDesC& aDll, const TDesC& aName, TCommAccess aMode)
       
    36 	{
       
    37 	TInt err;
       
    38 	if (err = iCommServer.Connect(), err!=KErrNone)
       
    39 		return err;
       
    40 	if (aDll.Length()>0)
       
    41 		{
       
    42 		if (err = iCommServer.LoadCommModule(aDll), err!=KErrNone)
       
    43 			{
       
    44 			iCommServer.Close();
       
    45 			return err;
       
    46 			}
       
    47 		}
       
    48 	if (err = iCommPort.Open(iCommServer, aName, ECommExclusive), err!=KErrNone)
       
    49 		// if any other TSY (or other app) is using comm port, we want to return gracefully
       
    50 		// with error
       
    51 		{
       
    52 		iCommServer.Close();
       
    53 		return err;
       
    54 		}
       
    55 	if (aMode==ECommShared)
       
    56 		{
       
    57 		iCommPort.Close();
       
    58 		if (err = iCommPort.Open(iCommServer, aName, aMode), err!=KErrNone)
       
    59 			{
       
    60 			iCommServer.Close();
       
    61 			return err;
       
    62 			}
       
    63 		}
       
    64 	return KErrNone;
       
    65 	}
       
    66 
       
    67 TInt MComm::CommOpen(const TDesC& aName, TCommAccess aMode)
       
    68 	{
       
    69 	TInt err;
       
    70 	if (err = iCommServer.Connect(), err!=KErrNone)
       
    71 		return err;
       
    72 	if (err = iCommPort.Open(iCommServer, aName, ECommExclusive), err!=KErrNone)
       
    73 		// if any other TSY (or other app) is using comm port, we want to return gracefully
       
    74 		// with error
       
    75 		{
       
    76 		iCommServer.Close();
       
    77 		return err;
       
    78 		}
       
    79 	if (aMode==ECommShared)
       
    80 		{
       
    81 		iCommPort.Close();
       
    82 		if (err = iCommPort.Open(iCommServer, aName, aMode), err!=KErrNone)
       
    83 			{
       
    84 			iCommServer.Close();
       
    85 			return err;
       
    86 			}
       
    87 		}
       
    88 	return KErrNone;
       
    89 	}
       
    90 
       
    91 void MComm::CommClose()
       
    92 	{
       
    93 	iCommReader->Cancel();
       
    94 	iCommWriter->Cancel();
       
    95 	iCommPort.Close();
       
    96 	iCommServer.Close();
       
    97 	}
       
    98 
       
    99 void MComm::CommConstructL(TInt aReadPriority, TInt aWritePriority)
       
   100 	{
       
   101 	iCommReader = new (ELeave) CCommReader(this, aReadPriority);
       
   102 	iCommWriter = new (ELeave) CCommWriter(this, aWritePriority);
       
   103 	};
       
   104 
       
   105 void MComm::CommWrite(const TDesC8& aDes)
       
   106 	{
       
   107 	__ASSERT_ALWAYS(iCommWriter!=NULL, Panic(EATCommand_NotConstructed));
       
   108 	iCommPort.Write(iCommWriter->StatusRef(), aDes);
       
   109 	iCommWriter->Activate();
       
   110 	}
       
   111 
       
   112 void MComm::CommWriteReady()
       
   113 	{
       
   114 	__ASSERT_ALWAYS(iCommWriter!=NULL, Panic(EATCommand_NotConstructed));
       
   115 	iCommPort.Write(iCommWriter->StatusRef(), TPtrC8(NULL, 0));
       
   116 	iCommWriter->Activate();
       
   117 	}
       
   118 
       
   119 void MComm::CommRead(TDes8& aDes)
       
   120 	{
       
   121 	__ASSERT_ALWAYS(iCommReader!=NULL, Panic(EATCommand_NotConstructed));
       
   122 	iCommPort.Read(iCommReader->StatusRef(), aDes, aDes.Length());
       
   123 	iCommReader->Activate();
       
   124 	}
       
   125 
       
   126 void MComm::CommReadOneOrMore(TDes8& aDes)
       
   127 	{
       
   128 	__ASSERT_ALWAYS(iCommReader!=NULL, Panic(EATCommand_NotConstructed));
       
   129 	iCommPort.ReadOneOrMore(iCommReader->StatusRef(), aDes);
       
   130 	iCommReader->Activate();
       
   131 	}
       
   132 
       
   133 void MComm::CommReadReady()
       
   134 	{
       
   135 	__ASSERT_ALWAYS(iCommReader!=NULL, Panic(EATCommand_NotConstructed));
       
   136 //	TRequestStatus aStatus;
       
   137 //	iCommPort.Read(aStatus,iBuf,0);
       
   138 //	User::WaitForRequest(aStatus);
       
   139 // JoeF 26.11.99. IrComm.Csy can't handle a read into a zero-length buffer and needs fixing. The
       
   140 // above lines have been removed from the TSY as a temporary workaround only, until a proper fix
       
   141 // can be implemented in IrComm.Csy
       
   142 	}
       
   143 
       
   144 void MComm::CommCancel()
       
   145 	{
       
   146 	if (iCommWriter)
       
   147 		iCommWriter->Cancel();
       
   148 	if (iCommReader)
       
   149 		iCommReader->Cancel();
       
   150 	}
       
   151 
       
   152 void MComm::CommWriteCancel()
       
   153 	{
       
   154 	if (iCommWriter)
       
   155 		iCommWriter->Cancel();
       
   156 	}
       
   157 
       
   158 void MComm::CommReadCancel()
       
   159 	{
       
   160 	if (iCommReader)
       
   161 		iCommReader->Cancel();
       
   162 	}
       
   163 
       
   164 //
       
   165 // CCommWriter
       
   166 //
       
   167 CCommWriter::CCommWriter(MComm* aComm, TInt aPriority)
       
   168 	: CActive(aPriority), iComm(aComm)
       
   169 	{
       
   170 	__DECLARE_NAME(_S("CCommWriter"));
       
   171 	CActiveScheduler::Add(this);
       
   172 	}
       
   173 
       
   174 CCommWriter::~CCommWriter()
       
   175 	{
       
   176 	Cancel();
       
   177 	}
       
   178 
       
   179 void CCommWriter::RunL()
       
   180 	{
       
   181 	iComm->CommWriteComplete(iStatus.Int());
       
   182 	}
       
   183 
       
   184 void CCommWriter::DoCancel()
       
   185 	{
       
   186 	iComm->iCommPort.WriteCancel();
       
   187 	}
       
   188 
       
   189 //
       
   190 // CCommReader
       
   191 //
       
   192 CCommReader::CCommReader(MComm* aComm, TInt aPriority)
       
   193 	: CActive(aPriority), iComm(aComm)
       
   194 	{
       
   195 	__DECLARE_NAME(_S("CCommReader"));
       
   196 	CActiveScheduler::Add(this);
       
   197 	}
       
   198 
       
   199 CCommReader::~CCommReader()
       
   200 	{
       
   201 	Cancel();
       
   202 	}
       
   203 
       
   204 void CCommReader::RunL()
       
   205 	{
       
   206 	iComm->CommReadComplete(iStatus.Int());
       
   207 	}
       
   208 
       
   209 void CCommReader::DoCancel()
       
   210 	{
       
   211 	iComm->iCommPort.ReadCancel();
       
   212 	}