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