1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/misc/t_ymodemz.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,291 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\misc\t_ymodemz.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +#include <e32test.h>
1.22 +#include "ymodemu.h"
1.23 +#include <f32file.h>
1.24 +#include "unzip.h"
1.25 +
1.26 +RTest test(_L("YModemZ"));
1.27 +
1.28 +#define TEST(c) ((void)((c)||(test.Printf(_L("Failed at line %d\n"),__LINE__),test.Getch(),test(0),0)))
1.29 +#define CHECK(c) ((void)(((c)==0)||(test.Printf(_L("Error %d at line %d\n"),(c),__LINE__),test.Getch(),test(0),0)))
1.30 +
1.31 +const TInt KBufferSize=4096;
1.32 +
1.33 +_LIT(KLddName,"ECOMM");
1.34 +_LIT(KPddName,"EUART");
1.35 +
1.36 +RFs TheFs;
1.37 +RFile TheOutputFile;
1.38 +YModemU* TheYModem;
1.39 +
1.40 +void LoadCommDrivers()
1.41 + {
1.42 + test.Printf(_L("Load LDD\n"));
1.43 + TInt r=User::LoadLogicalDevice(KLddName);
1.44 + TEST(r==KErrNone || r==KErrAlreadyExists);
1.45 +
1.46 + TInt i;
1.47 + TInt n=0;
1.48 + for (i=-1; i<10; ++i)
1.49 + {
1.50 + TBuf<16> pddName=KPddName();
1.51 + if (i>=0)
1.52 + pddName.Append('0'+i);
1.53 + TInt r=User::LoadPhysicalDevice(pddName);
1.54 + if (r==KErrNone || r==KErrAlreadyExists)
1.55 + {
1.56 + ++n;
1.57 + test.Printf(_L("%S found\n"),&pddName);
1.58 + }
1.59 + }
1.60 + TEST(n!=0);
1.61 + }
1.62 +
1.63 +GLDEF_C void AcceptUnzippedBlock(TZipInfo& aInfo, TUint8*& aOutPtr, TInt aError)
1.64 + {
1.65 + if (aError==KErrNone)
1.66 + {
1.67 + TInt avail=aOutPtr-aInfo.iOutBuf;
1.68 + if (avail>=KZipWindowSize+0x1000)
1.69 + {
1.70 + TInt len=avail-KZipWindowSize;
1.71 + TPtrC8 ptr(aInfo.iOutBuf,len);
1.72 + TInt r=TheOutputFile.Write(ptr);
1.73 + CHECK(r);
1.74 + Mem::Copy(aInfo.iOutBuf,aInfo.iOutBuf+len,KZipWindowSize);
1.75 + aOutPtr=aInfo.iOutBuf+KZipWindowSize;
1.76 + }
1.77 + }
1.78 + }
1.79 +
1.80 +GLDEF_C TInt ReadInputData(TUint8* aDest, TInt& aLength)
1.81 + {
1.82 + TUint8* pD=aDest;
1.83 +// test.Printf(_L("@%dms\n"),User::NTickCount());
1.84 + TInt r=TheYModem->ReadPackets(pD,aLength);
1.85 +// test.Printf(_L("ReadIP %d\n"),r);
1.86 + aLength=pD-aDest;
1.87 + return r;
1.88 + }
1.89 +
1.90 +GLDEF_C TInt UnzipComplete(TZipInfo& a, TUint8* aOutPtr, TInt aError)
1.91 + {
1.92 + TInt r=aError;
1.93 + if (r==KErrNone && aOutPtr>a.iOutBuf)
1.94 + r=TheOutputFile.Write(TPtrC8(a.iOutBuf,aOutPtr-a.iOutBuf));
1.95 + CHECK(r);
1.96 + return r;
1.97 + }
1.98 +
1.99 +_LIT(KLitThreadName,"Unzip");
1.100 +TInt Initialise(TZipInfo& a)
1.101 + {
1.102 + TInt r=InitInfo(a);
1.103 + if (r!=KErrNone)
1.104 + return r;
1.105 + a.iFileBufSize=4*a.iInBufSize;
1.106 + TAny* pFileBuf=MALLOC(a.iFileBufSize);
1.107 + if (!pFileBuf)
1.108 + return KErrNoMemory;
1.109 + a.iFileBuf=(TUint8*)pFileBuf;
1.110 + RThread t;
1.111 + r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a);
1.112 + if (r!=KErrNone)
1.113 + {
1.114 + FREE(pFileBuf);
1.115 + a.iFileBuf=NULL;
1.116 + return r;
1.117 + }
1.118 + t.SetPriority(EPriorityLess);
1.119 + t.Logon(a.iThreadStatus);
1.120 + t.Resume();
1.121 + a.iThreadHandle=t.Handle();
1.122 + return KErrNone;
1.123 + }
1.124 +
1.125 +void ProcessHeader(TZipInfo& a)
1.126 + {
1.127 + test.Printf(_L("Flags=%d\n"),a.iFlags);
1.128 + test.Printf(_L("Method=%d\n"),a.iMethod);
1.129 + test.Printf(_L("Crc=%d\n"),a.iCrc);
1.130 + test.Printf(_L("Compressed size=%d\n"),a.iCompressedSize);
1.131 + test.Printf(_L("Uncompressed size=%d\n"),a.iUncompressedSize);
1.132 + test.Printf(_L("File name %S\n"),&a.iName);
1.133 + test.Printf(_L("Data offset %d\n\n"),a.iDataOffset);
1.134 +
1.135 + TInt r=TheOutputFile.Replace(TheFs,a.iName,EFileWrite);
1.136 + CHECK(r);
1.137 + test.Printf(_L("Allocate memory for unzipped file\n"));
1.138 + a.iOutBuf=(TUint8*)User::Alloc(262144);
1.139 + TEST(a.iOutBuf!=NULL);
1.140 + test.Printf(_L("Begin unzipping\n"));
1.141 + a.iHeaderDone=2;
1.142 + TRequestStatus* pS=&a.iProcessedHeader;
1.143 + RThread t;
1.144 + t.SetHandle(a.iThreadHandle);
1.145 + t.RequestComplete(pS,0);
1.146 + }
1.147 +
1.148 +void Cleanup(TZipInfo& a)
1.149 + {
1.150 + delete a.iFileBuf;
1.151 + a.iFileBuf=NULL;
1.152 + delete a.iOutBuf;
1.153 + a.iOutBuf=NULL;
1.154 + RThread& t=*(RThread*)&a.iThreadHandle;
1.155 + t.Close();
1.156 + }
1.157 +
1.158 +GLDEF_C TInt E32Main()
1.159 + {
1.160 +// RThread().SetSystem(ETrue);
1.161 + RThread().SetPriority(EPriorityAbsoluteForeground);
1.162 + test.SetLogged(EFalse);
1.163 + test.Title();
1.164 +
1.165 + TBuf<256> cmd;
1.166 + User::CommandLine(cmd);
1.167 + TInt port=0;
1.168 + if (cmd.Length()!=0)
1.169 + {
1.170 + TUint8 c=(TUint8)cmd[0];
1.171 + if (c>='0' && c<='9')
1.172 + {
1.173 + port=c-'0';
1.174 + }
1.175 + }
1.176 +
1.177 + TInt r=KErrNone;
1.178 + LoadCommDrivers();
1.179 +
1.180 + test.Printf(_L("Connect to file server\n"));
1.181 + r=TheFs.Connect();
1.182 + CHECK(r);
1.183 + r=TheFs.ShareAuto();
1.184 + CHECK(r);
1.185 +
1.186 + test.Printf(_L("Create YModem object\n"));
1.187 + YModemU* pY=NULL;
1.188 + TRAP(r,pY=YModemU::NewL(port,ETrue));
1.189 + TEST(r==KErrNone && pY!=NULL);
1.190 + TheYModem=pY;
1.191 +
1.192 + test.Printf(_L("Create buffer\n"));
1.193 + TUint8* buffer=(TUint8*)User::Alloc(KBufferSize);
1.194 + TEST(buffer!=NULL);
1.195 +
1.196 + test.Printf(_L("Receive...\n"));
1.197 +
1.198 + TBool mode=1;
1.199 + FOREVER
1.200 + {
1.201 + TInt total_size=0;
1.202 + TInt size=-1;
1.203 + TBuf<256> name;
1.204 + r=pY->StartDownload(mode, size, name);
1.205 +// test.Printf(_L("@%dms"),User::NTickCount());
1.206 + if (r!=KErrNone)
1.207 + break;
1.208 + test.Printf(_L("r=%d, size=%d, name %S\n"),r,size,&name);
1.209 + if (r==KErrNone && name.Right(4).CompareF(_L(".zip"))==0 && size!=0)
1.210 + {
1.211 + test.Printf(_L("Initialising unzip...\n"));
1.212 + TZipInfo z;
1.213 + z.iRemain=size;
1.214 + r=Initialise(z);
1.215 + CHECK(r);
1.216 + test.Printf(_L("Read header\n"));
1.217 + TUint32 c=0;
1.218 + RThread t;
1.219 + t.SetHandle(z.iThreadHandle);
1.220 + while (z.iRemain && z.iThreadStatus==KRequestPending)
1.221 + {
1.222 + TRequestStatus dummy;
1.223 + TRequestStatus* pS=&dummy;
1.224 +// test.Printf(_L("remain=%d\n"),z.iRemain);
1.225 + r=ReadBlockToBuffer(z);
1.226 + CHECK(r);
1.227 + t.RequestComplete(pS,0); // same process
1.228 + while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
1.229 + DELAY(20000);
1.230 + if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
1.231 + {
1.232 + // after reading first block, process the header
1.233 + ProcessHeader(z);
1.234 + c=User::NTickCount();
1.235 + }
1.236 + }
1.237 + test.Printf(_L("\nWait for thread to exit\n"));
1.238 + User::WaitForRequest(z.iThreadStatus);
1.239 + TInt exitType=t.ExitType();
1.240 + TInt exitReason=t.ExitReason();
1.241 + if (z.iRemain || exitType!=EExitKill || exitReason!=KErrNone)
1.242 + {
1.243 + TBuf<32> exitCat=t.ExitCategory();
1.244 + test.Printf(_L("Exit code %d,%d,%S\n"),exitType,exitReason,&exitCat); test.Getch(); test(0);
1.245 + }
1.246 + TUint8* pD=buffer;
1.247 + r=pY->ReadPackets(pD,KBufferSize); // should get EOF response
1.248 + TEST(r==KErrEof);
1.249 + Cleanup(z);
1.250 + TheOutputFile.Close();
1.251 + }
1.252 + else if (r==KErrNone)
1.253 + {
1.254 + test.Printf(_L("Opening file for write\n"));
1.255 + RFile file;
1.256 + r=file.Replace(TheFs,name,EFileWrite);
1.257 + if (r!=KErrNone)
1.258 + {
1.259 + test.Printf(_L("RFile::Replace returns %d\n"),r); test.Getch(); test(0);
1.260 + }
1.261 + while (r==KErrNone)
1.262 + {
1.263 + TUint8* pD=buffer;
1.264 + r=pY->ReadPackets(pD,KBufferSize);
1.265 + if (r==KErrNone || r==KErrEof)
1.266 + {
1.267 + TInt blen=pD-buffer;
1.268 + if (size>0) // size was transmitted
1.269 + {
1.270 + if (blen>size-total_size)
1.271 + blen=size-total_size;
1.272 + }
1.273 + total_size+=blen;
1.274 + TPtrC8 fptr(buffer,blen);
1.275 + TInt s=file.Write(fptr);
1.276 + if (s!=KErrNone)
1.277 + {
1.278 + test.Printf(_L("RFile::Write returns %d\n"),s); test.Getch(); test(0);
1.279 + }
1.280 + }
1.281 + }
1.282 + file.Close();
1.283 + test.Printf(_L("rx size=%d\n"),total_size);
1.284 + }
1.285 + }
1.286 + delete buffer;
1.287 + delete pY;
1.288 + TheFs.Close();
1.289 + test.Printf(_L("r=%d\n"),r);
1.290 + test.Getch();
1.291 +
1.292 + return KErrNone;
1.293 + }
1.294 +