kerneltest/e32test/misc/t_ymodem.cpp
changeset 9 96e5fb8b040d
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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_ymodem.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include "ymodemu.h"
       
    20 #include <f32file.h>
       
    21 
       
    22 RTest test(_L("YModem"));
       
    23 
       
    24 #define TEST(c)	((void)((c)||(test.Printf(_L("Failed at line %d\n"),__LINE__),test.Getch(),test(0),0)))
       
    25 
       
    26 const TInt KBufferSize=4096;
       
    27 
       
    28 _LIT(KLddName,"ECOMM");
       
    29 _LIT(KPddName,"EUART");
       
    30 
       
    31 void LoadCommDrivers()
       
    32 	{
       
    33 	test.Printf(_L("Load LDD\n"));
       
    34 	TInt r=User::LoadLogicalDevice(KLddName);
       
    35 	TEST(r==KErrNone || r==KErrAlreadyExists);
       
    36 
       
    37 	TInt i;
       
    38 	TInt n=0;
       
    39 	for (i=-1; i<10; ++i)
       
    40 		{
       
    41 		TBuf<16> pddName=KPddName();
       
    42 		if (i>=0)
       
    43 			pddName.Append('0'+i);
       
    44 		TInt r=User::LoadPhysicalDevice(pddName);
       
    45 		if (r==KErrNone || r==KErrAlreadyExists)
       
    46 			{
       
    47 			++n;
       
    48 			test.Printf(_L("%S found\n"),&pddName);
       
    49 			}
       
    50 		}
       
    51 	TEST(n!=0);
       
    52 	}
       
    53 
       
    54 GLDEF_C TInt E32Main()
       
    55 	{
       
    56 	RThread().SetPriority(EPriorityAbsoluteForeground);
       
    57 	test.SetLogged(EFalse);
       
    58 	test.Title();
       
    59 
       
    60 	TBuf<256> cmd;
       
    61 	User::CommandLine(cmd);
       
    62 	TInt port=0;
       
    63 	if (cmd.Length()!=0)
       
    64 		{
       
    65 		TUint8 c=(TUint8)cmd[0];
       
    66 		if (c>='0' && c<='9')
       
    67 			{
       
    68 			port=c-'0';
       
    69 			}
       
    70 		}
       
    71 
       
    72 	TInt r=KErrNone;
       
    73 	LoadCommDrivers();
       
    74 
       
    75 	test.Next(_L("Connect to file server"));
       
    76 	RFs fs;
       
    77 	r=fs.Connect();
       
    78 	test(r==KErrNone);
       
    79 
       
    80 	test.Next(_L("Create YModem object"));
       
    81 	YModemU* pY=NULL;
       
    82 	TRAP(r,pY=YModemU::NewL(port,ETrue));
       
    83 	test(r==KErrNone && pY!=NULL);
       
    84 
       
    85 	test.Next(_L("Create buffer"));
       
    86 	TUint8* buffer=(TUint8*)User::Alloc(KBufferSize);
       
    87 	test(buffer!=NULL);
       
    88 
       
    89 	test.Start(_L("Receive..."));
       
    90 
       
    91 	TBool mode=1;
       
    92 	FOREVER
       
    93 		{
       
    94 		TInt total_size=0;
       
    95 		TInt size=-1;
       
    96 		TBuf<256> name;
       
    97 		r=pY->StartDownload(mode, size, name);
       
    98 		if (r!=KErrNone)
       
    99 			break;
       
   100 		test.Printf(_L("r=%d, size=%d, name %S\n"),r,size,&name);
       
   101 		if (r==KErrNone)
       
   102 			{
       
   103 			test.Printf(_L("Opening file for write\n"));
       
   104 			RFile file;
       
   105 			r=file.Replace(fs,name,EFileWrite);
       
   106 			if (r!=KErrNone)
       
   107 				{
       
   108 				test.Printf(_L("RFile::Replace returns %d\n"),r);
       
   109 				test.Getch();
       
   110 				test(0);
       
   111 				}
       
   112 			while (r==KErrNone)
       
   113 				{
       
   114 				TUint8* pD=buffer;
       
   115 				r=pY->ReadPackets(pD,KBufferSize);
       
   116 				if (r==KErrNone || r==KErrEof)
       
   117 					{
       
   118 					TInt blen=pD-buffer;
       
   119 					if (size>0)				// size was transmitted
       
   120 						{
       
   121 						if (blen>size-total_size)
       
   122 							blen=size-total_size;
       
   123 						}
       
   124 					total_size+=blen;
       
   125 					TPtrC8 fptr(buffer,blen);
       
   126 					TInt s=file.Write(fptr);
       
   127 					if (s!=KErrNone)
       
   128 						{
       
   129 						test.Printf(_L("RFile::Write returns %d\n"),s);
       
   130 						test.Getch();
       
   131 						test(0);
       
   132 						}
       
   133 					}
       
   134 				}
       
   135 			file.Close();
       
   136 			test.Printf(_L("rx size=%d\n"),total_size);
       
   137 			}
       
   138 		}
       
   139 	delete buffer;
       
   140 	delete pY;
       
   141 	fs.Close();
       
   142 	test.Printf(_L("r=%d\n"),r);
       
   143 	test.Getch();
       
   144 
       
   145 	return KErrNone;
       
   146 	}