//	COMEXE.H
//
// Copyright (c) 1996-1999 Symbian Ltd.  All rights reserved.
//
//	Comms statement executor
//

#ifndef __COMEXE_H__
#define __COMEXE_H__

#include <e32base.h>
#include <f32file.h>
#include <c32comm.h>
#include <badesca.h>
#include <comftp.h>
#include <s32file.h>
#include <s32crypt.h>

enum TCommsFileHandleState
	{
	ECommsHandleStateAny,
	ECommsHandleStateOpen,
	ECommsHandleStateFree
	};
enum TCommsFileOpenMode
	{
	ECommsFileInput,
	ECommsFileOutput,
	ECommsFileAppend,
	};
enum TCommsEmulatorState
	{
	ECommsEmulatorStart,
	ECommsEmulatorPause,
	ECommsEmulatorContinue,
	};
enum TCommsExePanic
	{
	ECommsExeArguments,
	ECommsExeLineTooLong,
	ECommsPasswordTooLong,
	};
enum TCommsVariableType
    {
    ECommsSystemVariableLocalEcho,
    ECommsSystemVariableIncomingLineFeeds,
    ECommsSystemVariableOutgoingLineFeeds,
    ECommsNormalVariable,
    };

const TInt KCommsHandleMaxNumber=9;
const TInt KCommsTextLineMaxLength=0x100;
const TInt KCommsVariablePersistentChar='$';
const TInt KCommsVarListGranularity=4;
const TInt KCommsMaxVariableNameLength=32;
const TInt KCommsMaxVariableValueLength=128;
const TInt KCommsMaxTranslateLength=10;
const TInt KCommReceiveMaxMatchLength=64;
const TInt KCommCaptureAppendModeFlag=1;
const TInt KCommCaptureDebugModeFlag=2;
const TInt KCommReceiveMaxIncomingLength=64;
const TInt KCommContinuousReceivingTimeoutMicroSeconds=500000;
const TInt KCommPortNameMaxLength=16;
//

class CTimeOuter : public CTimer
	{
public:
	static CTimeOuter* NewL(TInt aPriority);
	void TimeOut(const TCallBack& aCallBack,TTimeIntervalMicroSeconds32 aTime);
private:
	CTimeOuter(TInt aPriority);
	void RunL();
private:
	TCallBack iCallBack;
	};

class CVariableList : public CBase
	{
public:
	IMPORT_C CVariableList();
	IMPORT_C ~CVariableList();
	IMPORT_C void ConstructL();
	IMPORT_C TInt Count() const;
	IMPORT_C void DeleteAll();
	IMPORT_C void Delete(TInt aIndex);
	IMPORT_C void DeleteAllNonPersistent();
	IMPORT_C TPtrC Name(TInt aIndex) const;
	IMPORT_C TPtrC Value(TInt aIndex) const;
	IMPORT_C TBool IsPersistent(TInt aIndex) const;
	IMPORT_C void SetNameL(TInt aIndex,const TDesC& aName);
	IMPORT_C void SetValueL(TInt aIndex,const TDesC& aValue);
	IMPORT_C void SetL(const TDesC& aName,const TDesC& aValue,TBool aSort=ETrue,TBool aAllowDuplicates=EFalse);
	IMPORT_C TInt FindName(TInt& aIndex,const TDesC& aName) const;
	IMPORT_C CDesCArray* NameList();
	IMPORT_C void InternalizeL(RReadStream& aReadStream);
	IMPORT_C void ExternalizeL(RWriteStream& aWriteStream) const;
private:
	TPtrC Name(const TDesC& aBuffer) const;
	TPtrC Value(const TDesC& aBuffer) const;
	TBool IsPersistentName(const TDesC& aName) const;
private:
	CDesCArray* iNames;
	CDesCArray* iValue;
	};

class CTransferTracker;

class CCommsUserInterface : public CBase
	{
public:
	IMPORT_C CCommsUserInterface();
	IMPORT_C ~CCommsUserInterface();
	IMPORT_C virtual CTransferTracker* NewFileTransferTrackerL() const;
	IMPORT_C virtual TBool RunVerifyPasswordDialogL(const CSecurityBase& aSecurity,TPassword& aCorrectPassword);
	IMPORT_C virtual TInt ChooseOptionL(TInt& aResultIndex,const CPtrCArray& aOptions,const TDesC* aTitle);
	IMPORT_C virtual TBool UpdateVariablesL(CVariableList& aVarList,const CPtrCArray& aPrompts,const TDesC* aTitle);
	IMPORT_C virtual void AlertMessageL(const TDesC& aLineOne,const TDesC& aLineTwo);
	IMPORT_C virtual void SetStatusMessageL(const TDesC& aMessage);
	};

