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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\misc\t_ymodemz.cpp
23 RTest test(_L("YModemZ"));
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)))
28 const TInt KBufferSize=4096;
30 _LIT(KLddName,"ECOMM");
31 _LIT(KPddName,"EUART");
37 void LoadCommDrivers()
39 test.Printf(_L("Load LDD\n"));
40 TInt r=User::LoadLogicalDevice(KLddName);
41 TEST(r==KErrNone || r==KErrAlreadyExists);
47 TBuf<16> pddName=KPddName();
49 pddName.Append('0'+i);
50 TInt r=User::LoadPhysicalDevice(pddName);
51 if (r==KErrNone || r==KErrAlreadyExists)
54 test.Printf(_L("%S found\n"),&pddName);
60 GLDEF_C void AcceptUnzippedBlock(TZipInfo& aInfo, TUint8*& aOutPtr, TInt aError)
64 TInt avail=aOutPtr-aInfo.iOutBuf;
65 if (avail>=KZipWindowSize+0x1000)
67 TInt len=avail-KZipWindowSize;
68 TPtrC8 ptr(aInfo.iOutBuf,len);
69 TInt r=TheOutputFile.Write(ptr);
71 Mem::Copy(aInfo.iOutBuf,aInfo.iOutBuf+len,KZipWindowSize);
72 aOutPtr=aInfo.iOutBuf+KZipWindowSize;
77 GLDEF_C TInt ReadInputData(TUint8* aDest, TInt& aLength)
80 // test.Printf(_L("@%dms\n"),User::NTickCount());
81 TInt r=TheYModem->ReadPackets(pD,aLength);
82 // test.Printf(_L("ReadIP %d\n"),r);
87 GLDEF_C TInt UnzipComplete(TZipInfo& a, TUint8* aOutPtr, TInt aError)
90 if (r==KErrNone && aOutPtr>a.iOutBuf)
91 r=TheOutputFile.Write(TPtrC8(a.iOutBuf,aOutPtr-a.iOutBuf));
96 _LIT(KLitThreadName,"Unzip");
97 TInt Initialise(TZipInfo& a)
102 a.iFileBufSize=4*a.iInBufSize;
103 TAny* pFileBuf=MALLOC(a.iFileBufSize);
106 a.iFileBuf=(TUint8*)pFileBuf;
108 r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a);
115 t.SetPriority(EPriorityLess);
116 t.Logon(a.iThreadStatus);
118 a.iThreadHandle=t.Handle();
122 void ProcessHeader(TZipInfo& a)
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);
132 TInt r=TheOutputFile.Replace(TheFs,a.iName,EFileWrite);
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"));
139 TRequestStatus* pS=&a.iProcessedHeader;
141 t.SetHandle(a.iThreadHandle);
142 t.RequestComplete(pS,0);
145 void Cleanup(TZipInfo& a)
151 RThread& t=*(RThread*)&a.iThreadHandle;
155 GLDEF_C TInt E32Main()
157 // RThread().SetSystem(ETrue);
158 RThread().SetPriority(EPriorityAbsoluteForeground);
159 test.SetLogged(EFalse);
163 User::CommandLine(cmd);
167 TUint8 c=(TUint8)cmd[0];
168 if (c>='0' && c<='9')
177 test.Printf(_L("Connect to file server\n"));
183 test.Printf(_L("Create YModem object\n"));
185 TRAP(r,pY=YModemU::NewL(port,ETrue));
186 TEST(r==KErrNone && pY!=NULL);
189 test.Printf(_L("Create buffer\n"));
190 TUint8* buffer=(TUint8*)User::Alloc(KBufferSize);
193 test.Printf(_L("Receive...\n"));
201 r=pY->StartDownload(mode, size, name);
202 // test.Printf(_L("@%dms"),User::NTickCount());
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)
208 test.Printf(_L("Initialising unzip...\n"));
213 test.Printf(_L("Read header\n"));
216 t.SetHandle(z.iThreadHandle);
217 while (z.iRemain && z.iThreadStatus==KRequestPending)
219 TRequestStatus dummy;
220 TRequestStatus* pS=&dummy;
221 // test.Printf(_L("remain=%d\n"),z.iRemain);
222 r=ReadBlockToBuffer(z);
224 t.RequestComplete(pS,0); // same process
225 while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
227 if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
229 // after reading first block, process the header
231 c=User::NTickCount();
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)
240 TBuf<32> exitCat=t.ExitCategory();
241 test.Printf(_L("Exit code %d,%d,%S\n"),exitType,exitReason,&exitCat); test.Getch(); test(0);
244 r=pY->ReadPackets(pD,KBufferSize); // should get EOF response
247 TheOutputFile.Close();
249 else if (r==KErrNone)
251 test.Printf(_L("Opening file for write\n"));
253 r=file.Replace(TheFs,name,EFileWrite);
256 test.Printf(_L("RFile::Replace returns %d\n"),r); test.Getch(); test(0);
261 r=pY->ReadPackets(pD,KBufferSize);
262 if (r==KErrNone || r==KErrEof)
265 if (size>0) // size was transmitted
267 if (blen>size-total_size)
268 blen=size-total_size;
271 TPtrC8 fptr(buffer,blen);
272 TInt s=file.Write(fptr);
275 test.Printf(_L("RFile::Write returns %d\n"),s); test.Getch(); test(0);
280 test.Printf(_L("rx size=%d\n"),total_size);
286 test.Printf(_L("r=%d\n"),r);