fax/faxclientandserver/FAXSVR/FAXSERV.CPP
changeset 20 244d7c5f118e
parent 19 1f776524b15c
child 23 6b1d113cdff3
equal deleted inserted replaced
19:1f776524b15c 20:244d7c5f118e
     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 //
       
    15 
       
    16 #include "FAXSERV.H"
       
    17 #include "fax_reversebytes.h"
       
    18 #include "FAXMDRV.H"
       
    19 #include "FAXMODEM.H"
       
    20 #include <et_phone.h>
       
    21 
       
    22 #include "FAXLOG.H"
       
    23 
       
    24 
       
    25 /*********************************************************************/
       
    26 
       
    27 CFaxSession *CFaxSession::NewLC ()
       
    28 	{
       
    29 	CFaxSession *self = new (ELeave) CFaxSession;
       
    30 	CleanupStack::PushL (self);
       
    31 	return self;
       
    32 	}
       
    33 
       
    34 CFaxSession *CFaxSession::NewL ()
       
    35 	{
       
    36 	CFaxSession *self = NewLC ();
       
    37 	CleanupStack::Pop ();
       
    38 	return self;
       
    39 	}
       
    40 /********************************************************************/
       
    41 CFaxSession::CFaxSession(void)
       
    42             :CBase()
       
    43 {
       
    44 }
       
    45 
       
    46 CFaxSession::~CFaxSession ()
       
    47 	{
       
    48 	FxClose ();
       
    49 	delete iSharedFileHandles;
       
    50 	}
       
    51 
       
    52 void CFaxSession::SetCallBack(MFaxCompletionBase* aCompletionBase)
       
    53 	{
       
    54 	iCompletionBase = aCompletionBase;
       
    55 	}
       
    56 
       
    57 /********************************************************************/
       
    58 
       
    59 // this kicks off the send or receive session by launching a separate high
       
    60  // priority faxthread.  A FxOpen must be paired with a call to FxClose
       
    61  // as this is an EPOC32 requirement
       
    62  //
       
    63  // we are part of c32 here, so we cannot set a thread priority
       
    64  //
       
    65  // the heap and stack sizes set here (4K each) are pure guesswork
       
    66  // we need a handle to this parent thread so that our child thread
       
    67  // is able to signal back to us via a TRequestStatus
       
    68 
       
    69 TInt CFaxSession::FxOpen (TFaxServerSessionSettings & aSettings,RFax::TProgress* aProgress)
       
    70 {
       
    71 	
       
    72     __FLOG_FAXSRV( _L8("CFaxSession::FxOpen entering"));
       
    73 
       
    74 	ASSERT (iFaxRequest == NULL);
       
    75 	iFaxServerSessionSettings = aSettings;
       
    76 
       
    77 // Initialise the Progress Settings
       
    78 	iProgress = aProgress;
       
    79 	iProgress->iLastUpdateTime=0;
       
    80 	iProgress->iAnswerback.Zero();
       
    81 	iProgress->iPhase = ENotYetStarted;
       
    82 	iProgress->iSpeed = 9600;
       
    83 	iProgress->iResolution = EFaxNormal;
       
    84 	iProgress->iCompression = EModifiedHuffman;
       
    85 	iProgress->iECM = 0;
       
    86  	iProgress->iPage = 0;
       
    87 	iProgress->iLines = 0;
       
    88 
       
    89 	TRAPD (state, iFaxRequest = CFaxRequest::NewL (this));
       
    90 	if (state == KErrNone)
       
    91 		{
       
    92 		iFaxRequest->iChildThread.Logon (iChildDeath);
       
    93 		iFaxRequest->iChildThread.Resume ();
       
    94 		User::WaitForRequest (iFaxRequest->iThreadStat);
       
    95 		CActiveScheduler::Add (iFaxRequest);
       
    96 		}
       
    97 	return (state);
       
    98 }
       
    99 /********************************************************************/
       
   100 
       
   101 TInt CFaxSession::FxClose ()
       
   102 {
       
   103     __FLOG_FAXSRV( _L8("CFaxSession::FxClose entering"));
       
   104 
       
   105 	if (iFaxRequest)
       
   106 		{
       
   107 		if (iFaxRequest->IsActive ())
       
   108 			{
       
   109 			Cancel ();
       
   110 			}
       
   111 		iFaxRequest->FaxRequest (EFxClose);
       
   112 		User::WaitForRequest (iChildDeath);
       
   113 		delete iFaxRequest;
       
   114 		iFaxRequest = NULL;
       
   115 		}
       
   116 	if (!iAmDestructing)
       
   117 		{
       
   118 		iAmDestructing = ETrue;
       
   119 		delete this;
       
   120 		}
       
   121 	return (KErrNone);
       
   122 }
       
   123 /********************************************************************/
       
   124 
       
   125 MFaxCompletionBase* CFaxSession::ReturnCompletionBase ()
       
   126 {
       
   127 	return (iCompletionBase);
       
   128 }
       
   129 
       
   130 /*******************************************************************/
       
   131 
       
   132 void CFaxSession::RxConnect ()
       
   133 {
       
   134 	iFaxRequest->FaxRequest (ERxConnect);
       
   135 }
       
   136 /********************************************************************/
       
   137 
       
   138 void CFaxSession::RxFaxData (TDes8 & aData)
       
   139 {
       
   140 	iRxData = &aData;
       
   141 	iFaxRequest->FaxRequest (ERxFaxData);
       
   142 }
       
   143 /********************************************************************/
       
   144 
       
   145 void CFaxSession::RxPostPage ()
       
   146 {
       
   147 	iFaxRequest->FaxRequest (ERxPostPage);
       
   148 }
       
   149 /********************************************************************/
       
   150 
       
   151 void CFaxSession::TxConnect ()
       
   152 {
       
   153 	iFaxRequest->FaxRequest (ETxConnect);
       
   154 }
       
   155 /********************************************************************/
       
   156 
       
   157 void CFaxSession::TxFaxData (const TDesC8 & aData)
       
   158 {
       
   159 	iTxData = &aData;
       
   160 	iFaxRequest->FaxRequest (ETxFaxData);
       
   161 }
       
   162 /********************************************************************/
       
   163 
       
   164 void CFaxSession::TxPostPage ()
       
   165 {
       
   166 	iFaxRequest->FaxRequest (ETxPostPage);
       
   167 }
       
   168 
       
   169 void CFaxSession::Cancel ()
       
   170 {
       
   171 	iFaxRequest->Cancel ();
       
   172 }
       
   173 
       
   174 void CFaxSession::StartModemL ()
       
   175 {
       
   176     __FLOG_FAXSRV( _L8("CFaxSession::StartModemL entering"));
       
   177 
       
   178 	if (iModemDriver != NULL)
       
   179 		return;
       
   180 
       
   181 	if (iFaxServerSessionSettings.iFaxClass == EClass1)
       
   182 		iModemDriver = CFaxClass1::NewL (&iFaxServerSessionSettings,*iProgress);
       
   183 	else if (iFaxServerSessionSettings.iFaxClass == EClass2)
       
   184 		iModemDriver = CFaxClass2::NewL (&iFaxServerSessionSettings,*iProgress);
       
   185 	else if (iFaxServerSessionSettings.iFaxClass == EClass2point0)
       
   186 		iModemDriver = CFaxClass20::NewL (&iFaxServerSessionSettings,*iProgress);
       
   187 	else
       
   188 		User::Leave (KFaxCannotAutodetect);
       
   189 
       
   190 	iModemDriver->iFaxServerSessionSettings = &iFaxServerSessionSettings;
       
   191    iCompletionBase->GetCadenceAndTimeOfLastRing(iModemDriver->iCadence, iModemDriver->iTimeOfLastRing);
       
   192 }
       
   193 
       
   194 void CFaxSession::SetFaxHeaderFile(CFaxSharedFileHandles* aSharedFileHandles)
       
   195 	{
       
   196 	//if we already have an object then delete it and use this one instead
       
   197 	if(iSharedFileHandles)
       
   198 	{
       
   199 		delete iSharedFileHandles;
       
   200 		iSharedFileHandles=NULL;
       
   201 	}
       
   202 	//we are now owners of this object and are responsible for its deletion.
       
   203 	iSharedFileHandles = aSharedFileHandles;
       
   204 	}
       
   205 
       
   206 /********************************************************************/
       
   207 
       
   208 // the CFaxRequest class is our Fax Server Active Object
       
   209 
       
   210 /*********************************************************************/
       
   211 
       
   212 CFaxSession::CFaxRequest::CFaxRequest ()
       
   213 :CActive (1)
       
   214 {
       
   215 }
       
   216 /********************************************************************/
       
   217 
       
   218 CFaxSession::CFaxRequest *CFaxSession::CFaxRequest::NewLC (CFaxSession * aFaxSession)
       
   219 {
       
   220 	CFaxSession::CFaxRequest *self = new (ELeave) CFaxSession::CFaxRequest ();
       
   221 	CleanupStack::PushL (self);
       
   222 	self->ConstructL (aFaxSession);
       
   223 	return self;
       
   224 }
       
   225 /********************************************************************/
       
   226 
       
   227 CFaxSession::CFaxRequest *CFaxSession::CFaxRequest::NewL (CFaxSession * aFaxSession)
       
   228 {
       
   229 	CFaxSession::CFaxRequest *self = NewLC (aFaxSession);
       
   230 	CleanupStack::Pop ();
       
   231 	return self;
       
   232 }
       
   233 /********************************************************************/
       
   234 
       
   235 void CFaxSession::CFaxRequest::ConstructL (CFaxSession * aFaxSession)
       
   236 {
       
   237 	TInt stackSize = 0x1400;
       
   238 	_LIT(KFaxThread,"FaxServerThread");
       
   239 
       
   240 	iFaxSession = aFaxSession;
       
   241 	TInt res = iFaxSession->iParentThread.Duplicate (RThread ());
       
   242 	if (res == KErrNone)
       
   243 		res = iChildThread.Create (KFaxThread,
       
   244 			FaxServerThread,
       
   245 			stackSize,
       
   246 			NULL,
       
   247 			iFaxSession,
       
   248 			EOwnerProcess);
       
   249 	if (res)
       
   250 		User::Leave (KFaxThreadError);
       
   251 }
       
   252 /********************************************************************/
       
   253 
       
   254 CFaxSession::CFaxRequest::~CFaxRequest ()
       
   255 {
       
   256 	iFaxSession->iParentThread.Close ();
       
   257 	iChildThread.Close ();
       
   258 }
       
   259 /********************************************************************/
       
   260 
       
   261 // once we have our active object, we simply call its FaxRequest
       
   262  // it re-activates the faxserver thread to process the request
       
   263  // and sets the FaxRequest object active before returning
       
   264 
       
   265 void CFaxSession::CFaxRequest::FaxRequest (CFaxSession::TFaxThreadRequest aFaxThreadRequest)
       
   266 {
       
   267 	TRequestStatus *threadStatus = &iThreadStat;
       
   268 	iFaxThreadRequest = aFaxThreadRequest;
       
   269 	if (iFaxThreadRequest != EFxClose)
       
   270 		{
       
   271 		iStatus = KRequestPending;
       
   272 		SetActive ();
       
   273 		}
       
   274 	iChildThread.RequestComplete (threadStatus, aFaxThreadRequest);
       
   275 }
       
   276 /********************************************************************/
       
   277 
       
   278 // here we request a cancel of a fax call
       
   279 
       
   280 void CFaxSession::CFaxRequest::DoCancel ()
       
   281 {
       
   282 	iCancel = 1;
       
   283 	if (iFaxSession->iModemDriver)
       
   284 		{
       
   285 		if (iFaxSession->iModemDriver->iModem->iCancel == 0)
       
   286 			iFaxSession->iModemDriver->iModem->iCancel++;
       
   287 		}
       
   288 }
       
   289 /********************************************************************/
       
   290 
       
   291 void CFaxSession::CFaxRequest::RunL ()
       
   292 {
       
   293 	switch (iFaxThreadRequest)
       
   294 		{
       
   295 		case ERxConnect:
       
   296 				{
       
   297 			iFaxSession->ReturnCompletionBase()->RxConnectComplete (iStatus.Int ());
       
   298 			break;
       
   299 				}
       
   300 
       
   301 		case ERxFaxData:
       
   302 				{
       
   303 				iFaxSession->ReturnCompletionBase()->RxFaxDataComplete (iStatus.Int ());
       
   304 			break;
       
   305 				}
       
   306 
       
   307 		case ERxPostPage:
       
   308 				{
       
   309 				iFaxSession->ReturnCompletionBase()->RxPostPageComplete (iStatus.Int ());
       
   310 				break;
       
   311 				}
       
   312 
       
   313 		case ETxConnect:
       
   314 				{
       
   315 				iFaxSession->ReturnCompletionBase()->TxConnectComplete (iStatus.Int ());
       
   316 				break;
       
   317 				}
       
   318 
       
   319 		case ETxFaxData:
       
   320 				{
       
   321 				iFaxSession->ReturnCompletionBase()->TxFaxDataComplete (iStatus.Int ());
       
   322 				break;
       
   323 				}
       
   324 
       
   325 		case ETxPostPage:
       
   326 				{
       
   327 				iFaxSession->ReturnCompletionBase()->TxPostPageComplete (iStatus.Int ());
       
   328 				break;
       
   329 				}
       
   330 		default:;
       
   331 		}
       
   332 }
       
   333 /********************************************************************/
       
   334 // this is a utility function which is the entry to our thread
       
   335  // it isn't part of any class, but we pass the address
       
   336  // of our CFaxModemDriver in so that we can check the
       
   337  // session parameter and get back to the required function
       
   338  //
       
   339  // it also has the thread service request dispatcher
       
   340 
       
   341 TInt FaxServerThread (TAny * session)
       
   342 	{
       
   343 	TInt state=0;
       
   344 	TInt ret;
       
   345 	TBool terminateThread=EFalse;
       
   346 	CTrapCleanup *cleanup = CTrapCleanup::New ();
       
   347 	CFaxSession *faxsession = (CFaxSession *) session;
       
   348 	TRequestStatus *openStatus = &faxsession->iFaxRequest->iThreadStat;
       
   349 	faxsession->iParentThread.RequestComplete (openStatus, state);
       
   350 
       
   351 	FOREVER
       
   352 		{
       
   353 		User::WaitForRequest (faxsession->iFaxRequest->iThreadStat);
       
   354 		state = ret = KErrNone;
       
   355 		
       
   356         __FLOG_FAXSRV1( _L8("FaxServerThread: iThreadStat=%d"), faxsession->iFaxRequest->iThreadStat.Int ());
       
   357 		
       
   358 		switch (faxsession->iFaxRequest->iThreadStat.Int ())
       
   359 			{
       
   360 			case CFaxSession::ERxConnect:
       
   361 				TRAP (state, faxsession->StartModemL ());
       
   362 							
       
   363 				if (state == KErrNone)
       
   364 					{
       
   365 					faxsession->iModemDriver->iModem->iCancel = faxsession->iFaxRequest->iCancel;
       
   366 					TRAP (state, ret = faxsession->iModemDriver->RxConnectL ());
       
   367                     __FLOG_FAXSRV2(_L8("FaxServerThread state: ERxConnect returned ret=%d, state=%d"), ret, state);
       
   368 					}
       
   369 				break;
       
   370 			
       
   371 			case CFaxSession::ERxFaxData:
       
   372 				if(faxsession->iModemDriver)
       
   373 					{
       
   374 					TRAP (state, faxsession->iModemDriver->GetFaxDataL (faxsession->iRxData));
       
   375 					__FLOG_FAXSRV2( _L8("FaxServerThread state: ERxFaxData returned ret=%d, state=%d"), ret, state);
       
   376 					}
       
   377 				else
       
   378 					__FLOG_FAXSRV( _L8("FaxServerThread state: ERxFaxData - faxsession->iModemDriver=NULL"));
       
   379 				break;
       
   380 			
       
   381 			case CFaxSession::ERxPostPage:
       
   382 				if(faxsession->iModemDriver)
       
   383 					{			
       
   384 					TRAP (state, ret = faxsession->iModemDriver->RxPostPageL ());
       
   385 					__FLOG_FAXSRV2( _L8("FaxServerThread: iModemDriver->RxPostPageL returned ret=%d, state=%d"), ret, state);
       
   386 					}
       
   387 				else
       
   388 					__FLOG_FAXSRV( _L8("FaxServerThread: iModemDriver->RxPostPageL - faxsession->iModemDriver = NULL"));
       
   389 				break;
       
   390 			
       
   391 			case CFaxSession::ETxConnect:
       
   392 				__FLOG_FAXSRV(_L8("FaxServerThread state: ETxConnect"));
       
   393 				TRAP (state, faxsession->StartModemL ());		//creates an instance of the appropriate 
       
   394 																//modem driver (CFax1, CFax2, CFax2.0)
       
   395 				if (state == KErrNone)
       
   396 					{
       
   397 					faxsession->iModemDriver->iModem->iCancel = faxsession->iFaxRequest->iCancel; // added now
       
   398 					faxsession->iModemDriver->SetSharedFileHandles(faxsession->iSharedFileHandles);
       
   399 					TRAP (state, ret = faxsession->iModemDriver->TxConnectL ());
       
   400 					}
       
   401 				break;
       
   402 			
       
   403 			case CFaxSession::ETxFaxData:
       
   404 				if(faxsession->iModemDriver)
       
   405 					{			
       
   406 					TRAP (state, ret = faxsession->iModemDriver->SendFaxDataL (faxsession->iTxData));
       
   407 					__FLOG_FAXSRV2(_L8("FaxServerThread state: ETxFaxData, state =%d, ret=%d"),state, ret);
       
   408 					}
       
   409 				else
       
   410 					__FLOG_FAXSRV(_L8("FaxServerThread state: ETxFaxData, faxsession->iModemDriver = NULL"));
       
   411 				break;
       
   412 			
       
   413 			case CFaxSession::ETxPostPage:
       
   414                 __FLOG_FAXSRV(_L8("FaxServerThread state: ETxPostPage"));
       
   415 				if(faxsession->iModemDriver)
       
   416 					{			
       
   417 					TRAP (state, ret = faxsession->iModemDriver->TxPostPageL ());
       
   418 					}
       
   419 				else
       
   420 					{
       
   421 					__FLOG_FAXSRV(_L8("FaxServerThread state: ETxPostPage, faxsession->iModemDriver = NULL"));
       
   422 					}
       
   423 				break;
       
   424 			
       
   425 			case CFaxSession::EFxClose:
       
   426                 __FLOG_FAXSRV(_L8("FaxServerThread state: EFxClose:"));
       
   427 
       
   428 				delete faxsession->iModemDriver;
       
   429 				faxsession->iModemDriver = NULL;
       
   430 				terminateThread=ETrue;
       
   431 				state = KErrNone;
       
   432 				ret   = KErrNone;
       
   433 				break;
       
   434 			
       
   435 			default:
       
   436 				state = KErrNone;
       
   437 				ret   = KErrNone;
       
   438                 break;
       
   439 			}
       
   440 		if (state == KErrNone)
       
   441 			state = ret;
       
   442 		if (faxsession->iFaxRequest->iCancel)
       
   443 			{
       
   444 			
       
   445 			__FLOG_FAXSRV1(_L8("FaxServerThread: iCancel=%d"), faxsession->iFaxRequest->iCancel);
       
   446 			
       
   447 			state = KFaxCancelRequested;
       
   448 			delete faxsession->iModemDriver;
       
   449 			faxsession->iModemDriver = NULL;
       
   450 			}
       
   451 		if (faxsession->iFaxRequest->IsActive ())
       
   452 			{
       
   453 			TRequestStatus *returnStatus = &faxsession->iFaxRequest->iStatus;
       
   454 			faxsession->iParentThread.RequestComplete (returnStatus, state);
       
   455 			}
       
   456 		if(terminateThread)
       
   457 			break;
       
   458 //		if (faxsession->iModemDriver == NULL)
       
   459 //			break;
       
   460 		}
       
   461 	delete cleanup;
       
   462 	return (state);
       
   463 }
       
   464 /*********************************************************************/
       
   465 
       
   466 TFaxServerSessionSettings& TFaxServerSessionSettings::operator=(const TFaxServerSessionSettings& aSettings)
       
   467 	{
       
   468 	iPhoneNumber = aSettings.iPhoneNumber;
       
   469 	iLogging = aSettings.iLogging;
       
   470 	iFaxInitString = aSettings.iFaxInitString;
       
   471 	iMode = aSettings.iMode;;
       
   472 	iFaxClass = aSettings.iFaxClass;
       
   473 	iPortDriverName = aSettings.iPortDriverName;
       
   474 	iCommPortName = aSettings.iCommPortName;
       
   475 	iFaxId = aSettings.iFaxId;
       
   476 	iMaxSpeed = aSettings.iMaxSpeed;
       
   477 	iMinSpeed = aSettings.iMinSpeed;
       
   478 	iPreferredECM = aSettings.iPreferredECM;
       
   479 	iFaxOnDemandDelay = aSettings.iFaxOnDemandDelay;
       
   480 	iTxResolution = aSettings.iTxResolution;
       
   481 	iTxCompression = aSettings.iTxCompression;
       
   482 	iTxPages = aSettings.iTxPages;
       
   483 	iRxResolution = aSettings.iRxResolution;
       
   484 	iRxCompression = aSettings.iRxCompression;
       
   485 	return(*this);
       
   486 	}
       
   487  
       
   488 //
       
   489 // First Ordinal Functions
       
   490 //
       
   491 extern "C"
       
   492 	{
       
   493 	IMPORT_C CFaxSession* LibEntry(void);	// Force "Proper Name" export
       
   494 	}
       
   495 
       
   496 EXPORT_C CFaxSession* LibEntry()
       
   497 	{
       
   498 	return CFaxSession::NewL();
       
   499 	}
       
   500