class CCommReceiver;
class CCommTransmitter;
class CEmulator;
class CTransferTracker;
class CFileTransfer;
class TFileTransferCapabilities;

class CCommsExecutor : public CBase
	{
public:
	IMPORT_C static CCommsExecutor* NewL();
	IMPORT_C ~CCommsExecutor();
public:
	IMPORT_C TInt OpenPort(const TDesC& aCommPort,TCommAccess aAccess=ECommShared);
    IMPORT_C TInt OpenPort(const TDesC& aCommModule,const TDesC& aCommPort,TCommAccess aAccess=ECommShared);
    IMPORT_C void ClosePort();
	IMPORT_C TInt SetConfig(const TCommConfig& aConfig);
	IMPORT_C void Config(TCommConfig& aConfig);
	IMPORT_C void CancelCommsDevice();
	IMPORT_C TInt CommsDeviceStatus(TBool aReceive) const;
	IMPORT_C TInt MatchResultStatus() const;
	//
	IMPORT_C CEmulator* Emulator() const;
	IMPORT_C void SetEmulator(CEmulator* aEmul);
	IMPORT_C void SetScriptUiL(CCommsUserInterface* aUI);
	//
	IMPORT_C TBool HandleKeyPress(TInt aKeyCode);	// returns ETrue if keypress was handled
	IMPORT_C TInt SendToRemote(const TDesC& aBuffer);
	IMPORT_C TBool IsTerminalReceiving() const;
	IMPORT_C void StartTerminalReceiving();
	IMPORT_C void PauseTerminalReceiving();
	IMPORT_C void ContinueTerminalReceiving();
	//
	inline TBool LocalEcho() const;
    inline TBool OutgoingLineFeeds() const;
    inline TBool IncomingLineFeeds() const;
    inline void SetOutgoingLineFeeds(TBool aOutgoingLineFeeds);
    inline void SetIncomingLineFeeds(TBool aIncomingLineFeeds);
	IMPORT_C TPtrC ConnectString() const;
	inline void SetLocalEcho(TBool aLocalEchoWantedWhenSent);
	inline TInt PortStatus() const;
    inline RFs& FileServer();
    inline const RFs& FileServer() const;
    inline TPtrC CommPort() const;
    inline TPtrC CommModule() const;
	//
	IMPORT_C TBool AskPasswordL(CSecurityBase& aSecurity,TPassword& aCorrectPassword);
	IMPORT_C const TPassword& Password() const;
	IMPORT_C void RemoveSecurity();
	IMPORT_C CSecurityBase& SecurityL();
	IMPORT_C CSecurityBase& SecurityL(const TDesC8& aSecurity);
	//
	IMPORT_C void Hangup();
	IMPORT_C void Reset();
	IMPORT_C void Drain();
	//
	IMPORT_C void UpdatePersistentVariablesL(const CVariableList* aVarlist);
	IMPORT_C void CopyPersistentVariablesL(CVariableList* aVarlist);
	IMPORT_C void SaveNamesL();
	IMPORT_C void LoadNamesL(const TDesC& aFilename);
	IMPORT_C TPtrC PersistentVariableFilename() const;
	IMPORT_C void SetPersistentVariableFilename(const TDesC& aFilename);
	IMPORT_C TInt VariableCount();
    IMPORT_C void VariableValueByName(const TDesC& aName,TDes& aValue);
    IMPORT_C void SetVariableL(const TDesC& aName,const TDesC& aValue);
	IMPORT_C void SetPersistentVariableL(const TDesC& aName,const TDesC& aValue);
	//
	IMPORT_C void DecodeString(TDes& aString);
	//
	IMPORT_C void CloseDir();
	IMPORT_C void OpenDir(const TDesC& aFileSpec);
	IMPORT_C TInt NextDirEntry(TFileName& aResult);
	IMPORT_C TBool FileExistsL(const TDesC& aFileName);
	//
	IMPORT_C void ClearScreen();
	IMPORT_C void Beep(TInt aPitch,TTimeIntervalMicroSeconds32 aDuration);
	//
	IMPORT_C void DeleteFileL(const TDesC& aFileName);
	IMPORT_C void CopyFile(const TDesC& aSrcFileName,const TDesC& aDstFileName);
	IMPORT_C void MoveFile(const TDesC& aSrcFileName,const TDesC& aDstFileName);
	IMPORT_C void RenameFileL(const TDesC& aSrcFileName,const TDesC& aDstFileName);
	//
	IMPORT_C void CloseHandlesL();
	IMPORT_C void CloseHandleL(TInt aHandle);
	IMPORT_C TBool IsHandleAtEndOfFileL(TInt aHandle);
	IMPORT_C void ReadLineHandleL(TInt aHandle,TDes8& aDes);
	IMPORT_C void WriteHandleL(TInt aHandle,const TDesC8& aString);
	IMPORT_C void ReadHandleL(TInt aHandle,TDes8& aDes,TInt aMaxLength);
	IMPORT_C void VerifyHandleStateL(TInt aHandle,TCommsFileHandleState aMode);
	IMPORT_C void OpenHandleL(TInt aHandle,const TDesC& aFileName,TCommsFileOpenMode aMode);
	//
    IMPORT_C TPtrC MatchBuffer() const;
	IMPORT_C void Break(TTimeIntervalMicroSeconds32 aTimeout);
	IMPORT_C void Connect(const TCallBack& aCallback,TTimeIntervalMicroSeconds32 aTimeout);
	IMPORT_C void Send(const TCallBack& aCallback,TTimeIntervalMicroSeconds32 aTimeout,const TDesC8& aText);
	//
	IMPORT_C void ClearWaitTextArray();
	IMPORT_C TInt IndexWaitTextFound() const;
	IMPORT_C void WaitReplyL(const TCallBack& aCallback,TTimeIntervalMicroSeconds32 aTimeout,const TDesC& aTextToMatch);
	IMPORT_C void WaitReply(const TCallBack& aCallback,TTimeIntervalMicroSeconds32 aTimeout,const CPtrCArray& aTextToMatch);
	//
	IMPORT_C void StatusMessageL(const TDesC& aMessage);
	IMPORT_C void TerminalMessageL(const TDesC& aMessage);
	IMPORT_C void AlertMessageL(const TDesC& aLineOne,const TDesC& aLineTwo);
	//
	IMPORT_C void DeleteAllVariables();
	IMPORT_C void DeleteVariable(const TDesC& aName);
	IMPORT_C TBool ChooseOptionL(TInt& aChoice,const TDesC& aTitle,const CPtrCArray& aOption);
	IMPORT_C TBool EditVariablesL(const TDesC& aTitle,const CPtrCArray& aPrompts,CVariableList& aVarlist);
	//
	IMPORT_C TPtrC ReceiveIntoDirectory() const;
	IMPORT_C TPtrC TransmitFromDirectory() const;
	IMPORT_C void SetReceiveIntoDirectoryL(const TDesC& anUploadDirectory);
	IMPORT_C void SetTransmitFromDirectoryL(const TDesC& aDownloadDirectory);
	IMPORT_C void ReceiveFilesL(const TCallBack& aCallback,const TDesC* aFilename);
	IMPORT_C void TransmitFilesL(const TCallBack& aCallback,const CDesCArray& aFileList);
	//
	IMPORT_C TPtrC FileTransferName() const;
	IMPORT_C TInt FileTransferStatus() const;
	IMPORT_C void SetFileTransferProtocol(const TDesC& aName);
	IMPORT_C TFileTransferCapabilities FileTransferCapabilitiesL() const;
	IMPORT_C TFileTransferCapabilities FileTransferCapabilitiesL(const TDesC& aName);
	IMPORT_C void PopulateProtocolNameList(CProtocolNamesAndCaps& ProtocolNameArrayPtr);
	IMPORT_C void CreateProtocolByNameL(TFileTransferPtr& aTransfer,const TDesC& aName);
//
	IMPORT_C TBool IsCaptureOn() const;
	IMPORT_C TInt CaptureOn(const TDesC& aCaptureFileName,TBool aAppend,TBool aDebugMode);
	IMPORT_C void CaptureOff();
	IMPORT_C void CatchL(const TCallBack& aCallback,TTimeIntervalMicroSeconds32 aTimeout,const TDesC& aTerminator,TInt aMaxLength);
    IMPORT_C TVersion Version() const;
//
    inline void SetNamesUid3Value(TInt aUid) { iNamesUid3Value=aUid; }
protected:
	CCommsExecutor();
	void ConstructL();
	void Panic(TInt aPanic) const;
private:
	void ClearCircularMatchBuffer();
	static TInt CallbackSendToRemote(TAny *aObj);
	void DoCompletedSendToRemote();
	void StartMatching(const TCallBack& aMatchCallBack,TTimeIntervalMicroSeconds32 aTimeOut);
	void StopMatching(TInt aStatus);
	static TInt MatchTimeOut(TAny *anObj);
	static TInt ContinualReceiveCallBack(TAny *anObj);
	TInt DoReceivedCallbackResponse();
	inline TBool BusyCatching() const;
	inline TInt MaxCatchLength() const;
	//
	inline CVariableList& Variables() const;
	TPtrC SystemVariable(TCommsVariableType aType) const;
	TCommAccess iAccess;
	TCommsVariableType VariableType(const TDesC& aName) const;
	//
	void DoHandleMatchingForReceived(TInt /*aStatus*/,const TDesC& /*aText*/);
	void DoHandleCaptureForReceived(TInt /*aStatus*/,const TDesC& /*aText*/);
    //
    TPtrC TrueDes() const;
    TPtrC FalseDes() const;
private:
    RFs iFileServer;
	RCommServ iCommServer;
	RComm iRComm;
	RFile iCaptureFile;
	TPassword iPassword;
	CSecurityBase* iSecurity;
    CEmulator* iEmulator;
	CFileMan* iFileManager;
	CDesCArray* iUploadNames;
	CVariableList* iVariableList;
	CCommReceiver* iReceiver;
	CCommTransmitter* iTransmitter;
	CPtrCArray* iReceiveMatches;
	TFileTransferPtr iFileTransferProtocol;
	CTransferTracker* iTransferTracker;
	CCommsUserInterface* iUserInterface;
	RDir iDir;
	TBool iDirOpen;
	TFileName iNamesFilename;
	HBufC* iUploadDirectory;
	HBufC* iDownloadDirectory;
	HBufC* iSendBuffer; // copy of the buffer being sent
	HBufC* iConnectBuffer;
	TBool iLocalEchoWantedWhenSent;
	TBool iIncomingLineFeeds;
    TBool iOutgoingLineFeeds;
	TInt iPortStatus;
	TCommConfig iResetConfig;
	CArrayFix<RFile*>* iFileHandle;
	TProtocolManager iProtocolManager;
	TBuf8<KCommsTextLineMaxLength> iArgumentScratchBuffer;
	TBool iCapturingDebugmode;
	TBool iBusyCapturing;
	TBool iBusyMatching;
	TInt iMaxCatchLength; // non-zero shows that catching is in progress
    TInt iNamesUid3Value;
	TBool iMatchFound;
    TInt iMatchStatus;
	CTimeOuter* iMatchTimer;
	TCallBack iMatchCallBack;
	TBuf8<KCommReceiveMaxIncomingLength> iIncoming;
	TBuf<KCommPortNameMaxLength> iCommPort;
	TBuf<KCommPortNameMaxLength> iCommModule;
//
// Circular match buffer...
//
	TBuf<KCommReceiveMaxMatchLength> iCircularMatchBuffer;
	TInt iCircularMatchBufferPos;
	};

