os/kernelhwsrv/kerneltest/e32test/misc/t_ymodemz.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-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 the License "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 // e32test\misc\t_ymodemz.cpp
    15 // 
    16 //
    17 
    18 #include <e32test.h>
    19 #include "ymodemu.h"
    20 #include <f32file.h>
    21 #include "unzip.h"
    22 
    23 RTest test(_L("YModemZ"));
    24 
    25 #define TEST(c)		((void)((c)||(test.Printf(_L("Failed at line %d\n"),__LINE__),test.Getch(),test(0),0)))
    26 #define CHECK(c)	((void)(((c)==0)||(test.Printf(_L("Error %d at line %d\n"),(c),__LINE__),test.Getch(),test(0),0)))
    27 
    28 const TInt KBufferSize=4096;
    29 
    30 _LIT(KLddName,"ECOMM");
    31 _LIT(KPddName,"EUART");
    32 
    33 RFs TheFs;
    34 RFile TheOutputFile;
    35 YModemU* TheYModem;
    36 
    37 void LoadCommDrivers()
    38 	{
    39 	test.Printf(_L("Load LDD\n"));
    40 	TInt r=User::LoadLogicalDevice(KLddName);
    41 	TEST(r==KErrNone || r==KErrAlreadyExists);
    42 
    43 	TInt i;
    44 	TInt n=0;
    45 	for (i=-1; i<10; ++i)
    46 		{
    47 		TBuf<16> pddName=KPddName();
    48 		if (i>=0)
    49 			pddName.Append('0'+i);
    50 		TInt r=User::LoadPhysicalDevice(pddName);
    51 		if (r==KErrNone || r==KErrAlreadyExists)
    52 			{
    53 			++n;
    54 			test.Printf(_L("%S found\n"),&pddName);
    55 			}
    56 		}
    57 	TEST(n!=0);
    58 	}
    59 
    60 GLDEF_C void AcceptUnzippedBlock(TZipInfo& aInfo, TUint8*& aOutPtr, TInt aError)
    61 	{
    62 	if (aError==KErrNone)
    63 		{
    64 		TInt avail=aOutPtr-aInfo.iOutBuf;
    65 		if (avail>=KZipWindowSize+0x1000)
    66 			{
    67 			TInt len=avail-KZipWindowSize;
    68 			TPtrC8 ptr(aInfo.iOutBuf,len);
    69 			TInt r=TheOutputFile.Write(ptr);
    70 			CHECK(r);
    71 			Mem::Copy(aInfo.iOutBuf,aInfo.iOutBuf+len,KZipWindowSize);
    72 			aOutPtr=aInfo.iOutBuf+KZipWindowSize;
    73 			}
    74 		}
    75 	}
    76 
    77 GLDEF_C TInt ReadInputData(TUint8* aDest, TInt& aLength)
    78 	{
    79 	TUint8* pD=aDest;
    80 //	test.Printf(_L("@%dms\n"),User::NTickCount());
    81 	TInt r=TheYModem->ReadPackets(pD,aLength);
    82 //	test.Printf(_L("ReadIP %d\n"),r);
    83 	aLength=pD-aDest;
    84 	return r;
    85 	}
    86 
    87 GLDEF_C TInt UnzipComplete(TZipInfo& a, TUint8* aOutPtr, TInt aError)
    88 	{
    89 	TInt r=aError;
    90 	if (r==KErrNone && aOutPtr>a.iOutBuf)
    91 		r=TheOutputFile.Write(TPtrC8(a.iOutBuf,aOutPtr-a.iOutBuf));
    92 	CHECK(r);
    93 	return r;
    94 	}
    95 
    96 _LIT(KLitThreadName,"Unzip");
    97 TInt Initialise(TZipInfo& a)
    98 	{
    99 	TInt r=InitInfo(a);
   100 	if (r!=KErrNone)
   101 		return r;
   102 	a.iFileBufSize=4*a.iInBufSize;
   103 	TAny* pFileBuf=MALLOC(a.iFileBufSize);
   104 	if (!pFileBuf)
   105 		return KErrNoMemory;
   106 	a.iFileBuf=(TUint8*)pFileBuf;
   107 	RThread t;
   108 	r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a);
   109 	if (r!=KErrNone)
   110 		{
   111 		FREE(pFileBuf);
   112 		a.iFileBuf=NULL;
   113 		return r;
   114 		}
   115 	t.SetPriority(EPriorityLess);
   116 	t.Logon(a.iThreadStatus);
   117 	t.Resume();
   118 	a.iThreadHandle=t.Handle();
   119 	return KErrNone;
   120 	}
   121 
   122 void ProcessHeader(TZipInfo& a)
   123 	{
   124 	test.Printf(_L("Flags=%d\n"),a.iFlags);
   125 	test.Printf(_L("Method=%d\n"),a.iMethod);
   126 	test.Printf(_L("Crc=%d\n"),a.iCrc);
   127 	test.Printf(_L("Compressed size=%d\n"),a.iCompressedSize);
   128 	test.Printf(_L("Uncompressed size=%d\n"),a.iUncompressedSize);
   129 	test.Printf(_L("File name %S\n"),&a.iName);
   130 	test.Printf(_L("Data offset %d\n\n"),a.iDataOffset);
   131 
   132 	TInt r=TheOutputFile.Replace(TheFs,a.iName,EFileWrite);
   133 	CHECK(r);
   134 	test.Printf(_L("Allocate memory for unzipped file\n"));
   135 	a.iOutBuf=(TUint8*)User::Alloc(262144);
   136 	TEST(a.iOutBuf!=NULL);
   137 	test.Printf(_L("Begin unzipping\n"));
   138 	a.iHeaderDone=2;
   139 	TRequestStatus* pS=&a.iProcessedHeader;
   140 	RThread t;
   141 	t.SetHandle(a.iThreadHandle);
   142 	t.RequestComplete(pS,0);
   143 	}
   144 
   145 void Cleanup(TZipInfo& a)
   146 	{
   147 	delete a.iFileBuf;
   148 	a.iFileBuf=NULL;
   149 	delete a.iOutBuf;
   150 	a.iOutBuf=NULL;
   151 	RThread& t=*(RThread*)&a.iThreadHandle;
   152 	t.Close();
   153 	}
   154 
   155 GLDEF_C TInt E32Main()
   156 	{
   157 //	RThread().SetSystem(ETrue);
   158 	RThread().SetPriority(EPriorityAbsoluteForeground);
   159 	test.SetLogged(EFalse);
   160 	test.Title();
   161 
   162 	TBuf<256> cmd;
   163 	User::CommandLine(cmd);
   164 	TInt port=0;
   165 	if (cmd.Length()!=0)
   166 		{
   167 		TUint8 c=(TUint8)cmd[0];
   168 		if (c>='0' && c<='9')
   169 			{
   170 			port=c-'0';
   171 			}
   172 		}
   173 
   174 	TInt r=KErrNone;
   175 	LoadCommDrivers();
   176 
   177 	test.Printf(_L("Connect to file server\n"));
   178 	r=TheFs.Connect();
   179 	CHECK(r);
   180 	r=TheFs.ShareAuto();
   181 	CHECK(r);
   182 
   183 	test.Printf(_L("Create YModem object\n"));
   184 	YModemU* pY=NULL;
   185 	TRAP(r,pY=YModemU::NewL(port,ETrue));
   186 	TEST(r==KErrNone && pY!=NULL);
   187 	TheYModem=pY;
   188 
   189 	test.Printf(_L("Create buffer\n"));
   190 	TUint8* buffer=(TUint8*)User::Alloc(KBufferSize);
   191 	TEST(buffer!=NULL);
   192 
   193 	test.Printf(_L("Receive...\n"));
   194 
   195 	TBool mode=1;
   196 	FOREVER
   197 		{
   198 		TInt total_size=0;
   199 		TInt size=-1;
   200 		TBuf<256> name;
   201 		r=pY->StartDownload(mode, size, name);
   202 //		test.Printf(_L("@%dms"),User::NTickCount());
   203 		if (r!=KErrNone)
   204 			break;
   205 		test.Printf(_L("r=%d, size=%d, name %S\n"),r,size,&name);
   206 		if (r==KErrNone && name.Right(4).CompareF(_L(".zip"))==0 && size!=0)
   207 			{
   208 			test.Printf(_L("Initialising unzip...\n"));
   209 			TZipInfo z;
   210 			z.iRemain=size;
   211 			r=Initialise(z);
   212 			CHECK(r);
   213 			test.Printf(_L("Read header\n"));
   214 			TUint32 c=0;
   215 			RThread t;
   216 			t.SetHandle(z.iThreadHandle);
   217 			while (z.iRemain && z.iThreadStatus==KRequestPending)
   218 				{
   219 				TRequestStatus dummy;
   220 				TRequestStatus* pS=&dummy;
   221 //				test.Printf(_L("remain=%d\n"),z.iRemain);
   222 				r=ReadBlockToBuffer(z);
   223 				CHECK(r);
   224 				t.RequestComplete(pS,0);		// same process
   225 				while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
   226 					DELAY(20000);
   227 				if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
   228 					{
   229 					// after reading first block, process the header
   230 					ProcessHeader(z);
   231 					c=User::NTickCount();
   232 					}
   233 				}
   234 			test.Printf(_L("\nWait for thread to exit\n"));
   235 			User::WaitForRequest(z.iThreadStatus);
   236 			TInt exitType=t.ExitType();
   237 			TInt exitReason=t.ExitReason();
   238 			if (z.iRemain || exitType!=EExitKill || exitReason!=KErrNone)
   239 				{
   240 				TBuf<32> exitCat=t.ExitCategory();
   241 				test.Printf(_L("Exit code %d,%d,%S\n"),exitType,exitReason,&exitCat); test.Getch(); test(0);
   242 				}
   243 			TUint8* pD=buffer;
   244 			r=pY->ReadPackets(pD,KBufferSize);	// should get EOF response
   245 			TEST(r==KErrEof);
   246 			Cleanup(z);
   247 			TheOutputFile.Close();
   248 			}
   249 		else if (r==KErrNone)
   250 			{
   251 			test.Printf(_L("Opening file for write\n"));
   252 			RFile file;
   253 			r=file.Replace(TheFs,name,EFileWrite);
   254 			if (r!=KErrNone)
   255 				{
   256 				test.Printf(_L("RFile::Replace returns %d\n"),r); test.Getch();	test(0);
   257 				}
   258 			while (r==KErrNone)
   259 				{
   260 				TUint8* pD=buffer;
   261 				r=pY->ReadPackets(pD,KBufferSize);
   262 				if (r==KErrNone || r==KErrEof)
   263 					{
   264 					TInt blen=pD-buffer;
   265 					if (size>0)				// size was transmitted
   266 						{
   267 						if (blen>size-total_size)
   268 							blen=size-total_size;
   269 						}
   270 					total_size+=blen;
   271 					TPtrC8 fptr(buffer,blen);
   272 					TInt s=file.Write(fptr);
   273 					if (s!=KErrNone)
   274 						{
   275 						test.Printf(_L("RFile::Write returns %d\n"),s); test.Getch(); test(0);
   276 						}
   277 					}
   278 				}
   279 			file.Close();
   280 			test.Printf(_L("rx size=%d\n"),total_size);
   281 			}
   282 		}
   283 	delete buffer;
   284 	delete pY;
   285 	TheFs.Close();
   286 	test.Printf(_L("r=%d\n"),r);
   287 	test.Getch();
   288 
   289 	return KErrNone;
   290 	}
   291