linklayercontrol/networkinterfacemgr/src/MCOMM.CPP
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     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 /**
       
    19  @file
       
    20 */
       
    21 
       
    22 
       
    23 #include <nifutl.h>
       
    24 #include "NI_STD.H"
       
    25 #include "Ni_Log.h"
       
    26 
       
    27 class CCommLinkStatus;
       
    28 NONSHARABLE_CLASS(CCommReader) : public CActive
       
    29 /**
       
    30 @internalComponent
       
    31 */
       
    32 	{
       
    33 friend class MComm;
       
    34 friend class CCommLinkStatus;
       
    35 public:
       
    36 	CCommReader(MComm* aComm, TInt aPriority);
       
    37 	virtual ~CCommReader();
       
    38 	virtual void RunL();
       
    39 	virtual void DoCancel();
       
    40 private:
       
    41 	void CompleteNow(TInt aError);
       
    42 private:
       
    43 	MComm *iComm;
       
    44 	};
       
    45 
       
    46 NONSHARABLE_CLASS(CCommWriter) : public CActive
       
    47 /**
       
    48 @internalComponent
       
    49 */
       
    50 	{
       
    51 friend class MComm;
       
    52 friend class CCommLinkStatus;
       
    53 public:
       
    54 	CCommWriter(MComm* aComm, TInt aPriority);
       
    55 	virtual ~CCommWriter();
       
    56 	virtual void RunL();
       
    57 	virtual void DoCancel();
       
    58 private:
       
    59 	void CompleteNow(TInt aError);
       
    60 private:
       
    61 	MComm *iComm;
       
    62 	};
       
    63 
       
    64 NONSHARABLE_CLASS(CCommLinkStatus) : public CActive
       
    65 /**
       
    66 @internalComponent
       
    67 */
       
    68 	{
       
    69 friend class MComm;
       
    70 public:
       
    71 	CCommLinkStatus(MComm* aComm);
       
    72 	virtual ~CCommLinkStatus();
       
    73 	virtual void RunL();
       
    74 	virtual void DoCancel();
       
    75 	virtual void NotifyDisconnect();
       
    76 	virtual void SetRole(TCommRole aRole);
       
    77 private:
       
    78 	MComm *iComm;
       
    79 	TUint iSignals;
       
    80 	TUint iSavedSignalState;
       
    81 	TCommRole iRole;
       
    82 	TUint iNotifyChangeSignalMask;
       
    83 	};
       
    84 
       
    85 #if !defined(__EABI__) && !defined(__X86GCC__)
       
    86 EXPORT_C MComm::MComm()
       
    87 /**
       
    88 Constructor
       
    89 */
       
    90 	{
       
    91 	}
       
    92 #endif
       
    93 
       
    94 EXPORT_C void MComm::CommDelete()
       
    95 	{
       
    96 	delete iCommReader;
       
    97 	delete iCommWriter;
       
    98 	delete iCommLinkStatus;
       
    99 	iCommPort.Close();
       
   100 	iCommServer.Close();
       
   101 	}
       
   102 
       
   103 	
       
   104 EXPORT_C TInt MComm::CommOpen(const TDesC& aDll, const TDesC& aName, TCommAccess aMode)
       
   105 	{
       
   106 	return CommOpen(aDll, aName, aMode, ECommRoleDTE);
       
   107 	}
       
   108 
       
   109 EXPORT_C TInt MComm::CommOpen(const TDesC& aDll, const TDesC& aName, TCommAccess aMode, TCommRole aRole)
       
   110 	{
       
   111 
       
   112 	LOG( NifmanLog::Printf(_L("MComm:\t\t\tOpening CSY '%S' Port '%S' Role '%d'"), &aDll, &aName, aRole); )
       
   113 	
       
   114 	__ASSERT_ALWAYS(iCommLinkStatus!=NULL, NetUtilPanic(ENuPanic_NotConstructed));
       
   115     iCommLinkStatus->SetRole(aRole);
       
   116 
       
   117 	TInt err;
       
   118 	if (err = iCommServer.Connect(), err!=KErrNone)
       
   119 		{
       
   120 		LOG( NifmanLog::Printf(_L("MComm:\t\t\tConnect failed (error is %d)"), err); )
       
   121 		return err;
       
   122 		}
       
   123 	if (aDll.Length()>0)
       
   124 		{
       
   125 		if (err = iCommServer.LoadCommModule(aDll), err!=KErrNone)
       
   126 			{
       
   127 			LOG( NifmanLog::Printf(_L("MComm:\t\t\tCould not open CSY '%S' (error is %d)"), &aDll, err); )
       
   128 
       
   129 			iCommServer.Close();
       
   130 			return err;
       
   131 			}
       
   132 		}
       
   133 	if (err = iCommPort.Open(iCommServer, aName, aMode, aRole), err!=KErrNone)
       
   134 		{
       
   135 		LOG( NifmanLog::Printf(_L("MComm:\t\t\tCould not open Port '%S' (error is %d)"), &aName, err); )
       
   136 
       
   137 		iCommServer.Close();
       
   138 		return err;
       
   139 		}
       
   140 	return KErrNone;
       
   141 	}
       
   142 
       
   143 EXPORT_C TInt MComm::CommOpen(const TDesC& aName, TCommAccess aMode)
       
   144 	{
       
   145 
       
   146 	LOG( NifmanLog::Printf(_L("MComm:\t\t\tOpening Port '%S'"), &aName); )
       
   147 
       
   148 	TInt err;
       
   149 	if (err = iCommServer.Connect(), err!=KErrNone)
       
   150 		return err;
       
   151 	if (err = iCommPort.Open(iCommServer, aName, aMode), err!=KErrNone)
       
   152 		{
       
   153 		LOG( NifmanLog::Printf(_L("MComm:\t\t\tCould not open Port '%S' (error is %d)"), &aName, err); )
       
   154 
       
   155 		iCommServer.Close();
       
   156 		return err;
       
   157 		}
       
   158 	return KErrNone;
       
   159 	}
       
   160 
       
   161 EXPORT_C void MComm::CommClose()
       
   162 	{
       
   163 	iCommReader->Cancel();
       
   164 	iCommWriter->Cancel();
       
   165 	iCommLinkStatus->Cancel();
       
   166 	iCommPort.Close();
       
   167 	iCommServer.Close();
       
   168 	}
       
   169 
       
   170 EXPORT_C void MComm::CommConstructL(TInt aReadPriority, TInt aWritePriority)
       
   171 	{
       
   172 	iCommReader = new (ELeave) CCommReader(this, aReadPriority);
       
   173 	iCommWriter = new (ELeave) CCommWriter(this, aWritePriority);
       
   174 	iCommLinkStatus = new (ELeave) CCommLinkStatus(this);
       
   175 	}
       
   176 
       
   177 EXPORT_C void MComm::CommWrite(const TDesC8& aDes)
       
   178 	{
       
   179 	__ASSERT_ALWAYS(iCommWriter!=NULL, NetUtilPanic(ENuPanic_NotConstructed));
       
   180 	iCommPort.Write(iCommWriter->iStatus, aDes);
       
   181 	iCommWriter->SetActive();
       
   182 	iCommLinkStatus->NotifyDisconnect();
       
   183 	}
       
   184 
       
   185 EXPORT_C void MComm::CommWriteReady()
       
   186 	{
       
   187 	__ASSERT_ALWAYS(iCommWriter!=NULL, NetUtilPanic(ENuPanic_NotConstructed));
       
   188 	iCommPort.Write(iCommWriter->iStatus, TPtrC8(NULL, 0));
       
   189 	iCommWriter->SetActive();
       
   190 	iCommLinkStatus->NotifyDisconnect();
       
   191 	}
       
   192 
       
   193 EXPORT_C TBool MComm::CommIsWriting() const
       
   194 	{
       
   195 
       
   196 	return iCommWriter->IsActive();
       
   197 	}
       
   198 
       
   199 EXPORT_C void MComm::CommRead(TDes8& aDes)
       
   200 	{
       
   201 	__ASSERT_ALWAYS(iCommReader!=NULL, NetUtilPanic(ENuPanic_NotConstructed));
       
   202 	iCommPort.Read(iCommReader->iStatus, aDes, aDes.Length());
       
   203 	iCommReader->SetActive();
       
   204 	iCommLinkStatus->NotifyDisconnect();
       
   205 	}
       
   206 
       
   207 EXPORT_C void MComm::CommReadOneOrMore(TDes8& aDes)
       
   208 	{
       
   209 	__ASSERT_ALWAYS(iCommReader!=NULL, NetUtilPanic(ENuPanic_NotConstructed));
       
   210 	iCommPort.ReadOneOrMore(iCommReader->iStatus, aDes);
       
   211 	iCommReader->SetActive();
       
   212 	iCommLinkStatus->NotifyDisconnect();
       
   213 	}
       
   214 
       
   215 EXPORT_C void MComm::CommReadReady()
       
   216 	{
       
   217 	__ASSERT_ALWAYS(iCommReader!=NULL, NetUtilPanic(ENuPanic_NotConstructed));
       
   218 	iCommReader->SetActive();
       
   219 	iCommLinkStatus->NotifyDisconnect();
       
   220 	}
       
   221 
       
   222 EXPORT_C TBool MComm::CommIsReading() const
       
   223 	{
       
   224 
       
   225 	return iCommReader->IsActive();
       
   226 	}
       
   227 
       
   228 EXPORT_C void MComm::CommCancel()
       
   229 	{
       
   230 	if (iCommWriter)
       
   231 		iCommWriter->Cancel();
       
   232 	if (iCommReader)
       
   233 		iCommReader->Cancel();
       
   234 	if (iCommLinkStatus)
       
   235 		iCommLinkStatus->Cancel();
       
   236 	}
       
   237 
       
   238 EXPORT_C void MComm::CommWriteCancel()
       
   239 	{
       
   240 	if (iCommWriter)
       
   241 		iCommWriter->Cancel();
       
   242 	}
       
   243 
       
   244 EXPORT_C void MComm::CommReadCancel()
       
   245 	{
       
   246 	if (iCommReader)
       
   247 		iCommReader->Cancel();
       
   248 	}
       
   249 
       
   250 //
       
   251 
       
   252 CCommWriter::CCommWriter(MComm* aComm, TInt aPriority)
       
   253 	: CActive(aPriority), iComm(aComm)
       
   254 	{
       
   255 	CActiveScheduler::Add(this);
       
   256 	}
       
   257 
       
   258 CCommWriter::~CCommWriter()
       
   259 	{
       
   260 	Cancel();
       
   261 	}
       
   262 
       
   263 void CCommWriter::RunL()
       
   264 	{
       
   265 	iComm->CommWriteCancel();
       
   266 	iComm->CommWriteComplete(iStatus.Int());
       
   267 	}
       
   268 
       
   269 void CCommWriter::DoCancel()
       
   270 	{
       
   271 	iComm->iCommPort.WriteCancel();
       
   272 	}
       
   273 
       
   274 void CCommWriter::CompleteNow(TInt aError)
       
   275 	{
       
   276 	TRequestStatus* statusPtr=&iStatus;
       
   277 	User::RequestComplete(statusPtr,aError);
       
   278 	}
       
   279 
       
   280 //
       
   281 
       
   282 CCommReader::CCommReader(MComm* aComm, TInt aPriority)
       
   283 	: CActive(aPriority), iComm(aComm)
       
   284 	{
       
   285 	CActiveScheduler::Add(this);
       
   286 	}
       
   287 
       
   288 CCommReader::~CCommReader()
       
   289 	{
       
   290 	Cancel();
       
   291 	}
       
   292 
       
   293 void CCommReader::RunL()
       
   294 	{
       
   295 	iComm->CommReadCancel();
       
   296 	iComm->CommReadComplete(iStatus.Int());
       
   297 	}
       
   298 
       
   299 void CCommReader::DoCancel()
       
   300 	{
       
   301 	iComm->iCommPort.ReadCancel();
       
   302 	}
       
   303 
       
   304 void CCommReader::CompleteNow(TInt aError)
       
   305 	{
       
   306 	TRequestStatus* statusPtr=&iStatus;
       
   307 	User::RequestComplete(statusPtr,aError);
       
   308 	}
       
   309 
       
   310 //
       
   311 
       
   312 CCommLinkStatus::CCommLinkStatus(MComm* aComm)
       
   313 	: CActive(EPriorityHigh), iComm(aComm)
       
   314 	{
       
   315 	iRole = ECommRoleDTE;
       
   316 	iNotifyChangeSignalMask = KSignalDCD; // Defaults to KSignalDCD
       
   317 	CActiveScheduler::Add(this);
       
   318 	}
       
   319 
       
   320 CCommLinkStatus::~CCommLinkStatus()
       
   321 	{
       
   322 	Cancel();
       
   323 	}
       
   324 
       
   325 void CCommLinkStatus::SetRole(TCommRole aRole)
       
   326     {
       
   327     iRole = aRole;
       
   328 	if (iRole==ECommRoleDCE)
       
   329 	    {
       
   330 	    iNotifyChangeSignalMask = KSignalDTR;
       
   331 	    }
       
   332 	else
       
   333 	    {
       
   334 	    iNotifyChangeSignalMask = KSignalDCD; // Defaults to KSignalDCD
       
   335 	    }
       
   336     
       
   337     }
       
   338 
       
   339 
       
   340 void CCommLinkStatus::NotifyDisconnect()
       
   341 	{
       
   342 	if(IsActive())	// if we have already set up notification
       
   343 		return;		// there is nothing to do now
       
   344 
       
   345 	// get the current state of the line so when we get notification we can tell if it really has changed	
       
   346 	iSavedSignalState = iComm->iCommPort.Signals (iNotifyChangeSignalMask) & iNotifyChangeSignalMask ;
       
   347 	iComm->iCommPort.NotifySignalChange(iStatus, iSignals, iNotifyChangeSignalMask);
       
   348 
       
   349 	SetActive();
       
   350 	}
       
   351 
       
   352 void CCommLinkStatus::RunL()
       
   353 	{
       
   354 	// We should only tear down the connection if DCD/DTR (depending on role) is low.
       
   355 	//   and if the signal has changed (i.e. it was high)
       
   356 	if (!(iNotifyChangeSignalMask & iSignals) && (iNotifyChangeSignalMask & iSavedSignalState)) 
       
   357 		{
       
   358 		if (iComm->CommIsReading())
       
   359 			{
       
   360 			iComm->CommReadCancel();
       
   361 			iComm->CommReadComplete(KErrCommsLineFail);
       
   362 			}
       
   363 
       
   364 		if (iComm->CommIsWriting())
       
   365 			{
       
   366 			iComm->CommWriteCancel();
       
   367 			iComm->CommWriteComplete(KErrCommsLineFail);
       
   368 			}
       
   369 		}
       
   370 		iSavedSignalState = iSignals & iNotifyChangeSignalMask ;	// update saved state
       
   371 	}
       
   372 
       
   373 void CCommLinkStatus::DoCancel()
       
   374 	{
       
   375 	iComm->iCommPort.NotifySignalChangeCancel();
       
   376 	}