//	class CCommsExecutor
inline TPtrC CCommsExecutor::CommPort() const
    {
    return(iCommPort);
    }

inline TPtrC CCommsExecutor::CommModule() const
    {
    return(iCommModule);
    }

inline RFs& CCommsExecutor::FileServer()
    {
    return(iFileServer);
    }
inline const RFs& CCommsExecutor::FileServer() const
    {
    return(iFileServer);
    }
inline void CCommsExecutor::SetLocalEcho(TBool aLocalEchoWantedWhenSent)
	{
	iLocalEchoWantedWhenSent=aLocalEchoWantedWhenSent;
	}
inline TBool CCommsExecutor::LocalEcho() const
	{
	return (iLocalEchoWantedWhenSent);
	}
inline TBool CCommsExecutor::IncomingLineFeeds() const
    {
    return (iIncomingLineFeeds);
    }
inline TBool CCommsExecutor::OutgoingLineFeeds() const
    {
    return (iOutgoingLineFeeds);
    }
inline void CCommsExecutor::SetIncomingLineFeeds(TBool aIncomingLineFeeds)
    {
    iIncomingLineFeeds=aIncomingLineFeeds;
    }
inline void CCommsExecutor::SetOutgoingLineFeeds(TBool aOutgoingLineFeeds)
    {
    iOutgoingLineFeeds=aOutgoingLineFeeds;
    }
