0
|
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 |
RThread t;
|
|
215 |
t.SetHandle(z.iThreadHandle);
|
|
216 |
while (z.iRemain && z.iThreadStatus==KRequestPending)
|
|
217 |
{
|
|
218 |
TRequestStatus dummy;
|
|
219 |
TRequestStatus* pS=&dummy;
|
|
220 |
// test.Printf(_L("remain=%d\n"),z.iRemain);
|
|
221 |
r=ReadBlockToBuffer(z);
|
|
222 |
CHECK(r);
|
|
223 |
t.RequestComplete(pS,0); // same process
|
|
224 |
while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
|
|
225 |
DELAY(20000);
|
|
226 |
if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
|
|
227 |
{
|
|
228 |
// after reading first block, process the header
|
|
229 |
ProcessHeader(z);
|
|
230 |
}
|
|
231 |
}
|
|
232 |
test.Printf(_L("\nWait for thread to exit\n"));
|
|
233 |
User::WaitForRequest(z.iThreadStatus);
|
|
234 |
TInt exitType=t.ExitType();
|
|
235 |
TInt exitReason=t.ExitReason();
|
|
236 |
if (z.iRemain || exitType!=EExitKill || exitReason!=KErrNone)
|
|
237 |
{
|
|
238 |
TBuf<32> exitCat=t.ExitCategory();
|
|
239 |
test.Printf(_L("Exit code %d,%d,%S\n"),exitType,exitReason,&exitCat); test.Getch(); test(0);
|
|
240 |
}
|
|
241 |
TUint8* pD=buffer;
|
|
242 |
r=pY->ReadPackets(pD,KBufferSize); // should get EOF response
|
|
243 |
TEST(r==KErrEof);
|
|
244 |
Cleanup(z);
|
|
245 |
TheOutputFile.Close();
|
|
246 |
}
|
|
247 |
else if (r==KErrNone)
|
|
248 |
{
|
|
249 |
test.Printf(_L("Opening file for write\n"));
|
|
250 |
RFile file;
|
|
251 |
r=file.Replace(TheFs,name,EFileWrite);
|
|
252 |
if (r!=KErrNone)
|
|
253 |
{
|
|
254 |
test.Printf(_L("RFile::Replace returns %d\n"),r); test.Getch(); test(0);
|
|
255 |
}
|
|
256 |
while (r==KErrNone)
|
|
257 |
{
|
|
258 |
TUint8* pD=buffer;
|
|
259 |
r=pY->ReadPackets(pD,KBufferSize);
|
|
260 |
if (r==KErrNone || r==KErrEof)
|
|
261 |
{
|
|
262 |
TInt blen=pD-buffer;
|
|
263 |
if (size>0) // size was transmitted
|
|
264 |
{
|
|
265 |
if (blen>size-total_size)
|
|
266 |
blen=size-total_size;
|
|
267 |
}
|
|
268 |
total_size+=blen;
|
|
269 |
TPtrC8 fptr(buffer,blen);
|
|
270 |
TInt s=file.Write(fptr);
|
|
271 |
if (s!=KErrNone)
|
|
272 |
{
|
|
273 |
test.Printf(_L("RFile::Write returns %d\n"),s); test.Getch(); test(0);
|
|
274 |
}
|
|
275 |
}
|
|
276 |
}
|
|
277 |
file.Close();
|
|
278 |
test.Printf(_L("rx size=%d\n"),total_size);
|
|
279 |
}
|
|
280 |
}
|
|
281 |
delete buffer;
|
|
282 |
delete pY;
|
|
283 |
TheFs.Close();
|
|
284 |
test.Printf(_L("r=%d\n"),r);
|
|
285 |
test.Getch();
|
|
286 |
|
|
287 |
return KErrNone;
|
|
288 |
}
|
|
289 |
|