commands/rcomm/rcomm.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // rcomm.cpp
       
     2 // 
       
     3 // Copyright (c) 2007 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 #include <c32comm.h>
       
    15 
       
    16 using namespace IoUtils;
       
    17 
       
    18 class CCmdRcomm : public CCommandBase
       
    19 	{
       
    20 public:
       
    21 	static CCommandBase* NewLC();
       
    22 	~CCmdRcomm();
       
    23 private:
       
    24 	CCmdRcomm();
       
    25 	void ReadL();
       
    26 	void WriteL();
       
    27 private: // From CCommandBase.
       
    28 	virtual const TDesC& Name() const;
       
    29 	virtual void DoRunL();
       
    30 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    31 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    32 private:
       
    33 	HBufC* iCsyName;
       
    34 	HBufC* iPortName;
       
    35 	HBufC* iMode;
       
    36 	TCommAccess iCommAccess;
       
    37 	TUint iBufSize;
       
    38 	TBool iRaw;
       
    39 	TBool iVerbose;
       
    40 	RCommServ iCommServ;
       
    41 	RComm iComm;
       
    42 	};
       
    43 
       
    44 
       
    45 CCommandBase* CCmdRcomm::NewLC()
       
    46 	{
       
    47 	CCmdRcomm* self = new(ELeave) CCmdRcomm();
       
    48 	CleanupStack::PushL(self);
       
    49 	self->BaseConstructL();
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 CCmdRcomm::~CCmdRcomm()
       
    54 	{
       
    55 	delete iCsyName;
       
    56 	delete iPortName;
       
    57 	delete iMode;
       
    58 	iComm.Close();
       
    59 	iCommServ.Close();
       
    60 	}
       
    61 
       
    62 CCmdRcomm::CCmdRcomm()
       
    63 	: iCommAccess(ECommShared), iBufSize(1024)
       
    64 	{
       
    65 	}
       
    66 
       
    67 const TDesC& CCmdRcomm::Name() const
       
    68 	{
       
    69 	_LIT(KName, "rcomm");	
       
    70 	return KName;
       
    71 	}
       
    72 
       
    73 void CCmdRcomm::DoRunL()
       
    74 	{
       
    75 	User::LeaveIfError(iCommServ.Connect());
       
    76 
       
    77 	if (iCsyName)
       
    78 		{
       
    79 		TInt err = iCommServ.LoadCommModule(*iCsyName);
       
    80 		if (err && (err != KErrAlreadyExists))
       
    81 			{
       
    82 			PrintError(err, _L("Unable to load comm module \'%S\'"), iCsyName);
       
    83 			User::Leave(err);
       
    84 			}
       
    85 		}
       
    86 	else
       
    87 		{
       
    88 		TInt numPorts;
       
    89 		User::LeaveIfError(iCommServ.NumPorts(numPorts));
       
    90 		for (TInt i = 0; i < numPorts; ++i)
       
    91 			{
       
    92 			TName moduleName;
       
    93 			TSerialInfo serialInfo;
       
    94 			TInt err = iCommServ.GetPortInfo(i, moduleName, serialInfo);
       
    95 			if (err == KErrNone)
       
    96 				{
       
    97 				Printf(_L("%S:\r\n\tPort name: \'%S\'\r\n\tLow unit: %d\r\n\tHigh unit: %d\r\n\tDescription: %S\r\n\r\n"), &moduleName, &serialInfo.iName, serialInfo.iLowUnit, serialInfo.iHighUnit, &serialInfo.iDescription);
       
    98 				}
       
    99 			else
       
   100 				{
       
   101 				PrintWarning(_L("Couldn't get info for port %d, err=%d"), i, err);
       
   102 				}
       
   103 			}
       
   104 		return;
       
   105 		}
       
   106 
       
   107 	if (iPortName)
       
   108 		{
       
   109 		TInt err = iComm.Open(iCommServ, *iPortName, iCommAccess);
       
   110 		LeaveIfErr(err, _L("Unable to open port \'%S\'"), iPortName);
       
   111 		}
       
   112 	else
       
   113 		{
       
   114 		TSerialInfo serialInfo;
       
   115 		_LIT(KCsy, ".csy");
       
   116 		TPtrC csyName(*iCsyName);
       
   117 		if (csyName.Right(KCsy().Length()).CompareF(KCsy) == 0)
       
   118 			{
       
   119 			// Remove the trailing '.csy' because otherwise C32 won't find the module.
       
   120 			csyName.Set(csyName.Left(csyName.Length() - KCsy().Length()));
       
   121 			}
       
   122 		TInt err = iCommServ.GetPortInfo(csyName, serialInfo);
       
   123 		LeaveIfErr(err, _L("Couldn't get port info for CSY '%S'"), &csyName);
       
   124 		Printf(_L("Port name: \'%S\'\r\nLow unit: %d\r\nHigh unit: %d\r\nDescription: %S\r\n"), &serialInfo.iName, serialInfo.iLowUnit, serialInfo.iHighUnit, &serialInfo.iDescription);
       
   125 		return;
       
   126 		}
       
   127 
       
   128 
       
   129 	_LIT(KModeRead, "read");
       
   130 	_LIT(KModeWrite, "write");
       
   131 	if (iMode)
       
   132 		{
       
   133 		if (*iMode == KModeRead)
       
   134 			{
       
   135 			ReadL();
       
   136 			}
       
   137 		else if (*iMode == KModeWrite)
       
   138 			{
       
   139 			WriteL();
       
   140 			}
       
   141 		else
       
   142 			{
       
   143 			PrintError(KErrArgument, _L("Invalid mode: \'%S\'"), iMode);
       
   144 			}
       
   145 		}
       
   146 	}
       
   147 
       
   148 void CCmdRcomm::ReadL()
       
   149 	{
       
   150 	if (iRaw)
       
   151 		{
       
   152 		HBufC8* readBuf = HBufC8::NewLC(iBufSize);
       
   153 		TPtr8 readBufPtr(readBuf->Des());
       
   154 		TInt err = KErrNone;
       
   155 		while (err == KErrNone)
       
   156 			{
       
   157 			TRequestStatus status;
       
   158 			iComm.ReadOneOrMore(status, readBufPtr);
       
   159 			User::WaitForRequest(status);
       
   160 			err = status.Int();
       
   161 			if (err == KErrNone)
       
   162 				{
       
   163 				TPtrC writePtr((TUint16*)readBuf->Ptr(), readBuf->Length() / 2);
       
   164 				Write(writePtr);
       
   165 				}
       
   166 			else if (iVerbose)
       
   167 				{
       
   168 				PrintWarning(_L("RComm::ReadOneOrMore failed with %d"), err);
       
   169 				}
       
   170 			}
       
   171 		CleanupStack::PopAndDestroy(readBuf);
       
   172 		}
       
   173 	else
       
   174 		{
       
   175 		HBufC8* readBuf = HBufC8::NewLC(iBufSize);
       
   176 		TPtr8 readBufPtr(readBuf->Des());
       
   177 		HBufC* writeBuf = HBufC::NewLC(iBufSize / 2);
       
   178 		TPtr writeBufPtr(writeBuf->Des());
       
   179 		TInt err = KErrNone;
       
   180 		while (err == KErrNone)
       
   181 			{
       
   182 			TRequestStatus status;
       
   183 			iComm.ReadOneOrMore(status, readBufPtr);
       
   184 			User::WaitForRequest(status);
       
   185 			err = status.Int();
       
   186 			if (err == KErrNone)
       
   187 				{
       
   188 				writeBufPtr.Copy(*readBuf);
       
   189 				Write(*writeBuf);
       
   190 				}
       
   191 			else if (iVerbose)
       
   192 				{
       
   193 				PrintWarning(_L("RComm::ReadOneOrMore failed with %d"), err);
       
   194 				}
       
   195 			}
       
   196 		CleanupStack::PopAndDestroy(2, readBuf);
       
   197 		}
       
   198 	}
       
   199 
       
   200 void CCmdRcomm::WriteL()
       
   201 	{
       
   202 	RIoConsoleReadHandle& stdin = Stdin();
       
   203 	User::LeaveIfError(stdin.SetReadMode(RIoReadHandle::EOneOrMore));
       
   204 
       
   205 	if (iRaw)
       
   206 		{
       
   207 		HBufC* readBuf = HBufC::NewLC(iBufSize / 2);
       
   208 		TPtr readBufPtr(readBuf->Des());
       
   209 		TInt err = KErrNone;
       
   210 		while (err == KErrNone)
       
   211 			{
       
   212 			err = stdin.Read(readBufPtr);
       
   213 			if (err == KErrNone)
       
   214 				{
       
   215 				TPtrC8 writePtr((TUint8*)readBuf->Ptr(), readBuf->Size());
       
   216 				TRequestStatus status;
       
   217 				iComm.Write(status, writePtr);
       
   218 				User::WaitForRequest(status);
       
   219 				LeaveIfErr(status.Int(), _L("RComm::Write failed with %d"), status.Int());
       
   220 				}
       
   221 			}
       
   222 		CleanupStack::PopAndDestroy(readBuf);
       
   223 		}
       
   224 	else
       
   225 		{
       
   226 		HBufC* readBuf = HBufC::NewLC(iBufSize / 2);
       
   227 		TPtr readBufPtr(readBuf->Des());
       
   228 		HBufC8* writeBuf = HBufC8::NewLC(iBufSize);
       
   229 		TPtr8 writeBufPtr(writeBuf->Des());
       
   230 		TInt err = KErrNone;
       
   231 		while (err == KErrNone)
       
   232 			{
       
   233 			err = stdin.Read(readBufPtr);
       
   234 			if (err == KErrNone)
       
   235 				{
       
   236 				writeBufPtr.Copy(*readBuf);
       
   237 				TRequestStatus status;
       
   238 				iComm.Write(status, *writeBuf);
       
   239 				User::WaitForRequest(status);
       
   240 				LeaveIfErr(status.Int(), _L("RComm::Write failed with %d"), status.Int());
       
   241 				}
       
   242 			}
       
   243 		CleanupStack::PopAndDestroy(2, readBuf);
       
   244 		}
       
   245 	}
       
   246 
       
   247 void CCmdRcomm::ArgumentsL(RCommandArgumentList& aArguments)
       
   248 	{
       
   249 	_LIT(KArg1, "csy_name");
       
   250 	aArguments.AppendStringL(iCsyName, KArg1);
       
   251 
       
   252 	_LIT(KArg2, "port_name");
       
   253 	aArguments.AppendStringL(iPortName, KArg2);
       
   254 
       
   255 	_LIT(KArg3, "mode");
       
   256 	aArguments.AppendStringL(iMode, KArg3);
       
   257 	}
       
   258 
       
   259 void CCmdRcomm::OptionsL(RCommandOptionList& aOptions)
       
   260 	{
       
   261 	_LIT(KOption1, "access");
       
   262 	aOptions.AppendUintL((TUint&)iCommAccess, KOption1);
       
   263 
       
   264 	_LIT(KOption2, "buffer");
       
   265 	aOptions.AppendUintL(iBufSize, KOption2);
       
   266 
       
   267 	_LIT(KOption3, "raw");
       
   268 	aOptions.AppendBoolL(iRaw, KOption3);
       
   269 
       
   270 	_LIT(KOption4, "verbose");
       
   271 	aOptions.AppendBoolL(iVerbose, KOption4);
       
   272 	}
       
   273 
       
   274 
       
   275 EXE_BOILER_PLATE(CCmdRcomm)
       
   276