inline TInt CCommsExecutor::PortStatus() const
	{
	return iPortStatus;
	}
inline TInt CCommsExecutor::MaxCatchLength() const
	{
	return(iMaxCatchLength);
	}
inline TBool CCommsExecutor::BusyCatching() const
	{
	return(MaxCatchLength()!=0);
	}
inline CVariableList& CCommsExecutor::Variables() const
	{
	return(*iVariableList);
	}

class CCommsDevices	: public CBase
	{
public:
	IMPORT_C CCommsDevices();
	IMPORT_C ~CCommsDevices();
	IMPORT_C void ConstructL();
	IMPORT_C TInt GetDeviceIndex(TDesC& aModule,TDesC& aPort);
	IMPORT_C void CommModule(TInt aIndex,TDes& aModule);
	IMPORT_C void CommPort(TInt aIndex,TDes& aPort);
	IMPORT_C TBool SupportsSetup(TInt aIndex);
	IMPORT_C TInt Count() const;
private:
	void AppendDeviceL(const TDesC& aModule,const TDesC& aPort);
	void Panic(TInt aPanic) const;
	void DoAddDeviceNamesL(CDir* aDir);
	CDesCArray* iModuleNames; // list of modules EUCART etc
	CDesCArray* iPortNames;   // list of ports COMM::0 etc
	};

#endif

