irda/irdastack/irtranp/comreadwrite.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     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 // ccomreadwrite.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "debug.h"
       
    19 
       
    20 #include "comreadwrite.h"
       
    21 #include "GLOBAL.H"
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <f32file.h>
       
    25 #include <c32comm.h>
       
    26 #include <e32cons.h>
       
    27 
       
    28 
       
    29 CComReadWrite::CComReadWrite(TInt aPriority, MComReadWriteObserver& aObserver)
       
    30 				: CActive(aPriority), iObserver(aObserver)
       
    31 	{
       
    32 	}
       
    33 
       
    34 CComReadWrite* CComReadWrite::NewL(MComReadWriteObserver& aObserver)
       
    35 	{
       
    36 	DEBUG_OPEN();
       
    37 	COMRWPRINT(_L("CComReadWrite::NewL()\n"));
       
    38 	
       
    39 	CComReadWrite* self = new(ELeave) CComReadWrite(EPriorityHigh, aObserver);
       
    40 	CleanupStack::PushL(self);
       
    41 	self->ConstructL();
       
    42 	CleanupStack::Pop();
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 void CComReadWrite::ConstructL()
       
    47 	{
       
    48 	CActiveScheduler::Add(this);
       
    49 
       
    50 	COMRWPRINT(_L("CComReadWrite::ConstructL()\n"));
       
    51 	}
       
    52 
       
    53 void CComReadWrite::Open()
       
    54 	{
       
    55 	// Connect to Comserver and Open IrComm port
       
    56 	COMRWPRINT(_L("CComReadWrite::Open()\n"));
       
    57 	TInt error;
       
    58 	error = iServer.Connect();
       
    59 	error = iServer.LoadCommModule(_L("IrCOMM"));
       
    60 	error = iCommPort.Open(iServer,_L("IRCOMM::0"), ECommExclusive, ECommRoleDCE);
       
    61 	Signal(error);
       
    62 	iState = EOpen;
       
    63 	}
       
    64 
       
    65 void CComReadWrite::Close()
       
    66 	{
       
    67 	COMRWPRINT(_L("CComReadWrite::Close()\n"));
       
    68 	Cancel();
       
    69 	Signal(KErrNone);
       
    70 	iState = EClose;
       
    71 	}
       
    72 
       
    73 void CComReadWrite::Signal(TInt aError)
       
    74 	{
       
    75 	TRequestStatus *pS=(&iStatus);
       
    76 	SetActive();
       
    77 	User::RequestComplete(pS,aError);
       
    78 	}
       
    79 
       
    80 CComReadWrite::~CComReadWrite()
       
    81 	{
       
    82 	COMRWPRINT(_L("CComReadWrite:: ~CComReadWrite()\n"));
       
    83 	}
       
    84 
       
    85 void CComReadWrite::Send(TDesC8& aBuffer)
       
    86 	{
       
    87 	COMRWPRINT(_L("CComReadWrite:: ESend request\n"));
       
    88 	__ASSERT_DEBUG(aBuffer.Length()<= iTxBuffer.MaxLength(), User::Panic(_L("IrTranP"), KErrTooBig));
       
    89 	if (!(IsActive()))
       
    90 		{
       
    91 		iTxBuffer.Zero();
       
    92 		iTxBuffer.Copy(aBuffer);
       
    93 		iCommPort.Write(iStatus, iTxBuffer);
       
    94 		COMRWPRINT(_L("CComReadWrite:: iCommPort.Write cleared\n"));
       
    95 		iState = ESend;
       
    96 		SetActive();
       
    97 
       
    98 		}
       
    99 	else
       
   100 		{
       
   101 		COMRWPRINT(_L("CComReadWrite: Error\n"));
       
   102 		iObserver.SendError(KErrWrite);
       
   103 		}
       
   104 
       
   105 	}
       
   106 
       
   107 void CComReadWrite::Receive()
       
   108 	{
       
   109 	COMRWPRINT(_L("CComReadWrite: EReceive request\n"));
       
   110 	
       
   111 	// Clear the buffer and receive the next lot of data;
       
   112 
       
   113 	iRxBuffer.Zero();
       
   114 	iCommPort.ReadOneOrMore(iStatus, iRxBuffer);
       
   115 	iState = EReceive;
       
   116 	SetActive();
       
   117 	}
       
   118 
       
   119 void CComReadWrite::RunL()
       
   120 	{
       
   121 	COMRWPRINT(_L("CComReadWrite: RunL()\n"));
       
   122 	
       
   123 	switch (iState)
       
   124 		{
       
   125 	case EOpen:
       
   126 		{
       
   127 		if (iStatus == KErrNone)
       
   128 			{
       
   129 			COMRWPRINT(_L("CComReadWrite: EOpen sucessful\n"));
       
   130 		  	/*TBuf8<1> buf;
       
   131 			buf.SetLength(0);
       
   132 			Send(buf);*/
       
   133 
       
   134 			Receive();
       
   135 			}
       
   136 		else
       
   137 			{
       
   138 			iObserver.Error(iStatus.Int());
       
   139 			}
       
   140 		}
       
   141 		break;
       
   142 	case EStop:
       
   143 		{
       
   144 		iCommPort.Close();
       
   145 		iServer.UnloadCommModule(_L("IrCOMM"));
       
   146 		iServer.Close();
       
   147 		iObserver.Error(KErrAbort);
       
   148 		COMRWPRINT(_L("CComReadWrite: EStop sucessfull\n"));
       
   149 		}
       
   150 		break;
       
   151 	case EClose:
       
   152 		{
       
   153 		if (iStatus == KErrNone)
       
   154 			{
       
   155 			iCommPort.Close();
       
   156 			iServer.UnloadCommModule(_L("IrCOMM"));
       
   157 			iServer.Close();
       
   158 			COMRWPRINT(_L("CComReadWrite: EClose sucessfull\n"));
       
   159 			}
       
   160 		else
       
   161 			{
       
   162 			iObserver.Error(iStatus.Int());
       
   163 			}
       
   164 		}
       
   165 		break;
       
   166 	case ESend: // Send Complete
       
   167 		if (iStatus == KErrNone)
       
   168 			{
       
   169 			COMRWPRINT(_L("CComReadWrite: ESend sucessful\n"));
       
   170 			iObserver.SendComplete(); // posts read request.
       
   171 			}
       
   172 		else
       
   173 			{
       
   174 			iObserver.SendError(iStatus.Int());
       
   175 			}
       
   176 		break;
       
   177 	case EReceive:
       
   178 		{
       
   179 		if (iStatus == KErrNone)
       
   180 			{
       
   181 			COMRWPRINT_2(_L("CComReadWrite: ERecieve sucessful %d\n"),iRxBuffer.Length());
       
   182 			iObserver.ReceiveComplete(iRxBuffer);
       
   183 			}
       
   184 		else
       
   185 			{
       
   186 			iObserver.ReceiveError(iStatus.Int());
       
   187 			}
       
   188 		}
       
   189 		break;
       
   190 	default:
       
   191 		User::Leave(KErrGeneral);
       
   192 		break;
       
   193 		}
       
   194 	
       
   195 	}
       
   196 
       
   197 void CComReadWrite::DoCancel()
       
   198 	{
       
   199 	COMRWPRINT(_L("CComReadWrite: DoCancel()\n"));
       
   200 
       
   201 	iCommPort.WriteCancel();
       
   202 	iCommPort.ReadCancel();
       
   203 	iState = EStop;
       
   204 	}