serialserver/c32serialserver/Test/TE_C32/d_comm.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 1995-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 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include <e32base.h>
       
    18 
       
    19 #define __KERNEL_MODE__
       
    20 #include <kernel.h>
       
    21 #undef  __KERNEL_MODE__
       
    22 
       
    23 #include <d32comm.h>
       
    24 #include <e32ver.h>
       
    25 
       
    26 #include "d_comm.h"
       
    27 #include <e32hal.h>
       
    28 #include "DParams.h"
       
    29 
       
    30 const TUint KXoffSignal=0x80000000;
       
    31 //
       
    32 const TInt KInputHeldFree=(-1);
       
    33 const TInt KInputHeld=(-2);
       
    34 //
       
    35 const TUint KTransmitting=0x01;
       
    36 const TUint KBreaking=0x02;
       
    37 const TUint KBreakPending=0x04;
       
    38 //
       
    39 enum TPanic
       
    40 	{
       
    41 	ESetConfigWhileRequestPending,
       
    42 	ESetSignalsSetAndClear,
       
    43 	EResetBuffers,
       
    44 	ESetReceiveBufferLength,
       
    45 //	EDoubleTxBufFill,
       
    46 	};
       
    47 
       
    48 EXPORT_C DLogicalDevice *CreateLogicalDevice()
       
    49 //
       
    50 // Create a new device
       
    51 //
       
    52 	{
       
    53 	return(new DDeviceComm);
       
    54 	}
       
    55 
       
    56 DDeviceComm::DDeviceComm()
       
    57 //
       
    58 // Constructor
       
    59 //
       
    60 	{
       
    61 	//iParseMask=KDeviceAllowAll;
       
    62 	iParseMask=KDeviceAllowUnit|KDeviceAllowInfo;
       
    63 	iUnitsMask=0xffffffff; // Leave units decision to the driver
       
    64 	iVersion=TVersion(KCommsMajorVersionNumber,KCommsMinorVersionNumber,KCommsBuildVersionNumber);
       
    65 	}
       
    66 
       
    67 TInt DDeviceComm::Install()
       
    68 //
       
    69 // Install the device driver.
       
    70 //
       
    71 	{
       
    72     TPtrC name=_L("CommDCE");
       
    73 	return(SetName(&name));
       
    74 	}
       
    75 
       
    76 void DDeviceComm::GetCaps(TDes8 &aDes) const
       
    77 //
       
    78 // Return the Comm capabilities.
       
    79 //
       
    80 	{
       
    81 
       
    82 	TCapsDevCommV01 b;
       
    83 	b.version=iVersion;
       
    84 	aDes.FillZ(aDes.MaxLength());
       
    85 	aDes.Copy((TUint8 *)&b,Min(aDes.MaxLength(),sizeof(b)));
       
    86 	}
       
    87 
       
    88 DLogicalChannel *DDeviceComm::CreateL()
       
    89 //
       
    90 // Create a channel on the device.
       
    91 //
       
    92 	{
       
    93 
       
    94 	return(new(ELeave) DChannelComm(this));
       
    95 	}
       
    96 
       
    97 
       
    98 #pragma warning( disable : 4705 )	// statement has no effect
       
    99 #pragma warning( disable : 4355 )	// this used in intializer list
       
   100 DChannelComm::DChannelComm(DLogicalDevice *aDevice)
       
   101 //
       
   102 // Constructor
       
   103 //
       
   104 : DLogicalChannel(aDevice)
       
   105 	{
       
   106 
       
   107 	SetBehaviour(RBusDevComm::ERequestReadCancel|
       
   108 		RBusDevComm::ERequestWriteCancel|
       
   109 		RBusDevComm::ERequestBreakCancel|
       
   110 		RBusDevCommDCE::ERequestNotifySignalChangeCancel|
       
   111 		RBusDevCommDCE::ERequestNotifyFlowControlChangeCancel|
       
   112 		RBusDevCommDCE::ERequestNotifyConfigChangeCancel);
       
   113 //
       
   114 // Setup the default config
       
   115 //
       
   116 	iConfig.iRate=DUMMYCONFIGRATE;
       
   117 	iConfig.iDataBits=DUMMYDATABITS;
       
   118 	iConfig.iStopBits=DUMMYSTOPBITS;
       
   119 	iConfig.iHandshake=DUMMYHANDSHAKE;
       
   120 	}
       
   121 #pragma warning( default : 4705 )
       
   122 #pragma warning( disable : 4355 )
       
   123 
       
   124 DChannelComm::~DChannelComm()
       
   125 //
       
   126 // Destructor
       
   127 //
       
   128 	{
       
   129 	CompleteAll(KErrAbort);
       
   130 	delete iDriver;
       
   131 	}
       
   132 
       
   133 void DChannelComm::DoCreateL(TInt /*aUnit*/,CBase *aDriver,const TDesC * /*anInfo*/,const TVersion &aVer)
       
   134 //
       
   135 // Create the channel from the passed info.
       
   136 //
       
   137 	{
       
   138 	iDriver=(DComm *)aDriver; // Must do this to ensure the driver is destroyed by the destructor
       
   139 	if (!User::QueryVersionSupported(TVersion(KCommsMajorVersionNumber,KCommsMinorVersionNumber,KCommsBuildVersionNumber),aVer))
       
   140 		User::Leave(KErrNotSupported);
       
   141 
       
   142 //	((DComm *)aDriver)->SetComm(this); // Rog
       
   143 	}
       
   144 
       
   145 void DChannelComm::DoCancel(TInt aReqNo)
       
   146 //
       
   147 // Cancel an outstanding request.
       
   148 //
       
   149 	{
       
   150 
       
   151 	switch (aReqNo)
       
   152 		{
       
   153 	case RBusDevComm::ERequestRead:
       
   154 		iRxDataAvailablePending=EFalse;
       
   155 		break;
       
   156 	case RBusDevCommDCE::ERequestNotifySignalChange:
       
   157 		iSignalCount++;
       
   158 		iSignalNotifyPending=EFalse;
       
   159 		break;
       
   160 	case RBusDevCommDCE::ERequestNotifyFlowControlChange:
       
   161 		iFlowControlChangePending=EFalse;
       
   162 		break;
       
   163 	case RBusDevCommDCE::ERequestNotifyConfigChange:
       
   164 		iConfigChangePending=EFalse;
       
   165 		break;
       
   166 	default:
       
   167 		__ASSERT_ALWAYS(0,User::Invariant());
       
   168 		}
       
   169 	}
       
   170 
       
   171 void DChannelComm::DoRequest(TInt aReqNo,TAny *a1,TAny *a2)
       
   172 //
       
   173 // Async requests.
       
   174 //
       
   175 	{
       
   176 	aReqNo &= (~KDataAvailableNotifyFlag);
       
   177 	switch (aReqNo)
       
   178 		{
       
   179 	case RBusDevCommDCE::ERequestWrite:
       
   180 		{
       
   181 		__ASSERT_ALWAYS(iWritePending==EFalse,User::Invariant());
       
   182 		iWritePending=ETrue;
       
   183 		iQueuedWrite.OneShotInMicroSeconds(KDummyTimeOut,DChannelComm::WriteCompleted,this);
       
   184 		}
       
   185 		break;
       
   186 	case RBusDevCommDCE::ERequestRead:	// this is the RxDataAvailable notification. Base have chosen
       
   187 									// to use the enum for both this and Read().
       
   188 		{
       
   189 		__ASSERT_ALWAYS(iRxDataAvailablePending==EFalse,User::Invariant());
       
   190 		iRxDataAvailablePending=ETrue;
       
   191 		iQueuedRxDataAvailable.OneShotInMicroSeconds(KDummyTimeOut,DChannelComm::RxDataAvailableCompleted,this);
       
   192 		}
       
   193 		break;
       
   194 	case RBusDevCommDCE::ERequestNotifySignalChange:
       
   195 		{
       
   196 		__ASSERT_ALWAYS(iSignalNotifyPending==EFalse,User::Invariant());
       
   197 		iSignalNotifyPending=ETrue;
       
   198 		switch (iSignalCount)
       
   199 			{
       
   200 		case 0:
       
   201 		case 2:
       
   202 			__ASSERT_ALWAYS(*((TUint*)a2) == (DUMMYSIGNALMASK),User::Invariant());	// and wait for cancel
       
   203 			break;
       
   204 		case 1:
       
   205 			__ASSERT_ALWAYS(*((TUint*)a2) == (DUMMYSIGNALMASK|KSignalBreak),User::Invariant());// and wait for cancel
       
   206 			break;
       
   207 		case 3:
       
   208 		case 4:
       
   209 			{
       
   210 			iSignals=(TUint*)a1;
       
   211 			iSignalMask=*((TUint*)a2);
       
   212 			iQueuedSignalNotify.OneShotInMicroSeconds(KDummyTimeOut,DChannelComm::SignalChangeCompleted,this);
       
   213 			break;
       
   214 			}
       
   215 		default:
       
   216 			__ASSERT_ALWAYS(0,User::Invariant());
       
   217 			}
       
   218 		break;
       
   219 	case RBusDevCommDCE::ERequestNotifyFlowControlChange:
       
   220 		{
       
   221 		__ASSERT_ALWAYS(iFlowControlChangePending==EFalse,User::Invariant());
       
   222 		iFlowControlChangePending=ETrue;
       
   223 		iQueuedFlowControl.OneShotInMicroSeconds(KDummyTimeOut,DChannelComm::FlowControlChangeCompleted,this);
       
   224 		}
       
   225 		break;
       
   226 	case RBusDevCommDCE::ERequestNotifyConfigChange:
       
   227 		{
       
   228 		__ASSERT_ALWAYS(iConfigChangePending==EFalse,User::Invariant());
       
   229 		iConfigChangePending=ETrue;
       
   230 		iConfigPointer = (TCommNotificationPckg*)a1;
       
   231 
       
   232 		iQueuedConfigChange.OneShotInMicroSeconds(KDummyTimeOut,DChannelComm::ConfigChangeCompleted,this);
       
   233 		}
       
   234 		break;
       
   235 	default:
       
   236 		Complete(aReqNo,KErrNotSupported);
       
   237 		}
       
   238 		}
       
   239 	}
       
   240 
       
   241 TInt DChannelComm::DoControl(TInt aFunction,TAny *a1,TAny* /*a2*/)
       
   242 //
       
   243 // Sync requests.
       
   244 //
       
   245 	{
       
   246 	TInt r=KErrNone;
       
   247 	switch (aFunction)
       
   248 		{
       
   249 	case RBusDevCommDCE::EControlFlowControlStatus:
       
   250 		{
       
   251 		iFlowControl=DUMMYFLOWCONTROLSTATUS;
       
   252 		*((TFlowControl*)a1) = iFlowControl;
       
   253 		}
       
   254 		break;
       
   255 	case RBusDevCommDCE::EControlSetConfig:
       
   256 		break;
       
   257 	default:
       
   258 		r=KErrNotSupported;
       
   259 		}
       
   260 	return(r);
       
   261 	}
       
   262 
       
   263 
       
   264 void DChannelComm::SignalChangeCompleted(TAny* aChannel,TInt)
       
   265 	{
       
   266 	DChannelComm* dc = (DChannelComm*)aChannel;
       
   267 	dc->SetSignals();
       
   268 	dc->CompleteReq(RBusDevCommDCE::ERequestNotifySignalChange);
       
   269 	}
       
   270 void DChannelComm::ConfigChangeCompleted(TAny* aChannel,TInt)
       
   271 	{
       
   272 	DChannelComm* dc = (DChannelComm*)aChannel;
       
   273 	dc->ChangeConfig();
       
   274 	dc->CompleteReq(RBusDevCommDCE::ERequestNotifyConfigChange);
       
   275 	}
       
   276 void DChannelComm::RxDataAvailableCompleted(TAny* aChannel,TInt)
       
   277 	{
       
   278 	((DChannelComm*)aChannel)->CompleteReq(RBusDevCommDCE::ERequestRead);
       
   279 	}
       
   280 void DChannelComm::FlowControlChangeCompleted(TAny* aChannel,TInt)
       
   281 	{
       
   282 	((DChannelComm*)aChannel)->CompleteReq(RBusDevCommDCE::ERequestNotifyFlowControlChange);
       
   283 	}
       
   284 void DChannelComm::WriteCompleted(TAny* aChannel,TInt)
       
   285 	{
       
   286 	((DChannelComm*)aChannel)->CompleteReq(RBusDevCommDCE::ERequestWrite);
       
   287 	}
       
   288 
       
   289 void DChannelComm::CompleteReq(TInt aReqNo)
       
   290 	{
       
   291 
       
   292 	Complete(aReqNo);
       
   293 	}
       
   294 
       
   295 void DChannelComm::SetSignals()
       
   296 	{
       
   297 	switch(iSignalCount++)
       
   298 		{
       
   299 	case 3:
       
   300 		(*iSignals)=DUMMYSIGNALS;
       
   301 		break;
       
   302 	case 4:
       
   303 		(*iSignals)=(KSignalBreak|KBreakChanged);
       
   304 		break;
       
   305 	default:
       
   306 		__ASSERT_ALWAYS(0,User::Invariant());
       
   307 		};
       
   308 	iSignalNotifyPending=EFalse;
       
   309 	}
       
   310 
       
   311 void DChannelComm::ChangeConfig()
       
   312 	{
       
   313 	iConfig.iChangedMembers = DUMMYCHANGED;
       
   314 	iConfigPointer->FillZ(iConfigPointer->MaxLength());
       
   315 	TInt len=Min(iConfigPointer->MaxLength(),sizeof(iConfig));
       
   316 	iConfigPointer->Copy((TUint8 *)&iConfig,len);
       
   317 	}
       
   318 
       
   319